Class: PSD::Image

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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'
].freeze

Instance Attribute Summary collapse

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.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/psd/image.rb', line 35

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

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

  calculate_length
  set_channels_info

  @channel_data = []
  @pixel_data = []
  @opacity = 1.0
  @has_mask = false

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

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

Instance Attribute Details

#has_maskObject (readonly) Also known as: has_mask?

Returns the value of attribute has_mask.



19
20
21
# File 'lib/psd/image.rb', line 19

def has_mask
  @has_mask
end

#opacityObject (readonly)

Returns the value of attribute opacity.



19
20
21
# File 'lib/psd/image.rb', line 19

def opacity
  @opacity
end

#pixel_dataObject (readonly)

Returns the value of attribute pixel_data.



19
20
21
# File 'lib/psd/image.rb', line 19

def pixel_data
  @pixel_data
end

Instance Method Details

#parseObject

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/psd/image.rb', line 58

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