lib/image_voodoo/awt.rb in image_voodoo-0.8.7 vs lib/image_voodoo/awt.rb in image_voodoo-0.8.8

- old
+ new

@@ -18,14 +18,10 @@ java_import javax.imageio.ImageWriteParam java_import javax.imageio.stream.FileImageOutputStream java_import javax.swing.JFrame java_import javax.imageio.IIOException - require 'CMYKDemo.jar' - java_import org.monte.media.jpeg.CMYKJPEGImageReader - java_import org.monte.media.jpeg.CMYKJPEGImageReaderSpi - # FIXME: Move and rewrite in terms of new shape ## # # *AWT* (experimental) Add a border to the image and yield/return a new # image. The following options are supported: @@ -103,11 +99,11 @@ # *AWT* paint/render to the source # def paint(src=dup_src) yield src.graphics src.graphics.dispose - ImageVoodoo.new(src, @format) + ImageVoodoo.new(@io, src, @format) end ## # # TODO: Figure out how to determine whether source has alpha or not @@ -130,11 +126,11 @@ ## # *AWT* Create an image of width x height filled with a single color. # def self.canvas(width, height, rgb='000000') - image = ImageVoodoo.new(BufferedImage.new(width, height, ARGB)) + image = ImageVoodoo.new(@io, BufferedImage.new(width, height, ARGB)) image.rect(0, 0, width, height, rgb) end private @@ -145,19 +141,22 @@ SCALE_SMOOTH = java.awt.Image::SCALE_SMOOTH def self.with_image_impl(file) format = detect_format_from_input(file) buffered_image = read_image_from_input(file) - buffered_image ? ImageVoodoo.new(buffered_image, format) : nil + buffered_image ? ImageVoodoo.new(file, buffered_image, format) : nil end def self.read_image_from_input(input) - ImageIO.read(input) - rescue IIOException - cmyk_reader = CMYKJPEGImageReader.new(CMYKJPEGImageReaderSpi.new) - cmyk_reader.setInput(ImageIO.createImageInputStream(input)) - cmyk_reader.read(0) + ImageIO.read(input) + rescue IIOException + require 'CMYKDemo.jar' + + cmyk_spi = org.monte.media.jpeg.CMYKJPEGImageReaderSpi.new + cmyk_reader = org.monte.media.jpeg.CMYKJPEGImageReader.new cmyk_spi + cmyk_reader.input = ImageIO.createImageInputStream(input) + cmyk_reader.read 0 end def self.detect_format_from_input(input) stream = ImageIO.createImageInputStream(input) readers = ImageIO.getImageReaders(stream) @@ -166,11 +165,13 @@ def self.with_bytes_impl(bytes) input_stream = ByteArrayInputStream.new(bytes) format = detect_format_from_input(input_stream) input_stream.reset - ImageVoodoo.new(read_image_from_input(input_stream), format) + buffered_image = read_image_from_input(input_stream) + input_stream.reset + ImageVoodoo.new(input_stream, buffered_image, format) end # # Converts a RGB hex value into a java.awt.Color object or dies trying # with an ArgumentError. @@ -227,10 +228,31 @@ out = ByteArrayOutputStream.new write_new_image format, ImageIO.create_image_output_stream(out) out.to_byte_array end + def correct_orientation_impl + case metadata.orientation + when 2 then + flip_horizontally + when 3 then + rotate 180 + when 4 then + flip_vertically + when 5 then + flip_horizontally + rotate 270 + when 6 then + rotate 90 + when 7 then + flip_horizontally + rotate 270 + else + self + end + end + def flip_horizontally_impl paint {|g| g.draw_image @src, 0, 0, width, height, width, 0, 0, height, nil} end def flip_vertically_impl @@ -239,10 +261,16 @@ def greyscale_impl transform(GREY_OP) end + def metadata_impl + require 'image_voodoo/metadata' + + @metadata ||= ImageVoodoo::Metadata.new(@io) + end + def negative_impl transform(NEGATIVE_OP) end def resize_impl(width, height) @@ -250,19 +278,34 @@ scaled_image = @src.get_scaled_instance width, height, SCALE_SMOOTH g.draw_image scaled_image, 0, 0, nil end end + def rotate_impl(degrees) + radians = degrees * Math::PI / 180 + sin, cos = Math.sin(radians).abs, Math.cos(radians).abs + new_width = (width * cos + height * sin).floor + new_height = (width * sin + height * cos).floor + + paint(BufferedImage.new(new_width, new_height, color_type)) do |g| + + g.java_send :translate, [::Java::int, ::Java::int], + (new_width - width)/2, (new_height - height)/2 + g.rotate(radians, width / 2, height / 2); + g.draw_image @src, 0, 0, nil + end + end + # # Save using the format string (jpg, gif, etc..) to the open Java File # instance passed in. # def save_impl(format, file) write_new_image format, FileImageOutputStream.new(file) end def with_crop_impl(left, top, right, bottom) - ImageVoodoo.new(@src.get_subimage(left, top, right-left, bottom-top), @format) + ImageVoodoo.new(@io, @src.get_subimage(left, top, right-left, bottom-top), @format) end def write_new_image(format, stream) writer = ImageIO.getImageWritersByFormatName(format).next writer.output = stream