Module: PSD::Image::Export::PNG

Included in:
PSD::Image, Node::BuildPreview
Defined in:
lib/psd/image_exports/png.rb

Overview

PNG image export. This is the default export format.

Instance Method Summary collapse

Instance Method Details

#mask_to_pngObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/psd/image_exports/png.rb', line 62

def mask_to_png
  return unless has_mask?

  png = ChunkyPNG::Canvas.new(@layer.mask.width.to_i, @layer.mask.height.to_i, ChunkyPNG::Color::TRANSPARENT)

  i = 0
  @layer.mask.height.times do |y|
    @layer.mask.width.times do |x|
      png[x, y] = ChunkyPNG::Color.grayscale(@mask_data[i])
      i += 1
    end
  end

  png
end

#save_as_png(file) ⇒ Object

Saves the PNG data to disk.



79
80
81
# File 'lib/psd/image_exports/png.rb', line 79

def save_as_png(file)
  to_png.save(file, :fast_rgba)
end

#to_pngObject Also known as: export

Load the image pixels into a PNG file and return a reference to the data.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/psd/image_exports/png.rb', line 9

def to_png
  PSD.logger.debug "Beginning PNG export"
  png = ChunkyPNG::Canvas.new(width.to_i, height.to_i, ChunkyPNG::Color::TRANSPARENT)

  i = 0
  height.times do |y|
    width.times do |x|
      png[x,y] = @pixel_data[i]
      i += 1
    end
  end

  png
end

#to_png_with_maskObject



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
# File 'lib/psd/image_exports/png.rb', line 25

def to_png_with_mask
  return to_png unless has_mask?

  PSD.logger.debug "Beginning PNG export with mask"
  
  # We generate the preview at the document size instead to make applying the mask
  # significantly easier.
  width = @layer.header.width.to_i
  height = @layer.header.height.to_i
  png = ChunkyPNG::Canvas.new(width, height, ChunkyPNG::Color::TRANSPARENT)

  i = 0
  @layer.height.times do |y|
    @layer.width.times do |x|
      png[x + @layer.left, y + @layer.top] = @pixel_data[i]
      i += 1
    end
  end
  
  # Now we apply the mask
  i = 0
  @layer.mask.height.times do |y|
    @layer.mask.width.times do |x|
      offset_x = @layer.mask.left + x
      offset_y = @layer.mask.top + y

      color = ChunkyPNG::Color.to_truecolor_alpha_bytes(png.get_pixel(offset_x, offset_y))
      color[3] = color[3] * @mask_data[i] / 255

      png.set_pixel(offset_x, offset_y, ChunkyPNG::Color.rgba(*color))
      i += 1
    end
  end

  png.crop!(@layer.left, @layer.top, @layer.width.to_i, @layer.height.to_i)
end