1
2 type RGBQUAD {
3 ubyte rgbBlue
4 ubyte rgbGreen
5 ubyte rgbRed
6 ubyte rgbReserved
7 }
8
9 type PALETTEENTRY {
10 ubyte peRed
11 ubyte peGreen
12 ubyte peBlue
13 ubyte peFlags
14 }
15
16 type BITMAPINFOHEADER {
17 uint bmiSize
18 int bmiWidth
19 int bmiHeight
20 short bmiPlanes
21 short bmiBitCount
22 uint bmiCompression
23 uint bmiSizeImage
24 int bmiXPelsPerMeter
25 int bmiYPelsPerMeter
26 uint bmiClrUsed
27 uint bmiClrImportant
28 }
29
30 type BITMAPINFO {
31 BITMAPINFOHEADER bmiHeader
32 RGBQUAD bmiColors
33 }
34
35 type LOGPALETTE {
36 short palVersion
37 short palNumEntries
38 PALETTEENTRY palPalEntry
39 }
40
41
42 type DIBSECTION {
43 BITMAP dsBm
44 BITMAPINFOHEADER dsBmih
45 uint dsBitfields[3]
46 uint dshSection
47 uint dsOffset
48 }
49
50 define {
51 PC_NOCOLLAPSE = 0x04
52
53 BI_RGB = 0
54 BI_RLE8 = 1
55 BI_RLE4 = 2
56 BI_BITFIELDS = 3
57 BI_JPEG = 4
58 BI_PNG = 5
59
60 DIB_RGB_COLORS = 0
61 DIB_PAL_COLORS = 1
62
63
64 GMEM_MOVEABLE = 0x0002
65 GMEM_ZEROINIT = 0x0040
66 }
67
68 import "gdi32.dll"
69 {
70 uint CreatePalette( LOGPALETTE )
71 uint SelectPalette( uint, uint, uint )
72 uint RealizePalette( uint )
73 int GetDIBits( uint, uint, uint, uint, uint, BITMAPINFO, uint )
74 int StretchDIBits( uint, int, int, int, int, int, int, int, int, uint, BITMAPINFO, uint, int )
75 uint CreateDIBitmap( uint, BITMAPINFOHEADER, uint, uint, BITMAPINFO, uint )
76 uint MaskBlt( uint, uint, uint, uint, uint, uint, uint, uint, uint, uint, uint, uint )
77
78 }
79
80 import "kernel32.dll"
81 {
82 uint GlobalAlloc( uint, uint )
83 uint GlobalFree( uint )
84 uint GlobalLock( uint )
85 uint GlobalUnlock( uint )
86
87 }
88
89 func uint PaletteFromDIB( BITMAPINFO bmi )
90 {
91 LOGPALETTE lp
92 uint i, p
93 ubyte tmp
94
95 lp.palVersion = 0x300
96 lp.palNumEntries = 256
97 mcopy( &lp.palPalEntry, &bmi.bmiColors, 4 * 256 )
98 fornum i, 256
99 {
100 p as ( &lp.palPalEntry + i * 4 )->PALETTEENTRY
101 tmp = p.peBlue
102 p.peBlue = p.peRed
103 p.peRed = tmp
104 p.peFlags = $PC_NOCOLLAPSE
105 }
106 return CreatePalette(lp)
107 }
108
109 func int BytesPerScanLine( int PixelsPerScanline BitsPerPixel Alignment )
110 {
111 uint res
112 Alignment--
113 res = ((PixelsPerScanline * BitsPerPixel) + Alignment) & ~Alignment
114 res = res >> 4
115 return res
116 }
117
118 func uint InitializebmiHeader( uint Bitmap, BITMAPINFOHEADER BIH, int colors )
119 {
120 DIBSECTION DS
121 int Bytes
122
123 DS.dsBmih.bmiSize = 0
124 Bytes = GetObject(Bitmap, sizeof( DIBSECTION ), &DS )
125 if !Bytes
126 {
127 //print( "error\n" )
128 return 0 //Error
129 }
130 elif Bytes >= (sizeof( BITMAP ) + sizeof( BITMAPINFOHEADER )) &&
131 DS.dsBmih.bmiSize >= sizeof( BITMAPINFOHEADER )
132 {
133 mcopy( &BIH, &DS.dsBmih, sizeof( BITMAPINFOHEADER ) )
134 }
135 else
136 {
137 mzero( &BIH, sizeof( BITMAPINFOHEADER ))
138 BIH.bmiSize = sizeof( BITMAPINFOHEADER )
139 BIH.bmiWidth = DS.dsBm.bmWidth
140 BIH.bmiHeight = DS.dsBm.bmHeight
141 }
142 if colors == 2
143 {
144 BIH.bmiBitCount = 1
145 }
146 elif colors >= 3 && colors <= 16
147 {
148 BIH.bmiBitCount = 4
149 BIH.bmiClrUsed = colors
150 }
151 elif colors >= 17 && colors <= 256
152 {
153 BIH.bmiBitCount = 8
154 BIH.bmiClrUsed = colors
155 }
156 else
157 {
158 BIH.bmiBitCount = DS.dsBm.bmBitsPixel * DS.dsBm.bmPlanes
159
160 }
161 BIH.bmiPlanes = 1
162 if BIH.bmiClrImportant > BIH.bmiClrUsed
163 {
164 BIH.bmiClrImportant = BIH.bmiClrUsed
165 }
166 if !BIH.bmiSizeImage
167 {
168 BIH.bmiSizeImage = BytesPerScanLine( BIH.bmiWidth, BIH.bmiBitCount, 32 ) *
169 ?( BIH.bmiHeight < 0, -BIH.bmiHeight, BIH.bmiHeight )
170 }
171 return 1
172 }
173
174 func GetDIBSizes( uint Bitmap, uint pInfoHeaderSize pImageSize, int Colors )
175 {
176 BITMAPINFOHEADER BIH
177 InitializebmiHeader(Bitmap, BIH, Colors)
178 if BIH.bmiBitCount > 8
179 {
180 pInfoHeaderSize->uint = sizeof(BITMAPINFOHEADER)
181 if BIH.bmiCompression & $BI_BITFIELDS : pInfoHeaderSize->uint += 12
182 }
183 elif !BIH.bmiClrUsed
184 {
185 pInfoHeaderSize->uint = sizeof(BITMAPINFOHEADER) + 4 * ( BIH.bmiBitCount << 1 )
186 }
187 else
188 {
189 pInfoHeaderSize->uint = sizeof(BITMAPINFOHEADER) + 4 * BIH.bmiClrUsed
190 }
191 pImageSize->uint = BIH.bmiSizeImage
192 }
193
194 func uint FiddleBitmap( uint DC, uint Bitmap, int Width Height)
195 {
196 uint bmiSize = sizeof(BITMAPINFO) + 4 * 255
197 uint hbmi
198 //BITMAPINFO bmi
199 uint bmi
200 uint Pixels, hPixels
201 uint InfoSize
202 uint ADC
203 uint OldPalette, Palette
204 uint i
205 uint Red, Green, Blue, Grey
206
207 hbmi = GlobalAlloc( $GMEM_MOVEABLE | $GMEM_ZEROINIT, bmiSize )
208
209 bmi as GlobalLock( hbmi )->BITMAPINFO
210
211 //GetMem(bmi, bmiSize)
212 //меняем таблицу цветов - ПРИМЕЧАНИЕ: она использует 256 цветов DIB
213 // FillChar( &bmi^, bmiSize, 0)
214 with bmi.bmiHeader
215 {
216 .bmiSize = sizeof( BITMAPINFOHEADER )
217 .bmiWidth = Width
218 .bmiHeight = Height
219 .bmiPlanes = 1
220 .bmiBitCount = 8
221 .bmiCompression = $BI_RGB
222 .bmiClrUsed = 256
223 .bmiClrImportant = 256
224
225 GetDIBSizes( Bitmap, &InfoSize, &.bmiSizeImage, 0 )
226
227
228 //распределяем место для пикселей
229 hPixels = GlobalAlloc( $GMEM_MOVEABLE, .bmiSizeImage/* + sizeof(BITMAPINFO)*/ )
230
231 Pixels = GlobalLock( hPixels )
232
233 // получаем пиксели DIB
234 ADC = GetDC(0)
235 //ADC = CreateCompatibleDC(0)
236
237 //SelectObject( ADC, Bitmap )
238 //CreateCompatibleBitmap( ADC, 16, 16 )
239 //DIBSECTION xbi
240 //mcopy( Pixels, &bmi, sizeof(BITMAPINFO) )
241 //uint r = GetObject( Bitmap, sizeof( DIBSECTION ), &xbi )
242 //uint pal = PaletteFromDIB( xbi.dsBm )
243 //print( " \(r) pal =\(pal )\n" )
244 OldPalette = SelectPalette(ADC,GetStockObject(15), 0 )
245 RealizePalette(ADC)
246 GetDIBits(ADC, Bitmap, 0, .bmiHeight, Pixels, bmi, $DIB_RGB_COLORS )
247
248 SelectPalette(ADC, OldPalette, 1)
249
250 ReleaseDC(0, ADC)
251
252
253
254 //теперь изменяем таблицу цветов
255 fornum i = 0, 256
256 {
257
258 uint c as ( &bmi.bmiColors + sizeof(RGBQUAD) * i )->RGBQUAD
259
260 Red = c.rgbRed
261 Green = c.rgbGreen
262 Blue = c.rgbBlue
263 //Серое с подсветкой
264 /*Grey = min( 0xFF, ( Red + Green + Blue ) / 3 + 60 )
265 c.rgbRed = Grey
266 c.rgbGreen = Grey
267 c.rgbBlue = Grey*/
268
269 //Понижение контрастности
270 Grey = 0x120//min( 0xFF, ( Red + Green + Blue ) / 3 + 150 )
271 c.rgbRed = min( 0xDD,( Grey + c.rgbRed )/2)
272 c.rgbGreen = min( 0xDD,( Grey + c.rgbGreen )/2)
273 c.rgbBlue = min( 0xDD,( Grey + c.rgbBlue )/2)//min( 0xFF, Grey + 50 )
274
275 // //Подсветка
276 /*Grey = 0x50//min( 0xFF, ( Red + Green + Blue ) / 3 + 150 )
277 c.rgbRed = min( 0xFF, c.rgbRed*15/10)
278 c.rgbGreen = min( 0xFF, c.rgbGreen*15/10)
279 c.rgbBlue = min( 0xFF, c.rgbBlue*15/10)//min( 0xFF, Grey + 50 )*/
280 }
281 //создаем палитру на основе новой таблицы цветов
282 Palette = PaletteFromDIB(bmi)
283 OldPalette = SelectPalette(DC, Palette, 0)
284
285 RealizePalette(DC)
286
287 StretchDIBits(DC, 0, 0, .bmiWidth, .bmiHeight, 0, 0,
288 .bmiWidth, .bmiHeight,
289 Pixels, bmi, $DIB_RGB_COLORS, $SRCCOPY)
290 }
291 SelectPalette(DC, OldPalette, 1)
292 GlobalUnlock( hPixels )
293 GlobalFree( hPixels)
294 GlobalUnlock( hbmi )
295
296 GlobalFree( hbmi)
297 return 0
298 }
299
300
301 /*const
302 LogPaletteSize = sizeof(TLogPalette) + sizeof(TPaletteEntry) * 255*/
303 func uint BitmapColorsToGrey( uint hBitMap, uint hMask, int Width Height )
304 {
305 uint DC, MDC
306 uint hOldDC
307 uint hOldMDC
308
309 DC = CreateCompatibleDC(0)
310 hOldDC = SelectObject( DC, hBitMap )
311 FiddleBitmap( DC, hBitMap, Width, Height )
312
313 MDC = CreateCompatibleDC(0)
314 hOldMDC = SelectObject( MDC, hMask )
315 //uint NDC = CreateCompatibleDC(NDC)
316 //hNew = CreateCompatibleBitmap( DC, Width, Height )
317 //hOld = SelectObject( NDC, hNew )
318 //BitBlt( NDC, 0, 0, Width, Height, MDC, 0, 0, $NOTSRCCOPY )
319 BitBlt( DC, 0, 0, Width, Height, MDC, 0, 0, 0x22 << 16/*$SRCAND*/ )
320 SelectObject( MDC, hOldMDC )
321 DeleteDC( MDC )
322 SelectObject( DC, hOldDC )
323 DeleteDC( DC )
324 return 0
325 }