1 | /* $Id: blit.cpp,v 1.41 2002-11-26 10:53:06 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 |
|
---|
24 | static ULONG QueryPaletteSize(BITMAPINFOHEADER *pBHdr);
|
---|
25 | ULONG CalcBitmapSize(ULONG cBits, LONG cx, LONG cy);
|
---|
26 |
|
---|
27 | //******************************************************************************
|
---|
28 | //******************************************************************************
|
---|
29 | BOOL 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 | return rc;
|
---|
50 | }
|
---|
51 | }
|
---|
52 | rc = O32_StretchBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, dwRop);
|
---|
53 | if(DIBSection::getSection() != NULL) {
|
---|
54 | DIBSection *destdib = DIBSection::findHDC(hdcDest);
|
---|
55 | if(destdib) {
|
---|
56 | destdib->sync(hdcDest, nYOriginDest, nHeightDest);
|
---|
57 | }
|
---|
58 | }
|
---|
59 | if(rc == FALSE) {
|
---|
60 | dprintf(("!WARNING!: GDI32: StretchBlt returned FALSE; last error %x", rc, GetLastError()));
|
---|
61 | }
|
---|
62 | return rc;
|
---|
63 | }
|
---|
64 | //******************************************************************************
|
---|
65 | //******************************************************************************
|
---|
66 | BOOL WIN32API BitBlt(HDC hdcDest,
|
---|
67 | int nXDest,
|
---|
68 | int nYDest,
|
---|
69 | int nWidth,
|
---|
70 | int nHeight,
|
---|
71 | HDC hdcSrc,
|
---|
72 | int nXSrc,
|
---|
73 | int nYSrc,
|
---|
74 | DWORD dwRop)
|
---|
75 | {
|
---|
76 | BOOL rc;
|
---|
77 |
|
---|
78 | #ifdef DEBUG
|
---|
79 | POINT point1, point2;
|
---|
80 | GetViewportOrgEx(hdcDest, &point1);
|
---|
81 | GetViewportOrgEx(hdcSrc, &point2);
|
---|
82 | dprintf(("BitBlt: Viewport origin dest (%d,%d) src (%d,%d)", point1.x, point1.y, point2.x, point2.y));
|
---|
83 | #endif
|
---|
84 |
|
---|
85 | SetLastError(ERROR_SUCCESS);
|
---|
86 | if(DIBSection::getSection() != NULL)
|
---|
87 | {
|
---|
88 | DIBSection *dsect = DIBSection::findHDC(hdcSrc);
|
---|
89 | if(dsect)
|
---|
90 | {
|
---|
91 | return dsect->BitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, nXSrc, nYSrc, nWidth, nHeight, dwRop);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | dprintf(("GDI32: BitBlt to hdc %X from hdc %x (%d,%d) to (%d,%d), (%d,%d) rop %X\n",
|
---|
95 | hdcDest, hdcSrc, nXSrc, nYSrc, nXDest, nYDest, nWidth, nHeight, dwRop));
|
---|
96 |
|
---|
97 | rc = O32_BitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, dwRop);
|
---|
98 |
|
---|
99 | if(DIBSection::getSection() != NULL) {
|
---|
100 | DIBSection *destdib = DIBSection::findHDC(hdcDest);
|
---|
101 | if(destdib) {
|
---|
102 | destdib->sync(hdcDest, nYDest, nHeight);
|
---|
103 | }
|
---|
104 | }
|
---|
105 | return rc;
|
---|
106 | }
|
---|
107 | //******************************************************************************
|
---|
108 | //******************************************************************************
|
---|
109 | static INT SetDIBitsToDevice_(HDC hdc, INT xDest, INT yDest, DWORD cx,
|
---|
110 | DWORD cy, INT xSrc, INT ySrc,
|
---|
111 | UINT startscan, UINT lines, LPCVOID bits,
|
---|
112 | const BITMAPINFO *info, UINT coloruse)
|
---|
113 | {
|
---|
114 | INT result, imgsize, palsize, height, width;
|
---|
115 | char *ptr;
|
---|
116 | ULONG compression = 0, bmpsize;
|
---|
117 | WORD *newbits = NULL;
|
---|
118 | DWORD bitfields[3];
|
---|
119 |
|
---|
120 | 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",
|
---|
121 | hdc, xDest, yDest, cx, cy, xSrc, ySrc, startscan, lines, (LPVOID) bits, (PBITMAPINFO)info, coloruse));
|
---|
122 |
|
---|
123 | SetLastError(ERROR_SUCCESS);
|
---|
124 | if(info == NULL) {
|
---|
125 | goto invalid_parameter;
|
---|
126 | }
|
---|
127 | height = info->bmiHeader.biHeight;
|
---|
128 | width = info->bmiHeader.biWidth;
|
---|
129 |
|
---|
130 | if (height < 0) height = -height;
|
---|
131 | if (!lines || (startscan >= height)) {
|
---|
132 | goto invalid_parameter;
|
---|
133 | }
|
---|
134 | if (startscan + lines > height) lines = height - startscan;
|
---|
135 |
|
---|
136 | if (ySrc < startscan) ySrc = startscan;
|
---|
137 | else if (ySrc >= startscan + lines) goto invalid_parameter;
|
---|
138 |
|
---|
139 | if (xSrc >= width) goto invalid_parameter;
|
---|
140 |
|
---|
141 | if (ySrc + cy >= startscan + lines) cy = startscan + lines - ySrc;
|
---|
142 |
|
---|
143 | if (xSrc + cx >= width) cx = width - xSrc;
|
---|
144 |
|
---|
145 | if (!cx || !cy) goto invalid_parameter;
|
---|
146 |
|
---|
147 | //SvL: RP7's bitmap size is not correct; fix it here or else
|
---|
148 | // the blit is messed up in Open32
|
---|
149 | imgsize = CalcBitmapSize(info->bmiHeader.biBitCount,
|
---|
150 | info->bmiHeader.biWidth, info->bmiHeader.biHeight);
|
---|
151 | bmpsize = info->bmiHeader.biSizeImage;
|
---|
152 | if(info->bmiHeader.biCompression == 0 && info->bmiHeader.biSizeImage &&
|
---|
153 | info->bmiHeader.biSizeImage < imgsize)
|
---|
154 | {
|
---|
155 | ((BITMAPINFO *)info)->bmiHeader.biSizeImage = imgsize;
|
---|
156 | }
|
---|
157 |
|
---|
158 | switch(info->bmiHeader.biBitCount) {
|
---|
159 | case 15:
|
---|
160 | case 16: //Default if BI_BITFIELDS not set is RGB 555
|
---|
161 | bitfields[0] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *(DWORD *)info->bmiColors : DEFAULT_16BPP_RED_MASK;
|
---|
162 | bitfields[1] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 1) : DEFAULT_16BPP_GREEN_MASK;
|
---|
163 | bitfields[2] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 2) : DEFAULT_16BPP_BLUE_MASK;
|
---|
164 | break;
|
---|
165 |
|
---|
166 | case 24:
|
---|
167 | case 32:
|
---|
168 | bitfields[0] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *(DWORD *)info->bmiColors : DEFAULT_24BPP_RED_MASK;
|
---|
169 | bitfields[1] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 1) : DEFAULT_24BPP_GREEN_MASK;
|
---|
170 | bitfields[2] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 2) : DEFAULT_24BPP_BLUE_MASK;
|
---|
171 | break;
|
---|
172 | default:
|
---|
173 | bitfields[0] = 0;
|
---|
174 | bitfields[1] = 0;
|
---|
175 | bitfields[2] = 0;
|
---|
176 | break;
|
---|
177 | }
|
---|
178 |
|
---|
179 | if(bitfields[1] == 0x3E0)
|
---|
180 | {//RGB 555?
|
---|
181 | dprintf(("RGB 555->565 conversion required %x %x %x", bitfields[0], bitfields[1], bitfields[2]));
|
---|
182 |
|
---|
183 | newbits = (WORD *)malloc(imgsize);
|
---|
184 | if(CPUFeatures & CPUID_MMX) {
|
---|
185 | RGB555to565MMX(newbits, (WORD *)bits, imgsize/sizeof(WORD));
|
---|
186 | }
|
---|
187 | else
|
---|
188 | 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 |
|
---|
237 | invalid_parameter:
|
---|
238 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
239 | return 0;
|
---|
240 | }
|
---|
241 | //******************************************************************************
|
---|
242 | //******************************************************************************
|
---|
243 | INT 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 | //******************************************************************************
|
---|
333 | //******************************************************************************
|
---|
334 | BOOL WIN32API PatBlt(HDC hdc,int nXLeft,int nYLeft,int nWidth,int nHeight,DWORD dwRop)
|
---|
335 | {
|
---|
336 | BOOL rc;
|
---|
337 |
|
---|
338 | dprintf(("PatBlt %x (%d,%d)(%d,%d) %x", hdc, nXLeft,nYLeft,nWidth,nHeight, dwRop));
|
---|
339 | //CB: Open32 bug: negative width/height not supported!
|
---|
340 | if (nWidth < 0)
|
---|
341 | {
|
---|
342 | nXLeft += nWidth+1;
|
---|
343 | nWidth = -nWidth;
|
---|
344 | }
|
---|
345 | if (nHeight < 0)
|
---|
346 | {
|
---|
347 | nYLeft += nHeight+1;
|
---|
348 | nHeight = -nHeight;
|
---|
349 | }
|
---|
350 | rc = O32_PatBlt(hdc,nXLeft,nYLeft,nWidth,nHeight,dwRop);
|
---|
351 | if(rc) {
|
---|
352 | DIBSection *destdib = DIBSection::findHDC(hdc);
|
---|
353 | if(destdib) {
|
---|
354 | destdib->sync(hdc, nYLeft, nHeight);
|
---|
355 | }
|
---|
356 | }
|
---|
357 | return(rc);
|
---|
358 | }
|
---|
359 | //******************************************************************************
|
---|
360 | //******************************************************************************
|
---|
361 | BOOL 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)
|
---|
362 | {
|
---|
363 | return O32_MaskBlt(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12);
|
---|
364 | }
|
---|
365 | //******************************************************************************
|
---|
366 | //******************************************************************************
|
---|
367 | BOOL WIN32API PlgBlt(HDC hdcDest, CONST POINT *lpPoint, HDC hdcSrc, int nXSrc,
|
---|
368 | int nYSrc, int nWidth, int nHeight, HBITMAP hbmMask,
|
---|
369 | int xMast, int yMask)
|
---|
370 | {
|
---|
371 | dprintf(("GDI32: PlgBlt, not implemented\n"));
|
---|
372 | return(FALSE);
|
---|
373 | }
|
---|
374 | //******************************************************************************
|
---|
375 | //******************************************************************************
|
---|
376 | static INT StretchDIBits_(HDC hdc, INT xDst, INT yDst, INT widthDst,
|
---|
377 | INT heightDst, INT xSrc, INT ySrc, INT widthSrc,
|
---|
378 | INT heightSrc, const void *bits,
|
---|
379 | const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
|
---|
380 | {
|
---|
381 | INT rc;
|
---|
382 | DWORD bitfields[3], compression = 0;
|
---|
383 | WORD *newbits = NULL;
|
---|
384 |
|
---|
385 | 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));
|
---|
386 |
|
---|
387 | if(info->bmiHeader.biBitCount == 1) {
|
---|
388 | dprintf(("WARNING: StretchDIBits does NOT work correctly for 1 bpp bitmaps!!"));
|
---|
389 | }
|
---|
390 |
|
---|
391 | if(wUsage == DIB_PAL_COLORS && info->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
|
---|
392 | {
|
---|
393 | // workaround for open32 bug.
|
---|
394 | // If syscolors > 256 and wUsage == DIB_PAL_COLORS.
|
---|
395 |
|
---|
396 | int i;
|
---|
397 | USHORT *pColorIndex = (USHORT *)info->bmiColors;
|
---|
398 | RGBQUAD *pColors = (RGBQUAD *) alloca(info->bmiHeader.biClrUsed *
|
---|
399 | sizeof(RGBQUAD));
|
---|
400 | BITMAPINFO *infoLoc = (BITMAPINFO *) alloca(sizeof(BITMAPINFO) +
|
---|
401 | info->bmiHeader.biClrUsed * sizeof(RGBQUAD));
|
---|
402 |
|
---|
403 | memcpy(infoLoc, info, sizeof(BITMAPINFO));
|
---|
404 |
|
---|
405 | if(GetDIBColorTable(hdc, 0, info->bmiHeader.biClrUsed, pColors) == 0) {
|
---|
406 | dprintf(("ERROR: StretchDIBits: GetDIBColorTable failed!!"));
|
---|
407 | return FALSE;
|
---|
408 | }
|
---|
409 | for(i=0;i<info->bmiHeader.biClrUsed;i++, pColorIndex++)
|
---|
410 | {
|
---|
411 | infoLoc->bmiColors[i] = pColors[*pColorIndex];
|
---|
412 | }
|
---|
413 |
|
---|
414 | rc = O32_StretchDIBits(hdc, xDst, yDst, widthDst, heightDst, xSrc, ySrc,
|
---|
415 | widthSrc, heightSrc, (void *)bits,
|
---|
416 | (PBITMAPINFO)infoLoc, DIB_RGB_COLORS, dwRop);
|
---|
417 |
|
---|
418 | //Open32 always returns height of bitmap (regardless of how many scanlines were copied)
|
---|
419 | if(rc != heightSrc && rc != infoLoc->bmiHeader.biHeight) {
|
---|
420 | dprintf(("StretchDIBits failed with rc %x", rc));
|
---|
421 | }
|
---|
422 | else {
|
---|
423 | rc = heightSrc;
|
---|
424 |
|
---|
425 | DIBSection *destdib = DIBSection::findHDC(hdc);
|
---|
426 | if(destdib) {
|
---|
427 | if(widthDst == widthSrc && heightDst == heightSrc &&
|
---|
428 | destdib->GetBitCount() == infoLoc->bmiHeader.biBitCount &&
|
---|
429 | destdib->GetBitCount() == 8)
|
---|
430 | {
|
---|
431 | destdib->sync(xDst, yDst, widthDst, heightDst, (PVOID)bits);
|
---|
432 | }
|
---|
433 | else destdib->sync(hdc, yDst, heightDst);
|
---|
434 | }
|
---|
435 | }
|
---|
436 |
|
---|
437 | return rc;
|
---|
438 | }
|
---|
439 |
|
---|
440 | switch(info->bmiHeader.biBitCount) {
|
---|
441 | case 15:
|
---|
442 | case 16: //Default if BI_BITFIELDS not set is RGB 555
|
---|
443 | bitfields[0] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *(DWORD *)info->bmiColors : DEFAULT_16BPP_RED_MASK;
|
---|
444 | bitfields[1] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 1) : DEFAULT_16BPP_GREEN_MASK;
|
---|
445 | bitfields[2] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 2) : DEFAULT_16BPP_BLUE_MASK;
|
---|
446 | break;
|
---|
447 | case 24:
|
---|
448 | case 32:
|
---|
449 | bitfields[0] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *(DWORD *)info->bmiColors : DEFAULT_24BPP_RED_MASK;
|
---|
450 | bitfields[1] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 1) : DEFAULT_24BPP_GREEN_MASK;
|
---|
451 | bitfields[2] = (info->bmiHeader.biCompression == BI_BITFIELDS) ? *((DWORD *)info->bmiColors + 2) : DEFAULT_24BPP_BLUE_MASK;
|
---|
452 | break;
|
---|
453 | default:
|
---|
454 | bitfields[0] = 0;
|
---|
455 | bitfields[1] = 0;
|
---|
456 | bitfields[2] = 0;
|
---|
457 | break;
|
---|
458 | }
|
---|
459 | if(bitfields[1] == RGB555_GREEN_MASK)
|
---|
460 | {//RGB 555?
|
---|
461 | dprintf(("RGB 555->565 conversion required %x %x %x", bitfields[0], bitfields[1], bitfields[2]));
|
---|
462 |
|
---|
463 | ULONG imgsize = CalcBitmapSize(info->bmiHeader.biBitCount,
|
---|
464 | widthSrc, heightSrc);
|
---|
465 | ULONG offset = CalcBitmapSize(info->bmiHeader.biBitCount,
|
---|
466 | xSrc, ySrc)/sizeof(WORD);
|
---|
467 | newbits = (WORD *) HeapAlloc(GetProcessHeap(), 0, imgsize);
|
---|
468 | //bugbug (too much)
|
---|
469 | //bugbug
|
---|
470 | if(CPUFeatures & CPUID_MMX) {
|
---|
471 | RGB555to565MMX(newbits, (WORD *)bits+offset, imgsize/sizeof(WORD));
|
---|
472 | }
|
---|
473 | else
|
---|
474 | RGB555to565(newbits, (WORD *)bits+offset, imgsize/sizeof(WORD));
|
---|
475 | bits = newbits;
|
---|
476 | }
|
---|
477 | //SvL: Ignore BI_BITFIELDS type (StretchDIBits fails otherwise)
|
---|
478 | if(info->bmiHeader.biCompression == BI_BITFIELDS) {
|
---|
479 | ((BITMAPINFO *)info)->bmiHeader.biCompression = 0;
|
---|
480 | compression = BI_BITFIELDS;
|
---|
481 | }
|
---|
482 |
|
---|
483 | rc = O32_StretchDIBits(hdc, xDst, yDst, widthDst, heightDst, xSrc, ySrc,
|
---|
484 | widthSrc, heightSrc, (void *)bits,
|
---|
485 | (PBITMAPINFO)info, wUsage, dwRop);
|
---|
486 |
|
---|
487 | if(compression == BI_BITFIELDS) {
|
---|
488 | ((BITMAPINFO *)info)->bmiHeader.biCompression = BI_BITFIELDS;
|
---|
489 | }
|
---|
490 | if(newbits) HeapFree(GetProcessHeap(), 0, newbits);
|
---|
491 |
|
---|
492 | //Open32 always returns height of bitmap (regardless of how many scanlines were copied)
|
---|
493 | if(rc != heightSrc && rc != info->bmiHeader.biHeight) {
|
---|
494 | dprintf(("StretchDIBits failed with rc %x", rc));
|
---|
495 | }
|
---|
496 | else
|
---|
497 | {
|
---|
498 | rc = heightSrc;
|
---|
499 |
|
---|
500 | DIBSection *destdib = DIBSection::findHDC(hdc);
|
---|
501 | if(destdib) {
|
---|
502 | if(widthDst == widthSrc && heightDst == heightSrc &&
|
---|
503 | destdib->GetBitCount() == info->bmiHeader.biBitCount &&
|
---|
504 | destdib->GetBitCount() == 8)
|
---|
505 | {
|
---|
506 | destdib->sync(xDst, yDst, widthDst, heightDst, (PVOID)bits);
|
---|
507 | }
|
---|
508 | else destdib->sync(hdc, yDst, heightDst);
|
---|
509 | }
|
---|
510 | }
|
---|
511 |
|
---|
512 | return rc;
|
---|
513 | }
|
---|
514 | //******************************************************************************
|
---|
515 | //******************************************************************************
|
---|
516 | INT WIN32API StretchDIBits(HDC hdc, INT xDst, INT yDst, INT widthDst,
|
---|
517 | INT heightDst, INT xSrc, INT ySrc, INT widthSrc,
|
---|
518 | INT heightSrc, const void *bits,
|
---|
519 | const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
|
---|
520 | {
|
---|
521 |
|
---|
522 | if(info->bmiHeader.biHeight < 0) {
|
---|
523 | // upside down
|
---|
524 | INT rc = 0;
|
---|
525 | BITMAPINFO newInfo;
|
---|
526 | newInfo.bmiHeader = info->bmiHeader;
|
---|
527 | long lLineByte = ((newInfo.bmiHeader.biWidth * (info->bmiHeader.biBitCount == 15 ? 16 : info->bmiHeader.biBitCount) + 31) / 32) * 4;
|
---|
528 | long lHeight = -newInfo.bmiHeader.biHeight;
|
---|
529 | newInfo.bmiHeader.biHeight = -newInfo.bmiHeader.biHeight;
|
---|
530 |
|
---|
531 | //TODO: doesn't work if memory is readonly!!
|
---|
532 | ((BITMAPINFO *)info)->bmiHeader.biHeight = -info->bmiHeader.biHeight;
|
---|
533 |
|
---|
534 | char *newBits = (char *)malloc( lLineByte * lHeight );
|
---|
535 | if(newBits) {
|
---|
536 | unsigned char *pbSrc = (unsigned char *)bits + lLineByte * (lHeight - 1);
|
---|
537 | unsigned char *pbDst = (unsigned char *)newBits;
|
---|
538 | for(int y = 0; y < lHeight; y++) {
|
---|
539 | memcpy( pbDst, pbSrc, lLineByte );
|
---|
540 | pbDst += lLineByte;
|
---|
541 | pbSrc -= lLineByte;
|
---|
542 | }
|
---|
543 | rc = StretchDIBits_(hdc, xDst, yDst, widthDst, heightDst, xSrc, ySrc, widthSrc, heightSrc, newBits, info, wUsage, dwRop);
|
---|
544 | free( newBits );
|
---|
545 | }
|
---|
546 |
|
---|
547 | //TODO: doesn't work if memory is readonly!!
|
---|
548 | ((BITMAPINFO *)info)->bmiHeader.biHeight = -info->bmiHeader.biHeight;
|
---|
549 | return rc;
|
---|
550 | } else {
|
---|
551 | return StretchDIBits_(hdc, xDst, yDst, widthDst, heightDst, xSrc, ySrc, widthSrc, heightSrc, bits, info, wUsage, dwRop);
|
---|
552 | }
|
---|
553 | }
|
---|
554 | //******************************************************************************
|
---|
555 | //******************************************************************************
|
---|
556 | int WIN32API SetStretchBltMode( HDC arg1, int arg2)
|
---|
557 | {
|
---|
558 | if(DIBSection::getSection() != NULL)
|
---|
559 | {
|
---|
560 | DIBSection *dsect = DIBSection::findHDC(arg1);
|
---|
561 | if(dsect)
|
---|
562 | {
|
---|
563 | dprintf((" - DC is DIBSection\n"));
|
---|
564 | }
|
---|
565 | }
|
---|
566 | return O32_SetStretchBltMode(arg1, arg2);
|
---|
567 | }
|
---|
568 | //******************************************************************************
|
---|
569 | //******************************************************************************
|
---|
570 | int WIN32API GetStretchBltMode( HDC arg1)
|
---|
571 | {
|
---|
572 | return O32_GetStretchBltMode(arg1);
|
---|
573 | }
|
---|
574 | //******************************************************************************
|
---|
575 | //******************************************************************************
|
---|
576 | static ULONG QueryPaletteSize(BITMAPINFOHEADER *pBHdr)
|
---|
577 | {
|
---|
578 | ULONG cbPalette;
|
---|
579 |
|
---|
580 | switch (pBHdr->biBitCount)
|
---|
581 | {
|
---|
582 | case 1:
|
---|
583 | case 4:
|
---|
584 | case 8:
|
---|
585 | cbPalette = (1 << pBHdr->biBitCount) * sizeof(RGBQUAD);
|
---|
586 | break;
|
---|
587 |
|
---|
588 | case 16:
|
---|
589 | case 24:
|
---|
590 | case 32:
|
---|
591 | cbPalette = 0;
|
---|
592 | break;
|
---|
593 |
|
---|
594 | default:
|
---|
595 | dprintf(("QueryPaletteSize: error pBHdr->biBitCount = %d", pBHdr->biBitCount));
|
---|
596 | cbPalette = -1;
|
---|
597 | }
|
---|
598 |
|
---|
599 | return cbPalette;
|
---|
600 | }
|
---|
601 | //******************************************************************************
|
---|
602 | //******************************************************************************
|
---|
603 | ULONG CalcBitmapSize(ULONG cBits, LONG cx, LONG cy)
|
---|
604 | {
|
---|
605 | ULONG alignment;
|
---|
606 | ULONG factor;
|
---|
607 | BOOL flag = TRUE; //true: '*' false: '/'
|
---|
608 |
|
---|
609 | cy = cy < 0 ? -cy : cy;
|
---|
610 |
|
---|
611 | switch(cBits)
|
---|
612 | {
|
---|
613 | case 1:
|
---|
614 | factor = 8;
|
---|
615 | flag = FALSE;
|
---|
616 | break;
|
---|
617 |
|
---|
618 | case 4:
|
---|
619 | factor = 2;
|
---|
620 | flag = FALSE;
|
---|
621 | break;
|
---|
622 |
|
---|
623 | case 8:
|
---|
624 | factor = 1;
|
---|
625 | break;
|
---|
626 |
|
---|
627 | case 16:
|
---|
628 | factor = 2;
|
---|
629 | break;
|
---|
630 |
|
---|
631 | case 24:
|
---|
632 | factor = 3;
|
---|
633 | break;
|
---|
634 |
|
---|
635 | case 32:
|
---|
636 | return cx*cy*4;
|
---|
637 |
|
---|
638 | default:
|
---|
639 | return 0;
|
---|
640 | }
|
---|
641 |
|
---|
642 | if (flag)
|
---|
643 | alignment = (cx = (cx*factor)) % 4;
|
---|
644 | else
|
---|
645 | alignment = (cx = ((cx+factor-1)/factor)) % 4;
|
---|
646 |
|
---|
647 | if (alignment != 0)
|
---|
648 | cx += 4 - alignment;
|
---|
649 |
|
---|
650 | return cx*cy;
|
---|
651 | }
|
---|
652 | //******************************************************************************
|
---|
653 | //******************************************************************************
|
---|