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

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

cleaned up + dibsection fixes

File size: 12.8 KB
Line 
1/* $Id: dibsect.cpp,v 1.14 2000-02-01 12:53:29 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 iUsage, 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 //SvL: Ignore BI_BITFIELDS type (GpiDrawBits fails otherwise)
99 if(pOS2bmp->ulCompression == 3) {
100 pOS2bmp->ulCompression = 0;
101 }
102 pOS2bmp->cbImage = pbmi->biSizeImage;
103 dprintf(("handle %x", handle));
104 dprintf(("pOS2bmp->cx %d\n", pOS2bmp->cx));
105 dprintf(("pOS2bmp->cy %d\n", pOS2bmp->cy));
106 dprintf(("pOS2bmp->cPlanes %d\n", pOS2bmp->cPlanes));
107 dprintf(("pOS2bmp->cBitCount %d\n", pOS2bmp->cBitCount));
108 dprintf(("pOS2bmp->ulCompression %d\n", pOS2bmp->ulCompression));
109 dprintf(("pOS2bmp->cbImage %d\n", pOS2bmp->cbImage));
110 dprintf(("Bits at %x, size %d",bmpBits, bmpsize*pbmi->biHeight));
111
112 this->handle = handle;
113 this->iUsage = iUsage;
114
115 dibMutex.enter();
116 if(section == NULL)
117 {
118 dprintf(("section was NULL\n"));
119 section = this;
120 }
121 else
122 {
123 DIBSection *dsect = section;
124 dprintf2(("Increment section starting at %08X\n",dsect));
125
126 /* @@@PH 98/07/11 fix for dsect->next == NULL */
127 while ( (dsect->next != this) &&
128 (dsect->next != NULL) )
129 {
130 dprintf2(("Increment section to %08X\n",dsect->next));
131 dsect = dsect->next;
132 }
133 dsect->next = this;
134 }
135 dibMutex.leave();
136}
137//******************************************************************************
138//******************************************************************************
139DIBSection::~DIBSection()
140{
141 dprintf(("Delete DIBSection %x", handle));
142 if(bmpBits)
143 free(bmpBits);
144 if(pOS2bmp)
145 free(pOS2bmp);
146
147 dibMutex.enter();
148 if(section == this)
149 {
150 section = this->next;
151 }
152 else
153 {
154 DIBSection *dsect = section;
155
156 while(dsect->next != this)
157 {
158 dsect = dsect->next;
159 }
160 dsect->next = this->next;
161 }
162 dibMutex.leave();
163}
164//******************************************************************************
165//******************************************************************************
166int DIBSection::SetDIBits(HDC hdc, HBITMAP hbitmap, UINT startscan, UINT
167 lines, const VOID *bits, WINBITMAPINFOHEADER *pbmi,
168 UINT coloruse)
169{
170 lines = (int)lines >= 0 ? (int)lines : (int)-lines;
171 int os2bmpsize;
172 int palsize=0;
173
174 bmpsize = pbmi->biWidth;
175 os2bmpsize = sizeof(BITMAPINFO2);
176
177 switch(pbmi->biBitCount)
178 {
179 case 1:
180 bmpsize /= 8;
181 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
182 os2bmpsize += palsize;
183 break;
184 case 4:
185 bmpsize /= 2;
186 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
187 os2bmpsize += palsize;
188 break;
189 case 8:
190 palsize = ((1 << pbmi->biBitCount))*sizeof(RGB2);
191 os2bmpsize += palsize;
192 break;
193 case 16:
194 bmpsize *= 2;
195 break;
196 case 24:
197 bmpsize *= 3;
198 break;
199 case 32:
200 bmpsize *= 4;
201 break;
202 }
203
204 if(bmpsize & 3)
205 {
206 bmpsize = (bmpsize + 3) & ~3;
207 }
208
209 bmpBits = (char *)realloc(bmpBits, bmpsize*pbmi->biHeight);
210 pOS2bmp = (BITMAPINFO2 *)realloc(pOS2bmp, os2bmpsize);
211
212 pOS2bmp->cbFix = sizeof(BITMAPINFO2) - sizeof(RGB2);
213 pOS2bmp->cx = pbmi->biWidth;
214 pOS2bmp->cy = pbmi->biHeight;
215 pOS2bmp->cPlanes = pbmi->biPlanes;
216 pOS2bmp->cBitCount = pbmi->biBitCount;
217 pOS2bmp->ulCompression = pbmi->biCompression;
218 pOS2bmp->cbImage = pbmi->biSizeImage;
219
220 if(palsize)
221 memcpy(pOS2bmp->argbColor, (char *)pbmi + 1 , palsize);
222
223 if(bits)
224 {
225 int size = bmpsize*lines;
226 memcpy(bmpBits+bmpsize*startscan, bits, size);
227 }
228 return(lines);
229}
230//******************************************************************************
231//******************************************************************************
232int DIBSection::SetDIBColorTable(int startIdx, int cEntries, RGBQUAD *rgb)
233{
234 int i;
235
236 if(startIdx + cEntries > (1 << pOS2bmp->cBitCount))
237 {
238 dprintf(("DIBSection::SetDIBColorTable, invalid nr of entries %d %d\n", startIdx, cEntries));
239 return(0);
240 }
241
242 memcpy(&pOS2bmp->argbColor[startIdx], rgb, cEntries*sizeof(RGB2));
243
244 for(i=startIdx;i<cEntries;i++)
245 {
246 pOS2bmp->argbColor[i].fcOptions = 0;
247 dprintf2(("Index %d : 0x%08X\n",i, *((ULONG*)(&pOS2bmp->argbColor[i])) ));
248 }
249
250 return(cEntries);
251}
252//******************************************************************************
253//******************************************************************************
254BOOL DIBSection::BitBlt(HDC hdcDest, int nXdest, int nYdest, int nDestWidth,
255 int nDestHeight, int nXsrc, int nYsrc,
256 int nSrcWidth, int nSrcHeight, DWORD Rop)
257{
258 HPS hps = (HPS)hdcDest;
259 POINTL point[4];
260 LONG rc;
261
262 HWND hwndDest = WindowFromDC(hdcDest);
263 hwndDest = Win32ToOS2Handle(hwndDest);
264 if(hwndDest != 0)
265 {
266 hps = WinGetPS(hwndDest);
267 }
268 if(hps == 0)
269 {
270 dprintf(("ERROR: DIBSection::BitBlt, hps == 0 hwndDest = %X", hwndDest));
271 return(FALSE);
272 }
273
274 dprintf(("DIBSection::BitBlt %x %X (hps %x) %x to(%d,%d)(%d,%d) from (%d,%d)(%d,%d) rop %x flip %x",
275 handle, hdcDest, hps, hwndDest, nXdest, nYdest, nDestWidth, nDestHeight,
276 nXsrc, nYsrc, nSrcWidth, nSrcHeight, Rop, fFlip));
277
278 point[0].x = nXdest;
279 point[0].y = nYdest;
280 point[1].x = nXdest + nDestWidth - 1;
281 point[1].y = nYdest + nDestHeight - 1;
282 point[2].x = nXsrc;
283 point[2].y = nYsrc;
284 if(nXsrc + nSrcWidth > pOS2bmp->cx)
285 {
286 point[3].x = pOS2bmp->cx;
287 }
288 else
289 point[3].x = nXsrc + nSrcWidth;
290
291 if(nYsrc + nSrcHeight > pOS2bmp->cy)
292 {
293 point[3].y = pOS2bmp->cy;
294 }
295 else
296 point[3].y = nYsrc + nSrcHeight;
297
298#if 1
299 if(fFlip & FLIP_VERT)
300 {
301 GpiEnableYInversion(hps, nDestHeight);
302 }
303
304 if(fFlip & FLIP_HOR)
305 {
306 ULONG x;
307 x = point[0].x;
308 point[0].x = point[1].x;
309 point[1].x = x;
310 }
311#endif
312
313 rc = GpiDrawBits(hps, bmpBits, pOS2bmp, 4, &point[0], ROP_SRCCOPY, BBO_OR);
314
315 if(hwndDest != 0)
316 {
317 WinReleasePS(hps);
318 }
319 if(rc == GPI_OK) {
320 return(TRUE);
321 }
322 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));
323 dprintf(("WinGetLastError returned %X\n", WinGetLastError(WinQueryAnchorBlock(hwndDest)) & 0xFFFF));
324 return(FALSE);
325}
326//******************************************************************************
327//******************************************************************************
328void DIBSection::SelectDIBObject(HDC hdc)
329{
330 this->hdc = hdc;
331 hwndParent = WinWindowFromDC(hdc);
332 dprintf(("SelectDIBObject %x into %x hwndParent = %x", handle, hdc, hwndParent));
333}
334//******************************************************************************
335//******************************************************************************
336DIBSection *DIBSection::find(DWORD handle)
337{
338 DIBSection *dsect = section;
339
340 dibMutex.enter();
341 while(dsect)
342 {
343 if(dsect->handle == handle)
344 {
345 dibMutex.leave();
346 return(dsect);
347 }
348 dsect = dsect->next;
349 }
350 dibMutex.leave();
351 return(NULL);
352}
353//******************************************************************************
354//A bitmap can only be selected into one DC, so this works.
355//******************************************************************************
356DIBSection *DIBSection::findHDC(HDC hdc)
357{
358 DIBSection *dsect = section;
359
360 while(dsect)
361 {
362 if(dsect->hdc == hdc)
363 {
364 return(dsect);
365 }
366 dsect = dsect->next;
367 }
368 return(NULL);
369}
370//******************************************************************************
371//******************************************************************************
372void DIBSection::deleteSection(DWORD handle)
373{
374 DIBSection *dsect = find(handle);
375
376 if(dsect)
377 delete dsect;
378
379}
380//******************************************************************************
381//******************************************************************************
382int DIBSection::GetDIBSection(int iSize, void *lpBuffer)
383{
384 DIBSECTION *pDIBSection = (DIBSECTION *)lpBuffer;
385 LPWINBITMAP dsBm = (LPWINBITMAP)lpBuffer;
386
387 dprintf2(("GetDIBSection %x %d %x", handle, iSize, lpBuffer));
388 if(iSize == sizeof(DIBSECTION))
389 {
390 // BITMAP struct
391 pDIBSection->dsBm.bmType = 0; // TODO: put the correct value here
392 pDIBSection->dsBm.bmWidth = pOS2bmp->cx;
393 pDIBSection->dsBm.bmHeight = pOS2bmp->cy;
394 pDIBSection->dsBm.bmWidthBytes = bmpsize;
395 pDIBSection->dsBm.bmPlanes = pOS2bmp->cPlanes;
396 pDIBSection->dsBm.bmBitsPixel = pOS2bmp->cBitCount;
397 pDIBSection->dsBm.bmBits = bmpBits;
398 // BITMAPINFOHEADER data
399 pDIBSection->dsBmih.biSize = sizeof(BITMAPINFOHEADER);
400 pDIBSection->dsBmih.biWidth = pOS2bmp->cx;
401 pDIBSection->dsBmih.biHeight = pOS2bmp->cy;
402 pDIBSection->dsBmih.biPlanes = pOS2bmp->cPlanes;
403 pDIBSection->dsBmih.biBitCount = pOS2bmp->cBitCount;
404 pDIBSection->dsBmih.biCompression = pOS2bmp->ulCompression;
405 pDIBSection->dsBmih.biSizeImage = pOS2bmp->cbImage;
406 pDIBSection->dsBmih.biXPelsPerMeter = 0; // TODO: put the correct value here
407 pDIBSection->dsBmih.biYPelsPerMeter = 0;
408 pDIBSection->dsBmih.biClrUsed = (1<< pOS2bmp->cBitCount);
409 pDIBSection->dsBmih.biClrImportant = 0;
410
411 pDIBSection->dsBitfields[0] = 0; // TODO: put the correct value here
412 pDIBSection->dsBitfields[1] = 0;
413 pDIBSection->dsBitfields[2] = 0;
414
415 pDIBSection->dshSection = this->handle;
416
417 pDIBSection->dsOffset = 0; // TODO: put the correct value here
418
419 return sizeof(DIBSECTION);
420 }
421 else
422 if(iSize == sizeof(WINBITMAP))
423 {
424 dsBm->bmType = 0; // TODO: put the correct value here
425 dsBm->bmWidth = pOS2bmp->cx;
426 dsBm->bmHeight = pOS2bmp->cy;
427 dsBm->bmWidthBytes = bmpsize;
428 dsBm->bmPlanes = pOS2bmp->cPlanes;
429 dsBm->bmBitsPixel = pOS2bmp->cBitCount;
430 dsBm->bmBits = bmpBits;
431 return sizeof(WINBITMAP);
432 }
433 return 0;
434
435}
436//******************************************************************************
437//******************************************************************************
438char DIBSection::GetBitCount()
439{
440 if(pOS2bmp == NULL)
441 return 0;
442 else
443 return pOS2bmp->cBitCount;
444}
445//******************************************************************************
446//******************************************************************************
447DIBSection *DIBSection::section = NULL;
Note: See TracBrowser for help on using the repository browser.