1 | /* $Id: dibsect.cpp,v 1.49 2001-03-29 18:52:53 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * GDI32 DIB sections
|
---|
5 | *
|
---|
6 | * Copyright 1998-2000 Sander van Leeuwen (sandervl@xs4all.nl)
|
---|
7 | * Copyright 1998 Patrick Haller
|
---|
8 | *
|
---|
9 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
10 | *
|
---|
11 | * NOTE:
|
---|
12 | * This is not a complete solution for CreateDIBSection, but enough for Quake 2!
|
---|
13 | *
|
---|
14 | */
|
---|
15 | #define INCL_GPI
|
---|
16 | #define INCL_WIN
|
---|
17 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
---|
18 | #include <stdlib.h>
|
---|
19 | #include <string.h>
|
---|
20 | #include <win32type.h>
|
---|
21 | #include <misc.h>
|
---|
22 | #define OS2_ONLY
|
---|
23 | #include "dibsect.h"
|
---|
24 | #include <vmutex.h>
|
---|
25 | #include <win32api.h>
|
---|
26 | #include <winconst.h>
|
---|
27 | #include <winuser32.h>
|
---|
28 | #include <cpuhlp.h>
|
---|
29 | #include <dcdata.h>
|
---|
30 | #include "oslibgpi.h"
|
---|
31 | #include "rgbcvt.h"
|
---|
32 |
|
---|
33 | #define DBG_LOCALLOG DBG_dibsect
|
---|
34 | #include "dbglocal.h"
|
---|
35 |
|
---|
36 | static VMutex dibMutex;
|
---|
37 |
|
---|
38 | //******************************************************************************
|
---|
39 | //******************************************************************************
|
---|
40 | DIBSection::DIBSection(BITMAPINFOHEADER_W *pbmi, char *pColors, DWORD iUsage, DWORD hSection, DWORD dwOffset, DWORD handle, int fFlip)
|
---|
41 | : bmpBits(NULL), pOS2bmp(NULL), next(NULL), bmpBitsDblBuffer(NULL),
|
---|
42 | hdc(0), hwndParent(0)
|
---|
43 | {
|
---|
44 | int palsize=0;
|
---|
45 |
|
---|
46 | bmpsize = pbmi->biWidth;
|
---|
47 | /* @@@PH 98/06/07 -- high-color bitmaps don't have palette */
|
---|
48 |
|
---|
49 | this->fFlip = fFlip;
|
---|
50 | os2bmphdrsize = sizeof(BITMAPINFO2);
|
---|
51 |
|
---|
52 | switch(pbmi->biBitCount)
|
---|
53 | {
|
---|
54 | case 1:
|
---|
55 | bmpsize = ((bmpsize + 31) & ~31) / 8;
|
---|
56 | palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
|
---|
57 | os2bmphdrsize += palsize;
|
---|
58 | break;
|
---|
59 | case 4:
|
---|
60 | bmpsize = ((bmpsize + 7) & ~7) / 2;
|
---|
61 | palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
|
---|
62 | os2bmphdrsize += palsize;
|
---|
63 | break;
|
---|
64 | case 8:
|
---|
65 | palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
|
---|
66 | os2bmphdrsize += palsize;
|
---|
67 | bmpsize = (bmpsize + 3) & ~3;
|
---|
68 | break;
|
---|
69 | case 16:
|
---|
70 | bmpsize *= 2;
|
---|
71 | bmpsize = (bmpsize + 3) & ~3;
|
---|
72 | break;
|
---|
73 | case 24:
|
---|
74 | bmpsize *= 3;
|
---|
75 | bmpsize = (bmpsize + 3) & ~3;
|
---|
76 | break;
|
---|
77 | case 32:
|
---|
78 | bmpsize *= 4;
|
---|
79 | break;
|
---|
80 | default:
|
---|
81 | dprintf(("Unsupported nr of bits %d", pbmi->biBitCount));
|
---|
82 | DebugInt3();
|
---|
83 | break;
|
---|
84 | }
|
---|
85 |
|
---|
86 | this->hSection = hSection;
|
---|
87 | this->dwOffset = dwOffset;
|
---|
88 | if(hSection) {
|
---|
89 | bmpBits = (char *)MapViewOfFile(hSection, FILE_MAP_ALL_ACCESS_W, 0, dwOffset, bmpsize*pbmi->biHeight);
|
---|
90 | if(!bmpBits) {
|
---|
91 | dprintf(("Dibsection: mapViewOfFile %x failed!", hSection));
|
---|
92 | DebugInt3();
|
---|
93 | }
|
---|
94 | }
|
---|
95 | if(!bmpBits) {
|
---|
96 | DosAllocMem((PPVOID)&bmpBits, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
|
---|
97 | }
|
---|
98 | memset(bmpBits, 0, bmpsize*pbmi->biHeight);
|
---|
99 |
|
---|
100 | pOS2bmp = (BITMAPINFO2 *)malloc(os2bmphdrsize);
|
---|
101 |
|
---|
102 | memset(pOS2bmp, /* set header + palette entries to zero */
|
---|
103 | 0,
|
---|
104 | os2bmphdrsize);
|
---|
105 |
|
---|
106 | pOS2bmp->cbFix = sizeof(BITMAPINFO2) - sizeof(RGB2);
|
---|
107 | pOS2bmp->cx = pbmi->biWidth;
|
---|
108 | pOS2bmp->cy = pbmi->biHeight;
|
---|
109 | pOS2bmp->cPlanes = pbmi->biPlanes;
|
---|
110 | pOS2bmp->cBitCount = pbmi->biBitCount;
|
---|
111 | pOS2bmp->ulCompression = pbmi->biCompression; //same as OS/2 (uncompressed, rle8, rle4)
|
---|
112 | //SvL: Ignore BI_BITFIELDS_W type (GpiDrawBits fails otherwise)
|
---|
113 | if(pOS2bmp->ulCompression == BI_BITFIELDS_W) {
|
---|
114 | pOS2bmp->ulCompression = 0;
|
---|
115 | }
|
---|
116 | pOS2bmp->cbImage = pbmi->biSizeImage;
|
---|
117 | dprintf(("handle %x", handle));
|
---|
118 | dprintf(("pOS2bmp->cx %d\n", pOS2bmp->cx));
|
---|
119 | dprintf(("pOS2bmp->cy %d\n", pOS2bmp->cy));
|
---|
120 | dprintf(("pOS2bmp->cPlanes %d\n", pOS2bmp->cPlanes));
|
---|
121 | dprintf(("pOS2bmp->cBitCount %d\n", pOS2bmp->cBitCount));
|
---|
122 | dprintf(("pOS2bmp->ulCompression %d\n", pOS2bmp->ulCompression));
|
---|
123 | dprintf(("pOS2bmp->cbImage %d\n", pOS2bmp->cbImage));
|
---|
124 | dprintf(("Bits at %x, size %d",bmpBits, bmpsize*pbmi->biHeight));
|
---|
125 |
|
---|
126 | // clear DIBSECTION structure
|
---|
127 | memset(&dibinfo, 0, sizeof(dibinfo));
|
---|
128 |
|
---|
129 | // copy BITMAPINFOHEADER data into DIBSECTION structure
|
---|
130 | memcpy(&dibinfo.dsBmih, pbmi, sizeof(*pbmi));
|
---|
131 | dibinfo.dsBm.bmType = 0;
|
---|
132 | dibinfo.dsBm.bmWidth = pbmi->biWidth;
|
---|
133 | dibinfo.dsBm.bmHeight = pbmi->biHeight;
|
---|
134 | dibinfo.dsBm.bmWidthBytes= bmpsize;
|
---|
135 | dibinfo.dsBm.bmPlanes = pbmi->biPlanes;
|
---|
136 | dibinfo.dsBm.bmBitsPixel = pbmi->biBitCount;
|
---|
137 | dibinfo.dsBm.bmBits = bmpBits;
|
---|
138 |
|
---|
139 | dibinfo.dshSection = hSection;
|
---|
140 | dibinfo.dsOffset = dwOffset;
|
---|
141 |
|
---|
142 | if(iUsage == DIB_PAL_COLORS || pbmi->biBitCount <= 8)
|
---|
143 | {
|
---|
144 | dibinfo.dsBitfields[0] = dibinfo.dsBitfields[1] = dibinfo.dsBitfields[2] = 0;
|
---|
145 | if(palsize) {
|
---|
146 | SetDIBColorTable(0, (1 << pbmi->biBitCount), (RGBQUAD *)(pbmi+1));
|
---|
147 | }
|
---|
148 | }
|
---|
149 | else {
|
---|
150 | switch(pbmi->biBitCount)
|
---|
151 | {
|
---|
152 | case 16:
|
---|
153 | dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors : 0x7c00;
|
---|
154 | dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0x03e0;
|
---|
155 | dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0x001f;
|
---|
156 | break;
|
---|
157 |
|
---|
158 | case 24:
|
---|
159 | dibinfo.dsBitfields[0] = 0xff;
|
---|
160 | dibinfo.dsBitfields[1] = 0xff00;
|
---|
161 | dibinfo.dsBitfields[2] = 0xff0000;
|
---|
162 | break;
|
---|
163 |
|
---|
164 | case 32:
|
---|
165 | dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors : 0xff;
|
---|
166 | dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0xff00;
|
---|
167 | dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0xff0000;
|
---|
168 | if(dibinfo.dsBitfields[0] != 0xff && dibinfo.dsBitfields[1] != 0xff00 && dibinfo.dsBitfields[2] != 0xff0000) {
|
---|
169 | dprintf(("DIBSection: unsupported bitfields for 32 bits bitmap!!"));
|
---|
170 | }
|
---|
171 | break;
|
---|
172 | }
|
---|
173 | dprintf(("BI_BITFIELDS_W %x %x %x", dibinfo.dsBitfields[0], dibinfo.dsBitfields[1], dibinfo.dsBitfields[2]));
|
---|
174 | }
|
---|
175 | //double buffer for rgb 555 dib sections (for conversion) or flipped sections
|
---|
176 | if(dibinfo.dsBitfields[1] == 0x03e0 || (fFlip & FLIP_VERT)) {
|
---|
177 | DosAllocMem((PPVOID)&bmpBitsDblBuffer, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
|
---|
178 | }
|
---|
179 |
|
---|
180 | this->handle = handle;
|
---|
181 | this->iUsage = iUsage;
|
---|
182 |
|
---|
183 | dibMutex.enter();
|
---|
184 | if(section == NULL)
|
---|
185 | {
|
---|
186 | dprintf(("section was NULL\n"));
|
---|
187 | section = this;
|
---|
188 | }
|
---|
189 | else
|
---|
190 | {
|
---|
191 | DIBSection *dsect = section;
|
---|
192 | dprintf2(("Increment section starting at %08X\n",dsect));
|
---|
193 |
|
---|
194 | /* @@@PH 98/07/11 fix for dsect->next == NULL */
|
---|
195 | while ( (dsect->next != this) &&
|
---|
196 | (dsect->next != NULL) )
|
---|
197 | {
|
---|
198 | //// dprintf2(("Increment section to %08X\n",dsect->next));
|
---|
199 | dsect = dsect->next;
|
---|
200 | }
|
---|
201 | dsect->next = this;
|
---|
202 | }
|
---|
203 | dibMutex.leave();
|
---|
204 | }
|
---|
205 | //******************************************************************************
|
---|
206 | //******************************************************************************
|
---|
207 | DIBSection::~DIBSection()
|
---|
208 | {
|
---|
209 | dprintf(("Delete DIBSection %x", handle));
|
---|
210 |
|
---|
211 | if(hSection) {
|
---|
212 | UnmapViewOfFile(bmpBits);
|
---|
213 | }
|
---|
214 | else
|
---|
215 | if(bmpBits)
|
---|
216 | DosFreeMem(bmpBits);
|
---|
217 |
|
---|
218 | if(bmpBitsDblBuffer)
|
---|
219 | DosFreeMem(bmpBitsDblBuffer);
|
---|
220 |
|
---|
221 | if(pOS2bmp)
|
---|
222 | free(pOS2bmp);
|
---|
223 |
|
---|
224 | dibMutex.enter();
|
---|
225 | if(section == this)
|
---|
226 | {
|
---|
227 | section = this->next;
|
---|
228 | }
|
---|
229 | else
|
---|
230 | {
|
---|
231 | DIBSection *dsect = section;
|
---|
232 |
|
---|
233 | while(dsect->next != this)
|
---|
234 | {
|
---|
235 | dsect = dsect->next;
|
---|
236 | }
|
---|
237 | dsect->next = this->next;
|
---|
238 | }
|
---|
239 | dibMutex.leave();
|
---|
240 | }
|
---|
241 | //******************************************************************************
|
---|
242 | //******************************************************************************
|
---|
243 | int DIBSection::SetDIBits(HDC hdc, HBITMAP hbitmap, UINT startscan, UINT
|
---|
244 | lines, const VOID *bits, BITMAPINFOHEADER_W *pbmi,
|
---|
245 | UINT coloruse)
|
---|
246 | {
|
---|
247 | lines = (int)lines >= 0 ? (int)lines : (int)-lines;
|
---|
248 | int palsize=0;
|
---|
249 |
|
---|
250 | bmpsize = pbmi->biWidth;
|
---|
251 | os2bmphdrsize = sizeof(BITMAPINFO2);
|
---|
252 |
|
---|
253 | switch(pbmi->biBitCount)
|
---|
254 | {
|
---|
255 | case 1:
|
---|
256 | bmpsize = ((bmpsize + 31) & ~31) / 8;
|
---|
257 | palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
|
---|
258 | os2bmphdrsize += palsize;
|
---|
259 | break;
|
---|
260 | case 4:
|
---|
261 | bmpsize = ((bmpsize + 7) & ~7) / 2;
|
---|
262 | palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
|
---|
263 | os2bmphdrsize += palsize;
|
---|
264 | break;
|
---|
265 | case 8:
|
---|
266 | palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
|
---|
267 | os2bmphdrsize += palsize;
|
---|
268 | bmpsize = (bmpsize + 3) & ~3;
|
---|
269 | break;
|
---|
270 | case 16:
|
---|
271 | bmpsize *= 2;
|
---|
272 | bmpsize = (bmpsize + 3) & ~3;
|
---|
273 | break;
|
---|
274 | case 24:
|
---|
275 | bmpsize *= 3;
|
---|
276 | bmpsize = (bmpsize + 3) & ~3;
|
---|
277 | break;
|
---|
278 | case 32:
|
---|
279 | bmpsize *= 4;
|
---|
280 | break;
|
---|
281 | }
|
---|
282 |
|
---|
283 | //SvL: TODO: Correct??
|
---|
284 | if(!hSection && pOS2bmp->cx != pbmi->biWidth && pOS2bmp->cy != pbmi->biHeight &&
|
---|
285 | pOS2bmp->cBitCount != pbmi->biBitCount)
|
---|
286 | {
|
---|
287 | char *oldbits = bmpBits;
|
---|
288 | int oldsize = dibinfo.dsBm.bmWidthBytes * dibinfo.dsBm.bmHeight;
|
---|
289 |
|
---|
290 | DosAllocMem((PPVOID)&bmpBits, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
|
---|
291 | memcpy(bmpBits, oldbits, min(oldsize, bmpsize*pbmi->biHeight));
|
---|
292 | DosFreeMem(oldbits);
|
---|
293 | }
|
---|
294 | pOS2bmp = (BITMAPINFO2 *)realloc(pOS2bmp, os2bmphdrsize);
|
---|
295 | pOS2bmp->cbFix = sizeof(BITMAPINFO2) - sizeof(RGB2);
|
---|
296 | pOS2bmp->cx = pbmi->biWidth;
|
---|
297 | pOS2bmp->cy = pbmi->biHeight;
|
---|
298 | pOS2bmp->cPlanes = pbmi->biPlanes;
|
---|
299 | pOS2bmp->cBitCount = pbmi->biBitCount;
|
---|
300 | pOS2bmp->ulCompression = pbmi->biCompression; //same as OS/2 (uncompressed, rle8, rle4)
|
---|
301 | //SvL: Ignore BI_BITFIELDS_W type (GpiDrawBits fails otherwise)
|
---|
302 | if(pOS2bmp->ulCompression == BI_BITFIELDS_W) {
|
---|
303 | pOS2bmp->ulCompression = 0;
|
---|
304 | }
|
---|
305 | pOS2bmp->cbImage = pbmi->biSizeImage;
|
---|
306 |
|
---|
307 | // clear DIBSECTION structure
|
---|
308 | memset(&dibinfo, 0, sizeof(dibinfo));
|
---|
309 |
|
---|
310 | // copy BITMAPINFOHEADER data into DIBSECTION structure
|
---|
311 | memcpy(&dibinfo.dsBmih, pbmi, sizeof(*pbmi));
|
---|
312 | dibinfo.dsBm.bmType = 0;
|
---|
313 | dibinfo.dsBm.bmWidth = pbmi->biWidth;
|
---|
314 | dibinfo.dsBm.bmHeight = pbmi->biHeight;
|
---|
315 | dibinfo.dsBm.bmWidthBytes= bmpsize;
|
---|
316 | dibinfo.dsBm.bmPlanes = pbmi->biPlanes;
|
---|
317 | dibinfo.dsBm.bmBitsPixel = pbmi->biBitCount;
|
---|
318 | dibinfo.dsBm.bmBits = bmpBits;
|
---|
319 |
|
---|
320 | dibinfo.dshSection = hSection;
|
---|
321 | dibinfo.dsOffset = dwOffset;
|
---|
322 |
|
---|
323 | if(coloruse == DIB_PAL_COLORS || pbmi->biBitCount <= 8)
|
---|
324 | {
|
---|
325 | dibinfo.dsBitfields[0] = dibinfo.dsBitfields[1] = dibinfo.dsBitfields[2] = 0;
|
---|
326 | }
|
---|
327 | else {
|
---|
328 | char *pColors = (char *)pbmi + 1;
|
---|
329 |
|
---|
330 | switch(pbmi->biBitCount)
|
---|
331 | {
|
---|
332 | case 16:
|
---|
333 | dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors : 0x7c00;
|
---|
334 | dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0x03e0;
|
---|
335 | dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0x001f;
|
---|
336 | break;
|
---|
337 |
|
---|
338 | case 24:
|
---|
339 | dibinfo.dsBitfields[0] = 0xff;
|
---|
340 | dibinfo.dsBitfields[1] = 0xff00;
|
---|
341 | dibinfo.dsBitfields[2] = 0xff0000;
|
---|
342 | break;
|
---|
343 |
|
---|
344 | case 32:
|
---|
345 | dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS_W) ? *(DWORD *)pColors : 0xff;
|
---|
346 | dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 1) : 0xff00;
|
---|
347 | dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS_W) ? *((DWORD *)pColors + 2) : 0xff0000;
|
---|
348 | if(dibinfo.dsBitfields[0] != 0xff && dibinfo.dsBitfields[1] != 0xff00 && dibinfo.dsBitfields[2] != 0xff0000) {
|
---|
349 | dprintf(("DIBSection: unsupported bitfields for 32 bits bitmap!!"));
|
---|
350 | }
|
---|
351 | break;
|
---|
352 | }
|
---|
353 | dprintf(("BI_BITFIELDS_W %x %x %x", dibinfo.dsBitfields[0], dibinfo.dsBitfields[1], dibinfo.dsBitfields[2]));
|
---|
354 | }
|
---|
355 |
|
---|
356 | //double buffer for rgb 555 dib sections (for conversion) or flipped sections
|
---|
357 | if(dibinfo.dsBitfields[1] == 0x03e0 || (fFlip & FLIP_VERT)) {
|
---|
358 | if(bmpBitsDblBuffer) {
|
---|
359 | DosFreeMem(bmpBitsDblBuffer);
|
---|
360 | }
|
---|
361 | DosAllocMem((PPVOID)&bmpBitsDblBuffer, dibinfo.dsBm.bmWidthBytes*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
|
---|
362 | }
|
---|
363 |
|
---|
364 | dprintf(("DIBSection::SetDIBits (%d,%d), %d %d", pbmi->biWidth, pbmi->biHeight, pbmi->biBitCount, pbmi->biCompression));
|
---|
365 | if(palsize) {
|
---|
366 | SetDIBColorTable(0, 1 << pbmi->biBitCount, (RGBQUAD *)(pbmi+1));
|
---|
367 | }
|
---|
368 |
|
---|
369 | if(bits)
|
---|
370 | {
|
---|
371 | if(pOS2bmp->ulCompression == BCA_UNCOMP) {
|
---|
372 | int size = bmpsize*lines;
|
---|
373 | memcpy(bmpBits+bmpsize*startscan, bits, size);
|
---|
374 | }
|
---|
375 | else {
|
---|
376 | dprintf(("Compressed image!!"));
|
---|
377 | if(startscan != 0) {
|
---|
378 | dprintf(("WARNING: Compressed image & startscan != 0!!!!"));
|
---|
379 | }
|
---|
380 | memcpy(bmpBits, bits, pbmi->biSizeImage);
|
---|
381 | }
|
---|
382 | }
|
---|
383 | return(lines);
|
---|
384 | }
|
---|
385 | //******************************************************************************
|
---|
386 | //******************************************************************************
|
---|
387 | int DIBSection::SetDIBColorTable(int startIdx, int cEntries, RGBQUAD *rgb)
|
---|
388 | {
|
---|
389 | int i;
|
---|
390 |
|
---|
391 | if(startIdx + cEntries > (1 << pOS2bmp->cBitCount))
|
---|
392 | {
|
---|
393 | dprintf(("DIBSection::SetDIBColorTable, invalid nr of entries %d %d\n", startIdx, cEntries));
|
---|
394 | return(0);
|
---|
395 | }
|
---|
396 |
|
---|
397 | memcpy(&pOS2bmp->argbColor[startIdx], rgb, cEntries*sizeof(RGB2));
|
---|
398 |
|
---|
399 | for(i=startIdx;i<cEntries;i++)
|
---|
400 | {
|
---|
401 | pOS2bmp->argbColor[i].fcOptions = 0;
|
---|
402 | #ifdef DEBUG_PALETTE
|
---|
403 | dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&pOS2bmp->argbColor[i])) ));
|
---|
404 | #endif
|
---|
405 | }
|
---|
406 |
|
---|
407 | return(cEntries);
|
---|
408 | }
|
---|
409 | //******************************************************************************
|
---|
410 | //******************************************************************************
|
---|
411 | int DIBSection::SetDIBColorTable(int startIdx, int cEntries, PALETTEENTRY *palentry)
|
---|
412 | {
|
---|
413 | int i;
|
---|
414 |
|
---|
415 | if(startIdx + cEntries > (1 << pOS2bmp->cBitCount))
|
---|
416 | {
|
---|
417 | dprintf(("DIBSection::SetDIBColorTable, invalid nr of entries %d %d\n", startIdx, cEntries));
|
---|
418 | return(0);
|
---|
419 | }
|
---|
420 |
|
---|
421 | for(i=startIdx;i<cEntries;i++)
|
---|
422 | {
|
---|
423 | pOS2bmp->argbColor[i].fcOptions = 0;
|
---|
424 | pOS2bmp->argbColor[i].bBlue = palentry[i].peBlue;
|
---|
425 | pOS2bmp->argbColor[i].bGreen = palentry[i].peGreen;
|
---|
426 | pOS2bmp->argbColor[i].bRed = palentry[i].peRed;
|
---|
427 | }
|
---|
428 |
|
---|
429 | return(cEntries);
|
---|
430 | }
|
---|
431 | //******************************************************************************
|
---|
432 | //******************************************************************************
|
---|
433 | BOOL DIBSection::BitBlt(HDC hdcDest, int nXdest, int nYdest, int nDestWidth,
|
---|
434 | int nDestHeight, int nXsrc, int nYsrc,
|
---|
435 | int nSrcWidth, int nSrcHeight, DWORD Rop)
|
---|
436 | {
|
---|
437 | HPS hps = (HPS)hdcDest;
|
---|
438 | POINTL point[4];
|
---|
439 | LONG rc, hdcHeight, hdcWidth;
|
---|
440 | PVOID bitmapBits = NULL;
|
---|
441 | int oldyinversion = 0;
|
---|
442 | BOOL fRestoryYInversion = FALSE, fFrameWindowDC = FALSE;
|
---|
443 | HWND hwndDest;
|
---|
444 | pDCData pHps;
|
---|
445 |
|
---|
446 | pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdcDest);
|
---|
447 | if(!pHps)
|
---|
448 | {
|
---|
449 | SetLastError(ERROR_INVALID_HANDLE_W);
|
---|
450 | return FALSE;
|
---|
451 | }
|
---|
452 |
|
---|
453 | hwndDest = WindowFromDC(hdcDest); //could return desktop window, so check that
|
---|
454 | if(hwndDest && pHps->hwnd && !pHps->isClient) {
|
---|
455 | fFrameWindowDC = TRUE;
|
---|
456 | }
|
---|
457 |
|
---|
458 | dprintf(("DIBSection::BitBlt %x %X (hps %x) %x to(%d,%d)(%d,%d) from (%d,%d)(%d,%d) rop %x flip %x",
|
---|
459 | handle, hdcDest, hps, hwndDest, nXdest, nYdest, nDestWidth, nDestHeight,
|
---|
460 | nXsrc, nYsrc, nSrcWidth, nSrcHeight, Rop, fFlip));
|
---|
461 |
|
---|
462 | if(hwndDest) {
|
---|
463 | RECT rect;
|
---|
464 |
|
---|
465 | if(fFrameWindowDC) {
|
---|
466 | GetWindowRect(hwndDest, &rect);
|
---|
467 | }
|
---|
468 | else GetClientRect(hwndDest, &rect);
|
---|
469 | hdcHeight = rect.bottom - rect.top;
|
---|
470 | hdcWidth = rect.right - rect.left;
|
---|
471 | }
|
---|
472 | else {
|
---|
473 | hdcHeight = pHps->bitmapHeight;
|
---|
474 | hdcWidth = pHps->bitmapWidth;
|
---|
475 | }
|
---|
476 |
|
---|
477 | //win32 coordinates are relative to left top, OS/2 expects left bottom
|
---|
478 | //source rectangle is non-inclusive (top, right not included)
|
---|
479 | //destination rectangle is incl.-inclusive (everything included)
|
---|
480 |
|
---|
481 | if(nXdest + nDestWidth > hdcWidth) {
|
---|
482 | nDestWidth = hdcWidth - nXdest;
|
---|
483 | }
|
---|
484 |
|
---|
485 | if(nYdest + nDestHeight > hdcHeight) {
|
---|
486 | nDestHeight = hdcHeight - nYdest;
|
---|
487 | }
|
---|
488 |
|
---|
489 | point[0].x = nXdest;
|
---|
490 | point[0].y = hdcHeight - nYdest - nDestHeight;
|
---|
491 | point[1].x = nXdest + nDestWidth - 1;
|
---|
492 | point[1].y = hdcHeight - nYdest - 1;
|
---|
493 |
|
---|
494 | //target rectangle is inclusive-inclusive
|
---|
495 | if(nXsrc + nSrcWidth > pOS2bmp->cx) {
|
---|
496 | nSrcWidth = pOS2bmp->cx - nXsrc;
|
---|
497 | }
|
---|
498 | if(nYsrc + nSrcHeight > pOS2bmp->cy) {
|
---|
499 | nSrcHeight = pOS2bmp->cy - nYsrc;
|
---|
500 | }
|
---|
501 | point[2].x = nXsrc;
|
---|
502 | point[2].y = pOS2bmp->cy - nYsrc - nSrcHeight;
|
---|
503 | point[3].x = nXsrc + nSrcWidth;
|
---|
504 | point[3].y = pOS2bmp->cy - nYsrc;
|
---|
505 |
|
---|
506 | dprintf(("DIBSection::BitBlt (%d,%d)(%d,%d) from (%d,%d)(%d,%d) dim (%d,%d)(%d,%d)", point[0].x, point[0].y,
|
---|
507 | point[1].x, point[1].y, point[2].x, point[2].y, point[3].x, point[3].y,
|
---|
508 | nDestWidth, nDestHeight, nSrcWidth, nSrcHeight));
|
---|
509 |
|
---|
510 | #ifdef INVERT
|
---|
511 | oldyinversion = GpiQueryYInversion(hps);
|
---|
512 | if(oldyinversion != 0) {
|
---|
513 | GpiEnableYInversion(hps, 0);
|
---|
514 | fRestoryYInversion = TRUE;
|
---|
515 | }
|
---|
516 | #endif
|
---|
517 |
|
---|
518 | #ifdef DEBUG
|
---|
519 | RECTL rcltemp;
|
---|
520 | GreGetDCOrigin(hps, (PPOINTL)&rcltemp);
|
---|
521 | dprintf(("origin (%d,%d) yinv %d", rcltemp.xLeft, rcltemp.yBottom, oldyinversion));
|
---|
522 | #endif
|
---|
523 |
|
---|
524 | if(fFlip & FLIP_HOR)
|
---|
525 | {
|
---|
526 | ULONG x;
|
---|
527 | x = point[0].x;
|
---|
528 | point[0].x = point[1].x;
|
---|
529 | point[1].x = x;
|
---|
530 | }
|
---|
531 |
|
---|
532 | ULONG os2mode, winmode;
|
---|
533 |
|
---|
534 | os2mode = BBO_OR;
|
---|
535 | winmode = GetStretchBltMode(hdcDest);
|
---|
536 | switch(winmode) {
|
---|
537 | case BLACKONWHITE_W:
|
---|
538 | os2mode = BBO_AND;
|
---|
539 | break;
|
---|
540 | case WHITEONBLACK_W:
|
---|
541 | case HALFTONE_W: //TODO:
|
---|
542 | os2mode = BBO_OR;
|
---|
543 | break;
|
---|
544 | case COLORONCOLOR_W:
|
---|
545 | os2mode = BBO_IGNORE;
|
---|
546 | break;
|
---|
547 | }
|
---|
548 | if(fFlip & FLIP_VERT) {
|
---|
549 | //manually reverse bitmap data
|
---|
550 | char *src = bmpBits + (pOS2bmp->cy-1)*dibinfo.dsBm.bmWidthBytes;
|
---|
551 | char *dst = bmpBitsDblBuffer;
|
---|
552 | for(int i=0;i<pOS2bmp->cy;i++) {
|
---|
553 | memcpy(dst, src, dibinfo.dsBm.bmWidthBytes);
|
---|
554 | dst += dibinfo.dsBm.bmWidthBytes;
|
---|
555 | src -= dibinfo.dsBm.bmWidthBytes;
|
---|
556 | }
|
---|
557 | bitmapBits = bmpBitsDblBuffer;
|
---|
558 | }
|
---|
559 | else bitmapBits = bmpBits;
|
---|
560 |
|
---|
561 | //SvL: Optimize this.. (don't convert entire bitmap if only a part will be blitted to the dc)
|
---|
562 | if(dibinfo.dsBitfields[1] == 0x3E0) {//RGB 555?
|
---|
563 | dprintf(("DIBSection::BitBlt; convert rgb 555 to 565 (old y inv. = %d)", oldyinversion));
|
---|
564 |
|
---|
565 | if(bmpBitsDblBuffer == NULL)
|
---|
566 | DebugInt3();
|
---|
567 |
|
---|
568 | // PH 2000/10/01 - Fix for Beyond Compare 1.9d
|
---|
569 | // Note: according to documentation, cmImage can be zero for
|
---|
570 | // RGB- / non-compressed bitmaps.
|
---|
571 | int iLength = pOS2bmp->cbImage;
|
---|
572 | if (iLength == 0)
|
---|
573 | iLength = pOS2bmp->cx * pOS2bmp->cy * (pOS2bmp->cBitCount >> 3);
|
---|
574 |
|
---|
575 | if (iLength > 0)
|
---|
576 | {
|
---|
577 | if(CPUFeatures & CPUID_MMX)
|
---|
578 | RGB555to565MMX((WORD *)bmpBitsDblBuffer, (WORD *)bitmapBits, iLength/sizeof(WORD));
|
---|
579 | else RGB555to565((WORD *)bmpBitsDblBuffer, (WORD *)bitmapBits, iLength/sizeof(WORD));
|
---|
580 | }
|
---|
581 | else
|
---|
582 | {
|
---|
583 | dprintf(("GDI32: DIBSect::BitBlt: WARNING! zero-length bitmap! %08xh", pOS2bmp));
|
---|
584 | }
|
---|
585 |
|
---|
586 |
|
---|
587 | rc = GpiDrawBits(hps, bmpBitsDblBuffer, pOS2bmp, 4, &point[0], ROP_SRCCOPY, os2mode);
|
---|
588 | }
|
---|
589 | else {
|
---|
590 | rc = GpiDrawBits(hps, bitmapBits, pOS2bmp, 4, &point[0], ROP_SRCCOPY, os2mode);
|
---|
591 | }
|
---|
592 | if(rc == GPI_OK) {
|
---|
593 | DIBSection *destdib = DIBSection::findHDC(hdcDest);
|
---|
594 | if(destdib) {
|
---|
595 | destdib->sync(hps, nYdest, nDestHeight, FALSE);
|
---|
596 | }
|
---|
597 | #ifdef INVERT
|
---|
598 | //restore old y inversion height
|
---|
599 | if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion);
|
---|
600 | #endif
|
---|
601 | SetLastError(ERROR_SUCCESS_W);
|
---|
602 |
|
---|
603 | return(TRUE);
|
---|
604 | }
|
---|
605 | #ifdef INVERT
|
---|
606 | if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion);
|
---|
607 | #endif
|
---|
608 |
|
---|
609 | dprintf(("DIBSection::BitBlt %X (%d,%d) (%d,%d) to (%d,%d) (%d,%d) returned %d\n", hps, point[0].x, point[0].y, point[1].x, point[1].y, point[2].x, point[2].y, point[3].x, point[3].y, rc));
|
---|
610 | dprintf(("WinGetLastError returned %X\n", WinGetLastError(WinQueryAnchorBlock(hwndDest)) & 0xFFFF));
|
---|
611 | return(FALSE);
|
---|
612 | }
|
---|
613 | //******************************************************************************
|
---|
614 | //******************************************************************************
|
---|
615 | void DIBSection::sync(HDC hdc, DWORD nYdest, DWORD nDestHeight, BOOL orgYInversion)
|
---|
616 | {
|
---|
617 | APIRET rc;
|
---|
618 | char *destBuf;
|
---|
619 |
|
---|
620 | dprintf(("Sync destination dibsection %x (%x) (%d,%d) flip %d", handle, hdc, nYdest, nDestHeight, fFlip));
|
---|
621 |
|
---|
622 | BITMAPINFO2 *tmphdr = (BITMAPINFO2 *)malloc(os2bmphdrsize);
|
---|
623 | memcpy(tmphdr, pOS2bmp, os2bmphdrsize);
|
---|
624 |
|
---|
625 | #ifdef INVERT
|
---|
626 | int oldyinversion = 0;
|
---|
627 | if(orgYInversion == TRUE) {
|
---|
628 | oldyinversion = GpiQueryYInversion(hdc);
|
---|
629 | dprintf(("Sync destination dibsection: hdc y inversion = %d", oldyinversion));
|
---|
630 | if(oldyinversion != 0) {
|
---|
631 | GpiEnableYInversion(hdc, 0);
|
---|
632 | }
|
---|
633 | }
|
---|
634 | #endif
|
---|
635 |
|
---|
636 | if(fFlip & FLIP_VERT) {
|
---|
637 | destBuf = bmpBitsDblBuffer + nYdest*dibinfo.dsBm.bmWidthBytes;
|
---|
638 |
|
---|
639 | rc = GpiQueryBitmapBits(hdc, nYdest, nDestHeight, destBuf,
|
---|
640 | tmphdr);
|
---|
641 | //manually reverse bitmap data
|
---|
642 | char *src = destBuf;
|
---|
643 | char *dst = GetDIBObject() + (nYdest+nDestHeight-1)*dibinfo.dsBm.bmWidthBytes;
|
---|
644 | for(int i=0;i<nDestHeight;i++) {
|
---|
645 | memcpy(dst, src, dibinfo.dsBm.bmWidthBytes);
|
---|
646 | dst -= dibinfo.dsBm.bmWidthBytes;
|
---|
647 | src += dibinfo.dsBm.bmWidthBytes;
|
---|
648 | }
|
---|
649 | }
|
---|
650 | else {
|
---|
651 | destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes;
|
---|
652 | rc = GpiQueryBitmapBits(hdc, nYdest, nDestHeight, destBuf,
|
---|
653 | tmphdr);
|
---|
654 |
|
---|
655 | #ifdef DEBUG_PALETTE
|
---|
656 | if(rc != GPI_ALTERROR && tmphdr->cBitCount <= 8) {
|
---|
657 | for(int i=0;i<(1<<tmphdr->cBitCount);i++)
|
---|
658 | {
|
---|
659 | dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&tmphdr->argbColor[i])) ));
|
---|
660 | }
|
---|
661 | }
|
---|
662 | #endif
|
---|
663 | }
|
---|
664 |
|
---|
665 | #if 0
|
---|
666 | if(dibinfo.dsBitfields[1] == 0x3E0) {//RGB 555?
|
---|
667 | dprintf(("DIBSection::sync: convert RGB 565 to RGB 555"));
|
---|
668 |
|
---|
669 | destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes;
|
---|
670 |
|
---|
671 | if(CPUFeatures & CPUID_MMX) {
|
---|
672 | RGB565to555MMX((WORD *)destBuf, (WORD *)destBuf, (nDestHeight*dibinfo.dsBm.bmWidthBytes)/sizeof(WORD));
|
---|
673 | }
|
---|
674 | else RGB565to555((WORD *)destBuf, (WORD *)destBuf, (nDestHeight*dibinfo.dsBm.bmWidthBytes)/sizeof(WORD));
|
---|
675 | }
|
---|
676 | #endif
|
---|
677 | free(tmphdr);
|
---|
678 | if(rc != nDestHeight) {
|
---|
679 | DebugInt3();
|
---|
680 | }
|
---|
681 |
|
---|
682 | #ifdef INVERT
|
---|
683 | if(oldyinversion) GpiEnableYInversion(hdc, oldyinversion);
|
---|
684 | #endif
|
---|
685 |
|
---|
686 | }
|
---|
687 | //******************************************************************************
|
---|
688 | //manual sync if no stretching and bpp is the same
|
---|
689 | //WARNING: this also assumes the colortables are the same
|
---|
690 | //******************************************************************************
|
---|
691 | void DIBSection::sync(DWORD xDst, DWORD yDst, DWORD widthDst, DWORD heightDst, PVOID bits)
|
---|
692 | {
|
---|
693 | char *srcbuf, *destbuf;
|
---|
694 | int linesize;
|
---|
695 |
|
---|
696 | srcbuf = (char *)bits + dibinfo.dsBm.bmWidthBytes*yDst +
|
---|
697 | (xDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
|
---|
698 | destbuf = (char *)GetDIBObject() + dibinfo.dsBm.bmWidthBytes*yDst +
|
---|
699 | (xDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
|
---|
700 | linesize = (widthDst*dibinfo.dsBm.bmWidthBytes)/pOS2bmp->cx;
|
---|
701 | for(int i=0;i<heightDst;i++) {
|
---|
702 | memcpy(destbuf, srcbuf, linesize);
|
---|
703 | destbuf += dibinfo.dsBm.bmWidthBytes;
|
---|
704 | srcbuf += linesize;
|
---|
705 | }
|
---|
706 | }
|
---|
707 | //******************************************************************************
|
---|
708 | //******************************************************************************
|
---|
709 | void DIBSection::SelectDIBObject(HDC hdc)
|
---|
710 | {
|
---|
711 | this->hdc = hdc;
|
---|
712 | hwndParent = WindowFromDC(hdc);
|
---|
713 | dprintf(("SelectDIBObject %x into %x hwndParent = %x", handle, hdc, hwndParent));
|
---|
714 | }
|
---|
715 | //******************************************************************************
|
---|
716 | //******************************************************************************
|
---|
717 | DIBSection *DIBSection::findObj(HANDLE handle)
|
---|
718 | {
|
---|
719 | DIBSection *dsect = section;
|
---|
720 |
|
---|
721 | dibMutex.enter();
|
---|
722 | while(dsect)
|
---|
723 | {
|
---|
724 | if(dsect->handle == handle)
|
---|
725 | {
|
---|
726 | dibMutex.leave();
|
---|
727 | return(dsect);
|
---|
728 | }
|
---|
729 | dsect = dsect->next;
|
---|
730 | }
|
---|
731 | dibMutex.leave();
|
---|
732 | return(NULL);
|
---|
733 | }
|
---|
734 | //******************************************************************************
|
---|
735 | //A bitmap can only be selected into one DC, so this works.
|
---|
736 | //******************************************************************************
|
---|
737 | DIBSection *DIBSection::findHDC(HDC hdc)
|
---|
738 | {
|
---|
739 | DIBSection *dsect = section;
|
---|
740 |
|
---|
741 | while(dsect)
|
---|
742 | {
|
---|
743 | if(dsect->hdc == hdc)
|
---|
744 | {
|
---|
745 | return(dsect);
|
---|
746 | }
|
---|
747 | dsect = dsect->next;
|
---|
748 | }
|
---|
749 | return(NULL);
|
---|
750 | }
|
---|
751 | //******************************************************************************
|
---|
752 | //******************************************************************************
|
---|
753 | void DIBSection::deleteSection(HANDLE handle)
|
---|
754 | {
|
---|
755 | DIBSection *dsect = findObj(handle);
|
---|
756 |
|
---|
757 | if(dsect)
|
---|
758 | delete dsect;
|
---|
759 | }
|
---|
760 | //******************************************************************************
|
---|
761 | //******************************************************************************
|
---|
762 | int DIBSection::GetDIBSection(int iSize, void *lpBuffer)
|
---|
763 | {
|
---|
764 | DIBSECTION *pDIBSection = (DIBSECTION *)lpBuffer;
|
---|
765 | LPBITMAP_W dsBm = (LPBITMAP_W)lpBuffer;
|
---|
766 |
|
---|
767 | dprintf2(("GetDIBSection %x %d %x", handle, iSize, lpBuffer));
|
---|
768 | if(iSize == sizeof(DIBSECTION))
|
---|
769 | {
|
---|
770 | memcpy(pDIBSection, &dibinfo, sizeof(dibinfo));
|
---|
771 | return sizeof(DIBSECTION);
|
---|
772 | }
|
---|
773 | else
|
---|
774 | if(iSize == sizeof(BITMAP_W))
|
---|
775 | {
|
---|
776 | memcpy(dsBm, &dibinfo.dsBm, sizeof(dibinfo.dsBm));
|
---|
777 | return sizeof(BITMAP_W);
|
---|
778 | }
|
---|
779 | return 0;
|
---|
780 |
|
---|
781 | }
|
---|
782 | //******************************************************************************
|
---|
783 | //******************************************************************************
|
---|
784 | int DIBSection::GetBitCount()
|
---|
785 | {
|
---|
786 | if(pOS2bmp == NULL)
|
---|
787 | return 0;
|
---|
788 | else
|
---|
789 | return pOS2bmp->cBitCount;
|
---|
790 | }
|
---|
791 | //******************************************************************************
|
---|
792 | //******************************************************************************
|
---|
793 | int DIBSection::GetHeight()
|
---|
794 | {
|
---|
795 | if(pOS2bmp == NULL)
|
---|
796 | return 0;
|
---|
797 | else
|
---|
798 | return pOS2bmp->cy;
|
---|
799 | }
|
---|
800 | //******************************************************************************
|
---|
801 | //******************************************************************************
|
---|
802 | int DIBSection::GetWidth()
|
---|
803 | {
|
---|
804 | if(pOS2bmp == NULL)
|
---|
805 | return 0;
|
---|
806 | else
|
---|
807 | return pOS2bmp->cx;
|
---|
808 | }
|
---|
809 | //******************************************************************************
|
---|
810 | //******************************************************************************
|
---|
811 | DIBSection *DIBSection::section = NULL;
|
---|