mshtml: Populate dynamic properties table in get_dynamic_data.
[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
53 /***************************************************************************
54  *                PSDRV_WriteImageHeader
55  *
56  * Helper for PSDRV_StretchDIBits
57  *
58  * BUGS
59  *  Uses level 2 PostScript
60  */
61
62 static BOOL PSDRV_WriteImageHeader(PHYSDEV dev, const BITMAPINFO *info, INT xDst,
63                                    INT yDst, INT widthDst, INT heightDst,
64                                    INT widthSrc, INT heightSrc)
65 {
66     switch(info->bmiHeader.biBitCount)
67     {
68     case 1:
69     case 4:
70     case 8:
71         PSDRV_WriteIndexColorSpaceBegin(dev, (1 << info->bmiHeader.biBitCount) - 1);
72         PSDRV_WriteRGBQUAD(dev, info->bmiColors, 1 << info->bmiHeader.biBitCount);
73         PSDRV_WriteIndexColorSpaceEnd(dev);
74         break;
75
76     case 16:
77     case 24:
78     case 32:
79       {
80         PSCOLOR pscol;
81         pscol.type = PSCOLOR_RGB;
82         pscol.value.rgb.r = pscol.value.rgb.g = pscol.value.rgb.b = 0.0;
83         PSDRV_WriteSetColor(dev, &pscol);
84         break;
85       }
86     }
87
88     PSDRV_WriteImage(dev, info->bmiHeader.biBitCount, xDst, yDst,
89                      widthDst, heightDst, widthSrc, heightSrc, FALSE, info->bmiHeader.biHeight < 0);
90     return TRUE;
91 }
92
93
94 /***************************************************************************
95  *                PSDRV_WriteImageMaskHeader
96  *
97  * Helper for PSDRV_StretchDIBits
98  *
99  * We use the imagemask operator for 1bpp bitmaps since the output
100  * takes much less time for the printer to render.
101  *
102  * BUGS
103  *  Uses level 2 PostScript
104  */
105
106 static BOOL PSDRV_WriteImageMaskHeader(PHYSDEV dev, const BITMAPINFO *info, INT xDst,
107                                        INT yDst, INT widthDst, INT heightDst,
108                                        INT widthSrc, INT heightSrc)
109 {
110     PSCOLOR bkgnd, foregnd;
111
112     assert(info->bmiHeader.biBitCount == 1);
113
114     /* We'll write the mask with -ve polarity so that 
115        the foregnd color corresponds to a bit equal to
116        0 in the bitmap.
117     */
118     PSDRV_CreateColor(dev, &foregnd, RGB(info->bmiColors[0].rgbRed,
119                                          info->bmiColors[0].rgbGreen,
120                                          info->bmiColors[0].rgbBlue) );
121     PSDRV_CreateColor(dev, &bkgnd, RGB(info->bmiColors[1].rgbRed,
122                                        info->bmiColors[1].rgbGreen,
123                                        info->bmiColors[1].rgbBlue) );
124
125     PSDRV_WriteGSave(dev);
126     PSDRV_WriteNewPath(dev);
127     PSDRV_WriteRectangle(dev, xDst, yDst, widthDst, heightDst);
128     PSDRV_WriteSetColor(dev, &bkgnd);
129     PSDRV_WriteFill(dev);
130     PSDRV_WriteGRestore(dev);
131
132     PSDRV_WriteSetColor(dev, &foregnd);
133     PSDRV_WriteImage(dev, 1, xDst, yDst, widthDst, heightDst,
134                      widthSrc, heightSrc, TRUE, info->bmiHeader.biHeight < 0);
135
136     return TRUE;
137 }
138
139 static inline DWORD max_rle_size(DWORD size)
140 {
141     return size + (size + 127) / 128 + 1;
142 }
143
144 static inline DWORD max_ascii85_size(DWORD size)
145 {
146     return (size + 3) / 4 * 5;
147 }
148
149 static void free_heap_bits( struct gdi_image_bits *bits )
150 {
151     HeapFree( GetProcessHeap(), 0, bits->ptr );
152 }
153
154 /***************************************************************************
155  *                PSDRV_WriteImageBits
156  */
157 static void PSDRV_WriteImageBits( PHYSDEV dev, const BITMAPINFO *info, INT xDst, INT yDst,
158                                   INT widthDst, INT heightDst, INT widthSrc, INT heightSrc,
159                                   void *bits, DWORD size )
160 {
161     BYTE *rle, *ascii85;
162     DWORD rle_len, ascii85_len;
163
164     if (info->bmiHeader.biBitCount == 1)
165         /* Use imagemask rather than image */
166         PSDRV_WriteImageMaskHeader(dev, info, xDst, yDst, widthDst, heightDst,
167                                    widthSrc, heightSrc);
168     else
169         PSDRV_WriteImageHeader(dev, info, xDst, yDst, widthDst, heightDst,
170                                widthSrc, heightSrc);
171
172     rle = HeapAlloc(GetProcessHeap(), 0, max_rle_size(size));
173     rle_len = RLE_encode(bits, size, rle);
174     ascii85 = HeapAlloc(GetProcessHeap(), 0, max_ascii85_size(rle_len));
175     ascii85_len = ASCII85_encode(rle, rle_len, ascii85);
176     HeapFree(GetProcessHeap(), 0, rle);
177     PSDRV_WriteData(dev, ascii85, ascii85_len);
178     PSDRV_WriteSpool(dev, "~>\n", 3);
179     HeapFree(GetProcessHeap(), 0, ascii85);
180 }
181
182 /***********************************************************************
183  *           PSDRV_PutImage
184  */
185 DWORD PSDRV_PutImage( PHYSDEV dev, HBITMAP hbitmap, HRGN clip, BITMAPINFO *info,
186                       const struct gdi_image_bits *bits, struct bitblt_coords *src,
187                       struct bitblt_coords *dst, DWORD rop )
188 {
189     int src_stride, dst_stride, size, x, y, width, height, bit_offset;
190     int dst_x, dst_y, dst_width, dst_height;
191     unsigned char *src_ptr, *dst_ptr;
192     struct gdi_image_bits dst_bits;
193
194     if (hbitmap) return ERROR_NOT_SUPPORTED;
195
196     if (info->bmiHeader.biPlanes != 1) goto update_format;
197     if (info->bmiHeader.biCompression != BI_RGB) goto update_format;
198     if (info->bmiHeader.biBitCount == 16 || info->bmiHeader.biBitCount == 32) goto update_format;
199     if (!bits) return ERROR_SUCCESS;  /* just querying the format */
200
201     TRACE( "bpp %u %s -> %s\n", info->bmiHeader.biBitCount, wine_dbgstr_rect(&src->visrect),
202            wine_dbgstr_rect(&dst->visrect) );
203
204     width = src->visrect.right - src->visrect.left;
205     height = src->visrect.bottom - src->visrect.top;
206     src_stride = get_dib_width_bytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
207     dst_stride = (width * info->bmiHeader.biBitCount + 7) / 8;
208
209     src_ptr = bits->ptr;
210     if (info->bmiHeader.biHeight > 0)
211         src_ptr += (info->bmiHeader.biHeight - src->visrect.bottom) * src_stride;
212     else
213         src_ptr += src->visrect.top * src_stride;
214     bit_offset = src->visrect.left * info->bmiHeader.biBitCount;
215     src_ptr += bit_offset / 8;
216     bit_offset &= 7;
217     if (bit_offset) FIXME( "pos %s not supported\n", wine_dbgstr_rect(&src->visrect) );
218     size = height * dst_stride;
219
220     if (src_stride != dst_stride || (info->bmiHeader.biBitCount == 24 && !bits->is_copy))
221     {
222         if (!(dst_bits.ptr = HeapAlloc( GetProcessHeap(), 0, size ))) return ERROR_OUTOFMEMORY;
223         dst_bits.is_copy = TRUE;
224         dst_bits.free = free_heap_bits;
225     }
226     else
227     {
228         dst_bits.ptr = src_ptr;
229         dst_bits.is_copy = bits->is_copy;
230         dst_bits.free = NULL;
231     }
232     dst_ptr = dst_bits.ptr;
233
234     switch (info->bmiHeader.biBitCount)
235     {
236     case 1:
237     case 4:
238     case 8:
239         if (src_stride != dst_stride)
240             for (y = 0; y < height; y++, src_ptr += src_stride, dst_ptr += dst_stride)
241                 memcpy( dst_ptr, src_ptr, dst_stride );
242         break;
243     case 24:
244         if (dst_ptr != src_ptr)
245             for (y = 0; y < height; y++, src_ptr += src_stride, dst_ptr += dst_stride)
246                 for (x = 0; x < width; x++)
247                 {
248                     dst_ptr[x * 3]     = src_ptr[x * 3 + 2];
249                     dst_ptr[x * 3 + 1] = src_ptr[x * 3 + 1];
250                     dst_ptr[x * 3 + 2] = src_ptr[x * 3];
251                 }
252         else  /* swap R and B in place */
253             for (y = 0; y < height; y++, src_ptr += src_stride, dst_ptr += dst_stride)
254                 for (x = 0; x < width; x++)
255                 {
256                     unsigned char tmp = dst_ptr[x * 3];
257                     dst_ptr[x * 3] = dst_ptr[x * 3 + 2];
258                     dst_ptr[x * 3 + 2] = tmp;
259                 }
260         break;
261     }
262
263     dst_x = dst->visrect.left;
264     dst_y = dst->visrect.top,
265     dst_width = dst->visrect.right - dst->visrect.left;
266     dst_height = dst->visrect.bottom - dst->visrect.top;
267     if (src->width * dst->width < 0)
268     {
269         dst_x += dst_width;
270         dst_width = -dst_width;
271     }
272     if (src->height * dst->height < 0)
273     {
274         dst_y += dst_height;
275         dst_height = -dst_height;
276     }
277
278     PSDRV_SetClip(dev);
279     PSDRV_WriteGSave(dev);
280     if (clip) PSDRV_AddClip( dev, clip );
281     PSDRV_WriteImageBits( dev, info, dst_x, dst_y, dst_width, dst_height,
282                           width, height, dst_bits.ptr, size );
283     PSDRV_WriteGRestore(dev);
284     PSDRV_ResetClip(dev);
285     if (dst_bits.free) dst_bits.free( &dst_bits );
286     return ERROR_SUCCESS;
287
288 update_format:
289     info->bmiHeader.biPlanes = 1;
290     if (info->bmiHeader.biBitCount > 8) info->bmiHeader.biBitCount = 24;
291     info->bmiHeader.biCompression = BI_RGB;
292     return ERROR_BAD_FORMAT;
293 }
294
295 /***************************************************************************
296  *
297  *      PSDRV_StretchDIBits
298  *
299  * BUGS
300  *  Doesn't work correctly if xSrc isn't byte aligned - this affects 1 and 4
301  *  bit depths.
302  *  Compression not implemented.
303  */
304 INT PSDRV_StretchDIBits( PHYSDEV dev, INT xDst, INT yDst, INT widthDst,
305                          INT heightDst, INT xSrc, INT ySrc, INT widthSrc, INT heightSrc, const void *bits,
306                          const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
307 {
308     INT stride;
309     INT line;
310     POINT pt[2];
311     const BYTE *src_ptr;
312     BYTE *dst_ptr, *bitmap;
313     DWORD bitmap_size;
314
315     TRACE("%p (%d,%d %dx%d) -> (%d,%d %dx%d)\n", dev->hdc,
316           xSrc, ySrc, widthSrc, heightSrc, xDst, yDst, widthDst, heightDst);
317
318     stride = get_dib_width_bytes( info->bmiHeader.biWidth, info->bmiHeader.biBitCount );
319
320     TRACE("full size=%dx%d bpp=%d compression=%d rop=%08x\n", info->bmiHeader.biWidth,
321           info->bmiHeader.biHeight, info->bmiHeader.biBitCount, info->bmiHeader.biCompression, dwRop);
322
323
324     if(info->bmiHeader.biCompression != BI_RGB) {
325         FIXME("Compression not supported\n");
326         return FALSE;
327     }
328
329     pt[0].x = xDst;
330     pt[0].y = yDst;
331     pt[1].x = xDst + widthDst;
332     pt[1].y = yDst + heightDst;
333     LPtoDP( dev->hdc, pt, 2 );
334     xDst = pt[0].x;
335     yDst = pt[0].y;
336     widthDst = pt[1].x - pt[0].x;
337     heightDst = pt[1].y - pt[0].y;
338
339     switch (info->bmiHeader.biBitCount) {
340
341     case 1:
342         src_ptr = bits;
343         src_ptr += (ySrc * stride);
344         if(xSrc & 7)
345             FIXME("This won't work...\n");
346         bitmap_size = heightSrc * ((widthSrc + 7) / 8);
347         dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
348         for(line = 0; line < heightSrc; line++, src_ptr += stride, dst_ptr += ((widthSrc + 7) / 8))
349             memcpy(dst_ptr, src_ptr + xSrc / 8, (widthSrc + 7) / 8);
350         break;
351
352     case 4:
353         src_ptr = bits;
354         src_ptr += (ySrc * stride);
355         if(xSrc & 1)
356             FIXME("This won't work...\n");
357         bitmap_size = heightSrc * ((widthSrc + 1) / 2);
358         dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
359         for(line = 0; line < heightSrc; line++, src_ptr += stride, dst_ptr += ((widthSrc + 1) / 2))
360             memcpy(dst_ptr, src_ptr + xSrc/2, (widthSrc+1)/2);
361         break;
362
363     case 8:
364         src_ptr = bits;
365         src_ptr += (ySrc * stride);
366         bitmap_size = heightSrc * widthSrc;
367         dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
368         for(line = 0; line < heightSrc; line++, src_ptr += stride, dst_ptr += widthSrc)
369             memcpy(dst_ptr, src_ptr + xSrc, widthSrc);
370         break;
371
372     case 16:
373         src_ptr = bits;
374         src_ptr += (ySrc * stride);
375         bitmap_size = heightSrc * widthSrc * 3;
376         dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
377         
378         for(line = 0; line < heightSrc; line++, src_ptr += stride) {
379             const WORD *words = (const WORD *)src_ptr + xSrc;
380             int i;
381             for(i = 0; i < widthSrc; i++) {
382                 BYTE r, g, b;
383
384                 /* We want 0x0 -- 0x1f to map to 0x0 -- 0xff */
385                 r = words[i] >> 10 & 0x1f;
386                 r = r << 3 | r >> 2;
387                 g = words[i] >> 5 & 0x1f;
388                 g = g << 3 | g >> 2;
389                 b = words[i] & 0x1f;
390                 b = b << 3 | b >> 2;
391                 dst_ptr[0] = r;
392                 dst_ptr[1] = g;
393                 dst_ptr[2] = b;
394                 dst_ptr += 3;
395             }
396         }
397         break;
398
399     case 24:
400         src_ptr = bits;
401         src_ptr += (ySrc * stride);
402         bitmap_size = heightSrc * widthSrc * 3;
403         dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
404         for(line = 0; line < heightSrc; line++, src_ptr += stride) {
405             const BYTE *byte = src_ptr + xSrc * 3;
406             int i;
407             for(i = 0; i < widthSrc; i++) {
408                 dst_ptr[0] = byte[i * 3 + 2];
409                 dst_ptr[1] = byte[i * 3 + 1];
410                 dst_ptr[2] = byte[i * 3];
411                 dst_ptr += 3;
412             }
413         }
414         break;
415
416     case 32:
417         src_ptr = bits;
418         src_ptr += (ySrc * stride);
419         bitmap_size = heightSrc * widthSrc * 3;
420         dst_ptr = bitmap = HeapAlloc(GetProcessHeap(), 0, bitmap_size);
421         for(line = 0; line < heightSrc; line++, src_ptr += stride) {
422             const BYTE *byte = src_ptr + xSrc * 4;
423             int i;
424             for(i = 0; i < widthSrc; i++) {
425                 dst_ptr[0] = byte[i * 4 + 2];
426                 dst_ptr[1] = byte[i * 4 + 1];
427                 dst_ptr[2] = byte[i * 4];
428                 dst_ptr += 3;
429             }
430         }
431         break;
432
433     default:
434         FIXME("Unsupported depth\n");
435         return FALSE;
436
437     }
438
439     PSDRV_SetClip(dev);
440     PSDRV_WriteGSave(dev);
441     PSDRV_WriteImageBits( dev, info, xDst, yDst, widthDst, heightDst,
442                           widthSrc, heightSrc, bitmap, bitmap_size );
443     HeapFree(GetProcessHeap(), 0, bitmap);
444     PSDRV_WriteGRestore(dev);
445     PSDRV_ResetClip(dev);
446     return abs(heightSrc);
447 }