gdiplus: Added GdipCreateBitmapFromFile.
[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 /* This function returns a pointer to an array of pixels that represents the
92  * bitmap. The *entire* bitmap is locked according to the lock mode specified by
93  * flags.  It is correct behavior that a user who calls this function with write
94  * privileges can write to the whole bitmap (not just the area in rect).
95  *
96  * FIXME: only used portion of format is bits per pixel. */
97 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
98     UINT flags, PixelFormat format, BitmapData* lockeddata)
99 {
100     BOOL bm_is_selected;
101     INT stride, bitspp = PIXELFORMATBPP(format);
102     OLE_HANDLE hbm;
103     HDC hdc;
104     HBITMAP old = NULL;
105     BITMAPINFO bmi;
106     BYTE *buff = NULL;
107     UINT abs_height;
108
109     TRACE("%p %p %d %d %p\n", bitmap, rect, flags, format, lockeddata);
110
111     if(!lockeddata || !bitmap || !rect)
112         return InvalidParameter;
113
114     if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
115        (rect->Y + rect->Height > bitmap->height) || !flags)
116         return InvalidParameter;
117
118     if(flags & ImageLockModeUserInputBuf)
119         return NotImplemented;
120
121     if((bitmap->lockmode & ImageLockModeWrite) || (bitmap->lockmode &&
122         (flags & ImageLockModeWrite)))
123         return WrongState;
124
125     IPicture_get_Handle(bitmap->image.picture, &hbm);
126     IPicture_get_CurDC(bitmap->image.picture, &hdc);
127     bm_is_selected = (hdc != 0);
128
129     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
130     bmi.bmiHeader.biBitCount = 0;
131
132     if(!bm_is_selected){
133         hdc = GetDC(0);
134         old = SelectObject(hdc, (HBITMAP)hbm);
135     }
136
137     /* fill out bmi */
138     GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
139
140     abs_height = abs(bmi.bmiHeader.biHeight);
141     stride = bmi.bmiHeader.biWidth * bitspp / 8;
142     stride = (stride + 3) & ~3;
143
144     buff = GdipAlloc(stride * abs_height);
145     if(!buff)   return OutOfMemory;
146
147     bmi.bmiHeader.biBitCount = bitspp;
148     GetDIBits(hdc, (HBITMAP)hbm, 0, abs_height, buff, &bmi, DIB_RGB_COLORS);
149
150     if(!bm_is_selected){
151         SelectObject(hdc, old);
152         ReleaseDC(0, hdc);
153     }
154
155     lockeddata->Width = rect->Width;
156     lockeddata->Height = rect->Height;
157     lockeddata->PixelFormat = format;
158     lockeddata->Reserved = flags;
159
160     if(bmi.bmiHeader.biHeight > 0){
161         lockeddata->Stride = -stride;
162         lockeddata->Scan0 = buff + (bitspp / 8) * rect->X +
163                             stride * (abs_height - 1 - rect->Y);
164     }
165     else{
166         lockeddata->Stride = stride;
167         lockeddata->Scan0 = buff + (bitspp / 8) * rect->X + stride * rect->Y;
168     }
169
170     bitmap->lockmode = flags;
171     bitmap->numlocks++;
172
173     if(flags & ImageLockModeWrite)
174         bitmap->bitmapbits = buff;
175
176     return Ok;
177 }
178
179 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
180     BitmapData* lockeddata)
181 {
182     OLE_HANDLE hbm;
183     HDC hdc;
184     HBITMAP old = NULL;
185     BOOL bm_is_selected;
186     BITMAPINFO bmi;
187
188     if(!bitmap || !lockeddata)
189         return InvalidParameter;
190
191     if(!bitmap->lockmode)
192         return WrongState;
193
194     if(lockeddata->Reserved & ImageLockModeUserInputBuf)
195         return NotImplemented;
196
197     if(lockeddata->Reserved & ImageLockModeRead){
198         if(!(--bitmap->numlocks))
199             bitmap->lockmode = 0;
200
201         GdipFree(lockeddata->Scan0);
202         return Ok;
203     }
204
205     IPicture_get_Handle(bitmap->image.picture, &hbm);
206     IPicture_get_CurDC(bitmap->image.picture, &hdc);
207     bm_is_selected = (hdc != 0);
208
209     bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
210     bmi.bmiHeader.biBitCount = 0;
211
212     if(!bm_is_selected){
213         hdc = GetDC(0);
214         old = SelectObject(hdc, (HBITMAP)hbm);
215     }
216
217     GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
218     bmi.bmiHeader.biBitCount = PIXELFORMATBPP(lockeddata->PixelFormat);
219     SetDIBits(hdc, (HBITMAP)hbm, 0, abs(bmi.bmiHeader.biHeight),
220               bitmap->bitmapbits, &bmi, DIB_RGB_COLORS);
221
222     if(!bm_is_selected){
223         SelectObject(hdc, old);
224         ReleaseDC(0, hdc);
225     }
226
227     GdipFree(bitmap->bitmapbits);
228
229     return Ok;
230 }
231
232 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
233     GpBitmap **bitmap)
234 {
235     GpStatus stat;
236     IStream *stream;
237
238     if(!filename || !bitmap)
239         return InvalidParameter;
240
241     stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
242
243     if(stat != Ok)
244         return stat;
245
246     stat = GdipCreateBitmapFromStream(stream, bitmap);
247
248     if(!stat)
249         IStream_Release(stream);
250
251     return stat;
252 }
253
254 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
255     PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
256 {
257     BITMAPFILEHEADER *bmfh;
258     BITMAPINFOHEADER *bmih;
259     BYTE *buff;
260     INT datalen = stride * height, size;
261     IStream *stream;
262
263     TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
264
265     if(!scan0 || !bitmap)
266         return InvalidParameter;
267
268     *bitmap = GdipAlloc(sizeof(GpBitmap));
269     if(!*bitmap)    return OutOfMemory;
270
271     size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
272     buff = GdipAlloc(size);
273     if(!buff){
274         GdipFree(*bitmap);
275         return OutOfMemory;
276     }
277
278     bmfh = (BITMAPFILEHEADER*) buff;
279     bmih = (BITMAPINFOHEADER*) (bmfh + 1);
280
281     bmfh->bfType    = (((WORD)'M') << 8) + (WORD)'B';
282     bmfh->bfSize    = size;
283     bmfh->bfOffBits = size - datalen;
284
285     bmih->biSize            = sizeof(BITMAPINFOHEADER);
286     bmih->biWidth           = width;
287     bmih->biHeight          = height;
288     /* FIXME: use the rest of the data from format */
289     bmih->biBitCount        = PIXELFORMATBPP(format);
290     bmih->biCompression     = BI_RGB;
291
292     memcpy(bmih + 1, scan0, datalen);
293
294     if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
295         ERR("could not make stream\n");
296         GdipFree(*bitmap);
297         GdipFree(buff);
298         return GenericError;
299     }
300
301     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
302         (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
303         TRACE("Could not load picture\n");
304         IStream_Release(stream);
305         GdipFree(*bitmap);
306         GdipFree(buff);
307         return GenericError;
308     }
309
310     (*bitmap)->image.type = ImageTypeBitmap;
311     (*bitmap)->width = width;
312     (*bitmap)->height = height;
313     (*bitmap)->format = format;
314
315     return Ok;
316 }
317
318 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
319     GpBitmap **bitmap)
320 {
321     BITMAPINFO bmi;
322     BITMAPCOREHEADER* bmch;
323     OLE_HANDLE hbm;
324     HDC hdc;
325     GpStatus stat;
326
327     stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
328
329     if(stat != Ok)
330         return stat;
331
332     /* FIXME: make sure it's actually a bitmap */
333     (*bitmap)->image.type = ImageTypeBitmap;
334     (*bitmap)->width = ipicture_pixel_width((*bitmap)->image.picture);
335     (*bitmap)->height = ipicture_pixel_height((*bitmap)->image.picture);
336
337     /* get the pixel format */
338     IPicture_get_Handle((*bitmap)->image.picture, &hbm);
339     IPicture_get_CurDC((*bitmap)->image.picture, &hdc);
340
341     bmch = (BITMAPCOREHEADER*) (&bmi.bmiHeader);
342     bmch->bcSize = sizeof(BITMAPCOREHEADER);
343
344     if(!hdc){
345         HBITMAP old;
346         hdc = GetDC(0);
347         old = SelectObject(hdc, (HBITMAP)hbm);
348         GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
349         SelectObject(hdc, old);
350         ReleaseDC(0, hdc);
351     }
352     else
353         GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
354
355     (*bitmap)->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
356
357     return Ok;
358 }
359
360 /* FIXME: no icm */
361 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
362     GpBitmap **bitmap)
363 {
364     return GdipCreateBitmapFromStream(stream, bitmap);
365 }
366
367 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
368 {
369     if(!image)
370         return InvalidParameter;
371
372     IPicture_Release(image->picture);
373     GdipFree(image);
374
375     return Ok;
376 }
377
378 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
379 {
380     if(!image || !item)
381         return InvalidParameter;
382
383     return NotImplemented;
384 }
385
386 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
387     GpUnit *srcUnit)
388 {
389     if(!image || !srcRect || !srcUnit)
390         return InvalidParameter;
391     if(image->type == ImageTypeMetafile){
392         memcpy(srcRect, &((GpMetafile*)image)->bounds, sizeof(GpRectF));
393         *srcUnit = ((GpMetafile*)image)->unit;
394     }
395     else if(image->type == ImageTypeBitmap){
396         srcRect->X = srcRect->Y = 0.0;
397         srcRect->Width = (REAL) ((GpBitmap*)image)->width;
398         srcRect->Height = (REAL) ((GpBitmap*)image)->height;
399         *srcUnit = UnitPixel;
400     }
401     else{
402         srcRect->X = srcRect->Y = 0.0;
403         srcRect->Width = ipicture_pixel_width(image->picture);
404         srcRect->Height = ipicture_pixel_height(image->picture);
405         *srcUnit = UnitPixel;
406     }
407
408     TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
409           srcRect->Width, srcRect->Height, *srcUnit);
410
411     return Ok;
412 }
413
414 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
415 {
416     if(!image || !height)
417         return InvalidParameter;
418
419     if(image->type == ImageTypeMetafile){
420         FIXME("not implemented for metafiles\n");
421         return NotImplemented;
422     }
423     else if(image->type == ImageTypeBitmap)
424         *height = ((GpBitmap*)image)->height;
425     else
426         *height = ipicture_pixel_height(image->picture);
427
428     TRACE("returning %d\n", *height);
429
430     return Ok;
431 }
432
433 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
434 {
435     static int calls;
436
437     if(!image || !res)
438         return InvalidParameter;
439
440     if(!(calls++))
441         FIXME("not implemented\n");
442
443     return NotImplemented;
444 }
445
446 /* FIXME: test this function for non-bitmap types */
447 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
448 {
449     if(!image || !format)
450         return InvalidParameter;
451
452     if(image->type != ImageTypeBitmap)
453         *format = PixelFormat24bppRGB;
454     else
455         *format = ((GpBitmap*) image)->format;
456
457     return Ok;
458 }
459
460 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
461 {
462     static int calls;
463
464     if(!image || !format)
465         return InvalidParameter;
466
467     if(!(calls++))
468         FIXME("not implemented\n");
469
470     return NotImplemented;
471 }
472
473 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
474 {
475     if(!image || !type)
476         return InvalidParameter;
477
478     *type = image->type;
479
480     return Ok;
481 }
482
483 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
484 {
485     static int calls;
486
487     if(!image || !res)
488         return InvalidParameter;
489
490     if(!(calls++))
491         FIXME("not implemented\n");
492
493     return NotImplemented;
494 }
495
496 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
497 {
498     if(!image || !width)
499         return InvalidParameter;
500
501     if(image->type == ImageTypeMetafile){
502         FIXME("not implemented for metafiles\n");
503         return NotImplemented;
504     }
505     else if(image->type == ImageTypeBitmap)
506         *width = ((GpBitmap*)image)->width;
507     else
508         *width = ipicture_pixel_width(image->picture);
509
510     TRACE("returning %d\n", *width);
511
512     return Ok;
513 }
514
515 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
516     MetafileHeader * header)
517 {
518     static int calls;
519
520     if(!metafile || !header)
521         return InvalidParameter;
522
523     if(!(calls++))
524         FIXME("not implemented\n");
525
526     return NotImplemented;
527 }
528
529 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
530     UINT* size)
531 {
532     static int calls;
533
534     TRACE("%p %x %p\n", image, pid, size);
535
536     if(!size || !image)
537         return InvalidParameter;
538
539     if(!(calls++))
540         FIXME("not implemented\n");
541
542     return NotImplemented;
543 }
544
545 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
546     GDIPCONST GUID* dimensionID, UINT* count)
547 {
548     static int calls;
549
550     if(!image || !dimensionID || !count)
551         return InvalidParameter;
552
553     if(!(calls++))
554         FIXME("not implemented\n");
555
556     return NotImplemented;
557 }
558
559 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
560     GUID* dimensionIDs, UINT count)
561 {
562     static int calls;
563
564     if(!image || !dimensionIDs)
565         return InvalidParameter;
566
567     if(!(calls++))
568         FIXME("not implemented\n");
569
570     return Ok;
571 }
572
573 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
574     GDIPCONST GUID* dimensionID, UINT frameidx)
575 {
576     static int calls;
577
578     if(!image || !dimensionID)
579         return InvalidParameter;
580
581     if(!(calls++))
582         FIXME("not implemented\n");
583
584     return Ok;
585 }
586
587 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
588 {
589     if(!stream || !image)
590         return InvalidParameter;
591
592     *image = GdipAlloc(sizeof(GpImage));
593     if(!*image) return OutOfMemory;
594
595     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
596         (LPVOID*) &((*image)->picture)) != S_OK){
597         TRACE("Could not load picture\n");
598         GdipFree(*image);
599         return GenericError;
600     }
601
602     /* FIXME: use IPicture_get_Type to get image type? */
603     (*image)->type = ImageTypeUnknown;
604
605     return Ok;
606 }
607
608 /* FIXME: no ICM */
609 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
610 {
611     return GdipLoadImageFromStream(stream, image);
612 }
613
614 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
615 {
616     static int calls;
617
618     if(!image)
619         return InvalidParameter;
620
621     if(!(calls++))
622         FIXME("not implemented\n");
623
624     return NotImplemented;
625 }
626
627 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
628     GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
629 {
630     if(!image || !stream)
631         return InvalidParameter;
632
633     /* FIXME: CLSID, EncoderParameters not used */
634
635     IPicture_SaveAsFile(image->picture, stream, FALSE, NULL);
636
637     return Ok;
638 }