]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/io-gdip-png.c
8b094bfca4ec1cf429f9cda9a475a0c36e690d39
[~andy/gtk] / gdk-pixbuf / io-gdip-png.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */\r
2 /* GdkPixbuf library - Win32 GDI+ Pixbuf Loader\r
3  *\r
4  * Copyright (C) 2008 Dominic Lachowicz\r
5  * Copyright (C) 2008 Alberto Ruiz\r
6  *\r
7  * Authors: Dominic Lachowicz <domlachowicz@gmail.com>\r
8  *          Alberto Ruiz <aruiz@gnome.org>\r
9  *\r
10  * This library is free software; you can redistribute it and/or\r
11  * modify it under the terms of the GNU Lesser General Public\r
12  * License as published by the Free Software Foundation; either\r
13  * version 2 of the License, or (at your option) any later version.\r
14  *\r
15  * This library is distributed in the hope that it will be useful,\r
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
18  * Lesser General Public License for more  * You should have received a copy of the GNU Lesser General Public\r
19  * License along with this library; if not, write to the\r
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,\r
21  * Boston, MA 02111-1307, USA.\r
22  */\r
23 \r
24 #define INITGUID\r
25 #include "io-gdip-utils.h"\r
26 \r
27 DEFINE_GUID(EncoderCompression, 0xe09d739d,0xccd4,0x44ee,0x8e,0xba,0x3f,0xbf,0x8b,0xe4,0xfc,0x58);\r
28 \r
29 static gboolean\r
30 gdk_pixbuf__gdip_image_save_PNG_to_callback (GdkPixbufSaveFunc   save_func,\r
31                                              gpointer            user_data,\r
32                                              GdkPixbuf          *pixbuf,\r
33                                              gchar             **keys,\r
34                                              gchar             **values,\r
35                                              GError            **error)\r
36 {\r
37   EncoderParameters encoder_params;\r
38   LONG compression = 5;\r
39 \r
40   if (keys && *keys) {\r
41     gchar **kiter = keys;\r
42     gchar **viter = values;\r
43     \r
44     while (*kiter) {\r
45       if (strncmp (*kiter, "tEXt::", 6) == 0) {\r
46         /* TODO: support exif data and the like */\r
47       }\r
48       else if (strcmp (*kiter, "compression") == 0) {\r
49         char *endptr = NULL;\r
50         compression = strtol (*viter, &endptr, 10);\r
51         \r
52         if (endptr == *viter) {\r
53           g_set_error (error,\r
54                        GDK_PIXBUF_ERROR,\r
55                        GDK_PIXBUF_ERROR_BAD_OPTION,\r
56                        _("PNG compression level must be a value between 0 and 9; value '%s' could not be parsed."),\r
57                        *viter);\r
58           return FALSE;\r
59         }\r
60         if (compression < 0 || compression > 9) {\r
61           /* This is a user-visible error;\r
62            * lets people skip the range-checking\r
63            * in their app.\r
64            */\r
65           g_set_error (error,\r
66                        GDK_PIXBUF_ERROR,\r
67                        GDK_PIXBUF_ERROR_BAD_OPTION,\r
68                        _("PNG compression level must be a value between 0 and 9; value '%d' is not allowed."),\r
69                        (int)compression);\r
70           return FALSE;\r
71         }       \r
72       } else {\r
73         g_warning ("Unrecognized parameter (%s) passed to PNG saver.", *kiter);\r
74       }\r
75       \r
76       ++kiter;\r
77       ++viter;\r
78     }\r
79   }\r
80 \r
81   encoder_params.Count = 1;\r
82   encoder_params.Parameter[0].Guid = EncoderCompression;\r
83   encoder_params.Parameter[0].Type = EncoderParameterValueTypeLong;\r
84   encoder_params.Parameter[0].NumberOfValues = 1;\r
85   encoder_params.Parameter[0].Value = &compression;\r
86 \r
87   return gdip_save_pixbuf (pixbuf, L"image/png", &encoder_params, save_func, user_data, error);\r
88 }\r
89 \r
90 static gboolean\r
91 gdk_pixbuf__gdip_image_save_PNG (FILE          *f,\r
92                                  GdkPixbuf     *pixbuf,\r
93                                  gchar        **keys,\r
94                                  gchar        **values,\r
95                                  GError       **error)\r
96 {\r
97   return gdk_pixbuf__gdip_image_save_PNG_to_callback (gdip_save_to_file_callback, f, pixbuf, keys, values, error);\r
98 }\r
99 \r
100 #ifndef INCLUDE_gdip_png\r
101 #define MODULE_ENTRY(function) G_MODULE_EXPORT void function\r
102 #else\r
103 #define MODULE_ENTRY(function) void _gdk_pixbuf__gdip_png_ ## function\r
104 #endif\r
105 \r
106 MODULE_ENTRY (fill_vtable) (GdkPixbufModule *module)\r
107 {\r
108   gdip_fill_vtable (module);\r
109 \r
110   module->save_to_callback = gdk_pixbuf__gdip_image_save_PNG_to_callback;\r
111   module->save = gdk_pixbuf__gdip_image_save_PNG; /* for gtk < 2.14, you need to implement both. otherwise gdk-pixbuf-queryloaders fails */\r
112 }\r
113 \r
114 MODULE_ENTRY (fill_info) (GdkPixbufFormat *info)\r
115 {\r
116   static GdkPixbufModulePattern signature[] = {\r
117     { "\x89PNG\r\n\x1a\x0a", NULL, 100 }, /* PNG */\r
118     { NULL, NULL, 0 }\r
119   };\r
120 \r
121   static gchar *mime_types[] = {\r
122     "image/png",\r
123     NULL\r
124   };\r
125 \r
126   static gchar *extensions[] = {\r
127     "png",\r
128     NULL\r
129   };\r
130 \r
131   info->name        = "png";\r
132   info->signature   = signature;\r
133   info->description = _("The PNG image format");\r
134   info->mime_types  = mime_types;\r
135   info->extensions  = extensions;\r
136   info->flags       = GDK_PIXBUF_FORMAT_WRITABLE | GDK_PIXBUF_FORMAT_THREADSAFE;\r
137   info->license     = "LGPL";\r
138 }\r