2
* PSD Plugin version 3.0.14
3
* This GIMP plug-in is designed to load Adobe Photoshop(tm) files (.PSD)
5
* Adam D. Moss <adam@gimp.org> <adam@foxbox.org>
7
* If this plug-in fails to load a file which you think it should,
8
* please file a Bugzilla bug describing what seemed to go wrong,
9
* and anything you know about the image you tried to load. Attach a
10
* problematic PSD file to the bug report.
12
* Copyright (C) 1997-2004 Adam D. Moss
13
* Copyright (C) 1996 Torsten Martinsen
15
* This program is free software; you can redistribute it and/or modify
16
* it under the terms of the GNU General Public License as published by
17
* the Free Software Foundation; either version 2 of the License, or
18
* (at your option) any later version.
20
* This program is distributed in the hope that it will be useful,
21
* but WITHOUT ANY WARRANTY; without even the implied warranty of
22
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
* GNU General Public License for more details.
25
* You should have received a copy of the GNU General Public License
26
* along with this program; if not, write to the Free Software
27
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31
* Adobe and Adobe Photoshop are trademarks of Adobe Systems
32
* Incorporated that may be registered in certain jurisdictions.
38
* 2004-02-12 / v3.0.14 / Adam D. Moss
39
* Fix a twisted utf8-obsessive bug diagnosed by
40
* Piotr Krysiuk <KrysiukP@prokom.pl>
42
* 2004-01-06 / v3.0.13 / Adam D. Moss
43
* Disable one of the PanoTools fixes by default, since it causes
44
* regressions in some ordinary PSD file loading.
46
* 2004-01-06 / v3.0.12 / Adam D. Moss
47
* Try to avoid 0-sized drawables (including channels) in all
48
* circumstances under GIMP 2.
50
* 2004-01-01 / v3.0.11 / Daniel Rogers <dsrogers@phaseveloctiy.org>
51
* GIMP crashes on 0x0 layers, so we skip them.
53
* 2003-11-27 / v3.0.10 / Adam D. Moss
54
* GIMP 1.3/2.0 needs its layer/channel names to be UTF8 or it
55
* fails wackily, so convert the strings from the PSD file to
56
* UTF8 instead of using them raw.
58
* 2003-10-05 / v3.0.9 / Morten Eriksen
59
* Fixed memory corruption bug: too little memory was allocated
60
* for the bitmap image buffer if (imgwidth % 8 != 0) for
61
* monocolor PSD images.
63
* 2003-08-31 / v3.0.8 / applied (modified) patch from Andy Wallis
64
* Fix for handling of layer masks. See bug #68538.
66
* 2003-06-16 / v3.0.7 / Adam D. Moss
67
* Avoid memory corruption when things get shot to hell in the
68
* image unpacking phase. Major version bumped to distinguish
69
* GIMP 1.3 development thread.
71
* 2000-08-23 / v2.0.6 / Adam D. Moss
72
* Eliminate more debugging output (don't people have more
73
* substantial problems to report? I'm poised at my keyboard).
75
* 1999-11-14 / v2.0.5 / Adam D. Moss
76
* Applied patch by Andy Hefner to load 1-bit images.
78
* 1999-08-13 / v2.0.4 / Adam D. Moss
79
* Allowed NULL layer names again, whee. (Also fixed the time machine.)
81
* 1999-08-20 / v2.0.3 / Adam D. Moss
82
* Ensure that NULL name does not get passed to gimp_layer_new(),
83
* or it will fail to create the layer and cause problems down
84
* the line (only since April 1999).
86
* 1999-01-18 / v2.0.2 / Adam D. Moss
87
* Better guess at how PSD files store Guide position precision.
89
* 1999-01-10 / v2.0.1 / Adam D. Moss
90
* Greatly reduced memory requirements for layered image loading -
91
* we now do just-in-time channel unpacking. Some little
94
* 1998-09-04 / v2.0.0 / Adam D. Moss
95
* Now recognises and loads the new Guides extensions written
96
* by Photoshop 4 and 5.
98
* 1998-07-31 / v1.9.9.9f / Adam D. Moss
99
* Use GIMP_OVERLAY_MODE if available.
101
* 1998-07-31 / v1.9.9.9e / Adam D. Moss
102
* Worked around some buggy PSD savers (suspect PS4 on Mac) - ugh.
103
* Fixed a bug when loading layer masks of certain dimensions.
105
* 1998-05-04 / v1.9.9.9b / Adam D. Moss
106
* Changed the Pascal-style string-reading stuff. That fixed
107
* some file-padding problems. Made all debugging output
108
* compile-time optional (please leave it enabled for now).
109
* Reduced memory requirements; still much room for improvement.
111
* 1998-04-28 / v1.9.9.9 / Adam D. Moss
112
* Fixed the correct channel interlacing of 'raw' flat images.
113
* Thanks to Christian Kirsch and Jay Cox for spotting this.
114
* Changed some of the I/O routines.
116
* 1998-04-26 / v1.9.9.8 / Adam D. Moss
117
* Implemented Aux-channels for layered files. Got rid
118
* of <endian.h> nonsense. Improved Layer Mask padding.
119
* Enforced num_layers/num_channels limit checks.
121
* 1998-04-23 / v1.9.9.5 / Adam D. Moss
122
* Got Layer Masks working, got Aux-channels working
123
* for unlayered files, fixed 'raw' channel loading, fixed
124
* some other mini-bugs, slightly better progress meters.
125
* Thanks to everyone who is helping with the testing!
127
* 1998-04-21 / v1.9.9.1 / Adam D. Moss
128
* A little cleanup. Implemented Layer Masks but disabled
129
* them again - PS masks can be a different size to their
130
* owning layer, unlike those in GIMP.
132
* 1998-04-19 / v1.9.9.0 / Adam D. Moss
135
* 1997-03-13 / v1.9.0 / Adam D. Moss
136
* Layers, channels and masks, oh my.
137
* + Bugfixes & rearchitecturing.
139
* 1997-01-30 / v1.0.12 / Torsten Martinsen
140
* Flat PSD image loading.
146
* Crush 16bpp channels *
148
* * I don't think these should be done lossily -- wait for
149
* GIMP to be able to support them natively.
153
* File saving (someone has an alpha plugin for this)
159
* Sometimes creates a superfluous aux channel? Harmless.
164
/* *** USER DEFINES *** */
166
/* set to TRUE if you want debugging, FALSE otherwise */
167
#define PSD_DEBUG FALSE
169
/* the max number of channels that this plugin should let a layer have */
170
#define MAX_CHANNELS 30
172
/* set to TRUE to allow a fix for transparency in PSD files
173
generated by PanoTools that unfortunately causes regressions
174
in some other ordinary files saved by Photoshop. */
175
#define PANOTOOLS_FIX FALSE
177
/* *** END OF USER DEFINES *** */
181
#define IFDBG if (PSD_DEBUG)
195
#include <libgimp/gimp.h>
197
#include "libgimp/stdplugins-intl.h"
215
typedef struct PsdChannel
221
guint32 compressedsize;
223
fpos_t fpos; /* Remember where the data is in the file, so we can
226
/* We can't just assume that the channel's width and height are the
227
* same as those of the layer that owns the channel, since this
228
* channel may be a layer mask, which Photoshop allows to have a
229
* different size from the layer which it applies to.
236
typedef struct PsdGuide
238
gboolean horizontal; /* else vertical */
242
typedef struct PsdLayer
256
gboolean protecttrans;
269
typedef gint32 Fixed;
288
typedef struct PsdImage
293
gboolean absolute_alpha;
300
guint num_aux_channels;
301
PSDchannel aux_channel[MAX_CHANNELS];
308
guint active_layer_num;
310
guint resolution_is_set;
311
PSDresolution resolution;
314
/* Declare some local functions.
316
static void query (void);
317
static void run (const gchar *name,
319
const GimpParam *param,
321
GimpParam **return_vals);
323
static GimpImageType psd_type_to_gimp_type (psd_imagetype psdtype);
324
static GimpImageBaseType psd_type_to_gimp_base_type (psd_imagetype psdtype);
325
static GimpLayerModeEffects psd_lmode_to_gimp_lmode (gchar modekey[4]);
326
static GimpUnit psd_unit_to_gimp_unit (gint psdunit);
328
static gint32 load_image (const gchar *filename);
332
/* Various local variables...
334
GimpPlugInInfo PLUG_IN_INFO =
336
NULL, /* init_proc */
337
NULL, /* quit_proc */
338
query, /* query_proc */
343
static PSDimage psd_image;
364
static gchar * modename[] =
380
static const gchar *prog_name = "PSD";
383
static void unpack_pb_channel(FILE *fd, guchar *dst, gint32 unpackedlen,
385
static void decode(long clen, long uclen, guchar *src, guchar *dst, int step);
386
static void packbitsdecode(long *clenp, long uclen,
387
guchar *src, guchar *dst, int step);
388
static void cmyk2rgb(guchar *src, guchar *destp,
389
long width, long height, int alpha);
390
static void cmykp2rgb(guchar *src, guchar *destp,
391
long width, long height, int alpha);
392
static void bitmap2gray(guchar *src,guchar *dest,long w,long h);
393
static guchar getguchar(FILE *fd, gchar *why);
394
static gshort getgshort(FILE *fd, gchar *why);
395
static glong getglong(FILE *fd, gchar *why);
396
static void xfread(FILE *fd, void *buf, long len, gchar *why);
397
static void xfread_interlaced(FILE *fd, guchar *buf, long len, gchar *why,
399
static void read_whole_file(FILE *fd);
400
static void reshuffle_cmap(guchar *map256);
401
static gchar* getpascalstring(FILE *fd, gchar *why);
402
static gchar* getstring(size_t n, FILE * fd, gchar *why);
403
static void throwchunk(size_t n, FILE * fd, gchar *why);
404
static void dumpchunk(size_t n, FILE * fd, gchar *why);
405
static void seek_to_and_unpack_pixeldata(FILE* fd, gint layeri, gint channeli);
406
static void validate_aux_channel_name(gint aux_index);
415
static GimpParamDef load_args[] =
417
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
418
{ GIMP_PDB_STRING, "filename", "The name of the file to load" },
419
{ GIMP_PDB_STRING, "raw_filename", "The name of the file to load" }
421
static GimpParamDef load_return_vals[] =
423
{ GIMP_PDB_IMAGE, "image", "Output image" }
426
gimp_install_procedure ("file_psd_load",
427
"loads files of the Photoshop(tm) PSD file format",
428
"This filter loads files of Adobe Photoshop(tm) native PSD format. These files may be of any image type supported by GIMP, with or without layers, layer masks, aux channels and guides.",
429
"Adam D. Moss & Torsten Martinsen",
430
"Adam D. Moss & Torsten Martinsen",
435
G_N_ELEMENTS (load_args),
436
G_N_ELEMENTS (load_return_vals),
437
load_args, load_return_vals);
439
gimp_register_file_handler_mime ("file_psd_load", "image/x-psd");
440
gimp_register_magic_load_handler ("file_psd_load",
448
run (const gchar *name,
450
const GimpParam *param,
452
GimpParam **return_vals)
454
static GimpParam values[2];
455
GimpRunMode run_mode;
456
/* GimpPDBStatusType status = GIMP_PDB_SUCCESS;*/
459
run_mode = param[0].data.d_int32;
462
*return_vals = values;
463
values[0].type = GIMP_PDB_STATUS;
464
values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
466
if (strcmp (name, "file_psd_load") == 0)
468
image_ID = load_image (param[1].data.d_string);
473
values[0].data.d_status = GIMP_PDB_SUCCESS;
474
values[1].type = GIMP_PDB_IMAGE;
475
values[1].data.d_image = image_ID;
479
values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
486
sanitise_string(char *old_name)
489
char *rtn = gimp_any_to_utf8(old_name, -1,
490
_("Invalid UTF-8 string in PSD file"));
499
psd_type_to_gimp_type (psd_imagetype psdtype)
503
case PSD_RGBA_IMAGE: return(GIMP_RGBA_IMAGE);
504
case PSD_RGB_IMAGE: return(GIMP_RGB_IMAGE);
505
case PSD_GRAYA_IMAGE: return(GIMP_GRAYA_IMAGE);
506
case PSD_GRAY_IMAGE: return(GIMP_GRAY_IMAGE);
507
case PSD_INDEXEDA_IMAGE: return(GIMP_INDEXEDA_IMAGE);
508
case PSD_INDEXED_IMAGE: return(GIMP_INDEXED_IMAGE);
509
case PSD_BITMAP_IMAGE: return(GIMP_GRAY_IMAGE);
510
default: return(GIMP_RGB_IMAGE);
514
static GimpLayerModeEffects
515
psd_lmode_to_gimp_lmode (gchar modekey[4])
517
if (strncmp(modekey, "norm", 4)==0) return(GIMP_NORMAL_MODE);
518
if (strncmp(modekey, "dark", 4)==0) return(GIMP_DARKEN_ONLY_MODE);
519
if (strncmp(modekey, "lite", 4)==0) return(GIMP_LIGHTEN_ONLY_MODE);
520
if (strncmp(modekey, "hue ", 4)==0) return(GIMP_HUE_MODE);
521
if (strncmp(modekey, "sat ", 4)==0) return(GIMP_SATURATION_MODE);
522
if (strncmp(modekey, "colr", 4)==0) return(GIMP_COLOR_MODE);
523
if (strncmp(modekey, "mul ", 4)==0) return(GIMP_MULTIPLY_MODE);
524
if (strncmp(modekey, "scrn", 4)==0) return(GIMP_SCREEN_MODE);
525
if (strncmp(modekey, "diss", 4)==0) return(GIMP_DISSOLVE_MODE);
526
if (strncmp(modekey, "diff", 4)==0) return(GIMP_DIFFERENCE_MODE);
527
if (strncmp(modekey, "lum ", 4)==0) return(GIMP_VALUE_MODE);
528
if (strncmp(modekey, "hLit", 4)==0) return(GIMP_HARDLIGHT_MODE);
529
if (strncmp(modekey, "sLit", 4)==0) return(GIMP_SOFTLIGHT_MODE);
530
if (strncmp(modekey, "over", 4)==0) return(GIMP_OVERLAY_MODE);
532
printf("PSD: Warning - UNKNOWN layer-blend mode, reverting to 'normal'\n");
534
return(GIMP_NORMAL_MODE);
537
static GimpImageBaseType
538
psd_type_to_gimp_base_type (psd_imagetype psdtype)
543
case PSD_RGB_IMAGE: return(GIMP_RGB);
544
case PSD_BITMAP_IMAGE:
545
case PSD_GRAYA_IMAGE:
546
case PSD_GRAY_IMAGE: return(GIMP_GRAY);
547
case PSD_INDEXEDA_IMAGE:
548
case PSD_INDEXED_IMAGE: return(GIMP_INDEXED);
550
g_message ("Error: Can't convert PSD imagetype to GIMP imagetype");
557
psd_unit_to_gimp_unit (int psdunit)
562
return GIMP_UNIT_INCH;
563
case 2: /* this means cm to PS, but MM is as close as we have */
566
return GIMP_UNIT_POINT;
568
return GIMP_UNIT_PICA;
569
case 5: /* 5 == Columns, but what the heck is a column? */
571
IFDBG printf("Warning: unable to convert psd unit %d to gimp unit\n", psdunit);
572
return GIMP_UNIT_PIXEL;
576
static GimpImageBaseType
577
psd_mode_to_gimp_base_type (gushort psdtype)
581
case 1: return GIMP_GRAY;
582
case 2: return GIMP_INDEXED;
583
case 3: return GIMP_RGB;
585
g_message ("Error: Can't convert PSD mode to GIMP base imagetype");
592
reshuffle_cmap (guchar *map256)
597
tmpmap = g_malloc(3 * 256);
599
for (i = 0; i < 256; i++)
601
tmpmap[i*3 ] = map256[i];
602
tmpmap[i*3+1] = map256[i+256];
603
tmpmap[i*3+2] = map256[i+512];
606
memcpy(map256, tmpmap, 3 * 256);
611
dispatch_resID(guint ID, FILE *fd, guint32 *offset, guint32 Size)
613
if ( (ID < 0x0bb6) && (ID >0x07d0) )
615
IFDBG printf ("\t\tPath data is irrelevant to GIMP at this time.\n");
616
throwchunk(Size, fd, "dispatch_res path throw");
624
gint32 remaining = Size;
626
IFDBG printf ("\t\tALPHA CHANNEL NAMES:\n");
633
slen = getguchar (fd, "alpha channel name length");
637
/* Check for (Mac?) Photoshop (4?) file-writing bug */
638
if (slen > remaining)
640
IFDBG {printf("\nYay, a file bug. "
641
"Yuck. Photoshop 4/Mac? "
642
"I'll work around you.\n");fflush(stdout);}
648
guint32 alpha_name_len;
649
gchar* sname = getstring(slen, fd, "alpha channel name");
651
alpha_name_len = strlen(sname);
653
(*offset) += alpha_name_len;
654
remaining -= alpha_name_len;
656
sname = sanitise_string(sname);
658
psd_image.aux_channel[psd_image.num_aux_channels].name =
661
IFDBG printf("\t\t\tname: \"%s\"\n",
662
psd_image.aux_channel[psd_image.num_aux_channels].name);
666
psd_image.aux_channel[psd_image.num_aux_channels].name =
669
{printf("\t\t\tNull channel name %d.\n",
670
psd_image.num_aux_channels);fflush(stdout);}
673
psd_image.num_aux_channels++;
675
if (psd_image.num_aux_channels > MAX_CHANNELS)
677
printf("\nPSD: Sorry - this image has too many "
678
"aux channels. Tell Adam!\n");
682
while (remaining > 0);
688
dumpchunk(remaining, fd, "alphaname padding 0 throw");
690
throwchunk(remaining, fd, "alphaname padding 0 throw");
691
(*offset) += remaining;
697
IFDBG printf("\t\tDISPLAYINFO STRUCTURE: unhandled\n");
698
throwchunk(Size, fd, "dispatch_res");
701
case 0x03f0: /* FIXME: untested */
703
psd_image.caption = getpascalstring(fd, "caption string");
706
if (psd_image.caption)
708
IFDBG printf("\t\t\tcontent: \"%s\"\n",psd_image.caption);
709
(*offset) += strlen(psd_image.caption);
714
IFDBG printf("\t\tBACKGROUND COLOR: unhandled\n");
715
throwchunk(Size, fd, "dispatch_res");
719
IFDBG printf("\t\tGREY/MULTICHANNEL HALFTONING INFO: unhandled\n");
720
throwchunk(Size, fd, "dispatch_res");
724
IFDBG printf("\t\tCOLOUR HALFTONING INFO: unhandled\n");
725
throwchunk(Size, fd, "dispatch_res");
729
IFDBG printf("\t\tDUOTONE HALFTONING INFO: unhandled\n");
730
throwchunk(Size, fd, "dispatch_res");
734
IFDBG printf("\t\tGREYSCALE/MULTICHANNEL TRANSFER FUNCTION: unhandled\n");
735
throwchunk(Size, fd, "dispatch_res");
739
IFDBG printf("\t\tCOLOUR TRANSFER FUNCTION: unhandled\n");
740
throwchunk(Size, fd, "dispatch_res");
744
IFDBG printf("\t\tDUOTONE TRANSFER FUNCTION: unhandled\n");
745
throwchunk(Size, fd, "dispatch_res");
749
IFDBG printf("\t\tDUOTONE IMAGE INFO: unhandled\n");
750
throwchunk(Size, fd, "dispatch_res");
754
IFDBG printf("\t\tEFFECTIVE BLACK/WHITE VALUES: unhandled\n");
755
throwchunk(Size, fd, "dispatch_res");
759
IFDBG printf("\t\tQUICK MASK INFO: unhandled\n");
760
throwchunk(Size, fd, "dispatch_res");
765
IFDBG printf("\t\tLAYER STATE INFO:\n");
766
psd_image.active_layer_num = getgshort(fd, "ID target_layer_num");
768
IFDBG printf("\t\t\ttarget: %d\n",(gint)psd_image.active_layer_num);
773
IFDBG printf("\t\tLAYER GROUP INFO: unhandled\n");
774
IFDBG printf("\t\t\t(Inferred number of layers: %d)\n",(gint)(Size/2));
775
throwchunk(Size, fd, "dispatch_res");
779
IFDBG printf ("\t\tIMAGE MODE FOR RAW FORMAT: unhandled\n");
780
throwchunk(Size, fd, "dispatch_res");
785
gint32 remaining = Size;
787
IFDBG printf ("\t\tGUIDE INFORMATION:\n");
791
gshort magic1, magic2, magic3, magic4, magic5, magic6;
795
magic1 = getgshort(fd, "guide"); (*offset) += 2;
796
magic2 = getgshort(fd, "guide"); (*offset) += 2;
797
magic3 = getgshort(fd, "guide"); (*offset) += 2;
798
magic4 = getgshort(fd, "guide"); (*offset) += 2;
799
magic5 = getgshort(fd, "guide"); (*offset) += 2;
800
magic6 = getgshort(fd, "guide"); (*offset) += 2;
803
IFDBG printf("\t\t\tMagic: %d %d %d %d %d %d\n",
804
magic1, magic2, magic3, magic4, magic5, magic6);
805
IFDBG printf("\t\t\tMagic: 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x\n",
806
magic1, magic2, magic3, magic4, magic5, magic6);
808
num_guides = getglong(fd, "guide");
809
(*offset) += 4; remaining -= 4;
811
if (remaining != num_guides*5)
813
IFDBG printf ("** FUNNY AMOUNT OF GUIDE DATA (%d)\n",
815
goto funny_amount_of_guide_data;
818
IFDBG printf("\t\t\tNumber of guides is %ld\n", num_guides);
819
psd_image.num_guides = num_guides;
820
psd_image.guides = g_new(PSDguide, num_guides);
821
guide = psd_image.guides;
823
for (i = 0; i < num_guides; i++, guide++)
825
guide->position = getglong(fd, "guide");
827
guide->horizontal = (1 == getguchar(fd, "guide"));
828
(*offset) += 5; remaining -= 5;
830
if (guide->horizontal)
833
RINT((double)(guide->position * (magic4>>8))
834
/(double)(magic4&255));
839
RINT((double)(guide->position * (magic6>>8))
840
/(double)(magic6&255));
843
IFDBG printf("\t\t\tGuide %d at %d, %s\n", i+1,
845
guide->horizontal ? "horizontal" : "vertical");
849
funny_amount_of_guide_data:
855
printf ("** GUIDE INFORMATION DROSS: ");
856
dumpchunk(remaining, fd, "dispatch_res");
860
throwchunk(remaining, fd, "dispatch_res");
863
(*offset) += remaining;
869
IFDBG printf ("\t\tResolution Info:\n");
870
psd_image.resolution_is_set = 1;
872
psd_image.resolution.hRes = getglong(fd, "hRes");
873
psd_image.resolution.hRes_unit = getgshort(fd, "hRes_unit");
874
psd_image.resolution.widthUnit = getgshort(fd, "WidthUnit");
875
psd_image.resolution.vRes = getglong(fd, "vRes");
876
psd_image.resolution.vRes_unit = getgshort(fd, "vRes_unit");
877
psd_image.resolution.heightUnit = getgshort(fd, "HeightUnit");
879
IFDBG printf("\t\t\tres = %f, %f\n",
880
psd_image.resolution.hRes / 65536.0,
881
psd_image.resolution.vRes / 65536.0);
893
IFDBG printf ("\t\t<Field is irrelevant to GIMP at this time.>\n");
894
throwchunk(Size, fd, "dispatch_res");
900
IFDBG printf ("\t\t<Obsolete Photoshop 2.0 field.>\n");
901
throwchunk(Size, fd, "dispatch_res");
908
IFDBG printf ("\t\t<Obsolete field.>\n");
909
throwchunk(Size, fd, "dispatch_res");
916
printf ("\t\t<Undocumented field.>\n");
917
dumpchunk(Size, fd, "dispatch_res");
920
throwchunk(Size, fd, "dispatch_res");
928
do_layer_record(FILE *fd, guint32 *offset, gint layernum)
931
gint32 top, left, bottom, right;
932
guint32 extradatasize, layermaskdatasize, layerrangesdatasize;
938
IFDBG printf("\t\t\tLAYER RECORD (layer %d)\n", (int)layernum);
940
layer = psd_image.layer + layernum;
943
top = getglong (fd, "layer top");
945
left = getglong (fd, "layer left");
947
bottom = getglong (fd, "layer bottom");
949
right = getglong (fd, "layer right");
954
layer->width = right - left;
955
layer->height = bottom - top;
957
IFDBG printf("\t\t\t\tLayer extents: (%d,%d) -> (%d,%d)\n",
958
left,top,right,bottom);
960
layer->num_channels = getgshort (fd, "layer num_channels");
963
IFDBG printf("\t\t\t\tNumber of channels: %d\n",
964
(int)layer->num_channels);
966
if (layer->num_channels)
968
layer->channel = g_new(PSDchannel, layer->num_channels);
970
for (i = 0; i < layer->num_channels; i++)
972
PSDchannel *channel = layer->channel + i;
975
IFDBG printf("\t\t\t\tCHANNEL LENGTH INFO (%d)\n", i);
977
channel->type = getgshort(fd, "channel id");
979
IFDBG printf("\t\t\t\t\tChannel TYPE: %d\n", channel->type);
981
channel->compressedsize = getglong(fd, "channeldatalength");
983
IFDBG printf("\t\t\t\t\tChannel Data Length: %d\n",
984
channel->compressedsize);
987
IFDBG printf("\t\t\t\tOo-er, layer has no channels. Hmm.\n");
990
xfread(fd, sig, 4, "layer blend sig");
993
if (strncmp(sig, "8BIM", 4)!=0)
995
IFDBG printf("\t\t\t(layer blend signature '%c%c%c%c' is incorrect: quitting)\n", sig[0], sig[1], sig[2], sig[3]);
996
g_message ("Error: layer blend signature is incorrect. :-(");
1000
xfread(fd, layer->blendkey, 4, "layer blend key");
1003
IFDBG printf("\t\t\t\tBlend type: PSD(\"%c%c%c%c\") = GIMP(%d)\n",
1008
psd_lmode_to_gimp_lmode(layer->blendkey));
1010
layer->opacity = getguchar(fd, "layer opacity");
1013
IFDBG printf("\t\t\t\tLayer Opacity: %d\n", layer->opacity);
1015
layer->clipping = getguchar(fd, "layer clipping");
1018
IFDBG printf("\t\t\t\tLayer Clipping: %d (%s)\n",
1020
layer->clipping == 0 ? "base" : "non-base");
1022
flags = getguchar(fd, "layer flags");
1025
IFDBG printf("\t\t\t\tLayer Flags: %d (%s, %s)\n", flags,
1026
flags&1?"preserve transparency":"don't preserve transparency",
1027
flags&2?"visible":"not visible");
1029
layer->protecttrans = (flags & 1) ? TRUE : FALSE;
1030
layer->visible = (flags & 2) ? FALSE : TRUE;
1032
getguchar(fd, "layer record filler");
1035
extradatasize = getglong(fd, "layer extra data size");
1037
IFDBG printf("\t\t\t\tEXTRA DATA SIZE: %d\n",extradatasize);
1039
/* FIXME: should do something with this data */
1040
/*throwchunk(extradatasize, fd, "layer extradata throw");
1041
(*offset) += extradatasize;*/
1043
totaloff = (*offset) + extradatasize;
1046
layermaskdatasize = getglong(fd, "layer mask data size");
1048
IFDBG printf("\t\t\t\t\tLAYER MASK DATA SIZE: %d\n", layermaskdatasize);
1050
if (layermaskdatasize)
1052
top = getglong(fd, "lmask top");
1054
left = getglong(fd, "lmask left");
1056
bottom = getglong(fd, "lmask bottom");
1058
right = getglong(fd, "lmask right");
1063
layer->lm_width = right - left;
1064
layer->lm_height = bottom - top;
1066
getglong(fd, "lmask data throw");
1069
/* throwchunk(layermaskdatasize, fd, "layer mask data throw");
1070
(*offset) += layermaskdatasize;*/
1073
layerrangesdatasize = getglong(fd, "layer ranges data size");
1075
IFDBG printf("\t\t\t\t\t\tLAYER RANGES DATA SIZE: %d\n",layermaskdatasize);
1077
if (layerrangesdatasize)
1079
throwchunk(layerrangesdatasize, fd, "layer ranges data throw");
1080
(*offset) += layerrangesdatasize;
1083
layer->name = getpascalstring(fd, "layer name");
1088
(*offset) += strlen(layer->name);
1089
IFDBG printf("\t\t\t\t\t\tLAYER NAME: '%s'\n", layer->name);
1090
layer->name = sanitise_string(layer->name);
1094
IFDBG printf("\t\t\t\t\t\tNULL LAYER NAME\n");
1096
/* If no layermask data - set offset and size from layer data */
1097
if (! layermaskdatasize)
1100
fprintf(stderr, "Setting layer mask data layer\n");
1102
psd_image.layer[layernum].lm_x = psd_image.layer[layernum].x;
1103
psd_image.layer[layernum].lm_y = psd_image.layer[layernum].y;
1104
psd_image.layer[layernum].lm_width = psd_image.layer[layernum].width;
1105
psd_image.layer[layernum].lm_height = psd_image.layer[layernum].height;
1108
if (totaloff-(*offset) > 0)
1112
printf("Warning: layer record dross: ");
1113
dumpchunk(totaloff-(*offset), fd, "layer record dross throw");
1117
throwchunk(totaloff-(*offset), fd, "layer record dross throw");
1119
(*offset) = totaloff;
1124
do_layer_struct(FILE *fd, guint32 *offset)
1128
IFDBG printf("\t\tLAYER STRUCTURE SECTION\n");
1130
psd_image.num_layers = getgshort(fd, "layer struct numlayers");
1133
IFDBG printf("\t\t\tCanonical number of layers: %d%s\n",
1134
psd_image.num_layers>0?
1135
(int)psd_image.num_layers:abs(psd_image.num_layers),
1136
psd_image.num_layers>0?"":" (absolute/alpha)");
1138
if (psd_image.num_layers < 0)
1140
psd_image.num_layers = -psd_image.num_layers;
1141
psd_image.absolute_alpha = TRUE;
1145
psd_image.absolute_alpha = FALSE;
1148
psd_image.layer = g_new(PSDlayer, psd_image.num_layers);
1150
for (i = 0; i < psd_image.num_layers; i++)
1152
do_layer_record(fd, offset, i);
1157
do_layer_pixeldata(FILE *fd, guint32 *offset)
1159
gint layeri, channeli;
1161
for (layeri = 0; layeri < psd_image.num_layers; layeri++)
1163
PSDlayer *layer = psd_image.layer + layeri;
1165
for (channeli = 0; channeli < layer->num_channels; channeli++)
1167
PSDchannel *channel = layer->channel + channeli;
1169
if (channel->type == -2)
1171
channel->width = layer->lm_width;
1172
channel->height = layer->lm_height;
1176
channel->width = layer->width;
1177
channel->height = layer->height;
1180
fgetpos(fd, &channel->fpos);
1182
throwchunk(channel->compressedsize, fd, "channel data skip");
1183
(*offset) += channel->compressedsize;
1189
seek_to_and_unpack_pixeldata(FILE* fd, gint layeri, gint channeli)
1195
PSDchannel *channel = &psd_image.layer[layeri].channel[channeli];
1197
fsetpos(fd, &channel->fpos);
1199
compression = getgshort(fd, "layer channel compression type");
1202
width = channel->width;
1203
height = channel->height;
1207
printf("\t\t\tLayer (%d) Channel (%d:%d) Compression: %d (%s)\n",
1212
compression==0?"raw":(compression==1?"RLE":"*UNKNOWN!*"));
1217
channel->data = g_malloc (width * height);
1219
tmpline = g_malloc(width + 1);
1221
switch (compression)
1223
case 0: /* raw data */
1227
for (linei = 0; linei < height; linei++)
1229
xfread(fd, channel->data + linei * width, width,
1230
"raw channel line");
1235
/* Pad raw data to multiple of 2? */
1236
if ((height * width) & 1)
1238
getguchar(fd, "raw channel padding");
1244
case 1: /* RLE, one row at a time, padded to an even width */
1249
/* we throw this away because in theory we can trust the
1250
data to unpack to the right length... hmm... */
1251
throwchunk(height * 2, fd, "widthlist");
1252
offset += height * 2;
1256
/*IFDBG {printf("\nHere comes the guitar solo...\n");
1259
for (linei=0; linei<height; linei++)
1261
/*printf(" %d ", *offset);*/
1262
unpack_pb_channel(fd, tmpline,
1266
memcpy(channel->data + linei * width, tmpline, width);
1269
IFDBG {printf("\t\t\t\t\tActual compressed size was %d bytes\n"
1270
, offset-blockread);fflush(stdout);}
1273
default: /* *unknown* */
1274
IFDBG {printf("\nEEP!\n");fflush(stdout);}
1275
g_message ("*** Unknown compression type in channel.");
1283
do_layers(FILE *fd, guint32 *offset)
1285
guint32 section_length;
1287
section_length = getglong(fd, "layerinfo sectionlength");
1290
IFDBG printf("\tLAYER INFO SECTION\n");
1291
IFDBG printf("\t\tSECTION LENGTH: %u\n",section_length);
1293
do_layer_struct(fd, offset);
1295
do_layer_pixeldata(fd, offset);
1299
do_layer_and_mask(FILE *fd)
1302
guint32 Size = PSDheader.miscsizelen;
1304
guint32 offset_now = ftell(fd);
1306
IFDBG printf("LAYER AND MASK INFO\n");
1307
IFDBG printf("\tSECTION LENGTH: %u\n",Size);
1309
if (Size == 0) return;
1311
do_layers(fd, &offset);
1313
IFDBG {printf("And...?\n");fflush(stdout);}
1319
printf("PSD: Supposedly there are %d bytes of mask info left.\n",
1321
if ((Size-offset == 4) || (Size-offset == 24))
1322
printf(" That sounds good to me.\n");
1324
printf(" That sounds strange to me.\n");
1328
/* if ((getguchar(fd, "mask info throw")!=0) ||
1329
(getguchar(fd, "mask info throw")!=0) ||
1330
(getguchar(fd, "mask info throw")!=0) ||
1331
(getguchar(fd, "mask info throw")!=0))
1333
printf("*** This mask info block looks pretty bogus.\n");
1337
printf("PSD: Stern warning - no mask info.\n");
1340
/* If 'offset' wasn't being buggily updated, we wouldn't need this. (!?) */
1341
fseek(fd, Size+offset_now, SEEK_SET);
1345
do_image_resources(FILE *fd)
1353
IFDBG printf("IMAGE RESOURCE BLOCK:\n");
1355
psd_image.resolution_is_set = 0;
1357
/* FIXME: too trusting that the file isn't corrupt */
1358
while (offset < PSDheader.imgreslen-1)
1362
xfread(fd, sig, 4, "imageresources signature");
1366
/* generic information about a block ID */
1369
ID = getgshort(fd, "ID num");
1371
IFDBG printf("\tID: 0x%04x / ",ID);
1373
Name = getpascalstring(fd, "ID name");
1378
IFDBG printf("\"%s\" ", Name);
1379
offset += strlen(Name);
1381
if (!(strlen(Name)&1))
1383
throwchunk(1, fd, "ID name throw");
1390
throwchunk(1, fd, "ID name throw2");
1394
Size = getglong(fd, "ID Size");
1396
IFDBG printf("Size: %d\n", Size);
1398
if (strncmp(sig, "8BIM", 4) == 0)
1399
dispatch_resID(ID, fd, &offset, Size);
1402
printf("PSD: Warning, unknown resource signature \"%.4s\" at or before offset %d ::: skipping\n", sig, offset - 8);
1403
throwchunk(Size, fd, "Skipping Unknown Resource");
1410
throwchunk(1, fd, "ID content throw");
1415
/* if (offset != PSDheader.imgreslen)
1417
printf("\tSucking imageres byte...\n");
1418
throwchunk(1, fd, "imageres suck");
1426
/* Convert RGB data to RGBA data */
1428
RGB_to_RGBA (const guchar* rgb_data, gint numpix)
1435
printf("NULL rgb data - eep!");
1439
rtn = g_malloc(numpix * 4);
1442
for (i=0; i<numpix; i++)
1444
rtn[j++] = rgb_data[i*3];
1445
rtn[j++] = rgb_data[i*3+1];
1446
rtn[j++] = rgb_data[i*3+2];
1452
#endif /* PANOTOOLS_FIX */
1456
chans_to_GRAYA (const guchar* grey,
1457
const guchar* alpha,
1463
if (!grey || !alpha)
1465
printf("NULL channel - eep!");
1469
rtn = g_malloc(numpix * 2);
1471
for (i = 0; i < numpix; i++)
1473
rtn[i*2 ] = grey[i];
1474
rtn[i*2+1] = alpha[i];
1481
chans_to_RGB (const guchar* red,
1482
const guchar* green,
1489
if (!red || !green || !blue)
1491
printf("NULL channel - eep!");
1495
rtn = g_malloc(numpix * 3);
1497
for (i=0; i<numpix; i++)
1500
rtn[i*3+1] = green[i];
1501
rtn[i*3+2] = blue[i];
1508
chans_to_RGBA (const guchar* red,
1509
const guchar* green,
1511
const guchar* alpha,
1516
gboolean careful = FALSE;
1518
if (!red || !green || !blue || !alpha)
1520
printf("chans_to_RGBA : NULL channel - eep!");
1524
rtn = g_malloc(numpix * 4);
1528
for (i=0; i<numpix; i++)
1531
rtn[i*4+1] = green[i];
1532
rtn[i*4+2] = blue[i];
1533
rtn[i*4+3] = alpha[i];
1538
for (i=0; i<numpix; i++)
1540
rtn[i*4 ] = (red==NULL) ? 0 : red[i];
1541
rtn[i*4+1] = (green==NULL) ? 0 : green[i];
1542
rtn[i*4+2] = (blue==NULL) ? 0 : blue[i];
1543
rtn[i*4+3] = (alpha==NULL) ? 0 : alpha[i];
1551
gboolean psd_layer_has_alpha(PSDlayer* layer)
1555
for (i = 0; i < layer->num_channels; i++)
1557
if (layer->channel[i].type == -1)
1566
void validate_aux_channel_name(gint aux_index)
1568
if (psd_image.aux_channel[aux_index].name == NULL)
1571
psd_image.aux_channel[aux_index].name = g_strdup ("Aux channel");
1573
psd_image.aux_channel[aux_index].name =
1574
g_strdup_printf ("Aux channel #%d", aux_index);
1579
void extract_data_and_channels(guchar* src, gint gimpstep, gint psstep,
1580
gint32 image_ID, GimpDrawable* drawable,
1581
gint width, gint height)
1583
guchar* primary_data;
1585
GimpPixelRgn pixel_rgn;
1587
IFDBG printf("Extracting primary channel data (%d channels)\n"
1588
"\tand %d auxiliary channels.\n", gimpstep, psstep-gimpstep);
1590
/* gimp doesn't like 0 width/height drawables. */
1591
if ((width == 0) || (height == 0))
1593
IFDBG printf("(bad channel dimensions -- skipping)");
1597
primary_data = g_malloc(width * height * gimpstep);
1601
for (pix = 0; pix < width * height; pix++)
1603
for (chan = 0; chan < gimpstep; chan++)
1605
primary_data[pix * gimpstep + chan] = src[pix * psstep + chan];
1608
gimp_pixel_rgn_init (&pixel_rgn, drawable,
1609
0, 0, drawable->width, drawable->height,
1611
gimp_pixel_rgn_set_rect (&pixel_rgn, primary_data,
1612
0, 0, drawable->width, drawable->height);
1614
gimp_drawable_flush (drawable);
1615
gimp_drawable_detach (drawable);
1617
g_free (primary_data);
1619
aux_data = g_malloc (width * height);
1621
int pix, chan, aux_index;
1623
GimpDrawable* chdrawable;
1626
gimp_rgb_set (&colour, 0.0, 0.0, 0.0);
1628
for (chan=gimpstep; chan<psstep; chan++)
1630
for (pix = 0; pix < width * height; pix++)
1632
aux_data [pix] = src [pix * psstep + chan];
1635
aux_index = chan - gimpstep;
1636
validate_aux_channel_name (aux_index);
1638
channel_ID = gimp_channel_new (image_ID,
1639
psd_image.aux_channel[aux_index].name,
1642
gimp_image_add_channel (image_ID, channel_ID, 0);
1643
gimp_drawable_set_visible (channel_ID, FALSE);
1645
chdrawable = gimp_drawable_get (channel_ID);
1647
gimp_pixel_rgn_init (&pixel_rgn, chdrawable,
1648
0, 0, chdrawable->width, chdrawable->height,
1650
gimp_pixel_rgn_set_rect (&pixel_rgn, aux_data,
1651
0, 0, chdrawable->width, chdrawable->height);
1653
gimp_drawable_flush (chdrawable);
1654
gimp_drawable_detach (chdrawable);
1659
IFDBG printf("Done with that.\n\n");
1663
extract_channels(guchar* src, gint num_wanted, gint psstep,
1665
gint width, gint height)
1668
GimpPixelRgn pixel_rgn;
1670
IFDBG printf("Extracting %d/%d auxiliary channels.\n", num_wanted, psstep);
1672
/* gimp doesn't like 0 width/height drawables. */
1673
if ((width == 0) || (height == 0))
1675
IFDBG printf("(bad channel dimensions -- skipping)");
1679
aux_data = g_malloc(width * height);
1681
int pix, chan, aux_index;
1683
GimpDrawable* chdrawable;
1686
gimp_rgb_set (&colour, 0.0, 0.0, 0.0);
1688
for (chan=psstep-num_wanted; chan<psstep; chan++)
1690
for (pix = 0; pix < width * height; pix++)
1692
aux_data [pix] = src [pix * psstep + chan];
1695
aux_index = chan - (psstep - num_wanted);
1696
validate_aux_channel_name (aux_index);
1698
channel_ID = gimp_channel_new (image_ID,
1699
psd_image.aux_channel[aux_index].name,
1702
gimp_image_add_channel (image_ID, channel_ID, 0);
1703
gimp_drawable_set_visible (channel_ID, FALSE);
1705
chdrawable = gimp_drawable_get (channel_ID);
1707
gimp_pixel_rgn_init (&pixel_rgn, chdrawable,
1708
0, 0, chdrawable->width, chdrawable->height,
1710
gimp_pixel_rgn_set_rect (&pixel_rgn, aux_data,
1711
0, 0, chdrawable->width, chdrawable->height);
1713
gimp_drawable_flush (chdrawable);
1714
gimp_drawable_detach (chdrawable);
1719
IFDBG printf("Done with that.\n\n");
1723
resize_mask(guchar* src, guchar* dest,
1724
gint32 src_x, gint32 src_y,
1725
gint32 src_w, gint32 src_h,
1726
gint32 dest_w, gint32 dest_h)
1730
IFDBG printf("--> %p %p : %d %d . %d %d . %d %d\n",
1736
for (y=0; y<dest_h; y++)
1738
for (x=0; x<dest_w; x++)
1740
/* Avoid a 1-pixel border top-left */
1741
if ((x>=src_x) && (x<src_x+src_w) &&
1742
(y>=src_y) && (y<src_y+src_h))
1744
dest[dest_w * y + x] =
1745
src[src_w * (y-src_y) + (x-src_x)];
1749
dest[dest_w * y + x] = 255;
1756
load_image (const gchar *name)
1762
guchar *dest = NULL, *temp;
1763
long channels, nguchars;
1764
psd_imagetype imagetype;
1765
gboolean cmyk = FALSE;
1767
gint32 image_ID = -1;
1768
gint32 layer_ID = -1;
1769
GimpDrawable *drawable = NULL;
1770
GimpPixelRgn pixel_rgn;
1773
int red_chan, grn_chan, blu_chan, alpha_chan, ichan;
1775
IFDBG printf("------- %s ---------------------------------\n",name);
1777
fd = fopen (name, "rb");
1780
g_message (_("Could not open '%s' for reading: %s"),
1781
gimp_filename_to_utf8 (name), g_strerror (errno));
1785
name_buf = g_strdup_printf (_("Opening '%s'..."),
1786
gimp_filename_to_utf8 (name));
1787
gimp_progress_init (name_buf);
1790
read_whole_file (fd);
1792
if (psd_image.num_layers > 0) /* PS3-style */
1795
GimpImageBaseType gimagetype;
1797
gimagetype = psd_mode_to_gimp_base_type(PSDheader.mode);
1799
gimp_image_new (PSDheader.columns, PSDheader.rows, gimagetype);
1800
gimp_image_set_filename (image_ID, name);
1802
if (psd_image.resolution_is_set)
1804
gimp_image_set_resolution(image_ID,
1805
psd_image.resolution.hRes / 65536.0,
1806
psd_image.resolution.vRes / 65536.0);
1807
/* currently can only set one unit for the image so we use the
1808
horizontal unit from the psd image */
1809
gimp_image_set_unit(image_ID,
1810
psd_unit_to_gimp_unit (psd_image.resolution.widthUnit));
1813
fgetpos (fd, &tmpfpos);
1815
for (lnum = 0; lnum < psd_image.num_layers; lnum++)
1818
guchar* merged_data = NULL;
1819
PSDlayer *layer = psd_image.layer + lnum;
1822
* since ps supports sloppy bounding boxes it is possible to
1823
* have a 0x0 or Xx0 or 0xY layer. Gimp doesn't support a
1824
* 0x0 layer so we will just skip these. We might be able
1825
* to do something better here.
1827
if ((layer->width == 0) || (layer->height == 0))
1829
IFDBG printf("(bad layer dimensions -- skipping)");
1832
numc = layer->num_channels;
1834
IFDBG printf("Hey, it's a LAYER with %d channels!\n", numc);
1840
IFDBG printf("It's GRAY.\n");
1841
if (!psd_layer_has_alpha(layer))
1843
merged_data = g_malloc(layer->width * layer->height);
1844
seek_to_and_unpack_pixeldata(fd, lnum, 0);
1845
memcpy(merged_data, layer->channel[0].data,
1846
layer->width * layer->height);
1848
g_free(layer->channel[0].data);
1852
seek_to_and_unpack_pixeldata(fd, lnum, 0);
1853
seek_to_and_unpack_pixeldata(fd, lnum, 1);
1855
chans_to_GRAYA (layer->channel[1].data,
1856
layer->channel[0].data,
1857
layer->width * layer->height);
1859
g_free(layer->channel[0].data);
1860
g_free(layer->channel[1].data);
1863
layer_ID = gimp_layer_new (image_ID,
1867
(numc==1) ? GIMP_GRAY_IMAGE : GIMP_GRAYA_IMAGE,
1868
(100.0 * layer->opacity) / 255.0,
1869
psd_lmode_to_gimp_lmode(layer->blendkey));
1871
}; break; /* case GIMP_GRAY */
1875
IFDBG printf("It's RGB, %dx%d.\n", layer->width,
1877
if (!psd_layer_has_alpha(layer))
1879
seek_to_and_unpack_pixeldata(fd, lnum, 0);
1880
seek_to_and_unpack_pixeldata(fd, lnum, 1);
1881
seek_to_and_unpack_pixeldata(fd, lnum, 2);
1883
chans_to_RGB (layer->channel[0].data,
1884
layer->channel[1].data,
1885
layer->channel[2].data,
1889
g_free(layer->channel[0].data);
1890
g_free(layer->channel[1].data);
1891
g_free(layer->channel[2].data);
1894
fprintf(stderr, "YAH0a\n");
1898
seek_to_and_unpack_pixeldata(fd, lnum, 0);
1899
seek_to_and_unpack_pixeldata(fd, lnum, 1);
1900
seek_to_and_unpack_pixeldata(fd, lnum, 2);
1901
seek_to_and_unpack_pixeldata(fd, lnum, 3);
1903
/* Fix for unexpected layer data order for files
1904
* from PS files created by PanoTools. Rather
1905
* than assuming an order, we find the actual order.
1908
red_chan = grn_chan = blu_chan = alpha_chan = -1;
1910
for (ichan=0; ichan<numc; ichan++)
1912
switch(psd_image.layer[lnum].channel[ichan].type)
1914
case 0: red_chan = ichan; break;
1915
case 1: grn_chan = ichan; break;
1916
case 2: blu_chan = ichan; break;
1917
case -1: alpha_chan = ichan; break;
1921
if ((red_chan < 0) ||
1926
g_message ("Error: Cannot identify required RGBA channels");
1932
chans_to_RGBA (psd_image.layer[lnum].channel[red_chan].data,
1933
psd_image.layer[lnum].channel[grn_chan].data,
1934
psd_image.layer[lnum].channel[blu_chan].data,
1935
psd_image.layer[lnum].channel[alpha_chan].data,
1936
psd_image.layer[lnum].width *
1937
psd_image.layer[lnum].height);
1939
g_free(layer->channel[0].data);
1940
g_free(layer->channel[1].data);
1941
g_free(layer->channel[2].data);
1942
g_free(layer->channel[3].data);
1945
fprintf(stderr, "YAH0b\n");
1949
fprintf(stderr, "YAH1\n");
1951
layer_ID = gimp_layer_new (image_ID,
1952
psd_image.layer[lnum].name,
1953
psd_image.layer[lnum].width,
1954
psd_image.layer[lnum].height,
1955
psd_layer_has_alpha(&psd_image.layer[lnum]) ?
1956
GIMP_RGBA_IMAGE : GIMP_RGB_IMAGE,
1957
(100.0 * (double)psd_image.layer[lnum].opacity) / 255.0,
1958
psd_lmode_to_gimp_lmode(psd_image.layer[lnum].blendkey));
1961
fprintf(stderr, "YAH2\n");
1963
}; break; /* case GIMP_RGB */
1967
g_message ("Error: Sorry, can't deal with a layered image of this type.\n");
1969
}; break; /* default */
1972
gimp_image_add_layer (image_ID, layer_ID, 0);
1975
fprintf(stderr, "YAH3\n");
1977
/* Do a layer mask if it exists */
1978
for (iter = 0; iter < layer->num_channels; iter++)
1980
if (layer->channel[iter].type == -2) /* is mask */
1986
fprintf(stderr, "YAH3m\n");
1988
lm_data = g_malloc(layer->width * layer->height);
1994
seek_to_and_unpack_pixeldata(fd, lnum, iter);
1995
/* PS layer masks can be a different size to
1996
their owning layer, so we have to resize them. */
1997
resize_mask(layer->channel[iter].data,
1999
layer->lm_x - layer->x,
2000
layer->lm_y - layer->y,
2001
layer->lm_width, layer->lm_height,
2002
layer->width, layer->height);
2004
/* won't be needing the original data any more */
2005
g_free(layer->channel[iter].data);
2007
/* give it to GIMP */
2008
mask_id = gimp_layer_create_mask(layer_ID, 0);
2011
/* Convert the layer RGB data (not the mask) to RGBA */
2013
merged_data = RGB_to_RGBA (tmp,
2014
psd_image.layer[lnum].width *
2015
psd_image.layer[lnum].height);
2018
/* Add alpha - otherwise cannot add layer mask */
2019
gimp_layer_add_alpha (layer_ID);
2021
#endif /* PANOTOOLS_FIX */
2022
/* Add layer mask */
2023
gimp_layer_add_mask (layer_ID, mask_id);
2025
drawable = gimp_drawable_get (mask_id);
2027
gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,
2028
layer->width, layer->height,
2030
gimp_pixel_rgn_set_rect (&pixel_rgn, lm_data, 0, 0,
2031
layer->width, layer->height);
2035
gimp_drawable_flush (drawable);
2036
gimp_drawable_detach (drawable);
2041
fprintf(stderr, "YAH4\n");
2043
gimp_layer_translate (layer_ID, layer->x, layer->y);
2045
gimp_layer_set_preserve_trans (layer_ID, layer->protecttrans);
2046
gimp_drawable_set_visible (layer_ID, layer->visible);
2048
drawable = gimp_drawable_get (layer_ID);
2051
fprintf(stderr, "YAH5 - merged_data=%p, drawable=%p, drawdim=%dx%dx%d\n",
2058
gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,
2059
layer->width, layer->height,
2061
gimp_pixel_rgn_set_rect (&pixel_rgn, merged_data, 0, 0,
2062
layer->width, layer->height);
2065
fprintf(stderr, "YAH6\n");
2067
gimp_drawable_flush (drawable);
2068
gimp_drawable_detach (drawable);
2071
g_free (merged_data);
2073
gimp_progress_update ((double)(lnum+1.0) /
2074
(double)psd_image.num_layers);
2076
fsetpos (fd, &tmpfpos);
2079
if ((psd_image.num_aux_channels > 0) &&
2080
(psd_image.num_layers > 0))
2083
IFDBG printf("::::::::::: WANT AUX :::::::::::::::::::::::::::::::::::::::\n");
2091
if (want_aux || (psd_image.num_layers==0)) /* Photoshop2-style: NO LAYERS. */
2094
IFDBG printf("Image data %ld chars\n", PSDheader.imgdatalen);
2096
step = PSDheader.channels;
2098
imagetype = PSD_UNKNOWN_IMAGE;
2099
switch (PSDheader.mode)
2101
case 0: /* Bitmap */
2102
imagetype = PSD_BITMAP_IMAGE;
2104
case 1: /* Grayscale */
2105
imagetype = PSD_GRAY_IMAGE;
2107
case 2: /* Indexed Colour */
2108
imagetype = PSD_INDEXED_IMAGE;
2110
case 3: /* RGB Colour */
2111
imagetype = PSD_RGB_IMAGE;
2113
case 4: /* CMYK Colour */
2115
switch (PSDheader.channels)
2118
imagetype = PSD_RGB_IMAGE;
2121
imagetype = PSD_RGBA_IMAGE;
2124
printf("%s: cannot handle CMYK with more than 5 channels\n",
2130
case 7: /* Multichannel (?) */
2131
case 8: /* Duotone */
2132
case 9: /* Lab Colour */
2138
if (imagetype == PSD_UNKNOWN_IMAGE)
2140
printf("%s: Image type %d (%s) is not supported in this data format\n",
2141
prog_name, PSDheader.mode,
2142
(PSDheader.mode > 10) ?
2143
"<out of range>" : modename[PSDheader.mode]);
2148
if ((PSDheader.bpp != 8) && (PSDheader.bpp != 1))
2150
printf("%s: The GIMP only supports 8-bit or 1-bit deep PSD images "
2157
"psd:%d gimp:%d gimpbase:%d\n",
2159
psd_type_to_gimp_type(imagetype),
2160
psd_type_to_gimp_base_type(imagetype)
2166
/* gimp doesn't like 0 width/height drawables. */
2167
if ((PSDheader.columns == 0) || (PSDheader.rows == 0))
2169
IFDBG printf("(bad psd2-style image dimensions -- skipping)");
2174
image_ID = gimp_image_new (PSDheader.columns, PSDheader.rows,
2175
psd_type_to_gimp_base_type(imagetype));
2176
gimp_image_set_filename (image_ID, name);
2177
if (psd_type_to_gimp_base_type(imagetype) == GIMP_INDEXED)
2179
if ((psd_image.colmaplen%3)!=0)
2180
printf("PSD: Colourmap looks screwed! Aiee!\n");
2181
if (psd_image.colmaplen==0)
2182
printf("PSD: Indexed image has no colourmap!\n");
2183
if (psd_image.colmaplen!=768)
2184
printf("PSD: Warning: Indexed image is %ld!=256 colours.\n",
2185
psd_image.colmaplen/3);
2186
if (psd_image.colmaplen==768)
2188
reshuffle_cmap(psd_image.colmapdata);
2189
gimp_image_set_colormap (image_ID,
2190
psd_image.colmapdata,
2195
layer_ID = gimp_layer_new (image_ID, _("Background"),
2196
PSDheader.columns, PSDheader.rows,
2197
psd_type_to_gimp_type(imagetype),
2198
100, GIMP_NORMAL_MODE);
2200
gimp_image_add_layer (image_ID, layer_ID, 0);
2201
drawable = gimp_drawable_get (layer_ID);
2208
switch (PSDheader.mode)
2210
case 1: /* Grayscale */
2213
case 2: /* Indexed Colour */
2216
case 3: /* RGB Colour */
2220
printf("aux? Aieeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee!!!!!!!!!\n");
2225
channels = PSDheader.channels - channels;
2227
if (psd_image.absolute_alpha)
2234
channels = gimp_drawable_bpp(drawable->drawable_id);
2238
dest = g_malloc( step * PSDheader.columns * PSDheader.rows );
2240
if (PSDheader.compression == 1)
2242
nguchars = PSDheader.columns * PSDheader.rows;
2243
temp = g_malloc (PSDheader.imgdatalen);
2244
xfread (fd, temp, PSDheader.imgdatalen, "image data");
2247
gimp_progress_update ((double)1.00);
2249
if(imagetype==PSD_BITMAP_IMAGE) /* convert bitmap to grayscale */
2254
g_malloc (((PSDheader.columns + 7) >> 3) * PSDheader.rows);
2256
decode (PSDheader.imgdatalen,
2257
nguchars >> 3, temp,monobuf, step);
2258
bitmap2gray (monobuf, dest,
2259
PSDheader.columns, PSDheader.rows);
2265
decode(PSDheader.imgdatalen, nguchars, temp, dest, step);
2270
gimp_progress_update (1.0);
2272
cmykbuf = g_malloc (step * nguchars);
2273
decode (PSDheader.imgdatalen, nguchars, temp, cmykbuf, step);
2275
cmyk2rgb (cmykbuf, dest, PSDheader.columns, PSDheader.rows,
2286
gimp_progress_update ((double)1.00);
2288
xfread_interlaced(fd, dest, PSDheader.imgdatalen,
2289
"raw image data", step);
2293
gimp_progress_update ((double)1.00);
2295
cmykbuf = g_malloc(PSDheader.imgdatalen);
2296
xfread_interlaced(fd, cmykbuf, PSDheader.imgdatalen,
2297
"raw cmyk image data", step);
2299
cmykp2rgb(cmykbuf, dest,
2300
PSDheader.columns, PSDheader.rows, step > 4);
2306
if (want_aux) /* want_aux */
2308
extract_channels(dest, channels, step,
2310
PSDheader.columns, PSDheader.rows);
2312
goto finish_up; /* Haha! Look! A goto! */
2316
gimp_progress_update ((double)1.00);
2318
if (channels == step) /* gimp bpp == psd bpp */
2321
if (psd_type_to_gimp_type(imagetype)==GIMP_INDEXEDA_IMAGE)
2323
printf("@@@@ Didn't know that this could happen...\n");
2324
for (iter=0; iter<drawable->width*drawable->height; iter++)
2326
dest[iter*2+1] = 255;
2330
gimp_pixel_rgn_init (&pixel_rgn, drawable,
2331
0, 0, drawable->width, drawable->height,
2333
gimp_pixel_rgn_set_rect (&pixel_rgn, dest,
2334
0, 0, drawable->width, drawable->height);
2336
gimp_drawable_flush (drawable);
2337
gimp_drawable_detach (drawable);
2341
IFDBG printf("Uhhh... uhm... extra channels... heavy...\n");
2343
extract_data_and_channels(dest, channels, step,
2345
drawable->width, drawable->height);
2355
if (psd_image.colmaplen > 0)
2356
g_free(psd_image.colmapdata);
2359
if (psd_image.num_guides > 0)
2361
PSDguide *guide = psd_image.guides;
2364
IFDBG printf("--- Adding %d Guides\n", psd_image.num_guides);
2366
for (i = 0; i < psd_image.num_guides; i++, guide++)
2368
if (guide->horizontal)
2369
gimp_image_add_hguide (image_ID, guide->position);
2371
gimp_image_add_vguide (image_ID, guide->position);
2375
gimp_displays_flush();
2377
IFDBG printf("--- %d layers : pos %ld : a-alph %d ---\n",
2378
psd_image.num_layers, (long int)ftell(fd),
2379
psd_image.absolute_alpha);
2385
decode(long clen, long uclen, guchar *src, guchar* dst, int step)
2392
for (i = 0; i < PSDheader.rows*PSDheader.channels; ++i)
2394
l -= PSDheader.rowlength[i];
2398
g_warning("decode: %ld should be zero\n", (long)l);
2401
w = PSDheader.rowlength;
2403
packbitsdecode(&clen, uclen, src, dst++, step);
2405
for (j = 0; j < step-1; ++j)
2407
for (i = 0; i < PSDheader.rows; ++i)
2411
packbitsdecode(&clen, uclen, src, dst++, step);
2414
IFDBG printf("clen %ld\n", clen);
2418
* Decode a PackBits data stream.
2421
packbitsdecode(long * clenp, long uclen, guchar *src, guchar *dst, int step)
2424
gint32 clen = *clenp;
2426
while ((clen > 0) && (uclen > 0)) {
2430
if (n < 0) { /* replicate next guchar -n+1 times */
2432
if (n == -128) /* nop */
2436
for (b = *src++; n > 0; --n) {
2440
} else { /* copy next n+1 guchars literally */
2441
for (b = ++n; b > 0; --b) {
2450
printf("%s: unexpected EOF while reading image data\n", prog_name);
2457
* Decode a PackBits channel from file.
2460
unpack_pb_channel(FILE *fd, guchar *dst, gint32 unpackedlen, guint32 *offset)
2463
gint32 upremain = unpackedlen;
2465
while (upremain > 0)
2467
n = (int) getguchar(fd, "packbits1");
2473
{ /* replicate next guchar -n+1 times */
2474
if (n == -128) /* nop */
2479
b = getguchar(fd, "packbits2");
2483
if (upremain >= 0) {
2491
{ /* copy next n+1 guchars literally */
2492
for (b = ++n; b > 0; --b)
2494
const guchar c = getguchar(fd, "packbits3");
2495
if (upremain >= 0) {
2508
printf("*** Unpacking overshot destination (%d) buffer by %d bytes!\n",
2515
cmyk2rgb(unsigned char * src, unsigned char * dst,
2516
long width, long height, int alpha)
2522
for (i = 0; i < height; i++) {
2523
for (j = 0; j < width; j++) {
2529
gimp_cmyk_to_rgb_int (&r, &g, &b, &k);
2540
gimp_progress_update ((double)(2.0*(double)height)/
2541
((double)height+(double)i));
2546
* Decode planar CMYK(A) to RGB(A).
2549
cmykp2rgb(unsigned char * src, unsigned char * dst,
2550
long width, long height, int alpha)
2555
guchar *rp, *gp, *bp, *kp, *ap;
2564
for (i = 0; i < height; i++) {
2565
for (j = 0; j < width; j++) {
2571
gimp_cmyk_to_rgb_int (&r, &g, &b, &k);
2581
gimp_progress_update ((double)(2.0*(double)height)/
2582
((double)height+(double)i));
2587
bitmap2gray(guchar *src,guchar *dest,long w,long h)
2595
*dest++=(*src&mask)?0:255;
2603
if(mask!=0x80) src++;
2608
dumpchunk(size_t n, FILE * fd, gchar *why)
2616
printf("%02x ",(int)getguchar(fd, why));
2623
throwchunk(size_t n, FILE * fd, gchar *why)
2633
tmpchunk = g_malloc(n);
2634
xfread(fd, tmpchunk, n, why);
2642
if (fseek(fd, n, SEEK_CUR) != 0)
2644
printf("%s: unable to seek forward while reading '%s' chunk\n",
2652
getstring(size_t n, FILE * fd, gchar *why)
2656
tmpchunk = g_malloc(n + 1);
2657
xfread(fd, tmpchunk, n, why);
2660
return tmpchunk; /* caller should free memory */
2664
getpascalstring(FILE *fd, gchar *why)
2669
xfread(fd, &len, 1, why);
2676
tmpchunk = g_malloc(len+1);
2678
xfread(fd, tmpchunk, len, why);
2681
return (gchar *) tmpchunk; /* caller should free memory */
2685
getguchar(FILE *fd, gchar *why)
2693
printf("%s: unexpected EOF while reading '%s' chunk\n",
2701
getgshort(FILE *fd, gchar *why)
2705
b1 = getguchar(fd, why);
2706
b2 = getguchar(fd, why);
2708
return (gshort) ((b1 * 256) + b2);
2712
getglong(FILE *fd, gchar *why)
2714
guchar s1, s2, s3, s4;
2716
s1 = getguchar(fd, why);
2717
s2 = getguchar(fd, why);
2718
s3 = getguchar(fd, why);
2719
s4 = getguchar(fd, why);
2721
return (glong) ((s1*256*256*256) + (s2*256*256) + (s3*256) + s4);
2725
xfread(FILE * fd, void * buf, long len, gchar *why)
2727
if (fread(buf, len, 1, fd) == 0)
2729
printf("%s: unexpected EOF while reading '%s' chunk\n",
2736
xfread_interlaced(FILE* fd, guchar* buf, long len, gchar *why, gint step)
2739
gint pix, pos, bpplane;
2745
printf("PSD: Stern warning: data size is not a factor of step size!\n");
2748
for (pix=0; pix<step; pix++)
2752
for (pos=0; pos<bpplane; pos++)
2754
*dest = getguchar(fd, why);
2761
read_whole_file(FILE * fd)
2768
xfread(fd, &PSDheader.signature, 4, "signature");
2769
PSDheader.version = getgshort(fd, "version");
2770
xfread(fd, &dummy, 6, "reserved");
2771
PSDheader.channels = getgshort(fd, "channels");
2772
PSDheader.rows = getglong(fd, "rows");
2773
PSDheader.columns = getglong(fd, "columns");
2774
PSDheader.bpp = getgshort(fd, "depth");
2775
PSDheader.mode = getgshort(fd, "mode");
2778
psd_image.num_layers = 0;
2779
psd_image.type = PSDheader.mode;
2780
psd_image.colmaplen = 0;
2781
psd_image.num_aux_channels = 0;
2782
psd_image.num_guides = 0;
2785
psd_image.colmaplen = getglong(fd, "color data length");
2787
if (psd_image.colmaplen > 0)
2789
psd_image.colmapdata = g_malloc(psd_image.colmaplen);
2790
xfread(fd, psd_image.colmapdata, psd_image.colmaplen, "colormap");
2794
PSDheader.imgreslen = getglong(fd, "image resource length");
2795
if (PSDheader.imgreslen > 0)
2797
do_image_resources(fd);
2801
PSDheader.miscsizelen = getglong(fd, "misc size data length");
2802
if (PSDheader.miscsizelen > 0)
2804
do_layer_and_mask(fd);
2808
PSDheader.compression = getgshort(fd, "compression");
2809
IFDBG printf("<<compr:%d>>", (int)PSDheader.compression);
2810
if (PSDheader.compression == 1) /* RLE */
2812
PSDheader.rowlength = g_malloc(PSDheader.rows *
2813
PSDheader.channels * sizeof(gushort));
2814
for (i = 0; i < PSDheader.rows*PSDheader.channels; ++i)
2815
PSDheader.rowlength[i] = getgshort(fd, "x");
2818
fseek(fd, 0, SEEK_END);
2819
PSDheader.imgdatalen = ftell(fd)-pos;
2820
fseek(fd, pos, SEEK_SET);
2823
if (strncmp(PSDheader.signature, "8BPS", 4) != 0)
2825
printf("%s: not an Adobe Photoshop PSD file\n", prog_name);
2828
if (PSDheader.version != 1)
2830
printf("%s: bad version number '%d', not 1\n",
2831
prog_name, PSDheader.version);
2835
IFDBG printf("HEAD:\n"
2836
"\tChannels %d\n\tRows %ld\n\tColumns %ld\n\tDepth %d\n\tMode %d (%s)\n"
2837
"\tColour data %ld guchars\n",
2838
PSDheader.channels, PSDheader.rows,
2839
PSDheader.columns, PSDheader.bpp,
2840
w, modename[w < 10 ? w : 10],
2841
psd_image.colmaplen);
2842
/* printf("\tImage resource length: %lu\n", PSDheader.imgreslen);*/
2843
IFDBG printf("\tLayer/Mask Data length: %lu\n", PSDheader.miscsizelen);
2844
w = PSDheader.compression;
2845
IFDBG printf("\tCompression %d (%s)\n", w, w ? "RLE" : "raw");