gdiplus: Fix the linesfilled calculation in GdipMeasureString.
[wine.git] / dlls / gdiplus / image.c
blob8736b238d2650c6057b2d943a19d0a4f3a2c8956
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 height = abs(info->bmiHeader.biHeight);
1224 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
1226 if(info->bmiHeader.biHeight > 0) /* bottom-up */
1228 bits = (BYTE*)bits + (height - 1) * stride;
1229 stride = -stride;
1232 switch(info->bmiHeader.biBitCount) {
1233 case 1:
1234 format = PixelFormat1bppIndexed;
1235 break;
1236 case 4:
1237 format = PixelFormat4bppIndexed;
1238 break;
1239 case 8:
1240 format = PixelFormat8bppIndexed;
1241 break;
1242 case 24:
1243 format = PixelFormat24bppRGB;
1244 break;
1245 default:
1246 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
1247 *bitmap = NULL;
1248 return InvalidParameter;
1251 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
1252 bits, bitmap);
1256 /* FIXME: no icm */
1257 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
1258 GpBitmap **bitmap)
1260 TRACE("(%s) %p\n", debugstr_w(filename), bitmap);
1262 return GdipCreateBitmapFromFile(filename, bitmap);
1265 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
1266 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
1268 HBITMAP hbm;
1269 GpStatus stat = InvalidParameter;
1271 TRACE("%p (%s) %p\n", hInstance, debugstr_w(lpBitmapName), bitmap);
1273 if(!lpBitmapName || !bitmap)
1274 return InvalidParameter;
1276 /* load DIB */
1277 hbm = LoadImageW(hInstance, lpBitmapName, IMAGE_BITMAP, 0, 0,
1278 LR_CREATEDIBSECTION);
1280 if(hbm){
1281 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
1282 DeleteObject(hbm);
1285 return stat;
1288 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
1289 HBITMAP* hbmReturn, ARGB background)
1291 GpStatus stat;
1292 HBITMAP result, oldbitmap;
1293 UINT width, height;
1294 HDC hdc;
1295 GpGraphics *graphics;
1296 BITMAPINFOHEADER bih;
1297 void *bits;
1298 TRACE("(%p,%p,%x)\n", bitmap, hbmReturn, background);
1300 if (!bitmap || !hbmReturn) return InvalidParameter;
1302 GdipGetImageWidth((GpImage*)bitmap, &width);
1303 GdipGetImageHeight((GpImage*)bitmap, &height);
1305 bih.biSize = sizeof(bih);
1306 bih.biWidth = width;
1307 bih.biHeight = height;
1308 bih.biPlanes = 1;
1309 bih.biBitCount = 32;
1310 bih.biCompression = BI_RGB;
1311 bih.biSizeImage = 0;
1312 bih.biXPelsPerMeter = 0;
1313 bih.biYPelsPerMeter = 0;
1314 bih.biClrUsed = 0;
1315 bih.biClrImportant = 0;
1317 hdc = CreateCompatibleDC(NULL);
1318 if (!hdc) return GenericError;
1320 result = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS, &bits,
1321 NULL, 0);
1323 if (result)
1325 oldbitmap = SelectObject(hdc, result);
1327 stat = GdipCreateFromHDC(hdc, &graphics);
1328 if (stat == Ok)
1330 stat = GdipGraphicsClear(graphics, background);
1332 if (stat == Ok)
1333 stat = GdipDrawImage(graphics, (GpImage*)bitmap, 0, 0);
1335 GdipDeleteGraphics(graphics);
1338 SelectObject(hdc, oldbitmap);
1340 else
1341 stat = GenericError;
1343 DeleteDC(hdc);
1345 if (stat != Ok && result)
1347 DeleteObject(result);
1348 result = NULL;
1351 *hbmReturn = result;
1353 return stat;
1356 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
1357 GpMetafile* metafile, BOOL* succ, EmfType emfType,
1358 const WCHAR* description, GpMetafile** out_metafile)
1360 static int calls;
1362 TRACE("(%p,%p,%p,%u,%s,%p)\n", ref, metafile, succ, emfType,
1363 debugstr_w(description), out_metafile);
1365 if(!ref || !metafile || !out_metafile)
1366 return InvalidParameter;
1368 *succ = FALSE;
1369 *out_metafile = NULL;
1371 if(!(calls++))
1372 FIXME("not implemented\n");
1374 return NotImplemented;
1377 /* FIXME: this should create a bitmap in the given size with the attributes
1378 * (resolution etc.) of the graphics object */
1379 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
1380 GpGraphics* target, GpBitmap** bitmap)
1382 static int calls;
1383 GpStatus ret;
1385 if(!target || !bitmap)
1386 return InvalidParameter;
1388 if(!(calls++))
1389 FIXME("hacked stub\n");
1391 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
1392 NULL, bitmap);
1394 return ret;
1397 GpStatus WINGDIPAPI GdipCreateBitmapFromHICON(HICON hicon, GpBitmap** bitmap)
1399 GpStatus stat;
1400 ICONINFO iinfo;
1401 BITMAP bm;
1402 int ret;
1403 UINT width, height;
1404 GpRect rect;
1405 BitmapData lockeddata;
1406 HDC screendc;
1407 BOOL has_alpha;
1408 int x, y;
1409 BYTE *bits;
1410 BITMAPINFOHEADER bih;
1411 DWORD *src;
1412 BYTE *dst_row;
1413 DWORD *dst;
1415 TRACE("%p, %p\n", hicon, bitmap);
1417 if(!bitmap || !GetIconInfo(hicon, &iinfo))
1418 return InvalidParameter;
1420 /* get the size of the icon */
1421 ret = GetObjectA(iinfo.hbmColor ? iinfo.hbmColor : iinfo.hbmMask, sizeof(bm), &bm);
1422 if (ret == 0) {
1423 DeleteObject(iinfo.hbmColor);
1424 DeleteObject(iinfo.hbmMask);
1425 return GenericError;
1428 width = bm.bmWidth;
1430 if (iinfo.hbmColor)
1431 height = abs(bm.bmHeight);
1432 else /* combined bitmap + mask */
1433 height = abs(bm.bmHeight) / 2;
1435 bits = HeapAlloc(GetProcessHeap(), 0, 4*width*height);
1436 if (!bits) {
1437 DeleteObject(iinfo.hbmColor);
1438 DeleteObject(iinfo.hbmMask);
1439 return OutOfMemory;
1442 stat = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat32bppARGB, NULL, bitmap);
1443 if (stat != Ok) {
1444 DeleteObject(iinfo.hbmColor);
1445 DeleteObject(iinfo.hbmMask);
1446 HeapFree(GetProcessHeap(), 0, bits);
1447 return stat;
1450 rect.X = 0;
1451 rect.Y = 0;
1452 rect.Width = width;
1453 rect.Height = height;
1455 stat = GdipBitmapLockBits(*bitmap, &rect, ImageLockModeWrite, PixelFormat32bppARGB, &lockeddata);
1456 if (stat != Ok) {
1457 DeleteObject(iinfo.hbmColor);
1458 DeleteObject(iinfo.hbmMask);
1459 HeapFree(GetProcessHeap(), 0, bits);
1460 GdipDisposeImage((GpImage*)*bitmap);
1461 return stat;
1464 bih.biSize = sizeof(bih);
1465 bih.biWidth = width;
1466 bih.biHeight = -height;
1467 bih.biPlanes = 1;
1468 bih.biBitCount = 32;
1469 bih.biCompression = BI_RGB;
1470 bih.biSizeImage = 0;
1471 bih.biXPelsPerMeter = 0;
1472 bih.biYPelsPerMeter = 0;
1473 bih.biClrUsed = 0;
1474 bih.biClrImportant = 0;
1476 screendc = GetDC(0);
1477 if (iinfo.hbmColor)
1479 GetDIBits(screendc, iinfo.hbmColor, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1481 if (bm.bmBitsPixel == 32)
1483 has_alpha = FALSE;
1485 /* If any pixel has a non-zero alpha, ignore hbmMask */
1486 src = (DWORD*)bits;
1487 for (x=0; x<width && !has_alpha; x++)
1488 for (y=0; y<height && !has_alpha; y++)
1489 if ((*src++ & 0xff000000) != 0)
1490 has_alpha = TRUE;
1492 else has_alpha = FALSE;
1494 else
1496 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1497 has_alpha = FALSE;
1500 /* copy the image data to the Bitmap */
1501 src = (DWORD*)bits;
1502 dst_row = lockeddata.Scan0;
1503 for (y=0; y<height; y++)
1505 memcpy(dst_row, src, width*4);
1506 src += width;
1507 dst_row += lockeddata.Stride;
1510 if (!has_alpha)
1512 if (iinfo.hbmMask)
1514 /* read alpha data from the mask */
1515 if (iinfo.hbmColor)
1516 GetDIBits(screendc, iinfo.hbmMask, 0, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1517 else
1518 GetDIBits(screendc, iinfo.hbmMask, height, height, bits, (BITMAPINFO*)&bih, DIB_RGB_COLORS);
1520 src = (DWORD*)bits;
1521 dst_row = lockeddata.Scan0;
1522 for (y=0; y<height; y++)
1524 dst = (DWORD*)dst_row;
1525 for (x=0; x<height; x++)
1527 DWORD src_value = *src++;
1528 if (src_value)
1529 *dst++ = 0;
1530 else
1531 *dst++ |= 0xff000000;
1533 dst_row += lockeddata.Stride;
1536 else
1538 /* set constant alpha of 255 */
1539 dst_row = bits;
1540 for (y=0; y<height; y++)
1542 dst = (DWORD*)dst_row;
1543 for (x=0; x<height; x++)
1544 *dst++ |= 0xff000000;
1545 dst_row += lockeddata.Stride;
1550 ReleaseDC(0, screendc);
1552 DeleteObject(iinfo.hbmColor);
1553 DeleteObject(iinfo.hbmMask);
1555 GdipBitmapUnlockBits(*bitmap, &lockeddata);
1557 HeapFree(GetProcessHeap(), 0, bits);
1559 return Ok;
1562 static void generate_halftone_palette(ARGB *entries, UINT count)
1564 static const BYTE halftone_values[6]={0x00,0x33,0x66,0x99,0xcc,0xff};
1565 UINT i;
1567 for (i=0; i<8 && i<count; i++)
1569 entries[i] = 0xff000000;
1570 if (i&1) entries[i] |= 0x800000;
1571 if (i&2) entries[i] |= 0x8000;
1572 if (i&4) entries[i] |= 0x80;
1575 if (8 < count)
1576 entries[i] = 0xffc0c0c0;
1578 for (i=9; i<16 && i<count; i++)
1580 entries[i] = 0xff000000;
1581 if (i&1) entries[i] |= 0xff0000;
1582 if (i&2) entries[i] |= 0xff00;
1583 if (i&4) entries[i] |= 0xff;
1586 for (i=16; i<40 && i<count; i++)
1588 entries[i] = 0;
1591 for (i=40; i<256 && i<count; i++)
1593 entries[i] = 0xff000000;
1594 entries[i] |= halftone_values[(i-40)%6];
1595 entries[i] |= halftone_values[((i-40)/6)%6] << 8;
1596 entries[i] |= halftone_values[((i-40)/36)%6] << 16;
1600 static GpStatus get_screen_resolution(REAL *xres, REAL *yres)
1602 HDC screendc = GetDC(0);
1604 if (!screendc) return GenericError;
1606 *xres = (REAL)GetDeviceCaps(screendc, LOGPIXELSX);
1607 *yres = (REAL)GetDeviceCaps(screendc, LOGPIXELSY);
1609 ReleaseDC(0, screendc);
1611 return Ok;
1614 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
1615 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
1617 BITMAPINFO* pbmi;
1618 HBITMAP hbitmap;
1619 INT row_size, dib_stride;
1620 HDC hdc;
1621 BYTE *bits;
1622 int i;
1623 REAL xres, yres;
1624 GpStatus stat;
1626 TRACE("%d %d %d 0x%x %p %p\n", width, height, stride, format, scan0, bitmap);
1628 if (!bitmap) return InvalidParameter;
1630 if(width <= 0 || height <= 0 || (scan0 && (stride % 4))){
1631 *bitmap = NULL;
1632 return InvalidParameter;
1635 if(scan0 && !stride)
1636 return InvalidParameter;
1638 stat = get_screen_resolution(&xres, &yres);
1639 if (stat != Ok) return stat;
1641 row_size = (width * PIXELFORMATBPP(format)+7) / 8;
1642 dib_stride = (row_size + 3) & ~3;
1644 if(stride == 0)
1645 stride = dib_stride;
1647 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
1648 if (!pbmi)
1649 return OutOfMemory;
1651 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1652 pbmi->bmiHeader.biWidth = width;
1653 pbmi->bmiHeader.biHeight = -height;
1654 pbmi->bmiHeader.biPlanes = 1;
1655 /* FIXME: use the rest of the data from format */
1656 pbmi->bmiHeader.biBitCount = PIXELFORMATBPP(format);
1657 pbmi->bmiHeader.biCompression = BI_RGB;
1658 pbmi->bmiHeader.biSizeImage = 0;
1659 pbmi->bmiHeader.biXPelsPerMeter = 0;
1660 pbmi->bmiHeader.biYPelsPerMeter = 0;
1661 pbmi->bmiHeader.biClrUsed = 0;
1662 pbmi->bmiHeader.biClrImportant = 0;
1664 hdc = CreateCompatibleDC(NULL);
1665 if (!hdc) {
1666 GdipFree(pbmi);
1667 return GenericError;
1670 hbitmap = CreateDIBSection(hdc, pbmi, DIB_RGB_COLORS, (void**)&bits, NULL, 0);
1672 DeleteDC(hdc);
1673 GdipFree(pbmi);
1675 if (!hbitmap) return GenericError;
1677 /* copy bits to the dib if necessary */
1678 /* FIXME: should reference the bits instead of copying them */
1679 if (scan0)
1680 for (i=0; i<height; i++)
1681 memcpy(bits+i*dib_stride, scan0+i*stride, row_size);
1683 *bitmap = GdipAlloc(sizeof(GpBitmap));
1684 if(!*bitmap)
1686 DeleteObject(hbitmap);
1687 return OutOfMemory;
1690 (*bitmap)->image.type = ImageTypeBitmap;
1691 memcpy(&(*bitmap)->image.format, &ImageFormatMemoryBMP, sizeof(GUID));
1692 (*bitmap)->image.flags = ImageFlagsNone;
1693 (*bitmap)->image.palette_flags = 0;
1694 (*bitmap)->image.palette_count = 0;
1695 (*bitmap)->image.palette_size = 0;
1696 (*bitmap)->image.palette_entries = NULL;
1697 (*bitmap)->image.xres = xres;
1698 (*bitmap)->image.yres = yres;
1699 (*bitmap)->width = width;
1700 (*bitmap)->height = height;
1701 (*bitmap)->format = format;
1702 (*bitmap)->image.picture = NULL;
1703 (*bitmap)->hbitmap = hbitmap;
1704 (*bitmap)->hdc = NULL;
1705 (*bitmap)->bits = bits;
1706 (*bitmap)->stride = dib_stride;
1708 if (format == PixelFormat1bppIndexed ||
1709 format == PixelFormat4bppIndexed ||
1710 format == PixelFormat8bppIndexed)
1712 (*bitmap)->image.palette_size = (*bitmap)->image.palette_count = 1 << PIXELFORMATBPP(format);
1713 (*bitmap)->image.palette_entries = GdipAlloc(sizeof(ARGB) * ((*bitmap)->image.palette_size));
1715 if (!(*bitmap)->image.palette_entries)
1717 GdipDisposeImage(&(*bitmap)->image);
1718 *bitmap = NULL;
1719 return OutOfMemory;
1722 if (format == PixelFormat1bppIndexed)
1724 (*bitmap)->image.palette_flags = PaletteFlagsGrayScale;
1725 (*bitmap)->image.palette_entries[0] = 0xff000000;
1726 (*bitmap)->image.palette_entries[1] = 0xffffffff;
1728 else
1730 if (format == PixelFormat8bppIndexed)
1731 (*bitmap)->image.palette_flags = PaletteFlagsHalftone;
1733 generate_halftone_palette((*bitmap)->image.palette_entries,
1734 (*bitmap)->image.palette_count);
1738 TRACE("<-- %p\n", *bitmap);
1740 return Ok;
1743 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
1744 GpBitmap **bitmap)
1746 GpStatus stat;
1748 TRACE("%p %p\n", stream, bitmap);
1750 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
1752 if(stat != Ok)
1753 return stat;
1755 if((*bitmap)->image.type != ImageTypeBitmap){
1756 GdipDisposeImage(&(*bitmap)->image);
1757 *bitmap = NULL;
1758 return GenericError; /* FIXME: what error to return? */
1761 return Ok;
1764 /* FIXME: no icm */
1765 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
1766 GpBitmap **bitmap)
1768 TRACE("%p %p\n", stream, bitmap);
1770 return GdipCreateBitmapFromStream(stream, bitmap);
1773 GpStatus WINGDIPAPI GdipCreateCachedBitmap(GpBitmap *bitmap, GpGraphics *graphics,
1774 GpCachedBitmap **cachedbmp)
1776 GpStatus stat;
1778 TRACE("%p %p %p\n", bitmap, graphics, cachedbmp);
1780 if(!bitmap || !graphics || !cachedbmp)
1781 return InvalidParameter;
1783 *cachedbmp = GdipAlloc(sizeof(GpCachedBitmap));
1784 if(!*cachedbmp)
1785 return OutOfMemory;
1787 stat = GdipCloneImage(&(bitmap->image), &(*cachedbmp)->image);
1788 if(stat != Ok){
1789 GdipFree(*cachedbmp);
1790 return stat;
1793 return Ok;
1796 GpStatus WINGDIPAPI GdipCreateHICONFromBitmap(GpBitmap *bitmap, HICON *hicon)
1798 FIXME("(%p, %p)\n", bitmap, hicon);
1800 return NotImplemented;
1803 GpStatus WINGDIPAPI GdipDeleteCachedBitmap(GpCachedBitmap *cachedbmp)
1805 TRACE("%p\n", cachedbmp);
1807 if(!cachedbmp)
1808 return InvalidParameter;
1810 GdipDisposeImage(cachedbmp->image);
1811 GdipFree(cachedbmp);
1813 return Ok;
1816 GpStatus WINGDIPAPI GdipDrawCachedBitmap(GpGraphics *graphics,
1817 GpCachedBitmap *cachedbmp, INT x, INT y)
1819 TRACE("%p %p %d %d\n", graphics, cachedbmp, x, y);
1821 if(!graphics || !cachedbmp)
1822 return InvalidParameter;
1824 return GdipDrawImage(graphics, cachedbmp->image, (REAL)x, (REAL)y);
1827 GpStatus WINGDIPAPI GdipEmfToWmfBits(HENHMETAFILE hemf, UINT cbData16,
1828 LPBYTE pData16, INT iMapMode, INT eFlags)
1830 FIXME("(%p, %d, %p, %d, %d): stub\n", hemf, cbData16, pData16, iMapMode, eFlags);
1831 return NotImplemented;
1834 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
1836 TRACE("%p\n", image);
1838 if(!image)
1839 return InvalidParameter;
1841 if (image->picture)
1842 IPicture_Release(image->picture);
1843 if (image->type == ImageTypeBitmap)
1845 GdipFree(((GpBitmap*)image)->bitmapbits);
1846 DeleteDC(((GpBitmap*)image)->hdc);
1847 DeleteObject(((GpBitmap*)image)->hbitmap);
1849 GdipFree(image->palette_entries);
1850 GdipFree(image);
1852 return Ok;
1855 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
1857 static int calls;
1859 TRACE("(%p,%p)\n", image, item);
1861 if(!image || !item)
1862 return InvalidParameter;
1864 if (!(calls++))
1865 FIXME("not implemented\n");
1867 return NotImplemented;
1870 GpStatus WINGDIPAPI GdipGetImageItemData(GpImage *image, ImageItemData *item)
1872 static int calls;
1874 TRACE("(%p,%p)\n", image, item);
1876 if (!(calls++))
1877 FIXME("not implemented\n");
1879 return NotImplemented;
1882 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
1883 GpUnit *srcUnit)
1885 TRACE("%p %p %p\n", image, srcRect, srcUnit);
1887 if(!image || !srcRect || !srcUnit)
1888 return InvalidParameter;
1889 if(image->type == ImageTypeMetafile){
1890 *srcRect = ((GpMetafile*)image)->bounds;
1891 *srcUnit = ((GpMetafile*)image)->unit;
1893 else if(image->type == ImageTypeBitmap){
1894 srcRect->X = srcRect->Y = 0.0;
1895 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
1896 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
1897 *srcUnit = UnitPixel;
1899 else{
1900 srcRect->X = srcRect->Y = 0.0;
1901 srcRect->Width = ipicture_pixel_width(image->picture);
1902 srcRect->Height = ipicture_pixel_height(image->picture);
1903 *srcUnit = UnitPixel;
1906 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
1907 srcRect->Width, srcRect->Height, *srcUnit);
1909 return Ok;
1912 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
1913 REAL *height)
1915 TRACE("%p %p %p\n", image, width, height);
1917 if(!image || !height || !width)
1918 return InvalidParameter;
1920 if(image->type == ImageTypeMetafile){
1921 HDC hdc = GetDC(0);
1923 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
1924 ((GpMetafile*)image)->bounds.Height;
1926 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
1927 ((GpMetafile*)image)->bounds.Width;
1929 ReleaseDC(0, hdc);
1932 else if(image->type == ImageTypeBitmap){
1933 *height = ((GpBitmap*)image)->height;
1934 *width = ((GpBitmap*)image)->width;
1936 else{
1937 *height = ipicture_pixel_height(image->picture);
1938 *width = ipicture_pixel_width(image->picture);
1941 TRACE("returning (%f, %f)\n", *height, *width);
1942 return Ok;
1945 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
1946 GpGraphics **graphics)
1948 HDC hdc;
1949 GpStatus stat;
1951 TRACE("%p %p\n", image, graphics);
1953 if(!image || !graphics)
1954 return InvalidParameter;
1956 if(image->type != ImageTypeBitmap){
1957 FIXME("not implemented for image type %d\n", image->type);
1958 return NotImplemented;
1961 hdc = ((GpBitmap*)image)->hdc;
1963 if(!hdc){
1964 hdc = CreateCompatibleDC(0);
1965 SelectObject(hdc, ((GpBitmap*)image)->hbitmap);
1966 ((GpBitmap*)image)->hdc = hdc;
1969 stat = GdipCreateFromHDC(hdc, graphics);
1971 if (stat == Ok)
1972 (*graphics)->image = image;
1974 return stat;
1977 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
1979 TRACE("%p %p\n", image, height);
1981 if(!image || !height)
1982 return InvalidParameter;
1984 if(image->type == ImageTypeMetafile){
1985 HDC hdc = GetDC(0);
1987 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
1988 ((GpMetafile*)image)->bounds.Height);
1990 ReleaseDC(0, hdc);
1992 else if(image->type == ImageTypeBitmap)
1993 *height = ((GpBitmap*)image)->height;
1994 else
1995 *height = ipicture_pixel_height(image->picture);
1997 TRACE("returning %d\n", *height);
1999 return Ok;
2002 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
2004 if(!image || !res)
2005 return InvalidParameter;
2007 *res = image->xres;
2009 TRACE("(%p) <-- %0.2f\n", image, *res);
2011 return Ok;
2014 GpStatus WINGDIPAPI GdipGetImagePaletteSize(GpImage *image, INT *size)
2016 TRACE("%p %p\n", image, size);
2018 if(!image || !size)
2019 return InvalidParameter;
2021 if (image->palette_count == 0)
2022 *size = sizeof(ColorPalette);
2023 else
2024 *size = sizeof(UINT)*2 + sizeof(ARGB)*image->palette_count;
2026 TRACE("<-- %u\n", *size);
2028 return Ok;
2031 /* FIXME: test this function for non-bitmap types */
2032 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
2034 TRACE("%p %p\n", image, format);
2036 if(!image || !format)
2037 return InvalidParameter;
2039 if(image->type != ImageTypeBitmap)
2040 *format = PixelFormat24bppRGB;
2041 else
2042 *format = ((GpBitmap*) image)->format;
2044 return Ok;
2047 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
2049 if(!image || !format)
2050 return InvalidParameter;
2052 memcpy(format, &image->format, sizeof(GUID));
2054 return Ok;
2057 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
2059 TRACE("%p %p\n", image, type);
2061 if(!image || !type)
2062 return InvalidParameter;
2064 *type = image->type;
2066 return Ok;
2069 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
2071 if(!image || !res)
2072 return InvalidParameter;
2074 *res = image->yres;
2076 TRACE("(%p) <-- %0.2f\n", image, *res);
2078 return Ok;
2081 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
2083 TRACE("%p %p\n", image, width);
2085 if(!image || !width)
2086 return InvalidParameter;
2088 if(image->type == ImageTypeMetafile){
2089 HDC hdc = GetDC(0);
2091 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
2092 ((GpMetafile*)image)->bounds.Width);
2094 ReleaseDC(0, hdc);
2096 else if(image->type == ImageTypeBitmap)
2097 *width = ((GpBitmap*)image)->width;
2098 else
2099 *width = ipicture_pixel_width(image->picture);
2101 TRACE("returning %d\n", *width);
2103 return Ok;
2106 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
2107 MetafileHeader * header)
2109 static int calls;
2111 if(!metafile || !header)
2112 return InvalidParameter;
2114 if(!(calls++))
2115 FIXME("not implemented\n");
2117 memset(header, 0, sizeof(MetafileHeader));
2119 return Ok;
2122 GpStatus WINGDIPAPI GdipGetAllPropertyItems(GpImage *image, UINT size,
2123 UINT num, PropertyItem* items)
2125 static int calls;
2127 if(!(calls++))
2128 FIXME("not implemented\n");
2130 return InvalidParameter;
2133 GpStatus WINGDIPAPI GdipGetPropertyCount(GpImage *image, UINT* num)
2135 static int calls;
2137 if(!(calls++))
2138 FIXME("not implemented\n");
2140 return InvalidParameter;
2143 GpStatus WINGDIPAPI GdipGetPropertyIdList(GpImage *image, UINT num, PROPID* list)
2145 static int calls;
2147 if(!(calls++))
2148 FIXME("not implemented\n");
2150 return InvalidParameter;
2153 GpStatus WINGDIPAPI GdipGetPropertyItem(GpImage *image, PROPID id, UINT size,
2154 PropertyItem* buffer)
2156 static int calls;
2158 if(!(calls++))
2159 FIXME("not implemented\n");
2161 return InvalidParameter;
2164 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
2165 UINT* size)
2167 static int calls;
2169 TRACE("%p %x %p\n", image, pid, size);
2171 if(!size || !image)
2172 return InvalidParameter;
2174 if(!(calls++))
2175 FIXME("not implemented\n");
2177 return NotImplemented;
2180 GpStatus WINGDIPAPI GdipGetPropertySize(GpImage *image, UINT* size, UINT* num)
2182 static int calls;
2184 TRACE("(%p,%p,%p)\n", image, size, num);
2186 if(!(calls++))
2187 FIXME("not implemented\n");
2189 return InvalidParameter;
2192 struct image_format_dimension
2194 const GUID *format;
2195 const GUID *dimension;
2198 struct image_format_dimension image_format_dimensions[] =
2200 {&ImageFormatGIF, &FrameDimensionTime},
2201 {&ImageFormatIcon, &FrameDimensionResolution},
2202 {NULL}
2205 /* FIXME: Need to handle multi-framed images */
2206 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
2207 GDIPCONST GUID* dimensionID, UINT* count)
2209 static int calls;
2211 TRACE("(%p,%s,%p)\n", image, debugstr_guid(dimensionID), count);
2213 if(!image || !count)
2214 return InvalidParameter;
2216 if(!(calls++))
2217 FIXME("returning frame count of 1\n");
2219 *count = 1;
2221 return Ok;
2224 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
2225 UINT* count)
2227 /* Native gdiplus 1.1 does not yet support multiple frame dimensions. */
2229 if(!image || !count)
2230 return InvalidParameter;
2232 *count = 1;
2234 return Ok;
2237 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
2238 GUID* dimensionIDs, UINT count)
2240 int i;
2241 const GUID *result=NULL;
2243 TRACE("(%p,%p,%u)\n", image, dimensionIDs, count);
2245 if(!image || !dimensionIDs || count != 1)
2246 return InvalidParameter;
2248 for (i=0; image_format_dimensions[i].format; i++)
2250 if (IsEqualGUID(&image->format, image_format_dimensions[i].format))
2252 result = image_format_dimensions[i].dimension;
2253 break;
2257 if (!result)
2258 result = &FrameDimensionPage;
2260 memcpy(dimensionIDs, result, sizeof(GUID));
2262 return Ok;
2265 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
2266 GDIPCONST GUID* dimensionID, UINT frameidx)
2268 static int calls;
2270 if(!image || !dimensionID)
2271 return InvalidParameter;
2273 if(!(calls++))
2274 FIXME("not implemented\n");
2276 return Ok;
2279 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
2280 GpImage **image)
2282 GpStatus stat;
2283 IStream *stream;
2285 TRACE("(%s) %p\n", debugstr_w(filename), image);
2287 if (!filename || !image)
2288 return InvalidParameter;
2290 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
2292 if (stat != Ok)
2293 return stat;
2295 stat = GdipLoadImageFromStream(stream, image);
2297 IStream_Release(stream);
2299 return stat;
2302 /* FIXME: no icm handling */
2303 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
2305 TRACE("(%s) %p\n", debugstr_w(filename), image);
2307 return GdipLoadImageFromFile(filename, image);
2310 static const WICPixelFormatGUID *wic_pixel_formats[] = {
2311 &GUID_WICPixelFormat16bppBGR555,
2312 &GUID_WICPixelFormat24bppBGR,
2313 &GUID_WICPixelFormat32bppBGR,
2314 &GUID_WICPixelFormat32bppBGRA,
2315 &GUID_WICPixelFormat32bppPBGRA,
2316 NULL
2319 static const PixelFormat wic_gdip_formats[] = {
2320 PixelFormat16bppRGB555,
2321 PixelFormat24bppRGB,
2322 PixelFormat32bppRGB,
2323 PixelFormat32bppARGB,
2324 PixelFormat32bppPARGB,
2327 static GpStatus decode_image_wic(IStream* stream, REFCLSID clsid, GpImage **image)
2329 GpStatus status=Ok;
2330 GpBitmap *bitmap;
2331 HRESULT hr;
2332 IWICBitmapDecoder *decoder;
2333 IWICBitmapFrameDecode *frame;
2334 IWICBitmapSource *source=NULL;
2335 WICPixelFormatGUID wic_format;
2336 PixelFormat gdip_format=0;
2337 int i;
2338 UINT width, height;
2339 BitmapData lockeddata;
2340 WICRect wrc;
2341 HRESULT initresult;
2343 initresult = CoInitialize(NULL);
2345 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
2346 &IID_IWICBitmapDecoder, (void**)&decoder);
2347 if (FAILED(hr)) goto end;
2349 hr = IWICBitmapDecoder_Initialize(decoder, (IStream*)stream, WICDecodeMetadataCacheOnLoad);
2350 if (SUCCEEDED(hr))
2351 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
2353 if (SUCCEEDED(hr)) /* got frame */
2355 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &wic_format);
2357 if (SUCCEEDED(hr))
2359 for (i=0; wic_pixel_formats[i]; i++)
2361 if (IsEqualGUID(&wic_format, wic_pixel_formats[i]))
2363 source = (IWICBitmapSource*)frame;
2364 IWICBitmapSource_AddRef(source);
2365 gdip_format = wic_gdip_formats[i];
2366 break;
2369 if (!source)
2371 /* unknown format; fall back on 32bppARGB */
2372 hr = WICConvertBitmapSource(&GUID_WICPixelFormat32bppBGRA, (IWICBitmapSource*)frame, &source);
2373 gdip_format = PixelFormat32bppARGB;
2377 if (SUCCEEDED(hr)) /* got source */
2379 hr = IWICBitmapSource_GetSize(source, &width, &height);
2381 if (SUCCEEDED(hr))
2382 status = GdipCreateBitmapFromScan0(width, height, 0, gdip_format,
2383 NULL, &bitmap);
2385 if (SUCCEEDED(hr) && status == Ok) /* created bitmap */
2387 status = GdipBitmapLockBits(bitmap, NULL, ImageLockModeWrite,
2388 gdip_format, &lockeddata);
2389 if (status == Ok) /* locked bitmap */
2391 wrc.X = 0;
2392 wrc.Width = width;
2393 wrc.Height = 1;
2394 for (i=0; i<height; i++)
2396 wrc.Y = i;
2397 hr = IWICBitmapSource_CopyPixels(source, &wrc, abs(lockeddata.Stride),
2398 abs(lockeddata.Stride), (BYTE*)lockeddata.Scan0+lockeddata.Stride*i);
2399 if (FAILED(hr)) break;
2402 GdipBitmapUnlockBits(bitmap, &lockeddata);
2405 if (SUCCEEDED(hr) && status == Ok)
2406 *image = (GpImage*)bitmap;
2407 else
2409 *image = NULL;
2410 GdipDisposeImage((GpImage*)bitmap);
2414 IWICBitmapSource_Release(source);
2417 IWICBitmapFrameDecode_Release(frame);
2420 IWICBitmapDecoder_Release(decoder);
2422 end:
2423 if (SUCCEEDED(initresult)) CoUninitialize();
2425 if (FAILED(hr) && status == Ok) status = hresult_to_status(hr);
2427 return status;
2430 static GpStatus decode_image_icon(IStream* stream, REFCLSID clsid, GpImage **image)
2432 return decode_image_wic(stream, &CLSID_WICIcoDecoder, image);
2435 static GpStatus decode_image_bmp(IStream* stream, REFCLSID clsid, GpImage **image)
2437 GpStatus status;
2438 GpBitmap* bitmap;
2440 status = decode_image_wic(stream, &CLSID_WICBmpDecoder, image);
2442 bitmap = (GpBitmap*)*image;
2444 if (status == Ok && bitmap->format == PixelFormat32bppARGB)
2446 /* WIC supports bmp files with alpha, but gdiplus does not */
2447 bitmap->format = PixelFormat32bppRGB;
2450 return status;
2453 static GpStatus decode_image_jpeg(IStream* stream, REFCLSID clsid, GpImage **image)
2455 return decode_image_wic(stream, &CLSID_WICJpegDecoder, image);
2458 static GpStatus decode_image_png(IStream* stream, REFCLSID clsid, GpImage **image)
2460 return decode_image_wic(stream, &CLSID_WICPngDecoder, image);
2463 static GpStatus decode_image_gif(IStream* stream, REFCLSID clsid, GpImage **image)
2465 return decode_image_wic(stream, &CLSID_WICGifDecoder, image);
2468 static GpStatus decode_image_tiff(IStream* stream, REFCLSID clsid, GpImage **image)
2470 return decode_image_wic(stream, &CLSID_WICTiffDecoder, image);
2473 static GpStatus decode_image_olepicture_metafile(IStream* stream, REFCLSID clsid, GpImage **image)
2475 IPicture *pic;
2477 TRACE("%p %p\n", stream, image);
2479 if(!stream || !image)
2480 return InvalidParameter;
2482 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
2483 (LPVOID*) &pic) != S_OK){
2484 TRACE("Could not load picture\n");
2485 return GenericError;
2488 /* FIXME: missing initialization code */
2489 *image = GdipAlloc(sizeof(GpMetafile));
2490 if(!*image) return OutOfMemory;
2491 (*image)->type = ImageTypeMetafile;
2492 (*image)->picture = pic;
2493 (*image)->flags = ImageFlagsNone;
2494 (*image)->palette_flags = 0;
2495 (*image)->palette_count = 0;
2496 (*image)->palette_size = 0;
2497 (*image)->palette_entries = NULL;
2499 TRACE("<-- %p\n", *image);
2501 return Ok;
2504 typedef GpStatus (*encode_image_func)(GpImage *image, IStream* stream,
2505 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params);
2507 typedef GpStatus (*decode_image_func)(IStream *stream, REFCLSID clsid, GpImage** image);
2509 typedef struct image_codec {
2510 ImageCodecInfo info;
2511 encode_image_func encode_func;
2512 decode_image_func decode_func;
2513 } image_codec;
2515 typedef enum {
2516 BMP,
2517 JPEG,
2518 GIF,
2519 TIFF,
2520 EMF,
2521 WMF,
2522 PNG,
2523 ICO,
2524 NUM_CODECS
2525 } ImageFormat;
2527 static const struct image_codec codecs[NUM_CODECS];
2529 static GpStatus get_decoder_info(IStream* stream, const struct image_codec **result)
2531 BYTE signature[8];
2532 const BYTE *pattern, *mask;
2533 LARGE_INTEGER seek;
2534 HRESULT hr;
2535 UINT bytesread;
2536 int i, j, sig;
2538 /* seek to the start of the stream */
2539 seek.QuadPart = 0;
2540 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
2541 if (FAILED(hr)) return hresult_to_status(hr);
2543 /* read the first 8 bytes */
2544 /* FIXME: This assumes all codecs have signatures <= 8 bytes in length */
2545 hr = IStream_Read(stream, signature, 8, &bytesread);
2546 if (FAILED(hr)) return hresult_to_status(hr);
2547 if (hr == S_FALSE || bytesread == 0) return GenericError;
2549 for (i = 0; i < NUM_CODECS; i++) {
2550 if ((codecs[i].info.Flags & ImageCodecFlagsDecoder) &&
2551 bytesread >= codecs[i].info.SigSize)
2553 for (sig=0; sig<codecs[i].info.SigCount; sig++)
2555 pattern = &codecs[i].info.SigPattern[codecs[i].info.SigSize*sig];
2556 mask = &codecs[i].info.SigMask[codecs[i].info.SigSize*sig];
2557 for (j=0; j<codecs[i].info.SigSize; j++)
2558 if ((signature[j] & mask[j]) != pattern[j])
2559 break;
2560 if (j == codecs[i].info.SigSize)
2562 *result = &codecs[i];
2563 return Ok;
2569 TRACE("no match for %i byte signature %x %x %x %x %x %x %x %x\n", bytesread,
2570 signature[0],signature[1],signature[2],signature[3],
2571 signature[4],signature[5],signature[6],signature[7]);
2573 return GenericError;
2576 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
2578 GpStatus stat;
2579 LARGE_INTEGER seek;
2580 HRESULT hr;
2581 const struct image_codec *codec=NULL;
2583 /* choose an appropriate image decoder */
2584 stat = get_decoder_info(stream, &codec);
2585 if (stat != Ok) return stat;
2587 /* seek to the start of the stream */
2588 seek.QuadPart = 0;
2589 hr = IStream_Seek(stream, seek, STREAM_SEEK_SET, NULL);
2590 if (FAILED(hr)) return hresult_to_status(hr);
2592 /* call on the image decoder to do the real work */
2593 stat = codec->decode_func(stream, &codec->info.Clsid, image);
2595 /* take note of the original data format */
2596 if (stat == Ok)
2598 memcpy(&(*image)->format, &codec->info.FormatID, sizeof(GUID));
2601 return stat;
2604 /* FIXME: no ICM */
2605 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
2607 TRACE("%p %p\n", stream, image);
2609 return GdipLoadImageFromStream(stream, image);
2612 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
2614 static int calls;
2616 TRACE("(%p,%u)\n", image, propId);
2618 if(!image)
2619 return InvalidParameter;
2621 if(!(calls++))
2622 FIXME("not implemented\n");
2624 return NotImplemented;
2627 GpStatus WINGDIPAPI GdipSetPropertyItem(GpImage *image, GDIPCONST PropertyItem* item)
2629 static int calls;
2631 TRACE("(%p,%p)\n", image, item);
2633 if(!(calls++))
2634 FIXME("not implemented\n");
2636 return NotImplemented;
2639 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
2640 GDIPCONST CLSID *clsidEncoder,
2641 GDIPCONST EncoderParameters *encoderParams)
2643 GpStatus stat;
2644 IStream *stream;
2646 TRACE("%p (%s) %p %p\n", image, debugstr_w(filename), clsidEncoder, encoderParams);
2648 if (!image || !filename|| !clsidEncoder)
2649 return InvalidParameter;
2651 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
2652 if (stat != Ok)
2653 return GenericError;
2655 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
2657 IStream_Release(stream);
2658 return stat;
2661 /*************************************************************************
2662 * Encoding functions -
2663 * These functions encode an image in different image file formats.
2665 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
2666 #define BITMAP_FORMAT_JPEG 0xd8ff
2667 #define BITMAP_FORMAT_GIF 0x4947
2668 #define BITMAP_FORMAT_PNG 0x5089
2669 #define BITMAP_FORMAT_APM 0xcdd7
2671 static GpStatus encode_image_WIC(GpImage *image, IStream* stream,
2672 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2674 GpStatus stat;
2675 GpBitmap *bitmap;
2676 IWICBitmapEncoder *encoder;
2677 IWICBitmapFrameEncode *frameencode;
2678 IPropertyBag2 *encoderoptions;
2679 HRESULT hr;
2680 UINT width, height;
2681 PixelFormat gdipformat=0;
2682 WICPixelFormatGUID wicformat;
2683 GpRect rc;
2684 BitmapData lockeddata;
2685 HRESULT initresult;
2686 UINT i;
2688 if (image->type != ImageTypeBitmap)
2689 return GenericError;
2691 bitmap = (GpBitmap*)image;
2693 GdipGetImageWidth(image, &width);
2694 GdipGetImageHeight(image, &height);
2696 rc.X = 0;
2697 rc.Y = 0;
2698 rc.Width = width;
2699 rc.Height = height;
2701 initresult = CoInitialize(NULL);
2703 hr = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER,
2704 &IID_IWICBitmapEncoder, (void**)&encoder);
2705 if (FAILED(hr))
2707 if (SUCCEEDED(initresult)) CoUninitialize();
2708 return hresult_to_status(hr);
2711 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
2713 if (SUCCEEDED(hr))
2715 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frameencode, &encoderoptions);
2718 if (SUCCEEDED(hr)) /* created frame */
2720 hr = IWICBitmapFrameEncode_Initialize(frameencode, encoderoptions);
2722 if (SUCCEEDED(hr))
2723 hr = IWICBitmapFrameEncode_SetSize(frameencode, width, height);
2725 if (SUCCEEDED(hr))
2726 /* FIXME: use the resolution from the image */
2727 hr = IWICBitmapFrameEncode_SetResolution(frameencode, 96.0, 96.0);
2729 if (SUCCEEDED(hr))
2731 for (i=0; wic_pixel_formats[i]; i++)
2733 if (wic_gdip_formats[i] == bitmap->format)
2734 break;
2736 if (wic_pixel_formats[i])
2737 memcpy(&wicformat, wic_pixel_formats[i], sizeof(GUID));
2738 else
2739 memcpy(&wicformat, &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
2741 hr = IWICBitmapFrameEncode_SetPixelFormat(frameencode, &wicformat);
2743 for (i=0; wic_pixel_formats[i]; i++)
2745 if (IsEqualGUID(&wicformat, wic_pixel_formats[i]))
2746 break;
2748 if (wic_pixel_formats[i])
2749 gdipformat = wic_gdip_formats[i];
2750 else
2752 ERR("cannot provide pixel format %s\n", debugstr_guid(&wicformat));
2753 hr = E_FAIL;
2757 if (SUCCEEDED(hr))
2759 stat = GdipBitmapLockBits(bitmap, &rc, ImageLockModeRead, gdipformat,
2760 &lockeddata);
2762 if (stat == Ok)
2764 UINT row_size = (lockeddata.Width * PIXELFORMATBPP(gdipformat) + 7)/8;
2765 BYTE *row;
2767 /* write one row at a time in case stride is negative */
2768 row = lockeddata.Scan0;
2769 for (i=0; i<lockeddata.Height; i++)
2771 hr = IWICBitmapFrameEncode_WritePixels(frameencode, 1, row_size, row_size, row);
2772 if (FAILED(hr)) break;
2773 row += lockeddata.Stride;
2776 GdipBitmapUnlockBits(bitmap, &lockeddata);
2778 else
2779 hr = E_FAIL;
2782 if (SUCCEEDED(hr))
2783 hr = IWICBitmapFrameEncode_Commit(frameencode);
2785 IWICBitmapFrameEncode_Release(frameencode);
2786 IPropertyBag2_Release(encoderoptions);
2789 if (SUCCEEDED(hr))
2790 hr = IWICBitmapEncoder_Commit(encoder);
2792 IWICBitmapEncoder_Release(encoder);
2794 if (SUCCEEDED(initresult)) CoUninitialize();
2796 return hresult_to_status(hr);
2799 static GpStatus encode_image_BMP(GpImage *image, IStream* stream,
2800 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2802 return encode_image_WIC(image, stream, &CLSID_WICBmpEncoder, params);
2805 static GpStatus encode_image_png(GpImage *image, IStream* stream,
2806 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2808 return encode_image_WIC(image, stream, &CLSID_WICPngEncoder, params);
2811 /*****************************************************************************
2812 * GdipSaveImageToStream [GDIPLUS.@]
2814 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
2815 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
2817 GpStatus stat;
2818 encode_image_func encode_image;
2819 int i;
2821 TRACE("%p %p %p %p\n", image, stream, clsid, params);
2823 if(!image || !stream)
2824 return InvalidParameter;
2826 /* select correct encoder */
2827 encode_image = NULL;
2828 for (i = 0; i < NUM_CODECS; i++) {
2829 if ((codecs[i].info.Flags & ImageCodecFlagsEncoder) &&
2830 IsEqualCLSID(clsid, &codecs[i].info.Clsid))
2831 encode_image = codecs[i].encode_func;
2833 if (encode_image == NULL)
2834 return UnknownImageFormat;
2836 stat = encode_image(image, stream, clsid, params);
2838 return stat;
2841 /*****************************************************************************
2842 * GdipGetImagePalette [GDIPLUS.@]
2844 GpStatus WINGDIPAPI GdipGetImagePalette(GpImage *image, ColorPalette *palette, INT size)
2846 TRACE("(%p,%p,%i)\n", image, palette, size);
2848 if (!image || !palette)
2849 return InvalidParameter;
2851 if (size < (sizeof(UINT)*2+sizeof(ARGB)*image->palette_count))
2853 TRACE("<-- InsufficientBuffer\n");
2854 return InsufficientBuffer;
2857 palette->Flags = image->palette_flags;
2858 palette->Count = image->palette_count;
2859 memcpy(palette->Entries, image->palette_entries, sizeof(ARGB)*image->palette_count);
2861 return Ok;
2864 /*****************************************************************************
2865 * GdipSetImagePalette [GDIPLUS.@]
2867 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
2868 GDIPCONST ColorPalette *palette)
2870 TRACE("(%p,%p)\n", image, palette);
2872 if(!image || !palette || palette->Count > 256)
2873 return InvalidParameter;
2875 if (palette->Count > image->palette_size)
2877 ARGB *new_palette;
2879 new_palette = GdipAlloc(sizeof(ARGB) * palette->Count);
2880 if (!new_palette) return OutOfMemory;
2882 GdipFree(image->palette_entries);
2883 image->palette_entries = new_palette;
2884 image->palette_size = palette->Count;
2887 image->palette_flags = palette->Flags;
2888 image->palette_count = palette->Count;
2889 memcpy(image->palette_entries, palette->Entries, sizeof(ARGB)*palette->Count);
2891 return Ok;
2894 /*************************************************************************
2895 * Encoders -
2896 * Structures that represent which formats we support for encoding.
2899 /* ImageCodecInfo creation routines taken from libgdiplus */
2900 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
2901 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
2902 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
2903 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
2904 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
2905 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
2907 static const WCHAR jpeg_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'J','P','E','G', 0};
2908 static const WCHAR jpeg_extension[] = {'*','.','J','P','G',';', '*','.','J','P','E','G',';', '*','.','J','P','E',';', '*','.','J','F','I','F',0};
2909 static const WCHAR jpeg_mimetype[] = {'i','m','a','g','e','/','j','p','e','g', 0};
2910 static const WCHAR jpeg_format[] = {'J','P','E','G',0};
2911 static const BYTE jpeg_sig_pattern[] = { 0xFF, 0xD8 };
2912 static const BYTE jpeg_sig_mask[] = { 0xFF, 0xFF };
2914 static const WCHAR gif_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'G','I','F', 0};
2915 static const WCHAR gif_extension[] = {'*','.','G','I','F',0};
2916 static const WCHAR gif_mimetype[] = {'i','m','a','g','e','/','g','i','f', 0};
2917 static const WCHAR gif_format[] = {'G','I','F',0};
2918 static const BYTE gif_sig_pattern[4] = "GIF8";
2919 static const BYTE gif_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2921 static const WCHAR tiff_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'T','I','F','F', 0};
2922 static const WCHAR tiff_extension[] = {'*','.','T','I','F','F',';','*','.','T','I','F',0};
2923 static const WCHAR tiff_mimetype[] = {'i','m','a','g','e','/','t','i','f','f', 0};
2924 static const WCHAR tiff_format[] = {'T','I','F','F',0};
2925 static const BYTE tiff_sig_pattern[] = {0x49,0x49,42,0,0x4d,0x4d,0,42};
2926 static const BYTE tiff_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
2928 static const WCHAR emf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'E','M','F', 0};
2929 static const WCHAR emf_extension[] = {'*','.','E','M','F',0};
2930 static const WCHAR emf_mimetype[] = {'i','m','a','g','e','/','x','-','e','m','f', 0};
2931 static const WCHAR emf_format[] = {'E','M','F',0};
2932 static const BYTE emf_sig_pattern[] = { 0x01, 0x00, 0x00, 0x00 };
2933 static const BYTE emf_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2935 static const WCHAR wmf_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'W','M','F', 0};
2936 static const WCHAR wmf_extension[] = {'*','.','W','M','F',0};
2937 static const WCHAR wmf_mimetype[] = {'i','m','a','g','e','/','x','-','w','m','f', 0};
2938 static const WCHAR wmf_format[] = {'W','M','F',0};
2939 static const BYTE wmf_sig_pattern[] = { 0xd7, 0xcd };
2940 static const BYTE wmf_sig_mask[] = { 0xFF, 0xFF };
2942 static const WCHAR png_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'P','N','G', 0};
2943 static const WCHAR png_extension[] = {'*','.','P','N','G',0};
2944 static const WCHAR png_mimetype[] = {'i','m','a','g','e','/','p','n','g', 0};
2945 static const WCHAR png_format[] = {'P','N','G',0};
2946 static const BYTE png_sig_pattern[] = { 137, 80, 78, 71, 13, 10, 26, 10, };
2947 static const BYTE png_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
2949 static const WCHAR ico_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'I','C','O', 0};
2950 static const WCHAR ico_extension[] = {'*','.','I','C','O',0};
2951 static const WCHAR ico_mimetype[] = {'i','m','a','g','e','/','x','-','i','c','o','n', 0};
2952 static const WCHAR ico_format[] = {'I','C','O',0};
2953 static const BYTE ico_sig_pattern[] = { 0x00, 0x00, 0x01, 0x00 };
2954 static const BYTE ico_sig_mask[] = { 0xFF, 0xFF, 0xFF, 0xFF };
2956 static const struct image_codec codecs[NUM_CODECS] = {
2958 { /* BMP */
2959 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2960 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2961 /* CodecName */ bmp_codecname,
2962 /* DllName */ NULL,
2963 /* FormatDescription */ bmp_format,
2964 /* FilenameExtension */ bmp_extension,
2965 /* MimeType */ bmp_mimetype,
2966 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2967 /* Version */ 1,
2968 /* SigCount */ 1,
2969 /* SigSize */ 2,
2970 /* SigPattern */ bmp_sig_pattern,
2971 /* SigMask */ bmp_sig_mask,
2973 encode_image_BMP,
2974 decode_image_bmp
2977 { /* JPEG */
2978 /* Clsid */ { 0x557cf401, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2979 /* FormatID */ { 0xb96b3caeU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2980 /* CodecName */ jpeg_codecname,
2981 /* DllName */ NULL,
2982 /* FormatDescription */ jpeg_format,
2983 /* FilenameExtension */ jpeg_extension,
2984 /* MimeType */ jpeg_mimetype,
2985 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
2986 /* Version */ 1,
2987 /* SigCount */ 1,
2988 /* SigSize */ 2,
2989 /* SigPattern */ jpeg_sig_pattern,
2990 /* SigMask */ jpeg_sig_mask,
2992 NULL,
2993 decode_image_jpeg
2996 { /* GIF */
2997 /* Clsid */ { 0x557cf402, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
2998 /* FormatID */ { 0xb96b3cb0U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
2999 /* CodecName */ gif_codecname,
3000 /* DllName */ NULL,
3001 /* FormatDescription */ gif_format,
3002 /* FilenameExtension */ gif_extension,
3003 /* MimeType */ gif_mimetype,
3004 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3005 /* Version */ 1,
3006 /* SigCount */ 1,
3007 /* SigSize */ 4,
3008 /* SigPattern */ gif_sig_pattern,
3009 /* SigMask */ gif_sig_mask,
3011 NULL,
3012 decode_image_gif
3015 { /* TIFF */
3016 /* Clsid */ { 0x557cf405, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3017 /* FormatID */ { 0xb96b3cb1U, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3018 /* CodecName */ tiff_codecname,
3019 /* DllName */ NULL,
3020 /* FormatDescription */ tiff_format,
3021 /* FilenameExtension */ tiff_extension,
3022 /* MimeType */ tiff_mimetype,
3023 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3024 /* Version */ 1,
3025 /* SigCount */ 2,
3026 /* SigSize */ 4,
3027 /* SigPattern */ tiff_sig_pattern,
3028 /* SigMask */ tiff_sig_mask,
3030 NULL,
3031 decode_image_tiff
3034 { /* EMF */
3035 /* Clsid */ { 0x557cf403, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3036 /* FormatID */ { 0xb96b3cacU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3037 /* CodecName */ emf_codecname,
3038 /* DllName */ NULL,
3039 /* FormatDescription */ emf_format,
3040 /* FilenameExtension */ emf_extension,
3041 /* MimeType */ emf_mimetype,
3042 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
3043 /* Version */ 1,
3044 /* SigCount */ 1,
3045 /* SigSize */ 4,
3046 /* SigPattern */ emf_sig_pattern,
3047 /* SigMask */ emf_sig_mask,
3049 NULL,
3050 decode_image_olepicture_metafile
3053 { /* WMF */
3054 /* Clsid */ { 0x557cf404, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3055 /* FormatID */ { 0xb96b3cadU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3056 /* CodecName */ wmf_codecname,
3057 /* DllName */ NULL,
3058 /* FormatDescription */ wmf_format,
3059 /* FilenameExtension */ wmf_extension,
3060 /* MimeType */ wmf_mimetype,
3061 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportVector | ImageCodecFlagsBuiltin,
3062 /* Version */ 1,
3063 /* SigCount */ 1,
3064 /* SigSize */ 2,
3065 /* SigPattern */ wmf_sig_pattern,
3066 /* SigMask */ wmf_sig_mask,
3068 NULL,
3069 decode_image_olepicture_metafile
3072 { /* PNG */
3073 /* Clsid */ { 0x557cf406, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3074 /* FormatID */ { 0xb96b3cafU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3075 /* CodecName */ png_codecname,
3076 /* DllName */ NULL,
3077 /* FormatDescription */ png_format,
3078 /* FilenameExtension */ png_extension,
3079 /* MimeType */ png_mimetype,
3080 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3081 /* Version */ 1,
3082 /* SigCount */ 1,
3083 /* SigSize */ 8,
3084 /* SigPattern */ png_sig_pattern,
3085 /* SigMask */ png_sig_mask,
3087 encode_image_png,
3088 decode_image_png
3091 { /* ICO */
3092 /* Clsid */ { 0x557cf407, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
3093 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
3094 /* CodecName */ ico_codecname,
3095 /* DllName */ NULL,
3096 /* FormatDescription */ ico_format,
3097 /* FilenameExtension */ ico_extension,
3098 /* MimeType */ ico_mimetype,
3099 /* Flags */ ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
3100 /* Version */ 1,
3101 /* SigCount */ 1,
3102 /* SigSize */ 4,
3103 /* SigPattern */ ico_sig_pattern,
3104 /* SigMask */ ico_sig_mask,
3106 NULL,
3107 decode_image_icon
3111 /*****************************************************************************
3112 * GdipGetImageDecodersSize [GDIPLUS.@]
3114 GpStatus WINGDIPAPI GdipGetImageDecodersSize(UINT *numDecoders, UINT *size)
3116 int decoder_count=0;
3117 int i;
3118 TRACE("%p %p\n", numDecoders, size);
3120 if (!numDecoders || !size)
3121 return InvalidParameter;
3123 for (i=0; i<NUM_CODECS; i++)
3125 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
3126 decoder_count++;
3129 *numDecoders = decoder_count;
3130 *size = decoder_count * sizeof(ImageCodecInfo);
3132 return Ok;
3135 /*****************************************************************************
3136 * GdipGetImageDecoders [GDIPLUS.@]
3138 GpStatus WINGDIPAPI GdipGetImageDecoders(UINT numDecoders, UINT size, ImageCodecInfo *decoders)
3140 int i, decoder_count=0;
3141 TRACE("%u %u %p\n", numDecoders, size, decoders);
3143 if (!decoders ||
3144 size != numDecoders * sizeof(ImageCodecInfo))
3145 return GenericError;
3147 for (i=0; i<NUM_CODECS; i++)
3149 if (codecs[i].info.Flags & ImageCodecFlagsDecoder)
3151 if (decoder_count == numDecoders) return GenericError;
3152 memcpy(&decoders[decoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
3153 decoder_count++;
3157 if (decoder_count < numDecoders) return GenericError;
3159 return Ok;
3162 /*****************************************************************************
3163 * GdipGetImageEncodersSize [GDIPLUS.@]
3165 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
3167 int encoder_count=0;
3168 int i;
3169 TRACE("%p %p\n", numEncoders, size);
3171 if (!numEncoders || !size)
3172 return InvalidParameter;
3174 for (i=0; i<NUM_CODECS; i++)
3176 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
3177 encoder_count++;
3180 *numEncoders = encoder_count;
3181 *size = encoder_count * sizeof(ImageCodecInfo);
3183 return Ok;
3186 /*****************************************************************************
3187 * GdipGetImageEncoders [GDIPLUS.@]
3189 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
3191 int i, encoder_count=0;
3192 TRACE("%u %u %p\n", numEncoders, size, encoders);
3194 if (!encoders ||
3195 size != numEncoders * sizeof(ImageCodecInfo))
3196 return GenericError;
3198 for (i=0; i<NUM_CODECS; i++)
3200 if (codecs[i].info.Flags & ImageCodecFlagsEncoder)
3202 if (encoder_count == numEncoders) return GenericError;
3203 memcpy(&encoders[encoder_count], &codecs[i].info, sizeof(ImageCodecInfo));
3204 encoder_count++;
3208 if (encoder_count < numEncoders) return GenericError;
3210 return Ok;
3213 /*****************************************************************************
3214 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
3216 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
3218 BITMAP bm;
3219 GpStatus retval;
3220 PixelFormat format;
3221 BitmapData lockeddata;
3222 INT y;
3224 TRACE("%p %p %p\n", hbm, hpal, bitmap);
3226 if(!hbm || !bitmap)
3227 return InvalidParameter;
3229 /* TODO: Support for device-dependent bitmaps */
3230 if(hpal){
3231 FIXME("no support for device-dependent bitmaps\n");
3232 return NotImplemented;
3235 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
3236 return InvalidParameter;
3238 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
3239 switch(bm.bmBitsPixel) {
3240 case 1:
3241 format = PixelFormat1bppIndexed;
3242 break;
3243 case 4:
3244 format = PixelFormat4bppIndexed;
3245 break;
3246 case 8:
3247 format = PixelFormat8bppIndexed;
3248 break;
3249 case 24:
3250 format = PixelFormat24bppRGB;
3251 break;
3252 case 32:
3253 format = PixelFormat32bppRGB;
3254 break;
3255 case 48:
3256 format = PixelFormat48bppRGB;
3257 break;
3258 default:
3259 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
3260 return InvalidParameter;
3263 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, 0,
3264 format, NULL, bitmap);
3266 if (retval == Ok)
3268 retval = GdipBitmapLockBits(*bitmap, NULL, ImageLockModeWrite,
3269 format, &lockeddata);
3270 if (retval == Ok)
3272 if (bm.bmBits)
3274 for (y=0; y<bm.bmHeight; y++)
3276 memcpy((BYTE*)lockeddata.Scan0+lockeddata.Stride*y,
3277 (BYTE*)bm.bmBits+bm.bmWidthBytes*(bm.bmHeight-1-y),
3278 bm.bmWidthBytes);
3281 else
3283 HDC hdc;
3284 HBITMAP oldhbm;
3285 BITMAPINFO *pbmi;
3286 INT src_height, dst_stride;
3287 BYTE *dst_bits;
3289 hdc = CreateCompatibleDC(NULL);
3290 oldhbm = SelectObject(hdc, hbm);
3292 pbmi = GdipAlloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
3294 if (pbmi)
3296 pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3297 pbmi->bmiHeader.biBitCount = 0;
3299 GetDIBits(hdc, hbm, 0, 0, NULL, pbmi, DIB_RGB_COLORS);
3301 src_height = abs(pbmi->bmiHeader.biHeight);
3303 if (pbmi->bmiHeader.biHeight > 0)
3305 dst_bits = (BYTE*)lockeddata.Scan0+lockeddata.Stride*(src_height-1);
3306 dst_stride = -lockeddata.Stride;
3308 else
3310 dst_bits = lockeddata.Scan0;
3311 dst_stride = lockeddata.Stride;
3314 for (y=0; y<src_height; y++)
3316 GetDIBits(hdc, hbm, y, 1, dst_bits+dst_stride*y,
3317 pbmi, DIB_RGB_COLORS);
3320 GdipFree(pbmi);
3322 else
3323 retval = OutOfMemory;
3325 SelectObject(hdc, oldhbm);
3326 DeleteDC(hdc);
3329 GdipBitmapUnlockBits(*bitmap, &lockeddata);
3333 return retval;
3336 GpStatus WINGDIPAPI GdipDeleteEffect(CGpEffect *effect)
3338 FIXME("(%p): stub\n", effect);
3339 /* note: According to Jose Roca's GDI+ Docs, this is not implemented
3340 * in Windows's gdiplus */
3341 return NotImplemented;
3344 /*****************************************************************************
3345 * GdipSetEffectParameters [GDIPLUS.@]
3347 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
3348 const VOID *params, const UINT size)
3350 static int calls;
3352 TRACE("(%p,%p,%u)\n", effect, params, size);
3354 if(!(calls++))
3355 FIXME("not implemented\n");
3357 return NotImplemented;
3360 /*****************************************************************************
3361 * GdipGetImageFlags [GDIPLUS.@]
3363 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
3365 TRACE("%p %p\n", image, flags);
3367 if(!image || !flags)
3368 return InvalidParameter;
3370 *flags = image->flags;
3372 return Ok;
3375 GpStatus WINGDIPAPI GdipTestControl(GpTestControlEnum control, void *param)
3377 TRACE("(%d, %p)\n", control, param);
3379 switch(control){
3380 case TestControlForceBilinear:
3381 if(param)
3382 FIXME("TestControlForceBilinear not handled\n");
3383 break;
3384 case TestControlNoICM:
3385 if(param)
3386 FIXME("TestControlNoICM not handled\n");
3387 break;
3388 case TestControlGetBuildNumber:
3389 *((DWORD*)param) = 3102;
3390 break;
3393 return Ok;
3396 GpStatus WINGDIPAPI GdipRecordMetafileFileName(GDIPCONST WCHAR* fileName,
3397 HDC hdc, EmfType type, GDIPCONST GpRectF *pFrameRect,
3398 MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc,
3399 GpMetafile **metafile)
3401 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
3402 frameUnit, debugstr_w(desc), metafile);
3404 return NotImplemented;
3407 GpStatus WINGDIPAPI GdipRecordMetafileFileNameI(GDIPCONST WCHAR* fileName, HDC hdc, EmfType type,
3408 GDIPCONST GpRect *pFrameRect, MetafileFrameUnit frameUnit,
3409 GDIPCONST WCHAR *desc, GpMetafile **metafile)
3411 FIXME("%s %p %d %p %d %s %p stub!\n", debugstr_w(fileName), hdc, type, pFrameRect,
3412 frameUnit, debugstr_w(desc), metafile);
3414 return NotImplemented;
3417 GpStatus WINGDIPAPI GdipImageForceValidation(GpImage *image)
3419 FIXME("%p\n", image);
3421 return Ok;
3424 /*****************************************************************************
3425 * GdipGetImageThumbnail [GDIPLUS.@]
3427 GpStatus WINGDIPAPI GdipGetImageThumbnail(GpImage *image, UINT width, UINT height,
3428 GpImage **ret_image, GetThumbnailImageAbort cb,
3429 VOID * cb_data)
3431 FIXME("(%p %u %u %p %p %p) stub\n",
3432 image, width, height, ret_image, cb, cb_data);
3433 return NotImplemented;
3436 /*****************************************************************************
3437 * GdipImageRotateFlip [GDIPLUS.@]
3439 GpStatus WINGDIPAPI GdipImageRotateFlip(GpImage *image, RotateFlipType type)
3441 FIXME("(%p %u) stub\n", image, type);
3442 return NotImplemented;