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 /***************************************************************************
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
)
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
);
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
213 * Doesn't work correctly if xSrc isn't byte aligned - this affects 1 and 4
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
;
223 WORD bpp
, compression
;
227 BYTE
*dst_ptr
, *bitmap
;
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");
248 pt
[1].x
= xDst
+ widthDst
;
249 pt
[1].y
= yDst
+ heightDst
;
250 LPtoDP( dev
->hdc
, pt
, 2 );
253 widthDst
= pt
[1].x
- pt
[0].x
;
254 heightDst
= pt
[1].y
- pt
[0].y
;
260 src_ptr
+= (ySrc
* stride
);
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);
271 src_ptr
+= (ySrc
* stride
);
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);
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
);
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
;
298 for(i
= 0; i
< widthSrc
; i
++) {
301 /* We want 0x0 -- 0x1f to map to 0x0 -- 0xff */
302 r
= words
[i
] >> 10 & 0x1f;
304 g
= words
[i
] >> 5 & 0x1f;
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;
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];
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;
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];
351 FIXME("Unsupported depth\n");
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
);