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

Last change on this file since 1533 was 1533, checked in by achimha, 26 years ago

Added DIB changes from Markus Montkowski to return initalized DIB structure + various bug fixes in DIB handling

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