5 # A.k.a `Best web-album Of the world, Or your money back, Humerus'.
7 # The acronyn sucks, however this is a tribute to Dragon Ball by
8 # Akira Toriyama, where the last enemy beaten by heroes of Dragon
9 # Ball is named "Boo". But there was already a free software project
10 # called Boo, so this one will be it "Booh". Or whatever.
13 # Copyright (c) 2004 Guillaume Cottenceau <gc3 at bluewin.ch>
15 # This software may be freely redistributed under the terms of the GNU
16 # public license version 2.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 bindtextdomain("booh")
29 require 'booh/config.rb'
30 require 'booh/version.rb'
34 $CURRENT_CHARSET = `locale charmap`.chomp
35 $convert = 'convert -interlace line +profile "*"'
36 $convert_enhance = '-contrast -enhance -normalize'
39 return Iconv::iconv("UTF-8", $CURRENT_CHARSET, string).to_s
43 #- fake for gettext to find these; if themes need more sizes, english name for them should be added here
44 sizenames = { 'small' => utf8(_("small")), 'medium' => utf8(_("medium")), 'large' => utf8(_("large")) }
45 return sizenames[key] || key
49 return Iconv::iconv($CURRENT_CHARSET, "UTF-8", string).to_s
52 def make_dest_filename(orig_filename)
53 #- we remove non alphanumeric characters but need to do that
54 #- cleverly to not end up with two similar dest filenames. we won't
55 #- urlencode because urldecode might happen in the browser.
56 return orig_filename.unpack("C*").collect { |v| v.chr =~ /[a-zA-Z\-_0-9\.\/]/ ? v.chr : sprintf("%2X", v) }.to_s
59 def msg(verbose_level, msg)
60 if verbose_level <= $verbose_level
62 warn _("\t***ERROR***: %s\n") % msg
63 elsif verbose_level == 1
64 warn _("\tWarning: %s\n") % msg
71 def msg_(verbose_level, msg)
72 if verbose_level <= $verbose_level
74 warn _("\t***ERROR***: %s") % msg
75 elsif verbose_level == 1
76 warn _("\tWarning: %s") % msg
88 def select_theme(name, limit_sizes)
90 msg 3, _("Selecting theme `%s'") % $theme
91 themedir = "#{$FPATH}/themes/#{$theme}"
92 if !File.directory?(themedir)
93 die _("Theme was not found (tried %s directory).") % themedir
95 eval File.open("#{themedir}/metadata/parameters.rb").readlines.join
98 sizes = limit_sizes.split(/,/)
99 $images_size = $images_size.find_all { |e| sizes.include?(e['name']) }
100 if $images_size.length == 0
101 die _("Can't carry on, no valid size selected.")
104 $default_size = $images_size.detect { |sizeobj| sizeobj['default'] }
105 if $default_size == nil
106 $default_size = $images_size[0]
110 def entry2type(entry)
111 if entry =~ /\.(jpg|jpeg|jpe|gif|bmp|png)$/i
113 elsif entry =~ /\.(mov|avi|mpg|mpeg|mpe|wmv|asx)$/i
114 #- might consider using file magic later..
126 #- parallelizable sys
136 if $pids.length == $mproc
137 finished = Process.wait2
138 $pids.delete(finished[0])
139 $pids = $pids.find_all { |pid| Process.waitpid(pid, Process::WNOHANG) == nil }
146 #- grab the results of a command but don't sleep forever on a runaway process
147 def subproc_runaway_aware(command)
152 rescue Timeout::Error
153 msg 1, _("forgetting runaway process (transcode sucks again?)")
154 #- todo should slay transcode but dunno how to do that
159 def get_image_size(fullpath)
160 if `identify '#{fullpath}'` =~ / JPEG (\d+)x(\d+) /
161 return { :x => $1, :y => $2 }
167 #- commify from http://pleac.sourceforge.net/ (pleac rulz)
169 n.to_s =~ /([^\.]*)(\..*)?/
170 int, dec = $1.reverse, $2 ? $2 : ""
171 while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2' + _(",") + '\3')
176 def guess_rotate(filename)
177 orientation = `exif '#{filename}'`.detect { |line| line =~ /^Orientation/ }
178 if orientation =~ /right - top/
180 elsif orientation =~ /left - bottom/
185 size = get_image_size(filename)
186 #- remove rotate if image is obviously already in portrait (situation can come from gthumb)
187 if size && size[:x] < size[:y]
194 def rotate_pixbuf(pixbuf, angle)
195 return pixbuf.rotate(angle == 90 ? Gdk::Pixbuf::ROTATE_CLOCKWISE :
196 angle == 180 ? Gdk::Pixbuf::ROTATE_UPSIDEDOWN :
197 angle == 270 ? Gdk::Pixbuf::ROTATE_COUNTERCLOCKWISE :
198 Gdk::Pixbuf::ROTATE_NONE)
201 def gen_thumbnails_element(orig, xmldir, allow_background, dests)
202 gen_thumbnails(orig, xmldir, allow_background, dests, xmldir.elements["[@filename='#{utf8(File.basename(orig))}']"], '')
205 def gen_thumbnails_subdir(orig, xmldir, allow_background, dests, type)
206 #- type can be `subdirs' or `thumbnails'
207 gen_thumbnails(orig, xmldir, allow_background, dests, xmldir, type + '-')
210 def gen_thumbnails(orig, xmldir, allow_background, dests, felem, attributes_prefix)
211 if !dests.detect { |dest| !File.exists?(dest['filename']) }
217 if entry2type(orig) == 'image'
219 rotate = felem.attributes["#{attributes_prefix}rotate"]
221 felem.add_attribute("#{attributes_prefix}rotate", rotate = guess_rotate(orig).to_i)
223 convert_options += "-rotate #{rotate} "
224 if felem.attributes["#{attributes_prefix}enhance"]
225 convert_options += ($config['convert-enhance'] || $convert_enhance) + " "
229 if !File.exists?(dest['filename'])
230 cmd = "#{$convert} #{convert_options}-size #{dest['size']} -resize #{dest['size']} '#{orig}' '#{dest['filename']}'"
240 elsif entry2type(orig) == 'video'
241 dest_dir = make_dest_filename(File.dirname(dests[0]['filename']))
243 #- frame-offset is an attribute that allows to specify which frame to use for the thumbnail
244 frame_offset = felem.attributes["#{attributes_prefix}frame-offset"]
246 felem.add_attribute("#{attributes_prefix}frame-offset", frame_offset = "0")
248 frame_offset = frame_offset.to_i
249 if rotate = felem.attributes["#{attributes_prefix}rotate"]
250 convert_options += "-rotate #{rotate} "
252 if felem.attributes["#{attributes_prefix}enhance"]
253 convert_options += ($config['convert-enhance'] || $convert_enhance) + " "
257 if !File.exists?("#{dest_dir}/screenshot.jpg000000.jpg")
258 transcode_options = ''
260 if felem.attributes["#{attributes_prefix}color-swap"]
261 transcode_options += '-k '
264 cmd = "transcode -a 0 -c #{frame_offset}-#{frame_offset+1} -i '#{orig}' -y jpg -o '#{dest_dir}/screenshot.jpg' #{transcode_options} 2>&1"
266 results = subproc_runaway_aware(cmd)
267 if results =~ /skipping frames/ && results =~ /encoded 0 frames/
268 msg 0, _("specified frame-offset too large? max frame was: %s. that may also be another probleme. try another value.") %
269 results.scan(/skipping frames \[000000-(\d+)\]/)[-1]
271 elsif results =~ /V: import format.*unknown/ || !File.exists?("#{dest_dir}/screenshot.jpg000000.jpg")
272 msg 2, _("* could not extract first image of video %s with transcode, will try first converting with mencoder") % orig
273 cmd = "mencoder '#{orig}' -nosound -ovc lavc -lavcopts vcodec=mjpeg -o '#{dest_dir}/foo.avi' -frames #{frame_offset+1} -fps 25 >/dev/null 2>/dev/null"
276 if File.exists?("#{dest_dir}/foo.avi")
277 cmd = "transcode -a 0 -c #{frame_offset}-#{frame_offset+1} -i '#{dest_dir}/foo.avi' -y jpg -o '#{dest_dir}/screenshot.jpg' #{transcode_options} 2>&1"
279 results = subproc_runaway_aware(cmd)
280 system("rm -f '#{dest_dir}/foo.avi'")
281 if results =~ /skipping frames/ && results =~ /encoded 0 frames/
282 msg 0, _("specified frame-offset too large? max frame was: %s. that may also be another probleme. try another value.") %
283 results.scan(/skipping frames \[000000-(\d+)\]/)[-1]
285 elsif results =~ /V: import format.*unknown/ || !File.exists?("#{dest_dir}/screenshot.jpg000000.jpg")
286 msg 0, _("could not extract first image of video %s encoded by mencoder") % "#{dest_dir}/foo.avi"
290 msg 0, _("could not make mencoder to encode %s to mpeg4") % orig
296 if !File.exists?(dest['filename'])
297 sys("#{$convert} #{convert_options}-size #{dest['size']} -resize #{dest['size']} #{dest_dir}/screenshot.jpg000000.jpg '#{dest['filename']}'")
304 def invornil(obj, methodname)
308 return obj.method(methodname).call
312 def find_subalbum_info_type(xmldir)
313 #- first look for subdirs info; if not, means there is no subdir
314 if xmldir.attributes['subdirs-caption']
321 def find_subalbum_caption_info(xmldir)
322 type = find_subalbum_info_type(xmldir)
323 return from_utf8(xmldir.attributes["#{type}-captionfile"]), xmldir.attributes["#{type}-caption"]