2 * Copyright (C) 20019 - The Geeqie Team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * GdkPixbuf library - PSD image loader
25 * Copyright (C) 2008 Jan Dudek
27 * Authors: Jan Dudek <jd@jandudek.com>
29 * This library is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU Lesser General Public
31 * License as published by the Free Software Foundation; either
32 * version 2 of the License, or (at your option) any later version.
34 * This library is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37 * Lesser General Public License for more
38 * You should have received a copy of the GNU Lesser General Public
39 * License along with this library; if not, write to the
40 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
41 * Boston, MA 02111-1307, USA.
46 * - report errors from parse_psd_header
47 * - other color modes (CMYK at least)
53 #include "image-load.h"
54 #include "image_load_psd.h"
56 typedef struct _ImageLoaderPSD ImageLoaderPSD;
57 struct _ImageLoaderPSD {
58 ImageLoaderBackendCbAreaUpdated area_updated_cb;
59 ImageLoaderBackendCbSize size_cb;
60 ImageLoaderBackendCbAreaPrepared area_prepared_cb;
63 guint requested_width;
64 guint requested_height;
70 guchar signature[4]; /* file ID, always "8BPS" */
71 guint16 version; /* version number, always 1 */
73 guint16 channels; /* number of color channels (1-24) */
74 guint32 rows; /* height of image in pixels (1-30000) */
75 guint32 columns; /* width of image in pixels (1-30000) */
76 guint16 depth; /* number of bits per channel (1, 8, 16 or 32) */
77 guint16 color_mode; /* color mode as defined below */
80 #define PSD_HEADER_SIZE 26
85 PSD_MODE_GRAYSCALE = 1,
89 PSD_MODE_MULTICHANNEL = 7,
96 PSD_COMPRESSION_NONE = 0,
97 PSD_COMPRESSION_RLE = 1
103 PSD_STATE_COLOR_MODE_BLOCK,
104 PSD_STATE_RESOURCES_BLOCK,
105 PSD_STATE_LAYERS_BLOCK,
106 PSD_STATE_COMPRESSION,
107 PSD_STATE_LINES_LENGTHS,
108 PSD_STATE_CHANNEL_DATA,
121 guint32 bytes_to_skip;
122 gboolean bytes_to_skip_known;
129 PsdColorMode color_mode;
130 PsdCompressionType compression;
132 guchar** ch_bufs; /* channels buffers */
133 guint curr_ch; /* current channel */
136 guint16* lines_lengths;
142 read_uint16 (guchar* buf)
144 return (buf[0] << 8) | buf[1];
148 read_uint32 (guchar* buf)
150 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
155 * Parse Psdheader from buffer
157 * str is expected to be at least PSD_HEADER_SIZE long
160 psd_parse_header (guchar* str)
164 memcpy(hd.signature, str, 4);
165 hd.version = read_uint16(str + 4);
166 hd.channels = read_uint16(str + 12);
167 hd.rows = read_uint32(str + 14);
168 hd.columns = read_uint32(str + 18);
169 hd.depth = read_uint16(str + 22);
170 hd.color_mode = read_uint16(str + 24);
176 * Attempts to read bytes_needed bytes from data and stores them in buffer.
178 * Returns true if there were enough bytes and false otherwise
179 * (which means we need to call feed_buffer again)
182 feed_buffer (guchar* buffer,
188 guint how_many = bytes_needed - *bytes_read;
189 if (how_many > *size) {
192 memcpy(buffer + *bytes_read, *data, how_many);
193 *bytes_read += how_many;
196 return (*bytes_read == bytes_needed);
200 * Attempts to read size of the block and then skip this block.
202 * Returns true when finishes consuming block data, otherwise false
203 * (false means we need to call skip_block again)
206 skip_block (PsdContext* context, const guchar** data, guint* size)
208 static guint counter;
210 if (!context->bytes_to_skip_known) {
211 context->bytes_read = 0;
212 if (feed_buffer(context->buffer, &context->bytes_read, data, size, 4)) {
213 context->bytes_to_skip = read_uint32(context->buffer);
214 context->bytes_to_skip_known = TRUE;
220 if (*size < context->bytes_to_skip) {
222 context->bytes_to_skip -= *size;
227 counter += context->bytes_to_skip;
228 *size -= context->bytes_to_skip;
229 *data += context->bytes_to_skip;
235 * Decodes RLE-compressed data
238 decompress_line(const guchar* src, guint line_length, guchar* dest)
240 guint16 bytes_read = 0;
242 while (bytes_read < line_length) {
243 gchar byte = src[bytes_read];
248 } else if (byte > -1) {
249 gint count = byte + 1;
251 /* copy next count bytes */
252 for (k = 0; k < count; ++k) {
253 *dest = src[bytes_read];
258 gint count = -byte + 1;
260 /* copy next byte count times */
261 guchar next_byte = src[bytes_read];
263 for (k = 0; k < count; ++k) {
272 reset_context_buffer(PsdContext* ctx)
275 ctx->bytes_to_skip = 0;
276 ctx->bytes_to_skip_known = FALSE;
279 static void free_context(PsdContext *ctx)
282 g_free(ctx->lines_lengths);
285 for (i = 0; i < ctx->channels; i++) {
286 g_free(ctx->ch_bufs[i]);
292 static gboolean image_loader_psd_load(gpointer loader, const guchar *buf, gsize count, GError **error)
294 ImageLoaderPSD *ld = (ImageLoaderPSD *) loader;
295 PsdContext* ctx = g_new0(PsdContext, 1);
300 ctx->state = PSD_STATE_HEADER;
302 /* we'll allocate larger buffer once we know image size */
303 ctx->buffer = g_malloc(PSD_HEADER_SIZE);
304 reset_context_buffer(ctx);
310 ctx->lines_lengths = NULL;
311 ctx->finalized = FALSE;
314 switch (ctx->state) {
315 case PSD_STATE_HEADER:
317 ctx->buffer, &ctx->bytes_read,
318 &buf, &size, PSD_HEADER_SIZE))
320 PsdHeader hd = psd_parse_header(ctx->buffer);
322 ctx->width = hd.columns;
323 ctx->height = hd.rows;
324 ctx->channels = hd.channels;
325 ctx->depth = hd.depth;
326 ctx->depth_bytes = (ctx->depth/8 > 0 ? ctx->depth/8 : 1);
327 ctx->color_mode = hd.color_mode;
329 if (ctx->color_mode != PSD_MODE_RGB
330 && ctx->color_mode != PSD_MODE_GRAYSCALE
331 && ctx->color_mode != PSD_MODE_CMYK
332 && ctx->color_mode != PSD_MODE_DUOTONE
334 log_printf("warning: psd - Unsupported color mode\n");
339 if (ctx->depth != 8 && ctx->depth != 16) {
340 log_printf("warning: psd - Unsupported color depth\n");
345 /* we need buffer that can contain one channel data for one
346 row in RLE compressed format. 2*width should be enough */
348 ctx->buffer = g_malloc(ctx->width * 2 * ctx->depth_bytes);
350 /* this will be needed for RLE decompression */
352 g_malloc(2 * ctx->channels * ctx->height);
354 ctx->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
355 FALSE, 8, ctx->width, ctx->height);
357 if (ctx->lines_lengths == NULL || ctx->buffer == NULL ||
360 log_printf("warning: Insufficient memory to load PSD image file\n");
365 /* create separate buffers for each channel */
366 ctx->ch_bufs = g_malloc(sizeof(guchar*) * ctx->channels);
367 for (i = 0; i < ctx->channels; i++) {
369 g_malloc(ctx->width*ctx->height*ctx->depth_bytes);
371 if (ctx->ch_bufs[i] == NULL) {
372 log_printf("warning: Insufficient memory to load PSD image file\n");
378 ctx->state = PSD_STATE_COLOR_MODE_BLOCK;
379 reset_context_buffer(ctx);
382 case PSD_STATE_COLOR_MODE_BLOCK:
383 if (skip_block(ctx, &buf, &size)) {
384 ctx->state = PSD_STATE_RESOURCES_BLOCK;
385 reset_context_buffer(ctx);
388 case PSD_STATE_RESOURCES_BLOCK:
389 if (skip_block(ctx, &buf, &size)) {
390 ctx->state = PSD_STATE_LAYERS_BLOCK;
391 reset_context_buffer(ctx);
394 case PSD_STATE_LAYERS_BLOCK:
395 if (skip_block(ctx, &buf, &size)) {
396 ctx->state = PSD_STATE_COMPRESSION;
397 reset_context_buffer(ctx);
400 case PSD_STATE_COMPRESSION:
401 if (feed_buffer(ctx->buffer, &ctx->bytes_read, &buf, &size, 2))
403 ctx->compression = read_uint16(ctx->buffer);
405 if (ctx->compression == PSD_COMPRESSION_RLE) {
406 ctx->state = PSD_STATE_LINES_LENGTHS;
407 reset_context_buffer(ctx);
408 } else if (ctx->compression == PSD_COMPRESSION_NONE) {
409 ctx->state = PSD_STATE_CHANNEL_DATA;
410 reset_context_buffer(ctx);
412 log_printf("warning: psd - Unsupported compression type\n");
417 case PSD_STATE_LINES_LENGTHS:
419 (guchar*) ctx->lines_lengths, &ctx->bytes_read, &buf,
420 &size, 2 * ctx->height * ctx->channels))
422 /* convert from different endianness */
423 for (i = 0; i < ctx->height * ctx->channels; i++) {
424 ctx->lines_lengths[i] = read_uint16(
425 (guchar*) &ctx->lines_lengths[i]);
427 ctx->state = PSD_STATE_CHANNEL_DATA;
428 reset_context_buffer(ctx);
431 case PSD_STATE_CHANNEL_DATA:
433 guint line_length = ctx->width * ctx->depth_bytes;
434 if (ctx->compression == PSD_COMPRESSION_RLE) {
435 line_length = ctx->lines_lengths[
436 ctx->curr_ch * ctx->height + ctx->curr_row];
439 if (feed_buffer(ctx->buffer, &ctx->bytes_read, &buf, &size,
442 if (ctx->compression == PSD_COMPRESSION_RLE) {
443 decompress_line(ctx->buffer, line_length,
444 ctx->ch_bufs[ctx->curr_ch] + ctx->pos
447 memcpy(ctx->ch_bufs[ctx->curr_ch] + ctx->pos,
448 ctx->buffer, line_length);
451 ctx->pos += ctx->width * ctx->depth_bytes;
454 if (ctx->curr_row >= ctx->height) {
458 if (ctx->curr_ch >= ctx->channels) {
459 ctx->state = PSD_STATE_DONE;
463 reset_context_buffer(ctx);
474 if (ctx->state == PSD_STATE_DONE && !ctx->finalized) {
475 /* convert or copy channel buffers to our GdkPixbuf */
476 guchar* pixels = gdk_pixbuf_get_pixels(ctx->pixbuf);
477 guint b = ctx->depth_bytes;
479 if (ctx->color_mode == PSD_MODE_RGB) {
480 for (i = 0; i < ctx->height; i++) {
481 for (j = 0; j < ctx->width; j++) {
482 pixels[3*j+0] = ctx->ch_bufs[0][ctx->width*i*b + j*b];
483 pixels[3*j+1] = ctx->ch_bufs[1][ctx->width*i*b + j*b];
484 pixels[3*j+2] = ctx->ch_bufs[2][ctx->width*i*b + j*b];
486 pixels += gdk_pixbuf_get_rowstride(ctx->pixbuf);
488 } else if (ctx->color_mode == PSD_MODE_GRAYSCALE ||
489 ctx->color_mode == PSD_MODE_DUOTONE)
491 for (i = 0; i < ctx->height; i++) {
492 for (j = 0; j < ctx->width; j++) {
493 pixels[3*j+0] = pixels[3*j+1] = pixels[3*j+2] =
494 ctx->ch_bufs[0][ctx->width*i*b + j*b];
496 pixels += gdk_pixbuf_get_rowstride(ctx->pixbuf);
498 } else if (ctx->color_mode == PSD_MODE_CMYK) {
499 /* unfortunately, this doesn't work 100% correctly...
500 CMYK-RGB conversion distorts colors significantly */
502 guchar* pixels = gdk_pixbuf_get_pixels(ctx->pixbuf);
503 for (i = 0; i < ctx->height; i++) {
504 for (j = 0; j < ctx->width; j++) {
506 (double) ctx->ch_bufs[0][ctx->width*i + j] / 255.0;
508 (double) ctx->ch_bufs[1][ctx->width*i + j] / 255.0;
510 (double) ctx->ch_bufs[2][ctx->width*i + j] / 255.0;
512 (double) ctx->ch_bufs[3][ctx->width*i + j] / 255.0;
514 pixels[3*j+0] = (1.0 - (c * (1.0 - k) + k)) * 255.0;
515 pixels[3*j+1] = (1.0 - (m * (1.0 - k) + k)) * 255.0;
516 pixels[3*j+2] = (1.0 - (y * (1.0 - k) + k)) * 255.0;
518 pixels += gdk_pixbuf_get_rowstride(ctx->pixbuf);
521 ctx->finalized = TRUE;
522 ld->pixbuf = ctx->pixbuf;
523 ld->area_updated_cb(loader, 0, 0, ctx->width, ctx->height, ld->data);
533 /* ------- Geeqie ------------ */
535 static gpointer image_loader_psd_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
537 ImageLoaderPSD *loader = g_new0(ImageLoaderPSD, 1);
538 loader->area_updated_cb = area_updated_cb;
539 loader->size_cb = size_cb;
540 loader->area_prepared_cb = area_prepared_cb;
542 return (gpointer) loader;
545 static void image_loader_psd_set_size(gpointer loader, int width, int height)
547 ImageLoaderPSD *ld = (ImageLoaderPSD *) loader;
548 ld->requested_width = width;
549 ld->requested_height = height;
552 static GdkPixbuf* image_loader_psd_get_pixbuf(gpointer loader)
554 ImageLoaderPSD *ld = (ImageLoaderPSD *) loader;
558 static gchar* image_loader_psd_get_format_name(gpointer loader)
560 return g_strdup("psd");
563 static gchar** image_loader_psd_get_format_mime_types(gpointer loader)
565 static gchar *mime[] = {"application/psd", NULL};
566 return g_strdupv(mime);
569 static gboolean image_loader_psd_close(gpointer loader, GError **error)
574 static void image_loader_psd_abort(gpointer loader)
576 ImageLoaderPSD *ld = (ImageLoaderPSD *) loader;
580 static void image_loader_psd_free(gpointer loader)
582 ImageLoaderPSD *ld = (ImageLoaderPSD *) loader;
583 if (ld->pixbuf) g_object_unref(ld->pixbuf);
587 void image_loader_backend_set_psd(ImageLoaderBackend *funcs)
589 funcs->loader_new = image_loader_psd_new;
590 funcs->set_size = image_loader_psd_set_size;
591 funcs->load = image_loader_psd_load;
593 funcs->get_pixbuf = image_loader_psd_get_pixbuf;
594 funcs->close = image_loader_psd_close;
595 funcs->abort = image_loader_psd_abort;
596 funcs->free = image_loader_psd_free;
597 funcs->get_format_name = image_loader_psd_get_format_name;
598 funcs->get_format_mime_types = image_loader_psd_get_format_mime_types;
600 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */