gdiplus: IPicture_SaveAsFile updates seek pointer, so put it back.
[wine/wine64.git] / dlls / gdiplus / image.c
blob0cb5ded5aca2188c1324ca5f0013dabae7bc68a6
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
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.
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.
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
19 #include <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "winuser.h"
24 #include "wingdi.h"
26 #define COBJMACROS
27 #include "objbase.h"
28 #include "olectl.h"
29 #include "ole2.h"
31 #include "gdiplus.h"
32 #include "gdiplus_private.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
37 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
39 static INT ipicture_pixel_height(IPicture *pic)
41 HDC hdcref;
42 OLE_YSIZE_HIMETRIC y;
44 IPicture_get_Height(pic, &y);
46 hdcref = GetDC(0);
48 y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
49 ReleaseDC(0, hdcref);
51 return y;
54 static INT ipicture_pixel_width(IPicture *pic)
56 HDC hdcref;
57 OLE_XSIZE_HIMETRIC x;
59 IPicture_get_Width(pic, &x);
61 hdcref = GetDC(0);
63 x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
65 ReleaseDC(0, hdcref);
67 return x;
70 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
71 ARGB *color)
73 static int calls;
74 TRACE("%p %d %d %p\n", bitmap, x, y, color);
76 if(!bitmap || !color)
77 return InvalidParameter;
79 if(!(calls++))
80 FIXME("not implemented\n");
82 *color = 0xdeadbeef;
84 return NotImplemented;
87 /* This function returns a pointer to an array of pixels that represents the
88 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
89 * flags. It is correct behavior that a user who calls this function with write
90 * privileges can write to the whole bitmap (not just the area in rect).
92 * FIXME: only used portion of format is bits per pixel. */
93 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
94 UINT flags, PixelFormat format, BitmapData* lockeddata)
96 BOOL bm_is_selected;
97 INT stride, bitspp = PIXELFORMATBPP(format);
98 OLE_HANDLE hbm;
99 HDC hdc;
100 HBITMAP old = NULL;
101 BITMAPINFO *pbmi;
102 BYTE *buff = NULL;
103 UINT abs_height;
104 GpRect act_rect; /* actual rect to be used */
106 TRACE("%p %p %d %d %p\n", bitmap, rect, flags, format, lockeddata);
108 if(!lockeddata || !bitmap)
109 return InvalidParameter;
111 if(rect){
112 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
113 (rect->Y + rect->Height > bitmap->height) || !flags)
114 return InvalidParameter;
116 act_rect = *rect;
118 else{
119 act_rect.X = act_rect.Y = 0;
120 act_rect.Width = bitmap->width;
121 act_rect.Height = bitmap->height;
124 if(flags & ImageLockModeUserInputBuf)
125 return NotImplemented;
127 if(bitmap->lockmode)
128 return WrongState;
130 IPicture_get_Handle(bitmap->image.picture, &hbm);
131 IPicture_get_CurDC(bitmap->image.picture, &hdc);
132 bm_is_selected = (hdc != 0);
134 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
135 if (!pbmi)
136 return OutOfMemory;
137 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
138 pbmi->bmiHeader.biBitCount = 0;
140 if(!bm_is_selected){
141 hdc = CreateCompatibleDC(0);
142 old = SelectObject(hdc, (HBITMAP)hbm);
145 /* fill out bmi */
146 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
148 abs_height = abs(pbmi->bmiHeader.biHeight);
149 stride = pbmi->bmiHeader.biWidth * bitspp / 8;
150 stride = (stride + 3) & ~3;
152 buff = GdipAlloc(stride * abs_height);
154 pbmi->bmiHeader.biBitCount = bitspp;
156 if(buff)
157 GetDIBits(hdc, (HBITMAP)hbm, 0, abs_height, buff, pbmi, DIB_RGB_COLORS);
159 if(!bm_is_selected){
160 SelectObject(hdc, old);
161 DeleteDC(hdc);
164 if(!buff){
165 GdipFree(pbmi);
166 return OutOfMemory;
169 lockeddata->Width = act_rect.Width;
170 lockeddata->Height = act_rect.Height;
171 lockeddata->PixelFormat = format;
172 lockeddata->Reserved = flags;
174 if(pbmi->bmiHeader.biHeight > 0){
175 lockeddata->Stride = -stride;
176 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X +
177 stride * (abs_height - 1 - act_rect.Y);
179 else{
180 lockeddata->Stride = stride;
181 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X + stride * act_rect.Y;
184 bitmap->lockmode = flags;
185 bitmap->numlocks++;
187 bitmap->bitmapbits = buff;
189 GdipFree(pbmi);
190 return Ok;
193 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
194 BitmapData* lockeddata)
196 OLE_HANDLE hbm;
197 HDC hdc;
198 HBITMAP old = NULL;
199 BOOL bm_is_selected;
200 BITMAPINFO *pbmi;
202 if(!bitmap || !lockeddata)
203 return InvalidParameter;
205 if(!bitmap->lockmode)
206 return WrongState;
208 if(lockeddata->Reserved & ImageLockModeUserInputBuf)
209 return NotImplemented;
211 if(lockeddata->Reserved & ImageLockModeRead){
212 if(!(--bitmap->numlocks))
213 bitmap->lockmode = 0;
215 GdipFree(bitmap->bitmapbits);
216 bitmap->bitmapbits = NULL;
217 return Ok;
220 IPicture_get_Handle(bitmap->image.picture, &hbm);
221 IPicture_get_CurDC(bitmap->image.picture, &hdc);
222 bm_is_selected = (hdc != 0);
224 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
225 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
226 pbmi->bmiHeader.biBitCount = 0;
228 if(!bm_is_selected){
229 hdc = CreateCompatibleDC(0);
230 old = SelectObject(hdc, (HBITMAP)hbm);
233 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
234 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(lockeddata->PixelFormat);
235 SetDIBits(hdc, (HBITMAP)hbm, 0, abs(pbmi->bmiHeader.biHeight),
236 bitmap->bitmapbits, pbmi, DIB_RGB_COLORS);
238 if(!bm_is_selected){
239 SelectObject(hdc, old);
240 DeleteDC(hdc);
243 GdipFree(pbmi);
244 GdipFree(bitmap->bitmapbits);
245 bitmap->bitmapbits = NULL;
246 bitmap->lockmode = 0;
248 return Ok;
251 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
253 IStream* stream;
254 HRESULT hr;
255 INT size;
256 LARGE_INTEGER move;
258 TRACE("%p, %p\n", image, cloneImage);
260 if (!image || !cloneImage)
261 return InvalidParameter;
263 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
264 if (FAILED(hr))
265 return GenericError;
267 *cloneImage = GdipAlloc(sizeof(GpImage));
268 if (!*cloneImage)
270 IStream_Release(stream);
271 return OutOfMemory;
273 (*cloneImage)->type = image->type;
274 (*cloneImage)->flags = image->flags;
276 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
277 if(FAILED(hr))
279 WARN("Failed to save image on stream\n");
280 goto out;
283 /* Set seek pointer back to the beginning of the picture */
284 move.QuadPart = 0;
285 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
286 if (FAILED(hr))
287 goto out;
289 hr = OleLoadPicture(stream, size, FALSE, &IID_IPicture,
290 (LPVOID*) &(*cloneImage)->picture);
291 if (FAILED(hr))
293 WARN("Failed to load image from stream\n");
294 goto out;
297 IStream_Release(stream);
298 return Ok;
299 out:
300 IStream_Release(stream);
301 GdipFree(*cloneImage);
302 *cloneImage = NULL;
303 return GenericError;
306 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
307 GpBitmap **bitmap)
309 GpStatus stat;
310 IStream *stream;
312 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
314 if(!filename || !bitmap)
315 return InvalidParameter;
317 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
319 if(stat != Ok)
320 return stat;
322 stat = GdipCreateBitmapFromStream(stream, bitmap);
324 IStream_Release(stream);
326 return stat;
329 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
330 VOID *bits, GpBitmap **bitmap)
332 DWORD height, stride;
333 PixelFormat format;
335 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
337 height = abs(info->bmiHeader.biHeight);
338 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
340 if(info->bmiHeader.biHeight > 0) /* bottom-up */
342 bits = (BYTE*)bits + (height - 1) * stride;
343 stride = -stride;
346 switch(info->bmiHeader.biBitCount) {
347 case 1:
348 format = PixelFormat1bppIndexed;
349 break;
350 case 4:
351 format = PixelFormat4bppIndexed;
352 break;
353 case 8:
354 format = PixelFormat8bppIndexed;
355 break;
356 case 24:
357 format = PixelFormat24bppRGB;
358 break;
359 default:
360 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
361 *bitmap = NULL;
362 return InvalidParameter;
365 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
366 bits, bitmap);
370 /* FIXME: no icm */
371 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
372 GpBitmap **bitmap)
374 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
376 return GdipCreateBitmapFromFile(filename, bitmap);
379 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
380 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
382 HBITMAP hbm;
383 GpStatus stat = InvalidParameter;
385 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
387 if(!lpBitmapName || !bitmap)
388 return InvalidParameter;
390 /* load DIB */
391 hbm = (HBITMAP)LoadImageW(hInstance,lpBitmapName,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION);
393 if(hbm){
394 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
395 DeleteObject(hbm);
398 return stat;
401 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
402 HBITMAP* hbmReturn, ARGB background)
404 FIXME("stub\n");
406 hbmReturn = NULL;
408 return NotImplemented;
411 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
412 GpMetafile* metafile, BOOL* succ, EmfType emfType,
413 const WCHAR* description, GpMetafile** out_metafile)
415 static int calls;
417 if(!ref || !metafile || !out_metafile)
418 return InvalidParameter;
420 *succ = FALSE;
421 *out_metafile = NULL;
423 if(!(calls++))
424 FIXME("not implemented\n");
426 return NotImplemented;
429 /* FIXME: this should create a bitmap in the given size with the attributes
430 * (resolution etc.) of the graphics object */
431 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
432 GpGraphics* target, GpBitmap** bitmap)
434 static int calls;
435 GpStatus ret;
437 if(!target || !bitmap)
438 return InvalidParameter;
440 if(!(calls++))
441 FIXME("hacked stub\n");
443 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
444 NULL, bitmap);
446 return ret;
449 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
450 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
452 BITMAPFILEHEADER *bmfh;
453 BITMAPINFOHEADER *bmih;
454 BYTE *buff;
455 INT datalen, size;
456 IStream *stream;
458 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
460 if (!bitmap) return InvalidParameter;
462 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
463 *bitmap = NULL;
464 return InvalidParameter;
467 if(scan0 && !stride)
468 return InvalidParameter;
470 /* FIXME: windows allows negative stride (reads backwards from scan0) */
471 if(stride < 0){
472 FIXME("negative stride\n");
473 return InvalidParameter;
476 *bitmap = GdipAlloc(sizeof(GpBitmap));
477 if(!*bitmap) return OutOfMemory;
479 if(stride == 0){
480 stride = width * (PIXELFORMATBPP(format) / 8);
481 stride = (stride + 3) & ~3;
484 datalen = abs(stride * height);
485 size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
486 buff = GdipAlloc(size);
487 if(!buff){
488 GdipFree(*bitmap);
489 return OutOfMemory;
492 bmfh = (BITMAPFILEHEADER*) buff;
493 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
495 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
496 bmfh->bfSize = size;
497 bmfh->bfOffBits = size - datalen;
499 bmih->biSize = sizeof(BITMAPINFOHEADER);
500 bmih->biWidth = width;
501 bmih->biHeight = -height;
502 /* FIXME: use the rest of the data from format */
503 bmih->biBitCount = PIXELFORMATBPP(format);
504 bmih->biCompression = BI_RGB;
505 bmih->biSizeImage = datalen;
507 if(scan0)
508 memcpy(bmih + 1, scan0, datalen);
509 else
510 memset(bmih + 1, 0, datalen);
512 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
513 ERR("could not make stream\n");
514 GdipFree(*bitmap);
515 GdipFree(buff);
516 return GenericError;
519 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
520 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
521 TRACE("Could not load picture\n");
522 IStream_Release(stream);
523 GdipFree(*bitmap);
524 GdipFree(buff);
525 return GenericError;
528 (*bitmap)->image.type = ImageTypeBitmap;
529 (*bitmap)->image.flags = ImageFlagsNone;
530 (*bitmap)->width = width;
531 (*bitmap)->height = height;
532 (*bitmap)->format = format;
534 return Ok;
537 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
538 GpBitmap **bitmap)
540 GpStatus stat;
542 TRACE("%p %p\n", stream, bitmap);
544 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
546 if(stat != Ok)
547 return stat;
549 if((*bitmap)->image.type != ImageTypeBitmap){
550 IPicture_Release((*bitmap)->image.picture);
551 GdipFree(bitmap);
552 return GenericError; /* FIXME: what error to return? */
555 return Ok;
558 /* FIXME: no icm */
559 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
560 GpBitmap **bitmap)
562 TRACE("%p %p\n", stream, bitmap);
564 return GdipCreateBitmapFromStream(stream, bitmap);
567 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
569 HDC hdc;
571 TRACE("%p\n", image);
573 if(!image)
574 return InvalidParameter;
576 IPicture_get_CurDC(image->picture, &hdc);
577 DeleteDC(hdc);
578 IPicture_Release(image->picture);
579 if (image->type == ImageTypeBitmap)
580 GdipFree(((GpBitmap*)image)->bitmapbits);
581 GdipFree(image);
583 return Ok;
586 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
588 if(!image || !item)
589 return InvalidParameter;
591 return NotImplemented;
594 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
595 GpUnit *srcUnit)
597 TRACE("%p %p %p\n", image, srcRect, srcUnit);
599 if(!image || !srcRect || !srcUnit)
600 return InvalidParameter;
601 if(image->type == ImageTypeMetafile){
602 *srcRect = ((GpMetafile*)image)->bounds;
603 *srcUnit = ((GpMetafile*)image)->unit;
605 else if(image->type == ImageTypeBitmap){
606 srcRect->X = srcRect->Y = 0.0;
607 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
608 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
609 *srcUnit = UnitPixel;
611 else{
612 srcRect->X = srcRect->Y = 0.0;
613 srcRect->Width = ipicture_pixel_width(image->picture);
614 srcRect->Height = ipicture_pixel_height(image->picture);
615 *srcUnit = UnitPixel;
618 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
619 srcRect->Width, srcRect->Height, *srcUnit);
621 return Ok;
624 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
625 REAL *height)
627 TRACE("%p %p %p\n", image, width, height);
629 if(!image || !height || !width)
630 return InvalidParameter;
632 if(image->type == ImageTypeMetafile){
633 HDC hdc = GetDC(0);
635 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
636 ((GpMetafile*)image)->bounds.Height;
638 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
639 ((GpMetafile*)image)->bounds.Width;
641 ReleaseDC(0, hdc);
644 else if(image->type == ImageTypeBitmap){
645 *height = ((GpBitmap*)image)->height;
646 *width = ((GpBitmap*)image)->width;
648 else{
649 *height = ipicture_pixel_height(image->picture);
650 *width = ipicture_pixel_width(image->picture);
653 TRACE("returning (%f, %f)\n", *height, *width);
654 return Ok;
657 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
658 GpGraphics **graphics)
660 HDC hdc;
662 TRACE("%p %p\n", image, graphics);
664 if(!image || !graphics)
665 return InvalidParameter;
667 if(image->type != ImageTypeBitmap){
668 FIXME("not implemented for image type %d\n", image->type);
669 return NotImplemented;
672 IPicture_get_CurDC(image->picture, &hdc);
674 if(!hdc){
675 hdc = CreateCompatibleDC(0);
676 IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
679 return GdipCreateFromHDC(hdc, graphics);
682 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
684 TRACE("%p %p\n", image, height);
686 if(!image || !height)
687 return InvalidParameter;
689 if(image->type == ImageTypeMetafile){
690 HDC hdc = GetDC(0);
692 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
693 ((GpMetafile*)image)->bounds.Height);
695 ReleaseDC(0, hdc);
697 else if(image->type == ImageTypeBitmap)
698 *height = ((GpBitmap*)image)->height;
699 else
700 *height = ipicture_pixel_height(image->picture);
702 TRACE("returning %d\n", *height);
704 return Ok;
707 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
709 static int calls;
711 if(!image || !res)
712 return InvalidParameter;
714 if(!(calls++))
715 FIXME("not implemented\n");
717 return NotImplemented;
720 /* FIXME: test this function for non-bitmap types */
721 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
723 TRACE("%p %p\n", image, format);
725 if(!image || !format)
726 return InvalidParameter;
728 if(image->type != ImageTypeBitmap)
729 *format = PixelFormat24bppRGB;
730 else
731 *format = ((GpBitmap*) image)->format;
733 return Ok;
736 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
738 static int calls;
740 if(!image || !format)
741 return InvalidParameter;
743 if(!(calls++))
744 FIXME("not implemented\n");
746 return NotImplemented;
749 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
751 TRACE("%p %p\n", image, type);
753 if(!image || !type)
754 return InvalidParameter;
756 *type = image->type;
758 return Ok;
761 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
763 static int calls;
765 if(!image || !res)
766 return InvalidParameter;
768 if(!(calls++))
769 FIXME("not implemented\n");
771 return NotImplemented;
774 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
776 TRACE("%p %p\n", image, width);
778 if(!image || !width)
779 return InvalidParameter;
781 if(image->type == ImageTypeMetafile){
782 HDC hdc = GetDC(0);
784 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
785 ((GpMetafile*)image)->bounds.Width);
787 ReleaseDC(0, hdc);
789 else if(image->type == ImageTypeBitmap)
790 *width = ((GpBitmap*)image)->width;
791 else
792 *width = ipicture_pixel_width(image->picture);
794 TRACE("returning %d\n", *width);
796 return Ok;
799 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
800 MetafileHeader * header)
802 static int calls;
804 if(!metafile || !header)
805 return InvalidParameter;
807 if(!(calls++))
808 FIXME("not implemented\n");
810 return Ok;
813 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
814 UINT num, PropertyItem* items)
816 static int calls;
818 if(!(calls++))
819 FIXME("not implemented\n");
821 return InvalidParameter;
824 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
826 static int calls;
828 if(!(calls++))
829 FIXME("not implemented\n");
831 return InvalidParameter;
834 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
836 static int calls;
838 if(!(calls++))
839 FIXME("not implemented\n");
841 return InvalidParameter;
844 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
845 PropertyItem* buffer)
847 static int calls;
849 if(!(calls++))
850 FIXME("not implemented\n");
852 return InvalidParameter;
855 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
856 UINT* size)
858 static int calls;
860 TRACE("%p %x %p\n", image, pid, size);
862 if(!size || !image)
863 return InvalidParameter;
865 if(!(calls++))
866 FIXME("not implemented\n");
868 return NotImplemented;
871 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
873 static int calls;
875 if(!(calls++))
876 FIXME("not implemented\n");
878 return InvalidParameter;
881 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
882 GDIPCONST GUID* dimensionID, UINT* count)
884 static int calls;
886 if(!image || !dimensionID || !count)
887 return InvalidParameter;
889 if(!(calls++))
890 FIXME("not implemented\n");
892 return NotImplemented;
895 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
896 UINT* count)
898 if(!image || !count)
899 return InvalidParameter;
901 *count = 1;
903 FIXME("stub\n");
905 return Ok;
908 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
909 GUID* dimensionIDs, UINT count)
911 static int calls;
913 if(!image || !dimensionIDs)
914 return InvalidParameter;
916 if(!(calls++))
917 FIXME("not implemented\n");
919 return Ok;
922 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
923 GDIPCONST GUID* dimensionID, UINT frameidx)
925 static int calls;
927 if(!image || !dimensionID)
928 return InvalidParameter;
930 if(!(calls++))
931 FIXME("not implemented\n");
933 return Ok;
936 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
937 GpImage **image)
939 GpStatus stat;
940 IStream *stream;
942 TRACE("(%s) %p\n", debugstr_w(filename), image);
944 if (!filename || !image)
945 return InvalidParameter;
947 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
949 if (stat != Ok)
950 return stat;
952 stat = GdipLoadImageFromStream(stream, image);
954 IStream_Release(stream);
956 return stat;
959 /* FIXME: no icm handling */
960 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
962 TRACE("(%s) %p\n", debugstr_w(filename), image);
964 return GdipLoadImageFromFile(filename, image);
967 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
969 IPicture *pic;
970 short type;
972 TRACE("%p %p\n", stream, image);
974 if(!stream || !image)
975 return InvalidParameter;
977 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
978 (LPVOID*) &pic) != S_OK){
979 TRACE("Could not load picture\n");
980 return GenericError;
983 IPicture_get_Type(pic, &type);
985 if(type == PICTYPE_BITMAP){
986 BITMAPINFO *pbmi;
987 BITMAPCOREHEADER* bmch;
988 OLE_HANDLE hbm;
989 HDC hdc;
991 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
992 if (!pbmi)
993 return OutOfMemory;
994 *image = GdipAlloc(sizeof(GpBitmap));
995 if(!*image){
996 GdipFree(pbmi);
997 return OutOfMemory;
999 (*image)->type = ImageTypeBitmap;
1001 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
1002 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
1004 /* get the pixel format */
1005 IPicture_get_Handle(pic, &hbm);
1006 IPicture_get_CurDC(pic, &hdc);
1008 bmch = (BITMAPCOREHEADER*) (&pbmi->bmiHeader);
1009 bmch->bcSize = sizeof(BITMAPCOREHEADER);
1011 if(!hdc){
1012 HBITMAP old;
1013 hdc = CreateCompatibleDC(0);
1014 old = SelectObject(hdc, (HBITMAP)hbm);
1015 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
1016 SelectObject(hdc, old);
1017 DeleteDC(hdc);
1019 else
1020 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
1022 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
1023 GdipFree(pbmi);
1025 else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
1026 /* FIXME: missing initialization code */
1027 *image = GdipAlloc(sizeof(GpMetafile));
1028 if(!*image) return OutOfMemory;
1029 (*image)->type = ImageTypeMetafile;
1031 else{
1032 *image = GdipAlloc(sizeof(GpImage));
1033 if(!*image) return OutOfMemory;
1034 (*image)->type = ImageTypeUnknown;
1037 (*image)->picture = pic;
1038 (*image)->flags = ImageFlagsNone;
1040 return Ok;
1043 /* FIXME: no ICM */
1044 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
1046 TRACE("%p %p\n", stream, image);
1048 return GdipLoadImageFromStream(stream, image);
1051 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
1053 static int calls;
1055 if(!image)
1056 return InvalidParameter;
1058 if(!(calls++))
1059 FIXME("not implemented\n");
1061 return NotImplemented;
1064 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
1066 static int calls;
1068 if(!(calls++))
1069 FIXME("not implemented\n");
1071 return NotImplemented;
1074 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
1075 GDIPCONST CLSID *clsidEncoder,
1076 GDIPCONST EncoderParameters *encoderParams)
1078 GpStatus stat;
1079 IStream *stream;
1081 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
1083 if (!image || !filename|| !clsidEncoder)
1084 return InvalidParameter;
1086 if (!(image->picture))
1087 return InvalidParameter;
1089 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
1090 if (stat != Ok)
1091 return GenericError;
1093 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
1095 IStream_Release(stream);
1096 return stat;
1099 /*************************************************************************
1100 * Encoding functions -
1101 * These functions encode an image in different image file formats.
1103 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
1104 #define BITMAP_FORMAT_JPEG 0xd8ff
1105 #define BITMAP_FORMAT_GIF 0x4947
1106 #define BITMAP_FORMAT_PNG 0x5089
1107 #define BITMAP_FORMAT_APM 0xcdd7
1109 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1110 void **output, unsigned int *output_size)
1112 int num_palette_entries;
1113 BITMAPFILEHEADER *bmp_file_hdr;
1114 BITMAPINFO *bmp_info_hdr;
1116 if (bitmap_info->bmiHeader.biClrUsed) {
1117 num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
1118 if (num_palette_entries > 256) num_palette_entries = 256;
1119 } else {
1120 if (bitmap_info->bmiHeader.biBitCount <= 8)
1121 num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
1122 else
1123 num_palette_entries = 0;
1126 *output_size =
1127 sizeof(BITMAPFILEHEADER) +
1128 sizeof(BITMAPINFOHEADER) +
1129 num_palette_entries * sizeof(RGBQUAD) +
1130 bitmap_info->bmiHeader.biSizeImage;
1132 *output = GdipAlloc(*output_size);
1134 bmp_file_hdr = (BITMAPFILEHEADER*) *output;
1135 bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
1136 bmp_file_hdr->bfSize = *output_size;
1137 bmp_file_hdr->bfOffBits =
1138 sizeof(BITMAPFILEHEADER) +
1139 sizeof(BITMAPINFOHEADER) +
1140 num_palette_entries * sizeof (RGBQUAD);
1142 bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
1143 memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
1144 memcpy((unsigned char *)(*output) +
1145 sizeof(BITMAPFILEHEADER) +
1146 sizeof(BITMAPINFOHEADER) +
1147 num_palette_entries * sizeof(RGBQUAD),
1148 bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
1150 return Ok;
1153 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1154 void **output, unsigned int *output_size);
1156 typedef enum {
1157 BMP,
1158 NUM_ENCODERS_SUPPORTED
1159 } ImageFormat;
1161 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
1162 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
1163 encode_image_BMP,
1166 /*****************************************************************************
1167 * GdipSaveImageToStream [GDIPLUS.@]
1169 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
1170 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
1172 GpStatus stat;
1173 HRESULT hr;
1174 short type;
1175 HBITMAP hbmp;
1176 HBITMAP old_hbmp;
1177 HDC hdc;
1178 int bm_is_selected;
1179 BITMAPINFO bmp_info;
1180 LPVOID bmp_bits;
1181 encode_image_func* encode_image;
1182 LPVOID output;
1183 unsigned int output_size;
1184 unsigned int dummy;
1185 int i;
1187 old_hbmp = 0;
1188 output = NULL;
1189 output_size = 0;
1191 TRACE("%p %p %p %p\n", image, stream, clsid, params);
1193 if(!image || !stream)
1194 return InvalidParameter;
1196 if (!image->picture)
1197 return GenericError;
1199 hr = IPicture_get_Type(image->picture, &type);
1200 if (FAILED(hr) || type != PICTYPE_BITMAP)
1201 return GenericError;
1203 /* select correct encoder */
1204 encode_image = NULL;
1205 for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
1206 if (IsEqualCLSID(clsid, &codecs[i].Clsid))
1207 encode_image = encode_image_funcs[i];
1209 if (encode_image == NULL)
1210 return UnknownImageFormat;
1212 /* extract underlying hbitmap representation from the IPicture */
1213 hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
1214 if (FAILED(hr) || !hbmp)
1215 return GenericError;
1216 hr = IPicture_get_CurDC(image->picture, &hdc);
1217 if (FAILED(hr))
1218 return GenericError;
1219 bm_is_selected = (hdc != 0);
1220 if (!bm_is_selected) {
1221 hdc = CreateCompatibleDC(0);
1222 old_hbmp = SelectObject(hdc, hbmp);
1225 /* get bits from HBITMAP */
1226 bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
1227 bmp_info.bmiHeader.biBitCount = 0;
1228 GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
1230 bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
1232 if (bmp_bits)
1233 GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
1235 if (!bm_is_selected) {
1236 SelectObject(hdc, old_hbmp);
1237 DeleteDC(hdc);
1240 if (!bmp_bits)
1241 return OutOfMemory;
1243 stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
1244 if (stat == Ok)
1245 IStream_Write(stream, output, output_size, &dummy);
1247 GdipFree(output);
1248 GdipFree(bmp_bits);
1250 return stat;
1253 /*****************************************************************************
1254 * GdipSetImagePalette [GDIPLUS.@]
1256 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1257 GDIPCONST ColorPalette *palette)
1259 static int calls;
1261 if(!image || !palette)
1262 return InvalidParameter;
1264 if(!(calls++))
1265 FIXME("not implemented\n");
1267 return NotImplemented;
1270 /*************************************************************************
1271 * Encoders -
1272 * Structures that represent which formats we support for encoding.
1275 /* ImageCodecInfo creation routines taken from libgdiplus */
1276 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1277 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1278 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1279 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1280 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1281 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1283 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1285 { /* BMP */
1286 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1287 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1288 /* CodecName */ bmp_codecname,
1289 /* DllName */ NULL,
1290 /* FormatDescription */ bmp_format,
1291 /* FilenameExtension */ bmp_extension,
1292 /* MimeType */ bmp_mimetype,
1293 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1294 /* Version */ 1,
1295 /* SigCount */ 1,
1296 /* SigSize */ 2,
1297 /* SigPattern */ bmp_sig_pattern,
1298 /* SigMask */ bmp_sig_mask,
1302 /*****************************************************************************
1303 * GdipGetImageDecodersSize [GDIPLUS.@]
1305 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
1307 FIXME("%p %p stub!\n", numDecoders, size);
1309 if (!numDecoders || !size)
1310 return InvalidParameter;
1312 *numDecoders = 0;
1313 *size = 0;
1315 return Ok;
1318 /*****************************************************************************
1319 * GdipGetImageDecoders [GDIPLUS.@]
1321 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
1323 FIXME("%u %u %p stub!\n", numDecoders, size, decoders);
1325 if (!decoders)
1326 return GenericError;
1328 return NotImplemented;
1331 /*****************************************************************************
1332 * GdipGetImageEncodersSize [GDIPLUS.@]
1334 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1336 TRACE("%p %p\n", numEncoders, size);
1338 if (!numEncoders || !size)
1339 return InvalidParameter;
1341 *numEncoders = NUM_ENCODERS_SUPPORTED;
1342 *size = sizeof (codecs);
1344 return Ok;
1347 /*****************************************************************************
1348 * GdipGetImageEncoders [GDIPLUS.@]
1350 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1352 TRACE("%u %u %p\n", numEncoders, size, encoders);
1354 if (!encoders ||
1355 (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1356 (size != sizeof (codecs)))
1357 return GenericError;
1359 memcpy(encoders, codecs, sizeof (codecs));
1361 return Ok;
1364 /*****************************************************************************
1365 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
1367 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1369 BITMAP bm;
1370 GpStatus retval;
1371 PixelFormat format;
1373 TRACE("%p %p %p\n", hbm, hpal, bitmap);
1375 if(!hbm || !bitmap)
1376 return InvalidParameter;
1378 /* TODO: Support for device-dependent bitmaps */
1379 if(hpal){
1380 FIXME("no support for device-dependent bitmaps\n");
1381 return NotImplemented;
1384 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1385 return InvalidParameter;
1387 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1388 switch(bm.bmBitsPixel) {
1389 case 1:
1390 format = PixelFormat1bppIndexed;
1391 break;
1392 case 4:
1393 format = PixelFormat4bppIndexed;
1394 break;
1395 case 8:
1396 format = PixelFormat8bppIndexed;
1397 break;
1398 case 24:
1399 format = PixelFormat24bppRGB;
1400 break;
1401 case 32:
1402 format = PixelFormat32bppRGB;
1403 break;
1404 case 48:
1405 format = PixelFormat48bppRGB;
1406 break;
1407 default:
1408 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1409 return InvalidParameter;
1412 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes,
1413 format, bm.bmBits, bitmap);
1415 return retval;
1418 /*****************************************************************************
1419 * GdipSetEffectParameters [GDIPLUS.@]
1421 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1422 const VOID *params, const UINT size)
1424 static int calls;
1426 if(!(calls++))
1427 FIXME("not implemented\n");
1429 return NotImplemented;
1432 /*****************************************************************************
1433 * GdipGetImageFlags [GDIPLUS.@]
1435 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1437 TRACE("%p %p\n", image, flags);
1439 if(!image || !flags)
1440 return InvalidParameter;
1442 *flags = image->flags;
1444 return Ok;
1447 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
1449 TRACE("(%d, %p)\n", control, param);
1451 switch(control){
1452 case TestControlForceBilinear:
1453 if(param)
1454 FIXME("TestControlForceBilinear not handled\n");
1455 break;
1456 case TestControlNoICM:
1457 if(param)
1458 FIXME("TestControlNoICM not handled\n");
1459 break;
1460 case TestControlGetBuildNumber:
1461 *((DWORD*)param) = 3102;
1462 break;
1465 return Ok;
1468 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
1469 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
1470 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
1471 GpMetafile **metafile)
1473 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
1474 frameUnit, debugstr_w(desc), metafile);
1476 return NotImplemented;
1479 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
1480 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
1481 GDIPCONST WCHAR *desc, GpMetafile **metafile)
1483 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
1484 frameUnit, debugstr_w(desc), metafile);
1486 return NotImplemented;