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