Let image loader backend decide how to process image buffer
[geeqie.git] / src / image-load-psd.cc
1 /*
2  * Copyright (C) 20019 - The Geeqie Team
3  *
4  * Author: Colin Clark
5  *
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.
10  *
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.
15  *
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.
19  *
20  *
21  * Derived from:
22  *
23  * GdkPixbuf library - PSD image loader
24  *
25  * Copyright (C) 2008 Jan Dudek
26  *
27  * Authors: Jan Dudek <jd@jandudek.com>
28  *
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.
33  *
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.
42  */
43
44 /*
45  * TODO
46  * - report errors from parse_psd_header
47  * - other color modes (CMYK at least)
48  * - i18n
49  */
50
51 #include "image-load-psd.h"
52
53 #include <cstring>
54
55 #include <gdk-pixbuf/gdk-pixbuf.h>
56 #include <glib-object.h>
57 #include <glib.h>
58
59 #include "debug.h"
60 #include "image-load.h"
61
62 namespace
63 {
64
65 struct ImageLoaderPSD {
66         ImageLoaderBackendCbAreaUpdated area_updated_cb;
67         ImageLoaderBackendCbSize size_cb;
68         ImageLoaderBackendCbAreaPrepared area_prepared_cb;
69         gpointer data;
70         GdkPixbuf *pixbuf;
71         guint requested_width;
72         guint requested_height;
73         gboolean abort;
74 };
75
76 struct PsdHeader
77 {
78         guchar  signature[4];  /* file ID, always "8BPS" */
79         guint16 version;       /* version number, always 1 */
80         guchar  resetved[6];
81         guint16 channels;      /* number of color channels (1-24) */
82         guint32 rows;          /* height of image in pixels (1-30000) */
83         guint32 columns;       /* width of image in pixels (1-30000) */
84         guint16 depth;         /* number of bits per channel (1, 8, 16 or 32) */
85         guint16 color_mode;    /* color mode as defined below */
86 };
87
88 enum {
89         PSD_HEADER_SIZE = 26
90 };
91
92 enum PsdColorMode
93 {
94         PSD_MODE_MONO = 0,
95         PSD_MODE_GRAYSCALE = 1,
96         PSD_MODE_INDEXED = 2,
97         PSD_MODE_RGB = 3,
98         PSD_MODE_CMYK = 4,
99         PSD_MODE_MULTICHANNEL = 7,
100         PSD_MODE_DUOTONE = 8,
101         PSD_MODE_LAB = 9,
102 };
103
104 enum PsdCompressionType
105 {
106         PSD_COMPRESSION_NONE = 0,
107         PSD_COMPRESSION_RLE = 1
108 };
109
110 enum PsdReadState
111 {
112         PSD_STATE_HEADER,
113         PSD_STATE_COLOR_MODE_BLOCK,
114         PSD_STATE_RESOURCES_BLOCK,
115         PSD_STATE_LAYERS_BLOCK,
116         PSD_STATE_COMPRESSION,
117         PSD_STATE_LINES_LENGTHS,
118         PSD_STATE_CHANNEL_DATA,
119         PSD_STATE_DONE
120 };
121
122 struct PsdContext
123 {
124         PsdReadState       state;
125
126         GdkPixbuf*                  pixbuf;
127         gpointer                    user_data;
128
129         guchar*            buffer;
130         guint              bytes_read;
131         guint32            bytes_to_skip;
132         gboolean           bytes_to_skip_known;
133
134         guint32            width;
135         guint32            height;
136         guint16            channels;
137         guint16            depth;
138         guint16            depth_bytes;
139         PsdColorMode       color_mode;
140         PsdCompressionType compression;
141
142         guchar**           ch_bufs;       /* channels buffers */
143         guint              curr_ch;       /* current channel */
144         guint              curr_row;
145         guint              pos;
146         guint16*           lines_lengths;
147         gboolean           finalized;
148 };
149
150
151 guint16 read_uint16 (const guchar* buf)
152 {
153         return (buf[0] << 8) | buf[1];
154 }
155
156 guint32 read_uint32 (const guchar* buf)
157 {
158         return (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
159 }
160
161
162 /*
163  * Parse Psdheader from buffer
164  *
165  * str is expected to be at least PSD_HEADER_SIZE long
166  */
167 PsdHeader psd_parse_header (guchar* str)
168 {
169         PsdHeader hd;
170
171         memcpy(hd.signature, str, 4);
172         hd.version = read_uint16(str + 4);
173         hd.channels = read_uint16(str + 12);
174         hd.rows = read_uint32(str + 14);
175         hd.columns = read_uint32(str + 18);
176         hd.depth = read_uint16(str + 22);
177         hd.color_mode = read_uint16(str + 24);
178
179         return hd;
180 }
181
182 /*
183  * Attempts to read bytes_needed bytes from data and stores them in buffer.
184  *
185  * Returns true if there were enough bytes and false otherwise
186  * (which means we need to call feed_buffer again)
187  */
188 gboolean
189 feed_buffer (guchar*        buffer,
190              guint*         bytes_read,
191              const guchar** data,
192              guint*         size,
193              guint          bytes_needed)
194 {
195         guint how_many = bytes_needed - *bytes_read;
196         if (how_many > *size) {
197                 how_many = *size;
198         }
199         memcpy(buffer + *bytes_read, *data, how_many);
200         *bytes_read += how_many;
201         *data += how_many;
202         *size -= how_many;
203         return (*bytes_read == bytes_needed);
204 }
205
206 /*
207  * Attempts to read size of the block and then skip this block.
208  *
209  * Returns true when finishes consuming block data, otherwise false
210  * (false means we need to call skip_block again)
211  */
212 gboolean skip_block (PsdContext* context, const guchar** data, guint* size)
213 {
214         if (!context->bytes_to_skip_known) {
215                 context->bytes_read = 0;
216                 if (feed_buffer(context->buffer, &context->bytes_read, data, size, 4)) {
217                         context->bytes_to_skip = read_uint32(context->buffer);
218                         context->bytes_to_skip_known = TRUE;
219                 } else {
220                         return FALSE;
221                 }
222         }
223
224         if (*size < context->bytes_to_skip)
225                 {
226                 *data += *size;
227                 context->bytes_to_skip -= *size;
228                 *size = 0;
229                 return FALSE;
230                 }
231
232         *size -= context->bytes_to_skip;
233         *data += context->bytes_to_skip;
234         return TRUE;
235 }
236
237 /*
238  * Decodes RLE-compressed data
239  */
240 void decompress_line(const guchar* src, guint line_length, guchar* dest)
241 {
242         guint16 bytes_read = 0;
243         int k;
244         while (bytes_read < line_length)
245                 {
246                 gchar byte = src[bytes_read];
247                 ++bytes_read;
248
249                 if (byte == -128)
250                         {
251                         continue;
252                         }
253
254                 if (byte > -1)
255                         {
256                         gint count = byte + 1;
257
258                         /* copy next count bytes */
259                         for (k = 0; k < count; ++k)
260                                 {
261                                 *dest = src[bytes_read];
262                                 ++dest;
263                                 ++bytes_read;
264                                 }
265                         }
266                 else
267                         {
268                         gint count = -byte + 1;
269
270                         /* copy next byte count times */
271                         guchar next_byte = src[bytes_read];
272                         ++bytes_read;
273                         for (k = 0; k < count; ++k)
274                                 {
275                                 *dest = next_byte;
276                                 ++dest;
277                                 }
278                         }
279                 }
280 }
281
282 void reset_context_buffer(PsdContext* ctx)
283 {
284         ctx->bytes_read = 0;
285         ctx->bytes_to_skip = 0;
286         ctx->bytes_to_skip_known = FALSE;
287 }
288
289 void free_context(PsdContext *ctx)
290 {
291         g_free(ctx->buffer);
292         g_free(ctx->lines_lengths);
293         if (ctx->ch_bufs) {
294                 int i;
295                 for (i = 0; i < ctx->channels; i++) {
296                         g_free(ctx->ch_bufs[i]);
297                 }
298         }
299         g_free(ctx);
300 }
301
302 gboolean image_loader_psd_write(gpointer loader, const guchar *buf, gsize &chunk_size, gsize count, GError **)
303 {
304         auto ld = static_cast<ImageLoaderPSD *>(loader);
305         auto ctx = g_new0(PsdContext, 1);
306         guint i;
307         guint32 j;
308         guint size = count;
309
310         ctx->state = PSD_STATE_HEADER;
311
312         /* we'll allocate larger buffer once we know image size */
313         ctx->buffer = static_cast<guchar *>(g_malloc(PSD_HEADER_SIZE));
314         reset_context_buffer(ctx);
315
316         ctx->ch_bufs = nullptr;
317         ctx->curr_ch = 0;
318         ctx->curr_row = 0;
319         ctx->pos = 0;
320         ctx->lines_lengths = nullptr;
321         ctx->finalized = FALSE;
322
323         while (size > 0) {
324                 switch (ctx->state) {
325                         case PSD_STATE_HEADER:
326                                 if (feed_buffer(
327                                                 ctx->buffer, &ctx->bytes_read,
328                                                 &buf, &size, PSD_HEADER_SIZE))
329                                 {
330                                         PsdHeader hd = psd_parse_header(ctx->buffer);
331
332                                         ctx->width = hd.columns;
333                                         ctx->height = hd.rows;
334                                         ctx->channels = hd.channels;
335                                         ctx->depth = hd.depth;
336                                         ctx->depth_bytes = (ctx->depth/8 > 0 ? ctx->depth/8 : 1);
337                                         ctx->color_mode = static_cast<PsdColorMode>(hd.color_mode);
338
339                                         if (ctx->color_mode != PSD_MODE_RGB
340                                             && ctx->color_mode != PSD_MODE_GRAYSCALE
341                                             && ctx->color_mode != PSD_MODE_CMYK
342                                             && ctx->color_mode != PSD_MODE_DUOTONE
343                                         ) {
344                                                 log_printf("warning: psd - Unsupported color mode\n");
345                                                 free_context(ctx);
346                                                 return FALSE;
347                                         }
348
349                                         if (ctx->depth != 8 && ctx->depth != 16) {
350                                                 log_printf("warning: psd - Unsupported color depth\n");
351                                                 free_context(ctx);
352                                                 return FALSE;
353                                         }
354
355                                         /* we need buffer that can contain one channel data for one
356                                            row in RLE compressed format. 2*width should be enough */
357                                         g_free(ctx->buffer);
358                                         ctx->buffer = static_cast<guchar *>(g_malloc(ctx->width * 2 * ctx->depth_bytes));
359
360                                         /* this will be needed for RLE decompression */
361                                         ctx->lines_lengths =
362                                                 static_cast<guint16 *>(g_malloc(2 * ctx->channels * ctx->height));
363
364                                         ctx->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
365                                                 FALSE, 8, ctx->width, ctx->height);
366
367                                         if (ctx->lines_lengths == nullptr || ctx->buffer == nullptr ||
368                                                 ctx->pixbuf == nullptr)
369                                         {
370                                                 log_printf("warning: Insufficient memory to load PSD image file\n");
371                                                 free_context(ctx);
372                                                 return FALSE;
373                                         }
374
375                                         /* create separate buffers for each channel */
376                                         ctx->ch_bufs = static_cast<guchar **>(g_malloc(sizeof(guchar*) * ctx->channels));
377                                         for (i = 0; i < ctx->channels; i++) {
378                                                 ctx->ch_bufs[i] =
379                                                         static_cast<guchar *>(g_malloc(ctx->width*ctx->height*ctx->depth_bytes));
380
381                                                 if (ctx->ch_bufs[i] == nullptr) {
382                                                 log_printf("warning: Insufficient memory to load PSD image file\n");
383                                                 free_context(ctx);
384                                                 return FALSE;
385                                                 }
386                                         }
387
388                                         ctx->state = PSD_STATE_COLOR_MODE_BLOCK;
389                                         reset_context_buffer(ctx);
390                                 }
391                                 break;
392                         case PSD_STATE_COLOR_MODE_BLOCK:
393                                 if (skip_block(ctx, &buf, &size)) {
394                                         ctx->state = PSD_STATE_RESOURCES_BLOCK;
395                                         reset_context_buffer(ctx);
396                                 }
397                                 break;
398                         case PSD_STATE_RESOURCES_BLOCK:
399                                 if (skip_block(ctx, &buf, &size)) {
400                                         ctx->state = PSD_STATE_LAYERS_BLOCK;
401                                         reset_context_buffer(ctx);
402                                 }
403                                 break;
404                         case PSD_STATE_LAYERS_BLOCK:
405                                 if (skip_block(ctx, &buf, &size)) {
406                                         ctx->state = PSD_STATE_COMPRESSION;
407                                         reset_context_buffer(ctx);
408                                 }
409                                 break;
410                         case PSD_STATE_COMPRESSION:
411                                 if (feed_buffer(ctx->buffer, &ctx->bytes_read, &buf, &size, 2))
412                                 {
413                                         ctx->compression = static_cast<PsdCompressionType>(read_uint16(ctx->buffer));
414
415                                         if (ctx->compression == PSD_COMPRESSION_RLE) {
416                                                 ctx->state = PSD_STATE_LINES_LENGTHS;
417                                                 reset_context_buffer(ctx);
418                                         } else if (ctx->compression == PSD_COMPRESSION_NONE) {
419                                                 ctx->state = PSD_STATE_CHANNEL_DATA;
420                                                 reset_context_buffer(ctx);
421                                         } else {
422                                                 log_printf("warning: psd - Unsupported compression type\n");
423                                                 return FALSE;
424                                         }
425                                 }
426                                 break;
427                         case PSD_STATE_LINES_LENGTHS:
428                                 if (feed_buffer(
429                                                 reinterpret_cast<guchar*>(ctx->lines_lengths), &ctx->bytes_read, &buf,
430                                                  &size, 2 * ctx->height * ctx->channels))
431                                 {
432                                         /* convert from different endianness */
433                                         for (i = 0; i < ctx->height * ctx->channels; i++) {
434                                                 ctx->lines_lengths[i] = read_uint16(
435                                                         reinterpret_cast<guchar*>(&ctx->lines_lengths[i]));
436                                         }
437                                         ctx->state = PSD_STATE_CHANNEL_DATA;
438                                         reset_context_buffer(ctx);
439                                 }
440                                 break;
441                         case PSD_STATE_CHANNEL_DATA:
442                                 {
443                                         guint line_length = ctx->width * ctx->depth_bytes;
444                                         if (ctx->compression == PSD_COMPRESSION_RLE) {
445                                                 line_length = ctx->lines_lengths[
446                                                         ctx->curr_ch * ctx->height + ctx->curr_row];
447                                         }
448
449                                         if (feed_buffer(ctx->buffer, &ctx->bytes_read, &buf, &size,
450                                                         line_length))
451                                         {
452                                                 if (ctx->compression == PSD_COMPRESSION_RLE) {
453                                                         decompress_line(ctx->buffer, line_length,
454                                                                 ctx->ch_bufs[ctx->curr_ch] + ctx->pos
455                                                         );
456                                                 } else {
457                                                         memcpy(ctx->ch_bufs[ctx->curr_ch] + ctx->pos,
458                                                                 ctx->buffer, line_length);
459                                                 }
460
461                                                 ctx->pos += ctx->width * ctx->depth_bytes;
462                                                 ++ctx->curr_row;
463
464                                                 if (ctx->curr_row >= ctx->height) {
465                                                         ++ctx->curr_ch;
466                                                         ctx->curr_row = 0;
467                                                         ctx->pos = 0;
468                                                         if (ctx->curr_ch >= ctx->channels) {
469                                                                 ctx->state = PSD_STATE_DONE;
470                                                         }
471                                                 }
472
473                                                 reset_context_buffer(ctx);
474                                         }
475                                 }
476                                 break;
477                         case PSD_STATE_DONE:
478                         default:
479                                 size = 0;
480                                 break;
481                 }
482         }
483
484         if (ctx->state == PSD_STATE_DONE && !ctx->finalized) {
485                 /* convert or copy channel buffers to our GdkPixbuf */
486                 guchar* pixels = gdk_pixbuf_get_pixels(ctx->pixbuf);
487                 guint b = ctx->depth_bytes;
488
489                 if (ctx->color_mode == PSD_MODE_RGB) {
490                         for (i = 0; i < ctx->height; i++) {
491                                 for (j = 0; j < ctx->width; j++) {
492                                         pixels[3*j+0] = ctx->ch_bufs[0][ctx->width*i*b + j*b];
493                                         pixels[3*j+1] = ctx->ch_bufs[1][ctx->width*i*b + j*b];
494                                         pixels[3*j+2] = ctx->ch_bufs[2][ctx->width*i*b + j*b];
495                                 }
496                                 pixels += gdk_pixbuf_get_rowstride(ctx->pixbuf);
497                         }
498                 } else if (ctx->color_mode == PSD_MODE_GRAYSCALE ||
499                            ctx->color_mode == PSD_MODE_DUOTONE)
500                 {
501                         for (i = 0; i < ctx->height; i++) {
502                                 for (j = 0; j < ctx->width; j++) {
503                                         pixels[3*j+0] = pixels[3*j+1] = pixels[3*j+2] =
504                                                 ctx->ch_bufs[0][ctx->width*i*b + j*b];
505                                 }
506                                 pixels += gdk_pixbuf_get_rowstride(ctx->pixbuf);
507                         }
508                 } else if (ctx->color_mode == PSD_MODE_CMYK) {
509                         /* unfortunately, this doesn't work 100% correctly...
510                            CMYK-RGB conversion distorts colors significantly  */
511
512                         guchar* pixels = gdk_pixbuf_get_pixels(ctx->pixbuf);
513                         for (i = 0; i < ctx->height; i++) {
514                                 for (j = 0; j < ctx->width; j++) {
515                                         double c = 1.0 -
516                                                 static_cast<double>(ctx->ch_bufs[0][ctx->width*i + j]) / 255.0;
517                                         double m = 1.0 -
518                                                 static_cast<double>(ctx->ch_bufs[1][ctx->width*i + j]) / 255.0;
519                                         double y = 1.0 -
520                                                 static_cast<double>(ctx->ch_bufs[2][ctx->width*i + j]) / 255.0;
521                                         double k = 1.0 -
522                                                 static_cast<double>(ctx->ch_bufs[3][ctx->width*i + j]) / 255.0;
523
524                                         pixels[3*j+0] = (1.0 - (c * (1.0 - k) + k)) * 255.0;
525                                         pixels[3*j+1] = (1.0 - (m * (1.0 - k) + k)) * 255.0;
526                                         pixels[3*j+2] = (1.0 - (y * (1.0 - k) + k)) * 255.0;
527                                 }
528                                 pixels += gdk_pixbuf_get_rowstride(ctx->pixbuf);
529                         }
530                 }
531                 ctx->finalized = TRUE;
532                 ld->pixbuf = ctx->pixbuf;
533                 ld->area_updated_cb(loader, 0, 0, ctx->width, ctx->height, ld->data);
534                 free_context(ctx);
535
536                 chunk_size = count;
537                 return TRUE;
538         }
539
540         free_context(ctx);
541         return FALSE;
542 }
543
544 /* ------- Geeqie ------------ */
545
546 gpointer image_loader_psd_new(ImageLoaderBackendCbAreaUpdated area_updated_cb, ImageLoaderBackendCbSize size_cb, ImageLoaderBackendCbAreaPrepared area_prepared_cb, gpointer data)
547 {
548         auto loader = g_new0(ImageLoaderPSD, 1);
549         loader->area_updated_cb = area_updated_cb;
550         loader->size_cb = size_cb;
551         loader->area_prepared_cb = area_prepared_cb;
552         loader->data = data;
553         return loader;
554 }
555
556 void image_loader_psd_set_size(gpointer loader, int width, int height)
557 {
558         auto ld = static_cast<ImageLoaderPSD *>(loader);
559         ld->requested_width = width;
560         ld->requested_height = height;
561 }
562
563 GdkPixbuf* image_loader_psd_get_pixbuf(gpointer loader)
564 {
565         auto ld = static_cast<ImageLoaderPSD *>(loader);
566         return ld->pixbuf;
567 }
568
569 gchar* image_loader_psd_get_format_name(gpointer)
570 {
571         return g_strdup("psd");
572 }
573
574 gchar** image_loader_psd_get_format_mime_types(gpointer)
575 {
576         static const gchar *mime[] = {"application/psd", nullptr};
577         return g_strdupv(const_cast<gchar **>(mime));
578 }
579
580 gboolean image_loader_psd_close(gpointer, GError **)
581 {
582         return TRUE;
583 }
584
585 void image_loader_psd_abort(gpointer loader)
586 {
587         auto ld = static_cast<ImageLoaderPSD *>(loader);
588         ld->abort = TRUE;
589 }
590
591 void image_loader_psd_free(gpointer loader)
592 {
593         auto ld = static_cast<ImageLoaderPSD *>(loader);
594         if (ld->pixbuf) g_object_unref(ld->pixbuf);
595         g_free(ld);
596 }
597
598 } // namespace
599
600 void image_loader_backend_set_psd(ImageLoaderBackend *funcs)
601 {
602         funcs->loader_new = image_loader_psd_new;
603         funcs->set_size = image_loader_psd_set_size;
604         funcs->write = image_loader_psd_write;
605         funcs->get_pixbuf = image_loader_psd_get_pixbuf;
606         funcs->close = image_loader_psd_close;
607         funcs->abort = image_loader_psd_abort;
608         funcs->free = image_loader_psd_free;
609         funcs->get_format_name = image_loader_psd_get_format_name;
610         funcs->get_format_mime_types = image_loader_psd_get_format_mime_types;
611 }
612 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */