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

Last change on this file since 2802 was 2802, checked in by sandervl, 26 years ago

Added new logging feature

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