gdiplus: Add support for encoding TIFF images.
[wine/multimedia.git] / dlls / gdiplus / image.c
blobc0ea4a4e310ecaa2444b606e23fa7b13ef0e82cb
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>
21 #define NONAMELESSUNION
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
28 #define COBJMACROS
29 #include "objbase.h"
30 #include "olectl.h"
31 #include "ole2.h"
33 #include "initguid.h"
34 #include "wincodec.h"
35 #include "gdiplus.h"
36 #include "gdiplus_private.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
41 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
43 static INT ipicture_pixel_height(IPicture *pic)
45 HDC hdcref;
46 OLE_YSIZE_HIMETRIC y;
48 IPicture_get_Height(pic, &y);
50 hdcref = GetDC(0);
52 y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
53 ReleaseDC(0, hdcref);
55 return y;
58 static INT ipicture_pixel_width(IPicture *pic)
60 HDC hdcref;
61 OLE_XSIZE_HIMETRIC x;
63 IPicture_get_Width(pic, &x);
65 hdcref = GetDC(0);
67 x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
69 ReleaseDC(0, hdcref);
71 return x;
74 GpStatus WINGDIPAPI GdipBitmapApplyEffect(GpBitmap* bitmap, CGpEffect* effect,
75 RECT* roi, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
77 FIXME("(%p %p %p %d %p %p): stub\n", bitmap, effect, roi, useAuxData, auxData, auxDataSize);
79 * Note: According to Jose Roca's GDI+ docs, this function is not
80 * implemented in Windows's GDI+.
82 return NotImplemented;
85 GpStatus WINGDIPAPI GdipBitmapCreateApplyEffect(GpBitmap** inputBitmaps,
86 INT numInputs, CGpEffect* effect, RECT* roi, RECT* outputRect,
87 GpBitmap** outputBitmap, BOOL useAuxData, VOID** auxData, INT* auxDataSize)
89 FIXME("(%p %d %p %p %p %p %d %p %p): stub\n", inputBitmaps, numInputs, effect, roi, outputRect, outputBitmap, useAuxData, auxData, auxDataSize);
91 * Note: According to Jose Roca's GDI+ docs, this function is not
92 * implemented in Windows's GDI+.
94 return NotImplemented;
97 static inline void getpixel_1bppIndexed(BYTE *index, const BYTE *row, UINT x)
99 *index = (row[x/8]>>(7-x%8)) & 1;
102 static inline void getpixel_4bppIndexed(BYTE *index, const BYTE *row, UINT x)
104 if (x & 1)
105 *index = row[x/2]&0xf;
106 else
107 *index = row[x/2]>>4;
110 static inline void getpixel_8bppIndexed(BYTE *index, const BYTE *row, UINT x)
112 *index = row[x];
115 static inline void getpixel_16bppGrayScale(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
116 const BYTE *row, UINT x)
118 *r = *g = *b = row[x*2+1];
119 *a = 255;
122 static inline void getpixel_16bppRGB555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
123 const BYTE *row, UINT x)
125 WORD pixel = *((const WORD*)(row)+x);
126 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
127 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
128 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
129 *a = 255;
132 static inline void getpixel_16bppRGB565(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
133 const BYTE *row, UINT x)
135 WORD pixel = *((const WORD*)(row)+x);
136 *r = (pixel>>8&0xf8)|(pixel>>13&0x7);
137 *g = (pixel>>3&0xfc)|(pixel>>9&0x3);
138 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
139 *a = 255;
142 static inline void getpixel_16bppARGB1555(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
143 const BYTE *row, UINT x)
145 WORD pixel = *((const WORD*)(row)+x);
146 *r = (pixel>>7&0xf8)|(pixel>>12&0x7);
147 *g = (pixel>>2&0xf8)|(pixel>>6&0x7);
148 *b = (pixel<<3&0xf8)|(pixel>>2&0x7);
149 if ((pixel&0x8000) == 0x8000)
150 *a = 255;
151 else
152 *a = 0;
155 static inline void getpixel_24bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
156 const BYTE *row, UINT x)
158 *r = row[x*3+2];
159 *g = row[x*3+1];
160 *b = row[x*3];
161 *a = 255;
164 static inline void getpixel_32bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
165 const BYTE *row, UINT x)
167 *r = row[x*4+2];
168 *g = row[x*4+1];
169 *b = row[x*4];
170 *a = 255;
173 static inline void getpixel_32bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
174 const BYTE *row, UINT x)
176 *r = row[x*4+2];
177 *g = row[x*4+1];
178 *b = row[x*4];
179 *a = row[x*4+3];
182 static inline void getpixel_32bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
183 const BYTE *row, UINT x)
185 *a = row[x*4+3];
186 if (*a == 0)
187 *r = *g = *b = 0;
188 else
190 *r = row[x*4+2] * 255 / *a;
191 *g = row[x*4+1] * 255 / *a;
192 *b = row[x*4] * 255 / *a;
196 static inline void getpixel_48bppRGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
197 const BYTE *row, UINT x)
199 *r = row[x*6+5];
200 *g = row[x*6+3];
201 *b = row[x*6+1];
202 *a = 255;
205 static inline void getpixel_64bppARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
206 const BYTE *row, UINT x)
208 *r = row[x*8+5];
209 *g = row[x*8+3];
210 *b = row[x*8+1];
211 *a = row[x*8+7];
214 static inline void getpixel_64bppPARGB(BYTE *r, BYTE *g, BYTE *b, BYTE *a,
215 const BYTE *row, UINT x)
217 *a = row[x*8+7];
218 if (*a == 0)
219 *r = *g = *b = 0;
220 else
222 *r = row[x*8+5] * 255 / *a;
223 *g = row[x*8+3] * 255 / *a;
224 *b = row[x*8+1] * 255 / *a;
228 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
229 ARGB *color)
231 BYTE r, g, b, a;
232 BYTE index;
233 BYTE *row;
234 TRACE("%p %d %d %p\n", bitmap, x, y, color);
236 if(!bitmap || !color ||
237 x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
238 return InvalidParameter;
240 row = bitmap->bits+bitmap->stride*y;
242 switch (bitmap->format)
244 case PixelFormat1bppIndexed:
245 getpixel_1bppIndexed(&index,row,x);
246 break;
247 case PixelFormat4bppIndexed:
248 getpixel_4bppIndexed(&index,row,x);
249 break;
250 case PixelFormat8bppIndexed:
251 getpixel_8bppIndexed(&index,row,x);
252 break;
253 case PixelFormat16bppGrayScale:
254 getpixel_16bppGrayScale(&r,&g,&b,&a,row,x);
255 break;
256 case PixelFormat16bppRGB555:
257 getpixel_16bppRGB555(&r,&g,&b,&a,row,x);
258 break;
259 case PixelFormat16bppRGB565:
260 getpixel_16bppRGB565(&r,&g,&b,&a,row,x);
261 break;
262 case PixelFormat16bppARGB1555:
263 getpixel_16bppARGB1555(&r,&g,&b,&a,row,x);
264 break;
265 case PixelFormat24bppRGB:
266 getpixel_24bppRGB(&r,&g,&b,&a,row,x);
267 break;
268 case PixelFormat32bppRGB:
269 getpixel_32bppRGB(&r,&g,&b,&a,row,x);
270 break;
271 case PixelFormat32bppARGB:
272 getpixel_32bppARGB(&r,&g,&b,&a,row,x);
273 break;
274 case PixelFormat32bppPARGB:
275 getpixel_32bppPARGB(&r,&g,&b,&a,row,x);
276 break;
277 case PixelFormat48bppRGB:
278 getpixel_48bppRGB(&r,&g,&b,&a,row,x);
279 break;
280 case PixelFormat64bppARGB:
281 getpixel_64bppARGB(&r,&g,&b,&a,row,x);
282 break;
283 case PixelFormat64bppPARGB:
284 getpixel_64bppPARGB(&r,&g,&b,&a,row,x);
285 break;
286 default:
287 FIXME("not implemented for format 0x%x\n", bitmap->format);
288 return NotImplemented;
291 if (bitmap->format & PixelFormatIndexed)
292 *color = bitmap->image.palette_entries[index];
293 else
294 *color = a<<24|r<<16|g<<8|b;
296 return Ok;
299 static inline UINT get_palette_index(BYTE r, BYTE g, BYTE b, BYTE a, GpBitmap* bitmap) {
300 BYTE index = 0;
301 int best_distance = 0x7fff;
302 int distance;
303 int i;
304 /* This algorithm scans entire pallete,
305 computes difference from desired color (all color components have equal weight)
306 and returns the index of color with least difference.
308 Note: Maybe it could be replaced with a better algorithm for better image quality
309 and performance, though better algorithm would probably need some pre-built lookup
310 tables and thus may actually be slower if this method is called only few times per
311 every image.
313 for(i=0;i<bitmap->image.palette_size;i++) {
314 ARGB color=bitmap->image.palette_entries[i];
315 distance=abs(b-(color & 0xff)) + abs(g-(color>>8 & 0xff)) + abs(r-(color>>16 & 0xff)) + abs(a-(color>>24 & 0xff));
316 if (distance<best_distance) {
317 best_distance=distance;
318 index=i;
321 return index;
324 static inline void setpixel_8bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
325 BYTE *row, UINT x, GpBitmap* bitmap)
327 BYTE index = get_palette_index(r,g,b,a,bitmap);
328 row[x]=index;
331 static inline void setpixel_1bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
332 BYTE *row, UINT x, GpBitmap* bitmap)
334 row[x/8] = (row[x/8] & ~(1<<(7-x%8))) | (get_palette_index(r,g,b,a,bitmap)<<(7-x%8));
337 static inline void setpixel_4bppIndexed(BYTE r, BYTE g, BYTE b, BYTE a,
338 BYTE *row, UINT x, GpBitmap* bitmap)
340 if (x & 1)
341 row[x/2] = (row[x/2] & 0xf0) | get_palette_index(r,g,b,a,bitmap);
342 else
343 row[x/2] = (row[x/2] & 0x0f) | get_palette_index(r,g,b,a,bitmap)<<4;
346 static inline void setpixel_16bppGrayScale(BYTE r, BYTE g, BYTE b, BYTE a,
347 BYTE *row, UINT x)
349 *((WORD*)(row)+x) = (r+g+b)*85;
352 static inline void setpixel_16bppRGB555(BYTE r, BYTE g, BYTE b, BYTE a,
353 BYTE *row, UINT x)
355 *((WORD*)(row)+x) = (r<<7&0x7c00)|
356 (g<<2&0x03e0)|
357 (b>>3&0x001f);
360 static inline void setpixel_16bppRGB565(BYTE r, BYTE g, BYTE b, BYTE a,
361 BYTE *row, UINT x)
363 *((WORD*)(row)+x) = (r<<8&0xf800)|
364 (g<<3&0x07e0)|
365 (b>>3&0x001f);
368 static inline void setpixel_16bppARGB1555(BYTE r, BYTE g, BYTE b, BYTE a,
369 BYTE *row, UINT x)
371 *((WORD*)(row)+x) = (a<<8&0x8000)|
372 (r<<7&0x7c00)|
373 (g<<2&0x03e0)|
374 (b>>3&0x001f);
377 static inline void setpixel_24bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
378 BYTE *row, UINT x)
380 row[x*3+2] = r;
381 row[x*3+1] = g;
382 row[x*3] = b;
385 static inline void setpixel_32bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
386 BYTE *row, UINT x)
388 *((DWORD*)(row)+x) = (r<<16)|(g<<8)|b;
391 static inline void setpixel_32bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
392 BYTE *row, UINT x)
394 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
397 static inline void setpixel_32bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
398 BYTE *row, UINT x)
400 r = r * a / 255;
401 g = g * a / 255;
402 b = b * a / 255;
403 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
406 static inline void setpixel_48bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
407 BYTE *row, UINT x)
409 row[x*6+5] = row[x*6+4] = r;
410 row[x*6+3] = row[x*6+2] = g;
411 row[x*6+1] = row[x*6] = b;
414 static inline void setpixel_64bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
415 BYTE *row, UINT x)
417 UINT64 a64=a, r64=r, g64=g, b64=b;
418 *((UINT64*)(row)+x) = (a64<<56)|(a64<<48)|(r64<<40)|(r64<<32)|(g64<<24)|(g64<<16)|(b64<<8)|b64;
421 static inline void setpixel_64bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
422 BYTE *row, UINT x)
424 UINT64 a64, r64, g64, b64;
425 a64 = a * 257;
426 r64 = r * a / 255;
427 g64 = g * a / 255;
428 b64 = b * a / 255;
429 *((UINT64*)(row)+x) = (a64<<48)|(r64<<32)|(g64<<16)|b64;
432 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
433 ARGB color)
435 BYTE a, r, g, b;
436 BYTE *row;
437 TRACE("bitmap:%p, x:%d, y:%d, color:%08x\n", bitmap, x, y, color);
439 if(!bitmap || x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
440 return InvalidParameter;
442 a = color>>24;
443 r = color>>16;
444 g = color>>8;
445 b = color;
447 row = bitmap->bits + bitmap->stride * y;
449 switch (bitmap->format)
451 case PixelFormat16bppGrayScale:
452 setpixel_16bppGrayScale(r,g,b,a,row,x);
453 break;
454 case PixelFormat16bppRGB555:
455 setpixel_16bppRGB555(r,g,b,a,row,x);
456 break;
457 case PixelFormat16bppRGB565:
458 setpixel_16bppRGB565(r,g,b,a,row,x);
459 break;
460 case PixelFormat16bppARGB1555:
461 setpixel_16bppARGB1555(r,g,b,a,row,x);
462 break;
463 case PixelFormat24bppRGB:
464 setpixel_24bppRGB(r,g,b,a,row,x);
465 break;
466 case PixelFormat32bppRGB:
467 setpixel_32bppRGB(r,g,b,a,row,x);
468 break;
469 case PixelFormat32bppARGB:
470 setpixel_32bppARGB(r,g,b,a,row,x);
471 break;
472 case PixelFormat32bppPARGB:
473 setpixel_32bppPARGB(r,g,b,a,row,x);
474 break;
475 case PixelFormat48bppRGB:
476 setpixel_48bppRGB(r,g,b,a,row,x);
477 break;
478 case PixelFormat64bppARGB:
479 setpixel_64bppARGB(r,g,b,a,row,x);
480 break;
481 case PixelFormat64bppPARGB:
482 setpixel_64bppPARGB(r,g,b,a,row,x);
483 break;
484 case PixelFormat8bppIndexed:
485 setpixel_8bppIndexed(r,g,b,a,row,x,bitmap);
486 break;
487 case PixelFormat4bppIndexed:
488 setpixel_4bppIndexed(r,g,b,a,row,x,bitmap);
489 break;
490 case PixelFormat1bppIndexed:
491 setpixel_1bppIndexed(r,g,b,a,row,x,bitmap);
492 break;
493 default:
494 FIXME("not implemented for format 0x%x\n", bitmap->format);
495 return NotImplemented;
498 return Ok;
501 GpStatus convert_pixels(INT width, INT height,
502 INT dst_stride, BYTE *dst_bits, PixelFormat dst_format,
503 INT src_stride, const BYTE *src_bits, PixelFormat src_format, ARGB *src_palette)
505 INT x, y;
507 if (src_format == dst_format ||
508 (dst_format == PixelFormat32bppRGB && PIXELFORMATBPP(src_format) == 32))
510 UINT widthbytes = PIXELFORMATBPP(src_format) * width / 8;
511 for (y=0; y<height; y++)
512 memcpy(dst_bits+dst_stride*y, src_bits+src_stride*y, widthbytes);
513 return Ok;
516 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
517 for (x=0; x<width; x++) \
518 for (y=0; y<height; y++) { \
519 BYTE index; \
520 BYTE *color; \
521 getpixel_function(&index, src_bits+src_stride*y, x); \
522 color = (BYTE*)(&src_palette[index]); \
523 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
525 return Ok; \
526 } while (0);
528 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
529 for (x=0; x<width; x++) \
530 for (y=0; y<height; y++) { \
531 BYTE r, g, b, a; \
532 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
533 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
535 return Ok; \
536 } while (0);
538 switch (src_format)
540 case PixelFormat1bppIndexed:
541 switch (dst_format)
543 case PixelFormat16bppGrayScale:
544 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppGrayScale);
545 case PixelFormat16bppRGB555:
546 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB555);
547 case PixelFormat16bppRGB565:
548 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB565);
549 case PixelFormat16bppARGB1555:
550 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppARGB1555);
551 case PixelFormat24bppRGB:
552 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_24bppRGB);
553 case PixelFormat32bppRGB:
554 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppRGB);
555 case PixelFormat32bppARGB:
556 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppARGB);
557 case PixelFormat32bppPARGB:
558 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppPARGB);
559 case PixelFormat48bppRGB:
560 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_48bppRGB);
561 case PixelFormat64bppARGB:
562 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_64bppARGB);
563 default:
564 break;
566 break;
567 case PixelFormat4bppIndexed:
568 switch (dst_format)
570 case PixelFormat16bppGrayScale:
571 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppGrayScale);
572 case PixelFormat16bppRGB555:
573 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB555);
574 case PixelFormat16bppRGB565:
575 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB565);
576 case PixelFormat16bppARGB1555:
577 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppARGB1555);
578 case PixelFormat24bppRGB:
579 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_24bppRGB);
580 case PixelFormat32bppRGB:
581 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppRGB);
582 case PixelFormat32bppARGB:
583 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppARGB);
584 case PixelFormat32bppPARGB:
585 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppPARGB);
586 case PixelFormat48bppRGB:
587 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_48bppRGB);
588 case PixelFormat64bppARGB:
589 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_64bppARGB);
590 default:
591 break;
593 break;
594 case PixelFormat8bppIndexed:
595 switch (dst_format)
597 case PixelFormat16bppGrayScale:
598 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppGrayScale);
599 case PixelFormat16bppRGB555:
600 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB555);
601 case PixelFormat16bppRGB565:
602 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB565);
603 case PixelFormat16bppARGB1555:
604 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppARGB1555);
605 case PixelFormat24bppRGB:
606 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_24bppRGB);
607 case PixelFormat32bppRGB:
608 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppRGB);
609 case PixelFormat32bppARGB:
610 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppARGB);
611 case PixelFormat32bppPARGB:
612 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppPARGB);
613 case PixelFormat48bppRGB:
614 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_48bppRGB);
615 case PixelFormat64bppARGB:
616 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_64bppARGB);
617 default:
618 break;
620 break;
621 case PixelFormat16bppGrayScale:
622 switch (dst_format)
624 case PixelFormat16bppRGB555:
625 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB555);
626 case PixelFormat16bppRGB565:
627 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB565);
628 case PixelFormat16bppARGB1555:
629 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppARGB1555);
630 case PixelFormat24bppRGB:
631 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_24bppRGB);
632 case PixelFormat32bppRGB:
633 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppRGB);
634 case PixelFormat32bppARGB:
635 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppARGB);
636 case PixelFormat32bppPARGB:
637 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppPARGB);
638 case PixelFormat48bppRGB:
639 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_48bppRGB);
640 case PixelFormat64bppARGB:
641 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_64bppARGB);
642 default:
643 break;
645 break;
646 case PixelFormat16bppRGB555:
647 switch (dst_format)
649 case PixelFormat16bppGrayScale:
650 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppGrayScale);
651 case PixelFormat16bppRGB565:
652 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppRGB565);
653 case PixelFormat16bppARGB1555:
654 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppARGB1555);
655 case PixelFormat24bppRGB:
656 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_24bppRGB);
657 case PixelFormat32bppRGB:
658 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppRGB);
659 case PixelFormat32bppARGB:
660 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppARGB);
661 case PixelFormat32bppPARGB:
662 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppPARGB);
663 case PixelFormat48bppRGB:
664 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_48bppRGB);
665 case PixelFormat64bppARGB:
666 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_64bppARGB);
667 default:
668 break;
670 break;
671 case PixelFormat16bppRGB565:
672 switch (dst_format)
674 case PixelFormat16bppGrayScale:
675 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppGrayScale);
676 case PixelFormat16bppRGB555:
677 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppRGB555);
678 case PixelFormat16bppARGB1555:
679 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppARGB1555);
680 case PixelFormat24bppRGB:
681 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_24bppRGB);
682 case PixelFormat32bppRGB:
683 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppRGB);
684 case PixelFormat32bppARGB:
685 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppARGB);
686 case PixelFormat32bppPARGB:
687 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppPARGB);
688 case PixelFormat48bppRGB:
689 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_48bppRGB);
690 case PixelFormat64bppARGB:
691 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_64bppARGB);
692 default:
693 break;
695 break;
696 case PixelFormat16bppARGB1555:
697 switch (dst_format)
699 case PixelFormat16bppGrayScale:
700 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppGrayScale);
701 case PixelFormat16bppRGB555:
702 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB555);
703 case PixelFormat16bppRGB565:
704 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB565);
705 case PixelFormat24bppRGB:
706 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_24bppRGB);
707 case PixelFormat32bppRGB:
708 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppRGB);
709 case PixelFormat32bppARGB:
710 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppARGB);
711 case PixelFormat32bppPARGB:
712 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppPARGB);
713 case PixelFormat48bppRGB:
714 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_48bppRGB);
715 case PixelFormat64bppARGB:
716 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_64bppARGB);
717 default:
718 break;
720 break;
721 case PixelFormat24bppRGB:
722 switch (dst_format)
724 case PixelFormat16bppGrayScale:
725 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppGrayScale);
726 case PixelFormat16bppRGB555:
727 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB555);
728 case PixelFormat16bppRGB565:
729 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB565);
730 case PixelFormat16bppARGB1555:
731 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppARGB1555);
732 case PixelFormat32bppRGB:
733 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppRGB);
734 case PixelFormat32bppARGB:
735 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppARGB);
736 case PixelFormat32bppPARGB:
737 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppPARGB);
738 case PixelFormat48bppRGB:
739 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_48bppRGB);
740 case PixelFormat64bppARGB:
741 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_64bppARGB);
742 default:
743 break;
745 break;
746 case PixelFormat32bppRGB:
747 switch (dst_format)
749 case PixelFormat16bppGrayScale:
750 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppGrayScale);
751 case PixelFormat16bppRGB555:
752 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB555);
753 case PixelFormat16bppRGB565:
754 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB565);
755 case PixelFormat16bppARGB1555:
756 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppARGB1555);
757 case PixelFormat24bppRGB:
758 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_24bppRGB);
759 case PixelFormat32bppARGB:
760 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppARGB);
761 case PixelFormat32bppPARGB:
762 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppPARGB);
763 case PixelFormat48bppRGB:
764 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_48bppRGB);
765 case PixelFormat64bppARGB:
766 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_64bppARGB);
767 default:
768 break;
770 break;
771 case PixelFormat32bppARGB:
772 switch (dst_format)
774 case PixelFormat16bppGrayScale:
775 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppGrayScale);
776 case PixelFormat16bppRGB555:
777 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB555);
778 case PixelFormat16bppRGB565:
779 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB565);
780 case PixelFormat16bppARGB1555:
781 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppARGB1555);
782 case PixelFormat24bppRGB:
783 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_24bppRGB);
784 case PixelFormat32bppPARGB:
785 convert_32bppARGB_to_32bppPARGB(width, height, dst_bits, dst_stride, src_bits, src_stride);
786 return Ok;
787 case PixelFormat48bppRGB:
788 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_48bppRGB);
789 case PixelFormat64bppARGB:
790 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_64bppARGB);
791 default:
792 break;
794 break;
795 case PixelFormat32bppPARGB:
796 switch (dst_format)
798 case PixelFormat16bppGrayScale:
799 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppGrayScale);
800 case PixelFormat16bppRGB555:
801 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB555);
802 case PixelFormat16bppRGB565:
803 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB565);
804 case PixelFormat16bppARGB1555:
805 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppARGB1555);
806 case PixelFormat24bppRGB:
807 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_24bppRGB);
808 case PixelFormat32bppRGB:
809 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppRGB);
810 case PixelFormat32bppARGB:
811 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppARGB);
812 case PixelFormat48bppRGB:
813 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_48bppRGB);
814 case PixelFormat64bppARGB:
815 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_64bppARGB);
816 default:
817 break;
819 break;
820 case PixelFormat48bppRGB:
821 switch (dst_format)
823 case PixelFormat16bppGrayScale:
824 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppGrayScale);
825 case PixelFormat16bppRGB555:
826 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB555);
827 case PixelFormat16bppRGB565:
828 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB565);
829 case PixelFormat16bppARGB1555:
830 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppARGB1555);
831 case PixelFormat24bppRGB:
832 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_24bppRGB);
833 case PixelFormat32bppRGB:
834 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppRGB);
835 case PixelFormat32bppARGB:
836 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppARGB);
837 case PixelFormat32bppPARGB:
838 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppPARGB);
839 case PixelFormat64bppARGB:
840 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_64bppARGB);
841 default:
842 break;
844 break;
845 case PixelFormat64bppARGB:
846 switch (dst_format)
848 case PixelFormat16bppGrayScale:
849 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppGrayScale);
850 case PixelFormat16bppRGB555:
851 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB555);
852 case PixelFormat16bppRGB565:
853 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB565);
854 case PixelFormat16bppARGB1555:
855 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppARGB1555);
856 case PixelFormat24bppRGB:
857 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_24bppRGB);
858 case PixelFormat32bppRGB:
859 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppRGB);
860 case PixelFormat32bppARGB:
861 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppARGB);
862 case PixelFormat32bppPARGB:
863 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppPARGB);
864 case PixelFormat48bppRGB:
865 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_48bppRGB);
866 default:
867 break;
869 break;
870 case PixelFormat64bppPARGB:
871 switch (dst_format)
873 case PixelFormat16bppGrayScale:
874 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppGrayScale);
875 case PixelFormat16bppRGB555:
876 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB555);
877 case PixelFormat16bppRGB565:
878 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB565);
879 case PixelFormat16bppARGB1555:
880 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppARGB1555);
881 case PixelFormat24bppRGB:
882 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_24bppRGB);
883 case PixelFormat32bppRGB:
884 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppRGB);
885 case PixelFormat32bppARGB:
886 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppARGB);
887 case PixelFormat32bppPARGB:
888 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppPARGB);
889 case PixelFormat48bppRGB:
890 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_48bppRGB);
891 case PixelFormat64bppARGB:
892 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_64bppARGB);
893 default:
894 break;
896 break;
897 default:
898 break;
901 #undef convert_indexed_to_rgb
902 #undef convert_rgb_to_rgb
904 return NotImplemented;
907 /* This function returns a pointer to an array of pixels that represents the
908 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
909 * flags. It is correct behavior that a user who calls this function with write
910 * privileges can write to the whole bitmap (not just the area in rect).
912 * FIXME: only used portion of format is bits per pixel. */
913 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
914 UINT flags, PixelFormat format, BitmapData* lockeddata)
916 INT bitspp = PIXELFORMATBPP(format);
917 GpRect act_rect; /* actual rect to be used */
918 GpStatus stat;
920 TRACE("%p %p %d 0x%x %p\n", bitmap, rect, flags, format, lockeddata);
922 if(!lockeddata || !bitmap)
923 return InvalidParameter;
925 if(rect){
926 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
927 (rect->Y + rect->Height > bitmap->height) || !flags)
928 return InvalidParameter;
930 act_rect = *rect;
932 else{
933 act_rect.X = act_rect.Y = 0;
934 act_rect.Width = bitmap->width;
935 act_rect.Height = bitmap->height;
938 if(bitmap->lockmode)
940 WARN("bitmap is already locked and cannot be locked again\n");
941 return WrongState;
944 if (bitmap->bits && bitmap->format == format && !(flags & ImageLockModeUserInputBuf))
946 /* no conversion is necessary; just use the bits directly */
947 lockeddata->Width = act_rect.Width;
948 lockeddata->Height = act_rect.Height;
949 lockeddata->PixelFormat = format;
950 lockeddata->Reserved = flags;
951 lockeddata->Stride = bitmap->stride;
952 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
953 bitmap->stride * act_rect.Y;
955 bitmap->lockmode = flags;
956 bitmap->numlocks++;
958 return Ok;
961 /* Make sure we can convert to the requested format. */
962 if (flags & ImageLockModeRead)
964 stat = convert_pixels(0, 0, 0, NULL, format, 0, NULL, bitmap->format, NULL);
965 if (stat == NotImplemented)
967 FIXME("cannot read bitmap from %x to %x\n", bitmap->format, format);
968 return NotImplemented;
972 /* If we're opening for writing, make sure we'll be able to write back in
973 * the original format. */
974 if (flags & ImageLockModeWrite)
976 stat = convert_pixels(0, 0, 0, NULL, bitmap->format, 0, NULL, format, NULL);
977 if (stat == NotImplemented)
979 FIXME("cannot write bitmap from %x to %x\n", format, bitmap->format);
980 return NotImplemented;
984 lockeddata->Width = act_rect.Width;
985 lockeddata->Height = act_rect.Height;
986 lockeddata->PixelFormat = format;
987 lockeddata->Reserved = flags;
989 if(!(flags & ImageLockModeUserInputBuf))
991 lockeddata->Stride = (((act_rect.Width * bitspp + 7) / 8) + 3) & ~3;
993 bitmap->bitmapbits = GdipAlloc(lockeddata->Stride * act_rect.Height);
995 if (!bitmap->bitmapbits) return OutOfMemory;
997 lockeddata->Scan0 = bitmap->bitmapbits;
1000 if (flags & ImageLockModeRead)
1002 static int fixme=0;
1004 if (!fixme && (PIXELFORMATBPP(bitmap->format) * act_rect.X) % 8 != 0)
1006 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1007 fixme = 1;
1010 stat = convert_pixels(act_rect.Width, act_rect.Height,
1011 lockeddata->Stride, lockeddata->Scan0, format,
1012 bitmap->stride,
1013 bitmap->bits + bitmap->stride * act_rect.Y + PIXELFORMATBPP(bitmap->format) * act_rect.X / 8,
1014 bitmap->format, bitmap->image.palette_entries);
1016 if (stat != Ok)
1018 GdipFree(bitmap->bitmapbits);
1019 bitmap->bitmapbits = NULL;
1020 return stat;
1024 bitmap->lockmode = flags;
1025 bitmap->numlocks++;
1026 bitmap->lockx = act_rect.X;
1027 bitmap->locky = act_rect.Y;
1029 return Ok;
1032 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
1034 TRACE("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
1036 if (!bitmap || xdpi == 0.0 || ydpi == 0.0)
1037 return InvalidParameter;
1039 bitmap->image.xres = xdpi;
1040 bitmap->image.yres = ydpi;
1042 return Ok;
1045 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
1046 BitmapData* lockeddata)
1048 GpStatus stat;
1049 static int fixme=0;
1051 TRACE("(%p,%p)\n", bitmap, lockeddata);
1053 if(!bitmap || !lockeddata)
1054 return InvalidParameter;
1056 if(!bitmap->lockmode)
1057 return WrongState;
1059 if(!(lockeddata->Reserved & ImageLockModeWrite)){
1060 if(!(--bitmap->numlocks))
1061 bitmap->lockmode = 0;
1063 GdipFree(bitmap->bitmapbits);
1064 bitmap->bitmapbits = NULL;
1065 return Ok;
1068 if (!bitmap->bitmapbits && !(lockeddata->Reserved & ImageLockModeUserInputBuf))
1070 /* we passed a direct reference; no need to do anything */
1071 bitmap->lockmode = 0;
1072 bitmap->numlocks = 0;
1073 return Ok;
1076 if (!fixme && (PIXELFORMATBPP(bitmap->format) * bitmap->lockx) % 8 != 0)
1078 FIXME("Cannot copy rows that don't start at a whole byte.\n");
1079 fixme = 1;
1082 stat = convert_pixels(lockeddata->Width, lockeddata->Height,
1083 bitmap->stride,
1084 bitmap->bits + bitmap->stride * bitmap->locky + PIXELFORMATBPP(bitmap->format) * bitmap->lockx / 8,
1085 bitmap->format,
1086 lockeddata->Stride, lockeddata->Scan0, lockeddata->PixelFormat, NULL);
1088 if (stat != Ok)
1090 ERR("failed to convert pixels; this should never happen\n");
1093 GdipFree(bitmap->bitmapbits);
1094 bitmap->bitmapbits = NULL;
1095 bitmap->lockmode = 0;
1096 bitmap->numlocks = 0;
1098 return stat;
1101 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
1102 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1104 BitmapData lockeddata_src, lockeddata_dst;
1105 int i;
1106 UINT row_size;
1107 Rect area;
1108 GpStatus stat;
1110 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1112 if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
1113 x < 0 || y < 0 ||
1114 x + width > srcBitmap->width || y + height > srcBitmap->height)
1116 TRACE("<-- InvalidParameter\n");
1117 return InvalidParameter;
1120 if (format == PixelFormatDontCare)
1121 format = srcBitmap->format;
1123 area.X = roundr(x);
1124 area.Y = roundr(y);
1125 area.Width = roundr(width);
1126 area.Height = roundr(height);
1128 stat = GdipBitmapLockBits(srcBitmap, &area, ImageLockModeRead, format,
1129 &lockeddata_src);
1130 if (stat != Ok) return stat;
1132 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
1133 0, lockeddata_src.PixelFormat, NULL, dstBitmap);
1134 if (stat == Ok)
1136 stat = GdipBitmapLockBits(*dstBitmap, NULL, ImageLockModeWrite,
1137 lockeddata_src.PixelFormat, &lockeddata_dst);
1139 if (stat == Ok)
1141 /* copy the image data */
1142 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
1143 for (i=0; i<lockeddata_src.Height; i++)
1144 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
1145 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
1146 row_size);
1148 GdipBitmapUnlockBits(*dstBitmap, &lockeddata_dst);
1151 if (stat != Ok)
1152 GdipDisposeImage((GpImage*)*dstBitmap);
1155 GdipBitmapUnlockBits(srcBitmap, &lockeddata_src);
1157 if (stat != Ok)
1159 *dstBitmap = NULL;
1162 return stat;
1165 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
1166 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1168 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1170 return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
1173 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
1175 GpStatus stat = GenericError;
1177 TRACE("%p, %p\n", image, cloneImage);
1179 if (!image || !cloneImage)
1180 return InvalidParameter;
1182 if (image->picture)
1184 IStream* stream;
1185 HRESULT hr;
1186 INT size;
1187 LARGE_INTEGER move;
1189 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
1190 if (FAILED(hr))
1191 return GenericError;
1193 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
1194 if(FAILED(hr))
1196 WARN("Failed to save image on stream\n");
1197 goto out;
1200 /* Set seek pointer back to the beginning of the picture */
1201 move.QuadPart = 0;
1202 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1203 if (FAILED(hr))
1204 goto out;
1206 stat = GdipLoadImageFromStream(stream, cloneImage);
1207 if (stat != Ok) WARN("Failed to load image from stream\n");
1209 out:
1210 IStream_Release(stream);
1211 return stat;
1213 else if (image->type == ImageTypeBitmap)
1215 GpBitmap *bitmap = (GpBitmap*)image;
1216 BitmapData lockeddata_src, lockeddata_dst;
1217 int i;
1218 UINT row_size;
1220 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format,
1221 &lockeddata_src);
1222 if (stat != Ok) return stat;
1224 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
1225 0, lockeddata_src.PixelFormat, NULL, (GpBitmap**)cloneImage);
1226 if (stat == Ok)
1228 stat = GdipBitmapLockBits((GpBitmap*)*cloneImage, NULL, ImageLockModeWrite,
1229 lockeddata_src.PixelFormat, &lockeddata_dst);
1231 if (stat == Ok)
1233 /* copy the image data */
1234 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
1235 for (i=0; i<lockeddata_src.Height; i++)
1236 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
1237 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
1238 row_size);
1240 GdipBitmapUnlockBits((GpBitmap*)*cloneImage, &lockeddata_dst);
1243 if (stat != Ok)
1244 GdipDisposeImage(*cloneImage);
1247 GdipBitmapUnlockBits(bitmap, &lockeddata_src);
1249 if (stat != Ok)
1251 *cloneImage = NULL;
1253 else memcpy(&(*cloneImage)->format, &image->format, sizeof(GUID));
1255 return stat;
1257 else
1259 ERR("GpImage with no IPicture or bitmap?!\n");
1260 return NotImplemented;
1264 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
1265 GpBitmap **bitmap)
1267 GpStatus stat;
1268 IStream *stream;
1270 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1272 if(!filename || !bitmap)
1273 return InvalidParameter;
1275 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1277 if(stat != Ok)
1278 return stat;
1280 stat = GdipCreateBitmapFromStream(stream, bitmap);
1282 IStream_Release(stream);
1284 return stat;
1287 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
1288 VOID *bits, GpBitmap **bitmap)
1290 DWORD height, stride;
1291 PixelFormat format;
1293 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
1295 if (!info || !bits || !bitmap)
1296 return InvalidParameter;
1298 height = abs(info->bmiHeader.biHeight);
1299 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
1301 if(info->bmiHeader.biHeight > 0) /* bottom-up */
1303 bits = (BYTE*)bits + (height - 1) * stride;
1304 stride = -stride;
1307 switch(info->bmiHeader.biBitCount) {
1308 case 1:
1309 format = PixelFormat1bppIndexed;
1310 break;
1311 case 4:
1312 format = PixelFormat4bppIndexed;
1313 break;
1314 case 8:
1315 format = PixelFormat8bppIndexed;
1316 break;
1317 case 16:
1318 format = PixelFormat16bppRGB555;
1319 break;
1320 case 24:
1321 format = PixelFormat24bppRGB;
1322 break;
1323 case 32:
1324 format = PixelFormat32bppRGB;
1325 break;
1326 default:
1327 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
1328 *bitmap = NULL;
1329 return InvalidParameter;
1332 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
1333 bits, bitmap);
1337 /* FIXME: no icm */
1338 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
1339 GpBitmap **bitmap)
1341 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1343 return GdipCreateBitmapFromFile(filename, bitmap);
1346 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
1347 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
1349 HBITMAP hbm;
1350 GpStatus stat = InvalidParameter;
1352 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
1354 if(!lpBitmapName || !bitmap)
1355 return InvalidParameter;
1357 /* load DIB */
1358 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
1359 LR_CREATEDIBSECTION);
1361 if(hbm){
1362 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1363 DeleteObject(hbm);
1366 return stat;
1369 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
1370 HBITMAP* hbmReturn, ARGB background)
1372 GpStatus stat;
1373 HBITMAP result, oldbitmap;
1374 UINT width, height;
1375 HDC hdc;
1376 GpGraphics *graphics;
1377 BITMAPINFOHEADER bih;
1378 void *bits;
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 hdc = CreateCompatibleDC(NULL);
1399 if (!hdc) return GenericError;
1401 result = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS, &bits,
1402 NULL, 0);
1404 if (result)
1406 oldbitmap = SelectObject(hdc, result);
1408 stat = GdipCreateFromHDC(hdc, &graphics);
1409 if (stat == Ok)
1411 stat = GdipGraphicsClear(graphics, background);
1413 if (stat == Ok)
1414 stat = GdipDrawImage(graphics, (GpImage*)bitmap, 0, 0);
1416 GdipDeleteGraphics(graphics);
1419 SelectObject(hdc, oldbitmap);
1421 else
1422 stat = GenericError;
1424 DeleteDC(hdc);
1426 if (stat != Ok && result)
1428 DeleteObject(result);
1429 result = NULL;
1432 *hbmReturn = result;
1434 return stat;
1437 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
1438 GpMetafile* metafile, BOOL* succ, EmfType emfType,
1439 const WCHAR* description, GpMetafile** out_metafile)
1441 static int calls;
1443 TRACE("(%p,%p,%p,%u,%s,%p)\n", ref, metafile, succ, emfType,
1444 debugstr_w(description), out_metafile);
1446 if(!ref || !metafile || !out_metafile)
1447 return InvalidParameter;
1449 *succ = FALSE;
1450 *out_metafile = NULL;
1452 if(!(calls++))
1453 FIXME("not implemented\n");
1455 return NotImplemented;
1458 /* FIXME: this should create a bitmap in the given size with the attributes
1459 * (resolution etc.) of the graphics object */
1460 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
1461 GpGraphics* target, GpBitmap** bitmap)
1463 static int calls;
1464 GpStatus ret;
1466 TRACE("(%d, %d, %p, %p)\n", width, height, target, bitmap);
1468 if(!target || !bitmap)
1469 return InvalidParameter;
1471 if(!(calls++))
1472 FIXME("hacked stub\n");
1474 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
1475 NULL, bitmap);
1477 return ret;
1480 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1482 GpStatus stat;
1483 ICONINFO iinfo;
1484 BITMAP bm;
1485 int ret;
1486 UINT width, height;
1487 GpRect rect;
1488 BitmapData lockeddata;
1489 HDC screendc;
1490 BOOL has_alpha;
1491 int x, y;
1492 BYTE *bits;
1493 BITMAPINFOHEADER bih;
1494 DWORD *src;
1495 BYTE *dst_row;
1496 DWORD *dst;
1498 TRACE("%p, %p\n", hicon, bitmap);
1500 if(!bitmap || !GetIconInfo(hicon, &iinfo))
1501 return InvalidParameter;
1503 /* get the size of the icon */
1504 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1505 if (ret == 0) {
1506 DeleteObject(iinfo.hbmColor);
1507 DeleteObject(iinfo.hbmMask);
1508 return GenericError;
1511 width = bm.bmWidth;
1513 if (iinfo.hbmColor)
1514 height = abs(bm.bmHeight);
1515 else /* combined bitmap + mask */
1516 height = abs(bm.bmHeight) / 2;
1518 bits = HeapAlloc(GetProcessHeap(), 0, 4*width*height);
1519 if (!bits) {
1520 DeleteObject(iinfo.hbmColor);
1521 DeleteObject(iinfo.hbmMask);
1522 return OutOfMemory;
1525 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB, NULL, bitmap);
1526 if (stat != Ok) {
1527 DeleteObject(iinfo.hbmColor);
1528 DeleteObject(iinfo.hbmMask);
1529 HeapFree(GetProcessHeap(), 0, bits);
1530 return stat;
1533 rect.X = 0;
1534 rect.Y = 0;
1535 rect.Width = width;
1536 rect.Height = height;
1538 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1539 if (stat != Ok) {
1540 DeleteObject(iinfo.hbmColor);
1541 DeleteObject(iinfo.hbmMask);
1542 HeapFree(GetProcessHeap(), 0, bits);
1543 GdipDisposeImage((GpImage*)*bitmap);
1544 return stat;
1547 bih.biSize = sizeof(bih);
1548 bih.biWidth = width;
1549 bih.biHeight = -height;
1550 bih.biPlanes = 1;
1551 bih.biBitCount = 32;
1552 bih.biCompression = BI_RGB;
1553 bih.biSizeImage = 0;
1554 bih.biXPelsPerMeter = 0;
1555 bih.biYPelsPerMeter = 0;
1556 bih.biClrUsed = 0;
1557 bih.biClrImportant = 0;
1559 screendc = GetDC(0);
1560 if (iinfo.hbmColor)
1562 GetDIBits(screendc, iinfo.hbmColor, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1564 if (bm.bmBitsPixel == 32)
1566 has_alpha = FALSE;
1568 /* If any pixel has a non-zero alpha, ignore hbmMask */
1569 src = (DWORD*)bits;
1570 for (x=0; x<width && !has_alpha; x++)
1571 for (y=0; y<height && !has_alpha; y++)
1572 if ((*src++ & 0xff000000) != 0)
1573 has_alpha = TRUE;
1575 else has_alpha = FALSE;
1577 else
1579 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1580 has_alpha = FALSE;
1583 /* copy the image data to the Bitmap */
1584 src = (DWORD*)bits;
1585 dst_row = lockeddata.Scan0;
1586 for (y=0; y<height; y++)
1588 memcpy(dst_row, src, width*4);
1589 src += width;
1590 dst_row += lockeddata.Stride;
1593 if (!has_alpha)
1595 if (iinfo.hbmMask)
1597 /* read alpha data from the mask */
1598 if (iinfo.hbmColor)
1599 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1600 else
1601 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1603 src = (DWORD*)bits;
1604 dst_row = lockeddata.Scan0;
1605 for (y=0; y<height; y++)
1607 dst = (DWORD*)dst_row;
1608 for (x=0; x<height; x++)
1610 DWORD src_value = *src++;
1611 if (src_value)
1612 *dst++ = 0;
1613 else
1614 *dst++ |= 0xff000000;
1616 dst_row += lockeddata.Stride;
1619 else
1621 /* set constant alpha of 255 */
1622 dst_row = bits;
1623 for (y=0; y<height; y++)
1625 dst = (DWORD*)dst_row;
1626 for (x=0; x<height; x++)
1627 *dst++ |= 0xff000000;
1628 dst_row += lockeddata.Stride;
1633 ReleaseDC(0, screendc);
1635 DeleteObject(iinfo.hbmColor);
1636 DeleteObject(iinfo.hbmMask);
1638 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1640 HeapFree(GetProcessHeap(), 0, bits);
1642 return Ok;
1645 static void generate_halftone_palette(ARGB *entries, UINT count)
1647 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1648 UINT i;
1650 for (i=0; i<8 && i<count; i++)
1652 entries[i] = 0xff000000;
1653 if (i&1) entries[i] |= 0x800000;
1654 if (i&2) entries[i] |= 0x8000;
1655 if (i&4) entries[i] |= 0x80;
1658 if (8 < count)
1659 entries[i] = 0xffc0c0c0;
1661 for (i=9; i<16 && i<count; i++)
1663 entries[i] = 0xff000000;
1664 if (i&1) entries[i] |= 0xff0000;
1665 if (i&2) entries[i] |= 0xff00;
1666 if (i&4) entries[i] |= 0xff;
1669 for (i=16; i<40 && i<count; i++)
1671 entries[i] = 0;
1674 for (i=40; i<256 && i<count; i++)
1676 entries[i] = 0xff000000;
1677 entries[i] |= halftone_values[(i-40)%6];
1678 entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1679 entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1683 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1685 HDC screendc = GetDC(0);
1687 if (!screendc) return GenericError;
1689 *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1690 *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1692 ReleaseDC(0, screendc);
1694 return Ok;
1697 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1698 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1700 BITMAPINFO* pbmi;
1701 HBITMAP hbitmap=NULL;
1702 INT row_size, dib_stride;
1703 HDC hdc;
1704 BYTE *bits=NULL, *own_bits=NULL;
1705 REAL xres, yres;
1706 GpStatus stat;
1708 TRACE("%d %d %d 0x%x %p %p\n", width, height, stride, format, scan0, bitmap);
1710 if (!bitmap) return InvalidParameter;
1712 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1713 *bitmap = NULL;
1714 return InvalidParameter;
1717 if(scan0 && !stride)
1718 return InvalidParameter;
1720 stat = get_screen_resolution(&xres, &yres);
1721 if (stat != Ok) return stat;
1723 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1724 dib_stride = (row_size + 3) & ~3;
1726 if(stride == 0)
1727 stride = dib_stride;
1729 if (format & PixelFormatGDI && !(format & (PixelFormatAlpha|PixelFormatIndexed)) && !scan0)
1731 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1732 if (!pbmi)
1733 return OutOfMemory;
1735 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1736 pbmi->bmiHeader.biWidth = width;
1737 pbmi->bmiHeader.biHeight = -height;
1738 pbmi->bmiHeader.biPlanes = 1;
1739 /* FIXME: use the rest of the data from format */
1740 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(format);
1741 pbmi->bmiHeader.biCompression = BI_RGB;
1742 pbmi->bmiHeader.biSizeImage = 0;
1743 pbmi->bmiHeader.biXPelsPerMeter = 0;
1744 pbmi->bmiHeader.biYPelsPerMeter = 0;
1745 pbmi->bmiHeader.biClrUsed = 0;
1746 pbmi->bmiHeader.biClrImportant = 0;
1748 hdc = CreateCompatibleDC(NULL);
1749 if (!hdc) {
1750 GdipFree(pbmi);
1751 return GenericError;
1754 hbitmap = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1756 DeleteDC(hdc);
1757 GdipFree(pbmi);
1759 if (!hbitmap) return GenericError;
1761 stride = dib_stride;
1763 else
1765 /* Not a GDI format; don't try to make an HBITMAP. */
1766 if (scan0)
1767 bits = scan0;
1768 else
1770 INT size = abs(stride) * height;
1772 own_bits = bits = GdipAlloc(size);
1773 if (!own_bits) return OutOfMemory;
1775 if (stride < 0)
1776 bits += stride * (1 - height);
1780 *bitmap = GdipAlloc(sizeof(GpBitmap));
1781 if(!*bitmap)
1783 DeleteObject(hbitmap);
1784 GdipFree(own_bits);
1785 return OutOfMemory;
1788 (*bitmap)->image.type = ImageTypeBitmap;
1789 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1790 (*bitmap)->image.flags = ImageFlagsNone;
1791 (*bitmap)->image.palette_flags = 0;
1792 (*bitmap)->image.palette_count = 0;
1793 (*bitmap)->image.palette_size = 0;
1794 (*bitmap)->image.palette_entries = NULL;
1795 (*bitmap)->image.xres = xres;
1796 (*bitmap)->image.yres = yres;
1797 (*bitmap)->width = width;
1798 (*bitmap)->height = height;
1799 (*bitmap)->format = format;
1800 (*bitmap)->image.picture = NULL;
1801 (*bitmap)->hbitmap = hbitmap;
1802 (*bitmap)->hdc = NULL;
1803 (*bitmap)->bits = bits;
1804 (*bitmap)->stride = stride;
1805 (*bitmap)->own_bits = own_bits;
1807 /* set format-related flags */
1808 if (format & (PixelFormatAlpha|PixelFormatPAlpha|PixelFormatIndexed))
1809 (*bitmap)->image.flags |= ImageFlagsHasAlpha;
1811 if (format == PixelFormat1bppIndexed ||
1812 format == PixelFormat4bppIndexed ||
1813 format == PixelFormat8bppIndexed)
1815 (*bitmap)->image.palette_size = (*bitmap)->image.palette_count = 1 << PIXELFORMATBPP(format);
1816 (*bitmap)->image.palette_entries = GdipAlloc(sizeof(ARGB) * ((*bitmap)->image.palette_size));
1818 if (!(*bitmap)->image.palette_entries)
1820 GdipDisposeImage(&(*bitmap)->image);
1821 *bitmap = NULL;
1822 return OutOfMemory;
1825 if (format == PixelFormat1bppIndexed)
1827 (*bitmap)->image.palette_flags = PaletteFlagsGrayScale;
1828 (*bitmap)->image.palette_entries[0] = 0xff000000;
1829 (*bitmap)->image.palette_entries[1] = 0xffffffff;
1831 else
1833 if (format == PixelFormat8bppIndexed)
1834 (*bitmap)->image.palette_flags = PaletteFlagsHalftone;
1836 generate_halftone_palette((*bitmap)->image.palette_entries,
1837 (*bitmap)->image.palette_count);
1841 TRACE("<-- %p\n", *bitmap);
1843 return Ok;
1846 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1847 GpBitmap **bitmap)
1849 GpStatus stat;
1851 TRACE("%p %p\n", stream, bitmap);
1853 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1855 if(stat != Ok)
1856 return stat;
1858 if((*bitmap)->image.type != ImageTypeBitmap){
1859 GdipDisposeImage(&(*bitmap)->image);
1860 *bitmap = NULL;
1861 return GenericError; /* FIXME: what error to return? */
1864 return Ok;
1867 /* FIXME: no icm */
1868 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1869 GpBitmap **bitmap)
1871 TRACE("%p %p\n", stream, bitmap);
1873 return GdipCreateBitmapFromStream(stream, bitmap);
1876 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1877 GpCachedBitmap **cachedbmp)
1879 GpStatus stat;
1881 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1883 if(!bitmap || !graphics || !cachedbmp)
1884 return InvalidParameter;
1886 *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
1887 if(!*cachedbmp)
1888 return OutOfMemory;
1890 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1891 if(stat != Ok){
1892 GdipFree(*cachedbmp);
1893 return stat;
1896 return Ok;
1899 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1901 GpStatus stat;
1902 BitmapData lockeddata;
1903 ULONG andstride, xorstride, bitssize;
1904 LPBYTE andbits, xorbits, androw, xorrow, srcrow;
1905 UINT x, y;
1907 TRACE("(%p, %p)\n", bitmap, hicon);
1909 if (!bitmap || !hicon)
1910 return InvalidParameter;
1912 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead,
1913 PixelFormat32bppPARGB, &lockeddata);
1914 if (stat == Ok)
1916 andstride = ((lockeddata.Width+31)/32)*4;
1917 xorstride = lockeddata.Width*4;
1918 bitssize = (andstride + xorstride) * lockeddata.Height;
1920 andbits = GdipAlloc(bitssize);
1922 if (andbits)
1924 xorbits = andbits + andstride * lockeddata.Height;
1926 for (y=0; y<lockeddata.Height; y++)
1928 srcrow = ((LPBYTE)lockeddata.Scan0) + lockeddata.Stride * y;
1930 androw = andbits + andstride * y;
1931 for (x=0; x<lockeddata.Width; x++)
1932 if (srcrow[3+4*x] >= 128)
1933 androw[x/8] |= 1 << (7-x%8);
1935 xorrow = xorbits + xorstride * y;
1936 memcpy(xorrow, srcrow, xorstride);
1939 *hicon = CreateIcon(NULL, lockeddata.Width, lockeddata.Height, 1, 32,
1940 andbits, xorbits);
1942 GdipFree(andbits);
1944 else
1945 stat = OutOfMemory;
1947 GdipBitmapUnlockBits(bitmap, &lockeddata);
1950 return stat;
1953 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
1955 TRACE("%p\n", cachedbmp);
1957 if(!cachedbmp)
1958 return InvalidParameter;
1960 GdipDisposeImage(cachedbmp->image);
1961 GdipFree(cachedbmp);
1963 return Ok;
1966 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
1967 GpCachedBitmap *cachedbmp, INT x, INT y)
1969 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
1971 if(!graphics || !cachedbmp)
1972 return InvalidParameter;
1974 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
1977 GpStatus WINGDIPAPI GdipEmfToWmfBits(HENHMETAFILE hemf, UINT cbData16,
1978 LPBYTE pData16, INT iMapMode, INT eFlags)
1980 FIXME("(%p, %d, %p, %d, %d): stub\n", hemf, cbData16, pData16, iMapMode, eFlags);
1981 return NotImplemented;
1984 /* Internal utility function: Replace the image data of dst with that of src,
1985 * and free src. */
1986 static void move_bitmap(GpBitmap *dst, GpBitmap *src, BOOL clobber_palette)
1988 GdipFree(dst->bitmapbits);
1989 DeleteDC(dst->hdc);
1990 DeleteObject(dst->hbitmap);
1992 if (clobber_palette)
1994 GdipFree(dst->image.palette_entries);
1995 dst->image.palette_flags = src->image.palette_flags;
1996 dst->image.palette_count = src->image.palette_count;
1997 dst->image.palette_entries = src->image.palette_entries;
1999 else
2000 GdipFree(src->image.palette_entries);
2002 dst->image.xres = src->image.xres;
2003 dst->image.yres = src->image.yres;
2004 dst->width = src->width;
2005 dst->height = src->height;
2006 dst->format = src->format;
2007 dst->hbitmap = src->hbitmap;
2008 dst->hdc = src->hdc;
2009 dst->bits = src->bits;
2010 dst->stride = src->stride;
2011 dst->own_bits = src->own_bits;
2013 GdipFree(src);
2016 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
2018 TRACE("%p\n", image);
2020 if(!image)
2021 return InvalidParameter;
2023 if (image->type == ImageTypeBitmap)
2025 GdipFree(((GpBitmap*)image)->bitmapbits);
2026 GdipFree(((GpBitmap*)image)->own_bits);
2027 DeleteDC(((GpBitmap*)image)->hdc);
2028 DeleteObject(((GpBitmap*)image)->hbitmap);
2030 else if (image->type != ImageTypeMetafile)
2032 WARN("invalid image: %p\n", image);
2033 return ObjectBusy;
2035 if (image->picture)
2036 IPicture_Release(image->picture);
2037 GdipFree(image->palette_entries);
2038 image->type = ~0;
2039 GdipFree(image);
2041 return Ok;
2044 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
2046 static int calls;
2048 TRACE("(%p,%p)\n", image, item);
2050 if(!image || !item)
2051 return InvalidParameter;
2053 if (!(calls++))
2054 FIXME("not implemented\n");
2056 return NotImplemented;
2059 GpStatus WINGDIPAPI GdipGetImageItemData(GpImage *image, ImageItemData *item)
2061 static int calls;
2063 TRACE("(%p,%p)\n", image, item);
2065 if (!(calls++))
2066 FIXME("not implemented\n");
2068 return NotImplemented;
2071 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
2072 GpUnit *srcUnit)
2074 TRACE("%p %p %p\n", image, srcRect, srcUnit);
2076 if(!image || !srcRect || !srcUnit)
2077 return InvalidParameter;
2078 if(image->type == ImageTypeMetafile){
2079 *srcRect = ((GpMetafile*)image)->bounds;
2080 *srcUnit = ((GpMetafile*)image)->unit;
2082 else if(image->type == ImageTypeBitmap){
2083 srcRect->X = srcRect->Y = 0.0;
2084 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
2085 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
2086 *srcUnit = UnitPixel;
2088 else{
2089 srcRect->X = srcRect->Y = 0.0;
2090 srcRect->Width = ipicture_pixel_width(image->picture);
2091 srcRect->Height = ipicture_pixel_height(image->picture);
2092 *srcUnit = UnitPixel;
2095 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
2096 srcRect->Width, srcRect->Height, *srcUnit);
2098 return Ok;
2101 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
2102 REAL *height)
2104 TRACE("%p %p %p\n", image, width, height);
2106 if(!image || !height || !width)
2107 return InvalidParameter;
2109 if(image->type == ImageTypeMetafile){
2110 HDC hdc = GetDC(0);
2111 REAL res = (REAL)GetDeviceCaps(hdc, LOGPIXELSX);
2113 ReleaseDC(0, hdc);
2115 *height = convert_unit(res, ((GpMetafile*)image)->unit) *
2116 ((GpMetafile*)image)->bounds.Height;
2118 *width = convert_unit(res, ((GpMetafile*)image)->unit) *
2119 ((GpMetafile*)image)->bounds.Width;
2122 else if(image->type == ImageTypeBitmap){
2123 *height = ((GpBitmap*)image)->height;
2124 *width = ((GpBitmap*)image)->width;
2126 else{
2127 *height = ipicture_pixel_height(image->picture);
2128 *width = ipicture_pixel_width(image->picture);
2131 TRACE("returning (%f, %f)\n", *height, *width);
2132 return Ok;
2135 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
2136 GpGraphics **graphics)
2138 HDC hdc;
2139 GpStatus stat;
2141 TRACE("%p %p\n", image, graphics);
2143 if(!image || !graphics)
2144 return InvalidParameter;
2146 if(image->type != ImageTypeBitmap){
2147 FIXME("not implemented for image type %d\n", image->type);
2148 return NotImplemented;
2151 if (((GpBitmap*)image)->hbitmap)
2153 hdc = ((GpBitmap*)image)->hdc;
2155 if(!hdc){
2156 hdc = CreateCompatibleDC(0);
2157 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
2158 ((GpBitmap*)image)->hdc = hdc;
2161 stat = GdipCreateFromHDC(hdc, graphics);
2163 if (stat == Ok)
2164 (*graphics)->image = image;
2166 else
2167 stat = graphics_from_image(image, graphics);
2169 return stat;
2172 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
2174 TRACE("%p %p\n", image, height);
2176 if(!image || !height)
2177 return InvalidParameter;
2179 if(image->type == ImageTypeMetafile){
2180 HDC hdc = GetDC(0);
2181 REAL res = (REAL)GetDeviceCaps(hdc, LOGPIXELSX);
2183 ReleaseDC(0, hdc);
2185 *height = roundr(convert_unit(res, ((GpMetafile*)image)->unit) *
2186 ((GpMetafile*)image)->bounds.Height);
2188 else if(image->type == ImageTypeBitmap)
2189 *height = ((GpBitmap*)image)->height;
2190 else
2191 *height = ipicture_pixel_height(image->picture);
2193 TRACE("returning %d\n", *height);
2195 return Ok;
2198 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2200 if(!image || !res)
2201 return InvalidParameter;
2203 *res = image->xres;
2205 TRACE("(%p) <-- %0.2f\n", image, *res);
2207 return Ok;
2210 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2212 TRACE("%p %p\n", image, size);
2214 if(!image || !size)
2215 return InvalidParameter;
2217 if (image->palette_count == 0)
2218 *size = sizeof(ColorPalette);
2219 else
2220 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette_count;
2222 TRACE("<-- %u\n", *size);
2224 return Ok;
2227 /* FIXME: test this function for non-bitmap types */
2228 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2230 TRACE("%p %p\n", image, format);
2232 if(!image || !format)
2233 return InvalidParameter;
2235 if(image->type != ImageTypeBitmap)
2236 *format = PixelFormat24bppRGB;
2237 else
2238 *format = ((GpBitmap*) image)->format;
2240 return Ok;
2243 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2245 TRACE("(%p, %p)\n", image, format);
2247 if(!image || !format)
2248 return InvalidParameter;
2250 memcpy(format, &image->format, sizeof(GUID));
2252 return Ok;
2255 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2257 TRACE("%p %p\n", image, type);
2259 if(!image || !type)
2260 return InvalidParameter;
2262 *type = image->type;
2264 return Ok;
2267 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2269 if(!image || !res)
2270 return InvalidParameter;
2272 *res = image->yres;
2274 TRACE("(%p) <-- %0.2f\n", image, *res);
2276 return Ok;
2279 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2281 TRACE("%p %p\n", image, width);
2283 if(!image || !width)
2284 return InvalidParameter;
2286 if(image->type == ImageTypeMetafile){
2287 HDC hdc = GetDC(0);
2288 REAL res = (REAL)GetDeviceCaps(hdc, LOGPIXELSX);
2290 ReleaseDC(0, hdc);
2292 *width = roundr(convert_unit(res, ((GpMetafile*)image)->unit) *
2293 ((GpMetafile*)image)->bounds.Width);
2295 else if(image->type == ImageTypeBitmap)
2296 *width = ((GpBitmap*)image)->width;
2297 else
2298 *width = ipicture_pixel_width(image->picture);
2300 TRACE("returning %d\n", *width);
2302 return Ok;
2305 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
2306 MetafileHeader * header)
2308 static int calls;
2310 TRACE("(%p, %p)\n", metafile, header);
2312 if(!metafile || !header)
2313 return InvalidParameter;
2315 if(!(calls++))
2316 FIXME("not implemented\n");
2318 memset(header, 0, sizeof(MetafileHeader));
2320 return Ok;
2323 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromEmf(HENHMETAFILE hEmf,
2324 MetafileHeader *header)
2326 static int calls;
2328 if(!hEmf || !header)
2329 return InvalidParameter;
2331 if(!(calls++))
2332 FIXME("not implemented\n");
2334 memset(header, 0, sizeof(MetafileHeader));
2336 return Ok;
2339 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromFile(GDIPCONST WCHAR *filename,
2340 MetafileHeader *header)
2342 static int calls;
2344 TRACE("(%s,%p)\n", debugstr_w(filename), header);
2346 if(!filename || !header)
2347 return InvalidParameter;
2349 if(!(calls++))
2350 FIXME("not implemented\n");
2352 memset(header, 0, sizeof(MetafileHeader));
2354 return Ok;
2357 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromStream(IStream *stream,
2358 MetafileHeader *header)
2360 static int calls;
2362 TRACE("(%p,%p)\n", stream, header);
2364 if(!stream || !header)
2365 return InvalidParameter;
2367 if(!(calls++))
2368 FIXME("not implemented\n");
2370 memset(header, 0, sizeof(MetafileHeader));
2372 return Ok;
2375 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2376 UINT num, PropertyItem* items)
2378 static int calls;
2380 TRACE("(%p, %u, %u, %p)\n", image, size, num, items);
2382 if(!(calls++))
2383 FIXME("not implemented\n");
2385 return InvalidParameter;
2388 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
2390 static int calls;
2392 TRACE("(%p, %p)\n", image, num);
2394 if(!(calls++))
2395 FIXME("not implemented\n");
2397 return InvalidParameter;
2400 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
2402 static int calls;
2404 TRACE("(%p, %u, %p)\n", image, num, list);
2406 if(!(calls++))
2407 FIXME("not implemented\n");
2409 return InvalidParameter;
2412 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
2413 PropertyItem* buffer)
2415 static int calls;
2417 TRACE("(%p, %u, %u, %p)\n", image, id, size, buffer);
2419 if(!(calls++))
2420 FIXME("not implemented\n");
2422 return InvalidParameter;
2425 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
2426 UINT* size)
2428 static int calls;
2430 TRACE("%p %x %p\n", image, pid, size);
2432 if(!size || !image)
2433 return InvalidParameter;
2435 if(!(calls++))
2436 FIXME("not implemented\n");
2438 return NotImplemented;
2441 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
2443 static int calls;
2445 TRACE("(%p,%p,%p)\n", image, size, num);
2447 if(!(calls++))
2448 FIXME("not implemented\n");
2450 return InvalidParameter;
2453 struct image_format_dimension
2455 const GUID *format;
2456 const GUID *dimension;
2459 struct image_format_dimension image_format_dimensions[] =
2461 {&ImageFormatGIF, &FrameDimensionTime},
2462 {&ImageFormatIcon, &FrameDimensionResolution},
2463 {NULL}
2466 /* FIXME: Need to handle multi-framed images */
2467 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
2468 GDIPCONST GUID* dimensionID, UINT* count)
2470 static int calls;
2472 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
2474 if(!image || !count)
2475 return InvalidParameter;
2477 if(!(calls++))
2478 FIXME("returning frame count of 1\n");
2480 *count = 1;
2482 return Ok;
2485 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
2486 UINT* count)
2488 TRACE("(%p, %p)\n", image, count);
2490 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2492 if(!image || !count)
2493 return InvalidParameter;
2495 *count = 1;
2497 return Ok;
2500 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
2501 GUID* dimensionIDs, UINT count)
2503 int i;
2504 const GUID *result=NULL;
2506 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
2508 if(!image || !dimensionIDs || count != 1)
2509 return InvalidParameter;
2511 for (i=0; image_format_dimensions[i].format; i++)
2513 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
2515 result = image_format_dimensions[i].dimension;
2516 break;
2520 if (!result)
2521 result = &FrameDimensionPage;
2523 memcpy(dimensionIDs, result, sizeof(GUID));
2525 return Ok;
2528 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
2529 GDIPCONST GUID* dimensionID, UINT frameidx)
2531 static int calls;
2533 TRACE("(%p, %s, %u)\n", image, debugstr_guid(dimensionID), frameidx);
2535 if(!image || !dimensionID)
2536 return InvalidParameter;
2538 if(!(calls++))
2539 FIXME("not implemented\n");
2541 return Ok;
2544 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
2545 GpImage **image)
2547 GpStatus stat;
2548 IStream *stream;
2550 TRACE("(%s) %p\n", debugstr_w(filename), image);
2552 if (!filename || !image)
2553 return InvalidParameter;
2555 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
2557 if (stat != Ok)
2558 return stat;
2560 stat = GdipLoadImageFromStream(stream, image);
2562 IStream_Release(stream);
2564 return stat;
2567 /* FIXME: no icm handling */
2568 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
2570 TRACE("(%s) %p\n", debugstr_w(filename), image);
2572 return GdipLoadImageFromFile(filename, image);
2575 static const WICPixelFormatGUID *wic_pixel_formats[] = {
2576 &GUID_WICPixelFormat16bppBGR555,
2577 &GUID_WICPixelFormat24bppBGR,
2578 &GUID_WICPixelFormat32bppBGR,
2579 &GUID_WICPixelFormat32bppBGRA,
2580 &GUID_WICPixelFormat32bppPBGRA,
2581 NULL
2584 static const PixelFormat wic_gdip_formats[] = {
2585 PixelFormat16bppRGB555,
2586 PixelFormat24bppRGB,
2587 PixelFormat32bppRGB,
2588 PixelFormat32bppARGB,
2589 PixelFormat32bppPARGB,
2592 static GpStatus decode_image_wic(IStream* stream, REFCLSID clsid, GpImage **image)
2594 GpStatus status=Ok;
2595 GpBitmap *bitmap;
2596 HRESULT hr;
2597 IWICBitmapDecoder *decoder;
2598 IWICBitmapFrameDecode *frame;
2599 IWICBitmapSource *source=NULL;
2600 WICPixelFormatGUID wic_format;
2601 PixelFormat gdip_format=0;
2602 int i;
2603 UINT width, height;
2604 BitmapData lockeddata;
2605 WICRect wrc;
2606 HRESULT initresult;
2608 initresult = CoInitialize(NULL);
2610 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
2611 &IID_IWICBitmapDecoder, (void**)&decoder);
2612 if (FAILED(hr)) goto end;
2614 hr = IWICBitmapDecoder_Initialize(decoder, (IStream*)stream, WICDecodeMetadataCacheOnLoad);
2615 if (SUCCEEDED(hr))
2616 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
2618 if (SUCCEEDED(hr)) /* got frame */
2620 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
2622 if (SUCCEEDED(hr))
2624 for (i=0; wic_pixel_formats[i]; i++)
2626 if (IsEqualGUID(&wic_format, wic_pixel_formats[i]))
2628 source = (IWICBitmapSource*)frame;
2629 IWICBitmapSource_AddRef(source);
2630 gdip_format = wic_gdip_formats[i];
2631 break;
2634 if (!source)
2636 /* unknown format; fall back on 32bppARGB */
2637 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
2638 gdip_format = PixelFormat32bppARGB;
2642 if (SUCCEEDED(hr)) /* got source */
2644 hr = IWICBitmapSource_GetSize(source, &width, &height);
2646 if (SUCCEEDED(hr))
2647 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
2648 NULL, &bitmap);
2650 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
2652 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
2653 gdip_format, &lockeddata);
2654 if (status == Ok) /* locked bitmap */
2656 wrc.X = 0;
2657 wrc.Width = width;
2658 wrc.Height = 1;
2659 for (i=0; i<height; i++)
2661 wrc.Y = i;
2662 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
2663 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
2664 if (FAILED(hr)) break;
2667 GdipBitmapUnlockBits(bitmap, &lockeddata);
2670 if (SUCCEEDED(hr) && status == Ok)
2671 *image = (GpImage*)bitmap;
2672 else
2674 *image = NULL;
2675 GdipDisposeImage((GpImage*)bitmap);
2678 if (SUCCEEDED(hr) && status == Ok)
2680 double dpix, dpiy;
2681 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
2682 if (SUCCEEDED(hr))
2684 bitmap->image.xres = dpix;
2685 bitmap->image.yres = dpiy;
2687 hr = S_OK;
2691 IWICBitmapSource_Release(source);
2694 IWICBitmapFrameDecode_Release(frame);
2697 IWICBitmapDecoder_Release(decoder);
2699 end:
2700 if (SUCCEEDED(initresult)) CoUninitialize();
2702 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
2704 if (status == Ok)
2706 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
2707 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI|ImageFlagsColorSpaceRGB;
2710 return status;
2713 static GpStatus decode_image_icon(IStream* stream, REFCLSID clsid, GpImage **image)
2715 return decode_image_wic(stream, &CLSID_WICIcoDecoder, image);
2718 static GpStatus decode_image_bmp(IStream* stream, REFCLSID clsid, GpImage **image)
2720 GpStatus status;
2721 GpBitmap* bitmap;
2723 status = decode_image_wic(stream, &CLSID_WICBmpDecoder, image);
2725 bitmap = (GpBitmap*)*image;
2727 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
2729 /* WIC supports bmp files with alpha, but gdiplus does not */
2730 bitmap->format = PixelFormat32bppRGB;
2733 return status;
2736 static GpStatus decode_image_jpeg(IStream* stream, REFCLSID clsid, GpImage **image)
2738 return decode_image_wic(stream, &CLSID_WICJpegDecoder, image);
2741 static GpStatus decode_image_png(IStream* stream, REFCLSID clsid, GpImage **image)
2743 return decode_image_wic(stream, &CLSID_WICPngDecoder, image);
2746 static GpStatus decode_image_gif(IStream* stream, REFCLSID clsid, GpImage **image)
2748 return decode_image_wic(stream, &CLSID_WICGifDecoder, image);
2751 static GpStatus decode_image_tiff(IStream* stream, REFCLSID clsid, GpImage **image)
2753 return decode_image_wic(stream, &CLSID_WICTiffDecoder, image);
2756 static GpStatus decode_image_olepicture_metafile(IStream* stream, REFCLSID clsid, GpImage **image)
2758 IPicture *pic;
2760 TRACE("%p %p\n", stream, image);
2762 if(!stream || !image)
2763 return InvalidParameter;
2765 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
2766 (LPVOID*) &pic) != S_OK){
2767 TRACE("Could not load picture\n");
2768 return GenericError;
2771 /* FIXME: missing initialization code */
2772 *image = GdipAlloc(sizeof(GpMetafile));
2773 if(!*image) return OutOfMemory;
2774 (*image)->type = ImageTypeMetafile;
2775 (*image)->picture = pic;
2776 (*image)->flags = ImageFlagsNone;
2777 (*image)->palette_flags = 0;
2778 (*image)->palette_count = 0;
2779 (*image)->palette_size = 0;
2780 (*image)->palette_entries = NULL;
2782 TRACE("<-- %p\n", *image);
2784 return Ok;
2787 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
2788 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
2790 typedef GpStatus (*decode_image_func)(IStream *stream, REFCLSID clsid, GpImage** image);
2792 typedef struct image_codec {
2793 ImageCodecInfo info;
2794 encode_image_func encode_func;
2795 decode_image_func decode_func;
2796 } image_codec;
2798 typedef enum {
2799 BMP,
2800 JPEG,
2801 GIF,
2802 TIFF,
2803 EMF,
2804 WMF,
2805 PNG,
2806 ICO,
2807 NUM_CODECS
2808 } ImageFormat;
2810 static const struct image_codec codecs[NUM_CODECS];
2812 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
2814 BYTE signature[8];
2815 const BYTE *pattern, *mask;
2816 LARGE_INTEGER seek;
2817 HRESULT hr;
2818 UINT bytesread;
2819 int i, j, sig;
2821 /* seek to the start of the stream */
2822 seek.QuadPart = 0;
2823 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
2824 if (FAILED(hr)) return hresult_to_status(hr);
2826 /* read the first 8 bytes */
2827 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
2828 hr = IStream_Read(stream, signature, 8, &bytesread);
2829 if (FAILED(hr)) return hresult_to_status(hr);
2830 if (hr == S_FALSE || bytesread == 0) return GenericError;
2832 for (i = 0; i < NUM_CODECS; i++) {
2833 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
2834 bytesread >= codecs[i].info.SigSize)
2836 for (sig=0; sig<codecs[i].info.SigCount; sig++)
2838 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
2839 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
2840 for (j=0; j<codecs[i].info.SigSize; j++)
2841 if ((signature[j] & mask[j]) != pattern[j])
2842 break;
2843 if (j == codecs[i].info.SigSize)
2845 *result = &codecs[i];
2846 return Ok;
2852 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
2853 signature[0],signature[1],signature[2],signature[3],
2854 signature[4],signature[5],signature[6],signature[7]);
2856 return GenericError;
2859 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
2861 GpStatus stat;
2862 LARGE_INTEGER seek;
2863 HRESULT hr;
2864 const struct image_codec *codec=NULL;
2866 /* choose an appropriate image decoder */
2867 stat = get_decoder_info(stream, &codec);
2868 if (stat != Ok) return stat;
2870 /* seek to the start of the stream */
2871 seek.QuadPart = 0;
2872 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
2873 if (FAILED(hr)) return hresult_to_status(hr);
2875 /* call on the image decoder to do the real work */
2876 stat = codec->decode_func(stream, &codec->info.Clsid, image);
2878 /* take note of the original data format */
2879 if (stat == Ok)
2881 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
2884 return stat;
2887 /* FIXME: no ICM */
2888 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
2890 TRACE("%p %p\n", stream, image);
2892 return GdipLoadImageFromStream(stream, image);
2895 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
2897 static int calls;
2899 TRACE("(%p,%u)\n", image, propId);
2901 if(!image)
2902 return InvalidParameter;
2904 if(!(calls++))
2905 FIXME("not implemented\n");
2907 return NotImplemented;
2910 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
2912 static int calls;
2914 TRACE("(%p,%p)\n", image, item);
2916 if(!(calls++))
2917 FIXME("not implemented\n");
2919 return NotImplemented;
2922 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
2923 GDIPCONST CLSID *clsidEncoder,
2924 GDIPCONST EncoderParameters *encoderParams)
2926 GpStatus stat;
2927 IStream *stream;
2929 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
2931 if (!image || !filename|| !clsidEncoder)
2932 return InvalidParameter;
2934 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
2935 if (stat != Ok)
2936 return GenericError;
2938 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
2940 IStream_Release(stream);
2941 return stat;
2944 /*************************************************************************
2945 * Encoding functions -
2946 * These functions encode an image in different image file formats.
2948 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
2949 #define BITMAP_FORMAT_JPEG 0xd8ff
2950 #define BITMAP_FORMAT_GIF 0x4947
2951 #define BITMAP_FORMAT_PNG 0x5089
2952 #define BITMAP_FORMAT_APM 0xcdd7
2954 static GpStatus encode_image_WIC(GpImage *image, IStream* stream,
2955 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2957 GpStatus stat;
2958 GpBitmap *bitmap;
2959 IWICBitmapEncoder *encoder;
2960 IWICBitmapFrameEncode *frameencode;
2961 IPropertyBag2 *encoderoptions;
2962 HRESULT hr;
2963 UINT width, height;
2964 PixelFormat gdipformat=0;
2965 WICPixelFormatGUID wicformat;
2966 GpRect rc;
2967 BitmapData lockeddata;
2968 HRESULT initresult;
2969 UINT i;
2971 if (image->type != ImageTypeBitmap)
2972 return GenericError;
2974 bitmap = (GpBitmap*)image;
2976 GdipGetImageWidth(image, &width);
2977 GdipGetImageHeight(image, &height);
2979 rc.X = 0;
2980 rc.Y = 0;
2981 rc.Width = width;
2982 rc.Height = height;
2984 initresult = CoInitialize(NULL);
2986 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
2987 &IID_IWICBitmapEncoder, (void**)&encoder);
2988 if (FAILED(hr))
2990 if (SUCCEEDED(initresult)) CoUninitialize();
2991 return hresult_to_status(hr);
2994 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
2996 if (SUCCEEDED(hr))
2998 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
3001 if (SUCCEEDED(hr)) /* created frame */
3003 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
3005 if (SUCCEEDED(hr))
3006 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
3008 if (SUCCEEDED(hr))
3009 /* FIXME: use the resolution from the image */
3010 hr = IWICBitmapFrameEncode_SetResolution(frameencode, 96.0, 96.0);
3012 if (SUCCEEDED(hr))
3014 for (i=0; wic_pixel_formats[i]; i++)
3016 if (wic_gdip_formats[i] == bitmap->format)
3017 break;
3019 if (wic_pixel_formats[i])
3020 memcpy(&wicformat, wic_pixel_formats[i], sizeof(GUID));
3021 else
3022 memcpy(&wicformat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
3024 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
3026 for (i=0; wic_pixel_formats[i]; i++)
3028 if (IsEqualGUID(&wicformat, wic_pixel_formats[i]))
3029 break;
3031 if (wic_pixel_formats[i])
3032 gdipformat = wic_gdip_formats[i];
3033 else
3035 ERR("cannot provide pixel format %s\n", debugstr_guid(&wicformat));
3036 hr = E_FAIL;
3040 if (SUCCEEDED(hr))
3042 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
3043 &lockeddata);
3045 if (stat == Ok)
3047 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
3048 BYTE *row;
3050 /* write one row at a time in case stride is negative */
3051 row = lockeddata.Scan0;
3052 for (i=0; i<lockeddata.Height; i++)
3054 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
3055 if (FAILED(hr)) break;
3056 row += lockeddata.Stride;
3059 GdipBitmapUnlockBits(bitmap, &lockeddata);
3061 else
3062 hr = E_FAIL;
3065 if (SUCCEEDED(hr))
3066 hr = IWICBitmapFrameEncode_Commit(frameencode);
3068 IWICBitmapFrameEncode_Release(frameencode);
3069 IPropertyBag2_Release(encoderoptions);
3072 if (SUCCEEDED(hr))
3073 hr = IWICBitmapEncoder_Commit(encoder);
3075 IWICBitmapEncoder_Release(encoder);
3077 if (SUCCEEDED(initresult)) CoUninitialize();
3079 return hresult_to_status(hr);
3082 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
3083 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3085 return encode_image_WIC(image, stream, &CLSID_WICBmpEncoder, params);
3088 static GpStatus encode_image_tiff(GpImage *image, IStream* stream,
3089 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3091 return encode_image_WIC(image, stream, &CLSID_WICTiffEncoder, params);
3094 static GpStatus encode_image_png(GpImage *image, IStream* stream,
3095 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3097 return encode_image_WIC(image, stream, &CLSID_WICPngEncoder, params);
3100 /*****************************************************************************
3101 * GdipSaveImageToStream [GDIPLUS.@]
3103 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
3104 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3106 GpStatus stat;
3107 encode_image_func encode_image;
3108 int i;
3110 TRACE("%p %p %p %p\n", image, stream, clsid, params);
3112 if(!image || !stream)
3113 return InvalidParameter;
3115 /* select correct encoder */
3116 encode_image = NULL;
3117 for (i = 0; i < NUM_CODECS; i++) {
3118 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
3119 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
3120 encode_image = codecs[i].encode_func;
3122 if (encode_image == NULL)
3123 return UnknownImageFormat;
3125 stat = encode_image(image, stream, clsid, params);
3127 return stat;
3130 /*****************************************************************************
3131 * GdipGetImagePalette [GDIPLUS.@]
3133 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
3135 TRACE("(%p,%p,%i)\n", image, palette, size);
3137 if (!image || !palette)
3138 return InvalidParameter;
3140 if (size < (sizeof(UINT)*2+sizeof(ARGB)*image->palette_count))
3142 TRACE("<-- InsufficientBuffer\n");
3143 return InsufficientBuffer;
3146 palette->Flags = image->palette_flags;
3147 palette->Count = image->palette_count;
3148 memcpy(palette->Entries, image->palette_entries, sizeof(ARGB)*image->palette_count);
3150 return Ok;
3153 /*****************************************************************************
3154 * GdipSetImagePalette [GDIPLUS.@]
3156 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
3157 GDIPCONST ColorPalette *palette)
3159 TRACE("(%p,%p)\n", image, palette);
3161 if(!image || !palette || palette->Count > 256)
3162 return InvalidParameter;
3164 if (palette->Count > image->palette_size)
3166 ARGB *new_palette;
3168 new_palette = GdipAlloc(sizeof(ARGB) * palette->Count);
3169 if (!new_palette) return OutOfMemory;
3171 GdipFree(image->palette_entries);
3172 image->palette_entries = new_palette;
3173 image->palette_size = palette->Count;
3176 image->palette_flags = palette->Flags;
3177 image->palette_count = palette->Count;
3178 memcpy(image->palette_entries, palette->Entries, sizeof(ARGB)*palette->Count);
3180 return Ok;
3183 /*************************************************************************
3184 * Encoders -
3185 * Structures that represent which formats we support for encoding.
3188 /* ImageCodecInfo creation routines taken from libgdiplus */
3189 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
3190 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
3191 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
3192 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
3193 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
3194 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
3196 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
3197 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
3198 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
3199 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
3200 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
3201 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
3203 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
3204 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
3205 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
3206 static const WCHAR gif_format[] = {'G','I','F',0};
3207 static const BYTE gif_sig_pattern[4] = "GIF8";
3208 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3210 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
3211 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
3212 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
3213 static const WCHAR tiff_format[] = {'T','I','F','F',0};
3214 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
3215 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3217 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
3218 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
3219 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
3220 static const WCHAR emf_format[] = {'E','M','F',0};
3221 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
3222 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3224 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
3225 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
3226 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
3227 static const WCHAR wmf_format[] = {'W','M','F',0};
3228 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
3229 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
3231 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
3232 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
3233 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
3234 static const WCHAR png_format[] = {'P','N','G',0};
3235 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
3236 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3238 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
3239 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
3240 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
3241 static const WCHAR ico_format[] = {'I','C','O',0};
3242 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
3243 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3245 static const struct image_codec codecs[NUM_CODECS] = {
3247 { /* BMP */
3248 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3249 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3250 /* CodecName */ bmp_codecname,
3251 /* DllName */ NULL,
3252 /* FormatDescription */ bmp_format,
3253 /* FilenameExtension */ bmp_extension,
3254 /* MimeType */ bmp_mimetype,
3255 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3256 /* Version */ 1,
3257 /* SigCount */ 1,
3258 /* SigSize */ 2,
3259 /* SigPattern */ bmp_sig_pattern,
3260 /* SigMask */ bmp_sig_mask,
3262 encode_image_BMP,
3263 decode_image_bmp
3266 { /* JPEG */
3267 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3268 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3269 /* CodecName */ jpeg_codecname,
3270 /* DllName */ NULL,
3271 /* FormatDescription */ jpeg_format,
3272 /* FilenameExtension */ jpeg_extension,
3273 /* MimeType */ jpeg_mimetype,
3274 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3275 /* Version */ 1,
3276 /* SigCount */ 1,
3277 /* SigSize */ 2,
3278 /* SigPattern */ jpeg_sig_pattern,
3279 /* SigMask */ jpeg_sig_mask,
3281 NULL,
3282 decode_image_jpeg
3285 { /* GIF */
3286 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3287 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3288 /* CodecName */ gif_codecname,
3289 /* DllName */ NULL,
3290 /* FormatDescription */ gif_format,
3291 /* FilenameExtension */ gif_extension,
3292 /* MimeType */ gif_mimetype,
3293 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3294 /* Version */ 1,
3295 /* SigCount */ 1,
3296 /* SigSize */ 4,
3297 /* SigPattern */ gif_sig_pattern,
3298 /* SigMask */ gif_sig_mask,
3300 NULL,
3301 decode_image_gif
3304 { /* TIFF */
3305 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3306 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3307 /* CodecName */ tiff_codecname,
3308 /* DllName */ NULL,
3309 /* FormatDescription */ tiff_format,
3310 /* FilenameExtension */ tiff_extension,
3311 /* MimeType */ tiff_mimetype,
3312 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsEncoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3313 /* Version */ 1,
3314 /* SigCount */ 2,
3315 /* SigSize */ 4,
3316 /* SigPattern */ tiff_sig_pattern,
3317 /* SigMask */ tiff_sig_mask,
3319 encode_image_tiff,
3320 decode_image_tiff
3323 { /* EMF */
3324 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3325 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3326 /* CodecName */ emf_codecname,
3327 /* DllName */ NULL,
3328 /* FormatDescription */ emf_format,
3329 /* FilenameExtension */ emf_extension,
3330 /* MimeType */ emf_mimetype,
3331 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
3332 /* Version */ 1,
3333 /* SigCount */ 1,
3334 /* SigSize */ 4,
3335 /* SigPattern */ emf_sig_pattern,
3336 /* SigMask */ emf_sig_mask,
3338 NULL,
3339 decode_image_olepicture_metafile
3342 { /* WMF */
3343 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3344 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3345 /* CodecName */ wmf_codecname,
3346 /* DllName */ NULL,
3347 /* FormatDescription */ wmf_format,
3348 /* FilenameExtension */ wmf_extension,
3349 /* MimeType */ wmf_mimetype,
3350 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
3351 /* Version */ 1,
3352 /* SigCount */ 1,
3353 /* SigSize */ 2,
3354 /* SigPattern */ wmf_sig_pattern,
3355 /* SigMask */ wmf_sig_mask,
3357 NULL,
3358 decode_image_olepicture_metafile
3361 { /* PNG */
3362 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3363 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3364 /* CodecName */ png_codecname,
3365 /* DllName */ NULL,
3366 /* FormatDescription */ png_format,
3367 /* FilenameExtension */ png_extension,
3368 /* MimeType */ png_mimetype,
3369 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3370 /* Version */ 1,
3371 /* SigCount */ 1,
3372 /* SigSize */ 8,
3373 /* SigPattern */ png_sig_pattern,
3374 /* SigMask */ png_sig_mask,
3376 encode_image_png,
3377 decode_image_png
3380 { /* ICO */
3381 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3382 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3383 /* CodecName */ ico_codecname,
3384 /* DllName */ NULL,
3385 /* FormatDescription */ ico_format,
3386 /* FilenameExtension */ ico_extension,
3387 /* MimeType */ ico_mimetype,
3388 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3389 /* Version */ 1,
3390 /* SigCount */ 1,
3391 /* SigSize */ 4,
3392 /* SigPattern */ ico_sig_pattern,
3393 /* SigMask */ ico_sig_mask,
3395 NULL,
3396 decode_image_icon
3400 /*****************************************************************************
3401 * GdipGetImageDecodersSize [GDIPLUS.@]
3403 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
3405 int decoder_count=0;
3406 int i;
3407 TRACE("%p %p\n", numDecoders, size);
3409 if (!numDecoders || !size)
3410 return InvalidParameter;
3412 for (i=0; i<NUM_CODECS; i++)
3414 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
3415 decoder_count++;
3418 *numDecoders = decoder_count;
3419 *size = decoder_count * sizeof(ImageCodecInfo);
3421 return Ok;
3424 /*****************************************************************************
3425 * GdipGetImageDecoders [GDIPLUS.@]
3427 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
3429 int i, decoder_count=0;
3430 TRACE("%u %u %p\n", numDecoders, size, decoders);
3432 if (!decoders ||
3433 size != numDecoders * sizeof(ImageCodecInfo))
3434 return GenericError;
3436 for (i=0; i<NUM_CODECS; i++)
3438 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
3440 if (decoder_count == numDecoders) return GenericError;
3441 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
3442 decoder_count++;
3446 if (decoder_count < numDecoders) return GenericError;
3448 return Ok;
3451 /*****************************************************************************
3452 * GdipGetImageEncodersSize [GDIPLUS.@]
3454 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
3456 int encoder_count=0;
3457 int i;
3458 TRACE("%p %p\n", numEncoders, size);
3460 if (!numEncoders || !size)
3461 return InvalidParameter;
3463 for (i=0; i<NUM_CODECS; i++)
3465 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
3466 encoder_count++;
3469 *numEncoders = encoder_count;
3470 *size = encoder_count * sizeof(ImageCodecInfo);
3472 return Ok;
3475 /*****************************************************************************
3476 * GdipGetImageEncoders [GDIPLUS.@]
3478 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
3480 int i, encoder_count=0;
3481 TRACE("%u %u %p\n", numEncoders, size, encoders);
3483 if (!encoders ||
3484 size != numEncoders * sizeof(ImageCodecInfo))
3485 return GenericError;
3487 for (i=0; i<NUM_CODECS; i++)
3489 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
3491 if (encoder_count == numEncoders) return GenericError;
3492 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
3493 encoder_count++;
3497 if (encoder_count < numEncoders) return GenericError;
3499 return Ok;
3502 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
3503 GDIPCONST CLSID* clsidEncoder, UINT *size)
3505 static int calls;
3507 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
3509 if(!(calls++))
3510 FIXME("not implemented\n");
3512 *size = 0;
3514 return NotImplemented;
3517 /*****************************************************************************
3518 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
3520 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
3522 BITMAP bm;
3523 GpStatus retval;
3524 PixelFormat format;
3525 BitmapData lockeddata;
3526 INT y;
3528 TRACE("%p %p %p\n", hbm, hpal, bitmap);
3530 if(!hbm || !bitmap)
3531 return InvalidParameter;
3533 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
3534 return InvalidParameter;
3536 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
3537 switch(bm.bmBitsPixel) {
3538 case 1:
3539 format = PixelFormat1bppIndexed;
3540 break;
3541 case 4:
3542 format = PixelFormat4bppIndexed;
3543 break;
3544 case 8:
3545 format = PixelFormat8bppIndexed;
3546 break;
3547 case 24:
3548 format = PixelFormat24bppRGB;
3549 break;
3550 case 32:
3551 format = PixelFormat32bppRGB;
3552 break;
3553 case 48:
3554 format = PixelFormat48bppRGB;
3555 break;
3556 default:
3557 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
3558 return InvalidParameter;
3561 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
3562 format, NULL, bitmap);
3564 if (retval == Ok)
3566 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
3567 format, &lockeddata);
3568 if (retval == Ok)
3570 if (bm.bmBits)
3572 for (y=0; y<bm.bmHeight; y++)
3574 memcpy((BYTE*)lockeddata.Scan0+lockeddata.Stride*y,
3575 (BYTE*)bm.bmBits+bm.bmWidthBytes*(bm.bmHeight-1-y),
3576 bm.bmWidthBytes);
3579 else
3581 HDC hdc;
3582 HBITMAP oldhbm;
3583 BITMAPINFO *pbmi;
3584 INT src_height, dst_stride;
3585 BYTE *dst_bits;
3587 hdc = CreateCompatibleDC(NULL);
3588 oldhbm = SelectObject(hdc, hbm);
3590 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
3592 if (pbmi)
3594 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3595 pbmi->bmiHeader.biBitCount = 0;
3597 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
3599 src_height = abs(pbmi->bmiHeader.biHeight);
3601 if (pbmi->bmiHeader.biHeight > 0)
3603 dst_bits = (BYTE*)lockeddata.Scan0+lockeddata.Stride*(src_height-1);
3604 dst_stride = -lockeddata.Stride;
3606 else
3608 dst_bits = lockeddata.Scan0;
3609 dst_stride = lockeddata.Stride;
3612 for (y=0; y<src_height; y++)
3614 GetDIBits(hdc, hbm, y, 1, dst_bits+dst_stride*y,
3615 pbmi, DIB_RGB_COLORS);
3618 GdipFree(pbmi);
3620 else
3621 retval = OutOfMemory;
3623 SelectObject(hdc, oldhbm);
3624 DeleteDC(hdc);
3627 GdipBitmapUnlockBits(*bitmap, &lockeddata);
3630 if (retval == Ok && hpal)
3632 WORD num_palette_entries;
3633 PALETTEENTRY *palette_entries=NULL;
3634 ColorPalette *palette=NULL;
3635 int i;
3637 if (!GetObjectW(hpal, sizeof(num_palette_entries), &num_palette_entries))
3638 retval = GenericError;
3640 if (retval == Ok)
3642 palette_entries = GdipAlloc(sizeof(PALETTEENTRY) * num_palette_entries);
3643 palette = GdipAlloc(sizeof(ColorPalette) + sizeof(ARGB) * (num_palette_entries-1));
3645 if (!palette_entries || !palette)
3646 retval = OutOfMemory;
3649 if (retval == Ok)
3651 if (!GetPaletteEntries(hpal, 0, num_palette_entries, palette_entries))
3652 retval = GenericError;
3655 if (retval == Ok)
3657 palette->Flags = 0;
3658 palette->Count = num_palette_entries;
3660 for (i=0; i<num_palette_entries; i++)
3662 PALETTEENTRY * entry = &palette_entries[i];
3663 palette->Entries[i] = 0xff000000 | entry->peRed << 16 |
3664 entry->peGreen << 8 | entry->peBlue;
3667 retval = GdipSetImagePalette((GpImage*)*bitmap, palette);
3670 GdipFree(palette_entries);
3671 GdipFree(palette);
3674 if (retval != Ok)
3676 GdipDisposeImage((GpImage*)*bitmap);
3677 *bitmap = NULL;
3681 return retval;
3684 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
3686 FIXME("(%p): stub\n", effect);
3687 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
3688 * in Windows's gdiplus */
3689 return NotImplemented;
3692 /*****************************************************************************
3693 * GdipSetEffectParameters [GDIPLUS.@]
3695 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
3696 const VOID *params, const UINT size)
3698 static int calls;
3700 TRACE("(%p,%p,%u)\n", effect, params, size);
3702 if(!(calls++))
3703 FIXME("not implemented\n");
3705 return NotImplemented;
3708 /*****************************************************************************
3709 * GdipGetImageFlags [GDIPLUS.@]
3711 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
3713 TRACE("%p %p\n", image, flags);
3715 if(!image || !flags)
3716 return InvalidParameter;
3718 *flags = image->flags;
3720 return Ok;
3723 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
3725 TRACE("(%d, %p)\n", control, param);
3727 switch(control){
3728 case TestControlForceBilinear:
3729 if(param)
3730 FIXME("TestControlForceBilinear not handled\n");
3731 break;
3732 case TestControlNoICM:
3733 if(param)
3734 FIXME("TestControlNoICM not handled\n");
3735 break;
3736 case TestControlGetBuildNumber:
3737 *((DWORD*)param) = 3102;
3738 break;
3741 return Ok;
3744 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
3745 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
3746 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
3747 GpMetafile **metafile)
3749 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
3750 frameUnit, debugstr_w(desc), metafile);
3752 return NotImplemented;
3755 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
3756 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
3757 GDIPCONST WCHAR *desc, GpMetafile **metafile)
3759 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
3760 frameUnit, debugstr_w(desc), metafile);
3762 return NotImplemented;
3765 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
3767 TRACE("%p\n", image);
3769 return Ok;
3772 /*****************************************************************************
3773 * GdipGetImageThumbnail [GDIPLUS.@]
3775 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
3776 GpImage **ret_image, GetThumbnailImageAbort cb,
3777 VOID * cb_data)
3779 GpStatus stat;
3780 GpGraphics *graphics;
3781 UINT srcwidth, srcheight;
3783 TRACE("(%p %u %u %p %p %p)\n",
3784 image, width, height, ret_image, cb, cb_data);
3786 if (!image || !ret_image)
3787 return InvalidParameter;
3789 if (!width) width = 120;
3790 if (!height) height = 120;
3792 GdipGetImageWidth(image, &srcwidth);
3793 GdipGetImageHeight(image, &srcheight);
3795 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB,
3796 NULL, (GpBitmap**)ret_image);
3798 if (stat == Ok)
3800 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
3802 if (stat == Ok)
3804 stat = GdipDrawImageRectRectI(graphics, image,
3805 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
3806 NULL, NULL, NULL);
3808 GdipDeleteGraphics(graphics);
3811 if (stat != Ok)
3813 GdipDisposeImage(*ret_image);
3814 *ret_image = NULL;
3818 return stat;
3821 /*****************************************************************************
3822 * GdipImageRotateFlip [GDIPLUS.@]
3824 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
3826 GpBitmap *new_bitmap;
3827 GpBitmap *bitmap;
3828 int bpp, bytesperpixel;
3829 int rotate_90, flip_x, flip_y;
3830 int src_x_offset, src_y_offset;
3831 LPBYTE src_origin;
3832 UINT x, y, width, height;
3833 BitmapData src_lock, dst_lock;
3834 GpStatus stat;
3836 TRACE("(%p, %u)\n", image, type);
3838 rotate_90 = type&1;
3839 flip_x = (type&6) == 2 || (type&6) == 4;
3840 flip_y = (type&3) == 1 || (type&3) == 2;
3842 if (image->type != ImageTypeBitmap)
3844 FIXME("Not implemented for type %i\n", image->type);
3845 return NotImplemented;
3848 bitmap = (GpBitmap*)image;
3849 bpp = PIXELFORMATBPP(bitmap->format);
3851 if (bpp < 8)
3853 FIXME("Not implemented for %i bit images\n", bpp);
3854 return NotImplemented;
3857 if (rotate_90)
3859 width = bitmap->height;
3860 height = bitmap->width;
3862 else
3864 width = bitmap->width;
3865 height = bitmap->height;
3868 bytesperpixel = bpp/8;
3870 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
3872 if (stat != Ok)
3873 return stat;
3875 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
3877 if (stat == Ok)
3879 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
3881 if (stat == Ok)
3883 LPBYTE src_row, src_pixel;
3884 LPBYTE dst_row, dst_pixel;
3886 src_origin = src_lock.Scan0;
3887 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
3888 if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
3890 if (rotate_90)
3892 if (flip_y) src_x_offset = -src_lock.Stride;
3893 else src_x_offset = src_lock.Stride;
3894 if (flip_x) src_y_offset = -bytesperpixel;
3895 else src_y_offset = bytesperpixel;
3897 else
3899 if (flip_x) src_x_offset = -bytesperpixel;
3900 else src_x_offset = bytesperpixel;
3901 if (flip_y) src_y_offset = -src_lock.Stride;
3902 else src_y_offset = src_lock.Stride;
3905 src_row = src_origin;
3906 dst_row = dst_lock.Scan0;
3907 for (y=0; y<height; y++)
3909 src_pixel = src_row;
3910 dst_pixel = dst_row;
3911 for (x=0; x<width; x++)
3913 /* FIXME: This could probably be faster without memcpy. */
3914 memcpy(dst_pixel, src_pixel, bytesperpixel);
3915 dst_pixel += bytesperpixel;
3916 src_pixel += src_x_offset;
3918 src_row += src_y_offset;
3919 dst_row += dst_lock.Stride;
3922 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
3925 GdipBitmapUnlockBits(bitmap, &src_lock);
3928 if (stat == Ok)
3929 move_bitmap(bitmap, new_bitmap, FALSE);
3930 else
3931 GdipDisposeImage((GpImage*)new_bitmap);
3933 return stat;
3936 /*****************************************************************************
3937 * GdipConvertToEmfPlusToFile [GDIPLUS.@]
3940 GpStatus WINGDIPAPI GdipConvertToEmfPlusToFile(const GpGraphics* refGraphics,
3941 GpMetafile* metafile, BOOL* conversionSuccess,
3942 const WCHAR* filename, EmfType emfType,
3943 const WCHAR* description, GpMetafile** out_metafile)
3945 FIXME("stub: %p, %p, %p, %p, %u, %p, %p\n", refGraphics, metafile, conversionSuccess, filename, emfType, description, out_metafile);
3946 return NotImplemented;