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 #include "config.h"
22 
23 #include <string.h>
24 #include <errno.h>
25 
26 #include <glib/gstdio.h>
27 #include <libgimp/gimp.h>
28 
29 #include "psd.h"
30 #include "psd-util.h"
31 
32 #include "libgimp/stdplugins-intl.h"
33 
34 /*  Local constants  */
35 #define MIN_RUN     3
36 
37 /*  Local types  */
38 typedef struct
39 {
40   const gchar   *name;
41   const gchar   *psd_mode;
42   GimpLayerMode  gimp_mode;
43   gboolean       exact; /* does the modes behave (more-or-less) the same in
44                          * Photoshop and in GIMP?
45                          */
46 } LayerModeMapping;
47 
48 /*  Local function prototypes  */
49 static const gchar * get_enum_value_nick (GType type,
50                                           gint  value);
51 
52 /*  Local variables  */
53 
54 /* mapping table between Photoshop and GIMP modes.  in case a mode matches more
55  * than one entry (in either direction), the first entry wins.
56  */
57 static const LayerModeMapping layer_mode_map[] =
58 {
59 /*  Name             PSD     GIMP                                   Exact?  */
60 
61   /* Normal (ps3) */
62   { "Normal",        "norm", GIMP_LAYER_MODE_NORMAL,                TRUE },
63   { "Normal",        "norm", GIMP_LAYER_MODE_NORMAL_LEGACY,         TRUE },
64 
65   /* Dissolve (ps3) */
66   { "Dissolve",      "diss", GIMP_LAYER_MODE_DISSOLVE,              TRUE },
67 
68   /* Multiply (ps3) */
69   { "Multiply",      "mul ", GIMP_LAYER_MODE_MULTIPLY,              TRUE },
70   { "Multiply",      "mul ", GIMP_LAYER_MODE_MULTIPLY_LEGACY,       TRUE },
71 
72   /* Screen (ps3) */
73   { "Screen",        "scrn", GIMP_LAYER_MODE_SCREEN,                TRUE },
74   { "Screen",        "scrn", GIMP_LAYER_MODE_SCREEN_LEGACY,         TRUE },
75 
76   /* Overlay (ps3) */
77   { "Overlay",       "over", GIMP_LAYER_MODE_OVERLAY,               TRUE },
78 
79   /* Difference (ps3) */
80   { "Difference",    "diff", GIMP_LAYER_MODE_DIFFERENCE,            TRUE },
81   { "Difference",    "diff", GIMP_LAYER_MODE_DIFFERENCE_LEGACY,     TRUE },
82 
83   /* Linear Dodge (cs2) */
84   { "Linear Dodge",  "lddg", GIMP_LAYER_MODE_ADDITION,              TRUE },
85   { "Linear Dodge",  "lddg", GIMP_LAYER_MODE_ADDITION_LEGACY,       TRUE },
86 
87   /* Subtract (??) */
88   { "Subtract",      "fsub", GIMP_LAYER_MODE_SUBTRACT,              TRUE },
89   { "Subtract",      "fsub", GIMP_LAYER_MODE_SUBTRACT_LEGACY,       TRUE },
90 
91   /* Darken (ps3) */
92   { "Darken",        "dark", GIMP_LAYER_MODE_DARKEN_ONLY,           TRUE },
93   { "Darken",        "dark", GIMP_LAYER_MODE_DARKEN_ONLY_LEGACY,    TRUE },
94 
95   /* Lighten (ps3) */
96   { "Ligten",        "lite", GIMP_LAYER_MODE_LIGHTEN_ONLY,          TRUE },
97   { "Ligten",        "lite", GIMP_LAYER_MODE_LIGHTEN_ONLY_LEGACY,   TRUE },
98 
99   /* Hue (ps3) */
100   { "Hue",           "hue ", GIMP_LAYER_MODE_LCH_HUE,               FALSE },
101   { "Hue",           "hue ", GIMP_LAYER_MODE_HSV_HUE,               FALSE },
102   { "Hue",           "hue ", GIMP_LAYER_MODE_HSV_HUE_LEGACY,        FALSE },
103 
104   /* Stauration (ps3) */
105   { "Saturation",    "sat ", GIMP_LAYER_MODE_LCH_CHROMA,            FALSE },
106   { "Saturation",    "sat ", GIMP_LAYER_MODE_HSV_SATURATION,        FALSE },
107   { "Saturation",    "sat ", GIMP_LAYER_MODE_HSV_SATURATION_LEGACY, FALSE },
108 
109   /* Color (ps3) */
110   { "Color",         "colr", GIMP_LAYER_MODE_LCH_COLOR,             FALSE },
111   { "Color",         "colr", GIMP_LAYER_MODE_HSL_COLOR,             FALSE },
112   { "Color",         "colr", GIMP_LAYER_MODE_HSL_COLOR_LEGACY,      FALSE },
113 
114   /* Luminosity (ps3) */
115   { "Luminosity",    "lum ", GIMP_LAYER_MODE_LCH_LIGHTNESS,         FALSE },
116   { "Luminosity",    "lum ", GIMP_LAYER_MODE_HSV_VALUE,             FALSE },
117   { "Luminosity",    "lum ", GIMP_LAYER_MODE_HSV_VALUE_LEGACY,      FALSE },
118   { "Luminosity",    "lum ", GIMP_LAYER_MODE_LUMINANCE,             FALSE },
119 
120   /* Divide (??) */
121   { "Divide",        "fdiv", GIMP_LAYER_MODE_DIVIDE,                TRUE },
122   { "Divide",        "fdiv", GIMP_LAYER_MODE_DIVIDE_LEGACY,         TRUE },
123 
124   /* Color Dodge (ps6) */
125   { "Color Dodge",   "div ", GIMP_LAYER_MODE_DODGE,                 TRUE },
126   { "Color Dodge",   "div ", GIMP_LAYER_MODE_DODGE_LEGACY,          TRUE },
127 
128   /* Color Burn (ps6) */
129   { "Color Burn",    "idiv", GIMP_LAYER_MODE_BURN,                  TRUE },
130   { "Color Burn",    "idiv", GIMP_LAYER_MODE_BURN_LEGACY,           TRUE },
131 
132   /* Hard Light (ps3) */
133   { "Hard Light",    "hLit", GIMP_LAYER_MODE_HARDLIGHT,             TRUE },
134   { "Hard Light",    "hLit", GIMP_LAYER_MODE_HARDLIGHT_LEGACY,      TRUE },
135 
136   /* Soft Light (ps3) */
137   { "Soft Light",    "sLit", GIMP_LAYER_MODE_SOFTLIGHT,             FALSE },
138   { "Soft Light",    "sLit", GIMP_LAYER_MODE_SOFTLIGHT_LEGACY,      FALSE },
139   { "Soft Light",    "sLit", GIMP_LAYER_MODE_OVERLAY_LEGACY,        FALSE },
140 
141   /* Vivid Light (ps7)*/
142   { "Vivid Light",   "vLit", GIMP_LAYER_MODE_VIVID_LIGHT,           TRUE },
143 
144   /* Pin Light (ps7)*/
145   { "Pin Light",     "pLit", GIMP_LAYER_MODE_PIN_LIGHT,             TRUE },
146 
147   /* Linear Light (ps7)*/
148   { "Linear Light",  "lLit", GIMP_LAYER_MODE_LINEAR_LIGHT,          TRUE },
149 
150   /* Hard Mix (CS)*/
151   { "Hard Mix",      "hMix", GIMP_LAYER_MODE_HARD_MIX,              TRUE },
152 
153   /* Exclusion (ps6) */
154   { "Exclusion",     "smud", GIMP_LAYER_MODE_EXCLUSION,             TRUE },
155 
156   /* Linear Burn (ps7)*/
157   { "Linear Burn",   "lbrn", GIMP_LAYER_MODE_LINEAR_BURN,           TRUE },
158 
159   /* Darker Color (??)*/
160   { "Darker Color",  "dkCl", GIMP_LAYER_MODE_LUMA_DARKEN_ONLY,      FALSE },
161 
162   /* Lighter Color (??)*/
163   { "Lighter Color", "lgCl", GIMP_LAYER_MODE_LUMA_LIGHTEN_ONLY,     FALSE },
164 
165   /* Pass Through (CS)*/
166   { "Pass Through",  "pass", GIMP_LAYER_MODE_PASS_THROUGH,          TRUE },
167 };
168 
169 
170 /* Utility function */
171 void
psd_set_error(gboolean file_eof,gint err_no,GError ** error)172 psd_set_error (gboolean   file_eof,
173                gint       err_no,
174                GError   **error)
175 {
176   if (file_eof)
177     {
178       g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
179                    "%s", _("Unexpected end of file"));
180     }
181   else
182     {
183       g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (err_no),
184                    "%s", g_strerror (err_no));
185     }
186 
187   return;
188 }
189 
190 gchar *
fread_pascal_string(gint32 * bytes_read,gint32 * bytes_written,guint16 mod_len,FILE * f,GError ** error)191 fread_pascal_string (gint32   *bytes_read,
192                      gint32   *bytes_written,
193                      guint16   mod_len,
194                      FILE     *f,
195                      GError  **error)
196 {
197   /*
198    * Reads a pascal string from the file padded to a multiple of mod_len
199    * and returns a utf-8 string.
200    */
201 
202   gchar        *str;
203   gchar        *utf8_str;
204   guchar        len;
205   gint32        padded_len;
206 
207   *bytes_read = 0;
208   *bytes_written = -1;
209 
210   if (fread (&len, 1, 1, f) < 1)
211     {
212       psd_set_error (feof (f), errno, error);
213       return NULL;
214     }
215   (*bytes_read)++;
216   IFDBG(3) g_debug ("Pascal string length %d", len);
217 
218   if (len == 0)
219     {
220       if (fseek (f, mod_len - 1, SEEK_CUR) < 0)
221         {
222           psd_set_error (feof (f), errno, error);
223           return NULL;
224         }
225       *bytes_read += (mod_len - 1);
226       *bytes_written = 0;
227       return NULL;
228     }
229 
230   str = g_malloc (len);
231   if (fread (str, len, 1, f) < 1)
232     {
233       psd_set_error (feof (f), errno, error);
234       g_free (str);
235       return NULL;
236     }
237   *bytes_read += len;
238 
239   if (mod_len > 0)
240     {
241       padded_len = len + 1;
242       while (padded_len % mod_len != 0)
243         {
244           if (fseek (f, 1, SEEK_CUR) < 0)
245             {
246               psd_set_error (feof (f), errno, error);
247               g_free (str);
248               return NULL;
249             }
250           (*bytes_read)++;
251           padded_len++;
252         }
253     }
254 
255   utf8_str = gimp_any_to_utf8 (str, len, NULL);
256   *bytes_written = strlen (utf8_str);
257   g_free (str);
258 
259   IFDBG(3) g_debug ("Pascal string: %s, bytes_read: %d, bytes_written: %d",
260                     utf8_str, *bytes_read, *bytes_written);
261 
262   return utf8_str;
263 }
264 
265 gint32
fwrite_pascal_string(const gchar * src,guint16 mod_len,FILE * f,GError ** error)266 fwrite_pascal_string (const gchar  *src,
267                       guint16       mod_len,
268                       FILE         *f,
269                       GError      **error)
270 {
271   /*
272    *  Converts utf-8 string to current locale and writes as pascal
273    *  string with padding to a multiple of mod_len.
274    */
275 
276   gchar  *str;
277   gchar  *pascal_str;
278   gchar   null_str = 0x0;
279   guchar  pascal_len;
280   gint32  bytes_written = 0;
281   gsize   len;
282 
283   if (src == NULL)
284     {
285        /* Write null string as two null bytes (0x0) */
286       if (fwrite (&null_str, 1, 1, f) < 1
287           || fwrite (&null_str, 1, 1, f) < 1)
288         {
289           psd_set_error (feof (f), errno, error);
290           return -1;
291         }
292       bytes_written += 2;
293     }
294   else
295     {
296       str = g_locale_from_utf8 (src, -1, NULL, &len, NULL);
297       if (len > 255)
298         pascal_len = 255;
299       else
300         pascal_len = len;
301       pascal_str = g_strndup (str, pascal_len);
302       g_free (str);
303       if (fwrite (&pascal_len, 1, 1, f) < 1
304           || fwrite (pascal_str, pascal_len, 1, f) < 1)
305         {
306           psd_set_error (feof (f), errno, error);
307           g_free (pascal_str);
308           return -1;
309         }
310       bytes_written++;
311       bytes_written += pascal_len;
312       IFDBG(2) g_debug ("Pascal string: %s, bytes_written: %d",
313                         pascal_str, bytes_written);
314       g_free (pascal_str);
315     }
316 
317   /* Pad with nulls */
318   if (mod_len > 0)
319     {
320       while (bytes_written % mod_len != 0)
321         {
322           if (fwrite (&null_str, 1, 1, f) < 1)
323             {
324               psd_set_error (feof (f), errno, error);
325               return -1;
326             }
327           bytes_written++;
328         }
329     }
330 
331   return bytes_written;
332 }
333 
334 gchar *
fread_unicode_string(gint32 * bytes_read,gint32 * bytes_written,guint16 mod_len,FILE * f,GError ** error)335 fread_unicode_string (gint32   *bytes_read,
336                       gint32   *bytes_written,
337                       guint16   mod_len,
338                       FILE     *f,
339                       GError  **error)
340 {
341   /*
342    * Reads a utf-16 string from the file padded to a multiple of mod_len
343    * and returns a utf-8 string.
344    */
345 
346   gchar     *utf8_str;
347   gunichar2 *utf16_str;
348   gint32     len;
349   gint32     i;
350   gint32     padded_len;
351   glong      utf8_str_len;
352 
353   *bytes_read = 0;
354   *bytes_written = -1;
355 
356   if (fread (&len, 4, 1, f) < 1)
357     {
358       psd_set_error (feof (f), errno, error);
359       return NULL;
360     }
361   *bytes_read += 4;
362   len = GINT32_FROM_BE (len);
363   IFDBG(3) g_debug ("Unicode string length %d", len);
364 
365   if (len == 0)
366     {
367       if (fseek (f, mod_len - 1, SEEK_CUR) < 0)
368         {
369           psd_set_error (feof (f), errno, error);
370           return NULL;
371         }
372       *bytes_read += (mod_len - 1);
373       *bytes_written = 0;
374       return NULL;
375     }
376 
377   utf16_str = g_malloc (len * 2);
378   for (i = 0; i < len; ++i)
379     {
380       if (fread (&utf16_str[i], 2, 1, f) < 1)
381         {
382           psd_set_error (feof (f), errno, error);
383           g_free (utf16_str);
384           return NULL;
385         }
386       *bytes_read += 2;
387       utf16_str[i] = GINT16_FROM_BE (utf16_str[i]);
388     }
389 
390   if (mod_len > 0)
391     {
392       padded_len = len + 1;
393       while (padded_len % mod_len != 0)
394         {
395           if (fseek (f, 1, SEEK_CUR) < 0)
396             {
397               psd_set_error (feof (f), errno, error);
398               g_free (utf16_str);
399               return NULL;
400             }
401           (*bytes_read)++;
402           padded_len++;
403         }
404     }
405 
406   utf8_str = g_utf16_to_utf8 (utf16_str, len, NULL, &utf8_str_len, NULL);
407   *bytes_written = utf8_str_len;
408   g_free (utf16_str);
409 
410   IFDBG(3) g_debug ("Unicode string: %s, bytes_read: %d, bytes_written: %d",
411                     utf8_str, *bytes_read, *bytes_written);
412 
413   return utf8_str;
414 }
415 
416 gint32
fwrite_unicode_string(const gchar * src,guint16 mod_len,FILE * f,GError ** error)417 fwrite_unicode_string (const gchar  *src,
418                        guint16       mod_len,
419                        FILE         *f,
420                        GError      **error)
421 {
422   /*
423    *  Converts utf-8 string to utf-16 and writes 4 byte length
424    *  then string padding to multiple of mod_len.
425    */
426 
427   gunichar2 *utf16_str;
428   gchar      null_str = 0x0;
429   gint32     utf16_len = 0;
430   gint32     bytes_written = 0;
431   gint       i;
432   glong      len;
433 
434   if (src == NULL)
435     {
436        /* Write null string as four byte 0 int32 */
437       if (fwrite (&utf16_len, 4, 1, f) < 1)
438         {
439           psd_set_error (feof (f), errno, error);
440           return -1;
441         }
442       bytes_written += 4;
443     }
444   else
445     {
446       utf16_str = g_utf8_to_utf16 (src, -1, NULL, &len, NULL);
447       /* Byte swap as required */
448       utf16_len = len;
449       for (i = 0; i < utf16_len; ++i)
450           utf16_str[i] = GINT16_TO_BE (utf16_str[i]);
451       utf16_len = GINT32_TO_BE (utf16_len);
452 
453       if (fwrite (&utf16_len, 4, 1, f) < 1
454           || fwrite (utf16_str, 2, utf16_len + 1, f) < utf16_len + 1)
455         {
456           psd_set_error (feof (f), errno, error);
457           return -1;
458         }
459       bytes_written += (4 + 2 * utf16_len + 2);
460       IFDBG(2) g_debug ("Unicode string: %s, bytes_written: %d",
461                         src, bytes_written);
462     }
463 
464   /* Pad with nulls */
465   if (mod_len > 0)
466     {
467       while (bytes_written % mod_len != 0)
468         {
469           if (fwrite (&null_str, 1, 1, f) < 1)
470             {
471               psd_set_error (feof (f), errno, error);
472               return -1;
473             }
474           bytes_written++;
475         }
476     }
477 
478   return bytes_written;
479 }
480 
481 gint
decode_packbits(const gchar * src,gchar * dst,guint16 packed_len,guint32 unpacked_len)482 decode_packbits (const gchar *src,
483                  gchar       *dst,
484                  guint16      packed_len,
485                  guint32      unpacked_len)
486 {
487   /*
488    *  Decode a PackBits chunk.
489    */
490 
491   gint    n;
492   gint32  unpack_left = unpacked_len;
493   gint32  pack_left = packed_len;
494   gint32  error_code = 0;
495   gint32  return_val = 0;
496 
497   while (unpack_left > 0 && pack_left > 0)
498     {
499       n = *(const guchar *) src;
500       src++;
501       pack_left--;
502 
503       if (n == 128)     /* nop */
504         continue;
505       else if (n > 128)
506         n -= 256;
507 
508       if (n < 0)        /* replicate next gchar |n|+ 1 times */
509         {
510           n = 1 - n;
511           if (pack_left < 1)
512             {
513               IFDBG(2) g_debug ("Input buffer exhausted in replicate");
514               error_code = 1;
515               break;
516             }
517           if (unpack_left < n)
518             {
519               IFDBG(2) g_debug ("Overrun in packbits replicate of %d chars", n - unpack_left);
520               error_code = 2;
521             }
522           memset (dst, *src, n);
523           src++;
524           pack_left--;
525           dst         += n;
526           unpack_left -= n;
527         }
528       else              /* copy next n+1 gchars literally */
529         {
530           n++;
531           if (pack_left < n)
532             {
533               IFDBG(2) g_debug ("Input buffer exhausted in copy");
534               error_code = 3;
535               break;
536             }
537           if (unpack_left < n)
538             {
539               IFDBG(2) g_debug ("Output buffer exhausted in copy");
540               error_code = 4;
541               break;
542             }
543           memcpy (dst, src, n);
544           src         += n;
545           pack_left   -= n;
546           dst         += n;
547           unpack_left -= n;
548         }
549     }
550 
551   if (unpack_left > 0)
552     {
553       /* Pad with zeros to end of output buffer */
554       memset (dst, 0, unpack_left);
555     }
556 
557   if (unpack_left)
558     {
559       IFDBG(2) g_debug ("Packbits decode - unpack left %d", unpack_left);
560       return_val -= unpack_left;
561     }
562   if (pack_left)
563     {
564       /* Some images seem to have a pad byte at the end of the packed data */
565       if (error_code || pack_left != 1)
566         {
567           IFDBG(2) g_debug ("Packbits decode - pack left %d", pack_left);
568           return_val = pack_left;
569         }
570     }
571 
572   IFDBG(2)
573     if (error_code)
574       g_debug ("Error code %d", error_code);
575 
576   return return_val;
577 }
578 
579 gchar *
encode_packbits(const gchar * src,guint32 unpacked_len,guint16 * packed_len)580 encode_packbits (const gchar *src,
581                  guint32      unpacked_len,
582                  guint16     *packed_len)
583 {
584   /*
585    *  Encode a PackBits chunk.
586    */
587 
588   GString *dst_str;                      /* destination string */
589   gint     curr_char;                    /* current character */
590   gchar    char_buff[128];               /* buffer of already read characters */
591   guchar   run_len;                      /* number of characters in a run */
592   gint32   unpack_left = unpacked_len;
593 
594   IFDBG(2) g_debug ("Encode packbits");
595 
596   /* Initialise destination string */
597   dst_str = g_string_sized_new (unpacked_len);
598 
599   /* prime the read loop */
600   curr_char = *src;
601   src++;
602   run_len = 0;
603 
604   /* read input until there's nothing left */
605   while (unpack_left > 0)
606     {
607         char_buff[run_len] = (gchar) curr_char;
608         IFDBG(2) g_debug ("buff %x, run len %d, curr char %x",
609                           char_buff[run_len], run_len, (gchar) curr_char);
610         run_len++;
611 
612         if (run_len >= MIN_RUN)
613           {
614             gint i;
615 
616             /* check for run  */
617             for (i = 2; i <= MIN_RUN; ++i)
618               {
619                 if (curr_char != char_buff[run_len - i])
620                   {
621                     /* no run */
622                     i = 0;
623                     break;
624                   }
625               }
626 
627             if (i != 0)
628               {
629                 /* we have a run write out buffer before run*/
630                 gint next_char;
631 
632                 if (run_len > MIN_RUN)
633                   {
634                     /* block size - 1 followed by contents */
635                     g_string_append_c (dst_str, (run_len - MIN_RUN - 1));
636                     g_string_append_len (dst_str, char_buff, (run_len - MIN_RUN));
637                     IFDBG(2) g_debug ("(1) Number of chars: %d, run length tag: %d",
638                                       run_len - MIN_RUN, run_len - MIN_RUN - 1);
639                   }
640 
641                 /* determine run length (MIN_RUN so far) */
642                 run_len = MIN_RUN;
643 
644                 /* get next character */
645                 next_char = *src;
646                 src++;
647                 unpack_left--;
648                 while (next_char == curr_char)
649                   {
650                     run_len++;
651                     if (run_len == 128)
652                       {
653                         /* run is at max length */
654                         break;
655                       }
656                     next_char = *src;
657                     src++;
658                     unpack_left--;
659                   }
660 
661                 /* write out encoded run length and run symbol */
662                 g_string_append_c (dst_str, (gchar)(1 - (gint)(run_len)));
663                 g_string_append_c (dst_str, curr_char);
664                 IFDBG(2) g_debug ("(2) Number of chars: %d, run length tag: %d",
665                                   run_len, (1 - (gint)(run_len)));
666 
667                 if (unpack_left > 0)
668                   {
669                     /* make run breaker start of next buffer */
670                     char_buff[0] = next_char;
671                     run_len = 1;
672                   }
673                 else
674                   {
675                     /* file ends in a run */
676                     run_len = 0;
677                   }
678               }
679           }
680 
681         if (run_len == 128)
682           {
683             /* write out buffer */
684             g_string_append_c (dst_str, 127);
685             g_string_append_len (dst_str, char_buff, 128);
686             IFDBG(2) g_debug ("(3) Number of chars: 128, run length tag: 127");
687 
688             /* start a new buffer */
689             run_len = 0;
690           }
691 
692         curr_char = *src;
693         src++;
694         unpack_left--;
695     }
696 
697   /* write out last buffer */
698   if (run_len != 0)
699     {
700         if (run_len <= 128)
701           {
702             /* write out entire copy buffer */
703             g_string_append_c (dst_str, run_len - 1);
704             g_string_append_len (dst_str, char_buff, run_len);
705             IFDBG(2) g_debug ("(4) Number of chars: %d, run length tag: %d",
706                               run_len, run_len - 1);
707           }
708         else
709           {
710             IFDBG(2) g_debug ("(5) Very bad - should not be here");
711           }
712     }
713 
714   *packed_len = dst_str->len;
715   IFDBG(2) g_debug ("Packed len %d, unpacked %d", *packed_len, unpacked_len);
716 
717   return g_string_free (dst_str, FALSE);
718 }
719 
720 void
psd_to_gimp_blend_mode(const gchar * psd_mode,LayerModeInfo * mode_info)721 psd_to_gimp_blend_mode (const gchar   *psd_mode,
722                         LayerModeInfo *mode_info)
723 {
724   gint i;
725 
726   mode_info->mode            = GIMP_LAYER_MODE_NORMAL;
727   /* FIXME: use the image mode to select the correct color spaces.  for now,
728    * we use rgb-perceptual blending/compositing unconditionally.
729    */
730   mode_info->blend_space     = GIMP_LAYER_COLOR_SPACE_RGB_PERCEPTUAL;
731   mode_info->composite_space = GIMP_LAYER_COLOR_SPACE_RGB_PERCEPTUAL;
732   mode_info->composite_mode  = GIMP_LAYER_COMPOSITE_UNION;
733 
734   for (i = 0; i < G_N_ELEMENTS (layer_mode_map); i++)
735     {
736       if (g_ascii_strncasecmp (psd_mode, layer_mode_map[i].psd_mode, 4) == 0)
737         {
738           if (! layer_mode_map[i].exact && CONVERSION_WARNINGS)
739             {
740               g_message ("GIMP uses a different equation than Photoshop for "
741                          "blend mode: %s. Results will differ.",
742                          layer_mode_map[i].name);
743             }
744 
745           mode_info->mode = layer_mode_map[i].gimp_mode;
746 
747           return;
748         }
749     }
750 
751   if (CONVERSION_WARNINGS)
752     {
753       gchar *mode_name = g_strndup (psd_mode, 4);
754       g_message ("Unsupported blend mode: %s. Mode reverts to normal",
755                  mode_name);
756       g_free (mode_name);
757     }
758 }
759 
760 const gchar *
gimp_to_psd_blend_mode(const LayerModeInfo * mode_info)761 gimp_to_psd_blend_mode (const LayerModeInfo *mode_info)
762 {
763   gint i;
764 
765   /* FIXME: select the image mode based on the layer mode color spaces.  for
766    * now, we assume rgb-perceptual blending/compositing unconditionally.
767    */
768   if (mode_info->blend_space != GIMP_LAYER_COLOR_SPACE_AUTO &&
769       mode_info->blend_space != GIMP_LAYER_COLOR_SPACE_RGB_PERCEPTUAL)
770     {
771       if (CONVERSION_WARNINGS)
772         g_message ("Unsupported blend color space: %s. "
773                    "Blend color space reverts to rgb-perceptual",
774                    get_enum_value_nick (GIMP_TYPE_LAYER_COLOR_SPACE,
775                                         mode_info->blend_space));
776     }
777 
778   if (mode_info->composite_space != GIMP_LAYER_COLOR_SPACE_AUTO &&
779       mode_info->composite_space != GIMP_LAYER_COLOR_SPACE_RGB_PERCEPTUAL)
780     {
781       if (CONVERSION_WARNINGS)
782         g_message ("Unsupported composite color space: %s. "
783                    "Composite color space reverts to rgb-perceptual",
784                    get_enum_value_nick (GIMP_TYPE_LAYER_COLOR_SPACE,
785                                         mode_info->composite_space));
786     }
787 
788   if (mode_info->composite_mode != GIMP_LAYER_COMPOSITE_AUTO &&
789       mode_info->composite_mode != GIMP_LAYER_COMPOSITE_UNION)
790     {
791       if (CONVERSION_WARNINGS)
792         g_message ("Unsupported composite mode: %s. "
793                    "Composite mode reverts to union",
794                    get_enum_value_nick (GIMP_TYPE_LAYER_COMPOSITE_MODE,
795                                         mode_info->composite_mode));
796     }
797 
798   for (i = 0; i < G_N_ELEMENTS (layer_mode_map); i++)
799     {
800       if (layer_mode_map[i].gimp_mode == mode_info->mode)
801         {
802           if (! layer_mode_map[i].exact && CONVERSION_WARNINGS)
803             {
804               g_message ("GIMP uses a different equation than Photoshop for "
805                          "blend mode: %s. Results may differ.",
806                          get_enum_value_nick (GIMP_TYPE_LAYER_MODE,
807                                               mode_info->mode));
808             }
809 
810           return layer_mode_map[i].psd_mode;
811         }
812     }
813 
814   if (CONVERSION_WARNINGS)
815     g_message ("Unsupported blend mode: %s. Mode reverts to normal",
816                get_enum_value_nick (GIMP_TYPE_LAYER_MODE, mode_info->mode));
817 
818   return "norm";
819 }
820 
821 GimpColorTag
psd_to_gimp_layer_color_tag(guint16 layer_color_tag)822 psd_to_gimp_layer_color_tag (guint16 layer_color_tag)
823 {
824   GimpColorTag colorTag;
825 
826   switch (layer_color_tag)
827     {
828     case 1:
829       colorTag = GIMP_COLOR_TAG_RED;
830       break;
831 
832     case 2:
833       colorTag = GIMP_COLOR_TAG_ORANGE;
834       break;
835 
836     case 3:
837       colorTag = GIMP_COLOR_TAG_YELLOW;
838       break;
839 
840     case 4:
841       colorTag = GIMP_COLOR_TAG_GREEN;
842       break;
843 
844     case 5:
845       colorTag = GIMP_COLOR_TAG_BLUE;
846       break;
847 
848     case 6:
849       colorTag = GIMP_COLOR_TAG_VIOLET;
850       break;
851 
852     case 7:
853       colorTag = GIMP_COLOR_TAG_GRAY;
854       break;
855 
856     default:
857       if (CONVERSION_WARNINGS)
858         g_message ("Unsupported Photoshop layer color tag: %i. GIMP layer color tag set to none.",
859                        layer_color_tag);
860       colorTag = GIMP_COLOR_TAG_NONE;
861     }
862 
863   return colorTag;
864 }
865 
866 guint16
gimp_to_psd_layer_color_tag(GimpColorTag layer_color_tag)867 gimp_to_psd_layer_color_tag (GimpColorTag layer_color_tag)
868 {
869   guint16 color_tag;
870 
871   switch (layer_color_tag)
872     {
873     case GIMP_COLOR_TAG_RED:
874         color_tag = 1;
875       break;
876 
877     case GIMP_COLOR_TAG_ORANGE:
878         color_tag = 2;
879       break;
880 
881     case GIMP_COLOR_TAG_YELLOW:
882         color_tag = 3;
883       break;
884 
885     case GIMP_COLOR_TAG_GREEN:
886         color_tag = 4;
887       break;
888 
889     case GIMP_COLOR_TAG_BLUE:
890         color_tag = 5;
891       break;
892 
893     case GIMP_COLOR_TAG_VIOLET:
894         color_tag = 6;
895       break;
896 
897     case GIMP_COLOR_TAG_GRAY:
898         color_tag = 7;
899       break;
900 
901     default:
902       if (CONVERSION_WARNINGS)
903         g_message ("Photoshop doesn't support GIMP layer color tag: %i. Photoshop layer color tag set to none.",
904                    layer_color_tag);
905         color_tag = 0;
906     }
907 
908   return color_tag;
909 }
910 
911 static const gchar *
get_enum_value_nick(GType type,gint value)912 get_enum_value_nick (GType type,
913                      gint  value)
914 {
915   const gchar *nick;
916 
917   if (gimp_enum_get_value (type, value, NULL, &nick, NULL, NULL))
918     {
919       return nick;
920     }
921   else
922     {
923       static gchar err_name[32];
924 
925       snprintf (err_name, sizeof (err_name), "UNKNOWN (%d)", value);
926 
927       return err_name;
928     }
929 }
930