#!/usr/bin/env ruby require 'tempfile' require 'term/ansicolor' require 'open-uri' require 'tins/xt' include Tins::GO def provide_ppm_file(filename, opts) pnmtopnm = `which pnmtopnm` if pnmtopnm.empty? STDERR.puts "You might consider installing netpbm to enable image conversion/scaling!" File.new(filename, 'rb') else cmd = [ pnmtopnm.chomp! ] ext = File.extname(filename) scale = `which pamscale`.chomp! console_scale = "#{scale} -#{opts['s']} #{opts['C']} #{opts['R']}" cmd.unshift console_scale aspect_scaling = "#{scale} -xscale #{opts['a']}" cmd.unshift aspect_scaling convert_to_pnm = case ext when /\A(\.ppm\d*|\.pnm|\z)/ '' when /\A.(jpe?g|exif|jfif)\z/i 'jpegtopnm' when /\A.tiff?\z/i 'tifftopnm' else "#{ext[1..-1].downcase}topnm" end unless convert_to_pnm.empty? convert_to_pnm = `which #{convert_to_pnm}`.chomp! convert_to_pnm.empty? and fail "unknown pnm converter #{convert_to_pnm}" cmd.unshift convert_to_pnm end temp = Tempfile.open(File.basename($0)) cmd *= '|' STDERR.puts "Executing #{cmd.inspect}" IO.popen(cmd, 'r+') do |converter| open(filename, 'rb') do |input| until input.eof? converter.write input.read(8192) end converter.close_write end until converter.eof? temp.write(converter.read) end end temp.tap(&:rewind) end end def usage(rc = 0) puts < metric, :gray => gray ) puts ppm