winhttp/tests: Initialize a variant with a known value.
[wine/multimedia.git] / dlls / wineps.drv / bitmap.c
blobfd153badebe8a33f4c15052c93a89f95e1c9daaf
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 return ((width * depth + 31) / 8) & ~3;
38 /***************************************************************************
39 * PSDRV_WriteImageHeader
41 * Helper for PSDRV_StretchDIBits
43 * BUGS
44 * Uses level 2 PostScript
47 static BOOL PSDRV_WriteImageHeader(PHYSDEV dev, const BITMAPINFO *info, INT xDst,
48 INT yDst, INT widthDst, INT heightDst,
49 INT widthSrc, INT heightSrc)
51 switch(info->bmiHeader.biBitCount)
53 case 1:
54 case 4:
55 case 8:
56 PSDRV_WriteIndexColorSpaceBegin(dev, (1 << info->bmiHeader.biBitCount) - 1);
57 PSDRV_WriteRGBQUAD(dev, info->bmiColors, 1 << info->bmiHeader.biBitCount);
58 PSDRV_WriteIndexColorSpaceEnd(dev);
59 break;
61 case 16:
62 case 24:
63 case 32:
65 PSCOLOR pscol;
66 pscol.type = PSCOLOR_RGB;
67 pscol.value.rgb.r = pscol.value.rgb.g = pscol.value.rgb.b = 0.0;
68 PSDRV_WriteSetColor(dev, &pscol);
69 break;
73 PSDRV_WriteImage(dev, info->bmiHeader.biBitCount, xDst, yDst,
74 widthDst, heightDst, widthSrc, heightSrc, FALSE, info->bmiHeader.biHeight < 0);
75 return TRUE;
79 /***************************************************************************
80 * PSDRV_WriteImageMaskHeader
82 * Helper for PSDRV_StretchDIBits
84 * We use the imagemask operator for 1bpp bitmaps since the output
85 * takes much less time for the printer to render.
87 * BUGS
88 * Uses level 2 PostScript
91 static BOOL PSDRV_WriteImageMaskHeader(PHYSDEV dev, const BITMAPINFO *info, INT xDst,
92 INT yDst, INT widthDst, INT heightDst,
93 INT widthSrc, INT heightSrc)
95 PSCOLOR bkgnd, foregnd;
97 assert(info->bmiHeader.biBitCount == 1);
99 /* We'll write the mask with -ve polarity so that
100 the foregnd color corresponds to a bit equal to
101 0 in the bitmap.
103 PSDRV_CreateColor(dev, &foregnd, RGB(info->bmiColors[0].rgbRed,
104 info->bmiColors[0].rgbGreen,
105 info->bmiColors[0].rgbBlue) );
106 PSDRV_CreateColor(dev, &bkgnd, RGB(info->bmiColors[1].rgbRed,
107 info->bmiColors[1].rgbGreen,
108 info->bmiColors[1].rgbBlue) );
110 PSDRV_WriteGSave(dev);
111 PSDRV_WriteNewPath(dev);
112 PSDRV_WriteRectangle(dev, xDst, yDst, widthDst, heightDst);
113 PSDRV_WriteSetColor(dev, &bkgnd);
114 PSDRV_WriteFill(dev);
115 PSDRV_WriteGRestore(dev);
117 PSDRV_WriteSetColor(dev, &foregnd);
118 PSDRV_WriteImage(dev, 1, xDst, yDst, widthDst, heightDst,
119 widthSrc, heightSrc, TRUE, info->bmiHeader.biHeight < 0);
121 return TRUE;
124 static inline DWORD max_rle_size(DWORD size)
126 return size + (size + 127) / 128 + 1;
129 static inline DWORD max_ascii85_size(DWORD size)
131 return (size + 3) / 4 * 5;
134 static void free_heap_bits( struct gdi_image_bits *bits )
136 HeapFree( GetProcessHeap(), 0, bits->ptr );
139 /***************************************************************************
140 * PSDRV_WriteImageBits
142 static void PSDRV_WriteImageBits( PHYSDEV dev, const BITMAPINFO *info, INT xDst, INT yDst,
143 INT widthDst, INT heightDst, INT widthSrc, INT heightSrc,
144 void *bits, DWORD size )
146 BYTE *rle, *ascii85;
147 DWORD rle_len, ascii85_len;
149 if (info->bmiHeader.biBitCount == 1)
150 /* Use imagemask rather than image */
151 PSDRV_WriteImageMaskHeader(dev, info, xDst, yDst, widthDst, heightDst,
152 widthSrc, heightSrc);
153 else
154 PSDRV_WriteImageHeader(dev, info, xDst, yDst, widthDst, heightDst,
155 widthSrc, heightSrc);
157 rle = HeapAlloc(GetProcessHeap(), 0, max_rle_size(size));
158 rle_len = RLE_encode(bits, size, rle);
159 ascii85 = HeapAlloc(GetProcessHeap(), 0, max_ascii85_size(rle_len));
160 ascii85_len = ASCII85_encode(rle, rle_len, ascii85);
161 HeapFree(GetProcessHeap(), 0, rle);
162 PSDRV_WriteData(dev, ascii85, ascii85_len);
163 PSDRV_WriteSpool(dev, "~>\n", 3);
164 HeapFree(GetProcessHeap(), 0, ascii85);
167 /***********************************************************************
168 * PSDRV_PutImage
170 DWORD PSDRV_PutImage( PHYSDEV dev, HBITMAP hbitmap, HRGN clip, BITMAPINFO *info,
171 const struct gdi_image_bits *bits, struct bitblt_coords *src,
172 struct bitblt_coords *dst, DWORD rop )
174 int src_stride, dst_stride, size, x, y, width, height, bit_offset;
175 int dst_x, dst_y, dst_width, dst_height;
176 unsigned char *src_ptr, *dst_ptr;
177 struct gdi_image_bits dst_bits;
179 if (hbitmap) return ERROR_NOT_SUPPORTED;
181 if (info->bmiHeader.biPlanes != 1) goto update_format;
182 if (info->bmiHeader.biCompression != BI_RGB) goto update_format;
183 if (info->bmiHeader.biBitCount == 16 || info->bmiHeader.biBitCount == 32) goto update_format;
184 if (!bits) return ERROR_SUCCESS; /* just querying the format */
186 TRACE( "bpp %u %s -> %s\n", info->bmiHeader.biBitCount, wine_dbgstr_rect(&src->visrect),
187 wine_dbgstr_rect(&dst->visrect) );
189 width = src->visrect.right - src->visrect.left;
190 height = src->visrect.bottom - src->visrect.top;
191 src_stride = get_dib_width_bytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
192 dst_stride = (width * info->bmiHeader.biBitCount + 7) / 8;
194 src_ptr = bits->ptr;
195 if (info->bmiHeader.biHeight > 0)
196 src_ptr += (info->bmiHeader.biHeight - src->visrect.bottom) * src_stride;
197 else
198 src_ptr += src->visrect.top * src_stride;
199 bit_offset = src->visrect.left * info->bmiHeader.biBitCount;
200 src_ptr += bit_offset / 8;
201 bit_offset &= 7;
202 if (bit_offset) FIXME( "pos %s not supported\n", wine_dbgstr_rect(&src->visrect) );
203 size = height * dst_stride;
205 if (src_stride != dst_stride || (info->bmiHeader.biBitCount == 24 && !bits->is_copy))
207 if (!(dst_bits.ptr = HeapAlloc( GetProcessHeap(), 0, size ))) return ERROR_OUTOFMEMORY;
208 dst_bits.is_copy = TRUE;
209 dst_bits.free = free_heap_bits;
211 else
213 dst_bits.ptr = src_ptr;
214 dst_bits.is_copy = bits->is_copy;
215 dst_bits.free = NULL;
217 dst_ptr = dst_bits.ptr;
219 switch (info->bmiHeader.biBitCount)
221 case 1:
222 case 4:
223 case 8:
224 if (src_stride != dst_stride)
225 for (y = 0; y < height; y++, src_ptr += src_stride, dst_ptr += dst_stride)
226 memcpy( dst_ptr, src_ptr, dst_stride );
227 break;
228 case 24:
229 if (dst_ptr != src_ptr)
230 for (y = 0; y < height; y++, src_ptr += src_stride, dst_ptr += dst_stride)
231 for (x = 0; x < width; x++)
233 dst_ptr[x * 3] = src_ptr[x * 3 + 2];
234 dst_ptr[x * 3 + 1] = src_ptr[x * 3 + 1];
235 dst_ptr[x * 3 + 2] = src_ptr[x * 3];
237 else /* swap R and B in place */
238 for (y = 0; y < height; y++, src_ptr += src_stride, dst_ptr += dst_stride)
239 for (x = 0; x < width; x++)
241 unsigned char tmp = dst_ptr[x * 3];
242 dst_ptr[x * 3] = dst_ptr[x * 3 + 2];
243 dst_ptr[x * 3 + 2] = tmp;
245 break;
248 dst_x = dst->visrect.left;
249 dst_y = dst->visrect.top,
250 dst_width = dst->visrect.right - dst->visrect.left;
251 dst_height = dst->visrect.bottom - dst->visrect.top;
252 if (src->width * dst->width < 0)
254 dst_x += dst_width;
255 dst_width = -dst_width;
257 if (src->height * dst->height < 0)
259 dst_y += dst_height;
260 dst_height = -dst_height;
263 PSDRV_SetClip(dev);
264 PSDRV_WriteGSave(dev);
265 if (clip) PSDRV_AddClip( dev, clip );
266 PSDRV_WriteImageBits( dev, info, dst_x, dst_y, dst_width, dst_height,
267 width, height, dst_bits.ptr, size );
268 PSDRV_WriteGRestore(dev);
269 PSDRV_ResetClip(dev);
270 if (dst_bits.free) dst_bits.free( &dst_bits );
271 return ERROR_SUCCESS;
273 update_format:
274 info->bmiHeader.biPlanes = 1;
275 if (info->bmiHeader.biBitCount > 8) info->bmiHeader.biBitCount = 24;
276 info->bmiHeader.biCompression = BI_RGB;
277 return ERROR_BAD_FORMAT;
280 /***************************************************************************
282 * PSDRV_StretchDIBits
284 * BUGS
285 * Doesn't work correctly if xSrc isn't byte aligned - this affects 1 and 4
286 * bit depths.
287 * Compression not implemented.
289 INT PSDRV_StretchDIBits( PHYSDEV dev, INT xDst, INT yDst, INT widthDst,
290 INT heightDst, INT xSrc, INT ySrc, INT widthSrc, INT heightSrc, const void *bits,
291 BITMAPINFO *info, UINT wUsage, DWORD dwRop )
293 INT stride;
294 INT line;
295 POINT pt[2];
296 const BYTE *src_ptr;
297 BYTE *dst_ptr, *bitmap;
298 DWORD bitmap_size;
300 TRACE("%p (%d,%d %dx%d) -> (%d,%d %dx%d)\n", dev->hdc,
301 xSrc, ySrc, widthSrc, heightSrc, xDst, yDst, widthDst, heightDst);
303 stride = get_dib_width_bytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
305 TRACE("full size=%dx%d bpp=%d compression=%d rop=%08x\n", info->bmiHeader.biWidth,
306 info->bmiHeader.biHeight, info->bmiHeader.biBitCount, info->bmiHeader.biCompression, dwRop);
309 if(info->bmiHeader.biCompression != BI_RGB) {
310 FIXME("Compression not supported\n");
311 return FALSE;
314 pt[0].x = xDst;
315 pt[0].y = yDst;
316 pt[1].x = xDst + widthDst;
317 pt[1].y = yDst + heightDst;
318 LPtoDP( dev->hdc, pt, 2 );
319 xDst = pt[0].x;
320 yDst = pt[0].y;
321 widthDst = pt[1].x - pt[0].x;
322 heightDst = pt[1].y - pt[0].y;
324 switch (info->bmiHeader.biBitCount) {
326 case 1:
327 src_ptr = bits;
328 src_ptr += (ySrc * stride);
329 if(xSrc & 7)
330 FIXME("This won't work...\n");
331 bitmap_size = heightSrc * ((widthSrc + 7) / 8);
332 dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
333 for(line = 0; line < heightSrc; line++, src_ptr += stride, dst_ptr += ((widthSrc + 7) / 8))
334 memcpy(dst_ptr, src_ptr + xSrc / 8, (widthSrc + 7) / 8);
335 break;
337 case 4:
338 src_ptr = bits;
339 src_ptr += (ySrc * stride);
340 if(xSrc & 1)
341 FIXME("This won't work...\n");
342 bitmap_size = heightSrc * ((widthSrc + 1) / 2);
343 dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
344 for(line = 0; line < heightSrc; line++, src_ptr += stride, dst_ptr += ((widthSrc + 1) / 2))
345 memcpy(dst_ptr, src_ptr + xSrc/2, (widthSrc+1)/2);
346 break;
348 case 8:
349 src_ptr = bits;
350 src_ptr += (ySrc * stride);
351 bitmap_size = heightSrc * widthSrc;
352 dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
353 for(line = 0; line < heightSrc; line++, src_ptr += stride, dst_ptr += widthSrc)
354 memcpy(dst_ptr, src_ptr + xSrc, widthSrc);
355 break;
357 case 16:
358 src_ptr = bits;
359 src_ptr += (ySrc * stride);
360 bitmap_size = heightSrc * widthSrc * 3;
361 dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
363 for(line = 0; line < heightSrc; line++, src_ptr += stride) {
364 const WORD *words = (const WORD *)src_ptr + xSrc;
365 int i;
366 for(i = 0; i < widthSrc; i++) {
367 BYTE r, g, b;
369 /* We want 0x0 -- 0x1f to map to 0x0 -- 0xff */
370 r = words[i] >> 10 & 0x1f;
371 r = r << 3 | r >> 2;
372 g = words[i] >> 5 & 0x1f;
373 g = g << 3 | g >> 2;
374 b = words[i] & 0x1f;
375 b = b << 3 | b >> 2;
376 dst_ptr[0] = r;
377 dst_ptr[1] = g;
378 dst_ptr[2] = b;
379 dst_ptr += 3;
382 break;
384 case 24:
385 src_ptr = bits;
386 src_ptr += (ySrc * stride);
387 bitmap_size = heightSrc * widthSrc * 3;
388 dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
389 for(line = 0; line < heightSrc; line++, src_ptr += stride) {
390 const BYTE *byte = src_ptr + xSrc * 3;
391 int i;
392 for(i = 0; i < widthSrc; i++) {
393 dst_ptr[0] = byte[i * 3 + 2];
394 dst_ptr[1] = byte[i * 3 + 1];
395 dst_ptr[2] = byte[i * 3];
396 dst_ptr += 3;
399 break;
401 case 32:
402 src_ptr = bits;
403 src_ptr += (ySrc * stride);
404 bitmap_size = heightSrc * widthSrc * 3;
405 dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
406 for(line = 0; line < heightSrc; line++, src_ptr += stride) {
407 const BYTE *byte = src_ptr + xSrc * 4;
408 int i;
409 for(i = 0; i < widthSrc; i++) {
410 dst_ptr[0] = byte[i * 4 + 2];
411 dst_ptr[1] = byte[i * 4 + 1];
412 dst_ptr[2] = byte[i * 4];
413 dst_ptr += 3;
416 break;
418 default:
419 FIXME("Unsupported depth\n");
420 return FALSE;
424 PSDRV_SetClip(dev);
425 PSDRV_WriteGSave(dev);
426 PSDRV_WriteImageBits( dev, info, xDst, yDst, widthDst, heightDst,
427 widthSrc, heightSrc, bitmap, bitmap_size );
428 HeapFree(GetProcessHeap(), 0, bitmap);
429 PSDRV_WriteGRestore(dev);
430 PSDRV_ResetClip(dev);
431 return abs(heightSrc);