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

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

EB's dibsection updates

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