Title: [891] trunk/image_voodoo/lib/image_voodoo.rb: Cleanup, removed some dead/ useless code, shortened some lines, and using the right BufferedImage type now.
Revision
891
Author
headius
Date
2008-02-08 10:26:48 -0500 (Fri, 08 Feb 2008)

Log Message

Cleanup, removed some dead/useless code, shortened some lines, and using the right BufferedImage type now.

Modified Paths

Diff

Modified: trunk/image_voodoo/lib/image_voodoo.rb (890 => 891)


--- trunk/image_voodoo/lib/image_voodoo.rb	2008-02-07 05:37:07 UTC (rev 890)
+++ trunk/image_voodoo/lib/image_voodoo.rb	2008-02-08 15:26:48 UTC (rev 891)
@@ -1,24 +1,29 @@
-#
 class ImageVoodoo
   include Java
+  
   import java.awt.Image
   import java.awt.RenderingHints
   import java.awt.geom.AffineTransform
   import java.awt.image.BufferedImage
   import javax.imageio.ImageIO
-  import java.io.ByteArrayInputStream
-  import java.io.ByteArrayOutputStream
+  
+  JFile = java.io.File
+  BAIS = java.io.ByteArrayInputStream
+  BAOS = java.io.ByteArrayOutputStream
+  FOS = java.io.FileOutputStream
 
   def initialize(src)
     @src = ""
   end
 
   def cropped_thumbnail(size)
-    l, t, r, b, half = 0, 0, width, height, (width - height).abs / 2
+    half = (width - height).abs / 2
     l, r = half, half + height if width > height
     t, b = half, half + width if height > width
 
-    with_crop(l, t, r, b) { |img| img.thumbnail(size) { |thumb| yield thumb } }
+    with_crop(l, t, r, b) do |img|
+      img.thumbnail(size) { |thumb| yield thumb } 
+    end
   end
 
   def height
@@ -30,10 +35,22 @@
   end
 
   def resize(width, height)
-    target = BufferedImage.new(width, height, BufferedImage::TYPE_BYTE_BINARY)
+    target = BufferedImage.new(
+      width, height, BufferedImage::TYPE_4BYTE_ABGR)
     graphics = target.graphics
-    graphics.set_rendering_hint RenderingHints::KEY_INTERPOLATION, RenderingHints::VALUE_INTERPOLATION_BICUBIC
-    transform = AffineTransform.get_scale_instance width.to_f/@src.width, height.to_f/@src.height
+    graphics.set_rendering_hint(
+      RenderingHints::KEY_INTERPOLATION,
+      RenderingHints::VALUE_INTERPOLATION_BICUBIC)
+    graphics.set_rendering_hint(
+      RenderingHints::KEY_COLOR_RENDERING,
+      RenderingHints::VALUE_COLOR_RENDER_QUALITY)
+    
+    w_scale = width.to_f/@src.width
+    h_scale = height.to_f/@src.height
+    
+    transform =
+      AffineTransform.get_scale_instance w_scale, h_scale
+    
     graphics.draw_rendered_image @src, transform
     graphics.dispose
       
@@ -43,29 +60,33 @@
   def save(file)
     return false if file !~ /\.(.*)$/
         
-    out = java.io.FileOutputStream.new file
+    out = FOS.new file
     ImageIO.write(@src, $1, out)
     out.close
     true
   end
 
   def bytes(format)
-    out = ByteArrayOutputStream.new
+    out = BAOS.new
     ImageIO.write(@src, format, out)
     String.from_java_bytes(out.to_byte_array)
   end
 
   def thumbnail(size)
     scale = size.to_f / (width > height ? width : height)
-    resize((width * scale).to_i, (height * scale).to_i) {|image| yield image }
+    new_width = (width * scale).to_i
+    new_height = (height * scale).to_i
+    resize(new_width, new_height) {|image| yield image }
   end
 
   def with_crop(left, top, right, bottom)
-    yield ImageVoodoo.new(@src.get_subimage(left, top, right - left, bottom - top))
+    subimage = @src.get_subimage(left, top, right - left, bottom - top)
+    yield ImageVoodoo.new(subimage)
   end
 
   def self.with_image(file)
-    yield ImageVoodoo.new(ImageIO.read(java.io.File.new(file)))
+    image = ImageIO.read(JFile.new(file))
+    yield ImageVoodoo.new(image)
   end
 
   def self.with_bytes(bytes)
@@ -73,7 +94,8 @@
       bytes = bytes.to_java_bytes
     end
 
-    yield ImageVoodoo.new(ImageIO.read(ByteArrayInputStream.new(bytes)))
+    image = ImageIO.read(BAIS.new(bytes))
+    yield ImageVoodoo.new(image)
   end
 end
 
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to