Title: [890] trunk/image_voodoo: Add the ability to work with in-memory bytes as image source and desination (rather than files) and add a couple samples.
Revision
890
Author
headius
Date
2008-02-07 00:37:07 -0500 (Thu, 07 Feb 2008)

Log Message

Add the ability to work with in-memory bytes as image source and desination (rather than files) and add a couple samples.

Modified Paths

Added Paths

Diff

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


--- trunk/image_voodoo/lib/image_voodoo.rb	2008-02-07 05:00:46 UTC (rev 889)
+++ trunk/image_voodoo/lib/image_voodoo.rb	2008-02-07 05:37:07 UTC (rev 890)
@@ -6,6 +6,8 @@
   import java.awt.geom.AffineTransform
   import java.awt.image.BufferedImage
   import javax.imageio.ImageIO
+  import java.io.ByteArrayInputStream
+  import java.io.ByteArrayOutputStream
 
   def initialize(src)
     @src = ""
@@ -47,6 +49,12 @@
     true
   end
 
+  def bytes(format)
+    out = ByteArrayOutputStream.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 }
@@ -59,6 +67,14 @@
   def self.with_image(file)
     yield ImageVoodoo.new(ImageIO.read(java.io.File.new(file)))
   end
+
+  def self.with_bytes(bytes)
+    if String === bytes
+      bytes = bytes.to_java_bytes
+    end
+
+    yield ImageVoodoo.new(ImageIO.read(ByteArrayInputStream.new(bytes)))
+  end
 end
 
 #ImageVoodoo.with_image(ARGV[0]) do |img|

Added: trunk/image_voodoo/samples/file_thumbnail.rb (0 => 890)


--- trunk/image_voodoo/samples/file_thumbnail.rb	                        (rev 0)
+++ trunk/image_voodoo/samples/file_thumbnail.rb	2008-02-07 05:37:07 UTC (rev 890)
@@ -0,0 +1,11 @@
+require 'image_voodoo'
+
+# reads in the file specified by ARGV[0], transforms it into a 32-pixel thumbnail,
+# and writes it out to the file specified by ARGV[1], using that extension as the
+# target format.
+ImageVoodoo.with_image(ARGV[0]) do |img|
+  img.thumbnail(32) do |img2|
+    img2.save(ARGV[1])
+  end
+end
+

Added: trunk/image_voodoo/samples/in-memory.rb (0 => 890)


--- trunk/image_voodoo/samples/in-memory.rb	                        (rev 0)
+++ trunk/image_voodoo/samples/in-memory.rb	2008-02-07 05:37:07 UTC (rev 890)
@@ -0,0 +1,11 @@
+require 'image_voodoo'
+
+# reads in the image at ARGV[0], transforms it into a 32-pixel thumbnail in-memory,
+# and writes it back out as a png to the file specified by ARGV[1]
+ImageVoodoo.with_bytes(File.read(ARGV[0])) do |img|
+  img.thumbnail(32) do |img2|
+    File.open(ARGV[1], 'w') do |file|
+      file.write(img2.bytes('png'))
+    end
+  end
+end
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to