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
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 )
38 case 1: words = (width + 31) / 32; break;
39 case 4: words = (width + 7) / 8; break;
40 case 8: words = (width + 3) / 4; break;
42 case 16: words = (width + 1) / 2; break;
43 case 24: words = (width * 3 + 3)/4; break;
45 WARN("(%d): Unsupported depth\n", depth );
47 case 32: words = width; break;
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;
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;
78 ERR("(%d): unknown/wrong size for header\n", header->biSize );
84 /***************************************************************************
85 * PSDRV_WriteImageHeader
87 * Helper for PSDRV_StretchDIBits
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)
102 PSDRV_WriteIndexColorSpaceBegin(dev, (1 << info->bmiHeader.biBitCount) - 1);
103 PSDRV_WriteRGBQUAD(dev, info->bmiColors, 1 << info->bmiHeader.biBitCount);
104 PSDRV_WriteIndexColorSpaceEnd(dev);
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);
119 PSDRV_WriteImage(dev, info->bmiHeader.biBitCount, xDst, yDst,
120 widthDst, heightDst, widthSrc, heightSrc, FALSE, info->bmiHeader.biHeight < 0);
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.
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
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);
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 static void free_heap_bits( struct gdi_image_bits *bits )
182 HeapFree( GetProcessHeap(), 0, bits->ptr );
185 /***************************************************************************
186 * PSDRV_WriteImageBits
188 static void PSDRV_WriteImageBits( PHYSDEV dev, const BITMAPINFO *info, INT xDst, INT yDst,
189 INT widthDst, INT heightDst, INT widthSrc, INT heightSrc,
190 void *bits, DWORD size )
193 DWORD rle_len, ascii85_len;
195 if (info->bmiHeader.biBitCount == 1)
196 /* Use imagemask rather than image */
197 PSDRV_WriteImageMaskHeader(dev, info, xDst, yDst, widthDst, heightDst,
198 widthSrc, heightSrc);
200 PSDRV_WriteImageHeader(dev, info, xDst, yDst, widthDst, heightDst,
201 widthSrc, heightSrc);
203 rle = HeapAlloc(GetProcessHeap(), 0, max_rle_size(size));
204 rle_len = RLE_encode(bits, size, rle);
205 ascii85 = HeapAlloc(GetProcessHeap(), 0, max_ascii85_size(rle_len));
206 ascii85_len = ASCII85_encode(rle, rle_len, ascii85);
207 HeapFree(GetProcessHeap(), 0, rle);
208 PSDRV_WriteData(dev, ascii85, ascii85_len);
209 PSDRV_WriteSpool(dev, "~>\n", 3);
210 HeapFree(GetProcessHeap(), 0, ascii85);
213 /***********************************************************************
216 DWORD PSDRV_PutImage( PHYSDEV dev, HBITMAP hbitmap, HRGN clip, BITMAPINFO *info,
217 const struct gdi_image_bits *bits, struct bitblt_coords *src,
218 struct bitblt_coords *dst, DWORD rop )
220 int src_stride, dst_stride, size, x, y, width, height, bit_offset;
221 int dst_x, dst_y, dst_width, dst_height;
222 unsigned char *src_ptr, *dst_ptr;
223 struct gdi_image_bits dst_bits;
225 if (hbitmap) return ERROR_NOT_SUPPORTED;
227 if (info->bmiHeader.biPlanes != 1) goto update_format;
228 if (info->bmiHeader.biCompression != BI_RGB) goto update_format;
229 if (info->bmiHeader.biBitCount == 16 || info->bmiHeader.biBitCount == 32) goto update_format;
230 if (!bits) return ERROR_SUCCESS; /* just querying the format */
232 TRACE( "bpp %u %s -> %s\n", info->bmiHeader.biBitCount, wine_dbgstr_rect(&src->visrect),
233 wine_dbgstr_rect(&dst->visrect) );
235 width = src->visrect.right - src->visrect.left;
236 height = src->visrect.bottom - src->visrect.top;
237 src_stride = get_dib_width_bytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
238 dst_stride = (width * info->bmiHeader.biBitCount + 7) / 8;
241 if (info->bmiHeader.biHeight > 0)
242 src_ptr += (info->bmiHeader.biHeight - src->visrect.bottom) * src_stride;
244 src_ptr += src->visrect.top * src_stride;
245 bit_offset = src->visrect.left * info->bmiHeader.biBitCount;
246 src_ptr += bit_offset / 8;
248 if (bit_offset) FIXME( "pos %s not supported\n", wine_dbgstr_rect(&src->visrect) );
249 size = height * dst_stride;
251 if (src_stride != dst_stride || (info->bmiHeader.biBitCount == 24 && !bits->is_copy))
253 if (!(dst_bits.ptr = HeapAlloc( GetProcessHeap(), 0, size ))) return ERROR_OUTOFMEMORY;
254 dst_bits.is_copy = TRUE;
255 dst_bits.free = free_heap_bits;
259 dst_bits.ptr = src_ptr;
260 dst_bits.is_copy = bits->is_copy;
261 dst_bits.free = NULL;
263 dst_ptr = dst_bits.ptr;
265 switch (info->bmiHeader.biBitCount)
270 if (src_stride != dst_stride)
271 for (y = 0; y < height; y++, src_ptr += src_stride, dst_ptr += dst_stride)
272 memcpy( dst_ptr, src_ptr, dst_stride );
275 if (dst_ptr != src_ptr)
276 for (y = 0; y < height; y++, src_ptr += src_stride, dst_ptr += dst_stride)
277 for (x = 0; x < width; x++)
279 dst_ptr[x * 3] = src_ptr[x * 3 + 2];
280 dst_ptr[x * 3 + 1] = src_ptr[x * 3 + 1];
281 dst_ptr[x * 3 + 2] = src_ptr[x * 3];
283 else /* swap R and B in place */
284 for (y = 0; y < height; y++, src_ptr += src_stride, dst_ptr += dst_stride)
285 for (x = 0; x < width; x++)
287 unsigned char tmp = dst_ptr[x * 3];
288 dst_ptr[x * 3] = dst_ptr[x * 3 + 2];
289 dst_ptr[x * 3 + 2] = tmp;
294 dst_x = dst->visrect.left;
295 dst_y = dst->visrect.top,
296 dst_width = dst->visrect.right - dst->visrect.left;
297 dst_height = dst->visrect.bottom - dst->visrect.top;
298 if (src->width * dst->width < 0)
301 dst_width = -dst_width;
303 if (src->height * dst->height < 0)
306 dst_height = -dst_height;
310 PSDRV_WriteGSave(dev);
311 if (clip) PSDRV_AddClip( dev, clip );
312 PSDRV_WriteImageBits( dev, info, dst_x, dst_y, dst_width, dst_height,
313 width, height, dst_bits.ptr, size );
314 PSDRV_WriteGRestore(dev);
315 PSDRV_ResetClip(dev);
316 if (dst_bits.free) dst_bits.free( &dst_bits );
317 return ERROR_SUCCESS;
320 info->bmiHeader.biPlanes = 1;
321 if (info->bmiHeader.biBitCount > 8) info->bmiHeader.biBitCount = 24;
322 info->bmiHeader.biCompression = BI_RGB;
323 return ERROR_BAD_FORMAT;
326 /***************************************************************************
328 * PSDRV_StretchDIBits
331 * Doesn't work correctly if xSrc isn't byte aligned - this affects 1 and 4
333 * Compression not implemented.
335 INT PSDRV_StretchDIBits( PHYSDEV dev, INT xDst, INT yDst, INT widthDst,
336 INT heightDst, INT xSrc, INT ySrc, INT widthSrc, INT heightSrc, const void *bits,
337 const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
339 LONG fullSrcWidth, fullSrcHeight;
341 WORD bpp, compression;
345 BYTE *dst_ptr, *bitmap;
348 TRACE("%p (%d,%d %dx%d) -> (%d,%d %dx%d)\n", dev->hdc,
349 xSrc, ySrc, widthSrc, heightSrc, xDst, yDst, widthDst, heightDst);
351 if (!get_bitmap_info( info, &fullSrcWidth, &fullSrcHeight, &bpp, &compression )) return FALSE;
353 stride = get_dib_width_bytes(fullSrcWidth, bpp);
355 TRACE("full size=%dx%d bpp=%d compression=%d rop=%08x\n", fullSrcWidth,
356 fullSrcHeight, bpp, compression, dwRop);
359 if(compression != BI_RGB) {
360 FIXME("Compression not supported\n");
366 pt[1].x = xDst + widthDst;
367 pt[1].y = yDst + heightDst;
368 LPtoDP( dev->hdc, pt, 2 );
371 widthDst = pt[1].x - pt[0].x;
372 heightDst = pt[1].y - pt[0].y;
378 src_ptr += (ySrc * stride);
380 FIXME("This won't work...\n");
381 bitmap_size = heightSrc * ((widthSrc + 7) / 8);
382 dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
383 for(line = 0; line < heightSrc; line++, src_ptr += stride, dst_ptr += ((widthSrc + 7) / 8))
384 memcpy(dst_ptr, src_ptr + xSrc / 8, (widthSrc + 7) / 8);
389 src_ptr += (ySrc * stride);
391 FIXME("This won't work...\n");
392 bitmap_size = heightSrc * ((widthSrc + 1) / 2);
393 dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
394 for(line = 0; line < heightSrc; line++, src_ptr += stride, dst_ptr += ((widthSrc + 1) / 2))
395 memcpy(dst_ptr, src_ptr + xSrc/2, (widthSrc+1)/2);
400 src_ptr += (ySrc * stride);
401 bitmap_size = heightSrc * widthSrc;
402 dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
403 for(line = 0; line < heightSrc; line++, src_ptr += stride, dst_ptr += widthSrc)
404 memcpy(dst_ptr, src_ptr + xSrc, widthSrc);
409 src_ptr += (ySrc * stride);
410 bitmap_size = heightSrc * widthSrc * 3;
411 dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
413 for(line = 0; line < heightSrc; line++, src_ptr += stride) {
414 const WORD *words = (const WORD *)src_ptr + xSrc;
416 for(i = 0; i < widthSrc; i++) {
419 /* We want 0x0 -- 0x1f to map to 0x0 -- 0xff */
420 r = words[i] >> 10 & 0x1f;
422 g = words[i] >> 5 & 0x1f;
436 src_ptr += (ySrc * stride);
437 bitmap_size = heightSrc * widthSrc * 3;
438 dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
439 for(line = 0; line < heightSrc; line++, src_ptr += stride) {
440 const BYTE *byte = src_ptr + xSrc * 3;
442 for(i = 0; i < widthSrc; i++) {
443 dst_ptr[0] = byte[i * 3 + 2];
444 dst_ptr[1] = byte[i * 3 + 1];
445 dst_ptr[2] = byte[i * 3];
453 src_ptr += (ySrc * stride);
454 bitmap_size = heightSrc * widthSrc * 3;
455 dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
456 for(line = 0; line < heightSrc; line++, src_ptr += stride) {
457 const BYTE *byte = src_ptr + xSrc * 4;
459 for(i = 0; i < widthSrc; i++) {
460 dst_ptr[0] = byte[i * 4 + 2];
461 dst_ptr[1] = byte[i * 4 + 1];
462 dst_ptr[2] = byte[i * 4];
469 FIXME("Unsupported depth\n");
475 PSDRV_WriteGSave(dev);
476 PSDRV_WriteImageBits( dev, info, xDst, yDst, widthDst, heightDst,
477 widthSrc, heightSrc, bitmap, bitmap_size );
478 HeapFree(GetProcessHeap(), 0, bitmap);
479 PSDRV_WriteGRestore(dev);
480 PSDRV_ResetClip(dev);
481 return abs(heightSrc);