d3dcompiler: Parse "return" statement.
[wine/multimedia.git] / dlls / gdiplus / image.c
blobb657969ce9ea3bd8abda99bebb44399aef9c9880
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
3 * Copyright (C) 2012 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdarg.h>
21 #include <assert.h>
23 #define NONAMELESSUNION
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "wingdi.h"
30 #define COBJMACROS
31 #include "objbase.h"
32 #include "olectl.h"
33 #include "ole2.h"
35 #include "initguid.h"
36 #include "wincodec.h"
37 #include "gdiplus.h"
38 #include "gdiplus_private.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
43 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
45 static const struct
47 const WICPixelFormatGUID *wic_format;
48 PixelFormat gdip_format;
49 /* predefined palette type to use for pixel format conversions */
50 WICBitmapPaletteType palette_type;
51 } pixel_formats[] =
53 { &GUID_WICPixelFormatBlackWhite, PixelFormat1bppIndexed, WICBitmapPaletteTypeFixedBW },
54 { &GUID_WICPixelFormat1bppIndexed, PixelFormat1bppIndexed, WICBitmapPaletteTypeFixedBW },
55 { &GUID_WICPixelFormat8bppGray, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedGray256 },
56 { &GUID_WICPixelFormat8bppIndexed, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedHalftone256 },
57 { &GUID_WICPixelFormat16bppBGR555, PixelFormat16bppRGB555, WICBitmapPaletteTypeFixedHalftone256 },
58 { &GUID_WICPixelFormat24bppBGR, PixelFormat24bppRGB, WICBitmapPaletteTypeFixedHalftone256 },
59 { &GUID_WICPixelFormat32bppBGR, PixelFormat32bppRGB, WICBitmapPaletteTypeFixedHalftone256 },
60 { &GUID_WICPixelFormat32bppBGRA, PixelFormat32bppARGB, WICBitmapPaletteTypeFixedHalftone256 },
61 { &GUID_WICPixelFormat32bppPBGRA, PixelFormat32bppPARGB, WICBitmapPaletteTypeFixedHalftone256 },
62 { NULL }
65 static ColorPalette *get_palette(IWICBitmapFrameDecode *frame, WICBitmapPaletteType palette_type)
67 HRESULT hr;
68 IWICImagingFactory *factory;
69 IWICPalette *wic_palette;
70 ColorPalette *palette = NULL;
72 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
73 &IID_IWICImagingFactory, (void **)&factory);
74 if (hr != S_OK) return NULL;
76 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
77 if (hr == S_OK)
79 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
80 if (frame)
81 hr = IWICBitmapFrameDecode_CopyPalette(frame, wic_palette);
82 if (hr != S_OK)
84 TRACE("using predefined palette %#x\n", palette_type);
85 hr = IWICPalette_InitializePredefined(wic_palette, palette_type, FALSE);
87 if (hr == S_OK)
89 UINT count;
90 BOOL mono, gray;
92 IWICPalette_IsBlackWhite(wic_palette, &mono);
93 IWICPalette_IsGrayscale(wic_palette, &gray);
95 IWICPalette_GetColorCount(wic_palette, &count);
96 palette = HeapAlloc(GetProcessHeap(), 0, 2 * sizeof(UINT) + count * sizeof(ARGB));
97 IWICPalette_GetColors(wic_palette, count, palette->Entries, &palette->Count);
99 if (mono)
100 palette->Flags = 0;
101 else if (gray)
102 palette->Flags = PaletteFlagsGrayScale;
103 else
104 palette->Flags = PaletteFlagsHalftone;
106 IWICPalette_Release(wic_palette);
108 IWICImagingFactory_Release(factory);
109 return palette;
112 static INT ipicture_pixel_height(IPicture *pic)
114 HDC hdcref;
115 OLE_YSIZE_HIMETRIC y;
117 IPicture_get_Height(pic, &y);
119 hdcref = GetDC(0);
121 y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
122 ReleaseDC(0, hdcref);
124 return y;
127 static INT ipicture_pixel_width(IPicture *pic)
129 HDC hdcref;
130 OLE_XSIZE_HIMETRIC x;
132 IPicture_get_Width(pic, &x);
134 hdcref = GetDC(0);
136 x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
138 ReleaseDC(0, hdcref);
140 return x;
143 GpStatus WINGDIPAPI GdipBitmapApplyEffect(GpBitmap* bitmap, CGpEffect* effect,
144 RECT* roi, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
146 FIXME("(%p %p %p %d %p %p): stub\n", bitmap, effect, roi, useAuxData, auxData, auxDataSize);
148 * Note: According to Jose Roca's GDI+ docs, this function is not
149 * implemented in Windows's GDI+.
151 return NotImplemented;
154 GpStatus WINGDIPAPI GdipBitmapCreateApplyEffect(GpBitmap** inputBitmaps,
155 INT numInputs, CGpEffect* effect, RECT* roi, RECT* outputRect,
156 GpBitmap** outputBitmap, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
158 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps, numInputs, effect, roi, outputRect, outputBitmap, useAuxData, auxData, auxDataSize);
160 * Note: According to Jose Roca's GDI+ docs, this function is not
161 * implemented in Windows's GDI+.
163 return NotImplemented;
166 static inline void getpixel_1bppIndexed(BYTE *index, const BYTE *row, UINT x)
168 *index = (row[x/8]>>(7-x%8)) & 1;
171 static inline void getpixel_4bppIndexed(BYTE *index, const BYTE *row, UINT x)
173 if (x & 1)
174 *index = row[x/2]&0xf;
175 else
176 *index = row[x/2]>>4;
179 static inline void getpixel_8bppIndexed(BYTE *index, const BYTE *row, UINT x)
181 *index = row[x];
184 static inline void getpixel_16bppGrayScale(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
185 const BYTE *row, UINT x)
187 *r = *g = *b = row[x*2+1];
188 *a = 255;
191 static inline void getpixel_16bppRGB555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
192 const BYTE *row, UINT x)
194 WORD pixel = *((const WORD*)(row)+x);
195 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
196 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
197 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
198 *a = 255;
201 static inline void getpixel_16bppRGB565(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
202 const BYTE *row, UINT x)
204 WORD pixel = *((const WORD*)(row)+x);
205 *r = (pixel>>8&0xf8)|(pixel>>13&0x7);
206 *g = (pixel>>3&0xfc)|(pixel>>9&0x3);
207 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
208 *a = 255;
211 static inline void getpixel_16bppARGB1555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
212 const BYTE *row, UINT x)
214 WORD pixel = *((const WORD*)(row)+x);
215 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
216 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
217 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
218 if ((pixel&0x8000) == 0x8000)
219 *a = 255;
220 else
221 *a = 0;
224 static inline void getpixel_24bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
225 const BYTE *row, UINT x)
227 *r = row[x*3+2];
228 *g = row[x*3+1];
229 *b = row[x*3];
230 *a = 255;
233 static inline void getpixel_32bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
234 const BYTE *row, UINT x)
236 *r = row[x*4+2];
237 *g = row[x*4+1];
238 *b = row[x*4];
239 *a = 255;
242 static inline void getpixel_32bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
243 const BYTE *row, UINT x)
245 *r = row[x*4+2];
246 *g = row[x*4+1];
247 *b = row[x*4];
248 *a = row[x*4+3];
251 static inline void getpixel_32bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
252 const BYTE *row, UINT x)
254 *a = row[x*4+3];
255 if (*a == 0)
256 *r = *g = *b = 0;
257 else
259 *r = row[x*4+2] * 255 / *a;
260 *g = row[x*4+1] * 255 / *a;
261 *b = row[x*4] * 255 / *a;
265 static inline void getpixel_48bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
266 const BYTE *row, UINT x)
268 *r = row[x*6+5];
269 *g = row[x*6+3];
270 *b = row[x*6+1];
271 *a = 255;
274 static inline void getpixel_64bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
275 const BYTE *row, UINT x)
277 *r = row[x*8+5];
278 *g = row[x*8+3];
279 *b = row[x*8+1];
280 *a = row[x*8+7];
283 static inline void getpixel_64bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
284 const BYTE *row, UINT x)
286 *a = row[x*8+7];
287 if (*a == 0)
288 *r = *g = *b = 0;
289 else
291 *r = row[x*8+5] * 255 / *a;
292 *g = row[x*8+3] * 255 / *a;
293 *b = row[x*8+1] * 255 / *a;
297 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
298 ARGB *color)
300 BYTE r, g, b, a;
301 BYTE index;
302 BYTE *row;
303 TRACE("%p %d %d %p\n", bitmap, x, y, color);
305 if(!bitmap || !color ||
306 x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
307 return InvalidParameter;
309 row = bitmap->bits+bitmap->stride*y;
311 switch (bitmap->format)
313 case PixelFormat1bppIndexed:
314 getpixel_1bppIndexed(&index,row,x);
315 break;
316 case PixelFormat4bppIndexed:
317 getpixel_4bppIndexed(&index,row,x);
318 break;
319 case PixelFormat8bppIndexed:
320 getpixel_8bppIndexed(&index,row,x);
321 break;
322 case PixelFormat16bppGrayScale:
323 getpixel_16bppGrayScale(&r,&g,&b,&a,row,x);
324 break;
325 case PixelFormat16bppRGB555:
326 getpixel_16bppRGB555(&r,&g,&b,&a,row,x);
327 break;
328 case PixelFormat16bppRGB565:
329 getpixel_16bppRGB565(&r,&g,&b,&a,row,x);
330 break;
331 case PixelFormat16bppARGB1555:
332 getpixel_16bppARGB1555(&r,&g,&b,&a,row,x);
333 break;
334 case PixelFormat24bppRGB:
335 getpixel_24bppRGB(&r,&g,&b,&a,row,x);
336 break;
337 case PixelFormat32bppRGB:
338 getpixel_32bppRGB(&r,&g,&b,&a,row,x);
339 break;
340 case PixelFormat32bppARGB:
341 getpixel_32bppARGB(&r,&g,&b,&a,row,x);
342 break;
343 case PixelFormat32bppPARGB:
344 getpixel_32bppPARGB(&r,&g,&b,&a,row,x);
345 break;
346 case PixelFormat48bppRGB:
347 getpixel_48bppRGB(&r,&g,&b,&a,row,x);
348 break;
349 case PixelFormat64bppARGB:
350 getpixel_64bppARGB(&r,&g,&b,&a,row,x);
351 break;
352 case PixelFormat64bppPARGB:
353 getpixel_64bppPARGB(&r,&g,&b,&a,row,x);
354 break;
355 default:
356 FIXME("not implemented for format 0x%x\n", bitmap->format);
357 return NotImplemented;
360 if (bitmap->format & PixelFormatIndexed)
361 *color = bitmap->image.palette->Entries[index];
362 else
363 *color = a<<24|r<<16|g<<8|b;
365 return Ok;
368 static inline UINT get_palette_index(BYTE r, BYTE g, BYTE b, BYTE a, ColorPalette *palette)
370 BYTE index = 0;
371 int best_distance = 0x7fff;
372 int distance;
373 int i;
375 if (!palette) return 0;
376 /* This algorithm scans entire palette,
377 computes difference from desired color (all color components have equal weight)
378 and returns the index of color with least difference.
380 Note: Maybe it could be replaced with a better algorithm for better image quality
381 and performance, though better algorithm would probably need some pre-built lookup
382 tables and thus may actually be slower if this method is called only few times per
383 every image.
385 for(i=0;i<palette->Count;i++) {
386 ARGB color=palette->Entries[i];
387 distance=abs(b-(color & 0xff)) + abs(g-(color>>8 & 0xff)) + abs(r-(color>>16 & 0xff)) + abs(a-(color>>24 & 0xff));
388 if (distance<best_distance) {
389 best_distance=distance;
390 index=i;
393 return index;
396 static inline void setpixel_8bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
397 BYTE *row, UINT x, ColorPalette *palette)
399 BYTE index = get_palette_index(r,g,b,a,palette);
400 row[x]=index;
403 static inline void setpixel_1bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
404 BYTE *row, UINT x, ColorPalette *palette)
406 row[x/8] = (row[x/8] & ~(1<<(7-x%8))) | (get_palette_index(r,g,b,a,palette)<<(7-x%8));
409 static inline void setpixel_4bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
410 BYTE *row, UINT x, ColorPalette *palette)
412 if (x & 1)
413 row[x/2] = (row[x/2] & 0xf0) | get_palette_index(r,g,b,a,palette);
414 else
415 row[x/2] = (row[x/2] & 0x0f) | get_palette_index(r,g,b,a,palette)<<4;
418 static inline void setpixel_16bppGrayScale(BYTE r, BYTE g, BYTE b, BYTE a,
419 BYTE *row, UINT x)
421 *((WORD*)(row)+x) = (r+g+b)*85;
424 static inline void setpixel_16bppRGB555(BYTE r, BYTE g, BYTE b, BYTE a,
425 BYTE *row, UINT x)
427 *((WORD*)(row)+x) = (r<<7&0x7c00)|
428 (g<<2&0x03e0)|
429 (b>>3&0x001f);
432 static inline void setpixel_16bppRGB565(BYTE r, BYTE g, BYTE b, BYTE a,
433 BYTE *row, UINT x)
435 *((WORD*)(row)+x) = (r<<8&0xf800)|
436 (g<<3&0x07e0)|
437 (b>>3&0x001f);
440 static inline void setpixel_16bppARGB1555(BYTE r, BYTE g, BYTE b, BYTE a,
441 BYTE *row, UINT x)
443 *((WORD*)(row)+x) = (a<<8&0x8000)|
444 (r<<7&0x7c00)|
445 (g<<2&0x03e0)|
446 (b>>3&0x001f);
449 static inline void setpixel_24bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
450 BYTE *row, UINT x)
452 row[x*3+2] = r;
453 row[x*3+1] = g;
454 row[x*3] = b;
457 static inline void setpixel_32bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
458 BYTE *row, UINT x)
460 *((DWORD*)(row)+x) = (r<<16)|(g<<8)|b;
463 static inline void setpixel_32bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
464 BYTE *row, UINT x)
466 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
469 static inline void setpixel_32bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
470 BYTE *row, UINT x)
472 r = r * a / 255;
473 g = g * a / 255;
474 b = b * a / 255;
475 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
478 static inline void setpixel_48bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
479 BYTE *row, UINT x)
481 row[x*6+5] = row[x*6+4] = r;
482 row[x*6+3] = row[x*6+2] = g;
483 row[x*6+1] = row[x*6] = b;
486 static inline void setpixel_64bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
487 BYTE *row, UINT x)
489 UINT64 a64=a, r64=r, g64=g, b64=b;
490 *((UINT64*)(row)+x) = (a64<<56)|(a64<<48)|(r64<<40)|(r64<<32)|(g64<<24)|(g64<<16)|(b64<<8)|b64;
493 static inline void setpixel_64bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
494 BYTE *row, UINT x)
496 UINT64 a64, r64, g64, b64;
497 a64 = a * 257;
498 r64 = r * a / 255;
499 g64 = g * a / 255;
500 b64 = b * a / 255;
501 *((UINT64*)(row)+x) = (a64<<48)|(r64<<32)|(g64<<16)|b64;
504 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
505 ARGB color)
507 BYTE a, r, g, b;
508 BYTE *row;
509 TRACE("bitmap:%p, x:%d, y:%d, color:%08x\n", bitmap, x, y, color);
511 if(!bitmap || x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
512 return InvalidParameter;
514 a = color>>24;
515 r = color>>16;
516 g = color>>8;
517 b = color;
519 row = bitmap->bits + bitmap->stride * y;
521 switch (bitmap->format)
523 case PixelFormat16bppGrayScale:
524 setpixel_16bppGrayScale(r,g,b,a,row,x);
525 break;
526 case PixelFormat16bppRGB555:
527 setpixel_16bppRGB555(r,g,b,a,row,x);
528 break;
529 case PixelFormat16bppRGB565:
530 setpixel_16bppRGB565(r,g,b,a,row,x);
531 break;
532 case PixelFormat16bppARGB1555:
533 setpixel_16bppARGB1555(r,g,b,a,row,x);
534 break;
535 case PixelFormat24bppRGB:
536 setpixel_24bppRGB(r,g,b,a,row,x);
537 break;
538 case PixelFormat32bppRGB:
539 setpixel_32bppRGB(r,g,b,a,row,x);
540 break;
541 case PixelFormat32bppARGB:
542 setpixel_32bppARGB(r,g,b,a,row,x);
543 break;
544 case PixelFormat32bppPARGB:
545 setpixel_32bppPARGB(r,g,b,a,row,x);
546 break;
547 case PixelFormat48bppRGB:
548 setpixel_48bppRGB(r,g,b,a,row,x);
549 break;
550 case PixelFormat64bppARGB:
551 setpixel_64bppARGB(r,g,b,a,row,x);
552 break;
553 case PixelFormat64bppPARGB:
554 setpixel_64bppPARGB(r,g,b,a,row,x);
555 break;
556 case PixelFormat8bppIndexed:
557 setpixel_8bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
558 break;
559 case PixelFormat4bppIndexed:
560 setpixel_4bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
561 break;
562 case PixelFormat1bppIndexed:
563 setpixel_1bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
564 break;
565 default:
566 FIXME("not implemented for format 0x%x\n", bitmap->format);
567 return NotImplemented;
570 return Ok;
573 GpStatus convert_pixels(INT width, INT height,
574 INT dst_stride, BYTE *dst_bits, PixelFormat dst_format,
575 INT src_stride, const BYTE *src_bits, PixelFormat src_format,
576 ColorPalette *palette)
578 INT x, y;
580 if (src_format == dst_format ||
581 (dst_format == PixelFormat32bppRGB && PIXELFORMATBPP(src_format) == 32))
583 UINT widthbytes = PIXELFORMATBPP(src_format) * width / 8;
584 for (y=0; y<height; y++)
585 memcpy(dst_bits+dst_stride*y, src_bits+src_stride*y, widthbytes);
586 return Ok;
589 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
590 for (x=0; x<width; x++) \
591 for (y=0; y<height; y++) { \
592 BYTE index; \
593 ARGB argb; \
594 BYTE *color = (BYTE *)&argb; \
595 getpixel_function(&index, src_bits+src_stride*y, x); \
596 argb = (palette && index < palette->Count) ? palette->Entries[index] : 0; \
597 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
599 return Ok; \
600 } while (0);
602 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
603 for (x=0; x<width; x++) \
604 for (y=0; y<height; y++) { \
605 BYTE r, g, b, a; \
606 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
607 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
609 return Ok; \
610 } while (0);
612 #define convert_rgb_to_indexed(getpixel_function, setpixel_function) do { \
613 for (x=0; x<width; x++) \
614 for (y=0; y<height; y++) { \
615 BYTE r, g, b, a; \
616 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
617 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x, palette); \
619 return Ok; \
620 } while (0);
622 switch (src_format)
624 case PixelFormat1bppIndexed:
625 switch (dst_format)
627 case PixelFormat16bppGrayScale:
628 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppGrayScale);
629 case PixelFormat16bppRGB555:
630 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB555);
631 case PixelFormat16bppRGB565:
632 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB565);
633 case PixelFormat16bppARGB1555:
634 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppARGB1555);
635 case PixelFormat24bppRGB:
636 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_24bppRGB);
637 case PixelFormat32bppRGB:
638 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppRGB);
639 case PixelFormat32bppARGB:
640 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppARGB);
641 case PixelFormat32bppPARGB:
642 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppPARGB);
643 case PixelFormat48bppRGB:
644 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_48bppRGB);
645 case PixelFormat64bppARGB:
646 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_64bppARGB);
647 default:
648 break;
650 break;
651 case PixelFormat4bppIndexed:
652 switch (dst_format)
654 case PixelFormat16bppGrayScale:
655 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppGrayScale);
656 case PixelFormat16bppRGB555:
657 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB555);
658 case PixelFormat16bppRGB565:
659 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB565);
660 case PixelFormat16bppARGB1555:
661 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppARGB1555);
662 case PixelFormat24bppRGB:
663 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_24bppRGB);
664 case PixelFormat32bppRGB:
665 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppRGB);
666 case PixelFormat32bppARGB:
667 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppARGB);
668 case PixelFormat32bppPARGB:
669 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppPARGB);
670 case PixelFormat48bppRGB:
671 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_48bppRGB);
672 case PixelFormat64bppARGB:
673 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_64bppARGB);
674 default:
675 break;
677 break;
678 case PixelFormat8bppIndexed:
679 switch (dst_format)
681 case PixelFormat16bppGrayScale:
682 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppGrayScale);
683 case PixelFormat16bppRGB555:
684 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB555);
685 case PixelFormat16bppRGB565:
686 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB565);
687 case PixelFormat16bppARGB1555:
688 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppARGB1555);
689 case PixelFormat24bppRGB:
690 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_24bppRGB);
691 case PixelFormat32bppRGB:
692 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppRGB);
693 case PixelFormat32bppARGB:
694 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppARGB);
695 case PixelFormat32bppPARGB:
696 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppPARGB);
697 case PixelFormat48bppRGB:
698 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_48bppRGB);
699 case PixelFormat64bppARGB:
700 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_64bppARGB);
701 default:
702 break;
704 break;
705 case PixelFormat16bppGrayScale:
706 switch (dst_format)
708 case PixelFormat1bppIndexed:
709 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_1bppIndexed);
710 case PixelFormat8bppIndexed:
711 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_8bppIndexed);
712 case PixelFormat16bppRGB555:
713 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB555);
714 case PixelFormat16bppRGB565:
715 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB565);
716 case PixelFormat16bppARGB1555:
717 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppARGB1555);
718 case PixelFormat24bppRGB:
719 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_24bppRGB);
720 case PixelFormat32bppRGB:
721 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppRGB);
722 case PixelFormat32bppARGB:
723 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppARGB);
724 case PixelFormat32bppPARGB:
725 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppPARGB);
726 case PixelFormat48bppRGB:
727 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_48bppRGB);
728 case PixelFormat64bppARGB:
729 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_64bppARGB);
730 default:
731 break;
733 break;
734 case PixelFormat16bppRGB555:
735 switch (dst_format)
737 case PixelFormat1bppIndexed:
738 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_1bppIndexed);
739 case PixelFormat8bppIndexed:
740 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_8bppIndexed);
741 case PixelFormat16bppGrayScale:
742 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppGrayScale);
743 case PixelFormat16bppRGB565:
744 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppRGB565);
745 case PixelFormat16bppARGB1555:
746 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppARGB1555);
747 case PixelFormat24bppRGB:
748 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_24bppRGB);
749 case PixelFormat32bppRGB:
750 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppRGB);
751 case PixelFormat32bppARGB:
752 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppARGB);
753 case PixelFormat32bppPARGB:
754 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppPARGB);
755 case PixelFormat48bppRGB:
756 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_48bppRGB);
757 case PixelFormat64bppARGB:
758 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_64bppARGB);
759 default:
760 break;
762 break;
763 case PixelFormat16bppRGB565:
764 switch (dst_format)
766 case PixelFormat1bppIndexed:
767 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_1bppIndexed);
768 case PixelFormat8bppIndexed:
769 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_8bppIndexed);
770 case PixelFormat16bppGrayScale:
771 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppGrayScale);
772 case PixelFormat16bppRGB555:
773 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppRGB555);
774 case PixelFormat16bppARGB1555:
775 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppARGB1555);
776 case PixelFormat24bppRGB:
777 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_24bppRGB);
778 case PixelFormat32bppRGB:
779 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppRGB);
780 case PixelFormat32bppARGB:
781 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppARGB);
782 case PixelFormat32bppPARGB:
783 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppPARGB);
784 case PixelFormat48bppRGB:
785 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_48bppRGB);
786 case PixelFormat64bppARGB:
787 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_64bppARGB);
788 default:
789 break;
791 break;
792 case PixelFormat16bppARGB1555:
793 switch (dst_format)
795 case PixelFormat1bppIndexed:
796 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_1bppIndexed);
797 case PixelFormat8bppIndexed:
798 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_8bppIndexed);
799 case PixelFormat16bppGrayScale:
800 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppGrayScale);
801 case PixelFormat16bppRGB555:
802 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB555);
803 case PixelFormat16bppRGB565:
804 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB565);
805 case PixelFormat24bppRGB:
806 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_24bppRGB);
807 case PixelFormat32bppRGB:
808 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppRGB);
809 case PixelFormat32bppARGB:
810 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppARGB);
811 case PixelFormat32bppPARGB:
812 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppPARGB);
813 case PixelFormat48bppRGB:
814 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_48bppRGB);
815 case PixelFormat64bppARGB:
816 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_64bppARGB);
817 default:
818 break;
820 break;
821 case PixelFormat24bppRGB:
822 switch (dst_format)
824 case PixelFormat1bppIndexed:
825 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_1bppIndexed);
826 case PixelFormat8bppIndexed:
827 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_8bppIndexed);
828 case PixelFormat16bppGrayScale:
829 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppGrayScale);
830 case PixelFormat16bppRGB555:
831 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB555);
832 case PixelFormat16bppRGB565:
833 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB565);
834 case PixelFormat16bppARGB1555:
835 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppARGB1555);
836 case PixelFormat32bppRGB:
837 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppRGB);
838 case PixelFormat32bppARGB:
839 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppARGB);
840 case PixelFormat32bppPARGB:
841 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppPARGB);
842 case PixelFormat48bppRGB:
843 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_48bppRGB);
844 case PixelFormat64bppARGB:
845 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_64bppARGB);
846 default:
847 break;
849 break;
850 case PixelFormat32bppRGB:
851 switch (dst_format)
853 case PixelFormat1bppIndexed:
854 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_1bppIndexed);
855 case PixelFormat8bppIndexed:
856 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_8bppIndexed);
857 case PixelFormat16bppGrayScale:
858 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppGrayScale);
859 case PixelFormat16bppRGB555:
860 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB555);
861 case PixelFormat16bppRGB565:
862 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB565);
863 case PixelFormat16bppARGB1555:
864 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppARGB1555);
865 case PixelFormat24bppRGB:
866 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_24bppRGB);
867 case PixelFormat32bppARGB:
868 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppARGB);
869 case PixelFormat32bppPARGB:
870 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppPARGB);
871 case PixelFormat48bppRGB:
872 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_48bppRGB);
873 case PixelFormat64bppARGB:
874 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_64bppARGB);
875 default:
876 break;
878 break;
879 case PixelFormat32bppARGB:
880 switch (dst_format)
882 case PixelFormat1bppIndexed:
883 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_1bppIndexed);
884 case PixelFormat8bppIndexed:
885 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_8bppIndexed);
886 case PixelFormat16bppGrayScale:
887 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppGrayScale);
888 case PixelFormat16bppRGB555:
889 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB555);
890 case PixelFormat16bppRGB565:
891 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB565);
892 case PixelFormat16bppARGB1555:
893 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppARGB1555);
894 case PixelFormat24bppRGB:
895 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_24bppRGB);
896 case PixelFormat32bppPARGB:
897 convert_32bppARGB_to_32bppPARGB(width, height, dst_bits, dst_stride, src_bits, src_stride);
898 return Ok;
899 case PixelFormat48bppRGB:
900 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_48bppRGB);
901 case PixelFormat64bppARGB:
902 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_64bppARGB);
903 default:
904 break;
906 break;
907 case PixelFormat32bppPARGB:
908 switch (dst_format)
910 case PixelFormat1bppIndexed:
911 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_1bppIndexed);
912 case PixelFormat8bppIndexed:
913 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_8bppIndexed);
914 case PixelFormat16bppGrayScale:
915 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppGrayScale);
916 case PixelFormat16bppRGB555:
917 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB555);
918 case PixelFormat16bppRGB565:
919 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB565);
920 case PixelFormat16bppARGB1555:
921 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppARGB1555);
922 case PixelFormat24bppRGB:
923 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_24bppRGB);
924 case PixelFormat32bppRGB:
925 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppRGB);
926 case PixelFormat32bppARGB:
927 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppARGB);
928 case PixelFormat48bppRGB:
929 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_48bppRGB);
930 case PixelFormat64bppARGB:
931 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_64bppARGB);
932 default:
933 break;
935 break;
936 case PixelFormat48bppRGB:
937 switch (dst_format)
939 case PixelFormat1bppIndexed:
940 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_1bppIndexed);
941 case PixelFormat8bppIndexed:
942 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_8bppIndexed);
943 case PixelFormat16bppGrayScale:
944 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppGrayScale);
945 case PixelFormat16bppRGB555:
946 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB555);
947 case PixelFormat16bppRGB565:
948 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB565);
949 case PixelFormat16bppARGB1555:
950 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppARGB1555);
951 case PixelFormat24bppRGB:
952 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_24bppRGB);
953 case PixelFormat32bppRGB:
954 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppRGB);
955 case PixelFormat32bppARGB:
956 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppARGB);
957 case PixelFormat32bppPARGB:
958 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppPARGB);
959 case PixelFormat64bppARGB:
960 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_64bppARGB);
961 default:
962 break;
964 break;
965 case PixelFormat64bppARGB:
966 switch (dst_format)
968 case PixelFormat1bppIndexed:
969 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_1bppIndexed);
970 case PixelFormat8bppIndexed:
971 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_8bppIndexed);
972 case PixelFormat16bppGrayScale:
973 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppGrayScale);
974 case PixelFormat16bppRGB555:
975 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB555);
976 case PixelFormat16bppRGB565:
977 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB565);
978 case PixelFormat16bppARGB1555:
979 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppARGB1555);
980 case PixelFormat24bppRGB:
981 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_24bppRGB);
982 case PixelFormat32bppRGB:
983 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppRGB);
984 case PixelFormat32bppARGB:
985 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppARGB);
986 case PixelFormat32bppPARGB:
987 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppPARGB);
988 case PixelFormat48bppRGB:
989 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_48bppRGB);
990 default:
991 break;
993 break;
994 case PixelFormat64bppPARGB:
995 switch (dst_format)
997 case PixelFormat1bppIndexed:
998 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_1bppIndexed);
999 case PixelFormat8bppIndexed:
1000 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_8bppIndexed);
1001 case PixelFormat16bppGrayScale:
1002 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppGrayScale);
1003 case PixelFormat16bppRGB555:
1004 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB555);
1005 case PixelFormat16bppRGB565:
1006 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB565);
1007 case PixelFormat16bppARGB1555:
1008 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppARGB1555);
1009 case PixelFormat24bppRGB:
1010 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_24bppRGB);
1011 case PixelFormat32bppRGB:
1012 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppRGB);
1013 case PixelFormat32bppARGB:
1014 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppARGB);
1015 case PixelFormat32bppPARGB:
1016 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppPARGB);
1017 case PixelFormat48bppRGB:
1018 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_48bppRGB);
1019 case PixelFormat64bppARGB:
1020 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_64bppARGB);
1021 default:
1022 break;
1024 break;
1025 default:
1026 break;
1029 #undef convert_indexed_to_rgb
1030 #undef convert_rgb_to_rgb
1032 return NotImplemented;
1035 /* This function returns a pointer to an array of pixels that represents the
1036 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
1037 * flags. It is correct behavior that a user who calls this function with write
1038 * privileges can write to the whole bitmap (not just the area in rect).
1040 * FIXME: only used portion of format is bits per pixel. */
1041 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
1042 UINT flags, PixelFormat format, BitmapData* lockeddata)
1044 INT bitspp = PIXELFORMATBPP(format);
1045 GpRect act_rect; /* actual rect to be used */
1046 GpStatus stat;
1048 TRACE("%p %p %d 0x%x %p\n", bitmap, rect, flags, format, lockeddata);
1050 if(!lockeddata || !bitmap)
1051 return InvalidParameter;
1053 if(rect){
1054 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
1055 (rect->Y + rect->Height > bitmap->height) || !flags)
1056 return InvalidParameter;
1058 act_rect = *rect;
1060 else{
1061 act_rect.X = act_rect.Y = 0;
1062 act_rect.Width = bitmap->width;
1063 act_rect.Height = bitmap->height;
1066 if(bitmap->lockmode)
1068 WARN("bitmap is already locked and cannot be locked again\n");
1069 return WrongState;
1072 if (bitmap->bits && bitmap->format == format && !(flags & ImageLockModeUserInputBuf))
1074 /* no conversion is necessary; just use the bits directly */
1075 lockeddata->Width = act_rect.Width;
1076 lockeddata->Height = act_rect.Height;
1077 lockeddata->PixelFormat = format;
1078 lockeddata->Reserved = flags;
1079 lockeddata->Stride = bitmap->stride;
1080 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
1081 bitmap->stride * act_rect.Y;
1083 bitmap->lockmode = flags | ImageLockModeRead;
1084 bitmap->numlocks++;
1086 return Ok;
1089 /* Make sure we can convert to the requested format. */
1090 if (flags & ImageLockModeRead)
1092 stat = convert_pixels(0, 0, 0, NULL, format, 0, NULL, bitmap->format, NULL);
1093 if (stat == NotImplemented)
1095 FIXME("cannot read bitmap from %x to %x\n", bitmap->format, format);
1096 return NotImplemented;
1100 /* If we're opening for writing, make sure we'll be able to write back in
1101 * the original format. */
1102 if (flags & ImageLockModeWrite)
1104 stat = convert_pixels(0, 0, 0, NULL, bitmap->format, 0, NULL, format, NULL);
1105 if (stat == NotImplemented)
1107 FIXME("cannot write bitmap from %x to %x\n", format, bitmap->format);
1108 return NotImplemented;
1112 lockeddata->Width = act_rect.Width;
1113 lockeddata->Height = act_rect.Height;
1114 lockeddata->PixelFormat = format;
1115 lockeddata->Reserved = flags;
1117 if(!(flags & ImageLockModeUserInputBuf))
1119 lockeddata->Stride = (((act_rect.Width * bitspp + 7) / 8) + 3) & ~3;
1121 bitmap->bitmapbits = GdipAlloc(lockeddata->Stride * act_rect.Height);
1123 if (!bitmap->bitmapbits) return OutOfMemory;
1125 lockeddata->Scan0 = bitmap->bitmapbits;
1128 if (flags & ImageLockModeRead)
1130 static int fixme=0;
1132 if (!fixme && (PIXELFORMATBPP(bitmap->format) * act_rect.X) % 8 != 0)
1134 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1135 fixme = 1;
1138 stat = convert_pixels(act_rect.Width, act_rect.Height,
1139 lockeddata->Stride, lockeddata->Scan0, format,
1140 bitmap->stride,
1141 bitmap->bits + bitmap->stride * act_rect.Y + PIXELFORMATBPP(bitmap->format) * act_rect.X / 8,
1142 bitmap->format, bitmap->image.palette);
1144 if (stat != Ok)
1146 GdipFree(bitmap->bitmapbits);
1147 bitmap->bitmapbits = NULL;
1148 return stat;
1152 bitmap->lockmode = flags | ImageLockModeRead;
1153 bitmap->numlocks++;
1154 bitmap->lockx = act_rect.X;
1155 bitmap->locky = act_rect.Y;
1157 return Ok;
1160 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
1162 TRACE("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
1164 if (!bitmap || xdpi == 0.0 || ydpi == 0.0)
1165 return InvalidParameter;
1167 bitmap->image.xres = xdpi;
1168 bitmap->image.yres = ydpi;
1170 return Ok;
1173 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
1174 BitmapData* lockeddata)
1176 GpStatus stat;
1177 static int fixme=0;
1179 TRACE("(%p,%p)\n", bitmap, lockeddata);
1181 if(!bitmap || !lockeddata)
1182 return InvalidParameter;
1184 if(!bitmap->lockmode)
1185 return WrongState;
1187 if(!(lockeddata->Reserved & ImageLockModeWrite)){
1188 if(!(--bitmap->numlocks))
1189 bitmap->lockmode = 0;
1191 GdipFree(bitmap->bitmapbits);
1192 bitmap->bitmapbits = NULL;
1193 return Ok;
1196 if (!bitmap->bitmapbits && !(lockeddata->Reserved & ImageLockModeUserInputBuf))
1198 /* we passed a direct reference; no need to do anything */
1199 bitmap->lockmode = 0;
1200 bitmap->numlocks = 0;
1201 return Ok;
1204 if (!fixme && (PIXELFORMATBPP(bitmap->format) * bitmap->lockx) % 8 != 0)
1206 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1207 fixme = 1;
1210 stat = convert_pixels(lockeddata->Width, lockeddata->Height,
1211 bitmap->stride,
1212 bitmap->bits + bitmap->stride * bitmap->locky + PIXELFORMATBPP(bitmap->format) * bitmap->lockx / 8,
1213 bitmap->format,
1214 lockeddata->Stride, lockeddata->Scan0, lockeddata->PixelFormat, NULL);
1216 if (stat != Ok)
1218 ERR("failed to convert pixels; this should never happen\n");
1221 GdipFree(bitmap->bitmapbits);
1222 bitmap->bitmapbits = NULL;
1223 bitmap->lockmode = 0;
1224 bitmap->numlocks = 0;
1226 return stat;
1229 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
1230 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1232 BitmapData lockeddata_src, lockeddata_dst;
1233 int i;
1234 UINT row_size;
1235 Rect area;
1236 GpStatus stat;
1238 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1240 if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
1241 x < 0 || y < 0 ||
1242 x + width > srcBitmap->width || y + height > srcBitmap->height)
1244 TRACE("<-- InvalidParameter\n");
1245 return InvalidParameter;
1248 if (format == PixelFormatDontCare)
1249 format = srcBitmap->format;
1251 area.X = gdip_round(x);
1252 area.Y = gdip_round(y);
1253 area.Width = gdip_round(width);
1254 area.Height = gdip_round(height);
1256 stat = GdipBitmapLockBits(srcBitmap, &area, ImageLockModeRead, format,
1257 &lockeddata_src);
1258 if (stat != Ok) return stat;
1260 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
1261 0, lockeddata_src.PixelFormat, NULL, dstBitmap);
1262 if (stat == Ok)
1264 stat = GdipBitmapLockBits(*dstBitmap, NULL, ImageLockModeWrite,
1265 lockeddata_src.PixelFormat, &lockeddata_dst);
1267 if (stat == Ok)
1269 /* copy the image data */
1270 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
1271 for (i=0; i<lockeddata_src.Height; i++)
1272 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
1273 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
1274 row_size);
1276 GdipBitmapUnlockBits(*dstBitmap, &lockeddata_dst);
1279 if (stat != Ok)
1280 GdipDisposeImage((GpImage*)*dstBitmap);
1283 GdipBitmapUnlockBits(srcBitmap, &lockeddata_src);
1285 if (stat != Ok)
1287 *dstBitmap = NULL;
1290 return stat;
1293 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
1294 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1296 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1298 return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
1301 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
1303 GpStatus stat = GenericError;
1305 TRACE("%p, %p\n", image, cloneImage);
1307 if (!image || !cloneImage)
1308 return InvalidParameter;
1310 if (image->picture)
1312 IStream* stream;
1313 HRESULT hr;
1314 INT size;
1315 LARGE_INTEGER move;
1317 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
1318 if (FAILED(hr))
1319 return GenericError;
1321 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
1322 if(FAILED(hr))
1324 WARN("Failed to save image on stream\n");
1325 goto out;
1328 /* Set seek pointer back to the beginning of the picture */
1329 move.QuadPart = 0;
1330 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1331 if (FAILED(hr))
1332 goto out;
1334 stat = GdipLoadImageFromStream(stream, cloneImage);
1335 if (stat != Ok) WARN("Failed to load image from stream\n");
1337 out:
1338 IStream_Release(stream);
1339 return stat;
1341 else if (image->type == ImageTypeBitmap)
1343 GpBitmap *bitmap = (GpBitmap*)image;
1344 BitmapData lockeddata_src, lockeddata_dst;
1345 int i;
1346 UINT row_size;
1348 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format,
1349 &lockeddata_src);
1350 if (stat != Ok) return stat;
1352 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
1353 0, lockeddata_src.PixelFormat, NULL, (GpBitmap**)cloneImage);
1354 if (stat == Ok)
1356 stat = GdipBitmapLockBits((GpBitmap*)*cloneImage, NULL, ImageLockModeWrite,
1357 lockeddata_src.PixelFormat, &lockeddata_dst);
1359 if (stat == Ok)
1361 /* copy the image data */
1362 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
1363 for (i=0; i<lockeddata_src.Height; i++)
1364 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
1365 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
1366 row_size);
1368 GdipBitmapUnlockBits((GpBitmap*)*cloneImage, &lockeddata_dst);
1371 if (stat != Ok)
1372 GdipDisposeImage(*cloneImage);
1375 GdipBitmapUnlockBits(bitmap, &lockeddata_src);
1377 if (stat != Ok)
1379 *cloneImage = NULL;
1381 else memcpy(&(*cloneImage)->format, &image->format, sizeof(GUID));
1383 return stat;
1385 else
1387 ERR("GpImage with no IPicture or bitmap?!\n");
1388 return NotImplemented;
1392 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
1393 GpBitmap **bitmap)
1395 GpStatus stat;
1396 IStream *stream;
1398 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1400 if(!filename || !bitmap)
1401 return InvalidParameter;
1403 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1405 if(stat != Ok)
1406 return stat;
1408 stat = GdipCreateBitmapFromStream(stream, bitmap);
1410 IStream_Release(stream);
1412 return stat;
1415 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
1416 VOID *bits, GpBitmap **bitmap)
1418 DWORD height, stride;
1419 PixelFormat format;
1421 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
1423 if (!info || !bits || !bitmap)
1424 return InvalidParameter;
1426 height = abs(info->bmiHeader.biHeight);
1427 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
1429 if(info->bmiHeader.biHeight > 0) /* bottom-up */
1431 bits = (BYTE*)bits + (height - 1) * stride;
1432 stride = -stride;
1435 switch(info->bmiHeader.biBitCount) {
1436 case 1:
1437 format = PixelFormat1bppIndexed;
1438 break;
1439 case 4:
1440 format = PixelFormat4bppIndexed;
1441 break;
1442 case 8:
1443 format = PixelFormat8bppIndexed;
1444 break;
1445 case 16:
1446 format = PixelFormat16bppRGB555;
1447 break;
1448 case 24:
1449 format = PixelFormat24bppRGB;
1450 break;
1451 case 32:
1452 format = PixelFormat32bppRGB;
1453 break;
1454 default:
1455 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
1456 *bitmap = NULL;
1457 return InvalidParameter;
1460 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
1461 bits, bitmap);
1465 /* FIXME: no icm */
1466 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
1467 GpBitmap **bitmap)
1469 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1471 return GdipCreateBitmapFromFile(filename, bitmap);
1474 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
1475 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
1477 HBITMAP hbm;
1478 GpStatus stat = InvalidParameter;
1480 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
1482 if(!lpBitmapName || !bitmap)
1483 return InvalidParameter;
1485 /* load DIB */
1486 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
1487 LR_CREATEDIBSECTION);
1489 if(hbm){
1490 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1491 DeleteObject(hbm);
1494 return stat;
1497 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
1498 HBITMAP* hbmReturn, ARGB background)
1500 GpStatus stat;
1501 HBITMAP result;
1502 UINT width, height;
1503 BITMAPINFOHEADER bih;
1504 LPBYTE bits;
1505 BitmapData lockeddata;
1506 TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
1508 if (!bitmap || !hbmReturn) return InvalidParameter;
1510 GdipGetImageWidth((GpImage*)bitmap, &width);
1511 GdipGetImageHeight((GpImage*)bitmap, &height);
1513 bih.biSize = sizeof(bih);
1514 bih.biWidth = width;
1515 bih.biHeight = height;
1516 bih.biPlanes = 1;
1517 bih.biBitCount = 32;
1518 bih.biCompression = BI_RGB;
1519 bih.biSizeImage = 0;
1520 bih.biXPelsPerMeter = 0;
1521 bih.biYPelsPerMeter = 0;
1522 bih.biClrUsed = 0;
1523 bih.biClrImportant = 0;
1525 result = CreateDIBSection(0, (BITMAPINFO*)&bih, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1527 if (result)
1529 lockeddata.Stride = -width * 4;
1530 lockeddata.Scan0 = bits + (width * 4 * (height - 1));
1532 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead|ImageLockModeUserInputBuf,
1533 PixelFormat32bppPARGB, &lockeddata);
1535 if (stat == Ok)
1536 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1538 else
1539 stat = GenericError;
1541 if (stat != Ok && result)
1543 DeleteObject(result);
1544 result = NULL;
1547 *hbmReturn = result;
1549 return stat;
1552 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
1553 GpMetafile* metafile, BOOL* succ, EmfType emfType,
1554 const WCHAR* description, GpMetafile** out_metafile)
1556 static int calls;
1558 TRACE("(%p,%p,%p,%u,%s,%p)\n", ref, metafile, succ, emfType,
1559 debugstr_w(description), out_metafile);
1561 if(!ref || !metafile || !out_metafile)
1562 return InvalidParameter;
1564 *succ = FALSE;
1565 *out_metafile = NULL;
1567 if(!(calls++))
1568 FIXME("not implemented\n");
1570 return NotImplemented;
1573 /* FIXME: this should create a bitmap in the given size with the attributes
1574 * (resolution etc.) of the graphics object */
1575 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
1576 GpGraphics* target, GpBitmap** bitmap)
1578 static int calls;
1579 GpStatus ret;
1581 TRACE("(%d, %d, %p, %p)\n", width, height, target, bitmap);
1583 if(!target || !bitmap)
1584 return InvalidParameter;
1586 if(!(calls++))
1587 FIXME("hacked stub\n");
1589 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
1590 NULL, bitmap);
1592 return ret;
1595 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1597 GpStatus stat;
1598 ICONINFO iinfo;
1599 BITMAP bm;
1600 int ret;
1601 UINT width, height;
1602 GpRect rect;
1603 BitmapData lockeddata;
1604 HDC screendc;
1605 BOOL has_alpha;
1606 int x, y;
1607 BYTE *bits;
1608 BITMAPINFOHEADER bih;
1609 DWORD *src;
1610 BYTE *dst_row;
1611 DWORD *dst;
1613 TRACE("%p, %p\n", hicon, bitmap);
1615 if(!bitmap || !GetIconInfo(hicon, &iinfo))
1616 return InvalidParameter;
1618 /* get the size of the icon */
1619 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1620 if (ret == 0) {
1621 DeleteObject(iinfo.hbmColor);
1622 DeleteObject(iinfo.hbmMask);
1623 return GenericError;
1626 width = bm.bmWidth;
1628 if (iinfo.hbmColor)
1629 height = abs(bm.bmHeight);
1630 else /* combined bitmap + mask */
1631 height = abs(bm.bmHeight) / 2;
1633 bits = HeapAlloc(GetProcessHeap(), 0, 4*width*height);
1634 if (!bits) {
1635 DeleteObject(iinfo.hbmColor);
1636 DeleteObject(iinfo.hbmMask);
1637 return OutOfMemory;
1640 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB, NULL, bitmap);
1641 if (stat != Ok) {
1642 DeleteObject(iinfo.hbmColor);
1643 DeleteObject(iinfo.hbmMask);
1644 HeapFree(GetProcessHeap(), 0, bits);
1645 return stat;
1648 rect.X = 0;
1649 rect.Y = 0;
1650 rect.Width = width;
1651 rect.Height = height;
1653 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1654 if (stat != Ok) {
1655 DeleteObject(iinfo.hbmColor);
1656 DeleteObject(iinfo.hbmMask);
1657 HeapFree(GetProcessHeap(), 0, bits);
1658 GdipDisposeImage((GpImage*)*bitmap);
1659 return stat;
1662 bih.biSize = sizeof(bih);
1663 bih.biWidth = width;
1664 bih.biHeight = -height;
1665 bih.biPlanes = 1;
1666 bih.biBitCount = 32;
1667 bih.biCompression = BI_RGB;
1668 bih.biSizeImage = 0;
1669 bih.biXPelsPerMeter = 0;
1670 bih.biYPelsPerMeter = 0;
1671 bih.biClrUsed = 0;
1672 bih.biClrImportant = 0;
1674 screendc = GetDC(0);
1675 if (iinfo.hbmColor)
1677 GetDIBits(screendc, iinfo.hbmColor, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1679 if (bm.bmBitsPixel == 32)
1681 has_alpha = FALSE;
1683 /* If any pixel has a non-zero alpha, ignore hbmMask */
1684 src = (DWORD*)bits;
1685 for (x=0; x<width && !has_alpha; x++)
1686 for (y=0; y<height && !has_alpha; y++)
1687 if ((*src++ & 0xff000000) != 0)
1688 has_alpha = TRUE;
1690 else has_alpha = FALSE;
1692 else
1694 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1695 has_alpha = FALSE;
1698 /* copy the image data to the Bitmap */
1699 src = (DWORD*)bits;
1700 dst_row = lockeddata.Scan0;
1701 for (y=0; y<height; y++)
1703 memcpy(dst_row, src, width*4);
1704 src += width;
1705 dst_row += lockeddata.Stride;
1708 if (!has_alpha)
1710 if (iinfo.hbmMask)
1712 /* read alpha data from the mask */
1713 if (iinfo.hbmColor)
1714 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1715 else
1716 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1718 src = (DWORD*)bits;
1719 dst_row = lockeddata.Scan0;
1720 for (y=0; y<height; y++)
1722 dst = (DWORD*)dst_row;
1723 for (x=0; x<height; x++)
1725 DWORD src_value = *src++;
1726 if (src_value)
1727 *dst++ = 0;
1728 else
1729 *dst++ |= 0xff000000;
1731 dst_row += lockeddata.Stride;
1734 else
1736 /* set constant alpha of 255 */
1737 dst_row = bits;
1738 for (y=0; y<height; y++)
1740 dst = (DWORD*)dst_row;
1741 for (x=0; x<height; x++)
1742 *dst++ |= 0xff000000;
1743 dst_row += lockeddata.Stride;
1748 ReleaseDC(0, screendc);
1750 DeleteObject(iinfo.hbmColor);
1751 DeleteObject(iinfo.hbmMask);
1753 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1755 HeapFree(GetProcessHeap(), 0, bits);
1757 return Ok;
1760 static void generate_halftone_palette(ARGB *entries, UINT count)
1762 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1763 UINT i;
1765 for (i=0; i<8 && i<count; i++)
1767 entries[i] = 0xff000000;
1768 if (i&1) entries[i] |= 0x800000;
1769 if (i&2) entries[i] |= 0x8000;
1770 if (i&4) entries[i] |= 0x80;
1773 if (8 < count)
1774 entries[i] = 0xffc0c0c0;
1776 for (i=9; i<16 && i<count; i++)
1778 entries[i] = 0xff000000;
1779 if (i&1) entries[i] |= 0xff0000;
1780 if (i&2) entries[i] |= 0xff00;
1781 if (i&4) entries[i] |= 0xff;
1784 for (i=16; i<40 && i<count; i++)
1786 entries[i] = 0;
1789 for (i=40; i<256 && i<count; i++)
1791 entries[i] = 0xff000000;
1792 entries[i] |= halftone_values[(i-40)%6];
1793 entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1794 entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1798 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1800 HDC screendc = GetDC(0);
1802 if (!screendc) return GenericError;
1804 *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1805 *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1807 ReleaseDC(0, screendc);
1809 return Ok;
1812 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1813 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1815 BITMAPINFO* pbmi;
1816 HBITMAP hbitmap=NULL;
1817 INT row_size, dib_stride;
1818 BYTE *bits=NULL, *own_bits=NULL;
1819 REAL xres, yres;
1820 GpStatus stat;
1822 TRACE("%d %d %d 0x%x %p %p\n", width, height, stride, format, scan0, bitmap);
1824 if (!bitmap) return InvalidParameter;
1826 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1827 *bitmap = NULL;
1828 return InvalidParameter;
1831 if(scan0 && !stride)
1832 return InvalidParameter;
1834 stat = get_screen_resolution(&xres, &yres);
1835 if (stat != Ok) return stat;
1837 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1838 dib_stride = (row_size + 3) & ~3;
1840 if(stride == 0)
1841 stride = dib_stride;
1843 if (format & PixelFormatGDI && !(format & (PixelFormatAlpha|PixelFormatIndexed)) && !scan0)
1845 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1846 if (!pbmi)
1847 return OutOfMemory;
1849 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1850 pbmi->bmiHeader.biWidth = width;
1851 pbmi->bmiHeader.biHeight = -height;
1852 pbmi->bmiHeader.biPlanes = 1;
1853 /* FIXME: use the rest of the data from format */
1854 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(format);
1855 pbmi->bmiHeader.biCompression = BI_RGB;
1856 pbmi->bmiHeader.biSizeImage = 0;
1857 pbmi->bmiHeader.biXPelsPerMeter = 0;
1858 pbmi->bmiHeader.biYPelsPerMeter = 0;
1859 pbmi->bmiHeader.biClrUsed = 0;
1860 pbmi->bmiHeader.biClrImportant = 0;
1862 hbitmap = CreateDIBSection(0, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1864 GdipFree(pbmi);
1866 if (!hbitmap) return GenericError;
1868 stride = dib_stride;
1870 else
1872 /* Not a GDI format; don't try to make an HBITMAP. */
1873 if (scan0)
1874 bits = scan0;
1875 else
1877 INT size = abs(stride) * height;
1879 own_bits = bits = GdipAlloc(size);
1880 if (!own_bits) return OutOfMemory;
1882 if (stride < 0)
1883 bits += stride * (1 - height);
1887 *bitmap = GdipAlloc(sizeof(GpBitmap));
1888 if(!*bitmap)
1890 DeleteObject(hbitmap);
1891 GdipFree(own_bits);
1892 return OutOfMemory;
1895 (*bitmap)->image.type = ImageTypeBitmap;
1896 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1897 (*bitmap)->image.flags = ImageFlagsNone;
1898 (*bitmap)->image.frame_count = 1;
1899 (*bitmap)->image.current_frame = 0;
1900 (*bitmap)->image.palette = NULL;
1901 (*bitmap)->image.xres = xres;
1902 (*bitmap)->image.yres = yres;
1903 (*bitmap)->width = width;
1904 (*bitmap)->height = height;
1905 (*bitmap)->format = format;
1906 (*bitmap)->image.picture = NULL;
1907 (*bitmap)->image.stream = NULL;
1908 (*bitmap)->hbitmap = hbitmap;
1909 (*bitmap)->hdc = NULL;
1910 (*bitmap)->bits = bits;
1911 (*bitmap)->stride = stride;
1912 (*bitmap)->own_bits = own_bits;
1913 (*bitmap)->metadata_reader = NULL;
1915 /* set format-related flags */
1916 if (format & (PixelFormatAlpha|PixelFormatPAlpha|PixelFormatIndexed))
1917 (*bitmap)->image.flags |= ImageFlagsHasAlpha;
1919 if (format == PixelFormat1bppIndexed ||
1920 format == PixelFormat4bppIndexed ||
1921 format == PixelFormat8bppIndexed)
1923 (*bitmap)->image.palette = GdipAlloc(sizeof(UINT) * 2 + sizeof(ARGB) * (1 << PIXELFORMATBPP(format)));
1925 if (!(*bitmap)->image.palette)
1927 GdipDisposeImage(&(*bitmap)->image);
1928 *bitmap = NULL;
1929 return OutOfMemory;
1932 (*bitmap)->image.palette->Count = 1 << PIXELFORMATBPP(format);
1934 if (format == PixelFormat1bppIndexed)
1936 (*bitmap)->image.palette->Flags = PaletteFlagsGrayScale;
1937 (*bitmap)->image.palette->Entries[0] = 0xff000000;
1938 (*bitmap)->image.palette->Entries[1] = 0xffffffff;
1940 else
1942 if (format == PixelFormat8bppIndexed)
1943 (*bitmap)->image.palette->Flags = PaletteFlagsHalftone;
1945 generate_halftone_palette((*bitmap)->image.palette->Entries,
1946 (*bitmap)->image.palette->Count);
1950 TRACE("<-- %p\n", *bitmap);
1952 return Ok;
1955 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1956 GpBitmap **bitmap)
1958 GpStatus stat;
1960 TRACE("%p %p\n", stream, bitmap);
1962 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1964 if(stat != Ok)
1965 return stat;
1967 if((*bitmap)->image.type != ImageTypeBitmap){
1968 GdipDisposeImage(&(*bitmap)->image);
1969 *bitmap = NULL;
1970 return GenericError; /* FIXME: what error to return? */
1973 return Ok;
1976 /* FIXME: no icm */
1977 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1978 GpBitmap **bitmap)
1980 TRACE("%p %p\n", stream, bitmap);
1982 return GdipCreateBitmapFromStream(stream, bitmap);
1985 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1986 GpCachedBitmap **cachedbmp)
1988 GpStatus stat;
1990 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1992 if(!bitmap || !graphics || !cachedbmp)
1993 return InvalidParameter;
1995 *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
1996 if(!*cachedbmp)
1997 return OutOfMemory;
1999 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
2000 if(stat != Ok){
2001 GdipFree(*cachedbmp);
2002 return stat;
2005 return Ok;
2008 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
2010 GpStatus stat;
2011 BitmapData lockeddata;
2012 ULONG andstride, xorstride, bitssize;
2013 LPBYTE andbits, xorbits, androw, xorrow, srcrow;
2014 UINT x, y;
2016 TRACE("(%p, %p)\n", bitmap, hicon);
2018 if (!bitmap || !hicon)
2019 return InvalidParameter;
2021 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead,
2022 PixelFormat32bppPARGB, &lockeddata);
2023 if (stat == Ok)
2025 andstride = ((lockeddata.Width+31)/32)*4;
2026 xorstride = lockeddata.Width*4;
2027 bitssize = (andstride + xorstride) * lockeddata.Height;
2029 andbits = GdipAlloc(bitssize);
2031 if (andbits)
2033 xorbits = andbits + andstride * lockeddata.Height;
2035 for (y=0; y<lockeddata.Height; y++)
2037 srcrow = ((LPBYTE)lockeddata.Scan0) + lockeddata.Stride * y;
2039 androw = andbits + andstride * y;
2040 for (x=0; x<lockeddata.Width; x++)
2041 if (srcrow[3+4*x] >= 128)
2042 androw[x/8] |= 1 << (7-x%8);
2044 xorrow = xorbits + xorstride * y;
2045 memcpy(xorrow, srcrow, xorstride);
2048 *hicon = CreateIcon(NULL, lockeddata.Width, lockeddata.Height, 1, 32,
2049 andbits, xorbits);
2051 GdipFree(andbits);
2053 else
2054 stat = OutOfMemory;
2056 GdipBitmapUnlockBits(bitmap, &lockeddata);
2059 return stat;
2062 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
2064 TRACE("%p\n", cachedbmp);
2066 if(!cachedbmp)
2067 return InvalidParameter;
2069 GdipDisposeImage(cachedbmp->image);
2070 GdipFree(cachedbmp);
2072 return Ok;
2075 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
2076 GpCachedBitmap *cachedbmp, INT x, INT y)
2078 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
2080 if(!graphics || !cachedbmp)
2081 return InvalidParameter;
2083 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
2086 GpStatus WINGDIPAPI GdipEmfToWmfBits(HENHMETAFILE hemf, UINT cbData16,
2087 LPBYTE pData16, INT iMapMode, INT eFlags)
2089 FIXME("(%p, %d, %p, %d, %d): stub\n", hemf, cbData16, pData16, iMapMode, eFlags);
2090 return NotImplemented;
2093 /* Internal utility function: Replace the image data of dst with that of src,
2094 * and free src. */
2095 static void move_bitmap(GpBitmap *dst, GpBitmap *src, BOOL clobber_palette)
2097 assert(src->image.type == ImageTypeBitmap);
2098 assert(dst->image.type == ImageTypeBitmap);
2100 GdipFree(dst->bitmapbits);
2101 GdipFree(dst->own_bits);
2102 DeleteDC(dst->hdc);
2103 DeleteObject(dst->hbitmap);
2105 if (clobber_palette)
2107 GdipFree(dst->image.palette);
2108 dst->image.palette = src->image.palette;
2110 else
2111 GdipFree(src->image.palette);
2113 dst->image.xres = src->image.xres;
2114 dst->image.yres = src->image.yres;
2115 dst->width = src->width;
2116 dst->height = src->height;
2117 dst->format = src->format;
2118 dst->hbitmap = src->hbitmap;
2119 dst->hdc = src->hdc;
2120 dst->bits = src->bits;
2121 dst->stride = src->stride;
2122 dst->own_bits = src->own_bits;
2123 if (dst->metadata_reader)
2124 IWICMetadataReader_Release(dst->metadata_reader);
2125 dst->metadata_reader = src->metadata_reader;
2126 if (dst->image.stream)
2127 IStream_Release(dst->image.stream);
2128 dst->image.stream = src->image.stream;
2129 dst->image.frame_count = src->image.frame_count;
2130 dst->image.current_frame = src->image.current_frame;
2131 dst->image.format = src->image.format;
2133 src->image.type = ~0;
2134 GdipFree(src);
2137 static GpStatus free_image_data(GpImage *image)
2139 if(!image)
2140 return InvalidParameter;
2142 if (image->type == ImageTypeBitmap)
2144 GdipFree(((GpBitmap*)image)->bitmapbits);
2145 GdipFree(((GpBitmap*)image)->own_bits);
2146 DeleteDC(((GpBitmap*)image)->hdc);
2147 DeleteObject(((GpBitmap*)image)->hbitmap);
2148 if (((GpBitmap*)image)->metadata_reader)
2149 IWICMetadataReader_Release(((GpBitmap*)image)->metadata_reader);
2151 else if (image->type == ImageTypeMetafile)
2153 GpMetafile *metafile = (GpMetafile*)image;
2154 GdipFree(metafile->comment_data);
2155 DeleteEnhMetaFile(CloseEnhMetaFile(metafile->record_dc));
2156 DeleteEnhMetaFile(metafile->hemf);
2157 if (metafile->record_graphics)
2159 WARN("metafile closed while recording\n");
2160 /* not sure what to do here; for now just prevent the graphics from functioning or using this object */
2161 metafile->record_graphics->image = NULL;
2162 metafile->record_graphics->busy = TRUE;
2165 else
2167 WARN("invalid image: %p\n", image);
2168 return ObjectBusy;
2170 if (image->picture)
2171 IPicture_Release(image->picture);
2172 if (image->stream)
2173 IStream_Release(image->stream);
2174 GdipFree(image->palette);
2176 return Ok;
2179 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
2181 GpStatus status;
2183 TRACE("%p\n", image);
2185 status = free_image_data(image);
2186 if (status != Ok) return status;
2187 image->type = ~0;
2188 GdipFree(image);
2190 return Ok;
2193 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
2195 static int calls;
2197 TRACE("(%p,%p)\n", image, item);
2199 if(!image || !item)
2200 return InvalidParameter;
2202 if (!(calls++))
2203 FIXME("not implemented\n");
2205 return NotImplemented;
2208 GpStatus WINGDIPAPI GdipGetImageItemData(GpImage *image, ImageItemData *item)
2210 static int calls;
2212 TRACE("(%p,%p)\n", image, item);
2214 if (!(calls++))
2215 FIXME("not implemented\n");
2217 return NotImplemented;
2220 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
2221 GpUnit *srcUnit)
2223 TRACE("%p %p %p\n", image, srcRect, srcUnit);
2225 if(!image || !srcRect || !srcUnit)
2226 return InvalidParameter;
2227 if(image->type == ImageTypeMetafile){
2228 *srcRect = ((GpMetafile*)image)->bounds;
2229 *srcUnit = ((GpMetafile*)image)->unit;
2231 else if(image->type == ImageTypeBitmap){
2232 srcRect->X = srcRect->Y = 0.0;
2233 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
2234 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
2235 *srcUnit = UnitPixel;
2237 else{
2238 srcRect->X = srcRect->Y = 0.0;
2239 srcRect->Width = ipicture_pixel_width(image->picture);
2240 srcRect->Height = ipicture_pixel_height(image->picture);
2241 *srcUnit = UnitPixel;
2244 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
2245 srcRect->Width, srcRect->Height, *srcUnit);
2247 return Ok;
2250 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
2251 REAL *height)
2253 TRACE("%p %p %p\n", image, width, height);
2255 if(!image || !height || !width)
2256 return InvalidParameter;
2258 if(image->type == ImageTypeMetafile){
2259 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2260 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2262 else if(image->type == ImageTypeBitmap){
2263 *height = ((GpBitmap*)image)->height;
2264 *width = ((GpBitmap*)image)->width;
2266 else{
2267 *height = ipicture_pixel_height(image->picture);
2268 *width = ipicture_pixel_width(image->picture);
2271 TRACE("returning (%f, %f)\n", *height, *width);
2272 return Ok;
2275 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
2276 GpGraphics **graphics)
2278 HDC hdc;
2279 GpStatus stat;
2281 TRACE("%p %p\n", image, graphics);
2283 if(!image || !graphics)
2284 return InvalidParameter;
2286 if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
2288 hdc = ((GpBitmap*)image)->hdc;
2290 if(!hdc){
2291 hdc = CreateCompatibleDC(0);
2292 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
2293 ((GpBitmap*)image)->hdc = hdc;
2296 stat = GdipCreateFromHDC(hdc, graphics);
2298 if (stat == Ok)
2300 (*graphics)->image = image;
2301 (*graphics)->xres = image->xres;
2302 (*graphics)->yres = image->yres;
2305 else if (image->type == ImageTypeMetafile)
2306 stat = METAFILE_GetGraphicsContext((GpMetafile*)image, graphics);
2307 else
2308 stat = graphics_from_image(image, graphics);
2310 return stat;
2313 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
2315 TRACE("%p %p\n", image, height);
2317 if(!image || !height)
2318 return InvalidParameter;
2320 if(image->type == ImageTypeMetafile)
2321 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2322 else if(image->type == ImageTypeBitmap)
2323 *height = ((GpBitmap*)image)->height;
2324 else
2325 *height = ipicture_pixel_height(image->picture);
2327 TRACE("returning %d\n", *height);
2329 return Ok;
2332 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2334 if(!image || !res)
2335 return InvalidParameter;
2337 *res = image->xres;
2339 TRACE("(%p) <-- %0.2f\n", image, *res);
2341 return Ok;
2344 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2346 TRACE("%p %p\n", image, size);
2348 if(!image || !size)
2349 return InvalidParameter;
2351 if (!image->palette || image->palette->Count == 0)
2352 *size = sizeof(ColorPalette);
2353 else
2354 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette->Count;
2356 TRACE("<-- %u\n", *size);
2358 return Ok;
2361 /* FIXME: test this function for non-bitmap types */
2362 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2364 TRACE("%p %p\n", image, format);
2366 if(!image || !format)
2367 return InvalidParameter;
2369 if(image->type != ImageTypeBitmap)
2370 *format = PixelFormat24bppRGB;
2371 else
2372 *format = ((GpBitmap*) image)->format;
2374 return Ok;
2377 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2379 TRACE("(%p, %p)\n", image, format);
2381 if(!image || !format)
2382 return InvalidParameter;
2384 memcpy(format, &image->format, sizeof(GUID));
2386 return Ok;
2389 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2391 TRACE("%p %p\n", image, type);
2393 if(!image || !type)
2394 return InvalidParameter;
2396 *type = image->type;
2398 return Ok;
2401 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2403 if(!image || !res)
2404 return InvalidParameter;
2406 *res = image->yres;
2408 TRACE("(%p) <-- %0.2f\n", image, *res);
2410 return Ok;
2413 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2415 TRACE("%p %p\n", image, width);
2417 if(!image || !width)
2418 return InvalidParameter;
2420 if(image->type == ImageTypeMetafile)
2421 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2422 else if(image->type == ImageTypeBitmap)
2423 *width = ((GpBitmap*)image)->width;
2424 else
2425 *width = ipicture_pixel_width(image->picture);
2427 TRACE("returning %d\n", *width);
2429 return Ok;
2432 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
2433 MetafileHeader * header)
2435 static int calls;
2437 TRACE("(%p, %p)\n", metafile, header);
2439 if(!metafile || !header)
2440 return InvalidParameter;
2442 if(!(calls++))
2443 FIXME("not implemented\n");
2445 memset(header, 0, sizeof(MetafileHeader));
2447 return Ok;
2450 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromEmf(HENHMETAFILE hEmf,
2451 MetafileHeader *header)
2453 static int calls;
2455 if(!hEmf || !header)
2456 return InvalidParameter;
2458 if(!(calls++))
2459 FIXME("not implemented\n");
2461 memset(header, 0, sizeof(MetafileHeader));
2463 return Ok;
2466 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromFile(GDIPCONST WCHAR *filename,
2467 MetafileHeader *header)
2469 static int calls;
2471 TRACE("(%s,%p)\n", debugstr_w(filename), header);
2473 if(!filename || !header)
2474 return InvalidParameter;
2476 if(!(calls++))
2477 FIXME("not implemented\n");
2479 memset(header, 0, sizeof(MetafileHeader));
2481 return Ok;
2484 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromStream(IStream *stream,
2485 MetafileHeader *header)
2487 static int calls;
2489 TRACE("(%p,%p)\n", stream, header);
2491 if(!stream || !header)
2492 return InvalidParameter;
2494 if(!(calls++))
2495 FIXME("not implemented\n");
2497 memset(header, 0, sizeof(MetafileHeader));
2499 return Ok;
2502 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT *num)
2504 TRACE("(%p, %p)\n", image, num);
2506 if (!image || !num) return InvalidParameter;
2508 *num = 0;
2510 if (image->type == ImageTypeBitmap)
2512 if (((GpBitmap *)image)->metadata_reader)
2513 IWICMetadataReader_GetCount(((GpBitmap *)image)->metadata_reader, num);
2516 return Ok;
2519 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID *list)
2521 HRESULT hr;
2522 IWICMetadataReader *reader;
2523 IWICEnumMetadataItem *enumerator;
2524 UINT prop_count, i, items_returned;
2526 TRACE("(%p, %u, %p)\n", image, num, list);
2528 if (!image || !list) return InvalidParameter;
2530 if (image->type != ImageTypeBitmap)
2532 FIXME("Not implemented for type %d\n", image->type);
2533 return NotImplemented;
2536 reader = ((GpBitmap *)image)->metadata_reader;
2537 if (!reader)
2539 if (num != 0) return InvalidParameter;
2540 return Ok;
2543 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2544 if (FAILED(hr)) return hresult_to_status(hr);
2546 if (num != prop_count) return InvalidParameter;
2548 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2549 if (FAILED(hr)) return hresult_to_status(hr);
2551 IWICEnumMetadataItem_Reset(enumerator);
2553 for (i = 0; i < num; i++)
2555 PROPVARIANT id;
2557 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, NULL, &items_returned);
2558 if (hr != S_OK) break;
2560 if (id.vt != VT_UI2)
2562 FIXME("not supported propvariant type for id: %u\n", id.vt);
2563 list[i] = 0;
2564 continue;
2566 list[i] = id.u.uiVal;
2569 IWICEnumMetadataItem_Release(enumerator);
2571 return hr == S_OK ? Ok : hresult_to_status(hr);
2574 static UINT propvariant_size(PROPVARIANT *value)
2576 switch (value->vt & ~VT_VECTOR)
2578 case VT_EMPTY:
2579 return 0;
2580 case VT_I1:
2581 case VT_UI1:
2582 if (!(value->vt & VT_VECTOR)) return 1;
2583 return value->u.caub.cElems;
2584 case VT_I2:
2585 case VT_UI2:
2586 if (!(value->vt & VT_VECTOR)) return 2;
2587 return value->u.caui.cElems * 2;
2588 case VT_I4:
2589 case VT_UI4:
2590 case VT_R4:
2591 if (!(value->vt & VT_VECTOR)) return 4;
2592 return value->u.caul.cElems * 4;
2593 case VT_I8:
2594 case VT_UI8:
2595 case VT_R8:
2596 if (!(value->vt & VT_VECTOR)) return 8;
2597 return value->u.cauh.cElems * 8;
2598 case VT_LPSTR:
2599 return value->u.pszVal ? strlen(value->u.pszVal) + 1 : 0;
2600 case VT_BLOB:
2601 return value->u.blob.cbSize;
2602 default:
2603 FIXME("not supported variant type %d\n", value->vt);
2604 return 0;
2608 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID propid, UINT *size)
2610 HRESULT hr;
2611 IWICMetadataReader *reader;
2612 PROPVARIANT id, value;
2614 TRACE("(%p,%#x,%p)\n", image, propid, size);
2616 if (!size || !image) return InvalidParameter;
2618 if (image->type != ImageTypeBitmap)
2620 FIXME("Not implemented for type %d\n", image->type);
2621 return NotImplemented;
2624 reader = ((GpBitmap *)image)->metadata_reader;
2625 if (!reader) return PropertyNotFound;
2627 id.vt = VT_UI2;
2628 id.u.uiVal = propid;
2629 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2630 if (FAILED(hr)) return PropertyNotFound;
2632 *size = propvariant_size(&value);
2633 if (*size) *size += sizeof(PropertyItem);
2634 PropVariantClear(&value);
2636 return Ok;
2639 #ifndef PropertyTagTypeSByte
2640 #define PropertyTagTypeSByte 6
2641 #define PropertyTagTypeSShort 8
2642 #define PropertyTagTypeFloat 11
2643 #define PropertyTagTypeDouble 12
2644 #endif
2646 static UINT vt_to_itemtype(UINT vt)
2648 static const struct
2650 UINT vt, type;
2651 } vt2type[] =
2653 { VT_I1, PropertyTagTypeSByte },
2654 { VT_UI1, PropertyTagTypeByte },
2655 { VT_I2, PropertyTagTypeSShort },
2656 { VT_UI2, PropertyTagTypeShort },
2657 { VT_I4, PropertyTagTypeSLONG },
2658 { VT_UI4, PropertyTagTypeLong },
2659 { VT_I8, PropertyTagTypeSRational },
2660 { VT_UI8, PropertyTagTypeRational },
2661 { VT_R4, PropertyTagTypeFloat },
2662 { VT_R8, PropertyTagTypeDouble },
2663 { VT_LPSTR, PropertyTagTypeASCII },
2664 { VT_BLOB, PropertyTagTypeUndefined }
2666 UINT i;
2667 for (i = 0; i < sizeof(vt2type)/sizeof(vt2type[0]); i++)
2669 if (vt2type[i].vt == vt) return vt2type[i].type;
2671 FIXME("not supported variant type %u\n", vt);
2672 return 0;
2675 static GpStatus propvariant_to_item(PROPVARIANT *value, PropertyItem *item,
2676 UINT size, PROPID id)
2678 UINT item_size, item_type;
2680 item_size = propvariant_size(value);
2681 if (size != item_size + sizeof(PropertyItem)) return InvalidParameter;
2683 item_type = vt_to_itemtype(value->vt & ~VT_VECTOR);
2684 if (!item_type) return InvalidParameter;
2686 item->value = item + 1;
2688 switch (value->vt & ~VT_VECTOR)
2690 case VT_I1:
2691 case VT_UI1:
2692 if (!(value->vt & VT_VECTOR))
2693 *(BYTE *)item->value = value->u.bVal;
2694 else
2695 memcpy(item->value, value->u.caub.pElems, item_size);
2696 break;
2697 case VT_I2:
2698 case VT_UI2:
2699 if (!(value->vt & VT_VECTOR))
2700 *(USHORT *)item->value = value->u.uiVal;
2701 else
2702 memcpy(item->value, value->u.caui.pElems, item_size);
2703 break;
2704 case VT_I4:
2705 case VT_UI4:
2706 case VT_R4:
2707 if (!(value->vt & VT_VECTOR))
2708 *(ULONG *)item->value = value->u.ulVal;
2709 else
2710 memcpy(item->value, value->u.caul.pElems, item_size);
2711 break;
2712 case VT_I8:
2713 case VT_UI8:
2714 case VT_R8:
2715 if (!(value->vt & VT_VECTOR))
2716 *(ULONGLONG *)item->value = value->u.uhVal.QuadPart;
2717 else
2718 memcpy(item->value, value->u.cauh.pElems, item_size);
2719 break;
2720 case VT_LPSTR:
2721 memcpy(item->value, value->u.pszVal, item_size);
2722 break;
2723 case VT_BLOB:
2724 memcpy(item->value, value->u.blob.pBlobData, item_size);
2725 break;
2726 default:
2727 FIXME("not supported variant type %d\n", value->vt);
2728 return InvalidParameter;
2731 item->length = item_size;
2732 item->type = item_type;
2733 item->id = id;
2735 return Ok;
2738 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID propid, UINT size,
2739 PropertyItem *buffer)
2741 GpStatus stat;
2742 HRESULT hr;
2743 IWICMetadataReader *reader;
2744 PROPVARIANT id, value;
2746 TRACE("(%p,%#x,%u,%p)\n", image, propid, size, buffer);
2748 if (!image || !buffer) return InvalidParameter;
2750 if (image->type != ImageTypeBitmap)
2752 FIXME("Not implemented for type %d\n", image->type);
2753 return NotImplemented;
2756 reader = ((GpBitmap *)image)->metadata_reader;
2757 if (!reader) return PropertyNotFound;
2759 id.vt = VT_UI2;
2760 id.u.uiVal = propid;
2761 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2762 if (FAILED(hr)) return PropertyNotFound;
2764 stat = propvariant_to_item(&value, buffer, size, propid);
2765 PropVariantClear(&value);
2767 return stat;
2770 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT *size, UINT *count)
2772 HRESULT hr;
2773 IWICMetadataReader *reader;
2774 IWICEnumMetadataItem *enumerator;
2775 UINT prop_count, prop_size, i;
2776 PROPVARIANT id, value;
2778 TRACE("(%p,%p,%p)\n", image, size, count);
2780 if (!image || !size || !count) return InvalidParameter;
2782 if (image->type != ImageTypeBitmap)
2784 FIXME("Not implemented for type %d\n", image->type);
2785 return NotImplemented;
2788 reader = ((GpBitmap *)image)->metadata_reader;
2789 if (!reader) return PropertyNotFound;
2791 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2792 if (FAILED(hr)) return hresult_to_status(hr);
2794 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2795 if (FAILED(hr)) return hresult_to_status(hr);
2797 IWICEnumMetadataItem_Reset(enumerator);
2799 prop_size = 0;
2801 PropVariantInit(&id);
2802 PropVariantInit(&value);
2804 for (i = 0; i < prop_count; i++)
2806 UINT items_returned, item_size;
2808 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2809 if (hr != S_OK) break;
2811 item_size = propvariant_size(&value);
2812 if (item_size) prop_size += sizeof(PropertyItem) + item_size;
2814 PropVariantClear(&id);
2815 PropVariantClear(&value);
2818 IWICEnumMetadataItem_Release(enumerator);
2820 if (hr != S_OK) return PropertyNotFound;
2822 *count = prop_count;
2823 *size = prop_size;
2824 return Ok;
2827 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2828 UINT count, PropertyItem *buf)
2830 GpStatus status;
2831 HRESULT hr;
2832 IWICMetadataReader *reader;
2833 IWICEnumMetadataItem *enumerator;
2834 UINT prop_count, prop_size, i;
2835 PROPVARIANT id, value;
2836 char *item_value;
2838 TRACE("(%p,%u,%u,%p)\n", image, size, count, buf);
2840 if (!image || !buf) return InvalidParameter;
2842 if (image->type != ImageTypeBitmap)
2844 FIXME("Not implemented for type %d\n", image->type);
2845 return NotImplemented;
2848 status = GdipGetPropertySize(image, &prop_size, &prop_count);
2849 if (status != Ok) return status;
2851 if (prop_count != count || prop_size != size) return InvalidParameter;
2853 reader = ((GpBitmap *)image)->metadata_reader;
2854 if (!reader) return PropertyNotFound;
2856 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2857 if (FAILED(hr)) return hresult_to_status(hr);
2859 IWICEnumMetadataItem_Reset(enumerator);
2861 item_value = (char *)(buf + prop_count);
2863 PropVariantInit(&id);
2864 PropVariantInit(&value);
2866 for (i = 0; i < prop_count; i++)
2868 PropertyItem *item;
2869 UINT items_returned, item_size;
2871 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2872 if (hr != S_OK) break;
2874 if (id.vt != VT_UI2)
2876 FIXME("not supported propvariant type for id: %u\n", id.vt);
2877 continue;
2880 item_size = propvariant_size(&value);
2881 if (item_size)
2883 item = HeapAlloc(GetProcessHeap(), 0, item_size + sizeof(*item));
2885 propvariant_to_item(&value, item, item_size + sizeof(*item), id.u.uiVal);
2886 buf[i].id = item->id;
2887 buf[i].type = item->type;
2888 buf[i].length = item_size;
2889 buf[i].value = item_value;
2890 memcpy(item_value, item->value, item_size);
2891 item_value += item_size;
2893 HeapFree(GetProcessHeap(), 0, item);
2896 PropVariantClear(&id);
2897 PropVariantClear(&value);
2900 IWICEnumMetadataItem_Release(enumerator);
2902 if (hr != S_OK) return PropertyNotFound;
2904 return Ok;
2907 struct image_format_dimension
2909 const GUID *format;
2910 const GUID *dimension;
2913 static const struct image_format_dimension image_format_dimensions[] =
2915 {&ImageFormatGIF, &FrameDimensionTime},
2916 {&ImageFormatIcon, &FrameDimensionResolution},
2917 {NULL}
2920 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
2921 GDIPCONST GUID* dimensionID, UINT* count)
2923 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
2925 if(!image || !count)
2926 return InvalidParameter;
2928 if (!dimensionID ||
2929 IsEqualGUID(dimensionID, &image->format) ||
2930 IsEqualGUID(dimensionID, &FrameDimensionPage) ||
2931 IsEqualGUID(dimensionID, &FrameDimensionTime))
2933 *count = image->frame_count;
2934 return Ok;
2937 return InvalidParameter;
2940 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
2941 UINT* count)
2943 TRACE("(%p, %p)\n", image, count);
2945 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2947 if(!image || !count)
2948 return InvalidParameter;
2950 *count = 1;
2952 return Ok;
2955 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
2956 GUID* dimensionIDs, UINT count)
2958 int i;
2959 const GUID *result=NULL;
2961 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
2963 if(!image || !dimensionIDs || count != 1)
2964 return InvalidParameter;
2966 for (i=0; image_format_dimensions[i].format; i++)
2968 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
2970 result = image_format_dimensions[i].dimension;
2971 break;
2975 if (!result)
2976 result = &FrameDimensionPage;
2978 memcpy(dimensionIDs, result, sizeof(GUID));
2980 return Ok;
2983 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
2984 GpImage **image)
2986 GpStatus stat;
2987 IStream *stream;
2989 TRACE("(%s) %p\n", debugstr_w(filename), image);
2991 if (!filename || !image)
2992 return InvalidParameter;
2994 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
2996 if (stat != Ok)
2997 return stat;
2999 stat = GdipLoadImageFromStream(stream, image);
3001 IStream_Release(stream);
3003 return stat;
3006 /* FIXME: no icm handling */
3007 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
3009 TRACE("(%s) %p\n", debugstr_w(filename), image);
3011 return GdipLoadImageFromFile(filename, image);
3014 static GpStatus decode_image_wic(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3016 GpStatus status=Ok;
3017 GpBitmap *bitmap;
3018 HRESULT hr;
3019 IWICBitmapDecoder *decoder;
3020 IWICBitmapFrameDecode *frame;
3021 IWICBitmapSource *source=NULL;
3022 IWICMetadataBlockReader *block_reader;
3023 WICPixelFormatGUID wic_format;
3024 PixelFormat gdip_format=0;
3025 ColorPalette *palette = NULL;
3026 WICBitmapPaletteType palette_type = WICBitmapPaletteTypeFixedHalftone256;
3027 int i;
3028 UINT width, height, frame_count;
3029 BitmapData lockeddata;
3030 WICRect wrc;
3031 HRESULT initresult;
3033 initresult = CoInitialize(NULL);
3035 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
3036 &IID_IWICBitmapDecoder, (void**)&decoder);
3037 if (FAILED(hr)) goto end;
3039 hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
3040 if (SUCCEEDED(hr))
3042 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3043 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3046 if (SUCCEEDED(hr)) /* got frame */
3048 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
3050 if (SUCCEEDED(hr))
3052 IWICBitmapSource *bmp_source;
3053 IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICBitmapSource, (void **)&bmp_source);
3055 for (i=0; pixel_formats[i].wic_format; i++)
3057 if (IsEqualGUID(&wic_format, pixel_formats[i].wic_format))
3059 source = bmp_source;
3060 gdip_format = pixel_formats[i].gdip_format;
3061 palette_type = pixel_formats[i].palette_type;
3062 break;
3065 if (!source)
3067 /* unknown format; fall back on 32bppARGB */
3068 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, bmp_source, &source);
3069 gdip_format = PixelFormat32bppARGB;
3070 IWICBitmapSource_Release(bmp_source);
3072 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format), gdip_format);
3075 if (SUCCEEDED(hr)) /* got source */
3077 hr = IWICBitmapSource_GetSize(source, &width, &height);
3079 if (SUCCEEDED(hr))
3080 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
3081 NULL, &bitmap);
3083 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
3085 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
3086 gdip_format, &lockeddata);
3087 if (status == Ok) /* locked bitmap */
3089 wrc.X = 0;
3090 wrc.Width = width;
3091 wrc.Height = 1;
3092 for (i=0; i<height; i++)
3094 wrc.Y = i;
3095 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
3096 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
3097 if (FAILED(hr)) break;
3100 GdipBitmapUnlockBits(bitmap, &lockeddata);
3103 if (SUCCEEDED(hr) && status == Ok)
3104 *image = (GpImage*)bitmap;
3105 else
3107 *image = NULL;
3108 GdipDisposeImage((GpImage*)bitmap);
3111 if (SUCCEEDED(hr) && status == Ok)
3113 double dpix, dpiy;
3114 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
3115 if (SUCCEEDED(hr))
3117 bitmap->image.xres = dpix;
3118 bitmap->image.yres = dpiy;
3120 hr = S_OK;
3124 IWICBitmapSource_Release(source);
3127 if (SUCCEEDED(hr)) {
3128 bitmap->metadata_reader = NULL;
3129 if (IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader) == S_OK)
3131 UINT block_count = 0;
3132 if (IWICMetadataBlockReader_GetCount(block_reader, &block_count) == S_OK && block_count)
3133 IWICMetadataBlockReader_GetReaderByIndex(block_reader, 0, &bitmap->metadata_reader);
3134 IWICMetadataBlockReader_Release(block_reader);
3137 palette = get_palette(frame, palette_type);
3138 IWICBitmapFrameDecode_Release(frame);
3142 IWICBitmapDecoder_Release(decoder);
3144 end:
3145 if (SUCCEEDED(initresult)) CoUninitialize();
3147 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
3149 if (status == Ok)
3151 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3152 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI|ImageFlagsColorSpaceRGB;
3153 bitmap->image.frame_count = frame_count;
3154 bitmap->image.current_frame = active_frame;
3155 bitmap->image.stream = stream;
3156 if (palette)
3158 GdipFree(bitmap->image.palette);
3159 bitmap->image.palette = palette;
3161 else
3163 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormatBlackWhite))
3164 bitmap->image.palette->Flags = 0;
3166 /* Pin the source stream */
3167 IStream_AddRef(stream);
3170 return status;
3173 static GpStatus decode_image_icon(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3175 return decode_image_wic(stream, &CLSID_WICIcoDecoder, active_frame, image);
3178 static GpStatus decode_image_bmp(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3180 GpStatus status;
3181 GpBitmap* bitmap;
3183 status = decode_image_wic(stream, &CLSID_WICBmpDecoder, active_frame, image);
3185 bitmap = (GpBitmap*)*image;
3187 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
3189 /* WIC supports bmp files with alpha, but gdiplus does not */
3190 bitmap->format = PixelFormat32bppRGB;
3193 return status;
3196 static GpStatus decode_image_jpeg(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3198 return decode_image_wic(stream, &CLSID_WICJpegDecoder, active_frame, image);
3201 static GpStatus decode_image_png(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3203 return decode_image_wic(stream, &CLSID_WICPngDecoder, active_frame, image);
3206 static GpStatus decode_image_gif(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3208 return decode_image_wic(stream, &CLSID_WICGifDecoder, active_frame, image);
3211 static GpStatus decode_image_tiff(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3213 return decode_image_wic(stream, &CLSID_WICTiffDecoder, active_frame, image);
3216 static GpStatus decode_image_olepicture_metafile(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3218 IPicture *pic;
3220 TRACE("%p %p\n", stream, image);
3222 if(!stream || !image)
3223 return InvalidParameter;
3225 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
3226 (LPVOID*) &pic) != S_OK){
3227 TRACE("Could not load picture\n");
3228 return GenericError;
3231 /* FIXME: missing initialization code */
3232 *image = GdipAlloc(sizeof(GpMetafile));
3233 if(!*image) return OutOfMemory;
3234 (*image)->type = ImageTypeMetafile;
3235 (*image)->stream = NULL;
3236 (*image)->picture = pic;
3237 (*image)->flags = ImageFlagsNone;
3238 (*image)->frame_count = 1;
3239 (*image)->current_frame = 0;
3240 (*image)->palette = NULL;
3242 TRACE("<-- %p\n", *image);
3244 return Ok;
3247 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
3248 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
3250 typedef GpStatus (*decode_image_func)(IStream *stream, REFCLSID clsid, UINT active_frame, GpImage **image);
3252 typedef struct image_codec {
3253 ImageCodecInfo info;
3254 encode_image_func encode_func;
3255 decode_image_func decode_func;
3256 } image_codec;
3258 typedef enum {
3259 BMP,
3260 JPEG,
3261 GIF,
3262 TIFF,
3263 EMF,
3264 WMF,
3265 PNG,
3266 ICO,
3267 NUM_CODECS
3268 } ImageFormat;
3270 static const struct image_codec codecs[NUM_CODECS];
3272 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
3274 BYTE signature[8];
3275 const BYTE *pattern, *mask;
3276 LARGE_INTEGER seek;
3277 HRESULT hr;
3278 UINT bytesread;
3279 int i, j, sig;
3281 /* seek to the start of the stream */
3282 seek.QuadPart = 0;
3283 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3284 if (FAILED(hr)) return hresult_to_status(hr);
3286 /* read the first 8 bytes */
3287 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
3288 hr = IStream_Read(stream, signature, 8, &bytesread);
3289 if (FAILED(hr)) return hresult_to_status(hr);
3290 if (hr == S_FALSE || bytesread == 0) return GenericError;
3292 for (i = 0; i < NUM_CODECS; i++) {
3293 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
3294 bytesread >= codecs[i].info.SigSize)
3296 for (sig=0; sig<codecs[i].info.SigCount; sig++)
3298 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
3299 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
3300 for (j=0; j<codecs[i].info.SigSize; j++)
3301 if ((signature[j] & mask[j]) != pattern[j])
3302 break;
3303 if (j == codecs[i].info.SigSize)
3305 *result = &codecs[i];
3306 return Ok;
3312 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
3313 signature[0],signature[1],signature[2],signature[3],
3314 signature[4],signature[5],signature[6],signature[7]);
3316 return GenericError;
3319 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image, GDIPCONST GUID *dimensionID,
3320 UINT frame)
3322 GpStatus stat;
3323 LARGE_INTEGER seek;
3324 HRESULT hr;
3325 const struct image_codec *codec = NULL;
3326 IStream *stream;
3327 GpImage *new_image;
3329 TRACE("(%p,%s,%u)\n", image, debugstr_guid(dimensionID), frame);
3331 if (!image || !dimensionID)
3332 return InvalidParameter;
3334 if (frame >= image->frame_count)
3335 return InvalidParameter;
3337 if (image->type != ImageTypeBitmap && image->type != ImageTypeMetafile)
3339 WARN("invalid image type %d\n", image->type);
3340 return InvalidParameter;
3343 if (image->current_frame == frame)
3344 return Ok;
3346 if (!image->stream)
3348 TRACE("image doesn't have an associated stream\n");
3349 return Ok;
3352 hr = IStream_Clone(image->stream, &stream);
3353 if (FAILED(hr))
3355 STATSTG statstg;
3357 hr = IStream_Stat(image->stream, &statstg, STATFLAG_NOOPEN);
3358 if (FAILED(hr)) return hresult_to_status(hr);
3360 stat = GdipCreateStreamOnFile(statstg.pwcsName, GENERIC_READ, &stream);
3361 if (stat != Ok) return stat;
3364 /* choose an appropriate image decoder */
3365 stat = get_decoder_info(stream, &codec);
3366 if (stat != Ok)
3368 IStream_Release(stream);
3369 return stat;
3372 /* seek to the start of the stream */
3373 seek.QuadPart = 0;
3374 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3375 if (FAILED(hr))
3377 IStream_Release(stream);
3378 return hresult_to_status(hr);
3381 /* call on the image decoder to do the real work */
3382 stat = codec->decode_func(stream, &codec->info.Clsid, frame, &new_image);
3384 if (stat == Ok)
3386 memcpy(&new_image->format, &codec->info.FormatID, sizeof(GUID));
3387 free_image_data(image);
3388 if (image->type == ImageTypeBitmap)
3389 *(GpBitmap *)image = *(GpBitmap *)new_image;
3390 else if (image->type == ImageTypeMetafile)
3391 *(GpMetafile *)image = *(GpMetafile *)new_image;
3392 new_image->type = ~0;
3393 GdipFree(new_image);
3394 return Ok;
3397 IStream_Release(stream);
3398 return stat;
3401 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream *stream, GpImage **image)
3403 GpStatus stat;
3404 LARGE_INTEGER seek;
3405 HRESULT hr;
3406 const struct image_codec *codec=NULL;
3408 /* choose an appropriate image decoder */
3409 stat = get_decoder_info(stream, &codec);
3410 if (stat != Ok) return stat;
3412 /* seek to the start of the stream */
3413 seek.QuadPart = 0;
3414 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3415 if (FAILED(hr)) return hresult_to_status(hr);
3417 /* call on the image decoder to do the real work */
3418 stat = codec->decode_func(stream, &codec->info.Clsid, 0, image);
3420 /* take note of the original data format */
3421 if (stat == Ok)
3423 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
3424 return Ok;
3427 return stat;
3430 /* FIXME: no ICM */
3431 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
3433 TRACE("%p %p\n", stream, image);
3435 return GdipLoadImageFromStream(stream, image);
3438 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
3440 static int calls;
3442 TRACE("(%p,%u)\n", image, propId);
3444 if(!image)
3445 return InvalidParameter;
3447 if(!(calls++))
3448 FIXME("not implemented\n");
3450 return NotImplemented;
3453 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
3455 static int calls;
3457 if (!image || !item) return InvalidParameter;
3459 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image, item, item->id, item->type, item->length, item->value);
3461 if(!(calls++))
3462 FIXME("not implemented\n");
3464 return Ok;
3467 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
3468 GDIPCONST CLSID *clsidEncoder,
3469 GDIPCONST EncoderParameters *encoderParams)
3471 GpStatus stat;
3472 IStream *stream;
3474 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
3476 if (!image || !filename|| !clsidEncoder)
3477 return InvalidParameter;
3479 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
3480 if (stat != Ok)
3481 return GenericError;
3483 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
3485 IStream_Release(stream);
3486 return stat;
3489 /*************************************************************************
3490 * Encoding functions -
3491 * These functions encode an image in different image file formats.
3493 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
3494 #define BITMAP_FORMAT_JPEG 0xd8ff
3495 #define BITMAP_FORMAT_GIF 0x4947
3496 #define BITMAP_FORMAT_PNG 0x5089
3497 #define BITMAP_FORMAT_APM 0xcdd7
3499 static GpStatus encode_image_WIC(GpImage *image, IStream* stream,
3500 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3502 GpStatus stat;
3503 GpBitmap *bitmap;
3504 IWICBitmapEncoder *encoder;
3505 IWICBitmapFrameEncode *frameencode;
3506 IPropertyBag2 *encoderoptions;
3507 HRESULT hr;
3508 UINT width, height;
3509 PixelFormat gdipformat=0;
3510 WICPixelFormatGUID wicformat;
3511 GpRect rc;
3512 BitmapData lockeddata;
3513 HRESULT initresult;
3514 UINT i;
3516 if (image->type != ImageTypeBitmap)
3517 return GenericError;
3519 bitmap = (GpBitmap*)image;
3521 GdipGetImageWidth(image, &width);
3522 GdipGetImageHeight(image, &height);
3524 rc.X = 0;
3525 rc.Y = 0;
3526 rc.Width = width;
3527 rc.Height = height;
3529 initresult = CoInitialize(NULL);
3531 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
3532 &IID_IWICBitmapEncoder, (void**)&encoder);
3533 if (FAILED(hr))
3535 if (SUCCEEDED(initresult)) CoUninitialize();
3536 return hresult_to_status(hr);
3539 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
3541 if (SUCCEEDED(hr))
3543 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
3546 if (SUCCEEDED(hr)) /* created frame */
3548 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
3550 if (SUCCEEDED(hr))
3551 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
3553 if (SUCCEEDED(hr))
3554 hr = IWICBitmapFrameEncode_SetResolution(frameencode, image->xres, image->yres);
3556 if (SUCCEEDED(hr))
3558 for (i=0; pixel_formats[i].wic_format; i++)
3560 if (pixel_formats[i].gdip_format == bitmap->format)
3562 memcpy(&wicformat, pixel_formats[i].wic_format, sizeof(GUID));
3563 gdipformat = bitmap->format;
3564 break;
3567 if (!gdipformat)
3569 memcpy(&wicformat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
3570 gdipformat = PixelFormat32bppARGB;
3573 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
3576 if (SUCCEEDED(hr))
3578 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
3579 &lockeddata);
3581 if (stat == Ok)
3583 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
3584 BYTE *row;
3586 /* write one row at a time in case stride is negative */
3587 row = lockeddata.Scan0;
3588 for (i=0; i<lockeddata.Height; i++)
3590 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
3591 if (FAILED(hr)) break;
3592 row += lockeddata.Stride;
3595 GdipBitmapUnlockBits(bitmap, &lockeddata);
3597 else
3598 hr = E_FAIL;
3601 if (SUCCEEDED(hr))
3602 hr = IWICBitmapFrameEncode_Commit(frameencode);
3604 IWICBitmapFrameEncode_Release(frameencode);
3605 IPropertyBag2_Release(encoderoptions);
3608 if (SUCCEEDED(hr))
3609 hr = IWICBitmapEncoder_Commit(encoder);
3611 IWICBitmapEncoder_Release(encoder);
3613 if (SUCCEEDED(initresult)) CoUninitialize();
3615 return hresult_to_status(hr);
3618 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
3619 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3621 return encode_image_WIC(image, stream, &CLSID_WICBmpEncoder, params);
3624 static GpStatus encode_image_tiff(GpImage *image, IStream* stream,
3625 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3627 return encode_image_WIC(image, stream, &CLSID_WICTiffEncoder, params);
3630 static GpStatus encode_image_png(GpImage *image, IStream* stream,
3631 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3633 return encode_image_WIC(image, stream, &CLSID_WICPngEncoder, params);
3636 static GpStatus encode_image_jpeg(GpImage *image, IStream* stream,
3637 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3639 return encode_image_WIC(image, stream, &CLSID_WICJpegEncoder, params);
3642 /*****************************************************************************
3643 * GdipSaveImageToStream [GDIPLUS.@]
3645 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
3646 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3648 GpStatus stat;
3649 encode_image_func encode_image;
3650 int i;
3652 TRACE("%p %p %p %p\n", image, stream, clsid, params);
3654 if(!image || !stream)
3655 return InvalidParameter;
3657 /* select correct encoder */
3658 encode_image = NULL;
3659 for (i = 0; i < NUM_CODECS; i++) {
3660 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
3661 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
3662 encode_image = codecs[i].encode_func;
3664 if (encode_image == NULL)
3665 return UnknownImageFormat;
3667 stat = encode_image(image, stream, clsid, params);
3669 return stat;
3672 /*****************************************************************************
3673 * GdipSaveAdd [GDIPLUS.@]
3675 GpStatus WINGDIPAPI GdipSaveAdd(GpImage *image, GDIPCONST EncoderParameters *params)
3677 FIXME("(%p,%p): stub\n", image, params);
3678 return Ok;
3681 /*****************************************************************************
3682 * GdipGetImagePalette [GDIPLUS.@]
3684 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
3686 INT count;
3688 TRACE("(%p,%p,%i)\n", image, palette, size);
3690 if (!image || !palette)
3691 return InvalidParameter;
3693 count = image->palette ? image->palette->Count : 0;
3695 if (size < (sizeof(UINT)*2+sizeof(ARGB)*count))
3697 TRACE("<-- InsufficientBuffer\n");
3698 return InsufficientBuffer;
3701 if (image->palette)
3703 palette->Flags = image->palette->Flags;
3704 palette->Count = image->palette->Count;
3705 memcpy(palette->Entries, image->palette->Entries, sizeof(ARGB)*image->palette->Count);
3707 else
3709 palette->Flags = 0;
3710 palette->Count = 0;
3712 return Ok;
3715 /*****************************************************************************
3716 * GdipSetImagePalette [GDIPLUS.@]
3718 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
3719 GDIPCONST ColorPalette *palette)
3721 ColorPalette *new_palette;
3723 TRACE("(%p,%p)\n", image, palette);
3725 if(!image || !palette || palette->Count > 256)
3726 return InvalidParameter;
3728 new_palette = GdipAlloc(2 * sizeof(UINT) + palette->Count * sizeof(ARGB));
3729 if (!new_palette) return OutOfMemory;
3731 GdipFree(image->palette);
3732 image->palette = new_palette;
3733 image->palette->Flags = palette->Flags;
3734 image->palette->Count = palette->Count;
3735 memcpy(image->palette->Entries, palette->Entries, sizeof(ARGB)*palette->Count);
3737 return Ok;
3740 /*************************************************************************
3741 * Encoders -
3742 * Structures that represent which formats we support for encoding.
3745 /* ImageCodecInfo creation routines taken from libgdiplus */
3746 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
3747 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
3748 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
3749 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
3750 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
3751 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
3753 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
3754 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
3755 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
3756 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
3757 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
3758 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
3760 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
3761 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
3762 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
3763 static const WCHAR gif_format[] = {'G','I','F',0};
3764 static const BYTE gif_sig_pattern[12] = "GIF87aGIF89a";
3765 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3767 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
3768 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
3769 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
3770 static const WCHAR tiff_format[] = {'T','I','F','F',0};
3771 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
3772 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3774 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
3775 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
3776 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
3777 static const WCHAR emf_format[] = {'E','M','F',0};
3778 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
3779 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3781 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
3782 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
3783 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
3784 static const WCHAR wmf_format[] = {'W','M','F',0};
3785 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
3786 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
3788 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
3789 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
3790 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
3791 static const WCHAR png_format[] = {'P','N','G',0};
3792 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
3793 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3795 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
3796 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
3797 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
3798 static const WCHAR ico_format[] = {'I','C','O',0};
3799 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
3800 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3802 static const struct image_codec codecs[NUM_CODECS] = {
3804 { /* BMP */
3805 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3806 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3807 /* CodecName */ bmp_codecname,
3808 /* DllName */ NULL,
3809 /* FormatDescription */ bmp_format,
3810 /* FilenameExtension */ bmp_extension,
3811 /* MimeType */ bmp_mimetype,
3812 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3813 /* Version */ 1,
3814 /* SigCount */ 1,
3815 /* SigSize */ 2,
3816 /* SigPattern */ bmp_sig_pattern,
3817 /* SigMask */ bmp_sig_mask,
3819 encode_image_BMP,
3820 decode_image_bmp
3823 { /* JPEG */
3824 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3825 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3826 /* CodecName */ jpeg_codecname,
3827 /* DllName */ NULL,
3828 /* FormatDescription */ jpeg_format,
3829 /* FilenameExtension */ jpeg_extension,
3830 /* MimeType */ jpeg_mimetype,
3831 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3832 /* Version */ 1,
3833 /* SigCount */ 1,
3834 /* SigSize */ 2,
3835 /* SigPattern */ jpeg_sig_pattern,
3836 /* SigMask */ jpeg_sig_mask,
3838 encode_image_jpeg,
3839 decode_image_jpeg
3842 { /* GIF */
3843 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3844 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3845 /* CodecName */ gif_codecname,
3846 /* DllName */ NULL,
3847 /* FormatDescription */ gif_format,
3848 /* FilenameExtension */ gif_extension,
3849 /* MimeType */ gif_mimetype,
3850 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3851 /* Version */ 1,
3852 /* SigCount */ 2,
3853 /* SigSize */ 6,
3854 /* SigPattern */ gif_sig_pattern,
3855 /* SigMask */ gif_sig_mask,
3857 NULL,
3858 decode_image_gif
3861 { /* TIFF */
3862 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3863 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3864 /* CodecName */ tiff_codecname,
3865 /* DllName */ NULL,
3866 /* FormatDescription */ tiff_format,
3867 /* FilenameExtension */ tiff_extension,
3868 /* MimeType */ tiff_mimetype,
3869 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3870 /* Version */ 1,
3871 /* SigCount */ 2,
3872 /* SigSize */ 4,
3873 /* SigPattern */ tiff_sig_pattern,
3874 /* SigMask */ tiff_sig_mask,
3876 encode_image_tiff,
3877 decode_image_tiff
3880 { /* EMF */
3881 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3882 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3883 /* CodecName */ emf_codecname,
3884 /* DllName */ NULL,
3885 /* FormatDescription */ emf_format,
3886 /* FilenameExtension */ emf_extension,
3887 /* MimeType */ emf_mimetype,
3888 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
3889 /* Version */ 1,
3890 /* SigCount */ 1,
3891 /* SigSize */ 4,
3892 /* SigPattern */ emf_sig_pattern,
3893 /* SigMask */ emf_sig_mask,
3895 NULL,
3896 decode_image_olepicture_metafile
3899 { /* WMF */
3900 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3901 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3902 /* CodecName */ wmf_codecname,
3903 /* DllName */ NULL,
3904 /* FormatDescription */ wmf_format,
3905 /* FilenameExtension */ wmf_extension,
3906 /* MimeType */ wmf_mimetype,
3907 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
3908 /* Version */ 1,
3909 /* SigCount */ 1,
3910 /* SigSize */ 2,
3911 /* SigPattern */ wmf_sig_pattern,
3912 /* SigMask */ wmf_sig_mask,
3914 NULL,
3915 decode_image_olepicture_metafile
3918 { /* PNG */
3919 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3920 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3921 /* CodecName */ png_codecname,
3922 /* DllName */ NULL,
3923 /* FormatDescription */ png_format,
3924 /* FilenameExtension */ png_extension,
3925 /* MimeType */ png_mimetype,
3926 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3927 /* Version */ 1,
3928 /* SigCount */ 1,
3929 /* SigSize */ 8,
3930 /* SigPattern */ png_sig_pattern,
3931 /* SigMask */ png_sig_mask,
3933 encode_image_png,
3934 decode_image_png
3937 { /* ICO */
3938 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3939 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3940 /* CodecName */ ico_codecname,
3941 /* DllName */ NULL,
3942 /* FormatDescription */ ico_format,
3943 /* FilenameExtension */ ico_extension,
3944 /* MimeType */ ico_mimetype,
3945 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3946 /* Version */ 1,
3947 /* SigCount */ 1,
3948 /* SigSize */ 4,
3949 /* SigPattern */ ico_sig_pattern,
3950 /* SigMask */ ico_sig_mask,
3952 NULL,
3953 decode_image_icon
3957 /*****************************************************************************
3958 * GdipGetImageDecodersSize [GDIPLUS.@]
3960 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
3962 int decoder_count=0;
3963 int i;
3964 TRACE("%p %p\n", numDecoders, size);
3966 if (!numDecoders || !size)
3967 return InvalidParameter;
3969 for (i=0; i<NUM_CODECS; i++)
3971 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
3972 decoder_count++;
3975 *numDecoders = decoder_count;
3976 *size = decoder_count * sizeof(ImageCodecInfo);
3978 return Ok;
3981 /*****************************************************************************
3982 * GdipGetImageDecoders [GDIPLUS.@]
3984 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
3986 int i, decoder_count=0;
3987 TRACE("%u %u %p\n", numDecoders, size, decoders);
3989 if (!decoders ||
3990 size != numDecoders * sizeof(ImageCodecInfo))
3991 return GenericError;
3993 for (i=0; i<NUM_CODECS; i++)
3995 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
3997 if (decoder_count == numDecoders) return GenericError;
3998 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
3999 decoder_count++;
4003 if (decoder_count < numDecoders) return GenericError;
4005 return Ok;
4008 /*****************************************************************************
4009 * GdipGetImageEncodersSize [GDIPLUS.@]
4011 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
4013 int encoder_count=0;
4014 int i;
4015 TRACE("%p %p\n", numEncoders, size);
4017 if (!numEncoders || !size)
4018 return InvalidParameter;
4020 for (i=0; i<NUM_CODECS; i++)
4022 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4023 encoder_count++;
4026 *numEncoders = encoder_count;
4027 *size = encoder_count * sizeof(ImageCodecInfo);
4029 return Ok;
4032 /*****************************************************************************
4033 * GdipGetImageEncoders [GDIPLUS.@]
4035 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
4037 int i, encoder_count=0;
4038 TRACE("%u %u %p\n", numEncoders, size, encoders);
4040 if (!encoders ||
4041 size != numEncoders * sizeof(ImageCodecInfo))
4042 return GenericError;
4044 for (i=0; i<NUM_CODECS; i++)
4046 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4048 if (encoder_count == numEncoders) return GenericError;
4049 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4050 encoder_count++;
4054 if (encoder_count < numEncoders) return GenericError;
4056 return Ok;
4059 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
4060 GDIPCONST CLSID* clsidEncoder, UINT *size)
4062 static int calls;
4064 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
4066 if(!(calls++))
4067 FIXME("not implemented\n");
4069 *size = 0;
4071 return NotImplemented;
4074 static PixelFormat get_16bpp_format(HBITMAP hbm)
4076 BITMAPV4HEADER bmh;
4077 HDC hdc;
4078 PixelFormat result;
4080 hdc = CreateCompatibleDC(NULL);
4082 memset(&bmh, 0, sizeof(bmh));
4083 bmh.bV4Size = sizeof(bmh);
4084 bmh.bV4Width = 1;
4085 bmh.bV4Height = 1;
4086 bmh.bV4V4Compression = BI_BITFIELDS;
4087 bmh.bV4BitCount = 16;
4089 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO*)&bmh, DIB_RGB_COLORS);
4091 if (bmh.bV4RedMask == 0x7c00 &&
4092 bmh.bV4GreenMask == 0x3e0 &&
4093 bmh.bV4BlueMask == 0x1f)
4095 result = PixelFormat16bppRGB555;
4097 else if (bmh.bV4RedMask == 0xf800 &&
4098 bmh.bV4GreenMask == 0x7e0 &&
4099 bmh.bV4BlueMask == 0x1f)
4101 result = PixelFormat16bppRGB565;
4103 else
4105 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
4106 bmh.bV4GreenMask, bmh.bV4BlueMask);
4107 result = PixelFormatUndefined;
4110 DeleteDC(hdc);
4112 return result;
4115 /*****************************************************************************
4116 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
4118 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
4120 BITMAP bm;
4121 GpStatus retval;
4122 PixelFormat format;
4123 BitmapData lockeddata;
4124 INT y;
4126 TRACE("%p %p %p\n", hbm, hpal, bitmap);
4128 if(!hbm || !bitmap)
4129 return InvalidParameter;
4131 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
4132 return InvalidParameter;
4134 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
4135 switch(bm.bmBitsPixel) {
4136 case 1:
4137 format = PixelFormat1bppIndexed;
4138 break;
4139 case 4:
4140 format = PixelFormat4bppIndexed;
4141 break;
4142 case 8:
4143 format = PixelFormat8bppIndexed;
4144 break;
4145 case 16:
4146 format = get_16bpp_format(hbm);
4147 if (format == PixelFormatUndefined)
4148 return InvalidParameter;
4149 break;
4150 case 24:
4151 format = PixelFormat24bppRGB;
4152 break;
4153 case 32:
4154 format = PixelFormat32bppRGB;
4155 break;
4156 case 48:
4157 format = PixelFormat48bppRGB;
4158 break;
4159 default:
4160 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
4161 return InvalidParameter;
4164 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
4165 format, NULL, bitmap);
4167 if (retval == Ok)
4169 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
4170 format, &lockeddata);
4171 if (retval == Ok)
4173 if (bm.bmBits)
4175 for (y=0; y<bm.bmHeight; y++)
4177 memcpy((BYTE*)lockeddata.Scan0+lockeddata.Stride*y,
4178 (BYTE*)bm.bmBits+bm.bmWidthBytes*(bm.bmHeight-1-y),
4179 bm.bmWidthBytes);
4182 else
4184 HDC hdc;
4185 HBITMAP oldhbm;
4186 BITMAPINFO *pbmi;
4187 INT src_height, dst_stride;
4188 BYTE *dst_bits;
4190 hdc = CreateCompatibleDC(NULL);
4191 oldhbm = SelectObject(hdc, hbm);
4193 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
4195 if (pbmi)
4197 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
4198 pbmi->bmiHeader.biBitCount = 0;
4200 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
4202 src_height = abs(pbmi->bmiHeader.biHeight);
4204 if (pbmi->bmiHeader.biHeight > 0)
4206 dst_bits = (BYTE*)lockeddata.Scan0+lockeddata.Stride*(src_height-1);
4207 dst_stride = -lockeddata.Stride;
4209 else
4211 dst_bits = lockeddata.Scan0;
4212 dst_stride = lockeddata.Stride;
4215 for (y=0; y<src_height; y++)
4217 GetDIBits(hdc, hbm, y, 1, dst_bits+dst_stride*y,
4218 pbmi, DIB_RGB_COLORS);
4221 GdipFree(pbmi);
4223 else
4224 retval = OutOfMemory;
4226 SelectObject(hdc, oldhbm);
4227 DeleteDC(hdc);
4230 GdipBitmapUnlockBits(*bitmap, &lockeddata);
4233 if (retval == Ok && hpal)
4235 WORD num_palette_entries;
4236 PALETTEENTRY *palette_entries=NULL;
4237 ColorPalette *palette=NULL;
4238 int i;
4240 if (!GetObjectW(hpal, sizeof(num_palette_entries), &num_palette_entries))
4241 retval = GenericError;
4243 if (retval == Ok)
4245 palette_entries = GdipAlloc(sizeof(PALETTEENTRY) * num_palette_entries);
4246 palette = GdipAlloc(sizeof(ColorPalette) + sizeof(ARGB) * (num_palette_entries-1));
4248 if (!palette_entries || !palette)
4249 retval = OutOfMemory;
4252 if (retval == Ok)
4254 if (!GetPaletteEntries(hpal, 0, num_palette_entries, palette_entries))
4255 retval = GenericError;
4258 if (retval == Ok)
4260 palette->Flags = 0;
4261 palette->Count = num_palette_entries;
4263 for (i=0; i<num_palette_entries; i++)
4265 PALETTEENTRY * entry = &palette_entries[i];
4266 palette->Entries[i] = 0xff000000 | entry->peRed << 16 |
4267 entry->peGreen << 8 | entry->peBlue;
4270 retval = GdipSetImagePalette((GpImage*)*bitmap, palette);
4273 GdipFree(palette_entries);
4274 GdipFree(palette);
4277 if (retval != Ok)
4279 GdipDisposeImage((GpImage*)*bitmap);
4280 *bitmap = NULL;
4284 return retval;
4287 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
4289 FIXME("(%p): stub\n", effect);
4290 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
4291 * in Windows's gdiplus */
4292 return NotImplemented;
4295 /*****************************************************************************
4296 * GdipSetEffectParameters [GDIPLUS.@]
4298 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
4299 const VOID *params, const UINT size)
4301 static int calls;
4303 TRACE("(%p,%p,%u)\n", effect, params, size);
4305 if(!(calls++))
4306 FIXME("not implemented\n");
4308 return NotImplemented;
4311 /*****************************************************************************
4312 * GdipGetImageFlags [GDIPLUS.@]
4314 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
4316 TRACE("%p %p\n", image, flags);
4318 if(!image || !flags)
4319 return InvalidParameter;
4321 *flags = image->flags;
4323 return Ok;
4326 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
4328 TRACE("(%d, %p)\n", control, param);
4330 switch(control){
4331 case TestControlForceBilinear:
4332 if(param)
4333 FIXME("TestControlForceBilinear not handled\n");
4334 break;
4335 case TestControlNoICM:
4336 if(param)
4337 FIXME("TestControlNoICM not handled\n");
4338 break;
4339 case TestControlGetBuildNumber:
4340 *((DWORD*)param) = 3102;
4341 break;
4344 return Ok;
4347 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
4348 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
4349 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
4350 GpMetafile **metafile)
4352 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
4353 frameUnit, debugstr_w(desc), metafile);
4355 return NotImplemented;
4358 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
4359 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
4360 GDIPCONST WCHAR *desc, GpMetafile **metafile)
4362 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
4363 frameUnit, debugstr_w(desc), metafile);
4365 return NotImplemented;
4368 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
4370 TRACE("%p\n", image);
4372 return Ok;
4375 /*****************************************************************************
4376 * GdipGetImageThumbnail [GDIPLUS.@]
4378 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
4379 GpImage **ret_image, GetThumbnailImageAbort cb,
4380 VOID * cb_data)
4382 GpStatus stat;
4383 GpGraphics *graphics;
4384 UINT srcwidth, srcheight;
4386 TRACE("(%p %u %u %p %p %p)\n",
4387 image, width, height, ret_image, cb, cb_data);
4389 if (!image || !ret_image)
4390 return InvalidParameter;
4392 if (!width) width = 120;
4393 if (!height) height = 120;
4395 GdipGetImageWidth(image, &srcwidth);
4396 GdipGetImageHeight(image, &srcheight);
4398 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
4399 NULL, (GpBitmap**)ret_image);
4401 if (stat == Ok)
4403 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
4405 if (stat == Ok)
4407 stat = GdipDrawImageRectRectI(graphics, image,
4408 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
4409 NULL, NULL, NULL);
4411 GdipDeleteGraphics(graphics);
4414 if (stat != Ok)
4416 GdipDisposeImage(*ret_image);
4417 *ret_image = NULL;
4421 return stat;
4424 /*****************************************************************************
4425 * GdipImageRotateFlip [GDIPLUS.@]
4427 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
4429 GpBitmap *new_bitmap;
4430 GpBitmap *bitmap;
4431 int bpp, bytesperpixel;
4432 int rotate_90, flip_x, flip_y;
4433 int src_x_offset, src_y_offset;
4434 LPBYTE src_origin;
4435 UINT x, y, width, height;
4436 BitmapData src_lock, dst_lock;
4437 GpStatus stat;
4439 TRACE("(%p, %u)\n", image, type);
4441 if (!image)
4442 return InvalidParameter;
4444 rotate_90 = type&1;
4445 flip_x = (type&6) == 2 || (type&6) == 4;
4446 flip_y = (type&3) == 1 || (type&3) == 2;
4448 if (image->type != ImageTypeBitmap)
4450 FIXME("Not implemented for type %i\n", image->type);
4451 return NotImplemented;
4454 bitmap = (GpBitmap*)image;
4455 bpp = PIXELFORMATBPP(bitmap->format);
4457 if (bpp < 8)
4459 FIXME("Not implemented for %i bit images\n", bpp);
4460 return NotImplemented;
4463 if (rotate_90)
4465 width = bitmap->height;
4466 height = bitmap->width;
4468 else
4470 width = bitmap->width;
4471 height = bitmap->height;
4474 bytesperpixel = bpp/8;
4476 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
4478 if (stat != Ok)
4479 return stat;
4481 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
4483 if (stat == Ok)
4485 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
4487 if (stat == Ok)
4489 LPBYTE src_row, src_pixel;
4490 LPBYTE dst_row, dst_pixel;
4492 src_origin = src_lock.Scan0;
4493 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
4494 if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
4496 if (rotate_90)
4498 if (flip_y) src_x_offset = -src_lock.Stride;
4499 else src_x_offset = src_lock.Stride;
4500 if (flip_x) src_y_offset = -bytesperpixel;
4501 else src_y_offset = bytesperpixel;
4503 else
4505 if (flip_x) src_x_offset = -bytesperpixel;
4506 else src_x_offset = bytesperpixel;
4507 if (flip_y) src_y_offset = -src_lock.Stride;
4508 else src_y_offset = src_lock.Stride;
4511 src_row = src_origin;
4512 dst_row = dst_lock.Scan0;
4513 for (y=0; y<height; y++)
4515 src_pixel = src_row;
4516 dst_pixel = dst_row;
4517 for (x=0; x<width; x++)
4519 /* FIXME: This could probably be faster without memcpy. */
4520 memcpy(dst_pixel, src_pixel, bytesperpixel);
4521 dst_pixel += bytesperpixel;
4522 src_pixel += src_x_offset;
4524 src_row += src_y_offset;
4525 dst_row += dst_lock.Stride;
4528 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
4531 GdipBitmapUnlockBits(bitmap, &src_lock);
4534 if (stat == Ok)
4535 move_bitmap(bitmap, new_bitmap, FALSE);
4536 else
4537 GdipDisposeImage((GpImage*)new_bitmap);
4539 return stat;
4542 /*****************************************************************************
4543 * GdipConvertToEmfPlusToFile [GDIPLUS.@]
4546 GpStatus WINGDIPAPI GdipConvertToEmfPlusToFile(const GpGraphics* refGraphics,
4547 GpMetafile* metafile, BOOL* conversionSuccess,
4548 const WCHAR* filename, EmfType emfType,
4549 const WCHAR* description, GpMetafile** out_metafile)
4551 FIXME("stub: %p, %p, %p, %p, %u, %p, %p\n", refGraphics, metafile, conversionSuccess, filename, emfType, description, out_metafile);
4552 return NotImplemented;