VOGONS


First post, by videogamer555

User metadata
Rank Member
Rank
Member

When I do Ctrl+F5 to save a screenshot, it saves a PNG in indexed palette mode, with a copy of the VGA palette being the palette for the PNG file. However the values for indexes 16 to 31 (the grayscale entries part of the palette) seem to be incorrect. For example at index 17, it should be the darkest gray in that section of the palette with RGB values of 17,17,17. However instead the RGB values are 20,20,20
Likewise at index 18, it should have RGB values of 34,34,34. However instead the RGB values are 32,32,32.

To scale 16 values (range of values being 0 to 15) into 256 values (range of values being 0 to 255) all you need to do is multiply by 17, as the number 15 goes into 255 exactly 17 times. And since the indexes for the grayscale part of the palette start with index 16, all you need to do is first subtract by 16.

The formula for generating the RGB values for that set of grayscale colors for the palette should be: (index-16)*17
So with index=16 you have (16-16)*17 = 0 (RGB values are therefore 0,0,0 here)
With index=17 you have (17-16)*17 = 17 (RGB values are therefore 17,17,17 here)
With index=18 you have (18-16)*17 = 34 (RGB values are therefore 34,34,34 here)
But those are not the values I'm getting. Instead I'm getting the incorrect value of RGB=20,20,20 at index 17. And I'm getting the incorrect value of RGB=32,32,32 at index=18. Likewise all the remaining RGB values for the grayscale colors up to index 30 are incorrect. And then it's correct again at index 31 with the RGB values being 255,255,255. So only the RGB values for index 16 and index 31 are correct in this grayscale part of the palette.

Fortunately the EGA part of the 256 color palette (the first 16 indexes from 0 to 15 within the overall 256 color VGA palette) do have correct RGB values. And I have no idea how the pastel and dark pastel colors part of the palette are intended to be calculated, so I haven't checked them for accuracy yet.

It does seem though that the grayscale part of the VGA palette have been incorrectly calculated. I even tried various things like first scaling it into 64 values (I know that in 256 color mode only 64 levels of brightness are possible with each channel, due to this mode only using 6 bits per channel instead of the full 8 bits per channel) but even attempting to first scale the values into the 0-to-63 range and then scaling them again into the 0-to-255 range, still didn't produce results that match the values in the DOSBox color palette for the grayscale part of the palette. It did give some rounding errors, but those errors didn't generate the exact RGB values found in the DOSBox 256 color palette for mode 0x13.

Either there's something in the VGA official specs that involves calculating these using an algorithm that isn't the obvious one, or there's a bug in the DOSBox palette.

Reply 1 of 3, by ripsaw8080

User metadata
Rank DOSBox Author
Rank
DOSBox Author
videogamer555 wrote on 2024-05-25, 04:59:

15 goes into 255 exactly 17 times

VGA DAC RGB register values are not 8-bit, they're 6-bit, and 15 does not divide evenly into 63.

IBM, and therefore DOSBox, uses these RGB register values for indexes 16-31 of the 256-color default palette:

00,00,00
05,05,05
08,08,08
0B,0B,0B
0E,0E,0E
11,11,11
14,14,14
18,18,18
1C,1C,1C
20,20,20
24,24,24
28,28,28
2D,2D,2D
32,32,32
38,38,38
3F,3F,3F

There's no way to make it a perfectly linear 0-63 ramp, and any debate about the values chosen by IBM is academic because those values are historical.
 
To promote 6-bit color to 8-bit: shift left by two bits, then copy the two highest bits to the two lowest bits to create a full 0-255 range. This should explain the 8-bit RGB values you're seeing.

Reply 2 of 3, by videogamer555

User metadata
Rank Member
Rank
Member
ripsaw8080 wrote on 2024-05-25, 06:16:
VGA DAC RGB register values are not 8-bit, they're 6-bit, and 15 does not divide evenly into 63. […]
Show full quote
videogamer555 wrote on 2024-05-25, 04:59:

15 goes into 255 exactly 17 times

VGA DAC RGB register values are not 8-bit, they're 6-bit, and 15 does not divide evenly into 63.

IBM, and therefore DOSBox, uses these RGB register values for indexes 16-31 of the 256-color default palette:

00,00,00
05,05,05
08,08,08
0B,0B,0B
0E,0E,0E
11,11,11
14,14,14
18,18,18
1C,1C,1C
20,20,20
24,24,24
28,28,28
2D,2D,2D
32,32,32
38,38,38
3F,3F,3F

There's no way to make it a perfectly linear 0-63 ramp, and any debate about the values chosen by IBM is academic because those values are historical.
 
To promote 6-bit color to 8-bit: shift left by two bits, then copy the two highest bits to the two lowest bits to create a full 0-255 range. This should explain the 8-bit RGB values you're seeing.

Are the RGB values you have in the above table the actual historical values used by the IBM MCGA and VGA cards for 256 color mode?

Reply 3 of 3, by mkarcher

User metadata
Rank l33t
Rank
l33t
videogamer555 wrote on 2024-05-25, 19:26:
ripsaw8080 wrote on 2024-05-25, 06:16:
IBM, and therefore DOSBox, uses these RGB register values for indexes 16-31 of the 256-color default palette: […]
Show full quote

IBM, and therefore DOSBox, uses these RGB register values for indexes 16-31 of the 256-color default palette:

00,00,00
05,05,05
08,08,08
0B,0B,0B
0E,0E,0E
11,11,11
14,14,14
18,18,18
1C,1C,1C
20,20,20
24,24,24
28,28,28
2D,2D,2D
32,32,32
38,38,38
3F,3F,3F

Are the RGB values you have in the above table the actual historical values used by the IBM MCGA and VGA cards for 256 color mode?

I just compared these values to the values in the IBM PS/2 Model 30 (8086) BIOS with the IBM ROM part numbers 61X8939/61X8940. It is a perfect match.