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

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

changes from Markus

File size: 11.6 KB
Line 
1/* $Id: dibsect.cpp,v 1.10 1999-12-02 13:26:04 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
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 nDestWidth,
231 int nDestHeight, int nXsrc, int nYsrc,
232 int nSrcWidth, int nSrcHeight, DWORD Rop)
233{
234 HPS hps = (HPS)hdcDest;
235 POINTL point[4];
236 LONG rc;
237
238 HWND hwndDest = WindowFromDC(hdcDest);
239 hwndDest = Win32ToOS2Handle(hwndDest);
240 if(hwndDest != 0)
241 {
242 hps = WinGetPS(hwndDest);
243 }
244 if(hps == 0)
245 {
246 eprintf(("DIBSection::BitBlt, hps == 0 hwndDest = %X", hwndDest));
247 return(FALSE);
248 }
249
250 dprintf(("DIBSection::BitBlt %X %x to(%d,%d)(%d,%d) from (%d,%d)(%d,%d) rop %x\n",
251 hdcDest, hwndDest, nXdest, nYdest, nDestWidth, nDestHeight,
252 nXsrc, nYsrc, nSrcWidth, nSrcHeight, Rop));
253
254 point[0].x = nXdest;
255 point[0].y = nYdest;
256 point[1].x = nXdest + nDestWidth - 1;
257 point[1].y = nYdest + nDestHeight - 1;
258 point[2].x = nXsrc;
259 point[2].y = nYsrc;
260 if(nXsrc + nSrcWidth > pOS2bmp->cx)
261 {
262 point[3].x = pOS2bmp->cx;
263 }
264 else
265 point[3].x = nXsrc + nSrcWidth;
266
267 if(nYsrc + nSrcHeight > pOS2bmp->cy)
268 {
269 point[3].y = pOS2bmp->cy;
270 }
271 else
272 point[3].y = nYsrc + nSrcHeight;
273
274#if 1
275 if(fFlip & FLIP_VERT)
276 {
277 GpiEnableYInversion(hps, nDestHeight);
278 }
279
280 if(fFlip & FLIP_HOR)
281 {
282 ULONG x;
283 x = point[0].x;
284 point[0].x = point[1].x;
285 point[1].x = x;
286 }
287#endif
288
289 rc = GpiDrawBits(hps, bmpBits, pOS2bmp, 4, &point[0], ROP_SRCCOPY, BBO_OR);
290
291 if(hwndDest != 0)
292 {
293 WinReleasePS(hps);
294 }
295 if(rc == GPI_OK)
296 return(TRUE);
297
298 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));
299 dprintf(("WinGetLastError returned %X\n", WinGetLastError(WinQueryAnchorBlock(hwndDest)) & 0xFFFF));
300 return(FALSE);
301}
302//******************************************************************************
303//******************************************************************************
304void DIBSection::SelectDIBObject(HDC hdc)
305{
306 this->hdc = hdc;
307 hwndParent = WinWindowFromDC(hdc);
308}
309//******************************************************************************
310//******************************************************************************
311DIBSection *DIBSection::find(DWORD handle)
312{
313 DIBSection *dsect = section;
314
315 while(dsect) {
316 if(dsect->handle == handle) {
317 return(dsect);
318 }
319 dsect = dsect->next;
320 }
321 return(NULL);
322}
323//******************************************************************************
324//A bitmap can only be selected into one DC, so this works.
325//******************************************************************************
326DIBSection *DIBSection::findHDC(HDC hdc)
327{
328 DIBSection *dsect = section;
329
330 while(dsect) {
331 if(dsect->hdc == hdc) {
332 return(dsect);
333 }
334 dsect = dsect->next;
335 }
336 return(NULL);
337}
338//******************************************************************************
339//******************************************************************************
340void DIBSection::deleteSection(DWORD handle)
341{
342 DIBSection *dsect = find(handle);
343
344 if(dsect)
345 delete dsect;
346
347}
348//******************************************************************************
349//******************************************************************************
350int DIBSection::GetDIBSection(int iSize , DIBSECTION *pDIBSection){
351 if( (sizeof(DIBSECTION)==iSize) &&
352 (pDIBSection !=NULL))
353 {
354 // BITMAP struct
355 pDIBSection->dsBm.bmType = 0; // TODO: put the correct value here
356 pDIBSection->dsBm.bmWidth = pOS2bmp->cx;
357 pDIBSection->dsBm.bmHeight = pOS2bmp->cy;
358 pDIBSection->dsBm.bmWidthBytes = bmpsize;
359 pDIBSection->dsBm.bmPlanes = pOS2bmp->cPlanes;
360 pDIBSection->dsBm.bmBitsPixel = pOS2bmp->cBitCount;
361 pDIBSection->dsBm.bmBits = bmpBits;
362 // BITMAPINFOHEADER data
363 pDIBSection->dsBmih.biSize = sizeof(BITMAPINFOHEADER);
364 pDIBSection->dsBmih.biWidth = pOS2bmp->cx;
365 pDIBSection->dsBmih.biHeight = pOS2bmp->cy;
366 pDIBSection->dsBmih.biPlanes = pOS2bmp->cPlanes;
367 pDIBSection->dsBmih.biBitCount = pOS2bmp->cBitCount;
368 pDIBSection->dsBmih.biCompression = pOS2bmp->ulCompression;
369 pDIBSection->dsBmih.biSizeImage = pOS2bmp->cbImage;
370 pDIBSection->dsBmih.biXPelsPerMeter = 0; // TODO: put the correct value here
371 pDIBSection->dsBmih.biYPelsPerMeter = 0;
372 pDIBSection->dsBmih.biClrUsed = (1<< pOS2bmp->cBitCount);
373 pDIBSection->dsBmih.biClrImportant = 0;
374
375 pDIBSection->dsBitfields[0] = 0; // TODO: put the correct value here
376 pDIBSection->dsBitfields[1] = 0;
377 pDIBSection->dsBitfields[2] = 0;
378
379 pDIBSection->dshSection = this->handle;
380
381 pDIBSection->dsOffset = 0; // TODO: put the correct value here
382
383 return 0; //ERROR_SUCCESS
384 }
385 return 87; //ERROR_INVALID_PARAMETER
386
387}
388//******************************************************************************
389//******************************************************************************
390DIBSection *DIBSection::section = NULL;
391
Note: See TracBrowser for help on using the repository browser.