1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
5
* Copyright 2007 by John Marshall
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 2 of the License, or
10
* (at your option) any later version.
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.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27
#include <glib/gstdio.h>
28
#include <libgimp/gimp.h>
32
#include "psd-image-res-load.h"
33
#include "psd-layer-res-load.h"
36
#include "libgimp/stdplugins-intl.h"
39
#define COMP_MODE_SIZE sizeof(guint16)
42
/* Local function prototypes */
43
static gint read_header_block (PSDimage *img_a,
47
static gint read_color_mode_block (PSDimage *img_a,
51
static gint read_image_resource_block (PSDimage *img_a,
55
static PSDlayer ** read_layer_block (PSDimage *img_a,
59
static gint read_merged_image_block (PSDimage *img_a,
63
static gint32 create_gimp_image (PSDimage *img_a,
64
const gchar *filename);
66
static gint add_color_map (const gint32 image_id,
69
static gint add_image_resources (const gint32 image_id,
74
static gint add_layers (const gint32 image_id,
80
static gint add_merged_image (const gint32 image_id,
85
/* Local utility function prototypes */
86
static gchar * get_psd_color_mode_name (PSDColorMode mode);
88
static void psd_to_gimp_color_map (guchar *map256);
90
static GimpImageType get_gimp_image_type (const GimpImageBaseType image_base_type,
91
const gboolean alpha);
93
static gint read_channel_data (PSDchannel *channel,
95
const guint16 compression,
96
const guint16 *rle_pack_len,
100
static void convert_16_bit (const gchar *src,
104
static void convert_1_bit (const gchar *src,
110
/* Main file load function */
112
load_image (const gchar *filename,
119
gint32 image_id = -1;
120
GError *error = NULL;
122
/* ----- Open PSD file ----- */
123
if (g_stat (filename, &st) == -1)
126
IFDBG(1) g_debug ("Open file %s", gimp_filename_to_utf8 (filename));
127
f = g_fopen (filename, "rb");
130
g_set_error (load_error, G_FILE_ERROR, g_file_error_from_errno (errno),
131
_("Could not open '%s' for reading: %s"),
132
gimp_filename_to_utf8 (filename), g_strerror (errno));
136
gimp_progress_init_printf (_("Opening '%s'"),
137
gimp_filename_to_utf8 (filename));
139
/* ----- Read the PSD file Header block ----- */
140
IFDBG(2) g_debug ("Read header block");
141
if (read_header_block (&img_a, f, &error) < 0)
143
gimp_progress_update (0.1);
145
/* ----- Read the PSD file Colour Mode block ----- */
146
IFDBG(2) g_debug ("Read colour mode block");
147
if (read_color_mode_block (&img_a, f, &error) < 0)
149
gimp_progress_update (0.2);
151
/* ----- Read the PSD file Image Resource block ----- */
152
IFDBG(2) g_debug ("Read image resource block");
153
if (read_image_resource_block (&img_a, f, &error) < 0)
155
gimp_progress_update (0.3);
157
/* ----- Read the PSD file Layer & Mask block ----- */
158
IFDBG(2) g_debug ("Read layer & mask block");
159
lyr_a = read_layer_block (&img_a, f, &error);
160
if (img_a.num_layers != 0 && lyr_a == NULL)
162
gimp_progress_update (0.4);
164
/* ----- Read the PSD file Merged Image Data block ----- */
165
IFDBG(2) g_debug ("Read merged image and extra alpha channel block");
166
if (read_merged_image_block (&img_a, f, &error) < 0)
168
gimp_progress_update (0.5);
170
/* ----- Create GIMP image ----- */
171
IFDBG(2) g_debug ("Create GIMP image");
172
image_id = create_gimp_image (&img_a, filename);
175
gimp_progress_update (0.6);
177
/* ----- Add colour map ----- */
178
IFDBG(2) g_debug ("Add color map");
179
if (add_color_map (image_id, &img_a) < 0)
181
gimp_progress_update (0.7);
183
/* ----- Add image resources ----- */
184
IFDBG(2) g_debug ("Add image resources");
185
if (add_image_resources (image_id, &img_a, f, &error) < 0)
187
gimp_progress_update (0.8);
189
/* ----- Add layers -----*/
190
IFDBG(2) g_debug ("Add layers");
191
if (add_layers (image_id, &img_a, lyr_a, f, &error) < 0)
193
gimp_progress_update (0.9);
195
/* ----- Add merged image data and extra alpha channels ----- */
196
IFDBG(2) g_debug ("Add merged image data and extra alpha channels");
197
if (add_merged_image (image_id, &img_a, f, &error) < 0)
199
gimp_progress_update (1.0);
201
IFDBG(2) g_debug ("Close file & return, image id: %d", image_id);
202
IFDBG(1) g_debug ("\n----------------------------------------"
203
"----------------------------------------\n");
205
gimp_image_clean_all (image_id);
206
gimp_image_undo_enable (image_id);
210
/* ----- Process load errors ----- */
214
g_set_error (load_error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
215
_("Error loading PSD file: %s"), error->message);
216
g_error_free (error);
219
/* Delete partially loaded image */
221
gimp_image_delete (image_id);
223
/* Close file if Open */
231
/* Local functions */
234
read_header_block (PSDimage *img_a,
242
if (fread (sig, 4, 1, f) < 1
243
|| fread (&version, 2, 1, f) < 1
244
|| fread (buf, 6, 1, f) < 1
245
|| fread (&img_a->channels, 2, 1, f) < 1
246
|| fread (&img_a->rows, 4, 1, f) < 1
247
|| fread (&img_a->columns, 4, 1, f) < 1
248
|| fread (&img_a->bps, 2, 1, f) < 1
249
|| fread (&img_a->color_mode, 2, 1, f) < 1)
251
psd_set_error (feof (f), errno, error);
254
version = GUINT16_FROM_BE (version);
255
img_a->channels = GUINT16_FROM_BE (img_a->channels);
256
img_a->rows = GUINT32_FROM_BE (img_a->rows);
257
img_a->columns = GUINT32_FROM_BE (img_a->columns);
258
img_a->bps = GUINT16_FROM_BE (img_a->bps);
259
img_a->color_mode = GUINT16_FROM_BE (img_a->color_mode);
261
IFDBG(1) g_debug ("\n\n\tSig: %.4s\n\tVer: %d\n\tChannels: "
262
"%d\n\tSize: %dx%d\n\tBPS: %d\n\tMode: %d\n",
263
sig, version, img_a->channels,
264
img_a->columns, img_a->rows,
265
img_a->bps, img_a->color_mode);
267
if (memcmp (sig, "8BPS", 4) != 0)
269
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
270
_("Not a valid photoshop document file"));
276
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
277
_("Unsupported file format version: %d"), version);
281
if (img_a->channels > MAX_CHANNELS)
283
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
284
_("Too many channels in file: %d"), img_a->channels);
288
/* Photoshop CS (version 8) supports 300000 x 300000, but this
289
is currently larger than GIMP_MAX_IMAGE_SIZE */
291
if (img_a->rows < 1 || img_a->rows > GIMP_MAX_IMAGE_SIZE)
293
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
294
_("Unsupported or invalid image height: %d"),
299
if (img_a->columns < 1 || img_a->columns > GIMP_MAX_IMAGE_SIZE)
301
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
302
_("Unsupported or invalid image width: %d"),
307
if (img_a->color_mode != PSD_BITMAP
308
&& img_a->color_mode != PSD_GRAYSCALE
309
&& img_a->color_mode != PSD_INDEXED
310
&& img_a->color_mode != PSD_RGB
311
&& img_a->color_mode != PSD_DUOTONE)
313
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
314
_("Unsupported color mode: %s"),
315
get_psd_color_mode_name (img_a->color_mode));
319
/* Warnings for format conversions */
323
IFDBG(3) g_debug ("16 Bit Data");
324
if (CONVERSION_WARNINGS)
325
g_message (_("Warning:\n"
326
"The image you are loading has 16 bits per channel. GIMP "
327
"can only handle 8 bit, so it will be converted for you. "
328
"Information will be lost because of this conversion."));
332
IFDBG(3) g_debug ("8 Bit Data");
336
IFDBG(3) g_debug ("1 Bit Data");
340
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
341
_("Unsupported bit depth: %d"), img_a->bps);
350
read_color_mode_block (PSDimage *img_a,
354
static guchar cmap[] = {0, 0, 0, 255, 255, 255};
357
img_a->color_map_entries = 0;
358
img_a->color_map_len = 0;
359
if (fread (&block_len, 4, 1, f) < 1)
361
psd_set_error (feof (f), errno, error);
364
block_len = GUINT32_FROM_BE (block_len);
366
IFDBG(1) g_debug ("Color map block size = %d", block_len);
370
if (img_a->color_mode == PSD_INDEXED ||
371
img_a->color_mode == PSD_DUOTONE )
373
IFDBG(1) g_debug ("No color block for indexed or duotone image");
374
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
375
_("The file is corrupt!"));
379
else if (img_a->color_mode == PSD_INDEXED)
381
if (block_len != 768)
383
IFDBG(1) g_debug ("Invalid color block size for indexed image");
384
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
385
_("The file is corrupt!"));
390
img_a->color_map_len = block_len;
391
img_a->color_map = g_malloc (img_a->color_map_len);
392
if (fread (img_a->color_map, block_len, 1, f) < 1)
394
psd_set_error (feof (f), errno, error);
399
psd_to_gimp_color_map (img_a->color_map);
400
img_a->color_map_entries = 256;
404
else if (img_a->color_mode == PSD_DUOTONE)
406
img_a->color_map_len = block_len;
407
img_a->color_map = g_malloc (img_a->color_map_len);
408
if (fread (img_a->color_map, block_len, 1, f) < 1)
410
psd_set_error (feof (f), errno, error);
415
/* Create color map for bitmap image */
416
if (img_a->color_mode == PSD_BITMAP)
418
img_a->color_map_len = 6;
419
img_a->color_map = g_malloc (img_a->color_map_len);
420
memcpy (img_a->color_map, cmap, img_a->color_map_len);
421
img_a->color_map_entries = 2;
423
IFDBG(2) g_debug ("Colour map data length %d", img_a->color_map_len);
429
read_image_resource_block (PSDimage *img_a,
436
if (fread (&block_len, 4, 1, f) < 1)
438
psd_set_error (feof (f), errno, error);
441
img_a->image_res_len = GUINT32_FROM_BE (block_len);
443
IFDBG(1) g_debug ("Image resource block size = %d", (int)img_a->image_res_len);
445
img_a->image_res_start = ftell (f);
446
block_end = img_a->image_res_start + img_a->image_res_len;
448
if (fseek (f, block_end, SEEK_SET) < 0)
450
psd_set_error (feof (f), errno, error);
458
read_layer_block (PSDimage *img_a,
468
gint lidx; /* Layer index */
469
gint cidx; /* Channel index */
471
if (fread (&block_len, 4, 1, f) < 1)
473
psd_set_error (feof (f), errno, error);
474
img_a->num_layers = -1;
477
img_a->mask_layer_len = GUINT32_FROM_BE (block_len);
479
IFDBG(1) g_debug ("Layer and mask block size = %d", img_a->mask_layer_len);
481
img_a->transparency = FALSE;
482
img_a->layer_data_len = 0;
484
if (!img_a->mask_layer_len)
486
img_a->num_layers = 0;
491
img_a->mask_layer_start = ftell (f);
492
block_end = img_a->mask_layer_start + img_a->mask_layer_len;
494
/* Get number of layers */
495
if (fread (&block_len, 4, 1, f) < 1
496
|| fread (&img_a->num_layers, 2, 1, f) < 1)
498
psd_set_error (feof (f), errno, error);
499
img_a->num_layers = -1;
502
img_a->num_layers = GINT16_FROM_BE (img_a->num_layers);
503
IFDBG(2) g_debug ("Number of layers: %d", img_a->num_layers);
505
if (img_a->num_layers < 0)
507
img_a->transparency = TRUE;
508
img_a->num_layers = -img_a->num_layers;
511
if (img_a->num_layers)
513
/* Read layer records */
516
/* Create pointer array for the layer records */
517
lyr_a = g_new (PSDlayer *, img_a->num_layers);
518
for (lidx = 0; lidx < img_a->num_layers; ++lidx)
520
/* Allocate layer record */
521
lyr_a[lidx] = (PSDlayer *) g_malloc (sizeof (PSDlayer) );
523
/* Initialise record */
524
lyr_a[lidx]->drop = FALSE;
527
if (fread (&lyr_a[lidx]->top, 4, 1, f) < 1
528
|| fread (&lyr_a[lidx]->left, 4, 1, f) < 1
529
|| fread (&lyr_a[lidx]->bottom, 4, 1, f) < 1
530
|| fread (&lyr_a[lidx]->right, 4, 1, f) < 1
531
|| fread (&lyr_a[lidx]->num_channels, 2, 1, f) < 1)
533
psd_set_error (feof (f), errno, error);
536
lyr_a[lidx]->top = GUINT32_FROM_BE (lyr_a[lidx]->top);
537
lyr_a[lidx]->left = GUINT32_FROM_BE (lyr_a[lidx]->left);
538
lyr_a[lidx]->bottom = GUINT32_FROM_BE (lyr_a[lidx]->bottom);
539
lyr_a[lidx]->right = GUINT32_FROM_BE (lyr_a[lidx]->right);
540
lyr_a[lidx]->num_channels = GUINT16_FROM_BE (lyr_a[lidx]->num_channels);
542
if (lyr_a[lidx]->num_channels > MAX_CHANNELS)
544
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
545
_("Too many channels in layer: %d"),
546
lyr_a[lidx]->num_channels);
549
if (lyr_a[lidx]->bottom - lyr_a[lidx]->top > GIMP_MAX_IMAGE_SIZE)
551
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
552
_("Unsupported or invalid layer height: %d"),
553
lyr_a[lidx]->bottom - lyr_a[lidx]->top);
556
if (lyr_a[lidx]->right - lyr_a[lidx]->left > GIMP_MAX_IMAGE_SIZE)
558
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
559
_("Unsupported or invalid layer width: %d"),
560
lyr_a[lidx]->right - lyr_a[lidx]->left);
564
IFDBG(2) g_debug ("Layer %d, Coords %d %d %d %d, channels %d, ",
565
lidx, lyr_a[lidx]->left, lyr_a[lidx]->top,
566
lyr_a[lidx]->right, lyr_a[lidx]->bottom,
567
lyr_a[lidx]->num_channels);
569
lyr_a[lidx]->chn_info = g_new (ChannelLengthInfo, lyr_a[lidx]->num_channels);
570
for (cidx = 0; cidx < lyr_a[lidx]->num_channels; ++cidx)
572
if (fread (&lyr_a[lidx]->chn_info[cidx].channel_id, 2, 1, f) < 1
573
|| fread (&lyr_a[lidx]->chn_info[cidx].data_len, 4, 1, f) < 1)
575
psd_set_error (feof (f), errno, error);
578
lyr_a[lidx]->chn_info[cidx].channel_id =
579
GINT16_FROM_BE (lyr_a[lidx]->chn_info[cidx].channel_id);
580
lyr_a[lidx]->chn_info[cidx].data_len =
581
GUINT32_FROM_BE (lyr_a[lidx]->chn_info[cidx].data_len);
582
img_a->layer_data_len += lyr_a[lidx]->chn_info[cidx].data_len;
583
IFDBG(3) g_debug ("Channel ID %d, data len %d",
584
lyr_a[lidx]->chn_info[cidx].channel_id,
585
lyr_a[lidx]->chn_info[cidx].data_len);
588
if (fread (lyr_a[lidx]->mode_key, 4, 1, f) < 1
589
|| fread (lyr_a[lidx]->blend_mode, 4, 1, f) < 1
590
|| fread (&lyr_a[lidx]->opacity, 1, 1, f) < 1
591
|| fread (&lyr_a[lidx]->clipping, 1, 1, f) < 1
592
|| fread (&lyr_a[lidx]->flags, 1, 1, f) < 1
593
|| fread (&lyr_a[lidx]->filler, 1, 1, f) < 1
594
|| fread (&lyr_a[lidx]->extra_len, 4, 1, f) < 1)
596
psd_set_error (feof (f), errno, error);
599
if (memcmp (lyr_a[lidx]->mode_key, "8BIM", 4) != 0)
601
IFDBG(1) g_debug ("Incorrect layer mode signature %.4s",
602
lyr_a[lidx]->mode_key);
603
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
604
_("The file is corrupt!"));
608
lyr_a[lidx]->layer_flags.trans_prot = lyr_a[lidx]->flags & 1 ? TRUE : FALSE;
609
lyr_a[lidx]->layer_flags.visible = lyr_a[lidx]->flags & 2 ? FALSE : TRUE;
610
if (lyr_a[lidx]->flags & 8)
611
lyr_a[lidx]->layer_flags.irrelevant = lyr_a[lidx]->flags & 16 ? TRUE : FALSE;
613
lyr_a[lidx]->layer_flags.irrelevant = FALSE;
615
lyr_a[lidx]->extra_len = GUINT32_FROM_BE (lyr_a[lidx]->extra_len);
616
block_rem = lyr_a[lidx]->extra_len;
617
IFDBG(2) g_debug ("\n\tLayer mode sig: %.4s\n\tBlend mode: %.4s\n\t"
618
"Opacity: %d\n\tClipping: %d\n\tExtra data len: %d\n\t"
619
"Alpha lock: %d\n\tVisible: %d\n\tIrrelevant: %d",
620
lyr_a[lidx]->mode_key,
621
lyr_a[lidx]->blend_mode,
622
lyr_a[lidx]->opacity,
623
lyr_a[lidx]->clipping,
624
lyr_a[lidx]->extra_len,
625
lyr_a[lidx]->layer_flags.trans_prot,
626
lyr_a[lidx]->layer_flags.visible,
627
lyr_a[lidx]->layer_flags.irrelevant);
628
IFDBG(3) g_debug ("Remaining length %d", block_rem);
630
/* Layer mask data */
631
if (fread (&block_len, 4, 1, f) < 1)
633
psd_set_error (feof (f), errno, error);
636
block_len = GUINT32_FROM_BE (block_len);
637
block_rem -= (block_len + 4);
638
IFDBG(3) g_debug ("Remaining length %d", block_rem);
640
lyr_a[lidx]->layer_mask_extra.top = 0;
641
lyr_a[lidx]->layer_mask_extra.left = 0;
642
lyr_a[lidx]->layer_mask_extra.bottom = 0;
643
lyr_a[lidx]->layer_mask_extra.right = 0;
644
lyr_a[lidx]->layer_mask.top = 0;
645
lyr_a[lidx]->layer_mask.left = 0;
646
lyr_a[lidx]->layer_mask.bottom = 0;
647
lyr_a[lidx]->layer_mask.right = 0;
648
lyr_a[lidx]->layer_mask.def_color = 0;
649
lyr_a[lidx]->layer_mask.extra_def_color = 0;
650
lyr_a[lidx]->layer_mask.mask_flags.relative_pos = FALSE;
651
lyr_a[lidx]->layer_mask.mask_flags.disabled = FALSE;
652
lyr_a[lidx]->layer_mask.mask_flags.invert = FALSE;
660
if (fread (&lyr_a[lidx]->layer_mask.top, 4, 1, f) < 1
661
|| fread (&lyr_a[lidx]->layer_mask.left, 4, 1, f) < 1
662
|| fread (&lyr_a[lidx]->layer_mask.bottom, 4, 1, f) < 1
663
|| fread (&lyr_a[lidx]->layer_mask.right, 4, 1, f) < 1
664
|| fread (&lyr_a[lidx]->layer_mask.def_color, 1, 1, f) < 1
665
|| fread (&lyr_a[lidx]->layer_mask.flags, 1, 1, f) < 1
666
|| fread (&lyr_a[lidx]->layer_mask.extra_def_color, 1, 1, f) < 1
667
|| fread (&lyr_a[lidx]->layer_mask.extra_flags, 1, 1, f) < 1)
669
psd_set_error (feof (f), errno, error);
672
lyr_a[lidx]->layer_mask.top =
673
GUINT32_FROM_BE (lyr_a[lidx]->layer_mask.top);
674
lyr_a[lidx]->layer_mask.left =
675
GUINT32_FROM_BE (lyr_a[lidx]->layer_mask.left);
676
lyr_a[lidx]->layer_mask.bottom =
677
GUINT32_FROM_BE (lyr_a[lidx]->layer_mask.bottom);
678
lyr_a[lidx]->layer_mask.right =
679
GUINT32_FROM_BE (lyr_a[lidx]->layer_mask.right);
680
lyr_a[lidx]->layer_mask.mask_flags.relative_pos =
681
lyr_a[lidx]->layer_mask.flags & 1 ? TRUE : FALSE;
682
lyr_a[lidx]->layer_mask.mask_flags.disabled =
683
lyr_a[lidx]->layer_mask.flags & 2 ? TRUE : FALSE;
684
lyr_a[lidx]->layer_mask.mask_flags.invert =
685
lyr_a[lidx]->layer_mask.flags & 4 ? TRUE : FALSE;
687
case 36: /* If we have a 36 byte mask record assume second data set is correct */
688
if (fread (&lyr_a[lidx]->layer_mask_extra.top, 4, 1, f) < 1
689
|| fread (&lyr_a[lidx]->layer_mask_extra.left, 4, 1, f) < 1
690
|| fread (&lyr_a[lidx]->layer_mask_extra.bottom, 4, 1, f) < 1
691
|| fread (&lyr_a[lidx]->layer_mask_extra.right, 4, 1, f) < 1
692
|| fread (&lyr_a[lidx]->layer_mask.extra_def_color, 1, 1, f) < 1
693
|| fread (&lyr_a[lidx]->layer_mask.extra_flags, 1, 1, f) < 1
694
|| fread (&lyr_a[lidx]->layer_mask.def_color, 1, 1, f) < 1
695
|| fread (&lyr_a[lidx]->layer_mask.flags, 1, 1, f) < 1
696
|| fread (&lyr_a[lidx]->layer_mask.top, 4, 1, f) < 1
697
|| fread (&lyr_a[lidx]->layer_mask.left, 4, 1, f) < 1
698
|| fread (&lyr_a[lidx]->layer_mask.bottom, 4, 1, f) < 1
699
|| fread (&lyr_a[lidx]->layer_mask.right, 4, 1, f) < 1)
701
psd_set_error (feof (f), errno, error);
704
lyr_a[lidx]->layer_mask_extra.top =
705
GUINT32_FROM_BE (lyr_a[lidx]->layer_mask_extra.top);
706
lyr_a[lidx]->layer_mask_extra.left =
707
GUINT32_FROM_BE (lyr_a[lidx]->layer_mask_extra.left);
708
lyr_a[lidx]->layer_mask_extra.bottom =
709
GUINT32_FROM_BE (lyr_a[lidx]->layer_mask_extra.bottom);
710
lyr_a[lidx]->layer_mask_extra.right =
711
GUINT32_FROM_BE (lyr_a[lidx]->layer_mask_extra.right);
712
lyr_a[lidx]->layer_mask.top =
713
GUINT32_FROM_BE (lyr_a[lidx]->layer_mask.top);
714
lyr_a[lidx]->layer_mask.left =
715
GUINT32_FROM_BE (lyr_a[lidx]->layer_mask.left);
716
lyr_a[lidx]->layer_mask.bottom =
717
GUINT32_FROM_BE (lyr_a[lidx]->layer_mask.bottom);
718
lyr_a[lidx]->layer_mask.right =
719
GUINT32_FROM_BE (lyr_a[lidx]->layer_mask.right);
720
lyr_a[lidx]->layer_mask.mask_flags.relative_pos =
721
lyr_a[lidx]->layer_mask.flags & 1 ? TRUE : FALSE;
722
lyr_a[lidx]->layer_mask.mask_flags.disabled =
723
lyr_a[lidx]->layer_mask.flags & 2 ? TRUE : FALSE;
724
lyr_a[lidx]->layer_mask.mask_flags.invert =
725
lyr_a[lidx]->layer_mask.flags & 4 ? TRUE : FALSE;
729
IFDBG(1) g_debug ("Unknown layer mask record size ... skipping");
730
if (fseek (f, block_len, SEEK_CUR) < 0)
732
psd_set_error (feof (f), errno, error);
737
IFDBG(2) g_debug ("Layer mask coords %d %d %d %d, Rel pos %d",
738
lyr_a[lidx]->layer_mask.left,
739
lyr_a[lidx]->layer_mask.top,
740
lyr_a[lidx]->layer_mask.right,
741
lyr_a[lidx]->layer_mask.bottom,
742
lyr_a[lidx]->layer_mask.mask_flags.relative_pos);
744
IFDBG(3) g_debug ("Default mask color, %d, %d",
745
lyr_a[lidx]->layer_mask.def_color,
746
lyr_a[lidx]->layer_mask.extra_def_color);
748
/* Layer blending ranges */ /* FIXME */
749
if (fread (&block_len, 4, 1, f) < 1)
751
psd_set_error (feof (f), errno, error);
754
block_len = GUINT32_FROM_BE (block_len);
755
block_rem -= (block_len + 4);
756
IFDBG(3) g_debug ("Remaining length %d", block_rem);
759
if (fseek (f, block_len, SEEK_CUR) < 0)
761
psd_set_error (feof (f), errno, error);
766
lyr_a[lidx]->name = fread_pascal_string (&read_len, &write_len,
770
block_rem -= read_len;
771
IFDBG(3) g_debug ("Remaining length %d", block_rem);
773
/* Adjustment layer info */ /* FIXME */
775
while (block_rem > 7)
777
if (get_layer_resource_header (&res_a, f, error) < 0)
781
if (res_a.data_len > block_rem)
783
IFDBG(1) g_debug ("Unexpected end of layer resource data");
784
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
785
_("The file is corrupt!"));
789
if (load_layer_resource (&res_a, lyr_a[lidx], f, error) < 0)
791
block_rem -= res_a.data_len;
795
if (fseek (f, block_rem, SEEK_CUR) < 0)
797
psd_set_error (feof (f), errno, error);
803
img_a->layer_data_start = ftell(f);
804
if (fseek (f, img_a->layer_data_len, SEEK_CUR) < 0)
806
psd_set_error (feof (f), errno, error);
810
IFDBG(1) g_debug ("Layer image data block size %d",
811
img_a->layer_data_len);
816
/* Read global layer mask record */ /* FIXME */
818
/* Skip to end of block */
819
if (fseek (f, block_end, SEEK_SET) < 0)
821
psd_set_error (feof (f), errno, error);
830
read_merged_image_block (PSDimage *img_a,
834
img_a->merged_image_start = ftell(f);
835
if (fseek (f, 0, SEEK_END) < 0)
837
psd_set_error (feof (f), errno, error);
841
img_a->merged_image_len = ftell(f) - img_a->merged_image_start;
843
IFDBG(1) g_debug ("Merged image data block: Start: %d, len: %d",
844
img_a->merged_image_start, img_a->merged_image_len);
850
create_gimp_image (PSDimage *img_a,
851
const gchar *filename)
853
gint32 image_id = -1;
855
switch (img_a->color_mode)
859
img_a->base_type = GIMP_GRAY;
864
img_a->base_type = GIMP_INDEXED;
868
img_a->base_type = GIMP_RGB;
872
/* Color mode already validated - should not be here */
873
g_warning ("Invalid color mode");
878
/* Create gimp image */
879
IFDBG(2) g_debug ("Create image");
880
image_id = gimp_image_new (img_a->columns, img_a->rows, img_a->base_type);
882
gimp_image_set_filename (image_id, filename);
883
gimp_image_undo_disable (image_id);
889
add_color_map (const gint32 image_id,
892
GimpParasite *parasite;
894
if (img_a->color_map_len)
896
if (img_a->color_mode != PSD_DUOTONE)
897
gimp_image_set_colormap (image_id, img_a->color_map, img_a->color_map_entries);
900
/* Add parasite for Duotone color data */
901
IFDBG(2) g_debug ("Add Duotone color data parasite");
902
parasite = gimp_parasite_new (PSD_PARASITE_DUOTONE_DATA, 0,
903
img_a->color_map_len, img_a->color_map);
904
gimp_image_parasite_attach (image_id, parasite);
905
gimp_parasite_free (parasite);
907
g_free (img_a->color_map);
914
add_image_resources (const gint32 image_id,
921
if (fseek (f, img_a->image_res_start, SEEK_SET) < 0)
923
psd_set_error (feof (f), errno, error);
927
/* Initialise image resource variables */
928
img_a->no_icc = FALSE;
929
img_a->layer_state = 0;
930
img_a->alpha_names = NULL;
931
img_a->alpha_display_info = NULL;
932
img_a->alpha_display_count = 0;
933
img_a->alpha_id = NULL;
934
img_a->alpha_id_count = 0;
935
img_a->quick_mask_id = 0;
937
while (ftell (f) < img_a->image_res_start + img_a->image_res_len)
939
if (get_image_resource_header (&res_a, f, error) < 0)
942
if (res_a.data_start + res_a.data_len >
943
img_a->image_res_start + img_a->image_res_len)
945
IFDBG(1) g_debug ("Unexpected end of image resource data");
949
if (load_image_resource (&res_a, image_id, img_a, f, error) < 0)
957
add_layers (const gint32 image_id,
963
PSDchannel **lyr_chn;
966
guint16 user_mask_chn;
967
guint16 layer_channels;
968
guint16 channel_idx[MAX_CHANNELS];
969
guint16 *rle_pack_len;
970
gint32 l_x; /* Layer x */
971
gint32 l_y; /* Layer y */
972
gint32 l_w; /* Layer width */
973
gint32 l_h; /* Layer height */
974
gint32 lm_x; /* Layer mask x */
975
gint32 lm_y; /* Layer mask y */
976
gint32 lm_w; /* Layer mask width */
977
gint32 lm_h; /* Layer mask height */
979
gint32 layer_id = -1;
981
gint lidx; /* Layer index */
982
gint cidx; /* Channel index */
983
gint rowi; /* Row index */
984
gint coli; /* Column index */
990
GimpDrawable *drawable;
991
GimpPixelRgn pixel_rgn;
992
GimpImageType image_type;
993
GimpLayerModeEffects layer_mode;
996
IFDBG(2) g_debug ("Number of layers: %d", img_a->num_layers);
998
if (img_a->num_layers == 0)
1000
IFDBG(2) g_debug ("No layers to process");
1004
/* Layered image - Photoshop 3 style */
1005
if (fseek (f, img_a->layer_data_start, SEEK_SET) < 0)
1007
psd_set_error (feof (f), errno, error);
1011
for (lidx = 0; lidx < img_a->num_layers; ++lidx)
1013
IFDBG(2) g_debug ("Process Layer No %d.", lidx);
1015
if (lyr_a[lidx]->drop)
1017
IFDBG(2) g_debug ("Drop layer %d", lidx);
1019
/* Step past layer data */
1020
for (cidx = 0; cidx < lyr_a[lidx]->num_channels; ++cidx)
1022
if (fseek (f, lyr_a[lidx]->chn_info[cidx].data_len, SEEK_CUR) < 0)
1024
psd_set_error (feof (f), errno, error);
1028
g_free (lyr_a[lidx]->chn_info);
1029
g_free (lyr_a[lidx]->name);
1035
if (lyr_a[lidx]->bottom - lyr_a[lidx]->top == 0
1036
|| lyr_a[lidx]->right - lyr_a[lidx]->left == 0)
1042
if (lyr_a[lidx]->layer_mask.bottom - lyr_a[lidx]->layer_mask.top == 0
1043
|| lyr_a[lidx]->layer_mask.right - lyr_a[lidx]->layer_mask.left == 0)
1048
IFDBG(3) g_debug ("Empty mask %d, size %d %d", empty_mask,
1049
lyr_a[lidx]->layer_mask.bottom - lyr_a[lidx]->layer_mask.top,
1050
lyr_a[lidx]->layer_mask.right - lyr_a[lidx]->layer_mask.left);
1052
/* Load layer channel data */
1053
IFDBG(2) g_debug ("Number of channels: %d", lyr_a[lidx]->num_channels);
1054
/* Create pointer array for the channel records */
1055
lyr_chn = g_new (PSDchannel *, lyr_a[lidx]->num_channels);
1056
for (cidx = 0; cidx < lyr_a[lidx]->num_channels; ++cidx)
1058
/* Allocate channel record */
1059
lyr_chn[cidx] = g_malloc (sizeof (PSDchannel) );
1061
lyr_chn[cidx]->id = lyr_a[lidx]->chn_info[cidx].channel_id;
1062
lyr_chn[cidx]->rows = lyr_a[lidx]->bottom - lyr_a[lidx]->top;
1063
lyr_chn[cidx]->columns = lyr_a[lidx]->right - lyr_a[lidx]->left;
1065
if (lyr_chn[cidx]->id == PSD_CHANNEL_MASK)
1067
/* Works around a bug in panotools psd files where the layer mask
1068
size is given as 0 but data exists. Set mask size to layer size.
1070
if (empty_mask && lyr_a[lidx]->chn_info[cidx].data_len - 2 > 0)
1073
if (lyr_a[lidx]->layer_mask.top == lyr_a[lidx]->layer_mask.bottom)
1075
lyr_a[lidx]->layer_mask.top = lyr_a[lidx]->top;
1076
lyr_a[lidx]->layer_mask.bottom = lyr_a[lidx]->bottom;
1078
if (lyr_a[lidx]->layer_mask.right == lyr_a[lidx]->layer_mask.left)
1080
lyr_a[lidx]->layer_mask.right = lyr_a[lidx]->right;
1081
lyr_a[lidx]->layer_mask.left = lyr_a[lidx]->left;
1084
lyr_chn[cidx]->rows = (lyr_a[lidx]->layer_mask.bottom -
1085
lyr_a[lidx]->layer_mask.top);
1086
lyr_chn[cidx]->columns = (lyr_a[lidx]->layer_mask.right -
1087
lyr_a[lidx]->layer_mask.left);
1090
IFDBG(3) g_debug ("Channel id %d, %dx%d",
1092
lyr_chn[cidx]->columns,
1093
lyr_chn[cidx]->rows);
1095
/* Only read channel data if there is more data than
1096
* what compression method that is used
1098
if (lyr_a[lidx]->chn_info[cidx].data_len > COMP_MODE_SIZE)
1102
if (fread (&comp_mode, COMP_MODE_SIZE, 1, f) < 1)
1104
psd_set_error (feof (f), errno, error);
1107
comp_mode = GUINT16_FROM_BE (comp_mode);
1108
IFDBG(3) g_debug ("Compression mode: %d", comp_mode);
1112
case PSD_COMP_RAW: /* Planar raw data */
1113
IFDBG(3) g_debug ("Raw data length: %d",
1114
lyr_a[lidx]->chn_info[cidx].data_len - 2);
1115
if (read_channel_data (lyr_chn[cidx], img_a->bps,
1116
PSD_COMP_RAW, NULL, f, error) < 1)
1120
case PSD_COMP_RLE: /* Packbits */
1121
IFDBG(3) g_debug ("RLE channel length %d, RLE length data: %d, "
1122
"RLE data block: %d",
1123
lyr_a[lidx]->chn_info[cidx].data_len - 2,
1124
lyr_chn[cidx]->rows * 2,
1125
(lyr_a[lidx]->chn_info[cidx].data_len - 2 -
1126
lyr_chn[cidx]->rows * 2));
1127
rle_pack_len = g_malloc (lyr_chn[cidx]->rows * 2);
1128
for (rowi = 0; rowi < lyr_chn[cidx]->rows; ++rowi)
1130
if (fread (&rle_pack_len[rowi], 2, 1, f) < 1)
1132
psd_set_error (feof (f), errno, error);
1135
rle_pack_len[rowi] = GUINT16_FROM_BE (rle_pack_len[rowi]);
1138
IFDBG(3) g_debug ("RLE decode - data");
1139
if (read_channel_data (lyr_chn[cidx], img_a->bps,
1140
PSD_COMP_RLE, rle_pack_len, f, error) < 1)
1143
g_free (rle_pack_len);
1146
case PSD_COMP_ZIP: /* ? */
1147
case PSD_COMP_ZIP_PRED:
1149
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
1150
_("Unsupported compression mode: %d"), comp_mode);
1156
g_free (lyr_a[lidx]->chn_info);
1167
l_w = img_a->columns;
1170
IFDBG(3) g_debug ("Re-hash channel indices");
1171
for (cidx = 0; cidx < lyr_a[lidx]->num_channels; ++cidx)
1173
if (lyr_chn[cidx]->id == PSD_CHANNEL_MASK)
1176
user_mask_chn = cidx;
1178
else if (lyr_chn[cidx]->id == PSD_CHANNEL_ALPHA)
1185
channel_idx[layer_channels] = cidx; /* Assumes in sane order */
1186
layer_channels++; /* RGB, Lab, CMYK etc. */
1191
channel_idx[layer_channels] = alpha_chn;
1197
IFDBG(2) g_debug ("Create blank layer");
1198
image_type = get_gimp_image_type (img_a->base_type, TRUE);
1199
layer_id = gimp_layer_new (image_id, lyr_a[lidx]->name,
1200
img_a->columns, img_a->rows,
1201
image_type, 0, GIMP_NORMAL_MODE);
1202
g_free (lyr_a[lidx]->name);
1203
gimp_image_add_layer (image_id, layer_id, -1);
1204
drawable = gimp_drawable_get (layer_id);
1205
gimp_drawable_fill (drawable->drawable_id, GIMP_TRANSPARENT_FILL);
1206
gimp_drawable_set_visible (drawable->drawable_id, lyr_a[lidx]->layer_flags.visible);
1207
if (lyr_a[lidx]->id)
1208
gimp_drawable_set_tattoo (drawable->drawable_id, lyr_a[lidx]->id);
1209
if (lyr_a[lidx]->layer_flags.irrelevant)
1210
gimp_drawable_set_visible (drawable->drawable_id, FALSE);
1211
gimp_drawable_flush (drawable);
1212
gimp_drawable_detach (drawable);
1216
l_x = lyr_a[lidx]->left;
1217
l_y = lyr_a[lidx]->top;
1218
l_w = lyr_a[lidx]->right - lyr_a[lidx]->left;
1219
l_h = lyr_a[lidx]->bottom - lyr_a[lidx]->top;
1221
IFDBG(3) g_debug ("Draw layer");
1222
image_type = get_gimp_image_type (img_a->base_type, alpha);
1223
IFDBG(3) g_debug ("Layer type %d", image_type);
1224
layer_size = l_w * l_h;
1225
pixels = g_malloc (layer_size * layer_channels);
1226
for (cidx = 0; cidx < layer_channels; ++cidx)
1228
IFDBG(3) g_debug ("Start channel %d", channel_idx[cidx]);
1229
for (i = 0; i < layer_size; ++i)
1230
pixels[(i * layer_channels) + cidx] = lyr_chn[channel_idx[cidx]]->data[i];
1231
g_free (lyr_chn[channel_idx[cidx]]->data);
1234
layer_mode = psd_to_gimp_blend_mode (lyr_a[lidx]->blend_mode);
1235
layer_id = gimp_layer_new (image_id, lyr_a[lidx]->name, l_w, l_h,
1236
image_type, lyr_a[lidx]->opacity * 100 / 255,
1238
IFDBG(3) g_debug ("Layer tattoo: %d", layer_id);
1239
g_free (lyr_a[lidx]->name);
1240
gimp_image_add_layer (image_id, layer_id, -1);
1241
gimp_layer_set_offsets (layer_id, l_x, l_y);
1242
gimp_layer_set_lock_alpha (layer_id, lyr_a[lidx]->layer_flags.trans_prot);
1243
drawable = gimp_drawable_get (layer_id);
1244
gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,
1245
drawable->width, drawable->height, TRUE, FALSE);
1246
gimp_pixel_rgn_set_rect (&pixel_rgn, pixels,
1247
0, 0, drawable->width, drawable->height);
1248
gimp_drawable_set_visible (drawable->drawable_id, lyr_a[lidx]->layer_flags.visible);
1249
if (lyr_a[lidx]->id)
1250
gimp_drawable_set_tattoo (drawable->drawable_id, lyr_a[lidx]->id);
1251
gimp_drawable_flush (drawable);
1252
gimp_drawable_detach (drawable);
1261
IFDBG(3) g_debug ("Create empty mask");
1262
if (lyr_a[lidx]->layer_mask.def_color == 255)
1263
mask_id = gimp_layer_create_mask (layer_id, GIMP_ADD_WHITE_MASK);
1265
mask_id = gimp_layer_create_mask (layer_id, GIMP_ADD_BLACK_MASK);
1266
gimp_layer_add_mask (layer_id, mask_id);
1267
gimp_layer_set_apply_mask (layer_id,
1268
! lyr_a[lidx]->layer_mask.mask_flags.disabled);
1272
/* Load layer mask data */
1273
if (lyr_a[lidx]->layer_mask.mask_flags.relative_pos)
1275
lm_x = lyr_a[lidx]->layer_mask.left;
1276
lm_y = lyr_a[lidx]->layer_mask.top;
1277
lm_w = lyr_a[lidx]->layer_mask.right - lyr_a[lidx]->layer_mask.left;
1278
lm_h = lyr_a[lidx]->layer_mask.bottom - lyr_a[lidx]->layer_mask.top;
1282
lm_x = lyr_a[lidx]->layer_mask.left - l_x;
1283
lm_y = lyr_a[lidx]->layer_mask.top - l_y;
1284
lm_w = lyr_a[lidx]->layer_mask.right - lyr_a[lidx]->layer_mask.left;
1285
lm_h = lyr_a[lidx]->layer_mask.bottom - lyr_a[lidx]->layer_mask.top;
1287
IFDBG(3) g_debug ("Mask channel index %d", user_mask_chn);
1288
IFDBG(3) g_debug ("Relative pos %d",
1289
lyr_a[lidx]->layer_mask.mask_flags.relative_pos);
1290
layer_size = lm_w * lm_h;
1291
pixels = g_malloc (layer_size);
1292
IFDBG(3) g_debug ("Allocate Pixels %d", layer_size);
1293
/* Crop mask at layer boundry */
1294
IFDBG(3) g_debug ("Original Mask %d %d %d %d", lm_x, lm_y, lm_w, lm_h);
1297
|| lm_w + lm_x > l_w
1298
|| lm_h + lm_y > l_h)
1300
if (CONVERSION_WARNINGS)
1301
g_message ("Warning\n"
1302
"The layer mask is partly outside the "
1303
"layer boundary. The mask will be "
1304
"cropped which may result in data loss.");
1306
for (rowi = 0; rowi < lm_h; ++rowi)
1308
if (rowi + lm_y >= 0 && rowi + lm_y < l_h)
1310
for (coli = 0; coli < lm_w; ++coli)
1312
if (coli + lm_x >= 0 && coli + lm_x < l_w)
1315
lyr_chn[user_mask_chn]->data[(rowi * lm_w) + coli];
1331
if (lm_w + lm_x > l_w)
1333
if (lm_h + lm_y > l_h)
1337
memcpy (pixels, lyr_chn[user_mask_chn]->data, layer_size);
1338
g_free (lyr_chn[user_mask_chn]->data);
1339
/* Draw layer mask data */
1340
IFDBG(3) g_debug ("Layer %d %d %d %d", l_x, l_y, l_w, l_h);
1341
IFDBG(3) g_debug ("Mask %d %d %d %d", lm_x, lm_y, lm_w, lm_h);
1343
if (lyr_a[lidx]->layer_mask.def_color == 255)
1344
mask_id = gimp_layer_create_mask (layer_id, GIMP_ADD_WHITE_MASK);
1346
mask_id = gimp_layer_create_mask (layer_id, GIMP_ADD_BLACK_MASK);
1348
IFDBG(3) g_debug ("New layer mask %d", mask_id);
1349
gimp_layer_add_mask (layer_id, mask_id);
1350
drawable = gimp_drawable_get (mask_id);
1351
gimp_pixel_rgn_init (&pixel_rgn, drawable, 0 , 0,
1352
drawable->width, drawable->height, TRUE, FALSE);
1353
gimp_pixel_rgn_set_rect (&pixel_rgn, pixels, lm_x, lm_y, lm_w, lm_h);
1354
gimp_drawable_flush (drawable);
1355
gimp_drawable_detach (drawable);
1356
gimp_layer_set_apply_mask (layer_id,
1357
! lyr_a[lidx]->layer_mask.mask_flags.disabled);
1361
for (cidx = 0; cidx < lyr_a[lidx]->num_channels; ++cidx)
1363
g_free (lyr_chn[cidx]);
1366
g_free (lyr_a[lidx]);
1374
add_merged_image (const gint32 image_id,
1379
PSDchannel chn_a[MAX_CHANNELS];
1383
guint16 base_channels;
1384
guint16 extra_channels;
1385
guint16 total_channels;
1386
guint16 *rle_pack_len[MAX_CHANNELS];
1388
guint32 block_start;
1392
gint32 layer_id = -1;
1393
gint32 channel_id = -1;
1394
gint32 active_layer;
1395
gint16 alpha_opacity;
1397
gint cidx; /* Channel index */
1398
gint rowi; /* Row index */
1402
gboolean alpha_visible;
1403
GimpDrawable *drawable;
1404
GimpPixelRgn pixel_rgn;
1405
GimpImageType image_type;
1408
total_channels = img_a->channels;
1411
if ((img_a->color_mode == PSD_BITMAP ||
1412
img_a->color_mode == PSD_GRAYSCALE ||
1413
img_a->color_mode == PSD_DUOTONE ||
1414
img_a->color_mode == PSD_INDEXED) &&
1417
extra_channels = total_channels - 1;
1419
else if ((img_a->color_mode == PSD_RGB ||
1420
img_a->color_mode == PSD_LAB) &&
1423
extra_channels = total_channels - 3;
1425
else if ((img_a->color_mode == PSD_CMYK) &&
1428
extra_channels = total_channels - 4;
1430
if (img_a->transparency && extra_channels > 0)
1432
base_channels = total_channels - extra_channels;
1434
/* ----- Read merged image & extra channel pixel data ----- */
1435
if (img_a->num_layers == 0
1436
|| extra_channels > 0)
1438
block_start = img_a->merged_image_start;
1439
block_len = img_a->merged_image_len;
1440
block_end = block_start + block_len;
1441
fseek (f, block_start, SEEK_SET);
1443
if (fread (&comp_mode, COMP_MODE_SIZE, 1, f) < 1)
1445
psd_set_error (feof (f), errno, error);
1448
comp_mode = GUINT16_FROM_BE (comp_mode);
1452
case PSD_COMP_RAW: /* Planar raw data */
1453
IFDBG(3) g_debug ("Raw data length: %d", block_len);
1454
for (cidx = 0; cidx < total_channels; ++cidx)
1456
chn_a[cidx].columns = img_a->columns;
1457
chn_a[cidx].rows = img_a->rows;
1458
if (read_channel_data (&chn_a[cidx], img_a->bps,
1459
PSD_COMP_RAW, NULL, f, error) < 1)
1464
case PSD_COMP_RLE: /* Packbits */
1465
/* Image data is stored as packed scanlines in planar order
1466
with all compressed length counters stored first */
1467
IFDBG(3) g_debug ("RLE length data: %d, RLE data block: %d",
1468
total_channels * img_a->rows * 2,
1469
block_len - (total_channels * img_a->rows * 2));
1470
for (cidx = 0; cidx < total_channels; ++cidx)
1472
chn_a[cidx].columns = img_a->columns;
1473
chn_a[cidx].rows = img_a->rows;
1474
rle_pack_len[cidx] = g_malloc (img_a->rows * 2);
1475
for (rowi = 0; rowi < img_a->rows; ++rowi)
1477
if (fread (&rle_pack_len[cidx][rowi], 2, 1, f) < 1)
1479
psd_set_error (feof (f), errno, error);
1482
rle_pack_len[cidx][rowi] = GUINT16_FROM_BE (rle_pack_len[cidx][rowi]);
1486
IFDBG(3) g_debug ("RLE decode - data");
1487
for (cidx = 0; cidx < total_channels; ++cidx)
1489
if (read_channel_data (&chn_a[cidx], img_a->bps,
1490
PSD_COMP_RLE, rle_pack_len[cidx], f, error) < 1)
1492
g_free (rle_pack_len[cidx]);
1496
case PSD_COMP_ZIP: /* ? */
1497
case PSD_COMP_ZIP_PRED:
1498
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
1499
_("Unsupported compression mode: %d"), comp_mode);
1505
/* ----- Draw merged image ----- */
1506
if (img_a->num_layers == 0) /* Merged image - Photoshop 2 style */
1508
image_type = get_gimp_image_type (img_a->base_type, img_a->transparency);
1510
layer_size = img_a->columns * img_a->rows;
1511
pixels = g_malloc (layer_size * base_channels);
1512
for (cidx = 0; cidx < base_channels; ++cidx)
1514
for (i = 0; i < layer_size; ++i)
1516
pixels[(i * base_channels) + cidx] = chn_a[cidx].data[i];
1518
g_free (chn_a[cidx].data);
1521
/* Add background layer */
1522
IFDBG(2) g_debug ("Draw merged image");
1523
layer_id = gimp_layer_new (image_id, _("Background"),
1524
img_a->columns, img_a->rows,
1526
100, GIMP_NORMAL_MODE);
1527
gimp_image_add_layer (image_id, layer_id, 0);
1528
drawable = gimp_drawable_get (layer_id);
1529
gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,
1530
drawable->width, drawable->height, TRUE, FALSE);
1531
gimp_pixel_rgn_set_rect (&pixel_rgn, pixels,
1532
0, 0, drawable->width, drawable->height);
1533
gimp_drawable_flush (drawable);
1534
gimp_drawable_detach (drawable);
1539
/* Free merged image data for layered image */
1541
for (cidx = 0; cidx < base_channels; ++cidx)
1542
g_free (chn_a[cidx].data);
1545
/* ----- Draw extra alpha channels ----- */
1546
if ((extra_channels /* Extra alpha channels */
1547
|| img_a->transparency) /* Transparency alpha channel */
1550
IFDBG(2) g_debug ("Add extra channels");
1551
pixels = g_malloc(0);
1553
/* Get channel resource data */
1554
if (img_a->transparency)
1557
/* Free "Transparency" channel name */
1558
alpha_name = g_ptr_array_index (img_a->alpha_names, 0);
1560
g_free (alpha_name);
1566
IFDBG(2) g_debug ("Number of channels: %d", extra_channels);
1567
for (i = 0; i < extra_channels; ++i)
1569
/* Alpha channel name */
1571
alpha_visible = FALSE;
1572
/* Quick mask channel*/
1573
if (img_a->quick_mask_id)
1574
if (i == img_a->quick_mask_id - base_channels + offset)
1576
/* Free "Quick Mask" channel name */
1577
alpha_name = g_ptr_array_index (img_a->alpha_names, i + offset);
1579
g_free (alpha_name);
1580
alpha_name = g_strdup (GIMP_IMAGE_QUICK_MASK_NAME);
1581
alpha_visible = TRUE;
1583
if (! alpha_name && img_a->alpha_names)
1584
if (offset < img_a->alpha_names->len
1585
&& i + offset <= img_a->alpha_names->len)
1586
alpha_name = g_ptr_array_index (img_a->alpha_names, i + offset);
1588
alpha_name = g_strdup (_("Extra"));
1590
if (offset < img_a->alpha_id_count &&
1591
offset + i <= img_a->alpha_id_count)
1592
alpha_id = img_a->alpha_id[i + offset];
1595
if (offset < img_a->alpha_display_count &&
1596
i + offset <= img_a->alpha_display_count)
1598
alpha_rgb = img_a->alpha_display_info[i + offset]->gimp_color;
1599
alpha_opacity = img_a->alpha_display_info[i + offset]->opacity;
1603
gimp_rgba_set (&alpha_rgb, 1.0, 0.0, 0.0, 1.0);
1607
cidx = base_channels + i;
1608
pixels = g_realloc (pixels, chn_a[cidx].columns * chn_a[cidx].rows);
1609
memcpy (pixels, chn_a[cidx].data, chn_a[cidx].columns * chn_a[cidx].rows);
1610
channel_id = gimp_channel_new (image_id, alpha_name,
1611
chn_a[cidx].columns, chn_a[cidx].rows,
1612
alpha_opacity, &alpha_rgb);
1613
gimp_image_add_channel (image_id, channel_id, 0);
1614
g_free (alpha_name);
1615
drawable = gimp_drawable_get (channel_id);
1617
gimp_drawable_set_tattoo (drawable->drawable_id, alpha_id);
1618
gimp_drawable_set_visible (drawable->drawable_id, alpha_visible);
1619
gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,
1620
drawable->width, drawable->height,
1622
gimp_pixel_rgn_set_rect (&pixel_rgn, pixels,
1623
0, 0, drawable->width,
1625
gimp_drawable_flush (drawable);
1626
gimp_drawable_detach (drawable);
1627
g_free (chn_a[cidx].data);
1631
if (img_a->alpha_names)
1632
g_ptr_array_free (img_a->alpha_names, TRUE);
1634
if (img_a->alpha_id)
1635
g_free (img_a->alpha_id);
1637
if (img_a->alpha_display_info)
1639
for (cidx = 0; cidx < img_a->alpha_display_count; ++cidx)
1640
g_free (img_a->alpha_display_info[cidx]);
1641
g_free (img_a->alpha_display_info);
1645
/* Set active layer */
1646
lyr_lst = gimp_image_get_layers (image_id, &lyr_count);
1647
if (img_a->layer_state + 1 > lyr_count ||
1648
img_a->layer_state + 1 < 0)
1649
img_a->layer_state = 0;
1650
active_layer = lyr_lst[lyr_count - img_a->layer_state - 1];
1651
gimp_image_set_active_layer (image_id, active_layer);
1654
/* FIXME gimp image tattoo state */
1660
/* Local utility functions */
1662
get_psd_color_mode_name (PSDColorMode mode)
1664
static gchar * const psd_color_mode_names[] =
1678
static gchar *err_name = NULL;
1680
if (mode >= PSD_BITMAP && mode <= PSD_LAB)
1681
return psd_color_mode_names[mode];
1684
err_name = g_strdup_printf ("UNKNOWN (%d)", mode);
1690
psd_to_gimp_color_map (guchar *map256)
1695
tmpmap = g_malloc (3 * 256);
1697
for (i = 0; i < 256; ++i)
1699
tmpmap[i*3 ] = map256[i];
1700
tmpmap[i*3+1] = map256[i+256];
1701
tmpmap[i*3+2] = map256[i+512];
1704
memcpy (map256, tmpmap, 3 * 256);
1708
static GimpImageType
1709
get_gimp_image_type (const GimpImageBaseType image_base_type,
1710
const gboolean alpha)
1712
GimpImageType image_type;
1714
switch (image_base_type)
1717
image_type = (alpha) ? GIMP_GRAYA_IMAGE : GIMP_GRAY_IMAGE;
1721
image_type = (alpha) ? GIMP_INDEXEDA_IMAGE : GIMP_INDEXED_IMAGE;
1725
image_type = (alpha) ? GIMP_RGBA_IMAGE : GIMP_RGB_IMAGE;
1737
read_channel_data (PSDchannel *channel,
1739
const guint16 compression,
1740
const guint16 *rle_pack_len,
1747
guint32 readline_len;
1751
readline_len = ((channel->columns + 7) >> 3);
1753
readline_len = (channel->columns * bps >> 3);
1755
IFDBG(3) g_debug ("raw data size %d x %d = %d", readline_len,
1756
channel->rows, readline_len * channel->rows);
1757
raw_data = g_malloc (readline_len * channel->rows);
1758
switch (compression)
1761
if (fread (raw_data, readline_len, channel->rows, f) < 1)
1763
psd_set_error (feof (f), errno, error);
1769
for (i = 0; i < channel->rows; ++i)
1771
src = g_malloc (rle_pack_len[i]);
1772
dst = g_malloc (readline_len);
1773
/* FIXME check for over-run
1774
if (ftell (f) + rle_pack_len[i] > block_end)
1776
psd_set_error (TRUE, errno, error);
1780
if (fread (src, rle_pack_len[i], 1, f) < 1)
1782
psd_set_error (feof (f), errno, error);
1785
/* FIXME check for errors returned from decode packbits */
1786
decode_packbits (src, dst, rle_pack_len[i], readline_len);
1788
memcpy (raw_data + i * readline_len, dst, readline_len);
1794
/* Convert channel data to GIMP format */
1798
channel->data = (gchar *) g_malloc (channel->rows * channel->columns);
1799
convert_16_bit (raw_data, channel->data, (channel->rows * channel->columns) << 1);
1803
channel->data = (gchar *) g_malloc (channel->rows * channel->columns);
1804
memcpy (channel->data, raw_data, (channel->rows * channel->columns));
1808
channel->data = (gchar *) g_malloc (channel->rows * channel->columns);
1809
convert_1_bit (raw_data, channel->data, channel->rows, channel->columns);
1819
convert_16_bit (const gchar *src,
1823
/* Convert 16 bit to 8 bit dropping low byte
1827
IFDBG(3) g_debug ("Start 16 bit conversion");
1829
for (i = 0; i < len >> 1; ++i)
1836
IFDBG(3) g_debug ("End 16 bit conversion");
1840
convert_1_bit (const gchar *src,
1845
/* Convert bits to bytes left to right by row.
1846
Rows are padded out to a byte boundry.
1848
guint32 row_pos = 0;
1851
IFDBG(3) g_debug ("Start 1 bit conversion");
1853
for (i = 0; i < rows * ((columns + 7) >> 3); ++i)
1856
for (j = 0; j < 8 && row_pos < columns; ++j)
1858
*dst = (*src & mask) ? 0 : 1;
1859
IFDBG(3) g_debug ("byte %d, bit %d, offset %d, src %d, dst %d",
1860
i , j, row_pos, *src, *dst);
1865
if (row_pos >= columns)
1869
IFDBG(3) g_debug ("End 1 bit conversion");