Merge amd64 NDK from amd64 branch:
[reactos.git] / reactos / dll / win32 / 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 #define NONAMELESSUNION
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27
28 #define COBJMACROS
29 #include "objbase.h"
30 #include "olectl.h"
31 #include "ole2.h"
32
33 #include "initguid.h"
34 #include "wincodec.h"
35 #include "gdiplus.h"
36 #include "gdiplus_private.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
40
41 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
42
43 static INT ipicture_pixel_height(IPicture *pic)
44 {
45 HDC hdcref;
46 OLE_YSIZE_HIMETRIC y;
47
48 IPicture_get_Height(pic, &y);
49
50 hdcref = GetDC(0);
51
52 y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
53 ReleaseDC(0, hdcref);
54
55 return y;
56 }
57
58 static INT ipicture_pixel_width(IPicture *pic)
59 {
60 HDC hdcref;
61 OLE_XSIZE_HIMETRIC x;
62
63 IPicture_get_Width(pic, &x);
64
65 hdcref = GetDC(0);
66
67 x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
68
69 ReleaseDC(0, hdcref);
70
71 return x;
72 }
73
74 GpStatus WINGDIPAPI GdipBitmapApplyEffect(GpBitmap* bitmap, CGpEffect* effect,
75 RECT* roi, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
76 {
77 FIXME("(%p %p %p %d %p %p): stub\n", bitmap, effect, roi, useAuxData, auxData, auxDataSize);
78 /*
79 * Note: According to Jose Roca's GDI+ docs, this function is not
80 * implemented in Windows's GDI+.
81 */
82 return NotImplemented;
83 }
84
85 GpStatus WINGDIPAPI GdipBitmapCreateApplyEffect(GpBitmap** inputBitmaps,
86 INT numInputs, CGpEffect* effect, RECT* roi, RECT* outputRect,
87 GpBitmap** outputBitmap, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
88 {
89 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps, numInputs, effect, roi, outputRect, outputBitmap, useAuxData, auxData, auxDataSize);
90 /*
91 * Note: According to Jose Roca's GDI+ docs, this function is not
92 * implemented in Windows's GDI+.
93 */
94 return NotImplemented;
95 }
96
97 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
98 ARGB *color)
99 {
100 static int calls;
101 TRACE("%p %d %d %p\n", bitmap, x, y, color);
102
103 if(!bitmap || !color)
104 return InvalidParameter;
105
106 if(!(calls++))
107 FIXME("not implemented\n");
108
109 *color = 0xdeadbeef;
110
111 return NotImplemented;
112 }
113
114 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
115 ARGB color)
116 {
117 static int calls;
118 TRACE("bitmap:%p, x:%d, y:%d, color:%08x\n", bitmap, x, y, color);
119
120 if(!bitmap)
121 return InvalidParameter;
122
123 if(!(calls++))
124 FIXME("not implemented\n");
125
126 return NotImplemented;
127 }
128
129 /* This function returns a pointer to an array of pixels that represents the
130 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
131 * flags. It is correct behavior that a user who calls this function with write
132 * privileges can write to the whole bitmap (not just the area in rect).
133 *
134 * FIXME: only used portion of format is bits per pixel. */
135 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
136 UINT flags, PixelFormat format, BitmapData* lockeddata)
137 {
138 BOOL bm_is_selected;
139 INT stride, bitspp = PIXELFORMATBPP(format);
140 HDC hdc;
141 HBITMAP hbm, old = NULL;
142 BITMAPINFO *pbmi;
143 BYTE *buff = NULL;
144 UINT abs_height;
145 GpRect act_rect; /* actual rect to be used */
146
147 TRACE("%p %p %d %d %p\n", bitmap, rect, flags, format, lockeddata);
148
149 if(!lockeddata || !bitmap)
150 return InvalidParameter;
151
152 if(rect){
153 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
154 (rect->Y + rect->Height > bitmap->height) || !flags)
155 return InvalidParameter;
156
157 act_rect = *rect;
158 }
159 else{
160 act_rect.X = act_rect.Y = 0;
161 act_rect.Width = bitmap->width;
162 act_rect.Height = bitmap->height;
163 }
164
165 if(flags & ImageLockModeUserInputBuf)
166 return NotImplemented;
167
168 if(bitmap->lockmode)
169 return WrongState;
170
171 if (bitmap->bits && bitmap->format == format)
172 {
173 /* no conversion is necessary; just use the bits directly */
174 lockeddata->Width = act_rect.Width;
175 lockeddata->Height = act_rect.Height;
176 lockeddata->PixelFormat = format;
177 lockeddata->Reserved = flags;
178 lockeddata->Stride = bitmap->stride;
179 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
180 bitmap->stride * act_rect.Y;
181
182 bitmap->lockmode = flags;
183 bitmap->numlocks++;
184
185 return Ok;
186 }
187
188 hbm = bitmap->hbitmap;
189 hdc = bitmap->hdc;
190 bm_is_selected = (hdc != 0);
191
192 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
193 if (!pbmi)
194 return OutOfMemory;
195 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
196 pbmi->bmiHeader.biBitCount = 0;
197
198 if(!bm_is_selected){
199 hdc = CreateCompatibleDC(0);
200 old = SelectObject(hdc, hbm);
201 }
202
203 /* fill out bmi */
204 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
205
206 abs_height = abs(pbmi->bmiHeader.biHeight);
207 stride = pbmi->bmiHeader.biWidth * bitspp / 8;
208 stride = (stride + 3) & ~3;
209
210 buff = GdipAlloc(stride * abs_height);
211
212 pbmi->bmiHeader.biBitCount = bitspp;
213
214 if(buff)
215 GetDIBits(hdc, hbm, 0, abs_height, buff, pbmi, DIB_RGB_COLORS);
216
217 if(!bm_is_selected){
218 SelectObject(hdc, old);
219 DeleteDC(hdc);
220 }
221
222 if(!buff){
223 GdipFree(pbmi);
224 return OutOfMemory;
225 }
226
227 lockeddata->Width = act_rect.Width;
228 lockeddata->Height = act_rect.Height;
229 lockeddata->PixelFormat = format;
230 lockeddata->Reserved = flags;
231
232 if(pbmi->bmiHeader.biHeight > 0){
233 lockeddata->Stride = -stride;
234 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X +
235 stride * (abs_height - 1 - act_rect.Y);
236 }
237 else{
238 lockeddata->Stride = stride;
239 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X + stride * act_rect.Y;
240 }
241
242 bitmap->lockmode = flags;
243 bitmap->numlocks++;
244
245 bitmap->bitmapbits = buff;
246
247 GdipFree(pbmi);
248 return Ok;
249 }
250
251 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
252 {
253 FIXME("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
254
255 return NotImplemented;
256 }
257
258 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
259 BitmapData* lockeddata)
260 {
261 HDC hdc;
262 HBITMAP hbm, old = NULL;
263 BOOL bm_is_selected;
264 BITMAPINFO *pbmi;
265
266 if(!bitmap || !lockeddata)
267 return InvalidParameter;
268
269 if(!bitmap->lockmode)
270 return WrongState;
271
272 if(lockeddata->Reserved & ImageLockModeUserInputBuf)
273 return NotImplemented;
274
275 if(lockeddata->Reserved & ImageLockModeRead){
276 if(!(--bitmap->numlocks))
277 bitmap->lockmode = 0;
278
279 GdipFree(bitmap->bitmapbits);
280 bitmap->bitmapbits = NULL;
281 return Ok;
282 }
283
284 if (!bitmap->bitmapbits)
285 {
286 /* we passed a direct reference; no need to do anything */
287 bitmap->lockmode = 0;
288 return Ok;
289 }
290
291 hbm = bitmap->hbitmap;
292 hdc = bitmap->hdc;
293 bm_is_selected = (hdc != 0);
294
295 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
296 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
297 pbmi->bmiHeader.biBitCount = 0;
298
299 if(!bm_is_selected){
300 hdc = CreateCompatibleDC(0);
301 old = SelectObject(hdc, hbm);
302 }
303
304 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
305 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(lockeddata->PixelFormat);
306 SetDIBits(hdc, hbm, 0, abs(pbmi->bmiHeader.biHeight),
307 bitmap->bitmapbits, pbmi, DIB_RGB_COLORS);
308
309 if(!bm_is_selected){
310 SelectObject(hdc, old);
311 DeleteDC(hdc);
312 }
313
314 GdipFree(pbmi);
315 GdipFree(bitmap->bitmapbits);
316 bitmap->bitmapbits = NULL;
317 bitmap->lockmode = 0;
318
319 return Ok;
320 }
321
322 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
323 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
324 {
325 FIXME("(%f,%f,%f,%f,%i,%p,%p): stub\n", x, y, width, height, format, srcBitmap, dstBitmap);
326
327 return NotImplemented;
328 }
329
330 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
331 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
332 {
333 FIXME("(%i,%i,%i,%i,%i,%p,%p): stub\n", x, y, width, height, format, srcBitmap, dstBitmap);
334
335 return NotImplemented;
336 }
337
338 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
339 {
340 GpStatus stat = GenericError;
341
342 TRACE("%p, %p\n", image, cloneImage);
343
344 if (!image || !cloneImage)
345 return InvalidParameter;
346
347 if (image->picture)
348 {
349 IStream* stream;
350 HRESULT hr;
351 INT size;
352 LARGE_INTEGER move;
353
354 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
355 if (FAILED(hr))
356 return GenericError;
357
358 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
359 if(FAILED(hr))
360 {
361 WARN("Failed to save image on stream\n");
362 goto out;
363 }
364
365 /* Set seek pointer back to the beginning of the picture */
366 move.QuadPart = 0;
367 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
368 if (FAILED(hr))
369 goto out;
370
371 stat = GdipLoadImageFromStream(stream, cloneImage);
372 if (stat != Ok) WARN("Failed to load image from stream\n");
373
374 out:
375 IStream_Release(stream);
376 return stat;
377 }
378 else if (image->type == ImageTypeBitmap)
379 {
380 GpBitmap *bitmap = (GpBitmap*)image;
381 BitmapData lockeddata_src, lockeddata_dst;
382 int i;
383 UINT row_size;
384
385 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format,
386 &lockeddata_src);
387 if (stat != Ok) return stat;
388
389 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
390 0, lockeddata_src.PixelFormat, NULL, (GpBitmap**)cloneImage);
391 if (stat == Ok)
392 {
393 stat = GdipBitmapLockBits((GpBitmap*)*cloneImage, NULL, ImageLockModeWrite,
394 lockeddata_src.PixelFormat, &lockeddata_dst);
395
396 if (stat == Ok)
397 {
398 /* copy the image data */
399 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
400 for (i=0; i<lockeddata_src.Height; i++)
401 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
402 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
403 row_size);
404
405 GdipBitmapUnlockBits((GpBitmap*)*cloneImage, &lockeddata_dst);
406 }
407
408 GdipBitmapUnlockBits(bitmap, &lockeddata_src);
409 }
410
411 if (stat != Ok)
412 {
413 GdipDisposeImage(*cloneImage);
414 *cloneImage = NULL;
415 }
416 return stat;
417 }
418 else
419 {
420 ERR("GpImage with no IPicture or bitmap?!\n");
421 return NotImplemented;
422 }
423 }
424
425 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
426 GpBitmap **bitmap)
427 {
428 GpStatus stat;
429 IStream *stream;
430
431 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
432
433 if(!filename || !bitmap)
434 return InvalidParameter;
435
436 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
437
438 if(stat != Ok)
439 return stat;
440
441 stat = GdipCreateBitmapFromStream(stream, bitmap);
442
443 IStream_Release(stream);
444
445 return stat;
446 }
447
448 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
449 VOID *bits, GpBitmap **bitmap)
450 {
451 DWORD height, stride;
452 PixelFormat format;
453
454 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
455
456 height = abs(info->bmiHeader.biHeight);
457 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
458
459 if(info->bmiHeader.biHeight > 0) /* bottom-up */
460 {
461 bits = (BYTE*)bits + (height - 1) * stride;
462 stride = -stride;
463 }
464
465 switch(info->bmiHeader.biBitCount) {
466 case 1:
467 format = PixelFormat1bppIndexed;
468 break;
469 case 4:
470 format = PixelFormat4bppIndexed;
471 break;
472 case 8:
473 format = PixelFormat8bppIndexed;
474 break;
475 case 24:
476 format = PixelFormat24bppRGB;
477 break;
478 default:
479 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
480 *bitmap = NULL;
481 return InvalidParameter;
482 }
483
484 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
485 bits, bitmap);
486
487 }
488
489 /* FIXME: no icm */
490 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
491 GpBitmap **bitmap)
492 {
493 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
494
495 return GdipCreateBitmapFromFile(filename, bitmap);
496 }
497
498 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
499 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
500 {
501 HBITMAP hbm;
502 GpStatus stat = InvalidParameter;
503
504 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
505
506 if(!lpBitmapName || !bitmap)
507 return InvalidParameter;
508
509 /* load DIB */
510 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
511 LR_CREATEDIBSECTION);
512
513 if(hbm){
514 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
515 DeleteObject(hbm);
516 }
517
518 return stat;
519 }
520
521 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
522 HBITMAP* hbmReturn, ARGB background)
523 {
524 GpStatus stat;
525 HBITMAP result, oldbitmap;
526 UINT width, height;
527 HDC hdc;
528 GpGraphics *graphics;
529 BITMAPINFOHEADER bih;
530 void *bits;
531 TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
532
533 if (!bitmap || !hbmReturn) return InvalidParameter;
534
535 GdipGetImageWidth((GpImage*)bitmap, &width);
536 GdipGetImageHeight((GpImage*)bitmap, &height);
537
538 bih.biSize = sizeof(bih);
539 bih.biWidth = width;
540 bih.biHeight = height;
541 bih.biPlanes = 1;
542 bih.biBitCount = 32;
543 bih.biCompression = BI_RGB;
544 bih.biSizeImage = 0;
545 bih.biXPelsPerMeter = 0;
546 bih.biYPelsPerMeter = 0;
547 bih.biClrUsed = 0;
548 bih.biClrImportant = 0;
549
550 hdc = CreateCompatibleDC(NULL);
551 if (!hdc) return GenericError;
552
553 result = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS, &bits,
554 NULL, 0);
555
556 if (result)
557 {
558 oldbitmap = SelectObject(hdc, result);
559
560 stat = GdipCreateFromHDC(hdc, &graphics);
561 if (stat == Ok)
562 {
563 stat = GdipGraphicsClear(graphics, background);
564
565 if (stat == Ok)
566 stat = GdipDrawImage(graphics, (GpImage*)bitmap, 0, 0);
567
568 GdipDeleteGraphics(graphics);
569 }
570
571 SelectObject(hdc, oldbitmap);
572 }
573 else
574 stat = GenericError;
575
576 DeleteDC(hdc);
577
578 if (stat != Ok && result)
579 {
580 DeleteObject(result);
581 result = NULL;
582 }
583
584 *hbmReturn = result;
585
586 return stat;
587 }
588
589 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
590 GpMetafile* metafile, BOOL* succ, EmfType emfType,
591 const WCHAR* description, GpMetafile** out_metafile)
592 {
593 static int calls;
594
595 if(!ref || !metafile || !out_metafile)
596 return InvalidParameter;
597
598 *succ = FALSE;
599 *out_metafile = NULL;
600
601 if(!(calls++))
602 FIXME("not implemented\n");
603
604 return NotImplemented;
605 }
606
607 /* FIXME: this should create a bitmap in the given size with the attributes
608 * (resolution etc.) of the graphics object */
609 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
610 GpGraphics* target, GpBitmap** bitmap)
611 {
612 static int calls;
613 GpStatus ret;
614
615 if(!target || !bitmap)
616 return InvalidParameter;
617
618 if(!(calls++))
619 FIXME("hacked stub\n");
620
621 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
622 NULL, bitmap);
623
624 return ret;
625 }
626
627 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
628 {
629 GpStatus stat;
630 ICONINFO iinfo;
631 BITMAP bm;
632 int ret;
633 UINT width, height;
634 GpRect rect;
635 BitmapData lockeddata;
636 HDC screendc;
637 BOOL has_alpha;
638 int x, y;
639 BYTE *bits;
640 BITMAPINFOHEADER bih;
641 DWORD *src;
642 BYTE *dst_row;
643 DWORD *dst;
644
645 TRACE("%p, %p\n", hicon, bitmap);
646
647 if(!bitmap || !GetIconInfo(hicon, &iinfo))
648 {
649 DeleteObject(iinfo.hbmColor);
650 DeleteObject(iinfo.hbmMask);
651 return InvalidParameter;
652 }
653
654 /* get the size of the icon */
655 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
656 if (ret == 0) {
657 DeleteObject(iinfo.hbmColor);
658 DeleteObject(iinfo.hbmMask);
659 return GenericError;
660 }
661
662 width = bm.bmWidth;
663
664 if (iinfo.hbmColor)
665 height = abs(bm.bmHeight);
666 else /* combined bitmap + mask */
667 height = abs(bm.bmHeight) / 2;
668
669 bits = HeapAlloc(GetProcessHeap(), 0, 4*width*height);
670 if (!bits) {
671 DeleteObject(iinfo.hbmColor);
672 DeleteObject(iinfo.hbmMask);
673 return OutOfMemory;
674 }
675
676 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB, NULL, bitmap);
677 if (stat != Ok) {
678 DeleteObject(iinfo.hbmColor);
679 DeleteObject(iinfo.hbmMask);
680 HeapFree(GetProcessHeap(), 0, bits);
681 return stat;
682 }
683
684 rect.X = 0;
685 rect.Y = 0;
686 rect.Width = width;
687 rect.Height = height;
688
689 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
690 if (stat != Ok) {
691 DeleteObject(iinfo.hbmColor);
692 DeleteObject(iinfo.hbmMask);
693 HeapFree(GetProcessHeap(), 0, bits);
694 GdipDisposeImage((GpImage*)*bitmap);
695 return stat;
696 }
697
698 bih.biSize = sizeof(bih);
699 bih.biWidth = width;
700 bih.biHeight = -height;
701 bih.biPlanes = 1;
702 bih.biBitCount = 32;
703 bih.biCompression = BI_RGB;
704 bih.biSizeImage = 0;
705 bih.biXPelsPerMeter = 0;
706 bih.biYPelsPerMeter = 0;
707 bih.biClrUsed = 0;
708 bih.biClrImportant = 0;
709
710 screendc = GetDC(0);
711 if (iinfo.hbmColor)
712 {
713 GetDIBits(screendc, iinfo.hbmColor, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
714
715 if (bm.bmBitsPixel == 32)
716 {
717 has_alpha = FALSE;
718
719 /* If any pixel has a non-zero alpha, ignore hbmMask */
720 src = (DWORD*)bits;
721 for (x=0; x<width && !has_alpha; x++)
722 for (y=0; y<height && !has_alpha; y++)
723 if ((*src++ & 0xff000000) != 0)
724 has_alpha = TRUE;
725 }
726 else has_alpha = FALSE;
727 }
728 else
729 {
730 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
731 has_alpha = FALSE;
732 }
733
734 /* copy the image data to the Bitmap */
735 src = (DWORD*)bits;
736 dst_row = lockeddata.Scan0;
737 for (y=0; y<height; y++)
738 {
739 memcpy(dst_row, src, width*4);
740 src += width;
741 dst_row += lockeddata.Stride;
742 }
743
744 if (!has_alpha)
745 {
746 if (iinfo.hbmMask)
747 {
748 /* read alpha data from the mask */
749 if (iinfo.hbmColor)
750 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
751 else
752 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
753
754 src = (DWORD*)bits;
755 dst_row = lockeddata.Scan0;
756 for (y=0; y<height; y++)
757 {
758 dst = (DWORD*)dst_row;
759 for (x=0; x<height; x++)
760 {
761 DWORD src_value = *src++;
762 if (src_value)
763 *dst++ = 0;
764 else
765 *dst++ |= 0xff000000;
766 }
767 dst_row += lockeddata.Stride;
768 }
769 }
770 else
771 {
772 /* set constant alpha of 255 */
773 dst_row = bits;
774 for (y=0; y<height; y++)
775 {
776 dst = (DWORD*)dst_row;
777 for (x=0; x<height; x++)
778 *dst++ |= 0xff000000;
779 dst_row += lockeddata.Stride;
780 }
781 }
782 }
783
784 ReleaseDC(0, screendc);
785
786 DeleteObject(iinfo.hbmColor);
787 DeleteObject(iinfo.hbmMask);
788
789 GdipBitmapUnlockBits(*bitmap, &lockeddata);
790
791 HeapFree(GetProcessHeap(), 0, bits);
792
793 return Ok;
794 }
795
796 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
797 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
798 {
799 BITMAPINFOHEADER bmih;
800 HBITMAP hbitmap;
801 INT row_size, dib_stride;
802 HDC hdc;
803 BYTE *bits;
804 int i;
805
806 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
807
808 if (!bitmap) return InvalidParameter;
809
810 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
811 *bitmap = NULL;
812 return InvalidParameter;
813 }
814
815 if(scan0 && !stride)
816 return InvalidParameter;
817
818 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
819 dib_stride = (row_size + 3) & ~3;
820
821 if(stride == 0)
822 stride = dib_stride;
823
824 bmih.biSize = sizeof(BITMAPINFOHEADER);
825 bmih.biWidth = width;
826 bmih.biHeight = -height;
827 bmih.biPlanes = 1;
828 /* FIXME: use the rest of the data from format */
829 bmih.biBitCount = PIXELFORMATBPP(format);
830 bmih.biCompression = BI_RGB;
831 bmih.biSizeImage = 0;
832 bmih.biXPelsPerMeter = 0;
833 bmih.biYPelsPerMeter = 0;
834 bmih.biClrUsed = 0;
835 bmih.biClrImportant = 0;
836
837 hdc = CreateCompatibleDC(NULL);
838 if (!hdc) return GenericError;
839
840 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS, (void**)&bits,
841 NULL, 0);
842
843 DeleteDC(hdc);
844
845 if (!hbitmap) return GenericError;
846
847 /* copy bits to the dib if necessary */
848 /* FIXME: should reference the bits instead of copying them */
849 if (scan0)
850 for (i=0; i<height; i++)
851 memcpy(bits+i*dib_stride, scan0+i*stride, row_size);
852
853 *bitmap = GdipAlloc(sizeof(GpBitmap));
854 if(!*bitmap)
855 {
856 DeleteObject(hbitmap);
857 return OutOfMemory;
858 }
859
860 (*bitmap)->image.type = ImageTypeBitmap;
861 (*bitmap)->image.flags = ImageFlagsNone;
862 (*bitmap)->width = width;
863 (*bitmap)->height = height;
864 (*bitmap)->format = format;
865 (*bitmap)->image.picture = NULL;
866 (*bitmap)->hbitmap = hbitmap;
867 (*bitmap)->hdc = NULL;
868 (*bitmap)->bits = bits;
869 (*bitmap)->stride = dib_stride;
870
871 return Ok;
872 }
873
874 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
875 GpBitmap **bitmap)
876 {
877 GpStatus stat;
878
879 TRACE("%p %p\n", stream, bitmap);
880
881 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
882
883 if(stat != Ok)
884 return stat;
885
886 if((*bitmap)->image.type != ImageTypeBitmap){
887 GdipDisposeImage(&(*bitmap)->image);
888 *bitmap = NULL;
889 return GenericError; /* FIXME: what error to return? */
890 }
891
892 return Ok;
893 }
894
895 /* FIXME: no icm */
896 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
897 GpBitmap **bitmap)
898 {
899 TRACE("%p %p\n", stream, bitmap);
900
901 return GdipCreateBitmapFromStream(stream, bitmap);
902 }
903
904 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
905 GpCachedBitmap **cachedbmp)
906 {
907 GpStatus stat;
908
909 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
910
911 if(!bitmap || !graphics || !cachedbmp)
912 return InvalidParameter;
913
914 *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
915 if(!*cachedbmp)
916 return OutOfMemory;
917
918 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
919 if(stat != Ok){
920 GdipFree(*cachedbmp);
921 return stat;
922 }
923
924 return Ok;
925 }
926
927 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
928 {
929 FIXME("(%p, %p)\n", bitmap, hicon);
930
931 return NotImplemented;
932 }
933
934 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
935 {
936 TRACE("%p\n", cachedbmp);
937
938 if(!cachedbmp)
939 return InvalidParameter;
940
941 GdipDisposeImage(cachedbmp->image);
942 GdipFree(cachedbmp);
943
944 return Ok;
945 }
946
947 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
948 GpCachedBitmap *cachedbmp, INT x, INT y)
949 {
950 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
951
952 if(!graphics || !cachedbmp)
953 return InvalidParameter;
954
955 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
956 }
957
958 GpStatus WINGDIPAPI GdipEmfToWmfBits(HENHMETAFILE hemf, UINT cbData16,
959 LPBYTE pData16, INT iMapMode, INT eFlags)
960 {
961 FIXME("(%p, %d, %p, %d, %d): stub\n", hemf, cbData16, pData16, iMapMode, eFlags);
962 return NotImplemented;
963 }
964
965 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
966 {
967 TRACE("%p\n", image);
968
969 if(!image)
970 return InvalidParameter;
971
972 if (image->picture)
973 IPicture_Release(image->picture);
974 if (image->type == ImageTypeBitmap)
975 {
976 GdipFree(((GpBitmap*)image)->bitmapbits);
977 DeleteDC(((GpBitmap*)image)->hdc);
978 }
979 GdipFree(image);
980
981 return Ok;
982 }
983
984 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
985 {
986 if(!image || !item)
987 return InvalidParameter;
988
989 return NotImplemented;
990 }
991
992 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
993 GpUnit *srcUnit)
994 {
995 TRACE("%p %p %p\n", image, srcRect, srcUnit);
996
997 if(!image || !srcRect || !srcUnit)
998 return InvalidParameter;
999 if(image->type == ImageTypeMetafile){
1000 *srcRect = ((GpMetafile*)image)->bounds;
1001 *srcUnit = ((GpMetafile*)image)->unit;
1002 }
1003 else if(image->type == ImageTypeBitmap){
1004 srcRect->X = srcRect->Y = 0.0;
1005 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
1006 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
1007 *srcUnit = UnitPixel;
1008 }
1009 else{
1010 srcRect->X = srcRect->Y = 0.0;
1011 srcRect->Width = ipicture_pixel_width(image->picture);
1012 srcRect->Height = ipicture_pixel_height(image->picture);
1013 *srcUnit = UnitPixel;
1014 }
1015
1016 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
1017 srcRect->Width, srcRect->Height, *srcUnit);
1018
1019 return Ok;
1020 }
1021
1022 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
1023 REAL *height)
1024 {
1025 TRACE("%p %p %p\n", image, width, height);
1026
1027 if(!image || !height || !width)
1028 return InvalidParameter;
1029
1030 if(image->type == ImageTypeMetafile){
1031 HDC hdc = GetDC(0);
1032
1033 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
1034 ((GpMetafile*)image)->bounds.Height;
1035
1036 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
1037 ((GpMetafile*)image)->bounds.Width;
1038
1039 ReleaseDC(0, hdc);
1040 }
1041
1042 else if(image->type == ImageTypeBitmap){
1043 *height = ((GpBitmap*)image)->height;
1044 *width = ((GpBitmap*)image)->width;
1045 }
1046 else{
1047 *height = ipicture_pixel_height(image->picture);
1048 *width = ipicture_pixel_width(image->picture);
1049 }
1050
1051 TRACE("returning (%f, %f)\n", *height, *width);
1052 return Ok;
1053 }
1054
1055 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
1056 GpGraphics **graphics)
1057 {
1058 HDC hdc;
1059
1060 TRACE("%p %p\n", image, graphics);
1061
1062 if(!image || !graphics)
1063 return InvalidParameter;
1064
1065 if(image->type != ImageTypeBitmap){
1066 FIXME("not implemented for image type %d\n", image->type);
1067 return NotImplemented;
1068 }
1069
1070 hdc = ((GpBitmap*)image)->hdc;
1071
1072 if(!hdc){
1073 hdc = CreateCompatibleDC(0);
1074 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
1075 ((GpBitmap*)image)->hdc = hdc;
1076 }
1077
1078 return GdipCreateFromHDC(hdc, graphics);
1079 }
1080
1081 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
1082 {
1083 TRACE("%p %p\n", image, height);
1084
1085 if(!image || !height)
1086 return InvalidParameter;
1087
1088 if(image->type == ImageTypeMetafile){
1089 HDC hdc = GetDC(0);
1090
1091 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
1092 ((GpMetafile*)image)->bounds.Height);
1093
1094 ReleaseDC(0, hdc);
1095 }
1096 else if(image->type == ImageTypeBitmap)
1097 *height = ((GpBitmap*)image)->height;
1098 else
1099 *height = ipicture_pixel_height(image->picture);
1100
1101 TRACE("returning %d\n", *height);
1102
1103 return Ok;
1104 }
1105
1106 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
1107 {
1108 static int calls;
1109
1110 if(!image || !res)
1111 return InvalidParameter;
1112
1113 if(!(calls++))
1114 FIXME("not implemented\n");
1115
1116 return NotImplemented;
1117 }
1118
1119 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
1120 {
1121 FIXME("%p %p\n", image, size);
1122
1123 if(!image || !size)
1124 return InvalidParameter;
1125
1126 return NotImplemented;
1127 }
1128
1129 /* FIXME: test this function for non-bitmap types */
1130 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
1131 {
1132 TRACE("%p %p\n", image, format);
1133
1134 if(!image || !format)
1135 return InvalidParameter;
1136
1137 if(image->type != ImageTypeBitmap)
1138 *format = PixelFormat24bppRGB;
1139 else
1140 *format = ((GpBitmap*) image)->format;
1141
1142 return Ok;
1143 }
1144
1145 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
1146 {
1147 static int calls;
1148
1149 if(!image || !format)
1150 return InvalidParameter;
1151
1152 if(!(calls++))
1153 FIXME("stub\n");
1154
1155 /* FIXME: should be detected from embedded picture or stored separately */
1156 switch (image->type)
1157 {
1158 case ImageTypeBitmap: *format = ImageFormatBMP; break;
1159 case ImageTypeMetafile: *format = ImageFormatEMF; break;
1160 default:
1161 WARN("unknown type %u\n", image->type);
1162 *format = ImageFormatUndefined;
1163 }
1164 return Ok;
1165 }
1166
1167 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
1168 {
1169 TRACE("%p %p\n", image, type);
1170
1171 if(!image || !type)
1172 return InvalidParameter;
1173
1174 *type = image->type;
1175
1176 return Ok;
1177 }
1178
1179 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
1180 {
1181 static int calls;
1182
1183 if(!image || !res)
1184 return InvalidParameter;
1185
1186 if(!(calls++))
1187 FIXME("not implemented\n");
1188
1189 return NotImplemented;
1190 }
1191
1192 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
1193 {
1194 TRACE("%p %p\n", image, width);
1195
1196 if(!image || !width)
1197 return InvalidParameter;
1198
1199 if(image->type == ImageTypeMetafile){
1200 HDC hdc = GetDC(0);
1201
1202 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
1203 ((GpMetafile*)image)->bounds.Width);
1204
1205 ReleaseDC(0, hdc);
1206 }
1207 else if(image->type == ImageTypeBitmap)
1208 *width = ((GpBitmap*)image)->width;
1209 else
1210 *width = ipicture_pixel_width(image->picture);
1211
1212 TRACE("returning %d\n", *width);
1213
1214 return Ok;
1215 }
1216
1217 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
1218 MetafileHeader * header)
1219 {
1220 static int calls;
1221
1222 if(!metafile || !header)
1223 return InvalidParameter;
1224
1225 if(!(calls++))
1226 FIXME("not implemented\n");
1227
1228 return Ok;
1229 }
1230
1231 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
1232 UINT num, PropertyItem* items)
1233 {
1234 static int calls;
1235
1236 if(!(calls++))
1237 FIXME("not implemented\n");
1238
1239 return InvalidParameter;
1240 }
1241
1242 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
1243 {
1244 static int calls;
1245
1246 if(!(calls++))
1247 FIXME("not implemented\n");
1248
1249 return InvalidParameter;
1250 }
1251
1252 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
1253 {
1254 static int calls;
1255
1256 if(!(calls++))
1257 FIXME("not implemented\n");
1258
1259 return InvalidParameter;
1260 }
1261
1262 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
1263 PropertyItem* buffer)
1264 {
1265 static int calls;
1266
1267 if(!(calls++))
1268 FIXME("not implemented\n");
1269
1270 return InvalidParameter;
1271 }
1272
1273 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
1274 UINT* size)
1275 {
1276 static int calls;
1277
1278 TRACE("%p %x %p\n", image, pid, size);
1279
1280 if(!size || !image)
1281 return InvalidParameter;
1282
1283 if(!(calls++))
1284 FIXME("not implemented\n");
1285
1286 return NotImplemented;
1287 }
1288
1289 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
1290 {
1291 static int calls;
1292
1293 if(!(calls++))
1294 FIXME("not implemented\n");
1295
1296 return InvalidParameter;
1297 }
1298
1299 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
1300 GDIPCONST GUID* dimensionID, UINT* count)
1301 {
1302 static int calls;
1303
1304 if(!image || !dimensionID || !count)
1305 return InvalidParameter;
1306
1307 if(!(calls++))
1308 FIXME("not implemented\n");
1309
1310 return NotImplemented;
1311 }
1312
1313 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
1314 UINT* count)
1315 {
1316 if(!image || !count)
1317 return InvalidParameter;
1318
1319 *count = 1;
1320
1321 FIXME("stub\n");
1322
1323 return Ok;
1324 }
1325
1326 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
1327 GUID* dimensionIDs, UINT count)
1328 {
1329 static int calls;
1330
1331 if(!image || !dimensionIDs)
1332 return InvalidParameter;
1333
1334 if(!(calls++))
1335 FIXME("not implemented\n");
1336
1337 return Ok;
1338 }
1339
1340 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
1341 GDIPCONST GUID* dimensionID, UINT frameidx)
1342 {
1343 static int calls;
1344
1345 if(!image || !dimensionID)
1346 return InvalidParameter;
1347
1348 if(!(calls++))
1349 FIXME("not implemented\n");
1350
1351 return Ok;
1352 }
1353
1354 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
1355 GpImage **image)
1356 {
1357 GpStatus stat;
1358 IStream *stream;
1359
1360 TRACE("(%s) %p\n", debugstr_w(filename), image);
1361
1362 if (!filename || !image)
1363 return InvalidParameter;
1364
1365 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1366
1367 if (stat != Ok)
1368 return stat;
1369
1370 stat = GdipLoadImageFromStream(stream, image);
1371
1372 IStream_Release(stream);
1373
1374 return stat;
1375 }
1376
1377 /* FIXME: no icm handling */
1378 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
1379 {
1380 TRACE("(%s) %p\n", debugstr_w(filename), image);
1381
1382 return GdipLoadImageFromFile(filename, image);
1383 }
1384
1385 static const WICPixelFormatGUID *wic_pixel_formats[] = {
1386 &GUID_WICPixelFormat16bppBGR555,
1387 &GUID_WICPixelFormat24bppBGR,
1388 &GUID_WICPixelFormat32bppBGR,
1389 &GUID_WICPixelFormat32bppBGRA,
1390 &GUID_WICPixelFormat32bppPBGRA,
1391 NULL
1392 };
1393
1394 static const PixelFormat wic_gdip_formats[] = {
1395 PixelFormat16bppRGB555,
1396 PixelFormat24bppRGB,
1397 PixelFormat32bppRGB,
1398 PixelFormat32bppARGB,
1399 PixelFormat32bppPARGB,
1400 };
1401
1402 static GpStatus decode_image_wic(IStream* stream, REFCLSID clsid, GpImage **image)
1403 {
1404 GpStatus status=Ok;
1405 GpBitmap *bitmap;
1406 HRESULT hr;
1407 IWICBitmapDecoder *decoder;
1408 IWICBitmapFrameDecode *frame;
1409 IWICBitmapSource *source=NULL;
1410 WICPixelFormatGUID wic_format;
1411 PixelFormat gdip_format=0;
1412 int i;
1413 UINT width, height;
1414 BitmapData lockeddata;
1415 WICRect wrc;
1416 HRESULT initresult;
1417
1418 initresult = CoInitialize(NULL);
1419
1420 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
1421 &IID_IWICBitmapDecoder, (void**)&decoder);
1422 if (FAILED(hr)) goto end;
1423
1424 hr = IWICBitmapDecoder_Initialize(decoder, (IStream*)stream, WICDecodeMetadataCacheOnLoad);
1425 if (SUCCEEDED(hr))
1426 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
1427
1428 if (SUCCEEDED(hr)) /* got frame */
1429 {
1430 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
1431
1432 if (SUCCEEDED(hr))
1433 {
1434 for (i=0; wic_pixel_formats[i]; i++)
1435 {
1436 if (IsEqualGUID(&wic_format, wic_pixel_formats[i]))
1437 {
1438 source = (IWICBitmapSource*)frame;
1439 IWICBitmapSource_AddRef(source);
1440 gdip_format = wic_gdip_formats[i];
1441 break;
1442 }
1443 }
1444 if (!source)
1445 {
1446 /* unknown format; fall back on 32bppARGB */
1447 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
1448 gdip_format = PixelFormat32bppARGB;
1449 }
1450 }
1451
1452 if (SUCCEEDED(hr)) /* got source */
1453 {
1454 hr = IWICBitmapSource_GetSize(source, &width, &height);
1455
1456 if (SUCCEEDED(hr))
1457 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
1458 NULL, &bitmap);
1459
1460 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
1461 {
1462 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
1463 gdip_format, &lockeddata);
1464 if (status == Ok) /* locked bitmap */
1465 {
1466 wrc.X = 0;
1467 wrc.Width = width;
1468 wrc.Height = 1;
1469 for (i=0; i<height; i++)
1470 {
1471 wrc.Y = i;
1472 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
1473 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
1474 if (FAILED(hr)) break;
1475 }
1476
1477 GdipBitmapUnlockBits(bitmap, &lockeddata);
1478 }
1479
1480 if (SUCCEEDED(hr) && status == Ok)
1481 *image = (GpImage*)bitmap;
1482 else
1483 {
1484 *image = NULL;
1485 GdipDisposeImage((GpImage*)bitmap);
1486 }
1487 }
1488
1489 IWICBitmapSource_Release(source);
1490 }
1491
1492 IWICBitmapFrameDecode_Release(frame);
1493 }
1494
1495 IWICBitmapDecoder_Release(decoder);
1496
1497 end:
1498 if (SUCCEEDED(initresult)) CoUninitialize();
1499
1500 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
1501
1502 return status;
1503 }
1504
1505 static GpStatus decode_image_icon(IStream* stream, REFCLSID clsid, GpImage **image)
1506 {
1507 return decode_image_wic(stream, &CLSID_WICIcoDecoder, image);
1508 }
1509
1510 static GpStatus decode_image_jpeg(IStream* stream, REFCLSID clsid, GpImage **image)
1511 {
1512 return decode_image_wic(stream, &CLSID_WICJpegDecoder, image);
1513 }
1514
1515 static GpStatus decode_image_gif(IStream* stream, REFCLSID clsid, GpImage **image)
1516 {
1517 return decode_image_wic(stream, &CLSID_WICGifDecoder, image);
1518 }
1519
1520 static GpStatus decode_image_olepicture_bitmap(IStream* stream, REFCLSID clsid, GpImage **image)
1521 {
1522 IPicture *pic;
1523 BITMAPINFO *pbmi;
1524 BITMAPCOREHEADER* bmch;
1525 HBITMAP hbm;
1526 HDC hdc;
1527
1528 TRACE("%p %p\n", stream, image);
1529
1530 if(!stream || !image)
1531 return InvalidParameter;
1532
1533 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1534 (LPVOID*) &pic) != S_OK){
1535 TRACE("Could not load picture\n");
1536 return GenericError;
1537 }
1538
1539 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1540 if (!pbmi)
1541 return OutOfMemory;
1542 *image = GdipAlloc(sizeof(GpBitmap));
1543 if(!*image){
1544 GdipFree(pbmi);
1545 return OutOfMemory;
1546 }
1547 (*image)->type = ImageTypeBitmap;
1548
1549 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
1550 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
1551
1552 /* get the pixel format */
1553 IPicture_get_Handle(pic, (OLE_HANDLE*)&hbm);
1554 IPicture_get_CurDC(pic, &hdc);
1555
1556 (*((GpBitmap**) image))->hbitmap = hbm;
1557 (*((GpBitmap**) image))->hdc = hdc;
1558 (*((GpBitmap**) image))->bits = NULL;
1559
1560 bmch = (BITMAPCOREHEADER*) (&pbmi->bmiHeader);
1561 bmch->bcSize = sizeof(BITMAPCOREHEADER);
1562
1563 if(!hdc){
1564 HBITMAP old;
1565 hdc = CreateCompatibleDC(0);
1566 old = SelectObject(hdc, hbm);
1567 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
1568 SelectObject(hdc, old);
1569 DeleteDC(hdc);
1570 }
1571 else
1572 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
1573
1574 switch(bmch->bcBitCount)
1575 {
1576 case 1:
1577 (*((GpBitmap**) image))->format = PixelFormat1bppIndexed;
1578 break;
1579 case 4:
1580 (*((GpBitmap**) image))->format = PixelFormat4bppIndexed;
1581 break;
1582 case 8:
1583 (*((GpBitmap**) image))->format = PixelFormat8bppIndexed;
1584 break;
1585 case 16:
1586 (*((GpBitmap**) image))->format = PixelFormat16bppRGB565;
1587 break;
1588 case 24:
1589 (*((GpBitmap**) image))->format = PixelFormat24bppRGB;
1590 break;
1591 case 32:
1592 (*((GpBitmap**) image))->format = PixelFormat32bppRGB;
1593 break;
1594 case 48:
1595 (*((GpBitmap**) image))->format = PixelFormat48bppRGB;
1596 break;
1597 default:
1598 FIXME("Bit depth %d is not fully supported yet\n", bmch->bcBitCount);
1599 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
1600 break;
1601 }
1602
1603 GdipFree(pbmi);
1604
1605 (*image)->picture = pic;
1606 (*image)->flags = ImageFlagsNone;
1607
1608 return Ok;
1609 }
1610
1611 static GpStatus decode_image_olepicture_metafile(IStream* stream, REFCLSID clsid, GpImage **image)
1612 {
1613 IPicture *pic;
1614
1615 TRACE("%p %p\n", stream, image);
1616
1617 if(!stream || !image)
1618 return InvalidParameter;
1619
1620 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1621 (LPVOID*) &pic) != S_OK){
1622 TRACE("Could not load picture\n");
1623 return GenericError;
1624 }
1625
1626 /* FIXME: missing initialization code */
1627 *image = GdipAlloc(sizeof(GpMetafile));
1628 if(!*image) return OutOfMemory;
1629 (*image)->type = ImageTypeMetafile;
1630 (*image)->picture = pic;
1631 (*image)->flags = ImageFlagsNone;
1632
1633 return Ok;
1634 }
1635
1636 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
1637 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
1638
1639 typedef GpStatus (*decode_image_func)(IStream *stream, REFCLSID clsid, GpImage** image);
1640
1641 typedef struct image_codec {
1642 ImageCodecInfo info;
1643 encode_image_func encode_func;
1644 decode_image_func decode_func;
1645 } image_codec;
1646
1647 typedef enum {
1648 BMP,
1649 JPEG,
1650 GIF,
1651 EMF,
1652 WMF,
1653 PNG,
1654 ICO,
1655 NUM_CODECS
1656 } ImageFormat;
1657
1658 static const struct image_codec codecs[NUM_CODECS];
1659
1660 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
1661 {
1662 BYTE signature[8];
1663 LARGE_INTEGER seek;
1664 HRESULT hr;
1665 UINT bytesread;
1666 int i, j;
1667
1668 /* seek to the start of the stream */
1669 seek.QuadPart = 0;
1670 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
1671 if (FAILED(hr)) return hresult_to_status(hr);
1672
1673 /* read the first 8 bytes */
1674 /* FIXME: This assumes all codecs have one signature <= 8 bytes in length */
1675 hr = IStream_Read(stream, signature, 8, &bytesread);
1676 if (FAILED(hr)) return hresult_to_status(hr);
1677 if (hr == S_FALSE || bytesread == 0) return GenericError;
1678
1679 for (i = 0; i < NUM_CODECS; i++) {
1680 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
1681 bytesread >= codecs[i].info.SigSize)
1682 {
1683 for (j=0; j<codecs[i].info.SigSize; j++)
1684 if ((signature[j] & codecs[i].info.SigMask[j]) != codecs[i].info.SigPattern[j])
1685 break;
1686 if (j == codecs[i].info.SigSize)
1687 {
1688 *result = &codecs[i];
1689 return Ok;
1690 }
1691 }
1692 }
1693
1694 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
1695 signature[0],signature[1],signature[2],signature[3],
1696 signature[4],signature[5],signature[6],signature[7]);
1697
1698 return GenericError;
1699 }
1700
1701 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
1702 {
1703 GpStatus stat;
1704 LARGE_INTEGER seek;
1705 HRESULT hr;
1706 const struct image_codec *codec=NULL;
1707
1708 /* choose an appropriate image decoder */
1709 stat = get_decoder_info(stream, &codec);
1710 if (stat != Ok) return stat;
1711
1712 /* seek to the start of the stream */
1713 seek.QuadPart = 0;
1714 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
1715 if (FAILED(hr)) return hresult_to_status(hr);
1716
1717 /* call on the image decoder to do the real work */
1718 return codec->decode_func(stream, &codec->info.Clsid, image);
1719 }
1720
1721 /* FIXME: no ICM */
1722 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
1723 {
1724 TRACE("%p %p\n", stream, image);
1725
1726 return GdipLoadImageFromStream(stream, image);
1727 }
1728
1729 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
1730 {
1731 static int calls;
1732
1733 if(!image)
1734 return InvalidParameter;
1735
1736 if(!(calls++))
1737 FIXME("not implemented\n");
1738
1739 return NotImplemented;
1740 }
1741
1742 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
1743 {
1744 static int calls;
1745
1746 if(!(calls++))
1747 FIXME("not implemented\n");
1748
1749 return NotImplemented;
1750 }
1751
1752 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
1753 GDIPCONST CLSID *clsidEncoder,
1754 GDIPCONST EncoderParameters *encoderParams)
1755 {
1756 GpStatus stat;
1757 IStream *stream;
1758
1759 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
1760
1761 if (!image || !filename|| !clsidEncoder)
1762 return InvalidParameter;
1763
1764 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
1765 if (stat != Ok)
1766 return GenericError;
1767
1768 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
1769
1770 IStream_Release(stream);
1771 return stat;
1772 }
1773
1774 /*************************************************************************
1775 * Encoding functions -
1776 * These functions encode an image in different image file formats.
1777 */
1778 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
1779 #define BITMAP_FORMAT_JPEG 0xd8ff
1780 #define BITMAP_FORMAT_GIF 0x4947
1781 #define BITMAP_FORMAT_PNG 0x5089
1782 #define BITMAP_FORMAT_APM 0xcdd7
1783
1784 static GpStatus encode_image_WIC(GpImage *image, IStream* stream,
1785 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
1786 {
1787 GpStatus stat;
1788 GpBitmap *bitmap;
1789 IWICBitmapEncoder *encoder;
1790 IWICBitmapFrameEncode *frameencode;
1791 IPropertyBag2 *encoderoptions;
1792 HRESULT hr;
1793 UINT width, height;
1794 PixelFormat gdipformat=0;
1795 WICPixelFormatGUID wicformat;
1796 GpRect rc;
1797 BitmapData lockeddata;
1798 HRESULT initresult;
1799 UINT i;
1800
1801 if (image->type != ImageTypeBitmap)
1802 return GenericError;
1803
1804 bitmap = (GpBitmap*)image;
1805
1806 GdipGetImageWidth(image, &width);
1807 GdipGetImageHeight(image, &height);
1808
1809 rc.X = 0;
1810 rc.Y = 0;
1811 rc.Width = width;
1812 rc.Height = height;
1813
1814 initresult = CoInitialize(NULL);
1815
1816 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
1817 &IID_IWICBitmapEncoder, (void**)&encoder);
1818 if (FAILED(hr))
1819 {
1820 if (SUCCEEDED(initresult)) CoUninitialize();
1821 return hresult_to_status(hr);
1822 }
1823
1824 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
1825
1826 if (SUCCEEDED(hr))
1827 {
1828 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
1829 }
1830
1831 if (SUCCEEDED(hr)) /* created frame */
1832 {
1833 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
1834
1835 if (SUCCEEDED(hr))
1836 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
1837
1838 if (SUCCEEDED(hr))
1839 /* FIXME: use the resolution from the image */
1840 hr = IWICBitmapFrameEncode_SetResolution(frameencode, 96.0, 96.0);
1841
1842 if (SUCCEEDED(hr))
1843 {
1844 for (i=0; wic_pixel_formats[i]; i++)
1845 {
1846 if (wic_gdip_formats[i] == bitmap->format)
1847 break;
1848 }
1849 if (wic_pixel_formats[i])
1850 memcpy(&wicformat, wic_pixel_formats[i], sizeof(GUID));
1851 else
1852 memcpy(&wicformat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
1853
1854 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
1855
1856 for (i=0; wic_pixel_formats[i]; i++)
1857 {
1858 if (IsEqualGUID(&wicformat, wic_pixel_formats[i]))
1859 break;
1860 }
1861 if (wic_pixel_formats[i])
1862 gdipformat = wic_gdip_formats[i];
1863 else
1864 {
1865 ERR("cannot provide pixel format %s\n", debugstr_guid(&wicformat));
1866 hr = E_FAIL;
1867 }
1868 }
1869
1870 if (SUCCEEDED(hr))
1871 {
1872 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
1873 &lockeddata);
1874
1875 if (stat == Ok)
1876 {
1877 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
1878 BYTE *row;
1879
1880 /* write one row at a time in case stride is negative */
1881 row = lockeddata.Scan0;
1882 for (i=0; i<lockeddata.Height; i++)
1883 {
1884 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
1885 if (FAILED(hr)) break;
1886 row += lockeddata.Stride;
1887 }
1888
1889 GdipBitmapUnlockBits(bitmap, &lockeddata);
1890 }
1891 else
1892 hr = E_FAIL;
1893 }
1894
1895 if (SUCCEEDED(hr))
1896 hr = IWICBitmapFrameEncode_Commit(frameencode);
1897
1898 IWICBitmapFrameEncode_Release(frameencode);
1899 IPropertyBag2_Release(encoderoptions);
1900 }
1901
1902 if (SUCCEEDED(hr))
1903 hr = IWICBitmapEncoder_Commit(encoder);
1904
1905 IWICBitmapEncoder_Release(encoder);
1906
1907 if (SUCCEEDED(initresult)) CoUninitialize();
1908
1909 return hresult_to_status(hr);
1910 }
1911
1912 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
1913 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
1914 {
1915 return encode_image_WIC(image, stream, &CLSID_WICBmpEncoder, params);
1916 }
1917
1918 /*****************************************************************************
1919 * GdipSaveImageToStream [GDIPLUS.@]
1920 */
1921 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
1922 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
1923 {
1924 GpStatus stat;
1925 encode_image_func encode_image;
1926 int i;
1927
1928 TRACE("%p %p %p %p\n", image, stream, clsid, params);
1929
1930 if(!image || !stream)
1931 return InvalidParameter;
1932
1933 /* select correct encoder */
1934 encode_image = NULL;
1935 for (i = 0; i < NUM_CODECS; i++) {
1936 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
1937 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
1938 encode_image = codecs[i].encode_func;
1939 }
1940 if (encode_image == NULL)
1941 return UnknownImageFormat;
1942
1943 stat = encode_image(image, stream, clsid, params);
1944
1945 return stat;
1946 }
1947
1948 /*****************************************************************************
1949 * GdipGetImagePalette [GDIPLUS.@]
1950 */
1951 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
1952 {
1953 static int calls = 0;
1954
1955 if(!image)
1956 return InvalidParameter;
1957
1958 if(!(calls++))
1959 FIXME("not implemented\n");
1960
1961 return NotImplemented;
1962 }
1963
1964 /*****************************************************************************
1965 * GdipSetImagePalette [GDIPLUS.@]
1966 */
1967 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1968 GDIPCONST ColorPalette *palette)
1969 {
1970 static int calls;
1971
1972 if(!image || !palette)
1973 return InvalidParameter;
1974
1975 if(!(calls++))
1976 FIXME("not implemented\n");
1977
1978 return NotImplemented;
1979 }
1980
1981 /*************************************************************************
1982 * Encoders -
1983 * Structures that represent which formats we support for encoding.
1984 */
1985
1986 /* ImageCodecInfo creation routines taken from libgdiplus */
1987 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1988 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1989 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1990 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1991 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1992 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1993
1994 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
1995 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
1996 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
1997 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
1998 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
1999 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
2000
2001 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
2002 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
2003 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
2004 static const WCHAR gif_format[] = {'G','I','F',0};
2005 static const BYTE gif_sig_pattern[4] = "GIF8";
2006 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2007
2008 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
2009 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
2010 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
2011 static const WCHAR emf_format[] = {'E','M','F',0};
2012 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
2013 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2014
2015 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
2016 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
2017 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
2018 static const WCHAR wmf_format[] = {'W','M','F',0};
2019 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
2020 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
2021
2022 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
2023 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
2024 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
2025 static const WCHAR png_format[] = {'P','N','G',0};
2026 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
2027 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
2028
2029 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
2030 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
2031 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
2032 static const WCHAR ico_format[] = {'I','C','O',0};
2033 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
2034 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2035
2036 static const struct image_codec codecs[NUM_CODECS] = {
2037 {
2038 { /* BMP */
2039 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2040 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2041 /* CodecName */ bmp_codecname,
2042 /* DllName */ NULL,
2043 /* FormatDescription */ bmp_format,
2044 /* FilenameExtension */ bmp_extension,
2045 /* MimeType */ bmp_mimetype,
2046 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2047 /* Version */ 1,
2048 /* SigCount */ 1,
2049 /* SigSize */ 2,
2050 /* SigPattern */ bmp_sig_pattern,
2051 /* SigMask */ bmp_sig_mask,
2052 },
2053 encode_image_BMP,
2054 decode_image_olepicture_bitmap
2055 },
2056 {
2057 { /* JPEG */
2058 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2059 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2060 /* CodecName */ jpeg_codecname,
2061 /* DllName */ NULL,
2062 /* FormatDescription */ jpeg_format,
2063 /* FilenameExtension */ jpeg_extension,
2064 /* MimeType */ jpeg_mimetype,
2065 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2066 /* Version */ 1,
2067 /* SigCount */ 1,
2068 /* SigSize */ 2,
2069 /* SigPattern */ jpeg_sig_pattern,
2070 /* SigMask */ jpeg_sig_mask,
2071 },
2072 NULL,
2073 decode_image_jpeg
2074 },
2075 {
2076 { /* GIF */
2077 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2078 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2079 /* CodecName */ gif_codecname,
2080 /* DllName */ NULL,
2081 /* FormatDescription */ gif_format,
2082 /* FilenameExtension */ gif_extension,
2083 /* MimeType */ gif_mimetype,
2084 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2085 /* Version */ 1,
2086 /* SigCount */ 1,
2087 /* SigSize */ 4,
2088 /* SigPattern */ gif_sig_pattern,
2089 /* SigMask */ gif_sig_mask,
2090 },
2091 NULL,
2092 decode_image_gif
2093 },
2094 {
2095 { /* EMF */
2096 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2097 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2098 /* CodecName */ emf_codecname,
2099 /* DllName */ NULL,
2100 /* FormatDescription */ emf_format,
2101 /* FilenameExtension */ emf_extension,
2102 /* MimeType */ emf_mimetype,
2103 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
2104 /* Version */ 1,
2105 /* SigCount */ 1,
2106 /* SigSize */ 4,
2107 /* SigPattern */ emf_sig_pattern,
2108 /* SigMask */ emf_sig_mask,
2109 },
2110 NULL,
2111 decode_image_olepicture_metafile
2112 },
2113 {
2114 { /* WMF */
2115 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2116 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2117 /* CodecName */ wmf_codecname,
2118 /* DllName */ NULL,
2119 /* FormatDescription */ wmf_format,
2120 /* FilenameExtension */ wmf_extension,
2121 /* MimeType */ wmf_mimetype,
2122 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
2123 /* Version */ 1,
2124 /* SigCount */ 1,
2125 /* SigSize */ 2,
2126 /* SigPattern */ wmf_sig_pattern,
2127 /* SigMask */ wmf_sig_mask,
2128 },
2129 NULL,
2130 decode_image_olepicture_metafile
2131 },
2132 {
2133 { /* PNG */
2134 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2135 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2136 /* CodecName */ png_codecname,
2137 /* DllName */ NULL,
2138 /* FormatDescription */ png_format,
2139 /* FilenameExtension */ png_extension,
2140 /* MimeType */ png_mimetype,
2141 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2142 /* Version */ 1,
2143 /* SigCount */ 1,
2144 /* SigSize */ 8,
2145 /* SigPattern */ png_sig_pattern,
2146 /* SigMask */ png_sig_mask,
2147 },
2148 NULL,
2149 decode_image_olepicture_bitmap
2150 },
2151 {
2152 { /* ICO */
2153 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2154 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2155 /* CodecName */ ico_codecname,
2156 /* DllName */ NULL,
2157 /* FormatDescription */ ico_format,
2158 /* FilenameExtension */ ico_extension,
2159 /* MimeType */ ico_mimetype,
2160 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2161 /* Version */ 1,
2162 /* SigCount */ 1,
2163 /* SigSize */ 4,
2164 /* SigPattern */ ico_sig_pattern,
2165 /* SigMask */ ico_sig_mask,
2166 },
2167 NULL,
2168 decode_image_icon
2169 },
2170 };
2171
2172 /*****************************************************************************
2173 * GdipGetImageDecodersSize [GDIPLUS.@]
2174 */
2175 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
2176 {
2177 int decoder_count=0;
2178 int i;
2179 TRACE("%p %p\n", numDecoders, size);
2180
2181 if (!numDecoders || !size)
2182 return InvalidParameter;
2183
2184 for (i=0; i<NUM_CODECS; i++)
2185 {
2186 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
2187 decoder_count++;
2188 }
2189
2190 *numDecoders = decoder_count;
2191 *size = decoder_count * sizeof(ImageCodecInfo);
2192
2193 return Ok;
2194 }
2195
2196 /*****************************************************************************
2197 * GdipGetImageDecoders [GDIPLUS.@]
2198 */
2199 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
2200 {
2201 int i, decoder_count=0;
2202 TRACE("%u %u %p\n", numDecoders, size, decoders);
2203
2204 if (!decoders ||
2205 size != numDecoders * sizeof(ImageCodecInfo))
2206 return GenericError;
2207
2208 for (i=0; i<NUM_CODECS; i++)
2209 {
2210 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
2211 {
2212 if (decoder_count == numDecoders) return GenericError;
2213 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
2214 decoder_count++;
2215 }
2216 }
2217
2218 if (decoder_count < numDecoders) return GenericError;
2219
2220 return Ok;
2221 }
2222
2223 /*****************************************************************************
2224 * GdipGetImageEncodersSize [GDIPLUS.@]
2225 */
2226 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
2227 {
2228 int encoder_count=0;
2229 int i;
2230 TRACE("%p %p\n", numEncoders, size);
2231
2232 if (!numEncoders || !size)
2233 return InvalidParameter;
2234
2235 for (i=0; i<NUM_CODECS; i++)
2236 {
2237 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
2238 encoder_count++;
2239 }
2240
2241 *numEncoders = encoder_count;
2242 *size = encoder_count * sizeof(ImageCodecInfo);
2243
2244 return Ok;
2245 }
2246
2247 /*****************************************************************************
2248 * GdipGetImageEncoders [GDIPLUS.@]
2249 */
2250 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
2251 {
2252 int i, encoder_count=0;
2253 TRACE("%u %u %p\n", numEncoders, size, encoders);
2254
2255 if (!encoders ||
2256 size != numEncoders * sizeof(ImageCodecInfo))
2257 return GenericError;
2258
2259 for (i=0; i<NUM_CODECS; i++)
2260 {
2261 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
2262 {
2263 if (encoder_count == numEncoders) return GenericError;
2264 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
2265 encoder_count++;
2266 }
2267 }
2268
2269 if (encoder_count < numEncoders) return GenericError;
2270
2271 return Ok;
2272 }
2273
2274 /*****************************************************************************
2275 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
2276 */
2277 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
2278 {
2279 BITMAP bm;
2280 GpStatus retval;
2281 PixelFormat format;
2282 BYTE* bits;
2283
2284 TRACE("%p %p %p\n", hbm, hpal, bitmap);
2285
2286 if(!hbm || !bitmap)
2287 return InvalidParameter;
2288
2289 /* TODO: Support for device-dependent bitmaps */
2290 if(hpal){
2291 FIXME("no support for device-dependent bitmaps\n");
2292 return NotImplemented;
2293 }
2294
2295 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
2296 return InvalidParameter;
2297
2298 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
2299 switch(bm.bmBitsPixel) {
2300 case 1:
2301 format = PixelFormat1bppIndexed;
2302 break;
2303 case 4:
2304 format = PixelFormat4bppIndexed;
2305 break;
2306 case 8:
2307 format = PixelFormat8bppIndexed;
2308 break;
2309 case 24:
2310 format = PixelFormat24bppRGB;
2311 break;
2312 case 32:
2313 format = PixelFormat32bppRGB;
2314 break;
2315 case 48:
2316 format = PixelFormat48bppRGB;
2317 break;
2318 default:
2319 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
2320 return InvalidParameter;
2321 }
2322
2323 if (bm.bmBits)
2324 bits = (BYTE*)bm.bmBits + (bm.bmHeight - 1) * bm.bmWidthBytes;
2325 else
2326 {
2327 FIXME("can only get image data from DIB sections\n");
2328 bits = NULL;
2329 }
2330
2331 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, -bm.bmWidthBytes,
2332 format, bits, bitmap);
2333
2334 return retval;
2335 }
2336
2337 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
2338 {
2339 FIXME("(%p): stub\n", effect);
2340 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
2341 * in Windows's gdiplus */
2342 return NotImplemented;
2343 }
2344
2345 /*****************************************************************************
2346 * GdipSetEffectParameters [GDIPLUS.@]
2347 */
2348 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
2349 const VOID *params, const UINT size)
2350 {
2351 static int calls;
2352
2353 if(!(calls++))
2354 FIXME("not implemented\n");
2355
2356 return NotImplemented;
2357 }
2358
2359 /*****************************************************************************
2360 * GdipGetImageFlags [GDIPLUS.@]
2361 */
2362 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
2363 {
2364 TRACE("%p %p\n", image, flags);
2365
2366 if(!image || !flags)
2367 return InvalidParameter;
2368
2369 *flags = image->flags;
2370
2371 return Ok;
2372 }
2373
2374 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
2375 {
2376 TRACE("(%d, %p)\n", control, param);
2377
2378 switch(control){
2379 case TestControlForceBilinear:
2380 if(param)
2381 FIXME("TestControlForceBilinear not handled\n");
2382 break;
2383 case TestControlNoICM:
2384 if(param)
2385 FIXME("TestControlNoICM not handled\n");
2386 break;
2387 case TestControlGetBuildNumber:
2388 *((DWORD*)param) = 3102;
2389 break;
2390 }
2391
2392 return Ok;
2393 }
2394
2395 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
2396 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
2397 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
2398 GpMetafile **metafile)
2399 {
2400 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
2401 frameUnit, debugstr_w(desc), metafile);
2402
2403 return NotImplemented;
2404 }
2405
2406 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
2407 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
2408 GDIPCONST WCHAR *desc, GpMetafile **metafile)
2409 {
2410 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
2411 frameUnit, debugstr_w(desc), metafile);
2412
2413 return NotImplemented;
2414 }
2415
2416 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
2417 {
2418 FIXME("%p\n", image);
2419
2420 return Ok;
2421 }
2422
2423 /*****************************************************************************
2424 * GdipGetImageThumbnail [GDIPLUS.@]
2425 */
2426 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
2427 GpImage **ret_image, GetThumbnailImageAbort cb,
2428 VOID * cb_data)
2429 {
2430 FIXME("(%p %u %u %p %p %p) stub\n",
2431 image, width, height, ret_image, cb, cb_data);
2432 return NotImplemented;
2433 }
2434
2435 /*****************************************************************************
2436 * GdipImageRotateFlip [GDIPLUS.@]
2437 */
2438 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
2439 {
2440 FIXME("(%p %u) stub\n", image, type);
2441 return NotImplemented;
2442 }