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 'x-large' => utf8(_("x-large")), 'xx-large' => utf8(_("xx-large")),
46 'original' => utf8(_("original")) }
47 return sizenames[key] || key
51 return Iconv::iconv($CURRENT_CHARSET, "UTF-8", string).to_s
54 def make_dest_filename(orig_filename)
55 #- we remove non alphanumeric characters but need to do that
56 #- cleverly to not end up with two similar dest filenames. we won't
57 #- urlencode because urldecode might happen in the browser.
58 return orig_filename.unpack("C*").collect { |v| v.chr =~ /[a-zA-Z\-_0-9\.\/]/ ? v.chr : sprintf("%2X", v) }.to_s
61 def msg(verbose_level, msg)
62 if verbose_level <= $verbose_level
64 warn _("\t***ERROR***: %s\n") % msg
65 elsif verbose_level == 1
66 warn _("\tWarning: %s\n") % msg
73 def msg_(verbose_level, msg)
74 if verbose_level <= $verbose_level
76 warn _("\t***ERROR***: %s") % msg
77 elsif verbose_level == 1
78 warn _("\tWarning: %s") % msg
90 def select_theme(name, limit_sizes)
92 msg 3, _("Selecting theme `%s'") % $theme
93 themedir = "#{$FPATH}/themes/#{$theme}"
94 if !File.directory?(themedir)
95 die _("Theme was not found (tried %s directory).") % themedir
97 eval File.open("#{themedir}/metadata/parameters.rb").readlines.join
100 if limit_sizes != 'all'
101 sizes = limit_sizes.split(/,/)
102 $images_size = $images_size.find_all { |e| sizes.include?(e['name']) }
103 if $images_size.length == 0
104 die _("Can't carry on, no valid size selected.")
108 $images_size = $images_size.find_all { |e| !e['optional'] }
111 $default_size = $images_size.detect { |sizeobj| sizeobj['default'] }
112 if $default_size == nil
113 $default_size = $images_size[0]
117 def entry2type(entry)
118 if entry =~ /\.(jpg|jpeg|jpe|gif|bmp|png)$/i && entry !~ /['"\[\]]/
120 elsif entry =~ /\.(mov|avi|mpg|mpeg|mpe|wmv|asx)$/i && entry !~ /['"\[\]]/
121 #- might consider using file magic later..
133 #- parallelizable sys
143 if $pids.length == $mproc
144 finished = Process.wait2
145 $pids.delete(finished[0])
146 $pids = $pids.find_all { |pid| Process.waitpid(pid, Process::WNOHANG) == nil }
153 #- grab the results of a command but don't sleep forever on a runaway process
154 def subproc_runaway_aware(command)
159 rescue Timeout::Error
160 msg 1, _("forgetting runaway process (transcode sucks again?)")
161 #- todo should slay transcode but dunno how to do that
166 def get_image_size(fullpath)
167 if `identify '#{fullpath}'` =~ / JPEG (\d+)x(\d+) /
168 return { :x => $1.to_i, :y => $2.to_i }
174 #- commify from http://pleac.sourceforge.net/ (pleac rulz)
176 n.to_s =~ /([^\.]*)(\..*)?/
177 int, dec = $1.reverse, $2 ? $2 : ""
178 while int.gsub!(/(,|\.|^)(\d{3})(\d)/, '\1\2' + _(",") + '\3')
183 def guess_rotate(filename)
184 orientation = `exif '#{filename}'`.detect { |line| line =~ /^Orientation/ }
185 if orientation =~ /right - top/
187 elsif orientation =~ /left - bottom/
192 size = get_image_size(filename)
193 #- remove rotate if image is obviously already in portrait (situation can come from gthumb)
194 if size && size[:x] < size[:y]
201 def rotate_pixbuf(pixbuf, angle)
202 return pixbuf.rotate(angle == 90 ? Gdk::Pixbuf::ROTATE_CLOCKWISE :
203 angle == 180 ? Gdk::Pixbuf::ROTATE_UPSIDEDOWN :
204 angle == 270 ? Gdk::Pixbuf::ROTATE_COUNTERCLOCKWISE :
205 Gdk::Pixbuf::ROTATE_NONE)
208 def gen_thumbnails_element(orig, xmldir, allow_background, dests)
209 gen_thumbnails(orig, xmldir, allow_background, dests, xmldir.elements["[@filename='#{utf8(File.basename(orig))}']"], '')
212 def gen_thumbnails_subdir(orig, xmldir, allow_background, dests, type)
213 #- type can be `subdirs' or `thumbnails'
214 gen_thumbnails(orig, xmldir, allow_background, dests, xmldir, type + '-')
217 def gen_thumbnails(orig, xmldir, allow_background, dests, felem, attributes_prefix)
218 if !dests.detect { |dest| !File.exists?(dest['filename']) }
224 if entry2type(orig) == 'image'
226 rotate = felem.attributes["#{attributes_prefix}rotate"]
228 felem.add_attribute("#{attributes_prefix}rotate", rotate = guess_rotate(orig).to_i)
230 convert_options += "-rotate #{rotate} "
231 if felem.attributes["#{attributes_prefix}enhance"]
232 convert_options += ($config['convert-enhance'] || $convert_enhance) + " "
236 if !File.exists?(dest['filename'])
238 #- don't resize if image is already smaller than destination size
239 if size = get_image_size(orig)
240 dest['size'] =~ /(\d+)x(\d+)/
241 if (rotate == "90" || rotate == "270") && (size[:x] < $2.to_i || size[:y] < $1.to_i) ||
242 size[:x] < $1.to_i || size[:y] < $2.to_i
243 cmd = "#{$convert} #{convert_options} '#{orig}' '#{dest['filename']}'"
246 cmd ||= "#{$convert} #{convert_options}-size #{dest['size']} -resize #{dest['size']} '#{orig}' '#{dest['filename']}'"
256 elsif entry2type(orig) == 'video'
257 dest_dir = make_dest_filename(File.dirname(dests[0]['filename']))
259 #- frame-offset is an attribute that allows to specify which frame to use for the thumbnail
260 frame_offset = felem.attributes["#{attributes_prefix}frame-offset"]
262 felem.add_attribute("#{attributes_prefix}frame-offset", frame_offset = "0")
264 frame_offset = frame_offset.to_i
265 if rotate = felem.attributes["#{attributes_prefix}rotate"]
266 convert_options += "-rotate #{rotate} "
268 if felem.attributes["#{attributes_prefix}enhance"]
269 convert_options += ($config['convert-enhance'] || $convert_enhance) + " "
273 if !File.exists?("#{dest_dir}/screenshot.jpg000000.jpg")
274 transcode_options = ''
276 if felem.attributes["#{attributes_prefix}color-swap"]
277 transcode_options += '-k '
280 cmd = "transcode -a 0 -c #{frame_offset}-#{frame_offset+1} -i '#{orig}' -y jpg -o '#{dest_dir}/screenshot.jpg' #{transcode_options} 2>&1"
282 results = subproc_runaway_aware(cmd)
283 if results =~ /skipping frames/ && results =~ /encoded 0 frames/
284 msg 0, _("specified frame-offset too large? max frame was: %s. that may also be another problem. try another value.") %
285 results.scan(/skipping frames \[000000-(\d+)\]/)[-1]
287 elsif results =~ /V: import format.*unknown/ || !File.exists?("#{dest_dir}/screenshot.jpg000000.jpg")
288 msg 2, _("* could not extract first image of video %s with transcode, will try first converting with mencoder") % orig
289 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"
292 if File.exists?("#{dest_dir}/foo.avi")
293 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"
295 results = subproc_runaway_aware(cmd)
296 system("rm -f '#{dest_dir}/foo.avi'")
297 if results =~ /skipping frames/ && results =~ /encoded 0 frames/
298 msg 0, _("specified frame-offset too large? max frame was: %s. that may also be another probleme. try another value.") %
299 results.scan(/skipping frames \[000000-(\d+)\]/)[-1]
301 elsif results =~ /V: import format.*unknown/ || !File.exists?("#{dest_dir}/screenshot.jpg000000.jpg")
302 msg 0, _("could not extract first image of video %s encoded by mencoder") % "#{dest_dir}/foo.avi"
306 msg 0, _("could not make mencoder to encode %s to mpeg4") % orig
312 if !File.exists?(dest['filename'])
313 sys("#{$convert} #{convert_options}-size #{dest['size']} -resize #{dest['size']} #{dest_dir}/screenshot.jpg000000.jpg '#{dest['filename']}'")
320 def invornil(obj, methodname)
324 return obj.method(methodname).call
328 def find_subalbum_info_type(xmldir)
329 #- first look for subdirs info; if not, means there is no subdir
330 if xmldir.attributes['subdirs-caption']
337 def find_subalbum_caption_info(xmldir)
338 type = find_subalbum_info_type(xmldir)
339 return [ from_utf8(xmldir.attributes["#{type}-captionfile"]), xmldir.attributes["#{type}-caption"] ]
344 return File.size(path)