psd-load.c 67.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * GIMP PSD Plug-in
 * Copyright 2007 by John Marshall
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <string.h>
#include <errno.h>

#include <glib/gstdio.h>
#include <libgimp/gimp.h>

#include "psd.h"
#include "psd-util.h"
#include "psd-image-res-load.h"
#include "psd-layer-res-load.h"
#include "psd-load.h"

#include "libgimp/stdplugins-intl.h"

/*  Local function prototypes  */
static gint             read_header_block          (PSDimage     *img_a,
40 41
                                                    FILE         *f,
                                                    GError      **error);
42 43

static gint             read_color_mode_block      (PSDimage     *img_a,
44 45
                                                    FILE         *f,
                                                    GError      **error);
46 47

static gint             read_image_resource_block  (PSDimage     *img_a,
48 49
                                                    FILE         *f,
                                                    GError      **error);
50 51

static PSDlayer **      read_layer_block           (PSDimage     *img_a,
52 53
                                                    FILE         *f,
                                                    GError      **error);
54 55

static gint             read_merged_image_block    (PSDimage     *img_a,
56 57
                                                    FILE         *f,
                                                    GError      **error);
58 59 60 61 62 63 64 65 66

static gint32           create_gimp_image          (PSDimage     *img_a,
                                                    const gchar  *filename);

static gint             add_color_map              (const gint32  image_id,
                                                    PSDimage     *img_a);

static gint             add_image_resources        (const gint32  image_id,
                                                    PSDimage     *img_a,
67 68
                                                    FILE         *f,
                                                    GError      **error);
69 70 71 72

static gint             add_layers                 (const gint32  image_id,
                                                    PSDimage     *img_a,
                                                    PSDlayer    **lyr_a,
73 74
                                                    FILE         *f,
                                                    GError      **error);
75 76 77

static gint             add_merged_image           (const gint32  image_id,
                                                    PSDimage     *img_a,
78 79
                                                    FILE         *f,
                                                    GError      **error);
80 81

/*  Local utility function prototypes  */
82 83
static gchar          * get_psd_color_mode_name    (PSDColorMode  mode);

84 85 86 87 88 89 90 91 92
static void             psd_to_gimp_color_map      (guchar       *map256);

static GimpImageType    get_gimp_image_type        (const GimpImageBaseType image_base_type,
                                                    const gboolean          alpha);

static gint             read_channel_data          (PSDchannel     *channel,
                                                    const guint16   bps,
                                                    const guint16   compression,
                                                    const guint16  *rle_pack_len,
93 94
                                                    FILE           *f,
                                                    GError        **error);
95 96 97 98

static void             convert_16_bit             (const gchar *src,
                                                    gchar       *dst,
                                                    guint32      len);
99

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
static void             convert_1_bit              (const gchar *src,
                                                    gchar       *dst,
                                                    guint32      rows,
                                                    guint32      columns);


/* Main file load function */
gint32
load_image (const gchar *filename)
{
  FILE                 *f;
  struct stat           st;
  PSDimage              img_a;
  PSDlayer            **lyr_a;
  gint32                image_id = -1;
115
  GError               *error = NULL;
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

  /* ----- Open PSD file ----- */
  if (g_stat (filename, &st) == -1)
    return -1;

  IFDBG(1) g_debug ("Open file %s", gimp_filename_to_utf8 (filename));
  f = g_fopen (filename, "rb");
  if (f == NULL)
    {
      g_message (_("Could not open '%s' for reading: %s"),
                 gimp_filename_to_utf8 (filename), g_strerror (errno));
      return -1;
    }

  gimp_progress_init_printf (_("Opening '%s'"),
                             gimp_filename_to_utf8 (filename));

  /* ----- Read the PSD file Header block ----- */
134 135 136
  IFDBG(2) g_debug ("Read header block");
  if (read_header_block (&img_a, f, &error) < 0)
    goto load_error;
137 138 139
  gimp_progress_update (0.1);

  /* ----- Read the PSD file Colour Mode block ----- */
140 141 142
  IFDBG(2) g_debug ("Read colour mode block");
  if (read_color_mode_block (&img_a, f, &error) < 0)
    goto load_error;
143 144 145
  gimp_progress_update (0.2);

  /* ----- Read the PSD file Image Resource block ----- */
146 147 148
  IFDBG(2) g_debug ("Read image resource block");
  if (read_image_resource_block (&img_a, f, &error) < 0)
    goto load_error;
149 150 151
  gimp_progress_update (0.3);

  /* ----- Read the PSD file Layer & Mask block ----- */
152 153 154 155
  IFDBG(2) g_debug ("Read layer & mask block");
  lyr_a = read_layer_block (&img_a, f, &error);
  if (img_a.num_layers != 0 && lyr_a == NULL)
    goto load_error;
156 157 158
  gimp_progress_update (0.4);

  /* ----- Read the PSD file Merged Image Data block ----- */
159 160 161
  IFDBG(2) g_debug ("Read merged image and extra alpha channel block");
  if (read_merged_image_block (&img_a, f, &error) < 0)
    goto load_error;
162 163 164
  gimp_progress_update (0.5);

  /* ----- Create GIMP image ----- */
165
  IFDBG(2) g_debug ("Create GIMP image");
166
  image_id = create_gimp_image (&img_a, filename);
167 168
  if (image_id < 0)
    goto load_error;
169 170 171
  gimp_progress_update (0.6);

  /* ----- Add colour map ----- */
172
  IFDBG(2) g_debug ("Add color map");
173
  if (add_color_map (image_id, &img_a) < 0)
174
    goto load_error;
175 176 177
  gimp_progress_update (0.7);

  /* ----- Add image resources ----- */
178 179 180
  IFDBG(2) g_debug ("Add image resources");
  if (add_image_resources (image_id, &img_a, f, &error) < 0)
    goto load_error;
181 182 183
  gimp_progress_update (0.8);

  /* ----- Add layers -----*/
184 185 186
  IFDBG(2) g_debug ("Add layers");
  if (add_layers (image_id, &img_a, lyr_a, f, &error) < 0)
    goto load_error;
187 188 189
  gimp_progress_update (0.9);

  /* ----- Add merged image data and extra alpha channels ----- */
190 191 192 193
  IFDBG(2) g_debug ("Add merged image data and extra alpha channels");
  if (add_merged_image (image_id, &img_a, f, &error) < 0)
    goto load_error;
  gimp_progress_update (1.0);
194 195 196 197 198 199 200 201 202

  IFDBG(2) g_debug ("Close file & return, image id: %d", image_id);
  IFDBG(1) g_debug ("\n----------------------------------------"
                    "----------------------------------------\n");

  gimp_image_clean_all (image_id);
  gimp_image_undo_enable (image_id);
  fclose (f);
  return image_id;
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

  /* ----- Process load errors ----- */
 load_error:
  if (error)
    {
      g_message (_("Error loading PSD file:\n\n%s"), error->message);
      g_error_free (error);
    }

  /* Delete partially loaded image */
  if (image_id > 0)
    gimp_image_delete (image_id);

  /* Close file if Open */
  if (! (f == NULL))
    fclose (f);

  return -1;
221 222 223 224 225 226
}


/* Local functions */

static gint
227 228 229
read_header_block (PSDimage  *img_a,
                   FILE      *f,
                   GError   **error)
230 231 232 233 234 235 236 237 238 239 240 241 242 243
{
  guint16  version;
  gchar    sig[4];
  gchar    buf[6];

  if (fread (sig, 4, 1, f) < 1
      || fread (&version, 2, 1, f) < 1
      || fread (buf, 6, 1, f) < 1
      || fread (&img_a->channels, 2, 1, f) < 1
      || fread (&img_a->rows, 4, 1, f) < 1
      || fread (&img_a->columns, 4, 1, f) < 1
      || fread (&img_a->bps, 2, 1, f) < 1
      || fread (&img_a->color_mode, 2, 1, f) < 1)
    {
244
      psd_set_error (feof (f), errno, error);
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
      return -1;
    }
  version = GUINT16_FROM_BE (version);
  img_a->channels = GUINT16_FROM_BE (img_a->channels);
  img_a->rows = GUINT32_FROM_BE (img_a->rows);
  img_a->columns = GUINT32_FROM_BE (img_a->columns);
  img_a->bps = GUINT16_FROM_BE (img_a->bps);
  img_a->color_mode = GUINT16_FROM_BE (img_a->color_mode);

  IFDBG(1) g_debug ("\n\n\tSig: %.4s\n\tVer: %d\n\tChannels: "
                    "%d\n\tSize: %dx%d\n\tBPS: %d\n\tMode: %d\n",
                    sig, version, img_a->channels,
                    img_a->columns, img_a->rows,
                    img_a->bps, img_a->color_mode);

  if (memcmp (sig, "8BPS", 4) != 0)
    {
262 263
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                  _("Not a valid photoshop document file"));
264 265 266 267 268
      return -1;
    }

  if (version != 1)
    {
269 270
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                  _("Unsupported file format version: %d"), version);
271 272 273 274 275
      return -1;
    }

  if (img_a->channels > MAX_CHANNELS)
    {
276 277
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                  _("Too many channels in file: %d"), img_a->channels);
278 279 280 281 282 283
      return -1;
    }

    /* Photoshop CS (version 8) supports 300000 x 300000, but this
       is currently larger than GIMP_MAX_IMAGE_SIZE */

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
  if (img_a->rows < 1 || img_a->rows > GIMP_MAX_IMAGE_SIZE)
    {
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                  _("Unsupported or invalid image height: %d"),
                  img_a->rows);
      return -1;
    }

  if (img_a->columns < 1 || img_a->columns > GIMP_MAX_IMAGE_SIZE)
    {
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                  _("Unsupported or invalid image width: %d"),
                  img_a->columns);
      return -1;
    }
299 300 301 302 303 304 305

  if (img_a->color_mode != PSD_BITMAP
      && img_a->color_mode != PSD_GRAYSCALE
      && img_a->color_mode != PSD_INDEXED
      && img_a->color_mode != PSD_RGB
      && img_a->color_mode != PSD_DUOTONE)
    {
306 307 308
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                  _("Unsupported color mode: %s"),
                  get_psd_color_mode_name (img_a->color_mode));
309 310 311 312 313 314 315 316 317
      return -1;
    }

  /* Warnings for format conversions */
  switch (img_a->bps)
    {
      case 16:
        IFDBG(3) g_debug ("16 Bit Data");
        if (CONVERSION_WARNINGS)
318 319 320 321
          g_message ("Warning:\n"
                     "The image you are loading has 16 bits per channel. GIMP "
                     "can only handle 8 bit, so it will be converted for you. "
                     "Information will be lost because of this conversion.");
322 323 324 325 326 327 328 329 330 331 332
        break;

      case 8:
        IFDBG(3) g_debug ("8 Bit Data");
        break;

      case 1:
        IFDBG(3) g_debug ("1 Bit Data");
        break;

      default:
333 334
        g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                    _("Unsupported bit depth: %d"), img_a->bps);
335 336 337 338 339 340 341 342
        return -1;
        break;
    }

  return 0;
}

static gint
343 344 345
read_color_mode_block (PSDimage  *img_a,
                       FILE      *f,
                       GError   **error)
346 347 348 349 350 351 352 353
{
  static guchar cmap[] = {0, 0, 0, 255, 255, 255};
  guint32       block_len;

  img_a->color_map_entries = 0;
  img_a->color_map_len = 0;
  if (fread (&block_len, 4, 1, f) < 1)
    {
354
      psd_set_error (feof (f), errno, error);
355 356 357 358 359 360 361 362 363 364 365
      return -1;
    }
  block_len = GUINT32_FROM_BE (block_len);

  IFDBG(1) g_debug ("Color map block size = %d", block_len);

  if (block_len == 0)
    {
      if (img_a->color_mode == PSD_INDEXED ||
          img_a->color_mode == PSD_DUOTONE )
        {
366 367 368
          IFDBG(1) g_debug ("No color block for indexed or duotone image");
          g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                      _("The file is corrupt!"));
369 370 371 372 373 374 375
          return -1;
        }
    }
  else if (img_a->color_mode == PSD_INDEXED)
    {
      if (block_len != 768)
        {
376 377 378
          IFDBG(1) g_debug ("Invalid color block size for indexed image");
          g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                      _("The file is corrupt!"));
379 380 381 382 383 384 385 386
          return -1;
        }
      else
        {
          img_a->color_map_len = block_len;
          img_a->color_map = g_malloc (img_a->color_map_len);
          if (fread (img_a->color_map, block_len, 1, f) < 1)
            {
387
              psd_set_error (feof (f), errno, error);
388 389 390 391 392 393 394 395 396 397 398
              return -1;
            }
          else
            {
              psd_to_gimp_color_map (img_a->color_map);
              img_a->color_map_entries = 256;
            }
        }
    }
  else if (img_a->color_mode == PSD_DUOTONE)
    {
399 400 401 402 403 404 405
      img_a->color_map_len = block_len;
      img_a->color_map = g_malloc (img_a->color_map_len);
      if (fread (img_a->color_map, block_len, 1, f) < 1)
        {
          psd_set_error (feof (f), errno, error);
          return -1;
        }
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
    }

  /* Create color map for bitmap image */
  if (img_a->color_mode == PSD_BITMAP)
    {
      img_a->color_map_len = 6;
      img_a->color_map = g_malloc (img_a->color_map_len);
      memcpy (img_a->color_map, cmap, img_a->color_map_len);
      img_a->color_map_entries = 2;
    }
  IFDBG(2) g_debug ("Colour map data length %d", img_a->color_map_len);

  return 0;
}

static gint
422 423 424
read_image_resource_block (PSDimage  *img_a,
                           FILE      *f,
                           GError   **error)
425 426 427 428 429 430
{
  guint32 block_len;
  guint32 block_end;

  if (fread (&block_len, 4, 1, f) < 1)
    {
431
      psd_set_error (feof (f), errno, error);
432 433 434 435 436 437 438 439 440 441 442
      return -1;
    }
  img_a->image_res_len = GUINT32_FROM_BE (block_len);

  IFDBG(1) g_debug ("Image resource block size = %d", (int)img_a->image_res_len);

  img_a->image_res_start = ftell (f);
  block_end = img_a->image_res_start + img_a->image_res_len;

  if (fseek (f, block_end, SEEK_SET) < 0)
    {
443
      psd_set_error (feof (f), errno, error);
444 445 446 447 448 449 450
      return -1;
    }

  return 0;
}

static PSDlayer **
451 452 453
read_layer_block (PSDimage  *img_a,
                  FILE      *f,
                  GError   **error)
454 455
{
  PSDlayer **lyr_a;
456 457 458 459 460 461 462
  guint32    block_len;
  guint32    block_end;
  guint32    block_rem;
  gint32     read_len;
  gint32     write_len;
  gint       lidx;                  /* Layer index */
  gint       cidx;                  /* Channel index */
463 464 465

  if (fread (&block_len, 4, 1, f) < 1)
    {
466
      psd_set_error (feof (f), errno, error);
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
      img_a->num_layers = -1;
      return NULL;
    }
  img_a->mask_layer_len = GUINT32_FROM_BE (block_len);

  IFDBG(1) g_debug ("Layer and mask block size = %d", img_a->mask_layer_len);

  img_a->transparency = FALSE;
  img_a->layer_data_len = 0;

  if (!img_a->mask_layer_len)
    {
      img_a->num_layers = 0;
      return NULL;
    }
  else
    {
      img_a->mask_layer_start = ftell (f);
      block_end = img_a->mask_layer_start + img_a->mask_layer_len;

      /* Get number of layers */
      if (fread (&block_len, 4, 1, f) < 1
          || fread (&img_a->num_layers, 2, 1, f) < 1)
        {
491
          psd_set_error (feof (f), errno, error);
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
          img_a->num_layers = -1;
          return NULL;
        }
      img_a->num_layers = GINT16_FROM_BE (img_a->num_layers);
      IFDBG(2) g_debug ("Number of layers: %d", img_a->num_layers);

      if (img_a->num_layers < 0)
        {
          img_a->transparency = TRUE;
          img_a->num_layers = -img_a->num_layers;
        }

      if (img_a->num_layers)
        {
          /* Read layer records */
          PSDlayerres           res_a;

          /* Create pointer array for the layer records */
          lyr_a = g_new (PSDlayer *, img_a->num_layers);
          for (lidx = 0; lidx < img_a->num_layers; ++lidx)
            {
              /* Allocate layer record */
              lyr_a[lidx] = (PSDlayer *) g_malloc (sizeof (PSDlayer) );

              /* Initialise record */
              lyr_a[lidx]->drop = FALSE;
              lyr_a[lidx]->id = 0;

              if (fread (&lyr_a[lidx]->top, 4, 1, f) < 1
                  || fread (&lyr_a[lidx]->left, 4, 1, f) < 1
                  || fread (&lyr_a[lidx]->bottom, 4, 1, f) < 1
                  || fread (&lyr_a[lidx]->right, 4, 1, f) < 1
                  || fread (&lyr_a[lidx]->num_channels, 2, 1, f) < 1)
                {
526
                  psd_set_error (feof (f), errno, error);
527 528 529 530 531 532 533 534 535 536
                  return NULL;
                }
              lyr_a[lidx]->top = GUINT32_FROM_BE (lyr_a[lidx]->top);
              lyr_a[lidx]->left = GUINT32_FROM_BE (lyr_a[lidx]->left);
              lyr_a[lidx]->bottom = GUINT32_FROM_BE (lyr_a[lidx]->bottom);
              lyr_a[lidx]->right = GUINT32_FROM_BE (lyr_a[lidx]->right);
              lyr_a[lidx]->num_channels = GUINT16_FROM_BE (lyr_a[lidx]->num_channels);

              if (lyr_a[lidx]->num_channels > MAX_CHANNELS)
                {
537 538
                  g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                              _("Too many channels in layer: %d"),
539 540 541
                              lyr_a[lidx]->num_channels);
                  return NULL;
                }
542
              if (lyr_a[lidx]->bottom - lyr_a[lidx]->top > GIMP_MAX_IMAGE_SIZE)
543
                {
544 545 546 547 548 549 550 551 552 553
                  g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                              _("Unsupported or invalid layer height: %d"),
                              lyr_a[lidx]->bottom - lyr_a[lidx]->top);
                  return NULL;
                }
              if (lyr_a[lidx]->right - lyr_a[lidx]->left > GIMP_MAX_IMAGE_SIZE)
                {
                  g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                              _("Unsupported or invalid layer width: %d"),
                              lyr_a[lidx]->right - lyr_a[lidx]->left);
554 555 556 557 558 559 560 561 562 563 564 565 566 567
                  return NULL;
                }

              IFDBG(2) g_debug ("Layer %d, Coords %d %d %d %d, channels %d, ",
                                 lidx, lyr_a[lidx]->left, lyr_a[lidx]->top,
                                 lyr_a[lidx]->right, lyr_a[lidx]->bottom,
                                 lyr_a[lidx]->num_channels);

              lyr_a[lidx]->chn_info = g_new (ChannelLengthInfo, lyr_a[lidx]->num_channels);
              for (cidx = 0; cidx < lyr_a[lidx]->num_channels; ++cidx)
                {
                  if (fread (&lyr_a[lidx]->chn_info[cidx].channel_id, 2, 1, f) < 1
                      || fread (&lyr_a[lidx]->chn_info[cidx].data_len, 4, 1, f) < 1)
                    {
568
                      psd_set_error (feof (f), errno, error);
569 570 571 572 573 574 575
                      return NULL;
                    }
                  lyr_a[lidx]->chn_info[cidx].channel_id =
                    GINT16_FROM_BE (lyr_a[lidx]->chn_info[cidx].channel_id);
                  lyr_a[lidx]->chn_info[cidx].data_len =
                    GUINT32_FROM_BE (lyr_a[lidx]->chn_info[cidx].data_len);
                  img_a->layer_data_len += lyr_a[lidx]->chn_info[cidx].data_len;
576
                  IFDBG(3) g_debug ("Channel ID %d, data len %d",
577 578 579 580 581 582 583 584 585 586 587 588
                                     lyr_a[lidx]->chn_info[cidx].channel_id,
                                     lyr_a[lidx]->chn_info[cidx].data_len);
                }

              if (fread (lyr_a[lidx]->mode_key, 4, 1, f) < 1
                  || fread (lyr_a[lidx]->blend_mode, 4, 1, f) < 1
                  || fread (&lyr_a[lidx]->opacity, 1, 1, f) < 1
                  || fread (&lyr_a[lidx]->clipping, 1, 1, f) < 1
                  || fread (&lyr_a[lidx]->flags, 1, 1, f) < 1
                  || fread (&lyr_a[lidx]->filler, 1, 1, f) < 1
                  || fread (&lyr_a[lidx]->extra_len, 4, 1, f) < 1)
                {
589
                  psd_set_error (feof (f), errno, error);
590 591 592 593
                  return NULL;
                }
              if (memcmp (lyr_a[lidx]->mode_key, "8BIM", 4) != 0)
                {
594 595 596 597
                  IFDBG(1) g_debug ("Incorrect layer mode signature %.4s",
                                    lyr_a[lidx]->mode_key);
                  g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                              _("The file is corrupt!"));
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
                  return NULL;
                }

              lyr_a[lidx]->layer_flags.trans_prot = lyr_a[lidx]->flags & 1 ? TRUE : FALSE;
              lyr_a[lidx]->layer_flags.visible = lyr_a[lidx]->flags & 2 ? FALSE : TRUE;
              if (lyr_a[lidx]->flags & 8)
                lyr_a[lidx]->layer_flags.irrelevant = lyr_a[lidx]->flags & 16 ? TRUE : FALSE;
              else
                lyr_a[lidx]->layer_flags.irrelevant = FALSE;

              lyr_a[lidx]->extra_len = GUINT32_FROM_BE (lyr_a[lidx]->extra_len);
              block_rem = lyr_a[lidx]->extra_len;
              IFDBG(2) g_debug ("\n\tLayer mode sig: %.4s\n\tBlend mode: %.4s\n\t"
                                "Opacity: %d\n\tClipping: %d\n\tExtra data len: %d\n\t"
                                "Alpha lock: %d\n\tVisible: %d\n\tIrrelevant: %d",
                                    lyr_a[lidx]->mode_key,
                                    lyr_a[lidx]->blend_mode,
                                    lyr_a[lidx]->opacity,
                                    lyr_a[lidx]->clipping,
                                    lyr_a[lidx]->extra_len,
                                    lyr_a[lidx]->layer_flags.trans_prot,
                                    lyr_a[lidx]->layer_flags.visible,
                                    lyr_a[lidx]->layer_flags.irrelevant);
              IFDBG(3) g_debug ("Remaining length %d", block_rem);

              /* Layer mask data */
              if (fread (&block_len, 4, 1, f) < 1)
                {
626
                  psd_set_error (feof (f), errno, error);
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
                  return NULL;
                }
              block_len = GUINT32_FROM_BE (block_len);
              block_rem -= (block_len + 4);
              IFDBG(3) g_debug ("Remaining length %d", block_rem);

              lyr_a[lidx]->layer_mask_extra.top = 0;
              lyr_a[lidx]->layer_mask_extra.left = 0;
              lyr_a[lidx]->layer_mask_extra.bottom = 0;
              lyr_a[lidx]->layer_mask_extra.right = 0;
              lyr_a[lidx]->layer_mask.top = 0;
              lyr_a[lidx]->layer_mask.left = 0;
              lyr_a[lidx]->layer_mask.bottom = 0;
              lyr_a[lidx]->layer_mask.right = 0;
              lyr_a[lidx]->layer_mask.def_color = 0;
              lyr_a[lidx]->layer_mask.extra_def_color = 0;
              lyr_a[lidx]->layer_mask.mask_flags.relative_pos = FALSE;
              lyr_a[lidx]->layer_mask.mask_flags.disabled = FALSE;
              lyr_a[lidx]->layer_mask.mask_flags.invert = FALSE;

              switch (block_len)
                {
                  case 0:
                    break;

                  case 20:
                    if (fread (&lyr_a[lidx]->layer_mask.top, 4, 1, f) < 1
                        || fread (&lyr_a[lidx]->layer_mask.left, 4, 1, f) < 1
                        || fread (&lyr_a[lidx]->layer_mask.bottom, 4, 1, f) < 1
                        || fread (&lyr_a[lidx]->layer_mask.right, 4, 1, f) < 1
                        || fread (&lyr_a[lidx]->layer_mask.def_color, 1, 1, f) < 1
                        || fread (&lyr_a[lidx]->layer_mask.flags, 1, 1, f) < 1
                        || fread (&lyr_a[lidx]->layer_mask.extra_def_color, 1, 1, f) < 1
                        || fread (&lyr_a[lidx]->layer_mask.extra_flags, 1, 1, f) < 1)
                      {
662
                        psd_set_error (feof (f), errno, error);
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
                        return NULL;
                      }
                    lyr_a[lidx]->layer_mask.top =
                      GUINT32_FROM_BE (lyr_a[lidx]->layer_mask.top);
                    lyr_a[lidx]->layer_mask.left =
                      GUINT32_FROM_BE (lyr_a[lidx]->layer_mask.left);
                    lyr_a[lidx]->layer_mask.bottom =
                      GUINT32_FROM_BE (lyr_a[lidx]->layer_mask.bottom);
                    lyr_a[lidx]->layer_mask.right =
                      GUINT32_FROM_BE (lyr_a[lidx]->layer_mask.right);
                    lyr_a[lidx]->layer_mask.mask_flags.relative_pos =
                      lyr_a[lidx]->layer_mask.flags & 1 ? TRUE : FALSE;
                    lyr_a[lidx]->layer_mask.mask_flags.disabled =
                      lyr_a[lidx]->layer_mask.flags & 2 ? TRUE : FALSE;
                    lyr_a[lidx]->layer_mask.mask_flags.invert =
                      lyr_a[lidx]->layer_mask.flags & 4 ? TRUE : FALSE;
                    break;
                  case 36: /* If we have a 36 byte mask record assume second data set is correct */
                    if (fread (&lyr_a[lidx]->layer_mask_extra.top, 4, 1, f) < 1
                        || fread (&lyr_a[lidx]->layer_mask_extra.left, 4, 1, f) < 1
                        || fread (&lyr_a[lidx]->layer_mask_extra.bottom, 4, 1, f) < 1
                        || fread (&lyr_a[lidx]->layer_mask_extra.right, 4, 1, f) < 1
                        || fread (&lyr_a[lidx]->layer_mask.extra_def_color, 1, 1, f) < 1
                        || fread (&lyr_a[lidx]->layer_mask.extra_flags, 1, 1, f) < 1
                        || fread (&lyr_a[lidx]->layer_mask.def_color, 1, 1, f) < 1
                        || fread (&lyr_a[lidx]->layer_mask.flags, 1, 1, f) < 1
                        || fread (&lyr_a[lidx]->layer_mask.top, 4, 1, f) < 1
                        || fread (&lyr_a[lidx]->layer_mask.left, 4, 1, f) < 1
                        || fread (&lyr_a[lidx]->layer_mask.bottom, 4, 1, f) < 1
                        || fread (&lyr_a[lidx]->layer_mask.right, 4, 1, f) < 1)
                      {
694
                        psd_set_error (feof (f), errno, error);
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
                        return NULL;
                      }
                    lyr_a[lidx]->layer_mask_extra.top =
                      GUINT32_FROM_BE (lyr_a[lidx]->layer_mask_extra.top);
                    lyr_a[lidx]->layer_mask_extra.left =
                      GUINT32_FROM_BE (lyr_a[lidx]->layer_mask_extra.left);
                    lyr_a[lidx]->layer_mask_extra.bottom =
                      GUINT32_FROM_BE (lyr_a[lidx]->layer_mask_extra.bottom);
                    lyr_a[lidx]->layer_mask_extra.right =
                      GUINT32_FROM_BE (lyr_a[lidx]->layer_mask_extra.right);
                    lyr_a[lidx]->layer_mask.top =
                      GUINT32_FROM_BE (lyr_a[lidx]->layer_mask.top);
                    lyr_a[lidx]->layer_mask.left =
                      GUINT32_FROM_BE (lyr_a[lidx]->layer_mask.left);
                    lyr_a[lidx]->layer_mask.bottom =
                      GUINT32_FROM_BE (lyr_a[lidx]->layer_mask.bottom);
                    lyr_a[lidx]->layer_mask.right =
                      GUINT32_FROM_BE (lyr_a[lidx]->layer_mask.right);
                    lyr_a[lidx]->layer_mask.mask_flags.relative_pos =
                      lyr_a[lidx]->layer_mask.flags & 1 ? TRUE : FALSE;
                    lyr_a[lidx]->layer_mask.mask_flags.disabled =
                      lyr_a[lidx]->layer_mask.flags & 2 ? TRUE : FALSE;
                    lyr_a[lidx]->layer_mask.mask_flags.invert =
                      lyr_a[lidx]->layer_mask.flags & 4 ? TRUE : FALSE;
                    break;

                  default:
722
                    IFDBG(1) g_debug ("Unknown layer mask record size ... skipping");
723 724
                    if (fseek (f, block_len, SEEK_CUR) < 0)
                      {
725
                        psd_set_error (feof (f), errno, error);
726 727 728 729 730 731 732 733 734 735 736
                        return NULL;
                      }
                }

              IFDBG(2) g_debug ("Layer mask coords %d %d %d %d, Rel pos %d",
                                lyr_a[lidx]->layer_mask.left,
                                lyr_a[lidx]->layer_mask.top,
                                lyr_a[lidx]->layer_mask.right,
                                lyr_a[lidx]->layer_mask.bottom,
                                lyr_a[lidx]->layer_mask.mask_flags.relative_pos);

737
              IFDBG(3) g_debug ("Default mask color, %d, %d",
738 739 740 741 742 743
                                lyr_a[lidx]->layer_mask.def_color,
                                lyr_a[lidx]->layer_mask.extra_def_color);

              /* Layer blending ranges */           /* FIXME  */
              if (fread (&block_len, 4, 1, f) < 1)
                {
744
                  psd_set_error (feof (f), errno, error);
745 746 747 748 749 750 751 752 753
                  return NULL;
                }
              block_len = GUINT32_FROM_BE (block_len);
              block_rem -= (block_len + 4);
              IFDBG(3) g_debug ("Remaining length %d", block_rem);
              if (block_len > 0)
                {
                  if (fseek (f, block_len, SEEK_CUR) < 0)
                    {
754
                      psd_set_error (feof (f), errno, error);
755 756 757 758
                      return NULL;
                    }
                }

759 760 761 762
              lyr_a[lidx]->name = fread_pascal_string (&read_len, &write_len,
                                                       4, f, error);
              if (*error)
                return NULL;
763 764 765 766 767 768 769
              block_rem -= read_len;
              IFDBG(3) g_debug ("Remaining length %d", block_rem);

              /* Adjustment layer info */           /* FIXME */

              while (block_rem > 7)
                {
770
                  if (get_layer_resource_header (&res_a, f, error) < 0)
771 772 773 774 775
                    return NULL;
                  block_rem -= 12;

                  if (res_a.data_len > block_rem)
                    {
776 777 778
                      IFDBG(1) g_debug ("Unexpected end of layer resource data");
                      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                                  _("The file is corrupt!"));
779 780 781
                      return NULL;
                    }

782
                  if (load_layer_resource (&res_a, lyr_a[lidx], f, error) < 0)
783 784 785 786 787 788 789
                    return NULL;
                  block_rem -= res_a.data_len;
                }
              if (block_rem > 0)
                {
                  if (fseek (f, block_rem, SEEK_CUR) < 0)
                    {
790
                      psd_set_error (feof (f), errno, error);
791 792 793 794 795 796 797 798
                      return NULL;
                    }
                }
            }

          img_a->layer_data_start = ftell(f);
          if (fseek (f, img_a->layer_data_len, SEEK_CUR) < 0)
            {
799
              psd_set_error (feof (f), errno, error);
800 801 802 803 804 805 806 807 808 809 810 811 812 813
              return NULL;
            }

          IFDBG(1) g_debug ("Layer image data block size %d",
                             img_a->layer_data_len);
        }
      else
        lyr_a = NULL;

      /* Read global layer mask record */       /* FIXME */

      /* Skip to end of block */
      if (fseek (f, block_end, SEEK_SET) < 0)
        {
814
          psd_set_error (feof (f), errno, error);
815 816 817 818 819 820 821 822
          return NULL;
        }
    }

  return lyr_a;
}

static gint
823 824 825
read_merged_image_block (PSDimage  *img_a,
                         FILE      *f,
                         GError   **error)
826 827
{
  img_a->merged_image_start = ftell(f);
828 829 830 831 832 833
  if (fseek (f, 0, SEEK_END) < 0)
    {
      psd_set_error (feof (f), errno, error);
      return -1;
    }

834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
  img_a->merged_image_len = ftell(f) - img_a->merged_image_start;

  IFDBG(1) g_debug ("Merged image data block: Start: %d, len: %d",
                     img_a->merged_image_start, img_a->merged_image_len);

  return 0;
}

static gint32
create_gimp_image (PSDimage    *img_a,
                   const gchar *filename)
{
  gint32 image_id = -1;

  switch (img_a->color_mode)
    {
      case PSD_GRAYSCALE:
      case PSD_DUOTONE:
        img_a->base_type = GIMP_GRAY;
        break;

      case PSD_BITMAP:
      case PSD_INDEXED:
        img_a->base_type = GIMP_INDEXED;
        break;

      case PSD_RGB:
        img_a->base_type = GIMP_RGB;
        break;

      default:
865 866
        /* Color mode already validated - should not be here */
        g_warning ("Invalid color mode");
867 868 869 870 871 872
        return -1;
        break;
    }

  /* Create gimp image */
  IFDBG(2) g_debug ("Create image");
873
  image_id = gimp_image_new (img_a->columns, img_a->rows, img_a->base_type);
874 875

  gimp_image_set_filename (image_id, filename);
876 877
  gimp_image_undo_disable (image_id);

878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
  return image_id;
}

static gint
add_color_map (const gint32  image_id,
               PSDimage     *img_a)
{
  GimpParasite *parasite;

  if (img_a->color_map_len)
    {
      if (img_a->color_mode != PSD_DUOTONE)
        gimp_image_set_colormap (image_id, img_a->color_map, img_a->color_map_entries);
      else
        {
           /* Add parasite for Duotone color data */
          IFDBG(2) g_debug ("Add Duotone color data parasite");
          parasite = gimp_parasite_new (PSD_PARASITE_DUOTONE_DATA, 0,
                                        img_a->color_map_len, img_a->color_map);
          gimp_image_parasite_attach (image_id, parasite);
          gimp_parasite_free (parasite);
        }
      g_free (img_a->color_map);
    }

  return 0;
}

static gint
add_image_resources (const gint32  image_id,
                     PSDimage     *img_a,
909 910
                     FILE         *f,
                     GError      **error)
911 912 913 914 915
{
  PSDimageres  res_a;

  if (fseek (f, img_a->image_res_start, SEEK_SET) < 0)
    {
916
      psd_set_error (feof (f), errno, error);
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
      return -1;
    }

  /* Initialise image resource variables */
  img_a->no_icc = FALSE;
  img_a->layer_state = 0;
  img_a->alpha_names = NULL;
  img_a->alpha_display_info = NULL;
  img_a->alpha_display_count = 0;
  img_a->alpha_id = NULL;
  img_a->alpha_id_count = 0;
  img_a->quick_mask_id = 0;

  while (ftell (f) < img_a->image_res_start + img_a->image_res_len)
    {
932
      if (get_image_resource_header (&res_a, f, error) < 0)
933 934 935 936 937
        return -1;

      if (res_a.data_start + res_a.data_len >
          img_a->image_res_start + img_a->image_res_len)
        {
938
          IFDBG(1) g_debug ("Unexpected end of image resource data");
939 940 941
          return 0;
        }

942
      if (load_image_resource (&res_a, image_id, img_a, f, error) < 0)
943 944 945 946 947 948 949 950 951 952
        return -1;
    }

  return 0;
}

static gint
add_layers (const gint32  image_id,
            PSDimage     *img_a,
            PSDlayer    **lyr_a,
953 954
            FILE         *f,
            GError      **error)
955 956 957
{
  PSDchannel          **lyr_chn;
  guchar               *pixels;
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
  guint16               comp_mode;
  guint16               alpha_chn;
  guint16               user_mask_chn;
  guint16               layer_channels;
  guint16               channel_idx[MAX_CHANNELS];
  guint16              *rle_pack_len;
  gint32                l_x;                   /* Layer x */
  gint32                l_y;                   /* Layer y */
  gint32                l_w;                   /* Layer width */
  gint32                l_h;                   /* Layer height */
  gint32                lm_x;                  /* Layer mask x */
  gint32                lm_y;                  /* Layer mask y */
  gint32                lm_w;                  /* Layer mask width */
  gint32                lm_h;                  /* Layer mask height */
  gint32                layer_size;
  gint32                layer_id = -1;
  gint32                mask_id = -1;
  gint                  lidx;                  /* Layer index */
  gint                  cidx;                  /* Channel index */
  gint                  rowi;                  /* Row index */
  gint                  coli;                  /* Column index */
  gint                  i;
  gboolean              alpha;
  gboolean              user_mask;
  gboolean              empty;
  gboolean              empty_mask;
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
  GimpDrawable         *drawable;
  GimpPixelRgn          pixel_rgn;
  GimpImageType         image_type;
  GimpLayerModeEffects  layer_mode;


  IFDBG(2) g_debug ("Number of layers: %d", img_a->num_layers);

  if (img_a->num_layers == 0)
    {
      IFDBG(2) g_debug ("No layers to process");
      return 0;
    }

  /* Layered image - Photoshop 3 style */
  if (fseek (f, img_a->layer_data_start, SEEK_SET) < 0)
    {
1001
      psd_set_error (feof (f), errno, error);
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
      return -1;
    }

  for (lidx = 0; lidx < img_a->num_layers; ++lidx)
    {
      IFDBG(2) g_debug ("Process Layer No %d.", lidx);

      if (lyr_a[lidx]->drop)
        {
          IFDBG(2) g_debug ("Drop layer %d", lidx);

          /* Step past layer data */
          for (cidx = 0; cidx < lyr_a[lidx]->num_channels; ++cidx)
1015 1016 1017 1018 1019 1020 1021
            {
              if (fseek (f, lyr_a[lidx]->chn_info[cidx].data_len, SEEK_CUR) < 0)
                {
                  psd_set_error (feof (f), errno, error);
                  return -1;
                }
            }
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
          g_free (lyr_a[lidx]->chn_info);
          g_free (lyr_a[lidx]->name);
        }

      else
        {
          /* Empty layer */
          if (lyr_a[lidx]->bottom - lyr_a[lidx]->top == 0
              || lyr_a[lidx]->right - lyr_a[lidx]->left == 0)
              empty = TRUE;
          else
              empty = FALSE;

          /* Empty mask */
          if (lyr_a[lidx]->layer_mask.bottom - lyr_a[lidx]->layer_mask.top == 0
              || lyr_a[lidx]->layer_mask.right - lyr_a[lidx]->layer_mask.left == 0)
              empty_mask = TRUE;
          else
              empty_mask = FALSE;

1042
          IFDBG(3) g_debug ("Empty mask %d, size %d %d", empty_mask,
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
                            lyr_a[lidx]->layer_mask.bottom - lyr_a[lidx]->layer_mask.top,
                            lyr_a[lidx]->layer_mask.right - lyr_a[lidx]->layer_mask.left);

          /* Load layer channel data */
          IFDBG(2) g_debug ("Number of channels: %d", lyr_a[lidx]->num_channels);
          /* Create pointer array for the channel records */
          lyr_chn = g_new (PSDchannel *, lyr_a[lidx]->num_channels);
          for (cidx = 0; cidx < lyr_a[lidx]->num_channels; ++cidx)
            {
              /* Allocate channel record */
              lyr_chn[cidx] = g_malloc (sizeof (PSDchannel) );

              lyr_chn[cidx]->id = lyr_a[lidx]->chn_info[cidx].channel_id;
              lyr_chn[cidx]->rows = lyr_a[lidx]->bottom - lyr_a[lidx]->top;
              lyr_chn[cidx]->columns = lyr_a[lidx]->right - lyr_a[lidx]->left;

              if (lyr_chn[cidx]->id == PSD_CHANNEL_MASK)
                {
                  /* Works around a bug in panotools psd files where the layer mask
                     size is given as 0 but data exists. Set mask size to layer size.
                  */
                  if (empty_mask && lyr_a[lidx]->chn_info[cidx].data_len - 2 > 0)
                    {
                      empty_mask = FALSE;
                      if (lyr_a[lidx]->layer_mask.top == lyr_a[lidx]->layer_mask.bottom)
                        {
                          lyr_a[lidx]->layer_mask.top = lyr_a[lidx]->top;
                          lyr_a[lidx]->layer_mask.bottom = lyr_a[lidx]->bottom;
                        }
                      if (lyr_a[lidx]->layer_mask.right == lyr_a[lidx]->layer_mask.left)
                        {
                          lyr_a[lidx]->layer_mask.right = lyr_a[lidx]->right;
                          lyr_a[lidx]->layer_mask.left = lyr_a[lidx]->left;
                        }
                    }
                  lyr_chn[cidx]->rows = (lyr_a[lidx]->layer_mask.bottom -
                                        lyr_a[lidx]->layer_mask.top);
                  lyr_chn[cidx]->columns = (lyr_a[lidx]->layer_mask.right -
                                           lyr_a[lidx]->layer_mask.left);
                }

1084
              IFDBG(3) g_debug ("Channel id %d, %dx%d",
1085 1086 1087 1088 1089 1090
                                lyr_chn[cidx]->id,
                                lyr_chn[cidx]->columns,
                                lyr_chn[cidx]->rows);

              if (fread (&comp_mode, 2, 1, f) < 1)
                {
1091
                  psd_set_error (feof (f), errno, error);
1092 1093 1094
                  return -1;
                }
              comp_mode = GUINT16_FROM_BE (comp_mode);
1095
              IFDBG(3) g_debug ("Compression mode: %d", comp_mode);
1096 1097 1098 1099 1100 1101

              if (lyr_a[lidx]->chn_info[cidx].data_len - 2 > 0)
                {
                  switch (comp_mode)
                    {
                      case PSD_COMP_RAW:        /* Planar raw data */
1102
                        IFDBG(3) g_debug ("Raw data length: %d",
1103
                                          lyr_a[lidx]->chn_info[cidx].data_len - 2);
1104 1105 1106
                        if (read_channel_data (lyr_chn[cidx], img_a->bps,
                            PSD_COMP_RAW, NULL, f, error) < 1)
                          return -1;
1107 1108 1109
                        break;

                      case PSD_COMP_RLE:        /* Packbits */
1110
                        IFDBG(3) g_debug ("RLE channel length %d, RLE length data: %d, "
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
                                          "RLE data block: %d",
                                          lyr_a[lidx]->chn_info[cidx].data_len - 2,
                                          lyr_chn[cidx]->rows * 2,
                                          (lyr_a[lidx]->chn_info[cidx].data_len - 2 -
                                           lyr_chn[cidx]->rows * 2));
                        rle_pack_len = g_malloc (lyr_chn[cidx]->rows * 2);
                        for (rowi = 0; rowi < lyr_chn[cidx]->rows; ++rowi)
                          {
                            if (fread (&rle_pack_len[rowi], 2, 1, f) < 1)
                              {
1121
                                psd_set_error (feof (f), errno, error);
1122 1123 1124 1125 1126 1127 1128
                                return -1;
                              }
                                rle_pack_len[rowi] = GUINT16_FROM_BE (rle_pack_len[rowi]);
                          }

                        IFDBG(3) g_debug ("RLE decode - data");
                        if (read_channel_data (lyr_chn[cidx], img_a->bps,
1129 1130
                            PSD_COMP_RLE, rle_pack_len, f, error) < 1)
                          return -1;
1131 1132 1133 1134 1135 1136

                        g_free (rle_pack_len);
                        break;

                      case PSD_COMP_ZIP:                 /* ? */
                      case PSD_COMP_ZIP_PRED:
1137 1138
                        g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                                    _("Unsupported compression mode: %d"), comp_mode);
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
                        return -1;
                        break;
                    }
                }
            }
          g_free (lyr_a[lidx]->chn_info);

          /* Draw layer */

          alpha = FALSE;
          alpha_chn = -1;
          user_mask = FALSE;
          user_mask_chn = -1;
          layer_channels = 0;
          l_x = 0;
          l_y = 0;
          l_w = img_a->columns;
          l_h = img_a->rows;

1158
          IFDBG(3) g_debug ("Re-hash channel indices");
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
          for (cidx = 0; cidx < lyr_a[lidx]->num_channels; ++cidx)
            {
              if (lyr_chn[cidx]->id == PSD_CHANNEL_MASK)
                {
                  user_mask = TRUE;
                  user_mask_chn = cidx;
                }
              else if (lyr_chn[cidx]->id == PSD_CHANNEL_ALPHA)
                {
                  alpha = TRUE;
                  alpha_chn = cidx;
                }
              else
                {
                  channel_idx[layer_channels] = cidx;   /* Assumes in sane order */
                  layer_channels++;                     /* RGB, Lab, CMYK etc.   */
                }
            }
          if (alpha)
            {
              channel_idx[layer_channels] = alpha_chn;
              layer_channels++;
            }

          if (empty)
            {
              IFDBG(2) g_debug ("Create blank layer");
              image_type = get_gimp_image_type (img_a->base_type, TRUE);
              layer_id = gimp_layer_new (image_id, lyr_a[lidx]->name,
                                         img_a->columns, img_a->rows,
                                         image_type, 0, GIMP_NORMAL_MODE);
              g_free (lyr_a[lidx]->name);
              gimp_image_add_layer (image_id, layer_id, -1);
              drawable = gimp_drawable_get (layer_id);
              gimp_drawable_fill (drawable->drawable_id, GIMP_TRANSPARENT_FILL);
              gimp_drawable_set_visible (drawable->drawable_id, lyr_a[lidx]->layer_flags.visible);
              if (lyr_a[lidx]->id)
                gimp_drawable_set_tattoo (drawable->drawable_id, lyr_a[lidx]->id);
              if (lyr_a[lidx]->layer_flags.irrelevant)
                gimp_drawable_set_visible (drawable->drawable_id, FALSE);
              gimp_drawable_flush (drawable);
              gimp_drawable_detach (drawable);
            }
          else
            {
              l_x = lyr_a[lidx]->left;
              l_y = lyr_a[lidx]->top;
              l_w = lyr_a[lidx]->right - lyr_a[lidx]->left;
              l_h = lyr_a[lidx]->bottom - lyr_a[lidx]->top;

1209
              IFDBG(3) g_debug ("Draw layer");
1210
              image_type = get_gimp_image_type (img_a->base_type, alpha);
1211
              IFDBG(3) g_debug ("Layer type %d", image_type);
1212 1213 1214 1215
              layer_size = l_w * l_h;
              pixels = g_malloc (layer_size * layer_channels);
              for (cidx = 0; cidx < layer_channels; ++cidx)
                {
1216
                  IFDBG(3) g_debug ("Start channel %d", channel_idx[cidx]);
1217
                  for (i = 0; i < layer_size; ++i)
1218
                    pixels[(i * layer_channels) + cidx] = lyr_chn[channel_idx[cidx]]->data[i];
1219 1220 1221 1222 1223 1224 1225
                  g_free (lyr_chn[channel_idx[cidx]]->data);
                }

              layer_mode = psd_to_gimp_blend_mode (lyr_a[lidx]->blend_mode);
              layer_id = gimp_layer_new (image_id, lyr_a[lidx]->name, l_w, l_h,
                                         image_type, lyr_a[lidx]->opacity * 100 / 255,
                                         layer_mode);
1226
              IFDBG(3) g_debug ("Layer tattoo: %d", layer_id);
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
              g_free (lyr_a[lidx]->name);
              gimp_image_add_layer (image_id, layer_id, -1);
              gimp_layer_set_offsets (layer_id, l_x, l_y);
              gimp_layer_set_lock_alpha  (layer_id, lyr_a[lidx]->layer_flags.trans_prot);
              drawable = gimp_drawable_get (layer_id);
              gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,
                                   drawable->width, drawable->height, TRUE, FALSE);
              gimp_pixel_rgn_set_rect (&pixel_rgn, pixels,
                                       0, 0, drawable->width, drawable->height);
              gimp_drawable_set_visible (drawable->drawable_id, lyr_a[lidx]->layer_flags.visible);
              if (lyr_a[lidx]->id)
                gimp_drawable_set_tattoo (drawable->drawable_id, lyr_a[lidx]->id);
              gimp_drawable_flush (drawable);
              gimp_drawable_detach (drawable);
              g_free (pixels);
            }

          /* Layer mask */
          if (user_mask)
            {
              if (empty_mask)
                {
1249
                  IFDBG(3) g_debug ("Create empty mask");
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
                  if (lyr_a[lidx]->layer_mask.def_color == 255)
                    mask_id = gimp_layer_create_mask (layer_id, GIMP_ADD_WHITE_MASK);
                  else
                    mask_id = gimp_layer_create_mask (layer_id, GIMP_ADD_BLACK_MASK);
                  gimp_layer_add_mask (layer_id, mask_id);
                  gimp_layer_set_apply_mask (layer_id,
                    ! lyr_a[lidx]->layer_mask.mask_flags.disabled);
                }
              else
                {
                  /* Load layer mask data */
                  if (lyr_a[lidx]->layer_mask.mask_flags.relative_pos)
                    {
                      lm_x = lyr_a[lidx]->layer_mask.left;
                      lm_y = lyr_a[lidx]->layer_mask.top;
                      lm_w = lyr_a[lidx]->layer_mask.right - lyr_a[lidx]->layer_mask.left;
                      lm_h = lyr_a[lidx]->layer_mask.bottom - lyr_a[lidx]->layer_mask.top;
                    }
                  else
                    {
                      lm_x = lyr_a[lidx]->layer_mask.left - l_x;
                      lm_y = lyr_a[lidx]->layer_mask.top - l_y;
                      lm_w = lyr_a[lidx]->layer_mask.right - lyr_a[lidx]->layer_mask.left;
                      lm_h = lyr_a[lidx]->layer_mask.bottom - lyr_a[lidx]->layer_mask.top;
                    }
1275 1276
                  IFDBG(3) g_debug ("Mask channel index %d", user_mask_chn);
                  IFDBG(3) g_debug ("Relative pos %d",
1277 1278 1279
                                    lyr_a[lidx]->layer_mask.mask_flags.relative_pos);
                  layer_size = lm_w * lm_h;
                  pixels = g_malloc (layer_size);
1280
                  IFDBG(3) g_debug ("Allocate Pixels %d", layer_size);
1281
                  /* Crop mask at layer boundry */
1282
                  IFDBG(3) g_debug ("Original Mask %d %d %d %d", lm_x, lm_y, lm_w, lm_h);
1283 1284 1285 1286 1287 1288
                  if (lm_x < 0
                      || lm_y < 0
                      || lm_w + lm_x > l_w
                      || lm_h + lm_y > l_h)
                    {
                      if (CONVERSION_WARNINGS)
1289 1290 1291
                        g_message ("Warning\n"
                                   "Layer mask partly lies outside layer boundry. The mask will be "
                                   "cropped which may result in data loss.");
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
                      i = 0;
                      for (rowi = 0; rowi < lm_h; ++rowi)
                        {
                          if (rowi + lm_y >= 0 && rowi + lm_y < l_h)
                            {
                              for (coli = 0; coli < lm_w; ++coli)
                                {
                                  if (coli + lm_x >= 0 && coli + lm_x < l_w)
                                    {
                                      pixels[i] =
                                        lyr_chn[user_mask_chn]->data[(rowi * lm_w) + coli];
                                      i++;
                                    }
                                }
                            }
                        }
                      if (lm_x < 0)
                        {
                          lm_w += lm_x;
                          lm_x = 0;
                        }
                      if (lm_y < 0)
                        {
                          lm_h += lm_y;
                          lm_y = 0;
                        }
                      if (lm_w + lm_x > l_w)
                        lm_w = l_w - lm_x;
                      if (lm_h + lm_y > l_h)
                        lm_h = l_h - lm_y;
                    }
                  else
                    memcpy (pixels, lyr_chn[user_mask_chn]->data, layer_size);
                  g_free (lyr_chn[user_mask_chn]->data);
                  /* Draw layer mask data */
1327 1328
                  IFDBG(3) g_debug ("Layer %d %d %d %d", l_x, l_y, l_w, l_h);
                  IFDBG(3) g_debug ("Mask %d %d %d %d", lm_x, lm_y, lm_w, lm_h);
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362

                  if (lyr_a[lidx]->layer_mask.def_color == 255)
                    mask_id = gimp_layer_create_mask (layer_id, GIMP_ADD_WHITE_MASK);
                  else
                    mask_id = gimp_layer_create_mask (layer_id, GIMP_ADD_BLACK_MASK);

                  IFDBG(3) g_debug ("New layer mask %d", mask_id);
                  gimp_layer_add_mask (layer_id, mask_id);
                  drawable = gimp_drawable_get (mask_id);
                  gimp_pixel_rgn_init (&pixel_rgn, drawable, 0 , 0,
                                       drawable->width, drawable->height, TRUE, FALSE);
                  gimp_pixel_rgn_set_rect (&pixel_rgn, pixels, lm_x, lm_y, lm_w, lm_h);
                  gimp_drawable_flush (drawable);
                  gimp_drawable_detach (drawable);
                  gimp_layer_set_apply_mask (layer_id,
                    ! lyr_a[lidx]->layer_mask.mask_flags.disabled);
                  g_free (pixels);
                }
            }
          for (cidx = 0; cidx < lyr_a[lidx]->num_channels; ++cidx)
            if (lyr_chn[cidx])
              g_free (lyr_chn[cidx]);
          g_free (lyr_chn);
        }
      g_free (lyr_a[lidx]);
    }
  g_free (lyr_a);

  return 0;
}

static gint
add_merged_image (const gint32  image_id,
                  PSDimage     *img_a,
1363 1364
                  FILE         *f,
                  GError      **error)
1365 1366 1367 1368
{
  PSDchannel            chn_a[MAX_CHANNELS];
  gchar                *alpha_name;
  guchar               *pixels;
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
  guint16               comp_mode;
  guint16               base_channels;
  guint16               extra_channels;
  guint16               total_channels;
  guint16              *rle_pack_len[MAX_CHANNELS];
  guint32               block_len;
  guint32               block_start;
  guint32               block_end;
  guint32               alpha_id;
  gint32                layer_size;
  gint32                layer_id = -1;
  gint32                channel_id = -1;
  gint32                active_layer;
1382
  gint16                alpha_opacity;
1383 1384 1385 1386 1387 1388
  gint                 *lyr_lst;
  gint                  cidx;                  /* Channel index */
  gint                  rowi;                  /* Row index */
  gint                  lyr_count;
  gint                  offset;
  gint                  i;
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
  gboolean              alpha_visible;
  GimpDrawable         *drawable;
  GimpPixelRgn          pixel_rgn;
  GimpImageType         image_type;
  GimpRGB               alpha_rgb;

  total_channels = img_a->channels;
  extra_channels = 0;

  if ((img_a->color_mode == PSD_BITMAP ||
       img_a->color_mode == PSD_GRAYSCALE ||
       img_a->color_mode == PSD_DUOTONE ||
       img_a->color_mode == PSD_INDEXED) &&
       total_channels > 1)
    {
      extra_channels = total_channels - 1;
    }
  else if ((img_a->color_mode == PSD_RGB ||
            img_a->color_mode == PSD_LAB) &&
            total_channels > 3)
    {
      extra_channels = total_channels - 3;
    }
  else if ((img_a->color_mode == PSD_CMYK) &&
            total_channels > 4)
    {
      extra_channels = total_channels - 4;
    }
  if (img_a->transparency && extra_channels > 0)
    extra_channels--;
  base_channels = total_channels - extra_channels;

  /* ----- Read merged image & extra channel pixel data ----- */
  if (img_a->num_layers == 0
      || extra_channels > 0)
    {
      block_start = img_a->merged_image_start;
      block_len = img_a->merged_image_len;
      block_end = block_start + block_len;
      fseek (f, block_start, SEEK_SET);

      if (fread (&comp_mode, 2, 1, f) < 1)
        {
1432
          psd_set_error (feof (f), errno, error);
1433 1434 1435 1436 1437 1438 1439
          return -1;
        }
      comp_mode = GUINT16_FROM_BE (comp_mode);

      switch (comp_mode)
        {
          case PSD_COMP_RAW:        /* Planar raw data */
1440
            IFDBG(3) g_debug ("Raw data length: %d", block_len);
1441 1442 1443 1444
            for (cidx = 0; cidx < total_channels; ++cidx)
              {
                chn_a[cidx].columns = img_a->columns;
                chn_a[cidx].rows = img_a->rows;
1445 1446 1447
                if (read_channel_data (&chn_a[cidx], img_a->bps,
                    PSD_COMP_RAW, NULL, f, error) < 1)
                  return -1;
1448 1449 1450 1451 1452 1453
              }
            break;

          case PSD_COMP_RLE:        /* Packbits */
            /* Image data is stored as packed scanlines in planar order
               with all compressed length counters stored first */
1454
            IFDBG(3) g_debug ("RLE length data: %d, RLE data block: %d",
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
                               total_channels * img_a->rows * 2,
                               block_len - (total_channels * img_a->rows * 2));
            for (cidx = 0; cidx < total_channels; ++cidx)
              {
                chn_a[cidx].columns = img_a->columns;
                chn_a[cidx].rows = img_a->rows;
                rle_pack_len[cidx] = g_malloc (img_a->rows * 2);
                for (rowi = 0; rowi < img_a->rows; ++rowi)
                  {
                    if (fread (&rle_pack_len[cidx][rowi], 2, 1, f) < 1)
                      {
1466
                        psd_set_error (feof (f), errno, error);
1467 1468 1469 1470 1471 1472
                        return -1;
                      }
                    rle_pack_len[cidx][rowi] = GUINT16_FROM_BE (rle_pack_len[cidx][rowi]);
                  }
              }

1473
            IFDBG(3) g_debug ("RLE decode - data");
1474 1475 1476
            for (cidx = 0; cidx < total_channels; ++cidx)
              {
                if (read_channel_data (&chn_a[cidx], img_a->bps,
1477 1478
                    PSD_COMP_RLE, rle_pack_len[cidx], f, error) < 1)
                  return -1;
1479 1480 1481 1482 1483 1484
                g_free (rle_pack_len[cidx]);
              }
            break;

          case PSD_COMP_ZIP:                 /* ? */
          case PSD_COMP_ZIP_PRED:
1485 1486
            g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                        _("Unsupported compression mode: %d"), comp_mode);
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
            return -1;
            break;
        }
    }

  /* ----- Draw merged image ----- */
  if (img_a->num_layers == 0)            /* Merged image - Photoshop 2 style */
    {
      image_type = get_gimp_image_type (img_a->base_type, img_a->transparency);

      layer_size = img_a->columns * img_a->rows;
      pixels = g_malloc (layer_size * base_channels);
      for (cidx = 0; cidx < base_channels; ++cidx)
        {
          for (i = 0; i < layer_size; ++i)
            {
              pixels[(i * base_channels) + cidx] = chn_a[cidx].data[i];
            }
          g_free (chn_a[cidx].data);
        }

      /* Add background layer */
      IFDBG(2) g_debug ("Draw merged image");
      layer_id = gimp_layer_new (image_id, _("Background"),
                                 img_a->columns, img_a->rows,
                                 image_type,
                                 100, GIMP_NORMAL_MODE);
      gimp_image_add_layer (image_id, layer_id, 0);
      drawable = gimp_drawable_get (layer_id);
      gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,
                           drawable->width, drawable->height, TRUE, FALSE);
      gimp_pixel_rgn_set_rect (&pixel_rgn, pixels,
                               0, 0, drawable->width, drawable->height);
      gimp_drawable_flush (drawable);
      gimp_drawable_detach (drawable);
      g_free (pixels);
    }
  else
1525 1526 1527 1528 1529 1530
    {
      /* Free merged image data for layered image */
      if (extra_channels)
        for (cidx = 0; cidx < base_channels; ++cidx)
          g_free (chn_a[cidx].data);
    }
1531 1532

  /* ----- Draw extra alpha channels ----- */
1533 1534
  if ((extra_channels                   /* Extra alpha channels */
      || img_a->transparency)           /* Transparency alpha channel */
1535 1536 1537 1538 1539 1540 1541
      && image_id > -1)
    {
      IFDBG(2) g_debug ("Add extra channels");
      pixels = g_malloc(0);

      /* Get channel resource data */
      if (img_a->transparency)
1542 1543 1544 1545 1546 1547 1548
        {
          offset = 1;
          /* Free "Transparency" channel name */
          alpha_name = g_ptr_array_index (img_a->alpha_names, 0);
          if (alpha_name)
            g_free (alpha_name);
        }
1549 1550 1551 1552
      else
        offset = 0;

      /* Draw channels */
1553
      IFDBG(2) g_debug ("Number of channels: %d", extra_channels);
1554 1555 1556 1557 1558 1559 1560 1561 1562
      for (i = 0; i < extra_channels; ++i)
        {
          /* Alpha channel name */
          alpha_name = NULL;
          alpha_visible = FALSE;
          /* Quick mask channel*/
          if (img_a->quick_mask_id)
            if (i == img_a->quick_mask_id - base_channels + offset)
              {
1563 1564 1565 1566
                /* Free "Quick Mask" channel name */
                alpha_name = g_ptr_array_index (img_a->alpha_names, i + offset);
                if (alpha_name)
                  g_free (alpha_name);
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
                alpha_name = g_strdup (GIMP_IMAGE_QUICK_MASK_NAME);
                alpha_visible = TRUE;
              }
          if (! alpha_name && img_a->alpha_names)
            if (offset < img_a->alpha_names->len
                && i + offset <= img_a->alpha_names->len)
              alpha_name = g_ptr_array_index (img_a->alpha_names, i + offset);
          if (! alpha_name)
            alpha_name = g_strdup (_("Extra"));

          if (offset < img_a->alpha_id_count &&
              offset + i <= img_a->alpha_id_count)
            alpha_id = img_a->alpha_id[i + offset];
          else
            alpha_id = 0;
          if (offset < img_a->alpha_display_count &&
              i + offset <= img_a->alpha_display_count)
            {
              alpha_rgb = img_a->alpha_display_info[i + offset]->gimp_color;
              alpha_opacity = img_a->alpha_display_info[i + offset]->opacity;
            }
          else
            {
1590
              gimp_rgba_set (&alpha_rgb, 1.0, 0.0, 0.0, 1.0);
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
              alpha_opacity = 50;
            }

          cidx = base_channels + i;
          pixels = g_realloc (pixels, chn_a[cidx].columns * chn_a[cidx].rows);
          memcpy (pixels, chn_a[cidx].data, chn_a[cidx].columns * chn_a[cidx].rows);
          channel_id = gimp_channel_new (image_id, alpha_name,
                                         chn_a[cidx].columns, chn_a[cidx].rows,
                                         alpha_opacity, &alpha_rgb);
          gimp_image_add_channel (image_id, channel_id, 0);
1601
          g_free (alpha_name);
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
          drawable = gimp_drawable_get (channel_id);
          if (alpha_id)
            gimp_drawable_set_tattoo (drawable->drawable_id, alpha_id);
          gimp_drawable_set_visible (drawable->drawable_id, alpha_visible);
          gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,
                                drawable->width, drawable->height,
                                TRUE, FALSE);
          gimp_pixel_rgn_set_rect (&pixel_rgn, pixels,
                                   0, 0, drawable->width,
                                   drawable->height);
          gimp_drawable_flush (drawable);
          gimp_drawable_detach (drawable);
          g_free (chn_a[cidx].data);
        }

      g_free (pixels);
      if (img_a->alpha_names)
        g_ptr_array_free (img_a->alpha_names, TRUE);

      if (img_a->alpha_id)
        g_free (img_a->alpha_id);

      if (img_a->alpha_display_info)
        {
          for (cidx = 0; cidx < img_a->alpha_display_count; ++cidx)
            g_free (img_a->alpha_display_info[cidx]);
          g_free (img_a->alpha_display_info);
        }
    }

  /* Set active layer */
  lyr_lst = gimp_image_get_layers (image_id, &lyr_count);
  if (img_a->layer_state + 1 > lyr_count ||
      img_a->layer_state + 1 < 0)
    img_a->layer_state = 0;
  active_layer = lyr_lst[lyr_count - img_a->layer_state - 1];
  gimp_image_set_active_layer (image_id, active_layer);
  g_free (lyr_lst);

  /* FIXME gimp image tattoo state */

  return 0;
}


/* Local utility functions */
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674
static gchar *
get_psd_color_mode_name (PSDColorMode mode)
{
  static gchar *psd_color_mode_names[] =
  {
    "BITMAP",
    "GRAYSCALE",
    "INDEXED",
    "RGB",
    "CMYK",
    "UNKNOWN (5)",
    "UNKNOWN (6)",
    "MULTICHANNEL",
    "DUOTONE",
    "LAB"
  };

  static gchar *err_name = NULL;
  if (mode >= PSD_BITMAP && mode <= PSD_LAB)
    {
      return psd_color_mode_names[mode];
    }
  g_free (err_name);

  err_name = g_strdup_printf ("UNKNOWN (%d)", mode);
  return err_name;
}
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723

static void
psd_to_gimp_color_map (guchar *map256)
{
  guchar *tmpmap;
  gint    i;

  tmpmap = g_malloc (3 * 256);

  for (i = 0; i < 256; ++i)
    {
      tmpmap[i*3  ] = map256[i];
      tmpmap[i*3+1] = map256[i+256];
      tmpmap[i*3+2] = map256[i+512];
    }

  memcpy (map256, tmpmap, 3 * 256);
  g_free (tmpmap);
}

static GimpImageType
get_gimp_image_type (const GimpImageBaseType image_base_type,
                     const gboolean          alpha)
{
  GimpImageType image_type;

  switch (image_base_type)
    {
      case GIMP_GRAY:
        image_type = (alpha) ? GIMP_GRAYA_IMAGE : GIMP_GRAY_IMAGE;
        break;

      case GIMP_INDEXED:
        image_type = (alpha) ? GIMP_INDEXEDA_IMAGE : GIMP_INDEXED_IMAGE;
        break;

      case GIMP_RGB:
        image_type = (alpha) ? GIMP_RGBA_IMAGE : GIMP_RGB_IMAGE;
        break;

      default:
        image_type = -1;
        break;
    }

  return image_type;
}

static gint
1724 1725 1726 1727 1728 1729
read_channel_data (PSDchannel     *channel,
                   const guint16   bps,
                   const guint16   compression,
                   const guint16  *rle_pack_len,
                   FILE           *f,
                   GError        **error)
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
{
  gchar    *raw_data;
  gchar    *src;
  gchar    *dst;
  guint32   readline_len;
  gint      i;

  if (bps == 1)
    readline_len = ((channel->columns + 7) >> 3);
  else
    readline_len = (channel->columns * bps >> 3);

  IFDBG(3) g_debug ("raw data size %d x %d = %d", readline_len,
1743
                    channel->rows, readline_len * channel->rows);
1744 1745 1746 1747 1748
  raw_data = g_malloc (readline_len * channel->rows);
  switch (compression)
    {
      case PSD_COMP_RAW:
        if (fread (raw_data, readline_len, channel->rows, f) < 1)
1749 1750
          {
            psd_set_error (feof (f), errno, error);
1751
            return -1;
1752
          }