Class: SmartImage::JavaCanvas
- Inherits:
-
BaseCanvas
- Object
- BaseCanvas
- SmartImage::JavaCanvas
- Defined in:
- lib/smart_image/java_canvas.rb
Overview
Canvas object backed by Java Graphics2D
Instance Method Summary collapse
-
#alpha_mask(image_data, options = {}) ⇒ Object
Load the given file as an alpha mask for the image.
-
#composite(image_data, options = {}) ⇒ Object
Composite the given image data onto the canvas.
-
#destroy ⇒ Object
Stub out destroy since Java actually garbage collects crap, unlike…
-
#encode(format, options = {}) ⇒ Object
Encode the image to the given format.
-
#initialize(width, height) ⇒ JavaCanvas
constructor
A new instance of JavaCanvas.
Methods inherited from BaseCanvas
Constructor Details
#initialize(width, height) ⇒ JavaCanvas
Returns a new instance of JavaCanvas.
12 13 14 15 |
# File 'lib/smart_image/java_canvas.rb', line 12 def initialize(width, height) @width, @height = width, height @canvas = BufferedImage.new width, height, BufferedImage::TYPE_INT_ARGB end |
Instance Method Details
#alpha_mask(image_data, options = {}) ⇒ Object
Load the given file as an alpha mask for the image
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 |
# File 'lib/smart_image/java_canvas.rb', line 40 def alpha_mask(image_data, = {}) input_stream = ByteArrayInputStream.new image_data.to_java_bytes mask = ImageIO.read input_stream width = mask.width image_data, mask_data = Java::int[width].new, Java::int[width].new mask.height.times do |y| # fetch a line of data from each image @canvas.get_rgb 0, y, width, 1, image_data, 0, 1 mask.get_rgb 0, y, width, 1, mask_data, 0, 1 width.times do |x| # mask away the alpha color = image_data[x] & 0x00FFFFFF # turn red from the mask into alpha alpha = (mask_data[x] & 0x00FF0000) << 8 image_data[x] = color | alpha end @canvas.set_rgb 0, y, width, 1, image_data, 0, 1 end end |
#composite(image_data, options = {}) ⇒ Object
Composite the given image data onto the canvas
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/smart_image/java_canvas.rb', line 22 def composite(image_data, = {}) info = SmartImage.info image_data opts = { :width => info.width, :height => info.height, :x => 0, :y => 0 }.merge() input_stream = ByteArrayInputStream.new image_data.to_java_bytes image = ImageIO.read input_stream raise FormatError, "invalid image" unless image graphics = @canvas.graphics graphics.draw_image image, opts[:x], opts[:y], opts[:width], opts[:height], nil end |
#destroy ⇒ Object
Stub out destroy since Java actually garbage collects crap, unlike… C
18 19 |
# File 'lib/smart_image/java_canvas.rb', line 18 def destroy end |
#encode(format, options = {}) ⇒ Object
Encode the image to the given format
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/smart_image/java_canvas.rb', line 67 def encode(format, = {}) case format when 'jpg' # If we're trying to save an JPEG, we need to convert it to RGB canvas = BufferedImage.new @width, @height, BufferedImage::TYPE_INT_RGB graphics = canvas.graphics graphics.draw_image @canvas, 0, 0, @width, @height, nil else canvas = @canvas end output_stream = ByteArrayOutputStream.new ImageIO.write(canvas, format, output_stream) String.from_java_bytes output_stream.to_byte_array end |