source: trunk/src/gdi32/blit.cpp@ 8871

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

Handle management updates

File size: 28.2 KB
Line 
1/* $Id: blit.cpp,v 1.39 2002-07-15 10:02:28 sandervl Exp $ */
2
3/*
4 * GDI32 blit 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 <cpuhlp.h>
17#include "misc.h"
18#include "dibsect.h"
19#include "rgbcvt.h"
20
21#define DBG_LOCALLOG DBG_blit
22#include "dbglocal.h"
23
24static ULONG QueryPaletteSize(BITMAPINFOHEADER *pBHdr);
25ULONG CalcBitmapSize(ULONG cBits, LONG cx, LONG cy);
26
27//******************************************************************************
28//******************************************************************************
29BOOL WIN32API StretchBlt(HDC hdcDest, int nXOriginDest, int nYOriginDest,
30 int nWidthDest, int nHeightDest,
31 HDC hdcSrc, int nXOriginSrc, int nYOriginSrc,
32 int nWidthSrc, int nHeightSrc, DWORD dwRop)
33{
34 BOOL rc;
35
36 dprintf(("GDI32: StretchBlt Dest: %x (%d, %d) size (%d, %d) ROP %x",
37 hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, dwRop));
38 dprintf(("GDI32: StretchBlt Src : %x (%d, %d) size (%d, %d)\n",
39 hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc));
40
41 SetLastError(ERROR_SUCCESS);
42 if(DIBSection::getSection() != NULL)
43 {
44 DIBSection *dsect = DIBSection::findHDC(hdcSrc);
45 if(dsect)
46 {
47 rc = dsect->BitBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest,
48 nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, dwRop);
49 dprintf(("GDI32: StretchBlt returned %d", rc));
50 return rc;
51 }
52 }
53 rc = O32_StretchBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, dwRop);
54 if(DIBSection::getSection() != NULL) {
55 DIBSection *destdib = DIBSection::findHDC(hdcDest);
56 if(destdib) {
57 destdib->sync(hdcDest, nYOriginDest, nHeightDest);
58 }
59 }
60 if(rc == FALSE) {
61 dprintf(("!WARNING!: GDI32: StretchBlt returned FALSE; last error %x", rc, GetLastError()));
62 }
63 return rc;
64}
65//******************************************************************************
66//******************************************************************************
67BOOL WIN32API BitBlt(HDC hdcDest,
68 int nXDest,
69 int nYDest,
70 int nWidth,
71 int nHeight,
72 HDC hdcSrc,
73 int nXSrc,
74 int nYSrc,
75 DWORD dwRop)
76{
77 BOOL rc;
78
79#ifdef DEBUG
80 POINT point1, point2;
81 GetViewportOrgEx(hdcDest, &point1);
82 GetViewportOrgEx(hdcSrc, &point2);
83 dprintf(("BitBlt: Viewport origin dest (%d,%d) src (%d,%d)", point1.x, point1.y, point2.x, point2.y));
84#endif
85
86 SetLastError(ERROR_SUCCESS);
87 if(DIBSection::getSection() != NULL)
88 {
89 DIBSection *dsect = DIBSection::findHDC(hdcSrc);
90 if(dsect)
91 {
92 return dsect->BitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, nXSrc, nYSrc, nWidth, nHeight, dwRop);
93 }
94 }
95 dprintf(("GDI32: BitBlt to hdc %X from hdc %x (%d,%d) to (%d,%d), (%d,%d) rop %X\n",
96 hdcDest, hdcSrc, nXSrc, nYSrc, nXDest, nYDest, nWidth, nHeight, dwRop));
97
98 rc = O32_BitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, dwRop);
99
100 if(DIBSection::getSection() != NULL) {
101 DIBSection *destdib = DIBSection::findHDC(hdcDest);
102 if(destdib) {
103 destdib->sync(hdcDest, nYDest, nHeight);
104 }
105 }
106 return rc;
107}
108//******************************************************************************
109//******************************************************************************
110static INT SetDIBitsToDevice_(HDC hdc, INT xDest, INT yDest, DWORD cx,
111 DWORD cy, INT xSrc, INT ySrc,
112 UINT startscan, UINT lines, LPCVOID bits,
113 const BITMAPINFO *info, UINT coloruse)
114{
115 INT result, imgsize, palsize, height, width;
116 char *ptr;
117 ULONG compression = 0, bmpsize;
118 WORD *newbits = NULL;
119 DWORD bitfields[3];
120
121 dprintf(("GDI32: SetDIBitsToDevice hdc:%X xDest:%d yDest:%d, cx:%d, cy:%d, xSrc:%d, ySrc:%d, startscan:%d, lines:%d \nGDI32: bits 0x%X, info 0x%X, coloruse %d",
122 hdc, xDest, yDest, cx, cy, xSrc, ySrc, startscan, lines, (LPVOID) bits, (PBITMAPINFO)info, coloruse));
123
124 SetLastError(ERROR_SUCCESS);
125 if(info == NULL) {
126 goto invalid_parameter;
127 }
128 height = info->bmiHeader.biHeight;
129 width = info->bmiHeader.biWidth;
130
131 if (height < 0) height = -height;
132 if (!lines || (startscan >= height)) {
133 goto invalid_parameter;
134 }
135 if (startscan + lines > height) lines = height - startscan;
136
137 if (ySrc < startscan) ySrc = startscan;
138 else if (ySrc >= startscan + lines) goto invalid_parameter;
139
140 if (xSrc >= width) goto invalid_parameter;
141
142 if (ySrc + cy >= startscan + lines) cy = startscan + lines - ySrc;
143
144 if (xSrc + cx >= width) cx = width - xSrc;
145
146 if (!cx || !cy) goto invalid_parameter;
147
148 //SvL: RP7's bitmap size is not correct; fix it here or else
149 // the blit is messed up in Open32
150 imgsize = CalcBitmapSize(info->bmiHeader.biBitCount,
151 info->bmiHeader.biWidth, info->bmiHeader.biHeight);
152 bmpsize = info->bmiHeader.biSizeImage;
153 if(info->bmiHeader.biCompression == 0 && info->bmiHeader.biSizeImage &&
154 info->bmiHeader.biSizeImage < imgsize)
155 {
156 ((BITMAPINFO *)info)->bmiHeader.biSizeImage = imgsize;
157 }
158
159 switch(info->bmiHeader.biBitCount) {
160 case 15:
161 case 16: //Default if BI_BITFIELDS not set is RGB 555
162 bitfields[0] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *(DWORD *)info->bmiColors : DEFAULT_16BPP_RED_MASK;
163 bitfields[1] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 1) : DEFAULT_16BPP_GREEN_MASK;
164 bitfields[2] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 2) : DEFAULT_16BPP_BLUE_MASK;
165 break;
166
167 case 24:
168 case 32:
169 bitfields[0] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *(DWORD *)info->bmiColors : DEFAULT_24BPP_RED_MASK;
170 bitfields[1] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 1) : DEFAULT_24BPP_GREEN_MASK;
171 bitfields[2] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 2) : DEFAULT_24BPP_BLUE_MASK;
172 break;
173 default:
174 bitfields[0] = 0;
175 bitfields[1] = 0;
176 bitfields[2] = 0;
177 break;
178 }
179
180 if(bitfields[1] == 0x3E0)
181 {//RGB 555?
182 dprintf(("RGB 555->565 conversion required %x %x %x", bitfields[0], bitfields[1], bitfields[2]));
183
184 newbits = (WORD *)malloc(imgsize);
185 if(CPUFeatures & CPUID_MMX) {
186 RGB555to565MMX(newbits, (WORD *)bits, imgsize/sizeof(WORD));
187 }
188 else RGB555to565(newbits, (WORD *)bits, imgsize/sizeof(WORD));
189 bits = newbits;
190 }
191
192 //SvL: Ignore BI_BITFIELDS type (SetDIBitsToDevice fails otherwise)
193 if(info->bmiHeader.biCompression == BI_BITFIELDS) {
194 ((BITMAPINFO *)info)->bmiHeader.biCompression = 0;
195 compression = BI_BITFIELDS;
196 }
197 if(startscan != 0 || lines != info->bmiHeader.biHeight) {
198 dprintf(("WARNING: SetDIBitsToDevice startscan != 0 || lines != info->bmiHeader.biHeight"));
199 dprintf(("info bmp (%d,%d)", info->bmiHeader.biWidth, info->bmiHeader.biHeight));
200 }
201
202 result = O32_StretchDIBits(hdc, xDest, yDest, cx, cy, xSrc, ySrc,
203 cx, cy, (void *)bits,
204 (PBITMAPINFO)info, coloruse, SRCCOPY);
205
206 //Open32 always returns height of bitmap (regardless of how many scanlines were copied)
207 if(result != info->bmiHeader.biHeight) {
208 dprintf(("SetDIBitsToDevice failed with rc %x", result));
209 }
210 else
211 {
212 result = info->bmiHeader.biHeight;
213
214 DIBSection *destdib = DIBSection::findHDC(hdc);
215 if(destdib) {
216 if(cx == info->bmiHeader.biWidth && cy == info->bmiHeader.biHeight &&
217 destdib->GetBitCount() == info->bmiHeader.biBitCount &&
218 destdib->GetBitCount() == 8)
219 {
220 destdib->sync(xDest, yDest, cx, cy, (PVOID)bits);
221 }
222 else destdib->sync(hdc, yDest, cy);
223 }
224 }
225 dprintf(("GDI32: SetDIBitsToDevice hdc:%X xDest:%d yDest:%d, cx:%d, cy:%d, xSrc:%d, ySrc:%d, startscan:%d, lines:%d \nGDI32: bits 0x%X, info 0x%X, coloruse %d returned %d",
226 hdc, xDest, yDest, cx, cy, xSrc, ySrc, startscan, lines, (LPVOID) bits, (PBITMAPINFO)info, coloruse, result));
227 dprintf(("GDI32: SetDIBitsToDevice %d %d %d %d %x %d", info->bmiHeader.biWidth, info->bmiHeader.biHeight, info->bmiHeader.biPlanes, info->bmiHeader.biBitCount, info->bmiHeader.biCompression, info->bmiHeader.biSizeImage));
228
229 if(compression == BI_BITFIELDS) {
230 ((BITMAPINFO *)info)->bmiHeader.biCompression = BI_BITFIELDS;
231 }
232 if(newbits) free(newbits);
233
234 ((BITMAPINFO *)info)->bmiHeader.biSizeImage = bmpsize;
235 return result;
236
237invalid_parameter:
238 SetLastError(ERROR_INVALID_PARAMETER);
239 return 0;
240}
241//******************************************************************************
242//******************************************************************************
243INT WIN32API SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx,
244 DWORD cy, INT xSrc, INT ySrc,
245 UINT startscan, UINT lines, LPCVOID bits,
246 const BITMAPINFO *info, UINT coloruse)
247{
248 static BOOL fMatrox32BppBug = FALSE;
249 INT rc = 0;
250 char *newBits = NULL;
251
252 //If upside down, reverse scanlines and call SetDIBitsToDevice again
253 if(info->bmiHeader.biHeight < 0 && info->bmiHeader.biBitCount != 8 && info->bmiHeader.biCompression == 0) {
254 // upside down
255 INT rc = -1;
256 BITMAPINFO newInfo;
257 newInfo.bmiHeader = info->bmiHeader;
258 long lLineByte = ((newInfo.bmiHeader.biWidth * (info->bmiHeader.biBitCount == 15 ? 16 : info->bmiHeader.biBitCount) + 31) / 32) * 4;
259 long lHeight = -newInfo.bmiHeader.biHeight;
260 newInfo.bmiHeader.biHeight = -info->bmiHeader.biHeight;
261
262 char *newBits = (char *)malloc( lLineByte * lHeight );
263 if(newBits) {
264 unsigned char *pbSrc = (unsigned char *)bits + lLineByte * (lHeight - 1);
265 unsigned char *pbDst = (unsigned char *)newBits;
266 for(int y = 0; y < lHeight; y++) {
267 memcpy( pbDst, pbSrc, lLineByte );
268 pbDst += lLineByte;
269 pbSrc -= lLineByte;
270 }
271 rc = SetDIBitsToDevice( hdc, xDest, yDest, cx, cy, xSrc, ySrc, startscan, lines, (void *)newBits, &newInfo, coloruse );
272 free( newBits );
273 }
274 else DebugInt3();
275
276 return rc;
277 }
278
279 //We must convert 32 bpp bitmap data to 24 bpp on systems with the Matrox
280 //display driver. (GpiDrawBits for 32 bpp fails with insufficient memory error)
281 if(info->bmiHeader.biBitCount == 32 && fMatrox32BppBug)
282 {
283 BITMAPINFO newInfo;
284 newInfo.bmiHeader = info->bmiHeader;
285
286 long lLineWidth;
287 long lHeight = (newInfo.bmiHeader.biHeight > 0) ? newInfo.bmiHeader.biHeight : -newInfo.bmiHeader.biHeight;
288 long lWidth = newInfo.bmiHeader.biWidth;
289
290 newInfo.bmiHeader.biBitCount = 24;
291 newInfo.bmiHeader.biSizeImage = CalcBitmapSize(24, newInfo.bmiHeader.biWidth,
292 newInfo.bmiHeader.biHeight);
293
294 lLineWidth = newInfo.bmiHeader.biSizeImage / lHeight;
295
296 //convert 32 bits bitmap data to 24 bits
297 newBits = (char *)malloc(newInfo.bmiHeader.biSizeImage+16); //extra room needed for copying (too much)
298 if(!newBits) {
299 DebugInt3();
300 return -1;
301 }
302 unsigned char *pbSrc = (unsigned char *)bits;
303 unsigned char *pbDst = (unsigned char *)newBits;
304 //not very efficient
305 for(int i = 0; i < lHeight; i++) {
306 for(int j=0;j<lWidth;j++) {
307 *(DWORD *)pbDst = *(DWORD *)pbSrc;
308 pbSrc += 4;
309 pbDst += 3;
310 }
311 //24 bpp scanline must be aligned at 4 byte boundary
312 pbDst += (lLineWidth - 3*lWidth);
313 }
314 rc = SetDIBitsToDevice_( hdc, xDest, yDest, cx, cy, xSrc, ySrc, startscan, lines, newBits, &newInfo, coloruse );
315 free(newBits);
316 return rc;
317 }
318 rc = SetDIBitsToDevice_( hdc, xDest, yDest, cx, cy, xSrc, ySrc, startscan, lines, bits, info, coloruse );
319
320 if(rc == -1 && info->bmiHeader.biBitCount == 32 && !fMatrox32BppBug)
321 {
322 //The Matrox driver seems to have some difficulty blitting 32bpp
323 //data. (out of memory error) The same operation works fine with SDD.
324 fMatrox32BppBug = TRUE;
325 return SetDIBitsToDevice(hdc, xDest, yDest, cx,
326 cy, xSrc, ySrc,
327 startscan, lines, bits,
328 info, coloruse);
329 }
330 return rc;
331
332//SvL: Breaks startup bitmap of Acrobat Reader 4.05
333#if 0
334 else
335 if(info->bmiHeader.biBitCount == 8 && info->bmiHeader.biCompression == 0 && !(GetDeviceCaps( hdc, RASTERCAPS ) & RC_PALETTE)) {
336 INT rc = 0;
337 // convert 8bit to 24bit
338
339 BITMAPINFO newInfo;
340 newInfo.bmiHeader = info->bmiHeader;
341 newInfo.bmiHeader.biBitCount = 24;
342 long lLineByte24 = ((newInfo.bmiHeader.biWidth * 24 + 31) / 32) * 4;
343 long lLineByte8 = ((newInfo.bmiHeader.biWidth * 8 + 31) / 32) * 4;
344 long lHeight = newInfo.bmiHeader.biHeight;
345 if(lHeight < 0) lHeight = -lHeight;
346
347 char *newBits = (char *)malloc( lLineByte24 * lHeight );
348 if(newBits) {
349 //
350 // Get Palette Entries
351 //
352 PALETTEENTRY aEntries[256];
353 LOGPALETTE *pLog = (LOGPALETTE *)malloc( sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * 256 );
354 pLog->palVersion = 0x300;
355 pLog->palNumEntries = 256;
356
357 HPALETTE hPaletteDummy = CreatePalette( pLog );
358 free( pLog );
359 HPALETTE hPalette = SelectPalette( hdc, hPaletteDummy, FALSE );
360 GetPaletteEntries( hPalette, 0, 255, aEntries );
361 SelectPalette( hdc, hPalette, FALSE );
362 DeleteObject( hPaletteDummy );
363
364 //
365 // convert 8bit to 24bit
366 //
367 if(newInfo.bmiHeader.biHeight > 0) {
368 unsigned char *pbSrc = (unsigned char *)bits;
369 unsigned char *pbDst = (unsigned char *)newBits;
370 for(int y = 0; y < lHeight; y++) {
371 for(int x = 0; x < newInfo.bmiHeader.biWidth; x++) {
372 PALETTEENTRY src = aEntries[pbSrc[x]];
373 pbDst[x * 3 + 0] = src.peBlue;
374 pbDst[x * 3 + 1] = src.peGreen;
375 pbDst[x * 3 + 2] = src.peRed;
376 }
377 pbDst += lLineByte24;
378 pbSrc += lLineByte8;
379 }
380 } else {
381 // upside down
382 newInfo.bmiHeader.biHeight = -info->bmiHeader.biHeight;
383 unsigned char *pbSrc = (unsigned char *)bits + lLineByte8 * (lHeight - 1);
384 unsigned char *pbDst = (unsigned char *)newBits;
385 for(int y = 0; y < lHeight; y++) {
386 for(int x = 0; x < newInfo.bmiHeader.biWidth; x++) {
387 PALETTEENTRY src = aEntries[pbSrc[x]];
388 pbDst[x * 3 + 0] = src.peBlue;
389 pbDst[x * 3 + 1] = src.peGreen;
390 pbDst[x * 3 + 2] = src.peRed;
391 }
392 pbDst += lLineByte24;
393 pbSrc -= lLineByte8;
394 }
395 }
396 rc = SetDIBitsToDevice_( hdc, xDest, yDest, cx, cy, xSrc, ySrc, startscan, lines, (void *)newBits, &newInfo, DIB_RGB_COLORS );
397 free( newBits );
398 }
399 return rc;
400 }
401#endif
402}
403//******************************************************************************
404//******************************************************************************
405BOOL WIN32API PatBlt(HDC hdc,int nXLeft,int nYLeft,int nWidth,int nHeight,DWORD dwRop)
406{
407 BOOL rc;
408
409 dprintf(("PatBlt %x (%d,%d)(%d,%d) %x", hdc, nXLeft,nYLeft,nWidth,nHeight, dwRop));
410 //CB: Open32 bug: negative width/height not supported!
411 if (nWidth < 0)
412 {
413 nXLeft += nWidth+1;
414 nWidth = -nWidth;
415 }
416 if (nHeight < 0)
417 {
418 nYLeft += nHeight+1;
419 nHeight = -nHeight;
420 }
421 rc = O32_PatBlt(hdc,nXLeft,nYLeft,nWidth,nHeight,dwRop);
422 if(rc) {
423 DIBSection *destdib = DIBSection::findHDC(hdc);
424 if(destdib) {
425 destdib->sync(hdc, nYLeft, nHeight);
426 }
427 }
428
429 dprintf(("GDI32: PatBlt hdc %x (%d,%d) (%d,%d) returned %d\n",hdc, nXLeft,nYLeft,nWidth,nHeight,rc));
430 return(rc);
431}
432//******************************************************************************
433//******************************************************************************
434BOOL WIN32API MaskBlt( HDC arg1, int arg2, int arg3, int arg4, int arg5, HDC arg6, int arg7, int arg8, HBITMAP arg9, int arg10, int arg11, DWORD arg12)
435{
436 dprintf(("GDI32: MaskBlt"));
437 return O32_MaskBlt(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12);
438}
439//******************************************************************************
440//******************************************************************************
441BOOL WIN32API PlgBlt(HDC hdcDest, CONST POINT *lpPoint, HDC hdcSrc, int nXSrc,
442 int nYSrc, int nWidth, int nHeight, HBITMAP hbmMask,
443 int xMast, int yMask)
444{
445 dprintf(("GDI32: PlgBlt, not implemented\n"));
446 return(FALSE);
447}
448//******************************************************************************
449//******************************************************************************
450static INT StretchDIBits_(HDC hdc, INT xDst, INT yDst, INT widthDst,
451 INT heightDst, INT xSrc, INT ySrc, INT widthSrc,
452 INT heightSrc, const void *bits,
453 const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
454{
455 INT rc;
456 DWORD bitfields[3], compression = 0;
457 WORD *newbits = NULL;
458
459 dprintf(("GDI32: StretchDIBits %x to (%d,%d) (%d,%d) from (%d,%d) (%d,%d), %x %x %x %x", hdc, xDst, yDst, widthDst, heightDst, xSrc, ySrc, widthSrc, heightSrc, bits, info, wUsage, dwRop));
460
461 if(info->bmiHeader.biBitCount == 1) {
462 dprintf(("WARNING: StretchDIBits does NOT work correctly for 1 bpp bitmaps!!"));
463 }
464
465 if(wUsage == DIB_PAL_COLORS && info->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
466 {
467 // workaround for open32 bug.
468 // If syscolors > 256 and wUsage == DIB_PAL_COLORS.
469
470 int i;
471 USHORT *pColorIndex = (USHORT *)info->bmiColors;
472 RGBQUAD *pColors = (RGBQUAD *) alloca(info->bmiHeader.biClrUsed *
473 sizeof(RGBQUAD));
474 BITMAPINFO *infoLoc = (BITMAPINFO *) alloca(sizeof(BITMAPINFO) +
475 info->bmiHeader.biClrUsed * sizeof(RGBQUAD));
476
477 memcpy(infoLoc, info, sizeof(BITMAPINFO));
478
479 if(GetDIBColorTable(hdc, 0, info->bmiHeader.biClrUsed, pColors) == 0) {
480 dprintf(("ERROR: StretchDIBits: GetDIBColorTable failed!!"));
481 return FALSE;
482 }
483 for(i=0;i<info->bmiHeader.biClrUsed;i++, pColorIndex++)
484 {
485 infoLoc->bmiColors[i] = pColors[*pColorIndex];
486 }
487
488 rc = O32_StretchDIBits(hdc, xDst, yDst, widthDst, heightDst, xSrc, ySrc,
489 widthSrc, heightSrc, (void *)bits,
490 (PBITMAPINFO)infoLoc, DIB_RGB_COLORS, dwRop);
491
492 //Open32 always returns height of bitmap (regardless of how many scanlines were copied)
493 if(rc != heightSrc && rc != infoLoc->bmiHeader.biHeight) {
494 dprintf(("StretchDIBits failed with rc %x", rc));
495 }
496 else {
497 rc = heightSrc;
498
499 DIBSection *destdib = DIBSection::findHDC(hdc);
500 if(destdib) {
501 if(widthDst == widthSrc && heightDst == heightSrc &&
502 destdib->GetBitCount() == infoLoc->bmiHeader.biBitCount &&
503 destdib->GetBitCount() == 8)
504 {
505 destdib->sync(xDst, yDst, widthDst, heightDst, (PVOID)bits);
506 }
507 else destdib->sync(hdc, yDst, heightDst);
508 }
509 }
510
511 return rc;
512 }
513
514 switch(info->bmiHeader.biBitCount) {
515 case 15:
516 case 16: //Default if BI_BITFIELDS not set is RGB 555
517 bitfields[0] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *(DWORD *)info->bmiColors : DEFAULT_16BPP_RED_MASK;
518 bitfields[1] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 1) : DEFAULT_16BPP_GREEN_MASK;
519 bitfields[2] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 2) : DEFAULT_16BPP_BLUE_MASK;
520 break;
521 case 24:
522 case 32:
523 bitfields[0] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *(DWORD *)info->bmiColors : DEFAULT_24BPP_RED_MASK;
524 bitfields[1] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 1) : DEFAULT_24BPP_GREEN_MASK;
525 bitfields[2] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 2) : DEFAULT_24BPP_BLUE_MASK;
526 break;
527 default:
528 bitfields[0] = 0;
529 bitfields[1] = 0;
530 bitfields[2] = 0;
531 break;
532 }
533 if(bitfields[1] == RGB555_GREEN_MASK)
534 {//RGB 555?
535 dprintf(("RGB 555->565 conversion required %x %x %x", bitfields[0], bitfields[1], bitfields[2]));
536
537 int imgsize = CalcBitmapSize(info->bmiHeader.biBitCount,
538 info->bmiHeader.biWidth, info->bmiHeader.biHeight);
539
540 newbits = (WORD *)malloc(imgsize);
541 if(CPUFeatures & CPUID_MMX) {
542 RGB555to565MMX(newbits, (WORD *)bits, imgsize/sizeof(WORD));
543 }
544 else RGB555to565(newbits, (WORD *)bits, imgsize/sizeof(WORD));
545 bits = newbits;
546 }
547 //SvL: Ignore BI_BITFIELDS type (SetDIBitsToDevice fails otherwise)
548 if(info->bmiHeader.biCompression == BI_BITFIELDS) {
549 ((BITMAPINFO *)info)->bmiHeader.biCompression = 0;
550 compression = BI_BITFIELDS;
551 }
552
553 rc = O32_StretchDIBits(hdc, xDst, yDst, widthDst, heightDst, xSrc, ySrc,
554 widthSrc, heightSrc, (void *)bits,
555 (PBITMAPINFO)info, wUsage, dwRop);
556
557 if(compression == BI_BITFIELDS) {
558 ((BITMAPINFO *)info)->bmiHeader.biCompression = BI_BITFIELDS;
559 }
560 if(newbits) free(newbits);
561
562 //Open32 always returns height of bitmap (regardless of how many scanlines were copied)
563 if(rc != heightSrc && rc != info->bmiHeader.biHeight) {
564 dprintf(("StretchDIBits failed with rc %x", rc));
565 }
566 else
567 {
568 rc = heightSrc;
569
570 DIBSection *destdib = DIBSection::findHDC(hdc);
571 if(destdib) {
572 if(widthDst == widthSrc && heightDst == heightSrc &&
573 destdib->GetBitCount() == info->bmiHeader.biBitCount &&
574 destdib->GetBitCount() == 8)
575 {
576 destdib->sync(xDst, yDst, widthDst, heightDst, (PVOID)bits);
577 }
578 else destdib->sync(hdc, yDst, heightDst);
579 }
580 }
581
582 return rc;
583}
584//******************************************************************************
585//******************************************************************************
586INT WIN32API StretchDIBits(HDC hdc, INT xDst, INT yDst, INT widthDst,
587 INT heightDst, INT xSrc, INT ySrc, INT widthSrc,
588 INT heightSrc, const void *bits,
589 const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
590{
591
592 if(info->bmiHeader.biHeight < 0) {
593 // upside down
594 INT rc = 0;
595 BITMAPINFO newInfo;
596 newInfo.bmiHeader = info->bmiHeader;
597 long lLineByte = ((newInfo.bmiHeader.biWidth * (info->bmiHeader.biBitCount == 15 ? 16 : info->bmiHeader.biBitCount) + 31) / 32) * 4;
598 long lHeight = -newInfo.bmiHeader.biHeight;
599 newInfo.bmiHeader.biHeight = -newInfo.bmiHeader.biHeight;
600
601 //TODO: doesn't work if memory is readonly!!
602 ((BITMAPINFO *)info)->bmiHeader.biHeight = -info->bmiHeader.biHeight;
603
604 char *newBits = (char *)malloc( lLineByte * lHeight );
605 if(newBits) {
606 unsigned char *pbSrc = (unsigned char *)bits + lLineByte * (lHeight - 1);
607 unsigned char *pbDst = (unsigned char *)newBits;
608 for(int y = 0; y < lHeight; y++) {
609 memcpy( pbDst, pbSrc, lLineByte );
610 pbDst += lLineByte;
611 pbSrc -= lLineByte;
612 }
613 rc = StretchDIBits_(hdc, xDst, yDst, widthDst, heightDst, xSrc, ySrc, widthSrc, heightSrc, newBits, info, wUsage, dwRop);
614 free( newBits );
615 }
616
617 //TODO: doesn't work if memory is readonly!!
618 ((BITMAPINFO *)info)->bmiHeader.biHeight = -info->bmiHeader.biHeight;
619 return rc;
620 } else {
621 return StretchDIBits_(hdc, xDst, yDst, widthDst, heightDst, xSrc, ySrc, widthSrc, heightSrc, bits, info, wUsage, dwRop);
622 }
623}
624//******************************************************************************
625//******************************************************************************
626int WIN32API SetStretchBltMode( HDC arg1, int arg2)
627{
628 dprintf(("GDI32: SetStretchBltMode 0x%08X, 0x%08X\n",arg1, arg2));
629
630 if(DIBSection::getSection() != NULL)
631 {
632 DIBSection *dsect = DIBSection::findHDC(arg1);
633 if(dsect)
634 {
635 dprintf((" - DC is DIBSection\n"));
636 }
637 }
638 return O32_SetStretchBltMode(arg1, arg2);
639}
640//******************************************************************************
641//******************************************************************************
642int WIN32API GetStretchBltMode( HDC arg1)
643{
644 dprintf(("GDI32: GetStretchBltMode"));
645 return O32_GetStretchBltMode(arg1);
646}
647//******************************************************************************
648//******************************************************************************
649static ULONG QueryPaletteSize(BITMAPINFOHEADER *pBHdr)
650{
651 ULONG cbPalette;
652
653 switch (pBHdr->biBitCount)
654 {
655 case 1:
656 case 4:
657 case 8:
658 cbPalette = (1 << pBHdr->biBitCount) * sizeof(RGBQUAD);
659 break;
660
661 case 16:
662 case 24:
663 case 32:
664 cbPalette = 0;
665 break;
666
667 default:
668 dprintf(("QueryPaletteSize: error pBHdr->biBitCount = %d", pBHdr->biBitCount));
669 cbPalette = -1;
670 }
671
672 return cbPalette;
673}
674//******************************************************************************
675//******************************************************************************
676ULONG CalcBitmapSize(ULONG cBits, LONG cx, LONG cy)
677{
678 ULONG alignment;
679 ULONG factor;
680 BOOL flag = TRUE; //true: '*' false: '/'
681
682 cy = cy < 0 ? -cy : cy;
683
684 switch(cBits)
685 {
686 case 1:
687 factor = 8;
688 flag = FALSE;
689 break;
690
691 case 4:
692 factor = 2;
693 flag = FALSE;
694 break;
695
696 case 8:
697 factor = 1;
698 break;
699
700 case 16:
701 factor = 2;
702 break;
703
704 case 24:
705 factor = 3;
706 break;
707
708 case 32:
709 return cx*cy*4;
710
711 default:
712 return 0;
713 }
714
715 if (flag)
716 alignment = (cx = (cx*factor)) % 4;
717 else
718 alignment = (cx = ((cx+factor-1)/factor)) % 4;
719
720 if (alignment != 0)
721 cx += 4 - alignment;
722
723 return cx*cy;
724}
725//******************************************************************************
726//******************************************************************************
Note: See TracBrowser for help on using the repository browser.