INT21_GetFreeDiskSpace(): The drive parameter is found in the DL
[wine] / dlls / wineps / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 inline static 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 = (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("(%ld): 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(PSDRV_PDEVICE *physDev, 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(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;
107         }
108         PSDRV_WriteRGB(physDev, map, 256);
109         PSDRV_WriteIndexColorSpaceEnd(physDev);
110         break;
111
112     case 4:
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;
118         }
119         PSDRV_WriteRGB(physDev, map, 16);
120         PSDRV_WriteIndexColorSpaceEnd(physDev);
121         break;
122
123     case 1:
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;
129         }
130         PSDRV_WriteRGB(physDev, map, 2);
131         PSDRV_WriteIndexColorSpaceEnd(physDev);
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(physDev, &pscol);
143         break;
144       }
145
146     default:
147         FIXME("Not implemented yet\n");
148         return FALSE;
149         break;
150     }
151
152     PSDRV_WriteImageDict(physDev, info->bmiHeader.biBitCount, xDst, yDst,
153                           widthDst, heightDst, widthSrc, heightSrc, NULL, FALSE);
154     return TRUE;
155 }
156
157
158 /***************************************************************************
159  *                PSDRV_WriteImageMaskHeader
160  *
161  * Helper for PSDRV_StretchDIBits
162  *
163  * We use the imagemask operator for 1bpp bitmaps since the output
164  * takes much less time for the printer to render.
165  *
166  * BUGS
167  *  Uses level 2 PostScript
168  */
169
170 static BOOL PSDRV_WriteImageMaskHeader(PSDRV_PDEVICE *physDev, const BITMAPINFO *info, INT xDst,
171                                        INT yDst, INT widthDst, INT heightDst,
172                                        INT widthSrc, INT heightSrc)
173 {
174     COLORREF map[2];
175     PSCOLOR bkgnd, foregnd;
176     int i;
177
178     assert(info->bmiHeader.biBitCount == 1);
179
180     for(i = 0; i < 2; i++) {
181         map[i] =  info->bmiColors[i].rgbRed |
182             info->bmiColors[i].rgbGreen << 8 |
183             info->bmiColors[i].rgbBlue << 16;
184     }
185
186     /* We'll write the mask with -ve polarity so that 
187        the foregnd color corresponds to a bit equal to
188        0 in the bitmap.
189     */
190     PSDRV_CreateColor(physDev, &foregnd, map[0]);
191     PSDRV_CreateColor(physDev, &bkgnd, map[1]);
192
193     PSDRV_WriteGSave(physDev);
194     PSDRV_WriteNewPath(physDev);
195     PSDRV_WriteRectangle(physDev, xDst, yDst, widthDst, heightDst);
196     PSDRV_WriteSetColor(physDev, &bkgnd);
197     PSDRV_WriteFill(physDev);
198     PSDRV_WriteGRestore(physDev);
199
200     PSDRV_WriteSetColor(physDev, &foregnd);
201     PSDRV_WriteImageDict(physDev, 1, xDst, yDst, widthDst, heightDst,
202                          widthSrc, heightSrc, NULL, TRUE);
203
204     return TRUE;
205 }
206
207
208 /***************************************************************************
209  *
210  *      PSDRV_StretchDIBits
211  *
212  * BUGS
213  *  Doesn't work correctly if xSrc isn't byte aligned - this affects 1 and 4
214  *  bit depths.
215  *  Compression not implemented.
216  */
217 INT PSDRV_StretchDIBits( PSDRV_PDEVICE *physDev, INT xDst, INT yDst, INT widthDst,
218                          INT heightDst, INT xSrc, INT ySrc,
219                          INT widthSrc, INT heightSrc, const void *bits,
220                          const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
221 {
222     LONG fullSrcWidth, fullSrcHeight;
223     INT widthbytes;
224     WORD bpp, compression;
225     const char *ptr;
226     INT line;
227     POINT pt[2];
228
229     TRACE("%p (%d,%d %dx%d) -> (%d,%d %dx%d)\n", physDev->hdc,
230           xSrc, ySrc, widthSrc, heightSrc, xDst, yDst, widthDst, heightDst);
231
232     if (!get_bitmap_info( info, &fullSrcWidth, &fullSrcHeight, &bpp, &compression )) return FALSE;
233
234     widthbytes = get_dib_width_bytes(fullSrcWidth, bpp);
235
236     TRACE("full size=%ldx%ld bpp=%d compression=%d rop=%08lx\n", fullSrcWidth,
237           fullSrcHeight, bpp, compression, dwRop);
238
239
240     if(compression != BI_RGB) {
241         FIXME("Compression not supported\n");
242         return FALSE;
243     }
244
245     pt[0].x = xDst;
246     pt[0].y = yDst;
247     pt[1].x = xDst + widthDst;
248     pt[1].y = yDst + heightDst;
249     LPtoDP( physDev->hdc, pt, 2 );
250     xDst = pt[0].x;
251     yDst = pt[0].y;
252     widthDst = pt[1].x - pt[0].x;
253     heightDst = pt[1].y - pt[0].y;
254
255     switch(bpp) {
256
257     case 1:
258         PSDRV_SetClip(physDev);
259         PSDRV_WriteGSave(physDev);
260
261         /* Use imagemask rather than image */
262         PSDRV_WriteImageMaskHeader(physDev, info, xDst, yDst, widthDst, heightDst,
263                                    widthSrc, heightSrc);
264         ptr = bits;
265         ptr += (ySrc * widthbytes);
266         if(xSrc & 7)
267             FIXME("This won't work...\n");
268         for(line = 0; line < heightSrc; line++, ptr += widthbytes)
269             PSDRV_WriteBytes(physDev, ptr + xSrc/8, (widthSrc+7)/8);
270         break;
271
272     case 4:
273         PSDRV_SetClip(physDev);
274         PSDRV_WriteGSave(physDev);
275         PSDRV_WriteImageHeader(physDev, info, xDst, yDst, widthDst, heightDst,
276                                widthSrc, heightSrc);
277         ptr = bits;
278         ptr += (ySrc * widthbytes);
279         if(xSrc & 1)
280             FIXME("This won't work...\n");
281         for(line = 0; line < heightSrc; line++, ptr += widthbytes)
282             PSDRV_WriteBytes(physDev, ptr + xSrc/2, (widthSrc+1)/2);
283         break;
284
285     case 8:
286         PSDRV_SetClip(physDev);
287         PSDRV_WriteGSave(physDev);
288         PSDRV_WriteImageHeader(physDev, info, xDst, yDst, widthDst, heightDst,
289                                widthSrc, heightSrc);
290         ptr = bits;
291         ptr += (ySrc * widthbytes);
292         for(line = 0; line < heightSrc; line++, ptr += widthbytes)
293             PSDRV_WriteBytes(physDev, ptr + xSrc, widthSrc);
294         break;
295
296     case 15:
297     case 16:
298         PSDRV_SetClip(physDev);
299         PSDRV_WriteGSave(physDev);
300         PSDRV_WriteImageHeader(physDev, info, xDst, yDst, widthDst, heightDst,
301                                widthSrc, heightSrc);
302
303         ptr = bits;
304         ptr += (ySrc * widthbytes);
305         for(line = 0; line < heightSrc; line++, ptr += widthbytes)
306             PSDRV_WriteDIBits16(physDev, (WORD *)ptr + xSrc, widthSrc);
307         break;
308
309     case 24:
310         PSDRV_SetClip(physDev);
311         PSDRV_WriteGSave(physDev);
312         PSDRV_WriteImageHeader(physDev, info, xDst, yDst, widthDst, heightDst,
313                                widthSrc, heightSrc);
314
315         ptr = bits;
316         ptr += (ySrc * widthbytes);
317         for(line = 0; line < heightSrc; line++, ptr += widthbytes)
318             PSDRV_WriteDIBits24(physDev, ptr + xSrc * 3, widthSrc);
319         break;
320
321     case 32:
322         PSDRV_SetClip(physDev);
323         PSDRV_WriteGSave(physDev);
324         PSDRV_WriteImageHeader(physDev, info, xDst, yDst, widthDst, heightDst,
325                                widthSrc, heightSrc);
326
327         ptr = bits;
328         ptr += (ySrc * widthbytes);
329         for(line = 0; line < heightSrc; line++, ptr += widthbytes)
330             PSDRV_WriteDIBits32(physDev, ptr + xSrc * 3, widthSrc);
331         break;
332
333     default:
334         FIXME("Unsupported depth\n");
335         return FALSE;
336
337     }
338     PSDRV_WriteSpool(physDev, ">\n", 2);  /* End-of-Data for /HexASCIIDecodeFilter */
339     PSDRV_WriteGRestore(physDev);
340     PSDRV_ResetClip(physDev);
341     return abs(heightSrc);
342 }