1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * GIMP PSD Plug-in
5  * Copyright 2007 by John Marshall
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 /* ----- Known Image Resource Block Types -----
22   All image resources not otherwise handled, including unknown types
23   are added as image parasites.
24   The data is attached as-is from the file (i.e. in big endian order).
25 
26   PSD_PS2_IMAGE_INFO    = 1000,    Dropped    * 0x03e8 - Obsolete - ps 2.0 image info *
27   PSD_MAC_PRINT_INFO    = 1001,    PS Only    * 0x03e9 - Optional - Mac print manager print info record *
28   PSD_PS2_COLOR_TAB     = 1003,    Dropped    * 0x03eb - Obsolete - ps 2.0 indexed color table *
29   PSD_RESN_INFO         = 1005,    Loaded     * 0x03ed - ResolutionInfo structure *
30   PSD_ALPHA_NAMES       = 1006,    Loaded     * 0x03ee - Alpha channel names *
31   PSD_DISPLAY_INFO      = 1007,    Loaded     * 0x03ef - DisplayInfo structure *
32   PSD_CAPTION           = 1008,    Loaded     * 0x03f0 - Optional - Caption string *
33   PSD_BORDER_INFO       = 1009,               * 0x03f1 - Border info *
34   PSD_BACKGROUND_COL    = 1010,               * 0x03f2 - Background color *
35   PSD_PRINT_FLAGS       = 1011,               * 0x03f3 - Print flags *
36   PSD_GREY_HALFTONE     = 1012,               * 0x03f4 - Greyscale and multichannel halftoning info *
37   PSD_COLOR_HALFTONE    = 1013,               * 0x03f5 - Color halftoning info *
38   PSD_DUOTONE_HALFTONE  = 1014,               * 0x03f6 - Duotone halftoning info *
39   PSD_GREY_XFER         = 1015,               * 0x03f7 - Greyscale and multichannel transfer functions *
40   PSD_COLOR_XFER        = 1016,               * 0x03f8 - Color transfer functions *
41   PSD_DUOTONE_XFER      = 1017,               * 0x03f9 - Duotone transfer functions *
42   PSD_DUOTONE_INFO      = 1018,               * 0x03fa - Duotone image information *
43   PSD_EFFECTIVE_BW      = 1019,               * 0x03fb - Effective black & white values for dot range *
44   PSD_OBSOLETE_01       = 1020,    Dropped    * 0x03fc - Obsolete *
45   PSD_EPS_OPT           = 1021,               * 0x03fd - EPS options *
46   PSD_QUICK_MASK        = 1022,    Loaded     * 0x03fe - Quick mask info *
47   PSD_OBSOLETE_02       = 1023,    Dropped    * 0x03ff - Obsolete *
48   PSD_LAYER_STATE       = 1024,    Loaded     * 0x0400 - Layer state info *
49   PSD_WORKING_PATH      = 1025,               * 0x0401 - Working path (not saved) *
50   PSD_LAYER_GROUP       = 1026,               * 0x0402 - Layers group info *
51   PSD_OBSOLETE_03       = 1027,    Dropped    * 0x0403 - Obsolete *
52   PSD_IPTC_NAA_DATA     = 1028,    Loaded     * 0x0404 - IPTC-NAA record (IMV4.pdf) *
53   PSD_IMAGE_MODE_RAW    = 1029,               * 0x0405 - Image mode for raw format files *
54   PSD_JPEG_QUAL         = 1030,    PS Only    * 0x0406 - JPEG quality *
55   PSD_GRID_GUIDE        = 1032,    Loaded     * 0x0408 - Grid & guide info *
56   PSD_THUMB_RES         = 1033,    Special    * 0x0409 - Thumbnail resource *
57   PSD_COPYRIGHT_FLG     = 1034,               * 0x040a - Copyright flag *
58   PSD_URL               = 1035,               * 0x040b - URL string *
59   PSD_THUMB_RES2        = 1036,    Special    * 0x040c - Thumbnail resource *
60   PSD_GLOBAL_ANGLE      = 1037,               * 0x040d - Global angle *
61   PSD_COLOR_SAMPLER     = 1038,               * 0x040e - Color samplers resource *
62   PSD_ICC_PROFILE       = 1039,    Loaded     * 0x040f - ICC Profile *
63   PSD_WATERMARK         = 1040,               * 0x0410 - Watermark *
64   PSD_ICC_UNTAGGED      = 1041,               * 0x0411 - Do not use ICC profile flag *
65   PSD_EFFECTS_VISIBLE   = 1042,               * 0x0412 - Show   hide all effects layers *
66   PSD_SPOT_HALFTONE     = 1043,               * 0x0413 - Spot halftone *
67   PSD_DOC_IDS           = 1044,               * 0x0414 - Document specific IDs *
68   PSD_ALPHA_NAMES_UNI   = 1045,    Loaded     * 0x0415 - Unicode alpha names *
69   PSD_IDX_COL_TAB_CNT   = 1046,    Loaded     * 0x0416 - Indexed color table count *
70   PSD_IDX_TRANSPARENT   = 1047,               * 0x0417 - Index of transparent color (if any) *
71   PSD_GLOBAL_ALT        = 1049,               * 0x0419 - Global altitude *
72   PSD_SLICES            = 1050,               * 0x041a - Slices *
73   PSD_WORKFLOW_URL_UNI  = 1051,               * 0x041b - Workflow URL - Unicode string *
74   PSD_JUMP_TO_XPEP      = 1052,               * 0x041c - Jump to XPEP (?) *
75   PSD_ALPHA_ID          = 1053,    Loaded     * 0x041d - Alpha IDs *
76   PSD_URL_LIST_UNI      = 1054,               * 0x041e - URL list - unicode *
77   PSD_VERSION_INFO      = 1057,               * 0x0421 - Version info *
78   PSD_EXIF_DATA         = 1058,    Loaded     * 0x0422 - Exif data block 1 *
79   PSD_EXIF_DATA_3       = 1059                * 0X0423 - Exif data block 3 (?) *
80   PSD_XMP_DATA          = 1060,    Loaded     * 0x0424 - XMP data block *
81   PSD_CAPTION_DIGEST    = 1061,               * 0x0425 - Caption digest *
82   PSD_PRINT_SCALE       = 1062,               * 0x0426 - Print scale *
83   PSD_PIXEL_AR          = 1064,               * 0x0428 - Pixel aspect ratio *
84   PSD_LAYER_COMPS       = 1065,               * 0x0429 - Layer comps *
85   PSD_ALT_DUOTONE_COLOR = 1066,               * 0x042A - Alternative Duotone colors *
86   PSD_ALT_SPOT_COLOR    = 1067,               * 0x042B - Alternative Spot colors *
87   PSD_LAYER_SELECT_ID   = 1069,               * 0x042D - Layer selection ID *
88   PSD_HDR_TONING_INFO   = 1070,               * 0x042E - HDR toning information *
89   PSD_PRINT_INFO_SCALE  = 1071,               * 0x042F - Print scale *
90   PSD_LAYER_GROUP_E_ID  = 1072,               * 0x0430 - Layer group(s) enabled ID *
91   PSD_COLOR_SAMPLER_NEW = 1073,               * 0x0431 - Color sampler resource for ps CS3 and higher PSD files *
92   PSD_MEASURE_SCALE     = 1074,               * 0x0432 - Measurement scale *
93   PSD_TIMELINE_INFO     = 1075,               * 0x0433 - Timeline information *
94   PSD_SHEET_DISCLOSE    = 1076,               * 0x0434 - Sheet discloser *
95   PSD_DISPLAY_INFO_NEW  = 1077,    Loaded     * 0x0435 - DisplayInfo structure for ps CS3 and higher PSD files *
96   PSD_ONION_SKINS       = 1078,               * 0x0436 - Onion skins *
97   PSD_COUNT_INFO        = 1080,               * 0x0438 - Count information*
98   PSD_PRINT_INFO        = 1082,               * 0x043A - Print information added in ps CS5*
99   PSD_PRINT_STYLE       = 1083,               * 0x043B - Print style *
100   PSD_MAC_NSPRINTINFO   = 1084,               * 0x043C - Mac NSPrintInfo*
101   PSD_WIN_DEVMODE       = 1085,               * 0x043D - Windows DEVMODE *
102   PSD_AUTO_SAVE_PATH    = 1086,               * 0x043E - Auto save file path *
103   PSD_AUTO_SAVE_FORMAT  = 1087,               * 0x043F - Auto save format *
104   PSD_PATH_INFO_FIRST   = 2000,    Loaded     * 0x07d0 - First path info block *
105   PSD_PATH_INFO_LAST    = 2998,    Loaded     * 0x0bb6 - Last path info block *
106   PSD_CLIPPING_PATH     = 2999,               * 0x0bb7 - Name of clipping path *
107   PSD_PLUGIN_R_FIRST    = 4000,               * 0x0FA0 - First plugin resource *
108   PSD_PLUGIN_R_LAST     = 4999,               * 0x1387 - Last plugin resource *
109   PSD_IMAGEREADY_VARS   = 7000,    PS Only    * 0x1B58 - Imageready variables *
110   PSD_IMAGEREADY_DATA   = 7001,    PS Only    * 0x1B59 - Imageready data sets *
111   PSD_LIGHTROOM_WORK    = 8000,    PS Only    * 0x1F40 - Lightroom workflow *
112   PSD_PRINT_FLAGS_2     = 10000               * 0x2710 - Print flags *
113 */
114 
115 #include "config.h"
116 
117 #include <string.h>
118 #include <errno.h>
119 
120 #include <glib/gstdio.h>
121 #include <libgimp/gimp.h>
122 
123 #include <jpeglib.h>
124 #include <jerror.h>
125 
126 #ifdef HAVE_IPTCDATA
127 #include <libiptcdata/iptc-data.h>
128 #endif /* HAVE_IPTCDATA */
129 
130 #include "psd.h"
131 #include "psd-util.h"
132 #include "psd-image-res-load.h"
133 
134 #include "libgimp/stdplugins-intl.h"
135 
136 #define EXIF_HEADER_SIZE 8
137 
138 /*  Local function prototypes  */
139 static gint     load_resource_unknown  (const PSDimageres     *res_a,
140                                         gint32                 image_id,
141                                         FILE                  *f,
142                                         GError               **error);
143 
144 static gint     load_resource_ps_only  (const PSDimageres     *res_a,
145                                         gint32                 image_id,
146                                         FILE                  *f,
147                                         GError               **error);
148 
149 static gint     load_resource_1005     (const PSDimageres     *res_a,
150                                         gint32                 image_id,
151                                         FILE                  *f,
152                                         GError               **error);
153 
154 static gint     load_resource_1006     (const PSDimageres     *res_a,
155                                         gint32                 image_id,
156                                         PSDimage              *img_a,
157                                         FILE                  *f,
158                                         GError               **error);
159 
160 static gint     load_resource_1007     (const PSDimageres     *res_a,
161                                         gint32                 image_id,
162                                         PSDimage              *img_a,
163                                         FILE                  *f,
164                                         GError               **error);
165 
166 static gint     load_resource_1008     (const PSDimageres     *res_a,
167                                         gint32                 image_id,
168                                         FILE                  *f,
169                                         GError               **error);
170 
171 static gint     load_resource_1022     (const PSDimageres     *res_a,
172                                         gint32                 image_id,
173                                         PSDimage              *img_a,
174                                         FILE                  *f,
175                                         GError               **error);
176 
177 static gint     load_resource_1024     (const PSDimageres     *res_a,
178                                         gint32                 image_id,
179                                         PSDimage              *img_a,
180                                         FILE                  *f,
181                                         GError               **error);
182 
183 static gint     load_resource_1028     (const PSDimageres     *res_a,
184                                         gint32                 image_id,
185                                         FILE                  *f,
186                                         GError               **error);
187 
188 static gint     load_resource_1032     (const PSDimageres     *res_a,
189                                         gint32                 image_id,
190                                         FILE                  *f,
191                                         GError               **error);
192 
193 static gint     load_resource_1033     (const PSDimageres     *res_a,
194                                         gint32                 image_id,
195                                         FILE                  *f,
196                                         GError               **error);
197 
198 static gint     load_resource_1039     (const PSDimageres     *res_a,
199                                         PSDimage              *img_a,
200                                         gint32                 image_id,
201                                         FILE                  *f,
202                                         GError               **error);
203 
204 static gint     load_resource_1045     (const PSDimageres     *res_a,
205                                         gint32                 image_id,
206                                         PSDimage              *img_a,
207                                         FILE                  *f,
208                                         GError               **error);
209 
210 static gint     load_resource_1046     (const PSDimageres     *res_a,
211                                         gint32                 image_id,
212                                         FILE                  *f,
213                                         GError               **error);
214 
215 static gint     load_resource_1053     (const PSDimageres     *res_a,
216                                         gint32                 image_id,
217                                         PSDimage              *img_a,
218                                         FILE                  *f,
219                                         GError               **error);
220 
221 static gint     load_resource_1058     (const PSDimageres     *res_a,
222                                         gint32                 image_id,
223                                         FILE                  *f,
224                                         GError               **error);
225 
226 static gint     load_resource_1077     (const PSDimageres     *res_a,
227                                         gint32                 image_id,
228                                         PSDimage              *img_a,
229                                         FILE                  *f,
230                                         GError               **error);
231 
232 static gint     load_resource_2000     (const PSDimageres     *res_a,
233                                         gint32                 image_id,
234                                         FILE                  *f,
235                                         GError               **error);
236 
237 /* Public Functions */
238 gint
get_image_resource_header(PSDimageres * res_a,FILE * f,GError ** error)239 get_image_resource_header (PSDimageres  *res_a,
240                            FILE         *f,
241                            GError      **error)
242 {
243   gint32        read_len;
244   gint32        write_len;
245   gchar        *name;
246 
247   if (fread (&res_a->type, 4, 1, f) < 1
248       || fread (&res_a->id, 2, 1, f) < 1)
249     {
250       psd_set_error (feof (f), errno, error);
251       return -1;
252     }
253   res_a->id = GUINT16_FROM_BE (res_a->id);
254   name = fread_pascal_string (&read_len, &write_len, 2, f, error);
255   if (*error)
256     return -1;
257   if (name != NULL)
258     g_strlcpy (res_a->name, name, write_len + 1);
259   else
260     res_a->name[0] = 0x0;
261   g_free (name);
262   if (fread (&res_a->data_len, 4, 1, f) < 1)
263     {
264       psd_set_error (feof (f), errno, error);
265       return -1;
266     }
267   res_a->data_len = GUINT32_FROM_BE (res_a->data_len);
268   res_a->data_start = ftell (f);
269 
270   IFDBG(2) g_debug ("Type: %.4s, id: %d, start: %d, len: %d",
271                     res_a->type, res_a->id, res_a->data_start, res_a->data_len);
272 
273   return 0;
274 }
275 
276 gint
load_image_resource(PSDimageres * res_a,gint32 image_id,PSDimage * img_a,FILE * f,gboolean * resolution_loaded,gboolean * profile_loaded,GError ** error)277 load_image_resource (PSDimageres  *res_a,
278                      gint32        image_id,
279                      PSDimage     *img_a,
280                      FILE         *f,
281                      gboolean     *resolution_loaded,
282                      gboolean     *profile_loaded,
283                      GError      **error)
284 {
285   gint  pad;
286 
287   /* Set file position to start of image resource data block */
288   if (fseek (f, res_a->data_start, SEEK_SET) < 0)
289     {
290       psd_set_error (feof (f), errno, error);
291       return -1;
292     }
293 
294   /* Process image resource blocks */
295   if (memcmp (res_a->type, "8BIM", 4) != 0 &&
296       memcmp (res_a->type, "MeSa", 4) !=0)
297     {
298       IFDBG(1) g_debug ("Unknown image resource type signature %.4s",
299                         res_a->type);
300     }
301   else
302     {
303       switch (res_a->id)
304         {
305           case PSD_PS2_IMAGE_INFO:
306           case PSD_PS2_COLOR_TAB:
307           case PSD_OBSOLETE_01:
308           case PSD_OBSOLETE_02:
309           case PSD_OBSOLETE_03:
310             /* Drop obsolete image resource blocks */
311             IFDBG(2) g_debug ("Obsolete image resource block: %d",
312                                res_a->id);
313             break;
314 
315           case PSD_THUMB_RES:
316           case PSD_THUMB_RES2:
317             /* Drop thumbnails from standard file load */
318             IFDBG(2) g_debug ("Thumbnail resource block: %d",
319                                res_a->id);
320             break;
321 
322           case PSD_MAC_PRINT_INFO:
323           case PSD_JPEG_QUAL:
324             /* Save photoshop resources with no meaning for GIMP
325               as image parasites */
326             load_resource_ps_only (res_a, image_id, f, error);
327             break;
328 
329           case PSD_RESN_INFO:
330             if (! load_resource_1005 (res_a, image_id, f, error))
331               *resolution_loaded = TRUE;
332             break;
333 
334           case PSD_ALPHA_NAMES:
335             if (! img_a->merged_image_only)
336               load_resource_1006 (res_a, image_id, img_a, f, error);
337             break;
338 
339           case PSD_DISPLAY_INFO:
340             load_resource_1007 (res_a, image_id, img_a, f, error);
341             break;
342 
343           case PSD_CAPTION:
344             load_resource_1008 (res_a, image_id, f, error);
345             break;
346 
347           case PSD_QUICK_MASK:
348             if (! img_a->merged_image_only)
349               load_resource_1022 (res_a, image_id, img_a, f, error);
350             break;
351 
352           case PSD_LAYER_STATE:
353             if (! img_a->merged_image_only)
354               load_resource_1024 (res_a, image_id, img_a, f, error);
355             break;
356 
357           case PSD_WORKING_PATH:
358             if (! img_a->merged_image_only)
359               load_resource_2000 (res_a, image_id, f, error);
360             break;
361 
362           case PSD_IPTC_NAA_DATA:
363             load_resource_1028 (res_a, image_id, f, error);
364             break;
365 
366           case PSD_GRID_GUIDE:
367             if (! img_a->merged_image_only)
368               load_resource_1032 (res_a, image_id, f, error);
369             break;
370 
371           case PSD_ICC_PROFILE:
372             if (! load_resource_1039 (res_a, img_a, image_id, f, error))
373               *profile_loaded = TRUE;
374             break;
375 
376           case PSD_ALPHA_NAMES_UNI:
377             if (! img_a->merged_image_only)
378               load_resource_1045 (res_a, image_id, img_a, f, error);
379             break;
380 
381           case PSD_IDX_COL_TAB_CNT:
382             load_resource_1046 (res_a, image_id, f, error);
383             break;
384 
385           case PSD_ALPHA_ID:
386             if (! img_a->merged_image_only)
387               load_resource_1053 (res_a, image_id, img_a, f, error);
388             break;
389 
390           case PSD_EXIF_DATA:
391             load_resource_1058 (res_a, image_id, f, error);
392             break;
393 
394           case PSD_XMP_DATA:
395             break;
396 
397           case PSD_DISPLAY_INFO_NEW:
398             load_resource_1077 (res_a, image_id, img_a, f, error);
399             break;
400 
401           default:
402             if (res_a->id >= 2000 &&
403                 res_a->id <  2999)
404               load_resource_2000 (res_a, image_id, f, error);
405             else
406               load_resource_unknown (res_a, image_id, f, error);
407         }
408     }
409 
410   /* Image blocks are null padded to even length */
411   if (res_a->data_len % 2 == 0)
412     pad = 0;
413   else
414     pad = 1;
415 
416   /* Set file position to end of image resource block */
417   if (fseek (f, res_a->data_start + res_a->data_len + pad, SEEK_SET) < 0)
418     {
419       psd_set_error (feof (f), errno, error);
420       return -1;
421     }
422 
423   return 0;
424 }
425 
426 gint
load_thumbnail_resource(PSDimageres * res_a,gint32 image_id,FILE * f,GError ** error)427 load_thumbnail_resource (PSDimageres  *res_a,
428                          gint32        image_id,
429                          FILE         *f,
430                          GError      **error)
431 {
432   gint  rtn = 0;
433   gint  pad;
434 
435   /* Set file position to start of image resource data block */
436   if (fseek (f, res_a->data_start, SEEK_SET) < 0)
437     {
438       psd_set_error (feof (f), errno, error);
439       return -1;
440     }
441 
442   /* Process image resource blocks */
443   if (res_a->id == PSD_THUMB_RES
444       || res_a->id == PSD_THUMB_RES2)
445     {
446       /* Load thumbnails from standard file load */
447       load_resource_1033 (res_a, image_id, f, error);
448       rtn = 1;
449     }
450 
451   /* Image blocks are null padded to even length */
452   if (res_a->data_len % 2 == 0)
453     pad = 0;
454   else
455     pad = 1;
456 
457   /* Set file position to end of image resource block */
458   if (fseek (f, res_a->data_start + res_a->data_len + pad, SEEK_SET) < 0)
459     {
460       psd_set_error (feof (f), errno, error);
461       return -1;
462     }
463 
464   return rtn;
465 }
466 
467 /* Private Functions */
468 
469 static gint
load_resource_unknown(const PSDimageres * res_a,gint32 image_id,FILE * f,GError ** error)470 load_resource_unknown (const PSDimageres  *res_a,
471                        gint32              image_id,
472                        FILE               *f,
473                        GError            **error)
474 {
475   /* Unknown image resources attached as parasites to re-save later */
476   GimpParasite  *parasite;
477   gchar         *data;
478   gchar         *name;
479 
480   IFDBG(2) g_debug ("Process unknown image resource block: %d", res_a->id);
481 
482   data = g_malloc (res_a->data_len);
483   if (res_a->data_len > 0 && fread (data, res_a->data_len, 1, f) < 1)
484     {
485       psd_set_error (feof (f), errno, error);
486       g_free (data);
487       return -1;
488     }
489 
490   name = g_strdup_printf ("psd-image-resource-%.4s-%.4x",
491                            res_a->type, res_a->id);
492   IFDBG(2) g_debug ("Parasite name: %s", name);
493 
494   parasite = gimp_parasite_new (name, 0, res_a->data_len, data);
495   gimp_image_attach_parasite (image_id, parasite);
496   gimp_parasite_free (parasite);
497   g_free (data);
498   g_free (name);
499 
500   return 0;
501 }
502 
503 static gint
load_resource_ps_only(const PSDimageres * res_a,gint32 image_id,FILE * f,GError ** error)504 load_resource_ps_only (const PSDimageres  *res_a,
505                        gint32              image_id,
506                        FILE               *f,
507                        GError            **error)
508 {
509   /* Save photoshop resources with no meaning for GIMP as image parasites
510      to re-save later */
511   GimpParasite  *parasite;
512   gchar         *data;
513   gchar         *name;
514 
515   IFDBG(3) g_debug ("Process image resource block: %d", res_a->id);
516 
517   data = g_malloc (res_a->data_len);
518   if (fread (data, res_a->data_len, 1, f) < 1)
519     {
520       psd_set_error (feof (f), errno, error);
521       g_free (data);
522       return -1;
523     }
524 
525   name = g_strdup_printf ("psd-image-resource-%.4s-%.4x",
526                            res_a->type, res_a->id);
527   IFDBG(2) g_debug ("Parasite name: %s", name);
528 
529   parasite = gimp_parasite_new (name, 0, res_a->data_len, data);
530   gimp_image_attach_parasite (image_id, parasite);
531   gimp_parasite_free (parasite);
532   g_free (data);
533   g_free (name);
534 
535   return 0;
536 }
537 
538 static gint
load_resource_1005(const PSDimageres * res_a,gint32 image_id,FILE * f,GError ** error)539 load_resource_1005 (const PSDimageres  *res_a,
540                     gint32              image_id,
541                     FILE               *f,
542                     GError            **error)
543 {
544   /* Load image resolution and unit of measure */
545 
546   /* FIXME  width unit and height unit unused at present */
547 
548   ResolutionInfo        res_info;
549   GimpUnit              image_unit;
550 
551   IFDBG(2) g_debug ("Process image resource block 1005: Resolution Info");
552 
553   if (fread (&res_info.hRes, 4, 1, f) < 1
554       || fread (&res_info.hResUnit, 2, 1, f) < 1
555       || fread (&res_info.widthUnit, 2, 1, f) < 1
556       || fread (&res_info.vRes, 4, 1, f) < 1
557       || fread (&res_info.vResUnit, 2, 1, f) < 1
558       || fread (&res_info.heightUnit, 2, 1, f) < 1)
559     {
560       psd_set_error (feof (f), errno, error);
561       return -1;
562     }
563   res_info.hRes = GINT32_FROM_BE (res_info.hRes);
564   res_info.hResUnit = GINT16_FROM_BE (res_info.hResUnit);
565   res_info.widthUnit = GINT16_FROM_BE (res_info.widthUnit);
566   res_info.vRes = GINT32_FROM_BE (res_info.vRes);
567   res_info.vResUnit = GINT16_FROM_BE (res_info.vResUnit);
568   res_info.heightUnit = GINT16_FROM_BE (res_info.heightUnit);
569 
570   IFDBG(3) g_debug ("Resolution: %d, %d, %d, %d, %d, %d",
571                      res_info.hRes,
572                      res_info.hResUnit,
573                      res_info.widthUnit,
574                      res_info.vRes,
575                      res_info.vResUnit,
576                      res_info.heightUnit);
577 
578   /* Resolution always recorded as pixels / inch in a fixed point implied
579      decimal int32 with 16 bits before point and 16 after (i.e. cast as
580      double and divide resolution by 2^16 */
581   gimp_image_set_resolution (image_id,
582                              res_info.hRes / 65536.0, res_info.vRes / 65536.0);
583 
584   /* GIMP only has one display unit so use ps horizontal resolution unit */
585   switch (res_info.hResUnit)
586     {
587     case PSD_RES_INCH:
588       image_unit = GIMP_UNIT_INCH;
589       break;
590     case PSD_RES_CM:
591       image_unit = GIMP_UNIT_MM;
592       break;
593     default:
594       image_unit = GIMP_UNIT_INCH;
595     }
596 
597   gimp_image_set_unit (image_id, image_unit);
598 
599   return 0;
600 }
601 
602 static gint
load_resource_1006(const PSDimageres * res_a,gint32 image_id,PSDimage * img_a,FILE * f,GError ** error)603 load_resource_1006 (const PSDimageres  *res_a,
604                     gint32              image_id,
605                     PSDimage           *img_a,
606                     FILE               *f,
607                     GError            **error)
608 {
609   /* Load alpha channel names stored as a series of pascal strings
610      unpadded between strings */
611 
612   gchar        *str;
613   gint32        block_rem;
614   gint32        read_len;
615   gint32        write_len;
616 
617   IFDBG(2) g_debug ("Process image resource block 1006: Alpha Channel Names");
618 
619   if (img_a->alpha_names)
620     {
621       IFDBG(3) g_debug ("Alpha names loaded from unicode resource block");
622       return 0;
623     }
624 
625   img_a->alpha_names = g_ptr_array_new ();
626 
627   block_rem = res_a->data_len;
628   while (block_rem > 1)
629     {
630       str = fread_pascal_string (&read_len, &write_len, 1, f, error);
631       if (*error)
632         return -1;
633       IFDBG(3) g_debug ("String: %s, %d, %d", str, read_len, write_len);
634       if (write_len >= 0)
635         {
636           g_ptr_array_add (img_a->alpha_names, (gpointer) str);
637         }
638       block_rem -= read_len;
639     }
640 
641   return 0;
642 }
643 
644 static gint
load_resource_1007(const PSDimageres * res_a,gint32 image_id,PSDimage * img_a,FILE * f,GError ** error)645 load_resource_1007 (const PSDimageres  *res_a,
646                     gint32              image_id,
647                     PSDimage           *img_a,
648                     FILE               *f,
649                     GError            **error)
650 {
651   /* Load alpha channel display info */
652 
653   DisplayInfo       dsp_info;
654   CMColor           ps_color;
655   GimpRGB           gimp_rgb;
656   GimpHSV           gimp_hsv;
657   GimpCMYK          gimp_cmyk;
658   gint16            tot_rec;
659   gint              cidx;
660 
661   IFDBG(2) g_debug ("Process image resource block 1007: Display Info");
662   tot_rec = res_a->data_len / 14;
663   if (tot_rec == 0)
664     return 0;
665 
666   img_a->alpha_display_info = g_new (PSDchanneldata *, tot_rec);
667   img_a->alpha_display_count = tot_rec;
668   for (cidx = 0; cidx < tot_rec; ++cidx)
669     {
670       if (fread (&dsp_info.colorSpace, 2, 1, f) < 1
671           || fread (&dsp_info.color, 8, 1, f) < 1
672           || fread (&dsp_info.opacity, 2, 1, f) < 1
673           || fread (&dsp_info.kind, 1, 1, f) < 1
674           || fread (&dsp_info.padding, 1, 1, f) < 1)
675         {
676           psd_set_error (feof (f), errno, error);
677           return -1;
678         }
679       dsp_info.colorSpace = GINT16_FROM_BE (dsp_info.colorSpace);
680       ps_color.cmyk.cyan = GUINT16_FROM_BE (dsp_info.color[0]);
681       ps_color.cmyk.magenta = GUINT16_FROM_BE (dsp_info.color[1]);
682       ps_color.cmyk.yellow = GUINT16_FROM_BE (dsp_info.color[2]);
683       ps_color.cmyk.black = GUINT16_FROM_BE (dsp_info.color[3]);
684       dsp_info.opacity = GINT16_FROM_BE (dsp_info.opacity);
685 
686       switch (dsp_info.colorSpace)
687         {
688           case PSD_CS_RGB:
689             gimp_rgb_set (&gimp_rgb, ps_color.rgb.red / 65535.0,
690                           ps_color.rgb.green / 65535.0,
691                           ps_color.rgb.blue / 65535.0);
692             break;
693 
694           case PSD_CS_HSB:
695             gimp_hsv_set (&gimp_hsv, ps_color.hsv.hue / 65535.0,
696                           ps_color.hsv.saturation / 65535.0,
697                           ps_color.hsv.value / 65535.0);
698             gimp_hsv_to_rgb (&gimp_hsv, &gimp_rgb);
699             break;
700 
701           case PSD_CS_CMYK:
702             gimp_cmyk_set (&gimp_cmyk, 1.0 - ps_color.cmyk.cyan / 65535.0,
703                            1.0 - ps_color.cmyk.magenta / 65535.0,
704                            1.0 - ps_color.cmyk.yellow / 65535.0,
705                            1.0 - ps_color.cmyk.black / 65535.0);
706             gimp_cmyk_to_rgb (&gimp_cmyk, &gimp_rgb);
707             break;
708 
709           case PSD_CS_GRAYSCALE:
710             gimp_rgb_set (&gimp_rgb, ps_color.gray.gray / 10000.0,
711                           ps_color.gray.gray / 10000.0,
712                           ps_color.gray.gray / 10000.0);
713             break;
714 
715           case PSD_CS_FOCOLTONE:
716           case PSD_CS_TRUMATCH:
717           case PSD_CS_HKS:
718           case PSD_CS_LAB:
719           case PSD_CS_PANTONE:
720           case PSD_CS_TOYO:
721           case PSD_CS_DIC:
722           case PSD_CS_ANPA:
723           default:
724             if (CONVERSION_WARNINGS)
725               g_message ("Unsupported color space: %d",
726                          dsp_info.colorSpace);
727             gimp_rgb_set (&gimp_rgb, 1.0, 0.0, 0.0);
728         }
729 
730       gimp_rgb_set_alpha (&gimp_rgb, 1.0);
731 
732       IFDBG(2) g_debug ("PS cSpace: %d, col: %d %d %d %d, opacity: %d, kind: %d",
733                         dsp_info.colorSpace, ps_color.cmyk.cyan, ps_color.cmyk.magenta,
734                         ps_color.cmyk.yellow, ps_color.cmyk.black, dsp_info.opacity,
735                         dsp_info.kind);
736 
737       IFDBG(2) g_debug ("cSpace: %d, col: %g %g %g, opacity: %d, kind: %d",
738                         dsp_info.colorSpace, gimp_rgb.r * 255 , gimp_rgb.g * 255,
739                         gimp_rgb.b * 255, dsp_info.opacity, dsp_info.kind);
740 
741       img_a->alpha_display_info[cidx] = g_malloc0 (sizeof (PSDchanneldata));
742       img_a->alpha_display_info[cidx]->gimp_color = gimp_rgb;
743       img_a->alpha_display_info[cidx]->opacity = dsp_info.opacity;
744       img_a->alpha_display_info[cidx]->ps_kind = dsp_info.kind;
745       img_a->alpha_display_info[cidx]->ps_cspace = dsp_info.colorSpace;
746       img_a->alpha_display_info[cidx]->ps_color = ps_color;
747     }
748 
749   return 0;
750 }
751 
752 static gint
load_resource_1008(const PSDimageres * res_a,gint32 image_id,FILE * f,GError ** error)753 load_resource_1008 (const PSDimageres  *res_a,
754                     gint32              image_id,
755                     FILE               *f,
756                     GError            **error)
757 {
758   /* Load image caption */
759   GimpParasite  *parasite;
760   gchar         *caption;
761   gint32         read_len;
762   gint32         write_len;
763 
764   IFDBG(2) g_debug ("Process image resource block: 1008: Caption");
765   caption = fread_pascal_string (&read_len, &write_len, 1, f, error);
766   if (*error)
767     return -1;
768 
769   IFDBG(3) g_debug ("Caption: %s", caption);
770   parasite = gimp_parasite_new (GIMP_PARASITE_COMMENT, GIMP_PARASITE_PERSISTENT,
771                                 write_len, caption);
772   gimp_image_attach_parasite (image_id, parasite);
773   gimp_parasite_free (parasite);
774   g_free (caption);
775 
776   return 0;
777 }
778 
779 static gint
load_resource_1022(const PSDimageres * res_a,gint32 image_id,PSDimage * img_a,FILE * f,GError ** error)780 load_resource_1022 (const PSDimageres  *res_a,
781                     gint32              image_id,
782                     PSDimage           *img_a,
783                     FILE               *f,
784                     GError            **error)
785 {
786   /* Load quick mask info */
787   gboolean              quick_mask_empty;       /* Quick mask initially empty */
788 
789   IFDBG(2) g_debug ("Process image resource block: 1022: Quick Mask");
790 
791   if (fread (&img_a->quick_mask_id, 2, 1, f) < 1
792       || fread (&quick_mask_empty, 1, 1, f) < 1)
793     {
794       psd_set_error (feof (f), errno, error);
795       return -1;
796     }
797   img_a->quick_mask_id = GUINT16_FROM_BE (img_a->quick_mask_id);
798 
799   IFDBG(3) g_debug ("Quick mask channel: %d, empty: %d",
800                     img_a->quick_mask_id,
801                     quick_mask_empty);
802 
803   return 0;
804 }
805 
806 static gint
load_resource_1024(const PSDimageres * res_a,gint32 image_id,PSDimage * img_a,FILE * f,GError ** error)807 load_resource_1024 (const PSDimageres  *res_a,
808                     gint32              image_id,
809                     PSDimage           *img_a,
810                     FILE               *f,
811                     GError            **error)
812 {
813   /* Load image layer state - current active layer counting from bottom up */
814   IFDBG(2) g_debug ("Process image resource block: 1024: Layer State");
815 
816   if (fread (&img_a->layer_state, 2, 1, f) < 1)
817     {
818       psd_set_error (feof (f), errno, error);
819       return -1;
820     }
821   img_a->layer_state = GUINT16_FROM_BE (img_a->layer_state);
822 
823   return 0;
824 }
825 
826 static gint
load_resource_1028(const PSDimageres * res_a,gint32 image_id,FILE * f,GError ** error)827 load_resource_1028 (const PSDimageres  *res_a,
828                     gint32              image_id,
829                     FILE               *f,
830                     GError            **error)
831 {
832   /* Load IPTC data block */
833 
834 #ifdef HAVE_IPTCDATA
835   IptcData     *iptc_data;
836   guchar       *iptc_buf;
837   guint         iptc_buf_len;
838 #else
839   gchar        *name;
840 #endif /* HAVE_IPTCDATA */
841 
842   GimpParasite *parasite;
843   gchar        *res_data;
844 
845   IFDBG(2) g_debug ("Process image resource block: 1028: IPTC data");
846 
847   res_data = g_malloc (res_a->data_len);
848   if (fread (res_data, res_a->data_len, 1, f) < 1)
849     {
850       psd_set_error (feof (f), errno, error);
851       g_free (res_data);
852       return -1;
853     }
854 
855 #ifdef HAVE_IPTCDATA
856   /* Load IPTC data structure */
857   iptc_data = iptc_data_new_from_data (res_data, res_a->data_len);
858   IFDBG (3) iptc_data_dump (iptc_data, 0);
859 
860   /* Store resource data as a GIMP IPTC parasite */
861   IFDBG (2) g_debug ("Processing IPTC data as GIMP IPTC parasite");
862   /* Serialize IPTC data */
863   iptc_data_save (iptc_data, &iptc_buf, &iptc_buf_len);
864   if (iptc_buf_len > 0)
865     {
866       parasite = gimp_parasite_new (GIMP_PARASITE_IPTC,
867                                     GIMP_PARASITE_PERSISTENT,
868                                     iptc_buf_len, iptc_buf);
869       gimp_image_attach_parasite (image_id, parasite);
870       gimp_parasite_free (parasite);
871     }
872 
873   iptc_data_unref (iptc_data);
874   g_free (iptc_buf);
875 
876 #else
877   /* Store resource data as a standard psd parasite */
878   IFDBG (2) g_debug ("Processing IPTC data as psd parasite");
879   name = g_strdup_printf ("psd-image-resource-%.4s-%.4x",
880                            res_a->type, res_a->id);
881   IFDBG(3) g_debug ("Parasite name: %s", name);
882 
883   parasite = gimp_parasite_new (name, 0, res_a->data_len, res_data);
884   gimp_image_attach_parasite (image_id, parasite);
885   gimp_parasite_free (parasite);
886   g_free (name);
887 
888 #endif /* HAVE_IPTCDATA */
889 
890   g_free (res_data);
891   return 0;
892 }
893 
894 static gint
load_resource_1032(const PSDimageres * res_a,gint32 image_id,FILE * f,GError ** error)895 load_resource_1032 (const PSDimageres  *res_a,
896                     gint32              image_id,
897                     FILE               *f,
898                     GError            **error)
899 {
900   /* Load grid and guides */
901 
902   /* Grid info is not used (CS2 or earlier) */
903 
904   GuideHeader           hdr;
905   GuideResource         guide;
906   gint                  i;
907 
908   IFDBG(2) g_debug ("Process image resource block 1032: Grid and Guide Info");
909 
910   if (fread (&hdr.fVersion, 4, 1, f) < 1
911       || fread (&hdr.fGridCycleV, 4, 1, f) < 1
912       || fread (&hdr.fGridCycleH, 4, 1, f) < 1
913       || fread (&hdr.fGuideCount, 4, 1, f) < 1)
914     {
915       psd_set_error (feof (f), errno, error);
916       return -1;
917     }
918   hdr.fVersion = GUINT32_FROM_BE (hdr.fVersion);
919   hdr.fGridCycleV = GUINT32_FROM_BE (hdr.fGridCycleV);
920   hdr.fGridCycleH = GUINT32_FROM_BE (hdr.fGridCycleH);
921   hdr.fGuideCount = GUINT32_FROM_BE (hdr.fGuideCount);
922 
923   IFDBG(3) g_debug ("Grids & Guides: %d, %d, %d, %d",
924                      hdr.fVersion,
925                      hdr.fGridCycleV,
926                      hdr.fGridCycleH,
927                      hdr.fGuideCount);
928 
929   for (i = 0; i < hdr.fGuideCount; ++i)
930     {
931       if (fread (&guide.fLocation, 4, 1, f) < 1
932           || fread (&guide.fDirection, 1, 1, f) < 1)
933         {
934           psd_set_error (feof (f), errno, error);
935           return -1;
936         }
937       guide.fLocation = GUINT32_FROM_BE (guide.fLocation);
938       guide.fLocation /= 32;
939 
940       IFDBG(3) g_debug ("Guide: %d px, %d",
941                          guide.fLocation,
942                          guide.fDirection);
943 
944       if (guide.fDirection == PSD_VERTICAL)
945         gimp_image_add_vguide (image_id, guide.fLocation);
946       else
947         gimp_image_add_hguide (image_id, guide.fLocation);
948     }
949 
950   return 0;
951 }
952 
953 static gint
load_resource_1033(const PSDimageres * res_a,gint32 image_id,FILE * f,GError ** error)954 load_resource_1033 (const PSDimageres  *res_a,
955                     gint32              image_id,
956                     FILE               *f,
957                     GError            **error)
958 {
959   /* Load thumbnail image */
960 
961   struct jpeg_decompress_struct cinfo;
962   struct jpeg_error_mgr         jerr;
963 
964   ThumbnailInfo         thumb_info;
965   GeglBuffer           *buffer;
966   const Babl           *format;
967   gint32                layer_id;
968   guchar               *buf;
969   guchar               *rgb_buf;
970   guchar              **rowbuf;
971   gint                  i;
972 
973   IFDBG(2) g_debug ("Process image resource block %d: Thumbnail Image", res_a->id);
974 
975   /* Read thumbnail resource header info */
976   if (fread (&thumb_info.format, 4, 1, f) < 1
977       || fread (&thumb_info.width, 4, 1, f) < 1
978       || fread (&thumb_info.height, 4, 1, f) < 1
979       || fread (&thumb_info.widthbytes, 4, 1, f) < 1
980       || fread (&thumb_info.size, 4, 1, f) < 1
981       || fread (&thumb_info.compressedsize, 4, 1, f) < 1
982       || fread (&thumb_info.bitspixel, 2, 1, f) < 1
983       || fread (&thumb_info.planes, 2, 1, f) < 1)
984     {
985       psd_set_error (feof (f), errno, error);
986       return -1;
987     }
988   thumb_info.format = GINT32_FROM_BE (thumb_info.format);
989   thumb_info.width = GINT32_FROM_BE (thumb_info.width);
990   thumb_info.height = GINT32_FROM_BE (thumb_info.height);
991   thumb_info.widthbytes = GINT32_FROM_BE (thumb_info.widthbytes);
992   thumb_info.size = GINT32_FROM_BE (thumb_info.size);
993   thumb_info.compressedsize = GINT32_FROM_BE (thumb_info.compressedsize);
994   thumb_info.bitspixel = GINT16_FROM_BE (thumb_info.bitspixel);
995   thumb_info.planes = GINT16_FROM_BE (thumb_info.planes);
996 
997   IFDBG(2) g_debug ("\nThumbnail:\n"
998                     "\tFormat: %d\n"
999                     "\tDimensions: %d x %d\n",
1000                      thumb_info.format,
1001                      thumb_info.width,
1002                      thumb_info.height);
1003 
1004   if (thumb_info.format != 1)
1005     {
1006       IFDBG(1) g_debug ("Unknown thumbnail format %d", thumb_info.format);
1007       return -1;
1008     }
1009 
1010   /* Load Jpeg RGB thumbnail info */
1011 
1012   /* Step 1: Allocate and initialize JPEG decompression object */
1013   cinfo.err = jpeg_std_error (&jerr);
1014   jpeg_create_decompress (&cinfo);
1015 
1016   /* Step 2: specify data source (eg, a file) */
1017   jpeg_stdio_src(&cinfo, f);
1018 
1019   /* Step 3: read file parameters with jpeg_read_header() */
1020   jpeg_read_header (&cinfo, TRUE);
1021 
1022   /* Step 4: set parameters for decompression */
1023 
1024 
1025   /* Step 5: Start decompressor */
1026   jpeg_start_decompress (&cinfo);
1027 
1028   /* temporary buffers */
1029   buf = g_new (guchar, cinfo.output_height * cinfo.output_width
1030                * cinfo.output_components);
1031   if (res_a->id == PSD_THUMB_RES)
1032     rgb_buf = g_new (guchar, cinfo.output_height * cinfo.output_width
1033                      * cinfo.output_components);
1034   else
1035     rgb_buf = NULL;
1036   rowbuf = g_new (guchar *, cinfo.output_height);
1037 
1038   for (i = 0; i < cinfo.output_height; ++i)
1039     rowbuf[i] = buf + cinfo.output_width * cinfo.output_components * i;
1040 
1041   /* Create image layer */
1042   gimp_image_resize (image_id, cinfo.output_width, cinfo.output_height, 0, 0);
1043   layer_id = gimp_layer_new (image_id, _("Background"),
1044                              cinfo.output_width,
1045                              cinfo.output_height,
1046                              GIMP_RGB_IMAGE,
1047                              100,
1048                              gimp_image_get_default_new_layer_mode (image_id));
1049   buffer = gimp_drawable_get_buffer (layer_id);
1050   format = babl_format ("R'G'B' u8");
1051 
1052   /* Step 6: while (scan lines remain to be read) */
1053   /*           jpeg_read_scanlines(...); */
1054   while (cinfo.output_scanline < cinfo.output_height)
1055     {
1056       jpeg_read_scanlines (&cinfo,
1057                            (JSAMPARRAY) &rowbuf[cinfo.output_scanline], 1);
1058     }
1059 
1060   if (res_a->id == PSD_THUMB_RES)   /* Order is BGR for resource 1033 */
1061     {
1062       guchar *dst = rgb_buf;
1063       guchar *src = buf;
1064 
1065       for (i = 0; i < gegl_buffer_get_width (buffer) * gegl_buffer_get_height (buffer); ++i)
1066         {
1067           guchar r, g, b;
1068 
1069           r = *(src++);
1070           g = *(src++);
1071           b = *(src++);
1072           *(dst++) = b;
1073           *(dst++) = g;
1074           *(dst++) = r;
1075         }
1076     }
1077 
1078   gegl_buffer_set (buffer, GEGL_RECTANGLE (0, 0,
1079                                            gegl_buffer_get_width (buffer),
1080                                            gegl_buffer_get_height (buffer)),
1081                    0, format, rgb_buf ? rgb_buf : buf, GEGL_AUTO_ROWSTRIDE);
1082 
1083   /* Step 7: Finish decompression */
1084   jpeg_finish_decompress (&cinfo);
1085   /* We can ignore the return value since suspension is not possible
1086    * with the stdio data source.
1087    */
1088 
1089   /* Step 8: Release JPEG decompression object */
1090   jpeg_destroy_decompress (&cinfo);
1091 
1092   /* free up the temporary buffers */
1093   g_free (rowbuf);
1094   g_free (buf);
1095   g_free (rgb_buf);
1096 
1097   /* At this point you may want to check to see whether any
1098    * corrupt-data warnings occurred (test whether
1099    * jerr.num_warnings is nonzero).
1100    */
1101   gimp_image_insert_layer (image_id, layer_id, -1, 0);
1102   g_object_unref (buffer);
1103 
1104   return 0;
1105 }
1106 
1107 static gint
load_resource_1039(const PSDimageres * res_a,PSDimage * img_a,gint32 image_id,FILE * f,GError ** error)1108 load_resource_1039 (const PSDimageres  *res_a,
1109                     PSDimage           *img_a,
1110                     gint32              image_id,
1111                     FILE               *f,
1112                     GError            **error)
1113 {
1114   /* Load ICC profile */
1115   GimpColorProfile *profile;
1116   gchar            *icc_profile;
1117 
1118   IFDBG(2) g_debug ("Process image resource block: 1039: ICC Profile");
1119 
1120   icc_profile = g_malloc (res_a->data_len);
1121   if (fread (icc_profile, res_a->data_len, 1, f) < 1)
1122     {
1123       psd_set_error (feof (f), errno, error);
1124       g_free (icc_profile);
1125       return -1;
1126     }
1127 
1128   profile = gimp_color_profile_new_from_icc_profile ((guint8 *) icc_profile,
1129                                                      res_a->data_len,
1130                                                      NULL);
1131   if (profile)
1132     {
1133       if (img_a->color_mode == PSD_CMYK &&
1134           gimp_color_profile_is_cmyk (profile))
1135         {
1136           img_a->cmyk_profile = profile;
1137         }
1138       else
1139         {
1140           gimp_image_set_color_profile (image_id, profile);
1141           g_object_unref (profile);
1142         }
1143     }
1144 
1145   g_free (icc_profile);
1146 
1147   return 0;
1148 }
1149 
1150 static gint
load_resource_1045(const PSDimageres * res_a,gint32 image_id,PSDimage * img_a,FILE * f,GError ** error)1151 load_resource_1045 (const PSDimageres  *res_a,
1152                     gint32              image_id,
1153                     PSDimage           *img_a,
1154                     FILE               *f,
1155                     GError            **error)
1156 {
1157   /* Load alpha channel names stored as a series of unicode strings
1158      in a GPtrArray */
1159 
1160   gchar        *str;
1161   gint32        block_rem;
1162   gint32        read_len;
1163   gint32        write_len;
1164 
1165   IFDBG(2) g_debug ("Process image resource block 1045: Unicode Alpha Channel Names");
1166 
1167   if (img_a->alpha_names)
1168     {
1169       gint      i;
1170       IFDBG(3) g_debug ("Deleting localised alpha channel names");
1171       for (i = 0; i < img_a->alpha_names->len; ++i)
1172         {
1173           str = g_ptr_array_index (img_a->alpha_names, i);
1174           g_free (str);
1175         }
1176       g_ptr_array_free (img_a->alpha_names, TRUE);
1177     }
1178 
1179   img_a->alpha_names = g_ptr_array_new ();
1180 
1181   block_rem = res_a->data_len;
1182   while (block_rem > 1)
1183     {
1184       str = fread_unicode_string (&read_len, &write_len, 1, f, error);
1185       if (*error)
1186         return -1;
1187 
1188       IFDBG(3) g_debug ("String: %s, %d, %d", str, read_len, write_len);
1189       if (write_len >= 0)
1190         {
1191           g_ptr_array_add (img_a->alpha_names, (gpointer) str);
1192         }
1193       block_rem -= read_len;
1194     }
1195 
1196   return 0;
1197 }
1198 
1199 static gint
load_resource_1046(const PSDimageres * res_a,gint32 image_id,FILE * f,GError ** error)1200 load_resource_1046 (const PSDimageres  *res_a,
1201                     gint32              image_id,
1202                     FILE               *f,
1203                     GError            **error)
1204 {
1205   /* Load indexed color table count */
1206   guchar       *cmap;
1207   gint32        cmap_count = 0;
1208   gint16        index_count = 0;
1209 
1210   IFDBG(2) g_debug ("Process image resource block: 1046: Indexed Color Table Count");
1211 
1212   if (fread (&index_count, 2, 1, f) < 1)
1213     {
1214       psd_set_error (feof (f), errno, error);
1215       return -1;
1216     }
1217   index_count = GINT16_FROM_BE (index_count);
1218 
1219   IFDBG(3) g_debug ("Indexed color table count: %d", index_count);
1220   /* FIXME - check that we have indexed image */
1221   if (index_count && index_count < 256)
1222     {
1223       cmap = gimp_image_get_colormap (image_id, &cmap_count);
1224       if (cmap && index_count < cmap_count)
1225         gimp_image_set_colormap (image_id, cmap, index_count);
1226       g_free (cmap);
1227     }
1228   return 0;
1229 }
1230 
1231 static gint
load_resource_1053(const PSDimageres * res_a,gint32 image_id,PSDimage * img_a,FILE * f,GError ** error)1232 load_resource_1053 (const PSDimageres  *res_a,
1233                     gint32              image_id,
1234                     PSDimage           *img_a,
1235                     FILE               *f,
1236                     GError            **error)
1237 {
1238   /* Load image alpha channel ids (tattoos) */
1239   gint16        tot_rec;
1240   gint16        cidx;
1241 
1242   IFDBG(2) g_debug ("Process image resource block: 1053: Channel ID");
1243 
1244   tot_rec = res_a->data_len / 4;
1245   if (tot_rec ==0)
1246     return 0;
1247 
1248   img_a->alpha_id = g_malloc (sizeof (img_a->alpha_id) * tot_rec);
1249   img_a->alpha_id_count = tot_rec;
1250   for (cidx = 0; cidx < tot_rec; ++cidx)
1251     {
1252       if (fread (&img_a->alpha_id[cidx], 4, 1, f) < 1)
1253         {
1254           psd_set_error (feof (f), errno, error);
1255           return -1;
1256         }
1257       img_a->alpha_id[cidx] = GUINT32_FROM_BE (img_a->alpha_id[cidx]);
1258 
1259       IFDBG(3) g_debug ("Channel id: %d", img_a->alpha_id[cidx]);
1260     }
1261 
1262   return 0;
1263 }
1264 
1265 static gint
load_resource_1058(const PSDimageres * res_a,gint32 image_id,FILE * f,GError ** error)1266 load_resource_1058 (const PSDimageres  *res_a,
1267                     gint32              image_id,
1268                     FILE               *f,
1269                     GError            **error)
1270 {
1271   gchar        *name;
1272 
1273   GimpParasite *parasite;
1274   gchar        *res_data;
1275 
1276   IFDBG(2) g_debug ("Process image resource block: 1058: Exif data");
1277 
1278   res_data = g_malloc (res_a->data_len);
1279   if (fread (res_data, res_a->data_len, 1, f) < 1)
1280     {
1281       psd_set_error (feof (f), errno, error);
1282       g_free (res_data);
1283       return -1;
1284     }
1285 
1286   /* Store resource data as a standard psd parasite */
1287   IFDBG (2) g_debug ("Processing exif data as psd parasite");
1288   name = g_strdup_printf ("psd-image-resource-%.4s-%.4x",
1289                            res_a->type, res_a->id);
1290   IFDBG(3) g_debug ("Parasite name: %s", name);
1291 
1292   parasite = gimp_parasite_new (name, 0, res_a->data_len, res_data);
1293   gimp_image_attach_parasite (image_id, parasite);
1294   gimp_parasite_free (parasite);
1295   g_free (name);
1296 
1297   g_free (res_data);
1298   return 0;
1299 }
1300 
1301 static gint
load_resource_1077(const PSDimageres * res_a,gint32 image_id,PSDimage * img_a,FILE * f,GError ** error)1302 load_resource_1077 (const PSDimageres  *res_a,
1303                     gint32              image_id,
1304                     PSDimage           *img_a,
1305                     FILE               *f,
1306                     GError            **error)
1307 {
1308   /* Load alpha channel display info */
1309 
1310   DisplayInfoNew    dsp_info;
1311   CMColor           ps_color;
1312   GimpRGB           gimp_rgb;
1313   GimpHSV           gimp_hsv;
1314   GimpCMYK          gimp_cmyk;
1315   gint16            tot_rec;
1316   gint              cidx;
1317 
1318   IFDBG(2) g_debug ("Process image resource block 1077: Display Info New");
1319 
1320   /* For now, skip first 4 bytes since intention is unclear. Seems to be
1321      a version number that is always one, but who knows. */
1322   fseek (f, 4, SEEK_CUR);
1323 
1324   tot_rec = res_a->data_len / 13;
1325   if (tot_rec == 0)
1326     return 0;
1327 
1328   img_a->alpha_display_info = g_new (PSDchanneldata *, tot_rec);
1329   img_a->alpha_display_count = tot_rec;
1330   for (cidx = 0; cidx < tot_rec; ++cidx)
1331     {
1332       if (fread (&dsp_info.colorSpace, 2, 1, f) < 1
1333           || fread (&dsp_info.color, 8, 1, f) < 1
1334           || fread (&dsp_info.opacity, 2, 1, f) < 1
1335           || fread (&dsp_info.mode, 1, 1, f) < 1)
1336         {
1337           psd_set_error (feof (f), errno, error);
1338           return -1;
1339         }
1340       dsp_info.colorSpace = GINT16_FROM_BE (dsp_info.colorSpace);
1341       ps_color.cmyk.cyan = GUINT16_FROM_BE (dsp_info.color[0]);
1342       ps_color.cmyk.magenta = GUINT16_FROM_BE (dsp_info.color[1]);
1343       ps_color.cmyk.yellow = GUINT16_FROM_BE (dsp_info.color[2]);
1344       ps_color.cmyk.black = GUINT16_FROM_BE (dsp_info.color[3]);
1345       dsp_info.opacity = GINT16_FROM_BE (dsp_info.opacity);
1346 
1347       switch (dsp_info.colorSpace)
1348         {
1349           case PSD_CS_RGB:
1350             gimp_rgb_set (&gimp_rgb, ps_color.rgb.red / 65535.0,
1351                           ps_color.rgb.green / 65535.0,
1352                           ps_color.rgb.blue / 65535.0);
1353             break;
1354 
1355           case PSD_CS_HSB:
1356             gimp_hsv_set (&gimp_hsv, ps_color.hsv.hue / 65535.0,
1357                           ps_color.hsv.saturation / 65535.0,
1358                           ps_color.hsv.value / 65535.0);
1359             gimp_hsv_to_rgb (&gimp_hsv, &gimp_rgb);
1360             break;
1361 
1362           case PSD_CS_CMYK:
1363             gimp_cmyk_set (&gimp_cmyk, 1.0 - ps_color.cmyk.cyan / 65535.0,
1364                            1.0 - ps_color.cmyk.magenta / 65535.0,
1365                            1.0 - ps_color.cmyk.yellow / 65535.0,
1366                            1.0 - ps_color.cmyk.black / 65535.0);
1367             gimp_cmyk_to_rgb (&gimp_cmyk, &gimp_rgb);
1368             break;
1369 
1370           case PSD_CS_GRAYSCALE:
1371             gimp_rgb_set (&gimp_rgb, ps_color.gray.gray / 10000.0,
1372                           ps_color.gray.gray / 10000.0,
1373                           ps_color.gray.gray / 10000.0);
1374             break;
1375 
1376           case PSD_CS_FOCOLTONE:
1377           case PSD_CS_TRUMATCH:
1378           case PSD_CS_HKS:
1379           case PSD_CS_LAB:
1380           case PSD_CS_PANTONE:
1381           case PSD_CS_TOYO:
1382           case PSD_CS_DIC:
1383           case PSD_CS_ANPA:
1384           default:
1385             if (CONVERSION_WARNINGS)
1386               g_message ("Unsupported color space: %d",
1387                           dsp_info.colorSpace);
1388             gimp_rgb_set (&gimp_rgb, 1.0, 0.0, 0.0);
1389         }
1390 
1391       gimp_rgb_set_alpha (&gimp_rgb, 1.0);
1392 
1393       IFDBG(2) g_debug ("PS cSpace: %d, col: %d %d %d %d, opacity: %d, mode: %d",
1394                         dsp_info.colorSpace, ps_color.cmyk.cyan, ps_color.cmyk.magenta,
1395                         ps_color.cmyk.yellow, ps_color.cmyk.black, dsp_info.opacity,
1396                         dsp_info.mode);
1397 
1398       IFDBG(2) g_debug ("cSpace: %d, col: %g %g %g, opacity: %d, mode: %d",
1399                         dsp_info.colorSpace, gimp_rgb.r * 255 , gimp_rgb.g * 255,
1400                         gimp_rgb.b * 255, dsp_info.opacity, dsp_info.mode);
1401 
1402       img_a->alpha_display_info[cidx] = g_malloc0 (sizeof (PSDchanneldata));
1403       img_a->alpha_display_info[cidx]->gimp_color = gimp_rgb;
1404       img_a->alpha_display_info[cidx]->opacity = dsp_info.opacity;
1405       img_a->alpha_display_info[cidx]->ps_mode = dsp_info.mode;
1406       img_a->alpha_display_info[cidx]->ps_cspace = dsp_info.colorSpace;
1407       img_a->alpha_display_info[cidx]->ps_color = ps_color;
1408     }
1409 
1410   return 0;
1411 }
1412 
1413 static gint
load_resource_2000(const PSDimageres * res_a,gint32 image_id,FILE * f,GError ** error)1414 load_resource_2000 (const PSDimageres  *res_a,
1415                     gint32              image_id,
1416                     FILE               *f,
1417                     GError            **error)
1418 {
1419   gdouble      *controlpoints;
1420   gint32        x[3];
1421   gint32        y[3];
1422   gint32        vector_id = -1;
1423   gint16        type;
1424   gint16        init_fill;
1425   gint16        num_rec;
1426   gint16        path_rec;
1427   gint16        cntr;
1428   gint          image_width;
1429   gint          image_height;
1430   gint          i;
1431   gboolean      closed;
1432 
1433   /* Load path data from image resources 2000-2998 */
1434 
1435   IFDBG(2) g_debug ("Process image resource block: %d :Path data", res_a->id);
1436   path_rec = res_a->data_len / 26;
1437   if (path_rec ==0)
1438     return 0;
1439 
1440   if (fread (&type, 2, 1, f) < 1)
1441     {
1442       psd_set_error (feof (f), errno, error);
1443       return -1;
1444     }
1445   type = GINT16_FROM_BE (type);
1446   if (type != PSD_PATH_FILL_RULE)
1447     {
1448       IFDBG(1) g_debug ("Unexpected path record type: %d", type);
1449       return -1;
1450     }
1451 
1452   if (fseek (f, 24, SEEK_CUR) < 0)
1453     {
1454       psd_set_error (feof (f), errno, error);
1455       return -1;
1456     }
1457 
1458   path_rec--;
1459   if (path_rec ==0)
1460     return 0;
1461 
1462   image_width = gimp_image_width (image_id);
1463   image_height = gimp_image_height (image_id);
1464 
1465   /* Create path */
1466   if (res_a->id == PSD_WORKING_PATH)
1467     {
1468       /* use "Working Path" for the path name to match the Photoshop display */
1469       vector_id = gimp_vectors_new (image_id, "Working Path");
1470     }
1471   else
1472     {
1473       /* Use the name stored in the PSD to name the path */
1474       vector_id = gimp_vectors_new (image_id, res_a->name);
1475     }
1476 
1477   gimp_image_insert_vectors (image_id, vector_id, -1, -1);
1478 
1479   while (path_rec > 0)
1480     {
1481       if (fread (&type, 2, 1, f) < 1)
1482         {
1483           psd_set_error (feof (f), errno, error);
1484           return -1;
1485         }
1486       type = GINT16_FROM_BE (type);
1487       IFDBG(3) g_debug ("Path record type %d", type);
1488 
1489       if (type == PSD_PATH_FILL_RULE)
1490         {
1491           if (fseek (f, 24, SEEK_CUR) < 0)
1492             {
1493               psd_set_error (feof (f), errno, error);
1494               return -1;
1495             }
1496         }
1497 
1498       else if (type == PSD_PATH_FILL_INIT)
1499         {
1500           if (fread (&init_fill, 2, 1, f) < 1)
1501             {
1502               psd_set_error (feof (f), errno, error);
1503               return -1;
1504             }
1505 
1506           if (fseek (f, 22, SEEK_CUR) < 0)
1507             {
1508               psd_set_error (feof (f), errno, error);
1509               return -1;
1510             }
1511         }
1512 
1513       else if (type == PSD_PATH_CL_LEN
1514                || type == PSD_PATH_OP_LEN)
1515         {
1516           if (fread (&num_rec, 2, 1, f) < 1)
1517             {
1518               psd_set_error (feof (f), errno, error);
1519               return -1;
1520             }
1521           num_rec = GINT16_FROM_BE (num_rec);
1522           if (num_rec > path_rec)
1523             {
1524               psd_set_error (feof (f), errno, error);
1525               return - 1;
1526             }
1527           IFDBG(3) g_debug ("Num path records %d", num_rec);
1528 
1529           if (type == PSD_PATH_CL_LEN)
1530             closed = TRUE;
1531           else
1532             closed = FALSE;
1533           cntr = 0;
1534           controlpoints = g_malloc (sizeof (gdouble) * num_rec * 6);
1535           if (fseek (f, 22, SEEK_CUR) < 0)
1536             {
1537               psd_set_error (feof (f), errno, error);
1538               g_free (controlpoints);
1539               return -1;
1540             }
1541 
1542           while (num_rec > 0)
1543             {
1544               if (fread (&type, 2, 1, f) < 1)
1545                 {
1546                   psd_set_error (feof (f), errno, error);
1547                   return -1;
1548                 }
1549               type = GINT16_FROM_BE (type);
1550               IFDBG(3) g_debug ("Path record type %d", type);
1551 
1552               if (type == PSD_PATH_CL_LNK
1553                   || type == PSD_PATH_CL_UNLNK
1554                   || type == PSD_PATH_OP_LNK
1555                   || type == PSD_PATH_OP_UNLNK)
1556                 {
1557                   if (fread (&y[0], 4, 1, f) < 1
1558                       || fread (&x[0], 4, 1, f) < 1
1559                       || fread (&y[1], 4, 1, f) < 1
1560                       || fread (&x[1], 4, 1, f) < 1
1561                       || fread (&y[2], 4, 1, f) < 1
1562                       || fread (&x[2], 4, 1, f) < 1)
1563                     {
1564                       psd_set_error (feof (f), errno, error);
1565                       return -1;
1566                     }
1567                   for (i = 0; i < 3; ++i)
1568                     {
1569                       x[i] = GINT32_FROM_BE (x[i]);
1570                       controlpoints[cntr] = x[i] / 16777216.0 * image_width;
1571                       cntr++;
1572                       y[i] = GINT32_FROM_BE (y[i]);
1573                       controlpoints[cntr] = y[i] / 16777216.0 * image_height;
1574                       cntr++;
1575                     }
1576                   IFDBG(3) g_debug ("Path points (%d,%d), (%d,%d), (%d,%d)",
1577                                     x[0], y[0], x[1], y[1], x[2], y[2]);
1578                 }
1579               else
1580                 {
1581                   IFDBG(1) g_debug ("Unexpected path type record %d", type);
1582                   if (fseek (f, 24, SEEK_CUR) < 0)
1583                     {
1584                       psd_set_error (feof (f), errno, error);
1585                       return -1;
1586                     }
1587                 }
1588               path_rec--;
1589               num_rec--;
1590             }
1591           /* Add sub-path */
1592           gimp_vectors_stroke_new_from_points (vector_id,
1593                                                GIMP_VECTORS_STROKE_TYPE_BEZIER,
1594                                                cntr, controlpoints, closed);
1595           g_free (controlpoints);
1596         }
1597 
1598       else
1599         {
1600           if (fseek (f, 24, SEEK_CUR) < 0)
1601             {
1602               psd_set_error (feof (f), errno, error);
1603               return -1;
1604             }
1605         }
1606 
1607       path_rec--;
1608     }
1609 
1610   return 0;
1611 }
1612