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

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

CreateDIBSection enhancement + CreateICA/W bugfix

File size: 12.7 KB
Line 
1/* $Id: dibsect.cpp,v 1.17 2000-02-10 00:36: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 <winconst.h>
26#include <win32wnd.h>
27#include "oslibgpi.h"
28
29//Win32 apis used:
30HWND WIN32API WindowFromDC(HDC hdc);
31BOOL WINAPI UnmapViewOfFile(LPVOID addr);
32LPVOID WINAPI MapViewOfFile(HANDLE mapping, DWORD access, DWORD offset_high,
33 DWORD offset_low, DWORD count);
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 break;
63 case 16:
64 bmpsize *= 2;
65 break;
66 case 24:
67 bmpsize *= 3;
68 break;
69 case 32:
70 bmpsize *= 4;
71 break;
72 }
73 if(bmpsize & 3) {
74 bmpsize = (bmpsize + 3) & ~3;
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 break;
226 case 16:
227 bmpsize *= 2;
228 break;
229 case 24:
230 bmpsize *= 3;
231 break;
232 case 32:
233 bmpsize *= 4;
234 break;
235 }
236
237 if(bmpsize & 3)
238 {
239 bmpsize = (bmpsize + 3) & ~3;
240 }
241
242 bmpBits = (char *)realloc(bmpBits, bmpsize*pbmi->biHeight);
243 pOS2bmp = (BITMAPINFO2 *)realloc(pOS2bmp, os2bmpsize);
244
245 pOS2bmp->cbFix = sizeof(BITMAPINFO2) - sizeof(RGB2);
246 pOS2bmp->cx = pbmi->biWidth;
247 pOS2bmp->cy = pbmi->biHeight;
248 pOS2bmp->cPlanes = pbmi->biPlanes;
249 pOS2bmp->cBitCount = pbmi->biBitCount;
250 pOS2bmp->ulCompression = pbmi->biCompression;
251 pOS2bmp->cbImage = pbmi->biSizeImage;
252
253 if(palsize)
254 memcpy(pOS2bmp->argbColor, (char *)pbmi + 1 , palsize);
255
256 if(bits)
257 {
258 int size = bmpsize*lines;
259 memcpy(bmpBits+bmpsize*startscan, bits, size);
260 }
261 return(lines);
262}
263//******************************************************************************
264//******************************************************************************
265int DIBSection::SetDIBColorTable(int startIdx, int cEntries, RGBQUAD *rgb)
266{
267 int i;
268
269 if(startIdx + cEntries > (1 << pOS2bmp->cBitCount))
270 {
271 dprintf(("DIBSection::SetDIBColorTable, invalid nr of entries %d %d\n", startIdx, cEntries));
272 return(0);
273 }
274
275 memcpy(&pOS2bmp->argbColor[startIdx], rgb, cEntries*sizeof(RGB2));
276
277 for(i=startIdx;i<cEntries;i++)
278 {
279 pOS2bmp->argbColor[i].fcOptions = 0;
280 dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&pOS2bmp->argbColor[i])) ));
281 }
282
283 return(cEntries);
284}
285//******************************************************************************
286//******************************************************************************
287BOOL DIBSection::BitBlt(HDC hdcDest, int nXdest, int nYdest, int nDestWidth,
288 int nDestHeight, int nXsrc, int nYsrc,
289 int nSrcWidth, int nSrcHeight, DWORD Rop)
290{
291 HPS hps = (HPS)hdcDest;
292 POINTL point[4];
293 LONG rc;
294
295 HWND hwndDest = WindowFromDC(hdcDest);
296 hwndDest = Win32ToOS2Handle(hwndDest);
297 if(hwndDest != 0)
298 {
299 hps = WinGetPS(hwndDest);
300 }
301 if(hps == 0)
302 {
303 dprintf(("ERROR: DIBSection::BitBlt, hps == 0 hwndDest = %X", hwndDest));
304 return(FALSE);
305 }
306
307 dprintf(("DIBSection::BitBlt %x %X (hps %x) %x to(%d,%d)(%d,%d) from (%d,%d)(%d,%d) rop %x flip %x",
308 handle, hdcDest, hps, hwndDest, nXdest, nYdest, nDestWidth, nDestHeight,
309 nXsrc, nYsrc, nSrcWidth, nSrcHeight, Rop, fFlip));
310
311 point[0].x = nXdest;
312 point[0].y = nYdest;
313 point[1].x = nXdest + nDestWidth - 1;
314 point[1].y = nYdest + nDestHeight - 1;
315 point[2].x = nXsrc;
316 point[2].y = nYsrc;
317 if(nXsrc + nSrcWidth > pOS2bmp->cx)
318 {
319 point[3].x = pOS2bmp->cx;
320 }
321 else
322 point[3].x = nXsrc + nSrcWidth;
323
324 if(nYsrc + nSrcHeight > pOS2bmp->cy)
325 {
326 point[3].y = pOS2bmp->cy;
327 }
328 else
329 point[3].y = nYsrc + nSrcHeight;
330
331#if 1
332 if(fFlip & FLIP_VERT)
333 {
334 GpiEnableYInversion(hps, nDestHeight);
335 }
336
337 if(fFlip & FLIP_HOR)
338 {
339 ULONG x;
340 x = point[0].x;
341 point[0].x = point[1].x;
342 point[1].x = x;
343 }
344#endif
345
346 rc = GpiDrawBits(hps, bmpBits, pOS2bmp, 4, &point[0], ROP_SRCCOPY, BBO_OR);
347
348 if(hwndDest != 0)
349 {
350 WinReleasePS(hps);
351 }
352 if(rc == GPI_OK) {
353 return(TRUE);
354 }
355 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));
356 dprintf(("WinGetLastError returned %X\n", WinGetLastError(WinQueryAnchorBlock(hwndDest)) & 0xFFFF));
357 return(FALSE);
358}
359//******************************************************************************
360//******************************************************************************
361void DIBSection::SelectDIBObject(HDC hdc)
362{
363 this->hdc = hdc;
364 hwndParent = WinWindowFromDC(hdc);
365 dprintf(("SelectDIBObject %x into %x hwndParent = %x", handle, hdc, hwndParent));
366}
367//******************************************************************************
368//******************************************************************************
369DIBSection *DIBSection::find(DWORD handle)
370{
371 DIBSection *dsect = section;
372
373 dibMutex.enter();
374 while(dsect)
375 {
376 if(dsect->handle == handle)
377 {
378 dibMutex.leave();
379 return(dsect);
380 }
381 dsect = dsect->next;
382 }
383 dibMutex.leave();
384 return(NULL);
385}
386//******************************************************************************
387//A bitmap can only be selected into one DC, so this works.
388//******************************************************************************
389DIBSection *DIBSection::findHDC(HDC hdc)
390{
391 DIBSection *dsect = section;
392
393 while(dsect)
394 {
395 if(dsect->hdc == hdc)
396 {
397 return(dsect);
398 }
399 dsect = dsect->next;
400 }
401 return(NULL);
402}
403//******************************************************************************
404//******************************************************************************
405void DIBSection::deleteSection(DWORD handle)
406{
407 DIBSection *dsect = find(handle);
408
409 if(dsect)
410 delete dsect;
411
412}
413//******************************************************************************
414//******************************************************************************
415int DIBSection::GetDIBSection(int iSize, void *lpBuffer)
416{
417 DIBSECTION *pDIBSection = (DIBSECTION *)lpBuffer;
418 LPBITMAP_W dsBm = (LPBITMAP_W)lpBuffer;
419
420 dprintf2(("GetDIBSection %x %d %x", handle, iSize, lpBuffer));
421 if(iSize == sizeof(DIBSECTION))
422 {
423 memcpy(pDIBSection, &dibinfo, sizeof(dibinfo));
424 return sizeof(DIBSECTION);
425 }
426 else
427 if(iSize == sizeof(BITMAP_W))
428 {
429 memcpy(dsBm, &dibinfo.dsBm, sizeof(dibinfo.dsBm));
430 return sizeof(BITMAP_W);
431 }
432 return 0;
433
434}
435//******************************************************************************
436//******************************************************************************
437char DIBSection::GetBitCount()
438{
439 if(pOS2bmp == NULL)
440 return 0;
441 else
442 return pOS2bmp->cBitCount;
443}
444//******************************************************************************
445//******************************************************************************
446DIBSection *DIBSection::section = NULL;
Note: See TracBrowser for help on using the repository browser.