wineps: Add a couple of helper functions to simplify the StretchDIBits implementation.
[wine.git] / dlls / wineps.drv / bitmap.c
blobcb4ea3048be1d0d50dfb89d96a4936a05cdbebe2
1 /*
2 * PostScript driver bitmap functions
4 * Copyright 1998 Huw D M Davies
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <assert.h>
22 #include <stdlib.h>
24 #include "psdrv.h"
25 #include "winbase.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
31 /* Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned. */
32 static inline int get_dib_width_bytes( int width, int depth )
34 int words;
36 switch(depth)
38 case 1: words = (width + 31) / 32; break;
39 case 4: words = (width + 7) / 8; break;
40 case 8: words = (width + 3) / 4; break;
41 case 15:
42 case 16: words = (width + 1) / 2; break;
43 case 24: words = (width * 3 + 3)/4; break;
44 default:
45 WARN("(%d): Unsupported depth\n", depth );
46 /* fall through */
47 case 32: words = width; break;
49 return 4 * words;
52 /* get the bitmap info from either an INFOHEADER or COREHEADER bitmap */
53 static BOOL get_bitmap_info( const void *ptr, LONG *width, LONG *height, WORD *bpp, WORD *compr )
55 const BITMAPINFOHEADER *header = ptr;
57 switch(header->biSize)
59 case sizeof(BITMAPCOREHEADER):
61 const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
62 *width = core->bcWidth;
63 *height = core->bcHeight;
64 *bpp = core->bcBitCount;
65 *compr = 0;
67 return TRUE;
68 case sizeof(BITMAPINFOHEADER):
69 case sizeof(BITMAPV4HEADER):
70 case sizeof(BITMAPV5HEADER):
71 /* V4 and V5 structures are a superset of the INFOHEADER structure */
72 *width = header->biWidth;
73 *height = header->biHeight;
74 *bpp = header->biBitCount;
75 *compr = header->biCompression;
76 return TRUE;
77 default:
78 ERR("(%d): unknown/wrong size for header\n", header->biSize );
79 return FALSE;
84 /***************************************************************************
85 * PSDRV_WriteImageHeader
87 * Helper for PSDRV_StretchDIBits
89 * BUGS
90 * Uses level 2 PostScript
93 static BOOL PSDRV_WriteImageHeader(PHYSDEV dev, const BITMAPINFO *info, INT xDst,
94 INT yDst, INT widthDst, INT heightDst,
95 INT widthSrc, INT heightSrc)
97 switch(info->bmiHeader.biBitCount)
99 case 1:
100 case 4:
101 case 8:
102 PSDRV_WriteIndexColorSpaceBegin(dev, (1 << info->bmiHeader.biBitCount) - 1);
103 PSDRV_WriteRGBQUAD(dev, info->bmiColors, 1 << info->bmiHeader.biBitCount);
104 PSDRV_WriteIndexColorSpaceEnd(dev);
105 break;
107 case 16:
108 case 24:
109 case 32:
111 PSCOLOR pscol;
112 pscol.type = PSCOLOR_RGB;
113 pscol.value.rgb.r = pscol.value.rgb.g = pscol.value.rgb.b = 0.0;
114 PSDRV_WriteSetColor(dev, &pscol);
115 break;
119 PSDRV_WriteImage(dev, info->bmiHeader.biBitCount, xDst, yDst,
120 widthDst, heightDst, widthSrc, heightSrc, FALSE, info->bmiHeader.biHeight < 0);
121 return TRUE;
125 /***************************************************************************
126 * PSDRV_WriteImageMaskHeader
128 * Helper for PSDRV_StretchDIBits
130 * We use the imagemask operator for 1bpp bitmaps since the output
131 * takes much less time for the printer to render.
133 * BUGS
134 * Uses level 2 PostScript
137 static BOOL PSDRV_WriteImageMaskHeader(PHYSDEV dev, const BITMAPINFO *info, INT xDst,
138 INT yDst, INT widthDst, INT heightDst,
139 INT widthSrc, INT heightSrc)
141 PSCOLOR bkgnd, foregnd;
143 assert(info->bmiHeader.biBitCount == 1);
145 /* We'll write the mask with -ve polarity so that
146 the foregnd color corresponds to a bit equal to
147 0 in the bitmap.
149 PSDRV_CreateColor(dev, &foregnd, RGB(info->bmiColors[0].rgbRed,
150 info->bmiColors[0].rgbGreen,
151 info->bmiColors[0].rgbBlue) );
152 PSDRV_CreateColor(dev, &bkgnd, RGB(info->bmiColors[1].rgbRed,
153 info->bmiColors[1].rgbGreen,
154 info->bmiColors[1].rgbBlue) );
156 PSDRV_WriteGSave(dev);
157 PSDRV_WriteNewPath(dev);
158 PSDRV_WriteRectangle(dev, xDst, yDst, widthDst, heightDst);
159 PSDRV_WriteSetColor(dev, &bkgnd);
160 PSDRV_WriteFill(dev);
161 PSDRV_WriteGRestore(dev);
163 PSDRV_WriteSetColor(dev, &foregnd);
164 PSDRV_WriteImage(dev, 1, xDst, yDst, widthDst, heightDst,
165 widthSrc, heightSrc, TRUE, info->bmiHeader.biHeight < 0);
167 return TRUE;
170 static inline DWORD max_rle_size(DWORD size)
172 return size + (size + 127) / 128 + 1;
175 static inline DWORD max_ascii85_size(DWORD size)
177 return (size + 3) / 4 * 5;
180 /***************************************************************************
181 * PSDRV_WriteImageBits
183 static void PSDRV_WriteImageBits( PHYSDEV dev, const BITMAPINFO *info, INT xDst, INT yDst,
184 INT widthDst, INT heightDst, INT widthSrc, INT heightSrc,
185 void *bits, DWORD size )
187 BYTE *rle, *ascii85;
188 DWORD rle_len, ascii85_len;
190 if (info->bmiHeader.biBitCount == 1)
191 /* Use imagemask rather than image */
192 PSDRV_WriteImageMaskHeader(dev, info, xDst, yDst, widthDst, heightDst,
193 widthSrc, heightSrc);
194 else
195 PSDRV_WriteImageHeader(dev, info, xDst, yDst, widthDst, heightDst,
196 widthSrc, heightSrc);
198 rle = HeapAlloc(GetProcessHeap(), 0, max_rle_size(size));
199 rle_len = RLE_encode(bits, size, rle);
200 ascii85 = HeapAlloc(GetProcessHeap(), 0, max_ascii85_size(rle_len));
201 ascii85_len = ASCII85_encode(rle, rle_len, ascii85);
202 HeapFree(GetProcessHeap(), 0, rle);
203 PSDRV_WriteData(dev, ascii85, ascii85_len);
204 PSDRV_WriteSpool(dev, "~>\n", 3);
205 HeapFree(GetProcessHeap(), 0, ascii85);
208 /***************************************************************************
210 * PSDRV_StretchDIBits
212 * BUGS
213 * Doesn't work correctly if xSrc isn't byte aligned - this affects 1 and 4
214 * bit depths.
215 * Compression not implemented.
217 INT PSDRV_StretchDIBits( PHYSDEV dev, INT xDst, INT yDst, INT widthDst,
218 INT heightDst, INT xSrc, INT ySrc, INT widthSrc, INT heightSrc, const void *bits,
219 const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
221 LONG fullSrcWidth, fullSrcHeight;
222 INT stride;
223 WORD bpp, compression;
224 INT line;
225 POINT pt[2];
226 const BYTE *src_ptr;
227 BYTE *dst_ptr, *bitmap;
228 DWORD bitmap_size;
230 TRACE("%p (%d,%d %dx%d) -> (%d,%d %dx%d)\n", dev->hdc,
231 xSrc, ySrc, widthSrc, heightSrc, xDst, yDst, widthDst, heightDst);
233 if (!get_bitmap_info( info, &fullSrcWidth, &fullSrcHeight, &bpp, &compression )) return FALSE;
235 stride = get_dib_width_bytes(fullSrcWidth, bpp);
237 TRACE("full size=%dx%d bpp=%d compression=%d rop=%08x\n", fullSrcWidth,
238 fullSrcHeight, bpp, compression, dwRop);
241 if(compression != BI_RGB) {
242 FIXME("Compression not supported\n");
243 return FALSE;
246 pt[0].x = xDst;
247 pt[0].y = yDst;
248 pt[1].x = xDst + widthDst;
249 pt[1].y = yDst + heightDst;
250 LPtoDP( dev->hdc, pt, 2 );
251 xDst = pt[0].x;
252 yDst = pt[0].y;
253 widthDst = pt[1].x - pt[0].x;
254 heightDst = pt[1].y - pt[0].y;
256 switch(bpp) {
258 case 1:
259 src_ptr = bits;
260 src_ptr += (ySrc * stride);
261 if(xSrc & 7)
262 FIXME("This won't work...\n");
263 bitmap_size = heightSrc * ((widthSrc + 7) / 8);
264 dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
265 for(line = 0; line < heightSrc; line++, src_ptr += stride, dst_ptr += ((widthSrc + 7) / 8))
266 memcpy(dst_ptr, src_ptr + xSrc / 8, (widthSrc + 7) / 8);
267 break;
269 case 4:
270 src_ptr = bits;
271 src_ptr += (ySrc * stride);
272 if(xSrc & 1)
273 FIXME("This won't work...\n");
274 bitmap_size = heightSrc * ((widthSrc + 1) / 2);
275 dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
276 for(line = 0; line < heightSrc; line++, src_ptr += stride, dst_ptr += ((widthSrc + 1) / 2))
277 memcpy(dst_ptr, src_ptr + xSrc/2, (widthSrc+1)/2);
278 break;
280 case 8:
281 src_ptr = bits;
282 src_ptr += (ySrc * stride);
283 bitmap_size = heightSrc * widthSrc;
284 dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
285 for(line = 0; line < heightSrc; line++, src_ptr += stride, dst_ptr += widthSrc)
286 memcpy(dst_ptr, src_ptr + xSrc, widthSrc);
287 break;
289 case 16:
290 src_ptr = bits;
291 src_ptr += (ySrc * stride);
292 bitmap_size = heightSrc * widthSrc * 3;
293 dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
295 for(line = 0; line < heightSrc; line++, src_ptr += stride) {
296 const WORD *words = (const WORD *)src_ptr + xSrc;
297 int i;
298 for(i = 0; i < widthSrc; i++) {
299 BYTE r, g, b;
301 /* We want 0x0 -- 0x1f to map to 0x0 -- 0xff */
302 r = words[i] >> 10 & 0x1f;
303 r = r << 3 | r >> 2;
304 g = words[i] >> 5 & 0x1f;
305 g = g << 3 | g >> 2;
306 b = words[i] & 0x1f;
307 b = b << 3 | b >> 2;
308 dst_ptr[0] = r;
309 dst_ptr[1] = g;
310 dst_ptr[2] = b;
311 dst_ptr += 3;
314 break;
316 case 24:
317 src_ptr = bits;
318 src_ptr += (ySrc * stride);
319 bitmap_size = heightSrc * widthSrc * 3;
320 dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
321 for(line = 0; line < heightSrc; line++, src_ptr += stride) {
322 const BYTE *byte = src_ptr + xSrc * 3;
323 int i;
324 for(i = 0; i < widthSrc; i++) {
325 dst_ptr[0] = byte[i * 3 + 2];
326 dst_ptr[1] = byte[i * 3 + 1];
327 dst_ptr[2] = byte[i * 3];
328 dst_ptr += 3;
331 break;
333 case 32:
334 src_ptr = bits;
335 src_ptr += (ySrc * stride);
336 bitmap_size = heightSrc * widthSrc * 3;
337 dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
338 for(line = 0; line < heightSrc; line++, src_ptr += stride) {
339 const BYTE *byte = src_ptr + xSrc * 4;
340 int i;
341 for(i = 0; i < widthSrc; i++) {
342 dst_ptr[0] = byte[i * 4 + 2];
343 dst_ptr[1] = byte[i * 4 + 1];
344 dst_ptr[2] = byte[i * 4];
345 dst_ptr += 3;
348 break;
350 default:
351 FIXME("Unsupported depth\n");
352 return FALSE;
356 PSDRV_SetClip(dev);
357 PSDRV_WriteGSave(dev);
358 PSDRV_WriteImageBits( dev, info, xDst, yDst, widthDst, heightDst,
359 widthSrc, heightSrc, bitmap, bitmap_size );
360 HeapFree(GetProcessHeap(), 0, bitmap);
361 PSDRV_WriteGRestore(dev);
362 PSDRV_ResetClip(dev);
363 return abs(heightSrc);