1 | /* $Id: dibsect.cpp,v 1.29 2000-04-23 15:34:10 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * GDI32 DIB sections
|
---|
5 | *
|
---|
6 | * Copyright 1998 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 <win32wnd.h>
|
---|
28 | #include <cpuhlp.h>
|
---|
29 | #include "oslibgpi.h"
|
---|
30 | #include "rgbcvt.h"
|
---|
31 |
|
---|
32 | #define DBG_LOCALLOG DBG_dibsect
|
---|
33 | #include "dbglocal.h"
|
---|
34 |
|
---|
35 | static VMutex dibMutex;
|
---|
36 |
|
---|
37 | //******************************************************************************
|
---|
38 | //******************************************************************************
|
---|
39 | DIBSection::DIBSection(BITMAPINFOHEADER_W *pbmi, char *pColors, DWORD iUsage, DWORD hSection, DWORD dwOffset, DWORD handle, int fFlip)
|
---|
40 | : bmpBits(NULL), pOS2bmp(NULL), next(NULL), bmpBitsRGB565(NULL)
|
---|
41 | {
|
---|
42 | bmpsize = pbmi->biWidth;
|
---|
43 | /* @@@PH 98/06/07 -- high-color bitmaps don't have palette */
|
---|
44 |
|
---|
45 | this->fFlip = fFlip;
|
---|
46 | os2bmphdrsize = sizeof(BITMAPINFO2);
|
---|
47 |
|
---|
48 | switch(pbmi->biBitCount)
|
---|
49 | {
|
---|
50 | case 1:
|
---|
51 | bmpsize = ((bmpsize + 31) & ~31) / 8;
|
---|
52 | os2bmphdrsize += ((1 << pbmi->biBitCount)-1)*sizeof(RGB2);
|
---|
53 | break;
|
---|
54 | case 4:
|
---|
55 | bmpsize = ((bmpsize + 7) & ~7) / 2;
|
---|
56 | os2bmphdrsize += ((1 << pbmi->biBitCount)-1)*sizeof(RGB2);
|
---|
57 | break;
|
---|
58 | case 8:
|
---|
59 | os2bmphdrsize += ((1 << pbmi->biBitCount)-1)*sizeof(RGB2);
|
---|
60 | bmpsize = (bmpsize + 3) & ~3;
|
---|
61 | break;
|
---|
62 | case 16:
|
---|
63 | bmpsize *= 2;
|
---|
64 | bmpsize = (bmpsize + 3) & ~3;
|
---|
65 | break;
|
---|
66 | case 24:
|
---|
67 | bmpsize *= 3;
|
---|
68 | bmpsize = (bmpsize + 3) & ~3;
|
---|
69 | break;
|
---|
70 | case 32:
|
---|
71 | bmpsize *= 4;
|
---|
72 | break;
|
---|
73 | default:
|
---|
74 | dprintf(("Unsupported nr of bits %d", pbmi->biBitCount));
|
---|
75 | DebugInt3();
|
---|
76 | break;
|
---|
77 | }
|
---|
78 |
|
---|
79 | this->hSection = hSection;
|
---|
80 | if(hSection) {
|
---|
81 | bmpBits = (char *)MapViewOfFile(hSection, FILE_MAP_ALL_ACCESS_W, 0, dwOffset, bmpsize*pbmi->biHeight);
|
---|
82 | if(!bmpBits) {
|
---|
83 | dprintf(("Dibsection: mapViewOfFile %x failed!", hSection));
|
---|
84 | DebugInt3();
|
---|
85 | }
|
---|
86 | }
|
---|
87 | if(!bmpBits) {
|
---|
88 | DosAllocMem((PPVOID)&bmpBits, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
|
---|
89 | }
|
---|
90 | memset(bmpBits, 0, bmpsize*pbmi->biHeight);
|
---|
91 |
|
---|
92 | pOS2bmp = (BITMAPINFO2 *)malloc(os2bmphdrsize);
|
---|
93 |
|
---|
94 | memset(pOS2bmp, /* set header + palette entries to zero */
|
---|
95 | 0,
|
---|
96 | os2bmphdrsize);
|
---|
97 |
|
---|
98 | pOS2bmp->cbFix = sizeof(BITMAPINFO2) - sizeof(RGB2);
|
---|
99 | pOS2bmp->cx = pbmi->biWidth;
|
---|
100 | pOS2bmp->cy = pbmi->biHeight;
|
---|
101 | pOS2bmp->cPlanes = pbmi->biPlanes;
|
---|
102 | pOS2bmp->cBitCount = pbmi->biBitCount;
|
---|
103 | pOS2bmp->ulCompression = pbmi->biCompression;
|
---|
104 | //SvL: Ignore BI_BITFIELDS type (GpiDrawBits fails otherwise)
|
---|
105 | if(pOS2bmp->ulCompression == BI_BITFIELDS) {
|
---|
106 | pOS2bmp->ulCompression = 0;
|
---|
107 | }
|
---|
108 | pOS2bmp->cbImage = pbmi->biSizeImage;
|
---|
109 | dprintf(("handle %x", handle));
|
---|
110 | dprintf(("pOS2bmp->cx %d\n", pOS2bmp->cx));
|
---|
111 | dprintf(("pOS2bmp->cy %d\n", pOS2bmp->cy));
|
---|
112 | dprintf(("pOS2bmp->cPlanes %d\n", pOS2bmp->cPlanes));
|
---|
113 | dprintf(("pOS2bmp->cBitCount %d\n", pOS2bmp->cBitCount));
|
---|
114 | dprintf(("pOS2bmp->ulCompression %d\n", pOS2bmp->ulCompression));
|
---|
115 | dprintf(("pOS2bmp->cbImage %d\n", pOS2bmp->cbImage));
|
---|
116 | dprintf(("Bits at %x, size %d",bmpBits, bmpsize*pbmi->biHeight));
|
---|
117 |
|
---|
118 | // clear DIBSECTION structure
|
---|
119 | memset(&dibinfo, 0, sizeof(dibinfo));
|
---|
120 |
|
---|
121 | // copy BITMAPINFOHEADER data into DIBSECTION structure
|
---|
122 | memcpy(&dibinfo.dsBmih, pbmi, sizeof(*pbmi));
|
---|
123 | dibinfo.dsBm.bmType = 0;
|
---|
124 | dibinfo.dsBm.bmWidth = pbmi->biWidth;
|
---|
125 | dibinfo.dsBm.bmHeight = pbmi->biHeight;
|
---|
126 | dibinfo.dsBm.bmWidthBytes= bmpsize;
|
---|
127 | dibinfo.dsBm.bmPlanes = pbmi->biPlanes;
|
---|
128 | dibinfo.dsBm.bmBitsPixel = pbmi->biBitCount;
|
---|
129 | dibinfo.dsBm.bmBits = bmpBits;
|
---|
130 |
|
---|
131 | dibinfo.dshSection = handle;
|
---|
132 | dibinfo.dsOffset = 0; // TODO: put the correct value here (if createdibsection with file handle)
|
---|
133 |
|
---|
134 | if(iUsage == DIB_PAL_COLORS || pbmi->biBitCount <= 8)
|
---|
135 | {
|
---|
136 | dibinfo.dsBitfields[0] = dibinfo.dsBitfields[1] = dibinfo.dsBitfields[2] = 0;
|
---|
137 | }
|
---|
138 | else {
|
---|
139 | switch(pbmi->biBitCount)
|
---|
140 | {
|
---|
141 | case 16:
|
---|
142 | dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS) ? *(DWORD *)pColors : 0x7c00;
|
---|
143 | dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS) ? *((DWORD *)pColors + 1) : 0x03e0;
|
---|
144 | dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS) ? *((DWORD *)pColors + 2) : 0x001f;
|
---|
145 |
|
---|
146 | //double buffer for rgb 555 dib sections (for conversion)
|
---|
147 | if(dibinfo.dsBitfields[1] == 0x03e0) {
|
---|
148 | DosAllocMem((PPVOID)&bmpBitsRGB565, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
|
---|
149 | }
|
---|
150 | break;
|
---|
151 |
|
---|
152 | case 24:
|
---|
153 | dibinfo.dsBitfields[0] = 0xff;
|
---|
154 | dibinfo.dsBitfields[1] = 0xff00;
|
---|
155 | dibinfo.dsBitfields[2] = 0xff0000;
|
---|
156 | break;
|
---|
157 |
|
---|
158 | case 32:
|
---|
159 | dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS) ? *(DWORD *)pColors : 0xff;
|
---|
160 | dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS) ? *((DWORD *)pColors + 1) : 0xff00;
|
---|
161 | dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS) ? *((DWORD *)pColors + 2) : 0xff0000;
|
---|
162 | if(dibinfo.dsBitfields[0] != 0xff && dibinfo.dsBitfields[1] != 0xff00 && dibinfo.dsBitfields[2] != 0xff0000) {
|
---|
163 | dprintf(("DIBSection: unsupported bitfields for 32 bits bitmap!!"));
|
---|
164 | }
|
---|
165 | break;
|
---|
166 | }
|
---|
167 | dprintf(("BI_BITFIELDS %x %x %x", dibinfo.dsBitfields[0], dibinfo.dsBitfields[1], dibinfo.dsBitfields[2]));
|
---|
168 | }
|
---|
169 |
|
---|
170 | this->handle = handle;
|
---|
171 | this->iUsage = iUsage;
|
---|
172 |
|
---|
173 | dibMutex.enter();
|
---|
174 | if(section == NULL)
|
---|
175 | {
|
---|
176 | dprintf(("section was NULL\n"));
|
---|
177 | section = this;
|
---|
178 | }
|
---|
179 | else
|
---|
180 | {
|
---|
181 | DIBSection *dsect = section;
|
---|
182 | dprintf2(("Increment section starting at %08X\n",dsect));
|
---|
183 |
|
---|
184 | /* @@@PH 98/07/11 fix for dsect->next == NULL */
|
---|
185 | while ( (dsect->next != this) &&
|
---|
186 | (dsect->next != NULL) )
|
---|
187 | {
|
---|
188 | dprintf2(("Increment section to %08X\n",dsect->next));
|
---|
189 | dsect = dsect->next;
|
---|
190 | }
|
---|
191 | dsect->next = this;
|
---|
192 | }
|
---|
193 | dibMutex.leave();
|
---|
194 | }
|
---|
195 | //******************************************************************************
|
---|
196 | //******************************************************************************
|
---|
197 | DIBSection::~DIBSection()
|
---|
198 | {
|
---|
199 | dprintf(("Delete DIBSection %x", handle));
|
---|
200 |
|
---|
201 | if(hSection) {
|
---|
202 | UnmapViewOfFile(bmpBits);
|
---|
203 | }
|
---|
204 | else
|
---|
205 | if(bmpBits)
|
---|
206 | DosFreeMem(bmpBits);
|
---|
207 |
|
---|
208 | if(bmpBitsRGB565)
|
---|
209 | DosFreeMem(bmpBitsRGB565);
|
---|
210 |
|
---|
211 | if(pOS2bmp)
|
---|
212 | free(pOS2bmp);
|
---|
213 |
|
---|
214 | dibMutex.enter();
|
---|
215 | if(section == this)
|
---|
216 | {
|
---|
217 | section = this->next;
|
---|
218 | }
|
---|
219 | else
|
---|
220 | {
|
---|
221 | DIBSection *dsect = section;
|
---|
222 |
|
---|
223 | while(dsect->next != this)
|
---|
224 | {
|
---|
225 | dsect = dsect->next;
|
---|
226 | }
|
---|
227 | dsect->next = this->next;
|
---|
228 | }
|
---|
229 | dibMutex.leave();
|
---|
230 | }
|
---|
231 | //******************************************************************************
|
---|
232 | //******************************************************************************
|
---|
233 | int DIBSection::SetDIBits(HDC hdc, HBITMAP hbitmap, UINT startscan, UINT
|
---|
234 | lines, const VOID *bits, BITMAPINFOHEADER_W *pbmi,
|
---|
235 | UINT coloruse)
|
---|
236 | {
|
---|
237 | lines = (int)lines >= 0 ? (int)lines : (int)-lines;
|
---|
238 | int palsize=0;
|
---|
239 |
|
---|
240 | bmpsize = pbmi->biWidth;
|
---|
241 | os2bmphdrsize = sizeof(BITMAPINFO2);
|
---|
242 |
|
---|
243 | switch(pbmi->biBitCount)
|
---|
244 | {
|
---|
245 | case 1:
|
---|
246 | bmpsize = ((bmpsize + 31) & ~31) / 8;
|
---|
247 | palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
|
---|
248 | os2bmphdrsize += palsize;
|
---|
249 | break;
|
---|
250 | case 4:
|
---|
251 | bmpsize = ((bmpsize + 7) & ~7) / 2;
|
---|
252 | palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
|
---|
253 | os2bmphdrsize += palsize;
|
---|
254 | break;
|
---|
255 | case 8:
|
---|
256 | palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
|
---|
257 | os2bmphdrsize += palsize;
|
---|
258 | bmpsize = (bmpsize + 3) & ~3;
|
---|
259 | break;
|
---|
260 | case 16:
|
---|
261 | bmpsize *= 2;
|
---|
262 | bmpsize = (bmpsize + 3) & ~3;
|
---|
263 | break;
|
---|
264 | case 24:
|
---|
265 | bmpsize *= 3;
|
---|
266 | bmpsize = (bmpsize + 3) & ~3;
|
---|
267 | break;
|
---|
268 | case 32:
|
---|
269 | bmpsize *= 4;
|
---|
270 | break;
|
---|
271 | }
|
---|
272 |
|
---|
273 | //SvL: TODO: Correct??
|
---|
274 | if(!hSection && pOS2bmp->cx != pbmi->biWidth && pOS2bmp->cy != pbmi->biHeight &&
|
---|
275 | pOS2bmp->cBitCount != pbmi->biBitCount)
|
---|
276 | {
|
---|
277 | char *oldbits = bmpBits;
|
---|
278 | int oldsize = dibinfo.dsBm.bmWidthBytes * dibinfo.dsBm.bmHeight;
|
---|
279 |
|
---|
280 | DosAllocMem((PPVOID)&bmpBits, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
|
---|
281 | memcpy(bmpBits, oldbits, min(oldsize, bmpsize*pbmi->biHeight));
|
---|
282 | DosFreeMem(oldbits);
|
---|
283 | }
|
---|
284 | pOS2bmp = (BITMAPINFO2 *)realloc(pOS2bmp, os2bmphdrsize);
|
---|
285 | pOS2bmp->cbFix = sizeof(BITMAPINFO2) - sizeof(RGB2);
|
---|
286 | pOS2bmp->cx = pbmi->biWidth;
|
---|
287 | pOS2bmp->cy = pbmi->biHeight;
|
---|
288 | pOS2bmp->cPlanes = pbmi->biPlanes;
|
---|
289 | pOS2bmp->cBitCount = pbmi->biBitCount;
|
---|
290 | pOS2bmp->ulCompression = pbmi->biCompression;
|
---|
291 | pOS2bmp->cbImage = pbmi->biSizeImage;
|
---|
292 |
|
---|
293 | // clear DIBSECTION structure
|
---|
294 | memset(&dibinfo, 0, sizeof(dibinfo));
|
---|
295 |
|
---|
296 | // copy BITMAPINFOHEADER data into DIBSECTION structure
|
---|
297 | memcpy(&dibinfo.dsBmih, pbmi, sizeof(*pbmi));
|
---|
298 | dibinfo.dsBm.bmType = 0;
|
---|
299 | dibinfo.dsBm.bmWidth = pbmi->biWidth;
|
---|
300 | dibinfo.dsBm.bmHeight = pbmi->biHeight;
|
---|
301 | dibinfo.dsBm.bmWidthBytes= bmpsize;
|
---|
302 | dibinfo.dsBm.bmPlanes = pbmi->biPlanes;
|
---|
303 | dibinfo.dsBm.bmBitsPixel = pbmi->biBitCount;
|
---|
304 | dibinfo.dsBm.bmBits = bmpBits;
|
---|
305 |
|
---|
306 | dibinfo.dshSection = hSection;
|
---|
307 | dibinfo.dsOffset = 0; // TODO: put the correct value here (if createdibsection with file handle)
|
---|
308 |
|
---|
309 | if(coloruse == DIB_PAL_COLORS || pbmi->biBitCount <= 8)
|
---|
310 | {
|
---|
311 | dibinfo.dsBitfields[0] = dibinfo.dsBitfields[1] = dibinfo.dsBitfields[2] = 0;
|
---|
312 | }
|
---|
313 | else {
|
---|
314 | char *pColors = (char *)pbmi + 1;
|
---|
315 |
|
---|
316 | switch(pbmi->biBitCount)
|
---|
317 | {
|
---|
318 | case 16:
|
---|
319 | dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS) ? *(DWORD *)pColors : 0x7c00;
|
---|
320 | dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS) ? *((DWORD *)pColors + 1) : 0x03e0;
|
---|
321 | dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS) ? *((DWORD *)pColors + 2) : 0x001f;
|
---|
322 | if(dibinfo.dsBitfields[1] == 0x03e0) {
|
---|
323 | if(bmpBitsRGB565)
|
---|
324 | DosFreeMem(bmpBitsRGB565);
|
---|
325 | DosAllocMem((PPVOID)&bmpBitsRGB565, bmpsize*pbmi->biHeight, PAG_READ|PAG_WRITE|PAG_COMMIT);
|
---|
326 | }
|
---|
327 | break;
|
---|
328 |
|
---|
329 | case 24:
|
---|
330 | dibinfo.dsBitfields[0] = 0xff;
|
---|
331 | dibinfo.dsBitfields[1] = 0xff00;
|
---|
332 | dibinfo.dsBitfields[2] = 0xff0000;
|
---|
333 | break;
|
---|
334 |
|
---|
335 | case 32:
|
---|
336 | dibinfo.dsBitfields[0] = (pbmi->biCompression == BI_BITFIELDS) ? *(DWORD *)pColors : 0xff;
|
---|
337 | dibinfo.dsBitfields[1] = (pbmi->biCompression == BI_BITFIELDS) ? *((DWORD *)pColors + 1) : 0xff00;
|
---|
338 | dibinfo.dsBitfields[2] = (pbmi->biCompression == BI_BITFIELDS) ? *((DWORD *)pColors + 2) : 0xff0000;
|
---|
339 | if(dibinfo.dsBitfields[0] != 0xff && dibinfo.dsBitfields[1] != 0xff00 && dibinfo.dsBitfields[2] != 0xff0000) {
|
---|
340 | dprintf(("DIBSection: unsupported bitfields for 32 bits bitmap!!"));
|
---|
341 | }
|
---|
342 | break;
|
---|
343 | }
|
---|
344 | dprintf(("BI_BITFIELDS %x %x %x", dibinfo.dsBitfields[0], dibinfo.dsBitfields[1], dibinfo.dsBitfields[2]));
|
---|
345 | }
|
---|
346 |
|
---|
347 | dprintf(("DIBSection::SetDIBits (%d,%d), %d %d", pbmi->biWidth, pbmi->biHeight, pbmi->biBitCount, pbmi->biCompression));
|
---|
348 | if(palsize)
|
---|
349 | memcpy(pOS2bmp->argbColor, (char *)pbmi + 1 , palsize);
|
---|
350 |
|
---|
351 | if(bits)
|
---|
352 | {
|
---|
353 | int size = bmpsize*lines;
|
---|
354 | memcpy(bmpBits+bmpsize*startscan, bits, size);
|
---|
355 | }
|
---|
356 | return(lines);
|
---|
357 | }
|
---|
358 | //******************************************************************************
|
---|
359 | //******************************************************************************
|
---|
360 | int DIBSection::SetDIBColorTable(int startIdx, int cEntries, RGBQUAD *rgb)
|
---|
361 | {
|
---|
362 | int i;
|
---|
363 |
|
---|
364 | if(startIdx + cEntries > (1 << pOS2bmp->cBitCount))
|
---|
365 | {
|
---|
366 | dprintf(("DIBSection::SetDIBColorTable, invalid nr of entries %d %d\n", startIdx, cEntries));
|
---|
367 | return(0);
|
---|
368 | }
|
---|
369 |
|
---|
370 | memcpy(&pOS2bmp->argbColor[startIdx], rgb, cEntries*sizeof(RGB2));
|
---|
371 |
|
---|
372 | for(i=startIdx;i<cEntries;i++)
|
---|
373 | {
|
---|
374 | pOS2bmp->argbColor[i].fcOptions = 0;
|
---|
375 | dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&pOS2bmp->argbColor[i])) ));
|
---|
376 | }
|
---|
377 |
|
---|
378 | return(cEntries);
|
---|
379 | }
|
---|
380 | //******************************************************************************
|
---|
381 | //******************************************************************************
|
---|
382 | BOOL DIBSection::BitBlt(HDC hdcDest, int nXdest, int nYdest, int nDestWidth,
|
---|
383 | int nDestHeight, int nXsrc, int nYsrc,
|
---|
384 | int nSrcWidth, int nSrcHeight, DWORD Rop)
|
---|
385 | {
|
---|
386 | HPS hps = (HPS)hdcDest;
|
---|
387 | POINTL point[4];
|
---|
388 | LONG rc, hdcHeight, hdcWidth;
|
---|
389 | PVOID bitmapBits = NULL;
|
---|
390 | int oldyinversion = 0;
|
---|
391 | BOOL fRestoryYInversion = FALSE, fFrameWindowDC = FALSE;
|
---|
392 | HWND hwndDest;
|
---|
393 |
|
---|
394 | hwndDest = WindowFromDC(hdcDest);
|
---|
395 | //TODO: Test whether dc is for the client or frame window
|
---|
396 | // if(hwndDest && IsOS2FrameWindowHandle(hwndDest)) {
|
---|
397 | // fFrameWindowDC = TRUE;
|
---|
398 | // }
|
---|
399 |
|
---|
400 | dprintf(("DIBSection::BitBlt %x %X (hps %x) %x to(%d,%d)(%d,%d) from (%d,%d)(%d,%d) rop %x flip %x",
|
---|
401 | handle, hdcDest, hps, hwndDest, nXdest, nYdest, nDestWidth, nDestHeight,
|
---|
402 | nXsrc, nYsrc, nSrcWidth, nSrcHeight, Rop, fFlip));
|
---|
403 |
|
---|
404 | if(hwndDest) {
|
---|
405 | RECT rect;
|
---|
406 |
|
---|
407 | if(fFrameWindowDC) {
|
---|
408 | GetWindowRect(hwndDest, &rect);
|
---|
409 | }
|
---|
410 | else GetClientRect(hwndDest, &rect);
|
---|
411 | hdcHeight = rect.bottom - rect.top;
|
---|
412 | hdcWidth = rect.right - rect.left;
|
---|
413 | }
|
---|
414 | else {
|
---|
415 | hdcHeight = pOS2bmp->cy;
|
---|
416 | hdcWidth = pOS2bmp->cx;
|
---|
417 | }
|
---|
418 |
|
---|
419 | //win32 coordinates are of the left top, OS/2 expects left bottom
|
---|
420 | //source rectangle is non-inclusive (top, right not included)
|
---|
421 | if(nXdest + nDestWidth > hdcWidth) {
|
---|
422 | nDestWidth = hdcWidth - nXdest;
|
---|
423 | }
|
---|
424 | if(nYdest + nDestHeight > hdcHeight) {
|
---|
425 | nDestHeight = hdcHeight - nYdest;
|
---|
426 | }
|
---|
427 | point[0].x = nXdest;
|
---|
428 | point[0].y = hdcHeight - nYdest - nDestHeight;
|
---|
429 | point[1].x = nXdest + nDestWidth;
|
---|
430 | point[1].y = hdcHeight - nYdest;
|
---|
431 |
|
---|
432 | //target rectangle is inclusive-inclusive
|
---|
433 | if(nXsrc + nSrcWidth > pOS2bmp->cx) {
|
---|
434 | nSrcWidth = pOS2bmp->cx - nXsrc;
|
---|
435 | }
|
---|
436 | if(nYsrc + nSrcHeight > pOS2bmp->cy) {
|
---|
437 | nSrcHeight = pOS2bmp->cy - nYsrc;
|
---|
438 | }
|
---|
439 | point[2].x = nXsrc;
|
---|
440 | point[2].y = pOS2bmp->cy - nYsrc - nSrcHeight;
|
---|
441 | point[3].x = nXsrc + nSrcWidth;
|
---|
442 | point[3].y = pOS2bmp->cy - nYsrc;
|
---|
443 |
|
---|
444 | oldyinversion = GpiQueryYInversion(hps);
|
---|
445 | if(fFlip & FLIP_VERT)
|
---|
446 | {
|
---|
447 | if(oldyinversion != hdcHeight-1) {
|
---|
448 | GpiEnableYInversion(hps, hdcHeight-1);
|
---|
449 | fRestoryYInversion = TRUE;
|
---|
450 | }
|
---|
451 | }
|
---|
452 | else
|
---|
453 | if(oldyinversion != 0) {
|
---|
454 | GpiEnableYInversion(hps, 0);
|
---|
455 | fRestoryYInversion = TRUE;
|
---|
456 | }
|
---|
457 |
|
---|
458 | if(fFlip & FLIP_HOR)
|
---|
459 | {
|
---|
460 | ULONG x;
|
---|
461 | x = point[0].x;
|
---|
462 | point[0].x = point[1].x;
|
---|
463 | point[1].x = x;
|
---|
464 | }
|
---|
465 |
|
---|
466 | //SvL: Optimize this.. (don't convert entire bitmap if only a part will be blitted to the dc)
|
---|
467 | if(dibinfo.dsBitfields[1] == 0x3E0) {//RGB 555?
|
---|
468 | dprintf(("DIBSection::BitBlt; convert rgb 555 to 565 (old y inv. = %d)", oldyinversion));
|
---|
469 |
|
---|
470 | if(bmpBitsRGB565 == NULL)
|
---|
471 | DebugInt3();
|
---|
472 |
|
---|
473 | if(CPUFeatures & CPUID_MMX) {
|
---|
474 | RGB555to565MMX((WORD *)bmpBitsRGB565, (WORD *)bmpBits, pOS2bmp->cbImage/sizeof(WORD));
|
---|
475 | }
|
---|
476 | else RGB555to565((WORD *)bmpBitsRGB565, (WORD *)bmpBits, pOS2bmp->cbImage/sizeof(WORD));
|
---|
477 | rc = GpiDrawBits(hps, bmpBitsRGB565, pOS2bmp, 4, &point[0], ROP_SRCCOPY, BBO_OR);
|
---|
478 | }
|
---|
479 | else rc = GpiDrawBits(hps, bmpBits, pOS2bmp, 4, &point[0], ROP_SRCCOPY, BBO_OR);
|
---|
480 |
|
---|
481 | if(rc == GPI_OK) {
|
---|
482 | DIBSection *destdib = DIBSection::findHDC(hdcDest);
|
---|
483 | if(destdib) {
|
---|
484 | destdib->sync(hps, nYdest, nDestHeight);
|
---|
485 | }
|
---|
486 | //restore old y inversion height
|
---|
487 | if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion);
|
---|
488 | return(TRUE);
|
---|
489 | }
|
---|
490 | if(fRestoryYInversion) GpiEnableYInversion(hps, oldyinversion);
|
---|
491 |
|
---|
492 | 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));
|
---|
493 | dprintf(("WinGetLastError returned %X\n", WinGetLastError(WinQueryAnchorBlock(hwndDest)) & 0xFFFF));
|
---|
494 | return(FALSE);
|
---|
495 | }
|
---|
496 | //******************************************************************************
|
---|
497 | //******************************************************************************
|
---|
498 | void DIBSection::sync(HDC hdc, DWORD nYdest, DWORD nDestHeight)
|
---|
499 | {
|
---|
500 | APIRET rc;
|
---|
501 | char *destBuf;
|
---|
502 |
|
---|
503 | dprintf(("Sync destination dibsection %x (%x)", handle, hdc));
|
---|
504 |
|
---|
505 | //todo: rgb 565 to 555 conversion if bpp == 16
|
---|
506 | if(GetBitCount() == 16) {
|
---|
507 | dprintf(("WARNING: need to convert RGB 565 to RGB 555!!"));
|
---|
508 | }
|
---|
509 |
|
---|
510 | BITMAPINFO2 *tmphdr = (BITMAPINFO2 *)malloc(os2bmphdrsize);
|
---|
511 | memcpy(tmphdr, pOS2bmp, os2bmphdrsize);
|
---|
512 | destBuf = GetDIBObject() + nYdest*dibinfo.dsBm.bmWidthBytes;
|
---|
513 | rc = GpiQueryBitmapBits(hdc, nYdest, nDestHeight, destBuf,
|
---|
514 | tmphdr);
|
---|
515 | free(tmphdr);
|
---|
516 | if(rc != nDestHeight) {
|
---|
517 | DebugInt3();
|
---|
518 | }
|
---|
519 | }
|
---|
520 | //******************************************************************************
|
---|
521 | //******************************************************************************
|
---|
522 | void DIBSection::SelectDIBObject(HDC hdc)
|
---|
523 | {
|
---|
524 | this->hdc = hdc;
|
---|
525 | hwndParent = WindowFromDC(hdc);
|
---|
526 | dprintf(("SelectDIBObject %x into %x hwndParent = %x", handle, hdc, hwndParent));
|
---|
527 | }
|
---|
528 | //******************************************************************************
|
---|
529 | //******************************************************************************
|
---|
530 | DIBSection *DIBSection::find(DWORD handle)
|
---|
531 | {
|
---|
532 | DIBSection *dsect = section;
|
---|
533 |
|
---|
534 | dibMutex.enter();
|
---|
535 | while(dsect)
|
---|
536 | {
|
---|
537 | if(dsect->handle == handle)
|
---|
538 | {
|
---|
539 | dibMutex.leave();
|
---|
540 | return(dsect);
|
---|
541 | }
|
---|
542 | dsect = dsect->next;
|
---|
543 | }
|
---|
544 | dibMutex.leave();
|
---|
545 | return(NULL);
|
---|
546 | }
|
---|
547 | //******************************************************************************
|
---|
548 | //A bitmap can only be selected into one DC, so this works.
|
---|
549 | //******************************************************************************
|
---|
550 | DIBSection *DIBSection::findHDC(HDC hdc)
|
---|
551 | {
|
---|
552 | DIBSection *dsect = section;
|
---|
553 |
|
---|
554 | while(dsect)
|
---|
555 | {
|
---|
556 | if(dsect->hdc == hdc)
|
---|
557 | {
|
---|
558 | return(dsect);
|
---|
559 | }
|
---|
560 | dsect = dsect->next;
|
---|
561 | }
|
---|
562 | return(NULL);
|
---|
563 | }
|
---|
564 | //******************************************************************************
|
---|
565 | //******************************************************************************
|
---|
566 | void DIBSection::deleteSection(DWORD handle)
|
---|
567 | {
|
---|
568 | DIBSection *dsect = find(handle);
|
---|
569 |
|
---|
570 | if(dsect)
|
---|
571 | delete dsect;
|
---|
572 |
|
---|
573 | }
|
---|
574 | //******************************************************************************
|
---|
575 | //******************************************************************************
|
---|
576 | int DIBSection::GetDIBSection(int iSize, void *lpBuffer)
|
---|
577 | {
|
---|
578 | DIBSECTION *pDIBSection = (DIBSECTION *)lpBuffer;
|
---|
579 | LPBITMAP_W dsBm = (LPBITMAP_W)lpBuffer;
|
---|
580 |
|
---|
581 | dprintf2(("GetDIBSection %x %d %x", handle, iSize, lpBuffer));
|
---|
582 | if(iSize == sizeof(DIBSECTION))
|
---|
583 | {
|
---|
584 | memcpy(pDIBSection, &dibinfo, sizeof(dibinfo));
|
---|
585 | return sizeof(DIBSECTION);
|
---|
586 | }
|
---|
587 | else
|
---|
588 | if(iSize == sizeof(BITMAP_W))
|
---|
589 | {
|
---|
590 | memcpy(dsBm, &dibinfo.dsBm, sizeof(dibinfo.dsBm));
|
---|
591 | return sizeof(BITMAP_W);
|
---|
592 | }
|
---|
593 | return 0;
|
---|
594 |
|
---|
595 | }
|
---|
596 | //******************************************************************************
|
---|
597 | //******************************************************************************
|
---|
598 | char DIBSection::GetBitCount()
|
---|
599 | {
|
---|
600 | if(pOS2bmp == NULL)
|
---|
601 | return 0;
|
---|
602 | else
|
---|
603 | return pOS2bmp->cBitCount;
|
---|
604 | }
|
---|
605 | //******************************************************************************
|
---|
606 | //******************************************************************************
|
---|
607 | DIBSection *DIBSection::section = NULL;
|
---|