1 /* Mac OS X .icns icons loader
3 * Copyright (c) 2007 Lyonel Vincent <lyonel@ezix.org>
4 * Copyright (c) 2007 Bastien Nocera <hadess@hadess.net>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
27 #include "gdk-pixbuf-private.h"
28 #include "gdk-pixbuf-io.h"
30 G_MODULE_EXPORT void fill_vtable (GdkPixbufModule * module);
31 G_MODULE_EXPORT void fill_info (GdkPixbufFormat * info);
37 struct IcnsBlockHeader
40 guint32 size; /* caution: bigendian */
42 typedef struct IcnsBlockHeader IcnsBlockHeader;
45 * load raw icon data from 'icns' resource
47 * returns TRUE when successful
50 load_resources (unsigned size, IN gpointer data, gsize datalen,
51 OUT guchar ** picture, OUT gsize * plen,
52 OUT guchar ** mask, OUT gsize * mlen)
54 IcnsBlockHeader *header = NULL;
55 const char *bytes = NULL;
56 const char *current = NULL;
59 gboolean needs_mask = TRUE;
61 if (datalen < 2 * sizeof (guint32))
66 *picture = *mask = NULL;
70 header = (IcnsBlockHeader *) data;
71 if (memcmp (header->id, "icns", 4) != 0)
74 icnslen = GUINT32_FROM_BE (header->size);
75 if ((icnslen > datalen) || (icnslen < 2 * sizeof (guint32)))
78 current = bytes + sizeof (IcnsBlockHeader);
79 while ((current - bytes < icnslen) && (icnslen - (current - bytes) >= sizeof (IcnsBlockHeader)))
81 header = (IcnsBlockHeader *) current;
82 blocklen = GUINT32_FROM_BE (header->size);
84 /* Check that blocklen isn't garbage */
85 if (blocklen > icnslen - (current - bytes))
92 if (memcmp (header->id, "ic08", 4) == 0 /* 256x256 icon */
93 || memcmp (header->id, "ic09", 4) == 0) /* 512x512 icon */
95 *picture = (gpointer) (current + sizeof (IcnsBlockHeader));
96 *plen = blocklen - sizeof (IcnsBlockHeader);
101 if (memcmp (header->id, "it32", 4) == 0) /* 128x128 icon */
103 *picture = (gpointer) (current + sizeof (IcnsBlockHeader));
104 *plen = blocklen - sizeof (IcnsBlockHeader);
105 if (memcmp (*picture, "\0\0\0\0", 4) == 0)
111 if (memcmp (header->id, "t8mk", 4) == 0) /* 128x128 mask */
113 *mask = (gpointer) (current + sizeof (IcnsBlockHeader));
114 *mlen = blocklen - sizeof (IcnsBlockHeader);
118 if (memcmp (header->id, "ih32", 4) == 0) /* 48x48 icon */
120 *picture = (gpointer) (current + sizeof (IcnsBlockHeader));
121 *plen = blocklen - sizeof (IcnsBlockHeader);
123 if (memcmp (header->id, "h8mk", 4) == 0) /* 48x48 mask */
125 *mask = (gpointer) (current + sizeof (IcnsBlockHeader));
126 *mlen = blocklen - sizeof (IcnsBlockHeader);
130 if (memcmp (header->id, "il32", 4) == 0) /* 32x32 icon */
132 *picture = (gpointer) (current + sizeof (IcnsBlockHeader));
133 *plen = blocklen - sizeof (IcnsBlockHeader);
135 if (memcmp (header->id, "l8mk", 4) == 0) /* 32x32 mask */
137 *mask = (gpointer) (current + sizeof (IcnsBlockHeader));
138 *mlen = blocklen - sizeof (IcnsBlockHeader);
142 if (memcmp (header->id, "is32", 4) == 0) /* 16x16 icon */
144 *picture = (gpointer) (current + sizeof (IcnsBlockHeader));
145 *plen = blocklen - sizeof (IcnsBlockHeader);
147 if (memcmp (header->id, "s8mk", 4) == 0) /* 16x16 mask */
149 *mask = (gpointer) (current + sizeof (IcnsBlockHeader));
150 *mlen = blocklen - sizeof (IcnsBlockHeader);
162 if (needs_mask && !*mask)
168 * uncompress RLE-encoded bytes into RGBA scratch zone:
169 * if firstbyte >= 0x80, it indicates the number of identical bytes + 125
170 * (repeated value is stored next: 1 byte)
171 * otherwise, it indicates the number of non-repeating bytes - 1
172 * (non-repeating values are stored next: n bytes)
175 uncompress (unsigned size, INOUT guchar ** source, OUT guchar * target, INOUT gsize * _remaining)
177 guchar *data = *source;
181 /* The first time we're called, set remaining */
182 if (*_remaining == 0) {
183 remaining = size * size;
185 remaining = *_remaining;
188 while (remaining > 0)
192 if (data[0] & 0x80) /* repeating byte */
194 count = data[0] - 125;
196 if (count > remaining)
199 for (i = 0; i < count; i++)
207 else /* non-repeating bytes */
211 if (count > remaining)
214 for (i = 0; i < count; i++)
216 *target = data[i + 1];
226 *_remaining = remaining;
231 load_icon (unsigned size, IN gpointer data, gsize datalen)
235 gsize isize = 0, msize = 0, i;
236 guchar *image = NULL;
238 if (!load_resources (size, data, datalen, &icon, &isize, &mask, &msize))
241 /* 256x256 icons don't use RLE or uncompressed data,
242 * They're usually JPEG 2000 images */
245 GdkPixbufLoader *loader;
248 loader = gdk_pixbuf_loader_new ();
249 if (!gdk_pixbuf_loader_write (loader, icon, isize, NULL)
250 || !gdk_pixbuf_loader_close (loader, NULL))
252 g_object_unref (loader);
256 pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
257 g_object_ref (pixbuf);
258 g_object_unref (loader);
265 if (msize != size * size) /* wrong mask size */
268 image = (guchar *) g_try_malloc0 (size * size * 4); /* 4 bytes/pixel = RGBA */
273 if (isize == size * size * 4) /* icon data is uncompressed */
274 for (i = 0; i < size * size; i++) /* 4 bytes/pixel = ARGB (A: ignored) */
276 image[i * 4] = icon[4 * i + 1]; /* R */
277 image[i * 4 + 1] = icon[4 * i + 2]; /* G */
278 image[i * 4 + 2] = icon[4 * i + 3]; /* B */
286 if (!uncompress (size, &data, image, &remaining))
289 if (!uncompress (size, &data, image + 1, &remaining))
292 if (!uncompress (size, &data, image + 2, &remaining))
296 for (i = 0; i < size * size; i++) /* copy mask to alpha channel */
297 image[i * 4 + 3] = mask[i];
299 return gdk_pixbuf_new_from_data ((guchar *) image, GDK_COLORSPACE_RGB, /* RGB image */
300 TRUE, /* with alpha channel */
301 8, /* 8 bits per sample */
304 size * 4, /* no gap between rows */
305 (GdkPixbufDestroyNotify)g_free, /* free() function */
306 NULL); /* param to free() function */
313 static int sizes[] = {
314 256, /* late-Tiger icons */
315 128, /* Standard OS X */
316 48, /* Not very common */
317 32, /* Standard Mac OS Classic (8 & 9) */
318 24, /* OS X toolbars */
319 16 /* used in Mac OS Classic and dialog boxes */
323 icns_image_load (FILE *f, GError ** error)
326 GdkPixbuf *pixbuf = NULL;
329 data = g_byte_array_new ();
336 bytes = fread (buf, 1, sizeof (buf), f);
338 data = g_byte_array_append (data, buf, bytes);
344 g_file_error_from_errno (save_errno),
345 _("Error reading ICNS image: %s"),
346 g_strerror (save_errno));
348 g_byte_array_free (data, TRUE);
354 for (i = 0; i < G_N_ELEMENTS(sizes) && !pixbuf; i++)
355 pixbuf = load_icon (sizes[i], data->data, data->len);
357 g_byte_array_free (data, TRUE);
360 g_set_error_literal (error, GDK_PIXBUF_ERROR,
361 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
362 _("Could not decode ICNS file"));
368 #define MODULE_ENTRY(function) G_MODULE_EXPORT void function
370 #define MODULE_ENTRY(function) void _gdk_pixbuf__icns_ ## function
373 MODULE_ENTRY (fill_vtable) (GdkPixbufModule * module)
375 module->load = icns_image_load;
378 MODULE_ENTRY (fill_info) (GdkPixbufFormat * info)
380 static GdkPixbufModulePattern signature[] = {
381 {"icns", NULL, 100}, /* file begins with 'icns' */
384 static gchar *mime_types[] = {
388 static gchar *extensions[] = {
394 info->signature = signature;
395 info->description = N_("The ICNS image format");
396 info->mime_types = mime_types;
397 info->extensions = extensions;
398 info->flags = GDK_PIXBUF_FORMAT_THREADSAFE;
399 info->license = "GPL";
400 info->disabled = FALSE;