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

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

CreateDIBitmap fix for negative height

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