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

Last change on this file since 2602 was 2602, checked in by dengert, 26 years ago

speedup conversion RGB555 -> RGB565

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