Supersedes r37623 with fixes for cross compilation
[reactos.git] / reactos / dll / win32 / gdiplus / image.c
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include <stdarg.h>
20
21 #include "windef.h"
22 #include "winbase.h"
23 #include "winuser.h"
24 #include "wingdi.h"
25
26 #define COBJMACROS
27 #include "objbase.h"
28 #include "olectl.h"
29 #include "ole2.h"
30
31 #include "gdiplus.h"
32 #include "gdiplus_private.h"
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
36
37 #define PIXELFORMATBPP(x) ((x) ? ((x) >> 8) & 255 : 24)
38
39 static INT ipicture_pixel_height(IPicture *pic)
40 {
41 HDC hdcref;
42 OLE_YSIZE_HIMETRIC y;
43
44 IPicture_get_Height(pic, &y);
45
46 hdcref = GetDC(0);
47
48 y = MulDiv(y, GetDeviceCaps(hdcref, LOGPIXELSY), INCH_HIMETRIC);
49 ReleaseDC(0, hdcref);
50
51 return y;
52 }
53
54 static INT ipicture_pixel_width(IPicture *pic)
55 {
56 HDC hdcref;
57 OLE_XSIZE_HIMETRIC x;
58
59 IPicture_get_Width(pic, &x);
60
61 hdcref = GetDC(0);
62
63 x = MulDiv(x, GetDeviceCaps(hdcref, LOGPIXELSX), INCH_HIMETRIC);
64
65 ReleaseDC(0, hdcref);
66
67 return x;
68 }
69
70 GpStatus WINGDIPAPI GdipBitmapGetPixel(GpBitmap* bitmap, INT x, INT y,
71 ARGB *color)
72 {
73 static int calls;
74 TRACE("%p %d %d %p\n", bitmap, x, y, color);
75
76 if(!bitmap || !color)
77 return InvalidParameter;
78
79 if(!(calls++))
80 FIXME("not implemented\n");
81
82 *color = 0xdeadbeef;
83
84 return NotImplemented;
85 }
86
87 /* This function returns a pointer to an array of pixels that represents the
88 * bitmap. The *entire* bitmap is locked according to the lock mode specified by
89 * flags. It is correct behavior that a user who calls this function with write
90 * privileges can write to the whole bitmap (not just the area in rect).
91 *
92 * FIXME: only used portion of format is bits per pixel. */
93 GpStatus WINGDIPAPI GdipBitmapLockBits(GpBitmap* bitmap, GDIPCONST GpRect* rect,
94 UINT flags, PixelFormat format, BitmapData* lockeddata)
95 {
96 BOOL bm_is_selected;
97 INT stride, bitspp = PIXELFORMATBPP(format);
98 OLE_HANDLE hbm;
99 HDC hdc;
100 HBITMAP old = NULL;
101 BITMAPINFO bmi;
102 BYTE *buff = NULL;
103 UINT abs_height;
104 GpRect act_rect; /* actual rect to be used */
105
106 TRACE("%p %p %d %d %p\n", bitmap, rect, flags, format, lockeddata);
107
108 if(!lockeddata || !bitmap)
109 return InvalidParameter;
110
111 if(rect){
112 if(rect->X < 0 || rect->Y < 0 || (rect->X + rect->Width > bitmap->width) ||
113 (rect->Y + rect->Height > bitmap->height) || !flags)
114 return InvalidParameter;
115
116 act_rect = *rect;
117 }
118 else{
119 act_rect.X = act_rect.Y = 0;
120 act_rect.Width = bitmap->width;
121 act_rect.Height = bitmap->height;
122 }
123
124 if(flags & ImageLockModeUserInputBuf)
125 return NotImplemented;
126
127 if(bitmap->lockmode)
128 return WrongState;
129
130 IPicture_get_Handle(bitmap->image.picture, &hbm);
131 IPicture_get_CurDC(bitmap->image.picture, &hdc);
132 bm_is_selected = (hdc != 0);
133
134 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
135 bmi.bmiHeader.biBitCount = 0;
136
137 if(!bm_is_selected){
138 hdc = CreateCompatibleDC(0);
139 old = SelectObject(hdc, (HBITMAP)hbm);
140 }
141
142 /* fill out bmi */
143 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
144
145 abs_height = abs(bmi.bmiHeader.biHeight);
146 stride = bmi.bmiHeader.biWidth * bitspp / 8;
147 stride = (stride + 3) & ~3;
148
149 buff = GdipAlloc(stride * abs_height);
150
151 bmi.bmiHeader.biBitCount = bitspp;
152
153 if(buff)
154 GetDIBits(hdc, (HBITMAP)hbm, 0, abs_height, buff, &bmi, DIB_RGB_COLORS);
155
156 if(!bm_is_selected){
157 SelectObject(hdc, old);
158 DeleteDC(hdc);
159 }
160
161 if(!buff)
162 return OutOfMemory;
163
164 lockeddata->Width = act_rect.Width;
165 lockeddata->Height = act_rect.Height;
166 lockeddata->PixelFormat = format;
167 lockeddata->Reserved = flags;
168
169 if(bmi.bmiHeader.biHeight > 0){
170 lockeddata->Stride = -stride;
171 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X +
172 stride * (abs_height - 1 - act_rect.Y);
173 }
174 else{
175 lockeddata->Stride = stride;
176 lockeddata->Scan0 = buff + (bitspp / 8) * act_rect.X + stride * act_rect.Y;
177 }
178
179 bitmap->lockmode = flags;
180 bitmap->numlocks++;
181
182 bitmap->bitmapbits = buff;
183
184 return Ok;
185 }
186
187 GpStatus WINGDIPAPI GdipBitmapUnlockBits(GpBitmap* bitmap,
188 BitmapData* lockeddata)
189 {
190 OLE_HANDLE hbm;
191 HDC hdc;
192 HBITMAP old = NULL;
193 BOOL bm_is_selected;
194 BITMAPINFO bmi;
195
196 if(!bitmap || !lockeddata)
197 return InvalidParameter;
198
199 if(!bitmap->lockmode)
200 return WrongState;
201
202 if(lockeddata->Reserved & ImageLockModeUserInputBuf)
203 return NotImplemented;
204
205 if(lockeddata->Reserved & ImageLockModeRead){
206 if(!(--bitmap->numlocks))
207 bitmap->lockmode = 0;
208
209 GdipFree(bitmap->bitmapbits);
210 bitmap->bitmapbits = NULL;
211 return Ok;
212 }
213
214 IPicture_get_Handle(bitmap->image.picture, &hbm);
215 IPicture_get_CurDC(bitmap->image.picture, &hdc);
216 bm_is_selected = (hdc != 0);
217
218 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
219 bmi.bmiHeader.biBitCount = 0;
220
221 if(!bm_is_selected){
222 hdc = CreateCompatibleDC(0);
223 old = SelectObject(hdc, (HBITMAP)hbm);
224 }
225
226 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
227 bmi.bmiHeader.biBitCount = PIXELFORMATBPP(lockeddata->PixelFormat);
228 SetDIBits(hdc, (HBITMAP)hbm, 0, abs(bmi.bmiHeader.biHeight),
229 bitmap->bitmapbits, &bmi, DIB_RGB_COLORS);
230
231 if(!bm_is_selected){
232 SelectObject(hdc, old);
233 DeleteDC(hdc);
234 }
235
236 GdipFree(bitmap->bitmapbits);
237 bitmap->bitmapbits = NULL;
238 bitmap->lockmode = 0;
239
240 return Ok;
241 }
242
243 GpStatus WINGDIPAPI GdipCloneImage(GpImage *image, GpImage **cloneImage)
244 {
245 IStream* stream;
246 HRESULT hr;
247 LONG size;
248
249 TRACE("%p, %p\n", image, cloneImage);
250
251 if (!image || !cloneImage)
252 return InvalidParameter;
253
254 hr = CreateStreamOnHGlobal(0, TRUE, &stream);
255 if (FAILED(hr))
256 return GenericError;
257
258 *cloneImage = GdipAlloc(sizeof(GpImage));
259 if (!*cloneImage)
260 {
261 IStream_Release(stream);
262 return OutOfMemory;
263 }
264 (*cloneImage)->type = image->type;
265 (*cloneImage)->flags = image->flags;
266
267 hr = IPicture_SaveAsFile(image->picture, stream, FALSE, &size);
268 if(FAILED(hr))
269 {
270 WARN("Failed to save image on stream\n");
271 goto out;
272 }
273
274 hr = OleLoadPicture(stream, size, FALSE, &IID_IPicture,
275 (LPVOID*) &(*cloneImage)->picture);
276 if (FAILED(hr))
277 {
278 WARN("Failed to load image from stream\n");
279 goto out;
280 }
281
282 IStream_Release(stream);
283 return Ok;
284 out:
285 IStream_Release(stream);
286 GdipFree(*cloneImage);
287 *cloneImage = NULL;
288 return GenericError;
289 }
290
291 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
292 GpBitmap **bitmap)
293 {
294 GpStatus stat;
295 IStream *stream;
296
297 if(!filename || !bitmap)
298 return InvalidParameter;
299
300 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
301
302 if(stat != Ok)
303 return stat;
304
305 stat = GdipCreateBitmapFromStream(stream, bitmap);
306
307 IStream_Release(stream);
308
309 return stat;
310 }
311
312 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
313 VOID *bits, GpBitmap **bitmap)
314 {
315 DWORD height, stride;
316 PixelFormat format;
317
318 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
319
320 height = abs(info->bmiHeader.biHeight);
321 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
322
323 if(info->bmiHeader.biHeight > 0) /* bottom-up */
324 {
325 bits = (BYTE*)bits + (height - 1) * stride;
326 stride = -stride;
327 }
328
329 switch(info->bmiHeader.biBitCount) {
330 case 1:
331 format = PixelFormat1bppIndexed;
332 break;
333 case 4:
334 format = PixelFormat4bppIndexed;
335 break;
336 case 8:
337 format = PixelFormat8bppIndexed;
338 break;
339 case 24:
340 format = PixelFormat24bppRGB;
341 break;
342 default:
343 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
344 *bitmap = NULL;
345 return InvalidParameter;
346 }
347
348 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
349 bits, bitmap);
350
351 }
352
353 /* FIXME: no icm */
354 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
355 GpBitmap **bitmap)
356 {
357 return GdipCreateBitmapFromFile(filename, bitmap);
358 }
359
360 GpStatus WINGDIPAPI GdipCreateBitmapFromResource(HINSTANCE hInstance,
361 GDIPCONST WCHAR* lpBitmapName, GpBitmap** bitmap)
362 {
363 HBITMAP hbm;
364 GpStatus stat = InvalidParameter;
365
366 if(!lpBitmapName || !bitmap)
367 return InvalidParameter;
368
369 /* load DIB */
370 hbm = (HBITMAP)LoadImageW(hInstance,lpBitmapName,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION);
371
372 if(hbm){
373 stat = GdipCreateBitmapFromHBITMAP(hbm, NULL, bitmap);
374 DeleteObject(hbm);
375 }
376
377 return stat;
378 }
379
380 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
381 HBITMAP* hbmReturn, ARGB background)
382 {
383 FIXME("stub\n");
384
385 hbmReturn = NULL;
386
387 return NotImplemented;
388 }
389
390 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
391 GpMetafile* metafile, BOOL* succ, EmfType emfType,
392 const WCHAR* description, GpMetafile** out_metafile)
393 {
394 static int calls;
395
396 if(!ref || !metafile || !out_metafile)
397 return InvalidParameter;
398
399 *succ = FALSE;
400 *out_metafile = NULL;
401
402 if(!(calls++))
403 FIXME("not implemented\n");
404
405 return NotImplemented;
406 }
407
408 /* FIXME: this should create a bitmap in the given size with the attributes
409 * (resolution etc.) of the graphics object */
410 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
411 GpGraphics* target, GpBitmap** bitmap)
412 {
413 static int calls;
414 GpStatus ret;
415
416 if(!target || !bitmap)
417 return InvalidParameter;
418
419 if(!(calls++))
420 FIXME("hacked stub\n");
421
422 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
423 NULL, bitmap);
424
425 return ret;
426 }
427
428 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
429 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
430 {
431 BITMAPFILEHEADER *bmfh;
432 BITMAPINFOHEADER *bmih;
433 BYTE *buff;
434 INT datalen, size;
435 IStream *stream;
436
437 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
438
439 if(!bitmap || width <= 0 || height <= 0 || (scan0 && (stride % 4))){
440 *bitmap = NULL;
441 return InvalidParameter;
442 }
443
444 if(scan0 && !stride)
445 return InvalidParameter;
446
447 /* FIXME: windows allows negative stride (reads backwards from scan0) */
448 if(stride < 0){
449 FIXME("negative stride\n");
450 return InvalidParameter;
451 }
452
453 *bitmap = GdipAlloc(sizeof(GpBitmap));
454 if(!*bitmap) return OutOfMemory;
455
456 if(stride == 0){
457 stride = width * (PIXELFORMATBPP(format) / 8);
458 stride = (stride + 3) & ~3;
459 }
460
461 datalen = abs(stride * height);
462 size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
463 buff = GdipAlloc(size);
464 if(!buff){
465 GdipFree(*bitmap);
466 return OutOfMemory;
467 }
468
469 bmfh = (BITMAPFILEHEADER*) buff;
470 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
471
472 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
473 bmfh->bfSize = size;
474 bmfh->bfOffBits = size - datalen;
475
476 bmih->biSize = sizeof(BITMAPINFOHEADER);
477 bmih->biWidth = width;
478 bmih->biHeight = -height;
479 /* FIXME: use the rest of the data from format */
480 bmih->biBitCount = PIXELFORMATBPP(format);
481 bmih->biCompression = BI_RGB;
482 bmih->biSizeImage = datalen;
483
484 if(scan0)
485 memcpy(bmih + 1, scan0, datalen);
486 else
487 memset(bmih + 1, 0, datalen);
488
489 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
490 ERR("could not make stream\n");
491 GdipFree(*bitmap);
492 GdipFree(buff);
493 return GenericError;
494 }
495
496 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
497 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
498 TRACE("Could not load picture\n");
499 IStream_Release(stream);
500 GdipFree(*bitmap);
501 GdipFree(buff);
502 return GenericError;
503 }
504
505 (*bitmap)->image.type = ImageTypeBitmap;
506 (*bitmap)->image.flags = ImageFlagsNone;
507 (*bitmap)->width = width;
508 (*bitmap)->height = height;
509 (*bitmap)->format = format;
510
511 return Ok;
512 }
513
514 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
515 GpBitmap **bitmap)
516 {
517 GpStatus stat;
518
519 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
520
521 if(stat != Ok)
522 return stat;
523
524 if((*bitmap)->image.type != ImageTypeBitmap){
525 IPicture_Release((*bitmap)->image.picture);
526 GdipFree(bitmap);
527 return GenericError; /* FIXME: what error to return? */
528 }
529
530 return Ok;
531 }
532
533 /* FIXME: no icm */
534 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
535 GpBitmap **bitmap)
536 {
537 return GdipCreateBitmapFromStream(stream, bitmap);
538 }
539
540 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
541 {
542 HDC hdc;
543
544 if(!image)
545 return InvalidParameter;
546
547 IPicture_get_CurDC(image->picture, &hdc);
548 DeleteDC(hdc);
549 IPicture_Release(image->picture);
550 if (image->type == ImageTypeBitmap)
551 GdipFree(((GpBitmap*)image)->bitmapbits);
552 GdipFree(image);
553
554 return Ok;
555 }
556
557 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
558 {
559 if(!image || !item)
560 return InvalidParameter;
561
562 return NotImplemented;
563 }
564
565 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
566 GpUnit *srcUnit)
567 {
568 if(!image || !srcRect || !srcUnit)
569 return InvalidParameter;
570 if(image->type == ImageTypeMetafile){
571 *srcRect = ((GpMetafile*)image)->bounds;
572 *srcUnit = ((GpMetafile*)image)->unit;
573 }
574 else if(image->type == ImageTypeBitmap){
575 srcRect->X = srcRect->Y = 0.0;
576 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
577 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
578 *srcUnit = UnitPixel;
579 }
580 else{
581 srcRect->X = srcRect->Y = 0.0;
582 srcRect->Width = ipicture_pixel_width(image->picture);
583 srcRect->Height = ipicture_pixel_height(image->picture);
584 *srcUnit = UnitPixel;
585 }
586
587 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
588 srcRect->Width, srcRect->Height, *srcUnit);
589
590 return Ok;
591 }
592
593 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
594 REAL *height)
595 {
596 if(!image || !height || !width)
597 return InvalidParameter;
598
599 if(image->type == ImageTypeMetafile){
600 HDC hdc = GetDC(0);
601
602 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
603 ((GpMetafile*)image)->bounds.Height;
604
605 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
606 ((GpMetafile*)image)->bounds.Width;
607
608 ReleaseDC(0, hdc);
609 }
610
611 else if(image->type == ImageTypeBitmap){
612 *height = ((GpBitmap*)image)->height;
613 *width = ((GpBitmap*)image)->width;
614 }
615 else{
616 *height = ipicture_pixel_height(image->picture);
617 *width = ipicture_pixel_width(image->picture);
618 }
619
620 TRACE("returning (%f, %f)\n", *height, *width);
621 return Ok;
622 }
623
624 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
625 GpGraphics **graphics)
626 {
627 HDC hdc;
628
629 if(!image || !graphics)
630 return InvalidParameter;
631
632 if(image->type != ImageTypeBitmap){
633 FIXME("not implemented for image type %d\n", image->type);
634 return NotImplemented;
635 }
636
637 IPicture_get_CurDC(image->picture, &hdc);
638
639 if(!hdc){
640 hdc = CreateCompatibleDC(0);
641 IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
642 }
643
644 return GdipCreateFromHDC(hdc, graphics);
645 }
646
647 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
648 {
649 if(!image || !height)
650 return InvalidParameter;
651
652 if(image->type == ImageTypeMetafile){
653 HDC hdc = GetDC(0);
654
655 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
656 ((GpMetafile*)image)->bounds.Height);
657
658 ReleaseDC(0, hdc);
659 }
660 else if(image->type == ImageTypeBitmap)
661 *height = ((GpBitmap*)image)->height;
662 else
663 *height = ipicture_pixel_height(image->picture);
664
665 TRACE("returning %d\n", *height);
666
667 return Ok;
668 }
669
670 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
671 {
672 static int calls;
673
674 if(!image || !res)
675 return InvalidParameter;
676
677 if(!(calls++))
678 FIXME("not implemented\n");
679
680 return NotImplemented;
681 }
682
683 /* FIXME: test this function for non-bitmap types */
684 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
685 {
686 if(!image || !format)
687 return InvalidParameter;
688
689 if(image->type != ImageTypeBitmap)
690 *format = PixelFormat24bppRGB;
691 else
692 *format = ((GpBitmap*) image)->format;
693
694 return Ok;
695 }
696
697 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
698 {
699 static int calls;
700
701 if(!image || !format)
702 return InvalidParameter;
703
704 if(!(calls++))
705 FIXME("not implemented\n");
706
707 return NotImplemented;
708 }
709
710 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
711 {
712 if(!image || !type)
713 return InvalidParameter;
714
715 *type = image->type;
716
717 return Ok;
718 }
719
720 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
721 {
722 static int calls;
723
724 if(!image || !res)
725 return InvalidParameter;
726
727 if(!(calls++))
728 FIXME("not implemented\n");
729
730 return NotImplemented;
731 }
732
733 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
734 {
735 if(!image || !width)
736 return InvalidParameter;
737
738 if(image->type == ImageTypeMetafile){
739 HDC hdc = GetDC(0);
740
741 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
742 ((GpMetafile*)image)->bounds.Width);
743
744 ReleaseDC(0, hdc);
745 }
746 else if(image->type == ImageTypeBitmap)
747 *width = ((GpBitmap*)image)->width;
748 else
749 *width = ipicture_pixel_width(image->picture);
750
751 TRACE("returning %d\n", *width);
752
753 return Ok;
754 }
755
756 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
757 MetafileHeader * header)
758 {
759 static int calls;
760
761 if(!metafile || !header)
762 return InvalidParameter;
763
764 if(!(calls++))
765 FIXME("not implemented\n");
766
767 return Ok;
768 }
769
770 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
771 UINT* size)
772 {
773 static int calls;
774
775 TRACE("%p %x %p\n", image, pid, size);
776
777 if(!size || !image)
778 return InvalidParameter;
779
780 if(!(calls++))
781 FIXME("not implemented\n");
782
783 return NotImplemented;
784 }
785
786 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
787 GDIPCONST GUID* dimensionID, UINT* count)
788 {
789 static int calls;
790
791 if(!image || !dimensionID || !count)
792 return InvalidParameter;
793
794 if(!(calls++))
795 FIXME("not implemented\n");
796
797 return NotImplemented;
798 }
799
800 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
801 UINT* count)
802 {
803 if(!image || !count)
804 return InvalidParameter;
805
806 *count = 1;
807
808 FIXME("stub\n");
809
810 return Ok;
811 }
812
813 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
814 GUID* dimensionIDs, UINT count)
815 {
816 static int calls;
817
818 if(!image || !dimensionIDs)
819 return InvalidParameter;
820
821 if(!(calls++))
822 FIXME("not implemented\n");
823
824 return Ok;
825 }
826
827 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
828 GDIPCONST GUID* dimensionID, UINT frameidx)
829 {
830 static int calls;
831
832 if(!image || !dimensionID)
833 return InvalidParameter;
834
835 if(!(calls++))
836 FIXME("not implemented\n");
837
838 return Ok;
839 }
840
841 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
842 GpImage **image)
843 {
844 GpStatus stat;
845 IStream *stream;
846
847 if (!filename || !image)
848 return InvalidParameter;
849
850 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
851
852 if (stat != Ok)
853 return stat;
854
855 stat = GdipLoadImageFromStream(stream, image);
856
857 IStream_Release(stream);
858
859 return stat;
860 }
861
862 /* FIXME: no icm handling */
863 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
864 {
865 return GdipLoadImageFromFile(filename, image);
866 }
867
868 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
869 {
870 IPicture *pic;
871 short type;
872
873 if(!stream || !image)
874 return InvalidParameter;
875
876 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
877 (LPVOID*) &pic) != S_OK){
878 TRACE("Could not load picture\n");
879 return GenericError;
880 }
881
882 IPicture_get_Type(pic, &type);
883
884 if(type == PICTYPE_BITMAP){
885 BITMAPINFO bmi;
886 BITMAPCOREHEADER* bmch;
887 OLE_HANDLE hbm;
888 HDC hdc;
889
890 *image = GdipAlloc(sizeof(GpBitmap));
891 if(!*image) return OutOfMemory;
892 (*image)->type = ImageTypeBitmap;
893
894 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
895 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
896
897 /* get the pixel format */
898 IPicture_get_Handle(pic, &hbm);
899 IPicture_get_CurDC(pic, &hdc);
900
901 ZeroMemory(&bmi, sizeof(bmi));
902 bmch = (BITMAPCOREHEADER*) (&bmi.bmiHeader);
903 bmch->bcSize = sizeof(BITMAPCOREHEADER);
904
905 if(!hdc){
906 HBITMAP old;
907 hdc = CreateCompatibleDC(0);
908 old = SelectObject(hdc, (HBITMAP)hbm);
909 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
910 SelectObject(hdc, old);
911 DeleteDC(hdc);
912 }
913 else
914 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
915
916 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
917 }
918 else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
919 /* FIXME: missing initialization code */
920 *image = GdipAlloc(sizeof(GpMetafile));
921 if(!*image) return OutOfMemory;
922 (*image)->type = ImageTypeMetafile;
923 }
924 else{
925 *image = GdipAlloc(sizeof(GpImage));
926 if(!*image) return OutOfMemory;
927 (*image)->type = ImageTypeUnknown;
928 }
929
930 (*image)->picture = pic;
931 (*image)->flags = ImageFlagsNone;
932
933 return Ok;
934 }
935
936 /* FIXME: no ICM */
937 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
938 {
939 return GdipLoadImageFromStream(stream, image);
940 }
941
942 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
943 {
944 static int calls;
945
946 if(!image)
947 return InvalidParameter;
948
949 if(!(calls++))
950 FIXME("not implemented\n");
951
952 return NotImplemented;
953 }
954
955 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
956 GDIPCONST CLSID *clsidEncoder,
957 GDIPCONST EncoderParameters *encoderParams)
958 {
959 GpStatus stat;
960 IStream *stream;
961
962 if (!image || !filename|| !clsidEncoder)
963 return InvalidParameter;
964
965 if (!(image->picture))
966 return InvalidParameter;
967
968 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
969 if (stat != Ok)
970 return GenericError;
971
972 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
973
974 IStream_Release(stream);
975 return stat;
976 }
977
978 /*************************************************************************
979 * Encoding functions -
980 * These functions encode an image in different image file formats.
981 */
982 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
983 #define BITMAP_FORMAT_JPEG 0xd8ff
984 #define BITMAP_FORMAT_GIF 0x4947
985 #define BITMAP_FORMAT_PNG 0x5089
986 #define BITMAP_FORMAT_APM 0xcdd7
987
988 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
989 void **output, unsigned int *output_size)
990 {
991 int num_palette_entries;
992 BITMAPFILEHEADER *bmp_file_hdr;
993 BITMAPINFO *bmp_info_hdr;
994
995 if (bitmap_info->bmiHeader.biClrUsed) {
996 num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
997 if (num_palette_entries > 256) num_palette_entries = 256;
998 } else {
999 if (bitmap_info->bmiHeader.biBitCount <= 8)
1000 num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
1001 else
1002 num_palette_entries = 0;
1003 }
1004
1005 *output_size =
1006 sizeof(BITMAPFILEHEADER) +
1007 sizeof(BITMAPINFOHEADER) +
1008 num_palette_entries * sizeof(RGBQUAD) +
1009 bitmap_info->bmiHeader.biSizeImage;
1010
1011 *output = GdipAlloc(*output_size);
1012
1013 bmp_file_hdr = (BITMAPFILEHEADER*) *output;
1014 bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
1015 bmp_file_hdr->bfSize = *output_size;
1016 bmp_file_hdr->bfOffBits =
1017 sizeof(BITMAPFILEHEADER) +
1018 sizeof(BITMAPINFOHEADER) +
1019 num_palette_entries * sizeof (RGBQUAD);
1020
1021 bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
1022 memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
1023 memcpy((unsigned char *)(*output) +
1024 sizeof(BITMAPFILEHEADER) +
1025 sizeof(BITMAPINFOHEADER) +
1026 num_palette_entries * sizeof(RGBQUAD),
1027 bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
1028
1029 return Ok;
1030 }
1031
1032 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
1033 void **output, unsigned int *output_size);
1034
1035 typedef enum {
1036 BMP,
1037 NUM_ENCODERS_SUPPORTED
1038 } ImageFormat;
1039
1040 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
1041 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
1042 encode_image_BMP,
1043 };
1044
1045 /*****************************************************************************
1046 * GdipSaveImageToStream [GDIPLUS.@]
1047 */
1048 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
1049 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
1050 {
1051 GpStatus stat;
1052 HRESULT hr;
1053 short type;
1054 HBITMAP hbmp;
1055 HBITMAP old_hbmp;
1056 HDC hdc;
1057 int bm_is_selected;
1058 BITMAPINFO bmp_info;
1059 LPVOID bmp_bits;
1060 encode_image_func* encode_image;
1061 LPVOID output;
1062 unsigned int output_size;
1063 ULONG dummy;
1064 int i;
1065
1066 old_hbmp = 0;
1067 output = NULL;
1068 output_size = 0;
1069
1070 if(!image || !stream)
1071 return InvalidParameter;
1072
1073 if (!image->picture)
1074 return GenericError;
1075
1076 hr = IPicture_get_Type(image->picture, &type);
1077 if (FAILED(hr) || type != PICTYPE_BITMAP)
1078 return GenericError;
1079
1080 /* select correct encoder */
1081 encode_image = NULL;
1082 for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
1083 if (IsEqualCLSID(clsid, &codecs[i].Clsid))
1084 encode_image = encode_image_funcs[i];
1085 }
1086 if (encode_image == NULL)
1087 return UnknownImageFormat;
1088
1089 /* extract underlying hbitmap representation from the IPicture */
1090 hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
1091 if (FAILED(hr) || !hbmp)
1092 return GenericError;
1093 hr = IPicture_get_CurDC(image->picture, &hdc);
1094 if (FAILED(hr))
1095 return GenericError;
1096 bm_is_selected = (hdc != 0);
1097 if (!bm_is_selected) {
1098 hdc = CreateCompatibleDC(0);
1099 old_hbmp = SelectObject(hdc, hbmp);
1100 }
1101
1102 /* get bits from HBITMAP */
1103 bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
1104 bmp_info.bmiHeader.biBitCount = 0;
1105 GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
1106
1107 bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
1108
1109 if (bmp_bits)
1110 GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
1111
1112 if (!bm_is_selected) {
1113 SelectObject(hdc, old_hbmp);
1114 DeleteDC(hdc);
1115 }
1116
1117 if (!bmp_bits)
1118 return OutOfMemory;
1119
1120 stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
1121 if (stat == Ok)
1122 IStream_Write(stream, output, output_size, &dummy);
1123
1124 GdipFree(output);
1125 GdipFree(bmp_bits);
1126
1127 return stat;
1128 }
1129
1130 /*****************************************************************************
1131 * GdipSetImagePalette [GDIPLUS.@]
1132 */
1133 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1134 GDIPCONST ColorPalette *palette)
1135 {
1136 static int calls;
1137
1138 if(!image || !palette)
1139 return InvalidParameter;
1140
1141 if(!(calls++))
1142 FIXME("not implemented\n");
1143
1144 return NotImplemented;
1145 }
1146
1147 /*************************************************************************
1148 * Encoders -
1149 * Structures that represent which formats we support for encoding.
1150 */
1151
1152 /* ImageCodecInfo creation routines taken from libgdiplus */
1153 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1154 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1155 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1156 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1157 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1158 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1159
1160 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1161 {
1162 { /* BMP */
1163 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1164 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1165 /* CodecName */ bmp_codecname,
1166 /* DllName */ NULL,
1167 /* FormatDescription */ bmp_format,
1168 /* FilenameExtension */ bmp_extension,
1169 /* MimeType */ bmp_mimetype,
1170 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1171 /* Version */ 1,
1172 /* SigCount */ 1,
1173 /* SigSize */ 2,
1174 /* SigPattern */ bmp_sig_pattern,
1175 /* SigMask */ bmp_sig_mask,
1176 },
1177 };
1178
1179 /*****************************************************************************
1180 * GdipGetImageEncodersSize [GDIPLUS.@]
1181 */
1182 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1183 {
1184 if (!numEncoders || !size)
1185 return InvalidParameter;
1186
1187 *numEncoders = NUM_ENCODERS_SUPPORTED;
1188 *size = sizeof (codecs);
1189
1190 return Ok;
1191 }
1192
1193 /*****************************************************************************
1194 * GdipGetImageEncoders [GDIPLUS.@]
1195 */
1196 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1197 {
1198 if (!encoders ||
1199 (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1200 (size != sizeof (codecs)))
1201 return GenericError;
1202
1203 memcpy(encoders, codecs, sizeof (codecs));
1204
1205 return Ok;
1206 }
1207
1208 /*****************************************************************************
1209 * GdipCreateBitmapFromHBITMAP [GDIPLUS.@]
1210 */
1211 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1212 {
1213 BITMAP bm;
1214 GpStatus retval;
1215 PixelFormat format;
1216
1217 if(!hbm || !bitmap)
1218 return InvalidParameter;
1219
1220 /* TODO: Support for device-dependent bitmaps */
1221 if(hpal){
1222 FIXME("no support for device-dependent bitmaps\n");
1223 return NotImplemented;
1224 }
1225
1226 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1227 return InvalidParameter;
1228
1229 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1230 switch(bm.bmBitsPixel) {
1231 case 1:
1232 format = PixelFormat1bppIndexed;
1233 break;
1234 case 4:
1235 format = PixelFormat4bppIndexed;
1236 break;
1237 case 8:
1238 format = PixelFormat8bppIndexed;
1239 break;
1240 case 24:
1241 format = PixelFormat24bppRGB;
1242 break;
1243 case 48:
1244 format = PixelFormat48bppRGB;
1245 break;
1246 default:
1247 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1248 return InvalidParameter;
1249 }
1250
1251 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes,
1252 format, bm.bmBits, bitmap);
1253
1254 return retval;
1255 }
1256
1257 /*****************************************************************************
1258 * GdipSetEffectParameters [GDIPLUS.@]
1259 */
1260 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1261 const VOID *params, const UINT size)
1262 {
1263 static int calls;
1264
1265 if(!(calls++))
1266 FIXME("not implemented\n");
1267
1268 return NotImplemented;
1269 }
1270
1271 /*****************************************************************************
1272 * GdipGetImageFlags [GDIPLUS.@]
1273 */
1274 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1275 {
1276 if(!image || !flags)
1277 return InvalidParameter;
1278
1279 *flags = image->flags;
1280
1281 return Ok;
1282 }