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

Last change on this file since 9764 was 9764, checked in by sandervl, 23 years ago

CreateDIBSection: don't get palette from DC if bitmap bits provided

File size: 23.6 KB
Line 
1/* $Id: dibitmap.cpp,v 1.39 2003-02-06 20:28:09 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#include <stats.h>
22#include "oslibgpi.h"
23#include <objhandle.h>
24
25#define DBG_LOCALLOG DBG_dibitmap
26#include "dbglocal.h"
27
28ULONG CalcBitmapSize(ULONG cBits, LONG cx, LONG cy);
29
30//******************************************************************************
31//******************************************************************************
32HBITMAP WIN32API CreateDIBitmap(HDC hdc, const BITMAPINFOHEADER *lpbmih,
33 DWORD fdwInit, const void *lpbInit,
34 const BITMAPINFO *lpbmi, UINT fuUsage)
35{
36 int iHeight;
37 HBITMAP rc;
38 DWORD bitfields[3];
39 WORD *newbits = NULL;
40
41 //TEMPORARY HACK TO PREVENT CRASH IN OPEN32 (WSeB GA)
42 iHeight = lpbmih->biHeight;
43 if(lpbmih->biHeight < 0)
44 {
45 dprintf(("GDI32: CreateDIBitmap negative height! (%d,%d)", lpbmih->biWidth, lpbmih->biHeight));
46 //TODO: doesn't work if memory is readonly!!
47 ((BITMAPINFOHEADER *)lpbmih)->biHeight = -lpbmih->biHeight;
48
49 if(lpbInit && fdwInit == CBM_INIT)
50 {
51 // upside down
52 HBITMAP rc = 0;
53 long lLineByte = DIB_GetDIBWidthBytes(lpbmih->biWidth, lpbmih->biBitCount);
54 long lHeight = lpbmih->biHeight;
55
56 newbits = (WORD *)malloc( lLineByte * lHeight );
57 if(newbits) {
58 unsigned char *pbSrc = (unsigned char *)lpbInit + lLineByte * (lHeight - 1);
59 unsigned char *pbDst = (unsigned char *)newbits;
60 for(int y = 0; y < lHeight; y++) {
61 memcpy( pbDst, pbSrc, lLineByte );
62 pbDst += lLineByte;
63 pbSrc -= lLineByte;
64 }
65 rc = CreateDIBitmap(hdc, lpbmih, fdwInit, newbits, lpbmi, fuUsage);
66 free( newbits );
67 }
68
69 ((BITMAPINFOHEADER *)lpbmih)->biHeight = iHeight;
70 return rc;
71 }
72 }
73
74 // 2000/09/01 PH Netscape 4.7
75 // If color depth of lpbhmi is 16 bit and lpbmi is 8 bit,
76 // Open32 will crash since it won't allocate any palette color memory,
77 // however wants to copy it later on ...
78 int biBitCount = lpbmih->biBitCount;
79
80 if (lpbmih->biBitCount != lpbmi->bmiHeader.biBitCount)
81 {
82 dprintf(("GDI32: CreateDIBitmap: color depths of bitmaps differ! (%d,%d\n", lpbmih->biBitCount,
83 lpbmi->bmiHeader.biBitCount));
84
85 ((BITMAPINFOHEADER *)lpbmih)->biBitCount = lpbmi->bmiHeader.biBitCount;
86 }
87
88 switch(lpbmih->biBitCount) {
89 case 15:
90 case 16: //Default if BI_BITFIELDS not set is RGB 555
91 bitfields[0] = (lpbmih->biCompression == BI_BITFIELDS) ? *(DWORD *)lpbmi->bmiColors : DEFAULT_16BPP_RED_MASK;
92 bitfields[1] = (lpbmih->biCompression == BI_BITFIELDS) ? *((DWORD *)lpbmi->bmiColors + 1) : DEFAULT_16BPP_GREEN_MASK;
93 bitfields[2] = (lpbmih->biCompression == BI_BITFIELDS) ? *((DWORD *)lpbmi->bmiColors + 2) : DEFAULT_16BPP_BLUE_MASK;
94 break;
95 case 24:
96 case 32:
97 bitfields[0] = (lpbmih->biCompression == BI_BITFIELDS) ? *(DWORD *)lpbmi->bmiColors : DEFAULT_24BPP_RED_MASK;
98 bitfields[1] = (lpbmih->biCompression == BI_BITFIELDS) ? *((DWORD *)lpbmi->bmiColors + 1) : DEFAULT_24BPP_GREEN_MASK;
99 bitfields[2] = (lpbmih->biCompression == BI_BITFIELDS) ? *((DWORD *)lpbmi->bmiColors + 2) : DEFAULT_24BPP_BLUE_MASK;
100 break;
101 default:
102 bitfields[0] = 0;
103 bitfields[1] = 0;
104 bitfields[2] = 0;
105 break;
106 }
107 if(bitfields[1] == RGB555_GREEN_MASK && lpbInit && fdwInit == CBM_INIT)
108 {//RGB 555?
109 dprintf(("RGB 555->565 conversion required %x %x %x", bitfields[0], bitfields[1], bitfields[2]));
110
111 int imgsize = CalcBitmapSize(lpbmih->biBitCount, lpbmih->biWidth, lpbmih->biHeight);
112
113 newbits = (WORD *)malloc(imgsize);
114 if(CPUFeatures & CPUID_MMX) {
115 RGB555to565MMX(newbits, (WORD *)lpbInit, imgsize/sizeof(WORD));
116 }
117 else RGB555to565(newbits, (WORD *)lpbInit, imgsize/sizeof(WORD));
118 lpbInit = newbits;
119 }
120
121 rc = O32_CreateDIBitmap(hdc, lpbmih, fdwInit, lpbInit, lpbmi, fuUsage);
122
123 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));
124
125 if(newbits) free(newbits);
126
127 ((BITMAPINFOHEADER *)lpbmih)->biHeight = iHeight;
128 ((BITMAPINFOHEADER *)lpbmih)->biBitCount = biBitCount;
129
130 if(rc) {
131 STATS_CreateDIBitmap(rc, hdc, lpbmih, fdwInit, lpbInit, lpbmi, fuUsage);
132 if(bitfields[1] == RGB565_GREEN_MASK) {
133 //mark bitmap as RGB565
134 dprintf(("RGB565 bitmap"));
135 ObjSetHandleFlag(rc, OBJHANDLE_FLAG_BMP_RGB565, TRUE);
136 }
137 }
138
139 return rc;
140}
141//******************************************************************************
142//******************************************************************************
143HBITMAP WIN32API CreateCompatibleBitmap( HDC hdc, int nWidth, int nHeight)
144{
145 HBITMAP hBitmap;
146 pDCData pHps;
147
148 pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdc);
149 if(!pHps)
150 {
151 SetLastError(ERROR_INVALID_HANDLE);
152 return 0;
153 }
154
155 hBitmap = O32_CreateCompatibleBitmap(hdc, nWidth, nHeight);
156 if(hBitmap) {
157 STATS_CreateCompatibleBitmap(hBitmap,hdc, nWidth, nHeight);
158 if(pHps->hwnd == 1) { //1 == HWND_DESKTOP
159 dprintf(("Screen compatible bitmap"));
160 ObjSetHandleFlag(hBitmap, OBJHANDLE_FLAG_BMP_SCREEN_COMPATIBLE, 1);
161 }
162 }
163
164 return hBitmap;
165}
166//******************************************************************************
167//CreateDisardableBitmap is obsolete and can be replaced by CreateCompatibleBitmap
168//******************************************************************************
169HBITMAP WIN32API CreateDiscardableBitmap(HDC hDC, int nWidth, int nHeight)
170{
171 dprintf(("GDI32: CreateDisardableBitmap"));
172 return CreateCompatibleBitmap(hDC, nWidth, nHeight);
173}
174//******************************************************************************
175//******************************************************************************
176HBITMAP WIN32API CreateBitmap(int nWidth, int nHeight, UINT cPlanes,
177 UINT cBitsPerPel, const void *lpvBits)
178{
179 HBITMAP hBitmap;
180
181 hBitmap = O32_CreateBitmap(nWidth, nHeight, cPlanes, cBitsPerPel, lpvBits);
182 if(hBitmap) STATS_CreateBitmap(hBitmap,nWidth, nHeight, cPlanes, cBitsPerPel, lpvBits);
183 return(hBitmap);
184}
185//******************************************************************************
186//******************************************************************************
187HBITMAP WIN32API CreateBitmapIndirect( const BITMAP *pBitmap)
188{
189 HBITMAP hBitmap;
190
191 dprintf(("GDI32: CreateBitmapIndirect (%d,%d) bpp %d bits %x", pBitmap->bmWidth, pBitmap->bmHeight, pBitmap->bmBitsPixel, pBitmap->bmBits));
192 hBitmap = O32_CreateBitmapIndirect(pBitmap);
193 if(hBitmap) STATS_CreateBitmapIndirect(hBitmap, pBitmap);
194 return hBitmap;
195}
196//******************************************************************************
197//*********************************************************************************
198HBITMAP WIN32API CreateDIBSection( HDC hdc, BITMAPINFO *pbmi, UINT iUsage,
199 VOID **ppvBits, HANDLE hSection, DWORD dwOffset)
200{
201 HBITMAP res = 0;
202 BOOL fFlip = 0;
203 int iHeight, iWidth;
204 BOOL fCreateDC = FALSE;
205
206 dprintf(("GDI32: CreateDIBSection %x %x %x %x %x %d", hdc, pbmi, iUsage, ppvBits, hSection, dwOffset));
207
208 //SvL: 13-9-98: StarCraft uses bitmap with negative height
209 iWidth = pbmi->bmiHeader.biWidth;
210 if(pbmi->bmiHeader.biWidth < 0)
211 {
212 dprintf(("CreateDIBSection: width %d", pbmi->bmiHeader.biWidth));
213 pbmi->bmiHeader.biWidth = -pbmi->bmiHeader.biWidth;
214 fFlip = FLIP_HOR;
215 }
216 iHeight = pbmi->bmiHeader.biHeight;
217 if(pbmi->bmiHeader.biHeight < 0)
218 {
219 dprintf(("CreateDIBSection: height %d", pbmi->bmiHeader.biHeight));
220 pbmi->bmiHeader.biHeight = -pbmi->bmiHeader.biHeight;
221 fFlip |= FLIP_VERT;
222 }
223
224 //SvL: RP7 (update) calls this api with hdc == 0
225 if(hdc == 0) {
226 hdc = CreateCompatibleDC(0);
227 fCreateDC = TRUE;
228 }
229 res = O32_CreateDIBitmap(hdc, &pbmi->bmiHeader, 0, NULL, pbmi, iUsage);
230 if (res)
231 {
232 char PalSize;
233 DIBSection *dsect;
234
235 dsect = new DIBSection((BITMAPINFOHEADER_W *)&pbmi->bmiHeader, (char *)&pbmi->bmiColors, iUsage, hSection, dwOffset, (DWORD)res, fFlip);
236
237 if(dsect != NULL)
238 {
239 PalSize = dsect->GetBitCount();
240
241 if(!ppvBits && PalSize <= 8)
242 {
243 ULONG Pal[256], nrcolors;
244 LOGPALETTE tmpPal = { 0x300,1,{0,0,0,0}};
245 HPALETTE hpalCur;
246
247 // Now get the current Palette from the DC
248 hpalCur = GetCurrentObject(hdc, OBJ_PAL);
249
250 // and use it to set the DIBColorTable
251 nrcolors = GetPaletteEntries( hpalCur, 0, 1<<PalSize, (LPPALETTEENTRY)&Pal);
252 dsect->SetDIBColorTable(0, nrcolors, (LPPALETTEENTRY)&Pal);
253 }
254
255 if(ppvBits!=NULL)
256 *ppvBits = dsect->GetDIBObject();
257
258 pbmi->bmiHeader.biWidth = iWidth;
259 pbmi->bmiHeader.biHeight = iHeight;
260
261 if(fCreateDC) DeleteDC(hdc);
262
263 STATS_CreateDIBSection(res, hdc, pbmi, iUsage, ppvBits, hSection, dwOffset);
264
265 return(res);
266 }
267 }
268 if(fCreateDC) DeleteDC(hdc);
269
270 /* Error. */
271 if (res)
272 DeleteObject(res);
273 *ppvBits = NULL;
274
275#ifdef DEBUG
276 dprintf(("GDI32: CreateDIBSection, error!\n"));
277 dprintf(("pbmi->biWidth %d", pbmi->bmiHeader.biWidth));
278 dprintf(("pbmi->biHeight %d", pbmi->bmiHeader.biHeight));
279 dprintf(("pbmi->biBitCount %d", pbmi->bmiHeader.biBitCount));
280#endif
281
282 return 0;
283}
284//******************************************************************************
285//******************************************************************************
286UINT WIN32API GetDIBColorTable(HDC hdc, UINT uStartIndex, UINT cEntries,
287 RGBQUAD *pColors)
288{
289 DIBSection *dsect = DIBSection::findHDC(hdc);
290 UINT rc;
291 int i;
292
293 dprintf(("GetDIBColorTable %x %d->%d %x", hdc, uStartIndex, cEntries, pColors));
294
295 if(dsect)
296 {
297 return(dsect->GetDIBColorTable(uStartIndex, cEntries, pColors));
298 }
299 //TODO: Is this correct?????
300 // Wine returns 0 if bitmap selected into dc with bpp > 8
301 HPALETTE hpal = GetCurrentObject(hdc, OBJ_PAL);
302 rc = O32_GetPaletteEntries(hpal, uStartIndex, cEntries, (PALETTEENTRY *)pColors);
303 for(i=0;i<cEntries;i++)
304 {
305 BYTE tmp;
306 tmp = pColors[i].rgbBlue;
307 pColors[i].rgbBlue = pColors[i].rgbRed;
308 pColors[i].rgbRed = tmp;
309 pColors[i].rgbReserved = 0;
310 }
311 dprintf(("GDI32: GetDIBColorTable returns %d\n", rc));
312 return(rc);
313}
314//******************************************************************************
315//******************************************************************************
316UINT WIN32API SetDIBColorTable(HDC hdc, UINT uStartIndex, UINT cEntries,
317 RGBQUAD *pColors)
318{
319 DIBSection *dsect = DIBSection::findHDC(hdc);
320
321 dprintf(("GDI32: SetDIBColorTable %x %d,%d %x", hdc, uStartIndex, cEntries, pColors));
322 if(dsect)
323 {
324 return(dsect->SetDIBColorTable(uStartIndex, cEntries, pColors));
325 }
326 else return(0);
327}
328//******************************************************************************
329//******************************************************************************
330LONG WIN32API GetBitmapBits( HBITMAP hBitmap, LONG arg2, PVOID arg3)
331{
332 dprintf(("GDI32: GetBitmapBits %x", hBitmap));
333 return O32_GetBitmapBits(hBitmap, arg2, arg3);
334}
335//******************************************************************************
336//******************************************************************************
337LONG WIN32API SetBitmapBits( HBITMAP hBitmap, LONG arg2, const VOID * arg3)
338{
339 dprintf(("GDI32: SetBitmapBits %x", hBitmap));
340 return O32_SetBitmapBits(hBitmap, (DWORD)arg2, arg3);
341}
342//******************************************************************************
343//******************************************************************************
344BOOL WIN32API GetBitmapDimensionEx( HBITMAP hBitmap, PSIZE pSize)
345{
346 dprintf(("GDI32: GetBitmapDimensionEx %x (%d,%d)", hBitmap, pSize->cx, pSize->cy));
347 return O32_GetBitmapDimensionEx(hBitmap, pSize);
348}
349//******************************************************************************
350//******************************************************************************
351BOOL WIN32API SetBitmapDimensionEx( HBITMAP arg1, int arg2, int arg3, PSIZE arg4)
352{
353 dprintf(("GDI32: SetBitmapDimensionEx"));
354 return O32_SetBitmapDimensionEx(arg1, arg2, arg3, arg4);
355}
356//******************************************************************************
357//******************************************************************************
358int WIN32API GetDIBits(HDC hdc, HBITMAP hBitmap, UINT uStartScan, UINT cScanLines,
359 void *lpvBits, PBITMAPINFO lpbi, UINT uUsage)
360{
361 int nrlines;
362 pDCData pHps;
363 HDC hdcMem;
364 DWORD biCompression;
365
366 dprintf(("GDI32: GetDIBits %x %x %d %d %x %x (biBitCount %d) %d", hdc, hBitmap, uStartScan, cScanLines, lpvBits, lpbi, lpbi->bmiHeader.biBitCount, uUsage));
367
368 //SvL: WGSS screws up the DC if it's a memory DC
369 // TODO: Fix in WGSS (tries to select another bitmap in the DC which fails)
370 pHps = (pDCData)OSLibGpiQueryDCData((HPS)hdc);
371 if(!pHps)
372 {
373 SetLastError(ERROR_INVALID_HANDLE);
374 return 0;
375 }
376 if(pHps->isMemoryPS) {
377 hdcMem = CreateCompatibleDC(0);
378 }
379 else hdcMem = hdc;
380
381 if(lpvBits) {
382 biCompression = lpbi->bmiHeader.biCompression;
383 }
384
385 //If the app wants bitmap data and upside down, then flip image
386 if(lpvBits && lpbi->bmiHeader.biHeight < 0 && (lpbi->bmiHeader.biCompression == BI_RGB ||
387 lpbi->bmiHeader.biCompression == BI_BITFIELDS))
388 {
389 INT rc = -1;
390 long lLineByte;
391 LONG height = lpbi->bmiHeader.biHeight;
392
393 lpbi->bmiHeader.biHeight = -lpbi->bmiHeader.biHeight;
394
395 lLineByte = DIB_GetDIBWidthBytes(lpbi->bmiHeader.biWidth, lpbi->bmiHeader.biBitCount);
396
397 dprintf(("flip bitmap (negative height)"));
398 char *pNewBits = (char *)malloc( lLineByte * cScanLines );
399 if(pNewBits) {
400 nrlines = O32_GetDIBits(hdcMem, hBitmap, uStartScan, cScanLines, pNewBits, lpbi, uUsage);
401
402 unsigned char *pbSrc = (unsigned char *)pNewBits + lLineByte * (cScanLines - 1);
403 unsigned char *pbDst = (unsigned char *)lpvBits;
404 for(int y = 0; y < cScanLines; y++) {
405 memcpy( pbDst, pbSrc, lLineByte );
406 pbDst += lLineByte;
407 pbSrc -= lLineByte;
408 }
409 free(pNewBits);
410 }
411 else DebugInt3();
412
413 //restore height
414 lpbi->bmiHeader.biHeight = height;
415 }
416 else {
417 nrlines = O32_GetDIBits(hdcMem, hBitmap, uStartScan, cScanLines, lpvBits, lpbi, uUsage);
418 }
419
420 if(lpvBits) {
421 //WGSS always sets it to BI_RGB
422 lpbi->bmiHeader.biCompression = biCompression;
423 }
424
425 if(pHps->isMemoryPS)
426 DeleteDC(hdcMem);
427
428 //Only return bitfields info if the app wants it
429 if(lpvBits && lpbi->bmiHeader.biCompression == BI_BITFIELDS) {
430 // set proper color masks (only if lpvBits not NULL)
431 switch(lpbi->bmiHeader.biBitCount) {
432 case 15:
433 case 16: //RGB 565
434 {
435 DWORD dwFlags;
436
437 dwFlags = ObjQueryHandleFlags(hBitmap);
438 if(dwFlags & (OBJHANDLE_FLAG_BMP_SCREEN_COMPATIBLE|OBJHANDLE_FLAG_BMP_RGB565)) {
439 ((DWORD*)(lpbi->bmiColors))[0] = RGB565_RED_MASK;
440 ((DWORD*)(lpbi->bmiColors))[1] = RGB565_GREEN_MASK;
441 ((DWORD*)(lpbi->bmiColors))[2] = RGB565_BLUE_MASK;
442 }
443 else {
444 ((DWORD*)(lpbi->bmiColors))[0] = RGB555_RED_MASK;
445 ((DWORD*)(lpbi->bmiColors))[1] = RGB555_GREEN_MASK;
446 ((DWORD*)(lpbi->bmiColors))[2] = RGB555_BLUE_MASK;
447 }
448 break;
449 }
450 case 24:
451 case 32:
452 ((DWORD*)(lpbi->bmiColors))[0] = DEFAULT_24BPP_RED_MASK;
453 ((DWORD*)(lpbi->bmiColors))[1] = DEFAULT_24BPP_GREEN_MASK;
454 ((DWORD*)(lpbi->bmiColors))[2] = DEFAULT_24BPP_BLUE_MASK;
455 break;
456 }
457 dprintf(("BI_BITFIELDS: %x %x %x", ((DWORD*)(lpbi->bmiColors))[0], ((DWORD*)(lpbi->bmiColors))[1], ((DWORD*)(lpbi->bmiColors))[2]));
458 }
459 if(nrlines && lpvBits && lpbi->bmiHeader.biBitCount == 16 && ((DWORD*)(lpbi->bmiColors))[1] == RGB555_GREEN_MASK)
460 {//RGB 555?
461 dprintf(("RGB 565->555 conversion required"));
462
463 int imgsize = CalcBitmapSize(lpbi->bmiHeader.biBitCount,
464 lpbi->bmiHeader.biWidth, nrlines);
465
466 if(CPUFeatures & CPUID_MMX) {
467 RGB565to555MMX((WORD *)lpvBits, (WORD *)lpvBits, imgsize/sizeof(WORD));
468 }
469 else RGB565to555((WORD *)lpvBits, (WORD *)lpvBits, imgsize/sizeof(WORD));
470 }
471
472 //WGSS/Open32 returns 0 when querying the bitmap info; must return nr of scanlines
473 //as 0 signals failure
474 if(lpvBits == NULL) {
475 nrlines = cScanLines;
476 }
477 return nrlines;
478}
479//******************************************************************************
480//******************************************************************************
481void WIN32API ConvertRGB555to565(LPVOID dest, LPVOID src, UINT imgsize)
482{
483 if(CPUFeatures & CPUID_MMX) {
484 RGB555to565MMX((WORD *)dest, (WORD *)src, imgsize/sizeof(WORD));
485 }
486 else RGB555to565((WORD *)dest, (WORD *)src, imgsize/sizeof(WORD));
487}
488//******************************************************************************
489//******************************************************************************
490int WIN32API SetDIBits(HDC hdc, HBITMAP hBitmap, UINT startscan, UINT numlines, const VOID *pBits,
491 const BITMAPINFO *pBitmapInfo, UINT usage)
492{
493 int ret;
494 DWORD bitfields[3];
495 WORD *newbits = NULL;
496
497 dprintf(("GDI32: SetDIBits %x %x %x %x %x %x %x", hdc, hBitmap, startscan, numlines, pBits, pBitmapInfo, usage));
498
499 //SvL: Open32's SetDIBits really messes things up for 1 bpp bitmaps, must use SetBitmapBits
500 if(pBitmapInfo->bmiHeader.biBitCount == 1 && startscan == 0 && numlines == pBitmapInfo->bmiHeader.biHeight)
501 {//WARNING: hack alert!
502 int dibwidth = DIB_GetDIBWidthBytes(pBitmapInfo->bmiHeader.biWidth, 1);
503 int bmpwidth = BITMAP_GetWidthBytes(pBitmapInfo->bmiHeader.biWidth, 1);
504 char *newpix = (char *)malloc(dibwidth*pBitmapInfo->bmiHeader.biHeight);
505 char *orgpix = (char *)pBits;
506 int ret;
507
508 dprintf(("Flipping 1bpp bitmap and calling SetBitmapBits (WORKAROUND) (%d -> %d)", dibwidth, bmpwidth));
509 newpix += ((pBitmapInfo->bmiHeader.biHeight-1)*bmpwidth);
510
511 //flip bitmap here; SetDIBits assumes origin is left bottom, SetBitmapBits left top
512 //SetDIBits assumes DWORD aligned data
513 //SetBitmapBits assumes WORD aligned data
514 for(int i=0;i<pBitmapInfo->bmiHeader.biHeight;i++) {
515 memcpy(newpix, orgpix, bmpwidth);
516
517 newpix -= bmpwidth;
518 orgpix += dibwidth;
519 }
520 newpix += bmpwidth;
521 ret = O32_SetBitmapBits(hBitmap, pBitmapInfo->bmiHeader.biSizeImage, newpix);
522
523 free(newpix);
524 return ret;
525 }
526#ifdef DEBUG
527 if(pBitmapInfo->bmiHeader.biBitCount == 1) {
528 dprintf(("ERROR: SetDIBits does NOT work well for 1 bpp bitmaps!!!!!"));
529 }
530#endif
531
532 switch(pBitmapInfo->bmiHeader.biBitCount) {
533 case 15:
534 case 16: //Default if BI_BITFIELDS not set is RGB 555
535 bitfields[0] = (pBitmapInfo->bmiHeader.biCompression == BI_BITFIELDS) ? *(DWORD *)pBitmapInfo->bmiColors : DEFAULT_16BPP_RED_MASK;
536 bitfields[1] = (pBitmapInfo->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)pBitmapInfo->bmiColors + 1) : DEFAULT_16BPP_GREEN_MASK;
537 bitfields[2] = (pBitmapInfo->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)pBitmapInfo->bmiColors + 2) : DEFAULT_16BPP_BLUE_MASK;
538 break;
539
540 case 24:
541 case 32:
542 bitfields[0] = (pBitmapInfo->bmiHeader.biCompression == BI_BITFIELDS) ? *(DWORD *)pBitmapInfo->bmiColors : DEFAULT_24BPP_RED_MASK;
543 bitfields[1] = (pBitmapInfo->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)pBitmapInfo->bmiColors + 1) : DEFAULT_24BPP_GREEN_MASK;
544 bitfields[2] = (pBitmapInfo->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)pBitmapInfo->bmiColors + 2) : DEFAULT_24BPP_BLUE_MASK;
545 break;
546 default:
547 bitfields[0] = 0;
548 bitfields[1] = 0;
549 bitfields[2] = 0;
550 break;
551 }
552 if(pBits && bitfields[1] == RGB555_GREEN_MASK)
553 {//RGB 555?
554 dprintf(("RGB 555->565 conversion required %x %x %x", bitfields[0], bitfields[1], bitfields[2]));
555
556 int imgsize = CalcBitmapSize(pBitmapInfo->bmiHeader.biBitCount,
557 pBitmapInfo->bmiHeader.biWidth, numlines);
558
559 newbits = (WORD *)malloc(imgsize);
560 if(CPUFeatures & CPUID_MMX) {
561 RGB555to565MMX(newbits, (WORD *)pBits, imgsize/sizeof(WORD));
562 }
563 else RGB555to565(newbits, (WORD *)pBits, imgsize/sizeof(WORD));
564 pBits = newbits;
565 }
566
567 //If upside down, reverse scanlines and call SetDIBits again
568 if(pBitmapInfo->bmiHeader.biHeight < 0 && (pBitmapInfo->bmiHeader.biCompression == BI_RGB ||
569 pBitmapInfo->bmiHeader.biCompression == BI_BITFIELDS))
570 {
571 // upside down
572 INT rc = -1;
573 long lLineByte;
574
575 //TODO: doesn't work if memory is readonly!!
576 ((BITMAPINFO *)pBitmapInfo)->bmiHeader.biHeight = -pBitmapInfo->bmiHeader.biHeight;
577
578 lLineByte = DIB_GetDIBWidthBytes(pBitmapInfo->bmiHeader.biWidth, pBitmapInfo->bmiHeader.biBitCount);
579
580 dprintf(("Reverse bitmap (negative height)"));
581 char *pNewBits = (char *)malloc( lLineByte * numlines );
582 if(pNewBits) {
583 unsigned char *pbSrc = (unsigned char *)pBits + lLineByte * (numlines - 1);
584 unsigned char *pbDst = (unsigned char *)pNewBits;
585 for(int y = 0; y < numlines; y++) {
586 memcpy( pbDst, pbSrc, lLineByte );
587 pbDst += lLineByte;
588 pbSrc -= lLineByte;
589 }
590 ret = O32_SetDIBits(hdc, hBitmap, startscan, numlines, pNewBits, pBitmapInfo, usage);
591 free(pNewBits);
592 }
593 else DebugInt3();
594
595 //TODO: doesn't work if memory is readonly!!
596 ((BITMAPINFO *)pBitmapInfo)->bmiHeader.biHeight = -pBitmapInfo->bmiHeader.biHeight;
597 }
598 else {
599 ret = O32_SetDIBits(hdc, hBitmap, startscan, numlines, pBits, pBitmapInfo, usage);
600 }
601 if(newbits) free(newbits);
602
603 if(DIBSection::getSection() != NULL)
604 {
605 DIBSection *dsect;
606
607 dsect = DIBSection::findObj(hBitmap);
608 if(dsect) {
609 HBITMAP hBmpOld = SelectObject(hdc, hBitmap);
610 dsect->sync(hdc, 0, dsect->GetHeight());
611 SelectObject(hdc, hBmpOld);
612 }
613 }
614
615 return ret;
616}
617//******************************************************************************
618//******************************************************************************
Note: See TracBrowser for help on using the repository browser.