3 # A.k.a `Best web-album Of the world, Or your money back, Humerus'.
5 # The acronyn sucks, however this is a tribute to Dragon Ball by
6 # Akira Toriyama, where the last enemy beaten by heroes of Dragon
7 # Ball is named "Boo". But there was already a free software project
8 # called Boo, so this one will be it "Booh". Or whatever.
11 # Copyright (c) 2004 Guillaume Cottenceau <gc3 at bluewin.ch>
13 # This software may be freely redistributed under the terms of the GNU
14 # public license version 2.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 require 'rexml/document'
27 bindtextdomain("booh")
29 require 'booh/config.rb'
30 require 'booh/version.rb'
37 require 'booh/libadds'
44 $CURRENT_CHARSET = `locale charmap`.chomp
45 $convert = 'convert -interlace line +profile "*"'
46 $convert_enhance = '-contrast -enhance -normalize'
49 return Iconv::iconv("UTF-8", $CURRENT_CHARSET, string).to_s
52 def utf8cut(string, maxlen)
54 return Iconv::iconv("UTF-8", $CURRENT_CHARSET, string[0..maxlen-1]).to_s
55 rescue Iconv::InvalidCharacter
56 return utf8cut(string, maxlen-1)
61 #- fake for gettext to find these; if themes need more sizes, english name for them should be added here
62 sizenames = { 'small' => utf8(_("small")), 'medium' => utf8(_("medium")), 'large' => utf8(_("large")),
63 'x-large' => utf8(_("x-large")), 'xx-large' => utf8(_("xx-large")),
64 'original' => utf8(_("original")) }
65 return sizenames[key] || key
69 return Iconv::iconv($CURRENT_CHARSET, "UTF-8", string).to_s
72 def from_utf8_safe(string)
74 return Iconv::iconv($CURRENT_CHARSET, "UTF-8", string).to_s
75 rescue Iconv::IllegalSequence
80 def make_dest_filename_old(orig_filename)
81 #- we remove non alphanumeric characters but need to do that
82 #- cleverly to not end up with two similar dest filenames. we won't
83 #- urlencode because urldecode might happen in the browser.
84 return orig_filename.unpack("C*").collect { |v| v.chr =~ /[a-zA-Z\-_0-9\.\/]/ ? v.chr : sprintf("%2X", v) }.to_s
87 def make_dest_filename(orig_filename)
88 #- we remove non alphanumeric characters but need to do that
89 #- cleverly to not end up with two similar dest filenames. we won't
90 #- urlencode because urldecode might happen in the browser.
91 return orig_filename.unpack("C*").collect { |v| v.chr =~ /[a-zA-Z\-_0-9\.\/]/ ? v.chr : sprintf("~%02X", v) }.to_s
94 def msg(verbose_level, msg)
95 if verbose_level <= $verbose_level
97 warn _("\t***ERROR***: %s\n") % msg
98 elsif verbose_level == 1
99 warn _("\tWarning: %s\n") % msg
106 def msg_(verbose_level, msg)
107 if verbose_level <= $verbose_level
108 if verbose_level == 0
109 warn _("\t***ERROR***: %s") % msg
110 elsif verbose_level == 1
111 warn _("\tWarning: %s") % msg
123 def select_theme(name, limit_sizes, optimizefor32, nperrow)
125 msg 3, _("Selecting theme '%s'") % $theme
126 themedir = "#{$FPATH}/themes/#{$theme}"
127 if !File.directory?(themedir)
128 die _("Theme was not found (tried %s directory).") % themedir
130 eval File.open("#{themedir}/metadata/parameters.rb").readlines.join
133 if limit_sizes != 'all'
134 sizes = limit_sizes.split(/,/)
135 $images_size = $images_size.find_all { |e| sizes.include?(e['name']) }
136 if $images_size.length == 0
137 die _("Can't carry on, no valid size selected.")
141 $images_size = $images_size.find_all { |e| !e['optional'] }
145 $images_size.each { |e|
146 e['fullscreen'].gsub!(/(\d+x)(\d+)/) { $1 + ($2.to_f*8/9).to_i.to_s }
147 e['thumbnails'].gsub!(/(\d+x)(\d+)/) { $1 + ($2.to_f*8/9).to_i.to_s }
149 $albums_thumbnail_size.gsub!(/(\d+x)(\d+)/) { $1 + ($2.to_f*8/9).to_i.to_s }
152 if nperrow && nperrow != $default_N
153 ratio = nperrow.to_f / $default_N.to_f
154 $images_size.each { |e|
155 e['thumbnails'].gsub!(/(\d+)x(\d+)/) { ($1.to_f/ratio).to_i.to_s + 'x' + ($2.to_f/ratio).to_i.to_s }
159 $default_size = $images_size.detect { |sizeobj| sizeobj['default'] }
160 if $default_size == nil
161 $default_size = $images_size[0]
165 def entry2type(entry)
166 if entry =~ /\.(jpg|jpeg|jpe|gif|bmp|png)$/i && entry !~ /['"\[\]]/
168 elsif !$ignore_videos && entry =~ /\.(mov|avi|mpg|mpeg|mpe|wmv|asx|3gp|mp4)$/i && entry !~ /['"\[\]]/
169 #- might consider using file magic later..
182 finished = Process.wait2
183 $pids.delete(finished[0])
184 $pids = $pids.find_all { |pid| Process.waitpid(pid, Process::WNOHANG) == nil }
188 while $pids && $pids.length > 0
193 #- parallelizable sys
203 if $pids.length == $mproc
211 #- grab the results of a command but don't sleep forever on a runaway process
212 def subproc_runaway_aware(command)
217 rescue Timeout::Error
218 msg 1, _("forgetting runaway process (transcode sucks again?)")
219 #- todo should slay transcode but dunno how to do that
224 def get_image_size(fullpath)
229 if $sizes_cache[fullpath].nil?
230 #- identify is slow, try with gdk if available (4ms vs 35ms)
232 if `identify '#{fullpath}'` =~ / JPEG (\d+)x(\d+) /
233 $sizes_cache[fullpath] = { :x => $1.to_i, :y => $2.to_i }
236 if pxb = Gdk::Pixbuf.new(fullpath)
237 $sizes_cache[fullpath] = { :x => pxb.width, :y => pxb.height }
241 return $sizes_cache[fullpath]
247 #- commify from http://pleac.sourceforge.net/ (pleac rulz)
249 n.to_s =~ /([^\.]*)(\..*)?/
250 int, dec = $1.reverse, $2 ? $2 : ""
251 while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2' + _(",") + '\3')
256 def guess_rotate(filename)
257 #- identify is slow, try with libexif if available (4ms vs 35ms)
262 orientation = `identify -format "%[EXIF:orientation]" '#{filename}'`.chomp.to_i
264 orientation = Exif.orientation(filename)
269 elsif orientation == 8
274 size = get_image_size(filename)
275 #- remove rotate if image is obviously already in portrait (situation can come from gthumb)
276 if size && size[:x] < size[:y]
283 def rotate_pixbuf(pixbuf, angle)
284 return pixbuf.rotate(angle == 90 ? Gdk::Pixbuf::ROTATE_CLOCKWISE :
285 angle == 180 ? Gdk::Pixbuf::ROTATE_UPSIDEDOWN :
286 angle == 270 ? Gdk::Pixbuf::ROTATE_COUNTERCLOCKWISE :
287 Gdk::Pixbuf::ROTATE_NONE)
290 def gen_thumbnails_element(orig, xmldirorelem, allow_background, dests)
291 if xmldirorelem.name == 'dir'
292 xmldirorelem = xmldirorelem.elements["*[@filename='#{utf8(File.basename(orig))}']"]
294 gen_thumbnails(orig, allow_background, dests, xmldirorelem, '')
297 def gen_thumbnails_subdir(orig, xmldirorelem, allow_background, dests, type)
298 #- type can be `subdirs' or `thumbnails'
299 gen_thumbnails(orig, allow_background, dests, xmldirorelem, type + '-')
302 def gen_thumbnails(orig, allow_background, dests, felem, attributes_prefix)
303 if !dests.detect { |dest| !File.exists?(dest['filename']) }
308 dest_dir = make_dest_filename(File.dirname(dests[0]['filename']))
310 if entry2type(orig) == 'image'
312 if whitebalance = felem.attributes["#{attributes_prefix}white-balance"]
313 neworig = "#{dest_dir}/#{File.basename(orig)}-whitebalance#{whitebalance}.jpg"
314 cmd = "booh-fix-whitebalance '#{orig}' '#{neworig}' #{whitebalance}"
316 if File.exists?(neworig)
320 if gammacorrect = felem.attributes["#{attributes_prefix}gamma-correction"]
321 neworig = "#{dest_dir}/#{File.basename(orig)}-gammacorrect#{gammacorrect}.jpg"
322 cmd = "booh-gamma-correction '#{orig}' '#{neworig}' #{gammacorrect}"
324 if File.exists?(neworig)
328 rotate = felem.attributes["#{attributes_prefix}rotate"]
330 felem.add_attribute("#{attributes_prefix}rotate", rotate = guess_rotate(orig).to_i)
332 convert_options += "-rotate #{rotate} "
333 if felem.attributes["#{attributes_prefix}enhance"]
334 convert_options += ($config['convert-enhance'] || $convert_enhance) + " "
338 if !File.exists?(dest['filename'])
340 cmd ||= "#{$convert} #{convert_options}-size #{dest['size']} -resize '#{dest['size']}>' '#{orig}' '#{dest['filename']}'"
352 system("rm -f '#{neworig}'")
356 elsif entry2type(orig) == 'video'
358 #- frame-offset is an attribute that allows to specify which frame to use for the thumbnail
359 frame_offset = felem.attributes["#{attributes_prefix}frame-offset"]
361 felem.add_attribute("#{attributes_prefix}frame-offset", frame_offset = "0")
363 frame_offset = frame_offset.to_i
364 if rotate = felem.attributes["#{attributes_prefix}rotate"]
365 convert_options += "-rotate #{rotate} "
367 if felem.attributes["#{attributes_prefix}enhance"]
368 convert_options += ($config['convert-enhance'] || $convert_enhance) + " "
371 orig_base = File.basename(dests[0]['filename']) + File.basename(orig)
372 orig_image = "#{dest_dir}/#{orig_base}.jpg000000.jpg"
373 system("rm -f '#{orig_image}'")
375 if !File.exists?(orig_image)
376 transcode_options = ''
378 if felem.attributes["#{attributes_prefix}color-swap"]
379 transcode_options += '-k '
382 cmd = "mencoder '#{orig}' -nosound -ovc lavc -lavcopts vcodec=mjpeg -o '#{dest_dir}/#{orig_base}.avi' -frames #{frame_offset+26} -fps 25 >/dev/null 2>/dev/null"
385 if File.exists?("#{dest_dir}/#{orig_base}.avi")
386 cmd = "transcode -a 0 -c #{frame_offset}-#{frame_offset+1} -i '#{dest_dir}/#{orig_base}.avi' -y jpg -o '#{dest_dir}/#{orig_base}.jpg' #{transcode_options} 2>&1"
388 results = subproc_runaway_aware(cmd)
389 system("rm -f '#{dest_dir}/#{orig_base}.avi'")
390 if results =~ /skipping frames/ && results =~ /encoded 0 frames/
391 msg 0, _("specified frame-offset too large? max frame was: %s. that may also be another probleme. try another value.") %
392 results.scan(/skipping frames \[000000-(\d+)\]/)[-1]
394 elsif results =~ /V: import format.*unknown/ || !File.exists?(orig_image)
395 msg 0, _("could not extract first image of video %s encoded by mencoder") % "#{dest_dir}/#{orig_base}.avi"
399 msg 0, _("could not make mencoder to encode %s to mjpeg") % orig
402 if felem && whitebalance = felem.attributes["#{attributes_prefix}white-balance"]
403 if whitebalance.to_f != 0
404 neworig = "#{dest_dir}/#{orig_base}-whitebalance#{whitebalance}.jpg"
405 cmd = "booh-fix-whitebalance '#{orig_image}' '#{neworig}' #{whitebalance}"
407 if File.exists?(neworig)
412 if felem && gammacorrect = felem.attributes["#{attributes_prefix}gamma-correction"]
413 if gammacorrect.to_f != 0
414 neworig = "#{dest_dir}/#{orig_base}-gammacorrect#{gammacorrect}.jpg"
415 cmd = "booh-gamma-correction '#{orig_image}' '#{neworig}' #{gammacorrect}"
417 if File.exists?(neworig)
423 if !File.exists?(dest['filename'])
424 sys("#{$convert} #{convert_options}-size #{dest['size']} -resize #{dest['size']} '#{orig_image}' '#{dest['filename']}'")
428 system("rm -f '#{neworig}'")
434 def invornil(obj, methodname)
438 return obj.method(methodname).call
442 def find_subalbum_info_type(xmldir)
443 #- first look for subdirs info; if not, means there is no subdir
444 if xmldir.attributes['subdirs-caption']
451 def find_subalbum_caption_info(xmldir)
452 type = find_subalbum_info_type(xmldir)
453 return [ from_utf8(xmldir.attributes["#{type}-captionfile"]), xmldir.attributes["#{type}-caption"] ]
458 return File.size(path)
469 n < a ? a : n > b ? b : n
472 def pano_amount(elem)
473 if pano_amount = elem.attributes['pano-amount']
475 return clamp(pano_amount.to_f, 1, $N_per_row.to_i)
477 return clamp(pano_amount.to_f, 1, $default_N.to_i)
484 def substInFile(name)
485 newcontent = IO.readlines(name).collect { |l| yield l }
486 ios = File.open(name, "w")
487 ios.write(newcontent)
493 def File.reduce_path(path)
494 return path.gsub(/\w+\/\.\.\//, '')
499 def collect_with_index
501 each_with_index { |e,i|
509 def previous_element_byname(name)
511 while n = n.previous_element
519 def previous_element_byname_notattr(name, attr)
521 while n = n.previous_element
522 if n.name == name && !n.attributes[attr]
529 def next_element_byname(name)
531 while n = n.next_element
539 def next_element_byname_notattr(name, attr)
541 while n = n.next_element
542 if n.name == name && !n.attributes[attr]
549 def child_byname_notattr(name, attr)
550 elements.each(name) { |element|
551 if !element.attributes[attr]