class Quadtone::Renderer

Attributes

compress[RW]
desired_size[RW]
gamma[RW]
page_size[RW]
resolution[RW]
rotate[RW]

Public Class Methods

new(params={}) click to toggle source
# File lib/quadtone/renderer.rb, line 12
def initialize(params={})
  @compress = true
  @resolution = 720
  params.each { |k, v| send("#{k}=", v) }
end

Public Instance Methods

apply_gamma() click to toggle source
# File lib/quadtone/renderer.rb, line 91
def apply_gamma
  if @gamma
    ;;warn "\t\t" + "Applying gamma #{@gamma}"
    @current_image = @current_image.gamma_correct(@gamma)
  end
end
convert_to_16bit() click to toggle source
# File lib/quadtone/renderer.rb, line 86
def convert_to_16bit
  ;;warn "\t\t" + "Changing to grayscale"
  @current_image = @current_image.quantize(2 ** 16, Magick::GRAYColorspace)
end
crop_to_imageable_area() click to toggle source
# File lib/quadtone/renderer.rb, line 122
def crop_to_imageable_area
  x, y, w, h = @page_size.margin.left, @page_size.height - @page_size.margin.top, @page_size.imageable_width, @page_size.imageable_height
  ;;warn "\t\t" + "Cropping to imageable area (x,y = #{x},#{y}, w,h = #{w},#{h})"
  @current_image.crop!(x, y, w, h)
end
delete_profiles() click to toggle source
# File lib/quadtone/renderer.rb, line 81
def delete_profiles
  ;;warn "\t\t" + "Deleting profiles"
  @current_image.delete_profile('*')
end
extend_to_page() click to toggle source
# File lib/quadtone/renderer.rb, line 113
def extend_to_page
  ;;warn "\t\t" + "Extending canvas to page area"
  @current_image = @current_image.extent(
    @page_size.width,
    @page_size.height,
    -(@page_size.width - @current_image.columns) / 2,
    -(@page_size.height - @current_image.rows) / 2)
end
output_path() click to toggle source
# File lib/quadtone/renderer.rb, line 68
def output_path
  params = []
  params << "#{@desired_size.width}x#{@desired_size.height}"
  params << @page_size.name
  params << "@#{@resolution}"
  params << "g#{@gamma}" if @gamma
  @input_path.with_extname(".out-#{params.join('-')}.#{@current_image_index}.png")
end
render(input_path) click to toggle source
# File lib/quadtone/renderer.rb, line 18
def render(input_path)
  @input_path = input_path

  raise "Page size required" unless @page_size

  # Scale measurements to specified resolution

  @page_size.width = (@page_size.width / 72.0 * @resolution).to_i
  @page_size.height = (@page_size.height / 72.0 * @resolution).to_i
  @page_size.imageable_width = (@page_size.imageable_width / 72.0 * @resolution).to_i
  @page_size.imageable_height = (@page_size.imageable_height / 72.0 * @resolution).to_i
  @page_size.margin.left = (@page_size.margin.left / 72.0 * @resolution).to_i
  @page_size.margin.right = (@page_size.margin.right / 72.0 * @resolution).to_i
  @page_size.margin.top = (@page_size.margin.top / 72.0 * @resolution).to_i
  @page_size.margin.bottom = (@page_size.margin.bottom / 72.0 * @resolution).to_i

  if @desired_size
    @desired_size.width = (@desired_size.width * @resolution).to_i
    @desired_size.height = (@desired_size.height * @resolution).to_i
    if @desired_size.width > @page_size.imageable_width || @desired_size.height > @page_size.imageable_height
      raise "Image too large for page size (#{@page_size.name})"
    end
  else
    @desired_size = HashStruct.new(width: @page_size.imageable_width, height: @page_size.imageable_height)
  end

  ;;warn "Reading #{@input_path} @ #{@resolution}dpi"
  r = @resolution  # have to alias to avoid referring to ImageList object
  image_list = Magick::ImageList.new(@input_path) {
    self.density = r
  }
  output_paths = []
  image_list.each_with_index do |image, image_index|
    @current_image = image
    @current_image_index = image_index
    ;;warn "\t" + "Processing sub-image \##{@current_image_index}"
    show_info
    delete_profiles
    convert_to_16bit
    apply_gamma
    rotate
    resize
    extend_to_page
    crop_to_imageable_area
    show_info
    output_paths << write_to_file
  end
  output_paths
end
resize() click to toggle source
# File lib/quadtone/renderer.rb, line 108
def resize
  ;;warn "\t\t" + "Resizing to desired size"
  @current_image.resize_to_fit!(@desired_size.width, @desired_size.height)
end
show_info() click to toggle source
# File lib/quadtone/renderer.rb, line 77
def show_info
  ;;warn "\t\t" + @current_image.inspect
end
write_to_file() click to toggle source
# File lib/quadtone/renderer.rb, line 128
def write_to_file
  path = output_path
  ;;warn "\t\t" + "Writing image to #{path}"
  @current_image.write(path) do
    self.compression = @compress ? Magick::ZipCompression : Magick::NoCompression
  end
  path
end