gdiplus: Implement GdipCloneImage for metafiles with a handle.
[wine/multimedia.git] / dlls / gdiplus / image.c
blob391830149a7ce0177b070b29e74c52b15febc15b
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
3 * Copyright (C) 2012 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdarg.h>
21 #include <assert.h>
23 #define NONAMELESSUNION
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "wingdi.h"
30 #define COBJMACROS
31 #include "objbase.h"
32 #include "olectl.h"
33 #include "ole2.h"
35 #include "initguid.h"
36 #include "wincodec.h"
37 #include "gdiplus.h"
38 #include "gdiplus_private.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
43 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
45 static const struct
47 const WICPixelFormatGUID *wic_format;
48 PixelFormat gdip_format;
49 /* predefined palette type to use for pixel format conversions */
50 WICBitmapPaletteType palette_type;
51 } pixel_formats[] =
53 { &GUID_WICPixelFormatBlackWhite, PixelFormat1bppIndexed, WICBitmapPaletteTypeFixedBW },
54 { &GUID_WICPixelFormat1bppIndexed, PixelFormat1bppIndexed, WICBitmapPaletteTypeFixedBW },
55 { &GUID_WICPixelFormat8bppGray, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedGray256 },
56 { &GUID_WICPixelFormat8bppIndexed, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedHalftone256 },
57 { &GUID_WICPixelFormat16bppBGR555, PixelFormat16bppRGB555, WICBitmapPaletteTypeFixedHalftone256 },
58 { &GUID_WICPixelFormat24bppBGR, PixelFormat24bppRGB, WICBitmapPaletteTypeFixedHalftone256 },
59 { &GUID_WICPixelFormat32bppBGR, PixelFormat32bppRGB, WICBitmapPaletteTypeFixedHalftone256 },
60 { &GUID_WICPixelFormat32bppBGRA, PixelFormat32bppARGB, WICBitmapPaletteTypeFixedHalftone256 },
61 { &GUID_WICPixelFormat32bppPBGRA, PixelFormat32bppPARGB, WICBitmapPaletteTypeFixedHalftone256 },
62 { NULL }
65 static ColorPalette *get_palette(IWICBitmapFrameDecode *frame, WICBitmapPaletteType palette_type)
67 HRESULT hr;
68 IWICImagingFactory *factory;
69 IWICPalette *wic_palette;
70 ColorPalette *palette = NULL;
72 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
73 &IID_IWICImagingFactory, (void **)&factory);
74 if (hr != S_OK) return NULL;
76 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
77 if (hr == S_OK)
79 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
80 if (frame)
81 hr = IWICBitmapFrameDecode_CopyPalette(frame, wic_palette);
82 if (hr != S_OK)
84 TRACE("using predefined palette %#x\n", palette_type);
85 hr = IWICPalette_InitializePredefined(wic_palette, palette_type, FALSE);
87 if (hr == S_OK)
89 UINT count;
90 BOOL mono, gray;
92 IWICPalette_IsBlackWhite(wic_palette, &mono);
93 IWICPalette_IsGrayscale(wic_palette, &gray);
95 IWICPalette_GetColorCount(wic_palette, &count);
96 palette = HeapAlloc(GetProcessHeap(), 0, 2 * sizeof(UINT) + count * sizeof(ARGB));
97 IWICPalette_GetColors(wic_palette, count, palette->Entries, &palette->Count);
99 if (mono)
100 palette->Flags = 0;
101 else if (gray)
102 palette->Flags = PaletteFlagsGrayScale;
103 else
104 palette->Flags = PaletteFlagsHalftone;
106 IWICPalette_Release(wic_palette);
108 IWICImagingFactory_Release(factory);
109 return palette;
112 static INT ipicture_pixel_height(IPicture *pic)
114 HDC hdcref;
115 OLE_YSIZE_HIMETRIC y;
117 IPicture_get_Height(pic, &y);
119 hdcref = GetDC(0);
121 y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
122 ReleaseDC(0, hdcref);
124 return y;
127 static INT ipicture_pixel_width(IPicture *pic)
129 HDC hdcref;
130 OLE_XSIZE_HIMETRIC x;
132 IPicture_get_Width(pic, &x);
134 hdcref = GetDC(0);
136 x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
138 ReleaseDC(0, hdcref);
140 return x;
143 GpStatus WINGDIPAPI GdipBitmapApplyEffect(GpBitmap* bitmap, CGpEffect* effect,
144 RECT* roi, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
146 FIXME("(%p %p %p %d %p %p): stub\n", bitmap, effect, roi, useAuxData, auxData, auxDataSize);
148 * Note: According to Jose Roca's GDI+ docs, this function is not
149 * implemented in Windows's GDI+.
151 return NotImplemented;
154 GpStatus WINGDIPAPI GdipBitmapCreateApplyEffect(GpBitmap** inputBitmaps,
155 INT numInputs, CGpEffect* effect, RECT* roi, RECT* outputRect,
156 GpBitmap** outputBitmap, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
158 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps, numInputs, effect, roi, outputRect, outputBitmap, useAuxData, auxData, auxDataSize);
160 * Note: According to Jose Roca's GDI+ docs, this function is not
161 * implemented in Windows's GDI+.
163 return NotImplemented;
166 static inline void getpixel_1bppIndexed(BYTE *index, const BYTE *row, UINT x)
168 *index = (row[x/8]>>(7-x%8)) & 1;
171 static inline void getpixel_4bppIndexed(BYTE *index, const BYTE *row, UINT x)
173 if (x & 1)
174 *index = row[x/2]&0xf;
175 else
176 *index = row[x/2]>>4;
179 static inline void getpixel_8bppIndexed(BYTE *index, const BYTE *row, UINT x)
181 *index = row[x];
184 static inline void getpixel_16bppGrayScale(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
185 const BYTE *row, UINT x)
187 *r = *g = *b = row[x*2+1];
188 *a = 255;
191 static inline void getpixel_16bppRGB555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
192 const BYTE *row, UINT x)
194 WORD pixel = *((const WORD*)(row)+x);
195 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
196 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
197 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
198 *a = 255;
201 static inline void getpixel_16bppRGB565(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
202 const BYTE *row, UINT x)
204 WORD pixel = *((const WORD*)(row)+x);
205 *r = (pixel>>8&0xf8)|(pixel>>13&0x7);
206 *g = (pixel>>3&0xfc)|(pixel>>9&0x3);
207 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
208 *a = 255;
211 static inline void getpixel_16bppARGB1555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
212 const BYTE *row, UINT x)
214 WORD pixel = *((const WORD*)(row)+x);
215 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
216 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
217 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
218 if ((pixel&0x8000) == 0x8000)
219 *a = 255;
220 else
221 *a = 0;
224 static inline void getpixel_24bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
225 const BYTE *row, UINT x)
227 *r = row[x*3+2];
228 *g = row[x*3+1];
229 *b = row[x*3];
230 *a = 255;
233 static inline void getpixel_32bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
234 const BYTE *row, UINT x)
236 *r = row[x*4+2];
237 *g = row[x*4+1];
238 *b = row[x*4];
239 *a = 255;
242 static inline void getpixel_32bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
243 const BYTE *row, UINT x)
245 *r = row[x*4+2];
246 *g = row[x*4+1];
247 *b = row[x*4];
248 *a = row[x*4+3];
251 static inline void getpixel_32bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
252 const BYTE *row, UINT x)
254 *a = row[x*4+3];
255 if (*a == 0)
256 *r = *g = *b = 0;
257 else
259 *r = row[x*4+2] * 255 / *a;
260 *g = row[x*4+1] * 255 / *a;
261 *b = row[x*4] * 255 / *a;
265 static inline void getpixel_48bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
266 const BYTE *row, UINT x)
268 *r = row[x*6+5];
269 *g = row[x*6+3];
270 *b = row[x*6+1];
271 *a = 255;
274 static inline void getpixel_64bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
275 const BYTE *row, UINT x)
277 *r = row[x*8+5];
278 *g = row[x*8+3];
279 *b = row[x*8+1];
280 *a = row[x*8+7];
283 static inline void getpixel_64bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
284 const BYTE *row, UINT x)
286 *a = row[x*8+7];
287 if (*a == 0)
288 *r = *g = *b = 0;
289 else
291 *r = row[x*8+5] * 255 / *a;
292 *g = row[x*8+3] * 255 / *a;
293 *b = row[x*8+1] * 255 / *a;
297 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
298 ARGB *color)
300 BYTE r, g, b, a;
301 BYTE index;
302 BYTE *row;
303 TRACE("%p %d %d %p\n", bitmap, x, y, color);
305 if(!bitmap || !color ||
306 x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
307 return InvalidParameter;
309 row = bitmap->bits+bitmap->stride*y;
311 switch (bitmap->format)
313 case PixelFormat1bppIndexed:
314 getpixel_1bppIndexed(&index,row,x);
315 break;
316 case PixelFormat4bppIndexed:
317 getpixel_4bppIndexed(&index,row,x);
318 break;
319 case PixelFormat8bppIndexed:
320 getpixel_8bppIndexed(&index,row,x);
321 break;
322 case PixelFormat16bppGrayScale:
323 getpixel_16bppGrayScale(&r,&g,&b,&a,row,x);
324 break;
325 case PixelFormat16bppRGB555:
326 getpixel_16bppRGB555(&r,&g,&b,&a,row,x);
327 break;
328 case PixelFormat16bppRGB565:
329 getpixel_16bppRGB565(&r,&g,&b,&a,row,x);
330 break;
331 case PixelFormat16bppARGB1555:
332 getpixel_16bppARGB1555(&r,&g,&b,&a,row,x);
333 break;
334 case PixelFormat24bppRGB:
335 getpixel_24bppRGB(&r,&g,&b,&a,row,x);
336 break;
337 case PixelFormat32bppRGB:
338 getpixel_32bppRGB(&r,&g,&b,&a,row,x);
339 break;
340 case PixelFormat32bppARGB:
341 getpixel_32bppARGB(&r,&g,&b,&a,row,x);
342 break;
343 case PixelFormat32bppPARGB:
344 getpixel_32bppPARGB(&r,&g,&b,&a,row,x);
345 break;
346 case PixelFormat48bppRGB:
347 getpixel_48bppRGB(&r,&g,&b,&a,row,x);
348 break;
349 case PixelFormat64bppARGB:
350 getpixel_64bppARGB(&r,&g,&b,&a,row,x);
351 break;
352 case PixelFormat64bppPARGB:
353 getpixel_64bppPARGB(&r,&g,&b,&a,row,x);
354 break;
355 default:
356 FIXME("not implemented for format 0x%x\n", bitmap->format);
357 return NotImplemented;
360 if (bitmap->format & PixelFormatIndexed)
361 *color = bitmap->image.palette->Entries[index];
362 else
363 *color = a<<24|r<<16|g<<8|b;
365 return Ok;
368 static inline UINT get_palette_index(BYTE r, BYTE g, BYTE b, BYTE a, ColorPalette *palette)
370 BYTE index = 0;
371 int best_distance = 0x7fff;
372 int distance;
373 int i;
375 if (!palette) return 0;
376 /* This algorithm scans entire palette,
377 computes difference from desired color (all color components have equal weight)
378 and returns the index of color with least difference.
380 Note: Maybe it could be replaced with a better algorithm for better image quality
381 and performance, though better algorithm would probably need some pre-built lookup
382 tables and thus may actually be slower if this method is called only few times per
383 every image.
385 for(i=0;i<palette->Count;i++) {
386 ARGB color=palette->Entries[i];
387 distance=abs(b-(color & 0xff)) + abs(g-(color>>8 & 0xff)) + abs(r-(color>>16 & 0xff)) + abs(a-(color>>24 & 0xff));
388 if (distance<best_distance) {
389 best_distance=distance;
390 index=i;
393 return index;
396 static inline void setpixel_8bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
397 BYTE *row, UINT x, ColorPalette *palette)
399 BYTE index = get_palette_index(r,g,b,a,palette);
400 row[x]=index;
403 static inline void setpixel_1bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
404 BYTE *row, UINT x, ColorPalette *palette)
406 row[x/8] = (row[x/8] & ~(1<<(7-x%8))) | (get_palette_index(r,g,b,a,palette)<<(7-x%8));
409 static inline void setpixel_4bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
410 BYTE *row, UINT x, ColorPalette *palette)
412 if (x & 1)
413 row[x/2] = (row[x/2] & 0xf0) | get_palette_index(r,g,b,a,palette);
414 else
415 row[x/2] = (row[x/2] & 0x0f) | get_palette_index(r,g,b,a,palette)<<4;
418 static inline void setpixel_16bppGrayScale(BYTE r, BYTE g, BYTE b, BYTE a,
419 BYTE *row, UINT x)
421 *((WORD*)(row)+x) = (r+g+b)*85;
424 static inline void setpixel_16bppRGB555(BYTE r, BYTE g, BYTE b, BYTE a,
425 BYTE *row, UINT x)
427 *((WORD*)(row)+x) = (r<<7&0x7c00)|
428 (g<<2&0x03e0)|
429 (b>>3&0x001f);
432 static inline void setpixel_16bppRGB565(BYTE r, BYTE g, BYTE b, BYTE a,
433 BYTE *row, UINT x)
435 *((WORD*)(row)+x) = (r<<8&0xf800)|
436 (g<<3&0x07e0)|
437 (b>>3&0x001f);
440 static inline void setpixel_16bppARGB1555(BYTE r, BYTE g, BYTE b, BYTE a,
441 BYTE *row, UINT x)
443 *((WORD*)(row)+x) = (a<<8&0x8000)|
444 (r<<7&0x7c00)|
445 (g<<2&0x03e0)|
446 (b>>3&0x001f);
449 static inline void setpixel_24bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
450 BYTE *row, UINT x)
452 row[x*3+2] = r;
453 row[x*3+1] = g;
454 row[x*3] = b;
457 static inline void setpixel_32bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
458 BYTE *row, UINT x)
460 *((DWORD*)(row)+x) = (r<<16)|(g<<8)|b;
463 static inline void setpixel_32bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
464 BYTE *row, UINT x)
466 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
469 static inline void setpixel_32bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
470 BYTE *row, UINT x)
472 r = r * a / 255;
473 g = g * a / 255;
474 b = b * a / 255;
475 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
478 static inline void setpixel_48bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
479 BYTE *row, UINT x)
481 row[x*6+5] = row[x*6+4] = r;
482 row[x*6+3] = row[x*6+2] = g;
483 row[x*6+1] = row[x*6] = b;
486 static inline void setpixel_64bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
487 BYTE *row, UINT x)
489 UINT64 a64=a, r64=r, g64=g, b64=b;
490 *((UINT64*)(row)+x) = (a64<<56)|(a64<<48)|(r64<<40)|(r64<<32)|(g64<<24)|(g64<<16)|(b64<<8)|b64;
493 static inline void setpixel_64bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
494 BYTE *row, UINT x)
496 UINT64 a64, r64, g64, b64;
497 a64 = a * 257;
498 r64 = r * a / 255;
499 g64 = g * a / 255;
500 b64 = b * a / 255;
501 *((UINT64*)(row)+x) = (a64<<48)|(r64<<32)|(g64<<16)|b64;
504 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
505 ARGB color)
507 BYTE a, r, g, b;
508 BYTE *row;
509 TRACE("bitmap:%p, x:%d, y:%d, color:%08x\n", bitmap, x, y, color);
511 if(!bitmap || x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
512 return InvalidParameter;
514 a = color>>24;
515 r = color>>16;
516 g = color>>8;
517 b = color;
519 row = bitmap->bits + bitmap->stride * y;
521 switch (bitmap->format)
523 case PixelFormat16bppGrayScale:
524 setpixel_16bppGrayScale(r,g,b,a,row,x);
525 break;
526 case PixelFormat16bppRGB555:
527 setpixel_16bppRGB555(r,g,b,a,row,x);
528 break;
529 case PixelFormat16bppRGB565:
530 setpixel_16bppRGB565(r,g,b,a,row,x);
531 break;
532 case PixelFormat16bppARGB1555:
533 setpixel_16bppARGB1555(r,g,b,a,row,x);
534 break;
535 case PixelFormat24bppRGB:
536 setpixel_24bppRGB(r,g,b,a,row,x);
537 break;
538 case PixelFormat32bppRGB:
539 setpixel_32bppRGB(r,g,b,a,row,x);
540 break;
541 case PixelFormat32bppARGB:
542 setpixel_32bppARGB(r,g,b,a,row,x);
543 break;
544 case PixelFormat32bppPARGB:
545 setpixel_32bppPARGB(r,g,b,a,row,x);
546 break;
547 case PixelFormat48bppRGB:
548 setpixel_48bppRGB(r,g,b,a,row,x);
549 break;
550 case PixelFormat64bppARGB:
551 setpixel_64bppARGB(r,g,b,a,row,x);
552 break;
553 case PixelFormat64bppPARGB:
554 setpixel_64bppPARGB(r,g,b,a,row,x);
555 break;
556 case PixelFormat8bppIndexed:
557 setpixel_8bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
558 break;
559 case PixelFormat4bppIndexed:
560 setpixel_4bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
561 break;
562 case PixelFormat1bppIndexed:
563 setpixel_1bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
564 break;
565 default:
566 FIXME("not implemented for format 0x%x\n", bitmap->format);
567 return NotImplemented;
570 return Ok;
573 GpStatus convert_pixels(INT width, INT height,
574 INT dst_stride, BYTE *dst_bits, PixelFormat dst_format,
575 INT src_stride, const BYTE *src_bits, PixelFormat src_format,
576 ColorPalette *palette)
578 INT x, y;
580 if (src_format == dst_format ||
581 (dst_format == PixelFormat32bppRGB && PIXELFORMATBPP(src_format) == 32))
583 UINT widthbytes = PIXELFORMATBPP(src_format) * width / 8;
584 for (y=0; y<height; y++)
585 memcpy(dst_bits+dst_stride*y, src_bits+src_stride*y, widthbytes);
586 return Ok;
589 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
590 for (x=0; x<width; x++) \
591 for (y=0; y<height; y++) { \
592 BYTE index; \
593 ARGB argb; \
594 BYTE *color = (BYTE *)&argb; \
595 getpixel_function(&index, src_bits+src_stride*y, x); \
596 argb = (palette && index < palette->Count) ? palette->Entries[index] : 0; \
597 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
599 return Ok; \
600 } while (0);
602 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
603 for (x=0; x<width; x++) \
604 for (y=0; y<height; y++) { \
605 BYTE r, g, b, a; \
606 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
607 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
609 return Ok; \
610 } while (0);
612 #define convert_rgb_to_indexed(getpixel_function, setpixel_function) do { \
613 for (x=0; x<width; x++) \
614 for (y=0; y<height; y++) { \
615 BYTE r, g, b, a; \
616 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
617 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x, palette); \
619 return Ok; \
620 } while (0);
622 switch (src_format)
624 case PixelFormat1bppIndexed:
625 switch (dst_format)
627 case PixelFormat16bppGrayScale:
628 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppGrayScale);
629 case PixelFormat16bppRGB555:
630 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB555);
631 case PixelFormat16bppRGB565:
632 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB565);
633 case PixelFormat16bppARGB1555:
634 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppARGB1555);
635 case PixelFormat24bppRGB:
636 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_24bppRGB);
637 case PixelFormat32bppRGB:
638 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppRGB);
639 case PixelFormat32bppARGB:
640 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppARGB);
641 case PixelFormat32bppPARGB:
642 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppPARGB);
643 case PixelFormat48bppRGB:
644 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_48bppRGB);
645 case PixelFormat64bppARGB:
646 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_64bppARGB);
647 default:
648 break;
650 break;
651 case PixelFormat4bppIndexed:
652 switch (dst_format)
654 case PixelFormat16bppGrayScale:
655 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppGrayScale);
656 case PixelFormat16bppRGB555:
657 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB555);
658 case PixelFormat16bppRGB565:
659 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB565);
660 case PixelFormat16bppARGB1555:
661 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppARGB1555);
662 case PixelFormat24bppRGB:
663 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_24bppRGB);
664 case PixelFormat32bppRGB:
665 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppRGB);
666 case PixelFormat32bppARGB:
667 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppARGB);
668 case PixelFormat32bppPARGB:
669 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppPARGB);
670 case PixelFormat48bppRGB:
671 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_48bppRGB);
672 case PixelFormat64bppARGB:
673 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_64bppARGB);
674 default:
675 break;
677 break;
678 case PixelFormat8bppIndexed:
679 switch (dst_format)
681 case PixelFormat16bppGrayScale:
682 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppGrayScale);
683 case PixelFormat16bppRGB555:
684 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB555);
685 case PixelFormat16bppRGB565:
686 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB565);
687 case PixelFormat16bppARGB1555:
688 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppARGB1555);
689 case PixelFormat24bppRGB:
690 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_24bppRGB);
691 case PixelFormat32bppRGB:
692 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppRGB);
693 case PixelFormat32bppARGB:
694 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppARGB);
695 case PixelFormat32bppPARGB:
696 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppPARGB);
697 case PixelFormat48bppRGB:
698 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_48bppRGB);
699 case PixelFormat64bppARGB:
700 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_64bppARGB);
701 default:
702 break;
704 break;
705 case PixelFormat16bppGrayScale:
706 switch (dst_format)
708 case PixelFormat1bppIndexed:
709 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_1bppIndexed);
710 case PixelFormat8bppIndexed:
711 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_8bppIndexed);
712 case PixelFormat16bppRGB555:
713 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB555);
714 case PixelFormat16bppRGB565:
715 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB565);
716 case PixelFormat16bppARGB1555:
717 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppARGB1555);
718 case PixelFormat24bppRGB:
719 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_24bppRGB);
720 case PixelFormat32bppRGB:
721 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppRGB);
722 case PixelFormat32bppARGB:
723 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppARGB);
724 case PixelFormat32bppPARGB:
725 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppPARGB);
726 case PixelFormat48bppRGB:
727 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_48bppRGB);
728 case PixelFormat64bppARGB:
729 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_64bppARGB);
730 default:
731 break;
733 break;
734 case PixelFormat16bppRGB555:
735 switch (dst_format)
737 case PixelFormat1bppIndexed:
738 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_1bppIndexed);
739 case PixelFormat8bppIndexed:
740 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_8bppIndexed);
741 case PixelFormat16bppGrayScale:
742 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppGrayScale);
743 case PixelFormat16bppRGB565:
744 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppRGB565);
745 case PixelFormat16bppARGB1555:
746 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppARGB1555);
747 case PixelFormat24bppRGB:
748 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_24bppRGB);
749 case PixelFormat32bppRGB:
750 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppRGB);
751 case PixelFormat32bppARGB:
752 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppARGB);
753 case PixelFormat32bppPARGB:
754 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppPARGB);
755 case PixelFormat48bppRGB:
756 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_48bppRGB);
757 case PixelFormat64bppARGB:
758 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_64bppARGB);
759 default:
760 break;
762 break;
763 case PixelFormat16bppRGB565:
764 switch (dst_format)
766 case PixelFormat1bppIndexed:
767 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_1bppIndexed);
768 case PixelFormat8bppIndexed:
769 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_8bppIndexed);
770 case PixelFormat16bppGrayScale:
771 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppGrayScale);
772 case PixelFormat16bppRGB555:
773 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppRGB555);
774 case PixelFormat16bppARGB1555:
775 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppARGB1555);
776 case PixelFormat24bppRGB:
777 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_24bppRGB);
778 case PixelFormat32bppRGB:
779 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppRGB);
780 case PixelFormat32bppARGB:
781 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppARGB);
782 case PixelFormat32bppPARGB:
783 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppPARGB);
784 case PixelFormat48bppRGB:
785 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_48bppRGB);
786 case PixelFormat64bppARGB:
787 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_64bppARGB);
788 default:
789 break;
791 break;
792 case PixelFormat16bppARGB1555:
793 switch (dst_format)
795 case PixelFormat1bppIndexed:
796 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_1bppIndexed);
797 case PixelFormat8bppIndexed:
798 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_8bppIndexed);
799 case PixelFormat16bppGrayScale:
800 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppGrayScale);
801 case PixelFormat16bppRGB555:
802 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB555);
803 case PixelFormat16bppRGB565:
804 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB565);
805 case PixelFormat24bppRGB:
806 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_24bppRGB);
807 case PixelFormat32bppRGB:
808 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppRGB);
809 case PixelFormat32bppARGB:
810 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppARGB);
811 case PixelFormat32bppPARGB:
812 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppPARGB);
813 case PixelFormat48bppRGB:
814 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_48bppRGB);
815 case PixelFormat64bppARGB:
816 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_64bppARGB);
817 default:
818 break;
820 break;
821 case PixelFormat24bppRGB:
822 switch (dst_format)
824 case PixelFormat1bppIndexed:
825 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_1bppIndexed);
826 case PixelFormat8bppIndexed:
827 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_8bppIndexed);
828 case PixelFormat16bppGrayScale:
829 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppGrayScale);
830 case PixelFormat16bppRGB555:
831 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB555);
832 case PixelFormat16bppRGB565:
833 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB565);
834 case PixelFormat16bppARGB1555:
835 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppARGB1555);
836 case PixelFormat32bppRGB:
837 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppRGB);
838 case PixelFormat32bppARGB:
839 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppARGB);
840 case PixelFormat32bppPARGB:
841 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppPARGB);
842 case PixelFormat48bppRGB:
843 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_48bppRGB);
844 case PixelFormat64bppARGB:
845 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_64bppARGB);
846 default:
847 break;
849 break;
850 case PixelFormat32bppRGB:
851 switch (dst_format)
853 case PixelFormat1bppIndexed:
854 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_1bppIndexed);
855 case PixelFormat8bppIndexed:
856 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_8bppIndexed);
857 case PixelFormat16bppGrayScale:
858 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppGrayScale);
859 case PixelFormat16bppRGB555:
860 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB555);
861 case PixelFormat16bppRGB565:
862 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB565);
863 case PixelFormat16bppARGB1555:
864 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppARGB1555);
865 case PixelFormat24bppRGB:
866 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_24bppRGB);
867 case PixelFormat32bppARGB:
868 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppARGB);
869 case PixelFormat32bppPARGB:
870 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppPARGB);
871 case PixelFormat48bppRGB:
872 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_48bppRGB);
873 case PixelFormat64bppARGB:
874 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_64bppARGB);
875 default:
876 break;
878 break;
879 case PixelFormat32bppARGB:
880 switch (dst_format)
882 case PixelFormat1bppIndexed:
883 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_1bppIndexed);
884 case PixelFormat8bppIndexed:
885 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_8bppIndexed);
886 case PixelFormat16bppGrayScale:
887 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppGrayScale);
888 case PixelFormat16bppRGB555:
889 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB555);
890 case PixelFormat16bppRGB565:
891 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB565);
892 case PixelFormat16bppARGB1555:
893 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppARGB1555);
894 case PixelFormat24bppRGB:
895 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_24bppRGB);
896 case PixelFormat32bppPARGB:
897 convert_32bppARGB_to_32bppPARGB(width, height, dst_bits, dst_stride, src_bits, src_stride);
898 return Ok;
899 case PixelFormat48bppRGB:
900 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_48bppRGB);
901 case PixelFormat64bppARGB:
902 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_64bppARGB);
903 default:
904 break;
906 break;
907 case PixelFormat32bppPARGB:
908 switch (dst_format)
910 case PixelFormat1bppIndexed:
911 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_1bppIndexed);
912 case PixelFormat8bppIndexed:
913 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_8bppIndexed);
914 case PixelFormat16bppGrayScale:
915 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppGrayScale);
916 case PixelFormat16bppRGB555:
917 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB555);
918 case PixelFormat16bppRGB565:
919 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB565);
920 case PixelFormat16bppARGB1555:
921 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppARGB1555);
922 case PixelFormat24bppRGB:
923 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_24bppRGB);
924 case PixelFormat32bppRGB:
925 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppRGB);
926 case PixelFormat32bppARGB:
927 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppARGB);
928 case PixelFormat48bppRGB:
929 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_48bppRGB);
930 case PixelFormat64bppARGB:
931 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_64bppARGB);
932 default:
933 break;
935 break;
936 case PixelFormat48bppRGB:
937 switch (dst_format)
939 case PixelFormat1bppIndexed:
940 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_1bppIndexed);
941 case PixelFormat8bppIndexed:
942 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_8bppIndexed);
943 case PixelFormat16bppGrayScale:
944 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppGrayScale);
945 case PixelFormat16bppRGB555:
946 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB555);
947 case PixelFormat16bppRGB565:
948 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB565);
949 case PixelFormat16bppARGB1555:
950 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppARGB1555);
951 case PixelFormat24bppRGB:
952 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_24bppRGB);
953 case PixelFormat32bppRGB:
954 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppRGB);
955 case PixelFormat32bppARGB:
956 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppARGB);
957 case PixelFormat32bppPARGB:
958 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppPARGB);
959 case PixelFormat64bppARGB:
960 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_64bppARGB);
961 default:
962 break;
964 break;
965 case PixelFormat64bppARGB:
966 switch (dst_format)
968 case PixelFormat1bppIndexed:
969 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_1bppIndexed);
970 case PixelFormat8bppIndexed:
971 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_8bppIndexed);
972 case PixelFormat16bppGrayScale:
973 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppGrayScale);
974 case PixelFormat16bppRGB555:
975 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB555);
976 case PixelFormat16bppRGB565:
977 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB565);
978 case PixelFormat16bppARGB1555:
979 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppARGB1555);
980 case PixelFormat24bppRGB:
981 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_24bppRGB);
982 case PixelFormat32bppRGB:
983 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppRGB);
984 case PixelFormat32bppARGB:
985 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppARGB);
986 case PixelFormat32bppPARGB:
987 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppPARGB);
988 case PixelFormat48bppRGB:
989 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_48bppRGB);
990 default:
991 break;
993 break;
994 case PixelFormat64bppPARGB:
995 switch (dst_format)
997 case PixelFormat1bppIndexed:
998 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_1bppIndexed);
999 case PixelFormat8bppIndexed:
1000 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_8bppIndexed);
1001 case PixelFormat16bppGrayScale:
1002 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppGrayScale);
1003 case PixelFormat16bppRGB555:
1004 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB555);
1005 case PixelFormat16bppRGB565:
1006 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB565);
1007 case PixelFormat16bppARGB1555:
1008 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppARGB1555);
1009 case PixelFormat24bppRGB:
1010 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_24bppRGB);
1011 case PixelFormat32bppRGB:
1012 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppRGB);
1013 case PixelFormat32bppARGB:
1014 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppARGB);
1015 case PixelFormat32bppPARGB:
1016 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppPARGB);
1017 case PixelFormat48bppRGB:
1018 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_48bppRGB);
1019 case PixelFormat64bppARGB:
1020 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_64bppARGB);
1021 default:
1022 break;
1024 break;
1025 default:
1026 break;
1029 #undef convert_indexed_to_rgb
1030 #undef convert_rgb_to_rgb
1032 return NotImplemented;
1035 /* This function returns a pointer to an array of pixels that represents the
1036 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
1037 * flags. It is correct behavior that a user who calls this function with write
1038 * privileges can write to the whole bitmap (not just the area in rect).
1040 * FIXME: only used portion of format is bits per pixel. */
1041 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
1042 UINT flags, PixelFormat format, BitmapData* lockeddata)
1044 INT bitspp = PIXELFORMATBPP(format);
1045 GpRect act_rect; /* actual rect to be used */
1046 GpStatus stat;
1048 TRACE("%p %p %d 0x%x %p\n", bitmap, rect, flags, format, lockeddata);
1050 if(!lockeddata || !bitmap)
1051 return InvalidParameter;
1053 if(rect){
1054 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
1055 (rect->Y + rect->Height > bitmap->height) || !flags)
1056 return InvalidParameter;
1058 act_rect = *rect;
1060 else{
1061 act_rect.X = act_rect.Y = 0;
1062 act_rect.Width = bitmap->width;
1063 act_rect.Height = bitmap->height;
1066 if(bitmap->lockmode)
1068 WARN("bitmap is already locked and cannot be locked again\n");
1069 return WrongState;
1072 if (bitmap->bits && bitmap->format == format && !(flags & ImageLockModeUserInputBuf))
1074 /* no conversion is necessary; just use the bits directly */
1075 lockeddata->Width = act_rect.Width;
1076 lockeddata->Height = act_rect.Height;
1077 lockeddata->PixelFormat = format;
1078 lockeddata->Reserved = flags;
1079 lockeddata->Stride = bitmap->stride;
1080 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
1081 bitmap->stride * act_rect.Y;
1083 bitmap->lockmode = flags | ImageLockModeRead;
1084 bitmap->numlocks++;
1086 return Ok;
1089 /* Make sure we can convert to the requested format. */
1090 if (flags & ImageLockModeRead)
1092 stat = convert_pixels(0, 0, 0, NULL, format, 0, NULL, bitmap->format, NULL);
1093 if (stat == NotImplemented)
1095 FIXME("cannot read bitmap from %x to %x\n", bitmap->format, format);
1096 return NotImplemented;
1100 /* If we're opening for writing, make sure we'll be able to write back in
1101 * the original format. */
1102 if (flags & ImageLockModeWrite)
1104 stat = convert_pixels(0, 0, 0, NULL, bitmap->format, 0, NULL, format, NULL);
1105 if (stat == NotImplemented)
1107 FIXME("cannot write bitmap from %x to %x\n", format, bitmap->format);
1108 return NotImplemented;
1112 lockeddata->Width = act_rect.Width;
1113 lockeddata->Height = act_rect.Height;
1114 lockeddata->PixelFormat = format;
1115 lockeddata->Reserved = flags;
1117 if(!(flags & ImageLockModeUserInputBuf))
1119 lockeddata->Stride = (((act_rect.Width * bitspp + 7) / 8) + 3) & ~3;
1121 bitmap->bitmapbits = GdipAlloc(lockeddata->Stride * act_rect.Height);
1123 if (!bitmap->bitmapbits) return OutOfMemory;
1125 lockeddata->Scan0 = bitmap->bitmapbits;
1128 if (flags & ImageLockModeRead)
1130 static int fixme=0;
1132 if (!fixme && (PIXELFORMATBPP(bitmap->format) * act_rect.X) % 8 != 0)
1134 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1135 fixme = 1;
1138 stat = convert_pixels(act_rect.Width, act_rect.Height,
1139 lockeddata->Stride, lockeddata->Scan0, format,
1140 bitmap->stride,
1141 bitmap->bits + bitmap->stride * act_rect.Y + PIXELFORMATBPP(bitmap->format) * act_rect.X / 8,
1142 bitmap->format, bitmap->image.palette);
1144 if (stat != Ok)
1146 GdipFree(bitmap->bitmapbits);
1147 bitmap->bitmapbits = NULL;
1148 return stat;
1152 bitmap->lockmode = flags | ImageLockModeRead;
1153 bitmap->numlocks++;
1154 bitmap->lockx = act_rect.X;
1155 bitmap->locky = act_rect.Y;
1157 return Ok;
1160 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
1162 TRACE("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
1164 if (!bitmap || xdpi == 0.0 || ydpi == 0.0)
1165 return InvalidParameter;
1167 bitmap->image.xres = xdpi;
1168 bitmap->image.yres = ydpi;
1170 return Ok;
1173 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
1174 BitmapData* lockeddata)
1176 GpStatus stat;
1177 static int fixme=0;
1179 TRACE("(%p,%p)\n", bitmap, lockeddata);
1181 if(!bitmap || !lockeddata)
1182 return InvalidParameter;
1184 if(!bitmap->lockmode)
1185 return WrongState;
1187 if(!(lockeddata->Reserved & ImageLockModeWrite)){
1188 if(!(--bitmap->numlocks))
1189 bitmap->lockmode = 0;
1191 GdipFree(bitmap->bitmapbits);
1192 bitmap->bitmapbits = NULL;
1193 return Ok;
1196 if (!bitmap->bitmapbits && !(lockeddata->Reserved & ImageLockModeUserInputBuf))
1198 /* we passed a direct reference; no need to do anything */
1199 bitmap->lockmode = 0;
1200 bitmap->numlocks = 0;
1201 return Ok;
1204 if (!fixme && (PIXELFORMATBPP(bitmap->format) * bitmap->lockx) % 8 != 0)
1206 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1207 fixme = 1;
1210 stat = convert_pixels(lockeddata->Width, lockeddata->Height,
1211 bitmap->stride,
1212 bitmap->bits + bitmap->stride * bitmap->locky + PIXELFORMATBPP(bitmap->format) * bitmap->lockx / 8,
1213 bitmap->format,
1214 lockeddata->Stride, lockeddata->Scan0, lockeddata->PixelFormat, NULL);
1216 if (stat != Ok)
1218 ERR("failed to convert pixels; this should never happen\n");
1221 GdipFree(bitmap->bitmapbits);
1222 bitmap->bitmapbits = NULL;
1223 bitmap->lockmode = 0;
1224 bitmap->numlocks = 0;
1226 return stat;
1229 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
1230 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1232 BitmapData lockeddata_src, lockeddata_dst;
1233 int i;
1234 UINT row_size;
1235 Rect area;
1236 GpStatus stat;
1238 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1240 if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
1241 x < 0 || y < 0 ||
1242 x + width > srcBitmap->width || y + height > srcBitmap->height)
1244 TRACE("<-- InvalidParameter\n");
1245 return InvalidParameter;
1248 if (format == PixelFormatDontCare)
1249 format = srcBitmap->format;
1251 area.X = gdip_round(x);
1252 area.Y = gdip_round(y);
1253 area.Width = gdip_round(width);
1254 area.Height = gdip_round(height);
1256 stat = GdipBitmapLockBits(srcBitmap, &area, ImageLockModeRead, format,
1257 &lockeddata_src);
1258 if (stat != Ok) return stat;
1260 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
1261 0, lockeddata_src.PixelFormat, NULL, dstBitmap);
1262 if (stat == Ok)
1264 stat = GdipBitmapLockBits(*dstBitmap, NULL, ImageLockModeWrite,
1265 lockeddata_src.PixelFormat, &lockeddata_dst);
1267 if (stat == Ok)
1269 /* copy the image data */
1270 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
1271 for (i=0; i<lockeddata_src.Height; i++)
1272 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
1273 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
1274 row_size);
1276 GdipBitmapUnlockBits(*dstBitmap, &lockeddata_dst);
1279 if (stat != Ok)
1280 GdipDisposeImage((GpImage*)*dstBitmap);
1283 GdipBitmapUnlockBits(srcBitmap, &lockeddata_src);
1285 if (stat != Ok)
1287 *dstBitmap = NULL;
1290 return stat;
1293 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
1294 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1296 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1298 return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
1301 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
1303 GpStatus stat = GenericError;
1305 TRACE("%p, %p\n", image, cloneImage);
1307 if (!image || !cloneImage)
1308 return InvalidParameter;
1310 if (image->picture)
1312 IStream* stream;
1313 HRESULT hr;
1314 INT size;
1315 LARGE_INTEGER move;
1317 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
1318 if (FAILED(hr))
1319 return GenericError;
1321 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
1322 if(FAILED(hr))
1324 WARN("Failed to save image on stream\n");
1325 goto out;
1328 /* Set seek pointer back to the beginning of the picture */
1329 move.QuadPart = 0;
1330 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1331 if (FAILED(hr))
1332 goto out;
1334 stat = GdipLoadImageFromStream(stream, cloneImage);
1335 if (stat != Ok) WARN("Failed to load image from stream\n");
1337 out:
1338 IStream_Release(stream);
1339 return stat;
1341 else if (image->type == ImageTypeBitmap)
1343 GpBitmap *bitmap = (GpBitmap*)image;
1344 BitmapData lockeddata_src, lockeddata_dst;
1345 int i;
1346 UINT row_size;
1348 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format,
1349 &lockeddata_src);
1350 if (stat != Ok) return stat;
1352 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
1353 0, lockeddata_src.PixelFormat, NULL, (GpBitmap**)cloneImage);
1354 if (stat == Ok)
1356 stat = GdipBitmapLockBits((GpBitmap*)*cloneImage, NULL, ImageLockModeWrite,
1357 lockeddata_src.PixelFormat, &lockeddata_dst);
1359 if (stat == Ok)
1361 /* copy the image data */
1362 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
1363 for (i=0; i<lockeddata_src.Height; i++)
1364 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
1365 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
1366 row_size);
1368 GdipBitmapUnlockBits((GpBitmap*)*cloneImage, &lockeddata_dst);
1371 if (stat != Ok)
1372 GdipDisposeImage(*cloneImage);
1375 GdipBitmapUnlockBits(bitmap, &lockeddata_src);
1377 if (stat != Ok)
1379 *cloneImage = NULL;
1381 else memcpy(&(*cloneImage)->format, &image->format, sizeof(GUID));
1383 return stat;
1385 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
1387 GpMetafile *result, *metafile;
1389 metafile = (GpMetafile*)image;
1391 result = GdipAlloc(sizeof(*result));
1392 if (!result)
1393 return OutOfMemory;
1395 result->image.type = ImageTypeMetafile;
1396 result->image.format = image->format;
1397 result->image.flags = image->flags;
1398 result->image.frame_count = 1;
1399 result->image.xres = image->xres;
1400 result->image.yres = image->yres;
1401 result->bounds = metafile->bounds;
1402 result->unit = metafile->unit;
1403 result->metafile_type = metafile->metafile_type;
1404 result->hemf = CopyEnhMetaFileW(metafile->hemf, NULL);
1406 if (!result->hemf)
1408 GdipFree(result);
1409 return OutOfMemory;
1412 *cloneImage = &result->image;
1413 return Ok;
1415 else
1417 WARN("GpImage with no image data (metafile in wrong state?)\n");
1418 return InvalidParameter;
1422 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
1423 GpBitmap **bitmap)
1425 GpStatus stat;
1426 IStream *stream;
1428 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1430 if(!filename || !bitmap)
1431 return InvalidParameter;
1433 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1435 if(stat != Ok)
1436 return stat;
1438 stat = GdipCreateBitmapFromStream(stream, bitmap);
1440 IStream_Release(stream);
1442 return stat;
1445 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
1446 VOID *bits, GpBitmap **bitmap)
1448 DWORD height, stride;
1449 PixelFormat format;
1451 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
1453 if (!info || !bits || !bitmap)
1454 return InvalidParameter;
1456 height = abs(info->bmiHeader.biHeight);
1457 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
1459 if(info->bmiHeader.biHeight > 0) /* bottom-up */
1461 bits = (BYTE*)bits + (height - 1) * stride;
1462 stride = -stride;
1465 switch(info->bmiHeader.biBitCount) {
1466 case 1:
1467 format = PixelFormat1bppIndexed;
1468 break;
1469 case 4:
1470 format = PixelFormat4bppIndexed;
1471 break;
1472 case 8:
1473 format = PixelFormat8bppIndexed;
1474 break;
1475 case 16:
1476 format = PixelFormat16bppRGB555;
1477 break;
1478 case 24:
1479 format = PixelFormat24bppRGB;
1480 break;
1481 case 32:
1482 format = PixelFormat32bppRGB;
1483 break;
1484 default:
1485 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
1486 *bitmap = NULL;
1487 return InvalidParameter;
1490 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
1491 bits, bitmap);
1495 /* FIXME: no icm */
1496 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
1497 GpBitmap **bitmap)
1499 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1501 return GdipCreateBitmapFromFile(filename, bitmap);
1504 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
1505 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
1507 HBITMAP hbm;
1508 GpStatus stat = InvalidParameter;
1510 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
1512 if(!lpBitmapName || !bitmap)
1513 return InvalidParameter;
1515 /* load DIB */
1516 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
1517 LR_CREATEDIBSECTION);
1519 if(hbm){
1520 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1521 DeleteObject(hbm);
1524 return stat;
1527 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
1528 HBITMAP* hbmReturn, ARGB background)
1530 GpStatus stat;
1531 HBITMAP result;
1532 UINT width, height;
1533 BITMAPINFOHEADER bih;
1534 LPBYTE bits;
1535 BitmapData lockeddata;
1536 TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
1538 if (!bitmap || !hbmReturn) return InvalidParameter;
1540 GdipGetImageWidth((GpImage*)bitmap, &width);
1541 GdipGetImageHeight((GpImage*)bitmap, &height);
1543 bih.biSize = sizeof(bih);
1544 bih.biWidth = width;
1545 bih.biHeight = height;
1546 bih.biPlanes = 1;
1547 bih.biBitCount = 32;
1548 bih.biCompression = BI_RGB;
1549 bih.biSizeImage = 0;
1550 bih.biXPelsPerMeter = 0;
1551 bih.biYPelsPerMeter = 0;
1552 bih.biClrUsed = 0;
1553 bih.biClrImportant = 0;
1555 result = CreateDIBSection(0, (BITMAPINFO*)&bih, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1557 if (result)
1559 lockeddata.Stride = -width * 4;
1560 lockeddata.Scan0 = bits + (width * 4 * (height - 1));
1562 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead|ImageLockModeUserInputBuf,
1563 PixelFormat32bppPARGB, &lockeddata);
1565 if (stat == Ok)
1566 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1568 else
1569 stat = GenericError;
1571 if (stat != Ok && result)
1573 DeleteObject(result);
1574 result = NULL;
1577 *hbmReturn = result;
1579 return stat;
1582 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
1583 GpMetafile* metafile, BOOL* succ, EmfType emfType,
1584 const WCHAR* description, GpMetafile** out_metafile)
1586 static int calls;
1588 TRACE("(%p,%p,%p,%u,%s,%p)\n", ref, metafile, succ, emfType,
1589 debugstr_w(description), out_metafile);
1591 if(!ref || !metafile || !out_metafile)
1592 return InvalidParameter;
1594 *succ = FALSE;
1595 *out_metafile = NULL;
1597 if(!(calls++))
1598 FIXME("not implemented\n");
1600 return NotImplemented;
1603 /* FIXME: this should create a bitmap in the given size with the attributes
1604 * (resolution etc.) of the graphics object */
1605 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
1606 GpGraphics* target, GpBitmap** bitmap)
1608 static int calls;
1609 GpStatus ret;
1611 TRACE("(%d, %d, %p, %p)\n", width, height, target, bitmap);
1613 if(!target || !bitmap)
1614 return InvalidParameter;
1616 if(!(calls++))
1617 FIXME("hacked stub\n");
1619 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
1620 NULL, bitmap);
1622 return ret;
1625 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1627 GpStatus stat;
1628 ICONINFO iinfo;
1629 BITMAP bm;
1630 int ret;
1631 UINT width, height;
1632 GpRect rect;
1633 BitmapData lockeddata;
1634 HDC screendc;
1635 BOOL has_alpha;
1636 int x, y;
1637 BYTE *bits;
1638 BITMAPINFOHEADER bih;
1639 DWORD *src;
1640 BYTE *dst_row;
1641 DWORD *dst;
1643 TRACE("%p, %p\n", hicon, bitmap);
1645 if(!bitmap || !GetIconInfo(hicon, &iinfo))
1646 return InvalidParameter;
1648 /* get the size of the icon */
1649 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1650 if (ret == 0) {
1651 DeleteObject(iinfo.hbmColor);
1652 DeleteObject(iinfo.hbmMask);
1653 return GenericError;
1656 width = bm.bmWidth;
1658 if (iinfo.hbmColor)
1659 height = abs(bm.bmHeight);
1660 else /* combined bitmap + mask */
1661 height = abs(bm.bmHeight) / 2;
1663 bits = HeapAlloc(GetProcessHeap(), 0, 4*width*height);
1664 if (!bits) {
1665 DeleteObject(iinfo.hbmColor);
1666 DeleteObject(iinfo.hbmMask);
1667 return OutOfMemory;
1670 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB, NULL, bitmap);
1671 if (stat != Ok) {
1672 DeleteObject(iinfo.hbmColor);
1673 DeleteObject(iinfo.hbmMask);
1674 HeapFree(GetProcessHeap(), 0, bits);
1675 return stat;
1678 rect.X = 0;
1679 rect.Y = 0;
1680 rect.Width = width;
1681 rect.Height = height;
1683 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1684 if (stat != Ok) {
1685 DeleteObject(iinfo.hbmColor);
1686 DeleteObject(iinfo.hbmMask);
1687 HeapFree(GetProcessHeap(), 0, bits);
1688 GdipDisposeImage((GpImage*)*bitmap);
1689 return stat;
1692 bih.biSize = sizeof(bih);
1693 bih.biWidth = width;
1694 bih.biHeight = -height;
1695 bih.biPlanes = 1;
1696 bih.biBitCount = 32;
1697 bih.biCompression = BI_RGB;
1698 bih.biSizeImage = 0;
1699 bih.biXPelsPerMeter = 0;
1700 bih.biYPelsPerMeter = 0;
1701 bih.biClrUsed = 0;
1702 bih.biClrImportant = 0;
1704 screendc = GetDC(0);
1705 if (iinfo.hbmColor)
1707 GetDIBits(screendc, iinfo.hbmColor, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1709 if (bm.bmBitsPixel == 32)
1711 has_alpha = FALSE;
1713 /* If any pixel has a non-zero alpha, ignore hbmMask */
1714 src = (DWORD*)bits;
1715 for (x=0; x<width && !has_alpha; x++)
1716 for (y=0; y<height && !has_alpha; y++)
1717 if ((*src++ & 0xff000000) != 0)
1718 has_alpha = TRUE;
1720 else has_alpha = FALSE;
1722 else
1724 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1725 has_alpha = FALSE;
1728 /* copy the image data to the Bitmap */
1729 src = (DWORD*)bits;
1730 dst_row = lockeddata.Scan0;
1731 for (y=0; y<height; y++)
1733 memcpy(dst_row, src, width*4);
1734 src += width;
1735 dst_row += lockeddata.Stride;
1738 if (!has_alpha)
1740 if (iinfo.hbmMask)
1742 /* read alpha data from the mask */
1743 if (iinfo.hbmColor)
1744 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1745 else
1746 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1748 src = (DWORD*)bits;
1749 dst_row = lockeddata.Scan0;
1750 for (y=0; y<height; y++)
1752 dst = (DWORD*)dst_row;
1753 for (x=0; x<height; x++)
1755 DWORD src_value = *src++;
1756 if (src_value)
1757 *dst++ = 0;
1758 else
1759 *dst++ |= 0xff000000;
1761 dst_row += lockeddata.Stride;
1764 else
1766 /* set constant alpha of 255 */
1767 dst_row = bits;
1768 for (y=0; y<height; y++)
1770 dst = (DWORD*)dst_row;
1771 for (x=0; x<height; x++)
1772 *dst++ |= 0xff000000;
1773 dst_row += lockeddata.Stride;
1778 ReleaseDC(0, screendc);
1780 DeleteObject(iinfo.hbmColor);
1781 DeleteObject(iinfo.hbmMask);
1783 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1785 HeapFree(GetProcessHeap(), 0, bits);
1787 return Ok;
1790 static void generate_halftone_palette(ARGB *entries, UINT count)
1792 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1793 UINT i;
1795 for (i=0; i<8 && i<count; i++)
1797 entries[i] = 0xff000000;
1798 if (i&1) entries[i] |= 0x800000;
1799 if (i&2) entries[i] |= 0x8000;
1800 if (i&4) entries[i] |= 0x80;
1803 if (8 < count)
1804 entries[i] = 0xffc0c0c0;
1806 for (i=9; i<16 && i<count; i++)
1808 entries[i] = 0xff000000;
1809 if (i&1) entries[i] |= 0xff0000;
1810 if (i&2) entries[i] |= 0xff00;
1811 if (i&4) entries[i] |= 0xff;
1814 for (i=16; i<40 && i<count; i++)
1816 entries[i] = 0;
1819 for (i=40; i<256 && i<count; i++)
1821 entries[i] = 0xff000000;
1822 entries[i] |= halftone_values[(i-40)%6];
1823 entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1824 entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1828 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1830 HDC screendc = GetDC(0);
1832 if (!screendc) return GenericError;
1834 *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1835 *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1837 ReleaseDC(0, screendc);
1839 return Ok;
1842 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1843 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1845 BITMAPINFO* pbmi;
1846 HBITMAP hbitmap=NULL;
1847 INT row_size, dib_stride;
1848 BYTE *bits=NULL, *own_bits=NULL;
1849 REAL xres, yres;
1850 GpStatus stat;
1852 TRACE("%d %d %d 0x%x %p %p\n", width, height, stride, format, scan0, bitmap);
1854 if (!bitmap) return InvalidParameter;
1856 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1857 *bitmap = NULL;
1858 return InvalidParameter;
1861 if(scan0 && !stride)
1862 return InvalidParameter;
1864 stat = get_screen_resolution(&xres, &yres);
1865 if (stat != Ok) return stat;
1867 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1868 dib_stride = (row_size + 3) & ~3;
1870 if(stride == 0)
1871 stride = dib_stride;
1873 if (format & PixelFormatGDI && !(format & (PixelFormatAlpha|PixelFormatIndexed)) && !scan0)
1875 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1876 if (!pbmi)
1877 return OutOfMemory;
1879 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1880 pbmi->bmiHeader.biWidth = width;
1881 pbmi->bmiHeader.biHeight = -height;
1882 pbmi->bmiHeader.biPlanes = 1;
1883 /* FIXME: use the rest of the data from format */
1884 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(format);
1885 pbmi->bmiHeader.biCompression = BI_RGB;
1886 pbmi->bmiHeader.biSizeImage = 0;
1887 pbmi->bmiHeader.biXPelsPerMeter = 0;
1888 pbmi->bmiHeader.biYPelsPerMeter = 0;
1889 pbmi->bmiHeader.biClrUsed = 0;
1890 pbmi->bmiHeader.biClrImportant = 0;
1892 hbitmap = CreateDIBSection(0, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1894 GdipFree(pbmi);
1896 if (!hbitmap) return GenericError;
1898 stride = dib_stride;
1900 else
1902 /* Not a GDI format; don't try to make an HBITMAP. */
1903 if (scan0)
1904 bits = scan0;
1905 else
1907 INT size = abs(stride) * height;
1909 own_bits = bits = GdipAlloc(size);
1910 if (!own_bits) return OutOfMemory;
1912 if (stride < 0)
1913 bits += stride * (1 - height);
1917 *bitmap = GdipAlloc(sizeof(GpBitmap));
1918 if(!*bitmap)
1920 DeleteObject(hbitmap);
1921 GdipFree(own_bits);
1922 return OutOfMemory;
1925 (*bitmap)->image.type = ImageTypeBitmap;
1926 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1927 (*bitmap)->image.flags = ImageFlagsNone;
1928 (*bitmap)->image.frame_count = 1;
1929 (*bitmap)->image.current_frame = 0;
1930 (*bitmap)->image.palette = NULL;
1931 (*bitmap)->image.xres = xres;
1932 (*bitmap)->image.yres = yres;
1933 (*bitmap)->width = width;
1934 (*bitmap)->height = height;
1935 (*bitmap)->format = format;
1936 (*bitmap)->image.picture = NULL;
1937 (*bitmap)->image.stream = NULL;
1938 (*bitmap)->hbitmap = hbitmap;
1939 (*bitmap)->hdc = NULL;
1940 (*bitmap)->bits = bits;
1941 (*bitmap)->stride = stride;
1942 (*bitmap)->own_bits = own_bits;
1943 (*bitmap)->metadata_reader = NULL;
1944 (*bitmap)->prop_count = 0;
1945 (*bitmap)->prop_item = NULL;
1947 /* set format-related flags */
1948 if (format & (PixelFormatAlpha|PixelFormatPAlpha|PixelFormatIndexed))
1949 (*bitmap)->image.flags |= ImageFlagsHasAlpha;
1951 if (format == PixelFormat1bppIndexed ||
1952 format == PixelFormat4bppIndexed ||
1953 format == PixelFormat8bppIndexed)
1955 (*bitmap)->image.palette = GdipAlloc(sizeof(UINT) * 2 + sizeof(ARGB) * (1 << PIXELFORMATBPP(format)));
1957 if (!(*bitmap)->image.palette)
1959 GdipDisposeImage(&(*bitmap)->image);
1960 *bitmap = NULL;
1961 return OutOfMemory;
1964 (*bitmap)->image.palette->Count = 1 << PIXELFORMATBPP(format);
1966 if (format == PixelFormat1bppIndexed)
1968 (*bitmap)->image.palette->Flags = PaletteFlagsGrayScale;
1969 (*bitmap)->image.palette->Entries[0] = 0xff000000;
1970 (*bitmap)->image.palette->Entries[1] = 0xffffffff;
1972 else
1974 if (format == PixelFormat8bppIndexed)
1975 (*bitmap)->image.palette->Flags = PaletteFlagsHalftone;
1977 generate_halftone_palette((*bitmap)->image.palette->Entries,
1978 (*bitmap)->image.palette->Count);
1982 TRACE("<-- %p\n", *bitmap);
1984 return Ok;
1987 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1988 GpBitmap **bitmap)
1990 GpStatus stat;
1992 TRACE("%p %p\n", stream, bitmap);
1994 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1996 if(stat != Ok)
1997 return stat;
1999 if((*bitmap)->image.type != ImageTypeBitmap){
2000 GdipDisposeImage(&(*bitmap)->image);
2001 *bitmap = NULL;
2002 return GenericError; /* FIXME: what error to return? */
2005 return Ok;
2008 /* FIXME: no icm */
2009 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
2010 GpBitmap **bitmap)
2012 TRACE("%p %p\n", stream, bitmap);
2014 return GdipCreateBitmapFromStream(stream, bitmap);
2017 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
2018 GpCachedBitmap **cachedbmp)
2020 GpStatus stat;
2022 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
2024 if(!bitmap || !graphics || !cachedbmp)
2025 return InvalidParameter;
2027 *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
2028 if(!*cachedbmp)
2029 return OutOfMemory;
2031 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
2032 if(stat != Ok){
2033 GdipFree(*cachedbmp);
2034 return stat;
2037 return Ok;
2040 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
2042 GpStatus stat;
2043 BitmapData lockeddata;
2044 ULONG andstride, xorstride, bitssize;
2045 LPBYTE andbits, xorbits, androw, xorrow, srcrow;
2046 UINT x, y;
2048 TRACE("(%p, %p)\n", bitmap, hicon);
2050 if (!bitmap || !hicon)
2051 return InvalidParameter;
2053 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead,
2054 PixelFormat32bppPARGB, &lockeddata);
2055 if (stat == Ok)
2057 andstride = ((lockeddata.Width+31)/32)*4;
2058 xorstride = lockeddata.Width*4;
2059 bitssize = (andstride + xorstride) * lockeddata.Height;
2061 andbits = GdipAlloc(bitssize);
2063 if (andbits)
2065 xorbits = andbits + andstride * lockeddata.Height;
2067 for (y=0; y<lockeddata.Height; y++)
2069 srcrow = ((LPBYTE)lockeddata.Scan0) + lockeddata.Stride * y;
2071 androw = andbits + andstride * y;
2072 for (x=0; x<lockeddata.Width; x++)
2073 if (srcrow[3+4*x] >= 128)
2074 androw[x/8] |= 1 << (7-x%8);
2076 xorrow = xorbits + xorstride * y;
2077 memcpy(xorrow, srcrow, xorstride);
2080 *hicon = CreateIcon(NULL, lockeddata.Width, lockeddata.Height, 1, 32,
2081 andbits, xorbits);
2083 GdipFree(andbits);
2085 else
2086 stat = OutOfMemory;
2088 GdipBitmapUnlockBits(bitmap, &lockeddata);
2091 return stat;
2094 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
2096 TRACE("%p\n", cachedbmp);
2098 if(!cachedbmp)
2099 return InvalidParameter;
2101 GdipDisposeImage(cachedbmp->image);
2102 GdipFree(cachedbmp);
2104 return Ok;
2107 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
2108 GpCachedBitmap *cachedbmp, INT x, INT y)
2110 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
2112 if(!graphics || !cachedbmp)
2113 return InvalidParameter;
2115 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
2118 GpStatus WINGDIPAPI GdipEmfToWmfBits(HENHMETAFILE hemf, UINT cbData16,
2119 LPBYTE pData16, INT iMapMode, INT eFlags)
2121 FIXME("(%p, %d, %p, %d, %d): stub\n", hemf, cbData16, pData16, iMapMode, eFlags);
2122 return NotImplemented;
2125 /* Internal utility function: Replace the image data of dst with that of src,
2126 * and free src. */
2127 static void move_bitmap(GpBitmap *dst, GpBitmap *src, BOOL clobber_palette)
2129 assert(src->image.type == ImageTypeBitmap);
2130 assert(dst->image.type == ImageTypeBitmap);
2132 GdipFree(dst->bitmapbits);
2133 GdipFree(dst->own_bits);
2134 DeleteDC(dst->hdc);
2135 DeleteObject(dst->hbitmap);
2137 if (clobber_palette)
2139 GdipFree(dst->image.palette);
2140 dst->image.palette = src->image.palette;
2142 else
2143 GdipFree(src->image.palette);
2145 dst->image.xres = src->image.xres;
2146 dst->image.yres = src->image.yres;
2147 dst->width = src->width;
2148 dst->height = src->height;
2149 dst->format = src->format;
2150 dst->hbitmap = src->hbitmap;
2151 dst->hdc = src->hdc;
2152 dst->bits = src->bits;
2153 dst->stride = src->stride;
2154 dst->own_bits = src->own_bits;
2155 if (dst->metadata_reader)
2156 IWICMetadataReader_Release(dst->metadata_reader);
2157 dst->metadata_reader = src->metadata_reader;
2158 GdipFree(dst->prop_item);
2159 dst->prop_item = src->prop_item;
2160 dst->prop_count = src->prop_count;
2161 if (dst->image.stream)
2162 IStream_Release(dst->image.stream);
2163 dst->image.stream = src->image.stream;
2164 dst->image.frame_count = src->image.frame_count;
2165 dst->image.current_frame = src->image.current_frame;
2166 dst->image.format = src->image.format;
2168 src->image.type = ~0;
2169 GdipFree(src);
2172 static GpStatus free_image_data(GpImage *image)
2174 if(!image)
2175 return InvalidParameter;
2177 if (image->type == ImageTypeBitmap)
2179 GdipFree(((GpBitmap*)image)->bitmapbits);
2180 GdipFree(((GpBitmap*)image)->own_bits);
2181 DeleteDC(((GpBitmap*)image)->hdc);
2182 DeleteObject(((GpBitmap*)image)->hbitmap);
2183 if (((GpBitmap*)image)->metadata_reader)
2184 IWICMetadataReader_Release(((GpBitmap*)image)->metadata_reader);
2185 GdipFree(((GpBitmap*)image)->prop_item);
2187 else if (image->type == ImageTypeMetafile)
2189 GpMetafile *metafile = (GpMetafile*)image;
2190 GdipFree(metafile->comment_data);
2191 DeleteEnhMetaFile(CloseEnhMetaFile(metafile->record_dc));
2192 DeleteEnhMetaFile(metafile->hemf);
2193 if (metafile->record_graphics)
2195 WARN("metafile closed while recording\n");
2196 /* not sure what to do here; for now just prevent the graphics from functioning or using this object */
2197 metafile->record_graphics->image = NULL;
2198 metafile->record_graphics->busy = TRUE;
2201 else
2203 WARN("invalid image: %p\n", image);
2204 return ObjectBusy;
2206 if (image->picture)
2207 IPicture_Release(image->picture);
2208 if (image->stream)
2209 IStream_Release(image->stream);
2210 GdipFree(image->palette);
2212 return Ok;
2215 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
2217 GpStatus status;
2219 TRACE("%p\n", image);
2221 status = free_image_data(image);
2222 if (status != Ok) return status;
2223 image->type = ~0;
2224 GdipFree(image);
2226 return Ok;
2229 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
2231 static int calls;
2233 TRACE("(%p,%p)\n", image, item);
2235 if(!image || !item)
2236 return InvalidParameter;
2238 if (!(calls++))
2239 FIXME("not implemented\n");
2241 return NotImplemented;
2244 GpStatus WINGDIPAPI GdipGetImageItemData(GpImage *image, ImageItemData *item)
2246 static int calls;
2248 TRACE("(%p,%p)\n", image, item);
2250 if (!(calls++))
2251 FIXME("not implemented\n");
2253 return NotImplemented;
2256 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
2257 GpUnit *srcUnit)
2259 TRACE("%p %p %p\n", image, srcRect, srcUnit);
2261 if(!image || !srcRect || !srcUnit)
2262 return InvalidParameter;
2263 if(image->type == ImageTypeMetafile){
2264 *srcRect = ((GpMetafile*)image)->bounds;
2265 *srcUnit = ((GpMetafile*)image)->unit;
2267 else if(image->type == ImageTypeBitmap){
2268 srcRect->X = srcRect->Y = 0.0;
2269 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
2270 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
2271 *srcUnit = UnitPixel;
2273 else{
2274 srcRect->X = srcRect->Y = 0.0;
2275 srcRect->Width = ipicture_pixel_width(image->picture);
2276 srcRect->Height = ipicture_pixel_height(image->picture);
2277 *srcUnit = UnitPixel;
2280 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
2281 srcRect->Width, srcRect->Height, *srcUnit);
2283 return Ok;
2286 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
2287 REAL *height)
2289 TRACE("%p %p %p\n", image, width, height);
2291 if(!image || !height || !width)
2292 return InvalidParameter;
2294 if(image->type == ImageTypeMetafile){
2295 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2296 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2298 else if(image->type == ImageTypeBitmap){
2299 *height = ((GpBitmap*)image)->height;
2300 *width = ((GpBitmap*)image)->width;
2302 else{
2303 *height = ipicture_pixel_height(image->picture);
2304 *width = ipicture_pixel_width(image->picture);
2307 TRACE("returning (%f, %f)\n", *height, *width);
2308 return Ok;
2311 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
2312 GpGraphics **graphics)
2314 HDC hdc;
2315 GpStatus stat;
2317 TRACE("%p %p\n", image, graphics);
2319 if(!image || !graphics)
2320 return InvalidParameter;
2322 if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
2324 hdc = ((GpBitmap*)image)->hdc;
2326 if(!hdc){
2327 hdc = CreateCompatibleDC(0);
2328 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
2329 ((GpBitmap*)image)->hdc = hdc;
2332 stat = GdipCreateFromHDC(hdc, graphics);
2334 if (stat == Ok)
2336 (*graphics)->image = image;
2337 (*graphics)->xres = image->xres;
2338 (*graphics)->yres = image->yres;
2341 else if (image->type == ImageTypeMetafile)
2342 stat = METAFILE_GetGraphicsContext((GpMetafile*)image, graphics);
2343 else
2344 stat = graphics_from_image(image, graphics);
2346 return stat;
2349 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
2351 TRACE("%p %p\n", image, height);
2353 if(!image || !height)
2354 return InvalidParameter;
2356 if(image->type == ImageTypeMetafile)
2357 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2358 else if(image->type == ImageTypeBitmap)
2359 *height = ((GpBitmap*)image)->height;
2360 else
2361 *height = ipicture_pixel_height(image->picture);
2363 TRACE("returning %d\n", *height);
2365 return Ok;
2368 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2370 if(!image || !res)
2371 return InvalidParameter;
2373 *res = image->xres;
2375 TRACE("(%p) <-- %0.2f\n", image, *res);
2377 return Ok;
2380 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2382 TRACE("%p %p\n", image, size);
2384 if(!image || !size)
2385 return InvalidParameter;
2387 if (!image->palette || image->palette->Count == 0)
2388 *size = sizeof(ColorPalette);
2389 else
2390 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette->Count;
2392 TRACE("<-- %u\n", *size);
2394 return Ok;
2397 /* FIXME: test this function for non-bitmap types */
2398 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2400 TRACE("%p %p\n", image, format);
2402 if(!image || !format)
2403 return InvalidParameter;
2405 if(image->type != ImageTypeBitmap)
2406 *format = PixelFormat24bppRGB;
2407 else
2408 *format = ((GpBitmap*) image)->format;
2410 return Ok;
2413 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2415 TRACE("(%p, %p)\n", image, format);
2417 if(!image || !format)
2418 return InvalidParameter;
2420 memcpy(format, &image->format, sizeof(GUID));
2422 return Ok;
2425 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2427 TRACE("%p %p\n", image, type);
2429 if(!image || !type)
2430 return InvalidParameter;
2432 *type = image->type;
2434 return Ok;
2437 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2439 if(!image || !res)
2440 return InvalidParameter;
2442 *res = image->yres;
2444 TRACE("(%p) <-- %0.2f\n", image, *res);
2446 return Ok;
2449 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2451 TRACE("%p %p\n", image, width);
2453 if(!image || !width)
2454 return InvalidParameter;
2456 if(image->type == ImageTypeMetafile)
2457 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2458 else if(image->type == ImageTypeBitmap)
2459 *width = ((GpBitmap*)image)->width;
2460 else
2461 *width = ipicture_pixel_width(image->picture);
2463 TRACE("returning %d\n", *width);
2465 return Ok;
2468 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
2469 MetafileHeader * header)
2471 static int calls;
2473 TRACE("(%p, %p)\n", metafile, header);
2475 if(!metafile || !header)
2476 return InvalidParameter;
2478 if(!(calls++))
2479 FIXME("not implemented\n");
2481 memset(header, 0, sizeof(MetafileHeader));
2483 return Ok;
2486 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromEmf(HENHMETAFILE hEmf,
2487 MetafileHeader *header)
2489 static int calls;
2491 if(!hEmf || !header)
2492 return InvalidParameter;
2494 if(!(calls++))
2495 FIXME("not implemented\n");
2497 memset(header, 0, sizeof(MetafileHeader));
2499 return Ok;
2502 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromFile(GDIPCONST WCHAR *filename,
2503 MetafileHeader *header)
2505 static int calls;
2507 TRACE("(%s,%p)\n", debugstr_w(filename), header);
2509 if(!filename || !header)
2510 return InvalidParameter;
2512 if(!(calls++))
2513 FIXME("not implemented\n");
2515 memset(header, 0, sizeof(MetafileHeader));
2517 return Ok;
2520 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromStream(IStream *stream,
2521 MetafileHeader *header)
2523 static int calls;
2525 TRACE("(%p,%p)\n", stream, header);
2527 if(!stream || !header)
2528 return InvalidParameter;
2530 if(!(calls++))
2531 FIXME("not implemented\n");
2533 memset(header, 0, sizeof(MetafileHeader));
2535 return Ok;
2538 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT *num)
2540 TRACE("(%p, %p)\n", image, num);
2542 if (!image || !num) return InvalidParameter;
2544 *num = 0;
2546 if (image->type == ImageTypeBitmap)
2548 if (((GpBitmap *)image)->prop_item)
2550 *num = ((GpBitmap *)image)->prop_count;
2551 return Ok;
2554 if (((GpBitmap *)image)->metadata_reader)
2555 IWICMetadataReader_GetCount(((GpBitmap *)image)->metadata_reader, num);
2558 return Ok;
2561 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID *list)
2563 HRESULT hr;
2564 IWICMetadataReader *reader;
2565 IWICEnumMetadataItem *enumerator;
2566 UINT prop_count, i, items_returned;
2568 TRACE("(%p, %u, %p)\n", image, num, list);
2570 if (!image || !list) return InvalidParameter;
2572 if (image->type != ImageTypeBitmap)
2574 FIXME("Not implemented for type %d\n", image->type);
2575 return NotImplemented;
2578 if (((GpBitmap *)image)->prop_item)
2580 if (num != ((GpBitmap *)image)->prop_count) return InvalidParameter;
2582 for (i = 0; i < num; i++)
2584 list[i] = ((GpBitmap *)image)->prop_item[i].id;
2587 return Ok;
2590 reader = ((GpBitmap *)image)->metadata_reader;
2591 if (!reader)
2593 if (num != 0) return InvalidParameter;
2594 return Ok;
2597 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2598 if (FAILED(hr)) return hresult_to_status(hr);
2600 if (num != prop_count) return InvalidParameter;
2602 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2603 if (FAILED(hr)) return hresult_to_status(hr);
2605 IWICEnumMetadataItem_Reset(enumerator);
2607 for (i = 0; i < num; i++)
2609 PROPVARIANT id;
2611 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, NULL, &items_returned);
2612 if (hr != S_OK) break;
2614 if (id.vt != VT_UI2)
2616 FIXME("not supported propvariant type for id: %u\n", id.vt);
2617 list[i] = 0;
2618 continue;
2620 list[i] = id.u.uiVal;
2623 IWICEnumMetadataItem_Release(enumerator);
2625 return hr == S_OK ? Ok : hresult_to_status(hr);
2628 static UINT propvariant_size(PROPVARIANT *value)
2630 switch (value->vt & ~VT_VECTOR)
2632 case VT_EMPTY:
2633 return 0;
2634 case VT_I1:
2635 case VT_UI1:
2636 if (!(value->vt & VT_VECTOR)) return 1;
2637 return value->u.caub.cElems;
2638 case VT_I2:
2639 case VT_UI2:
2640 if (!(value->vt & VT_VECTOR)) return 2;
2641 return value->u.caui.cElems * 2;
2642 case VT_I4:
2643 case VT_UI4:
2644 case VT_R4:
2645 if (!(value->vt & VT_VECTOR)) return 4;
2646 return value->u.caul.cElems * 4;
2647 case VT_I8:
2648 case VT_UI8:
2649 case VT_R8:
2650 if (!(value->vt & VT_VECTOR)) return 8;
2651 return value->u.cauh.cElems * 8;
2652 case VT_LPSTR:
2653 return value->u.pszVal ? strlen(value->u.pszVal) + 1 : 0;
2654 case VT_BLOB:
2655 return value->u.blob.cbSize;
2656 default:
2657 FIXME("not supported variant type %d\n", value->vt);
2658 return 0;
2662 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID propid, UINT *size)
2664 HRESULT hr;
2665 IWICMetadataReader *reader;
2666 PROPVARIANT id, value;
2668 TRACE("(%p,%#x,%p)\n", image, propid, size);
2670 if (!size || !image) return InvalidParameter;
2672 if (image->type != ImageTypeBitmap)
2674 FIXME("Not implemented for type %d\n", image->type);
2675 return NotImplemented;
2678 if (((GpBitmap *)image)->prop_item)
2680 UINT i;
2682 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2684 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2686 *size = sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2687 return Ok;
2691 return PropertyNotFound;
2694 reader = ((GpBitmap *)image)->metadata_reader;
2695 if (!reader) return PropertyNotFound;
2697 id.vt = VT_UI2;
2698 id.u.uiVal = propid;
2699 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2700 if (FAILED(hr)) return PropertyNotFound;
2702 *size = propvariant_size(&value);
2703 if (*size) *size += sizeof(PropertyItem);
2704 PropVariantClear(&value);
2706 return Ok;
2709 #ifndef PropertyTagTypeSByte
2710 #define PropertyTagTypeSByte 6
2711 #define PropertyTagTypeSShort 8
2712 #define PropertyTagTypeFloat 11
2713 #define PropertyTagTypeDouble 12
2714 #endif
2716 static UINT vt_to_itemtype(UINT vt)
2718 static const struct
2720 UINT vt, type;
2721 } vt2type[] =
2723 { VT_I1, PropertyTagTypeSByte },
2724 { VT_UI1, PropertyTagTypeByte },
2725 { VT_I2, PropertyTagTypeSShort },
2726 { VT_UI2, PropertyTagTypeShort },
2727 { VT_I4, PropertyTagTypeSLONG },
2728 { VT_UI4, PropertyTagTypeLong },
2729 { VT_I8, PropertyTagTypeSRational },
2730 { VT_UI8, PropertyTagTypeRational },
2731 { VT_R4, PropertyTagTypeFloat },
2732 { VT_R8, PropertyTagTypeDouble },
2733 { VT_LPSTR, PropertyTagTypeASCII },
2734 { VT_BLOB, PropertyTagTypeUndefined }
2736 UINT i;
2737 for (i = 0; i < sizeof(vt2type)/sizeof(vt2type[0]); i++)
2739 if (vt2type[i].vt == vt) return vt2type[i].type;
2741 FIXME("not supported variant type %u\n", vt);
2742 return 0;
2745 static GpStatus propvariant_to_item(PROPVARIANT *value, PropertyItem *item,
2746 UINT size, PROPID id)
2748 UINT item_size, item_type;
2750 item_size = propvariant_size(value);
2751 if (size != item_size + sizeof(PropertyItem)) return InvalidParameter;
2753 item_type = vt_to_itemtype(value->vt & ~VT_VECTOR);
2754 if (!item_type) return InvalidParameter;
2756 item->value = item + 1;
2758 switch (value->vt & ~VT_VECTOR)
2760 case VT_I1:
2761 case VT_UI1:
2762 if (!(value->vt & VT_VECTOR))
2763 *(BYTE *)item->value = value->u.bVal;
2764 else
2765 memcpy(item->value, value->u.caub.pElems, item_size);
2766 break;
2767 case VT_I2:
2768 case VT_UI2:
2769 if (!(value->vt & VT_VECTOR))
2770 *(USHORT *)item->value = value->u.uiVal;
2771 else
2772 memcpy(item->value, value->u.caui.pElems, item_size);
2773 break;
2774 case VT_I4:
2775 case VT_UI4:
2776 case VT_R4:
2777 if (!(value->vt & VT_VECTOR))
2778 *(ULONG *)item->value = value->u.ulVal;
2779 else
2780 memcpy(item->value, value->u.caul.pElems, item_size);
2781 break;
2782 case VT_I8:
2783 case VT_UI8:
2784 case VT_R8:
2785 if (!(value->vt & VT_VECTOR))
2786 *(ULONGLONG *)item->value = value->u.uhVal.QuadPart;
2787 else
2788 memcpy(item->value, value->u.cauh.pElems, item_size);
2789 break;
2790 case VT_LPSTR:
2791 memcpy(item->value, value->u.pszVal, item_size);
2792 break;
2793 case VT_BLOB:
2794 memcpy(item->value, value->u.blob.pBlobData, item_size);
2795 break;
2796 default:
2797 FIXME("not supported variant type %d\n", value->vt);
2798 return InvalidParameter;
2801 item->length = item_size;
2802 item->type = item_type;
2803 item->id = id;
2805 return Ok;
2808 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID propid, UINT size,
2809 PropertyItem *buffer)
2811 GpStatus stat;
2812 HRESULT hr;
2813 IWICMetadataReader *reader;
2814 PROPVARIANT id, value;
2816 TRACE("(%p,%#x,%u,%p)\n", image, propid, size, buffer);
2818 if (!image || !buffer) return InvalidParameter;
2820 if (image->type != ImageTypeBitmap)
2822 FIXME("Not implemented for type %d\n", image->type);
2823 return NotImplemented;
2826 if (((GpBitmap *)image)->prop_item)
2828 UINT i;
2830 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2832 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2834 if (size != sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length)
2835 return InvalidParameter;
2837 *buffer = ((GpBitmap *)image)->prop_item[i];
2838 buffer->value = buffer + 1;
2839 memcpy(buffer->value, ((GpBitmap *)image)->prop_item[i].value, buffer->length);
2840 return Ok;
2844 return PropertyNotFound;
2847 reader = ((GpBitmap *)image)->metadata_reader;
2848 if (!reader) return PropertyNotFound;
2850 id.vt = VT_UI2;
2851 id.u.uiVal = propid;
2852 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2853 if (FAILED(hr)) return PropertyNotFound;
2855 stat = propvariant_to_item(&value, buffer, size, propid);
2856 PropVariantClear(&value);
2858 return stat;
2861 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT *size, UINT *count)
2863 HRESULT hr;
2864 IWICMetadataReader *reader;
2865 IWICEnumMetadataItem *enumerator;
2866 UINT prop_count, prop_size, i;
2867 PROPVARIANT id, value;
2869 TRACE("(%p,%p,%p)\n", image, size, count);
2871 if (!image || !size || !count) return InvalidParameter;
2873 if (image->type != ImageTypeBitmap)
2875 FIXME("Not implemented for type %d\n", image->type);
2876 return NotImplemented;
2879 if (((GpBitmap *)image)->prop_item)
2881 *count = ((GpBitmap *)image)->prop_count;
2882 *size = 0;
2884 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2886 *size += sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2889 return Ok;
2892 reader = ((GpBitmap *)image)->metadata_reader;
2893 if (!reader) return PropertyNotFound;
2895 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2896 if (FAILED(hr)) return hresult_to_status(hr);
2898 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2899 if (FAILED(hr)) return hresult_to_status(hr);
2901 IWICEnumMetadataItem_Reset(enumerator);
2903 prop_size = 0;
2905 PropVariantInit(&id);
2906 PropVariantInit(&value);
2908 for (i = 0; i < prop_count; i++)
2910 UINT items_returned, item_size;
2912 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2913 if (hr != S_OK) break;
2915 item_size = propvariant_size(&value);
2916 if (item_size) prop_size += sizeof(PropertyItem) + item_size;
2918 PropVariantClear(&id);
2919 PropVariantClear(&value);
2922 IWICEnumMetadataItem_Release(enumerator);
2924 if (hr != S_OK) return PropertyNotFound;
2926 *count = prop_count;
2927 *size = prop_size;
2928 return Ok;
2931 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2932 UINT count, PropertyItem *buf)
2934 GpStatus status;
2935 HRESULT hr;
2936 IWICMetadataReader *reader;
2937 IWICEnumMetadataItem *enumerator;
2938 UINT prop_count, prop_size, i;
2939 PROPVARIANT id, value;
2940 char *item_value;
2942 TRACE("(%p,%u,%u,%p)\n", image, size, count, buf);
2944 if (!image || !buf) return InvalidParameter;
2946 if (image->type != ImageTypeBitmap)
2948 FIXME("Not implemented for type %d\n", image->type);
2949 return NotImplemented;
2952 status = GdipGetPropertySize(image, &prop_size, &prop_count);
2953 if (status != Ok) return status;
2955 if (prop_count != count || prop_size != size) return InvalidParameter;
2957 if (((GpBitmap *)image)->prop_item)
2959 memcpy(buf, ((GpBitmap *)image)->prop_item, prop_size);
2961 item_value = (char *)(buf + prop_count);
2963 for (i = 0; i < prop_count; i++)
2965 buf[i].value = item_value;
2966 item_value += buf[i].length;
2969 return Ok;
2972 reader = ((GpBitmap *)image)->metadata_reader;
2973 if (!reader) return PropertyNotFound;
2975 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2976 if (FAILED(hr)) return hresult_to_status(hr);
2978 IWICEnumMetadataItem_Reset(enumerator);
2980 item_value = (char *)(buf + prop_count);
2982 PropVariantInit(&id);
2983 PropVariantInit(&value);
2985 for (i = 0; i < prop_count; i++)
2987 PropertyItem *item;
2988 UINT items_returned, item_size;
2990 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2991 if (hr != S_OK) break;
2993 if (id.vt != VT_UI2)
2995 FIXME("not supported propvariant type for id: %u\n", id.vt);
2996 continue;
2999 item_size = propvariant_size(&value);
3000 if (item_size)
3002 item = HeapAlloc(GetProcessHeap(), 0, item_size + sizeof(*item));
3004 propvariant_to_item(&value, item, item_size + sizeof(*item), id.u.uiVal);
3005 buf[i].id = item->id;
3006 buf[i].type = item->type;
3007 buf[i].length = item_size;
3008 buf[i].value = item_value;
3009 memcpy(item_value, item->value, item_size);
3010 item_value += item_size;
3012 HeapFree(GetProcessHeap(), 0, item);
3015 PropVariantClear(&id);
3016 PropVariantClear(&value);
3019 IWICEnumMetadataItem_Release(enumerator);
3021 if (hr != S_OK) return PropertyNotFound;
3023 return Ok;
3026 struct image_format_dimension
3028 const GUID *format;
3029 const GUID *dimension;
3032 static const struct image_format_dimension image_format_dimensions[] =
3034 {&ImageFormatGIF, &FrameDimensionTime},
3035 {&ImageFormatIcon, &FrameDimensionResolution},
3036 {NULL}
3039 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
3040 GDIPCONST GUID* dimensionID, UINT* count)
3042 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
3044 if(!image || !count)
3045 return InvalidParameter;
3047 if (!dimensionID ||
3048 IsEqualGUID(dimensionID, &image->format) ||
3049 IsEqualGUID(dimensionID, &FrameDimensionPage) ||
3050 IsEqualGUID(dimensionID, &FrameDimensionTime))
3052 *count = image->frame_count;
3053 return Ok;
3056 return InvalidParameter;
3059 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
3060 UINT* count)
3062 TRACE("(%p, %p)\n", image, count);
3064 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
3066 if(!image || !count)
3067 return InvalidParameter;
3069 *count = 1;
3071 return Ok;
3074 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
3075 GUID* dimensionIDs, UINT count)
3077 int i;
3078 const GUID *result=NULL;
3080 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
3082 if(!image || !dimensionIDs || count != 1)
3083 return InvalidParameter;
3085 for (i=0; image_format_dimensions[i].format; i++)
3087 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
3089 result = image_format_dimensions[i].dimension;
3090 break;
3094 if (!result)
3095 result = &FrameDimensionPage;
3097 memcpy(dimensionIDs, result, sizeof(GUID));
3099 return Ok;
3102 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
3103 GpImage **image)
3105 GpStatus stat;
3106 IStream *stream;
3108 TRACE("(%s) %p\n", debugstr_w(filename), image);
3110 if (!filename || !image)
3111 return InvalidParameter;
3113 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
3115 if (stat != Ok)
3116 return stat;
3118 stat = GdipLoadImageFromStream(stream, image);
3120 IStream_Release(stream);
3122 return stat;
3125 /* FIXME: no icm handling */
3126 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
3128 TRACE("(%s) %p\n", debugstr_w(filename), image);
3130 return GdipLoadImageFromFile(filename, image);
3133 static void add_property(GpBitmap *bitmap, PropertyItem *item)
3135 UINT prop_size, prop_count;
3136 PropertyItem *prop_item;
3138 if (bitmap->prop_item == NULL)
3140 prop_size = prop_count = 0;
3141 prop_item = GdipAlloc(item->length + sizeof(PropertyItem));
3142 if (!prop_item) return;
3144 else
3146 UINT i;
3147 char *item_value;
3149 GdipGetPropertySize((GpImage *)bitmap, &prop_size, &prop_count);
3151 prop_item = GdipAlloc(prop_size + item->length + sizeof(PropertyItem));
3152 if (!prop_item) return;
3153 memcpy(prop_item, bitmap->prop_item, sizeof(PropertyItem) * bitmap->prop_count);
3154 prop_size -= sizeof(PropertyItem) * bitmap->prop_count;
3155 memcpy(prop_item + prop_count + 1, bitmap->prop_item + prop_count, prop_size);
3157 item_value = (char *)(prop_item + prop_count + 1);
3159 for (i = 0; i < prop_count; i++)
3161 prop_item[i].value = item_value;
3162 item_value += prop_item[i].length;
3166 prop_item[prop_count].id = item->id;
3167 prop_item[prop_count].type = item->type;
3168 prop_item[prop_count].length = item->length;
3169 prop_item[prop_count].value = (char *)(prop_item + prop_count + 1) + prop_size;
3170 memcpy(prop_item[prop_count].value, item->value, item->length);
3172 GdipFree(bitmap->prop_item);
3173 bitmap->prop_item = prop_item;
3174 bitmap->prop_count++;
3177 static BOOL get_bool_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3179 HRESULT hr;
3180 GUID format;
3181 PROPVARIANT id, value;
3182 BOOL ret = FALSE;
3184 IWICMetadataReader_GetMetadataFormat(reader, &format);
3185 if (!IsEqualGUID(&format, guid)) return FALSE;
3187 PropVariantInit(&id);
3188 PropVariantInit(&value);
3190 id.vt = VT_LPWSTR;
3191 id.u.pwszVal = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3192 if (!id.u.pwszVal) return FALSE;
3193 lstrcpyW(id.u.pwszVal, prop_name);
3194 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3195 if (hr == S_OK && value.vt == VT_BOOL)
3196 ret = value.u.boolVal;
3198 PropVariantClear(&id);
3199 PropVariantClear(&value);
3201 return ret;
3204 static PropertyItem *get_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3206 HRESULT hr;
3207 GUID format;
3208 PROPVARIANT id, value;
3209 PropertyItem *item = NULL;
3211 IWICMetadataReader_GetMetadataFormat(reader, &format);
3212 if (!IsEqualGUID(&format, guid)) return NULL;
3214 PropVariantInit(&id);
3215 PropVariantInit(&value);
3217 id.vt = VT_LPWSTR;
3218 id.u.pwszVal = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3219 if (!id.u.pwszVal) return NULL;
3220 lstrcpyW(id.u.pwszVal, prop_name);
3221 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3222 if (hr == S_OK)
3224 UINT item_size = propvariant_size(&value);
3225 if (item_size)
3227 item_size += sizeof(*item);
3228 item = GdipAlloc(item_size);
3229 if (propvariant_to_item(&value, item, item_size, 0) != Ok)
3231 GdipFree(item);
3232 item = NULL;
3237 PropVariantClear(&id);
3238 PropVariantClear(&value);
3240 return item;
3243 static PropertyItem *get_gif_comment(IWICMetadataReader *reader)
3245 static const WCHAR textentryW[] = { 'T','e','x','t','E','n','t','r','y',0 };
3246 PropertyItem *comment;
3248 comment = get_property(reader, &GUID_MetadataFormatGifComment, textentryW);
3249 if (comment)
3250 comment->id = PropertyTagExifUserComment;
3252 return comment;
3255 static PropertyItem *get_gif_loopcount(IWICMetadataReader *reader)
3257 static const WCHAR applicationW[] = { 'A','p','p','l','i','c','a','t','i','o','n',0 };
3258 static const WCHAR dataW[] = { 'D','a','t','a',0 };
3259 PropertyItem *appext = NULL, *appdata = NULL, *loop = NULL;
3261 appext = get_property(reader, &GUID_MetadataFormatAPE, applicationW);
3262 if (appext)
3264 if (appext->type == PropertyTagTypeByte && appext->length == 11 &&
3265 (!memcmp(appext->value, "NETSCAPE2.0", 11) || !memcmp(appext->value, "ANIMEXTS1.0", 11)))
3267 appdata = get_property(reader, &GUID_MetadataFormatAPE, dataW);
3268 if (appdata)
3270 if (appdata->type == PropertyTagTypeByte && appdata->length == 4)
3272 BYTE *data = appdata->value;
3273 if (data[0] == 3 && data[1] == 1)
3275 loop = GdipAlloc(sizeof(*loop) + sizeof(SHORT));
3276 if (loop)
3278 loop->type = PropertyTagTypeShort;
3279 loop->id = PropertyTagLoopCount;
3280 loop->length = sizeof(SHORT);
3281 loop->value = loop + 1;
3282 *(SHORT *)loop->value = data[2] | (data[3] << 8);
3290 GdipFree(appext);
3291 GdipFree(appdata);
3293 return loop;
3296 static PropertyItem *get_gif_background(IWICMetadataReader *reader)
3298 static const WCHAR backgroundW[] = { 'B','a','c','k','g','r','o','u','n','d','C','o','l','o','r','I','n','d','e','x',0 };
3299 PropertyItem *background;
3301 background = get_property(reader, &GUID_MetadataFormatLSD, backgroundW);
3302 if (background)
3303 background->id = PropertyTagIndexBackground;
3305 return background;
3308 static PropertyItem *get_gif_palette(IWICBitmapDecoder *decoder, IWICMetadataReader *reader)
3310 static const WCHAR global_flagW[] = { 'G','l','o','b','a','l','C','o','l','o','r','T','a','b','l','e','F','l','a','g',0 };
3311 HRESULT hr;
3312 IWICImagingFactory *factory;
3313 IWICPalette *palette;
3314 UINT count = 0;
3315 WICColor colors[256];
3317 if (!get_bool_property(reader, &GUID_MetadataFormatLSD, global_flagW))
3318 return NULL;
3320 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
3321 &IID_IWICImagingFactory, (void **)&factory);
3322 if (hr != S_OK) return NULL;
3324 hr = IWICImagingFactory_CreatePalette(factory, &palette);
3325 if (hr == S_OK)
3327 hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
3328 if (hr == S_OK)
3329 IWICPalette_GetColors(palette, 256, colors, &count);
3331 IWICPalette_Release(palette);
3334 IWICImagingFactory_Release(factory);
3336 if (count)
3338 PropertyItem *pal;
3339 UINT i;
3340 BYTE *rgb;
3342 pal = GdipAlloc(sizeof(*pal) + count * 3);
3343 if (!pal) return NULL;
3344 pal->type = PropertyTagTypeByte;
3345 pal->id = PropertyTagGlobalPalette;
3346 pal->value = pal + 1;
3347 pal->length = count * 3;
3349 rgb = pal->value;
3351 for (i = 0; i < count; i++)
3353 rgb[i*3] = (colors[i] >> 16) & 0xff;
3354 rgb[i*3 + 1] = (colors[i] >> 8) & 0xff;
3355 rgb[i*3 + 2] = colors[i] & 0xff;
3358 return pal;
3361 return NULL;
3364 static PropertyItem *get_gif_transparent_idx(IWICMetadataReader *reader)
3366 static const WCHAR transparency_flagW[] = { 'T','r','a','n','s','p','a','r','e','n','c','y','F','l','a','g',0 };
3367 static const WCHAR colorW[] = { 'T','r','a','n','s','p','a','r','e','n','t','C','o','l','o','r','I','n','d','e','x',0 };
3368 PropertyItem *index = NULL;
3370 if (get_bool_property(reader, &GUID_MetadataFormatGCE, transparency_flagW))
3372 index = get_property(reader, &GUID_MetadataFormatGCE, colorW);
3373 if (index)
3374 index->id = PropertyTagIndexTransparent;
3376 return index;
3379 static LONG get_gif_frame_delay(IWICBitmapFrameDecode *frame)
3381 static const WCHAR delayW[] = { 'D','e','l','a','y',0 };
3382 HRESULT hr;
3383 IWICMetadataBlockReader *block_reader;
3384 IWICMetadataReader *reader;
3385 UINT block_count, i;
3386 PropertyItem *delay;
3387 LONG value = 0;
3389 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3390 if (hr == S_OK)
3392 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3393 if (hr == S_OK)
3395 for (i = 0; i < block_count; i++)
3397 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3398 if (hr == S_OK)
3400 delay = get_property(reader, &GUID_MetadataFormatGCE, delayW);
3401 if (delay)
3403 if (delay->type == PropertyTagTypeShort && delay->length == 2)
3404 value = *(SHORT *)delay->value;
3406 GdipFree(delay);
3408 IWICMetadataReader_Release(reader);
3412 IWICMetadataBlockReader_Release(block_reader);
3415 return value;
3418 static void gif_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3420 HRESULT hr;
3421 IWICBitmapFrameDecode *frame;
3422 IWICMetadataBlockReader *block_reader;
3423 IWICMetadataReader *reader;
3424 UINT frame_count, block_count, i;
3425 PropertyItem *delay = NULL, *comment = NULL, *background = NULL;
3426 PropertyItem *transparent_idx = NULL, *loop = NULL, *palette = NULL;
3428 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3429 if (frame_count > 1)
3431 delay = GdipAlloc(sizeof(*delay) + frame_count * sizeof(LONG));
3432 if (delay)
3434 LONG *value;
3436 delay->type = PropertyTagTypeLong;
3437 delay->id = PropertyTagFrameDelay;
3438 delay->length = frame_count * sizeof(LONG);
3439 delay->value = delay + 1;
3441 value = delay->value;
3443 for (i = 0; i < frame_count; i++)
3445 hr = IWICBitmapDecoder_GetFrame(decoder, i, &frame);
3446 if (hr == S_OK)
3448 value[i] = get_gif_frame_delay(frame);
3449 IWICBitmapFrameDecode_Release(frame);
3451 else value[i] = 0;
3456 hr = IWICBitmapDecoder_QueryInterface(decoder, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3457 if (hr == S_OK)
3459 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3460 if (hr == S_OK)
3462 for (i = 0; i < block_count; i++)
3464 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3465 if (hr == S_OK)
3467 if (!comment)
3468 comment = get_gif_comment(reader);
3470 if (frame_count > 1 && !loop)
3471 loop = get_gif_loopcount(reader);
3473 if (!background)
3474 background = get_gif_background(reader);
3476 if (!palette)
3477 palette = get_gif_palette(decoder, reader);
3479 IWICMetadataReader_Release(reader);
3483 IWICMetadataBlockReader_Release(block_reader);
3486 if (frame_count > 1 && !loop)
3488 loop = GdipAlloc(sizeof(*loop) + sizeof(SHORT));
3489 if (loop)
3491 loop->type = PropertyTagTypeShort;
3492 loop->id = PropertyTagLoopCount;
3493 loop->length = sizeof(SHORT);
3494 loop->value = loop + 1;
3495 *(SHORT *)loop->value = 1;
3499 if (delay) add_property(bitmap, delay);
3500 if (comment) add_property(bitmap, comment);
3501 if (loop) add_property(bitmap, loop);
3502 if (palette) add_property(bitmap, palette);
3503 if (background) add_property(bitmap, background);
3505 GdipFree(delay);
3506 GdipFree(comment);
3507 GdipFree(loop);
3508 GdipFree(palette);
3509 GdipFree(background);
3511 /* Win7 gdiplus always returns transparent color index from frame 0 */
3512 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
3513 if (hr != S_OK) return;
3515 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3516 if (hr == S_OK)
3518 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3519 if (hr == S_OK)
3521 for (i = 0; i < block_count; i++)
3523 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3524 if (hr == S_OK)
3526 if (!transparent_idx)
3527 transparent_idx = get_gif_transparent_idx(reader);
3529 IWICMetadataReader_Release(reader);
3533 IWICMetadataBlockReader_Release(block_reader);
3536 if (transparent_idx) add_property(bitmap, transparent_idx);
3537 GdipFree(transparent_idx);
3539 IWICBitmapFrameDecode_Release(frame);
3542 typedef void (*metadata_reader_func)(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT frame);
3544 static GpStatus decode_image_wic(IStream *stream, GDIPCONST CLSID *clsid,
3545 UINT active_frame, metadata_reader_func metadata_reader, GpImage **image)
3547 GpStatus status=Ok;
3548 GpBitmap *bitmap;
3549 HRESULT hr;
3550 IWICBitmapDecoder *decoder;
3551 IWICBitmapFrameDecode *frame;
3552 IWICBitmapSource *source=NULL;
3553 IWICMetadataBlockReader *block_reader;
3554 WICPixelFormatGUID wic_format;
3555 PixelFormat gdip_format=0;
3556 ColorPalette *palette = NULL;
3557 WICBitmapPaletteType palette_type = WICBitmapPaletteTypeFixedHalftone256;
3558 int i;
3559 UINT width, height, frame_count;
3560 BitmapData lockeddata;
3561 WICRect wrc;
3562 HRESULT initresult;
3564 TRACE("%p,%s,%u,%p\n", stream, wine_dbgstr_guid(clsid), active_frame, image);
3566 initresult = CoInitialize(NULL);
3568 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
3569 &IID_IWICBitmapDecoder, (void**)&decoder);
3570 if (FAILED(hr)) goto end;
3572 hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
3573 if (SUCCEEDED(hr))
3575 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3576 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3579 if (SUCCEEDED(hr)) /* got frame */
3581 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
3583 if (SUCCEEDED(hr))
3585 IWICBitmapSource *bmp_source;
3586 IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICBitmapSource, (void **)&bmp_source);
3588 for (i=0; pixel_formats[i].wic_format; i++)
3590 if (IsEqualGUID(&wic_format, pixel_formats[i].wic_format))
3592 source = bmp_source;
3593 gdip_format = pixel_formats[i].gdip_format;
3594 palette_type = pixel_formats[i].palette_type;
3595 break;
3598 if (!source)
3600 /* unknown format; fall back on 32bppARGB */
3601 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, bmp_source, &source);
3602 gdip_format = PixelFormat32bppARGB;
3603 IWICBitmapSource_Release(bmp_source);
3605 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format), gdip_format);
3608 if (SUCCEEDED(hr)) /* got source */
3610 hr = IWICBitmapSource_GetSize(source, &width, &height);
3612 if (SUCCEEDED(hr))
3613 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
3614 NULL, &bitmap);
3616 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
3618 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
3619 gdip_format, &lockeddata);
3620 if (status == Ok) /* locked bitmap */
3622 wrc.X = 0;
3623 wrc.Width = width;
3624 wrc.Height = 1;
3625 for (i=0; i<height; i++)
3627 wrc.Y = i;
3628 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
3629 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
3630 if (FAILED(hr)) break;
3633 GdipBitmapUnlockBits(bitmap, &lockeddata);
3636 if (SUCCEEDED(hr) && status == Ok)
3637 *image = (GpImage*)bitmap;
3638 else
3640 *image = NULL;
3641 GdipDisposeImage((GpImage*)bitmap);
3644 if (SUCCEEDED(hr) && status == Ok)
3646 double dpix, dpiy;
3647 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
3648 if (SUCCEEDED(hr))
3650 bitmap->image.xres = dpix;
3651 bitmap->image.yres = dpiy;
3653 hr = S_OK;
3657 IWICBitmapSource_Release(source);
3660 if (SUCCEEDED(hr)) {
3661 bitmap->metadata_reader = NULL;
3663 if (metadata_reader)
3664 metadata_reader(bitmap, decoder, active_frame);
3665 else if (IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader) == S_OK)
3667 UINT block_count = 0;
3668 if (IWICMetadataBlockReader_GetCount(block_reader, &block_count) == S_OK && block_count)
3669 IWICMetadataBlockReader_GetReaderByIndex(block_reader, 0, &bitmap->metadata_reader);
3670 IWICMetadataBlockReader_Release(block_reader);
3673 palette = get_palette(frame, palette_type);
3674 IWICBitmapFrameDecode_Release(frame);
3678 IWICBitmapDecoder_Release(decoder);
3680 end:
3681 if (SUCCEEDED(initresult)) CoUninitialize();
3683 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
3685 if (status == Ok)
3687 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3688 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI|ImageFlagsColorSpaceRGB;
3689 bitmap->image.frame_count = frame_count;
3690 bitmap->image.current_frame = active_frame;
3691 bitmap->image.stream = stream;
3692 if (palette)
3694 GdipFree(bitmap->image.palette);
3695 bitmap->image.palette = palette;
3697 else
3699 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormatBlackWhite))
3700 bitmap->image.palette->Flags = 0;
3702 /* Pin the source stream */
3703 IStream_AddRef(stream);
3704 TRACE("=> %p\n", *image);
3707 return status;
3710 static GpStatus decode_image_icon(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3712 return decode_image_wic(stream, &CLSID_WICIcoDecoder, active_frame, NULL, image);
3715 static GpStatus decode_image_bmp(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3717 GpStatus status;
3718 GpBitmap* bitmap;
3720 status = decode_image_wic(stream, &CLSID_WICBmpDecoder, active_frame, NULL, image);
3722 bitmap = (GpBitmap*)*image;
3724 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
3726 /* WIC supports bmp files with alpha, but gdiplus does not */
3727 bitmap->format = PixelFormat32bppRGB;
3730 return status;
3733 static GpStatus decode_image_jpeg(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3735 return decode_image_wic(stream, &CLSID_WICJpegDecoder, active_frame, NULL, image);
3738 static GpStatus decode_image_png(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3740 return decode_image_wic(stream, &CLSID_WICPngDecoder, active_frame, NULL, image);
3743 static GpStatus decode_image_gif(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3745 return decode_image_wic(stream, &CLSID_WICGifDecoder, active_frame, gif_metadata_reader, image);
3748 static GpStatus decode_image_tiff(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3750 return decode_image_wic(stream, &CLSID_WICTiffDecoder, active_frame, NULL, image);
3753 static GpStatus decode_image_olepicture_metafile(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3755 IPicture *pic;
3757 TRACE("%p %p\n", stream, image);
3759 if(!stream || !image)
3760 return InvalidParameter;
3762 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
3763 (LPVOID*) &pic) != S_OK){
3764 TRACE("Could not load picture\n");
3765 return GenericError;
3768 /* FIXME: missing initialization code */
3769 *image = GdipAlloc(sizeof(GpMetafile));
3770 if(!*image) return OutOfMemory;
3771 (*image)->type = ImageTypeMetafile;
3772 (*image)->stream = NULL;
3773 (*image)->picture = pic;
3774 (*image)->flags = ImageFlagsNone;
3775 (*image)->frame_count = 1;
3776 (*image)->current_frame = 0;
3777 (*image)->palette = NULL;
3779 TRACE("<-- %p\n", *image);
3781 return Ok;
3784 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
3785 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
3787 typedef GpStatus (*decode_image_func)(IStream *stream, REFCLSID clsid, UINT active_frame, GpImage **image);
3789 typedef struct image_codec {
3790 ImageCodecInfo info;
3791 encode_image_func encode_func;
3792 decode_image_func decode_func;
3793 } image_codec;
3795 typedef enum {
3796 BMP,
3797 JPEG,
3798 GIF,
3799 TIFF,
3800 EMF,
3801 WMF,
3802 PNG,
3803 ICO,
3804 NUM_CODECS
3805 } ImageFormat;
3807 static const struct image_codec codecs[NUM_CODECS];
3809 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
3811 BYTE signature[8];
3812 const BYTE *pattern, *mask;
3813 LARGE_INTEGER seek;
3814 HRESULT hr;
3815 UINT bytesread;
3816 int i, j, sig;
3818 /* seek to the start of the stream */
3819 seek.QuadPart = 0;
3820 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3821 if (FAILED(hr)) return hresult_to_status(hr);
3823 /* read the first 8 bytes */
3824 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
3825 hr = IStream_Read(stream, signature, 8, &bytesread);
3826 if (FAILED(hr)) return hresult_to_status(hr);
3827 if (hr == S_FALSE || bytesread == 0) return GenericError;
3829 for (i = 0; i < NUM_CODECS; i++) {
3830 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
3831 bytesread >= codecs[i].info.SigSize)
3833 for (sig=0; sig<codecs[i].info.SigCount; sig++)
3835 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
3836 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
3837 for (j=0; j<codecs[i].info.SigSize; j++)
3838 if ((signature[j] & mask[j]) != pattern[j])
3839 break;
3840 if (j == codecs[i].info.SigSize)
3842 *result = &codecs[i];
3843 return Ok;
3849 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
3850 signature[0],signature[1],signature[2],signature[3],
3851 signature[4],signature[5],signature[6],signature[7]);
3853 return GenericError;
3856 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image, GDIPCONST GUID *dimensionID,
3857 UINT frame)
3859 GpStatus stat;
3860 LARGE_INTEGER seek;
3861 HRESULT hr;
3862 const struct image_codec *codec = NULL;
3863 GpImage *new_image;
3865 TRACE("(%p,%s,%u)\n", image, debugstr_guid(dimensionID), frame);
3867 if (!image || !dimensionID)
3868 return InvalidParameter;
3870 if (frame >= image->frame_count)
3872 WARN("requested frame %u, but image has only %u\n", frame, image->frame_count);
3873 return InvalidParameter;
3876 if (image->type != ImageTypeBitmap && image->type != ImageTypeMetafile)
3878 WARN("invalid image type %d\n", image->type);
3879 return InvalidParameter;
3882 if (image->current_frame == frame)
3883 return Ok;
3885 if (!image->stream)
3887 TRACE("image doesn't have an associated stream\n");
3888 return Ok;
3891 /* choose an appropriate image decoder */
3892 stat = get_decoder_info(image->stream, &codec);
3893 if (stat != Ok)
3895 WARN("can't find decoder info\n");
3896 return stat;
3899 /* seek to the start of the stream */
3900 seek.QuadPart = 0;
3901 hr = IStream_Seek(image->stream, seek, STREAM_SEEK_SET, NULL);
3902 if (FAILED(hr))
3903 return hresult_to_status(hr);
3905 /* call on the image decoder to do the real work */
3906 stat = codec->decode_func(image->stream, &codec->info.Clsid, frame, &new_image);
3908 if (stat == Ok)
3910 memcpy(&new_image->format, &codec->info.FormatID, sizeof(GUID));
3911 free_image_data(image);
3912 if (image->type == ImageTypeBitmap)
3913 *(GpBitmap *)image = *(GpBitmap *)new_image;
3914 else if (image->type == ImageTypeMetafile)
3915 *(GpMetafile *)image = *(GpMetafile *)new_image;
3916 new_image->type = ~0;
3917 GdipFree(new_image);
3918 return Ok;
3921 return stat;
3924 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream *stream, GpImage **image)
3926 GpStatus stat;
3927 LARGE_INTEGER seek;
3928 HRESULT hr;
3929 const struct image_codec *codec=NULL;
3931 /* choose an appropriate image decoder */
3932 stat = get_decoder_info(stream, &codec);
3933 if (stat != Ok) return stat;
3935 /* seek to the start of the stream */
3936 seek.QuadPart = 0;
3937 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3938 if (FAILED(hr)) return hresult_to_status(hr);
3940 /* call on the image decoder to do the real work */
3941 stat = codec->decode_func(stream, &codec->info.Clsid, 0, image);
3943 /* take note of the original data format */
3944 if (stat == Ok)
3946 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
3947 return Ok;
3950 return stat;
3953 /* FIXME: no ICM */
3954 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
3956 TRACE("%p %p\n", stream, image);
3958 return GdipLoadImageFromStream(stream, image);
3961 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
3963 static int calls;
3965 TRACE("(%p,%u)\n", image, propId);
3967 if(!image)
3968 return InvalidParameter;
3970 if(!(calls++))
3971 FIXME("not implemented\n");
3973 return NotImplemented;
3976 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
3978 static int calls;
3980 if (!image || !item) return InvalidParameter;
3982 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image, item, item->id, item->type, item->length, item->value);
3984 if(!(calls++))
3985 FIXME("not implemented\n");
3987 return Ok;
3990 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
3991 GDIPCONST CLSID *clsidEncoder,
3992 GDIPCONST EncoderParameters *encoderParams)
3994 GpStatus stat;
3995 IStream *stream;
3997 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
3999 if (!image || !filename|| !clsidEncoder)
4000 return InvalidParameter;
4002 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
4003 if (stat != Ok)
4004 return GenericError;
4006 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
4008 IStream_Release(stream);
4009 return stat;
4012 /*************************************************************************
4013 * Encoding functions -
4014 * These functions encode an image in different image file formats.
4016 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
4017 #define BITMAP_FORMAT_JPEG 0xd8ff
4018 #define BITMAP_FORMAT_GIF 0x4947
4019 #define BITMAP_FORMAT_PNG 0x5089
4020 #define BITMAP_FORMAT_APM 0xcdd7
4022 static GpStatus encode_image_WIC(GpImage *image, IStream* stream,
4023 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4025 GpStatus stat;
4026 GpBitmap *bitmap;
4027 IWICBitmapEncoder *encoder;
4028 IWICBitmapFrameEncode *frameencode;
4029 IPropertyBag2 *encoderoptions;
4030 HRESULT hr;
4031 UINT width, height;
4032 PixelFormat gdipformat=0;
4033 WICPixelFormatGUID wicformat;
4034 GpRect rc;
4035 BitmapData lockeddata;
4036 HRESULT initresult;
4037 UINT i;
4039 if (image->type != ImageTypeBitmap)
4040 return GenericError;
4042 bitmap = (GpBitmap*)image;
4044 GdipGetImageWidth(image, &width);
4045 GdipGetImageHeight(image, &height);
4047 rc.X = 0;
4048 rc.Y = 0;
4049 rc.Width = width;
4050 rc.Height = height;
4052 initresult = CoInitialize(NULL);
4054 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
4055 &IID_IWICBitmapEncoder, (void**)&encoder);
4056 if (FAILED(hr))
4058 if (SUCCEEDED(initresult)) CoUninitialize();
4059 return hresult_to_status(hr);
4062 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
4064 if (SUCCEEDED(hr))
4066 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
4069 if (SUCCEEDED(hr)) /* created frame */
4071 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
4073 if (SUCCEEDED(hr))
4074 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
4076 if (SUCCEEDED(hr))
4077 hr = IWICBitmapFrameEncode_SetResolution(frameencode, image->xres, image->yres);
4079 if (SUCCEEDED(hr))
4081 for (i=0; pixel_formats[i].wic_format; i++)
4083 if (pixel_formats[i].gdip_format == bitmap->format)
4085 memcpy(&wicformat, pixel_formats[i].wic_format, sizeof(GUID));
4086 gdipformat = bitmap->format;
4087 break;
4090 if (!gdipformat)
4092 memcpy(&wicformat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
4093 gdipformat = PixelFormat32bppARGB;
4096 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
4099 if (SUCCEEDED(hr))
4101 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
4102 &lockeddata);
4104 if (stat == Ok)
4106 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
4107 BYTE *row;
4109 /* write one row at a time in case stride is negative */
4110 row = lockeddata.Scan0;
4111 for (i=0; i<lockeddata.Height; i++)
4113 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
4114 if (FAILED(hr)) break;
4115 row += lockeddata.Stride;
4118 GdipBitmapUnlockBits(bitmap, &lockeddata);
4120 else
4121 hr = E_FAIL;
4124 if (SUCCEEDED(hr))
4125 hr = IWICBitmapFrameEncode_Commit(frameencode);
4127 IWICBitmapFrameEncode_Release(frameencode);
4128 IPropertyBag2_Release(encoderoptions);
4131 if (SUCCEEDED(hr))
4132 hr = IWICBitmapEncoder_Commit(encoder);
4134 IWICBitmapEncoder_Release(encoder);
4136 if (SUCCEEDED(initresult)) CoUninitialize();
4138 return hresult_to_status(hr);
4141 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
4142 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4144 return encode_image_WIC(image, stream, &CLSID_WICBmpEncoder, params);
4147 static GpStatus encode_image_tiff(GpImage *image, IStream* stream,
4148 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4150 return encode_image_WIC(image, stream, &CLSID_WICTiffEncoder, params);
4153 static GpStatus encode_image_png(GpImage *image, IStream* stream,
4154 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4156 return encode_image_WIC(image, stream, &CLSID_WICPngEncoder, params);
4159 static GpStatus encode_image_jpeg(GpImage *image, IStream* stream,
4160 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4162 return encode_image_WIC(image, stream, &CLSID_WICJpegEncoder, params);
4165 /*****************************************************************************
4166 * GdipSaveImageToStream [GDIPLUS.@]
4168 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
4169 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4171 GpStatus stat;
4172 encode_image_func encode_image;
4173 int i;
4175 TRACE("%p %p %p %p\n", image, stream, clsid, params);
4177 if(!image || !stream)
4178 return InvalidParameter;
4180 /* select correct encoder */
4181 encode_image = NULL;
4182 for (i = 0; i < NUM_CODECS; i++) {
4183 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
4184 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
4185 encode_image = codecs[i].encode_func;
4187 if (encode_image == NULL)
4188 return UnknownImageFormat;
4190 stat = encode_image(image, stream, clsid, params);
4192 return stat;
4195 /*****************************************************************************
4196 * GdipSaveAdd [GDIPLUS.@]
4198 GpStatus WINGDIPAPI GdipSaveAdd(GpImage *image, GDIPCONST EncoderParameters *params)
4200 FIXME("(%p,%p): stub\n", image, params);
4201 return Ok;
4204 /*****************************************************************************
4205 * GdipGetImagePalette [GDIPLUS.@]
4207 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
4209 INT count;
4211 TRACE("(%p,%p,%i)\n", image, palette, size);
4213 if (!image || !palette)
4214 return InvalidParameter;
4216 count = image->palette ? image->palette->Count : 0;
4218 if (size < (sizeof(UINT)*2+sizeof(ARGB)*count))
4220 TRACE("<-- InsufficientBuffer\n");
4221 return InsufficientBuffer;
4224 if (image->palette)
4226 palette->Flags = image->palette->Flags;
4227 palette->Count = image->palette->Count;
4228 memcpy(palette->Entries, image->palette->Entries, sizeof(ARGB)*image->palette->Count);
4230 else
4232 palette->Flags = 0;
4233 palette->Count = 0;
4235 return Ok;
4238 /*****************************************************************************
4239 * GdipSetImagePalette [GDIPLUS.@]
4241 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
4242 GDIPCONST ColorPalette *palette)
4244 ColorPalette *new_palette;
4246 TRACE("(%p,%p)\n", image, palette);
4248 if(!image || !palette || palette->Count > 256)
4249 return InvalidParameter;
4251 new_palette = GdipAlloc(2 * sizeof(UINT) + palette->Count * sizeof(ARGB));
4252 if (!new_palette) return OutOfMemory;
4254 GdipFree(image->palette);
4255 image->palette = new_palette;
4256 image->palette->Flags = palette->Flags;
4257 image->palette->Count = palette->Count;
4258 memcpy(image->palette->Entries, palette->Entries, sizeof(ARGB)*palette->Count);
4260 return Ok;
4263 /*************************************************************************
4264 * Encoders -
4265 * Structures that represent which formats we support for encoding.
4268 /* ImageCodecInfo creation routines taken from libgdiplus */
4269 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
4270 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
4271 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
4272 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
4273 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
4274 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
4276 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
4277 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
4278 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
4279 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
4280 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
4281 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
4283 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
4284 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
4285 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
4286 static const WCHAR gif_format[] = {'G','I','F',0};
4287 static const BYTE gif_sig_pattern[12] = "GIF87aGIF89a";
4288 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4290 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
4291 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
4292 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
4293 static const WCHAR tiff_format[] = {'T','I','F','F',0};
4294 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
4295 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4297 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
4298 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
4299 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
4300 static const WCHAR emf_format[] = {'E','M','F',0};
4301 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
4302 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4304 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
4305 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
4306 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
4307 static const WCHAR wmf_format[] = {'W','M','F',0};
4308 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
4309 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
4311 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
4312 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
4313 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
4314 static const WCHAR png_format[] = {'P','N','G',0};
4315 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
4316 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4318 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
4319 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
4320 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
4321 static const WCHAR ico_format[] = {'I','C','O',0};
4322 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
4323 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4325 static const struct image_codec codecs[NUM_CODECS] = {
4327 { /* BMP */
4328 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4329 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4330 /* CodecName */ bmp_codecname,
4331 /* DllName */ NULL,
4332 /* FormatDescription */ bmp_format,
4333 /* FilenameExtension */ bmp_extension,
4334 /* MimeType */ bmp_mimetype,
4335 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4336 /* Version */ 1,
4337 /* SigCount */ 1,
4338 /* SigSize */ 2,
4339 /* SigPattern */ bmp_sig_pattern,
4340 /* SigMask */ bmp_sig_mask,
4342 encode_image_BMP,
4343 decode_image_bmp
4346 { /* JPEG */
4347 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4348 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4349 /* CodecName */ jpeg_codecname,
4350 /* DllName */ NULL,
4351 /* FormatDescription */ jpeg_format,
4352 /* FilenameExtension */ jpeg_extension,
4353 /* MimeType */ jpeg_mimetype,
4354 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4355 /* Version */ 1,
4356 /* SigCount */ 1,
4357 /* SigSize */ 2,
4358 /* SigPattern */ jpeg_sig_pattern,
4359 /* SigMask */ jpeg_sig_mask,
4361 encode_image_jpeg,
4362 decode_image_jpeg
4365 { /* GIF */
4366 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4367 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4368 /* CodecName */ gif_codecname,
4369 /* DllName */ NULL,
4370 /* FormatDescription */ gif_format,
4371 /* FilenameExtension */ gif_extension,
4372 /* MimeType */ gif_mimetype,
4373 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4374 /* Version */ 1,
4375 /* SigCount */ 2,
4376 /* SigSize */ 6,
4377 /* SigPattern */ gif_sig_pattern,
4378 /* SigMask */ gif_sig_mask,
4380 NULL,
4381 decode_image_gif
4384 { /* TIFF */
4385 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4386 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4387 /* CodecName */ tiff_codecname,
4388 /* DllName */ NULL,
4389 /* FormatDescription */ tiff_format,
4390 /* FilenameExtension */ tiff_extension,
4391 /* MimeType */ tiff_mimetype,
4392 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4393 /* Version */ 1,
4394 /* SigCount */ 2,
4395 /* SigSize */ 4,
4396 /* SigPattern */ tiff_sig_pattern,
4397 /* SigMask */ tiff_sig_mask,
4399 encode_image_tiff,
4400 decode_image_tiff
4403 { /* EMF */
4404 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4405 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4406 /* CodecName */ emf_codecname,
4407 /* DllName */ NULL,
4408 /* FormatDescription */ emf_format,
4409 /* FilenameExtension */ emf_extension,
4410 /* MimeType */ emf_mimetype,
4411 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4412 /* Version */ 1,
4413 /* SigCount */ 1,
4414 /* SigSize */ 4,
4415 /* SigPattern */ emf_sig_pattern,
4416 /* SigMask */ emf_sig_mask,
4418 NULL,
4419 decode_image_olepicture_metafile
4422 { /* WMF */
4423 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4424 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4425 /* CodecName */ wmf_codecname,
4426 /* DllName */ NULL,
4427 /* FormatDescription */ wmf_format,
4428 /* FilenameExtension */ wmf_extension,
4429 /* MimeType */ wmf_mimetype,
4430 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4431 /* Version */ 1,
4432 /* SigCount */ 1,
4433 /* SigSize */ 2,
4434 /* SigPattern */ wmf_sig_pattern,
4435 /* SigMask */ wmf_sig_mask,
4437 NULL,
4438 decode_image_olepicture_metafile
4441 { /* PNG */
4442 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4443 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4444 /* CodecName */ png_codecname,
4445 /* DllName */ NULL,
4446 /* FormatDescription */ png_format,
4447 /* FilenameExtension */ png_extension,
4448 /* MimeType */ png_mimetype,
4449 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4450 /* Version */ 1,
4451 /* SigCount */ 1,
4452 /* SigSize */ 8,
4453 /* SigPattern */ png_sig_pattern,
4454 /* SigMask */ png_sig_mask,
4456 encode_image_png,
4457 decode_image_png
4460 { /* ICO */
4461 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4462 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4463 /* CodecName */ ico_codecname,
4464 /* DllName */ NULL,
4465 /* FormatDescription */ ico_format,
4466 /* FilenameExtension */ ico_extension,
4467 /* MimeType */ ico_mimetype,
4468 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4469 /* Version */ 1,
4470 /* SigCount */ 1,
4471 /* SigSize */ 4,
4472 /* SigPattern */ ico_sig_pattern,
4473 /* SigMask */ ico_sig_mask,
4475 NULL,
4476 decode_image_icon
4480 /*****************************************************************************
4481 * GdipGetImageDecodersSize [GDIPLUS.@]
4483 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
4485 int decoder_count=0;
4486 int i;
4487 TRACE("%p %p\n", numDecoders, size);
4489 if (!numDecoders || !size)
4490 return InvalidParameter;
4492 for (i=0; i<NUM_CODECS; i++)
4494 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4495 decoder_count++;
4498 *numDecoders = decoder_count;
4499 *size = decoder_count * sizeof(ImageCodecInfo);
4501 return Ok;
4504 /*****************************************************************************
4505 * GdipGetImageDecoders [GDIPLUS.@]
4507 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
4509 int i, decoder_count=0;
4510 TRACE("%u %u %p\n", numDecoders, size, decoders);
4512 if (!decoders ||
4513 size != numDecoders * sizeof(ImageCodecInfo))
4514 return GenericError;
4516 for (i=0; i<NUM_CODECS; i++)
4518 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4520 if (decoder_count == numDecoders) return GenericError;
4521 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4522 decoder_count++;
4526 if (decoder_count < numDecoders) return GenericError;
4528 return Ok;
4531 /*****************************************************************************
4532 * GdipGetImageEncodersSize [GDIPLUS.@]
4534 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
4536 int encoder_count=0;
4537 int i;
4538 TRACE("%p %p\n", numEncoders, size);
4540 if (!numEncoders || !size)
4541 return InvalidParameter;
4543 for (i=0; i<NUM_CODECS; i++)
4545 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4546 encoder_count++;
4549 *numEncoders = encoder_count;
4550 *size = encoder_count * sizeof(ImageCodecInfo);
4552 return Ok;
4555 /*****************************************************************************
4556 * GdipGetImageEncoders [GDIPLUS.@]
4558 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
4560 int i, encoder_count=0;
4561 TRACE("%u %u %p\n", numEncoders, size, encoders);
4563 if (!encoders ||
4564 size != numEncoders * sizeof(ImageCodecInfo))
4565 return GenericError;
4567 for (i=0; i<NUM_CODECS; i++)
4569 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4571 if (encoder_count == numEncoders) return GenericError;
4572 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4573 encoder_count++;
4577 if (encoder_count < numEncoders) return GenericError;
4579 return Ok;
4582 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
4583 GDIPCONST CLSID* clsidEncoder, UINT *size)
4585 static int calls;
4587 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
4589 if(!(calls++))
4590 FIXME("not implemented\n");
4592 *size = 0;
4594 return NotImplemented;
4597 static PixelFormat get_16bpp_format(HBITMAP hbm)
4599 BITMAPV4HEADER bmh;
4600 HDC hdc;
4601 PixelFormat result;
4603 hdc = CreateCompatibleDC(NULL);
4605 memset(&bmh, 0, sizeof(bmh));
4606 bmh.bV4Size = sizeof(bmh);
4607 bmh.bV4Width = 1;
4608 bmh.bV4Height = 1;
4609 bmh.bV4V4Compression = BI_BITFIELDS;
4610 bmh.bV4BitCount = 16;
4612 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO*)&bmh, DIB_RGB_COLORS);
4614 if (bmh.bV4RedMask == 0x7c00 &&
4615 bmh.bV4GreenMask == 0x3e0 &&
4616 bmh.bV4BlueMask == 0x1f)
4618 result = PixelFormat16bppRGB555;
4620 else if (bmh.bV4RedMask == 0xf800 &&
4621 bmh.bV4GreenMask == 0x7e0 &&
4622 bmh.bV4BlueMask == 0x1f)
4624 result = PixelFormat16bppRGB565;
4626 else
4628 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
4629 bmh.bV4GreenMask, bmh.bV4BlueMask);
4630 result = PixelFormatUndefined;
4633 DeleteDC(hdc);
4635 return result;
4638 /*****************************************************************************
4639 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
4641 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
4643 BITMAP bm;
4644 GpStatus retval;
4645 PixelFormat format;
4646 BitmapData lockeddata;
4647 INT y;
4649 TRACE("%p %p %p\n", hbm, hpal, bitmap);
4651 if(!hbm || !bitmap)
4652 return InvalidParameter;
4654 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
4655 return InvalidParameter;
4657 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
4658 switch(bm.bmBitsPixel) {
4659 case 1:
4660 format = PixelFormat1bppIndexed;
4661 break;
4662 case 4:
4663 format = PixelFormat4bppIndexed;
4664 break;
4665 case 8:
4666 format = PixelFormat8bppIndexed;
4667 break;
4668 case 16:
4669 format = get_16bpp_format(hbm);
4670 if (format == PixelFormatUndefined)
4671 return InvalidParameter;
4672 break;
4673 case 24:
4674 format = PixelFormat24bppRGB;
4675 break;
4676 case 32:
4677 format = PixelFormat32bppRGB;
4678 break;
4679 case 48:
4680 format = PixelFormat48bppRGB;
4681 break;
4682 default:
4683 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
4684 return InvalidParameter;
4687 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
4688 format, NULL, bitmap);
4690 if (retval == Ok)
4692 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
4693 format, &lockeddata);
4694 if (retval == Ok)
4696 if (bm.bmBits)
4698 for (y=0; y<bm.bmHeight; y++)
4700 memcpy((BYTE*)lockeddata.Scan0+lockeddata.Stride*y,
4701 (BYTE*)bm.bmBits+bm.bmWidthBytes*(bm.bmHeight-1-y),
4702 bm.bmWidthBytes);
4705 else
4707 HDC hdc;
4708 HBITMAP oldhbm;
4709 BITMAPINFO *pbmi;
4710 INT src_height, dst_stride;
4711 BYTE *dst_bits;
4713 hdc = CreateCompatibleDC(NULL);
4714 oldhbm = SelectObject(hdc, hbm);
4716 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
4718 if (pbmi)
4720 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
4721 pbmi->bmiHeader.biBitCount = 0;
4723 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
4725 src_height = abs(pbmi->bmiHeader.biHeight);
4727 if (pbmi->bmiHeader.biHeight > 0)
4729 dst_bits = (BYTE*)lockeddata.Scan0+lockeddata.Stride*(src_height-1);
4730 dst_stride = -lockeddata.Stride;
4732 else
4734 dst_bits = lockeddata.Scan0;
4735 dst_stride = lockeddata.Stride;
4738 for (y=0; y<src_height; y++)
4740 GetDIBits(hdc, hbm, y, 1, dst_bits+dst_stride*y,
4741 pbmi, DIB_RGB_COLORS);
4744 GdipFree(pbmi);
4746 else
4747 retval = OutOfMemory;
4749 SelectObject(hdc, oldhbm);
4750 DeleteDC(hdc);
4753 GdipBitmapUnlockBits(*bitmap, &lockeddata);
4756 if (retval == Ok && hpal)
4758 WORD num_palette_entries;
4759 PALETTEENTRY *palette_entries=NULL;
4760 ColorPalette *palette=NULL;
4761 int i;
4763 if (!GetObjectW(hpal, sizeof(num_palette_entries), &num_palette_entries))
4764 retval = GenericError;
4766 if (retval == Ok)
4768 palette_entries = GdipAlloc(sizeof(PALETTEENTRY) * num_palette_entries);
4769 palette = GdipAlloc(sizeof(ColorPalette) + sizeof(ARGB) * (num_palette_entries-1));
4771 if (!palette_entries || !palette)
4772 retval = OutOfMemory;
4775 if (retval == Ok)
4777 if (!GetPaletteEntries(hpal, 0, num_palette_entries, palette_entries))
4778 retval = GenericError;
4781 if (retval == Ok)
4783 palette->Flags = 0;
4784 palette->Count = num_palette_entries;
4786 for (i=0; i<num_palette_entries; i++)
4788 PALETTEENTRY * entry = &palette_entries[i];
4789 palette->Entries[i] = 0xff000000 | entry->peRed << 16 |
4790 entry->peGreen << 8 | entry->peBlue;
4793 retval = GdipSetImagePalette((GpImage*)*bitmap, palette);
4796 GdipFree(palette_entries);
4797 GdipFree(palette);
4800 if (retval != Ok)
4802 GdipDisposeImage((GpImage*)*bitmap);
4803 *bitmap = NULL;
4807 return retval;
4810 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
4812 FIXME("(%p): stub\n", effect);
4813 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
4814 * in Windows's gdiplus */
4815 return NotImplemented;
4818 /*****************************************************************************
4819 * GdipSetEffectParameters [GDIPLUS.@]
4821 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
4822 const VOID *params, const UINT size)
4824 static int calls;
4826 TRACE("(%p,%p,%u)\n", effect, params, size);
4828 if(!(calls++))
4829 FIXME("not implemented\n");
4831 return NotImplemented;
4834 /*****************************************************************************
4835 * GdipGetImageFlags [GDIPLUS.@]
4837 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
4839 TRACE("%p %p\n", image, flags);
4841 if(!image || !flags)
4842 return InvalidParameter;
4844 *flags = image->flags;
4846 return Ok;
4849 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
4851 TRACE("(%d, %p)\n", control, param);
4853 switch(control){
4854 case TestControlForceBilinear:
4855 if(param)
4856 FIXME("TestControlForceBilinear not handled\n");
4857 break;
4858 case TestControlNoICM:
4859 if(param)
4860 FIXME("TestControlNoICM not handled\n");
4861 break;
4862 case TestControlGetBuildNumber:
4863 *((DWORD*)param) = 3102;
4864 break;
4867 return Ok;
4870 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
4871 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
4872 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
4873 GpMetafile **metafile)
4875 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
4876 frameUnit, debugstr_w(desc), metafile);
4878 return NotImplemented;
4881 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
4882 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
4883 GDIPCONST WCHAR *desc, GpMetafile **metafile)
4885 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
4886 frameUnit, debugstr_w(desc), metafile);
4888 return NotImplemented;
4891 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
4893 TRACE("%p\n", image);
4895 return Ok;
4898 /*****************************************************************************
4899 * GdipGetImageThumbnail [GDIPLUS.@]
4901 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
4902 GpImage **ret_image, GetThumbnailImageAbort cb,
4903 VOID * cb_data)
4905 GpStatus stat;
4906 GpGraphics *graphics;
4907 UINT srcwidth, srcheight;
4909 TRACE("(%p %u %u %p %p %p)\n",
4910 image, width, height, ret_image, cb, cb_data);
4912 if (!image || !ret_image)
4913 return InvalidParameter;
4915 if (!width) width = 120;
4916 if (!height) height = 120;
4918 GdipGetImageWidth(image, &srcwidth);
4919 GdipGetImageHeight(image, &srcheight);
4921 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
4922 NULL, (GpBitmap**)ret_image);
4924 if (stat == Ok)
4926 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
4928 if (stat == Ok)
4930 stat = GdipDrawImageRectRectI(graphics, image,
4931 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
4932 NULL, NULL, NULL);
4934 GdipDeleteGraphics(graphics);
4937 if (stat != Ok)
4939 GdipDisposeImage(*ret_image);
4940 *ret_image = NULL;
4944 return stat;
4947 /*****************************************************************************
4948 * GdipImageRotateFlip [GDIPLUS.@]
4950 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
4952 GpBitmap *new_bitmap;
4953 GpBitmap *bitmap;
4954 int bpp, bytesperpixel;
4955 int rotate_90, flip_x, flip_y;
4956 int src_x_offset, src_y_offset;
4957 LPBYTE src_origin;
4958 UINT x, y, width, height;
4959 BitmapData src_lock, dst_lock;
4960 GpStatus stat;
4962 TRACE("(%p, %u)\n", image, type);
4964 if (!image)
4965 return InvalidParameter;
4967 rotate_90 = type&1;
4968 flip_x = (type&6) == 2 || (type&6) == 4;
4969 flip_y = (type&3) == 1 || (type&3) == 2;
4971 if (image->type != ImageTypeBitmap)
4973 FIXME("Not implemented for type %i\n", image->type);
4974 return NotImplemented;
4977 bitmap = (GpBitmap*)image;
4978 bpp = PIXELFORMATBPP(bitmap->format);
4980 if (bpp < 8)
4982 FIXME("Not implemented for %i bit images\n", bpp);
4983 return NotImplemented;
4986 if (rotate_90)
4988 width = bitmap->height;
4989 height = bitmap->width;
4991 else
4993 width = bitmap->width;
4994 height = bitmap->height;
4997 bytesperpixel = bpp/8;
4999 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
5001 if (stat != Ok)
5002 return stat;
5004 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
5006 if (stat == Ok)
5008 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
5010 if (stat == Ok)
5012 LPBYTE src_row, src_pixel;
5013 LPBYTE dst_row, dst_pixel;
5015 src_origin = src_lock.Scan0;
5016 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
5017 if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
5019 if (rotate_90)
5021 if (flip_y) src_x_offset = -src_lock.Stride;
5022 else src_x_offset = src_lock.Stride;
5023 if (flip_x) src_y_offset = -bytesperpixel;
5024 else src_y_offset = bytesperpixel;
5026 else
5028 if (flip_x) src_x_offset = -bytesperpixel;
5029 else src_x_offset = bytesperpixel;
5030 if (flip_y) src_y_offset = -src_lock.Stride;
5031 else src_y_offset = src_lock.Stride;
5034 src_row = src_origin;
5035 dst_row = dst_lock.Scan0;
5036 for (y=0; y<height; y++)
5038 src_pixel = src_row;
5039 dst_pixel = dst_row;
5040 for (x=0; x<width; x++)
5042 /* FIXME: This could probably be faster without memcpy. */
5043 memcpy(dst_pixel, src_pixel, bytesperpixel);
5044 dst_pixel += bytesperpixel;
5045 src_pixel += src_x_offset;
5047 src_row += src_y_offset;
5048 dst_row += dst_lock.Stride;
5051 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
5054 GdipBitmapUnlockBits(bitmap, &src_lock);
5057 if (stat == Ok)
5058 move_bitmap(bitmap, new_bitmap, FALSE);
5059 else
5060 GdipDisposeImage((GpImage*)new_bitmap);
5062 return stat;
5065 /*****************************************************************************
5066 * GdipConvertToEmfPlusToFile [GDIPLUS.@]
5069 GpStatus WINGDIPAPI GdipConvertToEmfPlusToFile(const GpGraphics* refGraphics,
5070 GpMetafile* metafile, BOOL* conversionSuccess,
5071 const WCHAR* filename, EmfType emfType,
5072 const WCHAR* description, GpMetafile** out_metafile)
5074 FIXME("stub: %p, %p, %p, %p, %u, %p, %p\n", refGraphics, metafile, conversionSuccess, filename, emfType, description, out_metafile);
5075 return NotImplemented;