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

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

added rgb555 conversion to dibsection blits

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