Avoid returning an unlocked window pointer from WINPOS_WindowFromPoint.
[wine] / dlls / wineps / bitmap.c
1 /*
2  *      PostScript driver bitmap functions
3  *
4  * Copyright 1998  Huw D M Davies
5  *
6  */
7
8 #include "gdi.h"
9 #include "psdrv.h"
10 #include "debugtools.h"
11 #include "bitmap.h"
12 #include "winbase.h"
13
14 DEFAULT_DEBUG_CHANNEL(psdrv);
15
16
17 /***************************************************************************
18  *                PSDRV_WriteImageHeader
19  *
20  * Helper for PSDRV_StretchDIBits
21  *
22  * BUGS
23  *  Uses level 2 PostScript
24  */
25
26 static BOOL PSDRV_WriteImageHeader(DC *dc, const BITMAPINFO *info, INT xDst,
27                                    INT yDst, INT widthDst, INT heightDst,
28                                    INT widthSrc, INT heightSrc)
29 {
30     COLORREF map[256];
31     int i;
32
33     switch(info->bmiHeader.biBitCount) {
34     case 8:
35         PSDRV_WriteIndexColorSpaceBegin(dc, 255);
36         for(i = 0; i < 256; i++) {
37             map[i] =  info->bmiColors[i].rgbRed |
38               info->bmiColors[i].rgbGreen << 8 |
39               info->bmiColors[i].rgbBlue << 16;
40         }
41         PSDRV_WriteRGB(dc, map, 256);
42         PSDRV_WriteIndexColorSpaceEnd(dc);
43         break;
44
45     case 4:
46         PSDRV_WriteIndexColorSpaceBegin(dc, 15);
47         for(i = 0; i < 16; i++) {
48             map[i] =  info->bmiColors[i].rgbRed |
49               info->bmiColors[i].rgbGreen << 8 |
50               info->bmiColors[i].rgbBlue << 16;
51         }
52         PSDRV_WriteRGB(dc, map, 16);
53         PSDRV_WriteIndexColorSpaceEnd(dc);
54         break;
55
56     case 1:
57         PSDRV_WriteIndexColorSpaceBegin(dc, 1);
58         for(i = 0; i < 2; i++) {
59             map[i] =  info->bmiColors[i].rgbRed |
60               info->bmiColors[i].rgbGreen << 8 |
61               info->bmiColors[i].rgbBlue << 16;
62         }
63         PSDRV_WriteRGB(dc, map, 2);
64         PSDRV_WriteIndexColorSpaceEnd(dc);
65         break;
66
67     case 15:
68     case 16:
69     case 24:
70     case 32:
71       {
72         PSCOLOR pscol;
73         pscol.type = PSCOLOR_RGB;
74         pscol.value.rgb.r = pscol.value.rgb.g = pscol.value.rgb.b = 0.0;
75         PSDRV_WriteSetColor(dc, &pscol);
76         break;
77       }
78
79     default:
80         FIXME("Not implemented yet\n");
81         return FALSE;
82         break;
83     }
84
85     PSDRV_WriteImageDict(dc, info->bmiHeader.biBitCount, xDst, yDst,
86                           widthDst, heightDst, widthSrc, heightSrc, NULL);
87     return TRUE;
88 }
89
90
91 /***************************************************************************
92  *
93  *      PSDRV_StretchDIBits
94  *
95  * BUGS
96  *  Doesn't work correctly if xSrc isn't byte aligned - this affects 1 and 4
97  *  bit depths.
98  *  Compression not implemented.
99  */
100 INT PSDRV_StretchDIBits( DC *dc, INT xDst, INT yDst, INT widthDst,
101                          INT heightDst, INT xSrc, INT ySrc,
102                          INT widthSrc, INT heightSrc, const void *bits,
103                          const BITMAPINFO *info, UINT wUsage, DWORD dwRop )
104 {
105     DWORD fullSrcWidth;
106     INT widthbytes, fullSrcHeight;
107     WORD bpp, compression;
108     const char *ptr;
109     INT line;
110
111     TRACE("%08x (%d,%d %dx%d) -> (%d,%d %dx%d)\n", dc->hSelf,
112           xSrc, ySrc, widthSrc, heightSrc, xDst, yDst, widthDst, heightDst);
113
114     DIB_GetBitmapInfo((const BITMAPINFOHEADER *)info, &fullSrcWidth,
115                       &fullSrcHeight, &bpp, &compression);
116
117     widthbytes = DIB_GetDIBWidthBytes(fullSrcWidth, bpp);
118
119     TRACE("full size=%ldx%d bpp=%d compression=%d\n", fullSrcWidth,
120           fullSrcHeight, bpp, compression);
121
122
123     if(compression != BI_RGB) {
124         FIXME("Compression not supported\n");
125         return FALSE;
126     }
127
128     xDst = XLPTODP(dc, xDst);
129     yDst = YLPTODP(dc, yDst);
130     widthDst = XLSTODS(dc, widthDst);
131     heightDst = YLSTODS(dc, heightDst);
132
133     switch(bpp) {
134
135     case 1:
136         PSDRV_WriteGSave(dc);
137         PSDRV_WriteImageHeader(dc, info, xDst, yDst, widthDst, heightDst,
138                                widthSrc, heightSrc);
139         ptr = bits;
140         ptr += (ySrc * widthbytes);
141         if(xSrc & 7)
142             FIXME("This won't work...\n");
143         for(line = 0; line < heightSrc; line++, ptr += widthbytes)
144             PSDRV_WriteBytes(dc, ptr + xSrc/8, (widthSrc+7)/8);
145         break;
146
147     case 4:
148         PSDRV_WriteGSave(dc);
149         PSDRV_WriteImageHeader(dc, info, xDst, yDst, widthDst, heightDst,
150                                widthSrc, heightSrc);
151         ptr = bits;
152         ptr += (ySrc * widthbytes);
153         if(xSrc & 1)
154             FIXME("This won't work...\n");
155         for(line = 0; line < heightSrc; line++, ptr += widthbytes)
156             PSDRV_WriteBytes(dc, ptr + xSrc/2, (widthSrc+1)/2);
157         break;
158
159     case 8:
160         PSDRV_WriteGSave(dc);
161         PSDRV_WriteImageHeader(dc, info, xDst, yDst, widthDst, heightDst,
162                                widthSrc, heightSrc);
163         ptr = bits;
164         ptr += (ySrc * widthbytes);
165         for(line = 0; line < heightSrc; line++, ptr += widthbytes)
166             PSDRV_WriteBytes(dc, ptr + xSrc, widthSrc);
167         break;
168
169     case 15:
170     case 16:
171         PSDRV_WriteGSave(dc);
172         PSDRV_WriteImageHeader(dc, info, xDst, yDst, widthDst, heightDst,
173                                widthSrc, heightSrc);
174
175         ptr = bits;
176         ptr += (ySrc * widthbytes);
177         for(line = 0; line < heightSrc; line++, ptr += widthbytes)
178             PSDRV_WriteDIBits16(dc, (WORD *)ptr + xSrc, widthSrc);
179         break;
180
181     case 24:
182         PSDRV_WriteGSave(dc);
183         PSDRV_WriteImageHeader(dc, info, xDst, yDst, widthDst, heightDst,
184                                widthSrc, heightSrc);
185
186         ptr = bits;
187         ptr += (ySrc * widthbytes);
188         for(line = 0; line < heightSrc; line++, ptr += widthbytes)
189             PSDRV_WriteDIBits24(dc, ptr + xSrc * 3, widthSrc);
190         break;
191
192     case 32:
193         PSDRV_WriteGSave(dc);
194         PSDRV_WriteImageHeader(dc, info, xDst, yDst, widthDst, heightDst,
195                                widthSrc, heightSrc);
196
197         ptr = bits;
198         ptr += (ySrc * widthbytes);
199         for(line = 0; line < heightSrc; line++, ptr += widthbytes)
200             PSDRV_WriteDIBits32(dc, ptr + xSrc * 3, widthSrc);
201         break;
202
203     default:
204         FIXME("Unsupported depth\n");
205         return FALSE;
206
207     }
208     PSDRV_WriteSpool(dc, ">\n", 2);  /* End-of-Data for /HexASCIIDecodeFilter */
209     PSDRV_WriteGRestore(dc);
210     return TRUE;
211 }
212
213
214
215
216
217