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)
51 #include "image-load-psd.h"
55 #include <gdk-pixbuf/gdk-pixbuf.h>
56 #include <glib-object.h>
60 #include "image-load.h"
65 struct ImageLoaderPSD : public ImageLoaderBackend
68 ~ImageLoaderPSD() override;
70 void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
71 gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
72 GdkPixbuf *get_pixbuf() override;
73 gchar *get_format_name() override;
74 gchar **get_format_mime_types() override;
77 AreaUpdatedCb area_updated_cb;
85 guchar signature[4]; /* file ID, always "8BPS" */
86 guint16 version; /* version number, always 1 */
88 guint16 channels; /* number of color channels (1-24) */
89 guint32 rows; /* height of image in pixels (1-30000) */
90 guint32 columns; /* width of image in pixels (1-30000) */
91 guint16 depth; /* number of bits per channel (1, 8, 16 or 32) */
92 guint16 color_mode; /* color mode as defined below */
95 constexpr guint PSD_HEADER_SIZE = 26;
100 PSD_MODE_GRAYSCALE = 1,
101 PSD_MODE_INDEXED = 2,
104 PSD_MODE_MULTICHANNEL = 7,
105 PSD_MODE_DUOTONE = 8,
109 enum PsdCompressionType
111 PSD_COMPRESSION_NONE = 0,
112 PSD_COMPRESSION_RLE = 1
118 PSD_STATE_COLOR_MODE_BLOCK,
119 PSD_STATE_RESOURCES_BLOCK,
120 PSD_STATE_LAYERS_BLOCK,
121 PSD_STATE_COMPRESSION,
122 PSD_STATE_LINES_LENGTHS,
123 PSD_STATE_CHANNEL_DATA,
136 guint32 bytes_to_skip;
137 gboolean bytes_to_skip_known;
144 PsdColorMode color_mode;
145 PsdCompressionType compression;
147 guchar** ch_bufs; /* channels buffers */
148 guint curr_ch; /* current channel */
151 guint16* lines_lengths;
156 guint16 read_uint16 (const guchar* buf)
158 return (buf[0] << 8) | buf[1];
161 guint32 read_uint32 (const guchar* buf)
163 return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
168 * Parse Psdheader from buffer
170 * str is expected to be at least PSD_HEADER_SIZE long
172 PsdHeader psd_parse_header (guchar* str)
176 memcpy(hd.signature, str, 4);
177 hd.version = read_uint16(str + 4);
178 hd.channels = read_uint16(str + 12);
179 hd.rows = read_uint32(str + 14);
180 hd.columns = read_uint32(str + 18);
181 hd.depth = read_uint16(str + 22);
182 hd.color_mode = read_uint16(str + 24);
188 * Attempts to read bytes_needed bytes from data and stores them in buffer.
190 * Returns true if there were enough bytes and false otherwise
191 * (which means we need to call feed_buffer again)
194 feed_buffer (guchar* buffer,
200 guint how_many = bytes_needed - *bytes_read;
201 if (how_many > *size) {
204 memcpy(buffer + *bytes_read, *data, how_many);
205 *bytes_read += how_many;
208 return (*bytes_read == bytes_needed);
212 * Attempts to read size of the block and then skip this block.
214 * Returns true when finishes consuming block data, otherwise false
215 * (false means we need to call skip_block again)
217 gboolean skip_block (PsdContext* context, const guchar** data, guint* size)
219 if (!context->bytes_to_skip_known) {
220 context->bytes_read = 0;
221 if (feed_buffer(context->buffer, &context->bytes_read, data, size, 4)) {
222 context->bytes_to_skip = read_uint32(context->buffer);
223 context->bytes_to_skip_known = TRUE;
229 if (*size < context->bytes_to_skip)
232 context->bytes_to_skip -= *size;
237 *size -= context->bytes_to_skip;
238 *data += context->bytes_to_skip;
243 * Decodes RLE-compressed data
245 void decompress_line(const guchar* src, guint line_length, guchar* dest)
247 guint16 bytes_read = 0;
249 while (bytes_read < line_length)
251 gchar byte = src[bytes_read];
261 gint count = byte + 1;
263 /* copy next count bytes */
264 for (k = 0; k < count; ++k)
266 *dest = src[bytes_read];
273 gint count = -byte + 1;
275 /* copy next byte count times */
276 guchar next_byte = src[bytes_read];
278 for (k = 0; k < count; ++k)
287 void reset_context_buffer(PsdContext* ctx)
290 ctx->bytes_to_skip = 0;
291 ctx->bytes_to_skip_known = FALSE;
294 void free_context(PsdContext *ctx)
297 g_free(ctx->lines_lengths);
300 for (i = 0; i < ctx->channels; i++) {
301 g_free(ctx->ch_bufs[i]);
307 gboolean ImageLoaderPSD::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
309 auto ctx = g_new0(PsdContext, 1);
314 ctx->state = PSD_STATE_HEADER;
316 /* we'll allocate larger buffer once we know image size */
317 ctx->buffer = static_cast<guchar *>(g_malloc(PSD_HEADER_SIZE));
318 reset_context_buffer(ctx);
320 ctx->ch_bufs = nullptr;
324 ctx->lines_lengths = nullptr;
325 ctx->finalized = FALSE;
328 switch (ctx->state) {
329 case PSD_STATE_HEADER:
331 ctx->buffer, &ctx->bytes_read,
332 &buf, &size, PSD_HEADER_SIZE))
334 PsdHeader hd = psd_parse_header(ctx->buffer);
336 ctx->width = hd.columns;
337 ctx->height = hd.rows;
338 ctx->channels = hd.channels;
339 ctx->depth = hd.depth;
340 ctx->depth_bytes = (ctx->depth/8 > 0 ? ctx->depth/8 : 1);
341 ctx->color_mode = static_cast<PsdColorMode>(hd.color_mode);
343 if (ctx->color_mode != PSD_MODE_RGB
344 && ctx->color_mode != PSD_MODE_GRAYSCALE
345 && ctx->color_mode != PSD_MODE_CMYK
346 && ctx->color_mode != PSD_MODE_DUOTONE
348 log_printf("warning: psd - Unsupported color mode\n");
353 if (ctx->depth != 8 && ctx->depth != 16) {
354 log_printf("warning: psd - Unsupported color depth\n");
359 /* we need buffer that can contain one channel data for one
360 row in RLE compressed format. 2*width should be enough */
362 ctx->buffer = static_cast<guchar *>(g_malloc(ctx->width * 2 * ctx->depth_bytes));
364 /* this will be needed for RLE decompression */
366 static_cast<guint16 *>(g_malloc(2 * ctx->channels * ctx->height));
368 ctx->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
369 FALSE, 8, ctx->width, ctx->height);
371 if (ctx->lines_lengths == nullptr || ctx->buffer == nullptr ||
372 ctx->pixbuf == nullptr)
374 log_printf("warning: Insufficient memory to load PSD image file\n");
379 /* create separate buffers for each channel */
380 ctx->ch_bufs = static_cast<guchar **>(g_malloc(sizeof(guchar*) * ctx->channels));
381 for (i = 0; i < ctx->channels; i++) {
383 static_cast<guchar *>(g_malloc(ctx->width*ctx->height*ctx->depth_bytes));
385 if (ctx->ch_bufs[i] == nullptr) {
386 log_printf("warning: Insufficient memory to load PSD image file\n");
392 ctx->state = PSD_STATE_COLOR_MODE_BLOCK;
393 reset_context_buffer(ctx);
396 case PSD_STATE_COLOR_MODE_BLOCK:
397 if (skip_block(ctx, &buf, &size)) {
398 ctx->state = PSD_STATE_RESOURCES_BLOCK;
399 reset_context_buffer(ctx);
402 case PSD_STATE_RESOURCES_BLOCK:
403 if (skip_block(ctx, &buf, &size)) {
404 ctx->state = PSD_STATE_LAYERS_BLOCK;
405 reset_context_buffer(ctx);
408 case PSD_STATE_LAYERS_BLOCK:
409 if (skip_block(ctx, &buf, &size)) {
410 ctx->state = PSD_STATE_COMPRESSION;
411 reset_context_buffer(ctx);
414 case PSD_STATE_COMPRESSION:
415 if (feed_buffer(ctx->buffer, &ctx->bytes_read, &buf, &size, 2))
417 ctx->compression = static_cast<PsdCompressionType>(read_uint16(ctx->buffer));
419 if (ctx->compression == PSD_COMPRESSION_RLE) {
420 ctx->state = PSD_STATE_LINES_LENGTHS;
421 reset_context_buffer(ctx);
422 } else if (ctx->compression == PSD_COMPRESSION_NONE) {
423 ctx->state = PSD_STATE_CHANNEL_DATA;
424 reset_context_buffer(ctx);
426 log_printf("warning: psd - Unsupported compression type\n");
431 case PSD_STATE_LINES_LENGTHS:
433 reinterpret_cast<guchar*>(ctx->lines_lengths), &ctx->bytes_read, &buf,
434 &size, 2 * ctx->height * ctx->channels))
436 /* convert from different endianness */
437 for (i = 0; i < ctx->height * ctx->channels; i++) {
438 ctx->lines_lengths[i] = read_uint16(
439 reinterpret_cast<guchar*>(&ctx->lines_lengths[i]));
441 ctx->state = PSD_STATE_CHANNEL_DATA;
442 reset_context_buffer(ctx);
445 case PSD_STATE_CHANNEL_DATA:
447 guint line_length = ctx->width * ctx->depth_bytes;
448 if (ctx->compression == PSD_COMPRESSION_RLE) {
449 line_length = ctx->lines_lengths[
450 ctx->curr_ch * ctx->height + ctx->curr_row];
453 if (feed_buffer(ctx->buffer, &ctx->bytes_read, &buf, &size,
456 if (ctx->compression == PSD_COMPRESSION_RLE) {
457 decompress_line(ctx->buffer, line_length,
458 ctx->ch_bufs[ctx->curr_ch] + ctx->pos
461 memcpy(ctx->ch_bufs[ctx->curr_ch] + ctx->pos,
462 ctx->buffer, line_length);
465 ctx->pos += ctx->width * ctx->depth_bytes;
468 if (ctx->curr_row >= ctx->height) {
472 if (ctx->curr_ch >= ctx->channels) {
473 ctx->state = PSD_STATE_DONE;
477 reset_context_buffer(ctx);
488 if (ctx->state == PSD_STATE_DONE && !ctx->finalized) {
489 /* convert or copy channel buffers to our GdkPixbuf */
490 guchar* pixels = gdk_pixbuf_get_pixels(ctx->pixbuf);
491 guint b = ctx->depth_bytes;
493 if (ctx->color_mode == PSD_MODE_RGB) {
494 for (i = 0; i < ctx->height; i++) {
495 for (j = 0; j < ctx->width; j++) {
496 pixels[3*j+0] = ctx->ch_bufs[0][ctx->width*i*b + j*b];
497 pixels[3*j+1] = ctx->ch_bufs[1][ctx->width*i*b + j*b];
498 pixels[3*j+2] = ctx->ch_bufs[2][ctx->width*i*b + j*b];
500 pixels += gdk_pixbuf_get_rowstride(ctx->pixbuf);
502 } else if (ctx->color_mode == PSD_MODE_GRAYSCALE ||
503 ctx->color_mode == PSD_MODE_DUOTONE)
505 for (i = 0; i < ctx->height; i++) {
506 for (j = 0; j < ctx->width; j++) {
507 pixels[3*j+0] = pixels[3*j+1] = pixels[3*j+2] =
508 ctx->ch_bufs[0][ctx->width*i*b + j*b];
510 pixels += gdk_pixbuf_get_rowstride(ctx->pixbuf);
512 } else if (ctx->color_mode == PSD_MODE_CMYK) {
513 /* unfortunately, this doesn't work 100% correctly...
514 CMYK-RGB conversion distorts colors significantly */
516 guchar* pixels = gdk_pixbuf_get_pixels(ctx->pixbuf);
517 for (i = 0; i < ctx->height; i++) {
518 for (j = 0; j < ctx->width; j++) {
520 static_cast<double>(ctx->ch_bufs[0][ctx->width*i + j]) / 255.0;
522 static_cast<double>(ctx->ch_bufs[1][ctx->width*i + j]) / 255.0;
524 static_cast<double>(ctx->ch_bufs[2][ctx->width*i + j]) / 255.0;
526 static_cast<double>(ctx->ch_bufs[3][ctx->width*i + j]) / 255.0;
528 pixels[3*j+0] = (1.0 - (c * (1.0 - k) + k)) * 255.0;
529 pixels[3*j+1] = (1.0 - (m * (1.0 - k) + k)) * 255.0;
530 pixels[3*j+2] = (1.0 - (y * (1.0 - k) + k)) * 255.0;
532 pixels += gdk_pixbuf_get_rowstride(ctx->pixbuf);
535 ctx->finalized = TRUE;
536 pixbuf = ctx->pixbuf;
537 area_updated_cb(nullptr, 0, 0, ctx->width, ctx->height, data);
548 /* ------- Geeqie ------------ */
550 void ImageLoaderPSD::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
552 this->area_updated_cb = area_updated_cb;
556 GdkPixbuf *ImageLoaderPSD::get_pixbuf()
561 gchar *ImageLoaderPSD::get_format_name()
563 return g_strdup("psd");
566 gchar **ImageLoaderPSD::get_format_mime_types()
568 static const gchar *mime[] = {"application/psd", nullptr};
569 return g_strdupv(const_cast<gchar **>(mime));
572 ImageLoaderPSD::~ImageLoaderPSD()
574 if (pixbuf) g_object_unref(pixbuf);
579 std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_psd()
581 return std::make_unique<ImageLoaderPSD>();
583 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */