source: trunk/src/gdi32/dibsect.cpp@ 3227

Last change on this file since 3227 was 3227, checked in by sandervl, 25 years ago

default to rgb555 for 16 bits dib sections

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