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

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

init dibsection bmp data to 0 during creation

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