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

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

removed flipping in dibsection

File size: 10.7 KB
Line 
1/* $Id: dibsect.cpp,v 1.7 1999-11-10 14:15:23 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
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, int nXdest, int nYdest, int nWidth,
151 int nHeight, int nXsrc, int nYsrc, DWORD Rop)
152{
153 HWND hwndDest = WinWindowFromDC(hdcDest);
154 HPS hps = (HPS)hdcDest;
155 POINTL point[4];
156 LONG rc;
157
158 dprintf(("DIBSection::BitBlt %X %x (%d,%d) to (%d,%d) (%d,%d) rop %x\n", hdcDest, hwndDest, nXdest, nYdest, nWidth, nHeight, nXsrc, nYsrc, Rop));
159
160 if(hwndDest != 0) {
161 hps = WinGetPS(hwndDest);
162 }
163 if(hps == 0) {
164 eprintf(("DIBSection::BitBlt, hps == 0 hwndDest = %X", hwndDest));
165 return(FALSE);
166 }
167
168 point[0].x = nXdest;
169 point[0].y = nYdest;
170 point[1].x = nXdest + nWidth - 1;
171 point[1].y = nYdest + nHeight - 1;
172 point[2].x = nXsrc;
173 point[2].y = nYsrc;
174 if(nXsrc + nWidth > pOS2bmp->cx) {
175 point[3].x = pOS2bmp->cx;
176 }
177 else point[3].x = nXsrc + nWidth;
178
179 if(nYsrc + nHeight > pOS2bmp->cy) {
180 point[3].y = pOS2bmp->cy;
181 }
182 else point[3].y = nYsrc + nHeight;
183
184#if 0
185 if(fFlip & FLIP_VERT) {
186 ULONG y;
187 y = point[0].y;
188 point[0].y = point[1].y;
189 point[1].y = y;
190 }
191
192 if(fFlip & FLIP_HOR) {
193 ULONG x;
194 x = point[0].x;
195 point[0].x = point[1].x;
196 point[1].x = x;
197 }
198#endif
199
200 rc = GpiDrawBits(hps, bmpBits, pOS2bmp, 4, &point[0], ROP_SRCCOPY, BBO_OR);
201
202 if(hwndDest != 0) {
203 WinReleasePS(hps);
204 }
205 if(rc == GPI_OK)
206 return(TRUE);
207 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));
208 dprintf(("WinGetLastError returned %X\n", WinGetLastError(WinQueryAnchorBlock(hwndDest)) & 0xFFFF));
209 return(FALSE);
210}
211#else
212BOOL DIBSection::BitBlt(HDC hdcDest, int nXdest, int nYdest, int nWidth,
213 int nHeight, int nXsrc, int nYsrc, DWORD Rop)
214{
215 HPS hps = (HPS)hdcDest;
216 POINTL point[4];
217 LONG rc;
218
219 if(hps == 0) {
220 eprintf(("DIBSection::BitBlt, hps == 0"));
221 return(FALSE);
222 }
223
224// dprintf(("DIBSection::BitBlt (%d,%d) to (%d,%d) (%d,%d)\n", nXsrc, nYsrc, nXdest, nYdest, nXdest+ nWidth, nYdest + nHeight));
225 point[0].x = nXdest;
226 point[0].y = nYdest;
227 point[1].x = nXdest + nWidth - 1;
228 point[1].y = nYdest + nHeight - 1;
229 point[2].x = nXsrc;
230 point[2].y = nYsrc;
231 if(nXsrc + nWidth > pOS2bmp->cx) {
232 point[3].x = pOS2bmp->cx;
233 }
234 else point[3].x = nXsrc + nWidth;
235
236 if(nYsrc + nHeight > pOS2bmp->cy) {
237 point[3].y = pOS2bmp->cy;
238 }
239 else point[3].y = nYsrc + nHeight;
240
241 rc = GpiDrawBits(hps, bmpBits, pOS2bmp, 4, &point[0], ROP_SRCCOPY, BBO_OR);
242// 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));
243
244 if(rc == GPI_OK)
245 return(TRUE);
246 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));
247 dprintf(("WinGetLastError returned %X\n", WinGetLastError(WinQueryAnchorBlock(hwndParent)) & 0xFFFF));
248 return(FALSE);
249}
250#endif
251//******************************************************************************
252//******************************************************************************
253void DIBSection::SelectDIBObject(HDC hdc)
254{
255 this->hdc = hdc;
256 hwndParent = WinWindowFromDC(hdc);
257}
258//******************************************************************************
259//******************************************************************************
260DIBSection *DIBSection::find(DWORD handle)
261{
262 DIBSection *dsect = section;
263
264 while(dsect) {
265 if(dsect->handle == handle) {
266 return(dsect);
267 }
268 dsect = dsect->next;
269 }
270 return(NULL);
271}
272//******************************************************************************
273//A bitmap can only be selected into one DC, so this works.
274//******************************************************************************
275DIBSection *DIBSection::findHDC(HDC hdc)
276{
277 DIBSection *dsect = section;
278
279 while(dsect) {
280 if(dsect->hdc == hdc) {
281 return(dsect);
282 }
283 dsect = dsect->next;
284 }
285 return(NULL);
286}
287//******************************************************************************
288//******************************************************************************
289void DIBSection::deleteSection(DWORD handle)
290{
291 DIBSection *dsect = find(handle);
292
293 if(dsect)
294 delete dsect;
295
296}
297//******************************************************************************
298//******************************************************************************
299int DIBSection::GetDIBSection(int iSize , DIBSECTION *pDIBSection){
300 if( (sizeof(DIBSECTION)==iSize) &&
301 (pDIBSection !=NULL))
302 {
303 // BITMAP struct
304 pDIBSection->dsBm.bmType = 0; // TODO: put the correct value here
305 pDIBSection->dsBm.bmWidth = pOS2bmp->cx;
306 pDIBSection->dsBm.bmHeight = pOS2bmp->cy;
307 pDIBSection->dsBm.bmWidthBytes = bmpsize;
308 pDIBSection->dsBm.bmPlanes = pOS2bmp->cPlanes;
309 pDIBSection->dsBm.bmBitsPixel = pOS2bmp->cBitCount;
310 pDIBSection->dsBm.bmBits = bmpBits;
311 // BITMAPINFOHEADER data
312 pDIBSection->dsBmih.biSize = sizeof(BITMAPINFOHEADER);
313 pDIBSection->dsBmih.biWidth = pOS2bmp->cx;
314 pDIBSection->dsBmih.biHeight = pOS2bmp->cy;
315 pDIBSection->dsBmih.biPlanes = pOS2bmp->cPlanes;
316 pDIBSection->dsBmih.biBitCount = pOS2bmp->cBitCount;
317 pDIBSection->dsBmih.biCompression = pOS2bmp->ulCompression;
318 pDIBSection->dsBmih.biSizeImage = pOS2bmp->cbImage;
319 pDIBSection->dsBmih.biXPelsPerMeter = 0; // TODO: put the correct value here
320 pDIBSection->dsBmih.biYPelsPerMeter = 0;
321 pDIBSection->dsBmih.biClrUsed = (1<< pOS2bmp->cBitCount);
322 pDIBSection->dsBmih.biClrImportant = 0;
323
324 pDIBSection->dsBitfields[0] = 0; // TODO: put the correct value here
325 pDIBSection->dsBitfields[1] = 0;
326 pDIBSection->dsBitfields[2] = 0;
327
328 pDIBSection->dshSection = this->handle;
329
330 pDIBSection->dsOffset = 0; // TODO: put the correct value here
331
332 return 0; //ERROR_SUCCESS
333 }
334 return 87; //ERROR_INVALID_PARAMETER
335
336}
337//******************************************************************************
338//******************************************************************************
339DIBSection *DIBSection::section = NULL;
340
Note: See TracBrowser for help on using the repository browser.