Autosyncing with Wine HEAD
[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 if (!(image && cloneImage)) return InvalidParameter;
246
247 FIXME("stub: %p, %p", 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 /* FIXME: no icm */
274 GpStatus WINGDIPAPI GdipCreateBitmapFromFileICM(GDIPCONST WCHAR* filename,
275 GpBitmap **bitmap)
276 {
277 return GdipCreateBitmapFromFile(filename, bitmap);
278 }
279
280 GpStatus WINGDIPAPI GdipCreateHBITMAPFromBitmap(GpBitmap* bitmap,
281 HBITMAP* hbmReturn, ARGB background)
282 {
283 FIXME("stub\n");
284
285 hbmReturn = NULL;
286
287 return NotImplemented;
288 }
289
290 GpStatus WINGDIPAPI GdipConvertToEmfPlus(const GpGraphics* ref,
291 GpMetafile* metafile, BOOL* succ, EmfType emfType,
292 const WCHAR* description, GpMetafile** out_metafile)
293 {
294 static int calls;
295
296 if(!ref || !metafile || !out_metafile)
297 return InvalidParameter;
298
299 *succ = FALSE;
300 *out_metafile = NULL;
301
302 if(!(calls++))
303 FIXME("not implemented\n");
304
305 return NotImplemented;
306 }
307
308 /* FIXME: this should create a bitmap in the given size with the attributes
309 * (resolution etc.) of the graphics object */
310 GpStatus WINGDIPAPI GdipCreateBitmapFromGraphics(INT width, INT height,
311 GpGraphics* target, GpBitmap** bitmap)
312 {
313 static int calls;
314 GpStatus ret;
315
316 if(!target || !bitmap)
317 return InvalidParameter;
318
319 if(!(calls++))
320 FIXME("hacked stub\n");
321
322 ret = GdipCreateBitmapFromScan0(width, height, 0, PixelFormat24bppRGB,
323 NULL, bitmap);
324
325 return ret;
326 }
327
328 GpStatus WINGDIPAPI GdipCreateBitmapFromScan0(INT width, INT height, INT stride,
329 PixelFormat format, BYTE* scan0, GpBitmap** bitmap)
330 {
331 BITMAPFILEHEADER *bmfh;
332 BITMAPINFOHEADER *bmih;
333 BYTE *buff;
334 INT datalen, size;
335 IStream *stream;
336
337 TRACE("%d %d %d %d %p %p\n", width, height, stride, format, scan0, bitmap);
338
339 if(!bitmap || width <= 0 || height <= 0 || (scan0 && (stride % 4))){
340 *bitmap = NULL;
341 return InvalidParameter;
342 }
343
344 if(scan0 && !stride)
345 return InvalidParameter;
346
347 /* FIXME: windows allows negative stride (reads backwards from scan0) */
348 if(stride < 0){
349 FIXME("negative stride\n");
350 return InvalidParameter;
351 }
352
353 *bitmap = GdipAlloc(sizeof(GpBitmap));
354 if(!*bitmap) return OutOfMemory;
355
356 if(stride == 0){
357 stride = width * (PIXELFORMATBPP(format) / 8);
358 stride = (stride + 3) & ~3;
359 }
360
361 datalen = abs(stride * height);
362 size = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + datalen;
363 buff = GdipAlloc(size);
364 if(!buff){
365 GdipFree(*bitmap);
366 return OutOfMemory;
367 }
368
369 bmfh = (BITMAPFILEHEADER*) buff;
370 bmih = (BITMAPINFOHEADER*) (bmfh + 1);
371
372 bmfh->bfType = (((WORD)'M') << 8) + (WORD)'B';
373 bmfh->bfSize = size;
374 bmfh->bfOffBits = size - datalen;
375
376 bmih->biSize = sizeof(BITMAPINFOHEADER);
377 bmih->biWidth = width;
378 bmih->biHeight = -height;
379 /* FIXME: use the rest of the data from format */
380 bmih->biBitCount = PIXELFORMATBPP(format);
381 bmih->biCompression = BI_RGB;
382 bmih->biSizeImage = datalen;
383
384 if(scan0)
385 memcpy(bmih + 1, scan0, datalen);
386 else
387 memset(bmih + 1, 0, datalen);
388
389 if(CreateStreamOnHGlobal(buff, TRUE, &stream) != S_OK){
390 ERR("could not make stream\n");
391 GdipFree(*bitmap);
392 GdipFree(buff);
393 return GenericError;
394 }
395
396 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
397 (LPVOID*) &((*bitmap)->image.picture)) != S_OK){
398 TRACE("Could not load picture\n");
399 IStream_Release(stream);
400 GdipFree(*bitmap);
401 GdipFree(buff);
402 return GenericError;
403 }
404
405 (*bitmap)->image.type = ImageTypeBitmap;
406 (*bitmap)->image.flags = ImageFlagsNone;
407 (*bitmap)->width = width;
408 (*bitmap)->height = height;
409 (*bitmap)->format = format;
410
411 return Ok;
412 }
413
414 GpStatus WINGDIPAPI GdipCreateBitmapFromStream(IStream* stream,
415 GpBitmap **bitmap)
416 {
417 GpStatus stat;
418
419 stat = GdipLoadImageFromStream(stream, (GpImage**) bitmap);
420
421 if(stat != Ok)
422 return stat;
423
424 if((*bitmap)->image.type != ImageTypeBitmap){
425 IPicture_Release((*bitmap)->image.picture);
426 GdipFree(bitmap);
427 return GenericError; /* FIXME: what error to return? */
428 }
429
430 return Ok;
431 }
432
433 /* FIXME: no icm */
434 GpStatus WINGDIPAPI GdipCreateBitmapFromStreamICM(IStream* stream,
435 GpBitmap **bitmap)
436 {
437 return GdipCreateBitmapFromStream(stream, bitmap);
438 }
439
440 GpStatus WINGDIPAPI GdipDisposeImage(GpImage *image)
441 {
442 HDC hdc;
443
444 if(!image)
445 return InvalidParameter;
446
447 IPicture_get_CurDC(image->picture, &hdc);
448 DeleteDC(hdc);
449 IPicture_Release(image->picture);
450 if (image->type == ImageTypeBitmap)
451 GdipFree(((GpBitmap*)image)->bitmapbits);
452 GdipFree(image);
453
454 return Ok;
455 }
456
457 GpStatus WINGDIPAPI GdipFindFirstImageItem(GpImage *image, ImageItemData* item)
458 {
459 if(!image || !item)
460 return InvalidParameter;
461
462 return NotImplemented;
463 }
464
465 GpStatus WINGDIPAPI GdipGetImageBounds(GpImage *image, GpRectF *srcRect,
466 GpUnit *srcUnit)
467 {
468 if(!image || !srcRect || !srcUnit)
469 return InvalidParameter;
470 if(image->type == ImageTypeMetafile){
471 *srcRect = ((GpMetafile*)image)->bounds;
472 *srcUnit = ((GpMetafile*)image)->unit;
473 }
474 else if(image->type == ImageTypeBitmap){
475 srcRect->X = srcRect->Y = 0.0;
476 srcRect->Width = (REAL) ((GpBitmap*)image)->width;
477 srcRect->Height = (REAL) ((GpBitmap*)image)->height;
478 *srcUnit = UnitPixel;
479 }
480 else{
481 srcRect->X = srcRect->Y = 0.0;
482 srcRect->Width = ipicture_pixel_width(image->picture);
483 srcRect->Height = ipicture_pixel_height(image->picture);
484 *srcUnit = UnitPixel;
485 }
486
487 TRACE("returning (%f, %f) (%f, %f) unit type %d\n", srcRect->X, srcRect->Y,
488 srcRect->Width, srcRect->Height, *srcUnit);
489
490 return Ok;
491 }
492
493 GpStatus WINGDIPAPI GdipGetImageDimension(GpImage *image, REAL *width,
494 REAL *height)
495 {
496 if(!image || !height || !width)
497 return InvalidParameter;
498
499 if(image->type == ImageTypeMetafile){
500 HDC hdc = GetDC(0);
501
502 *height = convert_unit(hdc, ((GpMetafile*)image)->unit) *
503 ((GpMetafile*)image)->bounds.Height;
504
505 *width = convert_unit(hdc, ((GpMetafile*)image)->unit) *
506 ((GpMetafile*)image)->bounds.Width;
507
508 ReleaseDC(0, hdc);
509 }
510
511 else if(image->type == ImageTypeBitmap){
512 *height = ((GpBitmap*)image)->height;
513 *width = ((GpBitmap*)image)->width;
514 }
515 else{
516 *height = ipicture_pixel_height(image->picture);
517 *width = ipicture_pixel_width(image->picture);
518 }
519
520 TRACE("returning (%f, %f)\n", *height, *width);
521 return Ok;
522 }
523
524 GpStatus WINGDIPAPI GdipGetImageGraphicsContext(GpImage *image,
525 GpGraphics **graphics)
526 {
527 HDC hdc;
528
529 if(!image || !graphics)
530 return InvalidParameter;
531
532 if(image->type != ImageTypeBitmap){
533 FIXME("not implemented for image type %d\n", image->type);
534 return NotImplemented;
535 }
536
537 IPicture_get_CurDC(image->picture, &hdc);
538
539 if(!hdc){
540 hdc = CreateCompatibleDC(0);
541 IPicture_SelectPicture(image->picture, hdc, NULL, NULL);
542 }
543
544 return GdipCreateFromHDC(hdc, graphics);
545 }
546
547 GpStatus WINGDIPAPI GdipGetImageHeight(GpImage *image, UINT *height)
548 {
549 if(!image || !height)
550 return InvalidParameter;
551
552 if(image->type == ImageTypeMetafile){
553 HDC hdc = GetDC(0);
554
555 *height = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
556 ((GpMetafile*)image)->bounds.Height);
557
558 ReleaseDC(0, hdc);
559 }
560 else if(image->type == ImageTypeBitmap)
561 *height = ((GpBitmap*)image)->height;
562 else
563 *height = ipicture_pixel_height(image->picture);
564
565 TRACE("returning %d\n", *height);
566
567 return Ok;
568 }
569
570 GpStatus WINGDIPAPI GdipGetImageHorizontalResolution(GpImage *image, REAL *res)
571 {
572 static int calls;
573
574 if(!image || !res)
575 return InvalidParameter;
576
577 if(!(calls++))
578 FIXME("not implemented\n");
579
580 return NotImplemented;
581 }
582
583 /* FIXME: test this function for non-bitmap types */
584 GpStatus WINGDIPAPI GdipGetImagePixelFormat(GpImage *image, PixelFormat *format)
585 {
586 if(!image || !format)
587 return InvalidParameter;
588
589 if(image->type != ImageTypeBitmap)
590 *format = PixelFormat24bppRGB;
591 else
592 *format = ((GpBitmap*) image)->format;
593
594 return Ok;
595 }
596
597 GpStatus WINGDIPAPI GdipGetImageRawFormat(GpImage *image, GUID *format)
598 {
599 static int calls;
600
601 if(!image || !format)
602 return InvalidParameter;
603
604 if(!(calls++))
605 FIXME("not implemented\n");
606
607 return NotImplemented;
608 }
609
610 GpStatus WINGDIPAPI GdipGetImageType(GpImage *image, ImageType *type)
611 {
612 if(!image || !type)
613 return InvalidParameter;
614
615 *type = image->type;
616
617 return Ok;
618 }
619
620 GpStatus WINGDIPAPI GdipGetImageVerticalResolution(GpImage *image, REAL *res)
621 {
622 static int calls;
623
624 if(!image || !res)
625 return InvalidParameter;
626
627 if(!(calls++))
628 FIXME("not implemented\n");
629
630 return NotImplemented;
631 }
632
633 GpStatus WINGDIPAPI GdipGetImageWidth(GpImage *image, UINT *width)
634 {
635 if(!image || !width)
636 return InvalidParameter;
637
638 if(image->type == ImageTypeMetafile){
639 HDC hdc = GetDC(0);
640
641 *width = roundr(convert_unit(hdc, ((GpMetafile*)image)->unit) *
642 ((GpMetafile*)image)->bounds.Width);
643
644 ReleaseDC(0, hdc);
645 }
646 else if(image->type == ImageTypeBitmap)
647 *width = ((GpBitmap*)image)->width;
648 else
649 *width = ipicture_pixel_width(image->picture);
650
651 TRACE("returning %d\n", *width);
652
653 return Ok;
654 }
655
656 GpStatus WINGDIPAPI GdipGetMetafileHeaderFromMetafile(GpMetafile * metafile,
657 MetafileHeader * header)
658 {
659 static int calls;
660
661 if(!metafile || !header)
662 return InvalidParameter;
663
664 if(!(calls++))
665 FIXME("not implemented\n");
666
667 return Ok;
668 }
669
670 GpStatus WINGDIPAPI GdipGetPropertyItemSize(GpImage *image, PROPID pid,
671 UINT* size)
672 {
673 static int calls;
674
675 TRACE("%p %x %p\n", image, pid, size);
676
677 if(!size || !image)
678 return InvalidParameter;
679
680 if(!(calls++))
681 FIXME("not implemented\n");
682
683 return NotImplemented;
684 }
685
686 GpStatus WINGDIPAPI GdipImageGetFrameCount(GpImage *image,
687 GDIPCONST GUID* dimensionID, UINT* count)
688 {
689 static int calls;
690
691 if(!image || !dimensionID || !count)
692 return InvalidParameter;
693
694 if(!(calls++))
695 FIXME("not implemented\n");
696
697 return NotImplemented;
698 }
699
700 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsCount(GpImage *image,
701 UINT* count)
702 {
703 if(!image || !count)
704 return InvalidParameter;
705
706 *count = 1;
707
708 FIXME("stub\n");
709
710 return Ok;
711 }
712
713 GpStatus WINGDIPAPI GdipImageGetFrameDimensionsList(GpImage* image,
714 GUID* dimensionIDs, UINT count)
715 {
716 static int calls;
717
718 if(!image || !dimensionIDs)
719 return InvalidParameter;
720
721 if(!(calls++))
722 FIXME("not implemented\n");
723
724 return Ok;
725 }
726
727 GpStatus WINGDIPAPI GdipImageSelectActiveFrame(GpImage *image,
728 GDIPCONST GUID* dimensionID, UINT frameidx)
729 {
730 static int calls;
731
732 if(!image || !dimensionID)
733 return InvalidParameter;
734
735 if(!(calls++))
736 FIXME("not implemented\n");
737
738 return Ok;
739 }
740
741 GpStatus WINGDIPAPI GdipLoadImageFromFile(GDIPCONST WCHAR* filename,
742 GpImage **image)
743 {
744 GpStatus stat;
745 IStream *stream;
746
747 if (!filename || !image)
748 return InvalidParameter;
749
750 stat = GdipCreateStreamOnFile(filename, GENERIC_READ, &stream);
751
752 if (stat != Ok)
753 return stat;
754
755 stat = GdipLoadImageFromStream(stream, image);
756
757 IStream_Release(stream);
758
759 return stat;
760 }
761
762 /* FIXME: no icm handling */
763 GpStatus WINGDIPAPI GdipLoadImageFromFileICM(GDIPCONST WCHAR* filename,GpImage **image)
764 {
765 return GdipLoadImageFromFile(filename, image);
766 }
767
768 GpStatus WINGDIPAPI GdipLoadImageFromStream(IStream* stream, GpImage **image)
769 {
770 IPicture *pic;
771 short type;
772
773 if(!stream || !image)
774 return InvalidParameter;
775
776 if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
777 (LPVOID*) &pic) != S_OK){
778 TRACE("Could not load picture\n");
779 return GenericError;
780 }
781
782 IPicture_get_Type(pic, &type);
783
784 if(type == PICTYPE_BITMAP){
785 BITMAPINFO bmi;
786 BITMAPCOREHEADER* bmch;
787 OLE_HANDLE hbm;
788 HDC hdc;
789
790 *image = GdipAlloc(sizeof(GpBitmap));
791 if(!*image) return OutOfMemory;
792 (*image)->type = ImageTypeBitmap;
793
794 (*((GpBitmap**) image))->width = ipicture_pixel_width(pic);
795 (*((GpBitmap**) image))->height = ipicture_pixel_height(pic);
796
797 /* get the pixel format */
798 IPicture_get_Handle(pic, &hbm);
799 IPicture_get_CurDC(pic, &hdc);
800
801 ZeroMemory(&bmi, sizeof(bmi));
802 bmch = (BITMAPCOREHEADER*) (&bmi.bmiHeader);
803 bmch->bcSize = sizeof(BITMAPCOREHEADER);
804
805 if(!hdc){
806 HBITMAP old;
807 hdc = CreateCompatibleDC(0);
808 old = SelectObject(hdc, (HBITMAP)hbm);
809 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
810 SelectObject(hdc, old);
811 DeleteDC(hdc);
812 }
813 else
814 GetDIBits(hdc, (HBITMAP)hbm, 0, 0, NULL, &bmi, DIB_RGB_COLORS);
815
816 (*((GpBitmap**) image))->format = (bmch->bcBitCount << 8) | PixelFormatGDI;
817 }
818 else if(type == PICTYPE_METAFILE || type == PICTYPE_ENHMETAFILE){
819 /* FIXME: missing initialization code */
820 *image = GdipAlloc(sizeof(GpMetafile));
821 if(!*image) return OutOfMemory;
822 (*image)->type = ImageTypeMetafile;
823 }
824 else{
825 *image = GdipAlloc(sizeof(GpImage));
826 if(!*image) return OutOfMemory;
827 (*image)->type = ImageTypeUnknown;
828 }
829
830 (*image)->picture = pic;
831 (*image)->flags = ImageFlagsNone;
832
833 return Ok;
834 }
835
836 /* FIXME: no ICM */
837 GpStatus WINGDIPAPI GdipLoadImageFromStreamICM(IStream* stream, GpImage **image)
838 {
839 return GdipLoadImageFromStream(stream, image);
840 }
841
842 GpStatus WINGDIPAPI GdipRemovePropertyItem(GpImage *image, PROPID propId)
843 {
844 static int calls;
845
846 if(!image)
847 return InvalidParameter;
848
849 if(!(calls++))
850 FIXME("not implemented\n");
851
852 return NotImplemented;
853 }
854
855 GpStatus WINGDIPAPI GdipSaveImageToFile(GpImage *image, GDIPCONST WCHAR* filename,
856 GDIPCONST CLSID *clsidEncoder,
857 GDIPCONST EncoderParameters *encoderParams)
858 {
859 GpStatus stat;
860 IStream *stream;
861
862 if (!image || !filename|| !clsidEncoder)
863 return InvalidParameter;
864
865 if (!(image->picture))
866 return InvalidParameter;
867
868 stat = GdipCreateStreamOnFile(filename, GENERIC_WRITE, &stream);
869 if (stat != Ok)
870 return GenericError;
871
872 stat = GdipSaveImageToStream(image, stream, clsidEncoder, encoderParams);
873
874 IStream_Release(stream);
875 return stat;
876 }
877
878 /*************************************************************************
879 * Encoding functions -
880 * These functions encode an image in different image file formats.
881 */
882 #define BITMAP_FORMAT_BMP 0x4d42 /* "BM" */
883 #define BITMAP_FORMAT_JPEG 0xd8ff
884 #define BITMAP_FORMAT_GIF 0x4947
885 #define BITMAP_FORMAT_PNG 0x5089
886 #define BITMAP_FORMAT_APM 0xcdd7
887
888 static GpStatus encode_image_BMP(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
889 void **output, unsigned int *output_size)
890 {
891 int num_palette_entries;
892 BITMAPFILEHEADER *bmp_file_hdr;
893 BITMAPINFO *bmp_info_hdr;
894
895 if (bitmap_info->bmiHeader.biClrUsed) {
896 num_palette_entries = bitmap_info->bmiHeader.biClrUsed;
897 if (num_palette_entries > 256) num_palette_entries = 256;
898 } else {
899 if (bitmap_info->bmiHeader.biBitCount <= 8)
900 num_palette_entries = 1 << bitmap_info->bmiHeader.biBitCount;
901 else
902 num_palette_entries = 0;
903 }
904
905 *output_size =
906 sizeof(BITMAPFILEHEADER) +
907 sizeof(BITMAPINFOHEADER) +
908 num_palette_entries * sizeof(RGBQUAD) +
909 bitmap_info->bmiHeader.biSizeImage;
910
911 *output = GdipAlloc(*output_size);
912
913 bmp_file_hdr = (BITMAPFILEHEADER*) *output;
914 bmp_file_hdr->bfType = BITMAP_FORMAT_BMP;
915 bmp_file_hdr->bfSize = *output_size;
916 bmp_file_hdr->bfOffBits =
917 sizeof(BITMAPFILEHEADER) +
918 sizeof(BITMAPINFOHEADER) +
919 num_palette_entries * sizeof (RGBQUAD);
920
921 bmp_info_hdr = (BITMAPINFO*) ((unsigned char*)(*output) + sizeof(BITMAPFILEHEADER));
922 memcpy(bmp_info_hdr, bitmap_info, sizeof(BITMAPINFOHEADER) + num_palette_entries * sizeof(RGBQUAD));
923 memcpy((unsigned char *)(*output) +
924 sizeof(BITMAPFILEHEADER) +
925 sizeof(BITMAPINFOHEADER) +
926 num_palette_entries * sizeof(RGBQUAD),
927 bitmap_bits, bitmap_info->bmiHeader.biSizeImage);
928
929 return Ok;
930 }
931
932 typedef GpStatus encode_image_func(LPVOID bitmap_bits, LPBITMAPINFO bitmap_info,
933 void **output, unsigned int *output_size);
934
935 typedef enum {
936 BMP,
937 NUM_ENCODERS_SUPPORTED
938 } ImageFormat;
939
940 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED];
941 static encode_image_func *const encode_image_funcs[NUM_ENCODERS_SUPPORTED] = {
942 encode_image_BMP,
943 };
944
945 GpStatus WINGDIPAPI GdipSaveImageToStream(GpImage *image, IStream* stream,
946 GDIPCONST CLSID* clsid, GDIPCONST EncoderParameters* params)
947 {
948 GpStatus stat;
949 HRESULT hr;
950 short type;
951 HBITMAP hbmp;
952 HBITMAP old_hbmp;
953 HDC hdc;
954 int bm_is_selected;
955 BITMAPINFO bmp_info;
956 LPVOID bmp_bits;
957 encode_image_func* encode_image;
958 LPVOID output;
959 unsigned int output_size;
960 unsigned int dummy;
961 int i;
962
963 old_hbmp = 0;
964 output = NULL;
965 output_size = 0;
966
967 if(!image || !stream)
968 return InvalidParameter;
969
970 if (!image->picture)
971 return GenericError;
972
973 hr = IPicture_get_Type(image->picture, &type);
974 if (FAILED(hr) || type != PICTYPE_BITMAP)
975 return GenericError;
976
977 /* select correct encoder */
978 encode_image = NULL;
979 for (i = 0; i < NUM_ENCODERS_SUPPORTED; i++) {
980 if (IsEqualCLSID(clsid, &codecs[i].Clsid))
981 encode_image = encode_image_funcs[i];
982 }
983 if (encode_image == NULL)
984 return UnknownImageFormat;
985
986 /* extract underlying hbitmap representation from the IPicture */
987 hr = IPicture_get_Handle(image->picture, (OLE_HANDLE*)&hbmp);
988 if (FAILED(hr) || !hbmp)
989 return GenericError;
990 hr = IPicture_get_CurDC(image->picture, &hdc);
991 if (FAILED(hr))
992 return GenericError;
993 bm_is_selected = (hdc != 0);
994 if (!bm_is_selected) {
995 hdc = CreateCompatibleDC(0);
996 old_hbmp = SelectObject(hdc, hbmp);
997 }
998
999 /* get bits from HBITMAP */
1000 bmp_info.bmiHeader.biSize = sizeof(bmp_info.bmiHeader);
1001 bmp_info.bmiHeader.biBitCount = 0;
1002 GetDIBits(hdc, hbmp, 0, 0, NULL, &bmp_info, DIB_RGB_COLORS);
1003
1004 bmp_bits = GdipAlloc(bmp_info.bmiHeader.biSizeImage);
1005
1006 if (bmp_bits)
1007 GetDIBits(hdc, hbmp, 0, abs(bmp_info.bmiHeader.biHeight), bmp_bits, &bmp_info, DIB_RGB_COLORS);
1008
1009 if (!bm_is_selected) {
1010 SelectObject(hdc, old_hbmp);
1011 DeleteDC(hdc);
1012 }
1013
1014 if (!bmp_bits)
1015 return OutOfMemory;
1016
1017 stat = encode_image(bmp_bits, &bmp_info, &output, &output_size);
1018 if (stat == Ok)
1019 IStream_Write(stream, output, output_size, &dummy);
1020
1021 GdipFree(output);
1022 GdipFree(bmp_bits);
1023
1024 return stat;
1025 }
1026
1027 GpStatus WINGDIPAPI GdipSetImagePalette(GpImage *image,
1028 GDIPCONST ColorPalette *palette)
1029 {
1030 static int calls;
1031
1032 if(!image || !palette)
1033 return InvalidParameter;
1034
1035 if(!(calls++))
1036 FIXME("not implemented\n");
1037
1038 return NotImplemented;
1039 }
1040
1041 /*************************************************************************
1042 * Encoders -
1043 * Structures that represent which formats we support for encoding.
1044 */
1045
1046 /* ImageCodecInfo creation routines taken from libgdiplus */
1047 static const WCHAR bmp_codecname[] = {'B', 'u', 'i','l', 't', '-','i', 'n', ' ', 'B', 'M', 'P', 0}; /* Built-in BMP */
1048 static const WCHAR bmp_extension[] = {'*','.','B', 'M', 'P',';', '*','.', 'D','I', 'B',';', '*','.', 'R', 'L', 'E',0}; /* *.BMP;*.DIB;*.RLE */
1049 static const WCHAR bmp_mimetype[] = {'i', 'm', 'a','g', 'e', '/', 'b', 'm', 'p', 0}; /* image/bmp */
1050 static const WCHAR bmp_format[] = {'B', 'M', 'P', 0}; /* BMP */
1051 static const BYTE bmp_sig_pattern[] = { 0x42, 0x4D };
1052 static const BYTE bmp_sig_mask[] = { 0xFF, 0xFF };
1053
1054 static const ImageCodecInfo codecs[NUM_ENCODERS_SUPPORTED] =
1055 {
1056 { /* BMP */
1057 /* Clsid */ { 0x557cf400, 0x1a04, 0x11d3, { 0x9a, 0x73, 0x0, 0x0, 0xf8, 0x1e, 0xf3, 0x2e } },
1058 /* FormatID */ { 0xb96b3cabU, 0x0728U, 0x11d3U, {0x9d, 0x7b, 0x00, 0x00, 0xf8, 0x1e, 0xf3, 0x2e} },
1059 /* CodecName */ bmp_codecname,
1060 /* DllName */ NULL,
1061 /* FormatDescription */ bmp_format,
1062 /* FilenameExtension */ bmp_extension,
1063 /* MimeType */ bmp_mimetype,
1064 /* Flags */ ImageCodecFlagsEncoder | ImageCodecFlagsDecoder | ImageCodecFlagsSupportBitmap | ImageCodecFlagsBuiltin,
1065 /* Version */ 1,
1066 /* SigCount */ 1,
1067 /* SigSize */ 2,
1068 /* SigPattern */ bmp_sig_pattern,
1069 /* SigMask */ bmp_sig_mask,
1070 },
1071 };
1072
1073 GpStatus WINGDIPAPI GdipGetImageEncodersSize(UINT *numEncoders, UINT *size)
1074 {
1075 if (!numEncoders || !size)
1076 return InvalidParameter;
1077
1078 *numEncoders = NUM_ENCODERS_SUPPORTED;
1079 *size = sizeof (codecs);
1080
1081 return Ok;
1082 }
1083
1084 GpStatus WINGDIPAPI GdipGetImageEncoders(UINT numEncoders, UINT size, ImageCodecInfo *encoders)
1085 {
1086 if (!encoders ||
1087 (numEncoders != NUM_ENCODERS_SUPPORTED) ||
1088 (size != sizeof (codecs)))
1089 return GenericError;
1090
1091 memcpy(encoders, codecs, sizeof (codecs));
1092
1093 return Ok;
1094 }
1095 GpStatus WINGDIPAPI GdipCreateBitmapFromHBITMAP(HBITMAP hbm, HPALETTE hpal, GpBitmap** bitmap)
1096 {
1097 BITMAP bm;
1098 GpStatus retval;
1099 PixelFormat format;
1100
1101 if(!hbm || !bitmap)
1102 return InvalidParameter;
1103
1104 /* TODO: Support for device-dependent bitmaps */
1105 if(hpal){
1106 FIXME("no support for device-dependent bitmaps\n");
1107 return NotImplemented;
1108 }
1109
1110 if (GetObjectA(hbm, sizeof(bm), &bm) != sizeof(bm))
1111 return InvalidParameter;
1112
1113 /* TODO: Figure out the correct format for 16, 32, 64 bpp */
1114 switch(bm.bmBitsPixel) {
1115 case 1:
1116 format = PixelFormat1bppIndexed;
1117 break;
1118 case 4:
1119 format = PixelFormat4bppIndexed;
1120 break;
1121 case 8:
1122 format = PixelFormat8bppIndexed;
1123 break;
1124 case 24:
1125 format = PixelFormat24bppRGB;
1126 break;
1127 case 48:
1128 format = PixelFormat48bppRGB;
1129 break;
1130 default:
1131 FIXME("don't know how to handle %d bpp\n", bm.bmBitsPixel);
1132 return InvalidParameter;
1133 }
1134
1135 retval = GdipCreateBitmapFromScan0(bm.bmWidth, bm.bmHeight, bm.bmWidthBytes,
1136 format, bm.bmBits, bitmap);
1137
1138 return retval;
1139 }
1140
1141 GpStatus WINGDIPAPI GdipSetEffectParameters(CGpEffect *effect,
1142 const VOID *params, const UINT size)
1143 {
1144 static int calls;
1145
1146 if(!(calls++))
1147 FIXME("not implemented\n");
1148
1149 return NotImplemented;
1150 }
1151
1152 GpStatus WINGDIPAPI GdipGetImageFlags(GpImage *image, UINT *flags)
1153 {
1154 if(!image || !flags)
1155 return InvalidParameter;
1156
1157 *flags = image->flags;
1158
1159 return Ok;
1160 }