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
- #mask_to_png ⇒ Object
-
#save_as_png(file) ⇒ Object
Saves the PNG data to disk.
-
#to_png ⇒ Object
(also: #export)
Load the image pixels into a PNG file and return a reference to the data.
- #to_png_with_mask ⇒ Object
Instance Method Details
#mask_to_png ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/psd/image_exports/png.rb', line 74 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.
91 92 93 |
# File 'lib/psd/image_exports/png.rb', line 91 def save_as_png(file) to_png.save(file, :fast_rgba) end |
#to_png ⇒ Object 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_mask ⇒ Object
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 |
# 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| offset_x = x + @layer.left offset_y = y + @layer.top i +=1 and next if offset_x < 0 || offset_y < 0 || offset_x >= png.width || offset_y >= png.height png[offset_x, offset_y] = @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 i += 1 and next if offset_x < 0 || offset_y < 0 || offset_x >= png.width || offset_y >= png.height color = ChunkyPNG::Color.to_truecolor_alpha_bytes(png[offset_x, offset_y]) color[3] = color[3] * @mask_data[i] / 255 png[offset_x, offset_y] = ChunkyPNG::Color.rgba(*color) i += 1 end end crop_left = PSD::Util.clamp(@layer.left, 0, png.width) crop_top = PSD::Util.clamp(@layer.top, 0, png.height) crop_width = PSD::Util.clamp(@layer.width.to_i, 0, png.width - crop_left) crop_height = PSD::Util.clamp(@layer.height.to_i, 0, png.height - crop_top) png.crop!(crop_left, crop_top, crop_width, crop_height) end |