dramatic speed improvements
[booh] / lib / booh / booh-lib.rb
index f0df7dedb746217d060a6f84f3f5b0f01847591c..731b9ed1cf593c544e5e94779bbaa0b5970d0459 100644 (file)
@@ -28,6 +28,11 @@ bindtextdomain("booh")
 
 require 'booh/config.rb'
 require 'booh/version.rb'
+begin
+    require 'gtk2'
+rescue LoadError
+    $no_gtk2 = true
+end
 
 module Booh
     $verbose_level = 2
@@ -212,8 +217,23 @@ module Booh
     end
 
     def get_image_size(fullpath)
-        if !$no_identify && `identify '#{fullpath}'` =~ / JPEG (\d+)x(\d+) /
-            return { :x => $1.to_i, :y => $2.to_i }
+        if !$no_identify
+            if $sizes_cache.nil?
+                $sizes_cache = {}
+            end
+            if $sizes_cache[fullpath].nil?
+                #- identify is slow, try with gdk if available (4ms vs 35ms)
+                if $no_gtk2
+                    if `identify '#{fullpath}'` =~ / JPEG (\d+)x(\d+) /
+                        $sizes_cache[fullpath] = { :x => $1.to_i, :y => $2.to_i }
+                    end
+                else
+                    if pxb = Gdk::Pixbuf.new(fullpath)
+                        $sizes_cache[fullpath] = { :x => pxb.width, :y => pxb.height }
+                    end
+                end
+            end
+            return $sizes_cache[fullpath]
         else
             return nil
         end