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

Last change on this file since 3418 was 3418, checked in by sandervl, 25 years ago

dib section fixes

File size: 11.3 KB
Line 
1/* $Id: blit.cpp,v 1.10 2000-04-18 11:11:52 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);
25static ULONG 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)\n",
37 hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest));
38 dprintf(("GDI32: StretchBlt Src : %x (%d, %d) size (%d, %d)\n",
39 hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc));
40 SetLastError(0);
41 if(DIBSection::getSection() != NULL)
42 {
43 DIBSection *dsect = DIBSection::findHDC(hdcSrc);
44 if(dsect)
45 {
46 rc = dsect->BitBlt( hdcDest,
47 nXOriginDest, nYOriginDest, nWidthDest, nHeightDest,
48 nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc,
49 dwRop);
50 return rc;
51 }
52 }
53 return O32_StretchBlt(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, dwRop);
54}
55//******************************************************************************
56//******************************************************************************
57BOOL WIN32API BitBlt(HDC hdcDest, int arg2, int arg3, int arg4, int arg5, HDC hdcSrc, int arg7, int arg8, DWORD arg9)
58{
59 BOOL rc;
60
61 SetLastError(0);
62 if(DIBSection::getSection() != NULL) {
63 DIBSection *dsect = DIBSection::findHDC(hdcSrc);
64 if(dsect) {
65 return dsect->BitBlt(hdcDest, arg2, arg3, arg4, arg5, arg7, arg8, arg4, arg5, arg9);
66 }
67 }
68 dprintf(("GDI32: BitBlt to hdc %X from (%d,%d) to (%d,%d), (%d,%d) rop %X\n", hdcDest, arg7, arg8, arg2, arg3, arg4, arg5, arg9));
69 return O32_BitBlt(hdcDest, arg2, arg3, arg4, arg5, hdcSrc, arg7, arg8, arg9);
70}
71//******************************************************************************
72//******************************************************************************
73INT WIN32API SetDIBitsToDevice(HDC hdc, INT xDest, INT yDest, DWORD cx,
74 DWORD cy, INT xSrc, INT ySrc,
75 UINT startscan, UINT lines, LPCVOID bits,
76 const BITMAPINFO *info, UINT coloruse)
77{
78 INT result, imgsize, palsize, height, width;
79 char *ptr;
80 ULONG compression = 0, iHeight, bmpsize;
81 WORD *newbits = 0;
82
83 SetLastError(0);
84 if(info == NULL) {
85 goto invalid_parameter;
86 }
87 height = info->bmiHeader.biHeight;
88 width = info->bmiHeader.biWidth;
89
90 if (height < 0) height = -height;
91 if (!lines || (startscan >= height)) {
92 goto invalid_parameter;
93 }
94 if (startscan + lines > height) lines = height - startscan;
95
96 if (ySrc < startscan) ySrc = startscan;
97 else if (ySrc >= startscan + lines) goto invalid_parameter;
98
99 if (xSrc >= width) goto invalid_parameter;
100
101 if (ySrc + cy >= startscan + lines) cy = startscan + lines - ySrc;
102
103 if (xSrc + cx >= width) cx = width - xSrc;
104
105 if (!cx || !cy) goto invalid_parameter;
106
107 // EB: ->>> Crazy. Nobody seen this Open32 bug ?
108 // Dont't like dirty pointers, but Open32 needs a bit help.
109 // Only tested with winmine.
110 palsize = QueryPaletteSize((BITMAPINFOHEADER*)&info->bmiHeader);
111 imgsize = CalcBitmapSize(info->bmiHeader.biBitCount,
112 info->bmiHeader.biWidth, info->bmiHeader.biHeight);
113 ptr = ((char *)info) + palsize + sizeof(BITMAPINFOHEADER);
114 if(bits >= ptr && bits < ptr + imgsize)
115 {
116 bits = (char *)bits - imgsize +
117 CalcBitmapSize(info->bmiHeader.biBitCount,
118 info->bmiHeader.biWidth, lines);
119 }
120 // EB: <<<-
121
122 //SvL: RP7's bitmap size is not correct; fix it here or else
123 // the blit is messed up in Open32
124 bmpsize = info->bmiHeader.biSizeImage;
125 if(info->bmiHeader.biCompression == 0 && info->bmiHeader.biSizeImage &&
126 info->bmiHeader.biSizeImage < imgsize)
127 {
128 ((BITMAPINFO *)info)->bmiHeader.biSizeImage = imgsize;
129 }
130
131 //SvL: Ignore BI_BITFIELDS type (SetDIBitsToDevice fails otherwise)
132 if(info->bmiHeader.biCompression == BI_BITFIELDS) {
133 DWORD *bitfields = (DWORD *)info->bmiColors;
134
135 ((BITMAPINFO *)info)->bmiHeader.biCompression = 0;
136 compression = BI_BITFIELDS;
137
138 if(*(bitfields+1) == 0x3E0)
139 {//RGB 555?
140 dprintf(("BI_BITFIELDS compression %x %x %x", *bitfields, *(bitfields+1), *(bitfields+2)));
141
142 newbits = (WORD *)malloc(imgsize);
143 if(CPUFeatures & CPUID_MMX) {
144 RGB555to565MMX(newbits, (WORD *)bits, imgsize/sizeof(WORD));
145 }
146 else RGB555to565(newbits, (WORD *)bits, imgsize/sizeof(WORD));
147 bits = newbits;
148 }
149 }
150
151 iHeight = info->bmiHeader.biHeight;
152 if(info->bmiHeader.biHeight < 0) {
153 ((BITMAPINFO *)info)->bmiHeader.biHeight = -info->bmiHeader.biHeight;
154 }
155 result = O32_SetDIBitsToDevice(hdc, xDest, yDest, cx, cy, xSrc, ySrc, startscan, lines, (PVOID) bits, (PBITMAPINFO)info, coloruse);
156 //SvL: Wrong Open32 return value
157 result = (result == TRUE) ? lines : 0;
158
159 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",
160 hdc, xDest, yDest, cx, cy, xSrc, ySrc, startscan, lines, (LPVOID) bits, (PBITMAPINFO)info, coloruse, result));
161 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));
162
163 if(compression == BI_BITFIELDS) {
164 ((BITMAPINFO *)info)->bmiHeader.biCompression = BI_BITFIELDS;
165 if(newbits) free(newbits);
166 }
167 ((BITMAPINFO *)info)->bmiHeader.biHeight = iHeight;
168 ((BITMAPINFO *)info)->bmiHeader.biSizeImage = bmpsize;
169 return result;
170
171invalid_parameter:
172 SetLastError(ERROR_INVALID_PARAMETER);
173 return 0;
174}
175//******************************************************************************
176//******************************************************************************
177BOOL WIN32API PatBlt(HDC hdc,int nXLeft,int nYLeft,int nWidth,int nHeight,DWORD dwRop)
178{
179 BOOL rc;
180
181 //CB: Open32 bug: negative width/height not supported!
182 if (nWidth < 0)
183 {
184 nXLeft += nWidth+1;
185 nWidth = -nWidth;
186 }
187 if (nHeight < 0)
188 {
189 nYLeft += nHeight+1;
190 nHeight = -nHeight;
191 }
192 rc = O32_PatBlt(hdc,nXLeft,nYLeft,nWidth,nHeight,dwRop);
193 if(rc) {
194 DIBSection *destdib = DIBSection::findHDC(hdc);
195 if(destdib) {
196 destdib->sync(hdc, nYLeft, nHeight);
197 }
198 }
199
200 dprintf(("GDI32: PatBlt hdc %x (%d,%d) (%d,%d) returned %d\n",hdc, nXLeft,nYLeft,nWidth,nHeight,rc));
201 return(rc);
202}
203//******************************************************************************
204//******************************************************************************
205BOOL 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)
206{
207 dprintf(("GDI32: MaskBlt"));
208 return O32_MaskBlt(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12);
209}
210//******************************************************************************
211//******************************************************************************
212BOOL WIN32API PlgBlt(HDC hdcDest, CONST POINT *lpPoint, HDC hdcSrc, int nXSrc,
213 int nYSrc, int nWidth, int nHeight, HBITMAP hbmMask,
214 int xMast, int yMask)
215{
216 dprintf(("GDI32: PlgBlt, not implemented\n"));
217 return(FALSE);
218}
219//******************************************************************************
220//******************************************************************************
221int WIN32API SetStretchBltMode( HDC arg1, int arg2)
222{
223 dprintf(("GDI32: SetStretchBltMode 0x%08X, 0x%08X\n",arg1, arg2));
224
225 if(DIBSection::getSection() != NULL)
226 {
227 DIBSection *dsect = DIBSection::findHDC(arg1);
228 if(dsect)
229 {
230 dprintf((" - DC is DIBSection\n"));
231 }
232 }
233 return O32_SetStretchBltMode(arg1, arg2);
234}
235//******************************************************************************
236//******************************************************************************
237int WIN32API GetStretchBltMode( HDC arg1)
238{
239 dprintf(("GDI32: GetStretchBltMode"));
240 return O32_GetStretchBltMode(arg1);
241}
242//******************************************************************************
243//******************************************************************************
244static ULONG QueryPaletteSize(BITMAPINFOHEADER *pBHdr)
245{
246 ULONG cbPalette;
247
248 switch (pBHdr->biBitCount)
249 {
250 case 1:
251 case 4:
252 case 8:
253 cbPalette = (1 << pBHdr->biBitCount) * sizeof(RGBQUAD);
254 break;
255
256 case 16:
257 case 24:
258 case 32:
259 cbPalette = 0;
260 break;
261
262 default:
263 dprintf(("QueryPaletteSize: error pBHdr->biBitCount = %d", pBHdr->biBitCount));
264 cbPalette = -1;
265 }
266
267 return cbPalette;
268}
269//******************************************************************************
270//******************************************************************************
271static ULONG CalcBitmapSize(ULONG cBits, LONG cx, LONG cy)
272{
273 ULONG alignment;
274 ULONG factor;
275 BOOL flag = TRUE; //true: '*' false: '/'
276
277 cy = cy < 0 ? -cy : cy;
278
279 switch(cBits)
280 {
281 case 1:
282 factor = 8;
283 flag = FALSE;
284 break;
285
286 case 4:
287 factor = 2;
288 flag = FALSE;
289 break;
290
291 case 8:
292 factor = 1;
293 break;
294
295 case 16:
296 factor = 2;
297 break;
298
299 case 24:
300 factor = 3;
301 break;
302
303 case 32:
304 return cx*cy;
305
306 default:
307 return 0;
308 }
309
310 if (flag)
311 alignment = (cx = (cx*factor)) % 4;
312 else
313 alignment = (cx = ((cx+factor-1)/factor)) % 4;
314
315 if (alignment != 0)
316 cx += 4 - alignment;
317
318 return cx*cy;
319}
320//******************************************************************************
321//******************************************************************************
Note: See TracBrowser for help on using the repository browser.