po: Fix a mistake in Dutch translation.
[wine.git] / dlls / gdiplus / image.c
blobe6921f55a0db0e543498cb0198067bf5fe204f30
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
3 * Copyright (C) 2012 Dmitry Timoshkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdarg.h>
21 #include <assert.h>
23 #define NONAMELESSUNION
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "wingdi.h"
30 #define COBJMACROS
31 #include "objbase.h"
32 #include "olectl.h"
33 #include "ole2.h"
35 #include "initguid.h"
36 #include "wincodec.h"
37 #include "gdiplus.h"
38 #include "gdiplus_private.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
43 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
45 static const struct
47 const WICPixelFormatGUID *wic_format;
48 PixelFormat gdip_format;
49 /* predefined palette type to use for pixel format conversions */
50 WICBitmapPaletteType palette_type;
51 } pixel_formats[] =
53 { &GUID_WICPixelFormatBlackWhite, PixelFormat1bppIndexed, WICBitmapPaletteTypeFixedBW },
54 { &GUID_WICPixelFormat1bppIndexed, PixelFormat1bppIndexed, WICBitmapPaletteTypeFixedBW },
55 { &GUID_WICPixelFormat8bppGray, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedGray256 },
56 { &GUID_WICPixelFormat8bppIndexed, PixelFormat8bppIndexed, WICBitmapPaletteTypeFixedHalftone256 },
57 { &GUID_WICPixelFormat16bppBGR555, PixelFormat16bppRGB555, WICBitmapPaletteTypeFixedHalftone256 },
58 { &GUID_WICPixelFormat24bppBGR, PixelFormat24bppRGB, WICBitmapPaletteTypeFixedHalftone256 },
59 { &GUID_WICPixelFormat32bppBGR, PixelFormat32bppRGB, WICBitmapPaletteTypeFixedHalftone256 },
60 { &GUID_WICPixelFormat32bppBGRA, PixelFormat32bppARGB, WICBitmapPaletteTypeFixedHalftone256 },
61 { &GUID_WICPixelFormat32bppPBGRA, PixelFormat32bppPARGB, WICBitmapPaletteTypeFixedHalftone256 },
62 { NULL }
65 static ColorPalette *get_palette(IWICBitmapFrameDecode *frame, WICBitmapPaletteType palette_type)
67 HRESULT hr;
68 IWICImagingFactory *factory;
69 IWICPalette *wic_palette;
70 ColorPalette *palette = NULL;
72 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
73 &IID_IWICImagingFactory, (void **)&factory);
74 if (hr != S_OK) return NULL;
76 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
77 if (hr == S_OK)
79 hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
80 if (frame)
81 hr = IWICBitmapFrameDecode_CopyPalette(frame, wic_palette);
82 if (hr != S_OK)
84 TRACE("using predefined palette %#x\n", palette_type);
85 hr = IWICPalette_InitializePredefined(wic_palette, palette_type, FALSE);
87 if (hr == S_OK)
89 UINT count;
90 BOOL mono, gray;
92 IWICPalette_IsBlackWhite(wic_palette, &mono);
93 IWICPalette_IsGrayscale(wic_palette, &gray);
95 IWICPalette_GetColorCount(wic_palette, &count);
96 palette = HeapAlloc(GetProcessHeap(), 0, 2 * sizeof(UINT) + count * sizeof(ARGB));
97 IWICPalette_GetColors(wic_palette, count, palette->Entries, &palette->Count);
99 if (mono)
100 palette->Flags = 0;
101 else if (gray)
102 palette->Flags = PaletteFlagsGrayScale;
103 else
104 palette->Flags = PaletteFlagsHalftone;
106 IWICPalette_Release(wic_palette);
108 IWICImagingFactory_Release(factory);
109 return palette;
112 static INT ipicture_pixel_height(IPicture *pic)
114 HDC hdcref;
115 OLE_YSIZE_HIMETRIC y;
117 IPicture_get_Height(pic, &y);
119 hdcref = CreateCompatibleDC(0);
120 y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
121 DeleteDC(hdcref);
123 return y;
126 static INT ipicture_pixel_width(IPicture *pic)
128 HDC hdcref;
129 OLE_XSIZE_HIMETRIC x;
131 IPicture_get_Width(pic, &x);
133 hdcref = CreateCompatibleDC(0);
134 x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
135 DeleteDC(hdcref);
137 return x;
140 GpStatus WINGDIPAPI GdipBitmapApplyEffect(GpBitmap* bitmap, CGpEffect* effect,
141 RECT* roi, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
143 FIXME("(%p %p %p %d %p %p): stub\n", bitmap, effect, roi, useAuxData, auxData, auxDataSize);
145 * Note: According to Jose Roca's GDI+ docs, this function is not
146 * implemented in Windows's GDI+.
148 return NotImplemented;
151 GpStatus WINGDIPAPI GdipBitmapCreateApplyEffect(GpBitmap** inputBitmaps,
152 INT numInputs, CGpEffect* effect, RECT* roi, RECT* outputRect,
153 GpBitmap** outputBitmap, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
155 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps, numInputs, effect, roi, outputRect, outputBitmap, useAuxData, auxData, auxDataSize);
157 * Note: According to Jose Roca's GDI+ docs, this function is not
158 * implemented in Windows's GDI+.
160 return NotImplemented;
163 static inline void getpixel_1bppIndexed(BYTE *index, const BYTE *row, UINT x)
165 *index = (row[x/8]>>(7-x%8)) & 1;
168 static inline void getpixel_4bppIndexed(BYTE *index, const BYTE *row, UINT x)
170 if (x & 1)
171 *index = row[x/2]&0xf;
172 else
173 *index = row[x/2]>>4;
176 static inline void getpixel_8bppIndexed(BYTE *index, const BYTE *row, UINT x)
178 *index = row[x];
181 static inline void getpixel_16bppGrayScale(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
182 const BYTE *row, UINT x)
184 *r = *g = *b = row[x*2+1];
185 *a = 255;
188 static inline void getpixel_16bppRGB555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
189 const BYTE *row, UINT x)
191 WORD pixel = *((const WORD*)(row)+x);
192 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
193 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
194 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
195 *a = 255;
198 static inline void getpixel_16bppRGB565(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
199 const BYTE *row, UINT x)
201 WORD pixel = *((const WORD*)(row)+x);
202 *r = (pixel>>8&0xf8)|(pixel>>13&0x7);
203 *g = (pixel>>3&0xfc)|(pixel>>9&0x3);
204 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
205 *a = 255;
208 static inline void getpixel_16bppARGB1555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
209 const BYTE *row, UINT x)
211 WORD pixel = *((const WORD*)(row)+x);
212 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
213 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
214 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
215 if ((pixel&0x8000) == 0x8000)
216 *a = 255;
217 else
218 *a = 0;
221 static inline void getpixel_24bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
222 const BYTE *row, UINT x)
224 *r = row[x*3+2];
225 *g = row[x*3+1];
226 *b = row[x*3];
227 *a = 255;
230 static inline void getpixel_32bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
231 const BYTE *row, UINT x)
233 *r = row[x*4+2];
234 *g = row[x*4+1];
235 *b = row[x*4];
236 *a = 255;
239 static inline void getpixel_32bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
240 const BYTE *row, UINT x)
242 *r = row[x*4+2];
243 *g = row[x*4+1];
244 *b = row[x*4];
245 *a = row[x*4+3];
248 static inline void getpixel_32bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
249 const BYTE *row, UINT x)
251 *a = row[x*4+3];
252 if (*a == 0)
253 *r = *g = *b = 0;
254 else
256 *r = row[x*4+2] * 255 / *a;
257 *g = row[x*4+1] * 255 / *a;
258 *b = row[x*4] * 255 / *a;
262 static inline void getpixel_48bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
263 const BYTE *row, UINT x)
265 *r = row[x*6+5];
266 *g = row[x*6+3];
267 *b = row[x*6+1];
268 *a = 255;
271 static inline void getpixel_64bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
272 const BYTE *row, UINT x)
274 *r = row[x*8+5];
275 *g = row[x*8+3];
276 *b = row[x*8+1];
277 *a = row[x*8+7];
280 static inline void getpixel_64bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
281 const BYTE *row, UINT x)
283 *a = row[x*8+7];
284 if (*a == 0)
285 *r = *g = *b = 0;
286 else
288 *r = row[x*8+5] * 255 / *a;
289 *g = row[x*8+3] * 255 / *a;
290 *b = row[x*8+1] * 255 / *a;
294 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
295 ARGB *color)
297 BYTE r, g, b, a;
298 BYTE index;
299 BYTE *row;
301 if(!bitmap || !color ||
302 x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
303 return InvalidParameter;
305 row = bitmap->bits+bitmap->stride*y;
307 switch (bitmap->format)
309 case PixelFormat1bppIndexed:
310 getpixel_1bppIndexed(&index,row,x);
311 break;
312 case PixelFormat4bppIndexed:
313 getpixel_4bppIndexed(&index,row,x);
314 break;
315 case PixelFormat8bppIndexed:
316 getpixel_8bppIndexed(&index,row,x);
317 break;
318 case PixelFormat16bppGrayScale:
319 getpixel_16bppGrayScale(&r,&g,&b,&a,row,x);
320 break;
321 case PixelFormat16bppRGB555:
322 getpixel_16bppRGB555(&r,&g,&b,&a,row,x);
323 break;
324 case PixelFormat16bppRGB565:
325 getpixel_16bppRGB565(&r,&g,&b,&a,row,x);
326 break;
327 case PixelFormat16bppARGB1555:
328 getpixel_16bppARGB1555(&r,&g,&b,&a,row,x);
329 break;
330 case PixelFormat24bppRGB:
331 getpixel_24bppRGB(&r,&g,&b,&a,row,x);
332 break;
333 case PixelFormat32bppRGB:
334 getpixel_32bppRGB(&r,&g,&b,&a,row,x);
335 break;
336 case PixelFormat32bppARGB:
337 getpixel_32bppARGB(&r,&g,&b,&a,row,x);
338 break;
339 case PixelFormat32bppPARGB:
340 getpixel_32bppPARGB(&r,&g,&b,&a,row,x);
341 break;
342 case PixelFormat48bppRGB:
343 getpixel_48bppRGB(&r,&g,&b,&a,row,x);
344 break;
345 case PixelFormat64bppARGB:
346 getpixel_64bppARGB(&r,&g,&b,&a,row,x);
347 break;
348 case PixelFormat64bppPARGB:
349 getpixel_64bppPARGB(&r,&g,&b,&a,row,x);
350 break;
351 default:
352 FIXME("not implemented for format 0x%x\n", bitmap->format);
353 return NotImplemented;
356 if (bitmap->format & PixelFormatIndexed)
357 *color = bitmap->image.palette->Entries[index];
358 else
359 *color = a<<24|r<<16|g<<8|b;
361 return Ok;
364 static inline UINT get_palette_index(BYTE r, BYTE g, BYTE b, BYTE a, ColorPalette *palette)
366 BYTE index = 0;
367 int best_distance = 0x7fff;
368 int distance;
369 UINT i;
371 if (!palette) return 0;
372 /* This algorithm scans entire palette,
373 computes difference from desired color (all color components have equal weight)
374 and returns the index of color with least difference.
376 Note: Maybe it could be replaced with a better algorithm for better image quality
377 and performance, though better algorithm would probably need some pre-built lookup
378 tables and thus may actually be slower if this method is called only few times per
379 every image.
381 for(i=0;i<palette->Count;i++) {
382 ARGB color=palette->Entries[i];
383 distance=abs(b-(color & 0xff)) + abs(g-(color>>8 & 0xff)) + abs(r-(color>>16 & 0xff)) + abs(a-(color>>24 & 0xff));
384 if (distance<best_distance) {
385 best_distance=distance;
386 index=i;
389 return index;
392 static inline void setpixel_8bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
393 BYTE *row, UINT x, ColorPalette *palette)
395 BYTE index = get_palette_index(r,g,b,a,palette);
396 row[x]=index;
399 static inline void setpixel_1bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
400 BYTE *row, UINT x, ColorPalette *palette)
402 row[x/8] = (row[x/8] & ~(1<<(7-x%8))) | (get_palette_index(r,g,b,a,palette)<<(7-x%8));
405 static inline void setpixel_4bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
406 BYTE *row, UINT x, ColorPalette *palette)
408 if (x & 1)
409 row[x/2] = (row[x/2] & 0xf0) | get_palette_index(r,g,b,a,palette);
410 else
411 row[x/2] = (row[x/2] & 0x0f) | get_palette_index(r,g,b,a,palette)<<4;
414 static inline void setpixel_16bppGrayScale(BYTE r, BYTE g, BYTE b, BYTE a,
415 BYTE *row, UINT x)
417 *((WORD*)(row)+x) = (r+g+b)*85;
420 static inline void setpixel_16bppRGB555(BYTE r, BYTE g, BYTE b, BYTE a,
421 BYTE *row, UINT x)
423 *((WORD*)(row)+x) = (r<<7&0x7c00)|
424 (g<<2&0x03e0)|
425 (b>>3&0x001f);
428 static inline void setpixel_16bppRGB565(BYTE r, BYTE g, BYTE b, BYTE a,
429 BYTE *row, UINT x)
431 *((WORD*)(row)+x) = (r<<8&0xf800)|
432 (g<<3&0x07e0)|
433 (b>>3&0x001f);
436 static inline void setpixel_16bppARGB1555(BYTE r, BYTE g, BYTE b, BYTE a,
437 BYTE *row, UINT x)
439 *((WORD*)(row)+x) = (a<<8&0x8000)|
440 (r<<7&0x7c00)|
441 (g<<2&0x03e0)|
442 (b>>3&0x001f);
445 static inline void setpixel_24bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
446 BYTE *row, UINT x)
448 row[x*3+2] = r;
449 row[x*3+1] = g;
450 row[x*3] = b;
453 static inline void setpixel_32bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
454 BYTE *row, UINT x)
456 *((DWORD*)(row)+x) = (r<<16)|(g<<8)|b;
459 static inline void setpixel_32bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
460 BYTE *row, UINT x)
462 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
465 static inline void setpixel_32bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
466 BYTE *row, UINT x)
468 r = r * a / 255;
469 g = g * a / 255;
470 b = b * a / 255;
471 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
474 static inline void setpixel_48bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
475 BYTE *row, UINT x)
477 row[x*6+5] = row[x*6+4] = r;
478 row[x*6+3] = row[x*6+2] = g;
479 row[x*6+1] = row[x*6] = b;
482 static inline void setpixel_64bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
483 BYTE *row, UINT x)
485 UINT64 a64=a, r64=r, g64=g, b64=b;
486 *((UINT64*)(row)+x) = (a64<<56)|(a64<<48)|(r64<<40)|(r64<<32)|(g64<<24)|(g64<<16)|(b64<<8)|b64;
489 static inline void setpixel_64bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
490 BYTE *row, UINT x)
492 UINT64 a64, r64, g64, b64;
493 a64 = a * 257;
494 r64 = r * a / 255;
495 g64 = g * a / 255;
496 b64 = b * a / 255;
497 *((UINT64*)(row)+x) = (a64<<48)|(r64<<32)|(g64<<16)|b64;
500 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
501 ARGB color)
503 BYTE a, r, g, b;
504 BYTE *row;
506 if(!bitmap || x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
507 return InvalidParameter;
509 a = color>>24;
510 r = color>>16;
511 g = color>>8;
512 b = color;
514 row = bitmap->bits + bitmap->stride * y;
516 switch (bitmap->format)
518 case PixelFormat16bppGrayScale:
519 setpixel_16bppGrayScale(r,g,b,a,row,x);
520 break;
521 case PixelFormat16bppRGB555:
522 setpixel_16bppRGB555(r,g,b,a,row,x);
523 break;
524 case PixelFormat16bppRGB565:
525 setpixel_16bppRGB565(r,g,b,a,row,x);
526 break;
527 case PixelFormat16bppARGB1555:
528 setpixel_16bppARGB1555(r,g,b,a,row,x);
529 break;
530 case PixelFormat24bppRGB:
531 setpixel_24bppRGB(r,g,b,a,row,x);
532 break;
533 case PixelFormat32bppRGB:
534 setpixel_32bppRGB(r,g,b,a,row,x);
535 break;
536 case PixelFormat32bppARGB:
537 setpixel_32bppARGB(r,g,b,a,row,x);
538 break;
539 case PixelFormat32bppPARGB:
540 setpixel_32bppPARGB(r,g,b,a,row,x);
541 break;
542 case PixelFormat48bppRGB:
543 setpixel_48bppRGB(r,g,b,a,row,x);
544 break;
545 case PixelFormat64bppARGB:
546 setpixel_64bppARGB(r,g,b,a,row,x);
547 break;
548 case PixelFormat64bppPARGB:
549 setpixel_64bppPARGB(r,g,b,a,row,x);
550 break;
551 case PixelFormat8bppIndexed:
552 setpixel_8bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
553 break;
554 case PixelFormat4bppIndexed:
555 setpixel_4bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
556 break;
557 case PixelFormat1bppIndexed:
558 setpixel_1bppIndexed(r,g,b,a,row,x,bitmap->image.palette);
559 break;
560 default:
561 FIXME("not implemented for format 0x%x\n", bitmap->format);
562 return NotImplemented;
565 return Ok;
568 GpStatus convert_pixels(INT width, INT height,
569 INT dst_stride, BYTE *dst_bits, PixelFormat dst_format,
570 INT src_stride, const BYTE *src_bits, PixelFormat src_format,
571 ColorPalette *palette)
573 INT x, y;
575 if (src_format == dst_format ||
576 (dst_format == PixelFormat32bppRGB && PIXELFORMATBPP(src_format) == 32))
578 UINT widthbytes = PIXELFORMATBPP(src_format) * width / 8;
579 for (y=0; y<height; y++)
580 memcpy(dst_bits+dst_stride*y, src_bits+src_stride*y, widthbytes);
581 return Ok;
584 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
585 for (y=0; y<height; y++) \
586 for (x=0; x<width; x++) { \
587 BYTE index; \
588 ARGB argb; \
589 BYTE *color = (BYTE *)&argb; \
590 getpixel_function(&index, src_bits+src_stride*y, x); \
591 argb = (palette && index < palette->Count) ? palette->Entries[index] : 0; \
592 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
594 return Ok; \
595 } while (0);
597 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
598 for (y=0; y<height; y++) \
599 for (x=0; x<width; x++) { \
600 BYTE r, g, b, a; \
601 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
602 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
604 return Ok; \
605 } while (0);
607 #define convert_rgb_to_indexed(getpixel_function, setpixel_function) do { \
608 for (y=0; y<height; y++) \
609 for (x=0; x<width; x++) { \
610 BYTE r, g, b, a; \
611 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
612 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x, palette); \
614 return Ok; \
615 } while (0);
617 switch (src_format)
619 case PixelFormat1bppIndexed:
620 switch (dst_format)
622 case PixelFormat16bppGrayScale:
623 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppGrayScale);
624 case PixelFormat16bppRGB555:
625 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB555);
626 case PixelFormat16bppRGB565:
627 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB565);
628 case PixelFormat16bppARGB1555:
629 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppARGB1555);
630 case PixelFormat24bppRGB:
631 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_24bppRGB);
632 case PixelFormat32bppRGB:
633 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppRGB);
634 case PixelFormat32bppARGB:
635 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppARGB);
636 case PixelFormat32bppPARGB:
637 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppPARGB);
638 case PixelFormat48bppRGB:
639 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_48bppRGB);
640 case PixelFormat64bppARGB:
641 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_64bppARGB);
642 default:
643 break;
645 break;
646 case PixelFormat4bppIndexed:
647 switch (dst_format)
649 case PixelFormat16bppGrayScale:
650 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppGrayScale);
651 case PixelFormat16bppRGB555:
652 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB555);
653 case PixelFormat16bppRGB565:
654 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB565);
655 case PixelFormat16bppARGB1555:
656 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppARGB1555);
657 case PixelFormat24bppRGB:
658 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_24bppRGB);
659 case PixelFormat32bppRGB:
660 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppRGB);
661 case PixelFormat32bppARGB:
662 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppARGB);
663 case PixelFormat32bppPARGB:
664 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppPARGB);
665 case PixelFormat48bppRGB:
666 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_48bppRGB);
667 case PixelFormat64bppARGB:
668 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_64bppARGB);
669 default:
670 break;
672 break;
673 case PixelFormat8bppIndexed:
674 switch (dst_format)
676 case PixelFormat16bppGrayScale:
677 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppGrayScale);
678 case PixelFormat16bppRGB555:
679 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB555);
680 case PixelFormat16bppRGB565:
681 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB565);
682 case PixelFormat16bppARGB1555:
683 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppARGB1555);
684 case PixelFormat24bppRGB:
685 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_24bppRGB);
686 case PixelFormat32bppRGB:
687 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppRGB);
688 case PixelFormat32bppARGB:
689 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppARGB);
690 case PixelFormat32bppPARGB:
691 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppPARGB);
692 case PixelFormat48bppRGB:
693 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_48bppRGB);
694 case PixelFormat64bppARGB:
695 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_64bppARGB);
696 default:
697 break;
699 break;
700 case PixelFormat16bppGrayScale:
701 switch (dst_format)
703 case PixelFormat1bppIndexed:
704 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_1bppIndexed);
705 case PixelFormat8bppIndexed:
706 convert_rgb_to_indexed(getpixel_16bppGrayScale, setpixel_8bppIndexed);
707 case PixelFormat16bppRGB555:
708 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB555);
709 case PixelFormat16bppRGB565:
710 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB565);
711 case PixelFormat16bppARGB1555:
712 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppARGB1555);
713 case PixelFormat24bppRGB:
714 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_24bppRGB);
715 case PixelFormat32bppRGB:
716 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppRGB);
717 case PixelFormat32bppARGB:
718 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppARGB);
719 case PixelFormat32bppPARGB:
720 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppPARGB);
721 case PixelFormat48bppRGB:
722 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_48bppRGB);
723 case PixelFormat64bppARGB:
724 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_64bppARGB);
725 default:
726 break;
728 break;
729 case PixelFormat16bppRGB555:
730 switch (dst_format)
732 case PixelFormat1bppIndexed:
733 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_1bppIndexed);
734 case PixelFormat8bppIndexed:
735 convert_rgb_to_indexed(getpixel_16bppRGB555, setpixel_8bppIndexed);
736 case PixelFormat16bppGrayScale:
737 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppGrayScale);
738 case PixelFormat16bppRGB565:
739 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppRGB565);
740 case PixelFormat16bppARGB1555:
741 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppARGB1555);
742 case PixelFormat24bppRGB:
743 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_24bppRGB);
744 case PixelFormat32bppRGB:
745 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppRGB);
746 case PixelFormat32bppARGB:
747 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppARGB);
748 case PixelFormat32bppPARGB:
749 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppPARGB);
750 case PixelFormat48bppRGB:
751 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_48bppRGB);
752 case PixelFormat64bppARGB:
753 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_64bppARGB);
754 default:
755 break;
757 break;
758 case PixelFormat16bppRGB565:
759 switch (dst_format)
761 case PixelFormat1bppIndexed:
762 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_1bppIndexed);
763 case PixelFormat8bppIndexed:
764 convert_rgb_to_indexed(getpixel_16bppRGB565, setpixel_8bppIndexed);
765 case PixelFormat16bppGrayScale:
766 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppGrayScale);
767 case PixelFormat16bppRGB555:
768 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppRGB555);
769 case PixelFormat16bppARGB1555:
770 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppARGB1555);
771 case PixelFormat24bppRGB:
772 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_24bppRGB);
773 case PixelFormat32bppRGB:
774 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppRGB);
775 case PixelFormat32bppARGB:
776 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppARGB);
777 case PixelFormat32bppPARGB:
778 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppPARGB);
779 case PixelFormat48bppRGB:
780 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_48bppRGB);
781 case PixelFormat64bppARGB:
782 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_64bppARGB);
783 default:
784 break;
786 break;
787 case PixelFormat16bppARGB1555:
788 switch (dst_format)
790 case PixelFormat1bppIndexed:
791 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_1bppIndexed);
792 case PixelFormat8bppIndexed:
793 convert_rgb_to_indexed(getpixel_16bppARGB1555, setpixel_8bppIndexed);
794 case PixelFormat16bppGrayScale:
795 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppGrayScale);
796 case PixelFormat16bppRGB555:
797 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB555);
798 case PixelFormat16bppRGB565:
799 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB565);
800 case PixelFormat24bppRGB:
801 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_24bppRGB);
802 case PixelFormat32bppRGB:
803 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppRGB);
804 case PixelFormat32bppARGB:
805 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppARGB);
806 case PixelFormat32bppPARGB:
807 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppPARGB);
808 case PixelFormat48bppRGB:
809 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_48bppRGB);
810 case PixelFormat64bppARGB:
811 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_64bppARGB);
812 default:
813 break;
815 break;
816 case PixelFormat24bppRGB:
817 switch (dst_format)
819 case PixelFormat1bppIndexed:
820 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_1bppIndexed);
821 case PixelFormat8bppIndexed:
822 convert_rgb_to_indexed(getpixel_24bppRGB, setpixel_8bppIndexed);
823 case PixelFormat16bppGrayScale:
824 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppGrayScale);
825 case PixelFormat16bppRGB555:
826 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB555);
827 case PixelFormat16bppRGB565:
828 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB565);
829 case PixelFormat16bppARGB1555:
830 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppARGB1555);
831 case PixelFormat32bppRGB:
832 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppRGB);
833 case PixelFormat32bppARGB:
834 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppARGB);
835 case PixelFormat32bppPARGB:
836 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppPARGB);
837 case PixelFormat48bppRGB:
838 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_48bppRGB);
839 case PixelFormat64bppARGB:
840 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_64bppARGB);
841 default:
842 break;
844 break;
845 case PixelFormat32bppRGB:
846 switch (dst_format)
848 case PixelFormat1bppIndexed:
849 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_1bppIndexed);
850 case PixelFormat8bppIndexed:
851 convert_rgb_to_indexed(getpixel_32bppRGB, setpixel_8bppIndexed);
852 case PixelFormat16bppGrayScale:
853 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppGrayScale);
854 case PixelFormat16bppRGB555:
855 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB555);
856 case PixelFormat16bppRGB565:
857 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB565);
858 case PixelFormat16bppARGB1555:
859 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppARGB1555);
860 case PixelFormat24bppRGB:
861 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_24bppRGB);
862 case PixelFormat32bppARGB:
863 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppARGB);
864 case PixelFormat32bppPARGB:
865 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppPARGB);
866 case PixelFormat48bppRGB:
867 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_48bppRGB);
868 case PixelFormat64bppARGB:
869 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_64bppARGB);
870 default:
871 break;
873 break;
874 case PixelFormat32bppARGB:
875 switch (dst_format)
877 case PixelFormat1bppIndexed:
878 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_1bppIndexed);
879 case PixelFormat8bppIndexed:
880 convert_rgb_to_indexed(getpixel_32bppARGB, setpixel_8bppIndexed);
881 case PixelFormat16bppGrayScale:
882 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppGrayScale);
883 case PixelFormat16bppRGB555:
884 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB555);
885 case PixelFormat16bppRGB565:
886 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB565);
887 case PixelFormat16bppARGB1555:
888 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppARGB1555);
889 case PixelFormat24bppRGB:
890 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_24bppRGB);
891 case PixelFormat32bppPARGB:
892 convert_32bppARGB_to_32bppPARGB(width, height, dst_bits, dst_stride, src_bits, src_stride);
893 return Ok;
894 case PixelFormat48bppRGB:
895 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_48bppRGB);
896 case PixelFormat64bppARGB:
897 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_64bppARGB);
898 default:
899 break;
901 break;
902 case PixelFormat32bppPARGB:
903 switch (dst_format)
905 case PixelFormat1bppIndexed:
906 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_1bppIndexed);
907 case PixelFormat8bppIndexed:
908 convert_rgb_to_indexed(getpixel_32bppPARGB, setpixel_8bppIndexed);
909 case PixelFormat16bppGrayScale:
910 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppGrayScale);
911 case PixelFormat16bppRGB555:
912 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB555);
913 case PixelFormat16bppRGB565:
914 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB565);
915 case PixelFormat16bppARGB1555:
916 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppARGB1555);
917 case PixelFormat24bppRGB:
918 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_24bppRGB);
919 case PixelFormat32bppRGB:
920 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppRGB);
921 case PixelFormat32bppARGB:
922 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppARGB);
923 case PixelFormat48bppRGB:
924 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_48bppRGB);
925 case PixelFormat64bppARGB:
926 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_64bppARGB);
927 default:
928 break;
930 break;
931 case PixelFormat48bppRGB:
932 switch (dst_format)
934 case PixelFormat1bppIndexed:
935 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_1bppIndexed);
936 case PixelFormat8bppIndexed:
937 convert_rgb_to_indexed(getpixel_48bppRGB, setpixel_8bppIndexed);
938 case PixelFormat16bppGrayScale:
939 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppGrayScale);
940 case PixelFormat16bppRGB555:
941 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB555);
942 case PixelFormat16bppRGB565:
943 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB565);
944 case PixelFormat16bppARGB1555:
945 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppARGB1555);
946 case PixelFormat24bppRGB:
947 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_24bppRGB);
948 case PixelFormat32bppRGB:
949 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppRGB);
950 case PixelFormat32bppARGB:
951 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppARGB);
952 case PixelFormat32bppPARGB:
953 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppPARGB);
954 case PixelFormat64bppARGB:
955 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_64bppARGB);
956 default:
957 break;
959 break;
960 case PixelFormat64bppARGB:
961 switch (dst_format)
963 case PixelFormat1bppIndexed:
964 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_1bppIndexed);
965 case PixelFormat8bppIndexed:
966 convert_rgb_to_indexed(getpixel_64bppARGB, setpixel_8bppIndexed);
967 case PixelFormat16bppGrayScale:
968 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppGrayScale);
969 case PixelFormat16bppRGB555:
970 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB555);
971 case PixelFormat16bppRGB565:
972 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB565);
973 case PixelFormat16bppARGB1555:
974 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppARGB1555);
975 case PixelFormat24bppRGB:
976 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_24bppRGB);
977 case PixelFormat32bppRGB:
978 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppRGB);
979 case PixelFormat32bppARGB:
980 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppARGB);
981 case PixelFormat32bppPARGB:
982 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppPARGB);
983 case PixelFormat48bppRGB:
984 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_48bppRGB);
985 default:
986 break;
988 break;
989 case PixelFormat64bppPARGB:
990 switch (dst_format)
992 case PixelFormat1bppIndexed:
993 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_1bppIndexed);
994 case PixelFormat8bppIndexed:
995 convert_rgb_to_indexed(getpixel_64bppPARGB, setpixel_8bppIndexed);
996 case PixelFormat16bppGrayScale:
997 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppGrayScale);
998 case PixelFormat16bppRGB555:
999 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB555);
1000 case PixelFormat16bppRGB565:
1001 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB565);
1002 case PixelFormat16bppARGB1555:
1003 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppARGB1555);
1004 case PixelFormat24bppRGB:
1005 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_24bppRGB);
1006 case PixelFormat32bppRGB:
1007 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppRGB);
1008 case PixelFormat32bppARGB:
1009 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppARGB);
1010 case PixelFormat32bppPARGB:
1011 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppPARGB);
1012 case PixelFormat48bppRGB:
1013 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_48bppRGB);
1014 case PixelFormat64bppARGB:
1015 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_64bppARGB);
1016 default:
1017 break;
1019 break;
1020 default:
1021 break;
1024 #undef convert_indexed_to_rgb
1025 #undef convert_rgb_to_rgb
1027 return NotImplemented;
1030 /* This function returns a pointer to an array of pixels that represents the
1031 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
1032 * flags. It is correct behavior that a user who calls this function with write
1033 * privileges can write to the whole bitmap (not just the area in rect).
1035 * FIXME: only used portion of format is bits per pixel. */
1036 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
1037 UINT flags, PixelFormat format, BitmapData* lockeddata)
1039 INT bitspp = PIXELFORMATBPP(format);
1040 GpRect act_rect; /* actual rect to be used */
1041 GpStatus stat;
1043 TRACE("%p %p %d 0x%x %p\n", bitmap, rect, flags, format, lockeddata);
1045 if(!lockeddata || !bitmap)
1046 return InvalidParameter;
1048 if(rect){
1049 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
1050 (rect->Y + rect->Height > bitmap->height) || !flags)
1051 return InvalidParameter;
1053 act_rect = *rect;
1055 else{
1056 act_rect.X = act_rect.Y = 0;
1057 act_rect.Width = bitmap->width;
1058 act_rect.Height = bitmap->height;
1061 if(bitmap->lockmode)
1063 WARN("bitmap is already locked and cannot be locked again\n");
1064 return WrongState;
1067 if (bitmap->bits && bitmap->format == format && !(flags & ImageLockModeUserInputBuf))
1069 /* no conversion is necessary; just use the bits directly */
1070 lockeddata->Width = act_rect.Width;
1071 lockeddata->Height = act_rect.Height;
1072 lockeddata->PixelFormat = format;
1073 lockeddata->Reserved = flags;
1074 lockeddata->Stride = bitmap->stride;
1075 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
1076 bitmap->stride * act_rect.Y;
1078 bitmap->lockmode = flags | ImageLockModeRead;
1079 bitmap->numlocks++;
1081 return Ok;
1084 /* Make sure we can convert to the requested format. */
1085 if (flags & ImageLockModeRead)
1087 stat = convert_pixels(0, 0, 0, NULL, format, 0, NULL, bitmap->format, NULL);
1088 if (stat == NotImplemented)
1090 FIXME("cannot read bitmap from %x to %x\n", bitmap->format, format);
1091 return NotImplemented;
1095 /* If we're opening for writing, make sure we'll be able to write back in
1096 * the original format. */
1097 if (flags & ImageLockModeWrite)
1099 stat = convert_pixels(0, 0, 0, NULL, bitmap->format, 0, NULL, format, NULL);
1100 if (stat == NotImplemented)
1102 FIXME("cannot write bitmap from %x to %x\n", format, bitmap->format);
1103 return NotImplemented;
1107 lockeddata->Width = act_rect.Width;
1108 lockeddata->Height = act_rect.Height;
1109 lockeddata->PixelFormat = format;
1110 lockeddata->Reserved = flags;
1112 if(!(flags & ImageLockModeUserInputBuf))
1114 lockeddata->Stride = (((act_rect.Width * bitspp + 7) / 8) + 3) & ~3;
1116 bitmap->bitmapbits = GdipAlloc(lockeddata->Stride * act_rect.Height);
1118 if (!bitmap->bitmapbits) return OutOfMemory;
1120 lockeddata->Scan0 = bitmap->bitmapbits;
1123 if (flags & ImageLockModeRead)
1125 static BOOL fixme = FALSE;
1127 if (!fixme && (PIXELFORMATBPP(bitmap->format) * act_rect.X) % 8 != 0)
1129 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1130 fixme = TRUE;
1133 stat = convert_pixels(act_rect.Width, act_rect.Height,
1134 lockeddata->Stride, lockeddata->Scan0, format,
1135 bitmap->stride,
1136 bitmap->bits + bitmap->stride * act_rect.Y + PIXELFORMATBPP(bitmap->format) * act_rect.X / 8,
1137 bitmap->format, bitmap->image.palette);
1139 if (stat != Ok)
1141 GdipFree(bitmap->bitmapbits);
1142 bitmap->bitmapbits = NULL;
1143 return stat;
1147 bitmap->lockmode = flags | ImageLockModeRead;
1148 bitmap->numlocks++;
1149 bitmap->lockx = act_rect.X;
1150 bitmap->locky = act_rect.Y;
1152 return Ok;
1155 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
1157 TRACE("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
1159 if (!bitmap || xdpi == 0.0 || ydpi == 0.0)
1160 return InvalidParameter;
1162 bitmap->image.xres = xdpi;
1163 bitmap->image.yres = ydpi;
1165 return Ok;
1168 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
1169 BitmapData* lockeddata)
1171 GpStatus stat;
1172 static BOOL fixme = FALSE;
1174 TRACE("(%p,%p)\n", bitmap, lockeddata);
1176 if(!bitmap || !lockeddata)
1177 return InvalidParameter;
1179 if(!bitmap->lockmode)
1180 return WrongState;
1182 if(!(lockeddata->Reserved & ImageLockModeWrite)){
1183 if(!(--bitmap->numlocks))
1184 bitmap->lockmode = 0;
1186 GdipFree(bitmap->bitmapbits);
1187 bitmap->bitmapbits = NULL;
1188 return Ok;
1191 if (!bitmap->bitmapbits && !(lockeddata->Reserved & ImageLockModeUserInputBuf))
1193 /* we passed a direct reference; no need to do anything */
1194 bitmap->lockmode = 0;
1195 bitmap->numlocks = 0;
1196 return Ok;
1199 if (!fixme && (PIXELFORMATBPP(bitmap->format) * bitmap->lockx) % 8 != 0)
1201 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1202 fixme = TRUE;
1205 stat = convert_pixels(lockeddata->Width, lockeddata->Height,
1206 bitmap->stride,
1207 bitmap->bits + bitmap->stride * bitmap->locky + PIXELFORMATBPP(bitmap->format) * bitmap->lockx / 8,
1208 bitmap->format,
1209 lockeddata->Stride, lockeddata->Scan0, lockeddata->PixelFormat, NULL);
1211 if (stat != Ok)
1213 ERR("failed to convert pixels; this should never happen\n");
1216 GdipFree(bitmap->bitmapbits);
1217 bitmap->bitmapbits = NULL;
1218 bitmap->lockmode = 0;
1219 bitmap->numlocks = 0;
1221 return stat;
1224 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
1225 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1227 Rect area;
1228 GpStatus stat;
1230 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1232 if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
1233 x < 0 || y < 0 ||
1234 x + width > srcBitmap->width || y + height > srcBitmap->height)
1236 TRACE("<-- InvalidParameter\n");
1237 return InvalidParameter;
1240 if (format == PixelFormatDontCare)
1241 format = srcBitmap->format;
1243 area.X = gdip_round(x);
1244 area.Y = gdip_round(y);
1245 area.Width = gdip_round(width);
1246 area.Height = gdip_round(height);
1248 stat = GdipCreateBitmapFromScan0(area.Width, area.Height, 0, format, NULL, dstBitmap);
1249 if (stat == Ok)
1251 stat = convert_pixels(area.Width, area.Height, (*dstBitmap)->stride, (*dstBitmap)->bits, (*dstBitmap)->format,
1252 srcBitmap->stride,
1253 srcBitmap->bits + srcBitmap->stride * area.Y + PIXELFORMATBPP(srcBitmap->format) * area.X / 8,
1254 srcBitmap->format, srcBitmap->image.palette);
1256 if (stat == Ok && srcBitmap->image.palette)
1258 ColorPalette *src_palette, *dst_palette;
1260 src_palette = srcBitmap->image.palette;
1262 dst_palette = GdipAlloc(sizeof(UINT) * 2 + sizeof(ARGB) * src_palette->Count);
1264 if (dst_palette)
1266 dst_palette->Flags = src_palette->Flags;
1267 dst_palette->Count = src_palette->Count;
1268 memcpy(dst_palette->Entries, src_palette->Entries, sizeof(ARGB) * src_palette->Count);
1270 GdipFree((*dstBitmap)->image.palette);
1271 (*dstBitmap)->image.palette = dst_palette;
1273 else
1274 stat = OutOfMemory;
1277 if (stat != Ok)
1278 GdipDisposeImage((GpImage*)*dstBitmap);
1281 if (stat != Ok)
1282 *dstBitmap = NULL;
1284 return stat;
1287 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
1288 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1290 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1292 return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
1295 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
1297 GpStatus stat = GenericError;
1299 TRACE("%p, %p\n", image, cloneImage);
1301 if (!image || !cloneImage)
1302 return InvalidParameter;
1304 if (image->picture)
1306 IStream* stream;
1307 HRESULT hr;
1308 INT size;
1309 LARGE_INTEGER move;
1311 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
1312 if (FAILED(hr))
1313 return GenericError;
1315 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
1316 if(FAILED(hr))
1318 WARN("Failed to save image on stream\n");
1319 goto out;
1322 /* Set seek pointer back to the beginning of the picture */
1323 move.QuadPart = 0;
1324 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1325 if (FAILED(hr))
1326 goto out;
1328 stat = GdipLoadImageFromStream(stream, cloneImage);
1329 if (stat != Ok) WARN("Failed to load image from stream\n");
1331 out:
1332 IStream_Release(stream);
1333 return stat;
1335 else if (image->type == ImageTypeBitmap)
1337 GpBitmap *bitmap = (GpBitmap *)image;
1339 return GdipCloneBitmapAreaI(0, 0, bitmap->width, bitmap->height,
1340 bitmap->format, bitmap, (GpBitmap **)cloneImage);
1342 else if (image->type == ImageTypeMetafile && ((GpMetafile*)image)->hemf)
1344 GpMetafile *result, *metafile;
1346 metafile = (GpMetafile*)image;
1348 result = GdipAlloc(sizeof(*result));
1349 if (!result)
1350 return OutOfMemory;
1352 result->image.type = ImageTypeMetafile;
1353 result->image.format = image->format;
1354 result->image.flags = image->flags;
1355 result->image.frame_count = 1;
1356 result->image.xres = image->xres;
1357 result->image.yres = image->yres;
1358 result->bounds = metafile->bounds;
1359 result->unit = metafile->unit;
1360 result->metafile_type = metafile->metafile_type;
1361 result->hemf = CopyEnhMetaFileW(metafile->hemf, NULL);
1363 if (!result->hemf)
1365 GdipFree(result);
1366 return OutOfMemory;
1369 *cloneImage = &result->image;
1370 return Ok;
1372 else
1374 WARN("GpImage with no image data (metafile in wrong state?)\n");
1375 return InvalidParameter;
1379 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
1380 GpBitmap **bitmap)
1382 GpStatus stat;
1383 IStream *stream;
1385 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1387 if(!filename || !bitmap)
1388 return InvalidParameter;
1390 *bitmap = NULL;
1392 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1394 if(stat != Ok)
1395 return stat;
1397 stat = GdipCreateBitmapFromStream(stream, bitmap);
1399 IStream_Release(stream);
1401 return stat;
1404 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
1405 VOID *bits, GpBitmap **bitmap)
1407 DWORD height, stride;
1408 PixelFormat format;
1410 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
1412 if (!info || !bits || !bitmap)
1413 return InvalidParameter;
1415 height = abs(info->bmiHeader.biHeight);
1416 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
1418 if(info->bmiHeader.biHeight > 0) /* bottom-up */
1420 bits = (BYTE*)bits + (height - 1) * stride;
1421 stride = -stride;
1424 switch(info->bmiHeader.biBitCount) {
1425 case 1:
1426 format = PixelFormat1bppIndexed;
1427 break;
1428 case 4:
1429 format = PixelFormat4bppIndexed;
1430 break;
1431 case 8:
1432 format = PixelFormat8bppIndexed;
1433 break;
1434 case 16:
1435 format = PixelFormat16bppRGB555;
1436 break;
1437 case 24:
1438 format = PixelFormat24bppRGB;
1439 break;
1440 case 32:
1441 format = PixelFormat32bppRGB;
1442 break;
1443 default:
1444 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
1445 *bitmap = NULL;
1446 return InvalidParameter;
1449 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
1450 bits, bitmap);
1454 /* FIXME: no icm */
1455 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
1456 GpBitmap **bitmap)
1458 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1460 return GdipCreateBitmapFromFile(filename, bitmap);
1463 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
1464 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
1466 HBITMAP hbm;
1467 GpStatus stat = InvalidParameter;
1469 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
1471 if(!lpBitmapName || !bitmap)
1472 return InvalidParameter;
1474 /* load DIB */
1475 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
1476 LR_CREATEDIBSECTION);
1478 if(hbm){
1479 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1480 DeleteObject(hbm);
1483 return stat;
1486 static inline DWORD blend_argb_no_bkgnd_alpha(DWORD src, DWORD bkgnd)
1488 BYTE b = (BYTE)src;
1489 BYTE g = (BYTE)(src >> 8);
1490 BYTE r = (BYTE)(src >> 16);
1491 DWORD alpha = (BYTE)(src >> 24);
1492 return ((b + ((BYTE)bkgnd * (255 - alpha) + 127) / 255) |
1493 (g + ((BYTE)(bkgnd >> 8) * (255 - alpha) + 127) / 255) << 8 |
1494 (r + ((BYTE)(bkgnd >> 16) * (255 - alpha) + 127) / 255) << 16 |
1495 (alpha << 24));
1498 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
1499 HBITMAP* hbmReturn, ARGB background)
1501 GpStatus stat;
1502 HBITMAP result;
1503 UINT width, height;
1504 BITMAPINFOHEADER bih;
1505 LPBYTE bits;
1506 BitmapData lockeddata;
1507 TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
1509 if (!bitmap || !hbmReturn) return InvalidParameter;
1511 GdipGetImageWidth((GpImage*)bitmap, &width);
1512 GdipGetImageHeight((GpImage*)bitmap, &height);
1514 bih.biSize = sizeof(bih);
1515 bih.biWidth = width;
1516 bih.biHeight = height;
1517 bih.biPlanes = 1;
1518 bih.biBitCount = 32;
1519 bih.biCompression = BI_RGB;
1520 bih.biSizeImage = 0;
1521 bih.biXPelsPerMeter = 0;
1522 bih.biYPelsPerMeter = 0;
1523 bih.biClrUsed = 0;
1524 bih.biClrImportant = 0;
1526 result = CreateDIBSection(0, (BITMAPINFO*)&bih, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1528 if (result)
1530 lockeddata.Stride = -width * 4;
1531 lockeddata.Scan0 = bits + (width * 4 * (height - 1));
1533 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead|ImageLockModeUserInputBuf,
1534 PixelFormat32bppPARGB, &lockeddata);
1536 if (stat == Ok)
1537 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1539 if (stat == Ok && (background & 0xffffff))
1541 DWORD *ptr;
1542 UINT i;
1543 for (ptr = (DWORD*)bits, i = 0; i < width * height; ptr++, i++)
1545 if ((*ptr & 0xff000000) == 0xff000000) continue;
1546 *ptr = blend_argb_no_bkgnd_alpha(*ptr, background);
1550 else
1551 stat = GenericError;
1553 if (stat != Ok && result)
1555 DeleteObject(result);
1556 result = NULL;
1559 *hbmReturn = result;
1561 return stat;
1564 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
1565 GpGraphics* target, GpBitmap** bitmap)
1567 GpStatus ret;
1569 TRACE("(%d, %d, %p, %p)\n", width, height, target, bitmap);
1571 if(!target || !bitmap)
1572 return InvalidParameter;
1574 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
1575 NULL, bitmap);
1577 if (ret == Ok)
1579 GdipGetDpiX(target, &(*bitmap)->image.xres);
1580 GdipGetDpiY(target, &(*bitmap)->image.yres);
1583 return ret;
1586 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1588 GpStatus stat;
1589 ICONINFO iinfo;
1590 BITMAP bm;
1591 int ret;
1592 UINT width, height, stride;
1593 GpRect rect;
1594 BitmapData lockeddata;
1595 HDC screendc;
1596 BOOL has_alpha;
1597 int x, y;
1598 BITMAPINFOHEADER bih;
1599 DWORD *src;
1600 BYTE *dst_row;
1601 DWORD *dst;
1603 TRACE("%p, %p\n", hicon, bitmap);
1605 if(!bitmap || !GetIconInfo(hicon, &iinfo))
1606 return InvalidParameter;
1608 /* get the size of the icon */
1609 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1610 if (ret == 0) {
1611 DeleteObject(iinfo.hbmColor);
1612 DeleteObject(iinfo.hbmMask);
1613 return GenericError;
1616 width = bm.bmWidth;
1617 height = iinfo.hbmColor ? abs(bm.bmHeight) : abs(bm.bmHeight) / 2;
1618 stride = width * 4;
1620 stat = GdipCreateBitmapFromScan0(width, height, stride, PixelFormat32bppARGB, NULL, bitmap);
1621 if (stat != Ok) {
1622 DeleteObject(iinfo.hbmColor);
1623 DeleteObject(iinfo.hbmMask);
1624 return stat;
1627 rect.X = 0;
1628 rect.Y = 0;
1629 rect.Width = width;
1630 rect.Height = height;
1632 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1633 if (stat != Ok) {
1634 DeleteObject(iinfo.hbmColor);
1635 DeleteObject(iinfo.hbmMask);
1636 GdipDisposeImage((GpImage*)*bitmap);
1637 return stat;
1640 bih.biSize = sizeof(bih);
1641 bih.biWidth = width;
1642 bih.biHeight = iinfo.hbmColor ? -height: -height * 2;
1643 bih.biPlanes = 1;
1644 bih.biBitCount = 32;
1645 bih.biCompression = BI_RGB;
1646 bih.biSizeImage = 0;
1647 bih.biXPelsPerMeter = 0;
1648 bih.biYPelsPerMeter = 0;
1649 bih.biClrUsed = 0;
1650 bih.biClrImportant = 0;
1652 screendc = CreateCompatibleDC(0);
1653 if (iinfo.hbmColor)
1655 GetDIBits(screendc, iinfo.hbmColor, 0, height, lockeddata.Scan0, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1657 if (bm.bmBitsPixel == 32)
1659 has_alpha = FALSE;
1661 /* If any pixel has a non-zero alpha, ignore hbmMask */
1662 src = (DWORD*)lockeddata.Scan0;
1663 for (x=0; x<width && !has_alpha; x++)
1664 for (y=0; y<height && !has_alpha; y++)
1665 if ((*src++ & 0xff000000) != 0)
1666 has_alpha = TRUE;
1668 else has_alpha = FALSE;
1670 else
1672 GetDIBits(screendc, iinfo.hbmMask, 0, height, lockeddata.Scan0, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1673 has_alpha = FALSE;
1676 if (!has_alpha)
1678 if (iinfo.hbmMask)
1680 BYTE *bits = HeapAlloc(GetProcessHeap(), 0, height * stride);
1682 /* read alpha data from the mask */
1683 if (iinfo.hbmColor)
1684 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1685 else
1686 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1688 src = (DWORD*)bits;
1689 dst_row = lockeddata.Scan0;
1690 for (y=0; y<height; y++)
1692 dst = (DWORD*)dst_row;
1693 for (x=0; x<height; x++)
1695 DWORD src_value = *src++;
1696 if (src_value)
1697 *dst++ = 0;
1698 else
1699 *dst++ |= 0xff000000;
1701 dst_row += lockeddata.Stride;
1704 HeapFree(GetProcessHeap(), 0, bits);
1706 else
1708 /* set constant alpha of 255 */
1709 dst_row = lockeddata.Scan0;
1710 for (y=0; y<height; y++)
1712 dst = (DWORD*)dst_row;
1713 for (x=0; x<height; x++)
1714 *dst++ |= 0xff000000;
1715 dst_row += lockeddata.Stride;
1720 DeleteDC(screendc);
1722 DeleteObject(iinfo.hbmColor);
1723 DeleteObject(iinfo.hbmMask);
1725 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1727 return Ok;
1730 static void generate_halftone_palette(ARGB *entries, UINT count)
1732 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1733 UINT i;
1735 for (i=0; i<8 && i<count; i++)
1737 entries[i] = 0xff000000;
1738 if (i&1) entries[i] |= 0x800000;
1739 if (i&2) entries[i] |= 0x8000;
1740 if (i&4) entries[i] |= 0x80;
1743 if (8 < count)
1744 entries[i] = 0xffc0c0c0;
1746 for (i=9; i<16 && i<count; i++)
1748 entries[i] = 0xff000000;
1749 if (i&1) entries[i] |= 0xff0000;
1750 if (i&2) entries[i] |= 0xff00;
1751 if (i&4) entries[i] |= 0xff;
1754 for (i=16; i<40 && i<count; i++)
1756 entries[i] = 0;
1759 for (i=40; i<256 && i<count; i++)
1761 entries[i] = 0xff000000;
1762 entries[i] |= halftone_values[(i-40)%6];
1763 entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1764 entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1768 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1770 HDC screendc = CreateCompatibleDC(0);
1772 if (!screendc) return GenericError;
1774 *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1775 *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1777 DeleteDC(screendc);
1779 return Ok;
1782 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1783 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1785 HBITMAP hbitmap=NULL;
1786 INT row_size, dib_stride;
1787 BYTE *bits=NULL, *own_bits=NULL;
1788 REAL xres, yres;
1789 GpStatus stat;
1791 TRACE("%d %d %d 0x%x %p %p\n", width, height, stride, format, scan0, bitmap);
1793 if (!bitmap) return InvalidParameter;
1795 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1796 *bitmap = NULL;
1797 return InvalidParameter;
1800 if(scan0 && !stride)
1801 return InvalidParameter;
1803 stat = get_screen_resolution(&xres, &yres);
1804 if (stat != Ok) return stat;
1806 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1807 dib_stride = (row_size + 3) & ~3;
1809 if(stride == 0)
1810 stride = dib_stride;
1812 if (format & PixelFormatGDI && !(format & (PixelFormatAlpha|PixelFormatIndexed)) && !scan0)
1814 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
1815 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
1817 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1818 pbmi->bmiHeader.biWidth = width;
1819 pbmi->bmiHeader.biHeight = -height;
1820 pbmi->bmiHeader.biPlanes = 1;
1821 /* FIXME: use the rest of the data from format */
1822 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(format);
1823 pbmi->bmiHeader.biCompression = BI_RGB;
1824 pbmi->bmiHeader.biSizeImage = 0;
1825 pbmi->bmiHeader.biXPelsPerMeter = 0;
1826 pbmi->bmiHeader.biYPelsPerMeter = 0;
1827 pbmi->bmiHeader.biClrUsed = 0;
1828 pbmi->bmiHeader.biClrImportant = 0;
1830 hbitmap = CreateDIBSection(0, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1832 if (!hbitmap) return GenericError;
1834 stride = dib_stride;
1836 else
1838 /* Not a GDI format; don't try to make an HBITMAP. */
1839 if (scan0)
1840 bits = scan0;
1841 else
1843 INT size = abs(stride) * height;
1845 own_bits = bits = GdipAlloc(size);
1846 if (!own_bits) return OutOfMemory;
1848 if (stride < 0)
1849 bits += stride * (1 - height);
1853 *bitmap = GdipAlloc(sizeof(GpBitmap));
1854 if(!*bitmap)
1856 DeleteObject(hbitmap);
1857 GdipFree(own_bits);
1858 return OutOfMemory;
1861 (*bitmap)->image.type = ImageTypeBitmap;
1862 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1863 (*bitmap)->image.flags = ImageFlagsNone;
1864 (*bitmap)->image.frame_count = 1;
1865 (*bitmap)->image.current_frame = 0;
1866 (*bitmap)->image.palette = NULL;
1867 (*bitmap)->image.xres = xres;
1868 (*bitmap)->image.yres = yres;
1869 (*bitmap)->width = width;
1870 (*bitmap)->height = height;
1871 (*bitmap)->format = format;
1872 (*bitmap)->image.picture = NULL;
1873 (*bitmap)->image.stream = NULL;
1874 (*bitmap)->hbitmap = hbitmap;
1875 (*bitmap)->hdc = NULL;
1876 (*bitmap)->bits = bits;
1877 (*bitmap)->stride = stride;
1878 (*bitmap)->own_bits = own_bits;
1879 (*bitmap)->metadata_reader = NULL;
1880 (*bitmap)->prop_count = 0;
1881 (*bitmap)->prop_item = NULL;
1883 /* set format-related flags */
1884 if (format & (PixelFormatAlpha|PixelFormatPAlpha|PixelFormatIndexed))
1885 (*bitmap)->image.flags |= ImageFlagsHasAlpha;
1887 if (format == PixelFormat1bppIndexed ||
1888 format == PixelFormat4bppIndexed ||
1889 format == PixelFormat8bppIndexed)
1891 (*bitmap)->image.palette = GdipAlloc(sizeof(UINT) * 2 + sizeof(ARGB) * (1 << PIXELFORMATBPP(format)));
1893 if (!(*bitmap)->image.palette)
1895 GdipDisposeImage(&(*bitmap)->image);
1896 *bitmap = NULL;
1897 return OutOfMemory;
1900 (*bitmap)->image.palette->Count = 1 << PIXELFORMATBPP(format);
1902 if (format == PixelFormat1bppIndexed)
1904 (*bitmap)->image.palette->Flags = PaletteFlagsGrayScale;
1905 (*bitmap)->image.palette->Entries[0] = 0xff000000;
1906 (*bitmap)->image.palette->Entries[1] = 0xffffffff;
1908 else
1910 if (format == PixelFormat8bppIndexed)
1911 (*bitmap)->image.palette->Flags = PaletteFlagsHalftone;
1913 generate_halftone_palette((*bitmap)->image.palette->Entries,
1914 (*bitmap)->image.palette->Count);
1918 TRACE("<-- %p\n", *bitmap);
1920 return Ok;
1923 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1924 GpBitmap **bitmap)
1926 GpStatus stat;
1928 TRACE("%p %p\n", stream, bitmap);
1930 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1932 if(stat != Ok)
1933 return stat;
1935 if((*bitmap)->image.type != ImageTypeBitmap){
1936 GdipDisposeImage(&(*bitmap)->image);
1937 *bitmap = NULL;
1938 return GenericError; /* FIXME: what error to return? */
1941 return Ok;
1944 /* FIXME: no icm */
1945 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1946 GpBitmap **bitmap)
1948 TRACE("%p %p\n", stream, bitmap);
1950 return GdipCreateBitmapFromStream(stream, bitmap);
1953 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1954 GpCachedBitmap **cachedbmp)
1956 GpStatus stat;
1958 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1960 if(!bitmap || !graphics || !cachedbmp)
1961 return InvalidParameter;
1963 *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
1964 if(!*cachedbmp)
1965 return OutOfMemory;
1967 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1968 if(stat != Ok){
1969 GdipFree(*cachedbmp);
1970 return stat;
1973 return Ok;
1976 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1978 GpStatus stat;
1979 BitmapData lockeddata;
1980 ULONG andstride, xorstride, bitssize;
1981 LPBYTE andbits, xorbits, androw, xorrow, srcrow;
1982 UINT x, y;
1984 TRACE("(%p, %p)\n", bitmap, hicon);
1986 if (!bitmap || !hicon)
1987 return InvalidParameter;
1989 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead,
1990 PixelFormat32bppPARGB, &lockeddata);
1991 if (stat == Ok)
1993 andstride = ((lockeddata.Width+31)/32)*4;
1994 xorstride = lockeddata.Width*4;
1995 bitssize = (andstride + xorstride) * lockeddata.Height;
1997 andbits = GdipAlloc(bitssize);
1999 if (andbits)
2001 xorbits = andbits + andstride * lockeddata.Height;
2003 for (y=0; y<lockeddata.Height; y++)
2005 srcrow = ((LPBYTE)lockeddata.Scan0) + lockeddata.Stride * y;
2007 androw = andbits + andstride * y;
2008 for (x=0; x<lockeddata.Width; x++)
2009 if (srcrow[3+4*x] >= 128)
2010 androw[x/8] |= 1 << (7-x%8);
2012 xorrow = xorbits + xorstride * y;
2013 memcpy(xorrow, srcrow, xorstride);
2016 *hicon = CreateIcon(NULL, lockeddata.Width, lockeddata.Height, 1, 32,
2017 andbits, xorbits);
2019 GdipFree(andbits);
2021 else
2022 stat = OutOfMemory;
2024 GdipBitmapUnlockBits(bitmap, &lockeddata);
2027 return stat;
2030 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
2032 TRACE("%p\n", cachedbmp);
2034 if(!cachedbmp)
2035 return InvalidParameter;
2037 GdipDisposeImage(cachedbmp->image);
2038 GdipFree(cachedbmp);
2040 return Ok;
2043 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
2044 GpCachedBitmap *cachedbmp, INT x, INT y)
2046 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
2048 if(!graphics || !cachedbmp)
2049 return InvalidParameter;
2051 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
2054 /* Internal utility function: Replace the image data of dst with that of src,
2055 * and free src. */
2056 static void move_bitmap(GpBitmap *dst, GpBitmap *src, BOOL clobber_palette)
2058 assert(src->image.type == ImageTypeBitmap);
2059 assert(dst->image.type == ImageTypeBitmap);
2061 GdipFree(dst->bitmapbits);
2062 GdipFree(dst->own_bits);
2063 DeleteDC(dst->hdc);
2064 DeleteObject(dst->hbitmap);
2066 if (clobber_palette)
2068 GdipFree(dst->image.palette);
2069 dst->image.palette = src->image.palette;
2071 else
2072 GdipFree(src->image.palette);
2074 dst->image.xres = src->image.xres;
2075 dst->image.yres = src->image.yres;
2076 dst->width = src->width;
2077 dst->height = src->height;
2078 dst->format = src->format;
2079 dst->hbitmap = src->hbitmap;
2080 dst->hdc = src->hdc;
2081 dst->bits = src->bits;
2082 dst->stride = src->stride;
2083 dst->own_bits = src->own_bits;
2084 if (dst->metadata_reader)
2085 IWICMetadataReader_Release(dst->metadata_reader);
2086 dst->metadata_reader = src->metadata_reader;
2087 GdipFree(dst->prop_item);
2088 dst->prop_item = src->prop_item;
2089 dst->prop_count = src->prop_count;
2090 if (dst->image.stream)
2091 IStream_Release(dst->image.stream);
2092 dst->image.stream = src->image.stream;
2093 dst->image.frame_count = src->image.frame_count;
2094 dst->image.current_frame = src->image.current_frame;
2095 dst->image.format = src->image.format;
2097 src->image.type = ~0;
2098 GdipFree(src);
2101 static GpStatus free_image_data(GpImage *image)
2103 if(!image)
2104 return InvalidParameter;
2106 if (image->type == ImageTypeBitmap)
2108 GdipFree(((GpBitmap*)image)->bitmapbits);
2109 GdipFree(((GpBitmap*)image)->own_bits);
2110 DeleteDC(((GpBitmap*)image)->hdc);
2111 DeleteObject(((GpBitmap*)image)->hbitmap);
2112 if (((GpBitmap*)image)->metadata_reader)
2113 IWICMetadataReader_Release(((GpBitmap*)image)->metadata_reader);
2114 GdipFree(((GpBitmap*)image)->prop_item);
2116 else if (image->type == ImageTypeMetafile)
2118 GpMetafile *metafile = (GpMetafile*)image;
2119 GdipFree(metafile->comment_data);
2120 DeleteEnhMetaFile(CloseEnhMetaFile(metafile->record_dc));
2121 if (!metafile->preserve_hemf)
2122 DeleteEnhMetaFile(metafile->hemf);
2123 if (metafile->record_graphics)
2125 WARN("metafile closed while recording\n");
2126 /* not sure what to do here; for now just prevent the graphics from functioning or using this object */
2127 metafile->record_graphics->image = NULL;
2128 metafile->record_graphics->busy = TRUE;
2131 else
2133 WARN("invalid image: %p\n", image);
2134 return ObjectBusy;
2136 if (image->picture)
2137 IPicture_Release(image->picture);
2138 if (image->stream)
2139 IStream_Release(image->stream);
2140 GdipFree(image->palette);
2142 return Ok;
2145 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
2147 GpStatus status;
2149 TRACE("%p\n", image);
2151 status = free_image_data(image);
2152 if (status != Ok) return status;
2153 image->type = ~0;
2154 GdipFree(image);
2156 return Ok;
2159 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
2161 static int calls;
2163 TRACE("(%p,%p)\n", image, item);
2165 if(!image || !item)
2166 return InvalidParameter;
2168 if (!(calls++))
2169 FIXME("not implemented\n");
2171 return NotImplemented;
2174 GpStatus WINGDIPAPI GdipGetImageItemData(GpImage *image, ImageItemData *item)
2176 static int calls;
2178 TRACE("(%p,%p)\n", image, item);
2180 if (!(calls++))
2181 FIXME("not implemented\n");
2183 return NotImplemented;
2186 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
2187 GpUnit *srcUnit)
2189 TRACE("%p %p %p\n", image, srcRect, srcUnit);
2191 if(!image || !srcRect || !srcUnit)
2192 return InvalidParameter;
2193 if(image->type == ImageTypeMetafile){
2194 *srcRect = ((GpMetafile*)image)->bounds;
2195 *srcUnit = ((GpMetafile*)image)->unit;
2197 else if(image->type == ImageTypeBitmap){
2198 srcRect->X = srcRect->Y = 0.0;
2199 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
2200 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
2201 *srcUnit = UnitPixel;
2203 else{
2204 srcRect->X = srcRect->Y = 0.0;
2205 srcRect->Width = ipicture_pixel_width(image->picture);
2206 srcRect->Height = ipicture_pixel_height(image->picture);
2207 *srcUnit = UnitPixel;
2210 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
2211 srcRect->Width, srcRect->Height, *srcUnit);
2213 return Ok;
2216 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
2217 REAL *height)
2219 TRACE("%p %p %p\n", image, width, height);
2221 if(!image || !height || !width)
2222 return InvalidParameter;
2224 if(image->type == ImageTypeMetafile){
2225 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2226 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2228 else if(image->type == ImageTypeBitmap){
2229 *height = ((GpBitmap*)image)->height;
2230 *width = ((GpBitmap*)image)->width;
2232 else{
2233 *height = ipicture_pixel_height(image->picture);
2234 *width = ipicture_pixel_width(image->picture);
2237 TRACE("returning (%f, %f)\n", *height, *width);
2238 return Ok;
2241 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
2242 GpGraphics **graphics)
2244 HDC hdc;
2245 GpStatus stat;
2247 TRACE("%p %p\n", image, graphics);
2249 if(!image || !graphics)
2250 return InvalidParameter;
2252 if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
2254 hdc = ((GpBitmap*)image)->hdc;
2256 if(!hdc){
2257 hdc = CreateCompatibleDC(0);
2258 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
2259 ((GpBitmap*)image)->hdc = hdc;
2262 stat = GdipCreateFromHDC(hdc, graphics);
2264 if (stat == Ok)
2266 (*graphics)->image = image;
2267 (*graphics)->xres = image->xres;
2268 (*graphics)->yres = image->yres;
2271 else if (image->type == ImageTypeMetafile)
2272 stat = METAFILE_GetGraphicsContext((GpMetafile*)image, graphics);
2273 else
2274 stat = graphics_from_image(image, graphics);
2276 return stat;
2279 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
2281 TRACE("%p %p\n", image, height);
2283 if(!image || !height)
2284 return InvalidParameter;
2286 if(image->type == ImageTypeMetafile)
2287 *height = units_to_pixels(((GpMetafile*)image)->bounds.Height, ((GpMetafile*)image)->unit, image->yres);
2288 else if(image->type == ImageTypeBitmap)
2289 *height = ((GpBitmap*)image)->height;
2290 else
2291 *height = ipicture_pixel_height(image->picture);
2293 TRACE("returning %d\n", *height);
2295 return Ok;
2298 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2300 if(!image || !res)
2301 return InvalidParameter;
2303 *res = image->xres;
2305 TRACE("(%p) <-- %0.2f\n", image, *res);
2307 return Ok;
2310 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2312 TRACE("%p %p\n", image, size);
2314 if(!image || !size)
2315 return InvalidParameter;
2317 if (!image->palette || image->palette->Count == 0)
2318 *size = sizeof(ColorPalette);
2319 else
2320 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette->Count;
2322 TRACE("<-- %u\n", *size);
2324 return Ok;
2327 /* FIXME: test this function for non-bitmap types */
2328 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2330 TRACE("%p %p\n", image, format);
2332 if(!image || !format)
2333 return InvalidParameter;
2335 if(image->type != ImageTypeBitmap)
2336 *format = PixelFormat24bppRGB;
2337 else
2338 *format = ((GpBitmap*) image)->format;
2340 return Ok;
2343 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2345 TRACE("(%p, %p)\n", image, format);
2347 if(!image || !format)
2348 return InvalidParameter;
2350 memcpy(format, &image->format, sizeof(GUID));
2352 return Ok;
2355 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2357 TRACE("%p %p\n", image, type);
2359 if(!image || !type)
2360 return InvalidParameter;
2362 *type = image->type;
2364 return Ok;
2367 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2369 if(!image || !res)
2370 return InvalidParameter;
2372 *res = image->yres;
2374 TRACE("(%p) <-- %0.2f\n", image, *res);
2376 return Ok;
2379 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2381 TRACE("%p %p\n", image, width);
2383 if(!image || !width)
2384 return InvalidParameter;
2386 if(image->type == ImageTypeMetafile)
2387 *width = units_to_pixels(((GpMetafile*)image)->bounds.Width, ((GpMetafile*)image)->unit, image->xres);
2388 else if(image->type == ImageTypeBitmap)
2389 *width = ((GpBitmap*)image)->width;
2390 else
2391 *width = ipicture_pixel_width(image->picture);
2393 TRACE("returning %d\n", *width);
2395 return Ok;
2398 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT *num)
2400 TRACE("(%p, %p)\n", image, num);
2402 if (!image || !num) return InvalidParameter;
2404 *num = 0;
2406 if (image->type == ImageTypeBitmap)
2408 if (((GpBitmap *)image)->prop_item)
2410 *num = ((GpBitmap *)image)->prop_count;
2411 return Ok;
2414 if (((GpBitmap *)image)->metadata_reader)
2415 IWICMetadataReader_GetCount(((GpBitmap *)image)->metadata_reader, num);
2418 return Ok;
2421 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID *list)
2423 HRESULT hr;
2424 IWICMetadataReader *reader;
2425 IWICEnumMetadataItem *enumerator;
2426 UINT prop_count, i, items_returned;
2428 TRACE("(%p, %u, %p)\n", image, num, list);
2430 if (!image || !list) return InvalidParameter;
2432 if (image->type != ImageTypeBitmap)
2434 FIXME("Not implemented for type %d\n", image->type);
2435 return NotImplemented;
2438 if (((GpBitmap *)image)->prop_item)
2440 if (num != ((GpBitmap *)image)->prop_count) return InvalidParameter;
2442 for (i = 0; i < num; i++)
2444 list[i] = ((GpBitmap *)image)->prop_item[i].id;
2447 return Ok;
2450 reader = ((GpBitmap *)image)->metadata_reader;
2451 if (!reader)
2453 if (num != 0) return InvalidParameter;
2454 return Ok;
2457 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2458 if (FAILED(hr)) return hresult_to_status(hr);
2460 if (num != prop_count) return InvalidParameter;
2462 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2463 if (FAILED(hr)) return hresult_to_status(hr);
2465 IWICEnumMetadataItem_Reset(enumerator);
2467 for (i = 0; i < num; i++)
2469 PROPVARIANT id;
2471 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, NULL, &items_returned);
2472 if (hr != S_OK) break;
2474 if (id.vt != VT_UI2)
2476 FIXME("not supported propvariant type for id: %u\n", id.vt);
2477 list[i] = 0;
2478 continue;
2480 list[i] = id.u.uiVal;
2483 IWICEnumMetadataItem_Release(enumerator);
2485 return hr == S_OK ? Ok : hresult_to_status(hr);
2488 static UINT propvariant_size(PROPVARIANT *value)
2490 switch (value->vt & ~VT_VECTOR)
2492 case VT_EMPTY:
2493 return 0;
2494 case VT_I1:
2495 case VT_UI1:
2496 if (!(value->vt & VT_VECTOR)) return 1;
2497 return value->u.caub.cElems;
2498 case VT_I2:
2499 case VT_UI2:
2500 if (!(value->vt & VT_VECTOR)) return 2;
2501 return value->u.caui.cElems * 2;
2502 case VT_I4:
2503 case VT_UI4:
2504 case VT_R4:
2505 if (!(value->vt & VT_VECTOR)) return 4;
2506 return value->u.caul.cElems * 4;
2507 case VT_I8:
2508 case VT_UI8:
2509 case VT_R8:
2510 if (!(value->vt & VT_VECTOR)) return 8;
2511 return value->u.cauh.cElems * 8;
2512 case VT_LPSTR:
2513 return value->u.pszVal ? strlen(value->u.pszVal) + 1 : 0;
2514 case VT_BLOB:
2515 return value->u.blob.cbSize;
2516 default:
2517 FIXME("not supported variant type %d\n", value->vt);
2518 return 0;
2522 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID propid, UINT *size)
2524 HRESULT hr;
2525 IWICMetadataReader *reader;
2526 PROPVARIANT id, value;
2528 TRACE("(%p,%#x,%p)\n", image, propid, size);
2530 if (!size || !image) return InvalidParameter;
2532 if (image->type != ImageTypeBitmap)
2534 FIXME("Not implemented for type %d\n", image->type);
2535 return NotImplemented;
2538 if (((GpBitmap *)image)->prop_item)
2540 UINT i;
2542 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2544 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2546 *size = sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2547 return Ok;
2551 return PropertyNotFound;
2554 reader = ((GpBitmap *)image)->metadata_reader;
2555 if (!reader) return PropertyNotFound;
2557 id.vt = VT_UI2;
2558 id.u.uiVal = propid;
2559 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2560 if (FAILED(hr)) return PropertyNotFound;
2562 *size = propvariant_size(&value);
2563 if (*size) *size += sizeof(PropertyItem);
2564 PropVariantClear(&value);
2566 return Ok;
2569 #ifndef PropertyTagTypeSByte
2570 #define PropertyTagTypeSByte 6
2571 #define PropertyTagTypeSShort 8
2572 #define PropertyTagTypeFloat 11
2573 #define PropertyTagTypeDouble 12
2574 #endif
2576 static UINT vt_to_itemtype(UINT vt)
2578 static const struct
2580 UINT vt, type;
2581 } vt2type[] =
2583 { VT_I1, PropertyTagTypeSByte },
2584 { VT_UI1, PropertyTagTypeByte },
2585 { VT_I2, PropertyTagTypeSShort },
2586 { VT_UI2, PropertyTagTypeShort },
2587 { VT_I4, PropertyTagTypeSLONG },
2588 { VT_UI4, PropertyTagTypeLong },
2589 { VT_I8, PropertyTagTypeSRational },
2590 { VT_UI8, PropertyTagTypeRational },
2591 { VT_R4, PropertyTagTypeFloat },
2592 { VT_R8, PropertyTagTypeDouble },
2593 { VT_LPSTR, PropertyTagTypeASCII },
2594 { VT_BLOB, PropertyTagTypeUndefined }
2596 UINT i;
2597 for (i = 0; i < sizeof(vt2type)/sizeof(vt2type[0]); i++)
2599 if (vt2type[i].vt == vt) return vt2type[i].type;
2601 FIXME("not supported variant type %u\n", vt);
2602 return 0;
2605 static GpStatus propvariant_to_item(PROPVARIANT *value, PropertyItem *item,
2606 UINT size, PROPID id)
2608 UINT item_size, item_type;
2610 item_size = propvariant_size(value);
2611 if (size != item_size + sizeof(PropertyItem)) return InvalidParameter;
2613 item_type = vt_to_itemtype(value->vt & ~VT_VECTOR);
2614 if (!item_type) return InvalidParameter;
2616 item->value = item + 1;
2618 switch (value->vt & ~VT_VECTOR)
2620 case VT_I1:
2621 case VT_UI1:
2622 if (!(value->vt & VT_VECTOR))
2623 *(BYTE *)item->value = value->u.bVal;
2624 else
2625 memcpy(item->value, value->u.caub.pElems, item_size);
2626 break;
2627 case VT_I2:
2628 case VT_UI2:
2629 if (!(value->vt & VT_VECTOR))
2630 *(USHORT *)item->value = value->u.uiVal;
2631 else
2632 memcpy(item->value, value->u.caui.pElems, item_size);
2633 break;
2634 case VT_I4:
2635 case VT_UI4:
2636 case VT_R4:
2637 if (!(value->vt & VT_VECTOR))
2638 *(ULONG *)item->value = value->u.ulVal;
2639 else
2640 memcpy(item->value, value->u.caul.pElems, item_size);
2641 break;
2642 case VT_I8:
2643 case VT_UI8:
2644 case VT_R8:
2645 if (!(value->vt & VT_VECTOR))
2646 *(ULONGLONG *)item->value = value->u.uhVal.QuadPart;
2647 else
2648 memcpy(item->value, value->u.cauh.pElems, item_size);
2649 break;
2650 case VT_LPSTR:
2651 memcpy(item->value, value->u.pszVal, item_size);
2652 break;
2653 case VT_BLOB:
2654 memcpy(item->value, value->u.blob.pBlobData, item_size);
2655 break;
2656 default:
2657 FIXME("not supported variant type %d\n", value->vt);
2658 return InvalidParameter;
2661 item->length = item_size;
2662 item->type = item_type;
2663 item->id = id;
2665 return Ok;
2668 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID propid, UINT size,
2669 PropertyItem *buffer)
2671 GpStatus stat;
2672 HRESULT hr;
2673 IWICMetadataReader *reader;
2674 PROPVARIANT id, value;
2676 TRACE("(%p,%#x,%u,%p)\n", image, propid, size, buffer);
2678 if (!image || !buffer) return InvalidParameter;
2680 if (image->type != ImageTypeBitmap)
2682 FIXME("Not implemented for type %d\n", image->type);
2683 return NotImplemented;
2686 if (((GpBitmap *)image)->prop_item)
2688 UINT i;
2690 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2692 if (propid == ((GpBitmap *)image)->prop_item[i].id)
2694 if (size != sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length)
2695 return InvalidParameter;
2697 *buffer = ((GpBitmap *)image)->prop_item[i];
2698 buffer->value = buffer + 1;
2699 memcpy(buffer->value, ((GpBitmap *)image)->prop_item[i].value, buffer->length);
2700 return Ok;
2704 return PropertyNotFound;
2707 reader = ((GpBitmap *)image)->metadata_reader;
2708 if (!reader) return PropertyNotFound;
2710 id.vt = VT_UI2;
2711 id.u.uiVal = propid;
2712 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2713 if (FAILED(hr)) return PropertyNotFound;
2715 stat = propvariant_to_item(&value, buffer, size, propid);
2716 PropVariantClear(&value);
2718 return stat;
2721 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT *size, UINT *count)
2723 HRESULT hr;
2724 IWICMetadataReader *reader;
2725 IWICEnumMetadataItem *enumerator;
2726 UINT prop_count, prop_size, i;
2727 PROPVARIANT id, value;
2729 TRACE("(%p,%p,%p)\n", image, size, count);
2731 if (!image || !size || !count) return InvalidParameter;
2733 if (image->type != ImageTypeBitmap)
2735 FIXME("Not implemented for type %d\n", image->type);
2736 return NotImplemented;
2739 if (((GpBitmap *)image)->prop_item)
2741 *count = ((GpBitmap *)image)->prop_count;
2742 *size = 0;
2744 for (i = 0; i < ((GpBitmap *)image)->prop_count; i++)
2746 *size += sizeof(PropertyItem) + ((GpBitmap *)image)->prop_item[i].length;
2749 return Ok;
2752 reader = ((GpBitmap *)image)->metadata_reader;
2753 if (!reader) return PropertyNotFound;
2755 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2756 if (FAILED(hr)) return hresult_to_status(hr);
2758 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2759 if (FAILED(hr)) return hresult_to_status(hr);
2761 IWICEnumMetadataItem_Reset(enumerator);
2763 prop_size = 0;
2765 PropVariantInit(&id);
2766 PropVariantInit(&value);
2768 for (i = 0; i < prop_count; i++)
2770 UINT items_returned, item_size;
2772 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2773 if (hr != S_OK) break;
2775 item_size = propvariant_size(&value);
2776 if (item_size) prop_size += sizeof(PropertyItem) + item_size;
2778 PropVariantClear(&id);
2779 PropVariantClear(&value);
2782 IWICEnumMetadataItem_Release(enumerator);
2784 if (hr != S_OK) return PropertyNotFound;
2786 *count = prop_count;
2787 *size = prop_size;
2788 return Ok;
2791 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2792 UINT count, PropertyItem *buf)
2794 GpStatus status;
2795 HRESULT hr;
2796 IWICMetadataReader *reader;
2797 IWICEnumMetadataItem *enumerator;
2798 UINT prop_count, prop_size, i;
2799 PROPVARIANT id, value;
2800 char *item_value;
2802 TRACE("(%p,%u,%u,%p)\n", image, size, count, buf);
2804 if (!image || !buf) return InvalidParameter;
2806 if (image->type != ImageTypeBitmap)
2808 FIXME("Not implemented for type %d\n", image->type);
2809 return NotImplemented;
2812 status = GdipGetPropertySize(image, &prop_size, &prop_count);
2813 if (status != Ok) return status;
2815 if (prop_count != count || prop_size != size) return InvalidParameter;
2817 if (((GpBitmap *)image)->prop_item)
2819 memcpy(buf, ((GpBitmap *)image)->prop_item, prop_size);
2821 item_value = (char *)(buf + prop_count);
2823 for (i = 0; i < prop_count; i++)
2825 buf[i].value = item_value;
2826 item_value += buf[i].length;
2829 return Ok;
2832 reader = ((GpBitmap *)image)->metadata_reader;
2833 if (!reader) return PropertyNotFound;
2835 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2836 if (FAILED(hr)) return hresult_to_status(hr);
2838 IWICEnumMetadataItem_Reset(enumerator);
2840 item_value = (char *)(buf + prop_count);
2842 PropVariantInit(&id);
2843 PropVariantInit(&value);
2845 for (i = 0; i < prop_count; i++)
2847 PropertyItem *item;
2848 UINT items_returned, item_size;
2850 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2851 if (hr != S_OK) break;
2853 if (id.vt != VT_UI2)
2855 FIXME("not supported propvariant type for id: %u\n", id.vt);
2856 continue;
2859 item_size = propvariant_size(&value);
2860 if (item_size)
2862 item = HeapAlloc(GetProcessHeap(), 0, item_size + sizeof(*item));
2864 propvariant_to_item(&value, item, item_size + sizeof(*item), id.u.uiVal);
2865 buf[i].id = item->id;
2866 buf[i].type = item->type;
2867 buf[i].length = item_size;
2868 buf[i].value = item_value;
2869 memcpy(item_value, item->value, item_size);
2870 item_value += item_size;
2872 HeapFree(GetProcessHeap(), 0, item);
2875 PropVariantClear(&id);
2876 PropVariantClear(&value);
2879 IWICEnumMetadataItem_Release(enumerator);
2881 if (hr != S_OK) return PropertyNotFound;
2883 return Ok;
2886 struct image_format_dimension
2888 const GUID *format;
2889 const GUID *dimension;
2892 static const struct image_format_dimension image_format_dimensions[] =
2894 {&ImageFormatGIF, &FrameDimensionTime},
2895 {&ImageFormatIcon, &FrameDimensionResolution},
2896 {NULL}
2899 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
2900 GDIPCONST GUID* dimensionID, UINT* count)
2902 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
2904 if(!image || !count)
2905 return InvalidParameter;
2907 if (!dimensionID ||
2908 IsEqualGUID(dimensionID, &image->format) ||
2909 IsEqualGUID(dimensionID, &FrameDimensionPage) ||
2910 IsEqualGUID(dimensionID, &FrameDimensionTime))
2912 *count = image->frame_count;
2913 return Ok;
2916 return InvalidParameter;
2919 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
2920 UINT* count)
2922 TRACE("(%p, %p)\n", image, count);
2924 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2926 if(!image || !count)
2927 return InvalidParameter;
2929 *count = 1;
2931 return Ok;
2934 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
2935 GUID* dimensionIDs, UINT count)
2937 int i;
2938 const GUID *result=NULL;
2940 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
2942 if(!image || !dimensionIDs || count != 1)
2943 return InvalidParameter;
2945 for (i=0; image_format_dimensions[i].format; i++)
2947 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
2949 result = image_format_dimensions[i].dimension;
2950 break;
2954 if (!result)
2955 result = &FrameDimensionPage;
2957 memcpy(dimensionIDs, result, sizeof(GUID));
2959 return Ok;
2962 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
2963 GpImage **image)
2965 GpStatus stat;
2966 IStream *stream;
2968 TRACE("(%s) %p\n", debugstr_w(filename), image);
2970 if (!filename || !image)
2971 return InvalidParameter;
2973 *image = NULL;
2975 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
2977 if (stat != Ok)
2978 return stat;
2980 stat = GdipLoadImageFromStream(stream, image);
2982 IStream_Release(stream);
2984 return stat;
2987 /* FIXME: no icm handling */
2988 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
2990 TRACE("(%s) %p\n", debugstr_w(filename), image);
2992 return GdipLoadImageFromFile(filename, image);
2995 static void add_property(GpBitmap *bitmap, PropertyItem *item)
2997 UINT prop_size, prop_count;
2998 PropertyItem *prop_item;
3000 if (bitmap->prop_item == NULL)
3002 prop_size = prop_count = 0;
3003 prop_item = GdipAlloc(item->length + sizeof(PropertyItem));
3004 if (!prop_item) return;
3006 else
3008 UINT i;
3009 char *item_value;
3011 GdipGetPropertySize((GpImage *)bitmap, &prop_size, &prop_count);
3013 prop_item = GdipAlloc(prop_size + item->length + sizeof(PropertyItem));
3014 if (!prop_item) return;
3015 memcpy(prop_item, bitmap->prop_item, sizeof(PropertyItem) * bitmap->prop_count);
3016 prop_size -= sizeof(PropertyItem) * bitmap->prop_count;
3017 memcpy(prop_item + prop_count + 1, bitmap->prop_item + prop_count, prop_size);
3019 item_value = (char *)(prop_item + prop_count + 1);
3021 for (i = 0; i < prop_count; i++)
3023 prop_item[i].value = item_value;
3024 item_value += prop_item[i].length;
3028 prop_item[prop_count].id = item->id;
3029 prop_item[prop_count].type = item->type;
3030 prop_item[prop_count].length = item->length;
3031 prop_item[prop_count].value = (char *)(prop_item + prop_count + 1) + prop_size;
3032 memcpy(prop_item[prop_count].value, item->value, item->length);
3034 GdipFree(bitmap->prop_item);
3035 bitmap->prop_item = prop_item;
3036 bitmap->prop_count++;
3039 static BOOL get_bool_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3041 HRESULT hr;
3042 GUID format;
3043 PROPVARIANT id, value;
3044 BOOL ret = FALSE;
3046 IWICMetadataReader_GetMetadataFormat(reader, &format);
3047 if (!IsEqualGUID(&format, guid)) return FALSE;
3049 PropVariantInit(&id);
3050 PropVariantInit(&value);
3052 id.vt = VT_LPWSTR;
3053 id.u.pwszVal = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3054 if (!id.u.pwszVal) return FALSE;
3055 lstrcpyW(id.u.pwszVal, prop_name);
3056 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3057 if (hr == S_OK && value.vt == VT_BOOL)
3058 ret = value.u.boolVal;
3060 PropVariantClear(&id);
3061 PropVariantClear(&value);
3063 return ret;
3066 static PropertyItem *get_property(IWICMetadataReader *reader, const GUID *guid, const WCHAR *prop_name)
3068 HRESULT hr;
3069 GUID format;
3070 PROPVARIANT id, value;
3071 PropertyItem *item = NULL;
3073 IWICMetadataReader_GetMetadataFormat(reader, &format);
3074 if (!IsEqualGUID(&format, guid)) return NULL;
3076 PropVariantInit(&id);
3077 PropVariantInit(&value);
3079 id.vt = VT_LPWSTR;
3080 id.u.pwszVal = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(prop_name) + 1) * sizeof(WCHAR));
3081 if (!id.u.pwszVal) return NULL;
3082 lstrcpyW(id.u.pwszVal, prop_name);
3083 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
3084 if (hr == S_OK)
3086 UINT item_size = propvariant_size(&value);
3087 if (item_size)
3089 item_size += sizeof(*item);
3090 item = GdipAlloc(item_size);
3091 if (propvariant_to_item(&value, item, item_size, 0) != Ok)
3093 GdipFree(item);
3094 item = NULL;
3099 PropVariantClear(&id);
3100 PropVariantClear(&value);
3102 return item;
3105 static PropertyItem *get_gif_comment(IWICMetadataReader *reader)
3107 static const WCHAR textentryW[] = { 'T','e','x','t','E','n','t','r','y',0 };
3108 PropertyItem *comment;
3110 comment = get_property(reader, &GUID_MetadataFormatGifComment, textentryW);
3111 if (comment)
3112 comment->id = PropertyTagExifUserComment;
3114 return comment;
3117 static PropertyItem *get_gif_loopcount(IWICMetadataReader *reader)
3119 static const WCHAR applicationW[] = { 'A','p','p','l','i','c','a','t','i','o','n',0 };
3120 static const WCHAR dataW[] = { 'D','a','t','a',0 };
3121 PropertyItem *appext = NULL, *appdata = NULL, *loop = NULL;
3123 appext = get_property(reader, &GUID_MetadataFormatAPE, applicationW);
3124 if (appext)
3126 if (appext->type == PropertyTagTypeByte && appext->length == 11 &&
3127 (!memcmp(appext->value, "NETSCAPE2.0", 11) || !memcmp(appext->value, "ANIMEXTS1.0", 11)))
3129 appdata = get_property(reader, &GUID_MetadataFormatAPE, dataW);
3130 if (appdata)
3132 if (appdata->type == PropertyTagTypeByte && appdata->length == 4)
3134 BYTE *data = appdata->value;
3135 if (data[0] == 3 && data[1] == 1)
3137 loop = GdipAlloc(sizeof(*loop) + sizeof(SHORT));
3138 if (loop)
3140 loop->type = PropertyTagTypeShort;
3141 loop->id = PropertyTagLoopCount;
3142 loop->length = sizeof(SHORT);
3143 loop->value = loop + 1;
3144 *(SHORT *)loop->value = data[2] | (data[3] << 8);
3152 GdipFree(appext);
3153 GdipFree(appdata);
3155 return loop;
3158 static PropertyItem *get_gif_background(IWICMetadataReader *reader)
3160 static const WCHAR backgroundW[] = { 'B','a','c','k','g','r','o','u','n','d','C','o','l','o','r','I','n','d','e','x',0 };
3161 PropertyItem *background;
3163 background = get_property(reader, &GUID_MetadataFormatLSD, backgroundW);
3164 if (background)
3165 background->id = PropertyTagIndexBackground;
3167 return background;
3170 static PropertyItem *get_gif_palette(IWICBitmapDecoder *decoder, IWICMetadataReader *reader)
3172 static const WCHAR global_flagW[] = { 'G','l','o','b','a','l','C','o','l','o','r','T','a','b','l','e','F','l','a','g',0 };
3173 HRESULT hr;
3174 IWICImagingFactory *factory;
3175 IWICPalette *palette;
3176 UINT count = 0;
3177 WICColor colors[256];
3179 if (!get_bool_property(reader, &GUID_MetadataFormatLSD, global_flagW))
3180 return NULL;
3182 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
3183 &IID_IWICImagingFactory, (void **)&factory);
3184 if (hr != S_OK) return NULL;
3186 hr = IWICImagingFactory_CreatePalette(factory, &palette);
3187 if (hr == S_OK)
3189 hr = IWICBitmapDecoder_CopyPalette(decoder, palette);
3190 if (hr == S_OK)
3191 IWICPalette_GetColors(palette, 256, colors, &count);
3193 IWICPalette_Release(palette);
3196 IWICImagingFactory_Release(factory);
3198 if (count)
3200 PropertyItem *pal;
3201 UINT i;
3202 BYTE *rgb;
3204 pal = GdipAlloc(sizeof(*pal) + count * 3);
3205 if (!pal) return NULL;
3206 pal->type = PropertyTagTypeByte;
3207 pal->id = PropertyTagGlobalPalette;
3208 pal->value = pal + 1;
3209 pal->length = count * 3;
3211 rgb = pal->value;
3213 for (i = 0; i < count; i++)
3215 rgb[i*3] = (colors[i] >> 16) & 0xff;
3216 rgb[i*3 + 1] = (colors[i] >> 8) & 0xff;
3217 rgb[i*3 + 2] = colors[i] & 0xff;
3220 return pal;
3223 return NULL;
3226 static PropertyItem *get_gif_transparent_idx(IWICMetadataReader *reader)
3228 static const WCHAR transparency_flagW[] = { 'T','r','a','n','s','p','a','r','e','n','c','y','F','l','a','g',0 };
3229 static const WCHAR colorW[] = { 'T','r','a','n','s','p','a','r','e','n','t','C','o','l','o','r','I','n','d','e','x',0 };
3230 PropertyItem *index = NULL;
3232 if (get_bool_property(reader, &GUID_MetadataFormatGCE, transparency_flagW))
3234 index = get_property(reader, &GUID_MetadataFormatGCE, colorW);
3235 if (index)
3236 index->id = PropertyTagIndexTransparent;
3238 return index;
3241 static LONG get_gif_frame_delay(IWICBitmapFrameDecode *frame)
3243 static const WCHAR delayW[] = { 'D','e','l','a','y',0 };
3244 HRESULT hr;
3245 IWICMetadataBlockReader *block_reader;
3246 IWICMetadataReader *reader;
3247 UINT block_count, i;
3248 PropertyItem *delay;
3249 LONG value = 0;
3251 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3252 if (hr == S_OK)
3254 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3255 if (hr == S_OK)
3257 for (i = 0; i < block_count; i++)
3259 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3260 if (hr == S_OK)
3262 delay = get_property(reader, &GUID_MetadataFormatGCE, delayW);
3263 if (delay)
3265 if (delay->type == PropertyTagTypeShort && delay->length == 2)
3266 value = *(SHORT *)delay->value;
3268 GdipFree(delay);
3270 IWICMetadataReader_Release(reader);
3274 IWICMetadataBlockReader_Release(block_reader);
3277 return value;
3280 static void gif_metadata_reader(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT active_frame)
3282 HRESULT hr;
3283 IWICBitmapFrameDecode *frame;
3284 IWICMetadataBlockReader *block_reader;
3285 IWICMetadataReader *reader;
3286 UINT frame_count, block_count, i;
3287 PropertyItem *delay = NULL, *comment = NULL, *background = NULL;
3288 PropertyItem *transparent_idx = NULL, *loop = NULL, *palette = NULL;
3290 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3291 if (frame_count > 1)
3293 delay = GdipAlloc(sizeof(*delay) + frame_count * sizeof(LONG));
3294 if (delay)
3296 LONG *value;
3298 delay->type = PropertyTagTypeLong;
3299 delay->id = PropertyTagFrameDelay;
3300 delay->length = frame_count * sizeof(LONG);
3301 delay->value = delay + 1;
3303 value = delay->value;
3305 for (i = 0; i < frame_count; i++)
3307 hr = IWICBitmapDecoder_GetFrame(decoder, i, &frame);
3308 if (hr == S_OK)
3310 value[i] = get_gif_frame_delay(frame);
3311 IWICBitmapFrameDecode_Release(frame);
3313 else value[i] = 0;
3318 hr = IWICBitmapDecoder_QueryInterface(decoder, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3319 if (hr == S_OK)
3321 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3322 if (hr == S_OK)
3324 for (i = 0; i < block_count; i++)
3326 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3327 if (hr == S_OK)
3329 if (!comment)
3330 comment = get_gif_comment(reader);
3332 if (frame_count > 1 && !loop)
3333 loop = get_gif_loopcount(reader);
3335 if (!background)
3336 background = get_gif_background(reader);
3338 if (!palette)
3339 palette = get_gif_palette(decoder, reader);
3341 IWICMetadataReader_Release(reader);
3345 IWICMetadataBlockReader_Release(block_reader);
3348 if (frame_count > 1 && !loop)
3350 loop = GdipAlloc(sizeof(*loop) + sizeof(SHORT));
3351 if (loop)
3353 loop->type = PropertyTagTypeShort;
3354 loop->id = PropertyTagLoopCount;
3355 loop->length = sizeof(SHORT);
3356 loop->value = loop + 1;
3357 *(SHORT *)loop->value = 1;
3361 if (delay) add_property(bitmap, delay);
3362 if (comment) add_property(bitmap, comment);
3363 if (loop) add_property(bitmap, loop);
3364 if (palette) add_property(bitmap, palette);
3365 if (background) add_property(bitmap, background);
3367 GdipFree(delay);
3368 GdipFree(comment);
3369 GdipFree(loop);
3370 GdipFree(palette);
3371 GdipFree(background);
3373 /* Win7 gdiplus always returns transparent color index from frame 0 */
3374 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
3375 if (hr != S_OK) return;
3377 hr = IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader);
3378 if (hr == S_OK)
3380 hr = IWICMetadataBlockReader_GetCount(block_reader, &block_count);
3381 if (hr == S_OK)
3383 for (i = 0; i < block_count; i++)
3385 hr = IWICMetadataBlockReader_GetReaderByIndex(block_reader, i, &reader);
3386 if (hr == S_OK)
3388 if (!transparent_idx)
3389 transparent_idx = get_gif_transparent_idx(reader);
3391 IWICMetadataReader_Release(reader);
3395 IWICMetadataBlockReader_Release(block_reader);
3398 if (transparent_idx) add_property(bitmap, transparent_idx);
3399 GdipFree(transparent_idx);
3401 IWICBitmapFrameDecode_Release(frame);
3404 typedef void (*metadata_reader_func)(GpBitmap *bitmap, IWICBitmapDecoder *decoder, UINT frame);
3406 static GpStatus decode_image_wic(IStream *stream, GDIPCONST CLSID *clsid,
3407 UINT active_frame, metadata_reader_func metadata_reader, GpImage **image)
3409 GpStatus status=Ok;
3410 GpBitmap *bitmap;
3411 HRESULT hr;
3412 IWICBitmapDecoder *decoder;
3413 IWICBitmapFrameDecode *frame;
3414 IWICBitmapSource *source=NULL;
3415 IWICMetadataBlockReader *block_reader;
3416 WICPixelFormatGUID wic_format;
3417 PixelFormat gdip_format=0;
3418 ColorPalette *palette = NULL;
3419 WICBitmapPaletteType palette_type = WICBitmapPaletteTypeFixedHalftone256;
3420 int i;
3421 UINT width, height, frame_count;
3422 BitmapData lockeddata;
3423 WICRect wrc;
3424 HRESULT initresult;
3426 TRACE("%p,%s,%u,%p\n", stream, wine_dbgstr_guid(clsid), active_frame, image);
3428 initresult = CoInitialize(NULL);
3430 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
3431 &IID_IWICBitmapDecoder, (void**)&decoder);
3432 if (FAILED(hr)) goto end;
3434 hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
3435 if (SUCCEEDED(hr))
3437 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
3438 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
3441 if (SUCCEEDED(hr)) /* got frame */
3443 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
3445 if (SUCCEEDED(hr))
3447 IWICBitmapSource *bmp_source;
3448 IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICBitmapSource, (void **)&bmp_source);
3450 for (i=0; pixel_formats[i].wic_format; i++)
3452 if (IsEqualGUID(&wic_format, pixel_formats[i].wic_format))
3454 source = bmp_source;
3455 gdip_format = pixel_formats[i].gdip_format;
3456 palette_type = pixel_formats[i].palette_type;
3457 break;
3460 if (!source)
3462 /* unknown format; fall back on 32bppARGB */
3463 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, bmp_source, &source);
3464 gdip_format = PixelFormat32bppARGB;
3465 IWICBitmapSource_Release(bmp_source);
3467 TRACE("%s => %#x\n", wine_dbgstr_guid(&wic_format), gdip_format);
3470 if (SUCCEEDED(hr)) /* got source */
3472 hr = IWICBitmapSource_GetSize(source, &width, &height);
3474 if (SUCCEEDED(hr))
3475 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
3476 NULL, &bitmap);
3478 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
3480 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
3481 gdip_format, &lockeddata);
3482 if (status == Ok) /* locked bitmap */
3484 wrc.X = 0;
3485 wrc.Width = width;
3486 wrc.Height = 1;
3487 for (i=0; i<height; i++)
3489 wrc.Y = i;
3490 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
3491 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
3492 if (FAILED(hr)) break;
3495 GdipBitmapUnlockBits(bitmap, &lockeddata);
3498 if (SUCCEEDED(hr) && status == Ok)
3499 *image = (GpImage*)bitmap;
3500 else
3502 *image = NULL;
3503 GdipDisposeImage((GpImage*)bitmap);
3506 if (SUCCEEDED(hr) && status == Ok)
3508 double dpix, dpiy;
3509 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
3510 if (SUCCEEDED(hr))
3512 bitmap->image.xres = dpix;
3513 bitmap->image.yres = dpiy;
3515 hr = S_OK;
3519 IWICBitmapSource_Release(source);
3522 if (SUCCEEDED(hr)) {
3523 bitmap->metadata_reader = NULL;
3525 if (metadata_reader)
3526 metadata_reader(bitmap, decoder, active_frame);
3527 else if (IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader) == S_OK)
3529 UINT block_count = 0;
3530 if (IWICMetadataBlockReader_GetCount(block_reader, &block_count) == S_OK && block_count)
3531 IWICMetadataBlockReader_GetReaderByIndex(block_reader, 0, &bitmap->metadata_reader);
3532 IWICMetadataBlockReader_Release(block_reader);
3535 palette = get_palette(frame, palette_type);
3536 IWICBitmapFrameDecode_Release(frame);
3540 IWICBitmapDecoder_Release(decoder);
3542 end:
3543 if (SUCCEEDED(initresult)) CoUninitialize();
3545 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
3547 if (status == Ok)
3549 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3550 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI|ImageFlagsColorSpaceRGB;
3551 bitmap->image.frame_count = frame_count;
3552 bitmap->image.current_frame = active_frame;
3553 bitmap->image.stream = stream;
3554 if (palette)
3556 GdipFree(bitmap->image.palette);
3557 bitmap->image.palette = palette;
3559 else
3561 if (IsEqualGUID(&wic_format, &GUID_WICPixelFormatBlackWhite))
3562 bitmap->image.palette->Flags = 0;
3564 /* Pin the source stream */
3565 IStream_AddRef(stream);
3566 TRACE("=> %p\n", *image);
3569 return status;
3572 static GpStatus decode_image_icon(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3574 return decode_image_wic(stream, &CLSID_WICIcoDecoder, active_frame, NULL, image);
3577 static GpStatus decode_image_bmp(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3579 GpStatus status;
3580 GpBitmap* bitmap;
3582 status = decode_image_wic(stream, &CLSID_WICBmpDecoder, active_frame, NULL, image);
3584 bitmap = (GpBitmap*)*image;
3586 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
3588 /* WIC supports bmp files with alpha, but gdiplus does not */
3589 bitmap->format = PixelFormat32bppRGB;
3592 return status;
3595 static GpStatus decode_image_jpeg(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3597 return decode_image_wic(stream, &CLSID_WICJpegDecoder, active_frame, NULL, image);
3600 static GpStatus decode_image_png(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3602 return decode_image_wic(stream, &CLSID_WICPngDecoder, active_frame, NULL, image);
3605 static GpStatus decode_image_gif(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3607 return decode_image_wic(stream, &CLSID_WICGifDecoder, active_frame, gif_metadata_reader, image);
3610 static GpStatus decode_image_tiff(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3612 return decode_image_wic(stream, &CLSID_WICTiffDecoder, active_frame, NULL, image);
3615 static GpStatus decode_image_olepicture_metafile(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3617 IPicture *pic;
3619 TRACE("%p %p\n", stream, image);
3621 if(!stream || !image)
3622 return InvalidParameter;
3624 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
3625 (LPVOID*) &pic) != S_OK){
3626 TRACE("Could not load picture\n");
3627 return GenericError;
3630 /* FIXME: missing initialization code */
3631 *image = GdipAlloc(sizeof(GpMetafile));
3632 if(!*image) return OutOfMemory;
3633 (*image)->type = ImageTypeMetafile;
3634 (*image)->stream = NULL;
3635 (*image)->picture = pic;
3636 (*image)->flags = ImageFlagsNone;
3637 (*image)->frame_count = 1;
3638 (*image)->current_frame = 0;
3639 (*image)->palette = NULL;
3641 TRACE("<-- %p\n", *image);
3643 return Ok;
3646 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
3647 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
3649 typedef GpStatus (*decode_image_func)(IStream *stream, REFCLSID clsid, UINT active_frame, GpImage **image);
3651 typedef struct image_codec {
3652 ImageCodecInfo info;
3653 encode_image_func encode_func;
3654 decode_image_func decode_func;
3655 } image_codec;
3657 typedef enum {
3658 BMP,
3659 JPEG,
3660 GIF,
3661 TIFF,
3662 EMF,
3663 WMF,
3664 PNG,
3665 ICO,
3666 NUM_CODECS
3667 } ImageFormat;
3669 static const struct image_codec codecs[NUM_CODECS];
3671 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
3673 BYTE signature[8];
3674 const BYTE *pattern, *mask;
3675 LARGE_INTEGER seek;
3676 HRESULT hr;
3677 UINT bytesread;
3678 int i;
3679 DWORD j, sig;
3681 /* seek to the start of the stream */
3682 seek.QuadPart = 0;
3683 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3684 if (FAILED(hr)) return hresult_to_status(hr);
3686 /* read the first 8 bytes */
3687 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
3688 hr = IStream_Read(stream, signature, 8, &bytesread);
3689 if (FAILED(hr)) return hresult_to_status(hr);
3690 if (hr == S_FALSE || bytesread == 0) return GenericError;
3692 for (i = 0; i < NUM_CODECS; i++) {
3693 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
3694 bytesread >= codecs[i].info.SigSize)
3696 for (sig=0; sig<codecs[i].info.SigCount; sig++)
3698 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
3699 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
3700 for (j=0; j<codecs[i].info.SigSize; j++)
3701 if ((signature[j] & mask[j]) != pattern[j])
3702 break;
3703 if (j == codecs[i].info.SigSize)
3705 *result = &codecs[i];
3706 return Ok;
3712 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
3713 signature[0],signature[1],signature[2],signature[3],
3714 signature[4],signature[5],signature[6],signature[7]);
3716 return GenericError;
3719 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image, GDIPCONST GUID *dimensionID,
3720 UINT frame)
3722 GpStatus stat;
3723 LARGE_INTEGER seek;
3724 HRESULT hr;
3725 const struct image_codec *codec = NULL;
3726 GpImage *new_image;
3728 TRACE("(%p,%s,%u)\n", image, debugstr_guid(dimensionID), frame);
3730 if (!image || !dimensionID)
3731 return InvalidParameter;
3733 if (frame >= image->frame_count)
3735 WARN("requested frame %u, but image has only %u\n", frame, image->frame_count);
3736 return InvalidParameter;
3739 if (image->type != ImageTypeBitmap && image->type != ImageTypeMetafile)
3741 WARN("invalid image type %d\n", image->type);
3742 return InvalidParameter;
3745 if (image->current_frame == frame)
3746 return Ok;
3748 if (!image->stream)
3750 TRACE("image doesn't have an associated stream\n");
3751 return Ok;
3754 /* choose an appropriate image decoder */
3755 stat = get_decoder_info(image->stream, &codec);
3756 if (stat != Ok)
3758 WARN("can't find decoder info\n");
3759 return stat;
3762 /* seek to the start of the stream */
3763 seek.QuadPart = 0;
3764 hr = IStream_Seek(image->stream, seek, STREAM_SEEK_SET, NULL);
3765 if (FAILED(hr))
3766 return hresult_to_status(hr);
3768 /* call on the image decoder to do the real work */
3769 stat = codec->decode_func(image->stream, &codec->info.Clsid, frame, &new_image);
3771 if (stat == Ok)
3773 memcpy(&new_image->format, &codec->info.FormatID, sizeof(GUID));
3774 free_image_data(image);
3775 if (image->type == ImageTypeBitmap)
3776 *(GpBitmap *)image = *(GpBitmap *)new_image;
3777 else if (image->type == ImageTypeMetafile)
3778 *(GpMetafile *)image = *(GpMetafile *)new_image;
3779 new_image->type = ~0;
3780 GdipFree(new_image);
3781 return Ok;
3784 return stat;
3787 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream *stream, GpImage **image)
3789 GpStatus stat;
3790 LARGE_INTEGER seek;
3791 HRESULT hr;
3792 const struct image_codec *codec=NULL;
3794 /* choose an appropriate image decoder */
3795 stat = get_decoder_info(stream, &codec);
3796 if (stat != Ok) return stat;
3798 /* seek to the start of the stream */
3799 seek.QuadPart = 0;
3800 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3801 if (FAILED(hr)) return hresult_to_status(hr);
3803 /* call on the image decoder to do the real work */
3804 stat = codec->decode_func(stream, &codec->info.Clsid, 0, image);
3806 /* take note of the original data format */
3807 if (stat == Ok)
3809 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
3810 return Ok;
3813 return stat;
3816 /* FIXME: no ICM */
3817 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
3819 TRACE("%p %p\n", stream, image);
3821 return GdipLoadImageFromStream(stream, image);
3824 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
3826 static int calls;
3828 TRACE("(%p,%u)\n", image, propId);
3830 if(!image)
3831 return InvalidParameter;
3833 if(!(calls++))
3834 FIXME("not implemented\n");
3836 return NotImplemented;
3839 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
3841 static int calls;
3843 if (!image || !item) return InvalidParameter;
3845 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image, item, item->id, item->type, item->length, item->value);
3847 if(!(calls++))
3848 FIXME("not implemented\n");
3850 return Ok;
3853 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
3854 GDIPCONST CLSID *clsidEncoder,
3855 GDIPCONST EncoderParameters *encoderParams)
3857 GpStatus stat;
3858 IStream *stream;
3860 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
3862 if (!image || !filename|| !clsidEncoder)
3863 return InvalidParameter;
3865 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
3866 if (stat != Ok)
3867 return GenericError;
3869 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
3871 IStream_Release(stream);
3872 return stat;
3875 /*************************************************************************
3876 * Encoding functions -
3877 * These functions encode an image in different image file formats.
3880 static GpStatus encode_image_WIC(GpImage *image, IStream* stream,
3881 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3883 GpStatus stat;
3884 GpBitmap *bitmap;
3885 IWICBitmapEncoder *encoder;
3886 IWICBitmapFrameEncode *frameencode;
3887 IPropertyBag2 *encoderoptions;
3888 HRESULT hr;
3889 UINT width, height;
3890 PixelFormat gdipformat=0;
3891 const WICPixelFormatGUID *desired_wicformat;
3892 WICPixelFormatGUID wicformat;
3893 GpRect rc;
3894 BitmapData lockeddata;
3895 HRESULT initresult;
3896 UINT i;
3898 if (image->type != ImageTypeBitmap)
3899 return GenericError;
3901 bitmap = (GpBitmap*)image;
3903 GdipGetImageWidth(image, &width);
3904 GdipGetImageHeight(image, &height);
3906 rc.X = 0;
3907 rc.Y = 0;
3908 rc.Width = width;
3909 rc.Height = height;
3911 initresult = CoInitialize(NULL);
3913 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
3914 &IID_IWICBitmapEncoder, (void**)&encoder);
3915 if (FAILED(hr))
3917 if (SUCCEEDED(initresult)) CoUninitialize();
3918 return hresult_to_status(hr);
3921 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
3923 if (SUCCEEDED(hr))
3925 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
3928 if (SUCCEEDED(hr)) /* created frame */
3930 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
3932 if (SUCCEEDED(hr))
3933 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
3935 if (SUCCEEDED(hr))
3936 hr = IWICBitmapFrameEncode_SetResolution(frameencode, image->xres, image->yres);
3938 if (SUCCEEDED(hr))
3940 for (i=0; pixel_formats[i].wic_format; i++)
3942 if (pixel_formats[i].gdip_format == bitmap->format)
3944 desired_wicformat = pixel_formats[i].wic_format;
3945 gdipformat = bitmap->format;
3946 break;
3949 if (!gdipformat)
3951 desired_wicformat = &GUID_WICPixelFormat32bppBGRA;
3952 gdipformat = PixelFormat32bppARGB;
3955 memcpy(&wicformat, desired_wicformat, sizeof(GUID));
3956 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
3959 if (SUCCEEDED(hr) && !IsEqualGUID(desired_wicformat, &wicformat))
3961 /* Encoder doesn't support this bitmap's format. */
3962 gdipformat = 0;
3963 for (i=0; pixel_formats[i].wic_format; i++)
3965 if (IsEqualGUID(&wicformat, pixel_formats[i].wic_format))
3967 gdipformat = pixel_formats[i].gdip_format;
3968 break;
3971 if (!gdipformat)
3973 ERR("Cannot support encoder format %s\n", debugstr_guid(&wicformat));
3974 hr = E_FAIL;
3978 if (SUCCEEDED(hr))
3980 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
3981 &lockeddata);
3983 if (stat == Ok)
3985 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
3986 BYTE *row;
3988 /* write one row at a time in case stride is negative */
3989 row = lockeddata.Scan0;
3990 for (i=0; i<lockeddata.Height; i++)
3992 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
3993 if (FAILED(hr)) break;
3994 row += lockeddata.Stride;
3997 GdipBitmapUnlockBits(bitmap, &lockeddata);
3999 else
4000 hr = E_FAIL;
4003 if (SUCCEEDED(hr))
4004 hr = IWICBitmapFrameEncode_Commit(frameencode);
4006 IWICBitmapFrameEncode_Release(frameencode);
4007 IPropertyBag2_Release(encoderoptions);
4010 if (SUCCEEDED(hr))
4011 hr = IWICBitmapEncoder_Commit(encoder);
4013 IWICBitmapEncoder_Release(encoder);
4015 if (SUCCEEDED(initresult)) CoUninitialize();
4017 return hresult_to_status(hr);
4020 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
4021 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4023 return encode_image_WIC(image, stream, &CLSID_WICBmpEncoder, params);
4026 static GpStatus encode_image_tiff(GpImage *image, IStream* stream,
4027 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4029 return encode_image_WIC(image, stream, &CLSID_WICTiffEncoder, params);
4032 static GpStatus encode_image_png(GpImage *image, IStream* stream,
4033 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4035 return encode_image_WIC(image, stream, &CLSID_WICPngEncoder, params);
4038 static GpStatus encode_image_jpeg(GpImage *image, IStream* stream,
4039 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4041 return encode_image_WIC(image, stream, &CLSID_WICJpegEncoder, params);
4044 /*****************************************************************************
4045 * GdipSaveImageToStream [GDIPLUS.@]
4047 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
4048 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
4050 GpStatus stat;
4051 encode_image_func encode_image;
4052 int i;
4054 TRACE("%p %p %p %p\n", image, stream, clsid, params);
4056 if(!image || !stream)
4057 return InvalidParameter;
4059 /* select correct encoder */
4060 encode_image = NULL;
4061 for (i = 0; i < NUM_CODECS; i++) {
4062 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
4063 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
4064 encode_image = codecs[i].encode_func;
4066 if (encode_image == NULL)
4067 return UnknownImageFormat;
4069 stat = encode_image(image, stream, clsid, params);
4071 return stat;
4074 /*****************************************************************************
4075 * GdipSaveAdd [GDIPLUS.@]
4077 GpStatus WINGDIPAPI GdipSaveAdd(GpImage *image, GDIPCONST EncoderParameters *params)
4079 FIXME("(%p,%p): stub\n", image, params);
4080 return Ok;
4083 /*****************************************************************************
4084 * GdipGetImagePalette [GDIPLUS.@]
4086 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
4088 INT count;
4090 TRACE("(%p,%p,%i)\n", image, palette, size);
4092 if (!image || !palette)
4093 return InvalidParameter;
4095 count = image->palette ? image->palette->Count : 0;
4097 if (size < (sizeof(UINT)*2+sizeof(ARGB)*count))
4099 TRACE("<-- InsufficientBuffer\n");
4100 return InsufficientBuffer;
4103 if (image->palette)
4105 palette->Flags = image->palette->Flags;
4106 palette->Count = image->palette->Count;
4107 memcpy(palette->Entries, image->palette->Entries, sizeof(ARGB)*image->palette->Count);
4109 else
4111 palette->Flags = 0;
4112 palette->Count = 0;
4114 return Ok;
4117 /*****************************************************************************
4118 * GdipSetImagePalette [GDIPLUS.@]
4120 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
4121 GDIPCONST ColorPalette *palette)
4123 ColorPalette *new_palette;
4125 TRACE("(%p,%p)\n", image, palette);
4127 if(!image || !palette || palette->Count > 256)
4128 return InvalidParameter;
4130 new_palette = GdipAlloc(2 * sizeof(UINT) + palette->Count * sizeof(ARGB));
4131 if (!new_palette) return OutOfMemory;
4133 GdipFree(image->palette);
4134 image->palette = new_palette;
4135 image->palette->Flags = palette->Flags;
4136 image->palette->Count = palette->Count;
4137 memcpy(image->palette->Entries, palette->Entries, sizeof(ARGB)*palette->Count);
4139 return Ok;
4142 /*************************************************************************
4143 * Encoders -
4144 * Structures that represent which formats we support for encoding.
4147 /* ImageCodecInfo creation routines taken from libgdiplus */
4148 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
4149 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
4150 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
4151 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
4152 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
4153 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
4155 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
4156 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
4157 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
4158 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
4159 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
4160 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
4162 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
4163 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
4164 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
4165 static const WCHAR gif_format[] = {'G','I','F',0};
4166 static const BYTE gif_sig_pattern[12] = "GIF87aGIF89a";
4167 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4169 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
4170 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
4171 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
4172 static const WCHAR tiff_format[] = {'T','I','F','F',0};
4173 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
4174 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4176 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
4177 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
4178 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
4179 static const WCHAR emf_format[] = {'E','M','F',0};
4180 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
4181 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4183 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
4184 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
4185 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
4186 static const WCHAR wmf_format[] = {'W','M','F',0};
4187 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
4188 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
4190 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
4191 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
4192 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
4193 static const WCHAR png_format[] = {'P','N','G',0};
4194 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
4195 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4197 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
4198 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
4199 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
4200 static const WCHAR ico_format[] = {'I','C','O',0};
4201 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
4202 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
4204 static const struct image_codec codecs[NUM_CODECS] = {
4206 { /* BMP */
4207 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4208 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4209 /* CodecName */ bmp_codecname,
4210 /* DllName */ NULL,
4211 /* FormatDescription */ bmp_format,
4212 /* FilenameExtension */ bmp_extension,
4213 /* MimeType */ bmp_mimetype,
4214 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4215 /* Version */ 1,
4216 /* SigCount */ 1,
4217 /* SigSize */ 2,
4218 /* SigPattern */ bmp_sig_pattern,
4219 /* SigMask */ bmp_sig_mask,
4221 encode_image_BMP,
4222 decode_image_bmp
4225 { /* JPEG */
4226 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4227 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4228 /* CodecName */ jpeg_codecname,
4229 /* DllName */ NULL,
4230 /* FormatDescription */ jpeg_format,
4231 /* FilenameExtension */ jpeg_extension,
4232 /* MimeType */ jpeg_mimetype,
4233 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4234 /* Version */ 1,
4235 /* SigCount */ 1,
4236 /* SigSize */ 2,
4237 /* SigPattern */ jpeg_sig_pattern,
4238 /* SigMask */ jpeg_sig_mask,
4240 encode_image_jpeg,
4241 decode_image_jpeg
4244 { /* GIF */
4245 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4246 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4247 /* CodecName */ gif_codecname,
4248 /* DllName */ NULL,
4249 /* FormatDescription */ gif_format,
4250 /* FilenameExtension */ gif_extension,
4251 /* MimeType */ gif_mimetype,
4252 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4253 /* Version */ 1,
4254 /* SigCount */ 2,
4255 /* SigSize */ 6,
4256 /* SigPattern */ gif_sig_pattern,
4257 /* SigMask */ gif_sig_mask,
4259 NULL,
4260 decode_image_gif
4263 { /* TIFF */
4264 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4265 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4266 /* CodecName */ tiff_codecname,
4267 /* DllName */ NULL,
4268 /* FormatDescription */ tiff_format,
4269 /* FilenameExtension */ tiff_extension,
4270 /* MimeType */ tiff_mimetype,
4271 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4272 /* Version */ 1,
4273 /* SigCount */ 2,
4274 /* SigSize */ 4,
4275 /* SigPattern */ tiff_sig_pattern,
4276 /* SigMask */ tiff_sig_mask,
4278 encode_image_tiff,
4279 decode_image_tiff
4282 { /* EMF */
4283 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4284 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4285 /* CodecName */ emf_codecname,
4286 /* DllName */ NULL,
4287 /* FormatDescription */ emf_format,
4288 /* FilenameExtension */ emf_extension,
4289 /* MimeType */ emf_mimetype,
4290 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4291 /* Version */ 1,
4292 /* SigCount */ 1,
4293 /* SigSize */ 4,
4294 /* SigPattern */ emf_sig_pattern,
4295 /* SigMask */ emf_sig_mask,
4297 NULL,
4298 decode_image_olepicture_metafile
4301 { /* WMF */
4302 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4303 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4304 /* CodecName */ wmf_codecname,
4305 /* DllName */ NULL,
4306 /* FormatDescription */ wmf_format,
4307 /* FilenameExtension */ wmf_extension,
4308 /* MimeType */ wmf_mimetype,
4309 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
4310 /* Version */ 1,
4311 /* SigCount */ 1,
4312 /* SigSize */ 2,
4313 /* SigPattern */ wmf_sig_pattern,
4314 /* SigMask */ wmf_sig_mask,
4316 NULL,
4317 decode_image_olepicture_metafile
4320 { /* PNG */
4321 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4322 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4323 /* CodecName */ png_codecname,
4324 /* DllName */ NULL,
4325 /* FormatDescription */ png_format,
4326 /* FilenameExtension */ png_extension,
4327 /* MimeType */ png_mimetype,
4328 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4329 /* Version */ 1,
4330 /* SigCount */ 1,
4331 /* SigSize */ 8,
4332 /* SigPattern */ png_sig_pattern,
4333 /* SigMask */ png_sig_mask,
4335 encode_image_png,
4336 decode_image_png
4339 { /* ICO */
4340 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
4341 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
4342 /* CodecName */ ico_codecname,
4343 /* DllName */ NULL,
4344 /* FormatDescription */ ico_format,
4345 /* FilenameExtension */ ico_extension,
4346 /* MimeType */ ico_mimetype,
4347 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
4348 /* Version */ 1,
4349 /* SigCount */ 1,
4350 /* SigSize */ 4,
4351 /* SigPattern */ ico_sig_pattern,
4352 /* SigMask */ ico_sig_mask,
4354 NULL,
4355 decode_image_icon
4359 /*****************************************************************************
4360 * GdipGetImageDecodersSize [GDIPLUS.@]
4362 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
4364 int decoder_count=0;
4365 int i;
4366 TRACE("%p %p\n", numDecoders, size);
4368 if (!numDecoders || !size)
4369 return InvalidParameter;
4371 for (i=0; i<NUM_CODECS; i++)
4373 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4374 decoder_count++;
4377 *numDecoders = decoder_count;
4378 *size = decoder_count * sizeof(ImageCodecInfo);
4380 return Ok;
4383 /*****************************************************************************
4384 * GdipGetImageDecoders [GDIPLUS.@]
4386 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
4388 int i, decoder_count=0;
4389 TRACE("%u %u %p\n", numDecoders, size, decoders);
4391 if (!decoders ||
4392 size != numDecoders * sizeof(ImageCodecInfo))
4393 return GenericError;
4395 for (i=0; i<NUM_CODECS; i++)
4397 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
4399 if (decoder_count == numDecoders) return GenericError;
4400 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4401 decoder_count++;
4405 if (decoder_count < numDecoders) return GenericError;
4407 return Ok;
4410 /*****************************************************************************
4411 * GdipGetImageEncodersSize [GDIPLUS.@]
4413 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
4415 int encoder_count=0;
4416 int i;
4417 TRACE("%p %p\n", numEncoders, size);
4419 if (!numEncoders || !size)
4420 return InvalidParameter;
4422 for (i=0; i<NUM_CODECS; i++)
4424 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4425 encoder_count++;
4428 *numEncoders = encoder_count;
4429 *size = encoder_count * sizeof(ImageCodecInfo);
4431 return Ok;
4434 /*****************************************************************************
4435 * GdipGetImageEncoders [GDIPLUS.@]
4437 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
4439 int i, encoder_count=0;
4440 TRACE("%u %u %p\n", numEncoders, size, encoders);
4442 if (!encoders ||
4443 size != numEncoders * sizeof(ImageCodecInfo))
4444 return GenericError;
4446 for (i=0; i<NUM_CODECS; i++)
4448 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
4450 if (encoder_count == numEncoders) return GenericError;
4451 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
4452 encoder_count++;
4456 if (encoder_count < numEncoders) return GenericError;
4458 return Ok;
4461 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
4462 GDIPCONST CLSID* clsidEncoder, UINT *size)
4464 static int calls;
4466 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
4468 if(!(calls++))
4469 FIXME("not implemented\n");
4471 *size = 0;
4473 return NotImplemented;
4476 static PixelFormat get_16bpp_format(HBITMAP hbm)
4478 BITMAPV4HEADER bmh;
4479 HDC hdc;
4480 PixelFormat result;
4482 hdc = CreateCompatibleDC(NULL);
4484 memset(&bmh, 0, sizeof(bmh));
4485 bmh.bV4Size = sizeof(bmh);
4486 bmh.bV4Width = 1;
4487 bmh.bV4Height = 1;
4488 bmh.bV4V4Compression = BI_BITFIELDS;
4489 bmh.bV4BitCount = 16;
4491 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO*)&bmh, DIB_RGB_COLORS);
4493 if (bmh.bV4RedMask == 0x7c00 &&
4494 bmh.bV4GreenMask == 0x3e0 &&
4495 bmh.bV4BlueMask == 0x1f)
4497 result = PixelFormat16bppRGB555;
4499 else if (bmh.bV4RedMask == 0xf800 &&
4500 bmh.bV4GreenMask == 0x7e0 &&
4501 bmh.bV4BlueMask == 0x1f)
4503 result = PixelFormat16bppRGB565;
4505 else
4507 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
4508 bmh.bV4GreenMask, bmh.bV4BlueMask);
4509 result = PixelFormatUndefined;
4512 DeleteDC(hdc);
4514 return result;
4517 /*****************************************************************************
4518 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
4520 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
4522 BITMAP bm;
4523 GpStatus retval;
4524 PixelFormat format;
4525 BitmapData lockeddata;
4527 TRACE("%p %p %p\n", hbm, hpal, bitmap);
4529 if(!hbm || !bitmap)
4530 return InvalidParameter;
4532 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
4533 return InvalidParameter;
4535 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
4536 switch(bm.bmBitsPixel) {
4537 case 1:
4538 format = PixelFormat1bppIndexed;
4539 break;
4540 case 4:
4541 format = PixelFormat4bppIndexed;
4542 break;
4543 case 8:
4544 format = PixelFormat8bppIndexed;
4545 break;
4546 case 16:
4547 format = get_16bpp_format(hbm);
4548 if (format == PixelFormatUndefined)
4549 return InvalidParameter;
4550 break;
4551 case 24:
4552 format = PixelFormat24bppRGB;
4553 break;
4554 case 32:
4555 format = PixelFormat32bppRGB;
4556 break;
4557 case 48:
4558 format = PixelFormat48bppRGB;
4559 break;
4560 default:
4561 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
4562 return InvalidParameter;
4565 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
4566 format, NULL, bitmap);
4568 if (retval == Ok)
4570 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
4571 format, &lockeddata);
4572 if (retval == Ok)
4574 HDC hdc;
4575 char bmibuf[FIELD_OFFSET(BITMAPINFO, bmiColors[256])];
4576 BITMAPINFO *pbmi = (BITMAPINFO *)bmibuf;
4577 INT src_height;
4579 hdc = CreateCompatibleDC(NULL);
4581 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
4582 pbmi->bmiHeader.biBitCount = 0;
4584 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
4586 src_height = abs(pbmi->bmiHeader.biHeight);
4587 pbmi->bmiHeader.biHeight = -src_height;
4589 GetDIBits(hdc, hbm, 0, src_height, lockeddata.Scan0, pbmi, DIB_RGB_COLORS);
4591 DeleteDC(hdc);
4593 GdipBitmapUnlockBits(*bitmap, &lockeddata);
4596 if (retval == Ok && hpal)
4598 PALETTEENTRY entry[256];
4599 ColorPalette *palette=NULL;
4600 int i, num_palette_entries;
4602 num_palette_entries = GetPaletteEntries(hpal, 0, 256, entry);
4603 if (!num_palette_entries)
4604 retval = GenericError;
4606 palette = GdipAlloc(sizeof(ColorPalette) + sizeof(ARGB) * (num_palette_entries-1));
4607 if (!palette)
4608 retval = OutOfMemory;
4610 if (retval == Ok)
4612 palette->Flags = 0;
4613 palette->Count = num_palette_entries;
4615 for (i=0; i<num_palette_entries; i++)
4617 palette->Entries[i] = 0xff000000 | entry[i].peRed << 16 |
4618 entry[i].peGreen << 8 | entry[i].peBlue;
4621 retval = GdipSetImagePalette((GpImage*)*bitmap, palette);
4624 GdipFree(palette);
4627 if (retval != Ok)
4629 GdipDisposeImage((GpImage*)*bitmap);
4630 *bitmap = NULL;
4634 return retval;
4637 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
4639 FIXME("(%p): stub\n", effect);
4640 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
4641 * in Windows's gdiplus */
4642 return NotImplemented;
4645 /*****************************************************************************
4646 * GdipSetEffectParameters [GDIPLUS.@]
4648 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
4649 const VOID *params, const UINT size)
4651 static int calls;
4653 TRACE("(%p,%p,%u)\n", effect, params, size);
4655 if(!(calls++))
4656 FIXME("not implemented\n");
4658 return NotImplemented;
4661 /*****************************************************************************
4662 * GdipGetImageFlags [GDIPLUS.@]
4664 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
4666 TRACE("%p %p\n", image, flags);
4668 if(!image || !flags)
4669 return InvalidParameter;
4671 *flags = image->flags;
4673 return Ok;
4676 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
4678 TRACE("(%d, %p)\n", control, param);
4680 switch(control){
4681 case TestControlForceBilinear:
4682 if(param)
4683 FIXME("TestControlForceBilinear not handled\n");
4684 break;
4685 case TestControlNoICM:
4686 if(param)
4687 FIXME("TestControlNoICM not handled\n");
4688 break;
4689 case TestControlGetBuildNumber:
4690 *((DWORD*)param) = 3102;
4691 break;
4694 return Ok;
4697 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
4699 TRACE("%p\n", image);
4701 return Ok;
4704 /*****************************************************************************
4705 * GdipGetImageThumbnail [GDIPLUS.@]
4707 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
4708 GpImage **ret_image, GetThumbnailImageAbort cb,
4709 VOID * cb_data)
4711 GpStatus stat;
4712 GpGraphics *graphics;
4713 UINT srcwidth, srcheight;
4715 TRACE("(%p %u %u %p %p %p)\n",
4716 image, width, height, ret_image, cb, cb_data);
4718 if (!image || !ret_image)
4719 return InvalidParameter;
4721 if (!width) width = 120;
4722 if (!height) height = 120;
4724 GdipGetImageWidth(image, &srcwidth);
4725 GdipGetImageHeight(image, &srcheight);
4727 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppPARGB,
4728 NULL, (GpBitmap**)ret_image);
4730 if (stat == Ok)
4732 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
4734 if (stat == Ok)
4736 stat = GdipDrawImageRectRectI(graphics, image,
4737 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
4738 NULL, NULL, NULL);
4740 GdipDeleteGraphics(graphics);
4743 if (stat != Ok)
4745 GdipDisposeImage(*ret_image);
4746 *ret_image = NULL;
4750 return stat;
4753 /*****************************************************************************
4754 * GdipImageRotateFlip [GDIPLUS.@]
4756 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
4758 GpBitmap *new_bitmap;
4759 GpBitmap *bitmap;
4760 int bpp, bytesperpixel;
4761 BOOL rotate_90, flip_x, flip_y;
4762 int src_x_offset, src_y_offset;
4763 LPBYTE src_origin;
4764 UINT x, y, width, height;
4765 BitmapData src_lock, dst_lock;
4766 GpStatus stat;
4768 TRACE("(%p, %u)\n", image, type);
4770 if (!image)
4771 return InvalidParameter;
4773 rotate_90 = type&1;
4774 flip_x = (type&6) == 2 || (type&6) == 4;
4775 flip_y = (type&3) == 1 || (type&3) == 2;
4777 if (image->type != ImageTypeBitmap)
4779 FIXME("Not implemented for type %i\n", image->type);
4780 return NotImplemented;
4783 bitmap = (GpBitmap*)image;
4784 bpp = PIXELFORMATBPP(bitmap->format);
4786 if (bpp < 8)
4788 FIXME("Not implemented for %i bit images\n", bpp);
4789 return NotImplemented;
4792 if (rotate_90)
4794 width = bitmap->height;
4795 height = bitmap->width;
4797 else
4799 width = bitmap->width;
4800 height = bitmap->height;
4803 bytesperpixel = bpp/8;
4805 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
4807 if (stat != Ok)
4808 return stat;
4810 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
4812 if (stat == Ok)
4814 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
4816 if (stat == Ok)
4818 LPBYTE src_row, src_pixel;
4819 LPBYTE dst_row, dst_pixel;
4821 src_origin = src_lock.Scan0;
4822 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
4823 if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
4825 if (rotate_90)
4827 if (flip_y) src_x_offset = -src_lock.Stride;
4828 else src_x_offset = src_lock.Stride;
4829 if (flip_x) src_y_offset = -bytesperpixel;
4830 else src_y_offset = bytesperpixel;
4832 else
4834 if (flip_x) src_x_offset = -bytesperpixel;
4835 else src_x_offset = bytesperpixel;
4836 if (flip_y) src_y_offset = -src_lock.Stride;
4837 else src_y_offset = src_lock.Stride;
4840 src_row = src_origin;
4841 dst_row = dst_lock.Scan0;
4842 for (y=0; y<height; y++)
4844 src_pixel = src_row;
4845 dst_pixel = dst_row;
4846 for (x=0; x<width; x++)
4848 /* FIXME: This could probably be faster without memcpy. */
4849 memcpy(dst_pixel, src_pixel, bytesperpixel);
4850 dst_pixel += bytesperpixel;
4851 src_pixel += src_x_offset;
4853 src_row += src_y_offset;
4854 dst_row += dst_lock.Stride;
4857 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
4860 GdipBitmapUnlockBits(bitmap, &src_lock);
4863 if (stat == Ok)
4864 move_bitmap(bitmap, new_bitmap, FALSE);
4865 else
4866 GdipDisposeImage((GpImage*)new_bitmap);
4868 return stat;