vbscript: Added support for title and type arguments of MsgBox.
[wine.git] / dlls / gdiplus / image.c
blob7964cd8dfa2f7df6ba30d5113dbf67e2e7f39a67
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 = CreateCompatibleDC(0);
120 y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
121 DeleteDC(hdcref);
123 return y;
126 static INT ipicture_pixel_width(IPicture *pic)
128 HDC hdcref;
129 OLE_XSIZE_HIMETRIC x;
131 IPicture_get_Width(pic, &x);
133 hdcref = CreateCompatibleDC(0);
134 x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
135 DeleteDC(hdcref);
137 return x;
140 GpStatus WINGDIPAPI GdipBitmapApplyEffect(GpBitmap* bitmap, CGpEffect* effect,
141 RECT* roi, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
143 FIXME("(%p %p %p %d %p %p): stub\n", bitmap, effect, roi, useAuxData, auxData, auxDataSize);
145 * Note: According to Jose Roca's GDI+ docs, this function is not
146 * implemented in Windows's GDI+.
148 return NotImplemented;
151 GpStatus WINGDIPAPI GdipBitmapCreateApplyEffect(GpBitmap** inputBitmaps,
152 INT numInputs, CGpEffect* effect, RECT* roi, RECT* outputRect,
153 GpBitmap** outputBitmap, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
155 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps, numInputs, effect, roi, outputRect, outputBitmap, useAuxData, auxData, auxDataSize);
157 * Note: According to Jose Roca's GDI+ docs, this function is not
158 * implemented in Windows's GDI+.
160 return NotImplemented;
163 static inline void getpixel_1bppIndexed(BYTE *index, const BYTE *row, UINT x)
165 *index = (row[x/8]>>(7-x%8)) & 1;
168 static inline void getpixel_4bppIndexed(BYTE *index, const BYTE *row, UINT x)
170 if (x & 1)
171 *index = row[x/2]&0xf;
172 else
173 *index = row[x/2]>>4;
176 static inline void getpixel_8bppIndexed(BYTE *index, const BYTE *row, UINT x)
178 *index = row[x];
181 static inline void getpixel_16bppGrayScale(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
182 const BYTE *row, UINT x)
184 *r = *g = *b = row[x*2+1];
185 *a = 255;
188 static inline void getpixel_16bppRGB555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
189 const BYTE *row, UINT x)
191 WORD pixel = *((const WORD*)(row)+x);
192 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
193 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
194 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
195 *a = 255;
198 static inline void getpixel_16bppRGB565(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
199 const BYTE *row, UINT x)
201 WORD pixel = *((const WORD*)(row)+x);
202 *r = (pixel>>8&0xf8)|(pixel>>13&0x7);
203 *g = (pixel>>3&0xfc)|(pixel>>9&0x3);
204 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
205 *a = 255;
208 static inline void getpixel_16bppARGB1555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
209 const BYTE *row, UINT x)
211 WORD pixel = *((const WORD*)(row)+x);
212 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
213 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
214 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
215 if ((pixel&0x8000) == 0x8000)
216 *a = 255;
217 else
218 *a = 0;
221 static inline void getpixel_24bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
222 const BYTE *row, UINT x)
224 *r = row[x*3+2];
225 *g = row[x*3+1];
226 *b = row[x*3];
227 *a = 255;
230 static inline void getpixel_32bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
231 const BYTE *row, UINT x)
233 *r = row[x*4+2];
234 *g = row[x*4+1];
235 *b = row[x*4];
236 *a = 255;
239 static inline void getpixel_32bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
240 const BYTE *row, UINT x)
242 *r = row[x*4+2];
243 *g = row[x*4+1];
244 *b = row[x*4];
245 *a = row[x*4+3];
248 static inline void getpixel_32bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
249 const BYTE *row, UINT x)
251 *a = row[x*4+3];
252 if (*a == 0)
253 *r = *g = *b = 0;
254 else
256 *r = row[x*4+2] * 255 / *a;
257 *g = row[x*4+1] * 255 / *a;
258 *b = row[x*4] * 255 / *a;
262 static inline void getpixel_48bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
263 const BYTE *row, UINT x)
265 *r = row[x*6+5];
266 *g = row[x*6+3];
267 *b = row[x*6+1];
268 *a = 255;
271 static inline void getpixel_64bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
272 const BYTE *row, UINT x)
274 *r = row[x*8+5];
275 *g = row[x*8+3];
276 *b = row[x*8+1];
277 *a = row[x*8+7];
280 static inline void getpixel_64bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
281 const BYTE *row, UINT x)
283 *a = row[x*8+7];
284 if (*a == 0)
285 *r = *g = *b = 0;
286 else
288 *r = row[x*8+5] * 255 / *a;
289 *g = row[x*8+3] * 255 / *a;
290 *b = row[x*8+1] * 255 / *a;
294 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
295 ARGB *color)
297 BYTE r, g, b, a;
298 BYTE index;
299 BYTE *row;
301 if(!bitmap || !color ||
302 x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
303 return InvalidParameter;
305 row = bitmap->bits+bitmap->stride*y;
307 switch (bitmap->format)
309 case PixelFormat1bppIndexed:
310 getpixel_1bppIndexed(&index,row,x);
311 break;
312 case PixelFormat4bppIndexed:
313 getpixel_4bppIndexed(&index,row,x);
314 break;
315 case PixelFormat8bppIndexed:
316 getpixel_8bppIndexed(&index,row,x);
317 break;
318 case PixelFormat16bppGrayScale:
319 getpixel_16bppGrayScale(&r,&g,&b,&a,row,x);
320 break;
321 case PixelFormat16bppRGB555:
322 getpixel_16bppRGB555(&r,&g,&b,&a,row,x);
323 break;
324 case PixelFormat16bppRGB565:
325 getpixel_16bppRGB565(&r,&g,&b,&a,row,x);
326 break;
327 case PixelFormat16bppARGB1555:
328 getpixel_16bppARGB1555(&r,&g,&b,&a,row,x);
329 break;
330 case PixelFormat24bppRGB:
331 getpixel_24bppRGB(&r,&g,&b,&a,row,x);
332 break;
333 case PixelFormat32bppRGB:
334 getpixel_32bppRGB(&r,&g,&b,&a,row,x);
335 break;
336 case PixelFormat32bppARGB:
337 getpixel_32bppARGB(&r,&g,&b,&a,row,x);
338 break;
339 case PixelFormat32bppPARGB:
340 getpixel_32bppPARGB(&r,&g,&b,&a,row,x);
341 break;
342 case PixelFormat48bppRGB:
343 getpixel_48bppRGB(&r,&g,&b,&a,row,x);
344 break;
345 case PixelFormat64bppARGB:
346 getpixel_64bppARGB(&r,&g,&b,&a,row,x);
347 break;
348 case PixelFormat64bppPARGB:
349 getpixel_64bppPARGB(&r,&g,&b,&a,row,x);
350 break;
351 default:
352 FIXME("not implemented for format 0x%x\n", bitmap->format);
353 return NotImplemented;
356 if (bitmap->format & PixelFormatIndexed)
357 *color = bitmap->image.palette->Entries[index];
358 else
359 *color = a<<24|r<<16|g<<8|b;
361 return Ok;
364 static inline UINT get_palette_index(BYTE r, BYTE g, BYTE b, BYTE a, ColorPalette *palette)
366 BYTE index = 0;
367 int best_distance = 0x7fff;
368 int distance;
369 UINT i;
371 if (!palette) return 0;
372 /* This algorithm scans entire palette,
373 computes difference from desired color (all color components have equal weight)
374 and returns the index of color with least difference.
376 Note: Maybe it could be replaced with a better algorithm for better image quality
377 and performance, though better algorithm would probably need some pre-built lookup
378 tables and thus may actually be slower if this method is called only few times per
379 every image.
381 for(i=0;i<palette->Count;i++) {
382 ARGB color=palette->Entries[i];
383 distance=abs(b-(color & 0xff)) + abs(g-(color>>8 & 0xff)) + abs(r-(color>>16 & 0xff)) + abs(a-(color>>24 & 0xff));
384 if (distance<best_distance) {
385 best_distance=distance;
386 index=i;
389 return index;
392 static inline void setpixel_8bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
393 BYTE *row, UINT x, ColorPalette *palette)
395 BYTE index = get_palette_index(r,g,b,a,palette);
396 row[x]=index;
399 static inline void setpixel_1bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
400 BYTE *row, UINT x, ColorPalette *palette)
402 row[x/8] = (row[x/8] & ~(1<<(7-x%8))) | (get_palette_index(r,g,b,a,palette)<<(7-x%8));
405 static inline void setpixel_4bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
406 BYTE *row, UINT x, ColorPalette *palette)
408 if (x & 1)
409 row[x/2] = (row[x/2] & 0xf0) | get_palette_index(r,g,b,a,palette);
410 else
411 row[x/2] = (row[x/2] & 0x0f) | get_palette_index(r,g,b,a,palette)<<4;
414 static inline void setpixel_16bppGrayScale(BYTE r, BYTE g, BYTE b, BYTE a,
415 BYTE *row, UINT x)
417 *((WORD*)(row)+x) = (r+g+b)*85;
420 static inline void setpixel_16bppRGB555(BYTE r, BYTE g, BYTE b, BYTE a,
421 BYTE *row, UINT x)
423 *((WORD*)(row)+x) = (r<<7&0x7c00)|
424 (g<<2&0x03e0)|
425 (b>>3&0x001f);
428 static inline void setpixel_16bppRGB565(BYTE r, BYTE g, BYTE b, BYTE a,
429 BYTE *row, UINT x)
431 *((WORD*)(row)+x) = (r<<8&0xf800)|
432 (g<<3&0x07e0)|
433 (b>>3&0x001f);
436 static inline void setpixel_16bppARGB1555(BYTE r, BYTE g, BYTE b, BYTE a,
437 BYTE *row, UINT x)
439 *((WORD*)(row)+x) = (a<<8&0x8000)|
440 (r<<7&0x7c00)|
441 (g<<2&0x03e0)|
442 (b>>3&0x001f);
445 static inline void setpixel_24bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
446 BYTE *row, UINT x)
448 row[x*3+2] = r;
449 row[x*3+1] = g;
450 row[x*3] = b;
453 static inline void setpixel_32bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
454 BYTE *row, UINT x)
456 *((DWORD*)(row)+x) = (r<<16)|(g<<8)|b;
459 static inline void setpixel_32bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
460 BYTE *row, UINT x)
462 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
465 static inline void setpixel_32bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
466 BYTE *row, UINT x)
468 r = r * a / 255;
469 g = g * a / 255;
470 b = b * a / 255;
471 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
474 static inline void setpixel_48bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
475 BYTE *row, UINT x)
477 row[x*6+5] = row[x*6+4] = r;
478 row[x*6+3] = row[x*6+2] = g;
479 row[x*6+1] = row[x*6] = b;
482 static inline void setpixel_64bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
483 BYTE *row, UINT x)
485 UINT64 a64=a, r64=r, g64=g, b64=b;
486 *((UINT64*)(row)+x) = (a64<<56)|(a64<<48)|(r64<<40)|(r64<<32)|(g64<<24)|(g64<<16)|(b64<<8)|b64;
489 static inline void setpixel_64bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
490 BYTE *row, UINT x)
492 UINT64 a64, r64, g64, b64;
493 a64 = a * 257;
494 r64 = r * a / 255;
495 g64 = g * a / 255;
496 b64 = b * a / 255;
497 *((UINT64*)(row)+x) = (a64<<48)|(r64<<32)|(g64<<16)|b64;
500 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
501 ARGB color)
503 BYTE a, r, g, b;
504 BYTE *row;
506 if(!bitmap || x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
507 return InvalidParameter;
509 a = color>>24;
510 r = color>>16;
511 g = color>>8;
512 b = color;
514 row = bitmap->bits + bitmap->stride * y;
516 switch (bitmap->format)
518 case PixelFormat16bppGrayScale:
519 setpixel_16bppGrayScale(r,g,b,a,row,x);
520 break;
521 case PixelFormat16bppRGB555:
522 setpixel_16bppRGB555(r,g,b,a,row,x);
523 break;
524 case PixelFormat16bppRGB565:
525 setpixel_16bppRGB565(r,g,b,a,row,x);
526 break;
527 case PixelFormat16bppARGB1555:
528 setpixel_16bppARGB1555(r,g,b,a,row,x);
529 break;
530 case PixelFormat24bppRGB:
531 setpixel_24bppRGB(r,g,b,a,row,x);
532 break;
533 case PixelFormat32bppRGB:
534 setpixel_32bppRGB(r,g,b,a,row,x);
535 break;
536 case PixelFormat32bppARGB:
537 setpixel_32bppARGB(r,g,b,a,row,x);
538 break;
539 case PixelFormat32bppPARGB:
540 setpixel_32bppPARGB(r,g,b,a,row,x);
541 break;
542 case PixelFormat48bppRGB:
543 setpixel_48bppRGB(r,g,b,a,row,x);
544 break;
545 case PixelFormat64bppARGB:
546 setpixel_64bppARGB(r,g,b,a,row,x);
547 break;
548 case PixelFormat64bppPARGB:
549 setpixel_64bppPARGB(r,g,b,a,row,x);
550 break;
551 case PixelFormat8bppIndexed:
552 setpixel_8bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
553 break;
554 case PixelFormat4bppIndexed:
555 setpixel_4bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
556 break;
557 case PixelFormat1bppIndexed:
558 setpixel_1bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
559 break;
560 default:
561 FIXME("not implemented for format 0x%x\n", bitmap->format);
562 return NotImplemented;
565 return Ok;
568 GpStatus convert_pixels(INT width, INT height,
569 INT dst_stride, BYTE *dst_bits, PixelFormat dst_format,
570 INT src_stride, const BYTE *src_bits, PixelFormat src_format,
571 ColorPalette *palette)
573 INT x, y;
575 if (src_format == dst_format ||
576 (dst_format == PixelFormat32bppRGB && PIXELFORMATBPP(src_format) == 32))
578 UINT widthbytes = PIXELFORMATBPP(src_format) * width / 8;
579 for (y=0; y<height; y++)
580 memcpy(dst_bits+dst_stride*y, src_bits+src_stride*y, widthbytes);
581 return Ok;
584 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
585 for (x=0; x<width; x++) \
586 for (y=0; y<height; y++) { \
587 BYTE index; \
588 ARGB argb; \
589 BYTE *color = (BYTE *)&argb; \
590 getpixel_function(&index, src_bits+src_stride*y, x); \
591 argb = (palette && index < palette->Count) ? palette->Entries[index] : 0; \
592 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
594 return Ok; \
595 } while (0);
597 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
598 for (x=0; x<width; x++) \
599 for (y=0; y<height; y++) { \
600 BYTE r, g, b, a; \
601 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
602 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
604 return Ok; \
605 } while (0);
607 #define convert_rgb_to_indexed(getpixel_function, setpixel_function) do { \
608 for (x=0; x<width; x++) \
609 for (y=0; y<height; y++) { \
610 BYTE r, g, b, a; \
611 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
612 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x, palette); \
614 return Ok; \
615 } while (0);
617 switch (src_format)
619 case PixelFormat1bppIndexed:
620 switch (dst_format)
622 case PixelFormat16bppGrayScale:
623 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppGrayScale);
624 case PixelFormat16bppRGB555:
625 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB555);
626 case PixelFormat16bppRGB565:
627 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB565);
628 case PixelFormat16bppARGB1555:
629 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppARGB1555);
630 case PixelFormat24bppRGB:
631 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_24bppRGB);
632 case PixelFormat32bppRGB:
633 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppRGB);
634 case PixelFormat32bppARGB:
635 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppARGB);
636 case PixelFormat32bppPARGB:
637 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppPARGB);
638 case PixelFormat48bppRGB:
639 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_48bppRGB);
640 case PixelFormat64bppARGB:
641 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_64bppARGB);
642 default:
643 break;
645 break;
646 case PixelFormat4bppIndexed:
647 switch (dst_format)
649 case PixelFormat16bppGrayScale:
650 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppGrayScale);
651 case PixelFormat16bppRGB555:
652 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB555);
653 case PixelFormat16bppRGB565:
654 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB565);
655 case PixelFormat16bppARGB1555:
656 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppARGB1555);
657 case PixelFormat24bppRGB:
658 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_24bppRGB);
659 case PixelFormat32bppRGB:
660 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppRGB);
661 case PixelFormat32bppARGB:
662 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppARGB);
663 case PixelFormat32bppPARGB:
664 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppPARGB);
665 case PixelFormat48bppRGB:
666 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_48bppRGB);
667 case PixelFormat64bppARGB:
668 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_64bppARGB);
669 default:
670 break;
672 break;
673 case PixelFormat8bppIndexed:
674 switch (dst_format)
676 case PixelFormat16bppGrayScale:
677 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppGrayScale);
678 case PixelFormat16bppRGB555:
679 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB555);
680 case PixelFormat16bppRGB565:
681 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB565);
682 case PixelFormat16bppARGB1555:
683 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppARGB1555);
684 case PixelFormat24bppRGB:
685 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_24bppRGB);
686 case PixelFormat32bppRGB:
687 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppRGB);
688 case PixelFormat32bppARGB:
689 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppARGB);
690 case PixelFormat32bppPARGB:
691 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppPARGB);
692 case PixelFormat48bppRGB:
693 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_48bppRGB);
694 case PixelFormat64bppARGB:
695 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_64bppARGB);
696 default:
697 break;
699 break;
700 case PixelFormat16bppGrayScale:
701 switch (dst_format)
703 case PixelFormat1bppIndexed:
704 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_1bppIndexed);
705 case PixelFormat8bppIndexed:
706 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_8bppIndexed);
707 case PixelFormat16bppRGB555:
708 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB555);
709 case PixelFormat16bppRGB565:
710 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB565);
711 case PixelFormat16bppARGB1555:
712 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppARGB1555);
713 case PixelFormat24bppRGB:
714 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_24bppRGB);
715 case PixelFormat32bppRGB:
716 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppRGB);
717 case PixelFormat32bppARGB:
718 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppARGB);
719 case PixelFormat32bppPARGB:
720 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppPARGB);
721 case PixelFormat48bppRGB:
722 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_48bppRGB);
723 case PixelFormat64bppARGB:
724 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_64bppARGB);
725 default:
726 break;
728 break;
729 case PixelFormat16bppRGB555:
730 switch (dst_format)
732 case PixelFormat1bppIndexed:
733 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_1bppIndexed);
734 case PixelFormat8bppIndexed:
735 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_8bppIndexed);
736 case PixelFormat16bppGrayScale:
737 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppGrayScale);
738 case PixelFormat16bppRGB565:
739 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppRGB565);
740 case PixelFormat16bppARGB1555:
741 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppARGB1555);
742 case PixelFormat24bppRGB:
743 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_24bppRGB);
744 case PixelFormat32bppRGB:
745 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppRGB);
746 case PixelFormat32bppARGB:
747 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppARGB);
748 case PixelFormat32bppPARGB:
749 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppPARGB);
750 case PixelFormat48bppRGB:
751 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_48bppRGB);
752 case PixelFormat64bppARGB:
753 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_64bppARGB);
754 default:
755 break;
757 break;
758 case PixelFormat16bppRGB565:
759 switch (dst_format)
761 case PixelFormat1bppIndexed:
762 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_1bppIndexed);
763 case PixelFormat8bppIndexed:
764 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_8bppIndexed);
765 case PixelFormat16bppGrayScale:
766 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppGrayScale);
767 case PixelFormat16bppRGB555:
768 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppRGB555);
769 case PixelFormat16bppARGB1555:
770 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppARGB1555);
771 case PixelFormat24bppRGB:
772 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_24bppRGB);
773 case PixelFormat32bppRGB:
774 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppRGB);
775 case PixelFormat32bppARGB:
776 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppARGB);
777 case PixelFormat32bppPARGB:
778 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppPARGB);
779 case PixelFormat48bppRGB:
780 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_48bppRGB);
781 case PixelFormat64bppARGB:
782 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_64bppARGB);
783 default:
784 break;
786 break;
787 case PixelFormat16bppARGB1555:
788 switch (dst_format)
790 case PixelFormat1bppIndexed:
791 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_1bppIndexed);
792 case PixelFormat8bppIndexed:
793 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_8bppIndexed);
794 case PixelFormat16bppGrayScale:
795 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppGrayScale);
796 case PixelFormat16bppRGB555:
797 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB555);
798 case PixelFormat16bppRGB565:
799 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB565);
800 case PixelFormat24bppRGB:
801 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_24bppRGB);
802 case PixelFormat32bppRGB:
803 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppRGB);
804 case PixelFormat32bppARGB:
805 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppARGB);
806 case PixelFormat32bppPARGB:
807 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppPARGB);
808 case PixelFormat48bppRGB:
809 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_48bppRGB);
810 case PixelFormat64bppARGB:
811 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_64bppARGB);
812 default:
813 break;
815 break;
816 case PixelFormat24bppRGB:
817 switch (dst_format)
819 case PixelFormat1bppIndexed:
820 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_1bppIndexed);
821 case PixelFormat8bppIndexed:
822 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_8bppIndexed);
823 case PixelFormat16bppGrayScale:
824 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppGrayScale);
825 case PixelFormat16bppRGB555:
826 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB555);
827 case PixelFormat16bppRGB565:
828 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB565);
829 case PixelFormat16bppARGB1555:
830 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppARGB1555);
831 case PixelFormat32bppRGB:
832 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppRGB);
833 case PixelFormat32bppARGB:
834 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppARGB);
835 case PixelFormat32bppPARGB:
836 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppPARGB);
837 case PixelFormat48bppRGB:
838 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_48bppRGB);
839 case PixelFormat64bppARGB:
840 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_64bppARGB);
841 default:
842 break;
844 break;
845 case PixelFormat32bppRGB:
846 switch (dst_format)
848 case PixelFormat1bppIndexed:
849 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_1bppIndexed);
850 case PixelFormat8bppIndexed:
851 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_8bppIndexed);
852 case PixelFormat16bppGrayScale:
853 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppGrayScale);
854 case PixelFormat16bppRGB555:
855 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB555);
856 case PixelFormat16bppRGB565:
857 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB565);
858 case PixelFormat16bppARGB1555:
859 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppARGB1555);
860 case PixelFormat24bppRGB:
861 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_24bppRGB);
862 case PixelFormat32bppARGB:
863 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppARGB);
864 case PixelFormat32bppPARGB:
865 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppPARGB);
866 case PixelFormat48bppRGB:
867 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_48bppRGB);
868 case PixelFormat64bppARGB:
869 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_64bppARGB);
870 default:
871 break;
873 break;
874 case PixelFormat32bppARGB:
875 switch (dst_format)
877 case PixelFormat1bppIndexed:
878 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_1bppIndexed);
879 case PixelFormat8bppIndexed:
880 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_8bppIndexed);
881 case PixelFormat16bppGrayScale:
882 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppGrayScale);
883 case PixelFormat16bppRGB555:
884 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB555);
885 case PixelFormat16bppRGB565:
886 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB565);
887 case PixelFormat16bppARGB1555:
888 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppARGB1555);
889 case PixelFormat24bppRGB:
890 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_24bppRGB);
891 case PixelFormat32bppPARGB:
892 convert_32bppARGB_to_32bppPARGB(width, height, dst_bits, dst_stride, src_bits, src_stride);
893 return Ok;
894 case PixelFormat48bppRGB:
895 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_48bppRGB);
896 case PixelFormat64bppARGB:
897 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_64bppARGB);
898 default:
899 break;
901 break;
902 case PixelFormat32bppPARGB:
903 switch (dst_format)
905 case PixelFormat1bppIndexed:
906 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_1bppIndexed);
907 case PixelFormat8bppIndexed:
908 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_8bppIndexed);
909 case PixelFormat16bppGrayScale:
910 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppGrayScale);
911 case PixelFormat16bppRGB555:
912 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB555);
913 case PixelFormat16bppRGB565:
914 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB565);
915 case PixelFormat16bppARGB1555:
916 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppARGB1555);
917 case PixelFormat24bppRGB:
918 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_24bppRGB);
919 case PixelFormat32bppRGB:
920 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppRGB);
921 case PixelFormat32bppARGB:
922 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppARGB);
923 case PixelFormat48bppRGB:
924 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_48bppRGB);
925 case PixelFormat64bppARGB:
926 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_64bppARGB);
927 default:
928 break;
930 break;
931 case PixelFormat48bppRGB:
932 switch (dst_format)
934 case PixelFormat1bppIndexed:
935 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_1bppIndexed);
936 case PixelFormat8bppIndexed:
937 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_8bppIndexed);
938 case PixelFormat16bppGrayScale:
939 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppGrayScale);
940 case PixelFormat16bppRGB555:
941 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB555);
942 case PixelFormat16bppRGB565:
943 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB565);
944 case PixelFormat16bppARGB1555:
945 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppARGB1555);
946 case PixelFormat24bppRGB:
947 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_24bppRGB);
948 case PixelFormat32bppRGB:
949 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppRGB);
950 case PixelFormat32bppARGB:
951 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppARGB);
952 case PixelFormat32bppPARGB:
953 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppPARGB);
954 case PixelFormat64bppARGB:
955 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_64bppARGB);
956 default:
957 break;
959 break;
960 case PixelFormat64bppARGB:
961 switch (dst_format)
963 case PixelFormat1bppIndexed:
964 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_1bppIndexed);
965 case PixelFormat8bppIndexed:
966 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_8bppIndexed);
967 case PixelFormat16bppGrayScale:
968 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppGrayScale);
969 case PixelFormat16bppRGB555:
970 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB555);
971 case PixelFormat16bppRGB565:
972 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB565);
973 case PixelFormat16bppARGB1555:
974 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppARGB1555);
975 case PixelFormat24bppRGB:
976 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_24bppRGB);
977 case PixelFormat32bppRGB:
978 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppRGB);
979 case PixelFormat32bppARGB:
980 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppARGB);
981 case PixelFormat32bppPARGB:
982 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppPARGB);
983 case PixelFormat48bppRGB:
984 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_48bppRGB);
985 default:
986 break;
988 break;
989 case PixelFormat64bppPARGB:
990 switch (dst_format)
992 case PixelFormat1bppIndexed:
993 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_1bppIndexed);
994 case PixelFormat8bppIndexed:
995 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_8bppIndexed);
996 case PixelFormat16bppGrayScale:
997 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppGrayScale);
998 case PixelFormat16bppRGB555:
999 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB555);
1000 case PixelFormat16bppRGB565:
1001 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB565);
1002 case PixelFormat16bppARGB1555:
1003 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppARGB1555);
1004 case PixelFormat24bppRGB:
1005 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_24bppRGB);
1006 case PixelFormat32bppRGB:
1007 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppRGB);
1008 case PixelFormat32bppARGB:
1009 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppARGB);
1010 case PixelFormat32bppPARGB:
1011 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppPARGB);
1012 case PixelFormat48bppRGB:
1013 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_48bppRGB);
1014 case PixelFormat64bppARGB:
1015 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_64bppARGB);
1016 default:
1017 break;
1019 break;
1020 default:
1021 break;
1024 #undef convert_indexed_to_rgb
1025 #undef convert_rgb_to_rgb
1027 return NotImplemented;
1030 /* This function returns a pointer to an array of pixels that represents the
1031 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
1032 * flags. It is correct behavior that a user who calls this function with write
1033 * privileges can write to the whole bitmap (not just the area in rect).
1035 * FIXME: only used portion of format is bits per pixel. */
1036 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
1037 UINT flags, PixelFormat format, BitmapData* lockeddata)
1039 INT bitspp = PIXELFORMATBPP(format);
1040 GpRect act_rect; /* actual rect to be used */
1041 GpStatus stat;
1043 TRACE("%p %p %d 0x%x %p\n", bitmap, rect, flags, format, lockeddata);
1045 if(!lockeddata || !bitmap)
1046 return InvalidParameter;
1048 if(rect){
1049 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
1050 (rect->Y + rect->Height > bitmap->height) || !flags)
1051 return InvalidParameter;
1053 act_rect = *rect;
1055 else{
1056 act_rect.X = act_rect.Y = 0;
1057 act_rect.Width = bitmap->width;
1058 act_rect.Height = bitmap->height;
1061 if(bitmap->lockmode)
1063 WARN("bitmap is already locked and cannot be locked again\n");
1064 return WrongState;
1067 if (bitmap->bits && bitmap->format == format && !(flags & ImageLockModeUserInputBuf))
1069 /* no conversion is necessary; just use the bits directly */
1070 lockeddata->Width = act_rect.Width;
1071 lockeddata->Height = act_rect.Height;
1072 lockeddata->PixelFormat = format;
1073 lockeddata->Reserved = flags;
1074 lockeddata->Stride = bitmap->stride;
1075 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
1076 bitmap->stride * act_rect.Y;
1078 bitmap->lockmode = flags | ImageLockModeRead;
1079 bitmap->numlocks++;
1081 return Ok;
1084 /* Make sure we can convert to the requested format. */
1085 if (flags & ImageLockModeRead)
1087 stat = convert_pixels(0, 0, 0, NULL, format, 0, NULL, bitmap->format, NULL);
1088 if (stat == NotImplemented)
1090 FIXME("cannot read bitmap from %x to %x\n", bitmap->format, format);
1091 return NotImplemented;
1095 /* If we're opening for writing, make sure we'll be able to write back in
1096 * the original format. */
1097 if (flags & ImageLockModeWrite)
1099 stat = convert_pixels(0, 0, 0, NULL, bitmap->format, 0, NULL, format, NULL);
1100 if (stat == NotImplemented)
1102 FIXME("cannot write bitmap from %x to %x\n", format, bitmap->format);
1103 return NotImplemented;
1107 lockeddata->Width = act_rect.Width;
1108 lockeddata->Height = act_rect.Height;
1109 lockeddata->PixelFormat = format;
1110 lockeddata->Reserved = flags;
1112 if(!(flags & ImageLockModeUserInputBuf))
1114 lockeddata->Stride = (((act_rect.Width * bitspp + 7) / 8) + 3) & ~3;
1116 bitmap->bitmapbits = GdipAlloc(lockeddata->Stride * act_rect.Height);
1118 if (!bitmap->bitmapbits) return OutOfMemory;
1120 lockeddata->Scan0 = bitmap->bitmapbits;
1123 if (flags & ImageLockModeRead)
1125 static BOOL fixme = FALSE;
1127 if (!fixme && (PIXELFORMATBPP(bitmap->format) * act_rect.X) % 8 != 0)
1129 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1130 fixme = TRUE;
1133 stat = convert_pixels(act_rect.Width, act_rect.Height,
1134 lockeddata->Stride, lockeddata->Scan0, format,
1135 bitmap->stride,
1136 bitmap->bits + bitmap->stride * act_rect.Y + PIXELFORMATBPP(bitmap->format) * act_rect.X / 8,
1137 bitmap->format, bitmap->image.palette);
1139 if (stat != Ok)
1141 GdipFree(bitmap->bitmapbits);
1142 bitmap->bitmapbits = NULL;
1143 return stat;
1147 bitmap->lockmode = flags | ImageLockModeRead;
1148 bitmap->numlocks++;
1149 bitmap->lockx = act_rect.X;
1150 bitmap->locky = act_rect.Y;
1152 return Ok;
1155 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
1157 TRACE("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
1159 if (!bitmap || xdpi == 0.0 || ydpi == 0.0)
1160 return InvalidParameter;
1162 bitmap->image.xres = xdpi;
1163 bitmap->image.yres = ydpi;
1165 return Ok;
1168 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
1169 BitmapData* lockeddata)
1171 GpStatus stat;
1172 static BOOL fixme = FALSE;
1174 TRACE("(%p,%p)\n", bitmap, lockeddata);
1176 if(!bitmap || !lockeddata)
1177 return InvalidParameter;
1179 if(!bitmap->lockmode)
1180 return WrongState;
1182 if(!(lockeddata->Reserved & ImageLockModeWrite)){
1183 if(!(--bitmap->numlocks))
1184 bitmap->lockmode = 0;
1186 GdipFree(bitmap->bitmapbits);
1187 bitmap->bitmapbits = NULL;
1188 return Ok;
1191 if (!bitmap->bitmapbits && !(lockeddata->Reserved & ImageLockModeUserInputBuf))
1193 /* we passed a direct reference; no need to do anything */
1194 bitmap->lockmode = 0;
1195 bitmap->numlocks = 0;
1196 return Ok;
1199 if (!fixme && (PIXELFORMATBPP(bitmap->format) * bitmap->lockx) % 8 != 0)
1201 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1202 fixme = TRUE;
1205 stat = convert_pixels(lockeddata->Width, lockeddata->Height,
1206 bitmap->stride,
1207 bitmap->bits + bitmap->stride * bitmap->locky + PIXELFORMATBPP(bitmap->format) * bitmap->lockx / 8,
1208 bitmap->format,
1209 lockeddata->Stride, lockeddata->Scan0, lockeddata->PixelFormat, NULL);
1211 if (stat != Ok)
1213 ERR("failed to convert pixels; this should never happen\n");
1216 GdipFree(bitmap->bitmapbits);
1217 bitmap->bitmapbits = NULL;
1218 bitmap->lockmode = 0;
1219 bitmap->numlocks = 0;
1221 return stat;
1224 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
1225 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1227 Rect area;
1228 GpStatus stat;
1230 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1232 if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
1233 x < 0 || y < 0 ||
1234 x + width > srcBitmap->width || y + height > srcBitmap->height)
1236 TRACE("<-- InvalidParameter\n");
1237 return InvalidParameter;
1240 if (format == PixelFormatDontCare)
1241 format = srcBitmap->format;
1243 area.X = gdip_round(x);
1244 area.Y = gdip_round(y);
1245 area.Width = gdip_round(width);
1246 area.Height = gdip_round(height);
1248 stat = GdipCreateBitmapFromScan0(area.Width, area.Height, 0, format, NULL, dstBitmap);
1249 if (stat == Ok)
1251 stat = convert_pixels(area.Width, area.Height, (*dstBitmap)->stride, (*dstBitmap)->bits, (*dstBitmap)->format,
1252 srcBitmap->stride,
1253 srcBitmap->bits + srcBitmap->stride * area.Y + PIXELFORMATBPP(srcBitmap->format) * area.X / 8,
1254 srcBitmap->format, srcBitmap->image.palette);
1256 if (stat == Ok && srcBitmap->image.palette)
1258 ColorPalette *src_palette, *dst_palette;
1260 src_palette = srcBitmap->image.palette;
1262 dst_palette = GdipAlloc(sizeof(UINT) * 2 + sizeof(ARGB) * src_palette->Count);
1264 if (dst_palette)
1266 dst_palette->Flags = src_palette->Flags;
1267 dst_palette->Count = src_palette->Count;
1268 memcpy(dst_palette->Entries, src_palette->Entries, sizeof(ARGB) * src_palette->Count);
1270 GdipFree((*dstBitmap)->image.palette);
1271 (*dstBitmap)->image.palette = dst_palette;
1273 else
1274 stat = OutOfMemory;
1277 if (stat != Ok)
1278 GdipDisposeImage((GpImage*)*dstBitmap);
1281 if (stat != Ok)
1282 *dstBitmap = NULL;
1284 return stat;
1287 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
1288 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1290 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1292 return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
1295 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
1297 GpStatus stat = GenericError;
1299 TRACE("%p, %p\n", image, cloneImage);
1301 if (!image || !cloneImage)
1302 return InvalidParameter;
1304 if (image->picture)
1306 IStream* stream;
1307 HRESULT hr;
1308 INT size;
1309 LARGE_INTEGER move;
1311 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
1312 if (FAILED(hr))
1313 return GenericError;
1315 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
1316 if(FAILED(hr))
1318 WARN("Failed to save image on stream\n");
1319 goto out;
1322 /* Set seek pointer back to the beginning of the picture */
1323 move.QuadPart = 0;
1324 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1325 if (FAILED(hr))
1326 goto out;
1328 stat = GdipLoadImageFromStream(stream, cloneImage);
1329 if (stat != Ok) WARN("Failed to load image from stream\n");
1331 out:
1332 IStream_Release(stream);
1333 return stat;
1335 else if (image->type == ImageTypeBitmap)
1337 GpBitmap *bitmap = (GpBitmap *)image;
1339 return GdipCloneBitmapAreaI(0, 0, bitmap->width, bitmap->height,
1340 bitmap->format, bitmap, (GpBitmap **)cloneImage);
1342 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
1344 GpMetafile *result, *metafile;
1346 metafile = (GpMetafile*)image;
1348 result = GdipAlloc(sizeof(*result));
1349 if (!result)
1350 return OutOfMemory;
1352 result->image.type = ImageTypeMetafile;
1353 result->image.format = image->format;
1354 result->image.flags = image->flags;
1355 result->image.frame_count = 1;
1356 result->image.xres = image->xres;
1357 result->image.yres = image->yres;
1358 result->bounds = metafile->bounds;
1359 result->unit = metafile->unit;
1360 result->metafile_type = metafile->metafile_type;
1361 result->hemf = CopyEnhMetaFileW(metafile->hemf, NULL);
1363 if (!result->hemf)
1365 GdipFree(result);
1366 return OutOfMemory;
1369 *cloneImage = &result->image;
1370 return Ok;
1372 else
1374 WARN("GpImage with no image data (metafile in wrong state?)\n");
1375 return InvalidParameter;
1379 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
1380 GpBitmap **bitmap)
1382 GpStatus stat;
1383 IStream *stream;
1385 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1387 if(!filename || !bitmap)
1388 return InvalidParameter;
1390 *bitmap = NULL;
1392 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1394 if(stat != Ok)
1395 return stat;
1397 stat = GdipCreateBitmapFromStream(stream, bitmap);
1399 IStream_Release(stream);
1401 return stat;
1404 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
1405 VOID *bits, GpBitmap **bitmap)
1407 DWORD height, stride;
1408 PixelFormat format;
1410 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
1412 if (!info || !bits || !bitmap)
1413 return InvalidParameter;
1415 height = abs(info->bmiHeader.biHeight);
1416 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
1418 if(info->bmiHeader.biHeight > 0) /* bottom-up */
1420 bits = (BYTE*)bits + (height - 1) * stride;
1421 stride = -stride;
1424 switch(info->bmiHeader.biBitCount) {
1425 case 1:
1426 format = PixelFormat1bppIndexed;
1427 break;
1428 case 4:
1429 format = PixelFormat4bppIndexed;
1430 break;
1431 case 8:
1432 format = PixelFormat8bppIndexed;
1433 break;
1434 case 16:
1435 format = PixelFormat16bppRGB555;
1436 break;
1437 case 24:
1438 format = PixelFormat24bppRGB;
1439 break;
1440 case 32:
1441 format = PixelFormat32bppRGB;
1442 break;
1443 default:
1444 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
1445 *bitmap = NULL;
1446 return InvalidParameter;
1449 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
1450 bits, bitmap);
1454 /* FIXME: no icm */
1455 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
1456 GpBitmap **bitmap)
1458 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1460 return GdipCreateBitmapFromFile(filename, bitmap);
1463 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
1464 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
1466 HBITMAP hbm;
1467 GpStatus stat = InvalidParameter;
1469 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
1471 if(!lpBitmapName || !bitmap)
1472 return InvalidParameter;
1474 /* load DIB */
1475 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
1476 LR_CREATEDIBSECTION);
1478 if(hbm){
1479 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1480 DeleteObject(hbm);
1483 return stat;
1486 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
1487 HBITMAP* hbmReturn, ARGB background)
1489 GpStatus stat;
1490 HBITMAP result;
1491 UINT width, height;
1492 BITMAPINFOHEADER bih;
1493 LPBYTE bits;
1494 BitmapData lockeddata;
1495 TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
1497 if (!bitmap || !hbmReturn) return InvalidParameter;
1499 GdipGetImageWidth((GpImage*)bitmap, &width);
1500 GdipGetImageHeight((GpImage*)bitmap, &height);
1502 bih.biSize = sizeof(bih);
1503 bih.biWidth = width;
1504 bih.biHeight = height;
1505 bih.biPlanes = 1;
1506 bih.biBitCount = 32;
1507 bih.biCompression = BI_RGB;
1508 bih.biSizeImage = 0;
1509 bih.biXPelsPerMeter = 0;
1510 bih.biYPelsPerMeter = 0;
1511 bih.biClrUsed = 0;
1512 bih.biClrImportant = 0;
1514 result = CreateDIBSection(0, (BITMAPINFO*)&bih, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1516 if (result)
1518 lockeddata.Stride = -width * 4;
1519 lockeddata.Scan0 = bits + (width * 4 * (height - 1));
1521 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead|ImageLockModeUserInputBuf,
1522 PixelFormat32bppPARGB, &lockeddata);
1524 if (stat == Ok)
1525 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1527 else
1528 stat = GenericError;
1530 if (stat != Ok && result)
1532 DeleteObject(result);
1533 result = NULL;
1536 *hbmReturn = result;
1538 return stat;
1541 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
1542 GpGraphics* target, GpBitmap** bitmap)
1544 GpStatus ret;
1546 TRACE("(%d, %d, %p, %p)\n", width, height, target, bitmap);
1548 if(!target || !bitmap)
1549 return InvalidParameter;
1551 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
1552 NULL, bitmap);
1554 if (ret == Ok)
1556 GdipGetDpiX(target, &(*bitmap)->image.xres);
1557 GdipGetDpiY(target, &(*bitmap)->image.yres);
1560 return ret;
1563 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1565 GpStatus stat;
1566 ICONINFO iinfo;
1567 BITMAP bm;
1568 int ret;
1569 UINT width, height, stride;
1570 GpRect rect;
1571 BitmapData lockeddata;
1572 HDC screendc;
1573 BOOL has_alpha;
1574 int x, y;
1575 BITMAPINFOHEADER bih;
1576 DWORD *src;
1577 BYTE *dst_row;
1578 DWORD *dst;
1580 TRACE("%p, %p\n", hicon, bitmap);
1582 if(!bitmap || !GetIconInfo(hicon, &iinfo))
1583 return InvalidParameter;
1585 /* get the size of the icon */
1586 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1587 if (ret == 0) {
1588 DeleteObject(iinfo.hbmColor);
1589 DeleteObject(iinfo.hbmMask);
1590 return GenericError;
1593 width = bm.bmWidth;
1594 height = iinfo.hbmColor ? abs(bm.bmHeight) : abs(bm.bmHeight) / 2;
1595 stride = width * 4;
1597 stat = GdipCreateBitmapFromScan0(width, height, stride, PixelFormat32bppARGB, NULL, bitmap);
1598 if (stat != Ok) {
1599 DeleteObject(iinfo.hbmColor);
1600 DeleteObject(iinfo.hbmMask);
1601 return stat;
1604 rect.X = 0;
1605 rect.Y = 0;
1606 rect.Width = width;
1607 rect.Height = height;
1609 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1610 if (stat != Ok) {
1611 DeleteObject(iinfo.hbmColor);
1612 DeleteObject(iinfo.hbmMask);
1613 GdipDisposeImage((GpImage*)*bitmap);
1614 return stat;
1617 bih.biSize = sizeof(bih);
1618 bih.biWidth = width;
1619 bih.biHeight = iinfo.hbmColor ? -height: -height * 2;
1620 bih.biPlanes = 1;
1621 bih.biBitCount = 32;
1622 bih.biCompression = BI_RGB;
1623 bih.biSizeImage = 0;
1624 bih.biXPelsPerMeter = 0;
1625 bih.biYPelsPerMeter = 0;
1626 bih.biClrUsed = 0;
1627 bih.biClrImportant = 0;
1629 screendc = CreateCompatibleDC(0);
1630 if (iinfo.hbmColor)
1632 GetDIBits(screendc, iinfo.hbmColor, 0, height, lockeddata.Scan0, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1634 if (bm.bmBitsPixel == 32)
1636 has_alpha = FALSE;
1638 /* If any pixel has a non-zero alpha, ignore hbmMask */
1639 src = (DWORD*)lockeddata.Scan0;
1640 for (x=0; x<width && !has_alpha; x++)
1641 for (y=0; y<height && !has_alpha; y++)
1642 if ((*src++ & 0xff000000) != 0)
1643 has_alpha = TRUE;
1645 else has_alpha = FALSE;
1647 else
1649 GetDIBits(screendc, iinfo.hbmMask, 0, height, lockeddata.Scan0, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1650 has_alpha = FALSE;
1653 if (!has_alpha)
1655 if (iinfo.hbmMask)
1657 BYTE *bits = HeapAlloc(GetProcessHeap(), 0, height * stride);
1659 /* read alpha data from the mask */
1660 if (iinfo.hbmColor)
1661 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1662 else
1663 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1665 src = (DWORD*)bits;
1666 dst_row = lockeddata.Scan0;
1667 for (y=0; y<height; y++)
1669 dst = (DWORD*)dst_row;
1670 for (x=0; x<height; x++)
1672 DWORD src_value = *src++;
1673 if (src_value)
1674 *dst++ = 0;
1675 else
1676 *dst++ |= 0xff000000;
1678 dst_row += lockeddata.Stride;
1681 HeapFree(GetProcessHeap(), 0, bits);
1683 else
1685 /* set constant alpha of 255 */
1686 dst_row = lockeddata.Scan0;
1687 for (y=0; y<height; y++)
1689 dst = (DWORD*)dst_row;
1690 for (x=0; x<height; x++)
1691 *dst++ |= 0xff000000;
1692 dst_row += lockeddata.Stride;
1697 DeleteDC(screendc);
1699 DeleteObject(iinfo.hbmColor);
1700 DeleteObject(iinfo.hbmMask);
1702 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1704 return Ok;
1707 static void generate_halftone_palette(ARGB *entries, UINT count)
1709 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1710 UINT i;
1712 for (i=0; i<8 && i<count; i++)
1714 entries[i] = 0xff000000;
1715 if (i&1) entries[i] |= 0x800000;
1716 if (i&2) entries[i] |= 0x8000;
1717 if (i&4) entries[i] |= 0x80;
1720 if (8 < count)
1721 entries[i] = 0xffc0c0c0;
1723 for (i=9; i<16 && i<count; i++)
1725 entries[i] = 0xff000000;
1726 if (i&1) entries[i] |= 0xff0000;
1727 if (i&2) entries[i] |= 0xff00;
1728 if (i&4) entries[i] |= 0xff;
1731 for (i=16; i<40 && i<count; i++)
1733 entries[i] = 0;
1736 for (i=40; i<256 && i<count; i++)
1738 entries[i] = 0xff000000;
1739 entries[i] |= halftone_values[(i-40)%6];
1740 entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1741 entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1745 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1747 HDC screendc = CreateCompatibleDC(0);
1749 if (!screendc) return GenericError;
1751 *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1752 *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1754 DeleteDC(screendc);
1756 return Ok;
1759 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1760 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1762 HBITMAP hbitmap=NULL;
1763 INT row_size, dib_stride;
1764 BYTE *bits=NULL, *own_bits=NULL;
1765 REAL xres, yres;
1766 GpStatus stat;
1768 TRACE("%d %d %d 0x%x %p %p\n", width, height, stride, format, scan0, bitmap);
1770 if (!bitmap) return InvalidParameter;
1772 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1773 *bitmap = NULL;
1774 return InvalidParameter;
1777 if(scan0 && !stride)
1778 return InvalidParameter;
1780 stat = get_screen_resolution(&xres, &yres);
1781 if (stat != Ok) return stat;
1783 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1784 dib_stride = (row_size + 3) & ~3;
1786 if(stride == 0)
1787 stride = dib_stride;
1789 if (format & PixelFormatGDI && !(format & (PixelFormatAlpha|PixelFormatIndexed)) && !scan0)
1791 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
1792 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
1794 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1795 pbmi->bmiHeader.biWidth = width;
1796 pbmi->bmiHeader.biHeight = -height;
1797 pbmi->bmiHeader.biPlanes = 1;
1798 /* FIXME: use the rest of the data from format */
1799 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(format);
1800 pbmi->bmiHeader.biCompression = BI_RGB;
1801 pbmi->bmiHeader.biSizeImage = 0;
1802 pbmi->bmiHeader.biXPelsPerMeter = 0;
1803 pbmi->bmiHeader.biYPelsPerMeter = 0;
1804 pbmi->bmiHeader.biClrUsed = 0;
1805 pbmi->bmiHeader.biClrImportant = 0;
1807 hbitmap = CreateDIBSection(0, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1809 if (!hbitmap) return GenericError;
1811 stride = dib_stride;
1813 else
1815 /* Not a GDI format; don't try to make an HBITMAP. */
1816 if (scan0)
1817 bits = scan0;
1818 else
1820 INT size = abs(stride) * height;
1822 own_bits = bits = GdipAlloc(size);
1823 if (!own_bits) return OutOfMemory;
1825 if (stride < 0)
1826 bits += stride * (1 - height);
1830 *bitmap = GdipAlloc(sizeof(GpBitmap));
1831 if(!*bitmap)
1833 DeleteObject(hbitmap);
1834 GdipFree(own_bits);
1835 return OutOfMemory;
1838 (*bitmap)->image.type = ImageTypeBitmap;
1839 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1840 (*bitmap)->image.flags = ImageFlagsNone;
1841 (*bitmap)->image.frame_count = 1;
1842 (*bitmap)->image.current_frame = 0;
1843 (*bitmap)->image.palette = NULL;
1844 (*bitmap)->image.xres = xres;
1845 (*bitmap)->image.yres = yres;
1846 (*bitmap)->width = width;
1847 (*bitmap)->height = height;
1848 (*bitmap)->format = format;
1849 (*bitmap)->image.picture = NULL;
1850 (*bitmap)->image.stream = NULL;
1851 (*bitmap)->hbitmap = hbitmap;
1852 (*bitmap)->hdc = NULL;
1853 (*bitmap)->bits = bits;
1854 (*bitmap)->stride = stride;
1855 (*bitmap)->own_bits = own_bits;
1856 (*bitmap)->metadata_reader = NULL;
1857 (*bitmap)->prop_count = 0;
1858 (*bitmap)->prop_item = NULL;
1860 /* set format-related flags */
1861 if (format & (PixelFormatAlpha|PixelFormatPAlpha|PixelFormatIndexed))
1862 (*bitmap)->image.flags |= ImageFlagsHasAlpha;
1864 if (format == PixelFormat1bppIndexed ||
1865 format == PixelFormat4bppIndexed ||
1866 format == PixelFormat8bppIndexed)
1868 (*bitmap)->image.palette = GdipAlloc(sizeof(UINT) * 2 + sizeof(ARGB) * (1 << PIXELFORMATBPP(format)));
1870 if (!(*bitmap)->image.palette)
1872 GdipDisposeImage(&(*bitmap)->image);
1873 *bitmap = NULL;
1874 return OutOfMemory;
1877 (*bitmap)->image.palette->Count = 1 << PIXELFORMATBPP(format);
1879 if (format == PixelFormat1bppIndexed)
1881 (*bitmap)->image.palette->Flags = PaletteFlagsGrayScale;
1882 (*bitmap)->image.palette->Entries[0] = 0xff000000;
1883 (*bitmap)->image.palette->Entries[1] = 0xffffffff;
1885 else
1887 if (format == PixelFormat8bppIndexed)
1888 (*bitmap)->image.palette->Flags = PaletteFlagsHalftone;
1890 generate_halftone_palette((*bitmap)->image.palette->Entries,
1891 (*bitmap)->image.palette->Count);
1895 TRACE("<-- %p\n", *bitmap);
1897 return Ok;
1900 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1901 GpBitmap **bitmap)
1903 GpStatus stat;
1905 TRACE("%p %p\n", stream, bitmap);
1907 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1909 if(stat != Ok)
1910 return stat;
1912 if((*bitmap)->image.type != ImageTypeBitmap){
1913 GdipDisposeImage(&(*bitmap)->image);
1914 *bitmap = NULL;
1915 return GenericError; /* FIXME: what error to return? */
1918 return Ok;
1921 /* FIXME: no icm */
1922 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1923 GpBitmap **bitmap)
1925 TRACE("%p %p\n", stream, bitmap);
1927 return GdipCreateBitmapFromStream(stream, bitmap);
1930 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1931 GpCachedBitmap **cachedbmp)
1933 GpStatus stat;
1935 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1937 if(!bitmap || !graphics || !cachedbmp)
1938 return InvalidParameter;
1940 *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
1941 if(!*cachedbmp)
1942 return OutOfMemory;
1944 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1945 if(stat != Ok){
1946 GdipFree(*cachedbmp);
1947 return stat;
1950 return Ok;
1953 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1955 GpStatus stat;
1956 BitmapData lockeddata;
1957 ULONG andstride, xorstride, bitssize;
1958 LPBYTE andbits, xorbits, androw, xorrow, srcrow;
1959 UINT x, y;
1961 TRACE("(%p, %p)\n", bitmap, hicon);
1963 if (!bitmap || !hicon)
1964 return InvalidParameter;
1966 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead,
1967 PixelFormat32bppPARGB, &lockeddata);
1968 if (stat == Ok)
1970 andstride = ((lockeddata.Width+31)/32)*4;
1971 xorstride = lockeddata.Width*4;
1972 bitssize = (andstride + xorstride) * lockeddata.Height;
1974 andbits = GdipAlloc(bitssize);
1976 if (andbits)
1978 xorbits = andbits + andstride * lockeddata.Height;
1980 for (y=0; y<lockeddata.Height; y++)
1982 srcrow = ((LPBYTE)lockeddata.Scan0) + lockeddata.Stride * y;
1984 androw = andbits + andstride * y;
1985 for (x=0; x<lockeddata.Width; x++)
1986 if (srcrow[3+4*x] >= 128)
1987 androw[x/8] |= 1 << (7-x%8);
1989 xorrow = xorbits + xorstride * y;
1990 memcpy(xorrow, srcrow, xorstride);
1993 *hicon = CreateIcon(NULL, lockeddata.Width, lockeddata.Height, 1, 32,
1994 andbits, xorbits);
1996 GdipFree(andbits);
1998 else
1999 stat = OutOfMemory;
2001 GdipBitmapUnlockBits(bitmap, &lockeddata);
2004 return stat;
2007 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
2009 TRACE("%p\n", cachedbmp);
2011 if(!cachedbmp)
2012 return InvalidParameter;
2014 GdipDisposeImage(cachedbmp->image);
2015 GdipFree(cachedbmp);
2017 return Ok;
2020 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
2021 GpCachedBitmap *cachedbmp, INT x, INT y)
2023 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
2025 if(!graphics || !cachedbmp)
2026 return InvalidParameter;
2028 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
2031 /* Internal utility function: Replace the image data of dst with that of src,
2032 * and free src. */
2033 static void move_bitmap(GpBitmap *dst, GpBitmap *src, BOOL clobber_palette)
2035 assert(src->image.type == ImageTypeBitmap);
2036 assert(dst->image.type == ImageTypeBitmap);
2038 GdipFree(dst->bitmapbits);
2039 GdipFree(dst->own_bits);
2040 DeleteDC(dst->hdc);
2041 DeleteObject(dst->hbitmap);
2043 if (clobber_palette)
2045 GdipFree(dst->image.palette);
2046 dst->image.palette = src->image.palette;
2048 else
2049 GdipFree(src->image.palette);
2051 dst->image.xres = src->image.xres;
2052 dst->image.yres = src->image.yres;
2053 dst->width = src->width;
2054 dst->height = src->height;
2055 dst->format = src->format;
2056 dst->hbitmap = src->hbitmap;
2057 dst->hdc = src->hdc;
2058 dst->bits = src->bits;
2059 dst->stride = src->stride;
2060 dst->own_bits = src->own_bits;
2061 if (dst->metadata_reader)
2062 IWICMetadataReader_Release(dst->metadata_reader);
2063 dst->metadata_reader = src->metadata_reader;
2064 GdipFree(dst->prop_item);
2065 dst->prop_item = src->prop_item;
2066 dst->prop_count = src->prop_count;
2067 if (dst->image.stream)
2068 IStream_Release(dst->image.stream);
2069 dst->image.stream = src->image.stream;
2070 dst->image.frame_count = src->image.frame_count;
2071 dst->image.current_frame = src->image.current_frame;
2072 dst->image.format = src->image.format;
2074 src->image.type = ~0;
2075 GdipFree(src);
2078 static GpStatus free_image_data(GpImage *image)
2080 if(!image)
2081 return InvalidParameter;
2083 if (image->type == ImageTypeBitmap)
2085 GdipFree(((GpBitmap*)image)->bitmapbits);
2086 GdipFree(((GpBitmap*)image)->own_bits);
2087 DeleteDC(((GpBitmap*)image)->hdc);
2088 DeleteObject(((GpBitmap*)image)->hbitmap);
2089 if (((GpBitmap*)image)->metadata_reader)
2090 IWICMetadataReader_Release(((GpBitmap*)image)->metadata_reader);
2091 GdipFree(((GpBitmap*)image)->prop_item);
2093 else if (image->type == ImageTypeMetafile)
2095 GpMetafile *metafile = (GpMetafile*)image;
2096 GdipFree(metafile->comment_data);
2097 DeleteEnhMetaFile(CloseEnhMetaFile(metafile->record_dc));
2098 if (!metafile->preserve_hemf)
2099 DeleteEnhMetaFile(metafile->hemf);
2100 if (metafile->record_graphics)
2102 WARN("metafile closed while recording\n");
2103 /* not sure what to do here; for now just prevent the graphics from functioning or using this object */
2104 metafile->record_graphics->image = NULL;
2105 metafile->record_graphics->busy = TRUE;
2108 else
2110 WARN("invalid image: %p\n", image);
2111 return ObjectBusy;
2113 if (image->picture)
2114 IPicture_Release(image->picture);
2115 if (image->stream)
2116 IStream_Release(image->stream);
2117 GdipFree(image->palette);
2119 return Ok;
2122 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
2124 GpStatus status;
2126 TRACE("%p\n", image);
2128 status = free_image_data(image);
2129 if (status != Ok) return status;
2130 image->type = ~0;
2131 GdipFree(image);
2133 return Ok;
2136 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
2138 static int calls;
2140 TRACE("(%p,%p)\n", image, item);
2142 if(!image || !item)
2143 return InvalidParameter;
2145 if (!(calls++))
2146 FIXME("not implemented\n");
2148 return NotImplemented;
2151 GpStatus WINGDIPAPI GdipGetImageItemData(GpImage *image, ImageItemData *item)
2153 static int calls;
2155 TRACE("(%p,%p)\n", image, item);
2157 if (!(calls++))
2158 FIXME("not implemented\n");
2160 return NotImplemented;
2163 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
2164 GpUnit *srcUnit)
2166 TRACE("%p %p %p\n", image, srcRect, srcUnit);
2168 if(!image || !srcRect || !srcUnit)
2169 return InvalidParameter;
2170 if(image->type == ImageTypeMetafile){
2171 *srcRect = ((GpMetafile*)image)->bounds;
2172 *srcUnit = ((GpMetafile*)image)->unit;
2174 else if(image->type == ImageTypeBitmap){
2175 srcRect->X = srcRect->Y = 0.0;
2176 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
2177 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
2178 *srcUnit = UnitPixel;
2180 else{
2181 srcRect->X = srcRect->Y = 0.0;
2182 srcRect->Width = ipicture_pixel_width(image->picture);
2183 srcRect->Height = ipicture_pixel_height(image->picture);
2184 *srcUnit = UnitPixel;
2187 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
2188 srcRect->Width, srcRect->Height, *srcUnit);
2190 return Ok;
2193 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
2194 REAL *height)
2196 TRACE("%p %p %p\n", image, width, height);
2198 if(!image || !height || !width)
2199 return InvalidParameter;
2201 if(image->type == ImageTypeMetafile){
2202 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2203 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2205 else if(image->type == ImageTypeBitmap){
2206 *height = ((GpBitmap*)image)->height;
2207 *width = ((GpBitmap*)image)->width;
2209 else{
2210 *height = ipicture_pixel_height(image->picture);
2211 *width = ipicture_pixel_width(image->picture);
2214 TRACE("returning (%f, %f)\n", *height, *width);
2215 return Ok;
2218 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
2219 GpGraphics **graphics)
2221 HDC hdc;
2222 GpStatus stat;
2224 TRACE("%p %p\n", image, graphics);
2226 if(!image || !graphics)
2227 return InvalidParameter;
2229 if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
2231 hdc = ((GpBitmap*)image)->hdc;
2233 if(!hdc){
2234 hdc = CreateCompatibleDC(0);
2235 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
2236 ((GpBitmap*)image)->hdc = hdc;
2239 stat = GdipCreateFromHDC(hdc, graphics);
2241 if (stat == Ok)
2243 (*graphics)->image = image;
2244 (*graphics)->xres = image->xres;
2245 (*graphics)->yres = image->yres;
2248 else if (image->type == ImageTypeMetafile)
2249 stat = METAFILE_GetGraphicsContext((GpMetafile*)image, graphics);
2250 else
2251 stat = graphics_from_image(image, graphics);
2253 return stat;
2256 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
2258 TRACE("%p %p\n", image, height);
2260 if(!image || !height)
2261 return InvalidParameter;
2263 if(image->type == ImageTypeMetafile)
2264 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2265 else if(image->type == ImageTypeBitmap)
2266 *height = ((GpBitmap*)image)->height;
2267 else
2268 *height = ipicture_pixel_height(image->picture);
2270 TRACE("returning %d\n", *height);
2272 return Ok;
2275 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2277 if(!image || !res)
2278 return InvalidParameter;
2280 *res = image->xres;
2282 TRACE("(%p) <-- %0.2f\n", image, *res);
2284 return Ok;
2287 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2289 TRACE("%p %p\n", image, size);
2291 if(!image || !size)
2292 return InvalidParameter;
2294 if (!image->palette || image->palette->Count == 0)
2295 *size = sizeof(ColorPalette);
2296 else
2297 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette->Count;
2299 TRACE("<-- %u\n", *size);
2301 return Ok;
2304 /* FIXME: test this function for non-bitmap types */
2305 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2307 TRACE("%p %p\n", image, format);
2309 if(!image || !format)
2310 return InvalidParameter;
2312 if(image->type != ImageTypeBitmap)
2313 *format = PixelFormat24bppRGB;
2314 else
2315 *format = ((GpBitmap*) image)->format;
2317 return Ok;
2320 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2322 TRACE("(%p, %p)\n", image, format);
2324 if(!image || !format)
2325 return InvalidParameter;
2327 memcpy(format, &image->format, sizeof(GUID));
2329 return Ok;
2332 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2334 TRACE("%p %p\n", image, type);
2336 if(!image || !type)
2337 return InvalidParameter;
2339 *type = image->type;
2341 return Ok;
2344 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2346 if(!image || !res)
2347 return InvalidParameter;
2349 *res = image->yres;
2351 TRACE("(%p) <-- %0.2f\n", image, *res);
2353 return Ok;
2356 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2358 TRACE("%p %p\n", image, width);
2360 if(!image || !width)
2361 return InvalidParameter;
2363 if(image->type == ImageTypeMetafile)
2364 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2365 else if(image->type == ImageTypeBitmap)
2366 *width = ((GpBitmap*)image)->width;
2367 else
2368 *width = ipicture_pixel_width(image->picture);
2370 TRACE("returning %d\n", *width);
2372 return Ok;
2375 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT *num)
2377 TRACE("(%p, %p)\n", image, num);
2379 if (!image || !num) return InvalidParameter;
2381 *num = 0;
2383 if (image->type == ImageTypeBitmap)
2385 if (((GpBitmap *)image)->prop_item)
2387 *num = ((GpBitmap *)image)->prop_count;
2388 return Ok;
2391 if (((GpBitmap *)image)->metadata_reader)
2392 IWICMetadataReader_GetCount(((GpBitmap *)image)->metadata_reader, num);
2395 return Ok;
2398 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID *list)
2400 HRESULT hr;
2401 IWICMetadataReader *reader;
2402 IWICEnumMetadataItem *enumerator;
2403 UINT prop_count, i, items_returned;
2405 TRACE("(%p, %u, %p)\n", image, num, list);
2407 if (!image || !list) return InvalidParameter;
2409 if (image->type != ImageTypeBitmap)
2411 FIXME("Not implemented for type %d\n", image->type);
2412 return NotImplemented;
2415 if (((GpBitmap *)image)->prop_item)
2417 if (num != ((GpBitmap *)image)->prop_count) return InvalidParameter;
2419 for (i = 0; i < num; i++)
2421 list[i] = ((GpBitmap *)image)->prop_item[i].id;
2424 return Ok;
2427 reader = ((GpBitmap *)image)->metadata_reader;
2428 if (!reader)
2430 if (num != 0) return InvalidParameter;
2431 return Ok;
2434 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2435 if (FAILED(hr)) return hresult_to_status(hr);
2437 if (num != prop_count) return InvalidParameter;
2439 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2440 if (FAILED(hr)) return hresult_to_status(hr);
2442 IWICEnumMetadataItem_Reset(enumerator);
2444 for (i = 0; i < num; i++)
2446 PROPVARIANT id;
2448 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, NULL, &items_returned);
2449 if (hr != S_OK) break;
2451 if (id.vt != VT_UI2)
2453 FIXME("not supported propvariant type for id: %u\n", id.vt);
2454 list[i] = 0;
2455 continue;
2457 list[i] = id.u.uiVal;
2460 IWICEnumMetadataItem_Release(enumerator);
2462 return hr == S_OK ? Ok : hresult_to_status(hr);
2465 static UINT propvariant_size(PROPVARIANT *value)
2467 switch (value->vt & ~VT_VECTOR)
2469 case VT_EMPTY:
2470 return 0;
2471 case VT_I1:
2472 case VT_UI1:
2473 if (!(value->vt & VT_VECTOR)) return 1;
2474 return value->u.caub.cElems;
2475 case VT_I2:
2476 case VT_UI2:
2477 if (!(value->vt & VT_VECTOR)) return 2;
2478 return value->u.caui.cElems * 2;
2479 case VT_I4:
2480 case VT_UI4:
2481 case VT_R4:
2482 if (!(value->vt & VT_VECTOR)) return 4;
2483 return value->u.caul.cElems * 4;
2484 case VT_I8:
2485 case VT_UI8:
2486 case VT_R8:
2487 if (!(value->vt & VT_VECTOR)) return 8;
2488 return value->u.cauh.cElems * 8;
2489 case VT_LPSTR:
2490 return value->u.pszVal ? strlen(value->u.pszVal) + 1 : 0;
2491 case VT_BLOB:
2492 return value->u.blob.cbSize;
2493 default:
2494 FIXME("not supported variant type %d\n", value->vt);
2495 return 0;
2499 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID propid, UINT *size)
2501 HRESULT hr;
2502 IWICMetadataReader *reader;
2503 PROPVARIANT id, value;
2505 TRACE("(%p,%#x,%p)\n", image, propid, size);
2507 if (!size || !image) return InvalidParameter;
2509 if (image->type != ImageTypeBitmap)
2511 FIXME("Not implemented for type %d\n", image->type);
2512 return NotImplemented;
2515 if (((GpBitmap *)image)->prop_item)
2517 UINT i;
2519 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2521 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2523 *size = sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2524 return Ok;
2528 return PropertyNotFound;
2531 reader = ((GpBitmap *)image)->metadata_reader;
2532 if (!reader) return PropertyNotFound;
2534 id.vt = VT_UI2;
2535 id.u.uiVal = propid;
2536 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2537 if (FAILED(hr)) return PropertyNotFound;
2539 *size = propvariant_size(&value);
2540 if (*size) *size += sizeof(PropertyItem);
2541 PropVariantClear(&value);
2543 return Ok;
2546 #ifndef PropertyTagTypeSByte
2547 #define PropertyTagTypeSByte 6
2548 #define PropertyTagTypeSShort 8
2549 #define PropertyTagTypeFloat 11
2550 #define PropertyTagTypeDouble 12
2551 #endif
2553 static UINT vt_to_itemtype(UINT vt)
2555 static const struct
2557 UINT vt, type;
2558 } vt2type[] =
2560 { VT_I1, PropertyTagTypeSByte },
2561 { VT_UI1, PropertyTagTypeByte },
2562 { VT_I2, PropertyTagTypeSShort },
2563 { VT_UI2, PropertyTagTypeShort },
2564 { VT_I4, PropertyTagTypeSLONG },
2565 { VT_UI4, PropertyTagTypeLong },
2566 { VT_I8, PropertyTagTypeSRational },
2567 { VT_UI8, PropertyTagTypeRational },
2568 { VT_R4, PropertyTagTypeFloat },
2569 { VT_R8, PropertyTagTypeDouble },
2570 { VT_LPSTR, PropertyTagTypeASCII },
2571 { VT_BLOB, PropertyTagTypeUndefined }
2573 UINT i;
2574 for (i = 0; i < sizeof(vt2type)/sizeof(vt2type[0]); i++)
2576 if (vt2type[i].vt == vt) return vt2type[i].type;
2578 FIXME("not supported variant type %u\n", vt);
2579 return 0;
2582 static GpStatus propvariant_to_item(PROPVARIANT *value, PropertyItem *item,
2583 UINT size, PROPID id)
2585 UINT item_size, item_type;
2587 item_size = propvariant_size(value);
2588 if (size != item_size + sizeof(PropertyItem)) return InvalidParameter;
2590 item_type = vt_to_itemtype(value->vt & ~VT_VECTOR);
2591 if (!item_type) return InvalidParameter;
2593 item->value = item + 1;
2595 switch (value->vt & ~VT_VECTOR)
2597 case VT_I1:
2598 case VT_UI1:
2599 if (!(value->vt & VT_VECTOR))
2600 *(BYTE *)item->value = value->u.bVal;
2601 else
2602 memcpy(item->value, value->u.caub.pElems, item_size);
2603 break;
2604 case VT_I2:
2605 case VT_UI2:
2606 if (!(value->vt & VT_VECTOR))
2607 *(USHORT *)item->value = value->u.uiVal;
2608 else
2609 memcpy(item->value, value->u.caui.pElems, item_size);
2610 break;
2611 case VT_I4:
2612 case VT_UI4:
2613 case VT_R4:
2614 if (!(value->vt & VT_VECTOR))
2615 *(ULONG *)item->value = value->u.ulVal;
2616 else
2617 memcpy(item->value, value->u.caul.pElems, item_size);
2618 break;
2619 case VT_I8:
2620 case VT_UI8:
2621 case VT_R8:
2622 if (!(value->vt & VT_VECTOR))
2623 *(ULONGLONG *)item->value = value->u.uhVal.QuadPart;
2624 else
2625 memcpy(item->value, value->u.cauh.pElems, item_size);
2626 break;
2627 case VT_LPSTR:
2628 memcpy(item->value, value->u.pszVal, item_size);
2629 break;
2630 case VT_BLOB:
2631 memcpy(item->value, value->u.blob.pBlobData, item_size);
2632 break;
2633 default:
2634 FIXME("not supported variant type %d\n", value->vt);
2635 return InvalidParameter;
2638 item->length = item_size;
2639 item->type = item_type;
2640 item->id = id;
2642 return Ok;
2645 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID propid, UINT size,
2646 PropertyItem *buffer)
2648 GpStatus stat;
2649 HRESULT hr;
2650 IWICMetadataReader *reader;
2651 PROPVARIANT id, value;
2653 TRACE("(%p,%#x,%u,%p)\n", image, propid, size, buffer);
2655 if (!image || !buffer) return InvalidParameter;
2657 if (image->type != ImageTypeBitmap)
2659 FIXME("Not implemented for type %d\n", image->type);
2660 return NotImplemented;
2663 if (((GpBitmap *)image)->prop_item)
2665 UINT i;
2667 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2669 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2671 if (size != sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length)
2672 return InvalidParameter;
2674 *buffer = ((GpBitmap *)image)->prop_item[i];
2675 buffer->value = buffer + 1;
2676 memcpy(buffer->value, ((GpBitmap *)image)->prop_item[i].value, buffer->length);
2677 return Ok;
2681 return PropertyNotFound;
2684 reader = ((GpBitmap *)image)->metadata_reader;
2685 if (!reader) return PropertyNotFound;
2687 id.vt = VT_UI2;
2688 id.u.uiVal = propid;
2689 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2690 if (FAILED(hr)) return PropertyNotFound;
2692 stat = propvariant_to_item(&value, buffer, size, propid);
2693 PropVariantClear(&value);
2695 return stat;
2698 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT *size, UINT *count)
2700 HRESULT hr;
2701 IWICMetadataReader *reader;
2702 IWICEnumMetadataItem *enumerator;
2703 UINT prop_count, prop_size, i;
2704 PROPVARIANT id, value;
2706 TRACE("(%p,%p,%p)\n", image, size, count);
2708 if (!image || !size || !count) return InvalidParameter;
2710 if (image->type != ImageTypeBitmap)
2712 FIXME("Not implemented for type %d\n", image->type);
2713 return NotImplemented;
2716 if (((GpBitmap *)image)->prop_item)
2718 *count = ((GpBitmap *)image)->prop_count;
2719 *size = 0;
2721 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2723 *size += sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2726 return Ok;
2729 reader = ((GpBitmap *)image)->metadata_reader;
2730 if (!reader) return PropertyNotFound;
2732 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2733 if (FAILED(hr)) return hresult_to_status(hr);
2735 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2736 if (FAILED(hr)) return hresult_to_status(hr);
2738 IWICEnumMetadataItem_Reset(enumerator);
2740 prop_size = 0;
2742 PropVariantInit(&id);
2743 PropVariantInit(&value);
2745 for (i = 0; i < prop_count; i++)
2747 UINT items_returned, item_size;
2749 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2750 if (hr != S_OK) break;
2752 item_size = propvariant_size(&value);
2753 if (item_size) prop_size += sizeof(PropertyItem) + item_size;
2755 PropVariantClear(&id);
2756 PropVariantClear(&value);
2759 IWICEnumMetadataItem_Release(enumerator);
2761 if (hr != S_OK) return PropertyNotFound;
2763 *count = prop_count;
2764 *size = prop_size;
2765 return Ok;
2768 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2769 UINT count, PropertyItem *buf)
2771 GpStatus status;
2772 HRESULT hr;
2773 IWICMetadataReader *reader;
2774 IWICEnumMetadataItem *enumerator;
2775 UINT prop_count, prop_size, i;
2776 PROPVARIANT id, value;
2777 char *item_value;
2779 TRACE("(%p,%u,%u,%p)\n", image, size, count, buf);
2781 if (!image || !buf) return InvalidParameter;
2783 if (image->type != ImageTypeBitmap)
2785 FIXME("Not implemented for type %d\n", image->type);
2786 return NotImplemented;
2789 status = GdipGetPropertySize(image, &prop_size, &prop_count);
2790 if (status != Ok) return status;
2792 if (prop_count != count || prop_size != size) return InvalidParameter;
2794 if (((GpBitmap *)image)->prop_item)
2796 memcpy(buf, ((GpBitmap *)image)->prop_item, prop_size);
2798 item_value = (char *)(buf + prop_count);
2800 for (i = 0; i < prop_count; i++)
2802 buf[i].value = item_value;
2803 item_value += buf[i].length;
2806 return Ok;
2809 reader = ((GpBitmap *)image)->metadata_reader;
2810 if (!reader) return PropertyNotFound;
2812 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2813 if (FAILED(hr)) return hresult_to_status(hr);
2815 IWICEnumMetadataItem_Reset(enumerator);
2817 item_value = (char *)(buf + prop_count);
2819 PropVariantInit(&id);
2820 PropVariantInit(&value);
2822 for (i = 0; i < prop_count; i++)
2824 PropertyItem *item;
2825 UINT items_returned, item_size;
2827 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2828 if (hr != S_OK) break;
2830 if (id.vt != VT_UI2)
2832 FIXME("not supported propvariant type for id: %u\n", id.vt);
2833 continue;
2836 item_size = propvariant_size(&value);
2837 if (item_size)
2839 item = HeapAlloc(GetProcessHeap(), 0, item_size + sizeof(*item));
2841 propvariant_to_item(&value, item, item_size + sizeof(*item), id.u.uiVal);
2842 buf[i].id = item->id;
2843 buf[i].type = item->type;
2844 buf[i].length = item_size;
2845 buf[i].value = item_value;
2846 memcpy(item_value, item->value, item_size);
2847 item_value += item_size;
2849 HeapFree(GetProcessHeap(), 0, item);
2852 PropVariantClear(&id);
2853 PropVariantClear(&value);
2856 IWICEnumMetadataItem_Release(enumerator);
2858 if (hr != S_OK) return PropertyNotFound;
2860 return Ok;
2863 struct image_format_dimension
2865 const GUID *format;
2866 const GUID *dimension;
2869 static const struct image_format_dimension image_format_dimensions[] =
2871 {&ImageFormatGIF, &FrameDimensionTime},
2872 {&ImageFormatIcon, &FrameDimensionResolution},
2873 {NULL}
2876 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
2877 GDIPCONST GUID* dimensionID, UINT* count)
2879 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
2881 if(!image || !count)
2882 return InvalidParameter;
2884 if (!dimensionID ||
2885 IsEqualGUID(dimensionID, &image->format) ||
2886 IsEqualGUID(dimensionID, &FrameDimensionPage) ||
2887 IsEqualGUID(dimensionID, &FrameDimensionTime))
2889 *count = image->frame_count;
2890 return Ok;
2893 return InvalidParameter;
2896 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
2897 UINT* count)
2899 TRACE("(%p, %p)\n", image, count);
2901 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2903 if(!image || !count)
2904 return InvalidParameter;
2906 *count = 1;
2908 return Ok;
2911 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
2912 GUID* dimensionIDs, UINT count)
2914 int i;
2915 const GUID *result=NULL;
2917 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
2919 if(!image || !dimensionIDs || count != 1)
2920 return InvalidParameter;
2922 for (i=0; image_format_dimensions[i].format; i++)
2924 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
2926 result = image_format_dimensions[i].dimension;
2927 break;
2931 if (!result)
2932 result = &FrameDimensionPage;
2934 memcpy(dimensionIDs, result, sizeof(GUID));
2936 return Ok;
2939 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
2940 GpImage **image)
2942 GpStatus stat;
2943 IStream *stream;
2945 TRACE("(%s) %p\n", debugstr_w(filename), image);
2947 if (!filename || !image)
2948 return InvalidParameter;
2950 *image = NULL;
2952 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
2954 if (stat != Ok)
2955 return stat;
2957 stat = GdipLoadImageFromStream(stream, image);
2959 IStream_Release(stream);
2961 return stat;
2964 /* FIXME: no icm handling */
2965 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
2967 TRACE("(%s) %p\n", debugstr_w(filename), image);
2969 return GdipLoadImageFromFile(filename, image);
2972 static void add_property(GpBitmap *bitmap, PropertyItem *item)
2974 UINT prop_size, prop_count;
2975 PropertyItem *prop_item;
2977 if (bitmap->prop_item == NULL)
2979 prop_size = prop_count = 0;
2980 prop_item = GdipAlloc(item->length + sizeof(PropertyItem));
2981 if (!prop_item) return;
2983 else
2985 UINT i;
2986 char *item_value;
2988 GdipGetPropertySize((GpImage *)bitmap, &prop_size, &prop_count);
2990 prop_item = GdipAlloc(prop_size + item->length + sizeof(PropertyItem));
2991 if (!prop_item) return;
2992 memcpy(prop_item, bitmap->prop_item, sizeof(PropertyItem) * bitmap->prop_count);
2993 prop_size -= sizeof(PropertyItem) * bitmap->prop_count;
2994 memcpy(prop_item + prop_count + 1, bitmap->prop_item + prop_count, prop_size);
2996 item_value = (char *)(prop_item + prop_count + 1);
2998 for (i = 0; i < prop_count; i++)
3000 prop_item[i].value = item_value;
3001 item_value += prop_item[i].length;
3005 prop_item[prop_count].id = item->id;
3006 prop_item[prop_count].type = item->type;
3007 prop_item[prop_count].length = item->length;
3008 prop_item[prop_count].value = (char *)(prop_item + prop_count + 1) + prop_size;
3009 memcpy(prop_item[prop_count].value, item->value, item->length);
3011 GdipFree(bitmap->prop_item);
3012 bitmap->prop_item = prop_item;
3013 bitmap->prop_count++;
3016 static BOOL get_bool_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3018 HRESULT hr;
3019 GUID format;
3020 PROPVARIANT id, value;
3021 BOOL ret = FALSE;
3023 IWICMetadataReader_GetMetadataFormat(reader, &format);
3024 if (!IsEqualGUID(&format, guid)) return FALSE;
3026 PropVariantInit(&id);
3027 PropVariantInit(&value);
3029 id.vt = VT_LPWSTR;
3030 id.u.pwszVal = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3031 if (!id.u.pwszVal) return FALSE;
3032 lstrcpyW(id.u.pwszVal, prop_name);
3033 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3034 if (hr == S_OK && value.vt == VT_BOOL)
3035 ret = value.u.boolVal;
3037 PropVariantClear(&id);
3038 PropVariantClear(&value);
3040 return ret;
3043 static PropertyItem *get_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3045 HRESULT hr;
3046 GUID format;
3047 PROPVARIANT id, value;
3048 PropertyItem *item = NULL;
3050 IWICMetadataReader_GetMetadataFormat(reader, &format);
3051 if (!IsEqualGUID(&format, guid)) return NULL;
3053 PropVariantInit(&id);
3054 PropVariantInit(&value);
3056 id.vt = VT_LPWSTR;
3057 id.u.pwszVal = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3058 if (!id.u.pwszVal) return NULL;
3059 lstrcpyW(id.u.pwszVal, prop_name);
3060 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3061 if (hr == S_OK)
3063 UINT item_size = propvariant_size(&value);
3064 if (item_size)
3066 item_size += sizeof(*item);
3067 item = GdipAlloc(item_size);
3068 if (propvariant_to_item(&value, item, item_size, 0) != Ok)
3070 GdipFree(item);
3071 item = NULL;
3076 PropVariantClear(&id);
3077 PropVariantClear(&value);
3079 return item;
3082 static PropertyItem *get_gif_comment(IWICMetadataReader *reader)
3084 static const WCHAR textentryW[] = { 'T','e','x','t','E','n','t','r','y',0 };
3085 PropertyItem *comment;
3087 comment = get_property(reader, &GUID_MetadataFormatGifComment, textentryW);
3088 if (comment)
3089 comment->id = PropertyTagExifUserComment;
3091 return comment;
3094 static PropertyItem *get_gif_loopcount(IWICMetadataReader *reader)
3096 static const WCHAR applicationW[] = { 'A','p','p','l','i','c','a','t','i','o','n',0 };
3097 static const WCHAR dataW[] = { 'D','a','t','a',0 };
3098 PropertyItem *appext = NULL, *appdata = NULL, *loop = NULL;
3100 appext = get_property(reader, &GUID_MetadataFormatAPE, applicationW);
3101 if (appext)
3103 if (appext->type == PropertyTagTypeByte && appext->length == 11 &&
3104 (!memcmp(appext->value, "NETSCAPE2.0", 11) || !memcmp(appext->value, "ANIMEXTS1.0", 11)))
3106 appdata = get_property(reader, &GUID_MetadataFormatAPE, dataW);
3107 if (appdata)
3109 if (appdata->type == PropertyTagTypeByte && appdata->length == 4)
3111 BYTE *data = appdata->value;
3112 if (data[0] == 3 && data[1] == 1)
3114 loop = GdipAlloc(sizeof(*loop) + sizeof(SHORT));
3115 if (loop)
3117 loop->type = PropertyTagTypeShort;
3118 loop->id = PropertyTagLoopCount;
3119 loop->length = sizeof(SHORT);
3120 loop->value = loop + 1;
3121 *(SHORT *)loop->value = data[2] | (data[3] << 8);
3129 GdipFree(appext);
3130 GdipFree(appdata);
3132 return loop;
3135 static PropertyItem *get_gif_background(IWICMetadataReader *reader)
3137 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 };
3138 PropertyItem *background;
3140 background = get_property(reader, &GUID_MetadataFormatLSD, backgroundW);
3141 if (background)
3142 background->id = PropertyTagIndexBackground;
3144 return background;
3147 static PropertyItem *get_gif_palette(IWICBitmapDecoder *decoder, IWICMetadataReader *reader)
3149 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 };
3150 HRESULT hr;
3151 IWICImagingFactory *factory;
3152 IWICPalette *palette;
3153 UINT count = 0;
3154 WICColor colors[256];
3156 if (!get_bool_property(reader, &GUID_MetadataFormatLSD, global_flagW))
3157 return NULL;
3159 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
3160 &IID_IWICImagingFactory, (void **)&factory);
3161 if (hr != S_OK) return NULL;
3163 hr = IWICImagingFactory_CreatePalette(factory, &palette);
3164 if (hr == S_OK)
3166 hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
3167 if (hr == S_OK)
3168 IWICPalette_GetColors(palette, 256, colors, &count);
3170 IWICPalette_Release(palette);
3173 IWICImagingFactory_Release(factory);
3175 if (count)
3177 PropertyItem *pal;
3178 UINT i;
3179 BYTE *rgb;
3181 pal = GdipAlloc(sizeof(*pal) + count * 3);
3182 if (!pal) return NULL;
3183 pal->type = PropertyTagTypeByte;
3184 pal->id = PropertyTagGlobalPalette;
3185 pal->value = pal + 1;
3186 pal->length = count * 3;
3188 rgb = pal->value;
3190 for (i = 0; i < count; i++)
3192 rgb[i*3] = (colors[i] >> 16) & 0xff;
3193 rgb[i*3 + 1] = (colors[i] >> 8) & 0xff;
3194 rgb[i*3 + 2] = colors[i] & 0xff;
3197 return pal;
3200 return NULL;
3203 static PropertyItem *get_gif_transparent_idx(IWICMetadataReader *reader)
3205 static const WCHAR transparency_flagW[] = { 'T','r','a','n','s','p','a','r','e','n','c','y','F','l','a','g',0 };
3206 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 };
3207 PropertyItem *index = NULL;
3209 if (get_bool_property(reader, &GUID_MetadataFormatGCE, transparency_flagW))
3211 index = get_property(reader, &GUID_MetadataFormatGCE, colorW);
3212 if (index)
3213 index->id = PropertyTagIndexTransparent;
3215 return index;
3218 static LONG get_gif_frame_delay(IWICBitmapFrameDecode *frame)
3220 static const WCHAR delayW[] = { 'D','e','l','a','y',0 };
3221 HRESULT hr;
3222 IWICMetadataBlockReader *block_reader;
3223 IWICMetadataReader *reader;
3224 UINT block_count, i;
3225 PropertyItem *delay;
3226 LONG value = 0;
3228 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3229 if (hr == S_OK)
3231 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3232 if (hr == S_OK)
3234 for (i = 0; i < block_count; i++)
3236 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3237 if (hr == S_OK)
3239 delay = get_property(reader, &GUID_MetadataFormatGCE, delayW);
3240 if (delay)
3242 if (delay->type == PropertyTagTypeShort && delay->length == 2)
3243 value = *(SHORT *)delay->value;
3245 GdipFree(delay);
3247 IWICMetadataReader_Release(reader);
3251 IWICMetadataBlockReader_Release(block_reader);
3254 return value;
3257 static void gif_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3259 HRESULT hr;
3260 IWICBitmapFrameDecode *frame;
3261 IWICMetadataBlockReader *block_reader;
3262 IWICMetadataReader *reader;
3263 UINT frame_count, block_count, i;
3264 PropertyItem *delay = NULL, *comment = NULL, *background = NULL;
3265 PropertyItem *transparent_idx = NULL, *loop = NULL, *palette = NULL;
3267 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3268 if (frame_count > 1)
3270 delay = GdipAlloc(sizeof(*delay) + frame_count * sizeof(LONG));
3271 if (delay)
3273 LONG *value;
3275 delay->type = PropertyTagTypeLong;
3276 delay->id = PropertyTagFrameDelay;
3277 delay->length = frame_count * sizeof(LONG);
3278 delay->value = delay + 1;
3280 value = delay->value;
3282 for (i = 0; i < frame_count; i++)
3284 hr = IWICBitmapDecoder_GetFrame(decoder, i, &frame);
3285 if (hr == S_OK)
3287 value[i] = get_gif_frame_delay(frame);
3288 IWICBitmapFrameDecode_Release(frame);
3290 else value[i] = 0;
3295 hr = IWICBitmapDecoder_QueryInterface(decoder, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3296 if (hr == S_OK)
3298 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3299 if (hr == S_OK)
3301 for (i = 0; i < block_count; i++)
3303 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3304 if (hr == S_OK)
3306 if (!comment)
3307 comment = get_gif_comment(reader);
3309 if (frame_count > 1 && !loop)
3310 loop = get_gif_loopcount(reader);
3312 if (!background)
3313 background = get_gif_background(reader);
3315 if (!palette)
3316 palette = get_gif_palette(decoder, reader);
3318 IWICMetadataReader_Release(reader);
3322 IWICMetadataBlockReader_Release(block_reader);
3325 if (frame_count > 1 && !loop)
3327 loop = GdipAlloc(sizeof(*loop) + sizeof(SHORT));
3328 if (loop)
3330 loop->type = PropertyTagTypeShort;
3331 loop->id = PropertyTagLoopCount;
3332 loop->length = sizeof(SHORT);
3333 loop->value = loop + 1;
3334 *(SHORT *)loop->value = 1;
3338 if (delay) add_property(bitmap, delay);
3339 if (comment) add_property(bitmap, comment);
3340 if (loop) add_property(bitmap, loop);
3341 if (palette) add_property(bitmap, palette);
3342 if (background) add_property(bitmap, background);
3344 GdipFree(delay);
3345 GdipFree(comment);
3346 GdipFree(loop);
3347 GdipFree(palette);
3348 GdipFree(background);
3350 /* Win7 gdiplus always returns transparent color index from frame 0 */
3351 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
3352 if (hr != S_OK) return;
3354 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3355 if (hr == S_OK)
3357 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3358 if (hr == S_OK)
3360 for (i = 0; i < block_count; i++)
3362 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3363 if (hr == S_OK)
3365 if (!transparent_idx)
3366 transparent_idx = get_gif_transparent_idx(reader);
3368 IWICMetadataReader_Release(reader);
3372 IWICMetadataBlockReader_Release(block_reader);
3375 if (transparent_idx) add_property(bitmap, transparent_idx);
3376 GdipFree(transparent_idx);
3378 IWICBitmapFrameDecode_Release(frame);
3381 typedef void (*metadata_reader_func)(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT frame);
3383 static GpStatus decode_image_wic(IStream *stream, GDIPCONST CLSID *clsid,
3384 UINT active_frame, metadata_reader_func metadata_reader, GpImage **image)
3386 GpStatus status=Ok;
3387 GpBitmap *bitmap;
3388 HRESULT hr;
3389 IWICBitmapDecoder *decoder;
3390 IWICBitmapFrameDecode *frame;
3391 IWICBitmapSource *source=NULL;
3392 IWICMetadataBlockReader *block_reader;
3393 WICPixelFormatGUID wic_format;
3394 PixelFormat gdip_format=0;
3395 ColorPalette *palette = NULL;
3396 WICBitmapPaletteType palette_type = WICBitmapPaletteTypeFixedHalftone256;
3397 int i;
3398 UINT width, height, frame_count;
3399 BitmapData lockeddata;
3400 WICRect wrc;
3401 HRESULT initresult;
3403 TRACE("%p,%s,%u,%p\n", stream, wine_dbgstr_guid(clsid), active_frame, image);
3405 initresult = CoInitialize(NULL);
3407 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
3408 &IID_IWICBitmapDecoder, (void**)&decoder);
3409 if (FAILED(hr)) goto end;
3411 hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
3412 if (SUCCEEDED(hr))
3414 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3415 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3418 if (SUCCEEDED(hr)) /* got frame */
3420 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
3422 if (SUCCEEDED(hr))
3424 IWICBitmapSource *bmp_source;
3425 IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICBitmapSource, (void **)&bmp_source);
3427 for (i=0; pixel_formats[i].wic_format; i++)
3429 if (IsEqualGUID(&wic_format, pixel_formats[i].wic_format))
3431 source = bmp_source;
3432 gdip_format = pixel_formats[i].gdip_format;
3433 palette_type = pixel_formats[i].palette_type;
3434 break;
3437 if (!source)
3439 /* unknown format; fall back on 32bppARGB */
3440 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, bmp_source, &source);
3441 gdip_format = PixelFormat32bppARGB;
3442 IWICBitmapSource_Release(bmp_source);
3444 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format), gdip_format);
3447 if (SUCCEEDED(hr)) /* got source */
3449 hr = IWICBitmapSource_GetSize(source, &width, &height);
3451 if (SUCCEEDED(hr))
3452 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
3453 NULL, &bitmap);
3455 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
3457 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
3458 gdip_format, &lockeddata);
3459 if (status == Ok) /* locked bitmap */
3461 wrc.X = 0;
3462 wrc.Width = width;
3463 wrc.Height = 1;
3464 for (i=0; i<height; i++)
3466 wrc.Y = i;
3467 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
3468 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
3469 if (FAILED(hr)) break;
3472 GdipBitmapUnlockBits(bitmap, &lockeddata);
3475 if (SUCCEEDED(hr) && status == Ok)
3476 *image = (GpImage*)bitmap;
3477 else
3479 *image = NULL;
3480 GdipDisposeImage((GpImage*)bitmap);
3483 if (SUCCEEDED(hr) && status == Ok)
3485 double dpix, dpiy;
3486 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
3487 if (SUCCEEDED(hr))
3489 bitmap->image.xres = dpix;
3490 bitmap->image.yres = dpiy;
3492 hr = S_OK;
3496 IWICBitmapSource_Release(source);
3499 if (SUCCEEDED(hr)) {
3500 bitmap->metadata_reader = NULL;
3502 if (metadata_reader)
3503 metadata_reader(bitmap, decoder, active_frame);
3504 else if (IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader) == S_OK)
3506 UINT block_count = 0;
3507 if (IWICMetadataBlockReader_GetCount(block_reader, &block_count) == S_OK && block_count)
3508 IWICMetadataBlockReader_GetReaderByIndex(block_reader, 0, &bitmap->metadata_reader);
3509 IWICMetadataBlockReader_Release(block_reader);
3512 palette = get_palette(frame, palette_type);
3513 IWICBitmapFrameDecode_Release(frame);
3517 IWICBitmapDecoder_Release(decoder);
3519 end:
3520 if (SUCCEEDED(initresult)) CoUninitialize();
3522 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
3524 if (status == Ok)
3526 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3527 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI|ImageFlagsColorSpaceRGB;
3528 bitmap->image.frame_count = frame_count;
3529 bitmap->image.current_frame = active_frame;
3530 bitmap->image.stream = stream;
3531 if (palette)
3533 GdipFree(bitmap->image.palette);
3534 bitmap->image.palette = palette;
3536 else
3538 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormatBlackWhite))
3539 bitmap->image.palette->Flags = 0;
3541 /* Pin the source stream */
3542 IStream_AddRef(stream);
3543 TRACE("=> %p\n", *image);
3546 return status;
3549 static GpStatus decode_image_icon(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3551 return decode_image_wic(stream, &CLSID_WICIcoDecoder, active_frame, NULL, image);
3554 static GpStatus decode_image_bmp(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3556 GpStatus status;
3557 GpBitmap* bitmap;
3559 status = decode_image_wic(stream, &CLSID_WICBmpDecoder, active_frame, NULL, image);
3561 bitmap = (GpBitmap*)*image;
3563 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
3565 /* WIC supports bmp files with alpha, but gdiplus does not */
3566 bitmap->format = PixelFormat32bppRGB;
3569 return status;
3572 static GpStatus decode_image_jpeg(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3574 return decode_image_wic(stream, &CLSID_WICJpegDecoder, active_frame, NULL, image);
3577 static GpStatus decode_image_png(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3579 return decode_image_wic(stream, &CLSID_WICPngDecoder, active_frame, NULL, image);
3582 static GpStatus decode_image_gif(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3584 return decode_image_wic(stream, &CLSID_WICGifDecoder, active_frame, gif_metadata_reader, image);
3587 static GpStatus decode_image_tiff(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3589 return decode_image_wic(stream, &CLSID_WICTiffDecoder, active_frame, NULL, image);
3592 static GpStatus decode_image_olepicture_metafile(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3594 IPicture *pic;
3596 TRACE("%p %p\n", stream, image);
3598 if(!stream || !image)
3599 return InvalidParameter;
3601 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
3602 (LPVOID*) &pic) != S_OK){
3603 TRACE("Could not load picture\n");
3604 return GenericError;
3607 /* FIXME: missing initialization code */
3608 *image = GdipAlloc(sizeof(GpMetafile));
3609 if(!*image) return OutOfMemory;
3610 (*image)->type = ImageTypeMetafile;
3611 (*image)->stream = NULL;
3612 (*image)->picture = pic;
3613 (*image)->flags = ImageFlagsNone;
3614 (*image)->frame_count = 1;
3615 (*image)->current_frame = 0;
3616 (*image)->palette = NULL;
3618 TRACE("<-- %p\n", *image);
3620 return Ok;
3623 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
3624 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
3626 typedef GpStatus (*decode_image_func)(IStream *stream, REFCLSID clsid, UINT active_frame, GpImage **image);
3628 typedef struct image_codec {
3629 ImageCodecInfo info;
3630 encode_image_func encode_func;
3631 decode_image_func decode_func;
3632 } image_codec;
3634 typedef enum {
3635 BMP,
3636 JPEG,
3637 GIF,
3638 TIFF,
3639 EMF,
3640 WMF,
3641 PNG,
3642 ICO,
3643 NUM_CODECS
3644 } ImageFormat;
3646 static const struct image_codec codecs[NUM_CODECS];
3648 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
3650 BYTE signature[8];
3651 const BYTE *pattern, *mask;
3652 LARGE_INTEGER seek;
3653 HRESULT hr;
3654 UINT bytesread;
3655 int i;
3656 DWORD j, sig;
3658 /* seek to the start of the stream */
3659 seek.QuadPart = 0;
3660 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3661 if (FAILED(hr)) return hresult_to_status(hr);
3663 /* read the first 8 bytes */
3664 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
3665 hr = IStream_Read(stream, signature, 8, &bytesread);
3666 if (FAILED(hr)) return hresult_to_status(hr);
3667 if (hr == S_FALSE || bytesread == 0) return GenericError;
3669 for (i = 0; i < NUM_CODECS; i++) {
3670 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
3671 bytesread >= codecs[i].info.SigSize)
3673 for (sig=0; sig<codecs[i].info.SigCount; sig++)
3675 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
3676 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
3677 for (j=0; j<codecs[i].info.SigSize; j++)
3678 if ((signature[j] & mask[j]) != pattern[j])
3679 break;
3680 if (j == codecs[i].info.SigSize)
3682 *result = &codecs[i];
3683 return Ok;
3689 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
3690 signature[0],signature[1],signature[2],signature[3],
3691 signature[4],signature[5],signature[6],signature[7]);
3693 return GenericError;
3696 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image, GDIPCONST GUID *dimensionID,
3697 UINT frame)
3699 GpStatus stat;
3700 LARGE_INTEGER seek;
3701 HRESULT hr;
3702 const struct image_codec *codec = NULL;
3703 GpImage *new_image;
3705 TRACE("(%p,%s,%u)\n", image, debugstr_guid(dimensionID), frame);
3707 if (!image || !dimensionID)
3708 return InvalidParameter;
3710 if (frame >= image->frame_count)
3712 WARN("requested frame %u, but image has only %u\n", frame, image->frame_count);
3713 return InvalidParameter;
3716 if (image->type != ImageTypeBitmap && image->type != ImageTypeMetafile)
3718 WARN("invalid image type %d\n", image->type);
3719 return InvalidParameter;
3722 if (image->current_frame == frame)
3723 return Ok;
3725 if (!image->stream)
3727 TRACE("image doesn't have an associated stream\n");
3728 return Ok;
3731 /* choose an appropriate image decoder */
3732 stat = get_decoder_info(image->stream, &codec);
3733 if (stat != Ok)
3735 WARN("can't find decoder info\n");
3736 return stat;
3739 /* seek to the start of the stream */
3740 seek.QuadPart = 0;
3741 hr = IStream_Seek(image->stream, seek, STREAM_SEEK_SET, NULL);
3742 if (FAILED(hr))
3743 return hresult_to_status(hr);
3745 /* call on the image decoder to do the real work */
3746 stat = codec->decode_func(image->stream, &codec->info.Clsid, frame, &new_image);
3748 if (stat == Ok)
3750 memcpy(&new_image->format, &codec->info.FormatID, sizeof(GUID));
3751 free_image_data(image);
3752 if (image->type == ImageTypeBitmap)
3753 *(GpBitmap *)image = *(GpBitmap *)new_image;
3754 else if (image->type == ImageTypeMetafile)
3755 *(GpMetafile *)image = *(GpMetafile *)new_image;
3756 new_image->type = ~0;
3757 GdipFree(new_image);
3758 return Ok;
3761 return stat;
3764 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream *stream, GpImage **image)
3766 GpStatus stat;
3767 LARGE_INTEGER seek;
3768 HRESULT hr;
3769 const struct image_codec *codec=NULL;
3771 /* choose an appropriate image decoder */
3772 stat = get_decoder_info(stream, &codec);
3773 if (stat != Ok) return stat;
3775 /* seek to the start of the stream */
3776 seek.QuadPart = 0;
3777 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3778 if (FAILED(hr)) return hresult_to_status(hr);
3780 /* call on the image decoder to do the real work */
3781 stat = codec->decode_func(stream, &codec->info.Clsid, 0, image);
3783 /* take note of the original data format */
3784 if (stat == Ok)
3786 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
3787 return Ok;
3790 return stat;
3793 /* FIXME: no ICM */
3794 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
3796 TRACE("%p %p\n", stream, image);
3798 return GdipLoadImageFromStream(stream, image);
3801 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
3803 static int calls;
3805 TRACE("(%p,%u)\n", image, propId);
3807 if(!image)
3808 return InvalidParameter;
3810 if(!(calls++))
3811 FIXME("not implemented\n");
3813 return NotImplemented;
3816 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
3818 static int calls;
3820 if (!image || !item) return InvalidParameter;
3822 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image, item, item->id, item->type, item->length, item->value);
3824 if(!(calls++))
3825 FIXME("not implemented\n");
3827 return Ok;
3830 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
3831 GDIPCONST CLSID *clsidEncoder,
3832 GDIPCONST EncoderParameters *encoderParams)
3834 GpStatus stat;
3835 IStream *stream;
3837 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
3839 if (!image || !filename|| !clsidEncoder)
3840 return InvalidParameter;
3842 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
3843 if (stat != Ok)
3844 return GenericError;
3846 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
3848 IStream_Release(stream);
3849 return stat;
3852 /*************************************************************************
3853 * Encoding functions -
3854 * These functions encode an image in different image file formats.
3856 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
3857 #define BITMAP_FORMAT_JPEG 0xd8ff
3858 #define BITMAP_FORMAT_GIF 0x4947
3859 #define BITMAP_FORMAT_PNG 0x5089
3860 #define BITMAP_FORMAT_APM 0xcdd7
3862 static GpStatus encode_image_WIC(GpImage *image, IStream* stream,
3863 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3865 GpStatus stat;
3866 GpBitmap *bitmap;
3867 IWICBitmapEncoder *encoder;
3868 IWICBitmapFrameEncode *frameencode;
3869 IPropertyBag2 *encoderoptions;
3870 HRESULT hr;
3871 UINT width, height;
3872 PixelFormat gdipformat=0;
3873 const WICPixelFormatGUID *desired_wicformat;
3874 WICPixelFormatGUID wicformat;
3875 GpRect rc;
3876 BitmapData lockeddata;
3877 HRESULT initresult;
3878 UINT i;
3880 if (image->type != ImageTypeBitmap)
3881 return GenericError;
3883 bitmap = (GpBitmap*)image;
3885 GdipGetImageWidth(image, &width);
3886 GdipGetImageHeight(image, &height);
3888 rc.X = 0;
3889 rc.Y = 0;
3890 rc.Width = width;
3891 rc.Height = height;
3893 initresult = CoInitialize(NULL);
3895 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
3896 &IID_IWICBitmapEncoder, (void**)&encoder);
3897 if (FAILED(hr))
3899 if (SUCCEEDED(initresult)) CoUninitialize();
3900 return hresult_to_status(hr);
3903 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
3905 if (SUCCEEDED(hr))
3907 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
3910 if (SUCCEEDED(hr)) /* created frame */
3912 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
3914 if (SUCCEEDED(hr))
3915 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
3917 if (SUCCEEDED(hr))
3918 hr = IWICBitmapFrameEncode_SetResolution(frameencode, image->xres, image->yres);
3920 if (SUCCEEDED(hr))
3922 for (i=0; pixel_formats[i].wic_format; i++)
3924 if (pixel_formats[i].gdip_format == bitmap->format)
3926 desired_wicformat = pixel_formats[i].wic_format;
3927 gdipformat = bitmap->format;
3928 break;
3931 if (!gdipformat)
3933 desired_wicformat = &GUID_WICPixelFormat32bppBGRA;
3934 gdipformat = PixelFormat32bppARGB;
3937 memcpy(&wicformat, desired_wicformat, sizeof(GUID));
3938 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
3941 if (SUCCEEDED(hr) && !IsEqualGUID(desired_wicformat, &wicformat))
3943 /* Encoder doesn't support this bitmap's format. */
3944 gdipformat = 0;
3945 for (i=0; pixel_formats[i].wic_format; i++)
3947 if (IsEqualGUID(&wicformat, pixel_formats[i].wic_format))
3949 gdipformat = pixel_formats[i].gdip_format;
3950 break;
3953 if (!gdipformat)
3955 ERR("Cannot support encoder format %s\n", debugstr_guid(&wicformat));
3956 hr = E_FAIL;
3960 if (SUCCEEDED(hr))
3962 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
3963 &lockeddata);
3965 if (stat == Ok)
3967 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
3968 BYTE *row;
3970 /* write one row at a time in case stride is negative */
3971 row = lockeddata.Scan0;
3972 for (i=0; i<lockeddata.Height; i++)
3974 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
3975 if (FAILED(hr)) break;
3976 row += lockeddata.Stride;
3979 GdipBitmapUnlockBits(bitmap, &lockeddata);
3981 else
3982 hr = E_FAIL;
3985 if (SUCCEEDED(hr))
3986 hr = IWICBitmapFrameEncode_Commit(frameencode);
3988 IWICBitmapFrameEncode_Release(frameencode);
3989 IPropertyBag2_Release(encoderoptions);
3992 if (SUCCEEDED(hr))
3993 hr = IWICBitmapEncoder_Commit(encoder);
3995 IWICBitmapEncoder_Release(encoder);
3997 if (SUCCEEDED(initresult)) CoUninitialize();
3999 return hresult_to_status(hr);
4002 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
4003 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4005 return encode_image_WIC(image, stream, &CLSID_WICBmpEncoder, params);
4008 static GpStatus encode_image_tiff(GpImage *image, IStream* stream,
4009 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4011 return encode_image_WIC(image, stream, &CLSID_WICTiffEncoder, params);
4014 static GpStatus encode_image_png(GpImage *image, IStream* stream,
4015 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4017 return encode_image_WIC(image, stream, &CLSID_WICPngEncoder, params);
4020 static GpStatus encode_image_jpeg(GpImage *image, IStream* stream,
4021 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4023 return encode_image_WIC(image, stream, &CLSID_WICJpegEncoder, params);
4026 /*****************************************************************************
4027 * GdipSaveImageToStream [GDIPLUS.@]
4029 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
4030 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4032 GpStatus stat;
4033 encode_image_func encode_image;
4034 int i;
4036 TRACE("%p %p %p %p\n", image, stream, clsid, params);
4038 if(!image || !stream)
4039 return InvalidParameter;
4041 /* select correct encoder */
4042 encode_image = NULL;
4043 for (i = 0; i < NUM_CODECS; i++) {
4044 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
4045 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
4046 encode_image = codecs[i].encode_func;
4048 if (encode_image == NULL)
4049 return UnknownImageFormat;
4051 stat = encode_image(image, stream, clsid, params);
4053 return stat;
4056 /*****************************************************************************
4057 * GdipSaveAdd [GDIPLUS.@]
4059 GpStatus WINGDIPAPI GdipSaveAdd(GpImage *image, GDIPCONST EncoderParameters *params)
4061 FIXME("(%p,%p): stub\n", image, params);
4062 return Ok;
4065 /*****************************************************************************
4066 * GdipGetImagePalette [GDIPLUS.@]
4068 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
4070 INT count;
4072 TRACE("(%p,%p,%i)\n", image, palette, size);
4074 if (!image || !palette)
4075 return InvalidParameter;
4077 count = image->palette ? image->palette->Count : 0;
4079 if (size < (sizeof(UINT)*2+sizeof(ARGB)*count))
4081 TRACE("<-- InsufficientBuffer\n");
4082 return InsufficientBuffer;
4085 if (image->palette)
4087 palette->Flags = image->palette->Flags;
4088 palette->Count = image->palette->Count;
4089 memcpy(palette->Entries, image->palette->Entries, sizeof(ARGB)*image->palette->Count);
4091 else
4093 palette->Flags = 0;
4094 palette->Count = 0;
4096 return Ok;
4099 /*****************************************************************************
4100 * GdipSetImagePalette [GDIPLUS.@]
4102 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
4103 GDIPCONST ColorPalette *palette)
4105 ColorPalette *new_palette;
4107 TRACE("(%p,%p)\n", image, palette);
4109 if(!image || !palette || palette->Count > 256)
4110 return InvalidParameter;
4112 new_palette = GdipAlloc(2 * sizeof(UINT) + palette->Count * sizeof(ARGB));
4113 if (!new_palette) return OutOfMemory;
4115 GdipFree(image->palette);
4116 image->palette = new_palette;
4117 image->palette->Flags = palette->Flags;
4118 image->palette->Count = palette->Count;
4119 memcpy(image->palette->Entries, palette->Entries, sizeof(ARGB)*palette->Count);
4121 return Ok;
4124 /*************************************************************************
4125 * Encoders -
4126 * Structures that represent which formats we support for encoding.
4129 /* ImageCodecInfo creation routines taken from libgdiplus */
4130 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
4131 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
4132 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
4133 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
4134 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
4135 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
4137 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
4138 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
4139 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
4140 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
4141 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
4142 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
4144 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
4145 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
4146 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
4147 static const WCHAR gif_format[] = {'G','I','F',0};
4148 static const BYTE gif_sig_pattern[12] = "GIF87aGIF89a";
4149 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4151 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
4152 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
4153 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
4154 static const WCHAR tiff_format[] = {'T','I','F','F',0};
4155 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
4156 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4158 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
4159 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
4160 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
4161 static const WCHAR emf_format[] = {'E','M','F',0};
4162 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
4163 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4165 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
4166 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
4167 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
4168 static const WCHAR wmf_format[] = {'W','M','F',0};
4169 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
4170 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
4172 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
4173 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
4174 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
4175 static const WCHAR png_format[] = {'P','N','G',0};
4176 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
4177 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4179 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
4180 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
4181 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
4182 static const WCHAR ico_format[] = {'I','C','O',0};
4183 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
4184 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4186 static const struct image_codec codecs[NUM_CODECS] = {
4188 { /* BMP */
4189 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4190 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4191 /* CodecName */ bmp_codecname,
4192 /* DllName */ NULL,
4193 /* FormatDescription */ bmp_format,
4194 /* FilenameExtension */ bmp_extension,
4195 /* MimeType */ bmp_mimetype,
4196 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4197 /* Version */ 1,
4198 /* SigCount */ 1,
4199 /* SigSize */ 2,
4200 /* SigPattern */ bmp_sig_pattern,
4201 /* SigMask */ bmp_sig_mask,
4203 encode_image_BMP,
4204 decode_image_bmp
4207 { /* JPEG */
4208 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4209 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4210 /* CodecName */ jpeg_codecname,
4211 /* DllName */ NULL,
4212 /* FormatDescription */ jpeg_format,
4213 /* FilenameExtension */ jpeg_extension,
4214 /* MimeType */ jpeg_mimetype,
4215 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4216 /* Version */ 1,
4217 /* SigCount */ 1,
4218 /* SigSize */ 2,
4219 /* SigPattern */ jpeg_sig_pattern,
4220 /* SigMask */ jpeg_sig_mask,
4222 encode_image_jpeg,
4223 decode_image_jpeg
4226 { /* GIF */
4227 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4228 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4229 /* CodecName */ gif_codecname,
4230 /* DllName */ NULL,
4231 /* FormatDescription */ gif_format,
4232 /* FilenameExtension */ gif_extension,
4233 /* MimeType */ gif_mimetype,
4234 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4235 /* Version */ 1,
4236 /* SigCount */ 2,
4237 /* SigSize */ 6,
4238 /* SigPattern */ gif_sig_pattern,
4239 /* SigMask */ gif_sig_mask,
4241 NULL,
4242 decode_image_gif
4245 { /* TIFF */
4246 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4247 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4248 /* CodecName */ tiff_codecname,
4249 /* DllName */ NULL,
4250 /* FormatDescription */ tiff_format,
4251 /* FilenameExtension */ tiff_extension,
4252 /* MimeType */ tiff_mimetype,
4253 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4254 /* Version */ 1,
4255 /* SigCount */ 2,
4256 /* SigSize */ 4,
4257 /* SigPattern */ tiff_sig_pattern,
4258 /* SigMask */ tiff_sig_mask,
4260 encode_image_tiff,
4261 decode_image_tiff
4264 { /* EMF */
4265 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4266 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4267 /* CodecName */ emf_codecname,
4268 /* DllName */ NULL,
4269 /* FormatDescription */ emf_format,
4270 /* FilenameExtension */ emf_extension,
4271 /* MimeType */ emf_mimetype,
4272 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4273 /* Version */ 1,
4274 /* SigCount */ 1,
4275 /* SigSize */ 4,
4276 /* SigPattern */ emf_sig_pattern,
4277 /* SigMask */ emf_sig_mask,
4279 NULL,
4280 decode_image_olepicture_metafile
4283 { /* WMF */
4284 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4285 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4286 /* CodecName */ wmf_codecname,
4287 /* DllName */ NULL,
4288 /* FormatDescription */ wmf_format,
4289 /* FilenameExtension */ wmf_extension,
4290 /* MimeType */ wmf_mimetype,
4291 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4292 /* Version */ 1,
4293 /* SigCount */ 1,
4294 /* SigSize */ 2,
4295 /* SigPattern */ wmf_sig_pattern,
4296 /* SigMask */ wmf_sig_mask,
4298 NULL,
4299 decode_image_olepicture_metafile
4302 { /* PNG */
4303 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4304 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4305 /* CodecName */ png_codecname,
4306 /* DllName */ NULL,
4307 /* FormatDescription */ png_format,
4308 /* FilenameExtension */ png_extension,
4309 /* MimeType */ png_mimetype,
4310 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4311 /* Version */ 1,
4312 /* SigCount */ 1,
4313 /* SigSize */ 8,
4314 /* SigPattern */ png_sig_pattern,
4315 /* SigMask */ png_sig_mask,
4317 encode_image_png,
4318 decode_image_png
4321 { /* ICO */
4322 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4323 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4324 /* CodecName */ ico_codecname,
4325 /* DllName */ NULL,
4326 /* FormatDescription */ ico_format,
4327 /* FilenameExtension */ ico_extension,
4328 /* MimeType */ ico_mimetype,
4329 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4330 /* Version */ 1,
4331 /* SigCount */ 1,
4332 /* SigSize */ 4,
4333 /* SigPattern */ ico_sig_pattern,
4334 /* SigMask */ ico_sig_mask,
4336 NULL,
4337 decode_image_icon
4341 /*****************************************************************************
4342 * GdipGetImageDecodersSize [GDIPLUS.@]
4344 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
4346 int decoder_count=0;
4347 int i;
4348 TRACE("%p %p\n", numDecoders, size);
4350 if (!numDecoders || !size)
4351 return InvalidParameter;
4353 for (i=0; i<NUM_CODECS; i++)
4355 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4356 decoder_count++;
4359 *numDecoders = decoder_count;
4360 *size = decoder_count * sizeof(ImageCodecInfo);
4362 return Ok;
4365 /*****************************************************************************
4366 * GdipGetImageDecoders [GDIPLUS.@]
4368 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
4370 int i, decoder_count=0;
4371 TRACE("%u %u %p\n", numDecoders, size, decoders);
4373 if (!decoders ||
4374 size != numDecoders * sizeof(ImageCodecInfo))
4375 return GenericError;
4377 for (i=0; i<NUM_CODECS; i++)
4379 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4381 if (decoder_count == numDecoders) return GenericError;
4382 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4383 decoder_count++;
4387 if (decoder_count < numDecoders) return GenericError;
4389 return Ok;
4392 /*****************************************************************************
4393 * GdipGetImageEncodersSize [GDIPLUS.@]
4395 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
4397 int encoder_count=0;
4398 int i;
4399 TRACE("%p %p\n", numEncoders, size);
4401 if (!numEncoders || !size)
4402 return InvalidParameter;
4404 for (i=0; i<NUM_CODECS; i++)
4406 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4407 encoder_count++;
4410 *numEncoders = encoder_count;
4411 *size = encoder_count * sizeof(ImageCodecInfo);
4413 return Ok;
4416 /*****************************************************************************
4417 * GdipGetImageEncoders [GDIPLUS.@]
4419 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
4421 int i, encoder_count=0;
4422 TRACE("%u %u %p\n", numEncoders, size, encoders);
4424 if (!encoders ||
4425 size != numEncoders * sizeof(ImageCodecInfo))
4426 return GenericError;
4428 for (i=0; i<NUM_CODECS; i++)
4430 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4432 if (encoder_count == numEncoders) return GenericError;
4433 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4434 encoder_count++;
4438 if (encoder_count < numEncoders) return GenericError;
4440 return Ok;
4443 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
4444 GDIPCONST CLSID* clsidEncoder, UINT *size)
4446 static int calls;
4448 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
4450 if(!(calls++))
4451 FIXME("not implemented\n");
4453 *size = 0;
4455 return NotImplemented;
4458 static PixelFormat get_16bpp_format(HBITMAP hbm)
4460 BITMAPV4HEADER bmh;
4461 HDC hdc;
4462 PixelFormat result;
4464 hdc = CreateCompatibleDC(NULL);
4466 memset(&bmh, 0, sizeof(bmh));
4467 bmh.bV4Size = sizeof(bmh);
4468 bmh.bV4Width = 1;
4469 bmh.bV4Height = 1;
4470 bmh.bV4V4Compression = BI_BITFIELDS;
4471 bmh.bV4BitCount = 16;
4473 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO*)&bmh, DIB_RGB_COLORS);
4475 if (bmh.bV4RedMask == 0x7c00 &&
4476 bmh.bV4GreenMask == 0x3e0 &&
4477 bmh.bV4BlueMask == 0x1f)
4479 result = PixelFormat16bppRGB555;
4481 else if (bmh.bV4RedMask == 0xf800 &&
4482 bmh.bV4GreenMask == 0x7e0 &&
4483 bmh.bV4BlueMask == 0x1f)
4485 result = PixelFormat16bppRGB565;
4487 else
4489 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
4490 bmh.bV4GreenMask, bmh.bV4BlueMask);
4491 result = PixelFormatUndefined;
4494 DeleteDC(hdc);
4496 return result;
4499 /*****************************************************************************
4500 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
4502 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
4504 BITMAP bm;
4505 GpStatus retval;
4506 PixelFormat format;
4507 BitmapData lockeddata;
4509 TRACE("%p %p %p\n", hbm, hpal, bitmap);
4511 if(!hbm || !bitmap)
4512 return InvalidParameter;
4514 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
4515 return InvalidParameter;
4517 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
4518 switch(bm.bmBitsPixel) {
4519 case 1:
4520 format = PixelFormat1bppIndexed;
4521 break;
4522 case 4:
4523 format = PixelFormat4bppIndexed;
4524 break;
4525 case 8:
4526 format = PixelFormat8bppIndexed;
4527 break;
4528 case 16:
4529 format = get_16bpp_format(hbm);
4530 if (format == PixelFormatUndefined)
4531 return InvalidParameter;
4532 break;
4533 case 24:
4534 format = PixelFormat24bppRGB;
4535 break;
4536 case 32:
4537 format = PixelFormat32bppRGB;
4538 break;
4539 case 48:
4540 format = PixelFormat48bppRGB;
4541 break;
4542 default:
4543 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
4544 return InvalidParameter;
4547 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
4548 format, NULL, bitmap);
4550 if (retval == Ok)
4552 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
4553 format, &lockeddata);
4554 if (retval == Ok)
4556 HDC hdc;
4557 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
4558 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
4559 INT src_height;
4561 hdc = CreateCompatibleDC(NULL);
4563 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
4564 pbmi->bmiHeader.biBitCount = 0;
4566 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
4568 src_height = abs(pbmi->bmiHeader.biHeight);
4569 pbmi->bmiHeader.biHeight = -src_height;
4571 GetDIBits(hdc, hbm, 0, src_height, lockeddata.Scan0, pbmi, DIB_RGB_COLORS);
4573 DeleteDC(hdc);
4575 GdipBitmapUnlockBits(*bitmap, &lockeddata);
4578 if (retval == Ok && hpal)
4580 PALETTEENTRY entry[256];
4581 ColorPalette *palette=NULL;
4582 int i, num_palette_entries;
4584 num_palette_entries = GetPaletteEntries(hpal, 0, 256, entry);
4585 if (!num_palette_entries)
4586 retval = GenericError;
4588 palette = GdipAlloc(sizeof(ColorPalette) + sizeof(ARGB) * (num_palette_entries-1));
4589 if (!palette)
4590 retval = OutOfMemory;
4592 if (retval == Ok)
4594 palette->Flags = 0;
4595 palette->Count = num_palette_entries;
4597 for (i=0; i<num_palette_entries; i++)
4599 palette->Entries[i] = 0xff000000 | entry[i].peRed << 16 |
4600 entry[i].peGreen << 8 | entry[i].peBlue;
4603 retval = GdipSetImagePalette((GpImage*)*bitmap, palette);
4606 GdipFree(palette);
4609 if (retval != Ok)
4611 GdipDisposeImage((GpImage*)*bitmap);
4612 *bitmap = NULL;
4616 return retval;
4619 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
4621 FIXME("(%p): stub\n", effect);
4622 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
4623 * in Windows's gdiplus */
4624 return NotImplemented;
4627 /*****************************************************************************
4628 * GdipSetEffectParameters [GDIPLUS.@]
4630 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
4631 const VOID *params, const UINT size)
4633 static int calls;
4635 TRACE("(%p,%p,%u)\n", effect, params, size);
4637 if(!(calls++))
4638 FIXME("not implemented\n");
4640 return NotImplemented;
4643 /*****************************************************************************
4644 * GdipGetImageFlags [GDIPLUS.@]
4646 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
4648 TRACE("%p %p\n", image, flags);
4650 if(!image || !flags)
4651 return InvalidParameter;
4653 *flags = image->flags;
4655 return Ok;
4658 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
4660 TRACE("(%d, %p)\n", control, param);
4662 switch(control){
4663 case TestControlForceBilinear:
4664 if(param)
4665 FIXME("TestControlForceBilinear not handled\n");
4666 break;
4667 case TestControlNoICM:
4668 if(param)
4669 FIXME("TestControlNoICM not handled\n");
4670 break;
4671 case TestControlGetBuildNumber:
4672 *((DWORD*)param) = 3102;
4673 break;
4676 return Ok;
4679 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
4681 TRACE("%p\n", image);
4683 return Ok;
4686 /*****************************************************************************
4687 * GdipGetImageThumbnail [GDIPLUS.@]
4689 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
4690 GpImage **ret_image, GetThumbnailImageAbort cb,
4691 VOID * cb_data)
4693 GpStatus stat;
4694 GpGraphics *graphics;
4695 UINT srcwidth, srcheight;
4697 TRACE("(%p %u %u %p %p %p)\n",
4698 image, width, height, ret_image, cb, cb_data);
4700 if (!image || !ret_image)
4701 return InvalidParameter;
4703 if (!width) width = 120;
4704 if (!height) height = 120;
4706 GdipGetImageWidth(image, &srcwidth);
4707 GdipGetImageHeight(image, &srcheight);
4709 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
4710 NULL, (GpBitmap**)ret_image);
4712 if (stat == Ok)
4714 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
4716 if (stat == Ok)
4718 stat = GdipDrawImageRectRectI(graphics, image,
4719 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
4720 NULL, NULL, NULL);
4722 GdipDeleteGraphics(graphics);
4725 if (stat != Ok)
4727 GdipDisposeImage(*ret_image);
4728 *ret_image = NULL;
4732 return stat;
4735 /*****************************************************************************
4736 * GdipImageRotateFlip [GDIPLUS.@]
4738 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
4740 GpBitmap *new_bitmap;
4741 GpBitmap *bitmap;
4742 int bpp, bytesperpixel;
4743 BOOL rotate_90, flip_x, flip_y;
4744 int src_x_offset, src_y_offset;
4745 LPBYTE src_origin;
4746 UINT x, y, width, height;
4747 BitmapData src_lock, dst_lock;
4748 GpStatus stat;
4750 TRACE("(%p, %u)\n", image, type);
4752 if (!image)
4753 return InvalidParameter;
4755 rotate_90 = type&1;
4756 flip_x = (type&6) == 2 || (type&6) == 4;
4757 flip_y = (type&3) == 1 || (type&3) == 2;
4759 if (image->type != ImageTypeBitmap)
4761 FIXME("Not implemented for type %i\n", image->type);
4762 return NotImplemented;
4765 bitmap = (GpBitmap*)image;
4766 bpp = PIXELFORMATBPP(bitmap->format);
4768 if (bpp < 8)
4770 FIXME("Not implemented for %i bit images\n", bpp);
4771 return NotImplemented;
4774 if (rotate_90)
4776 width = bitmap->height;
4777 height = bitmap->width;
4779 else
4781 width = bitmap->width;
4782 height = bitmap->height;
4785 bytesperpixel = bpp/8;
4787 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
4789 if (stat != Ok)
4790 return stat;
4792 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
4794 if (stat == Ok)
4796 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
4798 if (stat == Ok)
4800 LPBYTE src_row, src_pixel;
4801 LPBYTE dst_row, dst_pixel;
4803 src_origin = src_lock.Scan0;
4804 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
4805 if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
4807 if (rotate_90)
4809 if (flip_y) src_x_offset = -src_lock.Stride;
4810 else src_x_offset = src_lock.Stride;
4811 if (flip_x) src_y_offset = -bytesperpixel;
4812 else src_y_offset = bytesperpixel;
4814 else
4816 if (flip_x) src_x_offset = -bytesperpixel;
4817 else src_x_offset = bytesperpixel;
4818 if (flip_y) src_y_offset = -src_lock.Stride;
4819 else src_y_offset = src_lock.Stride;
4822 src_row = src_origin;
4823 dst_row = dst_lock.Scan0;
4824 for (y=0; y<height; y++)
4826 src_pixel = src_row;
4827 dst_pixel = dst_row;
4828 for (x=0; x<width; x++)
4830 /* FIXME: This could probably be faster without memcpy. */
4831 memcpy(dst_pixel, src_pixel, bytesperpixel);
4832 dst_pixel += bytesperpixel;
4833 src_pixel += src_x_offset;
4835 src_row += src_y_offset;
4836 dst_row += dst_lock.Stride;
4839 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
4842 GdipBitmapUnlockBits(bitmap, &src_lock);
4845 if (stat == Ok)
4846 move_bitmap(bitmap, new_bitmap, FALSE);
4847 else
4848 GdipDisposeImage((GpImage*)new_bitmap);
4850 return stat;