This file is indexed.

/usr/lib/pike7.8/modules/_Image_PSD.pmod is in pike7.8-image 7.8.866-5build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
#pike __REAL_VERSION__

/* Note: I'm following the convention from Image.XCF here to not
   document all optional arguments. Makes the functions look easier to
   use to the casual Pike programmer, so it might be a good idea, but
   I'm not sure. /Zino */

//! @appears Image.PSD

//! @ignore
 inherit Image._PSD;
//! @endignore

class Layer
{
  string mode, name;
  int opacity;
  object image;
  object alpha;
  
  int flags;
  int xoffset, yoffset;
  int width, height;

  int mask_flags;
  int mask_xoffset, mask_yoffset;
  int mask_width, mask_height;
  int mask_default_color;
}


Layer decode_layer(mapping layer, mapping i)
{
//   int stt = gethrtime();
  Layer l = Layer();
  int use_cmap;
  l->opacity = layer->opacity;
  l->width = layer->right-layer->left;
  l->height = layer->bottom-layer->top;
  l->xoffset = layer->left;
  l->yoffset = layer->top;
  l->image = Image.Image( l->width, l->height );
  l->mode = layer->mode;
  l->flags = layer->flags;
  l->name = layer->name;
  l->mask_flags = layer->mask_flags;
  l->mask_default_color = layer->mask_default_color;
 
  l->mask_width = layer->mask_right-layer->mask_left;
  l->mask_height = layer->mask_bottom-layer->mask_top;
  l->mask_xoffset = layer->mask_left;
  l->mask_yoffset = layer->mask_top;

  if(l->mask_flags & 1) // pos relative to layer
  {
    l->mask_xoffset += l->xoffset;
    l->mask_yoffset += l->yoffset;
  }
  array colors;
  int inverted;

  switch(i->mode)
  {
   case RGB:
     array lays = ({});
     foreach( layer->channels, mapping c )
     {
       string mode;
       switch( (int)c->id )
       {
        case 0:
          mode = "red"; 
          break;
        case 1:
          mode = "green"; 
          break;
        case 2:
          mode = "blue"; 
          break;
       }
       if( mode )
       {
//          int st = gethrtime();
         if( !sizeof(lays) )
           lays += ({ 
             Image.Layer(___decode_image_channel(l->width,
						 l->height,
                                                 c->data))
           });
         else
           lays += (({ Image.Layer( ([
             "image":___decode_image_channel(l->width, l->height, c->data),
//             "alpha_value":1.0,
             "mode":mode, 
	   ]) )
           }));
//          werror(mode+" took %4.5f seconds\n", (gethrtime()-st)/1000000.0 );
         c->data = 0;
       }
     }
//      int st = gethrtime();
     l->image = Image.lay( lays )->image();
//      werror("combine took %4.5f seconds\n", (gethrtime()-st)/1000000.0 );
     break;
    case CMYK:
     inverted = 1;
     colors = ({ ({255,0,0,}),
                 ({0,255,0,}),
                 ({0,0,255,}),
               }) + ({ 255,255,255 }) * 24;
     l->image = Image.Image( l->width, l->height, 255, 255, 255);
     break;

   case Indexed:
     use_cmap = 1;
     break;
   default:
     werror("Unsupported layer format mode ("+i->mode+"), using greyscale\n");
   case Greyscale:
     colors = ({
       255,255,255
     })*24;
     break;
  }
//   int st = gethrtime();
  foreach(layer->channels, mapping c)
  {
    object tmp;

    if( !colors && (c->id >= 0 ))
      continue;

    if( c->id != -2)
      tmp = ___decode_image_channel(l->width, l->height, c->data);
    else
      tmp = ___decode_image_channel(l->mask_width,l->mask_height,c->data);

    switch( c->id )
    {
     default:
       if(!use_cmap)
       {
         if(inverted)
           l->image -= tmp*colors[c->id%sizeof(colors)];
         else
           l->image += tmp*colors[c->id%sizeof(colors)];
       } else {
         __apply_cmap( tmp, i->color_data );
         l->image = tmp;
       }
       break;
     case -1: /* alpha */
       if(!l->alpha)
         l->alpha = tmp;
       else
         l->alpha *= tmp;
       break;
     case -2: /* user mask */
       if(!(l->mask_flags & 2)) /* layer mask disabled */
       {
	 array pad_color = ({ l->mask_default_color }) * 3;
	 int x0 = l->xoffset - l->mask_xoffset;
	 int y0 = l->yoffset - l->mask_yoffset;
	 tmp = tmp->copy(x0, y0,
			 x0 + l->image->xsize() - 1,
			 y0 + l->image->ysize() - 1,
			 @pad_color);
	 
         if(l->mask_flags & 4) /* invert mask */
           tmp = tmp->invert();
	 
         if(!l->alpha)
           l->alpha = tmp;
         else
           l->alpha *= tmp;
         break;
       }
    }
    c->data = 0;
  }
//   werror(" mode %s image %O alpha %O\n",
// 	 l->mode, l->image, l->alpha );
//   werror("alpha/mask took %4.5f seconds\n", (gethrtime()-st)/1000000.0 );
//   werror("TOTAL took %4.5f seconds\n\n", (gethrtime()-stt)/1000000.0 );
  return l;
}

//! @decl mapping __decode(string|mapping data)
//! 
//! Decodes a PSD image to a mapping, defined as follows.
//!
//! @mapping
//!   @member int(1..24) "channels"
//!     The number of channels in the image, including any alpha channels.
//!   @member int(1..30000) "height"
//!   @member int(1..30000) "width"
//!     The image dimensions.
//!   @member int(0..1) "compression"
//!     1 if the image is compressed, 0 if not.
//!   @member int(1..1)|int(8..8)|int(16..16) "depth"
//!     The number of bits per channel.
//!   @member int(0..4)|int(7..9) "mode"
//!     The color mode of the file.
//!     @int
//!       @value 0
//!         Bitmap
//!       @value 1
//!         Greyscale
//!       @value 2
//!         Indexed
//!       @value 3
//!         RGB
//!       @value 4
//!         CMYK
//!       @value 7
//!         Multichannel
//!       @value 8
//!         Duotone
//!       @value 9
//!         Lab
//!     @endint
//!   @member string "color_data"
//!     Raw color data.
//!   @member string "image_data"
//!     Ram image data.
//!   @member  mapping(string|int:mixed) "resources"
//!     Additional image data. Se mappping below.
//!   @member array(mapping) "layers"
//!     An array with the layers of the image. See mapping below.
//! @endmapping
//!
//! The resources mapping. Unknown resources will be identified
//! by their ID number (as an int).
//! @mapping
//!   @member string "caption"
//!     Image caption.
//!   @member string "url"
//!     Image associated URL.
//!   @member int "active_layer"
//!     Which layer is active.
//!   @member array(mapping(string:int)) "guides"
//!     An array with all guides stored in the image file.
//!     @mapping
//!       @member int "pos"
//!         The position of the guide.
//!       @member int(0..1) "vertical"
//!         1 if the guide is vertical, 0 if it is horizontal.
//!     @endmapping
//!   @member mapping(string:int) "resinof"
//!     Resolution information
//!     @mapping
//!       @member int "hres"
//!       @member int "hres_unit"
//!       @member int "width_unit"
//!       @member int "vres"
//!       @member int "vres_unit"
//!       @member int "height_unit"
//!         FIXME: Document these.
//!     @endmapping
//! @endmapping
//!
//! The layer mapping:
//! @mapping
//!   @member int "top"
//!   @member int "left"
//!   @member int "right"
//!   @member int "bottom"
//!     The rectangle containing the contents of the layer.
//!   @member int "mask_top"
//!   @member int "mask_left"
//!   @member int "mask_right"
//!   @member int "mask_bottom"
//!   @member int "mask_flags"
//!     FIXME: Document these
//!   @member int(0..255) "opacity"
//!     0=transparent, 255=opaque.
//!   @member int "clipping"
//!     0=base, 1=non-base.
//!   @member int "flags"
//!     bit 0=transparency protected
//!     bit 1=visible
//!   @member string "mode"
//!     Blend mode.
//!     @string
//!       @value "norm"
//!         Normal
//!       @value "dark"
//!         Darken
//!       @value "lite"
//!         Lighten
//!       @value "hue "
//!         Hue
//!       @value "sat "
//!         Saturation
//!       @value "colr"
//!         Color
//!       @value "lum "
//!         Luminosity
//!       @value "mul "
//!         Multiply
//!       @value "scrn"
//!         Screen
//!       @value "diss"
//!         Dissolve
//!       @value "over"
//!         Overlay
//!       @value "hLit"
//!         Hard light
//!       @value "sLit"
//!         Soft light
//!       @value "diff"
//!         Difference
//!     @endstring
//!   @member string "extra_data"
//!     Raw extra data.
//!   @member string "name"
//!     The name of the layer
//!   @member array(mapping(string:int|string)) "channels"
//!     The channels of the layer. Each array element is a
//!     mapping as follows
//!     @mapping
//!       @member int "id"
//!         The ID of the channel
//!         @int
//!           @value -2
//!             User supplied layer mask
//!           @value -1
//!             Transparency mask
//!           @value 0
//!             Red
//!           @value 1
//!             Green
//!           @value 2
//!             Blue
//!         @endint
//!       @member string "data"
//!         The image data
//!     @endmapping
//! @endmapping

mapping __decode( mapping|string what, mapping|void options )
{
  mapping data;
  if(mappingp(what))
    data = what;
  else 
    data = ___decode( what );
  what=0;
  array rl = ({});
  foreach( data->layers, mapping l )
    rl += ({ decode_layer( l, data ) });

  data->layers = rl;
  return data;
}


array(object) decode_background( mapping data )
{
  object img;

  if( data->image_data )
    img = ___decode_image_data(data->width,       data->height, 
                               data->channels,    data->mode,
                               data->compression, data->image_data,

                               data->color_data);
  return ({ img, 0 });
}

string translate_mode( string mode )
{
  switch( mode )
  {
   case "norm":      return "normal";
   case "mul ":      return "multiply";
   case "add ":      return "add";
   case "diff":      return "difference";
   case "sub ":      return "subtract";
   case "diss":      return "dissolve";
   case "scrn":      return "screen";
   case "over":      return "overlay";
   case "dark":      return "min";
   case "lite":      return "max";
   case "hue ":      return "hue";
   case "div ":      return "idivide";
   case "hLit":      return "hardlight";

   case "colr":      return "color"; // Not 100% like photoshop.
   case "lum ":      return "value"; // Not 100% like photoshop.
   case "sat ":      return "saturation";// Not 100% like photoshop.

      // Colorburn, not really multiply, but the best aproximation soo far.
    case "idiv":
     return "multiply";

     // Exclusion. Not really difference, but very close.
    case "smud":     
     return "difference";

     // Soft light. Not really supported yet. For now, use hardlight with lower
     // opacity. Gives a rather good aproximation.
    case "sLit":     
     return "hardlight";

   default:
     werror("WARNING: PSD: Unsupported mode: "+mode+". Skipping layer\n");
     return 0;
  }
}

//! @decl array(Image.Layer) decode_layers( string data, mapping|void options )
//!
//! Decodes a PSD image to an array of Image.Layer objects
//!
//! Takes the same aptions mapping as @[_decode], note especially 
//! "draw_all_layers":1, but implements "crop_to_bounds" which preserves
//! the bounding box for the whole image (i.e. discards data that extend
//! outside of the global bounds).
//!
//! The layer object have the following extra variables (to be queried
//! using @[Image.Layer()->get_misc_value]):
//!
//! @string
//!   @value "image_guides"
//!     Returns array containing guide definitions.
//!   @value "name"
//!     Returns string containing the name of the layer.
//!   @value "visible"
//!     Is 1 of the layer is visible and 0 if it is hidden.
//! @endstring
array decode_layers( string|mapping what, mapping|void opts )
{
  if(!opts) opts = ([]);

  if(!mappingp( what ) )
    what = __decode( what );
  
  mapping lopts = ([ "tiled":1, ]);

  if( opts->background )
  {
    lopts->image = Image.Image( 32, 32, opts->background );
    lopts->alpha = Image.Image( 32, 32, Image.Color.white );
    lopts->alpha_value = 1.0;
  }

  object img, alpha;
  if( !what->layers || !sizeof(what->layers))
  {
    [ img, alpha ] = decode_background( what );
    if( img )
    {
      lopts->image = img;
      if( alpha )
        lopts->alpha = alpha;
      else
        lopts->alpha = 0;
      lopts->alpha_value = 1.0;
    }
  }
  array layers;
  Image.Layer lay;
  if( lopts->image )
  {
    layers = ({ (lay=Image.Layer( lopts )) });
    lay->set_misc_value( "visible", 1 );
    lay->set_misc_value( "name",    "Background" );
    lay->set_misc_value( "image_guides", what->resources->guides );
  }
  else
    layers = ({});

  foreach(reverse(what->layers), object l)
  {
    if( !(l->flags & LAYER_FLAG_INVISIBLE) || opts->draw_all_layers )
    {
      if( string m = translate_mode( l->mode ) )
      {
	lay = Image.Layer( l->image, l->alpha, m );

        lay->set_misc_value( "visible", !(l->flags & LAYER_FLAG_INVISIBLE) );
        lay->set_misc_value( "name",      l->name );
        lay->set_misc_value( "image_guides", what->resources->guides );
	if( l->mode == "sLit" )
	  l->opacity /= 3;

	l->image = 0; l->alpha = 0;
        
	if( l->opacity != 255 )
        {
	  float lo =  l->opacity / 255.0;
          if( lay->alpha() )
            lay->set_image( lay->image(), lay->alpha()*lo );
          else
            lay->set_image( lay->image(), Image.Image( lay->xsize(),
                                                       lay->yszize(),
                                                       (int)(255*lo),
                                                       (int)(255*lo),
                                                       (int)(255*lo)));
        }

	if (opts->crop_to_bounds && lay->image()) {
	  //  Crop/expand this layer so it matches the image bounds.
	  //  This will lose data which extends beyond the image bounds
	  //  but keeps the image dimensions consistent.
	  int x0 = -l->xoffset, y0 = -l->yoffset;
	  int x1 = x0 + what->width - 1, y1 = y0 + what->height - 1;
	  Image.Image new_img =
	    lay->image()->copy(x0, y0, x1, y1);
	  Image.Image new_alpha =
	    lay->alpha() &&
	    lay->alpha()->copy(x0, y0, x1, y1);
	  lay->set_image(new_img, new_alpha);
	} else
	  lay->set_offset( l->xoffset, l->yoffset );

	layers += ({ lay });
      }
    }
  }
//   werror("%O\n", layers );
  layers->set_misc_value( "size", ({ what->width, what->height }) );
  return layers;
}

//! @decl mapping _decode(string|mapping data, mapping|void options)
//!
//! Decodes a PSD image to a mapping, with at least an
//! 'image' and possibly an 'alpha' object. Data is either a PSD image, or
//! a mapping (as received from @[__decode])
//!
//! @param options
//! @mapping
//!   @member array(int)|Image.Color "background"
//!     Sets the background to the given color. Arrays should be in
//!     the format ({r,g,b}).
//!
//!   @member int(0..1) "draw_all_layers"
//!     Draw invisible layers as well.
//!
//!   @member int(0..1) "draw_guides"
//!     Draw the guides.
//!
//!   @member int(0..1) "draw_selection"
//!     Mark the selection using an overlay.
//!
//!   @member int(0..1) "ignore_unknown_layer_modes"
//!     Do not asume 'Normal' for unknown layer modes.
//!
//!   @member int(0..1) "mark_layers"
//!     Draw an outline around all (drawn) layers.
//!
//!   @member Image.Font "mark_layer_names"
//!     Write the name of all layers using the font object,
//!
//!   @member int(0..1) "mark_active_layer"
//!     Draw an outline around the active layer
//! @endmapping
//!
//! @returns
//! @mapping
//!   @member Image.Image "image"
//!     The image object.
//!   @member Image.Image "alpha"
//!     The alpha channel image object.
//! @endmapping
//!
//! @note
//!   Throws upon error in data. For more information, see @[__decode]
mapping _decode( string|mapping what, mapping|void opts )
{
  mapping data;
  if(!opts) opts = ([]);
  if(mappingp(what))
    data = what;
  else 
    data = __decode( what );
  what=0;

  Image.Layer res = Image.lay(decode_layers( data, opts ),
                              0,0,data->width,data->height );
  Image.Image img = res->image();
  Image.Image alpha = res->alpha();

  return 
  ([
    "image":img,
    "alpha":alpha,
    "type":"application/x-photoshop",
  ]);
}

//! @decl Image.Image decode(string data)
//!   Decodes a PSD image to a single image object.
//!
//! @note
//!   Throws upon error in data. To get access to more information like
//!   alpha channels and layer names, see @[_decode] and @[__decode].
Image.Image decode( string|mapping what, mapping|void opts )
{
  mapping data;
  if(!opts) opts = ([]);
  if(mappingp(what))
    data = what;
  else 
    data = __decode( what );
  what=0;

  Image.Layer res = Image.lay(decode_layers( data, opts ),
                              0,0,data->width,data->height );
  return res->image();
}