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

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

manual flipping of dibsection images

File size: 11.4 KB
Line 
1/* $Id: blit.cpp,v 1.11 2000-05-12 19:14:55 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 result = (result == TRUE) ? cy : 0;
159
160 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",
161 hdc, xDest, yDest, cx, cy, xSrc, ySrc, startscan, lines, (LPVOID) bits, (PBITMAPINFO)info, coloruse, result));
162 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));
163
164 if(compression == BI_BITFIELDS) {
165 ((BITMAPINFO *)info)->bmiHeader.biCompression = BI_BITFIELDS;
166 if(newbits) free(newbits);
167 }
168 ((BITMAPINFO *)info)->bmiHeader.biHeight = iHeight;
169 ((BITMAPINFO *)info)->bmiHeader.biSizeImage = bmpsize;
170 return result;
171
172invalid_parameter:
173 SetLastError(ERROR_INVALID_PARAMETER);
174 return 0;
175}
176//******************************************************************************
177//******************************************************************************
178BOOL WIN32API PatBlt(HDC hdc,int nXLeft,int nYLeft,int nWidth,int nHeight,DWORD dwRop)
179{
180 BOOL rc;
181
182 //CB: Open32 bug: negative width/height not supported!
183 if (nWidth < 0)
184 {
185 nXLeft += nWidth+1;
186 nWidth = -nWidth;
187 }
188 if (nHeight < 0)
189 {
190 nYLeft += nHeight+1;
191 nHeight = -nHeight;
192 }
193 rc = O32_PatBlt(hdc,nXLeft,nYLeft,nWidth,nHeight,dwRop);
194 if(rc) {
195 DIBSection *destdib = DIBSection::findHDC(hdc);
196 if(destdib) {
197 destdib->sync(hdc, nYLeft, nHeight);
198 }
199 }
200
201 dprintf(("GDI32: PatBlt hdc %x (%d,%d) (%d,%d) returned %d\n",hdc, nXLeft,nYLeft,nWidth,nHeight,rc));
202 return(rc);
203}
204//******************************************************************************
205//******************************************************************************
206BOOL 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)
207{
208 dprintf(("GDI32: MaskBlt"));
209 return O32_MaskBlt(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12);
210}
211//******************************************************************************
212//******************************************************************************
213BOOL WIN32API PlgBlt(HDC hdcDest, CONST POINT *lpPoint, HDC hdcSrc, int nXSrc,
214 int nYSrc, int nWidth, int nHeight, HBITMAP hbmMask,
215 int xMast, int yMask)
216{
217 dprintf(("GDI32: PlgBlt, not implemented\n"));
218 return(FALSE);
219}
220//******************************************************************************
221//******************************************************************************
222int WIN32API SetStretchBltMode( HDC arg1, int arg2)
223{
224 dprintf(("GDI32: SetStretchBltMode 0x%08X, 0x%08X\n",arg1, arg2));
225
226 if(DIBSection::getSection() != NULL)
227 {
228 DIBSection *dsect = DIBSection::findHDC(arg1);
229 if(dsect)
230 {
231 dprintf((" - DC is DIBSection\n"));
232 }
233 }
234 return O32_SetStretchBltMode(arg1, arg2);
235}
236//******************************************************************************
237//******************************************************************************
238int WIN32API GetStretchBltMode( HDC arg1)
239{
240 dprintf(("GDI32: GetStretchBltMode"));
241 return O32_GetStretchBltMode(arg1);
242}
243//******************************************************************************
244//******************************************************************************
245static ULONG QueryPaletteSize(BITMAPINFOHEADER *pBHdr)
246{
247 ULONG cbPalette;
248
249 switch (pBHdr->biBitCount)
250 {
251 case 1:
252 case 4:
253 case 8:
254 cbPalette = (1 << pBHdr->biBitCount) * sizeof(RGBQUAD);
255 break;
256
257 case 16:
258 case 24:
259 case 32:
260 cbPalette = 0;
261 break;
262
263 default:
264 dprintf(("QueryPaletteSize: error pBHdr->biBitCount = %d", pBHdr->biBitCount));
265 cbPalette = -1;
266 }
267
268 return cbPalette;
269}
270//******************************************************************************
271//******************************************************************************
272static ULONG CalcBitmapSize(ULONG cBits, LONG cx, LONG cy)
273{
274 ULONG alignment;
275 ULONG factor;
276 BOOL flag = TRUE; //true: '*' false: '/'
277
278 cy = cy < 0 ? -cy : cy;
279
280 switch(cBits)
281 {
282 case 1:
283 factor = 8;
284 flag = FALSE;
285 break;
286
287 case 4:
288 factor = 2;
289 flag = FALSE;
290 break;
291
292 case 8:
293 factor = 1;
294 break;
295
296 case 16:
297 factor = 2;
298 break;
299
300 case 24:
301 factor = 3;
302 break;
303
304 case 32:
305 return cx*cy;
306
307 default:
308 return 0;
309 }
310
311 if (flag)
312 alignment = (cx = (cx*factor)) % 4;
313 else
314 alignment = (cx = ((cx+factor-1)/factor)) % 4;
315
316 if (alignment != 0)
317 cx += 4 - alignment;
318
319 return cx*cy;
320}
321//******************************************************************************
322//******************************************************************************
Note: See TracBrowser for help on using the repository browser.