1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* GdkPixbuf library - Win32 GDI+ Pixbuf Loader
3 *
4 * Copyright (C) 2008 Dominic Lachowicz
5 * Copyright (C) 2008 Alberto Ruiz
6 *
7 * Authors: Dominic Lachowicz <domlachowicz@gmail.com>
8 * Alberto Ruiz <aruiz@gnome.org>
9 *
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.
14 *
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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "config.h"
23 #include <glib/gi18n-lib.h>
24
25 #define INITGUID
26 #include "io-gdip-utils.h"
27
28 DEFINE_GUID(EncoderQuality, 0x1d5be4b5,0xfa4a,0x452d,0x9c,0xdd,0x5d,0xb3,0x51,0x05,0xe7,0xeb);
29
30 static gboolean
gdk_pixbuf__gdip_image_save_JPEG_to_callback(GdkPixbufSaveFunc save_func,gpointer user_data,GdkPixbuf * pixbuf,gchar ** keys,gchar ** values,GError ** error)31 gdk_pixbuf__gdip_image_save_JPEG_to_callback (GdkPixbufSaveFunc save_func,
32 gpointer user_data,
33 GdkPixbuf *pixbuf,
34 gchar **keys,
35 gchar **values,
36 GError **error)
37 {
38 EncoderParameters encoder_params;
39 LONG quality = 75; /* default; must be between 0 and 100 */
40
41 if (keys && *keys) {
42 gchar **kiter = keys;
43 gchar **viter = values;
44
45 while (*kiter) {
46 if (strcmp (*kiter, "quality") == 0) {
47 char *endptr = NULL;
48 quality = strtol (*viter, &endptr, 10);
49
50 if (endptr == *viter) {
51 g_set_error (error,
52 GDK_PIXBUF_ERROR,
53 GDK_PIXBUF_ERROR_BAD_OPTION,
54 _("JPEG quality must be a value between 0 and 100; value “%s” could not be parsed."),
55 *viter);
56
57 return FALSE;
58 }
59
60 if (quality < 0 ||
61 quality > 100) {
62 /* This is a user-visible error;
63 * lets people skip the range-checking
64 * in their app.
65 */
66 g_set_error (error,
67 GDK_PIXBUF_ERROR,
68 GDK_PIXBUF_ERROR_BAD_OPTION,
69 _("JPEG quality must be a value between 0 and 100; value “%d” is not allowed."),
70 (int)quality);
71
72 return FALSE;
73 }
74 } else {
75 g_warning ("Unrecognized parameter (%s) passed to JPEG saver.", *kiter);
76 }
77
78 ++kiter;
79 ++viter;
80 }
81 }
82
83 encoder_params.Count = 1;
84 encoder_params.Parameter[0].Guid = EncoderQuality;
85 encoder_params.Parameter[0].Type = EncoderParameterValueTypeLong;
86 encoder_params.Parameter[0].NumberOfValues = 1;
87 encoder_params.Parameter[0].Value = &quality;
88
89 return gdip_save_pixbuf (pixbuf, L"image/jpeg", &encoder_params, save_func, user_data, error);
90 }
91
92 static gboolean
gdk_pixbuf__gdip_image_save_JPEG(FILE * f,GdkPixbuf * pixbuf,gchar ** keys,gchar ** values,GError ** error)93 gdk_pixbuf__gdip_image_save_JPEG (FILE *f,
94 GdkPixbuf *pixbuf,
95 gchar **keys,
96 gchar **values,
97 GError **error)
98 {
99 return gdk_pixbuf__gdip_image_save_JPEG_to_callback (gdip_save_to_file_callback, f, pixbuf, keys, values, error);
100 }
101
102 static gboolean
gdk_pixbuf__gdip_is_save_option_supported_JPEG(const gchar * option_key)103 gdk_pixbuf__gdip_is_save_option_supported_JPEG (const gchar *option_key)
104 {
105 if (g_strcmp0 (option_key, "quality") == 0)
106 return TRUE;
107
108 return FALSE;
109 }
110
111 #ifndef INCLUDE_gdiplus
112 #define MODULE_ENTRY(function) G_MODULE_EXPORT void function
113 #else
114 #define MODULE_ENTRY(function) void _gdk_pixbuf__gdip_jpeg_ ## function
115 #endif
116
MODULE_ENTRY(fill_vtable)117 MODULE_ENTRY (fill_vtable) (GdkPixbufModule *module)
118 {
119 gdip_fill_vtable (module);
120
121 module->save_to_callback = gdk_pixbuf__gdip_image_save_JPEG_to_callback;
122 module->save = gdk_pixbuf__gdip_image_save_JPEG; /* for gtk < 2.14, you need to implement both. otherwise gdk-pixbuf-queryloaders fails */
123 module->is_save_option_supported = gdk_pixbuf__gdip_is_save_option_supported_JPEG;
124 }
125
MODULE_ENTRY(fill_info)126 MODULE_ENTRY (fill_info) (GdkPixbufFormat *info)
127 {
128 static const GdkPixbufModulePattern signature[] = {
129 { "\xff\xd8", NULL, 100 }, /* JPEG */
130 { NULL, NULL, 0 }
131 };
132
133 static const gchar *mime_types[] = {
134 "image/jpeg",
135 NULL
136 };
137
138 static const gchar *extensions[] = {
139 "jpeg",
140 "jpe",
141 "jpg",
142 NULL
143 };
144
145 info->name = "jpeg";
146 info->signature = (GdkPixbufModulePattern *) signature;
147 info->description = NC_("image format", "JPEG");
148 info->mime_types = (gchar **) mime_types;
149 info->extensions = (gchar **) extensions;
150 info->flags = GDK_PIXBUF_FORMAT_WRITABLE | GDK_PIXBUF_FORMAT_THREADSAFE;
151 info->license = "LGPL";
152 }
153