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
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
429
  
430
  
431
  
432
  
433
  
434
  
435
  
436
  
437
  
438
  
439
  
440
  
441
  
442
  
443
  
444
  
445
  
446
  
447
  
448
  
449
  
450
  
451
  
452
  
453
  
454
  
455
  
456
  
457
  
458
  
459
  
460
  
461
  
462
  
463
  
464
  
465
  
466
  
467
  
468
  
469
  
470
  
471
  
472
  
473
  
474
  
475
  
476
  
477
  
478
  
479
  
480
  
481
  
482
  
483
  
484
  
485
  
486
  
487
  
488
  
489
  
490
  
491
  
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
  
526
  
527
  
528
  
529
  
530
  
531
  
532
  
533
  
534
  
535
  
536
  
537
  
538
  
539
  
540
  
541
  
542
  
543
  
544
  
545
  
546
  
547
  
548
  
549
  
550
  
551
  
552
  
553
  
554
  
555
  
556
  
557
  
558
  
559
  
560
  
561
  
562
  
563
  
564
  
565
  
566
  
567
  
568
  
569
  
570
  
571
  
572
  
573
  
574
  
575
  
576
  
577
  
578
  
579
  
580
  
581
  
582
  
583
  
584
  
585
  
586
  
587
  
588
  
589
  
590
  
591
  
592
  
593
  
594
  
595
  
596
  
597
  
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
  
626
  
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
  
662
  
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
  
694
  
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
  
722
  
723
  
724
  
725
  
726
  
727
  
728
  
729
  
730
  
731
  
732
  
733
  
734
  
735
  
736
  
737
  
738
  
739
  
740
  
741
  
742
  
743
  
744
  
745
  
746
  
747
  
748
  
749
  
750
  
751
  
752
  
753
  
754
  
755
  
756
  
757
  
758
  
759
  
760
  
761
  
762
  
763
  
764
  
765
  
766
  
767
  
768
  
769
  
770
  
771
  
772
  
773
  
774
  
775
  
776
  
777
  
778
  
779
  
780
  
781
  
782
  
783
  
784
  
785
  
786
  
787
  
788
  
789
  
790
  
791
  
792
  
793
  
794
  
795
  
796
  
797
  
798
  
799
  
800
  
801
  
802
  
803
  
804
  
805
  
806
  
807
  
808
  
809
  
810
  
811
  
812
  
813
  
814
  
815
  
816
  
817
  
818
  
819
  
820
  
821
  
822
  
823
  
824
  
825
  
826
  
827
  
828
  
829
  
830
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
#include "global.h" 
#include "image_machine.h" 
 
#include "pike_macros.h" 
#include "object.h" 
#include "module_support.h" 
#include "interpret.h" 
#include "svalue.h" 
#include "threads.h" 
#include "mapping.h" 
#include "pike_error.h" 
#include "stralloc.h" 
#include "builtin_functions.h" 
 
#include "image.h" 
#include "colortable.h" 
#include "bignum.h" 
 
#ifdef HAVE_NETINET_IN_H 
#include <netinet/in.h> 
#endif 
 
 
#define sp Pike_sp 
 
extern struct program *image_colortable_program; 
extern struct program *image_program; 
 
#define STRING(X) static struct pike_string *PIKE_CONCAT(s_, X) 
#include "psd_constant_strings.h" 
#undef STRING 
 
 
struct buffer 
{ 
  size_t len; 
  unsigned char *str; 
}; 
 
static unsigned int psd_read_uint( struct buffer *from ) 
{ 
  unsigned int res; 
  if(from->len < 4) 
    Pike_error("Not enough space for 4 bytes (uint32)\n"); 
  res = from->str[0]<<24|from->str[1]<<16|from->str[2]<<8|from->str[3]; 
  from->str += 4; 
  from->len -= 4; 
  return res; 
} 
 
static int psd_read_int( struct buffer *from ) 
{ 
  return (int)psd_read_uint( from ); 
} 
 
static unsigned short psd_read_ushort( struct buffer *from ) 
{ 
  unsigned short res; 
  if(from->len < 2) 
    Pike_error("Not enough space for 2 bytes (uint16)\n"); 
  res = from->str[0]<<8 | from->str[1]; 
  from->str += 2; 
  from->len -= 2; 
  return res; 
} 
 
static int psd_read_short( struct buffer *from ) 
{ 
  return (short)psd_read_ushort( from ); 
} 
 
static unsigned char psd_read_uchar( struct buffer *from ) 
{ 
  unsigned char res = 0; 
  if(from->len) 
  { 
    res = from->str[0]; 
    from->str++; 
    from->len--; 
  } 
  return res; 
} 
 
static int psd_read_schar(struct buffer *from) 
{ 
  return (signed char)psd_read_uchar(from); 
} 
 
static char *psd_read_data( struct buffer * from, size_t len ) 
{ 
  char *res; 
  if( from->len < len ) 
    Pike_error("Not enough space for %lu bytes\n", (unsigned long)len); 
  res = (char *)from->str; 
  from->str += len; 
  from->len -= len; 
  return res; 
} 
 
static struct buffer psd_read_string( struct buffer *data ) 
{ 
  struct buffer res; 
  res.len = psd_read_int( data ); 
  res.str = (unsigned char *)psd_read_data( data, res.len ); 
  if(res.len > 0) res.len--; /* len includes ending \0 */ 
  if(!res.str) 
    Pike_error("String read failed\n"); 
  return res; 
} 
 
static struct buffer psd_read_pstring( struct buffer *data ) 
{ 
  struct buffer res; 
  res.len = psd_read_uchar( data ); 
  res.str = (unsigned char *)psd_read_data( data, res.len ); 
  if(!res.str) 
    Pike_error("PString read failed\n"); 
  return res; 
} 
 
enum image_mode 
{ 
  Bitmap = 0, 
  Greyscale = 1, 
  Indexed = 2, 
  RGB = 3, 
  CMYK = 4, 
  Multichannel = 7, 
  Duotone = 8, 
  Lab = 9, 
}; 
 
 
struct psd_image 
{ 
  unsigned short num_channels; 
  unsigned int rows; 
  unsigned int columns; 
  unsigned int compression; 
  unsigned short depth; 
  enum image_mode mode; 
  struct buffer color_data; 
  struct buffer resource_data; 
  struct buffer layer_data; 
  struct buffer image_data; 
 
 
  struct layer *first_layer; 
}; 
 
struct channel_info 
{ 
  short id; 
  struct buffer data; 
}; 
 
struct layer 
{ 
  struct layer *next; 
  struct layer *prev; 
  int top; 
  int left; 
  int right; 
  int bottom; 
 
  int mask_top; 
  int mask_left; 
  int mask_right; 
  int mask_bottom; 
  unsigned int mask_default_color; 
  unsigned int mask_flags; 
 
  unsigned int opacity; 
  unsigned int num_channels; 
  unsigned int clipping; 
  unsigned int flags; 
  struct channel_info channel_info[24]; 
  struct buffer mode; 
  struct buffer extra_data; 
  struct buffer name; 
}; 
 
static void decode_layers_and_masks( struct psd_image *dst, 
                                     struct buffer *src ) 
{ 
  short count, first_alpha_is_magic; 
  struct layer *layer = NULL; 
  ptrdiff_t exp_offset; 
  if(!src->len) 
    return; 
 
  exp_offset = src->len-psd_read_int( src ); /* size of layer region */ 
  count = psd_read_short( src ); 
 
  if( count < 0 ) 
  { 
    if (DO_INT16_NEG_OVERFLOW(count, &count)) 
        Pike_error("overflow\n"); 
    first_alpha_is_magic = 1; 
  } else if(count == 0) 
    return; 
  while( count-- ) 
  { 
    unsigned int cnt; 
    struct layer *l = layer = xcalloc( sizeof( struct layer ), 1); 
    layer->next = dst->first_layer; 
    if(dst->first_layer) dst->first_layer->prev = layer; 
    dst->first_layer = layer; 
    layer->top = psd_read_int( src ); 
    layer->left = psd_read_int( src ); 
    layer->bottom = psd_read_int( src ); 
    layer->right = psd_read_int( src ); 
    layer->num_channels = psd_read_ushort( src ); 
    if (layer->num_channels * 6 > src->len) 
      Pike_error("Too many channels.\n"); 
    for(cnt=0; cnt<layer->num_channels; cnt++) 
    { 
      layer->channel_info[cnt].id = psd_read_ushort(src); 
      layer->channel_info[cnt].data.len = psd_read_uint(src); 
    } 
    psd_read_uint( src ); /* '8BIM' */ 
    layer->mode.len = 4; 
    layer->mode.str = (unsigned char *)psd_read_data( src, 4 ); 
    layer->opacity = psd_read_uchar( src ); 
    layer->clipping = psd_read_uchar( src ); 
    layer->flags = psd_read_uchar( src ); 
    psd_read_uchar( src ); /* filler byte */ 
    layer->extra_data = psd_read_string( src ); 
    layer->extra_data.len++; 
    if(layer->extra_data.len) 
    { 
      struct buffer tmp = layer->extra_data; 
      struct buffer tmp2; 
      tmp2 = psd_read_string( &tmp ); 
      if( tmp2.len ) 
      { 
        layer->mask_top    = psd_read_int( &tmp2 ); 
        layer->mask_left   = psd_read_int( &tmp2 ); 
        layer->mask_bottom = psd_read_int( &tmp2 ); 
        layer->mask_right  = psd_read_int( &tmp2 ); 
        layer->mask_default_color = psd_read_uchar( &tmp2 ); 
        layer->mask_flags = psd_read_uchar( &tmp2 ); 
      } 
      tmp2 = psd_read_string( &tmp ); 
      if( tmp2.len ) 
      { 
        /* Layer blending ranges data */ 
      } 
      layer->name = psd_read_pstring( &tmp ); 
    } 
  } 
  while(layer->next) 
    layer = layer->next; 
  /* Now process the layer channel data.. */ 
  while(layer) 
  { 
    unsigned int i; 
    for(i=0; i<layer->num_channels; i++) 
    { 
      layer->channel_info[i].data.str= 
        (unsigned char *)psd_read_data(src,layer->channel_info[i].data.len); 
    } 
    layer = layer->prev; 
  } 
} 
 
 
static struct buffer 
packbitsdecode(struct buffer src, 
               struct buffer dst, 
               size_t nbytes) 
{ 
  int n; 
 
  while( nbytes--  ) 
  { 
    n = psd_read_schar( &src ); 
    if (n > 0) 
    { 
      ++n; 
      while(n--) 
      { 
        if(dst.len) 
        { 
          *(dst.str++) = psd_read_uchar( &src ); 
          dst.len--; 
        } else 
          return src; 
      } 
    } else if( n == -128 ){ 
      /* noop */ 
    } else { 
      unsigned char val; 
      n = -n+1; 
      val = psd_read_uchar( &src ); 
      while(n--) 
      { 
        if(dst.len) 
        { 
          *(dst.str++) = val; 
          dst.len--; 
        } else 
          return src; 
      } 
    } 
  } 
  if(dst.len) 
    fprintf(stderr, "%ld bytes left to write! (should be 0)\n", 
            (long)dst.len); 
  return src; 
} 
 
static void f_decode_packbits_encoded(INT32 args) 
{ 
  struct pike_string *src = NULL; 
  int nelems = 0; 
  int width = 0; 
  int multiplier = 1; 
  struct pike_string *dest; 
  int compression = -1; 
  struct buffer b, ob, d; 
  if(TYPEOF(sp[-args]) != T_STRING) 
    Pike_error("Internal argument error.\n"); 
 
  get_all_args(NULL, args, 
               "%T%d%d.%d%d", 
               &src, &nelems, &width, 
               &multiplier, &compression); 
 
  nelems *= multiplier; 
  b.str = (unsigned char *)src->str; 
  b.len = src->len; 
  if (compression < 0) { 
    compression = psd_read_ushort(&b); 
  } 
 
  pop_n_elems(args-1); 
 
  if (nelems < 0 || b.len < (size_t)nelems*2) 
    Pike_error("Not enough space for %d short integers.\n", nelems); 
 
  ob = b; 
  ob.str += nelems*2; 
  ob.len -= nelems*2; 
 
  switch(compression) 
  { 
   case 1: 
     d.len = width * nelems; 
     dest = begin_shared_string( d.len ); 
     d.str = (unsigned char *)dest->str; 
     packbitsdecode( ob, d, d.len ); 
     push_string( end_shared_string( dest ) ); 
     break; 
   case 0: 
     push_string( make_shared_binary_string((char *)b.str,b.len)); 
     break; 
   default: 
     Pike_error("Unsupported compression (%d)!\n", compression); 
  } 
  stack_swap(); 
  pop_stack(); 
  return; 
} 
 
static void f_decode_image_channel( INT32 args ) 
{ 
  INT_TYPE w, h; 
  int y; 
  struct pike_string *s; 
  struct object *io; 
  unsigned char *source; 
  rgb_group *dst; 
  get_all_args( NULL, args, "%i%i%S", &w,&h,&s); 
 
  ref_push_string( s ); 
  push_int( h ); 
  push_int( w ); 
  f_decode_packbits_encoded( 3 ); 
  s = sp[-1].u.string; 
  stack_swap(); 
  pop_stack(); 
  if(s->len < w*h) 
    Pike_error("Not enough data in string for this channel\n"); 
  source = (unsigned char *)s->str; 
  push_int( w ); push_int( h ); 
  io = clone_object( image_program, 2 ); 
  dst = ((struct image*)get_storage(io,image_program))->img; 
  for(y=0; y<w*h; y++) 
  { 
    dst->r = dst->g = dst->b = *(source++); 
    dst++; 
  } 
  pop_n_elems(args); 
  push_object( io ); 
} 
 
 
 
static void f_decode_image_data( INT32 args ) 
{ 
  INT_TYPE w, h, c, d, m; 
  int y; 
  struct pike_string *s, *ct; 
  struct object *io; 
  unsigned char *source, *source2, *source3, *source4; 
  rgb_group *dst; 
  get_all_args( NULL, args, "%i%i%i%i%i%S%S", 
                &w,&h,&d,&m,&c,&s,&ct); 
 
  if(!ct->len) ct = NULL; 
 
  ref_push_string( s ); 
  push_int( h ); 
  push_int( w ); 
  push_int( d ); 
  push_int( c ); 
  f_decode_packbits_encoded( 5 ); 
  s = sp[-1].u.string; 
  stack_swap(); 
  pop_stack(); 
  if(s->len < w*h*d) 
    Pike_error("Not enough data in string for this channel\n"); 
  source = (unsigned char *)s->str; 
  source2 = source+w*h; 
  source3 = source+w*h*2; 
  source4 = source+w*h*3; 
  push_int( w ); push_int( h ); 
  io = clone_object( image_program, 2 ); 
  dst = ((struct image*)get_storage(io,image_program))->img; 
  for(y=0; y<w*h; y++) 
  { 
    switch( d ) 
    { 
     case 4: 
       /* cmyk.. */ 
       dst->r = MAXIMUM(255-(*(source++) + *source4),  0); 
       dst->g = MAXIMUM(255-(*(source2++) + *source4), 0); 
       dst->b = MAXIMUM(255-(*(source3++) + *source4), 0); 
       dst++; source4++; 
       break; 
     case 3: 
       if( m != CMYK ) 
       { 
         dst->r = *(source++); 
         dst->g = *(source2++); 
         (dst++)->b = *(source3++); 
       } else { 
         dst->r = 255-*(source++); 
         dst->g = 255-*(source2++); 
         dst->b = 255-*(source3++); 
       } 
       break; 
     case 2: 
     case 1: 
       if(ct) 
       { 
         dst->r = ct->str[*source]; 
         dst->g = ct->str[*source+256]; 
         dst->b = ct->str[*source+256*2]; 
         source++; 
         dst++; 
       } 
       else 
       { 
         dst->r = dst->g = dst->b = *(source++); 
         dst++; 
       } 
       break; 
    } 
  } 
  pop_n_elems(args); 
  push_object( io ); 
} 
 
 
 
 
static void free_image( struct psd_image *i ) 
{ 
  while(i->first_layer) 
  { 
    struct layer *t = i->first_layer; 
    i->first_layer = t->next; 
    free(t); 
  } 
} 
 
static struct psd_image low_psd_decode( struct buffer *b ) 
{ 
  int *s = (int *)b->str; 
  struct psd_image i; 
  ONERROR err; 
  memset(&i, 0, sizeof(i)); 
  SET_ONERROR( err, free_image, &i ); 
  i.num_channels = psd_read_ushort( b ); 
  i.rows = psd_read_uint( b ); 
  i.columns = psd_read_uint( b ); 
  i.depth = psd_read_ushort( b ); 
  i.mode = psd_read_ushort( b ); 
  i.color_data = psd_read_string( b ); 
  i.resource_data = psd_read_string( b ); i.resource_data.len++; 
  i.layer_data = psd_read_string( b );    /*i.layer_data.len++;*/ 
  i.compression = psd_read_short( b ); 
  i.image_data = *b; 
  decode_layers_and_masks( &i, &i.layer_data ); 
  UNSET_ONERROR( err ); 
  return i; 
} 
 
 
void push_buffer( struct buffer *b ) 
{ 
  push_string( make_shared_binary_string( (char *)b->str, b->len ) ); 
} 
 
void push_layer( struct layer  *l) 
{ 
  unsigned int i; 
  struct svalue *osp = sp; 
  ref_push_string( s_top );           push_int( l->top ); 
  ref_push_string( s_left );          push_int( l->left ); 
  ref_push_string( s_right );         push_int( l->right ); 
  ref_push_string( s_bottom );        push_int( l->bottom ); 
  ref_push_string( s_mask_top );      push_int( l->mask_top ); 
  ref_push_string( s_mask_left );     push_int( l->mask_left ); 
  ref_push_string( s_mask_right );    push_int( l->mask_right ); 
  ref_push_string( s_mask_bottom );   push_int( l->mask_bottom ); 
  ref_push_string( s_mask_flags );    push_int( l->mask_flags ); 
  ref_push_string( s_mask_default_color ); push_int( l->mask_default_color ); 
  ref_push_string( s_opacity );       push_int( l->opacity ); 
  ref_push_string( s_clipping );      push_int( l->clipping ); 
  ref_push_string( s_flags );         push_int( l->flags ); 
  ref_push_string( s_mode );          push_buffer( &l->mode ); 
  ref_push_string( s_extra_data );    push_buffer( &l->extra_data ); 
  ref_push_string( s_name );          push_buffer( &l->name ); 
  ref_push_string( s_channels ); 
  for( i = 0; i<l->num_channels; i++ ) 
  { 
    ref_push_string( s_id ); 
    push_int( l->channel_info[i].id ); 
    ref_push_string( s_data ); 
    push_buffer( &l->channel_info[i].data ); 
    f_aggregate_mapping( 4 ); 
  } 
  f_aggregate( l->num_channels ); 
  f_aggregate_mapping((INT32)(sp - osp)); 
} 
 
 
static void decode_resources( struct buffer *b ); 
void push_psd_image( struct psd_image *i ) 
{ 
  struct svalue *osp = sp, *tsp; 
  struct layer *l; 
  ref_push_string( s_channels ); push_int( i->num_channels ); 
  ref_push_string( s_height ); push_int( i->rows ); 
  ref_push_string( s_width );  push_int( i->columns ); 
  ref_push_string( s_compression ); push_int( i->compression ); 
  ref_push_string( s_depth ); push_int( i->depth ); 
  ref_push_string( s_mode ); push_int( i->mode ); 
  ref_push_string( s_color_data ); push_buffer( &i->color_data ); 
  ref_push_string( s_resources ); decode_resources( &i->resource_data ); 
  ref_push_string( s_image_data ); push_buffer( &i->image_data ); 
  ref_push_string( s_layers ); 
  l = i->first_layer; 
  tsp = sp; 
  while( l ) 
  { 
    push_layer( l ); 
    l = l->next; 
  } 
  f_aggregate((INT32)(sp - tsp)); 
  f_aggregate_mapping((INT32)(sp - osp)); 
} 
 
static void decode_resources( struct buffer *b ) 
{ 
  struct svalue *osp = Pike_sp; 
 
  while( b->len > 11 ) 
  { 
    char  *signature = psd_read_data( b, 4 ); 
 
    int id; 
    struct buffer data; 
    struct buffer name; 
 
    /* Non-photoshop software may use another identifier and 
       still be compatible with the Adobe PSD file format specification. 
       FIXME? */ 
    if( signature[0] != '8' ||  signature[1] != 'B' || 
        signature[2] != 'I' ||  signature[3] != 'M' ) 
      break; 
 
    id = psd_read_short( b ); 
    name = psd_read_pstring( b ); 
    if( !(name.len & 1) ) psd_read_uchar( b ); 
    data = psd_read_string( b ); 
    data.len++; 
 
    if( data.len & 1 ) psd_read_uchar( b ); 
 
    switch( id ) 
    { 
      case 0x03f0: /* caption */ 
        { 
          struct buffer b = psd_read_pstring( &data ); 
          push_static_text( "caption" ); 
          push_buffer( &b ); 
        } 
        break; 
 
      case 0x0400: /* layer state info */ 
        push_static_text( "active_layer" ); 
        push_int( psd_read_short( &data ) ); 
        break; 
      case 0x0408: /* guides */ 
        push_static_text( "guides" ); 
        { 
          int i,num_guides; 
          short magic1, magic2, magic3, magic4, magic5, magic6; /*from gimp.*/ 
          magic1 = psd_read_short( &data ); magic2 = psd_read_short( &data ); 
          magic3 = psd_read_short( &data ); magic4 = psd_read_short( &data ); 
          magic5 = psd_read_short( &data ); magic6 = psd_read_short( &data ); 
          num_guides = psd_read_int( &data ); 
 
          if( data.len != (unsigned)(num_guides * 5) ) 
          { 
            f_aggregate( 0 ); 
            break; 
          } 
          for (i=0; i<num_guides; i++) 
          { 
            ptrdiff_t p = psd_read_int( &data ); 
            int h = psd_read_uchar( &data ); 
            if( h ) 
              p = (ptrdiff_t)((((double)p) * (magic4>>8)) / ((double)(magic4&255))); 
            else 
              p = (ptrdiff_t)((((double)p) * (magic6>>8)) / ((double)(magic6&255))); 
            push_static_text( "pos" );      push_int64( p ); 
            push_static_text( "vertical" ); push_int( !h ); 
            f_aggregate_mapping( 4 ); 
          } 
          f_aggregate( num_guides ); 
        } 
        break; 
      case 0x040b: /* URL */ 
        { 
          struct buffer b = psd_read_pstring( &data ); 
          push_static_text( "url" ); 
          push_buffer( &b ); 
        } 
        break; 
      case 0x03ed: /* res. info. */ 
        push_static_text( "resinfo" ); 
 
        push_static_text( "hres" );       push_int(psd_read_int( &data ) ); 
        push_static_text( "hres_unit" );  push_int(psd_read_short( &data ) ); 
        push_static_text( "width_unit" ); push_int(psd_read_short( &data ) ); 
 
        push_static_text( "vres" );       push_int(psd_read_int( &data ) ); 
        push_static_text( "vres_unit" );  push_int(psd_read_short( &data ) ); 
        push_static_text( "height_unit" );push_int(psd_read_short( &data ) ); 
 
        f_aggregate_mapping( 12 ); 
        break; 
 
      case 0x03fc: /* Declared completely obsolete by Adobe */ 
      case 0x03ff: 
      case 0x0403: 
        break; 
 
      case 0x03e8: /* Photoshop 2.0. Obsolete! */ 
        /* 5*int16  channels, rows, columns, depth, mode */ 
 
      case 0x03e9: /* Machintosh print manager print info record. */ 
 
      case 0x03eb: /* Photoshop 2.0. Obsolete! Indexed color table */ 
 
      case 0x03ee: 
        /* Names of alpha channels as a series of pascal strings */ 
 
      case 0x03ef: /* DisplayInfo structure */ 
 
      case 0x03f1: /* Border information */ 
        /* Contains a fixed-number for the border width and an int16 for the 
           border units (1=inches, 2=cm, 3=points, 4=picas, 5=columns) */ 
 
      case 0x03f2: /* Background color. */ 
 
      case 0x03f3: /* Print flags */ 
        /* A series of one byte boolean values for 
           labels, crop marks, color bars, registration marks, negative, flip 
           interpolate and caption */ 
 
      case 0x03f4: /* Grayscale and multichannel halftoning information. */ 
      case 0x03f5: /* Color halftoning information. */ 
      case 0x03f6: /* Duotone halftoning information. */ 
      case 0x03f7: /* Grayscale and multichannel transfer functions. */ 
      case 0x03f8: /* Color transfer functions. */ 
      case 0x03f9: /* Duotone transfer functions. */ 
      case 0x03fa: /* Duotone image information. */ 
      case 0x03fb: /* Two bytes for the effective black and white values 
                      for the dot range. */ 
      case 0x03fd: /* EPS options */ 
      case 0x03fe: /* Quick Mask information. 2 bytes containing Quick Mask 
                      channel ID. One byte boolean indicating whether the 
                      mask was initially empty. */ 
      case 0x0401: /* Working path */ 
      case 0x0402: /* Layers group information. 2 bytes per layer containing a 
                      group ID for the dragging groups. */ 
      case 0x0404: /* IPTC-NAA record */ 
      case 0x0405: /* Image mode for raw format files. */ 
      case 0x0406: /* JPEG quality */ 
      case 0x040A: /* Copyright flag */ 
      case 0x0bb7: /* Name of clipping path */ 
      case 0x2710: /* Print flags information . 
                      2 bytes version (=1), 1 byte center crop marks, 
                      1 zeroed byte, 4 bytes bleed width value, 
                      2 bytes bleed width scale. */ 
      default: 
        if ((id >= 0x07d0) && (id <= 0x0bb6)) { 
          /* Path information */ 
        } 
        push_int( id ); 
        push_buffer( &data ); 
        break; 
    } 
  } 
  f_aggregate_mapping( (int)(sp-osp) ); 
} 
 
static void image_f_psd___decode( INT32 args ) 
{ 
  struct pike_string *s; 
  struct buffer b; 
  get_all_args( NULL, args, "%S", &s ); 
  if(args > 1) 
    pop_n_elems( args-1 ); 
  if(s->len < 26+4+4+4 ) /* header+color mode+image res+layers */ 
    Pike_error("This is not a Photoshop PSD file (too short)\n"); 
  if(s->str[0] != '8' || s->str[1] != 'B' 
     || s->str[2] != 'P'  || s->str[3] != 'S' ) 
    Pike_error("This is not a Photoshop PSD file (invalid signature)\n"); 
  if(s->str[4] || s->str[5] != 1) 
    Pike_error("This is not a Photoshop PSD file (invalid version)\n"); 
 
  b.len = s->len-12; 
  b.str = (unsigned char *)s->str+12; 
  { 
    ONERROR onerr; 
    struct psd_image i = low_psd_decode( &b ); 
    SET_ONERROR( onerr, free_image, &i ); 
    push_psd_image( &i ); 
    UNSET_ONERROR( onerr ); 
    free_image( &i ); 
    stack_swap(); 
    pop_stack(); 
  } 
} 
 
static void f_apply_cmap( INT32 args ) 
{ 
  struct object *io; 
  struct image *i; 
  rgb_group *d; 
  struct pike_string *cmap; 
  int n; 
  get_all_args( NULL, args, "%o%S", &io, &cmap ); 
  if(cmap->len < 256*3) 
    Pike_error("Invalid colormap resource\n"); 
  if(!(i = get_storage( io, image_program ))) 
    Pike_error("Invalid image object\n"); 
  n = i->xsize * i->ysize; 
  d = i->img; 
  THREADS_ALLOW(); 
  while(n--) 
  { 
    int i = d->g; 
    d->r = cmap->str[i]; 
    d->g = cmap->str[i+256]; 
    d->b = cmap->str[i+512]; 
  } 
  THREADS_DISALLOW(); 
} 
 
static struct program *image_encoding_psd_program=NULL; 
void init_image_psd(void) 
{ 
  ADD_FUNCTION( "___decode", image_f_psd___decode, tFunc(tStr,tMapping), 0); 
  ADD_FUNCTION( "___decode_image_channel", f_decode_image_channel,tFunction,0); 
  ADD_FUNCTION( "___decode_image_data", f_decode_image_data,tFunction,0); 
  ADD_FUNCTION( "__apply_cmap", f_apply_cmap, tFunc(tObj tStr,tVoid), 0); 
 
  add_integer_constant("Bitmap" , Bitmap, 0 ); 
  add_integer_constant("Greyscale" , Greyscale, 0 ); 
  add_integer_constant("Indexed" , Indexed, 0 ); 
  add_integer_constant("RGB" , RGB, 0 ); 
  add_integer_constant("CMYK" , CMYK, 0 ); 
  add_integer_constant("Multichannel" , Multichannel, 0 ); 
  add_integer_constant("Duotone" , Duotone, 0 ); 
  add_integer_constant("Lab" , Lab, 0 ); 
 
  add_integer_constant("LAYER_FLAG_PRESERVE_TRANSPARENCY", 0x01, 0 ); 
  add_integer_constant("LAYER_FLAG_INVISIBLE", 0x02, 0 ); 
  add_integer_constant("LAYER_FLAG_BIT4", 0x04, 0 ); 
  add_integer_constant("LAYER_FLAG_NOPIX", 0x08, 0 ); 
 
#define STRING(X) PIKE_CONCAT(s_, X) = \ 
  make_shared_binary_string(#X,sizeof( #X )-sizeof("")) 
#include "psd_constant_strings.h" 
#undef STRING 
} 
 
 
void exit_image_psd(void) 
{ 
#define STRING(X) free_string(PIKE_CONCAT(s_, X)) 
#include "psd_constant_strings.h" 
#undef STRING 
}