Sha256: 873a6e7a9fbbf034737aae12d979ae43dbbba8ae4c21736e00c9e0b9a05f31e5
Contents?: true
Size: 1.79 KB
Versions: 1
Compression:
Stored size: 1.79 KB
Contents
require File.dirname(__FILE__) + '/../spec_helper' require 'rspec' require 'pdf_ravager' require 'securerandom' require 'chunky_png' def mktemp # the tests are already dependent on Linux, so /tmp/ usage should be OK "/tmp/#{SecureRandom.uuid}" end def pdf_to_ps(pdf_file, out_file=nil) out_file ||= "#{mktemp}.ps" system("acroread -toPostScript -markupsOn -pairs #{pdf_file} #{out_file} >/dev/null 2>&1") out_file end def ps_to_png(ps_file, out_file=nil) out_file ||= "#{mktemp}.png" system("gs -dSAFER -dBATCH -dNOPAUSE -r150 -sDEVICE=png16m -dTextAlphaBits=4 -sOutputFile=#{out_file} #{ps_file} >/dev/null 2>&1") out_file end def pdf_to_png(pdf_file) ps_to_png(pdf_to_ps(pdf_file)) end # code taken from http://jeffkreeftmeijer.com/2011/comparing-images-and-creating-image-diffs/ def png_diff(img1, img2) images = [ ChunkyPNG::Image.from_file(img1), ChunkyPNG::Image.from_file(img2) ] output = ChunkyPNG::Image.new(images.first.width, images.last.width, ChunkyPNG::Color::WHITE) diff = [] images.first.height.times do |y| images.first.row(y).each_with_index do |pixel, x| unless pixel == images.last[x,y] score = Math.sqrt( (ChunkyPNG::Color::r(images.last[x,y]) - ChunkyPNG::Color::r(pixel)) ** 2 + (ChunkyPNG::Color::g(images.last[x,y]) - ChunkyPNG::Color::g(pixel)) ** 2 + (ChunkyPNG::Color::b(images.last[x,y]) - ChunkyPNG::Color::b(pixel)) ** 2 ) / Math.sqrt(ChunkyPNG::Color::MAX ** 2 * 3) output[x,y] = ChunkyPNG::Color::grayscale(ChunkyPNG::Color::MAX - (score * ChunkyPNG::Color::MAX).round) diff << score end end end num_pixels_changed = diff.length pct_changed = (num_pixels_changed == 0) ? 0 : (diff.inject(:+) / images.first.pixels.length) * 100 [num_pixels_changed, pct_changed] end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
pdf_ravager-0.0.8 | spec/integration/integration_helper.rb |