Class: PSD::Image

Inherits:
Object
  • Object
show all
Includes:
Export::PNG, PSD::ImageFormat::RAW, PSD::ImageFormat::RLE, PSD::ImageMode::CMYK, PSD::ImageMode::Greyscale, PSD::ImageMode::RGB
Defined in:
lib/psd/image.rb,
lib/psd/image_exports/png.rb

Overview

Parses the full preview image at the end of the PSD document.

Direct Known Subclasses

ChannelImage

Defined Under Namespace

Modules: Export

Constant Summary collapse

COMPRESSIONS =

All of the possible compression formats Photoshop uses.

[
  'Raw',
  'RLE',
  'ZIP',
  'ZIPPrediction'
]

Instance Method Summary collapse

Methods included from Export::PNG

#save_as_png, #to_png

Constructor Details

#initialize(file, header) ⇒ Image

Store a reference to the file and the header. We also do a few simple calculations to figure out the number of pixels in the image and the length of each channel.



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
# File 'lib/psd/image.rb', line 21

def initialize(file, header)
  @file = file
  @header = header

  @num_pixels = width * height
  @num_pixels *= 2 if depth == 16

  calculate_length
  @channel_data = {} # Using a Hash over an NArray, because NArray has problems w/ Ruby 2.0. Hashes are faster than Arrays

  @start_pos = @file.tell
  @end_pos = @start_pos + @length

  @pixel_data = []

  PSD.logger.debug "Image: #{width}x#{height}, length = #{@length}, mode = #{@header.mode_name}, position = #{@start_pos}"

  # Each color channel is represented by a unique ID
  @channels_info = [
    {id: 0},
    {id: 1},
    {id: 2},
    {id: -1}
  ]
end

Instance Method Details

#parseObject

Begins parsing the image by first figuring out the compression format used, and then by reading the image data.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/psd/image.rb', line 49

def parse
  @compression = parse_compression!

  # ZIP not implemented
  if [2, 3].include?(@compression)
    PSD.logger.debug "Warning: ZIP image compression not supported yet. Skipping."
    @file.seek @end_pos and return
  end

  PSD.logger.debug "Compression: id = #{@compression}, name = #{COMPRESSIONS[@compression]}"

  parse_image_data!

  return self
end