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(PSDRV_PDEVICE
*physDev
, const BITMAPINFO
*info
, INT xDst
,
94 INT yDst
, INT widthDst
, INT heightDst
,
95 INT widthSrc
, INT heightSrc
)
100 switch(info
->bmiHeader
.biBitCount
) {
102 PSDRV_WriteIndexColorSpaceBegin(physDev
, 255);
103 for(i
= 0; i
< 256; i
++) {
104 map
[i
] = info
->bmiColors
[i
].rgbRed
|
105 info
->bmiColors
[i
].rgbGreen
<< 8 |
106 info
->bmiColors
[i
].rgbBlue
<< 16;
108 PSDRV_WriteRGB(physDev
, map
, 256);
109 PSDRV_WriteIndexColorSpaceEnd(physDev
);
113 PSDRV_WriteIndexColorSpaceBegin(physDev
, 15);
114 for(i
= 0; i
< 16; i
++) {
115 map
[i
] = info
->bmiColors
[i
].rgbRed
|
116 info
->bmiColors
[i
].rgbGreen
<< 8 |
117 info
->bmiColors
[i
].rgbBlue
<< 16;
119 PSDRV_WriteRGB(physDev
, map
, 16);
120 PSDRV_WriteIndexColorSpaceEnd(physDev
);
124 PSDRV_WriteIndexColorSpaceBegin(physDev
, 1);
125 for(i
= 0; i
< 2; i
++) {
126 map
[i
] = info
->bmiColors
[i
].rgbRed
|
127 info
->bmiColors
[i
].rgbGreen
<< 8 |
128 info
->bmiColors
[i
].rgbBlue
<< 16;
130 PSDRV_WriteRGB(physDev
, map
, 2);
131 PSDRV_WriteIndexColorSpaceEnd(physDev
);
140 pscol
.type
= PSCOLOR_RGB
;
141 pscol
.value
.rgb
.r
= pscol
.value
.rgb
.g
= pscol
.value
.rgb
.b
= 0.0;
142 PSDRV_WriteSetColor(physDev
, &pscol
);
147 FIXME("Not implemented yet\n");
151 PSDRV_WriteImage(physDev
, info
->bmiHeader
.biBitCount
, xDst
, yDst
,
152 widthDst
, heightDst
, widthSrc
, heightSrc
, FALSE
);
157 /***************************************************************************
158 * PSDRV_WriteImageMaskHeader
160 * Helper for PSDRV_StretchDIBits
162 * We use the imagemask operator for 1bpp bitmaps since the output
163 * takes much less time for the printer to render.
166 * Uses level 2 PostScript
169 static BOOL
PSDRV_WriteImageMaskHeader(PSDRV_PDEVICE
*physDev
, const BITMAPINFO
*info
, INT xDst
,
170 INT yDst
, INT widthDst
, INT heightDst
,
171 INT widthSrc
, INT heightSrc
)
174 PSCOLOR bkgnd
, foregnd
;
177 assert(info
->bmiHeader
.biBitCount
== 1);
179 for(i
= 0; i
< 2; i
++) {
180 map
[i
] = info
->bmiColors
[i
].rgbRed
|
181 info
->bmiColors
[i
].rgbGreen
<< 8 |
182 info
->bmiColors
[i
].rgbBlue
<< 16;
185 /* We'll write the mask with -ve polarity so that
186 the foregnd color corresponds to a bit equal to
189 PSDRV_CreateColor(physDev
, &foregnd
, map
[0]);
190 PSDRV_CreateColor(physDev
, &bkgnd
, map
[1]);
192 PSDRV_WriteGSave(physDev
);
193 PSDRV_WriteNewPath(physDev
);
194 PSDRV_WriteRectangle(physDev
, xDst
, yDst
, widthDst
, heightDst
);
195 PSDRV_WriteSetColor(physDev
, &bkgnd
);
196 PSDRV_WriteFill(physDev
);
197 PSDRV_WriteGRestore(physDev
);
199 PSDRV_WriteSetColor(physDev
, &foregnd
);
200 PSDRV_WriteImage(physDev
, 1, xDst
, yDst
, widthDst
, heightDst
,
201 widthSrc
, heightSrc
, TRUE
);
206 static inline DWORD
max_rle_size(DWORD size
)
208 return size
+ (size
+ 127) / 128 + 1;
211 static inline DWORD
max_ascii85_size(DWORD size
)
213 return (size
+ 3) / 4 * 5;
216 /***************************************************************************
218 * PSDRV_StretchDIBits
221 * Doesn't work correctly if xSrc isn't byte aligned - this affects 1 and 4
223 * Compression not implemented.
225 INT CDECL
PSDRV_StretchDIBits( PSDRV_PDEVICE
*physDev
, INT xDst
, INT yDst
, INT widthDst
,
226 INT heightDst
, INT xSrc
, INT ySrc
,
227 INT widthSrc
, INT heightSrc
, const void *bits
,
228 const BITMAPINFO
*info
, UINT wUsage
, DWORD dwRop
)
230 LONG fullSrcWidth
, fullSrcHeight
;
232 WORD bpp
, compression
;
236 BYTE
*dst_ptr
, *bitmap
, *rle
, *ascii85
;
237 DWORD rle_len
, ascii85_len
, bitmap_size
;
239 TRACE("%p (%d,%d %dx%d) -> (%d,%d %dx%d)\n", physDev
->hdc
,
240 xSrc
, ySrc
, widthSrc
, heightSrc
, xDst
, yDst
, widthDst
, heightDst
);
242 if (!get_bitmap_info( info
, &fullSrcWidth
, &fullSrcHeight
, &bpp
, &compression
)) return FALSE
;
244 stride
= get_dib_width_bytes(fullSrcWidth
, bpp
);
245 if(fullSrcHeight
< 0) stride
= -stride
; /* top-down */
247 TRACE("full size=%dx%d bpp=%d compression=%d rop=%08x\n", fullSrcWidth
,
248 fullSrcHeight
, bpp
, compression
, dwRop
);
251 if(compression
!= BI_RGB
) {
252 FIXME("Compression not supported\n");
258 pt
[1].x
= xDst
+ widthDst
;
259 pt
[1].y
= yDst
+ heightDst
;
260 LPtoDP( physDev
->hdc
, pt
, 2 );
263 widthDst
= pt
[1].x
- pt
[0].x
;
264 heightDst
= pt
[1].y
- pt
[0].y
;
269 PSDRV_SetClip(physDev
);
270 PSDRV_WriteGSave(physDev
);
272 /* Use imagemask rather than image */
273 PSDRV_WriteImageMaskHeader(physDev
, info
, xDst
, yDst
, widthDst
, heightDst
,
274 widthSrc
, heightSrc
);
276 if(stride
< 0) src_ptr
+= (fullSrcHeight
+ 1) * stride
;
277 src_ptr
+= (ySrc
* stride
);
279 FIXME("This won't work...\n");
280 bitmap_size
= heightSrc
* ((widthSrc
+ 7) / 8);
281 dst_ptr
= bitmap
= HeapAlloc(GetProcessHeap(), 0, bitmap_size
);
282 for(line
= 0; line
< heightSrc
; line
++, src_ptr
+= stride
, dst_ptr
+= ((widthSrc
+ 7) / 8))
283 memcpy(dst_ptr
, src_ptr
+ xSrc
/ 8, (widthSrc
+ 7) / 8);
287 PSDRV_SetClip(physDev
);
288 PSDRV_WriteGSave(physDev
);
289 PSDRV_WriteImageHeader(physDev
, info
, xDst
, yDst
, widthDst
, heightDst
,
290 widthSrc
, heightSrc
);
292 if(stride
< 0) src_ptr
+= (fullSrcHeight
+ 1) * stride
;
293 src_ptr
+= (ySrc
* stride
);
295 FIXME("This won't work...\n");
296 bitmap_size
= heightSrc
* ((widthSrc
+ 1) / 2);
297 dst_ptr
= bitmap
= HeapAlloc(GetProcessHeap(), 0, bitmap_size
);
298 for(line
= 0; line
< heightSrc
; line
++, src_ptr
+= stride
, dst_ptr
+= ((widthSrc
+ 1) / 2))
299 memcpy(dst_ptr
, src_ptr
+ xSrc
/2, (widthSrc
+1)/2);
303 PSDRV_SetClip(physDev
);
304 PSDRV_WriteGSave(physDev
);
305 PSDRV_WriteImageHeader(physDev
, info
, xDst
, yDst
, widthDst
, heightDst
,
306 widthSrc
, heightSrc
);
308 if(stride
< 0) src_ptr
+= (fullSrcHeight
+ 1) * stride
;
309 src_ptr
+= (ySrc
* stride
);
310 bitmap_size
= heightSrc
* widthSrc
;
311 dst_ptr
= bitmap
= HeapAlloc(GetProcessHeap(), 0, bitmap_size
);
312 for(line
= 0; line
< heightSrc
; line
++, src_ptr
+= stride
, dst_ptr
+= widthSrc
)
313 memcpy(dst_ptr
, src_ptr
+ xSrc
, widthSrc
);
318 PSDRV_SetClip(physDev
);
319 PSDRV_WriteGSave(physDev
);
320 PSDRV_WriteImageHeader(physDev
, info
, xDst
, yDst
, widthDst
, heightDst
,
321 widthSrc
, heightSrc
);
325 if(stride
< 0) src_ptr
+= (fullSrcHeight
+ 1) * stride
;
326 src_ptr
+= (ySrc
* stride
);
327 bitmap_size
= heightSrc
* widthSrc
* 3;
328 dst_ptr
= bitmap
= HeapAlloc(GetProcessHeap(), 0, bitmap_size
);
330 for(line
= 0; line
< heightSrc
; line
++, src_ptr
+= stride
) {
331 const WORD
*words
= (const WORD
*)src_ptr
+ xSrc
;
333 for(i
= 0; i
< widthSrc
; i
++) {
336 /* We want 0x0 -- 0x1f to map to 0x0 -- 0xff */
337 r
= words
[i
] >> 10 & 0x1f;
339 g
= words
[i
] >> 5 & 0x1f;
352 PSDRV_SetClip(physDev
);
353 PSDRV_WriteGSave(physDev
);
354 PSDRV_WriteImageHeader(physDev
, info
, xDst
, yDst
, widthDst
, heightDst
,
355 widthSrc
, heightSrc
);
358 if(stride
< 0) src_ptr
+= (fullSrcHeight
+ 1) * stride
;
359 src_ptr
+= (ySrc
* stride
);
360 bitmap_size
= heightSrc
* widthSrc
* 3;
361 dst_ptr
= bitmap
= HeapAlloc(GetProcessHeap(), 0, bitmap_size
);
362 for(line
= 0; line
< heightSrc
; line
++, src_ptr
+= stride
) {
363 const BYTE
*byte
= src_ptr
+ xSrc
* 3;
365 for(i
= 0; i
< widthSrc
; i
++) {
366 dst_ptr
[0] = byte
[i
* 3 + 2];
367 dst_ptr
[1] = byte
[i
* 3 + 1];
368 dst_ptr
[2] = byte
[i
* 3];
375 PSDRV_SetClip(physDev
);
376 PSDRV_WriteGSave(physDev
);
377 PSDRV_WriteImageHeader(physDev
, info
, xDst
, yDst
, widthDst
, heightDst
,
378 widthSrc
, heightSrc
);
381 if(stride
< 0) src_ptr
+= (fullSrcHeight
+ 1) * stride
;
382 src_ptr
+= (ySrc
* stride
);
383 bitmap_size
= heightSrc
* widthSrc
* 3;
384 dst_ptr
= bitmap
= HeapAlloc(GetProcessHeap(), 0, bitmap_size
);
385 for(line
= 0; line
< heightSrc
; line
++, src_ptr
+= stride
) {
386 const BYTE
*byte
= src_ptr
+ xSrc
* 4;
388 for(i
= 0; i
< widthSrc
; i
++) {
389 dst_ptr
[0] = byte
[i
* 4 + 2];
390 dst_ptr
[1] = byte
[i
* 4 + 1];
391 dst_ptr
[2] = byte
[i
* 4];
398 FIXME("Unsupported depth\n");
403 rle
= HeapAlloc(GetProcessHeap(), 0, max_rle_size(bitmap_size
));
404 rle_len
= RLE_encode(bitmap
, bitmap_size
, rle
);
405 HeapFree(GetProcessHeap(), 0, bitmap
);
406 ascii85
= HeapAlloc(GetProcessHeap(), 0, max_ascii85_size(rle_len
));
407 ascii85_len
= ASCII85_encode(rle
, rle_len
, ascii85
);
408 HeapFree(GetProcessHeap(), 0, rle
);
409 PSDRV_WriteData(physDev
, ascii85
, ascii85_len
);
410 HeapFree(GetProcessHeap(), 0, ascii85
);
411 PSDRV_WriteSpool(physDev
, "~>\n", 3);
412 PSDRV_WriteGRestore(physDev
);
413 PSDRV_ResetClip(physDev
);
414 return abs(heightSrc
);