gdiplus: Don't try to free invalid or already-freed images.
[wine/multimedia.git] / dlls / gdiplus / image.c
blob9b83d95064fbf9941532e6d2623c3504a5d4ef1a
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 void setpixel_16bppGrayScale(BYTE r, BYTE g, BYTE b, BYTE a,
300 BYTE *row, UINT x)
302 *((WORD*)(row)+x) = (r+g+b)*85;
305 static inline void setpixel_16bppRGB555(BYTE r, BYTE g, BYTE b, BYTE a,
306 BYTE *row, UINT x)
308 *((WORD*)(row)+x) = (r<<7&0x7c00)|
309 (g<<2&0x03e0)|
310 (b>>3&0x001f);
313 static inline void setpixel_16bppRGB565(BYTE r, BYTE g, BYTE b, BYTE a,
314 BYTE *row, UINT x)
316 *((WORD*)(row)+x) = (r<<8&0xf800)|
317 (g<<3&0x07e0)|
318 (b>>3&0x001f);
321 static inline void setpixel_16bppARGB1555(BYTE r, BYTE g, BYTE b, BYTE a,
322 BYTE *row, UINT x)
324 *((WORD*)(row)+x) = (a<<8&0x8000)|
325 (r<<7&0x7c00)|
326 (g<<2&0x03e0)|
327 (b>>3&0x001f);
330 static inline void setpixel_24bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
331 BYTE *row, UINT x)
333 row[x*3+2] = r;
334 row[x*3+1] = g;
335 row[x*3] = b;
338 static inline void setpixel_32bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
339 BYTE *row, UINT x)
341 *((DWORD*)(row)+x) = (r<<16)|(g<<8)|b;
344 static inline void setpixel_32bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
345 BYTE *row, UINT x)
347 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
350 static inline void setpixel_32bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
351 BYTE *row, UINT x)
353 r = r * a / 255;
354 g = g * a / 255;
355 b = b * a / 255;
356 *((DWORD*)(row)+x) = (a<<24)|(r<<16)|(g<<8)|b;
359 static inline void setpixel_48bppRGB(BYTE r, BYTE g, BYTE b, BYTE a,
360 BYTE *row, UINT x)
362 row[x*6+5] = row[x*6+4] = r;
363 row[x*6+3] = row[x*6+2] = g;
364 row[x*6+1] = row[x*6] = b;
367 static inline void setpixel_64bppARGB(BYTE r, BYTE g, BYTE b, BYTE a,
368 BYTE *row, UINT x)
370 UINT64 a64=a, r64=r, g64=g, b64=b;
371 *((UINT64*)(row)+x) = (a64<<56)|(a64<<48)|(r64<<40)|(r64<<32)|(g64<<24)|(g64<<16)|(b64<<8)|b64;
374 static inline void setpixel_64bppPARGB(BYTE r, BYTE g, BYTE b, BYTE a,
375 BYTE *row, UINT x)
377 UINT64 a64, r64, g64, b64;
378 a64 = a * 257;
379 r64 = r * a / 255;
380 g64 = g * a / 255;
381 b64 = b * a / 255;
382 *((UINT64*)(row)+x) = (a64<<48)|(r64<<32)|(g64<<16)|b64;
385 GpStatus WINGDIPAPI GdipBitmapSetPixel(GpBitmap* bitmap, INT x, INT y,
386 ARGB color)
388 BYTE a, r, g, b;
389 BYTE *row;
390 TRACE("bitmap:%p, x:%d, y:%d, color:%08x\n", bitmap, x, y, color);
392 if(!bitmap || x < 0 || y < 0 || x >= bitmap->width || y >= bitmap->height)
393 return InvalidParameter;
395 a = color>>24;
396 r = color>>16;
397 g = color>>8;
398 b = color;
400 row = bitmap->bits + bitmap->stride * y;
402 switch (bitmap->format)
404 case PixelFormat16bppGrayScale:
405 setpixel_16bppGrayScale(r,g,b,a,row,x);
406 break;
407 case PixelFormat16bppRGB555:
408 setpixel_16bppRGB555(r,g,b,a,row,x);
409 break;
410 case PixelFormat16bppRGB565:
411 setpixel_16bppRGB565(r,g,b,a,row,x);
412 break;
413 case PixelFormat16bppARGB1555:
414 setpixel_16bppARGB1555(r,g,b,a,row,x);
415 break;
416 case PixelFormat24bppRGB:
417 setpixel_24bppRGB(r,g,b,a,row,x);
418 break;
419 case PixelFormat32bppRGB:
420 setpixel_32bppRGB(r,g,b,a,row,x);
421 break;
422 case PixelFormat32bppARGB:
423 setpixel_32bppARGB(r,g,b,a,row,x);
424 break;
425 case PixelFormat32bppPARGB:
426 setpixel_32bppPARGB(r,g,b,a,row,x);
427 break;
428 case PixelFormat48bppRGB:
429 setpixel_48bppRGB(r,g,b,a,row,x);
430 break;
431 case PixelFormat64bppARGB:
432 setpixel_64bppARGB(r,g,b,a,row,x);
433 break;
434 case PixelFormat64bppPARGB:
435 setpixel_64bppPARGB(r,g,b,a,row,x);
436 break;
437 default:
438 FIXME("not implemented for format 0x%x\n", bitmap->format);
439 return NotImplemented;
442 return Ok;
445 GpStatus convert_pixels(UINT width, UINT height,
446 INT dst_stride, BYTE *dst_bits, PixelFormat dst_format,
447 INT src_stride, const BYTE *src_bits, PixelFormat src_format, ARGB *src_palette)
449 UINT x, y;
451 if (src_format == dst_format ||
452 (dst_format == PixelFormat32bppRGB && PIXELFORMATBPP(src_format) == 32))
454 UINT widthbytes = PIXELFORMATBPP(src_format) * width / 8;
455 for (y=0; y<height; y++)
456 memcpy(dst_bits+dst_stride*y, src_bits+src_stride*y, widthbytes);
457 return Ok;
460 #define convert_indexed_to_rgb(getpixel_function, setpixel_function) do { \
461 for (x=0; x<width; x++) \
462 for (y=0; y<height; y++) { \
463 BYTE index; \
464 BYTE *color; \
465 getpixel_function(&index, src_bits+src_stride*y, x); \
466 color = (BYTE*)(&src_palette[index]); \
467 setpixel_function(color[2], color[1], color[0], color[3], dst_bits+dst_stride*y, x); \
469 return Ok; \
470 } while (0);
472 #define convert_rgb_to_rgb(getpixel_function, setpixel_function) do { \
473 for (x=0; x<width; x++) \
474 for (y=0; y<height; y++) { \
475 BYTE r, g, b, a; \
476 getpixel_function(&r, &g, &b, &a, src_bits+src_stride*y, x); \
477 setpixel_function(r, g, b, a, dst_bits+dst_stride*y, x); \
479 return Ok; \
480 } while (0);
482 switch (src_format)
484 case PixelFormat1bppIndexed:
485 switch (dst_format)
487 case PixelFormat16bppGrayScale:
488 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppGrayScale);
489 case PixelFormat16bppRGB555:
490 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB555);
491 case PixelFormat16bppRGB565:
492 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppRGB565);
493 case PixelFormat16bppARGB1555:
494 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_16bppARGB1555);
495 case PixelFormat24bppRGB:
496 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_24bppRGB);
497 case PixelFormat32bppRGB:
498 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppRGB);
499 case PixelFormat32bppARGB:
500 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppARGB);
501 case PixelFormat32bppPARGB:
502 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_32bppPARGB);
503 case PixelFormat48bppRGB:
504 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_48bppRGB);
505 case PixelFormat64bppARGB:
506 convert_indexed_to_rgb(getpixel_1bppIndexed, setpixel_64bppARGB);
507 default:
508 break;
510 break;
511 case PixelFormat4bppIndexed:
512 switch (dst_format)
514 case PixelFormat16bppGrayScale:
515 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppGrayScale);
516 case PixelFormat16bppRGB555:
517 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB555);
518 case PixelFormat16bppRGB565:
519 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppRGB565);
520 case PixelFormat16bppARGB1555:
521 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_16bppARGB1555);
522 case PixelFormat24bppRGB:
523 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_24bppRGB);
524 case PixelFormat32bppRGB:
525 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppRGB);
526 case PixelFormat32bppARGB:
527 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppARGB);
528 case PixelFormat32bppPARGB:
529 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_32bppPARGB);
530 case PixelFormat48bppRGB:
531 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_48bppRGB);
532 case PixelFormat64bppARGB:
533 convert_indexed_to_rgb(getpixel_4bppIndexed, setpixel_64bppARGB);
534 default:
535 break;
537 break;
538 case PixelFormat8bppIndexed:
539 switch (dst_format)
541 case PixelFormat16bppGrayScale:
542 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppGrayScale);
543 case PixelFormat16bppRGB555:
544 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB555);
545 case PixelFormat16bppRGB565:
546 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppRGB565);
547 case PixelFormat16bppARGB1555:
548 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_16bppARGB1555);
549 case PixelFormat24bppRGB:
550 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_24bppRGB);
551 case PixelFormat32bppRGB:
552 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppRGB);
553 case PixelFormat32bppARGB:
554 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppARGB);
555 case PixelFormat32bppPARGB:
556 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_32bppPARGB);
557 case PixelFormat48bppRGB:
558 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_48bppRGB);
559 case PixelFormat64bppARGB:
560 convert_indexed_to_rgb(getpixel_8bppIndexed, setpixel_64bppARGB);
561 default:
562 break;
564 break;
565 case PixelFormat16bppGrayScale:
566 switch (dst_format)
568 case PixelFormat16bppRGB555:
569 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB555);
570 case PixelFormat16bppRGB565:
571 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppRGB565);
572 case PixelFormat16bppARGB1555:
573 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_16bppARGB1555);
574 case PixelFormat24bppRGB:
575 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_24bppRGB);
576 case PixelFormat32bppRGB:
577 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppRGB);
578 case PixelFormat32bppARGB:
579 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppARGB);
580 case PixelFormat32bppPARGB:
581 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_32bppPARGB);
582 case PixelFormat48bppRGB:
583 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_48bppRGB);
584 case PixelFormat64bppARGB:
585 convert_rgb_to_rgb(getpixel_16bppGrayScale, setpixel_64bppARGB);
586 default:
587 break;
589 break;
590 case PixelFormat16bppRGB555:
591 switch (dst_format)
593 case PixelFormat16bppGrayScale:
594 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppGrayScale);
595 case PixelFormat16bppRGB565:
596 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppRGB565);
597 case PixelFormat16bppARGB1555:
598 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_16bppARGB1555);
599 case PixelFormat24bppRGB:
600 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_24bppRGB);
601 case PixelFormat32bppRGB:
602 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppRGB);
603 case PixelFormat32bppARGB:
604 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppARGB);
605 case PixelFormat32bppPARGB:
606 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_32bppPARGB);
607 case PixelFormat48bppRGB:
608 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_48bppRGB);
609 case PixelFormat64bppARGB:
610 convert_rgb_to_rgb(getpixel_16bppRGB555, setpixel_64bppARGB);
611 default:
612 break;
614 break;
615 case PixelFormat16bppRGB565:
616 switch (dst_format)
618 case PixelFormat16bppGrayScale:
619 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppGrayScale);
620 case PixelFormat16bppRGB555:
621 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppRGB555);
622 case PixelFormat16bppARGB1555:
623 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_16bppARGB1555);
624 case PixelFormat24bppRGB:
625 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_24bppRGB);
626 case PixelFormat32bppRGB:
627 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppRGB);
628 case PixelFormat32bppARGB:
629 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppARGB);
630 case PixelFormat32bppPARGB:
631 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_32bppPARGB);
632 case PixelFormat48bppRGB:
633 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_48bppRGB);
634 case PixelFormat64bppARGB:
635 convert_rgb_to_rgb(getpixel_16bppRGB565, setpixel_64bppARGB);
636 default:
637 break;
639 break;
640 case PixelFormat16bppARGB1555:
641 switch (dst_format)
643 case PixelFormat16bppGrayScale:
644 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppGrayScale);
645 case PixelFormat16bppRGB555:
646 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB555);
647 case PixelFormat16bppRGB565:
648 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_16bppRGB565);
649 case PixelFormat24bppRGB:
650 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_24bppRGB);
651 case PixelFormat32bppRGB:
652 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppRGB);
653 case PixelFormat32bppARGB:
654 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppARGB);
655 case PixelFormat32bppPARGB:
656 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_32bppPARGB);
657 case PixelFormat48bppRGB:
658 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_48bppRGB);
659 case PixelFormat64bppARGB:
660 convert_rgb_to_rgb(getpixel_16bppARGB1555, setpixel_64bppARGB);
661 default:
662 break;
664 break;
665 case PixelFormat24bppRGB:
666 switch (dst_format)
668 case PixelFormat16bppGrayScale:
669 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppGrayScale);
670 case PixelFormat16bppRGB555:
671 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB555);
672 case PixelFormat16bppRGB565:
673 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppRGB565);
674 case PixelFormat16bppARGB1555:
675 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_16bppARGB1555);
676 case PixelFormat32bppRGB:
677 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppRGB);
678 case PixelFormat32bppARGB:
679 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppARGB);
680 case PixelFormat32bppPARGB:
681 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_32bppPARGB);
682 case PixelFormat48bppRGB:
683 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_48bppRGB);
684 case PixelFormat64bppARGB:
685 convert_rgb_to_rgb(getpixel_24bppRGB, setpixel_64bppARGB);
686 default:
687 break;
689 break;
690 case PixelFormat32bppRGB:
691 switch (dst_format)
693 case PixelFormat16bppGrayScale:
694 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppGrayScale);
695 case PixelFormat16bppRGB555:
696 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB555);
697 case PixelFormat16bppRGB565:
698 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppRGB565);
699 case PixelFormat16bppARGB1555:
700 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_16bppARGB1555);
701 case PixelFormat24bppRGB:
702 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_24bppRGB);
703 case PixelFormat32bppARGB:
704 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppARGB);
705 case PixelFormat32bppPARGB:
706 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_32bppPARGB);
707 case PixelFormat48bppRGB:
708 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_48bppRGB);
709 case PixelFormat64bppARGB:
710 convert_rgb_to_rgb(getpixel_32bppRGB, setpixel_64bppARGB);
711 default:
712 break;
714 break;
715 case PixelFormat32bppARGB:
716 switch (dst_format)
718 case PixelFormat16bppGrayScale:
719 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppGrayScale);
720 case PixelFormat16bppRGB555:
721 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB555);
722 case PixelFormat16bppRGB565:
723 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppRGB565);
724 case PixelFormat16bppARGB1555:
725 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_16bppARGB1555);
726 case PixelFormat24bppRGB:
727 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_24bppRGB);
728 case PixelFormat32bppPARGB:
729 convert_32bppARGB_to_32bppPARGB(width, height, dst_bits, dst_stride, src_bits, src_stride);
730 return Ok;
731 case PixelFormat48bppRGB:
732 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_48bppRGB);
733 case PixelFormat64bppARGB:
734 convert_rgb_to_rgb(getpixel_32bppARGB, setpixel_64bppARGB);
735 default:
736 break;
738 break;
739 case PixelFormat32bppPARGB:
740 switch (dst_format)
742 case PixelFormat16bppGrayScale:
743 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppGrayScale);
744 case PixelFormat16bppRGB555:
745 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB555);
746 case PixelFormat16bppRGB565:
747 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppRGB565);
748 case PixelFormat16bppARGB1555:
749 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_16bppARGB1555);
750 case PixelFormat24bppRGB:
751 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_24bppRGB);
752 case PixelFormat32bppRGB:
753 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppRGB);
754 case PixelFormat32bppARGB:
755 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_32bppARGB);
756 case PixelFormat48bppRGB:
757 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_48bppRGB);
758 case PixelFormat64bppARGB:
759 convert_rgb_to_rgb(getpixel_32bppPARGB, setpixel_64bppARGB);
760 default:
761 break;
763 break;
764 case PixelFormat48bppRGB:
765 switch (dst_format)
767 case PixelFormat16bppGrayScale:
768 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppGrayScale);
769 case PixelFormat16bppRGB555:
770 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB555);
771 case PixelFormat16bppRGB565:
772 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppRGB565);
773 case PixelFormat16bppARGB1555:
774 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_16bppARGB1555);
775 case PixelFormat24bppRGB:
776 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_24bppRGB);
777 case PixelFormat32bppRGB:
778 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppRGB);
779 case PixelFormat32bppARGB:
780 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppARGB);
781 case PixelFormat32bppPARGB:
782 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_32bppPARGB);
783 case PixelFormat64bppARGB:
784 convert_rgb_to_rgb(getpixel_48bppRGB, setpixel_64bppARGB);
785 default:
786 break;
788 break;
789 case PixelFormat64bppARGB:
790 switch (dst_format)
792 case PixelFormat16bppGrayScale:
793 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppGrayScale);
794 case PixelFormat16bppRGB555:
795 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB555);
796 case PixelFormat16bppRGB565:
797 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppRGB565);
798 case PixelFormat16bppARGB1555:
799 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_16bppARGB1555);
800 case PixelFormat24bppRGB:
801 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_24bppRGB);
802 case PixelFormat32bppRGB:
803 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppRGB);
804 case PixelFormat32bppARGB:
805 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppARGB);
806 case PixelFormat32bppPARGB:
807 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_32bppPARGB);
808 case PixelFormat48bppRGB:
809 convert_rgb_to_rgb(getpixel_64bppARGB, setpixel_48bppRGB);
810 default:
811 break;
813 break;
814 case PixelFormat64bppPARGB:
815 switch (dst_format)
817 case PixelFormat16bppGrayScale:
818 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppGrayScale);
819 case PixelFormat16bppRGB555:
820 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB555);
821 case PixelFormat16bppRGB565:
822 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppRGB565);
823 case PixelFormat16bppARGB1555:
824 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_16bppARGB1555);
825 case PixelFormat24bppRGB:
826 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_24bppRGB);
827 case PixelFormat32bppRGB:
828 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppRGB);
829 case PixelFormat32bppARGB:
830 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppARGB);
831 case PixelFormat32bppPARGB:
832 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_32bppPARGB);
833 case PixelFormat48bppRGB:
834 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_48bppRGB);
835 case PixelFormat64bppARGB:
836 convert_rgb_to_rgb(getpixel_64bppPARGB, setpixel_64bppARGB);
837 default:
838 break;
840 break;
841 default:
842 break;
845 #undef convert_indexed_to_rgb
846 #undef convert_rgb_to_rgb
848 return NotImplemented;
851 /* This function returns a pointer to an array of pixels that represents the
852 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
853 * flags. It is correct behavior that a user who calls this function with write
854 * privileges can write to the whole bitmap (not just the area in rect).
856 * FIXME: only used portion of format is bits per pixel. */
857 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
858 UINT flags, PixelFormat format, BitmapData* lockeddata)
860 INT stride, bitspp = PIXELFORMATBPP(format);
861 BYTE *buff = NULL;
862 UINT abs_height;
863 GpRect act_rect; /* actual rect to be used */
864 GpStatus stat;
866 TRACE("%p %p %d 0x%x %p\n", bitmap, rect, flags, format, lockeddata);
868 if(!lockeddata || !bitmap)
869 return InvalidParameter;
871 if(rect){
872 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
873 (rect->Y + rect->Height > bitmap->height) || !flags)
874 return InvalidParameter;
876 act_rect = *rect;
878 else{
879 act_rect.X = act_rect.Y = 0;
880 act_rect.Width = bitmap->width;
881 act_rect.Height = bitmap->height;
884 if(flags & ImageLockModeUserInputBuf)
886 static int fixme=0;
887 if (!fixme++) FIXME("ImageLockModeUserInputBuf not implemented\n");
888 return NotImplemented;
891 if(bitmap->lockmode)
893 WARN("bitmap is already locked and cannot be locked again\n");
894 return WrongState;
897 if (bitmap->bits && bitmap->format == format)
899 /* no conversion is necessary; just use the bits directly */
900 lockeddata->Width = act_rect.Width;
901 lockeddata->Height = act_rect.Height;
902 lockeddata->PixelFormat = format;
903 lockeddata->Reserved = flags;
904 lockeddata->Stride = bitmap->stride;
905 lockeddata->Scan0 = bitmap->bits + (bitspp / 8) * act_rect.X +
906 bitmap->stride * act_rect.Y;
908 bitmap->lockmode = flags;
909 bitmap->numlocks++;
911 return Ok;
914 /* Make sure we can convert to the requested format. */
915 stat = convert_pixels(0, 0, 0, NULL, format, 0, NULL, bitmap->format, NULL);
916 if (stat == NotImplemented)
918 FIXME("cannot read bitmap from %x to %x\n", bitmap->format, format);
919 return NotImplemented;
922 /* If we're opening for writing, make sure we'll be able to write back in
923 * the original format. */
924 if (flags & ImageLockModeWrite)
926 stat = convert_pixels(0, 0, 0, NULL, bitmap->format, 0, NULL, format, NULL);
927 if (stat == NotImplemented)
929 FIXME("cannot write bitmap from %x to %x\n", format, bitmap->format);
930 return NotImplemented;
934 abs_height = bitmap->height;
935 stride = (bitmap->width * bitspp + 7) / 8;
936 stride = (stride + 3) & ~3;
938 buff = GdipAlloc(stride * abs_height);
940 if (!buff) return OutOfMemory;
942 stat = convert_pixels(bitmap->width, bitmap->height,
943 stride, buff, format,
944 bitmap->stride, bitmap->bits, bitmap->format, bitmap->image.palette_entries);
946 if (stat != Ok)
948 GdipFree(buff);
949 return stat;
952 lockeddata->Width = act_rect.Width;
953 lockeddata->Height = act_rect.Height;
954 lockeddata->PixelFormat = format;
955 lockeddata->Reserved = flags;
956 lockeddata->Stride = stride;
957 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X + stride * act_rect.Y;
959 bitmap->lockmode = flags;
960 bitmap->numlocks++;
961 bitmap->bitmapbits = buff;
963 return Ok;
966 GpStatus WINGDIPAPI GdipBitmapSetResolution(GpBitmap* bitmap, REAL xdpi, REAL ydpi)
968 TRACE("(%p, %.2f, %.2f)\n", bitmap, xdpi, ydpi);
970 if (!bitmap || xdpi == 0.0 || ydpi == 0.0)
971 return InvalidParameter;
973 bitmap->image.xres = xdpi;
974 bitmap->image.yres = ydpi;
976 return Ok;
979 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
980 BitmapData* lockeddata)
982 GpStatus stat;
984 TRACE("(%p,%p)\n", bitmap, lockeddata);
986 if(!bitmap || !lockeddata)
987 return InvalidParameter;
989 if(!bitmap->lockmode)
990 return WrongState;
992 if(lockeddata->Reserved & ImageLockModeUserInputBuf)
993 return NotImplemented;
995 if(lockeddata->Reserved & ImageLockModeRead){
996 if(!(--bitmap->numlocks))
997 bitmap->lockmode = 0;
999 GdipFree(bitmap->bitmapbits);
1000 bitmap->bitmapbits = NULL;
1001 return Ok;
1004 if (!bitmap->bitmapbits)
1006 /* we passed a direct reference; no need to do anything */
1007 bitmap->lockmode = 0;
1008 bitmap->numlocks = 0;
1009 return Ok;
1012 stat = convert_pixels(bitmap->width, bitmap->height,
1013 bitmap->stride, bitmap->bits, bitmap->format,
1014 lockeddata->Stride, bitmap->bitmapbits, lockeddata->PixelFormat, NULL);
1016 if (stat != Ok)
1018 ERR("failed to convert pixels; this should never happen\n");
1021 GdipFree(bitmap->bitmapbits);
1022 bitmap->bitmapbits = NULL;
1023 bitmap->lockmode = 0;
1024 bitmap->numlocks = 0;
1026 return stat;
1029 GpStatus WINGDIPAPI GdipCloneBitmapArea(REAL x, REAL y, REAL width, REAL height,
1030 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1032 BitmapData lockeddata_src, lockeddata_dst;
1033 int i;
1034 UINT row_size;
1035 Rect area;
1036 GpStatus stat;
1038 TRACE("(%f,%f,%f,%f,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1040 if (!srcBitmap || !dstBitmap || srcBitmap->image.type != ImageTypeBitmap ||
1041 x < 0 || y < 0 ||
1042 x + width > srcBitmap->width || y + height > srcBitmap->height)
1044 TRACE("<-- InvalidParameter\n");
1045 return InvalidParameter;
1048 if (format == PixelFormatDontCare)
1049 format = srcBitmap->format;
1051 area.X = roundr(x);
1052 area.Y = roundr(y);
1053 area.Width = roundr(width);
1054 area.Height = roundr(height);
1056 stat = GdipBitmapLockBits(srcBitmap, &area, ImageLockModeRead, format,
1057 &lockeddata_src);
1058 if (stat != Ok) return stat;
1060 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
1061 0, lockeddata_src.PixelFormat, NULL, dstBitmap);
1062 if (stat == Ok)
1064 stat = GdipBitmapLockBits(*dstBitmap, NULL, ImageLockModeWrite,
1065 lockeddata_src.PixelFormat, &lockeddata_dst);
1067 if (stat == Ok)
1069 /* copy the image data */
1070 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
1071 for (i=0; i<lockeddata_src.Height; i++)
1072 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
1073 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
1074 row_size);
1076 GdipBitmapUnlockBits(*dstBitmap, &lockeddata_dst);
1079 if (stat != Ok)
1080 GdipDisposeImage((GpImage*)*dstBitmap);
1083 GdipBitmapUnlockBits(srcBitmap, &lockeddata_src);
1085 if (stat != Ok)
1087 *dstBitmap = NULL;
1090 return stat;
1093 GpStatus WINGDIPAPI GdipCloneBitmapAreaI(INT x, INT y, INT width, INT height,
1094 PixelFormat format, GpBitmap* srcBitmap, GpBitmap** dstBitmap)
1096 TRACE("(%i,%i,%i,%i,0x%x,%p,%p)\n", x, y, width, height, format, srcBitmap, dstBitmap);
1098 return GdipCloneBitmapArea(x, y, width, height, format, srcBitmap, dstBitmap);
1101 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
1103 GpStatus stat = GenericError;
1105 TRACE("%p, %p\n", image, cloneImage);
1107 if (!image || !cloneImage)
1108 return InvalidParameter;
1110 if (image->picture)
1112 IStream* stream;
1113 HRESULT hr;
1114 INT size;
1115 LARGE_INTEGER move;
1117 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
1118 if (FAILED(hr))
1119 return GenericError;
1121 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
1122 if(FAILED(hr))
1124 WARN("Failed to save image on stream\n");
1125 goto out;
1128 /* Set seek pointer back to the beginning of the picture */
1129 move.QuadPart = 0;
1130 hr = IStream_Seek(stream, move, STREAM_SEEK_SET, NULL);
1131 if (FAILED(hr))
1132 goto out;
1134 stat = GdipLoadImageFromStream(stream, cloneImage);
1135 if (stat != Ok) WARN("Failed to load image from stream\n");
1137 out:
1138 IStream_Release(stream);
1139 return stat;
1141 else if (image->type == ImageTypeBitmap)
1143 GpBitmap *bitmap = (GpBitmap*)image;
1144 BitmapData lockeddata_src, lockeddata_dst;
1145 int i;
1146 UINT row_size;
1148 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format,
1149 &lockeddata_src);
1150 if (stat != Ok) return stat;
1152 stat = GdipCreateBitmapFromScan0(lockeddata_src.Width, lockeddata_src.Height,
1153 0, lockeddata_src.PixelFormat, NULL, (GpBitmap**)cloneImage);
1154 if (stat == Ok)
1156 stat = GdipBitmapLockBits((GpBitmap*)*cloneImage, NULL, ImageLockModeWrite,
1157 lockeddata_src.PixelFormat, &lockeddata_dst);
1159 if (stat == Ok)
1161 /* copy the image data */
1162 row_size = (lockeddata_src.Width * PIXELFORMATBPP(lockeddata_src.PixelFormat) +7)/8;
1163 for (i=0; i<lockeddata_src.Height; i++)
1164 memcpy((BYTE*)lockeddata_dst.Scan0+lockeddata_dst.Stride*i,
1165 (BYTE*)lockeddata_src.Scan0+lockeddata_src.Stride*i,
1166 row_size);
1168 GdipBitmapUnlockBits((GpBitmap*)*cloneImage, &lockeddata_dst);
1171 if (stat != Ok)
1172 GdipDisposeImage(*cloneImage);
1175 GdipBitmapUnlockBits(bitmap, &lockeddata_src);
1177 if (stat != Ok)
1179 *cloneImage = NULL;
1181 else memcpy(&(*cloneImage)->format, &image->format, sizeof(GUID));
1183 return stat;
1185 else
1187 ERR("GpImage with no IPicture or bitmap?!\n");
1188 return NotImplemented;
1192 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
1193 GpBitmap **bitmap)
1195 GpStatus stat;
1196 IStream *stream;
1198 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1200 if(!filename || !bitmap)
1201 return InvalidParameter;
1203 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
1205 if(stat != Ok)
1206 return stat;
1208 stat = GdipCreateBitmapFromStream(stream, bitmap);
1210 IStream_Release(stream);
1212 return stat;
1215 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
1216 VOID *bits, GpBitmap **bitmap)
1218 DWORD height, stride;
1219 PixelFormat format;
1221 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
1223 if (!info || !bits || !bitmap)
1224 return InvalidParameter;
1226 height = abs(info->bmiHeader.biHeight);
1227 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
1229 if(info->bmiHeader.biHeight > 0) /* bottom-up */
1231 bits = (BYTE*)bits + (height - 1) * stride;
1232 stride = -stride;
1235 switch(info->bmiHeader.biBitCount) {
1236 case 1:
1237 format = PixelFormat1bppIndexed;
1238 break;
1239 case 4:
1240 format = PixelFormat4bppIndexed;
1241 break;
1242 case 8:
1243 format = PixelFormat8bppIndexed;
1244 break;
1245 case 16:
1246 format = PixelFormat16bppRGB555;
1247 break;
1248 case 24:
1249 format = PixelFormat24bppRGB;
1250 break;
1251 case 32:
1252 format = PixelFormat32bppRGB;
1253 break;
1254 default:
1255 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
1256 *bitmap = NULL;
1257 return InvalidParameter;
1260 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
1261 bits, bitmap);
1265 /* FIXME: no icm */
1266 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
1267 GpBitmap **bitmap)
1269 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1271 return GdipCreateBitmapFromFile(filename, bitmap);
1274 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
1275 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
1277 HBITMAP hbm;
1278 GpStatus stat = InvalidParameter;
1280 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
1282 if(!lpBitmapName || !bitmap)
1283 return InvalidParameter;
1285 /* load DIB */
1286 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
1287 LR_CREATEDIBSECTION);
1289 if(hbm){
1290 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1291 DeleteObject(hbm);
1294 return stat;
1297 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
1298 HBITMAP* hbmReturn, ARGB background)
1300 GpStatus stat;
1301 HBITMAP result, oldbitmap;
1302 UINT width, height;
1303 HDC hdc;
1304 GpGraphics *graphics;
1305 BITMAPINFOHEADER bih;
1306 void *bits;
1307 TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
1309 if (!bitmap || !hbmReturn) return InvalidParameter;
1311 GdipGetImageWidth((GpImage*)bitmap, &width);
1312 GdipGetImageHeight((GpImage*)bitmap, &height);
1314 bih.biSize = sizeof(bih);
1315 bih.biWidth = width;
1316 bih.biHeight = height;
1317 bih.biPlanes = 1;
1318 bih.biBitCount = 32;
1319 bih.biCompression = BI_RGB;
1320 bih.biSizeImage = 0;
1321 bih.biXPelsPerMeter = 0;
1322 bih.biYPelsPerMeter = 0;
1323 bih.biClrUsed = 0;
1324 bih.biClrImportant = 0;
1326 hdc = CreateCompatibleDC(NULL);
1327 if (!hdc) return GenericError;
1329 result = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS, &bits,
1330 NULL, 0);
1332 if (result)
1334 oldbitmap = SelectObject(hdc, result);
1336 stat = GdipCreateFromHDC(hdc, &graphics);
1337 if (stat == Ok)
1339 stat = GdipGraphicsClear(graphics, background);
1341 if (stat == Ok)
1342 stat = GdipDrawImage(graphics, (GpImage*)bitmap, 0, 0);
1344 GdipDeleteGraphics(graphics);
1347 SelectObject(hdc, oldbitmap);
1349 else
1350 stat = GenericError;
1352 DeleteDC(hdc);
1354 if (stat != Ok && result)
1356 DeleteObject(result);
1357 result = NULL;
1360 *hbmReturn = result;
1362 return stat;
1365 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
1366 GpMetafile* metafile, BOOL* succ, EmfType emfType,
1367 const WCHAR* description, GpMetafile** out_metafile)
1369 static int calls;
1371 TRACE("(%p,%p,%p,%u,%s,%p)\n", ref, metafile, succ, emfType,
1372 debugstr_w(description), out_metafile);
1374 if(!ref || !metafile || !out_metafile)
1375 return InvalidParameter;
1377 *succ = FALSE;
1378 *out_metafile = NULL;
1380 if(!(calls++))
1381 FIXME("not implemented\n");
1383 return NotImplemented;
1386 /* FIXME: this should create a bitmap in the given size with the attributes
1387 * (resolution etc.) of the graphics object */
1388 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
1389 GpGraphics* target, GpBitmap** bitmap)
1391 static int calls;
1392 GpStatus ret;
1394 TRACE("(%d, %d, %p, %p)\n", width, height, target, bitmap);
1396 if(!target || !bitmap)
1397 return InvalidParameter;
1399 if(!(calls++))
1400 FIXME("hacked stub\n");
1402 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
1403 NULL, bitmap);
1405 return ret;
1408 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1410 GpStatus stat;
1411 ICONINFO iinfo;
1412 BITMAP bm;
1413 int ret;
1414 UINT width, height;
1415 GpRect rect;
1416 BitmapData lockeddata;
1417 HDC screendc;
1418 BOOL has_alpha;
1419 int x, y;
1420 BYTE *bits;
1421 BITMAPINFOHEADER bih;
1422 DWORD *src;
1423 BYTE *dst_row;
1424 DWORD *dst;
1426 TRACE("%p, %p\n", hicon, bitmap);
1428 if(!bitmap || !GetIconInfo(hicon, &iinfo))
1429 return InvalidParameter;
1431 /* get the size of the icon */
1432 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1433 if (ret == 0) {
1434 DeleteObject(iinfo.hbmColor);
1435 DeleteObject(iinfo.hbmMask);
1436 return GenericError;
1439 width = bm.bmWidth;
1441 if (iinfo.hbmColor)
1442 height = abs(bm.bmHeight);
1443 else /* combined bitmap + mask */
1444 height = abs(bm.bmHeight) / 2;
1446 bits = HeapAlloc(GetProcessHeap(), 0, 4*width*height);
1447 if (!bits) {
1448 DeleteObject(iinfo.hbmColor);
1449 DeleteObject(iinfo.hbmMask);
1450 return OutOfMemory;
1453 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB, NULL, bitmap);
1454 if (stat != Ok) {
1455 DeleteObject(iinfo.hbmColor);
1456 DeleteObject(iinfo.hbmMask);
1457 HeapFree(GetProcessHeap(), 0, bits);
1458 return stat;
1461 rect.X = 0;
1462 rect.Y = 0;
1463 rect.Width = width;
1464 rect.Height = height;
1466 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1467 if (stat != Ok) {
1468 DeleteObject(iinfo.hbmColor);
1469 DeleteObject(iinfo.hbmMask);
1470 HeapFree(GetProcessHeap(), 0, bits);
1471 GdipDisposeImage((GpImage*)*bitmap);
1472 return stat;
1475 bih.biSize = sizeof(bih);
1476 bih.biWidth = width;
1477 bih.biHeight = -height;
1478 bih.biPlanes = 1;
1479 bih.biBitCount = 32;
1480 bih.biCompression = BI_RGB;
1481 bih.biSizeImage = 0;
1482 bih.biXPelsPerMeter = 0;
1483 bih.biYPelsPerMeter = 0;
1484 bih.biClrUsed = 0;
1485 bih.biClrImportant = 0;
1487 screendc = GetDC(0);
1488 if (iinfo.hbmColor)
1490 GetDIBits(screendc, iinfo.hbmColor, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1492 if (bm.bmBitsPixel == 32)
1494 has_alpha = FALSE;
1496 /* If any pixel has a non-zero alpha, ignore hbmMask */
1497 src = (DWORD*)bits;
1498 for (x=0; x<width && !has_alpha; x++)
1499 for (y=0; y<height && !has_alpha; y++)
1500 if ((*src++ & 0xff000000) != 0)
1501 has_alpha = TRUE;
1503 else has_alpha = FALSE;
1505 else
1507 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1508 has_alpha = FALSE;
1511 /* copy the image data to the Bitmap */
1512 src = (DWORD*)bits;
1513 dst_row = lockeddata.Scan0;
1514 for (y=0; y<height; y++)
1516 memcpy(dst_row, src, width*4);
1517 src += width;
1518 dst_row += lockeddata.Stride;
1521 if (!has_alpha)
1523 if (iinfo.hbmMask)
1525 /* read alpha data from the mask */
1526 if (iinfo.hbmColor)
1527 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1528 else
1529 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1531 src = (DWORD*)bits;
1532 dst_row = lockeddata.Scan0;
1533 for (y=0; y<height; y++)
1535 dst = (DWORD*)dst_row;
1536 for (x=0; x<height; x++)
1538 DWORD src_value = *src++;
1539 if (src_value)
1540 *dst++ = 0;
1541 else
1542 *dst++ |= 0xff000000;
1544 dst_row += lockeddata.Stride;
1547 else
1549 /* set constant alpha of 255 */
1550 dst_row = bits;
1551 for (y=0; y<height; y++)
1553 dst = (DWORD*)dst_row;
1554 for (x=0; x<height; x++)
1555 *dst++ |= 0xff000000;
1556 dst_row += lockeddata.Stride;
1561 ReleaseDC(0, screendc);
1563 DeleteObject(iinfo.hbmColor);
1564 DeleteObject(iinfo.hbmMask);
1566 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1568 HeapFree(GetProcessHeap(), 0, bits);
1570 return Ok;
1573 static void generate_halftone_palette(ARGB *entries, UINT count)
1575 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1576 UINT i;
1578 for (i=0; i<8 && i<count; i++)
1580 entries[i] = 0xff000000;
1581 if (i&1) entries[i] |= 0x800000;
1582 if (i&2) entries[i] |= 0x8000;
1583 if (i&4) entries[i] |= 0x80;
1586 if (8 < count)
1587 entries[i] = 0xffc0c0c0;
1589 for (i=9; i<16 && i<count; i++)
1591 entries[i] = 0xff000000;
1592 if (i&1) entries[i] |= 0xff0000;
1593 if (i&2) entries[i] |= 0xff00;
1594 if (i&4) entries[i] |= 0xff;
1597 for (i=16; i<40 && i<count; i++)
1599 entries[i] = 0;
1602 for (i=40; i<256 && i<count; i++)
1604 entries[i] = 0xff000000;
1605 entries[i] |= halftone_values[(i-40)%6];
1606 entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1607 entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1611 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1613 HDC screendc = GetDC(0);
1615 if (!screendc) return GenericError;
1617 *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1618 *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1620 ReleaseDC(0, screendc);
1622 return Ok;
1625 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1626 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1628 BITMAPINFO* pbmi;
1629 HBITMAP hbitmap=NULL;
1630 INT row_size, dib_stride;
1631 HDC hdc;
1632 BYTE *bits=NULL, *own_bits=NULL;
1633 int i;
1634 REAL xres, yres;
1635 GpStatus stat;
1637 TRACE("%d %d %d 0x%x %p %p\n", width, height, stride, format, scan0, bitmap);
1639 if (!bitmap) return InvalidParameter;
1641 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1642 *bitmap = NULL;
1643 return InvalidParameter;
1646 if(scan0 && !stride)
1647 return InvalidParameter;
1649 stat = get_screen_resolution(&xres, &yres);
1650 if (stat != Ok) return stat;
1652 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1653 dib_stride = (row_size + 3) & ~3;
1655 if(stride == 0)
1656 stride = dib_stride;
1658 if (format & PixelFormatGDI)
1660 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1661 if (!pbmi)
1662 return OutOfMemory;
1664 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1665 pbmi->bmiHeader.biWidth = width;
1666 pbmi->bmiHeader.biHeight = -height;
1667 pbmi->bmiHeader.biPlanes = 1;
1668 /* FIXME: use the rest of the data from format */
1669 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(format);
1670 pbmi->bmiHeader.biCompression = BI_RGB;
1671 pbmi->bmiHeader.biSizeImage = 0;
1672 pbmi->bmiHeader.biXPelsPerMeter = 0;
1673 pbmi->bmiHeader.biYPelsPerMeter = 0;
1674 pbmi->bmiHeader.biClrUsed = 0;
1675 pbmi->bmiHeader.biClrImportant = 0;
1677 hdc = CreateCompatibleDC(NULL);
1678 if (!hdc) {
1679 GdipFree(pbmi);
1680 return GenericError;
1683 hbitmap = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1685 DeleteDC(hdc);
1686 GdipFree(pbmi);
1688 if (!hbitmap) return GenericError;
1690 else
1692 /* Not a GDI format; don't try to make an HBITMAP. */
1693 if (scan0)
1695 /* FIXME: We should do this with GDI formats too when scan0 is
1696 * provided, but for now we need the HDC for most drawing
1697 * operations. */
1698 bits = scan0;
1700 else
1702 INT size = abs(stride) * height;
1704 own_bits = bits = GdipAlloc(size);
1705 if (!own_bits) return OutOfMemory;
1707 if (stride < 0)
1708 bits += stride * (1 - height);
1712 /* copy bits to the dib if necessary */
1713 /* FIXME: should reference the bits instead of copying them */
1714 if (scan0 && bits != scan0)
1715 for (i=0; i<height; i++)
1716 memcpy(bits+i*dib_stride, scan0+i*stride, row_size);
1718 *bitmap = GdipAlloc(sizeof(GpBitmap));
1719 if(!*bitmap)
1721 DeleteObject(hbitmap);
1722 GdipFree(own_bits);
1723 return OutOfMemory;
1726 (*bitmap)->image.type = ImageTypeBitmap;
1727 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1728 (*bitmap)->image.flags = ImageFlagsNone;
1729 (*bitmap)->image.palette_flags = 0;
1730 (*bitmap)->image.palette_count = 0;
1731 (*bitmap)->image.palette_size = 0;
1732 (*bitmap)->image.palette_entries = NULL;
1733 (*bitmap)->image.xres = xres;
1734 (*bitmap)->image.yres = yres;
1735 (*bitmap)->width = width;
1736 (*bitmap)->height = height;
1737 (*bitmap)->format = format;
1738 (*bitmap)->image.picture = NULL;
1739 (*bitmap)->hbitmap = hbitmap;
1740 (*bitmap)->hdc = NULL;
1741 (*bitmap)->bits = bits;
1742 (*bitmap)->stride = dib_stride;
1743 (*bitmap)->own_bits = own_bits;
1745 /* set format-related flags */
1746 if (format & (PixelFormatAlpha|PixelFormatPAlpha|PixelFormatIndexed))
1747 (*bitmap)->image.flags |= ImageFlagsHasAlpha;
1749 if (format == PixelFormat1bppIndexed ||
1750 format == PixelFormat4bppIndexed ||
1751 format == PixelFormat8bppIndexed)
1753 (*bitmap)->image.palette_size = (*bitmap)->image.palette_count = 1 << PIXELFORMATBPP(format);
1754 (*bitmap)->image.palette_entries = GdipAlloc(sizeof(ARGB) * ((*bitmap)->image.palette_size));
1756 if (!(*bitmap)->image.palette_entries)
1758 GdipDisposeImage(&(*bitmap)->image);
1759 *bitmap = NULL;
1760 return OutOfMemory;
1763 if (format == PixelFormat1bppIndexed)
1765 (*bitmap)->image.palette_flags = PaletteFlagsGrayScale;
1766 (*bitmap)->image.palette_entries[0] = 0xff000000;
1767 (*bitmap)->image.palette_entries[1] = 0xffffffff;
1769 else
1771 if (format == PixelFormat8bppIndexed)
1772 (*bitmap)->image.palette_flags = PaletteFlagsHalftone;
1774 generate_halftone_palette((*bitmap)->image.palette_entries,
1775 (*bitmap)->image.palette_count);
1779 TRACE("<-- %p\n", *bitmap);
1781 return Ok;
1784 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1785 GpBitmap **bitmap)
1787 GpStatus stat;
1789 TRACE("%p %p\n", stream, bitmap);
1791 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1793 if(stat != Ok)
1794 return stat;
1796 if((*bitmap)->image.type != ImageTypeBitmap){
1797 GdipDisposeImage(&(*bitmap)->image);
1798 *bitmap = NULL;
1799 return GenericError; /* FIXME: what error to return? */
1802 return Ok;
1805 /* FIXME: no icm */
1806 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1807 GpBitmap **bitmap)
1809 TRACE("%p %p\n", stream, bitmap);
1811 return GdipCreateBitmapFromStream(stream, bitmap);
1814 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1815 GpCachedBitmap **cachedbmp)
1817 GpStatus stat;
1819 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1821 if(!bitmap || !graphics || !cachedbmp)
1822 return InvalidParameter;
1824 *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
1825 if(!*cachedbmp)
1826 return OutOfMemory;
1828 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1829 if(stat != Ok){
1830 GdipFree(*cachedbmp);
1831 return stat;
1834 return Ok;
1837 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1839 GpStatus stat;
1840 BitmapData lockeddata;
1841 ULONG andstride, xorstride, bitssize;
1842 LPBYTE andbits, xorbits, androw, xorrow, srcrow;
1843 UINT x, y;
1845 TRACE("(%p, %p)\n", bitmap, hicon);
1847 if (!bitmap || !hicon)
1848 return InvalidParameter;
1850 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead,
1851 PixelFormat32bppPARGB, &lockeddata);
1852 if (stat == Ok)
1854 andstride = ((lockeddata.Width+31)/32)*4;
1855 xorstride = lockeddata.Width*4;
1856 bitssize = (andstride + xorstride) * lockeddata.Height;
1858 andbits = GdipAlloc(bitssize);
1860 if (andbits)
1862 xorbits = andbits + andstride * lockeddata.Height;
1864 for (y=0; y<lockeddata.Height; y++)
1866 srcrow = ((LPBYTE)lockeddata.Scan0) + lockeddata.Stride * y;
1868 androw = andbits + andstride * y;
1869 for (x=0; x<lockeddata.Width; x++)
1870 if (srcrow[3+4*x] >= 128)
1871 androw[x/8] |= 1 << (7-x%8);
1873 xorrow = xorbits + xorstride * y;
1874 memcpy(xorrow, srcrow, xorstride);
1877 *hicon = CreateIcon(NULL, lockeddata.Width, lockeddata.Height, 1, 32,
1878 andbits, xorbits);
1880 GdipFree(andbits);
1882 else
1883 stat = OutOfMemory;
1885 GdipBitmapUnlockBits(bitmap, &lockeddata);
1888 return stat;
1891 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
1893 TRACE("%p\n", cachedbmp);
1895 if(!cachedbmp)
1896 return InvalidParameter;
1898 GdipDisposeImage(cachedbmp->image);
1899 GdipFree(cachedbmp);
1901 return Ok;
1904 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
1905 GpCachedBitmap *cachedbmp, INT x, INT y)
1907 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
1909 if(!graphics || !cachedbmp)
1910 return InvalidParameter;
1912 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
1915 GpStatus WINGDIPAPI GdipEmfToWmfBits(HENHMETAFILE hemf, UINT cbData16,
1916 LPBYTE pData16, INT iMapMode, INT eFlags)
1918 FIXME("(%p, %d, %p, %d, %d): stub\n", hemf, cbData16, pData16, iMapMode, eFlags);
1919 return NotImplemented;
1922 /* Internal utility function: Replace the image data of dst with that of src,
1923 * and free src. */
1924 static void move_bitmap(GpBitmap *dst, GpBitmap *src, BOOL clobber_palette)
1926 GdipFree(dst->bitmapbits);
1927 DeleteDC(dst->hdc);
1928 DeleteObject(dst->hbitmap);
1930 if (clobber_palette)
1932 GdipFree(dst->image.palette_entries);
1933 dst->image.palette_flags = src->image.palette_flags;
1934 dst->image.palette_count = src->image.palette_count;
1935 dst->image.palette_entries = src->image.palette_entries;
1937 else
1938 GdipFree(src->image.palette_entries);
1940 dst->image.xres = src->image.xres;
1941 dst->image.yres = src->image.yres;
1942 dst->width = src->width;
1943 dst->height = src->height;
1944 dst->format = src->format;
1945 dst->hbitmap = src->hbitmap;
1946 dst->hdc = src->hdc;
1947 dst->bits = src->bits;
1948 dst->stride = src->stride;
1949 dst->own_bits = src->own_bits;
1951 GdipFree(src);
1954 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
1956 TRACE("%p\n", image);
1958 if(!image)
1959 return InvalidParameter;
1961 if (image->type == ImageTypeBitmap)
1963 GdipFree(((GpBitmap*)image)->bitmapbits);
1964 GdipFree(((GpBitmap*)image)->own_bits);
1965 DeleteDC(((GpBitmap*)image)->hdc);
1966 DeleteObject(((GpBitmap*)image)->hbitmap);
1968 else if (image->type != ImageTypeMetafile)
1970 WARN("invalid image: %p\n", image);
1971 return ObjectBusy;
1973 if (image->picture)
1974 IPicture_Release(image->picture);
1975 GdipFree(image->palette_entries);
1976 image->type = ~0;
1977 GdipFree(image);
1979 return Ok;
1982 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
1984 static int calls;
1986 TRACE("(%p,%p)\n", image, item);
1988 if(!image || !item)
1989 return InvalidParameter;
1991 if (!(calls++))
1992 FIXME("not implemented\n");
1994 return NotImplemented;
1997 GpStatus WINGDIPAPI GdipGetImageItemData(GpImage *image, ImageItemData *item)
1999 static int calls;
2001 TRACE("(%p,%p)\n", image, item);
2003 if (!(calls++))
2004 FIXME("not implemented\n");
2006 return NotImplemented;
2009 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
2010 GpUnit *srcUnit)
2012 TRACE("%p %p %p\n", image, srcRect, srcUnit);
2014 if(!image || !srcRect || !srcUnit)
2015 return InvalidParameter;
2016 if(image->type == ImageTypeMetafile){
2017 *srcRect = ((GpMetafile*)image)->bounds;
2018 *srcUnit = ((GpMetafile*)image)->unit;
2020 else if(image->type == ImageTypeBitmap){
2021 srcRect->X = srcRect->Y = 0.0;
2022 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
2023 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
2024 *srcUnit = UnitPixel;
2026 else{
2027 srcRect->X = srcRect->Y = 0.0;
2028 srcRect->Width = ipicture_pixel_width(image->picture);
2029 srcRect->Height = ipicture_pixel_height(image->picture);
2030 *srcUnit = UnitPixel;
2033 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
2034 srcRect->Width, srcRect->Height, *srcUnit);
2036 return Ok;
2039 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
2040 REAL *height)
2042 TRACE("%p %p %p\n", image, width, height);
2044 if(!image || !height || !width)
2045 return InvalidParameter;
2047 if(image->type == ImageTypeMetafile){
2048 HDC hdc = GetDC(0);
2049 REAL res = (REAL)GetDeviceCaps(hdc, LOGPIXELSX);
2051 ReleaseDC(0, hdc);
2053 *height = convert_unit(res, ((GpMetafile*)image)->unit) *
2054 ((GpMetafile*)image)->bounds.Height;
2056 *width = convert_unit(res, ((GpMetafile*)image)->unit) *
2057 ((GpMetafile*)image)->bounds.Width;
2060 else if(image->type == ImageTypeBitmap){
2061 *height = ((GpBitmap*)image)->height;
2062 *width = ((GpBitmap*)image)->width;
2064 else{
2065 *height = ipicture_pixel_height(image->picture);
2066 *width = ipicture_pixel_width(image->picture);
2069 TRACE("returning (%f, %f)\n", *height, *width);
2070 return Ok;
2073 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
2074 GpGraphics **graphics)
2076 HDC hdc;
2077 GpStatus stat;
2079 TRACE("%p %p\n", image, graphics);
2081 if(!image || !graphics)
2082 return InvalidParameter;
2084 if(image->type != ImageTypeBitmap){
2085 FIXME("not implemented for image type %d\n", image->type);
2086 return NotImplemented;
2089 if (((GpBitmap*)image)->hbitmap)
2091 hdc = ((GpBitmap*)image)->hdc;
2093 if(!hdc){
2094 hdc = CreateCompatibleDC(0);
2095 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
2096 ((GpBitmap*)image)->hdc = hdc;
2099 stat = GdipCreateFromHDC(hdc, graphics);
2101 if (stat == Ok)
2102 (*graphics)->image = image;
2104 else
2105 stat = graphics_from_image(image, graphics);
2107 return stat;
2110 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
2112 TRACE("%p %p\n", image, height);
2114 if(!image || !height)
2115 return InvalidParameter;
2117 if(image->type == ImageTypeMetafile){
2118 HDC hdc = GetDC(0);
2119 REAL res = (REAL)GetDeviceCaps(hdc, LOGPIXELSX);
2121 ReleaseDC(0, hdc);
2123 *height = roundr(convert_unit(res, ((GpMetafile*)image)->unit) *
2124 ((GpMetafile*)image)->bounds.Height);
2126 else if(image->type == ImageTypeBitmap)
2127 *height = ((GpBitmap*)image)->height;
2128 else
2129 *height = ipicture_pixel_height(image->picture);
2131 TRACE("returning %d\n", *height);
2133 return Ok;
2136 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2138 if(!image || !res)
2139 return InvalidParameter;
2141 *res = image->xres;
2143 TRACE("(%p) <-- %0.2f\n", image, *res);
2145 return Ok;
2148 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2150 TRACE("%p %p\n", image, size);
2152 if(!image || !size)
2153 return InvalidParameter;
2155 if (image->palette_count == 0)
2156 *size = sizeof(ColorPalette);
2157 else
2158 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette_count;
2160 TRACE("<-- %u\n", *size);
2162 return Ok;
2165 /* FIXME: test this function for non-bitmap types */
2166 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2168 TRACE("%p %p\n", image, format);
2170 if(!image || !format)
2171 return InvalidParameter;
2173 if(image->type != ImageTypeBitmap)
2174 *format = PixelFormat24bppRGB;
2175 else
2176 *format = ((GpBitmap*) image)->format;
2178 return Ok;
2181 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2183 TRACE("(%p, %p)\n", image, format);
2185 if(!image || !format)
2186 return InvalidParameter;
2188 memcpy(format, &image->format, sizeof(GUID));
2190 return Ok;
2193 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2195 TRACE("%p %p\n", image, type);
2197 if(!image || !type)
2198 return InvalidParameter;
2200 *type = image->type;
2202 return Ok;
2205 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2207 if(!image || !res)
2208 return InvalidParameter;
2210 *res = image->yres;
2212 TRACE("(%p) <-- %0.2f\n", image, *res);
2214 return Ok;
2217 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2219 TRACE("%p %p\n", image, width);
2221 if(!image || !width)
2222 return InvalidParameter;
2224 if(image->type == ImageTypeMetafile){
2225 HDC hdc = GetDC(0);
2226 REAL res = (REAL)GetDeviceCaps(hdc, LOGPIXELSX);
2228 ReleaseDC(0, hdc);
2230 *width = roundr(convert_unit(res, ((GpMetafile*)image)->unit) *
2231 ((GpMetafile*)image)->bounds.Width);
2233 else if(image->type == ImageTypeBitmap)
2234 *width = ((GpBitmap*)image)->width;
2235 else
2236 *width = ipicture_pixel_width(image->picture);
2238 TRACE("returning %d\n", *width);
2240 return Ok;
2243 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
2244 MetafileHeader * header)
2246 static int calls;
2248 TRACE("(%p, %p)\n", metafile, header);
2250 if(!metafile || !header)
2251 return InvalidParameter;
2253 if(!(calls++))
2254 FIXME("not implemented\n");
2256 memset(header, 0, sizeof(MetafileHeader));
2258 return Ok;
2261 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromEmf(HENHMETAFILE hEmf,
2262 MetafileHeader *header)
2264 static int calls;
2266 if(!hEmf || !header)
2267 return InvalidParameter;
2269 if(!(calls++))
2270 FIXME("not implemented\n");
2272 memset(header, 0, sizeof(MetafileHeader));
2274 return Ok;
2277 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromFile(GDIPCONST WCHAR *filename,
2278 MetafileHeader *header)
2280 static int calls;
2282 TRACE("(%s,%p)\n", debugstr_w(filename), header);
2284 if(!filename || !header)
2285 return InvalidParameter;
2287 if(!(calls++))
2288 FIXME("not implemented\n");
2290 memset(header, 0, sizeof(MetafileHeader));
2292 return Ok;
2295 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromStream(IStream *stream,
2296 MetafileHeader *header)
2298 static int calls;
2300 TRACE("(%p,%p)\n", stream, header);
2302 if(!stream || !header)
2303 return InvalidParameter;
2305 if(!(calls++))
2306 FIXME("not implemented\n");
2308 memset(header, 0, sizeof(MetafileHeader));
2310 return Ok;
2313 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2314 UINT num, PropertyItem* items)
2316 static int calls;
2318 TRACE("(%p, %u, %u, %p)\n", image, size, num, items);
2320 if(!(calls++))
2321 FIXME("not implemented\n");
2323 return InvalidParameter;
2326 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
2328 static int calls;
2330 TRACE("(%p, %p)\n", image, num);
2332 if(!(calls++))
2333 FIXME("not implemented\n");
2335 return InvalidParameter;
2338 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
2340 static int calls;
2342 TRACE("(%p, %u, %p)\n", image, num, list);
2344 if(!(calls++))
2345 FIXME("not implemented\n");
2347 return InvalidParameter;
2350 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
2351 PropertyItem* buffer)
2353 static int calls;
2355 TRACE("(%p, %u, %u, %p)\n", image, id, size, buffer);
2357 if(!(calls++))
2358 FIXME("not implemented\n");
2360 return InvalidParameter;
2363 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
2364 UINT* size)
2366 static int calls;
2368 TRACE("%p %x %p\n", image, pid, size);
2370 if(!size || !image)
2371 return InvalidParameter;
2373 if(!(calls++))
2374 FIXME("not implemented\n");
2376 return NotImplemented;
2379 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
2381 static int calls;
2383 TRACE("(%p,%p,%p)\n", image, size, num);
2385 if(!(calls++))
2386 FIXME("not implemented\n");
2388 return InvalidParameter;
2391 struct image_format_dimension
2393 const GUID *format;
2394 const GUID *dimension;
2397 struct image_format_dimension image_format_dimensions[] =
2399 {&ImageFormatGIF, &FrameDimensionTime},
2400 {&ImageFormatIcon, &FrameDimensionResolution},
2401 {NULL}
2404 /* FIXME: Need to handle multi-framed images */
2405 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
2406 GDIPCONST GUID* dimensionID, UINT* count)
2408 static int calls;
2410 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
2412 if(!image || !count)
2413 return InvalidParameter;
2415 if(!(calls++))
2416 FIXME("returning frame count of 1\n");
2418 *count = 1;
2420 return Ok;
2423 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
2424 UINT* count)
2426 TRACE("(%p, %p)\n", image, count);
2428 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2430 if(!image || !count)
2431 return InvalidParameter;
2433 *count = 1;
2435 return Ok;
2438 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
2439 GUID* dimensionIDs, UINT count)
2441 int i;
2442 const GUID *result=NULL;
2444 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
2446 if(!image || !dimensionIDs || count != 1)
2447 return InvalidParameter;
2449 for (i=0; image_format_dimensions[i].format; i++)
2451 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
2453 result = image_format_dimensions[i].dimension;
2454 break;
2458 if (!result)
2459 result = &FrameDimensionPage;
2461 memcpy(dimensionIDs, result, sizeof(GUID));
2463 return Ok;
2466 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
2467 GDIPCONST GUID* dimensionID, UINT frameidx)
2469 static int calls;
2471 TRACE("(%p, %s, %u)\n", image, debugstr_guid(dimensionID), frameidx);
2473 if(!image || !dimensionID)
2474 return InvalidParameter;
2476 if(!(calls++))
2477 FIXME("not implemented\n");
2479 return Ok;
2482 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
2483 GpImage **image)
2485 GpStatus stat;
2486 IStream *stream;
2488 TRACE("(%s) %p\n", debugstr_w(filename), image);
2490 if (!filename || !image)
2491 return InvalidParameter;
2493 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
2495 if (stat != Ok)
2496 return stat;
2498 stat = GdipLoadImageFromStream(stream, image);
2500 IStream_Release(stream);
2502 return stat;
2505 /* FIXME: no icm handling */
2506 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
2508 TRACE("(%s) %p\n", debugstr_w(filename), image);
2510 return GdipLoadImageFromFile(filename, image);
2513 static const WICPixelFormatGUID *wic_pixel_formats[] = {
2514 &GUID_WICPixelFormat16bppBGR555,
2515 &GUID_WICPixelFormat24bppBGR,
2516 &GUID_WICPixelFormat32bppBGR,
2517 &GUID_WICPixelFormat32bppBGRA,
2518 &GUID_WICPixelFormat32bppPBGRA,
2519 NULL
2522 static const PixelFormat wic_gdip_formats[] = {
2523 PixelFormat16bppRGB555,
2524 PixelFormat24bppRGB,
2525 PixelFormat32bppRGB,
2526 PixelFormat32bppARGB,
2527 PixelFormat32bppPARGB,
2530 static GpStatus decode_image_wic(IStream* stream, REFCLSID clsid, GpImage **image)
2532 GpStatus status=Ok;
2533 GpBitmap *bitmap;
2534 HRESULT hr;
2535 IWICBitmapDecoder *decoder;
2536 IWICBitmapFrameDecode *frame;
2537 IWICBitmapSource *source=NULL;
2538 WICPixelFormatGUID wic_format;
2539 PixelFormat gdip_format=0;
2540 int i;
2541 UINT width, height;
2542 BitmapData lockeddata;
2543 WICRect wrc;
2544 HRESULT initresult;
2546 initresult = CoInitialize(NULL);
2548 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
2549 &IID_IWICBitmapDecoder, (void**)&decoder);
2550 if (FAILED(hr)) goto end;
2552 hr = IWICBitmapDecoder_Initialize(decoder, (IStream*)stream, WICDecodeMetadataCacheOnLoad);
2553 if (SUCCEEDED(hr))
2554 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
2556 if (SUCCEEDED(hr)) /* got frame */
2558 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
2560 if (SUCCEEDED(hr))
2562 for (i=0; wic_pixel_formats[i]; i++)
2564 if (IsEqualGUID(&wic_format, wic_pixel_formats[i]))
2566 source = (IWICBitmapSource*)frame;
2567 IWICBitmapSource_AddRef(source);
2568 gdip_format = wic_gdip_formats[i];
2569 break;
2572 if (!source)
2574 /* unknown format; fall back on 32bppARGB */
2575 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
2576 gdip_format = PixelFormat32bppARGB;
2580 if (SUCCEEDED(hr)) /* got source */
2582 hr = IWICBitmapSource_GetSize(source, &width, &height);
2584 if (SUCCEEDED(hr))
2585 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
2586 NULL, &bitmap);
2588 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
2590 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
2591 gdip_format, &lockeddata);
2592 if (status == Ok) /* locked bitmap */
2594 wrc.X = 0;
2595 wrc.Width = width;
2596 wrc.Height = 1;
2597 for (i=0; i<height; i++)
2599 wrc.Y = i;
2600 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
2601 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
2602 if (FAILED(hr)) break;
2605 GdipBitmapUnlockBits(bitmap, &lockeddata);
2608 if (SUCCEEDED(hr) && status == Ok)
2609 *image = (GpImage*)bitmap;
2610 else
2612 *image = NULL;
2613 GdipDisposeImage((GpImage*)bitmap);
2616 if (SUCCEEDED(hr) && status == Ok)
2618 double dpix, dpiy;
2619 hr = IWICBitmapSource_GetResolution(source, &dpix, &dpiy);
2620 if (SUCCEEDED(hr))
2622 bitmap->image.xres = dpix;
2623 bitmap->image.yres = dpiy;
2625 hr = S_OK;
2629 IWICBitmapSource_Release(source);
2632 IWICBitmapFrameDecode_Release(frame);
2635 IWICBitmapDecoder_Release(decoder);
2637 end:
2638 if (SUCCEEDED(initresult)) CoUninitialize();
2640 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
2642 if (status == Ok)
2644 /* Native GDI+ used to be smarter, but since Win7 it just sets these flags. */
2645 bitmap->image.flags |= ImageFlagsReadOnly|ImageFlagsHasRealPixelSize|ImageFlagsHasRealDPI|ImageFlagsColorSpaceRGB;
2648 return status;
2651 static GpStatus decode_image_icon(IStream* stream, REFCLSID clsid, GpImage **image)
2653 return decode_image_wic(stream, &CLSID_WICIcoDecoder, image);
2656 static GpStatus decode_image_bmp(IStream* stream, REFCLSID clsid, GpImage **image)
2658 GpStatus status;
2659 GpBitmap* bitmap;
2661 status = decode_image_wic(stream, &CLSID_WICBmpDecoder, image);
2663 bitmap = (GpBitmap*)*image;
2665 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
2667 /* WIC supports bmp files with alpha, but gdiplus does not */
2668 bitmap->format = PixelFormat32bppRGB;
2671 return status;
2674 static GpStatus decode_image_jpeg(IStream* stream, REFCLSID clsid, GpImage **image)
2676 return decode_image_wic(stream, &CLSID_WICJpegDecoder, image);
2679 static GpStatus decode_image_png(IStream* stream, REFCLSID clsid, GpImage **image)
2681 return decode_image_wic(stream, &CLSID_WICPngDecoder, image);
2684 static GpStatus decode_image_gif(IStream* stream, REFCLSID clsid, GpImage **image)
2686 return decode_image_wic(stream, &CLSID_WICGifDecoder, image);
2689 static GpStatus decode_image_tiff(IStream* stream, REFCLSID clsid, GpImage **image)
2691 return decode_image_wic(stream, &CLSID_WICTiffDecoder, image);
2694 static GpStatus decode_image_olepicture_metafile(IStream* stream, REFCLSID clsid, GpImage **image)
2696 IPicture *pic;
2698 TRACE("%p %p\n", stream, image);
2700 if(!stream || !image)
2701 return InvalidParameter;
2703 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
2704 (LPVOID*) &pic) != S_OK){
2705 TRACE("Could not load picture\n");
2706 return GenericError;
2709 /* FIXME: missing initialization code */
2710 *image = GdipAlloc(sizeof(GpMetafile));
2711 if(!*image) return OutOfMemory;
2712 (*image)->type = ImageTypeMetafile;
2713 (*image)->picture = pic;
2714 (*image)->flags = ImageFlagsNone;
2715 (*image)->palette_flags = 0;
2716 (*image)->palette_count = 0;
2717 (*image)->palette_size = 0;
2718 (*image)->palette_entries = NULL;
2720 TRACE("<-- %p\n", *image);
2722 return Ok;
2725 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
2726 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
2728 typedef GpStatus (*decode_image_func)(IStream *stream, REFCLSID clsid, GpImage** image);
2730 typedef struct image_codec {
2731 ImageCodecInfo info;
2732 encode_image_func encode_func;
2733 decode_image_func decode_func;
2734 } image_codec;
2736 typedef enum {
2737 BMP,
2738 JPEG,
2739 GIF,
2740 TIFF,
2741 EMF,
2742 WMF,
2743 PNG,
2744 ICO,
2745 NUM_CODECS
2746 } ImageFormat;
2748 static const struct image_codec codecs[NUM_CODECS];
2750 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
2752 BYTE signature[8];
2753 const BYTE *pattern, *mask;
2754 LARGE_INTEGER seek;
2755 HRESULT hr;
2756 UINT bytesread;
2757 int i, j, sig;
2759 /* seek to the start of the stream */
2760 seek.QuadPart = 0;
2761 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
2762 if (FAILED(hr)) return hresult_to_status(hr);
2764 /* read the first 8 bytes */
2765 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
2766 hr = IStream_Read(stream, signature, 8, &bytesread);
2767 if (FAILED(hr)) return hresult_to_status(hr);
2768 if (hr == S_FALSE || bytesread == 0) return GenericError;
2770 for (i = 0; i < NUM_CODECS; i++) {
2771 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
2772 bytesread >= codecs[i].info.SigSize)
2774 for (sig=0; sig<codecs[i].info.SigCount; sig++)
2776 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
2777 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
2778 for (j=0; j<codecs[i].info.SigSize; j++)
2779 if ((signature[j] & mask[j]) != pattern[j])
2780 break;
2781 if (j == codecs[i].info.SigSize)
2783 *result = &codecs[i];
2784 return Ok;
2790 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
2791 signature[0],signature[1],signature[2],signature[3],
2792 signature[4],signature[5],signature[6],signature[7]);
2794 return GenericError;
2797 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
2799 GpStatus stat;
2800 LARGE_INTEGER seek;
2801 HRESULT hr;
2802 const struct image_codec *codec=NULL;
2804 /* choose an appropriate image decoder */
2805 stat = get_decoder_info(stream, &codec);
2806 if (stat != Ok) return stat;
2808 /* seek to the start of the stream */
2809 seek.QuadPart = 0;
2810 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
2811 if (FAILED(hr)) return hresult_to_status(hr);
2813 /* call on the image decoder to do the real work */
2814 stat = codec->decode_func(stream, &codec->info.Clsid, image);
2816 /* take note of the original data format */
2817 if (stat == Ok)
2819 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
2822 return stat;
2825 /* FIXME: no ICM */
2826 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
2828 TRACE("%p %p\n", stream, image);
2830 return GdipLoadImageFromStream(stream, image);
2833 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
2835 static int calls;
2837 TRACE("(%p,%u)\n", image, propId);
2839 if(!image)
2840 return InvalidParameter;
2842 if(!(calls++))
2843 FIXME("not implemented\n");
2845 return NotImplemented;
2848 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
2850 static int calls;
2852 TRACE("(%p,%p)\n", image, item);
2854 if(!(calls++))
2855 FIXME("not implemented\n");
2857 return NotImplemented;
2860 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
2861 GDIPCONST CLSID *clsidEncoder,
2862 GDIPCONST EncoderParameters *encoderParams)
2864 GpStatus stat;
2865 IStream *stream;
2867 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
2869 if (!image || !filename|| !clsidEncoder)
2870 return InvalidParameter;
2872 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
2873 if (stat != Ok)
2874 return GenericError;
2876 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
2878 IStream_Release(stream);
2879 return stat;
2882 /*************************************************************************
2883 * Encoding functions -
2884 * These functions encode an image in different image file formats.
2886 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
2887 #define BITMAP_FORMAT_JPEG 0xd8ff
2888 #define BITMAP_FORMAT_GIF 0x4947
2889 #define BITMAP_FORMAT_PNG 0x5089
2890 #define BITMAP_FORMAT_APM 0xcdd7
2892 static GpStatus encode_image_WIC(GpImage *image, IStream* stream,
2893 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2895 GpStatus stat;
2896 GpBitmap *bitmap;
2897 IWICBitmapEncoder *encoder;
2898 IWICBitmapFrameEncode *frameencode;
2899 IPropertyBag2 *encoderoptions;
2900 HRESULT hr;
2901 UINT width, height;
2902 PixelFormat gdipformat=0;
2903 WICPixelFormatGUID wicformat;
2904 GpRect rc;
2905 BitmapData lockeddata;
2906 HRESULT initresult;
2907 UINT i;
2909 if (image->type != ImageTypeBitmap)
2910 return GenericError;
2912 bitmap = (GpBitmap*)image;
2914 GdipGetImageWidth(image, &width);
2915 GdipGetImageHeight(image, &height);
2917 rc.X = 0;
2918 rc.Y = 0;
2919 rc.Width = width;
2920 rc.Height = height;
2922 initresult = CoInitialize(NULL);
2924 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
2925 &IID_IWICBitmapEncoder, (void**)&encoder);
2926 if (FAILED(hr))
2928 if (SUCCEEDED(initresult)) CoUninitialize();
2929 return hresult_to_status(hr);
2932 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
2934 if (SUCCEEDED(hr))
2936 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
2939 if (SUCCEEDED(hr)) /* created frame */
2941 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
2943 if (SUCCEEDED(hr))
2944 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
2946 if (SUCCEEDED(hr))
2947 /* FIXME: use the resolution from the image */
2948 hr = IWICBitmapFrameEncode_SetResolution(frameencode, 96.0, 96.0);
2950 if (SUCCEEDED(hr))
2952 for (i=0; wic_pixel_formats[i]; i++)
2954 if (wic_gdip_formats[i] == bitmap->format)
2955 break;
2957 if (wic_pixel_formats[i])
2958 memcpy(&wicformat, wic_pixel_formats[i], sizeof(GUID));
2959 else
2960 memcpy(&wicformat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
2962 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
2964 for (i=0; wic_pixel_formats[i]; i++)
2966 if (IsEqualGUID(&wicformat, wic_pixel_formats[i]))
2967 break;
2969 if (wic_pixel_formats[i])
2970 gdipformat = wic_gdip_formats[i];
2971 else
2973 ERR("cannot provide pixel format %s\n", debugstr_guid(&wicformat));
2974 hr = E_FAIL;
2978 if (SUCCEEDED(hr))
2980 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
2981 &lockeddata);
2983 if (stat == Ok)
2985 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
2986 BYTE *row;
2988 /* write one row at a time in case stride is negative */
2989 row = lockeddata.Scan0;
2990 for (i=0; i<lockeddata.Height; i++)
2992 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
2993 if (FAILED(hr)) break;
2994 row += lockeddata.Stride;
2997 GdipBitmapUnlockBits(bitmap, &lockeddata);
2999 else
3000 hr = E_FAIL;
3003 if (SUCCEEDED(hr))
3004 hr = IWICBitmapFrameEncode_Commit(frameencode);
3006 IWICBitmapFrameEncode_Release(frameencode);
3007 IPropertyBag2_Release(encoderoptions);
3010 if (SUCCEEDED(hr))
3011 hr = IWICBitmapEncoder_Commit(encoder);
3013 IWICBitmapEncoder_Release(encoder);
3015 if (SUCCEEDED(initresult)) CoUninitialize();
3017 return hresult_to_status(hr);
3020 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
3021 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3023 return encode_image_WIC(image, stream, &CLSID_WICBmpEncoder, params);
3026 static GpStatus encode_image_png(GpImage *image, IStream* stream,
3027 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3029 return encode_image_WIC(image, stream, &CLSID_WICPngEncoder, params);
3032 /*****************************************************************************
3033 * GdipSaveImageToStream [GDIPLUS.@]
3035 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
3036 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
3038 GpStatus stat;
3039 encode_image_func encode_image;
3040 int i;
3042 TRACE("%p %p %p %p\n", image, stream, clsid, params);
3044 if(!image || !stream)
3045 return InvalidParameter;
3047 /* select correct encoder */
3048 encode_image = NULL;
3049 for (i = 0; i < NUM_CODECS; i++) {
3050 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
3051 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
3052 encode_image = codecs[i].encode_func;
3054 if (encode_image == NULL)
3055 return UnknownImageFormat;
3057 stat = encode_image(image, stream, clsid, params);
3059 return stat;
3062 /*****************************************************************************
3063 * GdipGetImagePalette [GDIPLUS.@]
3065 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
3067 TRACE("(%p,%p,%i)\n", image, palette, size);
3069 if (!image || !palette)
3070 return InvalidParameter;
3072 if (size < (sizeof(UINT)*2+sizeof(ARGB)*image->palette_count))
3074 TRACE("<-- InsufficientBuffer\n");
3075 return InsufficientBuffer;
3078 palette->Flags = image->palette_flags;
3079 palette->Count = image->palette_count;
3080 memcpy(palette->Entries, image->palette_entries, sizeof(ARGB)*image->palette_count);
3082 return Ok;
3085 /*****************************************************************************
3086 * GdipSetImagePalette [GDIPLUS.@]
3088 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
3089 GDIPCONST ColorPalette *palette)
3091 TRACE("(%p,%p)\n", image, palette);
3093 if(!image || !palette || palette->Count > 256)
3094 return InvalidParameter;
3096 if (palette->Count > image->palette_size)
3098 ARGB *new_palette;
3100 new_palette = GdipAlloc(sizeof(ARGB) * palette->Count);
3101 if (!new_palette) return OutOfMemory;
3103 GdipFree(image->palette_entries);
3104 image->palette_entries = new_palette;
3105 image->palette_size = palette->Count;
3108 image->palette_flags = palette->Flags;
3109 image->palette_count = palette->Count;
3110 memcpy(image->palette_entries, palette->Entries, sizeof(ARGB)*palette->Count);
3112 return Ok;
3115 /*************************************************************************
3116 * Encoders -
3117 * Structures that represent which formats we support for encoding.
3120 /* ImageCodecInfo creation routines taken from libgdiplus */
3121 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
3122 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
3123 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
3124 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
3125 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
3126 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
3128 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
3129 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
3130 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
3131 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
3132 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
3133 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
3135 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
3136 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
3137 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
3138 static const WCHAR gif_format[] = {'G','I','F',0};
3139 static const BYTE gif_sig_pattern[4] = "GIF8";
3140 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3142 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
3143 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
3144 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
3145 static const WCHAR tiff_format[] = {'T','I','F','F',0};
3146 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
3147 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3149 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
3150 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
3151 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
3152 static const WCHAR emf_format[] = {'E','M','F',0};
3153 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
3154 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3156 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
3157 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
3158 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
3159 static const WCHAR wmf_format[] = {'W','M','F',0};
3160 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
3161 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
3163 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
3164 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
3165 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
3166 static const WCHAR png_format[] = {'P','N','G',0};
3167 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
3168 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3170 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
3171 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
3172 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
3173 static const WCHAR ico_format[] = {'I','C','O',0};
3174 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
3175 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
3177 static const struct image_codec codecs[NUM_CODECS] = {
3179 { /* BMP */
3180 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3181 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3182 /* CodecName */ bmp_codecname,
3183 /* DllName */ NULL,
3184 /* FormatDescription */ bmp_format,
3185 /* FilenameExtension */ bmp_extension,
3186 /* MimeType */ bmp_mimetype,
3187 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3188 /* Version */ 1,
3189 /* SigCount */ 1,
3190 /* SigSize */ 2,
3191 /* SigPattern */ bmp_sig_pattern,
3192 /* SigMask */ bmp_sig_mask,
3194 encode_image_BMP,
3195 decode_image_bmp
3198 { /* JPEG */
3199 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3200 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3201 /* CodecName */ jpeg_codecname,
3202 /* DllName */ NULL,
3203 /* FormatDescription */ jpeg_format,
3204 /* FilenameExtension */ jpeg_extension,
3205 /* MimeType */ jpeg_mimetype,
3206 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3207 /* Version */ 1,
3208 /* SigCount */ 1,
3209 /* SigSize */ 2,
3210 /* SigPattern */ jpeg_sig_pattern,
3211 /* SigMask */ jpeg_sig_mask,
3213 NULL,
3214 decode_image_jpeg
3217 { /* GIF */
3218 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3219 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3220 /* CodecName */ gif_codecname,
3221 /* DllName */ NULL,
3222 /* FormatDescription */ gif_format,
3223 /* FilenameExtension */ gif_extension,
3224 /* MimeType */ gif_mimetype,
3225 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3226 /* Version */ 1,
3227 /* SigCount */ 1,
3228 /* SigSize */ 4,
3229 /* SigPattern */ gif_sig_pattern,
3230 /* SigMask */ gif_sig_mask,
3232 NULL,
3233 decode_image_gif
3236 { /* TIFF */
3237 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3238 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3239 /* CodecName */ tiff_codecname,
3240 /* DllName */ NULL,
3241 /* FormatDescription */ tiff_format,
3242 /* FilenameExtension */ tiff_extension,
3243 /* MimeType */ tiff_mimetype,
3244 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3245 /* Version */ 1,
3246 /* SigCount */ 2,
3247 /* SigSize */ 4,
3248 /* SigPattern */ tiff_sig_pattern,
3249 /* SigMask */ tiff_sig_mask,
3251 NULL,
3252 decode_image_tiff
3255 { /* EMF */
3256 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3257 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3258 /* CodecName */ emf_codecname,
3259 /* DllName */ NULL,
3260 /* FormatDescription */ emf_format,
3261 /* FilenameExtension */ emf_extension,
3262 /* MimeType */ emf_mimetype,
3263 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
3264 /* Version */ 1,
3265 /* SigCount */ 1,
3266 /* SigSize */ 4,
3267 /* SigPattern */ emf_sig_pattern,
3268 /* SigMask */ emf_sig_mask,
3270 NULL,
3271 decode_image_olepicture_metafile
3274 { /* WMF */
3275 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3276 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3277 /* CodecName */ wmf_codecname,
3278 /* DllName */ NULL,
3279 /* FormatDescription */ wmf_format,
3280 /* FilenameExtension */ wmf_extension,
3281 /* MimeType */ wmf_mimetype,
3282 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
3283 /* Version */ 1,
3284 /* SigCount */ 1,
3285 /* SigSize */ 2,
3286 /* SigPattern */ wmf_sig_pattern,
3287 /* SigMask */ wmf_sig_mask,
3289 NULL,
3290 decode_image_olepicture_metafile
3293 { /* PNG */
3294 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3295 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3296 /* CodecName */ png_codecname,
3297 /* DllName */ NULL,
3298 /* FormatDescription */ png_format,
3299 /* FilenameExtension */ png_extension,
3300 /* MimeType */ png_mimetype,
3301 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3302 /* Version */ 1,
3303 /* SigCount */ 1,
3304 /* SigSize */ 8,
3305 /* SigPattern */ png_sig_pattern,
3306 /* SigMask */ png_sig_mask,
3308 encode_image_png,
3309 decode_image_png
3312 { /* ICO */
3313 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3314 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3315 /* CodecName */ ico_codecname,
3316 /* DllName */ NULL,
3317 /* FormatDescription */ ico_format,
3318 /* FilenameExtension */ ico_extension,
3319 /* MimeType */ ico_mimetype,
3320 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3321 /* Version */ 1,
3322 /* SigCount */ 1,
3323 /* SigSize */ 4,
3324 /* SigPattern */ ico_sig_pattern,
3325 /* SigMask */ ico_sig_mask,
3327 NULL,
3328 decode_image_icon
3332 /*****************************************************************************
3333 * GdipGetImageDecodersSize [GDIPLUS.@]
3335 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
3337 int decoder_count=0;
3338 int i;
3339 TRACE("%p %p\n", numDecoders, size);
3341 if (!numDecoders || !size)
3342 return InvalidParameter;
3344 for (i=0; i<NUM_CODECS; i++)
3346 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
3347 decoder_count++;
3350 *numDecoders = decoder_count;
3351 *size = decoder_count * sizeof(ImageCodecInfo);
3353 return Ok;
3356 /*****************************************************************************
3357 * GdipGetImageDecoders [GDIPLUS.@]
3359 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
3361 int i, decoder_count=0;
3362 TRACE("%u %u %p\n", numDecoders, size, decoders);
3364 if (!decoders ||
3365 size != numDecoders * sizeof(ImageCodecInfo))
3366 return GenericError;
3368 for (i=0; i<NUM_CODECS; i++)
3370 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
3372 if (decoder_count == numDecoders) return GenericError;
3373 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
3374 decoder_count++;
3378 if (decoder_count < numDecoders) return GenericError;
3380 return Ok;
3383 /*****************************************************************************
3384 * GdipGetImageEncodersSize [GDIPLUS.@]
3386 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
3388 int encoder_count=0;
3389 int i;
3390 TRACE("%p %p\n", numEncoders, size);
3392 if (!numEncoders || !size)
3393 return InvalidParameter;
3395 for (i=0; i<NUM_CODECS; i++)
3397 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
3398 encoder_count++;
3401 *numEncoders = encoder_count;
3402 *size = encoder_count * sizeof(ImageCodecInfo);
3404 return Ok;
3407 /*****************************************************************************
3408 * GdipGetImageEncoders [GDIPLUS.@]
3410 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
3412 int i, encoder_count=0;
3413 TRACE("%u %u %p\n", numEncoders, size, encoders);
3415 if (!encoders ||
3416 size != numEncoders * sizeof(ImageCodecInfo))
3417 return GenericError;
3419 for (i=0; i<NUM_CODECS; i++)
3421 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
3423 if (encoder_count == numEncoders) return GenericError;
3424 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
3425 encoder_count++;
3429 if (encoder_count < numEncoders) return GenericError;
3431 return Ok;
3434 GpStatus WINGDIPAPI GdipGetEncoderParameterListSize(GpImage *image,
3435 GDIPCONST CLSID* clsidEncoder, UINT *size)
3437 static int calls;
3439 TRACE("(%p,%s,%p)\n", image, debugstr_guid(clsidEncoder), size);
3441 if(!(calls++))
3442 FIXME("not implemented\n");
3444 *size = 0;
3446 return NotImplemented;
3449 /*****************************************************************************
3450 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
3452 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
3454 BITMAP bm;
3455 GpStatus retval;
3456 PixelFormat format;
3457 BitmapData lockeddata;
3458 INT y;
3460 TRACE("%p %p %p\n", hbm, hpal, bitmap);
3462 if(!hbm || !bitmap)
3463 return InvalidParameter;
3465 /* TODO: Support for device-dependent bitmaps */
3466 if(hpal){
3467 FIXME("no support for device-dependent bitmaps\n");
3468 return NotImplemented;
3471 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
3472 return InvalidParameter;
3474 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
3475 switch(bm.bmBitsPixel) {
3476 case 1:
3477 format = PixelFormat1bppIndexed;
3478 break;
3479 case 4:
3480 format = PixelFormat4bppIndexed;
3481 break;
3482 case 8:
3483 format = PixelFormat8bppIndexed;
3484 break;
3485 case 24:
3486 format = PixelFormat24bppRGB;
3487 break;
3488 case 32:
3489 format = PixelFormat32bppRGB;
3490 break;
3491 case 48:
3492 format = PixelFormat48bppRGB;
3493 break;
3494 default:
3495 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
3496 return InvalidParameter;
3499 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
3500 format, NULL, bitmap);
3502 if (retval == Ok)
3504 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
3505 format, &lockeddata);
3506 if (retval == Ok)
3508 if (bm.bmBits)
3510 for (y=0; y<bm.bmHeight; y++)
3512 memcpy((BYTE*)lockeddata.Scan0+lockeddata.Stride*y,
3513 (BYTE*)bm.bmBits+bm.bmWidthBytes*(bm.bmHeight-1-y),
3514 bm.bmWidthBytes);
3517 else
3519 HDC hdc;
3520 HBITMAP oldhbm;
3521 BITMAPINFO *pbmi;
3522 INT src_height, dst_stride;
3523 BYTE *dst_bits;
3525 hdc = CreateCompatibleDC(NULL);
3526 oldhbm = SelectObject(hdc, hbm);
3528 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
3530 if (pbmi)
3532 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3533 pbmi->bmiHeader.biBitCount = 0;
3535 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
3537 src_height = abs(pbmi->bmiHeader.biHeight);
3539 if (pbmi->bmiHeader.biHeight > 0)
3541 dst_bits = (BYTE*)lockeddata.Scan0+lockeddata.Stride*(src_height-1);
3542 dst_stride = -lockeddata.Stride;
3544 else
3546 dst_bits = lockeddata.Scan0;
3547 dst_stride = lockeddata.Stride;
3550 for (y=0; y<src_height; y++)
3552 GetDIBits(hdc, hbm, y, 1, dst_bits+dst_stride*y,
3553 pbmi, DIB_RGB_COLORS);
3556 GdipFree(pbmi);
3558 else
3559 retval = OutOfMemory;
3561 SelectObject(hdc, oldhbm);
3562 DeleteDC(hdc);
3565 GdipBitmapUnlockBits(*bitmap, &lockeddata);
3569 return retval;
3572 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
3574 FIXME("(%p): stub\n", effect);
3575 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
3576 * in Windows's gdiplus */
3577 return NotImplemented;
3580 /*****************************************************************************
3581 * GdipSetEffectParameters [GDIPLUS.@]
3583 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
3584 const VOID *params, const UINT size)
3586 static int calls;
3588 TRACE("(%p,%p,%u)\n", effect, params, size);
3590 if(!(calls++))
3591 FIXME("not implemented\n");
3593 return NotImplemented;
3596 /*****************************************************************************
3597 * GdipGetImageFlags [GDIPLUS.@]
3599 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
3601 TRACE("%p %p\n", image, flags);
3603 if(!image || !flags)
3604 return InvalidParameter;
3606 *flags = image->flags;
3608 return Ok;
3611 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
3613 TRACE("(%d, %p)\n", control, param);
3615 switch(control){
3616 case TestControlForceBilinear:
3617 if(param)
3618 FIXME("TestControlForceBilinear not handled\n");
3619 break;
3620 case TestControlNoICM:
3621 if(param)
3622 FIXME("TestControlNoICM not handled\n");
3623 break;
3624 case TestControlGetBuildNumber:
3625 *((DWORD*)param) = 3102;
3626 break;
3629 return Ok;
3632 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
3633 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
3634 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
3635 GpMetafile **metafile)
3637 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
3638 frameUnit, debugstr_w(desc), metafile);
3640 return NotImplemented;
3643 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
3644 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
3645 GDIPCONST WCHAR *desc, GpMetafile **metafile)
3647 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
3648 frameUnit, debugstr_w(desc), metafile);
3650 return NotImplemented;
3653 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
3655 TRACE("%p\n", image);
3657 return Ok;
3660 /*****************************************************************************
3661 * GdipGetImageThumbnail [GDIPLUS.@]
3663 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
3664 GpImage **ret_image, GetThumbnailImageAbort cb,
3665 VOID * cb_data)
3667 GpStatus stat;
3668 GpGraphics *graphics;
3669 UINT srcwidth, srcheight;
3671 TRACE("(%p %u %u %p %p %p)\n",
3672 image, width, height, ret_image, cb, cb_data);
3674 if (!image || !ret_image)
3675 return InvalidParameter;
3677 if (!width) width = 120;
3678 if (!height) height = 120;
3680 GdipGetImageWidth(image, &srcwidth);
3681 GdipGetImageHeight(image, &srcheight);
3683 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB,
3684 NULL, (GpBitmap**)ret_image);
3686 if (stat == Ok)
3688 stat = GdipGetImageGraphicsContext(*ret_image, &graphics);
3690 if (stat == Ok)
3692 stat = GdipDrawImageRectRectI(graphics, image,
3693 0, 0, width, height, 0, 0, srcwidth, srcheight, UnitPixel,
3694 NULL, NULL, NULL);
3696 GdipDeleteGraphics(graphics);
3699 if (stat != Ok)
3701 GdipDisposeImage(*ret_image);
3702 *ret_image = NULL;
3706 return stat;
3709 /*****************************************************************************
3710 * GdipImageRotateFlip [GDIPLUS.@]
3712 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
3714 GpBitmap *new_bitmap;
3715 GpBitmap *bitmap;
3716 int bpp, bytesperpixel;
3717 int rotate_90, flip_x, flip_y;
3718 int src_x_offset, src_y_offset;
3719 LPBYTE src_origin;
3720 UINT x, y, width, height;
3721 BitmapData src_lock, dst_lock;
3722 GpStatus stat;
3724 TRACE("(%p, %u)\n", image, type);
3726 rotate_90 = type&1;
3727 flip_x = (type&6) == 2 || (type&6) == 4;
3728 flip_y = (type&3) == 1 || (type&3) == 2;
3730 if (image->type != ImageTypeBitmap)
3732 FIXME("Not implemented for type %i\n", image->type);
3733 return NotImplemented;
3736 bitmap = (GpBitmap*)image;
3737 bpp = PIXELFORMATBPP(bitmap->format);
3739 if (bpp < 8)
3741 FIXME("Not implemented for %i bit images\n", bpp);
3742 return NotImplemented;
3745 if (rotate_90)
3747 width = bitmap->height;
3748 height = bitmap->width;
3750 else
3752 width = bitmap->width;
3753 height = bitmap->height;
3756 bytesperpixel = bpp/8;
3758 stat = GdipCreateBitmapFromScan0(width, height, 0, bitmap->format, NULL, &new_bitmap);
3760 if (stat != Ok)
3761 return stat;
3763 stat = GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead, bitmap->format, &src_lock);
3765 if (stat == Ok)
3767 stat = GdipBitmapLockBits(new_bitmap, NULL, ImageLockModeWrite, bitmap->format, &dst_lock);
3769 if (stat == Ok)
3771 LPBYTE src_row, src_pixel;
3772 LPBYTE dst_row, dst_pixel;
3774 src_origin = src_lock.Scan0;
3775 if (flip_x) src_origin += bytesperpixel * (bitmap->width - 1);
3776 if (flip_y) src_origin += src_lock.Stride * (bitmap->height - 1);
3778 if (rotate_90)
3780 if (flip_y) src_x_offset = -src_lock.Stride;
3781 else src_x_offset = src_lock.Stride;
3782 if (flip_x) src_y_offset = -bytesperpixel;
3783 else src_y_offset = bytesperpixel;
3785 else
3787 if (flip_x) src_x_offset = -bytesperpixel;
3788 else src_x_offset = bytesperpixel;
3789 if (flip_y) src_y_offset = -src_lock.Stride;
3790 else src_y_offset = src_lock.Stride;
3793 src_row = src_origin;
3794 dst_row = dst_lock.Scan0;
3795 for (y=0; y<height; y++)
3797 src_pixel = src_row;
3798 dst_pixel = dst_row;
3799 for (x=0; x<width; x++)
3801 /* FIXME: This could probably be faster without memcpy. */
3802 memcpy(dst_pixel, src_pixel, bytesperpixel);
3803 dst_pixel += bytesperpixel;
3804 src_pixel += src_x_offset;
3806 src_row += src_y_offset;
3807 dst_row += dst_lock.Stride;
3810 GdipBitmapUnlockBits(new_bitmap, &dst_lock);
3813 GdipBitmapUnlockBits(bitmap, &src_lock);
3816 if (stat == Ok)
3817 move_bitmap(bitmap, new_bitmap, FALSE);
3818 else
3819 GdipDisposeImage((GpImage*)new_bitmap);
3821 return stat;
3824 /*****************************************************************************
3825 * GdipConvertToEmfPlusToFile [GDIPLUS.@]
3828 GpStatus WINGDIPAPI GdipConvertToEmfPlusToFile(const GpGraphics* refGraphics,
3829 GpMetafile* metafile, BOOL* conversionSuccess,
3830 const WCHAR* filename, EmfType emfType,
3831 const WCHAR* description, GpMetafile** out_metafile)
3833 FIXME("stub: %p, %p, %p, %p, %u, %p, %p\n", refGraphics, metafile, conversionSuccess, filename, emfType, description, out_metafile);
3834 return NotImplemented;