1 /*const
2 LogPaletteSize = sizeof(TLogPalette) + sizeof(TPaletteEntry) * 255
3 function BitmapColorsToGray( hBitMap : Cardinal Width, Height : integer ) : Cardinal stdcall
4 var
5 hOld : Cardinal
6 DC : HDC
7 {
8 DC = CreateCompatibleDC(0)
9 hOld = Selectobject( DC, hBitMap )
10 FiddleBitmap( DC, hBitMap, Width, Height )
11 SelectObject( DC, hOld )
12 Result = hBitMap
13 }*/
14
15 func uint PaletteFromDIB( BITMAPINFO BitmapInfo )
16 {
17 lp lp
18 int i
19 ubyte tmp
20
21 lp.palVersion = 0x300
22 lp.palNumEntries = 256
23 mcopy( lp.palPalEntry, BitmapInfo.bmiColors, 4 * 256 )
24 for i, 256
25 {
26 p as ( &lp.palPalEntry + i * 4 )->PALENTRY
27 tmp = p.peBlue
28 p.peBlue = p.peRed
29 p.peRed = tmp
30 p.peFlags = $PC_NOCOLLAPSE
31 }
32 return CreatePalette(lp)
33 }
34
35 funct int BytesPerScanline( int PixelsPerScanline, BitsPerPixel, Alignment )
36 {
37 Alignment--
38 Result = ((PixelsPerScanline * BitsPerPixel) + Alignment) & ~Alignment
39 Result = Result >> 4
40 }
41
42 func InitializeBitmapInfoHeader( uint Bitmap, BITMAPINFOHEADER BI, int Colors )
43 {
44 DIBSECTION DS
45 int Bytes
46
47 DS.dsbmih.biSize = 0
48 Bytes = GetObject(Bitmap, sizeof(DS), &DS )
49 if !Bytes
50 {
51 InvalidBitmap
52 }
53 elif (Bytes >= (sizeof(DS.dsbm) + sizeof(DS.dsbmih))) &&
54 (DS.dsbmih.biSize >= DWORD(sizeof(DS.dsbmih)))
55 {
56 BI = DS.dsbmih
57 }
58 else
59 {
60 FillChar(BI, sizeof(BI), 0)
61 with BI, DS.dsbm do
62 {
63 biSize = sizeof(BI)
64 biWidth = bmWidth
65 biHeight = bmHeight
66 }
67 }
68 if colors == 2
69 {
70 BI.biBitCount = 1
71 }
72 elif colors >= 3 and colors <= 16
73 {
74 BI.biBitCount = 4
75 BI.biClrUsed = Colors
76 }
77 elif colors >= 17 and colors <= 256
78 {
79 BI.biBitCount = 8
80 BI.biClrUsed = Colors
81 }
82 else
83 {
84 BI.biBitCount = DS.dsbm.bmBitsPixel * DS.dsbm.bmPlanes
85 }
86 BI.biPlanes = 1
87 if BI.biClrImportant > BI.biClrUsed
88 {
89 BI.biClrImportant = BI.biClrUsed
90 }
91 if !BI.biSizeImage
92 {
93 BI.biSizeImage = BytesPerScanLine(BI.biWidth, BI.biBitCount, 32) * Abs(BI.biHeight)
94 }
95 }
96
97 func GetDIBSizes( uint Bitmap, uint pInfoHeaderSize, ImageSize, int Colors )
98 {
99 BITMAPINFOHEADER BI
100 InitializeBitmapInfoHeader(Bitmap, BI, Colors)
101 if BI.biBitCount > 8
102 {
103 InfoHeaderSize->uint = sizeof(BITMAPINFOHEADER)
104 if BI.biCompression & $BI_BITFIELDS : InfoHeaderSize->uint += 12
105 }
106 elif !BI.biClrUsed
107 {
108 InfoHeaderSize->uint = sizeof(BITMAPINFOHEADER) + 4 * ( BI.biBitCount << 1 )
109 }
110 else
111 {
112 InfoHeaderSize = sizeof(BITMAPINFOHEADER) + 4 * BI.biClrUsed
113 }
114 ImageSize = BI.biSizeImage
115 }
116
117 func FiddleBitmap(uint HDC, uint Bitmap, int Width, Height)
118 {
119 uint BitmapInfoSize = sizeof(BITMAPINFO) + 4 * 255
120
121 BITMAPINFO BitmapInfo
122 POINTER Pixels
123 uint InfoSize
124 uint ADC
125 uint OldPalette, Palette
126 int index
127 ubyte Red, Green, Blue, Gray
128
129 //GetMem(BitmapInfo, BitmapInfoSize)
130 //меняем таблицу цветов - ПРИМЕЧАНИЕ: она использует 256 цветов DIB
131 // FillChar( &BitmapInfo^, BitmapInfoSize, 0)
132 with BitmapInfo.bmiHeader
133 {
134 .biSize = sizeof(BITMAPINFOHEADER)
135 .biWidth = Width
136 .biHeight = Height
137 .biPlanes = 1
138 .biBitCount = 8
139 .biCompression = $BI_RGB
140 .biClrUsed = 256
141 .biClrImportant = 256
142 //GetDIBSizes(Bitmap, InfoSize, .biSizeImage )
143 /*BITMAPINFOHEADER BI
144 InitializeBitmapInfoHeader(Bitmap, BI, 0 )
145 if BI.biBitCount > 8
146 {
147 InfoHeaderSize = sizeof(TBitmapInfoHeader)
148 if (BI.biCompression and BI_BITFIELDS) <> 0
149 Inc(InfoHeaderSize, 12)
150 }
151 else
152 if BI.biClrUsed = 0
153 InfoHeaderSize = sizeof(TBitmapInfoHeader) +
154 sizeof(TRGBQuad) * (1 shl BI.biBitCount)
155 else
156 InfoHeaderSize = sizeof(TBitmapInfoHeader) +
157 sizeof(TRGBQuad) * BI.biClrUsed
158 ImageSize = BI.biSizeImage
159 */
160
161
162 //распределяем место для пикселей
163 Pixels = GlobalAlloc( $GMEM_MOVEABLE, biSizeImage )
164
165 // получаем пиксели DIB
166 ADC = GetDC(0)
167 OldPalette = SelectPalette(ADC, 0, 0)
168 RealizePalette(ADC)
169 GetDIBits(ADC, Bitmap, 0, biHeight, Pixels, BitmapInfo^,
170 DIB_RGB_COLORS)
171 SelectPalette(ADC, OldPalette, true)
172
173 ReleaseDC(0, ADC)
174
175
176 //теперь изменяем таблицу цветов
177 /* for index = 0 to 255 do
178 {
179 Red = BitmapInfo^.bmiColors[ index ].rgbRed
180 Green = BitmapInfo^.bmiColors[ index ].rgbGreen
181 Blue = BitmapInfo^.bmiColors[ index ].rgbBlue
182
183 Gray = RgbToGray( RGB( red, Green, Blue ) )
184
185 BitmapInfo^.bmiColors[ index ].rgbRed = Gray
186 BitmapInfo^.bmiColors[ index ].rgbGreen = Gray
187 BitmapInfo^.bmiColors[ index ].rgbBlue = Gray
188 }
189 */
190 //создаем палитру на основе новой таблицы цветов
191 Palette = PaletteFromDIB(BitmapInfo)
192 OldPalette = SelectPalette(DC, Palette, 0)
193 RealizePalette(DC)
194 StretchDIBits(DC, 0, 0, biWidth, biHeight, 0, 0,
195 biWidth, biHeight,
196 Pixels, BitmapInfo^, $DIB_RGB_COLORS, $SRCCOPY)
197
198 SelectPalette(DC, OldPalette, true)
199
200 GlobalFree(Pixels)
201
202 }