crypt32: Add additional path for Solaris 11 Express.
[wine] / dlls / wineps.drv / bitmap.c
1 /*
2  *      PostScript driver bitmap functions
3  *
4  * Copyright 1998  Huw D M Davies
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <assert.h>
22 #include <stdlib.h>
23
24 #include "psdrv.h"
25 #include "winbase.h"
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
29
30
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 )
33 {
34     int words;
35
36     switch(depth)
37     {
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;
48     }
49     return 4 * words;
50 }
51
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 )
54 {
55     const BITMAPINFOHEADER *header = ptr;
56
57     switch(header->biSize)
58     {
59     case sizeof(BITMAPCOREHEADER):
60         {
61             const BITMAPCOREHEADER *core = (const BITMAPCOREHEADER *)header;
62             *width  = core->bcWidth;
63             *height = core->bcHeight;
64             *bpp    = core->bcBitCount;
65             *compr  = 0;
66         }
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;
80     }
81 }
82
83
84 /***************************************************************************
85  *                PSDRV_WriteImageHeader
86  *
87  * Helper for PSDRV_StretchDIBits
88  *
89  * BUGS
90  *  Uses level 2 PostScript
91  */
92
93 static BOOL PSDRV_WriteImageHeader(PHYSDEV dev, const BITMAPINFO *info, INT xDst,
94                                    INT yDst, INT widthDst, INT heightDst,
95                                    INT widthSrc, INT heightSrc)
96 {
97     COLORREF map[256];
98     int i;
99
100     switch(info->bmiHeader.biBitCount) {
101     case 8:
102         PSDRV_WriteIndexColorSpaceBegin(dev, 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;
107         }
108         PSDRV_WriteRGB(dev, map, 256);
109         PSDRV_WriteIndexColorSpaceEnd(dev);
110         break;
111
112     case 4:
113         PSDRV_WriteIndexColorSpaceBegin(dev, 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;
118         }
119         PSDRV_WriteRGB(dev, map, 16);
120         PSDRV_WriteIndexColorSpaceEnd(dev);
121         break;
122
123     case 1:
124         PSDRV_WriteIndexColorSpaceBegin(dev, 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;
129         }
130         PSDRV_WriteRGB(dev, map, 2);
131         PSDRV_WriteIndexColorSpaceEnd(dev);
132         break;
133
134     case 15:
135     case 16:
136     case 24:
137     case 32:
138       {
139         PSCOLOR pscol;
140         pscol.type = PSCOLOR_RGB;
141         pscol.value.rgb.r = pscol.value.rgb.g = pscol.value.rgb.b = 0.0;
142         PSDRV_WriteSetColor(dev, &pscol);
143         break;
144       }
145
146     default:
147         FIXME("Not implemented yet\n");
148         return FALSE;
149     }
150
151     PSDRV_WriteImage(dev, info->bmiHeader.biBitCount, xDst, yDst,
152                      widthDst, heightDst, widthSrc, heightSrc, FALSE);
153     return TRUE;
154 }
155
156
157 /***************************************************************************
158  *                PSDRV_WriteImageMaskHeader
159  *
160  * Helper for PSDRV_StretchDIBits
161  *
162  * We use the imagemask operator for 1bpp bitmaps since the output
163  * takes much less time for the printer to render.
164  *
165  * BUGS
166  *  Uses level 2 PostScript
167  */
168
169 static BOOL PSDRV_WriteImageMaskHeader(PHYSDEV dev, const BITMAPINFO *info, INT xDst,
170                                        INT yDst, INT widthDst, INT heightDst,
171                                        INT widthSrc, INT heightSrc)
172 {
173     COLORREF map[2];
174     PSCOLOR bkgnd, foregnd;
175     int i;
176
177     assert(info->bmiHeader.biBitCount == 1);
178
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;
183     }
184
185     /* We'll write the mask with -ve polarity so that 
186        the foregnd color corresponds to a bit equal to
187        0 in the bitmap.
188     */
189     PSDRV_CreateColor(dev, &foregnd, map[0]);
190     PSDRV_CreateColor(dev, &bkgnd, map[1]);
191
192     PSDRV_WriteGSave(dev);
193     PSDRV_WriteNewPath(dev);
194     PSDRV_WriteRectangle(dev, xDst, yDst, widthDst, heightDst);
195     PSDRV_WriteSetColor(dev, &bkgnd);
196     PSDRV_WriteFill(dev);
197     PSDRV_WriteGRestore(dev);
198
199     PSDRV_WriteSetColor(dev, &foregnd);
200     PSDRV_WriteImage(dev, 1, xDst, yDst, widthDst, heightDst,
201                      widthSrc, heightSrc, TRUE);
202
203     return TRUE;
204 }
205
206 static inline DWORD max_rle_size(DWORD size)
207 {
208     return size + (size + 127) / 128 + 1;
209 }
210
211 static inline DWORD max_ascii85_size(DWORD size)
212 {
213     return (size + 3) / 4 * 5;
214 }
215
216 /***************************************************************************
217  *
218  *      PSDRV_StretchDIBits
219  *
220  * BUGS
221  *  Doesn't work correctly if xSrc isn't byte aligned - this affects 1 and 4
222  *  bit depths.
223  *  Compression not implemented.
224  */
225 INT PSDRV_StretchDIBits( PHYSDEV dev, INT xDst, INT yDst, INT widthDst,
226                          INT heightDst, INT xSrc, INT ySrc, INT widthSrc, INT heightSrc, const void *bits,
227                          const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
228 {
229     LONG fullSrcWidth, fullSrcHeight;
230     INT stride;
231     WORD bpp, compression;
232     INT line;
233     POINT pt[2];
234     const BYTE *src_ptr;
235     BYTE *dst_ptr, *bitmap, *rle, *ascii85;
236     DWORD rle_len, ascii85_len, bitmap_size;
237
238     TRACE("%p (%d,%d %dx%d) -> (%d,%d %dx%d)\n", dev->hdc,
239           xSrc, ySrc, widthSrc, heightSrc, xDst, yDst, widthDst, heightDst);
240
241     if (!get_bitmap_info( info, &fullSrcWidth, &fullSrcHeight, &bpp, &compression )) return FALSE;
242
243     stride = get_dib_width_bytes(fullSrcWidth, bpp);
244     if(fullSrcHeight < 0) stride = -stride; /* top-down */
245
246     TRACE("full size=%dx%d bpp=%d compression=%d rop=%08x\n", fullSrcWidth,
247           fullSrcHeight, bpp, compression, dwRop);
248
249
250     if(compression != BI_RGB) {
251         FIXME("Compression not supported\n");
252         return FALSE;
253     }
254
255     pt[0].x = xDst;
256     pt[0].y = yDst;
257     pt[1].x = xDst + widthDst;
258     pt[1].y = yDst + heightDst;
259     LPtoDP( dev->hdc, pt, 2 );
260     xDst = pt[0].x;
261     yDst = pt[0].y;
262     widthDst = pt[1].x - pt[0].x;
263     heightDst = pt[1].y - pt[0].y;
264
265     switch(bpp) {
266
267     case 1:
268         PSDRV_SetClip(dev);
269         PSDRV_WriteGSave(dev);
270
271         /* Use imagemask rather than image */
272         PSDRV_WriteImageMaskHeader(dev, info, xDst, yDst, widthDst, heightDst,
273                                    widthSrc, heightSrc);
274         src_ptr = bits;
275         if(stride < 0) src_ptr += (fullSrcHeight + 1) * stride;
276         src_ptr += (ySrc * stride);
277         if(xSrc & 7)
278             FIXME("This won't work...\n");
279         bitmap_size = heightSrc * ((widthSrc + 7) / 8);
280         dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
281         for(line = 0; line < heightSrc; line++, src_ptr += stride, dst_ptr += ((widthSrc + 7) / 8))
282             memcpy(dst_ptr, src_ptr + xSrc / 8, (widthSrc + 7) / 8);
283         break;
284
285     case 4:
286         PSDRV_SetClip(dev);
287         PSDRV_WriteGSave(dev);
288         PSDRV_WriteImageHeader(dev, info, xDst, yDst, widthDst, heightDst,
289                                widthSrc, heightSrc);
290         src_ptr = bits;
291         if(stride < 0) src_ptr += (fullSrcHeight + 1) * stride;
292         src_ptr += (ySrc * stride);
293         if(xSrc & 1)
294             FIXME("This won't work...\n");
295         bitmap_size = heightSrc * ((widthSrc + 1) / 2);
296         dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
297         for(line = 0; line < heightSrc; line++, src_ptr += stride, dst_ptr += ((widthSrc + 1) / 2))
298             memcpy(dst_ptr, src_ptr + xSrc/2, (widthSrc+1)/2);
299         break;
300
301     case 8:
302         PSDRV_SetClip(dev);
303         PSDRV_WriteGSave(dev);
304         PSDRV_WriteImageHeader(dev, info, xDst, yDst, widthDst, heightDst,
305                                widthSrc, heightSrc);
306         src_ptr = bits;
307         if(stride < 0) src_ptr += (fullSrcHeight + 1) * stride;
308         src_ptr += (ySrc * stride);
309         bitmap_size = heightSrc * widthSrc;
310         dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
311         for(line = 0; line < heightSrc; line++, src_ptr += stride, dst_ptr += widthSrc)
312             memcpy(dst_ptr, src_ptr + xSrc, widthSrc);
313         break;
314
315     case 15:
316     case 16:
317         PSDRV_SetClip(dev);
318         PSDRV_WriteGSave(dev);
319         PSDRV_WriteImageHeader(dev, info, xDst, yDst, widthDst, heightDst,
320                                widthSrc, heightSrc);
321
322
323         src_ptr = bits;
324         if(stride < 0) src_ptr += (fullSrcHeight + 1) * stride;
325         src_ptr += (ySrc * stride);
326         bitmap_size = heightSrc * widthSrc * 3;
327         dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
328         
329         for(line = 0; line < heightSrc; line++, src_ptr += stride) {
330             const WORD *words = (const WORD *)src_ptr + xSrc;
331             int i;
332             for(i = 0; i < widthSrc; i++) {
333                 BYTE r, g, b;
334
335                 /* We want 0x0 -- 0x1f to map to 0x0 -- 0xff */
336                 r = words[i] >> 10 & 0x1f;
337                 r = r << 3 | r >> 2;
338                 g = words[i] >> 5 & 0x1f;
339                 g = g << 3 | g >> 2;
340                 b = words[i] & 0x1f;
341                 b = b << 3 | b >> 2;
342                 dst_ptr[0] = r;
343                 dst_ptr[1] = g;
344                 dst_ptr[2] = b;
345                 dst_ptr += 3;
346             }
347         }
348         break;
349
350     case 24:
351         PSDRV_SetClip(dev);
352         PSDRV_WriteGSave(dev);
353         PSDRV_WriteImageHeader(dev, info, xDst, yDst, widthDst, heightDst,
354                                widthSrc, heightSrc);
355
356         src_ptr = bits;
357         if(stride < 0) src_ptr += (fullSrcHeight + 1) * stride;
358         src_ptr += (ySrc * stride);
359         bitmap_size = heightSrc * widthSrc * 3;
360         dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
361         for(line = 0; line < heightSrc; line++, src_ptr += stride) {
362             const BYTE *byte = src_ptr + xSrc * 3;
363             int i;
364             for(i = 0; i < widthSrc; i++) {
365                 dst_ptr[0] = byte[i * 3 + 2];
366                 dst_ptr[1] = byte[i * 3 + 1];
367                 dst_ptr[2] = byte[i * 3];
368                 dst_ptr += 3;
369             }
370         }
371         break;
372
373     case 32:
374         PSDRV_SetClip(dev);
375         PSDRV_WriteGSave(dev);
376         PSDRV_WriteImageHeader(dev, info, xDst, yDst, widthDst, heightDst,
377                                widthSrc, heightSrc);
378
379         src_ptr = bits;
380         if(stride < 0) src_ptr += (fullSrcHeight + 1) * stride;
381         src_ptr += (ySrc * stride);
382         bitmap_size = heightSrc * widthSrc * 3;
383         dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
384         for(line = 0; line < heightSrc; line++, src_ptr += stride) {
385             const BYTE *byte = src_ptr + xSrc * 4;
386             int i;
387             for(i = 0; i < widthSrc; i++) {
388                 dst_ptr[0] = byte[i * 4 + 2];
389                 dst_ptr[1] = byte[i * 4 + 1];
390                 dst_ptr[2] = byte[i * 4];
391                 dst_ptr += 3;
392             }
393         }
394         break;
395
396     default:
397         FIXME("Unsupported depth\n");
398         return FALSE;
399
400     }
401
402     rle = HeapAlloc(GetProcessHeap(), 0, max_rle_size(bitmap_size));
403     rle_len = RLE_encode(bitmap, bitmap_size, rle);
404     HeapFree(GetProcessHeap(), 0, bitmap);
405     ascii85 = HeapAlloc(GetProcessHeap(), 0, max_ascii85_size(rle_len));
406     ascii85_len = ASCII85_encode(rle, rle_len, ascii85);
407     HeapFree(GetProcessHeap(), 0, rle);
408     PSDRV_WriteData(dev, ascii85, ascii85_len);
409     HeapFree(GetProcessHeap(), 0, ascii85);
410     PSDRV_WriteSpool(dev, "~>\n", 3);
411     PSDRV_WriteGRestore(dev);
412     PSDRV_ResetClip(dev);
413     return abs(heightSrc);
414 }