From: "Earl F. Glynn" Subject: Re: 15-bit-color value to TColor value Date: Friday, June 23, 2000 10:08 AM "Earl F. Glynn" wrote in message news:8ivona$ehe4@bornews.borland.com... > I have never studied exactly what Windows does when you take a pf15bit bitmap > and convert it to a pf24bit bitmap -- perhaps I should. To find out what's going on in converting 15-bit color to 24-bit color, let's look at the shades of gray in 15-bit and see what they become in a 24-bit bitmap. There are 32 shades of gray in 15-bit, RGB(index,index,index), where index = 0..31. Here's how to create a display a pf15bit bitmap with these 32 shades of gray: procedure TForm1.Button1Click(Sender: TObject); TYPE TRGBTripleArray = ARRAY[WORD] OF TRGBTriple; pRGBTripleArray = ^TRGBTripleArray; VAR Bitmap : TBitmap; i : WORD; j : WORD; Pixel15: WORD; Row15 : pWordArray; Row24 : pRGBTripleArray; begin Bitmap := TBitmap.Create; TRY Bitmap.Width := 32; Bitmap.Height := 32; Bitmap.PixelFormat := pf15bit; Memo1.Clear; Memo1.Lines.Add('15-bit shades of gray'); FOR j := 0 TO Bitmap.Height-1 DO BEGIN Row15 := Bitmap.Scanline[j]; Pixel15 := (j SHL 10) + (j SHL 5) + j; Memo1.Lines.Add(IntToStr(j) + ' ' + IntToHex(j, 4) + ' ' + IntToHex(Pixel15,4)); FOR i := 0 TO Bitmap.Width-1 DO BEGIN // shades of gray Row15[i] := Pixel15 END END; Image1.Picture.Graphic := Bitmap; So defining these 32 shades of gray results with the following in the Memo box: 15-bit shades of gray 0 0000 0000 1 0001 0421 2 0002 0842 3 0003 0C63 4 0004 1084 5 0005 14A5 6 0006 18C6 7 0007 1CE7 8 0008 2108 9 0009 2529 10 000A 294A 11 000B 2D6B 12 000C 318C 13 000D 35AD 14 000E 39CE 15 000F 3DEF 16 0010 4210 17 0011 4631 18 0012 4A52 19 0013 4E73 20 0014 5294 21 0015 56B5 22 0016 5AD6 23 0017 5EF7 24 0018 6318 25 0019 6739 26 001A 6B5A 27 001B 6F7B 28 001C 739C 29 001D 77BD 30 001E 7BDE 31 001F 7FFF All of the above is as expected. Conversion of pf15bit to a pf24bit bitmap is easy with just one statement: Bitmap.PixelFormat := pf24bit BUT, if you let Windows (Delphi?) do this, the process is NOT deterministic. The results appear to depend on your video adapter. (Non-deterministic results like this are one reason not to trust Windows and write your own code the produces consistent results.) So the following code analyzes the 24-bit shades of gray from the conversion of the pf15bit bitmap: Memo1.Lines.Add('15-bit shades of gray converted to 24-bits'); FOR j := 0 TO Bitmap.Height-1 DO BEGIN Row24 := Bitmap.Scanline[j]; WITH Row24[0] DO BEGIN Memo1.Lines.Add(IntToStr(j) + ' ' + IntToHex(j SHL 3, 2) + ' ' + IntToHex(rgbtRed, 2) + ' ' + IntToHex(rgbtGreen,2) + ' ' + IntToHex(rgbtBlue,2)) END END; FINALLY Bitmap.Free END Each line in the output below is like this: <"expected"> One one machine, the results were: 15-bit shades of gray converted to 24-bits 0 00 00 00 00 1 08 08 08 08 2 10 10 10 10 3 18 18 18 18 4 20 21 21 21 5 28 29 29 29 6 30 31 31 31 7 38 39 39 39 8 40 42 42 42 9 48 4A 4A 4A 10 50 52 52 52 11 58 5A 5A 5A 12 60 63 63 63 13 68 6B 6B 6B 14 70 73 73 73 15 78 7B 7B 7B 16 80 84 84 84 17 88 8C 8C 8C 18 90 94 94 94 19 98 9C 9C 9C 20 A0 A5 A5 A5 21 A8 AD AD AD 22 B0 B5 B5 B5 23 B8 BD BD BD 24 C0 C6 C6 C6 25 C8 CE CE CE 26 D0 D6 D6 D6 27 D8 DE DE DE 28 E0 E7 E7 E7 29 E8 EF EF EF 30 F0 F7 F7 F7 31 F8 FF FF FF On another machine the results were: 15-bit shades of gray converted to 24-bits 0 00 00 00 00 1 08 0F 0F 0F 2 10 10 10 10 3 18 1F 1F 1F 4 20 20 20 20 5 28 2F 2F 2F 6 30 30 30 30 7 38 3F 3F 3F 8 40 40 40 40 9 48 4F 4F 4F 10 50 50 50 50 11 58 5F 5F 5F 12 60 60 60 60 13 68 6F 6F 6F 14 70 70 70 70 15 78 7F 7F 7F 16 80 80 80 80 17 88 8F 8F 8F 18 90 90 90 90 19 98 9F 9F 9F 20 A0 A0 A0 A0 21 A8 AF AF AF 22 B0 B0 B0 B0 23 B8 BF BF BF 24 C0 C0 C0 C0 25 C8 CF CF CF 26 D0 D0 D0 D0 27 D8 DF DF DF 28 E0 E0 E0 E0 29 E8 EF EF EF 30 F0 F0 F0 F0 31 F8 FF FF FF So you might get black ($000000) and white ($FFFFFF) the same on both machines, but there still are some shades of gray that are not identical across different machines. When working on image processing projects, I would want code to work the same on every machine, so I'd take a slower, predicatble conversion over what is being done now. -- efg Earl F. Glynn Overland Park, KS USA efg's Computer Lab: http://www.efg2.com/Lab