1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* GdkPixbuf library - Win32 GDI+ Pixbuf Loader
4 * Copyright (C) 2008 Dominic Lachowicz
5 * Copyright (C) 2008 Alberto Ruiz
7 * Authors: Dominic Lachowicz <domlachowicz@gmail.com>
8 * Alberto Ruiz <aruiz@gnome.org>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
27 #include "io-gdip-utils.h"
28 #include "io-gdip-native.h"
29 #include "io-gdip-propertytags.h"
30 #include "io-gdip-animation.h"
32 #define LOAD_BUFFER_SIZE 65536
35 GdkPixbufModuleUpdatedFunc updated_func;
36 GdkPixbufModulePreparedFunc prepared_func;
37 GdkPixbufModuleSizeFunc size_func;
44 typedef struct _GdipContext GdipContext;
46 static GdiplusStartupFunc GdiplusStartup;
47 static GdipCreateBitmapFromStreamFunc GdipCreateBitmapFromStream;
48 static GdipBitmapGetPixelFunc GdipBitmapGetPixel;
49 static GdipGetImageHeightFunc GdipGetImageHeight;
50 static GdipDisposeImageFunc GdipDisposeImage;
51 static GdipGetImageFlagsFunc GdipGetImageFlags;
52 static GdipGetImageWidthFunc GdipGetImageWidth;
53 static GdipImageGetFrameCountFunc GdipImageGetFrameCount;
54 static GdipImageSelectActiveFrameFunc GdipImageSelectActiveFrame;
55 static GdipGetPropertyItemSizeFunc GdipGetPropertyItemSize;
56 static GdipGetPropertyItemFunc GdipGetPropertyItem;
57 static GdipGetPropertyCountFunc GdipGetPropertyCount;
58 static GdipGetPropertyIdListFunc GdipGetPropertyIdList;
59 static GdipCreateBitmapFromScan0Func GdipCreateBitmapFromScan0;
60 static GdipSaveImageToStreamFunc GdipSaveImageToStream;
61 static GdipBitmapSetPixelFunc GdipBitmapSetPixel;
62 static GdipDrawImageIFunc GdipDrawImageI;
63 static GdipGetImageGraphicsContextFunc GdipGetImageGraphicsContext;
64 static GdipFlushFunc GdipFlush;
65 static GdipGraphicsClearFunc GdipGraphicsClear;
66 static GdipBitmapSetResolutionFunc GdipBitmapSetResolution;
67 static GdipGetImageHorizontalResolutionFunc GdipGetImageHorizontalResolution;
68 static GdipGetImageVerticalResolutionFunc GdipGetImageVerticalResolution;
69 static GdipLoadImageFromStreamFunc GdipLoadImageFromStream;
70 static GdipDeleteGraphicsFunc GdipDeleteGraphics;
71 static GdipGetImageEncodersFunc GdipGetImageEncoders;
72 static GdipGetImageEncodersSizeFunc GdipGetImageEncodersSize;
73 static GdipBitmapLockBitsFunc GdipBitmapLockBits;
74 static GdipBitmapUnlockBitsFunc GdipBitmapUnlockBits;
75 static GdipGetImagePixelFormatFunc GdipGetImagePixelFormat;
76 static GdipCloneBitmapAreaIFunc GdipCloneBitmapAreaI;
78 DEFINE_GUID(FrameDimensionTime, 0x6aedbd6d,0x3fb5,0x418a,0x83,0xa6,0x7f,0x45,0x22,0x9d,0xc8,0x72);
79 DEFINE_GUID(FrameDimensionPage, 0x7462dc86,0x6180,0x4c7e,0x8e,0x3f,0xee,0x73,0x33,0xa7,0xa4,0x83);
82 gdip_set_error_from_hresult (GError **error, gint code, HRESULT hr, const char *format)
86 msg = g_win32_error_message (hr);
89 g_set_error (error, GDK_PIXBUF_ERROR, code, format, msg);
95 gdip_set_error_from_gpstatus (GError **error, gint code, GpStatus status)
101 #define CASE(x) case x: msg = #x; break
103 CASE (InvalidParameter);
106 CASE (InsufficientBuffer);
107 CASE (NotImplemented);
112 CASE (ValueOverflow);
114 CASE (UnknownImageFormat);
115 CASE (FontFamilyNotFound);
116 CASE (FontStyleNotFound);
117 CASE (NotTrueTypeFont);
118 CASE (UnsupportedGdiplusVersion);
119 CASE (GdiplusNotInitialized);
120 CASE (PropertyNotFound);
121 CASE (PropertyNotSupported);
122 CASE (ProfileNotFound);
124 msg = "Unknown error";
126 g_set_error_literal (error, GDK_PIXBUF_ERROR, code, msg);
132 GdiplusStartupInput input;
133 ULONG_PTR gdiplusToken = 0;
134 static HINSTANCE gdipluslib = NULL;
137 gdipluslib = LoadLibrary ("gdiplus.dll");
139 return TRUE; /* gdip_init() is idempotent */
144 #define LOOKUP(func) \
146 func = (func##Func) GetProcAddress (gdipluslib, #func); \
148 g_warning ("Couldn't find GDI+ function %s\n", #func); \
153 LOOKUP (GdiplusStartup);
154 LOOKUP (GdipCreateBitmapFromStream);
155 LOOKUP (GdipBitmapGetPixel);
156 LOOKUP (GdipGetImageHeight);
157 LOOKUP (GdipDisposeImage);
158 LOOKUP (GdipGetImageFlags);
159 LOOKUP (GdipGetImageWidth);
160 LOOKUP (GdipImageGetFrameCount);
161 LOOKUP (GdipImageSelectActiveFrame);
162 LOOKUP (GdipGetPropertyItemSize);
163 LOOKUP (GdipGetPropertyItem);
164 LOOKUP (GdipGetPropertyCount);
165 LOOKUP (GdipGetPropertyIdList);
166 LOOKUP (GdipCreateBitmapFromScan0);
167 LOOKUP (GdipSaveImageToStream);
168 LOOKUP (GdipBitmapSetPixel);
169 LOOKUP (GdipDrawImageI);
170 LOOKUP (GdipGetImageGraphicsContext);
172 LOOKUP (GdipGraphicsClear);
173 LOOKUP (GdipBitmapSetResolution);
174 LOOKUP (GdipGetImageHorizontalResolution);
175 LOOKUP (GdipGetImageVerticalResolution);
176 LOOKUP (GdipLoadImageFromStream);
177 LOOKUP (GdipDeleteGraphics);
178 LOOKUP (GdipGetImageEncoders);
179 LOOKUP (GdipGetImageEncodersSize);
180 LOOKUP (GdipBitmapLockBits);
181 LOOKUP (GdipBitmapUnlockBits);
182 LOOKUP (GdipGetImagePixelFormat);
183 LOOKUP (GdipCloneBitmapAreaI);
187 input.GdiplusVersion = 1;
188 input.DebugEventCallback = NULL;
189 input.SuppressBackgroundThread = input.SuppressExternalCodecs = FALSE;
191 return (GdiplusStartup (&gdiplusToken, &input, NULL) == 0 ? TRUE : FALSE);
195 GetEncoderClsid (const WCHAR *format, CLSID *pClsid)
199 ImageCodecInfo *pImageCodecInfo;
201 if (Ok != GdipGetImageEncodersSize (&num, &size))
204 pImageCodecInfo = (ImageCodecInfo *) g_malloc (size);
206 if (Ok != GdipGetImageEncoders (num, size, pImageCodecInfo)) {
207 g_free (pImageCodecInfo);
211 for (j = 0; j < num; j++) {
212 if (wcscmp (pImageCodecInfo[j].MimeType, format) == 0) {
213 *pClsid = pImageCodecInfo[j].Clsid;
214 g_free (pImageCodecInfo);
219 g_free (pImageCodecInfo);
225 gdip_buffer_to_hglobal (const gchar *buffer, size_t size, GError **error)
229 hg = GlobalAlloc (GPTR, size);
232 gdip_set_error_from_hresult (error, GDK_PIXBUF_ERROR_FAILED, GetLastError (), _("Could not allocate memory: %s"));
236 CopyMemory (hg, buffer, size);
242 gdip_save_bitmap_to_callback (GpBitmap *bitmap,
244 const EncoderParameters *encoder_params,
245 GdkPixbufSaveFunc save_func,
250 IStream *streamOut = NULL;
251 gboolean success = FALSE;
255 hr = CreateStreamOnHGlobal (NULL, TRUE, &streamOut);
256 if (!SUCCEEDED (hr)) {
257 gdip_set_error_from_hresult (error, GDK_PIXBUF_ERROR_FAILED, hr, _("Could not create stream: %s"));
261 status = GdipSaveImageToStream ((GpImage *)bitmap, streamOut, format, encoder_params);
263 gdip_set_error_from_gpstatus (error, GDK_PIXBUF_ERROR_FAILED, status);
264 IStream_Release (streamOut);
268 /* seek back to the beginning of the stream */
269 hr = IStream_Seek (streamOut, *(LARGE_INTEGER *)&zero, STREAM_SEEK_SET, NULL);
270 if (!SUCCEEDED (hr)) {
271 gdip_set_error_from_hresult (error, GDK_PIXBUF_ERROR_FAILED, hr, _("Could not seek stream: %s"));
272 IStream_Release (streamOut);
277 char buffer[LOAD_BUFFER_SIZE];
280 hr = IStream_Read (streamOut, buffer, sizeof(buffer), &nread);
283 gdip_set_error_from_hresult (error, GDK_PIXBUF_ERROR_FAILED, hr, _("Could not read from stream: %s"));
286 else if (0 == nread) {
287 success = TRUE; /* EOF */
290 else if (!(*save_func) (buffer, nread, error, user_data))
294 IStream_Release (streamOut);
300 gdip_pixbuf_to_bitmap (GdkPixbuf *pixbuf)
302 GpBitmap *bitmap = NULL;
304 int width, height, stride, n_channels;
307 width = gdk_pixbuf_get_width (pixbuf);
308 height = gdk_pixbuf_get_height (pixbuf);
309 stride = gdk_pixbuf_get_rowstride (pixbuf);
310 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
311 pixels = gdk_pixbuf_get_pixels (pixbuf);
313 if (n_channels == 3 || n_channels == 4) {
314 /* rgbX. need to convert to argb. pass a null data to get an empty bitmap */
315 GdipCreateBitmapFromScan0 (width, height, 0, PixelFormat32bppARGB, NULL, &bitmap);
320 for (y = 0; y < height; y++) {
321 for (x = 0; x < width; x++) {
324 guchar *base = pixels + (y * stride + (x * n_channels));
334 guint8 red = base[0];
335 guint8 green = base[1];
336 guint8 blue = base[2];
338 p = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
341 GdipBitmapSetPixel (bitmap, x, y, p);
347 g_warning ("Unsupported number of channels: %d\n", n_channels);
354 gdip_buffer_to_bitmap (GdipContext *context, GError **error)
358 GpBitmap *bitmap = NULL;
359 IStream *stream = NULL;
361 guint64 size64 = context->buffer->len;
363 hg = gdip_buffer_to_hglobal (context->buffer->data, context->buffer->len, error);
368 hr = CreateStreamOnHGlobal (hg, FALSE, (LPSTREAM *)&stream);
370 if (!SUCCEEDED (hr)) {
371 gdip_set_error_from_hresult (error, GDK_PIXBUF_ERROR_FAILED, hr, _("Could not create stream: %s"));
376 IStream_SetSize (stream, *(ULARGE_INTEGER *)&size64);
378 status = GdipCreateBitmapFromStream (stream, &bitmap);
381 gdip_set_error_from_gpstatus (error, GDK_PIXBUF_ERROR_FAILED, status);
382 IStream_Release (stream);
387 context->stream = stream;
394 gdip_buffer_to_image (GdipContext *context, GError **error)
398 GpImage *image = NULL;
399 IStream *stream = NULL;
401 guint64 size64 = context->buffer->len;
403 hg = gdip_buffer_to_hglobal (context->buffer->data, context->buffer->len, error);
408 hr = CreateStreamOnHGlobal (hg, FALSE, (LPSTREAM *)&stream);
410 if (!SUCCEEDED (hr)) {
411 gdip_set_error_from_hresult (error, GDK_PIXBUF_ERROR_FAILED, hr, _("Could not create stream: %s"));
416 IStream_SetSize (stream, *(ULARGE_INTEGER *)&size64);
417 status = GdipLoadImageFromStream (stream, &image);
420 gdip_set_error_from_gpstatus (error, GDK_PIXBUF_ERROR_FAILED, status);
421 IStream_Release (stream);
426 context->stream = stream;
433 gdip_bitmap_get_size (GpBitmap *bitmap, guint *width, guint *height)
435 if (bitmap == NULL || width == NULL || height == NULL)
438 *width = *height = 0;
440 GdipGetImageWidth ((GpImage *) bitmap, width);
441 GdipGetImageHeight ((GpImage *) bitmap, height);
445 gdip_bitmap_get_has_alpha (GpBitmap *bitmap, gboolean *has_alpha)
449 if (bitmap == NULL || has_alpha == NULL)
452 GdipGetImageFlags ((GpImage *) bitmap, &flags);
453 *has_alpha = (flags & ImageFlagsHasAlpha);
457 gdip_bitmap_get_n_frames (GpBitmap *bitmap, guint *n_frames, gboolean timeDimension)
459 if (bitmap == NULL || n_frames == NULL)
464 return (Ok == GdipImageGetFrameCount ((GpImage *) bitmap, (timeDimension ? &FrameDimensionTime : &FrameDimensionPage), n_frames));
468 gdip_bitmap_select_frame (GpBitmap *bitmap, guint frame, gboolean timeDimension)
473 return (Ok == GdipImageSelectActiveFrame ((GpImage *)bitmap, (timeDimension ? &FrameDimensionTime : &FrameDimensionPage), frame));
477 gdip_bitmap_get_property_as_string (GpBitmap *bitmap, guint propertyId, gchar **str)
480 gboolean success = FALSE;
482 if (bitmap == NULL || str == NULL)
487 if (Ok == GdipGetPropertyItemSize ((GpImage *)bitmap, propertyId, &item_size)) {
490 item = (PropertyItem *)g_try_malloc (item_size);
491 if (Ok == GdipGetPropertyItem ((GpImage *)bitmap, propertyId, item_size, item)) {
495 gstr = g_string_new (NULL);
498 switch (item->type) {
499 case PropertyTagTypeByte:
500 for (i = 0; i < item->length / sizeof(guint8); i++) {
501 guint8 *bytes = (guint8 *)item->value;
504 g_string_append_c(gstr, ',');
505 g_string_append_printf (gstr, "%u", (guint32)bytes[i]);
509 case PropertyTagTypeASCII:
510 g_string_append_len (gstr, (const char *)item->value, item->length);
513 case PropertyTagTypeShort:
514 for (i = 0; i < item->length / sizeof(guint16); i++) {
515 guint16 *shorts = (guint16 *)item->value;
518 g_string_append_c (gstr, ',');
519 g_string_append_printf (gstr, "%u", (guint32)shorts[i]);
523 case PropertyTagTypeLong:
524 for (i = 0; i < item->length / sizeof(guint32); i++) {
525 guint32 *longs = (guint32 *)item->value;
528 g_string_append_c (gstr, ',');
529 g_string_append_printf (gstr, "%u", longs[i]);
533 case PropertyTagTypeSLONG:
534 for (i = 0; i < item->length / sizeof(guint32); i++) {
535 gint32 *longs = (gint32 *)item->value;
538 g_string_append_c (gstr, ',');
539 g_string_append_printf (gstr, "%d", longs[i]);
549 *str = g_string_free (gstr, FALSE);
551 g_string_free (gstr, TRUE);
561 gdip_bitmap_get_frame_delay (GpBitmap *bitmap, guint *delay)
564 gboolean success = FALSE;
566 if (bitmap == NULL || delay == NULL)
571 if (Ok == GdipGetPropertyItemSize ((GpImage *)bitmap, PropertyTagFrameDelay, &item_size)) {
574 item = (PropertyItem *)g_try_malloc (item_size);
575 if (Ok == GdipGetPropertyItem ((GpImage *)bitmap, PropertyTagFrameDelay, item_size, item)) {
576 /* PropertyTagFrameDelay. Time delay, in hundredths of a second, between two frames in an animated GIF image. */
577 *delay = *((long *)item->value);
588 gdip_bitmap_get_n_loops (GpBitmap *bitmap, guint *loops)
591 gboolean success = FALSE;
593 if (bitmap == NULL || loops == NULL)
598 /* PropertyTagLoopCount. 0 == infinitely */
599 if (Ok == GdipGetPropertyItemSize ((GpImage *)bitmap, PropertyTagLoopCount, &item_size)) {
602 item = (PropertyItem *)g_try_malloc (item_size);
603 if (Ok == GdipGetPropertyItem ((GpImage *)bitmap, PropertyTagLoopCount, item_size, item)) {
604 *loops = *((short *)item->value);
615 destroy_gdipcontext (GdipContext *context)
617 if (context != NULL) {
618 if (context->stream != NULL) {
619 IStream_Release(context->stream);
620 GlobalFree (context->hg);
622 g_byte_array_free (context->buffer, TRUE);
628 emit_updated (GdipContext *context, GdkPixbuf *pixbuf)
630 if (context->updated_func)
631 (*context->updated_func) (pixbuf,
633 gdk_pixbuf_get_width (pixbuf),
634 gdk_pixbuf_get_height (pixbuf),
639 emit_prepared (GdipContext *context, GdkPixbuf *pixbuf, GdkPixbufAnimation *anim)
641 if (context->prepared_func)
642 (*context->prepared_func) (pixbuf, anim, context->user_data);
646 gdk_pixbuf__gdip_image_begin_load (GdkPixbufModuleSizeFunc size_func,
647 GdkPixbufModulePreparedFunc prepared_func,
648 GdkPixbufModuleUpdatedFunc updated_func,
652 GdipContext *context = g_new0 (GdipContext, 1);
654 context->size_func = size_func;
655 context->prepared_func = prepared_func;
656 context->updated_func = updated_func;
657 context->user_data = user_data;
658 context->buffer = g_byte_array_new ();
664 gdk_pixbuf__gdip_image_load_increment (gpointer data,
665 const guchar *buf, guint size,
668 GdipContext *context = (GdipContext *)data;
669 GByteArray *image_buffer = context->buffer;
671 g_byte_array_append (image_buffer, (guint8 *)buf, size);
677 gdip_bitmap_to_pixbuf (GpBitmap *bitmap, GError **error)
679 GdkPixbuf *pixbuf = NULL;
680 guchar *cursor = NULL;
682 gboolean has_alpha = FALSE;
686 guint width = 0, height = 0, x, y;
688 gdip_bitmap_get_size (bitmap, &width, &height);
689 gdip_bitmap_get_has_alpha (bitmap, &has_alpha);
691 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, has_alpha, 8, width, height);
694 g_set_error_literal (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, _("Couldn't load bitmap"));
698 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
699 cursor = gdk_pixbuf_get_pixels (pixbuf);
700 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
702 for (y = 0; y < height; y++) {
703 for (x = 0; x < width; x++) {
706 guchar *b = cursor + (y * rowstride + (x * n_channels));
708 if (Ok != (status = GdipBitmapGetPixel (bitmap, x, y, &pixel))) {
709 gdip_set_error_from_gpstatus (error, GDK_PIXBUF_ERROR_FAILED, status);
710 g_object_unref (pixbuf);
714 b[0] = (pixel & 0xff0000) >> 16;
715 b[1] = (pixel & 0x00ff00) >> 8;
716 b[2] = (pixel & 0x0000ff) >> 0;
719 b[3] = (pixel & 0xff000000) >> 24;
723 if (gdip_bitmap_get_property_as_string (bitmap, PropertyTagOrientation, &option)) {
724 gdk_pixbuf_set_option (pixbuf, "orientation", option);
728 if (gdip_bitmap_get_property_as_string (bitmap, PropertyTagArtist, &option)) {
729 gdk_pixbuf_set_option (pixbuf, "Author", option);
733 if (gdip_bitmap_get_property_as_string (bitmap, PropertyTagImageTitle, &option)) {
734 gdk_pixbuf_set_option (pixbuf, "Title", option);
742 stop_load (GpBitmap *bitmap, GdipContext *context, GError **error)
744 guint n_frames = 1, i;
745 GdkPixbufGdipAnim *animation = NULL;
747 gdip_bitmap_get_n_frames (bitmap, &n_frames, TRUE);
749 for (i = 0; i < n_frames; i++) {
750 GdkPixbuf *pixbuf = NULL;
751 GdkPixbufFrame *frame;
752 guint frame_delay = 0;
754 gdip_bitmap_select_frame (bitmap, i, TRUE);
756 pixbuf = gdip_bitmap_to_pixbuf (bitmap, error);
759 if (animation != NULL)
760 g_object_unref (G_OBJECT (animation));
762 GdipDisposeImage ((GpImage *)bitmap);
763 destroy_gdipcontext (context);
767 if (animation == NULL) {
770 animation = g_object_new (GDK_TYPE_PIXBUF_GDIP_ANIM, NULL);
771 gdip_bitmap_get_n_loops (bitmap, &n_loops);
772 animation->loop = n_loops;
775 frame = g_new (GdkPixbufFrame, 1);
776 frame->pixbuf = pixbuf;
778 gdip_bitmap_get_frame_delay (bitmap, &frame_delay);
780 animation->n_frames++;
781 animation->frames = g_list_append (animation->frames, frame);
783 animation->width = gdk_pixbuf_get_width (pixbuf);
784 animation->height = gdk_pixbuf_get_height (pixbuf);
786 /* GIF delay is in hundredths, we want thousandths */
787 frame->delay_time = frame_delay * 10;
788 frame->elapsed = animation->total_time;
790 /* Some GIFs apparently have delay time of 0,
791 * that crashes everything so set it to "fast".
792 * Also, timeouts less than 20 or so just lock up
793 * the app or make the animation choppy, so fix them.
795 if (frame->delay_time < 20)
796 frame->delay_time = 20; /* 20 = "fast" */
798 animation->total_time += frame->delay_time;
801 emit_prepared (context, pixbuf, GDK_PIXBUF_ANIMATION (animation));
803 emit_updated (context, pixbuf);
806 if (animation != NULL)
807 g_object_unref (G_OBJECT (animation));
809 GdipDisposeImage ((GpImage *)bitmap);
810 destroy_gdipcontext (context);
816 gdk_pixbuf__gdip_image_stop_load (gpointer data, GError **error)
818 GdipContext *context = (GdipContext *)data;
819 GpBitmap *bitmap = NULL;
821 bitmap = gdip_buffer_to_bitmap (context, error);
824 destroy_gdipcontext (context);
825 g_set_error_literal (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE, _("Couldn't load bitmap"));
829 return stop_load (bitmap, context, error);
833 gdk_pixbuf__gdip_image_stop_vector_load (gpointer data, GError **error)
835 GdipContext *context = (GdipContext *)data;
838 GpGraphics *graphics;
841 float metafile_xres, metafile_yres;
844 metafile = gdip_buffer_to_image (context, error);
846 destroy_gdipcontext (context);
847 g_set_error_literal (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE, _("Couldn't load metafile"));
851 GdipGetImageWidth (metafile, &width);
852 GdipGetImageHeight (metafile, &height);
854 status = GdipCreateBitmapFromScan0 (width, height, 0, PixelFormat32bppARGB, NULL, &bitmap);
856 gdip_set_error_from_gpstatus (error, GDK_PIXBUF_ERROR_FAILED, status);
857 GdipDisposeImage (metafile);
862 GdipGetImageHorizontalResolution (metafile, &metafile_xres);
863 GdipGetImageVerticalResolution (metafile, &metafile_yres);
864 GdipBitmapSetResolution (bitmap, metafile_xres, metafile_yres);
866 status = GdipGetImageGraphicsContext ((GpImage *)bitmap, &graphics);
868 gdip_set_error_from_gpstatus (error, GDK_PIXBUF_ERROR_FAILED, status);
869 GdipDisposeImage ((GpImage *)bitmap);
870 GdipDisposeImage (metafile);
875 /* gotta clear the bitmap */
876 GdipGraphicsClear (graphics, 0xffffffff);
878 status = GdipDrawImageI (graphics, metafile, 0, 0);
880 gdip_set_error_from_gpstatus (error, GDK_PIXBUF_ERROR_FAILED, status);
881 GdipDeleteGraphics (graphics);
882 GdipDisposeImage ((GpImage *)bitmap);
883 GdipDisposeImage (metafile);
888 GdipFlush (graphics, 1);
890 GdipDeleteGraphics (graphics);
891 GdipDisposeImage (metafile);
893 return stop_load (bitmap, context, error);
897 gdip_animation_prepare (GdkPixbuf *pixbuf,
898 GdkPixbufAnimation *animation,
901 GdkPixbufAnimation **anim;
903 anim = (GdkPixbufAnimation **)user_data;
905 /* save a reference to the animation */
906 g_object_ref (animation);
910 static GdkPixbufAnimation *
911 gdk_pixbuf__gdip_image_load_animation (FILE *file,
914 GdkPixbufAnimation *animation = NULL;
917 char buffer[LOAD_BUFFER_SIZE];
920 context = gdk_pixbuf__gdip_image_begin_load (NULL, gdip_animation_prepare, NULL, &animation, error);
922 while (!feof (file) && !ferror (file)) {
923 length = fread (buffer, 1, sizeof (buffer), file);
925 if (!gdk_pixbuf__gdip_image_load_increment (context, buffer, length, error)) {
926 gdk_pixbuf__gdip_image_stop_load (context, NULL);
929 g_object_unref (animation);
936 if (!gdk_pixbuf__gdip_image_stop_load(context, error)) {
938 g_object_unref (animation);
947 gdip_save_to_file_callback (const gchar *buf,
952 FILE *filehandle = data;
955 n = fwrite (buf, 1, count, filehandle);
957 gint save_errno = errno;
960 g_file_error_from_errno (save_errno),
961 _("Error writing to image file: %s"),
962 g_strerror (save_errno));
970 gdip_fill_vtable (GdkPixbufModule *module)
973 module->begin_load = gdk_pixbuf__gdip_image_begin_load;
974 module->stop_load = gdk_pixbuf__gdip_image_stop_load;
975 module->load_increment = gdk_pixbuf__gdip_image_load_increment;
977 /* this is the only way to get gtk_image_new_from_file() to load animations. it regrettably
978 does not use the GdkPixbufLoader interface. */
979 module->load_animation = gdk_pixbuf__gdip_image_load_animation;
984 gdip_fill_vector_vtable (GdkPixbufModule *module)
987 module->begin_load = gdk_pixbuf__gdip_image_begin_load;
988 module->stop_load = gdk_pixbuf__gdip_image_stop_vector_load;
989 module->load_increment = gdk_pixbuf__gdip_image_load_increment;
994 gdip_save_pixbuf (GdkPixbuf *pixbuf,
996 const EncoderParameters *encoder_params,
997 GdkPixbufSaveFunc save_func,
1005 if (!GetEncoderClsid (format, &clsid)) {
1006 g_set_error_literal (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED, _("Unsupported image format for GDI+"));
1010 image = gdip_pixbuf_to_bitmap (pixbuf);
1012 if (image == NULL) {
1013 g_set_error_literal (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED, _("Couldn't save"));
1017 success = gdip_save_bitmap_to_callback (image, &clsid, encoder_params, save_func, user_data, error);
1019 GdipDisposeImage ((GpImage *)image);