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 if (!(image && cloneImage)) return InvalidParameter;
246
247 FIXME("stub: %p, %p\n", image, cloneImage);
248
249 return NotImplemented;
250 }
251
252 GpStatus WINGDIPAPI GdipCreateBitmapFromFile(GDIPCONST WCHAR* filename,
253 GpBitmap **bitmap)
254 {
255 GpStatus stat;
256 IStream *stream;
257
258 if(!filename || !bitmap)
259 return InvalidParameter;
260
261 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
262
263 if(stat != Ok)
264 return stat;
265
266 stat = GdipCreateBitmapFromStream(stream, bitmap);
267
268 IStream_Release(stream);
269
270 return stat;
271 }
272
273 GpStatus WINGDIPAPI GdipCreateBitmapFromGdiDib(GDIPCONST BITMAPINFO* info,
274 VOID *bits, GpBitmap **bitmap)
275 {
276 DWORD height, stride;
277 PixelFormat format;
278
279 FIXME("(%p, %p, %p) - partially implemented\n", info, bits, bitmap);
280
281 height = abs(info->bmiHeader.biHeight);
282 stride = ((info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) >> 3) & ~3;
283
284 if(info->bmiHeader.biHeight > 0) /* bottom-up */
285 {
286 bits = (BYTE*)bits + (height - 1) * stride;
287 stride = -stride;
288 }
289
290 switch(info->bmiHeader.biBitCount) {
291 case 1:
292 format = PixelFormat1bppIndexed;
293 break;
294 case 4:
295 format = PixelFormat4bppIndexed;
296 break;
297 case 8:
298 format = PixelFormat8bppIndexed;
299 break;
300 case 24:
301 format = PixelFormat24bppRGB;
302 break;
303 default:
304 FIXME("don't know how to handle %d bpp\n", info->bmiHeader.biBitCount);
305 *bitmap = NULL;
306 return InvalidParameter;
307 }
308
309 return GdipCreateBitmapFromScan0(info->bmiHeader.biWidth, height, stride, format,
310 bits, bitmap);
311
312 }
313
314 /* FIXME: no icm */
315 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
316 GpBitmap **bitmap)
317 {
318 return GdipCreateBitmapFromFile(filename, bitmap);
319 }
320
321 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
322 HBITMAP* hbmReturn, ARGB background)
323 {
324 FIXME("stub\n");
325
326 hbmReturn = NULL;
327
328 return NotImplemented;
329 }
330
331 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
332 GpMetafile* metafile, BOOL* succ, EmfType emfType,
333 const WCHAR* description, GpMetafile** out_metafile)
334 {
335 static int calls;
336
337 if(!ref || !metafile || !out_metafile)
338 return InvalidParameter;
339
340 *succ = FALSE;
341 *out_metafile = NULL;
342
343 if(!(calls++))
344 FIXME("not implemented\n");
345
346 return NotImplemented;
347 }
348
349 /* FIXME: this should create a bitmap in the given size with the attributes
350 * (resolution etc.) of the graphics object */
351 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
352 GpGraphics* target, GpBitmap** bitmap)
353 {
354 static int calls;
355 GpStatus ret;
356
357 if(!target || !bitmap)
358 return InvalidParameter;
359
360 if(!(calls++))
361 FIXME("hacked stub\n");
362
363 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
364 NULL, bitmap);
365
366 return ret;
367 }
368
369 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
370 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
371 {
372 BITMAPFILEHEADER *bmfh;
373 BITMAPINFOHEADER *bmih;
374 BYTE *buff;
375 INT datalen, size;
376 IStream *stream;
377
378 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
379
380 if(!bitmap || width <= 0 || height <= 0 || (scan0 && (stride % 4))){
381 *bitmap = NULL;
382 return InvalidParameter;
383 }
384
385 if(scan0 && !stride)
386 return InvalidParameter;
387
388 /* FIXME: windows allows negative stride (reads backwards from scan0) */
389 if(stride < 0){
390 FIXME("negative stride\n");
391 return InvalidParameter;
392 }
393
394 *bitmap = GdipAlloc(sizeof(GpBitmap));
395 if(!*bitmap) return OutOfMemory;
396
397 if(stride == 0){
398 stride = width * (PIXELFORMATBPP(format) / 8);
399 stride = (stride + 3) & ~3;
400 }
401
402 datalen = abs(stride * height);
403 size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
404 buff = GdipAlloc(size);
405 if(!buff){
406 GdipFree(*bitmap);
407 return OutOfMemory;
408 }
409
410 bmfh = (BITMAPFILEHEADER*) buff;
411 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
412
413 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
414 bmfh->bfSize = size;
415 bmfh->bfOffBits = size - datalen;
416
417 bmih->biSize = sizeof(BITMAPINFOHEADER);
418 bmih->biWidth = width;
419 bmih->biHeight = -height;
420 /* FIXME: use the rest of the data from format */
421 bmih->biBitCount = PIXELFORMATBPP(format);
422 bmih->biCompression = BI_RGB;
423 bmih->biSizeImage = datalen;
424
425 if(scan0)
426 memcpy(bmih + 1, scan0, datalen);
427 else
428 memset(bmih + 1, 0, datalen);
429
430 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
431 ERR("could not make stream\n");
432 GdipFree(*bitmap);
433 GdipFree(buff);
434 return GenericError;
435 }
436
437 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
438 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
439 TRACE("Could not load picture\n");
440 IStream_Release(stream);
441 GdipFree(*bitmap);
442 GdipFree(buff);
443 return GenericError;
444 }
445
446 (*bitmap)->image.type = ImageTypeBitmap;
447 (*bitmap)->image.flags = ImageFlagsNone;
448 (*bitmap)->width = width;
449 (*bitmap)->height = height;
450 (*bitmap)->format = format;
451
452 return Ok;
453 }
454
455 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
456 GpBitmap **bitmap)
457 {
458 GpStatus stat;
459
460 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
461
462 if(stat != Ok)
463 return stat;
464
465 if((*bitmap)->image.type != ImageTypeBitmap){
466 IPicture_Release((*bitmap)->image.picture);
467 GdipFree(bitmap);
468 return GenericError; /* FIXME: what error to return? */
469 }
470
471 return Ok;
472 }
473
474 /* FIXME: no icm */
475 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
476 GpBitmap **bitmap)
477 {
478 return GdipCreateBitmapFromStream(stream, bitmap);
479 }
480
481 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
482 {
483 HDC hdc;
484
485 if(!image)
486 return InvalidParameter;
487
488 IPicture_get_CurDC(image->picture, &hdc);
489 DeleteDC(hdc);
490 IPicture_Release(image->picture);
491 if (image->type == ImageTypeBitmap)
492 GdipFree(((GpBitmap*)image)->bitmapbits);
493 GdipFree(image);
494
495 return Ok;
496 }
497
498 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
499 {
500 if(!image || !item)
501 return InvalidParameter;
502
503 return NotImplemented;
504 }
505
506 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
507 GpUnit *srcUnit)
508 {
509 if(!image || !srcRect || !srcUnit)
510 return InvalidParameter;
511 if(image->type == ImageTypeMetafile){
512 *srcRect = ((GpMetafile*)image)->bounds;
513 *srcUnit = ((GpMetafile*)image)->unit;
514 }
515 else if(image->type == ImageTypeBitmap){
516 srcRect->X = srcRect->Y = 0.0;
517 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
518 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
519 *srcUnit = UnitPixel;
520 }
521 else{
522 srcRect->X = srcRect->Y = 0.0;
523 srcRect->Width = ipicture_pixel_width(image->picture);
524 srcRect->Height = ipicture_pixel_height(image->picture);
525 *srcUnit = UnitPixel;
526 }
527
528 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
529 srcRect->Width, srcRect->Height, *srcUnit);
530
531 return Ok;
532 }
533
534 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
535 REAL *height)
536 {
537 if(!image || !height || !width)
538 return InvalidParameter;
539
540 if(image->type == ImageTypeMetafile){
541 HDC hdc = GetDC(0);
542
543 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
544 ((GpMetafile*)image)->bounds.Height;
545
546 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
547 ((GpMetafile*)image)->bounds.Width;
548
549 ReleaseDC(0, hdc);
550 }
551
552 else if(image->type == ImageTypeBitmap){
553 *height = ((GpBitmap*)image)->height;
554 *width = ((GpBitmap*)image)->width;
555 }
556 else{
557 *height = ipicture_pixel_height(image->picture);
558 *width = ipicture_pixel_width(image->picture);
559 }
560
561 TRACE("returning (%f, %f)\n", *height, *width);
562 return Ok;
563 }
564
565 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
566 GpGraphics **graphics)
567 {
568 HDC hdc;
569
570 if(!image || !graphics)
571 return InvalidParameter;
572
573 if(image->type != ImageTypeBitmap){
574 FIXME("not implemented for image type %d\n", image->type);
575 return NotImplemented;
576 }
577
578 IPicture_get_CurDC(image->picture, &hdc);
579
580 if(!hdc){
581 hdc = CreateCompatibleDC(0);
582 IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
583 }
584
585 return GdipCreateFromHDC(hdc, graphics);
586 }
587
588 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
589 {
590 if(!image || !height)
591 return InvalidParameter;
592
593 if(image->type == ImageTypeMetafile){
594 HDC hdc = GetDC(0);
595
596 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
597 ((GpMetafile*)image)->bounds.Height);
598
599 ReleaseDC(0, hdc);
600 }
601 else if(image->type == ImageTypeBitmap)
602 *height = ((GpBitmap*)image)->height;
603 else
604 *height = ipicture_pixel_height(image->picture);
605
606 TRACE("returning %d\n", *height);
607
608 return Ok;
609 }
610
611 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
612 {
613 static int calls;
614
615 if(!image || !res)
616 return InvalidParameter;
617
618 if(!(calls++))
619 FIXME("not implemented\n");
620
621 return NotImplemented;
622 }
623
624 /* FIXME: test this function for non-bitmap types */
625 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
626 {
627 if(!image || !format)
628 return InvalidParameter;
629
630 if(image->type != ImageTypeBitmap)
631 *format = PixelFormat24bppRGB;
632 else
633 *format = ((GpBitmap*) image)->format;
634
635 return Ok;
636 }
637
638 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
639 {
640 static int calls;
641
642 if(!image || !format)
643 return InvalidParameter;
644
645 if(!(calls++))
646 FIXME("not implemented\n");
647
648 return NotImplemented;
649 }
650
651 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
652 {
653 if(!image || !type)
654 return InvalidParameter;
655
656 *type = image->type;
657
658 return Ok;
659 }
660
661 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
662 {
663 static int calls;
664
665 if(!image || !res)
666 return InvalidParameter;
667
668 if(!(calls++))
669 FIXME("not implemented\n");
670
671 return NotImplemented;
672 }
673
674 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
675 {
676 if(!image || !width)
677 return InvalidParameter;
678
679 if(image->type == ImageTypeMetafile){
680 HDC hdc = GetDC(0);
681
682 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
683 ((GpMetafile*)image)->bounds.Width);
684
685 ReleaseDC(0, hdc);
686 }
687 else if(image->type == ImageTypeBitmap)
688 *width = ((GpBitmap*)image)->width;
689 else
690 *width = ipicture_pixel_width(image->picture);
691
692 TRACE("returning %d\n", *width);
693
694 return Ok;
695 }
696
697 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
698 MetafileHeader * header)
699 {
700 static int calls;
701
702 if(!metafile || !header)
703 return InvalidParameter;
704
705 if(!(calls++))
706 FIXME("not implemented\n");
707
708 return Ok;
709 }
710
711 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
712 UINT* size)
713 {
714 static int calls;
715
716 TRACE("%p %x %p\n", image, pid, size);
717
718 if(!size || !image)
719 return InvalidParameter;
720
721 if(!(calls++))
722 FIXME("not implemented\n");
723
724 return NotImplemented;
725 }
726
727 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
728 GDIPCONST GUID* dimensionID, UINT* count)
729 {
730 static int calls;
731
732 if(!image || !dimensionID || !count)
733 return InvalidParameter;
734
735 if(!(calls++))
736 FIXME("not implemented\n");
737
738 return NotImplemented;
739 }
740
741 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
742 UINT* count)
743 {
744 if(!image || !count)
745 return InvalidParameter;
746
747 *count = 1;
748
749 FIXME("stub\n");
750
751 return Ok;
752 }
753
754 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
755 GUID* dimensionIDs, UINT count)
756 {
757 static int calls;
758
759 if(!image || !dimensionIDs)
760 return InvalidParameter;
761
762 if(!(calls++))
763 FIXME("not implemented\n");
764
765 return Ok;
766 }
767
768 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
769 GDIPCONST GUID* dimensionID, UINT frameidx)
770 {
771 static int calls;
772
773 if(!image || !dimensionID)
774 return InvalidParameter;
775
776 if(!(calls++))
777 FIXME("not implemented\n");
778
779 return Ok;
780 }
781
782 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
783 GpImage **image)
784 {
785 GpStatus stat;
786 IStream *stream;
787
788 if (!filename || !image)
789 return InvalidParameter;
790
791 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
792
793 if (stat != Ok)
794 return stat;
795
796 stat = GdipLoadImageFromStream(stream, image);
797
798 IStream_Release(stream);
799
800 return stat;
801 }
802
803 /* FIXME: no icm handling */
804 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
805 {
806 return GdipLoadImageFromFile(filename, image);
807 }
808
809 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
810 {
811 IPicture *pic;
812 short type;
813
814 if(!stream || !image)
815 return InvalidParameter;
816
817 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
818 (LPVOID*) &pic) != S_OK){
819 TRACE("Could not load picture\n");
820 return GenericError;
821 }
822
823 IPicture_get_Type(pic, &type);
824
825 if(type == PICTYPE_BITMAP){
826 BITMAPINFO bmi;
827 BITMAPCOREHEADER* bmch;
828 OLE_HANDLE hbm;
829 HDC hdc;
830
831 *image = GdipAlloc(sizeof(GpBitmap));
832 if(!*image) return OutOfMemory;
833 (*image)->type = ImageTypeBitmap;
834
835 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
836 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
837
838 /* get the pixel format */
839 IPicture_get_Handle(pic, &hbm);
840 IPicture_get_CurDC(pic, &hdc);
841
842 ZeroMemory(&bmi, sizeof(bmi));
843 bmch = (BITMAPCOREHEADER*) (&bmi.bmiHeader);
844 bmch->bcSize = sizeof(BITMAPCOREHEADER);
845
846 if(!hdc){
847 HBITMAP old;
848 hdc = CreateCompatibleDC(0);
849 old = SelectObject(hdc, (HBITMAP)hbm);
850 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
851 SelectObject(hdc, old);
852 DeleteDC(hdc);
853 }
854 else
855 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
856
857 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
858 }
859 else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
860 /* FIXME: missing initialization code */
861 *image = GdipAlloc(sizeof(GpMetafile));
862 if(!*image) return OutOfMemory;
863 (*image)->type = ImageTypeMetafile;
864 }
865 else{
866 *image = GdipAlloc(sizeof(GpImage));
867 if(!*image) return OutOfMemory;
868 (*image)->type = ImageTypeUnknown;
869 }
870
871 (*image)->picture = pic;
872 (*image)->flags = ImageFlagsNone;
873
874 return Ok;
875 }
876
877 /* FIXME: no ICM */
878 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
879 {
880 return GdipLoadImageFromStream(stream, image);
881 }
882
883 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
884 {
885 static int calls;
886
887 if(!image)
888 return InvalidParameter;
889
890 if(!(calls++))
891 FIXME("not implemented\n");
892
893 return NotImplemented;
894 }
895
896 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
897 GDIPCONST CLSID *clsidEncoder,
898 GDIPCONST EncoderParameters *encoderParams)
899 {
900 GpStatus stat;
901 IStream *stream;
902
903 if (!image || !filename|| !clsidEncoder)
904 return InvalidParameter;
905
906 if (!(image->picture))
907 return InvalidParameter;
908
909 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
910 if (stat != Ok)
911 return GenericError;
912
913 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
914
915 IStream_Release(stream);
916 return stat;
917 }
918
919 /*************************************************************************
920 * Encoding functions -
921 * These functions encode an image in different image file formats.
922 */
923 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
924 #define BITMAP_FORMAT_JPEG 0xd8ff
925 #define BITMAP_FORMAT_GIF 0x4947
926 #define BITMAP_FORMAT_PNG 0x5089
927 #define BITMAP_FORMAT_APM 0xcdd7
928
929 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
930 void **output, unsigned int *output_size)
931 {
932 int num_palette_entries;
933 BITMAPFILEHEADER *bmp_file_hdr;
934 BITMAPINFO *bmp_info_hdr;
935
936 if (bitmap_info->bmiHeader.biClrUsed) {
937 num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
938 if (num_palette_entries > 256) num_palette_entries = 256;
939 } else {
940 if (bitmap_info->bmiHeader.biBitCount <= 8)
941 num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
942 else
943 num_palette_entries = 0;
944 }
945
946 *output_size =
947 sizeof(BITMAPFILEHEADER) +
948 sizeof(BITMAPINFOHEADER) +
949 num_palette_entries * sizeof(RGBQUAD) +
950 bitmap_info->bmiHeader.biSizeImage;
951
952 *output = GdipAlloc(*output_size);
953
954 bmp_file_hdr = (BITMAPFILEHEADER*) *output;
955 bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
956 bmp_file_hdr->bfSize = *output_size;
957 bmp_file_hdr->bfOffBits =
958 sizeof(BITMAPFILEHEADER) +
959 sizeof(BITMAPINFOHEADER) +
960 num_palette_entries * sizeof (RGBQUAD);
961
962 bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
963 memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
964 memcpy((unsigned char *)(*output) +
965 sizeof(BITMAPFILEHEADER) +
966 sizeof(BITMAPINFOHEADER) +
967 num_palette_entries * sizeof(RGBQUAD),
968 bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
969
970 return Ok;
971 }
972
973 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
974 void **output, unsigned int *output_size);
975
976 typedef enum {
977 BMP,
978 NUM_ENCODERS_SUPPORTED
979 } ImageFormat;
980
981 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
982 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
983 encode_image_BMP,
984 };
985
986 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
987 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
988 {
989 GpStatus stat;
990 HRESULT hr;
991 short type;
992 HBITMAP hbmp;
993 HBITMAP old_hbmp;
994 HDC hdc;
995 int bm_is_selected;
996 BITMAPINFO bmp_info;
997 LPVOID bmp_bits;
998 encode_image_func* encode_image;
999 LPVOID output;
1000 unsigned int output_size;
1001 unsigned int dummy;
1002 int i;
1003
1004 old_hbmp = 0;
1005 output = NULL;
1006 output_size = 0;
1007
1008 if(!image || !stream)
1009 return InvalidParameter;
1010
1011 if (!image->picture)
1012 return GenericError;
1013
1014 hr = IPicture_get_Type(image->picture, &type);
1015 if (FAILED(hr) || type != PICTYPE_BITMAP)
1016 return GenericError;
1017
1018 /* select correct encoder */
1019 encode_image = NULL;
1020 for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
1021 if (IsEqualCLSID(clsid, &codecs[i].Clsid))
1022 encode_image = encode_image_funcs[i];
1023 }
1024 if (encode_image == NULL