ddraw: Create surfaces with the correct mip level in device_parent_create_texture_sur...
[wine/multimedia.git] / dlls / gdiplus / image.c
blob9b362809583a1542a848a9569a7b6cab4f640016
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
20 #include <assert.h>
22 #define NONAMELESSUNION
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "wingdi.h"
29 #define COBJMACROS
30 #include "objbase.h"
31 #include "olectl.h"
32 #include "ole2.h"
34 #include "initguid.h"
35 #include "wincodec.h"
36 #include "gdiplus.h"
37 #include "gdiplus_private.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
42 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
44 static INT ipicture_pixel_height(IPicture *pic)
46 HDC hdcref;
47 OLE_YSIZE_HIMETRIC y;
49 IPicture_get_Height(pic, &y);
51 hdcref = GetDC(0);
53 y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
54 ReleaseDC(0, hdcref);
56 return y;
59 static INT ipicture_pixel_width(IPicture *pic)
61 HDC hdcref;
62 OLE_XSIZE_HIMETRIC x;
64 IPicture_get_Width(pic, &x);
66 hdcref = GetDC(0);
68 x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
70 ReleaseDC(0, hdcref);
72 return x;
75 GpStatus WINGDIPAPI GdipBitmapApplyEffect(GpBitmap* bitmap, CGpEffect* effect,
76 RECT* roi, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
78 FIXME("(%p %p %p %d %p %p): stub\n", bitmap, effect, roi, useAuxData, auxData, auxDataSize);
80 * Note: According to Jose Roca's GDI+ docs, this function is not
81 * implemented in Windows's GDI+.
83 return NotImplemented;
86 GpStatus WINGDIPAPI GdipBitmapCreateApplyEffect(GpBitmap** inputBitmaps,
87 INT numInputs, CGpEffect* effect, RECT* roi, RECT* outputRect,
88 GpBitmap** outputBitmap, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
90 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps, numInputs, effect, roi, outputRect, outputBitmap, useAuxData, auxData, auxDataSize);
92 * Note: According to Jose Roca's GDI+ docs, this function is not
93 * implemented in Windows's GDI+.
95 return NotImplemented;
98 static inline void getpixel_1bppIndexed(BYTE *index, const BYTE *row, UINT x)
100 *index = (row[x/8]>>(7-x%8)) & 1;
103 static inline void getpixel_4bppIndexed(BYTE *index, const BYTE *row, UINT x)
105 if (x & 1)
106 *index = row[x/2]&0xf;
107 else
108 *index = row[x/2]>>4;
111 static inline void getpixel_8bppIndexed(BYTE *index, const BYTE *row, UINT x)
113 *index = row[x];
116 static inline void getpixel_16bppGrayScale(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
117 const BYTE *row, UINT x)
119 *r = *g = *b = row[x*2+1];
120 *a = 255;
123 static inline void getpixel_16bppRGB555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
124 const BYTE *row, UINT x)
126 WORD pixel = *((const WORD*)(row)+x);
127 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
128 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
129 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
130 *a = 255;
133 static inline void getpixel_16bppRGB565(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
134 const BYTE *row, UINT x)
136 WORD pixel = *((const WORD*)(row)+x);
137 *r = (pixel>>8&0xf8)|(pixel>>13&0x7);
138 *g = (pixel>>3&0xfc)|(pixel>>9&0x3);
139 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
140 *a = 255;
143 static inline void getpixel_16bppARGB1555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
144 const BYTE *row, UINT x)
146 WORD pixel = *((const WORD*)(row)+x);
147 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
148 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
149 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
150 if ((pixel&0x8000) == 0x8000)
151 *a = 255;
152 else
153 *a = 0;
156 static inline void getpixel_24bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
157 const BYTE *row, UINT x)
159 *r = row[x*3+2];
160 *g = row[x*3+1];
161 *b = row[x*3];
162 *a = 255;
165 static inline void getpixel_32bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
166 const BYTE *row, UINT x)
168 *r = row[x*4+2];
169 *g = row[x*4+1];
170 *b = row[x*4];
171 *a = 255;
174 static inline void getpixel_32bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
175 const BYTE *row, UINT x)
177 *r = row[x*4+2];
178 *g = row[x*4+1];
179 *b = row[x*4];
180 *a = row[x*4+3];
183 static inline void getpixel_32bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
184 const BYTE *row, UINT x)
186 *a = row[x*4+3];
187 if (*a == 0)
188 *r = *g = *b = 0;
189 else
191 *r = row[x*4+2] * 255 / *a;
192 *g = row[x*4+1] * 255 / *a;
193 *b = row[x*4] * 255 / *a;
197 static inline void getpixel_48bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
198 const BYTE *row, UINT x)
200 *r = row[x*6+5];
201 *g = row[x*6+3];
202 *b = row[x*6+1];
203 *a = 255;
206 static inline void getpixel_64bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
207 const BYTE *row, UINT x)
209 *r = row[x*8+5];
210 *g = row[x*8+3];
211 *b = row[x*8+1];
212 *a = row[x*8+7];
215 static inline void getpixel_64bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
216 const BYTE *row, UINT x)
218 *a = row[x*8+7];
219 if (*a == 0)
220 *r = *g = *b = 0;
221 else
223 *r = row[x*8+5] * 255 / *a;
224 *g = row[x*8+3] * 255 / *a;
225 *b = row[x*8+1] * 255 / *a;
229 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
230 ARGB *color)
232 BYTE r, g, b, a;
233 BYTE index;
234 BYTE *row;
235 TRACE("%p %d %d %p\n", bitmap, x, y, color);
237 if(!bitmap || !color ||
238 x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
239 return InvalidParameter;
241 row = bitmap->bits+bitmap->stride*y;
243 switch (bitmap->format)
245 case PixelFormat1bppIndexed:
246 getpixel_1bppIndexed(&index,row,x);
247 break;
248 case PixelFormat4bppIndexed:
249 getpixel_4bppIndexed(&index,row,x);
250 break;
251 case PixelFormat8bppIndexed:
252 getpixel_8bppIndexed(&index,row,x);
253 break;
254 case PixelFormat16bppGrayScale:
255 getpixel_16bppGrayScale(&r,&g,&b,&a,row,x);
256 break;
257 case PixelFormat16bppRGB555:
258 getpixel_16bppRGB555(&r,&g,&b,&a,row,x);
259 break;
260 case PixelFormat16bppRGB565:
261 getpixel_16bppRGB565(&r,&g,&b,&a,row,x);
262 break;
263 case PixelFormat16bppARGB1555:
264 getpixel_16bppARGB1555(&r,&g,&b,&a,row,x);
265 break;
266 case PixelFormat24bppRGB:
267 getpixel_24bppRGB(&r,&g,&b,&a,row,x);
268 break;
269 case PixelFormat32bppRGB:
270 getpixel_32bppRGB(&r,&g,&b,&a,row,x);
271 break;
272 case PixelFormat32bppARGB:
273 getpixel_32bppARGB(&r,&g,&b,&a,row,x);
274 break;
275 case PixelFormat32bppPARGB:
276 getpixel_32bppPARGB(&r,&g,&b,&a,row,x);
277 break;
278 case PixelFormat48bppRGB:
279 getpixel_48bppRGB(&r,&g,&b,&a,row,x);
280 break;
281 case PixelFormat64bppARGB:
282 getpixel_64bppARGB(&r,&g,&b,&a,row,x);
283 break;
284 case PixelFormat64bppPARGB:
285 getpixel_64bppPARGB(&r,&g,&b,&a,row,x);
286 break;
287 default:
288 FIXME("not implemented for format 0x%x\n", bitmap->format);
289 return NotImplemented;
292 if (bitmap->format & PixelFormatIndexed)
293 *color = bitmap->image.palette_entries[index];
294 else
295 *color = a<<24|r<<16|g<<8|b;
297 return Ok;
300 static inline UINT get_palette_index(BYTE r, BYTE g, BYTE b, BYTE a, GpBitmap* bitmap) {
301 BYTE index = 0;
302 int best_distance = 0x7fff;
303 int distance;
304 int i;
305 /* This algorithm scans entire palette,
306 computes difference from desired color (all color components have equal weight)
307 and returns the index of color with least difference.
309 Note: Maybe it could be replaced with a better algorithm for better image quality
310 and performance, though better algorithm would probably need some pre-built lookup
311 tables and thus may actually be slower if this method is called only few times per
312 every image.
314 for(i=0;i<bitmap->image.palette_size;i++) {
315 ARGB color=bitmap->image.palette_entries[i];
316 distance=abs(b-(color & 0xff)) + abs(g-(color>>8 & 0xff)) + abs(r-(color>>16 & 0xff)) + abs(a-(color>>24 & 0xff));
317 if (distance<best_distance) {
318 best_distance=distance;
319 index=i;
322 return index;
325 static inline void setpixel_8bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
326 BYTE *row, UINT x, GpBitmap* bitmap)
328 BYTE index = get_palette_index(r,g,b,a,bitmap);
329 row[x]=index;
332 static inline void setpixel_1bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
333 BYTE *row, UINT x, GpBitmap* bitmap)
335 row[x/8] = (row[x/8] & ~(1<<(7-x%8))) | (get_palette_index(r,g,b,a,bitmap)<<(7-x%8));
338 static inline void setpixel_4bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
339 BYTE *row, UINT x, GpBitmap* bitmap)
341 if (x & 1)
342 row[x/2] = (row[x/2] & 0xf0) | get_palette_index(r,g,b,a,bitmap);
343 else
344 row[x/2] = (row[x/2] & 0x0f) | get_palette_index(r,g,b,a,bitmap)<<4;
347 static inline void setpixel_16bppGrayScale(BYTE r, BYTE g, BYTE b, BYTE a,
348 BYTE *row, UINT x)
350 *((WORD*)(row)+x) = (r+g+b)*85;
353 static inline void setpixel_16bppRGB555(BYTE r, BYTE g, BYTE b, BYTE a,
354 BYTE *row, UINT x)
356 *((WORD*)(row)+x) = (r<<7&0x7c00)|
357 (g<<2&0x03e0)|
358 (b>>3&0x001f);
361 static inline void setpixel_16bppRGB565(BYTE r, BYTE g, BYTE b, BYTE a,
362 BYTE *row, UINT x)
364 *((WORD*)(row)+x) = (r<<8&0xf800)|
365 (g<<3&0x07e0)|
366 (b>>3&0x001f);
369 static inline void setpixel_16bppARGB1555(BYTE r, BYTE g, BYTE b, BYTE a,
370 BYTE *row, UINT x)
372 *((WORD*)(row)+x) = (a<<8&0x8000)|
373 (r<<7&0x7c00)|
374 (g<<2&0x03e0)|
375 (b>>3&0x001f);
378 static inline void setpixel_24bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
379 BYTE *row, UINT x)
381 row[x*3+2] = r;
382 row[x*3+1] = g;
383 row[x*3] = b;
386 static inline void setpixel_32bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
387 BYTE *row, UINT x)
389 *((DWORD*)(row)+x) = (r<<16)|(g<<8)|b;
392 static inline void setpixel_32bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
393 BYTE *row, UINT x)
395 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
398 static inline void setpixel_32bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
399 BYTE *row, UINT x)
401 r = r * a / 255;
402 g = g * a / 255;
403 b = b * a / 255;
404 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
407 static inline void setpixel_48bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
408 BYTE *row, UINT x)
410 row[x*6+5] = row[x*6+4] = r;
411 row[x*6+3] = row[x*6+2] = g;
412 row[x*6+1] = row[x*6] = b;
415 static inline void setpixel_64bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
416 BYTE *row, UINT x)
418 UINT64 a64=a, r64=r, g64=g, b64=b;
419 *((UINT64*)(row)+x) = (a64<<56)|(a64<<48)|(r64<<40)|(r64<<32)|(g64<<24)|(g64<<16)|(b64<<8)|b64;
422 static inline void setpixel_64bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
423 BYTE *row, UINT x)
425 UINT64 a64, r64, g64, b64;
426 a64 = a * 257;
427 r64 = r * a / 255;
428 g64 = g * a / 255;
429 b64 = b * a / 255;
430 *((UINT64*)(row)+x) = (a64<<48)|(r64<<32)|(g64<<16)|b64;
433 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
434 ARGB color)
436 BYTE a, r, g, b;
437 BYTE *row;
438 TRACE("bitmap:%p, x:%d, y:%d, color:%08x\n", bitmap, x, y, color);
440 if(!bitmap || x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
441 return InvalidParameter;
443 a = color>>24;
444 r = color>>16;
445 g = color>>8;
446 b = color;
448 row = bitmap->bits + bitmap->stride * y;
450 switch (bitmap->format)
452 case PixelFormat16bppGrayScale:
453 setpixel_16bppGrayScale(r,g,b,a,row,x);
454 break;
455 case PixelFormat16bppRGB555:
456 setpixel_16bppRGB555(r,g,b,a,row,x);
457 break;
458 case PixelFormat16bppRGB565:
459 setpixel_16bppRGB565(r,g,b,a,row,x);
460 break;
461 case PixelFormat16bppARGB1555:
462 setpixel_16bppARGB1555(r,g,b,a,row,x);
463 break;
464 case PixelFormat24bppRGB:
465 setpixel_24bppRGB(r,g,b,a,row,x);
466 break;
467 case PixelFormat32bppRGB:
468 setpixel_32bppRGB(r,g,b,a,row,x);
469 break;
470 case PixelFormat32bppARGB:
471 setpixel_32bppARGB(r,g,b,a,row,x);
472 break;
473 case PixelFormat32bppPARGB:
474 setpixel_32bppPARGB(r,g,b,a,row,x);
475 break;
476 case PixelFormat48bppRGB:
477 setpixel_48bppRGB(r,g,b,a,row,x);
478 break;
479 case PixelFormat64bppARGB:
480 setpixel_64bppARGB(r,g,b,a,row,x);
481 break;
482 case PixelFormat64bppPARGB:
483 setpixel_64bppPARGB(r,g,b,a,row,x);
484 break;
485 case PixelFormat8bppIndexed:
486 setpixel_8bppIndexed(r,g,b,a,row,x,bitmap);
487 break;
488 case PixelFormat4bppIndexed:
489 setpixel_4bppIndexed(r,g,b,a,row,x,bitmap);
490 break;
491 case PixelFormat1bppIndexed:
492 setpixel_1bppIndexed(r,g,b,a,row,x,bitmap);
493 break;
494 default:
495 FIXME("not implemented for format 0x%x\n", bitmap->format);
496 return NotImplemented;
499 return Ok;
502 GpStatus convert_pixels(INT width, INT height,
503 INT dst_stride, BYTE *dst_bits, PixelFormat dst_format,
504 INT src_stride, const BYTE *src_bits, PixelFormat src_format, ARGB *src_palette)
506 INT x, y;
508 if (src_format == dst_format ||
509 (dst_format == PixelFormat32bppRGB && PIXELFORMATBPP(src_format) == 32))
511 UINT widthbytes = PIXELFORMATBPP(src_format) * width / 8;
512 for (y=0; y<height; y++)
513 memcpy(dst_bits+dst_stride*y, src_bits+src_stride*y, widthbytes);
514 return Ok;
517 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
518 for (x=0; x<width; x++) \
519 for (y=0; y<height; y++) { \
520 BYTE index; \
521 BYTE *color; \
522 getpixel_function(&index, src_bits+src_stride*y, x); \
523 color = (BYTE*)(&src_palette[index]); \
524 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
526 return Ok; \
527 } while (0);
529 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
530 for (x=0; x<width; x++) \
531 for (y=0; y<height; y++) { \
532 BYTE r, g, b, a; \
533 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
534 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
536 return Ok; \
537 } while (0);
539 switch (src_format)
541 case PixelFormat1bppIndexed:
542 switch (dst_format)
544 case PixelFormat16bppGrayScale:
545 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppGrayScale);
546 case PixelFormat16bppRGB555:
547 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB555);
548 case PixelFormat16bppRGB565:
549 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB565);
550 case PixelFormat16bppARGB1555:
551 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppARGB1555);
552 case PixelFormat24bppRGB:
553 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_24bppRGB);
554 case PixelFormat32bppRGB:
555 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppRGB);
556 case PixelFormat32bppARGB:
557 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppARGB);
558 case PixelFormat32bppPARGB:
559 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppPARGB);
560 case PixelFormat48bppRGB:
561 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_48bppRGB);
562 case PixelFormat64bppARGB:
563 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_64bppARGB);
564 default:
565 break;
567 break;
568 case PixelFormat4bppIndexed:
569 switch (dst_format)
571 case PixelFormat16bppGrayScale:
572 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppGrayScale);
573 case PixelFormat16bppRGB555:
574 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB555);
575 case PixelFormat16bppRGB565:
576 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB565);
577 case PixelFormat16bppARGB1555:
578 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppARGB1555);
579 case PixelFormat24bppRGB:
580 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_24bppRGB);
581 case PixelFormat32bppRGB:
582 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppRGB);
583 case PixelFormat32bppARGB:
584 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppARGB);
585 case PixelFormat32bppPARGB:
586 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppPARGB);
587 case PixelFormat48bppRGB:
588 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_48bppRGB);
589 case PixelFormat64bppARGB:
590 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_64bppARGB);
591 default:
592 break;
594 break;
595 case PixelFormat8bppIndexed:
596 switch (dst_format)
598 case PixelFormat16bppGrayScale:
599 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppGrayScale);
600 case PixelFormat16bppRGB555:
601 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB555);
602 case PixelFormat16bppRGB565:
603 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB565);
604 case PixelFormat16bppARGB1555:
605 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppARGB1555);
606 case PixelFormat24bppRGB:
607 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_24bppRGB);
608 case PixelFormat32bppRGB:
609 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppRGB);
610 case PixelFormat32bppARGB:
611 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppARGB);
612 case PixelFormat32bppPARGB:
613 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppPARGB);
614 case PixelFormat48bppRGB:
615 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_48bppRGB);
616 case PixelFormat64bppARGB:
617 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_64bppARGB);
618 default:
619 break;
621 break;
622 case PixelFormat16bppGrayScale:
623 switch (dst_format)
625 case PixelFormat16bppRGB555:
626 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB555);
627 case PixelFormat16bppRGB565:
628 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB565);
629 case PixelFormat16bppARGB1555:
630 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppARGB1555);
631 case PixelFormat24bppRGB:
632 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_24bppRGB);
633 case PixelFormat32bppRGB:
634 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppRGB);
635 case PixelFormat32bppARGB:
636 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppARGB);
637 case PixelFormat32bppPARGB:
638 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppPARGB);
639 case PixelFormat48bppRGB:
640 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_48bppRGB);
641 case PixelFormat64bppARGB:
642 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_64bppARGB);
643 default:
644 break;
646 break;
647 case PixelFormat16bppRGB555:
648 switch (dst_format)
650 case PixelFormat16bppGrayScale:
651 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppGrayScale);
652 case PixelFormat16bppRGB565:
653 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppRGB565);
654 case PixelFormat16bppARGB1555:
655 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppARGB1555);
656 case PixelFormat24bppRGB:
657 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_24bppRGB);
658 case PixelFormat32bppRGB:
659 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppRGB);
660 case PixelFormat32bppARGB:
661 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppARGB);
662 case PixelFormat32bppPARGB:
663 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppPARGB);
664 case PixelFormat48bppRGB:
665 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_48bppRGB);
666 case PixelFormat64bppARGB:
667 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_64bppARGB);
668 default:
669 break;
671 break;
672 case PixelFormat16bppRGB565:
673 switch (dst_format)
675 case PixelFormat16bppGrayScale:
676 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppGrayScale);
677 case PixelFormat16bppRGB555:
678 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppRGB555);
679 case PixelFormat16bppARGB1555:
680 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppARGB1555);
681 case PixelFormat24bppRGB:
682 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_24bppRGB);
683 case PixelFormat32bppRGB:
684 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppRGB);
685 case PixelFormat32bppARGB:
686 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppARGB);
687 case PixelFormat32bppPARGB:
688 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppPARGB);
689 case PixelFormat48bppRGB:
690 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_48bppRGB);
691 case PixelFormat64bppARGB:
692 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_64bppARGB);
693 default:
694 break;
696 break;
697 case PixelFormat16bppARGB1555:
698 switch (dst_format)
700 case PixelFormat16bppGrayScale:
701 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppGrayScale);
702 case PixelFormat16bppRGB555:
703 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB555);
704 case PixelFormat16bppRGB565:
705 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB565);
706 case PixelFormat24bppRGB:
707 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_24bppRGB);
708 case PixelFormat32bppRGB:
709 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppRGB);
710 case PixelFormat32bppARGB:
711 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppARGB);
712 case PixelFormat32bppPARGB:
713 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppPARGB);
714 case PixelFormat48bppRGB:
715 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_48bppRGB);
716 case PixelFormat64bppARGB:
717 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_64bppARGB);
718 default:
719 break;
721 break;
722 case PixelFormat24bppRGB:
723 switch (dst_format)
725 case PixelFormat16bppGrayScale:
726 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppGrayScale);
727 case PixelFormat16bppRGB555:
728 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB555);
729 case PixelFormat16bppRGB565:
730 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB565);
731 case PixelFormat16bppARGB1555:
732 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppARGB1555);
733 case PixelFormat32bppRGB:
734 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppRGB);
735 case PixelFormat32bppARGB:
736 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppARGB);
737 case PixelFormat32bppPARGB:
738 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppPARGB);
739 case PixelFormat48bppRGB:
740 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_48bppRGB);
741 case PixelFormat64bppARGB:
742 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_64bppARGB);
743 default:
744 break;
746 break;
747 case PixelFormat32bppRGB:
748 switch (dst_format)
750 case PixelFormat16bppGrayScale:
751 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppGrayScale);
752 case PixelFormat16bppRGB555:
753 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB555);
754 case PixelFormat16bppRGB565:
755 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB565);
756 case PixelFormat16bppARGB1555:
757 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppARGB1555);
758 case PixelFormat24bppRGB:
759 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_24bppRGB);
760 case PixelFormat32bppARGB:
761 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppARGB);
762 case PixelFormat32bppPARGB:
763 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppPARGB);
764 case PixelFormat48bppRGB:
765 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_48bppRGB);
766 case PixelFormat64bppARGB:
767 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_64bppARGB);
768 default:
769 break;
771 break;
772 case PixelFormat32bppARGB:
773 switch (dst_format)
775 case PixelFormat16bppGrayScale:
776 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppGrayScale);
777 case PixelFormat16bppRGB555:
778 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB555);
779 case PixelFormat16bppRGB565:
780 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB565);
781 case PixelFormat16bppARGB1555:
782 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppARGB1555);
783 case PixelFormat24bppRGB:
784 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_24bppRGB);
785 case PixelFormat32bppPARGB:
786 convert_32bppARGB_to_32bppPARGB(width, height, dst_bits, dst_stride, src_bits, src_stride);
787 return Ok;
788 case PixelFormat48bppRGB:
789 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_48bppRGB);
790 case PixelFormat64bppARGB:
791 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_64bppARGB);
792 default:
793 break;
795 break;
796 case PixelFormat32bppPARGB:
797 switch (dst_format)
799 case PixelFormat16bppGrayScale:
800 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppGrayScale);
801 case PixelFormat16bppRGB555:
802 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB555);
803 case PixelFormat16bppRGB565:
804 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB565);
805 case PixelFormat16bppARGB1555:
806 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppARGB1555);
807 case PixelFormat24bppRGB:
808 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_24bppRGB);
809 case PixelFormat32bppRGB:
810 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppRGB);
811 case PixelFormat32bppARGB:
812 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppARGB);
813 case PixelFormat48bppRGB:
814 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_48bppRGB);
815 case PixelFormat64bppARGB:
816 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_64bppARGB);
817 default:
818 break;
820 break;
821 case PixelFormat48bppRGB:
822 switch (dst_format)
824 case PixelFormat16bppGrayScale:
825 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppGrayScale);
826 case PixelFormat16bppRGB555:
827 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB555);
828 case PixelFormat16bppRGB565:
829 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB565);
830 case PixelFormat16bppARGB1555:
831 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppARGB1555);
832 case PixelFormat24bppRGB:
833 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_24bppRGB);
834 case PixelFormat32bppRGB:
835 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppRGB);
836 case PixelFormat32bppARGB:
837 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppARGB);
838 case PixelFormat32bppPARGB:
839 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppPARGB);
840 case PixelFormat64bppARGB:
841 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_64bppARGB);
842 default:
843 break;
845 break;
846 case PixelFormat64bppARGB:
847 switch (dst_format)
849 case PixelFormat16bppGrayScale:
850 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppGrayScale);
851 case PixelFormat16bppRGB555:
852 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB555);
853 case PixelFormat16bppRGB565:
854 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB565);
855 case PixelFormat16bppARGB1555:
856 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppARGB1555);
857 case PixelFormat24bppRGB:
858 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_24bppRGB);
859 case PixelFormat32bppRGB:
860 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppRGB);
861 case PixelFormat32bppARGB:
862 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppARGB);
863 case PixelFormat32bppPARGB:
864 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppPARGB);
865 case PixelFormat48bppRGB:
866 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_48bppRGB);
867 default:
868 break;
870 break;
871 case PixelFormat64bppPARGB:
872 switch (dst_format)
874 case PixelFormat16bppGrayScale:
875 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppGrayScale);
876 case PixelFormat16bppRGB555:
877 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB555);
878 case PixelFormat16bppRGB565:
879 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB565);
880 case PixelFormat16bppARGB1555:
881 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppARGB1555);
882 case PixelFormat24bppRGB:
883 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_24bppRGB);
884 case PixelFormat32bppRGB:
885 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppRGB);
886 case PixelFormat32bppARGB:
887 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppARGB);
888 case PixelFormat32bppPARGB:
889 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppPARGB);
890 case PixelFormat48bppRGB:
891 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_48bppRGB);
892 case PixelFormat64bppARGB:
893 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_64bppARGB);
894 default:
895 break;
897 break;
898 default:
899 break;
902 #undef convert_indexed_to_rgb
903 #undef convert_rgb_to_rgb
905 return NotImplemented;
908 /* This function returns a pointer to an array of pixels that represents the
909 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
910 * flags. It is correct behavior that a user who calls this function with write
911 * privileges can write to the whole bitmap (not just the area in rect).
913 * FIXME: only used portion of format is bits per pixel. */
914 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
915 UINT flags, PixelFormat format, BitmapData* lockeddata)
917 INT bitspp = PIXELFORMATBPP(format);
918 GpRect act_rect; /* actual rect to be used */
919 GpStatus stat;
921 TRACE("%p %p %d 0x%x %p\n", bitmap, rect, flags, format, lockeddata);
923 if(!lockeddata || !bitmap)
924 return InvalidParameter;
926 if(rect){
927 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
928 (rect->Y + rect->Height > bitmap->height) || !flags)
929 return InvalidParameter;
931 act_rect = *rect;
933 else{
934 act_rect.X = act_rect.Y = 0;
935 act_rect.Width = bitmap->width;
936 act_rect.Height = bitmap->height;
939 if(bitmap->lockmode)
941 WARN("bitmap is already locked and cannot be locked again\n");
942 return WrongState;
945 if (bitmap->bits && bitmap->format == format && !(flags & ImageLockModeUserInputBuf))
947 /* no conversion is necessary; just use the bits directly */
948 lockeddata->Width = act_rect.Width;
949 lockeddata->Height = act_rect.Height;
950 lockeddata->PixelFormat = format;
951 lockeddata->Reserved = flags;
952 lockeddata->Stride = bitmap->stride;
953 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
954 bitmap->stride * act_rect.Y;
956 bitmap->lockmode = flags;
957 bitmap->numlocks++;
959 return Ok;
962 /* Make sure we can convert to the requested format. */
963 if (flags & ImageLockModeRead)
965 stat = convert_pixels(0, 0, 0, NULL, format, 0, NULL, bitmap->format, NULL);
966 if (stat == NotImplemented)
968 FIXME("cannot read bitmap from %x to %x\n", bitmap->format, format);
969 return NotImplemented;
973 /* If we're opening for writing, make sure we'll be able to write back in
974 * the original format. */
975 if (flags & ImageLockModeWrite)
977 stat = convert_pixels(0, 0, 0, NULL, bitmap->format, 0, NULL, format, NULL);
978 if (stat == NotImplemented)
980 FIXME("cannot write bitmap from %x to %x\n", format, bitmap->format);
981 return NotImplemented;
985 lockeddata->Width = act_rect.Width;
986 lockeddata->Height = act_rect.Height;
987 lockeddata->PixelFormat = format;
988 lockeddata->Reserved = flags;
990 if(!(flags & ImageLockModeUserInputBuf))
992 lockeddata->Stride = (((act_rect.Width * bitspp + 7) / 8) + 3) & ~3;
994 bitmap->bitmapbits = GdipAlloc(lockeddata->Stride * act_rect.Height);
996 if (!bitmap->bitmapbits) return OutOfMemory;
998 lockeddata->Scan0 = bitmap->bitmapbits;
1001 if (flags & ImageLockModeRead)
1003 static int fixme=0;
1005 if (!fixme && (PIXELFORMATBPP(bitmap->format) * act_rect.X) % 8 != 0)
1007 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1008 fixme = 1;
1011 stat = convert_pixels(act_rect.Width, act_rect.Height,
1012 lockeddata->Stride, lockeddata->Scan0, format,
1013 bitmap->stride,
1014 bitmap->bits + bitmap->stride * act_rect.Y + PIXELFORMATBPP(bitmap->format) * act_rect.X / 8,
1015 bitmap->format, bitmap->image.palette_entries);
1017 if (stat != Ok)
1019 GdipFree(bitmap->bitmapbits);
1020 bitmap->bitmapbits = NULL;
1021 return stat;
1025 bitmap->lockmode = flags;
1026 bitmap->numlocks++;
1027 bitmap->lockx = act_rect.X;
1028 bitmap->locky = act_rect.Y;
1030 return Ok;
1033 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
1035 TRACE("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
1037 if (!bitmap || xdpi == 0.0 || ydpi == 0.0)
1038 return InvalidParameter;
1040 bitmap->image.xres = xdpi;
1041 bitmap->image.yres = ydpi;
1043 return Ok;
1046 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
1047 BitmapData* lockeddata)
1049 GpStatus stat;
1050 static int fixme=0;
1052 TRACE("(%p,%p)\n", bitmap, lockeddata);
1054 if(!bitmap || !lockeddata)
1055 return InvalidParameter;
1057 if(!bitmap->lockmode)
1058 return WrongState;
1060 if(!(lockeddata->Reserved & ImageLockModeWrite)){
1061 if(!(--bitmap->numlocks))
1062 bitmap->lockmode = 0;
1064 GdipFree(bitmap->bitmapbits);
1065 bitmap->bitmapbits = NULL;
1066 return Ok;
1069 if (!bitmap->bitmapbits && !(lockeddata->Reserved & ImageLockModeUserInputBuf))
1071 /* we passed a direct reference; no need to do anything */
1072 bitmap->lockmode = 0;
1073 bitmap->numlocks = 0;
1074 return Ok;
1077 if (!fixme && (PIXELFORMATBPP(bitmap->format) * bitmap->lockx) % 8 != 0)
1079 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1080 fixme = 1;
1083 stat = convert_pixels(lockeddata->Width, lockeddata->Height,
1084 bitmap->stride,
1085 bitmap->bits + bitmap->stride * bitmap->locky + PIXELFORMATBPP(bitmap->format) * bitmap->lockx / 8,
1086 bitmap->format,
1087 lockeddata->Stride, lockeddata->Scan0, lockeddata->PixelFormat, NULL);
1089 if (stat != Ok)
1091 ERR("failed to convert pixels; this should never happen\n");
1094 GdipFree(bitmap->bitmapbits);
1095 bitmap->bitmapbits = NULL;
1096 bitmap->lockmode = 0;
1097 bitmap->numlocks = 0;
1099 return stat;
1102 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
1103 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1105 BitmapData lockeddata_src, lockeddata_dst;
1106 int i;
1107 UINT row_size;
1108 Rect area;
1109 GpStatus stat;
1111 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1113 if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
1114 x < 0 || y < 0 ||
1115 x + width > srcBitmap->width || y + height > srcBitmap->height)
1117 TRACE("<-- InvalidParameter\n");
1118 return InvalidParameter;
1121 if (format == PixelFormatDontCare)
1122 format = srcBitmap->format;
1124 area.X = roundr(x);
1125 area.Y = roundr(y);
1126 area.Width = roundr(width);
1127 area.Height = roundr(height);
1129 stat = GdipBitmapLockBits(srcBitmap, &area, ImageLockModeRead, format,
1130 &lockeddata_src);
1131 if (stat != Ok) return stat;
1133 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
1134 0, lockeddata_src.PixelFormat, NULL, dstBitmap);
1135 if (stat == Ok)
1137 stat = GdipBitmapLockBits(*dstBitmap, NULL, ImageLockModeWrite,
1138 lockeddata_src.PixelFormat, &lockeddata_dst);
1140 if (stat == Ok)
1142 /* copy the image data */
1143 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
1144 for (i=0; i<lockeddata_src.Height; i++)
1145 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
1146 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
1147 row_size);
1149 GdipBitmapUnlockBits(*dstBitmap, &lockeddata_dst);
1152 if (stat != Ok)
1153 GdipDisposeImage((GpImage*)*dstBitmap);
1156 GdipBitmapUnlockBits(srcBitmap, &lockeddata_src);
1158 if (stat != Ok)
1160 *dstBitmap = NULL;
1163 return stat;
1166 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
1167 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1169 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1171 return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
1174 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
1176 GpStatus stat = GenericError;
1178 TRACE("%p, %p\n", image, cloneImage);
1180 if (!image || !cloneImage)
1181 return InvalidParameter;
1183 if (image->picture)
1185 IStream* stream;
1186 HRESULT hr;
1187 INT size;
1188 LARGE_INTEGER move;
1190 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
1191 if (FAILED(hr))
1192 return GenericError;
1194 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
1195 if(FAILED(hr))
1197 WARN("Failed to save image on stream\n");
1198 goto out;
1201 /* Set seek pointer back to the beginning of the picture */
1202 move.QuadPart = 0;
1203 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1204 if (FAILED(hr))
1205 goto out;
1207 stat = GdipLoadImageFromStream(stream, cloneImage);
1208 if (stat != Ok) WARN("Failed to load image from stream\n");
1210 out:
1211 IStream_Release(stream);
1212 return stat;
1214 else if (image->type == ImageTypeBitmap)
1216 GpBitmap *bitmap = (GpBitmap*)image;
1217 BitmapData lockeddata_src, lockeddata_dst;
1218 int i;
1219 UINT row_size;
1221 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format,
1222 &lockeddata_src);
1223 if (stat != Ok) return stat;
1225 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
1226 0, lockeddata_src.PixelFormat, NULL, (GpBitmap**)cloneImage);
1227 if (stat == Ok)
1229 stat = GdipBitmapLockBits((GpBitmap*)*cloneImage, NULL, ImageLockModeWrite,
1230 lockeddata_src.PixelFormat, &lockeddata_dst);
1232 if (stat == Ok)
1234 /* copy the image data */
1235 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
1236 for (i=0; i<lockeddata_src.Height; i++)
1237 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
1238 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
1239 row_size);
1241 GdipBitmapUnlockBits((GpBitmap*)*cloneImage, &lockeddata_dst);
1244 if (stat != Ok)
1245 GdipDisposeImage(*cloneImage);
1248 GdipBitmapUnlockBits(bitmap, &lockeddata_src);
1250 if (stat != Ok)
1252 *cloneImage = NULL;
1254 else memcpy(&(*cloneImage)->format, &image->format, sizeof(GUID));
1256 return stat;
1258 else
1260 ERR("GpImage with no IPicture or bitmap?!\n");
1261 return NotImplemented;
1265 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
1266 GpBitmap **bitmap)
1268 GpStatus stat;
1269 IStream *stream;
1271 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1273 if(!filename || !bitmap)
1274 return InvalidParameter;
1276 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1278 if(stat != Ok)
1279 return stat;
1281 stat = GdipCreateBitmapFromStream(stream, bitmap);
1283 IStream_Release(stream);
1285 return stat;
1288 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
1289 VOID *bits, GpBitmap **bitmap)
1291 DWORD height, stride;
1292 PixelFormat format;
1294 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
1296 if (!info || !bits || !bitmap)
1297 return InvalidParameter;
1299 height = abs(info->bmiHeader.biHeight);
1300 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
1302 if(info->bmiHeader.biHeight > 0) /* bottom-up */
1304 bits = (BYTE*)bits + (height - 1) * stride;
1305 stride = -stride;
1308 switch(info->bmiHeader.biBitCount) {
1309 case 1:
1310 format = PixelFormat1bppIndexed;
1311 break;
1312 case 4:
1313 format = PixelFormat4bppIndexed;
1314 break;
1315 case 8:
1316 format = PixelFormat8bppIndexed;
1317 break;
1318 case 16:
1319 format = PixelFormat16bppRGB555;
1320 break;
1321 case 24:
1322 format = PixelFormat24bppRGB;
1323 break;
1324 case 32:
1325 format = PixelFormat32bppRGB;
1326 break;
1327 default:
1328 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
1329 *bitmap = NULL;
1330 return InvalidParameter;
1333 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
1334 bits, bitmap);
1338 /* FIXME: no icm */
1339 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
1340 GpBitmap **bitmap)
1342 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1344 return GdipCreateBitmapFromFile(filename, bitmap);
1347 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
1348 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
1350 HBITMAP hbm;
1351 GpStatus stat = InvalidParameter;
1353 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
1355 if(!lpBitmapName || !bitmap)
1356 return InvalidParameter;
1358 /* load DIB */
1359 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
1360 LR_CREATEDIBSECTION);
1362 if(hbm){
1363 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1364 DeleteObject(hbm);
1367 return stat;
1370 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
1371 HBITMAP* hbmReturn, ARGB background)
1373 GpStatus stat;
1374 HBITMAP result;
1375 UINT width, height;
1376 BITMAPINFOHEADER bih;
1377 LPBYTE bits;
1378 BitmapData lockeddata;
1379 TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
1381 if (!bitmap || !hbmReturn) return InvalidParameter;
1383 GdipGetImageWidth((GpImage*)bitmap, &width);
1384 GdipGetImageHeight((GpImage*)bitmap, &height);
1386 bih.biSize = sizeof(bih);
1387 bih.biWidth = width;
1388 bih.biHeight = height;
1389 bih.biPlanes = 1;
1390 bih.biBitCount = 32;
1391 bih.biCompression = BI_RGB;
1392 bih.biSizeImage = 0;
1393 bih.biXPelsPerMeter = 0;
1394 bih.biYPelsPerMeter = 0;
1395 bih.biClrUsed = 0;
1396 bih.biClrImportant = 0;
1398 result = CreateDIBSection(0, (BITMAPINFO*)&bih, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1400 if (result)
1402 lockeddata.Stride = -width * 4;
1403 lockeddata.Scan0 = bits + (width * 4 * (height - 1));
1405 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead|ImageLockModeUserInputBuf,
1406 PixelFormat32bppPARGB, &lockeddata);
1408 if (stat == Ok)
1409 stat = GdipBitmapUnlockBits(bitmap, &lockeddata);
1411 else
1412 stat = GenericError;
1414 if (stat != Ok && result)
1416 DeleteObject(result);
1417 result = NULL;
1420 *hbmReturn = result;
1422 return stat;
1425 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
1426 GpMetafile* metafile, BOOL* succ, EmfType emfType,
1427 const WCHAR* description, GpMetafile** out_metafile)
1429 static int calls;
1431 TRACE("(%p,%p,%p,%u,%s,%p)\n", ref, metafile, succ, emfType,
1432 debugstr_w(description), out_metafile);
1434 if(!ref || !metafile || !out_metafile)
1435 return InvalidParameter;
1437 *succ = FALSE;
1438 *out_metafile = NULL;
1440 if(!(calls++))
1441 FIXME("not implemented\n");
1443 return NotImplemented;
1446 /* FIXME: this should create a bitmap in the given size with the attributes
1447 * (resolution etc.) of the graphics object */
1448 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
1449 GpGraphics* target, GpBitmap** bitmap)
1451 static int calls;
1452 GpStatus ret;
1454 TRACE("(%d, %d, %p, %p)\n", width, height, target, bitmap);
1456 if(!target || !bitmap)
1457 return InvalidParameter;
1459 if(!(calls++))
1460 FIXME("hacked stub\n");
1462 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
1463 NULL, bitmap);
1465 return ret;
1468 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1470 GpStatus stat;
1471 ICONINFO iinfo;
1472 BITMAP bm;
1473 int ret;
1474 UINT width, height;
1475 GpRect rect;
1476 BitmapData lockeddata;
1477 HDC screendc;
1478 BOOL has_alpha;
1479 int x, y;
1480 BYTE *bits;
1481 BITMAPINFOHEADER bih;
1482 DWORD *src;
1483 BYTE *dst_row;
1484 DWORD *dst;
1486 TRACE("%p, %p\n", hicon, bitmap);
1488 if(!bitmap || !GetIconInfo(hicon, &iinfo))
1489 return InvalidParameter;
1491 /* get the size of the icon */
1492 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1493 if (ret == 0) {
1494 DeleteObject(iinfo.hbmColor);
1495 DeleteObject(iinfo.hbmMask);
1496 return GenericError;
1499 width = bm.bmWidth;
1501 if (iinfo.hbmColor)
1502 height = abs(bm.bmHeight);
1503 else /* combined bitmap + mask */
1504 height = abs(bm.bmHeight) / 2;
1506 bits = HeapAlloc(GetProcessHeap(), 0, 4*width*height);
1507 if (!bits) {
1508 DeleteObject(iinfo.hbmColor);
1509 DeleteObject(iinfo.hbmMask);
1510 return OutOfMemory;
1513 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB, NULL, bitmap);
1514 if (stat != Ok) {
1515 DeleteObject(iinfo.hbmColor);
1516 DeleteObject(iinfo.hbmMask);
1517 HeapFree(GetProcessHeap(), 0, bits);
1518 return stat;
1521 rect.X = 0;
1522 rect.Y = 0;
1523 rect.Width = width;
1524 rect.Height = height;
1526 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1527 if (stat != Ok) {
1528 DeleteObject(iinfo.hbmColor);
1529 DeleteObject(iinfo.hbmMask);
1530 HeapFree(GetProcessHeap(), 0, bits);
1531 GdipDisposeImage((GpImage*)*bitmap);
1532 return stat;
1535 bih.biSize = sizeof(bih);
1536 bih.biWidth = width;
1537 bih.biHeight = -height;
1538 bih.biPlanes = 1;
1539 bih.biBitCount = 32;
1540 bih.biCompression = BI_RGB;
1541 bih.biSizeImage = 0;
1542 bih.biXPelsPerMeter = 0;
1543 bih.biYPelsPerMeter = 0;
1544 bih.biClrUsed = 0;
1545 bih.biClrImportant = 0;
1547 screendc = GetDC(0);
1548 if (iinfo.hbmColor)
1550 GetDIBits(screendc, iinfo.hbmColor, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1552 if (bm.bmBitsPixel == 32)
1554 has_alpha = FALSE;
1556 /* If any pixel has a non-zero alpha, ignore hbmMask */
1557 src = (DWORD*)bits;
1558 for (x=0; x<width && !has_alpha; x++)
1559 for (y=0; y<height && !has_alpha; y++)
1560 if ((*src++ & 0xff000000) != 0)
1561 has_alpha = TRUE;
1563 else has_alpha = FALSE;
1565 else
1567 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1568 has_alpha = FALSE;
1571 /* copy the image data to the Bitmap */
1572 src = (DWORD*)bits;
1573 dst_row = lockeddata.Scan0;
1574 for (y=0; y<height; y++)
1576 memcpy(dst_row, src, width*4);
1577 src += width;
1578 dst_row += lockeddata.Stride;
1581 if (!has_alpha)
1583 if (iinfo.hbmMask)
1585 /* read alpha data from the mask */
1586 if (iinfo.hbmColor)
1587 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1588 else
1589 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1591 src = (DWORD*)bits;
1592 dst_row = lockeddata.Scan0;
1593 for (y=0; y<height; y++)
1595 dst = (DWORD*)dst_row;
1596 for (x=0; x<height; x++)
1598 DWORD src_value = *src++;
1599 if (src_value)
1600 *dst++ = 0;
1601 else
1602 *dst++ |= 0xff000000;
1604 dst_row += lockeddata.Stride;
1607 else
1609 /* set constant alpha of 255 */
1610 dst_row = bits;
1611 for (y=0; y<height; y++)
1613 dst = (DWORD*)dst_row;
1614 for (x=0; x<height; x++)
1615 *dst++ |= 0xff000000;
1616 dst_row += lockeddata.Stride;
1621 ReleaseDC(0, screendc);
1623 DeleteObject(iinfo.hbmColor);
1624 DeleteObject(iinfo.hbmMask);
1626 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1628 HeapFree(GetProcessHeap(), 0, bits);
1630 return Ok;
1633 static void generate_halftone_palette(ARGB *entries, UINT count)
1635 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1636 UINT i;
1638 for (i=0; i<8 && i<count; i++)
1640 entries[i] = 0xff000000;
1641 if (i&1) entries[i] |= 0x800000;
1642 if (i&2) entries[i] |= 0x8000;
1643 if (i&4) entries[i] |= 0x80;
1646 if (8 < count)
1647 entries[i] = 0xffc0c0c0;
1649 for (i=9; i<16 && i<count; i++)
1651 entries[i] = 0xff000000;
1652 if (i&1) entries[i] |= 0xff0000;
1653 if (i&2) entries[i] |= 0xff00;
1654 if (i&4) entries[i] |= 0xff;
1657 for (i=16; i<40 && i<count; i++)
1659 entries[i] = 0;
1662 for (i=40; i<256 && i<count; i++)
1664 entries[i] = 0xff000000;
1665 entries[i] |= halftone_values[(i-40)%6];
1666 entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1667 entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1671 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1673 HDC screendc = GetDC(0);
1675 if (!screendc) return GenericError;
1677 *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1678 *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1680 ReleaseDC(0, screendc);
1682 return Ok;
1685 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1686 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1688 BITMAPINFO* pbmi;
1689 HBITMAP hbitmap=NULL;
1690 INT row_size, dib_stride;
1691 BYTE *bits=NULL, *own_bits=NULL;
1692 REAL xres, yres;
1693 GpStatus stat;
1695 TRACE("%d %d %d 0x%x %p %p\n", width, height, stride, format, scan0, bitmap);
1697 if (!bitmap) return InvalidParameter;
1699 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1700 *bitmap = NULL;
1701 return InvalidParameter;
1704 if(scan0 && !stride)
1705 return InvalidParameter;
1707 stat = get_screen_resolution(&xres, &yres);
1708 if (stat != Ok) return stat;
1710 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1711 dib_stride = (row_size + 3) & ~3;
1713 if(stride == 0)
1714 stride = dib_stride;
1716 if (format & PixelFormatGDI && !(format & (PixelFormatAlpha|PixelFormatIndexed)) && !scan0)
1718 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1719 if (!pbmi)
1720 return OutOfMemory;
1722 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1723 pbmi->bmiHeader.biWidth = width;
1724 pbmi->bmiHeader.biHeight = -height;
1725 pbmi->bmiHeader.biPlanes = 1;
1726 /* FIXME: use the rest of the data from format */
1727 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(format);
1728 pbmi->bmiHeader.biCompression = BI_RGB;
1729 pbmi->bmiHeader.biSizeImage = 0;
1730 pbmi->bmiHeader.biXPelsPerMeter = 0;
1731 pbmi->bmiHeader.biYPelsPerMeter = 0;
1732 pbmi->bmiHeader.biClrUsed = 0;
1733 pbmi->bmiHeader.biClrImportant = 0;
1735 hbitmap = CreateDIBSection(0, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1737 GdipFree(pbmi);
1739 if (!hbitmap) return GenericError;
1741 stride = dib_stride;
1743 else
1745 /* Not a GDI format; don't try to make an HBITMAP. */
1746 if (scan0)
1747 bits = scan0;
1748 else
1750 INT size = abs(stride) * height;
1752 own_bits = bits = GdipAlloc(size);
1753 if (!own_bits) return OutOfMemory;
1755 if (stride < 0)
1756 bits += stride * (1 - height);
1760 *bitmap = GdipAlloc(sizeof(GpBitmap));
1761 if(!*bitmap)
1763 DeleteObject(hbitmap);
1764 GdipFree(own_bits);
1765 return OutOfMemory;
1768 (*bitmap)->image.type = ImageTypeBitmap;
1769 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1770 (*bitmap)->image.flags = ImageFlagsNone;
1771 (*bitmap)->image.frame_count = 1;
1772 (*bitmap)->image.current_frame = 0;
1773 (*bitmap)->image.palette_flags = 0;
1774 (*bitmap)->image.palette_count = 0;
1775 (*bitmap)->image.palette_size = 0;
1776 (*bitmap)->image.palette_entries = NULL;
1777 (*bitmap)->image.xres = xres;
1778 (*bitmap)->image.yres = yres;
1779 (*bitmap)->width = width;
1780 (*bitmap)->height = height;
1781 (*bitmap)->format = format;
1782 (*bitmap)->image.picture = NULL;
1783 (*bitmap)->image.stream = NULL;
1784 (*bitmap)->hbitmap = hbitmap;
1785 (*bitmap)->hdc = NULL;
1786 (*bitmap)->bits = bits;
1787 (*bitmap)->stride = stride;
1788 (*bitmap)->own_bits = own_bits;
1789 (*bitmap)->metadata_reader = NULL;
1791 /* set format-related flags */
1792 if (format & (PixelFormatAlpha|PixelFormatPAlpha|PixelFormatIndexed))
1793 (*bitmap)->image.flags |= ImageFlagsHasAlpha;
1795 if (format == PixelFormat1bppIndexed ||
1796 format == PixelFormat4bppIndexed ||
1797 format == PixelFormat8bppIndexed)
1799 (*bitmap)->image.palette_size = (*bitmap)->image.palette_count = 1 << PIXELFORMATBPP(format);
1800 (*bitmap)->image.palette_entries = GdipAlloc(sizeof(ARGB) * ((*bitmap)->image.palette_size));
1802 if (!(*bitmap)->image.palette_entries)
1804 GdipDisposeImage(&(*bitmap)->image);
1805 *bitmap = NULL;
1806 return OutOfMemory;
1809 if (format == PixelFormat1bppIndexed)
1811 (*bitmap)->image.palette_flags = PaletteFlagsGrayScale;
1812 (*bitmap)->image.palette_entries[0] = 0xff000000;
1813 (*bitmap)->image.palette_entries[1] = 0xffffffff;
1815 else
1817 if (format == PixelFormat8bppIndexed)
1818 (*bitmap)->image.palette_flags = PaletteFlagsHalftone;
1820 generate_halftone_palette((*bitmap)->image.palette_entries,
1821 (*bitmap)->image.palette_count);
1825 TRACE("<-- %p\n", *bitmap);
1827 return Ok;
1830 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1831 GpBitmap **bitmap)
1833 GpStatus stat;
1835 TRACE("%p %p\n", stream, bitmap);
1837 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1839 if(stat != Ok)
1840 return stat;
1842 if((*bitmap)->image.type != ImageTypeBitmap){
1843 GdipDisposeImage(&(*bitmap)->image);
1844 *bitmap = NULL;
1845 return GenericError; /* FIXME: what error to return? */
1848 return Ok;
1851 /* FIXME: no icm */
1852 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1853 GpBitmap **bitmap)
1855 TRACE("%p %p\n", stream, bitmap);
1857 return GdipCreateBitmapFromStream(stream, bitmap);
1860 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1861 GpCachedBitmap **cachedbmp)
1863 GpStatus stat;
1865 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1867 if(!bitmap || !graphics || !cachedbmp)
1868 return InvalidParameter;
1870 *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
1871 if(!*cachedbmp)
1872 return OutOfMemory;
1874 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1875 if(stat != Ok){
1876 GdipFree(*cachedbmp);
1877 return stat;
1880 return Ok;
1883 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1885 GpStatus stat;
1886 BitmapData lockeddata;
1887 ULONG andstride, xorstride, bitssize;
1888 LPBYTE andbits, xorbits, androw, xorrow, srcrow;
1889 UINT x, y;
1891 TRACE("(%p, %p)\n", bitmap, hicon);
1893 if (!bitmap || !hicon)
1894 return InvalidParameter;
1896 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead,
1897 PixelFormat32bppPARGB, &lockeddata);
1898 if (stat == Ok)
1900 andstride = ((lockeddata.Width+31)/32)*4;
1901 xorstride = lockeddata.Width*4;
1902 bitssize = (andstride + xorstride) * lockeddata.Height;
1904 andbits = GdipAlloc(bitssize);
1906 if (andbits)
1908 xorbits = andbits + andstride * lockeddata.Height;
1910 for (y=0; y<lockeddata.Height; y++)
1912 srcrow = ((LPBYTE)lockeddata.Scan0) + lockeddata.Stride * y;
1914 androw = andbits + andstride * y;
1915 for (x=0; x<lockeddata.Width; x++)
1916 if (srcrow[3+4*x] >= 128)
1917 androw[x/8] |= 1 << (7-x%8);
1919 xorrow = xorbits + xorstride * y;
1920 memcpy(xorrow, srcrow, xorstride);
1923 *hicon = CreateIcon(NULL, lockeddata.Width, lockeddata.Height, 1, 32,
1924 andbits, xorbits);
1926 GdipFree(andbits);
1928 else
1929 stat = OutOfMemory;
1931 GdipBitmapUnlockBits(bitmap, &lockeddata);
1934 return stat;
1937 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
1939 TRACE("%p\n", cachedbmp);
1941 if(!cachedbmp)
1942 return InvalidParameter;
1944 GdipDisposeImage(cachedbmp->image);
1945 GdipFree(cachedbmp);
1947 return Ok;
1950 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
1951 GpCachedBitmap *cachedbmp, INT x, INT y)
1953 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
1955 if(!graphics || !cachedbmp)
1956 return InvalidParameter;
1958 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
1961 GpStatus WINGDIPAPI GdipEmfToWmfBits(HENHMETAFILE hemf, UINT cbData16,
1962 LPBYTE pData16, INT iMapMode, INT eFlags)
1964 FIXME("(%p, %d, %p, %d, %d): stub\n", hemf, cbData16, pData16, iMapMode, eFlags);
1965 return NotImplemented;
1968 /* Internal utility function: Replace the image data of dst with that of src,
1969 * and free src. */
1970 static void move_bitmap(GpBitmap *dst, GpBitmap *src, BOOL clobber_palette)
1972 assert(src->image.type == ImageTypeBitmap);
1973 assert(dst->image.type == ImageTypeBitmap);
1975 GdipFree(dst->bitmapbits);
1976 GdipFree(dst->own_bits);
1977 DeleteDC(dst->hdc);
1978 DeleteObject(dst->hbitmap);
1980 if (clobber_palette)
1982 GdipFree(dst->image.palette_entries);
1983 dst->image.palette_flags = src->image.palette_flags;
1984 dst->image.palette_count = src->image.palette_count;
1985 dst->image.palette_entries = src->image.palette_entries;
1987 else
1988 GdipFree(src->image.palette_entries);
1990 dst->image.xres = src->image.xres;
1991 dst->image.yres = src->image.yres;
1992 dst->width = src->width;
1993 dst->height = src->height;
1994 dst->format = src->format;
1995 dst->hbitmap = src->hbitmap;
1996 dst->hdc = src->hdc;
1997 dst->bits = src->bits;
1998 dst->stride = src->stride;
1999 dst->own_bits = src->own_bits;
2000 if (dst->metadata_reader)
2001 IWICMetadataReader_Release(dst->metadata_reader);
2002 dst->metadata_reader = src->metadata_reader;
2003 if (dst->image.stream)
2004 IStream_Release(dst->image.stream);
2005 dst->image.stream = src->image.stream;
2006 dst->image.frame_count = src->image.frame_count;
2007 dst->image.current_frame = src->image.current_frame;
2008 dst->image.format = src->image.format;
2010 src->image.type = ~0;
2011 GdipFree(src);
2014 static GpStatus free_image_data(GpImage *image)
2016 if(!image)
2017 return InvalidParameter;
2019 if (image->type == ImageTypeBitmap)
2021 GdipFree(((GpBitmap*)image)->bitmapbits);
2022 GdipFree(((GpBitmap*)image)->own_bits);
2023 DeleteDC(((GpBitmap*)image)->hdc);
2024 DeleteObject(((GpBitmap*)image)->hbitmap);
2025 if (((GpBitmap*)image)->metadata_reader)
2026 IWICMetadataReader_Release(((GpBitmap*)image)->metadata_reader);
2028 else if (image->type == ImageTypeMetafile)
2030 GpMetafile *metafile = (GpMetafile*)image;
2031 GdipFree(metafile->comment_data);
2032 DeleteEnhMetaFile(CloseEnhMetaFile(metafile->record_dc));
2033 DeleteEnhMetaFile(metafile->hemf);
2034 if (metafile->record_graphics)
2036 WARN("metafile closed while recording\n");
2037 /* not sure what to do here; for now just prevent the graphics from functioning or using this object */
2038 metafile->record_graphics->image = NULL;
2039 metafile->record_graphics->busy = TRUE;
2042 else
2044 WARN("invalid image: %p\n", image);
2045 return ObjectBusy;
2047 if (image->picture)
2048 IPicture_Release(image->picture);
2049 if (image->stream)
2050 IStream_Release(image->stream);
2051 GdipFree(image->palette_entries);
2053 return Ok;
2056 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
2058 GpStatus status;
2060 TRACE("%p\n", image);
2062 status = free_image_data(image);
2063 if (status != Ok) return status;
2064 image->type = ~0;
2065 GdipFree(image);
2067 return Ok;
2070 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
2072 static int calls;
2074 TRACE("(%p,%p)\n", image, item);
2076 if(!image || !item)
2077 return InvalidParameter;
2079 if (!(calls++))
2080 FIXME("not implemented\n");
2082 return NotImplemented;
2085 GpStatus WINGDIPAPI GdipGetImageItemData(GpImage *image, ImageItemData *item)
2087 static int calls;
2089 TRACE("(%p,%p)\n", image, item);
2091 if (!(calls++))
2092 FIXME("not implemented\n");
2094 return NotImplemented;
2097 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
2098 GpUnit *srcUnit)
2100 TRACE("%p %p %p\n", image, srcRect, srcUnit);
2102 if(!image || !srcRect || !srcUnit)
2103 return InvalidParameter;
2104 if(image->type == ImageTypeMetafile){
2105 *srcRect = ((GpMetafile*)image)->bounds;
2106 *srcUnit = ((GpMetafile*)image)->unit;
2108 else if(image->type == ImageTypeBitmap){
2109 srcRect->X = srcRect->Y = 0.0;
2110 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
2111 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
2112 *srcUnit = UnitPixel;
2114 else{
2115 srcRect->X = srcRect->Y = 0.0;
2116 srcRect->Width = ipicture_pixel_width(image->picture);
2117 srcRect->Height = ipicture_pixel_height(image->picture);
2118 *srcUnit = UnitPixel;
2121 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
2122 srcRect->Width, srcRect->Height, *srcUnit);
2124 return Ok;
2127 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
2128 REAL *height)
2130 TRACE("%p %p %p\n", image, width, height);
2132 if(!image || !height || !width)
2133 return InvalidParameter;
2135 if(image->type == ImageTypeMetafile){
2136 HDC hdc = GetDC(0);
2137 REAL res = (REAL)GetDeviceCaps(hdc, LOGPIXELSX);
2139 ReleaseDC(0, hdc);
2141 *height = convert_unit(res, ((GpMetafile*)image)->unit) *
2142 ((GpMetafile*)image)->bounds.Height;
2144 *width = convert_unit(res, ((GpMetafile*)image)->unit) *
2145 ((GpMetafile*)image)->bounds.Width;
2148 else if(image->type == ImageTypeBitmap){
2149 *height = ((GpBitmap*)image)->height;
2150 *width = ((GpBitmap*)image)->width;
2152 else{
2153 *height = ipicture_pixel_height(image->picture);
2154 *width = ipicture_pixel_width(image->picture);
2157 TRACE("returning (%f, %f)\n", *height, *width);
2158 return Ok;
2161 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
2162 GpGraphics **graphics)
2164 HDC hdc;
2165 GpStatus stat;
2167 TRACE("%p %p\n", image, graphics);
2169 if(!image || !graphics)
2170 return InvalidParameter;
2172 if (image->type == ImageTypeBitmap && ((GpBitmap*)image)->hbitmap)
2174 hdc = ((GpBitmap*)image)->hdc;
2176 if(!hdc){
2177 hdc = CreateCompatibleDC(0);
2178 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
2179 ((GpBitmap*)image)->hdc = hdc;
2182 stat = GdipCreateFromHDC(hdc, graphics);
2184 if (stat == Ok)
2185 (*graphics)->image = image;
2187 else if (image->type == ImageTypeMetafile)
2188 stat = METAFILE_GetGraphicsContext((GpMetafile*)image, graphics);
2189 else
2190 stat = graphics_from_image(image, graphics);
2192 return stat;
2195 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
2197 TRACE("%p %p\n", image, height);
2199 if(!image || !height)
2200 return InvalidParameter;
2202 if(image->type == ImageTypeMetafile){
2203 HDC hdc = GetDC(0);
2204 REAL res = (REAL)GetDeviceCaps(hdc, LOGPIXELSX);
2206 ReleaseDC(0, hdc);
2208 *height = roundr(convert_unit(res, ((GpMetafile*)image)->unit) *
2209 ((GpMetafile*)image)->bounds.Height);
2211 else if(image->type == ImageTypeBitmap)
2212 *height = ((GpBitmap*)image)->height;
2213 else
2214 *height = ipicture_pixel_height(image->picture);
2216 TRACE("returning %d\n", *height);
2218 return Ok;
2221 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2223 if(!image || !res)
2224 return InvalidParameter;
2226 *res = image->xres;
2228 TRACE("(%p) <-- %0.2f\n", image, *res);
2230 return Ok;
2233 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2235 TRACE("%p %p\n", image, size);
2237 if(!image || !size)
2238 return InvalidParameter;
2240 if (image->palette_count == 0)
2241 *size = sizeof(ColorPalette);
2242 else
2243 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette_count;
2245 TRACE("<-- %u\n", *size);
2247 return Ok;
2250 /* FIXME: test this function for non-bitmap types */
2251 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2253 TRACE("%p %p\n", image, format);
2255 if(!image || !format)
2256 return InvalidParameter;
2258 if(image->type != ImageTypeBitmap)
2259 *format = PixelFormat24bppRGB;
2260 else
2261 *format = ((GpBitmap*) image)->format;
2263 return Ok;
2266 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2268 TRACE("(%p, %p)\n", image, format);
2270 if(!image || !format)
2271 return InvalidParameter;
2273 memcpy(format, &image->format, sizeof(GUID));
2275 return Ok;
2278 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2280 TRACE("%p %p\n", image, type);
2282 if(!image || !type)
2283 return InvalidParameter;
2285 *type = image->type;
2287 return Ok;
2290 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2292 if(!image || !res)
2293 return InvalidParameter;
2295 *res = image->yres;
2297 TRACE("(%p) <-- %0.2f\n", image, *res);
2299 return Ok;
2302 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2304 TRACE("%p %p\n", image, width);
2306 if(!image || !width)
2307 return InvalidParameter;
2309 if(image->type == ImageTypeMetafile){
2310 HDC hdc = GetDC(0);
2311 REAL res = (REAL)GetDeviceCaps(hdc, LOGPIXELSX);
2313 ReleaseDC(0, hdc);
2315 *width = roundr(convert_unit(res, ((GpMetafile*)image)->unit) *
2316 ((GpMetafile*)image)->bounds.Width);
2318 else if(image->type == ImageTypeBitmap)
2319 *width = ((GpBitmap*)image)->width;
2320 else
2321 *width = ipicture_pixel_width(image->picture);
2323 TRACE("returning %d\n", *width);
2325 return Ok;
2328 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
2329 MetafileHeader * header)
2331 static int calls;
2333 TRACE("(%p, %p)\n", metafile, header);
2335 if(!metafile || !header)
2336 return InvalidParameter;
2338 if(!(calls++))
2339 FIXME("not implemented\n");
2341 memset(header, 0, sizeof(MetafileHeader));
2343 return Ok;
2346 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromEmf(HENHMETAFILE hEmf,
2347 MetafileHeader *header)
2349 static int calls;
2351 if(!hEmf || !header)
2352 return InvalidParameter;
2354 if(!(calls++))
2355 FIXME("not implemented\n");
2357 memset(header, 0, sizeof(MetafileHeader));
2359 return Ok;
2362 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromFile(GDIPCONST WCHAR *filename,
2363 MetafileHeader *header)
2365 static int calls;
2367 TRACE("(%s,%p)\n", debugstr_w(filename), header);
2369 if(!filename || !header)
2370 return InvalidParameter;
2372 if(!(calls++))
2373 FIXME("not implemented\n");
2375 memset(header, 0, sizeof(MetafileHeader));
2377 return Ok;
2380 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromStream(IStream *stream,
2381 MetafileHeader *header)
2383 static int calls;
2385 TRACE("(%p,%p)\n", stream, header);
2387 if(!stream || !header)
2388 return InvalidParameter;
2390 if(!(calls++))
2391 FIXME("not implemented\n");
2393 memset(header, 0, sizeof(MetafileHeader));
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)->metadata_reader)
2409 IWICMetadataReader_GetCount(((GpBitmap *)image)->metadata_reader, num);
2412 return Ok;
2415 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID *list)
2417 HRESULT hr;
2418 IWICMetadataReader *reader;
2419 IWICEnumMetadataItem *enumerator;
2420 UINT prop_count, i, items_returned;
2422 TRACE("(%p, %u, %p)\n", image, num, list);
2424 if (!image || !list) return InvalidParameter;
2426 if (image->type != ImageTypeBitmap)
2428 FIXME("Not implemented for type %d\n", image->type);
2429 return NotImplemented;
2432 reader = ((GpBitmap *)image)->metadata_reader;
2433 if (!reader)
2435 if (num != 0) return InvalidParameter;
2436 return Ok;
2439 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2440 if (FAILED(hr)) return hresult_to_status(hr);
2442 if (num != prop_count) return InvalidParameter;
2444 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2445 if (FAILED(hr)) return hresult_to_status(hr);
2447 IWICEnumMetadataItem_Reset(enumerator);
2449 for (i = 0; i < num; i++)
2451 PROPVARIANT id;
2453 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, NULL, &items_returned);
2454 if (hr != S_OK) break;
2456 if (id.vt != VT_UI2)
2458 FIXME("not supported propvariant type for id: %u\n", id.vt);
2459 list[i] = 0;
2460 continue;
2462 list[i] = id.u.uiVal;
2465 IWICEnumMetadataItem_Release(enumerator);
2467 return hr == S_OK ? Ok : hresult_to_status(hr);
2470 static UINT propvariant_size(PROPVARIANT *value)
2472 switch (value->vt & ~VT_VECTOR)
2474 case VT_EMPTY:
2475 return 0;
2476 case VT_I1:
2477 case VT_UI1:
2478 if (!(value->vt & VT_VECTOR)) return 1;
2479 return value->u.caub.cElems;
2480 case VT_I2:
2481 case VT_UI2:
2482 if (!(value->vt & VT_VECTOR)) return 2;
2483 return value->u.caui.cElems * 2;
2484 case VT_I4:
2485 case VT_UI4:
2486 case VT_R4:
2487 if (!(value->vt & VT_VECTOR)) return 4;
2488 return value->u.caul.cElems * 4;
2489 case VT_I8:
2490 case VT_UI8:
2491 case VT_R8:
2492 if (!(value->vt & VT_VECTOR)) return 8;
2493 return value->u.cauh.cElems * 8;
2494 case VT_LPSTR:
2495 return value->u.pszVal ? strlen(value->u.pszVal) + 1 : 0;
2496 case VT_BLOB:
2497 return value->u.blob.cbSize;
2498 default:
2499 FIXME("not supported variant type %d\n", value->vt);
2500 return 0;
2504 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID propid, UINT *size)
2506 HRESULT hr;
2507 IWICMetadataReader *reader;
2508 PROPVARIANT id, value;
2510 TRACE("(%p,%#x,%p)\n", image, propid, size);
2512 if (!size || !image) return InvalidParameter;
2514 if (image->type != ImageTypeBitmap)
2516 FIXME("Not implemented for type %d\n", image->type);
2517 return NotImplemented;
2520 reader = ((GpBitmap *)image)->metadata_reader;
2521 if (!reader) return PropertyNotFound;
2523 id.vt = VT_UI2;
2524 id.u.uiVal = propid;
2525 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2526 if (FAILED(hr)) return PropertyNotFound;
2528 *size = propvariant_size(&value);
2529 if (*size) *size += sizeof(PropertyItem);
2530 PropVariantClear(&value);
2532 return Ok;
2535 #ifndef PropertyTagTypeSByte
2536 #define PropertyTagTypeSByte 6
2537 #define PropertyTagTypeSShort 8
2538 #define PropertyTagTypeFloat 11
2539 #define PropertyTagTypeDouble 12
2540 #endif
2542 static UINT vt_to_itemtype(UINT vt)
2544 static const struct
2546 UINT vt, type;
2547 } vt2type[] =
2549 { VT_I1, PropertyTagTypeSByte },
2550 { VT_UI1, PropertyTagTypeByte },
2551 { VT_I2, PropertyTagTypeSShort },
2552 { VT_UI2, PropertyTagTypeShort },
2553 { VT_I4, PropertyTagTypeSLONG },
2554 { VT_UI4, PropertyTagTypeLong },
2555 { VT_I8, PropertyTagTypeSRational },
2556 { VT_UI8, PropertyTagTypeRational },
2557 { VT_R4, PropertyTagTypeFloat },
2558 { VT_R8, PropertyTagTypeDouble },
2559 { VT_LPSTR, PropertyTagTypeASCII },
2560 { VT_BLOB, PropertyTagTypeUndefined }
2562 UINT i;
2563 for (i = 0; i < sizeof(vt2type)/sizeof(vt2type[0]); i++)
2565 if (vt2type[i].vt == vt) return vt2type[i].type;
2567 FIXME("not supported variant type %u\n", vt);
2568 return 0;
2571 static GpStatus propvariant_to_item(PROPVARIANT *value, PropertyItem *item,
2572 UINT size, PROPID id)
2574 UINT item_size, item_type;
2576 item_size = propvariant_size(value);
2577 if (size != item_size + sizeof(PropertyItem)) return InvalidParameter;
2579 item_type = vt_to_itemtype(value->vt & ~VT_VECTOR);
2580 if (!item_type) return InvalidParameter;
2582 item->value = item + 1;
2584 switch (value->vt & ~VT_VECTOR)
2586 case VT_I1:
2587 case VT_UI1:
2588 if (!(value->vt & VT_VECTOR))
2589 *(BYTE *)item->value = value->u.bVal;
2590 else
2591 memcpy(item->value, value->u.caub.pElems, item_size);
2592 break;
2593 case VT_I2:
2594 case VT_UI2:
2595 if (!(value->vt & VT_VECTOR))
2596 *(USHORT *)item->value = value->u.uiVal;
2597 else
2598 memcpy(item->value, value->u.caui.pElems, item_size);
2599 break;
2600 case VT_I4:
2601 case VT_UI4:
2602 case VT_R4:
2603 if (!(value->vt & VT_VECTOR))
2604 *(ULONG *)item->value = value->u.ulVal;
2605 else
2606 memcpy(item->value, value->u.caul.pElems, item_size);
2607 break;
2608 case VT_I8:
2609 case VT_UI8:
2610 case VT_R8:
2611 if (!(value->vt & VT_VECTOR))
2612 *(ULONGLONG *)item->value = value->u.uhVal.QuadPart;
2613 else
2614 memcpy(item->value, value->u.cauh.pElems, item_size);
2615 break;
2616 case VT_LPSTR:
2617 memcpy(item->value, value->u.pszVal, item_size);
2618 break;
2619 case VT_BLOB:
2620 memcpy(item->value, value->u.blob.pBlobData, item_size);
2621 break;
2622 default:
2623 FIXME("not supported variant type %d\n", value->vt);
2624 return InvalidParameter;
2627 item->length = item_size;
2628 item->type = item_type;
2629 item->id = id;
2631 return Ok;
2634 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID propid, UINT size,
2635 PropertyItem *buffer)
2637 GpStatus stat;
2638 HRESULT hr;
2639 IWICMetadataReader *reader;
2640 PROPVARIANT id, value;
2642 TRACE("(%p,%#x,%u,%p)\n", image, propid, size, buffer);
2644 if (!image || !buffer) return InvalidParameter;
2646 if (image->type != ImageTypeBitmap)
2648 FIXME("Not implemented for type %d\n", image->type);
2649 return NotImplemented;
2652 reader = ((GpBitmap *)image)->metadata_reader;
2653 if (!reader) return PropertyNotFound;
2655 id.vt = VT_UI2;
2656 id.u.uiVal = propid;
2657 hr = IWICMetadataReader_GetValue(reader, NULL, &id, &value);
2658 if (FAILED(hr)) return PropertyNotFound;
2660 stat = propvariant_to_item(&value, buffer, size, propid);
2661 PropVariantClear(&value);
2663 return stat;
2666 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT *size, UINT *count)
2668 HRESULT hr;
2669 IWICMetadataReader *reader;
2670 IWICEnumMetadataItem *enumerator;
2671 UINT prop_count, prop_size, i;
2672 PROPVARIANT id, value;
2674 TRACE("(%p,%p,%p)\n", image, size, count);
2676 if (!image || !size || !count) return InvalidParameter;
2678 if (image->type != ImageTypeBitmap)
2680 FIXME("Not implemented for type %d\n", image->type);
2681 return NotImplemented;
2684 reader = ((GpBitmap *)image)->metadata_reader;
2685 if (!reader) return PropertyNotFound;
2687 hr = IWICMetadataReader_GetCount(reader, &prop_count);
2688 if (FAILED(hr)) return hresult_to_status(hr);
2690 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2691 if (FAILED(hr)) return hresult_to_status(hr);
2693 IWICEnumMetadataItem_Reset(enumerator);
2695 prop_size = 0;
2697 PropVariantInit(&id);
2698 PropVariantInit(&value);
2700 for (i = 0; i < prop_count; i++)
2702 UINT items_returned, item_size;
2704 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2705 if (hr != S_OK) break;
2707 item_size = propvariant_size(&value);
2708 if (item_size) prop_size += sizeof(PropertyItem) + item_size;
2710 PropVariantClear(&id);
2711 PropVariantClear(&value);
2714 IWICEnumMetadataItem_Release(enumerator);
2716 if (hr != S_OK) return PropertyNotFound;
2718 *count = prop_count;
2719 *size = prop_size;
2720 return Ok;
2723 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2724 UINT count, PropertyItem *buf)
2726 GpStatus status;
2727 HRESULT hr;
2728 IWICMetadataReader *reader;
2729 IWICEnumMetadataItem *enumerator;
2730 UINT prop_count, prop_size, i;
2731 PROPVARIANT id, value;
2732 char *item_value;
2734 TRACE("(%p,%u,%u,%p)\n", image, size, count, buf);
2736 if (!image || !buf) return InvalidParameter;
2738 if (image->type != ImageTypeBitmap)
2740 FIXME("Not implemented for type %d\n", image->type);
2741 return NotImplemented;
2744 status = GdipGetPropertySize(image, &prop_size, &prop_count);
2745 if (status != Ok) return status;
2747 if (prop_count != count || prop_size != size) return InvalidParameter;
2749 reader = ((GpBitmap *)image)->metadata_reader;
2750 if (!reader) return PropertyNotFound;
2752 hr = IWICMetadataReader_GetEnumerator(reader, &enumerator);
2753 if (FAILED(hr)) return hresult_to_status(hr);
2755 IWICEnumMetadataItem_Reset(enumerator);
2757 item_value = (char *)(buf + prop_count);
2759 PropVariantInit(&id);
2760 PropVariantInit(&value);
2762 for (i = 0; i < prop_count; i++)
2764 PropertyItem *item;
2765 UINT items_returned, item_size;
2767 hr = IWICEnumMetadataItem_Next(enumerator, 1, NULL, &id, &value, &items_returned);
2768 if (hr != S_OK) break;
2770 if (id.vt != VT_UI2)
2772 FIXME("not supported propvariant type for id: %u\n", id.vt);
2773 continue;
2776 item_size = propvariant_size(&value);
2777 if (item_size)
2779 item = HeapAlloc(GetProcessHeap(), 0, item_size + sizeof(*item));
2781 propvariant_to_item(&value, item, item_size + sizeof(*item), id.u.uiVal);
2782 buf[i].id = item->id;
2783 buf[i].type = item->type;
2784 buf[i].length = item_size;
2785 buf[i].value = item_value;
2786 memcpy(item_value, item->value, item_size);
2787 item_value += item_size;
2789 HeapFree(GetProcessHeap(), 0, item);
2792 PropVariantClear(&id);
2793 PropVariantClear(&value);
2796 IWICEnumMetadataItem_Release(enumerator);
2798 if (hr != S_OK) return PropertyNotFound;
2800 return Ok;
2803 struct image_format_dimension
2805 const GUID *format;
2806 const GUID *dimension;
2809 static const struct image_format_dimension image_format_dimensions[] =
2811 {&ImageFormatGIF, &FrameDimensionTime},
2812 {&ImageFormatIcon, &FrameDimensionResolution},
2813 {NULL}
2816 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
2817 GDIPCONST GUID* dimensionID, UINT* count)
2819 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
2821 if(!image || !count)
2822 return InvalidParameter;
2824 if (!dimensionID ||
2825 IsEqualGUID(dimensionID, &image->format) ||
2826 IsEqualGUID(dimensionID, &FrameDimensionPage) ||
2827 IsEqualGUID(dimensionID, &FrameDimensionTime))
2829 *count = image->frame_count;
2830 return Ok;
2833 return InvalidParameter;
2836 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
2837 UINT* count)
2839 TRACE("(%p, %p)\n", image, count);
2841 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2843 if(!image || !count)
2844 return InvalidParameter;
2846 *count = 1;
2848 return Ok;
2851 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
2852 GUID* dimensionIDs, UINT count)
2854 int i;
2855 const GUID *result=NULL;
2857 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
2859 if(!image || !dimensionIDs || count != 1)
2860 return InvalidParameter;
2862 for (i=0; image_format_dimensions[i].format; i++)
2864 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
2866 result = image_format_dimensions[i].dimension;
2867 break;
2871 if (!result)
2872 result = &FrameDimensionPage;
2874 memcpy(dimensionIDs, result, sizeof(GUID));
2876 return Ok;
2879 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
2880 GpImage **image)
2882 GpStatus stat;
2883 IStream *stream;
2885 TRACE("(%s) %p\n", debugstr_w(filename), image);
2887 if (!filename || !image)
2888 return InvalidParameter;
2890 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
2892 if (stat != Ok)
2893 return stat;
2895 stat = GdipLoadImageFromStream(stream, image);
2897 IStream_Release(stream);
2899 return stat;
2902 /* FIXME: no icm handling */
2903 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
2905 TRACE("(%s) %p\n", debugstr_w(filename), image);
2907 return GdipLoadImageFromFile(filename, image);
2910 static const WICPixelFormatGUID * const wic_pixel_formats[] = {
2911 &GUID_WICPixelFormat16bppBGR555,
2912 &GUID_WICPixelFormat24bppBGR,
2913 &GUID_WICPixelFormat32bppBGR,
2914 &GUID_WICPixelFormat32bppBGRA,
2915 &GUID_WICPixelFormat32bppPBGRA,
2916 NULL
2919 static const PixelFormat wic_gdip_formats[] = {
2920 PixelFormat16bppRGB555,
2921 PixelFormat24bppRGB,
2922 PixelFormat32bppRGB,
2923 PixelFormat32bppARGB,
2924 PixelFormat32bppPARGB,
2927 static GpStatus decode_image_wic(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
2929 GpStatus status=Ok;
2930 GpBitmap *bitmap;
2931 HRESULT hr;
2932 IWICBitmapDecoder *decoder;
2933 IWICBitmapFrameDecode *frame;
2934 IWICBitmapSource *source=NULL;
2935 IWICMetadataBlockReader *block_reader;
2936 WICPixelFormatGUID wic_format;
2937 PixelFormat gdip_format=0;
2938 int i;
2939 UINT width, height, frame_count;
2940 BitmapData lockeddata;
2941 WICRect wrc;
2942 HRESULT initresult;
2944 initresult = CoInitialize(NULL);
2946 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
2947 &IID_IWICBitmapDecoder, (void**)&decoder);
2948 if (FAILED(hr)) goto end;
2950 hr = IWICBitmapDecoder_Initialize(decoder, stream, WICDecodeMetadataCacheOnLoad);
2951 if (SUCCEEDED(hr))
2953 IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
2954 hr = IWICBitmapDecoder_GetFrame(decoder, active_frame, &frame);
2957 if (SUCCEEDED(hr)) /* got frame */
2959 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
2961 if (SUCCEEDED(hr))
2963 IWICBitmapSource *bmp_source;
2964 IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICBitmapSource, (void **)&bmp_source);
2966 for (i=0; wic_pixel_formats[i]; i++)
2968 if (IsEqualGUID(&wic_format, wic_pixel_formats[i]))
2970 source = bmp_source;
2971 gdip_format = wic_gdip_formats[i];
2972 break;
2975 if (!source)
2977 /* unknown format; fall back on 32bppARGB */
2978 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, bmp_source, &source);
2979 gdip_format = PixelFormat32bppARGB;
2980 IWICBitmapSource_Release(bmp_source);
2984 if (SUCCEEDED(hr)) /* got source */
2986 hr = IWICBitmapSource_GetSize(source, &width, &height);
2988 if (SUCCEEDED(hr))
2989 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
2990 NULL, &bitmap);
2992 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
2994 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
2995 gdip_format, &lockeddata);
2996 if (status == Ok) /* locked bitmap */
2998 wrc.X = 0;
2999 wrc.Width = width;
3000 wrc.Height = 1;
3001 for (i=0; i<height; i++)
3003 wrc.Y = i;
3004 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
3005 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
3006 if (FAILED(hr)) break;
3009 GdipBitmapUnlockBits(bitmap, &lockeddata);
3012 if (SUCCEEDED(hr) && status == Ok)
3013 *image = (GpImage*)bitmap;
3014 else
3016 *image = NULL;
3017 GdipDisposeImage((GpImage*)bitmap);
3020 if (SUCCEEDED(hr) && status == Ok)
3022 double dpix, dpiy;
3023 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
3024 if (SUCCEEDED(hr))
3026 bitmap->image.xres = dpix;
3027 bitmap->image.yres = dpiy;
3029 hr = S_OK;
3033 IWICBitmapSource_Release(source);
3036 bitmap->metadata_reader = NULL;
3037 if (IWICBitmapFrameDecode_QueryInterface(frame, &IID_IWICMetadataBlockReader, (void **)&block_reader) == S_OK)
3039 UINT block_count = 0;
3040 if (IWICMetadataBlockReader_GetCount(block_reader, &block_count) == S_OK && block_count)
3041 IWICMetadataBlockReader_GetReaderByIndex(block_reader, 0, &bitmap->metadata_reader);
3042 IWICMetadataBlockReader_Release(block_reader);
3045 IWICBitmapFrameDecode_Release(frame);
3048 IWICBitmapDecoder_Release(decoder);
3050 end:
3051 if (SUCCEEDED(initresult)) CoUninitialize();
3053 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
3055 if (status == Ok)
3057 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
3058 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI|ImageFlagsColorSpaceRGB;
3059 bitmap->image.frame_count = frame_count;
3060 bitmap->image.current_frame = active_frame;
3061 bitmap->image.stream = stream;
3062 /* Pin the source stream */
3063 IStream_AddRef(stream);
3066 return status;
3069 static GpStatus decode_image_icon(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3071 return decode_image_wic(stream, &CLSID_WICIcoDecoder, active_frame, image);
3074 static GpStatus decode_image_bmp(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3076 GpStatus status;
3077 GpBitmap* bitmap;
3079 status = decode_image_wic(stream, &CLSID_WICBmpDecoder, active_frame, image);
3081 bitmap = (GpBitmap*)*image;
3083 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
3085 /* WIC supports bmp files with alpha, but gdiplus does not */
3086 bitmap->format = PixelFormat32bppRGB;
3089 return status;
3092 static GpStatus decode_image_jpeg(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3094 return decode_image_wic(stream, &CLSID_WICJpegDecoder, active_frame, image);
3097 static GpStatus decode_image_png(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3099 return decode_image_wic(stream, &CLSID_WICPngDecoder, active_frame, image);
3102 static GpStatus decode_image_gif(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3104 return decode_image_wic(stream, &CLSID_WICGifDecoder, active_frame, image);
3107 static GpStatus decode_image_tiff(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3109 return decode_image_wic(stream, &CLSID_WICTiffDecoder, active_frame, image);
3112 static GpStatus decode_image_olepicture_metafile(IStream* stream, REFCLSID clsid, UINT active_frame, GpImage **image)
3114 IPicture *pic;
3116 TRACE("%p %p\n", stream, image);
3118 if(!stream || !image)
3119 return InvalidParameter;
3121 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
3122 (LPVOID*) &pic) != S_OK){
3123 TRACE("Could not load picture\n");
3124 return GenericError;
3127 /* FIXME: missing initialization code */
3128 *image = GdipAlloc(sizeof(GpMetafile));
3129 if(!*image) return OutOfMemory;
3130 (*image)->type = ImageTypeMetafile;
3131 (*image)->stream = NULL;
3132 (*image)->picture = pic;
3133 (*image)->flags = ImageFlagsNone;
3134 (*image)->frame_count = 1;
3135 (*image)->current_frame = 0;
3136 (*image)->palette_flags = 0;
3137 (*image)->palette_count = 0;
3138 (*image)->palette_size = 0;
3139 (*image)->palette_entries = NULL;
3141 TRACE("<-- %p\n", *image);
3143 return Ok;
3146 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
3147 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
3149 typedef GpStatus (*decode_image_func)(IStream *stream, REFCLSID clsid, UINT active_frame, GpImage **image);
3151 typedef struct image_codec {
3152 ImageCodecInfo info;
3153 encode_image_func encode_func;
3154 decode_image_func decode_func;
3155 } image_codec;
3157 typedef enum {
3158 BMP,
3159 JPEG,
3160 GIF,
3161 TIFF,
3162 EMF,
3163 WMF,
3164 PNG,
3165 ICO,
3166 NUM_CODECS
3167 } ImageFormat;
3169 static const struct image_codec codecs[NUM_CODECS];
3171 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
3173 BYTE signature[8];
3174 const BYTE *pattern, *mask;
3175 LARGE_INTEGER seek;
3176 HRESULT hr;
3177 UINT bytesread;
3178 int i, j, sig;
3180 /* seek to the start of the stream */
3181 seek.QuadPart = 0;
3182 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3183 if (FAILED(hr)) return hresult_to_status(hr);
3185 /* read the first 8 bytes */
3186 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
3187 hr = IStream_Read(stream, signature, 8, &bytesread);
3188 if (FAILED(hr)) return hresult_to_status(hr);
3189 if (hr == S_FALSE || bytesread == 0) return GenericError;
3191 for (i = 0; i < NUM_CODECS; i++) {
3192 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
3193 bytesread >= codecs[i].info.SigSize)
3195 for (sig=0; sig<codecs[i].info.SigCount; sig++)
3197 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
3198 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
3199 for (j=0; j<codecs[i].info.SigSize; j++)
3200 if ((signature[j] & mask[j]) != pattern[j])
3201 break;
3202 if (j == codecs[i].info.SigSize)
3204 *result = &codecs[i];
3205 return Ok;
3211 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
3212 signature[0],signature[1],signature[2],signature[3],
3213 signature[4],signature[5],signature[6],signature[7]);
3215 return GenericError;
3218 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image, GDIPCONST GUID *dimensionID,
3219 UINT frame)
3221 GpStatus stat;
3222 LARGE_INTEGER seek;
3223 HRESULT hr;
3224 const struct image_codec *codec = NULL;
3225 IStream *stream;
3226 GpImage *new_image;
3228 TRACE("(%p,%s,%u)\n", image, debugstr_guid(dimensionID), frame);
3230 if (!image || !dimensionID)
3231 return InvalidParameter;
3233 if (frame >= image->frame_count)
3234 return InvalidParameter;
3236 if (image->type != ImageTypeBitmap && image->type != ImageTypeMetafile)
3238 WARN("invalid image type %d\n", image->type);
3239 return InvalidParameter;
3242 if (image->current_frame == frame)
3243 return Ok;
3245 if (!image->stream)
3247 TRACE("image doesn't have an associated stream\n");
3248 return Ok;
3251 hr = IStream_Clone(image->stream, &stream);
3252 if (FAILED(hr))
3254 STATSTG statstg;
3256 hr = IStream_Stat(image->stream, &statstg, STATFLAG_NOOPEN);
3257 if (FAILED(hr)) return hresult_to_status(hr);
3259 stat = GdipCreateStreamOnFile(statstg.pwcsName, GENERIC_READ, &stream);
3260 if (stat != Ok) return stat;
3263 /* choose an appropriate image decoder */
3264 stat = get_decoder_info(stream, &codec);
3265 if (stat != Ok)
3267 IStream_Release(stream);
3268 return stat;
3271 /* seek to the start of the stream */
3272 seek.QuadPart = 0;
3273 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3274 if (FAILED(hr))
3276 IStream_Release(stream);
3277 return hresult_to_status(hr);
3280 /* call on the image decoder to do the real work */
3281 stat = codec->decode_func(stream, &codec->info.Clsid, frame, &new_image);
3283 if (stat == Ok)
3285 memcpy(&new_image->format, &codec->info.FormatID, sizeof(GUID));
3286 free_image_data(image);
3287 if (image->type == ImageTypeBitmap)
3288 *(GpBitmap *)image = *(GpBitmap *)new_image;
3289 else if (image->type == ImageTypeMetafile)
3290 *(GpMetafile *)image = *(GpMetafile *)new_image;
3291 new_image->type = ~0;
3292 GdipFree(new_image);
3293 return Ok;
3296 IStream_Release(stream);
3297 return stat;
3300 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream *stream, GpImage **image)
3302 GpStatus stat;
3303 LARGE_INTEGER seek;
3304 HRESULT hr;
3305 const struct image_codec *codec=NULL;
3307 /* choose an appropriate image decoder */
3308 stat = get_decoder_info(stream, &codec);
3309 if (stat != Ok) return stat;
3311 /* seek to the start of the stream */
3312 seek.QuadPart = 0;
3313 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
3314 if (FAILED(hr)) return hresult_to_status(hr);
3316 /* call on the image decoder to do the real work */
3317 stat = codec->decode_func(stream, &codec->info.Clsid, 0, image);
3319 /* take note of the original data format */
3320 if (stat == Ok)
3322 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
3323 return Ok;
3326 return stat;
3329 /* FIXME: no ICM */
3330 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
3332 TRACE("%p %p\n", stream, image);
3334 return GdipLoadImageFromStream(stream, image);
3337 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
3339 static int calls;
3341 TRACE("(%p,%u)\n", image, propId);
3343 if(!image)
3344 return InvalidParameter;
3346 if(!(calls++))
3347 FIXME("not implemented\n");
3349 return NotImplemented;
3352 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
3354 static int calls;
3356 if (!image || !item) return InvalidParameter;
3358 TRACE("(%p,%p:%#x,%u,%u,%p)\n", image, item, item->id, item->type, item->length, item->value);
3360 if(!(calls++))
3361 FIXME("not implemented\n");
3363 return NotImplemented;
3366 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
3367 GDIPCONST CLSID *clsidEncoder,
3368 GDIPCONST EncoderParameters *encoderParams)
3370 GpStatus stat;
3371 IStream *stream;
3373 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
3375 if (!image || !filename|| !clsidEncoder)
3376 return InvalidParameter;
3378 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
3379 if (stat != Ok)
3380 return GenericError;
3382 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
3384 IStream_Release(stream);
3385 return stat;
3388 /*************************************************************************
3389 * Encoding functions -
3390 * These functions encode an image in different image file formats.
3392 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
3393 #define BITMAP_FORMAT_JPEG 0xd8ff
3394 #define BITMAP_FORMAT_GIF 0x4947
3395 #define BITMAP_FORMAT_PNG 0x5089
3396 #define BITMAP_FORMAT_APM 0xcdd7
3398 static GpStatus encode_image_WIC(GpImage *image, IStream* stream,
3399 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3401 GpStatus stat;
3402 GpBitmap *bitmap;
3403 IWICBitmapEncoder *encoder;
3404 IWICBitmapFrameEncode *frameencode;
3405 IPropertyBag2 *encoderoptions;
3406 HRESULT hr;
3407 UINT width, height;
3408 PixelFormat gdipformat=0;
3409 WICPixelFormatGUID wicformat;
3410 GpRect rc;
3411 BitmapData lockeddata;
3412 HRESULT initresult;
3413 UINT i;
3415 if (image->type != ImageTypeBitmap)
3416 return GenericError;
3418 bitmap = (GpBitmap*)image;
3420 GdipGetImageWidth(image, &width);
3421 GdipGetImageHeight(image, &height);
3423 rc.X = 0;
3424 rc.Y = 0;
3425 rc.Width = width;
3426 rc.Height = height;
3428 initresult = CoInitialize(NULL);
3430 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
3431 &IID_IWICBitmapEncoder, (void**)&encoder);
3432 if (FAILED(hr))
3434 if (SUCCEEDED(initresult)) CoUninitialize();
3435 return hresult_to_status(hr);
3438 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
3440 if (SUCCEEDED(hr))
3442 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
3445 if (SUCCEEDED(hr)) /* created frame */
3447 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
3449 if (SUCCEEDED(hr))
3450 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
3452 if (SUCCEEDED(hr))
3453 /* FIXME: use the resolution from the image */
3454 hr = IWICBitmapFrameEncode_SetResolution(frameencode, 96.0, 96.0);
3456 if (SUCCEEDED(hr))
3458 for (i=0; wic_pixel_formats[i]; i++)
3460 if (wic_gdip_formats[i] == bitmap->format)
3461 break;
3463 if (wic_pixel_formats[i])
3464 memcpy(&wicformat, wic_pixel_formats[i], sizeof(GUID));
3465 else
3466 memcpy(&wicformat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
3468 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
3470 for (i=0; wic_pixel_formats[i]; i++)
3472 if (IsEqualGUID(&wicformat, wic_pixel_formats[i]))
3473 break;
3475 if (wic_pixel_formats[i])
3476 gdipformat = wic_gdip_formats[i];
3477 else
3479 ERR("cannot provide pixel format %s\n", debugstr_guid(&wicformat));
3480 hr = E_FAIL;
3484 if (SUCCEEDED(hr))
3486 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
3487 &lockeddata);
3489 if (stat == Ok)
3491 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
3492 BYTE *row;
3494 /* write one row at a time in case stride is negative */
3495 row = lockeddata.Scan0;
3496 for (i=0; i<lockeddata.Height; i++)
3498 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
3499 if (FAILED(hr)) break;
3500 row += lockeddata.Stride;
3503 GdipBitmapUnlockBits(bitmap, &lockeddata);
3505 else
3506 hr = E_FAIL;
3509 if (SUCCEEDED(hr))
3510 hr = IWICBitmapFrameEncode_Commit(frameencode);
3512 IWICBitmapFrameEncode_Release(frameencode);
3513 IPropertyBag2_Release(encoderoptions);
3516 if (SUCCEEDED(hr))
3517 hr = IWICBitmapEncoder_Commit(encoder);
3519 IWICBitmapEncoder_Release(encoder);
3521 if (SUCCEEDED(initresult)) CoUninitialize();
3523 return hresult_to_status(hr);
3526 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
3527 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3529 return encode_image_WIC(image, stream, &CLSID_WICBmpEncoder, params);
3532 static GpStatus encode_image_tiff(GpImage *image, IStream* stream,
3533 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3535 return encode_image_WIC(image, stream, &CLSID_WICTiffEncoder, params);
3538 static GpStatus encode_image_png(GpImage *image, IStream* stream,
3539 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3541 return encode_image_WIC(image, stream, &CLSID_WICPngEncoder, params);
3544 static GpStatus encode_image_jpeg(GpImage *image, IStream* stream,
3545 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3547 return encode_image_WIC(image, stream, &CLSID_WICJpegEncoder, params);
3550 /*****************************************************************************
3551 * GdipSaveImageToStream [GDIPLUS.@]
3553 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
3554 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3556 GpStatus stat;
3557 encode_image_func encode_image;
3558 int i;
3560 TRACE("%p %p %p %p\n", image, stream, clsid, params);
3562 if(!image || !stream)
3563 return InvalidParameter;
3565 /* select correct encoder */
3566 encode_image = NULL;
3567 for (i = 0; i < NUM_CODECS; i++) {
3568 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
3569 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
3570 encode_image = codecs[i].encode_func;
3572 if (encode_image == NULL)
3573 return UnknownImageFormat;
3575 stat = encode_image(image, stream, clsid, params);
3577 return stat;
3580 /*****************************************************************************
3581 * GdipSaveAdd [GDIPLUS.@]
3583 GpStatus WINGDIPAPI GdipSaveAdd(GpImage *image, GDIPCONST EncoderParameters *params)
3585 FIXME("(%p,%p): stub\n", image, params);
3586 return Ok;
3589 /*****************************************************************************
3590 * GdipGetImagePalette [GDIPLUS.@]
3592 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
3594 TRACE("(%p,%p,%i)\n", image, palette, size);
3596 if (!image || !palette)
3597 return InvalidParameter;
3599 if (size < (sizeof(UINT)*2+sizeof(ARGB)*image->palette_count))
3601 TRACE("<-- InsufficientBuffer\n");
3602 return InsufficientBuffer;
3605 palette->Flags = image->palette_flags;
3606 palette->Count = image->palette_count;
3607 memcpy(palette->Entries, image->palette_entries, sizeof(ARGB)*image->palette_count);
3609 return Ok;
3612 /*****************************************************************************
3613 * GdipSetImagePalette [GDIPLUS.@]
3615 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
3616 GDIPCONST ColorPalette *palette)
3618 TRACE("(%p,%p)\n", image, palette);
3620 if(!image || !palette || palette->Count > 256)
3621 return InvalidParameter;
3623 if (palette->Count > image->palette_size)
3625 ARGB *new_palette;
3627 new_palette = GdipAlloc(sizeof(ARGB) * palette->Count);
3628 if (!new_palette) return OutOfMemory;
3630 GdipFree(image->palette_entries);
3631 image->palette_entries = new_palette;
3632 image->palette_size = palette->Count;
3635 image->palette_flags = palette->Flags;
3636 image->palette_count = palette->Count;
3637 memcpy(image->palette_entries, palette->Entries, sizeof(ARGB)*palette->Count);
3639 return Ok;
3642 /*************************************************************************
3643 * Encoders -
3644 * Structures that represent which formats we support for encoding.
3647 /* ImageCodecInfo creation routines taken from libgdiplus */
3648 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
3649 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
3650 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
3651 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
3652 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
3653 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
3655 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
3656 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
3657 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
3658 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
3659 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
3660 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
3662 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
3663 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
3664 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
3665 static const WCHAR gif_format[] = {'G','I','F',0};
3666 static const BYTE gif_sig_pattern[4] = "GIF8";
3667 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3669 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
3670 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
3671 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
3672 static const WCHAR tiff_format[] = {'T','I','F','F',0};
3673 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
3674 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3676 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
3677 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
3678 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
3679 static const WCHAR emf_format[] = {'E','M','F',0};
3680 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
3681 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3683 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
3684 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
3685 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
3686 static const WCHAR wmf_format[] = {'W','M','F',0};
3687 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
3688 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
3690 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
3691 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
3692 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
3693 static const WCHAR png_format[] = {'P','N','G',0};
3694 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
3695 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3697 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
3698 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
3699 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
3700 static const WCHAR ico_format[] = {'I','C','O',0};
3701 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
3702 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3704 static const struct image_codec codecs[NUM_CODECS] = {
3706 { /* BMP */
3707 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3708 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3709 /* CodecName */ bmp_codecname,
3710 /* DllName */ NULL,
3711 /* FormatDescription */ bmp_format,
3712 /* FilenameExtension */ bmp_extension,
3713 /* MimeType */ bmp_mimetype,
3714 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3715 /* Version */ 1,
3716 /* SigCount */ 1,
3717 /* SigSize */ 2,
3718 /* SigPattern */ bmp_sig_pattern,
3719 /* SigMask */ bmp_sig_mask,
3721 encode_image_BMP,
3722 decode_image_bmp
3725 { /* JPEG */
3726 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3727 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3728 /* CodecName */ jpeg_codecname,
3729 /* DllName */ NULL,
3730 /* FormatDescription */ jpeg_format,
3731 /* FilenameExtension */ jpeg_extension,
3732 /* MimeType */ jpeg_mimetype,
3733 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3734 /* Version */ 1,
3735 /* SigCount */ 1,
3736 /* SigSize */ 2,
3737 /* SigPattern */ jpeg_sig_pattern,
3738 /* SigMask */ jpeg_sig_mask,
3740 encode_image_jpeg,
3741 decode_image_jpeg
3744 { /* GIF */
3745 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3746 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3747 /* CodecName */ gif_codecname,
3748 /* DllName */ NULL,
3749 /* FormatDescription */ gif_format,
3750 /* FilenameExtension */ gif_extension,
3751 /* MimeType */ gif_mimetype,
3752 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3753 /* Version */ 1,
3754 /* SigCount */ 1,
3755 /* SigSize */ 4,
3756 /* SigPattern */ gif_sig_pattern,
3757 /* SigMask */ gif_sig_mask,
3759 NULL,
3760 decode_image_gif
3763 { /* TIFF */
3764 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3765 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3766 /* CodecName */ tiff_codecname,
3767 /* DllName */ NULL,
3768 /* FormatDescription */ tiff_format,
3769 /* FilenameExtension */ tiff_extension,
3770 /* MimeType */ tiff_mimetype,
3771 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3772 /* Version */ 1,
3773 /* SigCount */ 2,
3774 /* SigSize */ 4,
3775 /* SigPattern */ tiff_sig_pattern,
3776 /* SigMask */ tiff_sig_mask,
3778 encode_image_tiff,
3779 decode_image_tiff
3782 { /* EMF */
3783 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3784 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3785 /* CodecName */ emf_codecname,
3786 /* DllName */ NULL,
3787 /* FormatDescription */ emf_format,
3788 /* FilenameExtension */ emf_extension,
3789 /* MimeType */ emf_mimetype,
3790 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
3791 /* Version */ 1,
3792 /* SigCount */ 1,
3793 /* SigSize */ 4,
3794 /* SigPattern */ emf_sig_pattern,
3795 /* SigMask */ emf_sig_mask,
3797 NULL,
3798 decode_image_olepicture_metafile
3801 { /* WMF */
3802 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3803 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3804 /* CodecName */ wmf_codecname,
3805 /* DllName */ NULL,
3806 /* FormatDescription */ wmf_format,
3807 /* FilenameExtension */ wmf_extension,
3808 /* MimeType */ wmf_mimetype,
3809 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
3810 /* Version */ 1,
3811 /* SigCount */ 1,
3812 /* SigSize */ 2,
3813 /* SigPattern */ wmf_sig_pattern,
3814 /* SigMask */ wmf_sig_mask,
3816 NULL,
3817 decode_image_olepicture_metafile
3820 { /* PNG */
3821 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3822 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3823 /* CodecName */ png_codecname,
3824 /* DllName */ NULL,
3825 /* FormatDescription */ png_format,
3826 /* FilenameExtension */ png_extension,
3827 /* MimeType */ png_mimetype,
3828 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3829 /* Version */ 1,
3830 /* SigCount */ 1,
3831 /* SigSize */ 8,
3832 /* SigPattern */ png_sig_pattern,
3833 /* SigMask */ png_sig_mask,
3835 encode_image_png,
3836 decode_image_png
3839 { /* ICO */
3840 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3841 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3842 /* CodecName */ ico_codecname,
3843 /* DllName */ NULL,
3844 /* FormatDescription */ ico_format,
3845 /* FilenameExtension */ ico_extension,
3846 /* MimeType */ ico_mimetype,
3847 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3848 /* Version */ 1,
3849 /* SigCount */ 1,
3850 /* SigSize */ 4,
3851 /* SigPattern */ ico_sig_pattern,
3852 /* SigMask */ ico_sig_mask,
3854 NULL,
3855 decode_image_icon
3859 /*****************************************************************************
3860 * GdipGetImageDecodersSize [GDIPLUS.@]
3862 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
3864 int decoder_count=0;
3865 int i;
3866 TRACE("%p %p\n", numDecoders, size);
3868 if (!numDecoders || !size)
3869 return InvalidParameter;
3871 for (i=0; i<NUM_CODECS; i++)
3873 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
3874 decoder_count++;
3877 *numDecoders = decoder_count;
3878 *size = decoder_count * sizeof(ImageCodecInfo);
3880 return Ok;
3883 /*****************************************************************************
3884 * GdipGetImageDecoders [GDIPLUS.@]
3886 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
3888 int i, decoder_count=0;
3889 TRACE("%u %u %p\n", numDecoders, size, decoders);
3891 if (!decoders ||
3892 size != numDecoders * sizeof(ImageCodecInfo))
3893 return GenericError;
3895 for (i=0; i<NUM_CODECS; i++)
3897 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
3899 if (decoder_count == numDecoders) return GenericError;
3900 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
3901 decoder_count++;
3905 if (decoder_count < numDecoders) return GenericError;
3907 return Ok;
3910 /*****************************************************************************
3911 * GdipGetImageEncodersSize [GDIPLUS.@]
3913 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
3915 int encoder_count=0;
3916 int i;
3917 TRACE("%p %p\n", numEncoders, size);
3919 if (!numEncoders || !size)
3920 return InvalidParameter;
3922 for (i=0; i<NUM_CODECS; i++)
3924 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
3925 encoder_count++;
3928 *numEncoders = encoder_count;
3929 *size = encoder_count * sizeof(ImageCodecInfo);
3931 return Ok;
3934 /*****************************************************************************
3935 * GdipGetImageEncoders [GDIPLUS.@]
3937 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
3939 int i, encoder_count=0;
3940 TRACE("%u %u %p\n", numEncoders, size, encoders);
3942 if (!encoders ||
3943 size != numEncoders * sizeof(ImageCodecInfo))
3944 return GenericError;
3946 for (i=0; i<NUM_CODECS; i++)
3948 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
3950 if (encoder_count == numEncoders) return GenericError;
3951 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
3952 encoder_count++;
3956 if (encoder_count < numEncoders) return GenericError;
3958 return Ok;
3961 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
3962 GDIPCONST CLSID* clsidEncoder, UINT *size)
3964 static int calls;
3966 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
3968 if(!(calls++))
3969 FIXME("not implemented\n");
3971 *size = 0;
3973 return NotImplemented;
3976 static PixelFormat get_16bpp_format(HBITMAP hbm)
3978 BITMAPV4HEADER bmh;
3979 HDC hdc;
3980 PixelFormat result;
3982 hdc = CreateCompatibleDC(NULL);
3984 memset(&bmh, 0, sizeof(bmh));
3985 bmh.bV4Size = sizeof(bmh);
3986 bmh.bV4Width = 1;
3987 bmh.bV4Height = 1;
3988 bmh.bV4V4Compression = BI_BITFIELDS;
3989 bmh.bV4BitCount = 16;
3991 GetDIBits(hdc, hbm, 0, 0, NULL, (BITMAPINFO*)&bmh, DIB_RGB_COLORS);
3993 if (bmh.bV4RedMask == 0x7c00 &&
3994 bmh.bV4GreenMask == 0x3e0 &&
3995 bmh.bV4BlueMask == 0x1f)
3997 result = PixelFormat16bppRGB555;
3999 else if (bmh.bV4RedMask == 0xf800 &&
4000 bmh.bV4GreenMask == 0x7e0 &&
4001 bmh.bV4BlueMask == 0x1f)
4003 result = PixelFormat16bppRGB565;
4005 else
4007 FIXME("unrecognized bitfields %x,%x,%x\n", bmh.bV4RedMask,
4008 bmh.bV4GreenMask, bmh.bV4BlueMask);
4009 result = PixelFormatUndefined;
4012 DeleteDC(hdc);
4014 return result;
4017 /*****************************************************************************
4018 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
4020 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
4022 BITMAP bm;
4023 GpStatus retval;
4024 PixelFormat format;
4025 BitmapData lockeddata;
4026 INT y;
4028 TRACE("%p %p %p\n", hbm, hpal, bitmap);
4030 if(!hbm || !bitmap)
4031 return InvalidParameter;
4033 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
4034 return InvalidParameter;
4036 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
4037 switch(bm.bmBitsPixel) {
4038 case 1:
4039 format = PixelFormat1bppIndexed;
4040 break;
4041 case 4:
4042 format = PixelFormat4bppIndexed;
4043 break;
4044 case 8:
4045 format = PixelFormat8bppIndexed;
4046 break;
4047 case 16:
4048 format = get_16bpp_format(hbm);
4049 if (format == PixelFormatUndefined)
4050 return InvalidParameter;
4051 break;
4052 case 24:
4053 format = PixelFormat24bppRGB;
4054 break;
4055 case 32:
4056 format = PixelFormat32bppRGB;
4057 break;
4058 case 48:
4059 format = PixelFormat48bppRGB;
4060 break;
4061 default:
4062 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
4063 return InvalidParameter;
4066 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
4067 format, NULL, bitmap);
4069 if (retval == Ok)
4071 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
4072 format, &lockeddata);
4073 if (retval == Ok)
4075 if (bm.bmBits)
4077 for (y=0; y<bm.bmHeight; y++)
4079 memcpy((BYTE*)lockeddata.Scan0+lockeddata.Stride*y,
4080 (BYTE*)bm.bmBits+bm.bmWidthBytes*(bm.bmHeight-1-y),
4081 bm.bmWidthBytes);
4084 else
4086 HDC hdc;
4087 HBITMAP oldhbm;
4088 BITMAPINFO *pbmi;
4089 INT src_height, dst_stride;
4090 BYTE *dst_bits;
4092 hdc = CreateCompatibleDC(NULL);
4093 oldhbm = SelectObject(hdc, hbm);
4095 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
4097 if (pbmi)
4099 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
4100 pbmi->bmiHeader.biBitCount = 0;
4102 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
4104 src_height = abs(pbmi->bmiHeader.biHeight);
4106 if (pbmi->bmiHeader.biHeight > 0)
4108 dst_bits = (BYTE*)lockeddata.Scan0+lockeddata.Stride*(src_height-1);
4109 dst_stride = -lockeddata.Stride;
4111 else
4113 dst_bits = lockeddata.Scan0;
4114 dst_stride = lockeddata.Stride;
4117 for (y=0; y<src_height; y++)
4119 GetDIBits(hdc, hbm, y, 1, dst_bits+dst_stride*y,
4120 pbmi, DIB_RGB_COLORS);
4123 GdipFree(pbmi);
4125 else
4126 retval = OutOfMemory;
4128 SelectObject(hdc, oldhbm);
4129 DeleteDC(hdc);
4132 GdipBitmapUnlockBits(*bitmap, &lockeddata);
4135 if (retval == Ok && hpal)
4137 WORD num_palette_entries;
4138 PALETTEENTRY *palette_entries=NULL;
4139 ColorPalette *palette=NULL;
4140 int i;
4142 if (!GetObjectW(hpal, sizeof(num_palette_entries), &num_palette_entries))
4143 retval = GenericError;
4145 if (retval == Ok)
4147 palette_entries = GdipAlloc(sizeof(PALETTEENTRY) * num_palette_entries);
4148 palette = GdipAlloc(sizeof(ColorPalette) + sizeof(ARGB) * (num_palette_entries-1));
4150 if (!palette_entries || !palette)
4151 retval = OutOfMemory;
4154 if (retval == Ok)
4156 if (!GetPaletteEntries(hpal, 0, num_palette_entries, palette_entries))
4157 retval = GenericError;
4160 if (retval == Ok)
4162 palette->Flags = 0;
4163 palette->Count = num_palette_entries;
4165 for (i=0; i<num_palette_entries; i++)
4167 PALETTEENTRY * entry = &palette_entries[i];
4168 palette->Entries[i] = 0xff000000 | entry->peRed << 16 |
4169 entry->peGreen << 8 | entry->peBlue;
4172 retval = GdipSetImagePalette((GpImage*)*bitmap, palette);
4175 GdipFree(palette_entries);
4176 GdipFree(palette);
4179 if (retval != Ok)
4181 GdipDisposeImage((GpImage*)*bitmap);
4182 *bitmap = NULL;
4186 return retval;
4189 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
4191 FIXME("(%p): stub\n", effect);
4192 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
4193 * in Windows's gdiplus */
4194 return NotImplemented;
4197 /*****************************************************************************
4198 * GdipSetEffectParameters [GDIPLUS.@]
4200 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
4201 const VOID *params, const UINT size)
4203 static int calls;
4205 TRACE("(%p,%p,%u)\n", effect, params, size);
4207 if(!(calls++))
4208 FIXME("not implemented\n");
4210 return NotImplemented;
4213 /*****************************************************************************
4214 * GdipGetImageFlags [GDIPLUS.@]
4216 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
4218 TRACE("%p %p\n", image, flags);
4220 if(!image || !flags)
4221 return InvalidParameter;
4223 *flags = image->flags;
4225 return Ok;
4228 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
4230 TRACE("(%d, %p)\n", control, param);
4232 switch(control){
4233 case TestControlForceBilinear:
4234 if(param)
4235 FIXME("TestControlForceBilinear not handled\n");
4236 break;
4237 case TestControlNoICM:
4238 if(param)
4239 FIXME("TestControlNoICM not handled\n");
4240 break;
4241 case TestControlGetBuildNumber:
4242 *((DWORD*)param) = 3102;
4243 break;
4246 return Ok;
4249 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
4250 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
4251 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
4252 GpMetafile **metafile)
4254 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
4255 frameUnit, debugstr_w(desc), metafile);
4257 return NotImplemented;
4260 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
4261 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
4262 GDIPCONST WCHAR *desc, GpMetafile **metafile)
4264 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
4265 frameUnit, debugstr_w(desc), metafile);
4267 return NotImplemented;
4270 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
4272 TRACE("%p\n", image);
4274 return Ok;
4277 /*****************************************************************************
4278 * GdipGetImageThumbnail [GDIPLUS.@]
4280 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
4281 GpImage **ret_image, GetThumbnailImageAbort cb,
4282 VOID * cb_data)
4284 GpStatus stat;
4285 GpGraphics *graphics;
4286 UINT srcwidth, srcheight;
4288 TRACE("(%p %u %u %p %p %p)\n",
4289 image, width, height, ret_image, cb, cb_data);
4291 if (!image || !ret_image)
4292 return InvalidParameter;
4294 if (!width) width = 120;
4295 if (!height) height = 120;
4297 GdipGetImageWidth(image, &srcwidth);
4298 GdipGetImageHeight(image, &srcheight);
4300 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB,
4301 NULL, (GpBitmap**)ret_image);
4303 if (stat == Ok)
4305 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
4307 if (stat == Ok)
4309 stat = GdipDrawImageRectRectI(graphics, image,
4310 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
4311 NULL, NULL, NULL);
4313 GdipDeleteGraphics(graphics);
4316 if (stat != Ok)
4318 GdipDisposeImage(*ret_image);
4319 *ret_image = NULL;
4323 return stat;
4326 /*****************************************************************************
4327 * GdipImageRotateFlip [GDIPLUS.@]
4329 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
4331 GpBitmap *new_bitmap;
4332 GpBitmap *bitmap;
4333 int bpp, bytesperpixel;
4334 int rotate_90, flip_x, flip_y;
4335 int src_x_offset, src_y_offset;
4336 LPBYTE src_origin;
4337 UINT x, y, width, height;
4338 BitmapData src_lock, dst_lock;
4339 GpStatus stat;
4341 TRACE("(%p, %u)\n", image, type);
4343 if (!image)
4344 return InvalidParameter;
4346 rotate_90 = type&1;
4347 flip_x = (type&6) == 2 || (type&6) == 4;
4348 flip_y = (type&3) == 1 || (type&3) == 2;
4350 if (image->type != ImageTypeBitmap)
4352 FIXME("Not implemented for type %i\n", image->type);
4353 return NotImplemented;
4356 bitmap = (GpBitmap*)image;
4357 bpp = PIXELFORMATBPP(bitmap->format);
4359 if (bpp < 8)
4361 FIXME("Not implemented for %i bit images\n", bpp);
4362 return NotImplemented;
4365 if (rotate_90)
4367 width = bitmap->height;
4368 height = bitmap->width;
4370 else
4372 width = bitmap->width;
4373 height = bitmap->height;
4376 bytesperpixel = bpp/8;
4378 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
4380 if (stat != Ok)
4381 return stat;
4383 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
4385 if (stat == Ok)
4387 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
4389 if (stat == Ok)
4391 LPBYTE src_row, src_pixel;
4392 LPBYTE dst_row, dst_pixel;
4394 src_origin = src_lock.Scan0;
4395 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
4396 if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
4398 if (rotate_90)
4400 if (flip_y) src_x_offset = -src_lock.Stride;
4401 else src_x_offset = src_lock.Stride;
4402 if (flip_x) src_y_offset = -bytesperpixel;
4403 else src_y_offset = bytesperpixel;
4405 else
4407 if (flip_x) src_x_offset = -bytesperpixel;
4408 else src_x_offset = bytesperpixel;
4409 if (flip_y) src_y_offset = -src_lock.Stride;
4410 else src_y_offset = src_lock.Stride;
4413 src_row = src_origin;
4414 dst_row = dst_lock.Scan0;
4415 for (y=0; y<height; y++)
4417 src_pixel = src_row;
4418 dst_pixel = dst_row;
4419 for (x=0; x<width; x++)
4421 /* FIXME: This could probably be faster without memcpy. */
4422 memcpy(dst_pixel, src_pixel, bytesperpixel);
4423 dst_pixel += bytesperpixel;
4424 src_pixel += src_x_offset;
4426 src_row += src_y_offset;
4427 dst_row += dst_lock.Stride;
4430 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
4433 GdipBitmapUnlockBits(bitmap, &src_lock);
4436 if (stat == Ok)
4437 move_bitmap(bitmap, new_bitmap, FALSE);
4438 else
4439 GdipDisposeImage((GpImage*)new_bitmap);
4441 return stat;
4444 /*****************************************************************************
4445 * GdipConvertToEmfPlusToFile [GDIPLUS.@]
4448 GpStatus WINGDIPAPI GdipConvertToEmfPlusToFile(const GpGraphics* refGraphics,
4449 GpMetafile* metafile, BOOL* conversionSuccess,
4450 const WCHAR* filename, EmfType emfType,
4451 const WCHAR* description, GpMetafile** out_metafile)
4453 FIXME("stub: %p, %p, %p, %p, %u, %p, %p\n", refGraphics, metafile, conversionSuccess, filename, emfType, description, out_metafile);
4454 return NotImplemented;