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

Last change on this file since 1966 was 1966, checked in by hugh, 26 years ago

Implemented DEFAULT_GUI_FONT for GetStockObject
DIBSecttion now get the DC Palette on creation and updated if DC pal changes.

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