source: trunk/src/gdi32/dibitmap.cpp@ 5992

Last change on this file since 5992 was 5992, checked in by sandervl, 24 years ago

CreateDIBSection bugfix (wrong ReleaseDC call)

File size: 17.5 KB
Line 
1/* $Id: dibitmap.cpp,v 1.23 2001-06-13 10:29:05 sandervl Exp $ */
2
3/*
4 * GDI32 dib & bitmap code
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#include <os2win.h>
13#include <stdlib.h>
14#include <stdarg.h>
15#include <string.h>
16#include <misc.h>
17#include <cpuhlp.h>
18#include <winuser32.h>
19#include "dibsect.h"
20#include "rgbcvt.h"
21
22#define DBG_LOCALLOG DBG_dibitmap
23#include "dbglocal.h"
24
25ULONG CalcBitmapSize(ULONG cBits, LONG cx, LONG cy);
26
27//******************************************************************************
28//******************************************************************************
29HBITMAP WIN32API CreateDIBitmap(HDC hdc, const BITMAPINFOHEADER *lpbmih,
30 DWORD fdwInit, const void *lpbInit,
31 const BITMAPINFO *lpbmi, UINT fuUsage)
32{
33 int iHeight;
34 HBITMAP rc;
35 DWORD bitfields[3];
36 WORD *newbits = NULL;
37
38 //SvL: Completely wrong result when creating a 1bpp bitmap here (converted
39 // to 8bpp by Open32)
40 if(lpbmih->biBitCount == 1) {
41 dprintf(("WARNING: CreateDIBitmap doesn't handle 1bpp bitmaps very well!!!!!"));
42 }
43
44 //TEMPORARY HACK TO PREVENT CRASH IN OPEN32 (WSeB GA)
45
46 iHeight = lpbmih->biHeight;
47 if(lpbmih->biHeight < 0)
48 {
49 dprintf(("GDI32: CreateDIBitmap negative height! (%d,%d)", lpbmih->biWidth, lpbmih->biHeight));
50 ((BITMAPINFOHEADER *)lpbmih)->biHeight = -lpbmih->biHeight;
51 }
52
53 // 2000/09/01 PH Netscape 4.7
54 // If color depth of lpbhmi is 16 bit and lpbmi is 8 bit,
55 // Open32 will crash since it won't allocate any palette color memory,
56 // however wants to copy it later on ...
57 int biBitCount = lpbmih->biBitCount;
58
59 if (lpbmih->biBitCount != lpbmi->bmiHeader.biBitCount)
60 {
61 dprintf(("GDI32: CreateDIBitmap: color depths of bitmaps differ! (%d,%d\n", lpbmih->biBitCount,
62 lpbmi->bmiHeader.biBitCount));
63
64 ((BITMAPINFOHEADER *)lpbmih)->biBitCount = lpbmi->bmiHeader.biBitCount;
65 }
66
67 switch(lpbmih->biBitCount) {
68 case 15:
69 case 16: //Default if BI_BITFIELDS not set is RGB 555
70 bitfields[0] = (lpbmih->biCompression == BI_BITFIELDS) ? *(DWORD *)lpbmi->bmiColors : 0x7c00;
71 bitfields[1] = (lpbmih->biCompression == BI_BITFIELDS) ? *((DWORD *)lpbmi->bmiColors + 1) : 0x03e0;
72 bitfields[2] = (lpbmih->biCompression == BI_BITFIELDS) ? *((DWORD *)lpbmi->bmiColors + 2) : 0x001f;
73 break;
74 case 32:
75 bitfields[0] = (lpbmih->biCompression == BI_BITFIELDS) ? *(DWORD *)lpbmi->bmiColors : 0xff0000;
76 bitfields[1] = (lpbmih->biCompression == BI_BITFIELDS) ? *((DWORD *)lpbmi->bmiColors + 1) : 0xff00;
77 bitfields[2] = (lpbmih->biCompression == BI_BITFIELDS) ? *((DWORD *)lpbmi->bmiColors + 2) : 0xff;
78 break;
79 default:
80 bitfields[0] = 0;
81 bitfields[1] = 0;
82 bitfields[2] = 0;
83 break;
84 }
85 if(bitfields[1] == 0x3E0 && lpbInit && fdwInit == CBM_INIT)
86 {//RGB 555?
87 dprintf(("RGB 555->565 conversion required %x %x %x", bitfields[0], bitfields[1], bitfields[2]));
88
89 int imgsize = CalcBitmapSize(lpbmih->biBitCount, lpbmih->biWidth, lpbmih->biHeight);
90
91 newbits = (WORD *)malloc(imgsize);
92 if(CPUFeatures & CPUID_MMX) {
93 RGB555to565MMX(newbits, (WORD *)lpbInit, imgsize/sizeof(WORD));
94 }
95 else RGB555to565(newbits, (WORD *)lpbInit, imgsize/sizeof(WORD));
96 lpbInit = newbits;
97 }
98
99 rc = O32_CreateDIBitmap(hdc, lpbmih, fdwInit, lpbInit, lpbmi, fuUsage);
100
101 dprintf(("GDI32: CreateDIBitmap %x %x %x %x %x returned %x (%d,%d, bps %d)", hdc, lpbmih, fdwInit, lpbInit, fuUsage, rc, lpbmih->biWidth, lpbmih->biHeight, lpbmih->biBitCount));
102
103 if(newbits) free(newbits);
104
105 ((BITMAPINFOHEADER *)lpbmih)->biHeight = iHeight;
106 ((BITMAPINFOHEADER *)lpbmih)->biBitCount = biBitCount;
107
108 return rc;
109}
110//******************************************************************************
111//******************************************************************************
112HBITMAP WIN32API CreateCompatibleBitmap( HDC hdc, int nWidth, int nHeight)
113{
114 HBITMAP hBitmap;
115
116 hBitmap = O32_CreateCompatibleBitmap(hdc, nWidth, nHeight);
117 dprintf(("GDI32: CreateCompatibleBitmap %x (%d,%d) returned %x", hdc, nWidth, nHeight, hBitmap));
118 return hBitmap;
119}
120//******************************************************************************
121//CreateDisardableBitmap is obsolete and can be replaced by CreateCompatibleBitmap
122//******************************************************************************
123HBITMAP WIN32API CreateDiscardableBitmap(HDC hDC, int nWidth, int nHeight)
124{
125 dprintf(("GDI32: CreateDisardableBitmap\n"));
126 return CreateCompatibleBitmap(hDC, nWidth, nHeight);
127}
128//******************************************************************************
129//******************************************************************************
130HBITMAP WIN32API CreateBitmap(int nWidth, int nHeight, UINT cPlanes,
131 UINT cBitsPerPel, const void *lpvBits)
132{
133 HBITMAP hBitmap;
134
135 hBitmap = O32_CreateBitmap(nWidth, nHeight, cPlanes, cBitsPerPel, lpvBits);
136 dprintf(("GDI32: CreateBitmap (%d,%d) bps %d returned %x", nWidth, nHeight, cBitsPerPel, hBitmap));
137 return(hBitmap);
138}
139//******************************************************************************
140//******************************************************************************
141HBITMAP WIN32API CreateBitmapIndirect( const BITMAP * arg1)
142{
143 dprintf(("GDI32: CreateBitmapIndirect"));
144 return O32_CreateBitmapIndirect(arg1);
145}
146//******************************************************************************
147//*********************************************************************************
148HBITMAP WIN32API CreateDIBSection( HDC hdc, BITMAPINFO *pbmi, UINT iUsage,
149 VOID **ppvBits, HANDLE hSection, DWORD dwOffset)
150{
151 HBITMAP res = 0;
152 BOOL fFlip = 0;
153 int iHeight, iWidth;
154 BOOL fCreateDC = FALSE;
155
156 dprintf(("GDI32: CreateDIBSection %x %x %x %x %x %d", hdc, pbmi, iUsage, ppvBits, hSection, dwOffset));
157
158 //SvL: 13-9-98: StarCraft uses bitmap with negative height
159 iWidth = pbmi->bmiHeader.biWidth;
160 if(pbmi->bmiHeader.biWidth < 0)
161 {
162 dprintf(("CreateDIBSection: width %d", pbmi->bmiHeader.biWidth));
163 pbmi->bmiHeader.biWidth = -pbmi->bmiHeader.biWidth;
164 fFlip = FLIP_HOR;
165 }
166 iHeight = pbmi->bmiHeader.biHeight;
167 if(pbmi->bmiHeader.biHeight < 0)
168 {
169 dprintf(("CreateDIBSection: height %d", pbmi->bmiHeader.biHeight));
170 pbmi->bmiHeader.biHeight = -pbmi->bmiHeader.biHeight;
171 fFlip |= FLIP_VERT;
172 }
173
174 //SvL: RP7 (update) calls this api with hdc == 0
175 if(hdc == 0) {
176 hdc = CreateCompatibleDC(0);
177 fCreateDC = TRUE;
178 }
179 res = O32_CreateDIBitmap(hdc, &pbmi->bmiHeader, 0, NULL, pbmi, iUsage);
180 if (res)
181 {
182 char PalSize;
183 DIBSection *dsect;
184
185 dsect = new DIBSection((BITMAPINFOHEADER_W *)&pbmi->bmiHeader, (char *)&pbmi->bmiColors, iUsage, hSection, dwOffset, (DWORD)res, fFlip);
186
187 if(dsect != NULL)
188 {
189 PalSize = dsect->GetBitCount();
190 if(PalSize <= 8)
191 {
192 ULONG Pal[256], nrcolors;
193 LOGPALETTE tmpPal = { 0x300,1,{0,0,0,0}};
194 HPALETTE hpalCur, hpalTmp;
195
196 // Now get the current Palette from the DC
197 hpalTmp = CreatePalette(&tmpPal);
198 hpalCur = SelectPalette(hdc, hpalTmp, FALSE);
199
200 // and use it to set the DIBColorTable
201 nrcolors = GetPaletteEntries( hpalCur, 0, 1<<PalSize, (LPPALETTEENTRY)&Pal);
202 dsect->SetDIBColorTable(0, nrcolors, (LPPALETTEENTRY)&Pal);
203
204 // Restore the DC Palette
205 SelectPalette(hdc,hpalCur,FALSE);
206 DeleteObject(hpalTmp);
207 }
208
209 if(ppvBits!=NULL)
210 *ppvBits = dsect->GetDIBObject();
211
212 pbmi->bmiHeader.biWidth = iWidth;
213 pbmi->bmiHeader.biHeight = iHeight;
214
215 if(fCreateDC) DeleteDC(hdc);
216 return(res);
217 }
218 }
219 if(fCreateDC) DeleteDC(hdc);
220
221 /* Error. */
222 if (res)
223 DeleteObject(res);
224 *ppvBits = NULL;
225
226#ifdef DEBUG
227 dprintf(("GDI32: CreateDIBSection, error!\n"));
228 dprintf(("pbmi->biWidth %d", pbmi->bmiHeader.biWidth));
229 dprintf(("pbmi->biHeight %d", pbmi->bmiHeader.biHeight));
230 dprintf(("pbmi->biBitCount %d", pbmi->bmiHeader.biBitCount));
231#endif
232
233 return 0;
234}
235//******************************************************************************
236//******************************************************************************
237UINT WIN32API GetDIBColorTable(HDC hdc, UINT uStartIndex, UINT cEntries,
238 RGBQUAD *pColors)
239{
240 DIBSection *dsect = DIBSection::findHDC(hdc);
241 UINT rc;
242 int i;
243
244 dprintf(("GetDIBColorTable %x %d->%d %x", hdc, uStartIndex, cEntries, pColors));
245
246 if(dsect)
247 {
248 return(dsect->GetDIBColorTable(uStartIndex, cEntries, pColors));
249 }
250 //TODO: Is this correct?????
251 // Wine returns 0 if bitmap selected into dc with bpp > 8
252 HPALETTE hpal = GetCurrentObject(hdc, OBJ_PAL);
253 rc = O32_GetPaletteEntries(hpal, uStartIndex, cEntries, (PALETTEENTRY *)pColors);
254 for(i=0;i<cEntries;i++)
255 {
256 BYTE tmp;
257 tmp = pColors[i].rgbBlue;
258 pColors[i].rgbBlue = pColors[i].rgbRed;
259 pColors[i].rgbRed = tmp;
260 pColors[i].rgbReserved = 0;
261 }
262 dprintf(("GDI32: GetDIBColorTable returns %d\n", rc));
263 return(rc);
264}
265//******************************************************************************
266//******************************************************************************
267UINT WIN32API SetDIBColorTable(HDC hdc, UINT uStartIndex, UINT cEntries,
268 RGBQUAD *pColors)
269{
270 DIBSection *dsect = DIBSection::findHDC(hdc);
271
272 dprintf(("GDI32: SetDIBColorTable %x %d,%d %x", hdc, uStartIndex, cEntries, pColors));
273 if(dsect)
274 {
275 return(dsect->SetDIBColorTable(uStartIndex, cEntries, pColors));
276 }
277 else return(0);
278}
279//******************************************************************************
280//******************************************************************************
281LONG WIN32API GetBitmapBits( HBITMAP hBitmap, LONG arg2, PVOID arg3)
282{
283 dprintf(("GDI32: GetBitmapBits %x", hBitmap));
284 return O32_GetBitmapBits(hBitmap, arg2, arg3);
285}
286//******************************************************************************
287//******************************************************************************
288LONG WIN32API SetBitmapBits( HBITMAP hBitmap, LONG arg2, const VOID * arg3)
289{
290 dprintf(("GDI32: SetBitmapBits %x", hBitmap));
291 return O32_SetBitmapBits(hBitmap, (DWORD)arg2, arg3);
292}
293//******************************************************************************
294//******************************************************************************
295BOOL WIN32API GetBitmapDimensionEx( HBITMAP hBitmap, PSIZE pSize)
296{
297 dprintf(("GDI32: GetBitmapDimensionEx %x (%d,%d)", hBitmap, pSize->cx, pSize->cy));
298 return O32_GetBitmapDimensionEx(hBitmap, pSize);
299}
300//******************************************************************************
301//******************************************************************************
302BOOL WIN32API SetBitmapDimensionEx( HBITMAP arg1, int arg2, int arg3, PSIZE arg4)
303{
304 dprintf(("GDI32: SetBitmapDimensionEx"));
305 return O32_SetBitmapDimensionEx(arg1, arg2, arg3, arg4);
306}
307//******************************************************************************
308//******************************************************************************
309int WIN32API GetDIBits(HDC hdc, HBITMAP hBitmap, UINT uStartScan, UINT cScanLines,
310 void *lpvBits, PBITMAPINFO lpbi, UINT uUsage)
311{
312 int nrlines;
313
314 dprintf(("GDI32: GetDIBits %x %x %d %d %x %x (biBitCount %d) %d", hdc, hBitmap, uStartScan, cScanLines, lpvBits, lpbi, lpbi->bmiHeader.biBitCount, uUsage));
315
316 //SvL: WGSS screws up the DC if it's a memory DC
317 // TODO: Fix in WGSS
318 HDC hdcMem = CreateCompatibleDC(0);
319
320 nrlines = O32_GetDIBits(hdcMem, hBitmap, uStartScan, cScanLines, lpvBits, lpbi, uUsage);
321
322 DeleteDC(hdcMem);
323
324 // set proper color masks
325 switch(lpbi->bmiHeader.biBitCount) {
326 case 15:
327 case 16: //RGB 565
328 ((DWORD*)(lpbi->bmiColors))[0] = 0x7c00;
329 ((DWORD*)(lpbi->bmiColors))[1] = 0x03E0;
330 ((DWORD*)(lpbi->bmiColors))[2] = 0x001F;
331 break;
332 case 24:
333 case 32:
334 ((DWORD*)(lpbi->bmiColors))[0] = 0x0000FF;
335 ((DWORD*)(lpbi->bmiColors))[1] = 0x00FF00;
336 ((DWORD*)(lpbi->bmiColors))[2] = 0xFF0000;
337 break;
338 }
339 if(nrlines && lpvBits && lpbi->bmiHeader.biBitCount == 16 && ((DWORD*)(lpbi->bmiColors))[1] == 0x3E0)
340 {//RGB 555?
341 dprintf(("RGB 565->555 conversion required"));
342
343 int imgsize = CalcBitmapSize(lpbi->bmiHeader.biBitCount,
344 lpbi->bmiHeader.biWidth, nrlines);
345
346 if(CPUFeatures & CPUID_MMX) {
347 RGB565to555MMX((WORD *)lpvBits, (WORD *)lpvBits, imgsize/sizeof(WORD));
348 }
349 else RGB565to555((WORD *)lpvBits, (WORD *)lpvBits, imgsize/sizeof(WORD));
350 }
351
352 //WGSS/Open32 returns 0 when querying the bitmap info; must return nr of scanlines
353 //as 0 signals failure
354 if(lpvBits == NULL) {
355 nrlines = cScanLines;
356 }
357 dprintf(("GDI32: GetDIBits returned %d", nrlines));
358 return nrlines;
359}
360//******************************************************************************
361//******************************************************************************
362int WIN32API SetDIBits(HDC hdc, HBITMAP hBitmap, UINT startscan, UINT numlines, const VOID *pBits,
363 const BITMAPINFO *pBitmapInfo, UINT usage)
364{
365 int ret;
366 DWORD bitfields[3];
367 WORD *newbits = NULL;
368
369 dprintf(("GDI32: SetDIBits %x %x %x %x %x %x %x", hdc, hBitmap, startscan, numlines, pBits, pBitmapInfo, usage));
370
371 //SvL: Open32's SetDIBits really messes things up for 1 bpp bitmaps, must use SetBitmapBits
372 if(pBitmapInfo->bmiHeader.biBitCount == 1 && startscan == 0 && numlines == pBitmapInfo->bmiHeader.biHeight)
373 {//WARNING: hack alert!
374 int dibwidth = DIB_GetDIBWidthBytes(pBitmapInfo->bmiHeader.biWidth, 1);
375 int bmpwidth = BITMAP_GetWidthBytes(pBitmapInfo->bmiHeader.biWidth, 1);
376 char *newpix = (char *)malloc(dibwidth*pBitmapInfo->bmiHeader.biHeight);
377 char *orgpix = (char *)pBits;
378 int ret;
379
380 dprintf(("Flipping 1bpp bitmap and calling SetBitmapBits (WORKAROUND) (%d -> %d)", dibwidth, bmpwidth));
381 newpix += ((pBitmapInfo->bmiHeader.biHeight-1)*bmpwidth);
382
383 //flip bitmap here; SetDIBits assumes origin is left bottom, SetBitmapBits left top
384 //SetDIBits assumes DWORD aligned data
385 //SetBitmapBits assumes WORD aligned data
386 for(int i=0;i<pBitmapInfo->bmiHeader.biHeight;i++) {
387 memcpy(newpix, orgpix, bmpwidth);
388
389 newpix -= bmpwidth;
390 orgpix += dibwidth;
391 }
392 newpix += bmpwidth;
393 ret = O32_SetBitmapBits(hBitmap, pBitmapInfo->bmiHeader.biSizeImage, newpix);
394
395 free(newpix);
396 return ret;
397 }
398#ifdef DEBUG
399 if(pBitmapInfo->bmiHeader.biBitCount == 1) {
400 dprintf(("ERROR: SetDIBits does NOT work well for 1 bpp bitmaps!!!!!"));
401 }
402#endif
403
404 switch(pBitmapInfo->bmiHeader.biBitCount) {
405 case 15:
406 case 16: //Default if BI_BITFIELDS not set is RGB 555
407 bitfields[0] = (pBitmapInfo->bmiHeader.biCompression == BI_BITFIELDS) ? *(DWORD *)pBitmapInfo->bmiColors : 0x7c00;
408 bitfields[1] = (pBitmapInfo->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)pBitmapInfo->bmiColors + 1) : 0x03e0;
409 bitfields[2] = (pBitmapInfo->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)pBitmapInfo->bmiColors + 2) : 0x001f;
410 break;
411 case 32:
412 bitfields[0] = (pBitmapInfo->bmiHeader.biCompression == BI_BITFIELDS) ? *(DWORD *)pBitmapInfo->bmiColors : 0xff0000;
413 bitfields[1] = (pBitmapInfo->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)pBitmapInfo->bmiColors + 1) : 0xff00;
414 bitfields[2] = (pBitmapInfo->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)pBitmapInfo->bmiColors + 2) : 0xff;
415 break;
416 default:
417 bitfields[0] = 0;
418 bitfields[1] = 0;
419 bitfields[2] = 0;
420 break;
421 }
422 if(pBits && bitfields[1] == 0x3E0)
423 {//RGB 555?
424 dprintf(("RGB 555->565 conversion required %x %x %x", bitfields[0], bitfields[1], bitfields[2]));
425
426 int imgsize = CalcBitmapSize(pBitmapInfo->bmiHeader.biBitCount,
427 pBitmapInfo->bmiHeader.biWidth, numlines);
428
429 newbits = (WORD *)malloc(imgsize);
430 if(CPUFeatures & CPUID_MMX) {
431 RGB555to565MMX(newbits, (WORD *)pBits, imgsize/sizeof(WORD));
432 }
433 else RGB555to565(newbits, (WORD *)pBits, imgsize/sizeof(WORD));
434 pBits = newbits;
435 }
436
437 ret = O32_SetDIBits(hdc, hBitmap, startscan, numlines, pBits, pBitmapInfo, usage);
438 if(newbits) free(newbits);
439
440 if(DIBSection::getSection() != NULL)
441 {
442 DIBSection *dsect;
443
444 dsect = DIBSection::findObj(hBitmap);
445 if(dsect) {
446 HBITMAP hBmpOld = SelectObject(hdc, hBitmap);
447 dsect->sync(hdc, 0, dsect->GetHeight());
448 SelectObject(hdc, hBmpOld);
449 }
450 }
451
452 return ret;
453}
454//******************************************************************************
455//******************************************************************************
Note: See TracBrowser for help on using the repository browser.