gdiplus: Added GdipImageSelectActiveFrame stub.
[wine] / dlls / gdiplus / image.c
1 /*
2  * Copyright (C) 2007 Google (Evan Stade)
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdarg.h>
20
21 #include "windef.h"
22 #include "winbase.h"
23 #include "winuser.h"
24 #include "wingdi.h"
25
26 #define COBJMACROS
27 #include "objbase.h"
28 #include "olectl.h"
29 #include "ole2.h"
30
31 #include "gdiplus.h"
32 #include "gdiplus_private.h"
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
36
37 typedef void ImageItemData;
38
39 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
40
41 static INT ipicture_pixel_height(IPicture *pic)
42 {
43     HDC hdcref;
44     OLE_YSIZE_HIMETRIC y;
45
46     IPicture_get_Height(pic, &y);
47
48     hdcref = GetDC(0);
49
50     y = (UINT)(((REAL)y) * ((REAL)GetDeviceCaps(hdcref, LOGPIXELSY)) /
51               ((REAL)INCH_HIMETRIC));
52     ReleaseDC(0, hdcref);
53
54     return y;
55 }
56
57 static INT ipicture_pixel_width(IPicture *pic)
58 {
59     HDC hdcref;
60     OLE_XSIZE_HIMETRIC x;
61
62     IPicture_get_Width(pic, &x);
63
64     hdcref = GetDC(0);
65
66     x = (UINT)(((REAL)x) * ((REAL)GetDeviceCaps(hdcref, LOGPIXELSX)) /
67               ((REAL)INCH_HIMETRIC));
68
69     ReleaseDC(0, hdcref);
70
71     return x;
72 }
73
74 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
75     ARGB *color)
76 {
77     static int calls;
78     TRACE("%p %d %d %p\n", bitmap, x, y, color);
79
80     if(!bitmap || !color)
81         return InvalidParameter;
82
83     if(!(calls++))
84         FIXME("not implemented\n");
85
86     *color = 0xdeadbeef;
87
88     return NotImplemented;
89 }
90
91 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
92     PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
93 {
94     BITMAPFILEHEADER *bmfh;
95     BITMAPINFOHEADER *bmih;
96     BYTE *buff;
97     INT datalen = stride * height, size;
98     IStream *stream;
99
100     TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
101
102     if(!scan0 || !bitmap)
103         return InvalidParameter;
104
105     *bitmap = GdipAlloc(sizeof(GpBitmap));
106     if(!*bitmap)    return OutOfMemory;
107
108     size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
109     buff = GdipAlloc(size);
110     if(!buff){
111         GdipFree(*bitmap);
112         return OutOfMemory;
113     }
114
115     bmfh = (BITMAPFILEHEADER*) buff;
116     bmih = (BITMAPINFOHEADER*) (bmfh + 1);
117
118     bmfh->bfType    = (((WORD)'M') << 8) + (WORD)'B';
119     bmfh->bfSize    = size;
120     bmfh->bfOffBits = size - datalen;
121
122     bmih->biSize            = sizeof(BITMAPINFOHEADER);
123     bmih->biWidth           = width;
124     bmih->biHeight          = height;
125     /* FIXME: use the rest of the data from format */
126     bmih->biBitCount        = PIXELFORMATBPP(format);
127     bmih->biCompression     = BI_RGB;
128
129     memcpy(bmih + 1, scan0, datalen);
130
131     if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
132         ERR("could not make stream\n");
133         GdipFree(*bitmap);
134         GdipFree(buff);
135         return GenericError;
136     }
137
138     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
139         (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
140         TRACE("Could not load picture\n");
141         IStream_Release(stream);
142         GdipFree(*bitmap);
143         GdipFree(buff);
144         return GenericError;
145     }
146
147     (*bitmap)->image.type = ImageTypeBitmap;
148     (*bitmap)->width = width;
149     (*bitmap)->height = height;
150     (*bitmap)->format = format;
151
152     return Ok;
153 }
154
155 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
156     GpBitmap **bitmap)
157 {
158     BITMAPINFO bmi;
159     BITMAPCOREHEADER* bmch;
160     OLE_HANDLE hbm;
161     HDC hdc;
162     GpStatus stat;
163
164     stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
165
166     if(stat != Ok)
167         return stat;
168
169     /* FIXME: make sure it's actually a bitmap */
170     (*bitmap)->image.type = ImageTypeBitmap;
171     (*bitmap)->width = ipicture_pixel_width((*bitmap)->image.picture);
172     (*bitmap)->height = ipicture_pixel_height((*bitmap)->image.picture);
173
174     /* get the pixel format */
175     IPicture_get_Handle((*bitmap)->image.picture, &hbm);
176     IPicture_get_CurDC((*bitmap)->image.picture, &hdc);
177
178     bmch = (BITMAPCOREHEADER*) (&bmi.bmiHeader);
179     bmch->bcSize = sizeof(BITMAPCOREHEADER);
180
181     if(!hdc){
182         HBITMAP old;
183         hdc = GetDC(0);
184         old = SelectObject(hdc, (HBITMAP)hbm);
185         GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
186         SelectObject(hdc, old);
187         ReleaseDC(0, hdc);
188     }
189     else
190         GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
191
192     (*bitmap)->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
193
194     return Ok;
195 }
196
197 /* FIXME: no icm */
198 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
199     GpBitmap **bitmap)
200 {
201     return GdipCreateBitmapFromStream(stream, bitmap);
202 }
203
204 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
205 {
206     if(!image)
207         return InvalidParameter;
208
209     IPicture_Release(image->picture);
210     GdipFree(image);
211
212     return Ok;
213 }
214
215 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
216 {
217     if(!image || !item)
218         return InvalidParameter;
219
220     return NotImplemented;
221 }
222
223 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
224     GpUnit *srcUnit)
225 {
226     if(!image || !srcRect || !srcUnit)
227         return InvalidParameter;
228     if(image->type == ImageTypeMetafile){
229         memcpy(srcRect, &((GpMetafile*)image)->bounds, sizeof(GpRectF));
230         *srcUnit = ((GpMetafile*)image)->unit;
231     }
232     else if(image->type == ImageTypeBitmap){
233         srcRect->X = srcRect->Y = 0.0;
234         srcRect->Width = (REAL) ((GpBitmap*)image)->width;
235         srcRect->Height = (REAL) ((GpBitmap*)image)->height;
236         *srcUnit = UnitPixel;
237     }
238     else{
239         srcRect->X = srcRect->Y = 0.0;
240         srcRect->Width = ipicture_pixel_width(image->picture);
241         srcRect->Height = ipicture_pixel_height(image->picture);
242         *srcUnit = UnitPixel;
243     }
244
245     TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
246           srcRect->Width, srcRect->Height, *srcUnit);
247
248     return Ok;
249 }
250
251 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
252 {
253     if(!image || !height)
254         return InvalidParameter;
255
256     if(image->type == ImageTypeMetafile){
257         FIXME("not implemented for metafiles\n");
258         return NotImplemented;
259     }
260     else if(image->type == ImageTypeBitmap)
261         *height = ((GpBitmap*)image)->height;
262     else
263         *height = ipicture_pixel_height(image->picture);
264
265     TRACE("returning %d\n", *height);
266
267     return Ok;
268 }
269
270 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
271 {
272     static int calls;
273
274     if(!image || !res)
275         return InvalidParameter;
276
277     if(!(calls++))
278         FIXME("not implemented\n");
279
280     return NotImplemented;
281 }
282
283 /* FIXME: test this function for non-bitmap types */
284 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
285 {
286     if(!image || !format)
287         return InvalidParameter;
288
289     if(image->type != ImageTypeBitmap)
290         *format = PixelFormat24bppRGB;
291     else
292         *format = ((GpBitmap*) image)->format;
293
294     return Ok;
295 }
296
297 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
298 {
299     static int calls;
300
301     if(!image || !format)
302         return InvalidParameter;
303
304     if(!(calls++))
305         FIXME("not implemented\n");
306
307     return NotImplemented;
308 }
309
310 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
311 {
312     if(!image || !type)
313         return InvalidParameter;
314
315     *type = image->type;
316
317     return Ok;
318 }
319
320 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
321 {
322     static int calls;
323
324     if(!image || !res)
325         return InvalidParameter;
326
327     if(!(calls++))
328         FIXME("not implemented\n");
329
330     return NotImplemented;
331 }
332
333 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
334 {
335     if(!image || !width)
336         return InvalidParameter;
337
338     if(image->type == ImageTypeMetafile){
339         FIXME("not implemented for metafiles\n");
340         return NotImplemented;
341     }
342     else if(image->type == ImageTypeBitmap)
343         *width = ((GpBitmap*)image)->width;
344     else
345         *width = ipicture_pixel_width(image->picture);
346
347     TRACE("returning %d\n", *width);
348
349     return Ok;
350 }
351
352 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
353     MetafileHeader * header)
354 {
355     static int calls;
356
357     if(!metafile || !header)
358         return InvalidParameter;
359
360     if(!(calls++))
361         FIXME("not implemented\n");
362
363     return NotImplemented;
364 }
365
366 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
367     UINT* size)
368 {
369     static int calls;
370
371     TRACE("%p %x %p\n", image, pid, size);
372
373     if(!size || !image)
374         return InvalidParameter;
375
376     if(!(calls++))
377         FIXME("not implemented\n");
378
379     return NotImplemented;
380 }
381
382 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
383     GDIPCONST GUID* dimensionID, UINT* count)
384 {
385     static int calls;
386
387     if(!image || !dimensionID || !count)
388         return InvalidParameter;
389
390     if(!(calls++))
391         FIXME("not implemented\n");
392
393     return NotImplemented;
394 }
395
396 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
397     GUID* dimensionIDs, UINT count)
398 {
399     static int calls;
400
401     if(!image || !dimensionIDs)
402         return InvalidParameter;
403
404     if(!(calls++))
405         FIXME("not implemented\n");
406
407     return Ok;
408 }
409
410 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
411     GDIPCONST GUID* dimensionID, UINT frameidx)
412 {
413     static int calls;
414
415     if(!image || !dimensionID)
416         return InvalidParameter;
417
418     if(!(calls++))
419         FIXME("not implemented\n");
420
421     return Ok;
422 }
423
424 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
425 {
426     if(!stream || !image)
427         return InvalidParameter;
428
429     *image = GdipAlloc(sizeof(GpImage));
430     if(!*image) return OutOfMemory;
431
432     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
433         (LPVOID*) &((*image)->picture)) != S_OK){
434         TRACE("Could not load picture\n");
435         GdipFree(*image);
436         return GenericError;
437     }
438
439     /* FIXME: use IPicture_get_Type to get image type? */
440     (*image)->type = ImageTypeUnknown;
441
442     return Ok;
443 }
444
445 /* FIXME: no ICM */
446 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
447 {
448     return GdipLoadImageFromStream(stream, image);
449 }
450
451 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
452 {
453     static int calls;
454
455     if(!image)
456         return InvalidParameter;
457
458     if(!(calls++))
459         FIXME("not implemented\n");
460
461     return NotImplemented;
462 }
463
464 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
465     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
466 {
467     if(!image || !stream)
468         return InvalidParameter;
469
470     /* FIXME: CLSID, EncoderParameters not used */
471
472     IPicture_SaveAsFile(image->picture, stream, FALSE, NULL);
473
474     return Ok;
475 }