Class: Quadtone::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/quadtone/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Renderer

Returns a new instance of Renderer.



12
13
14
15
16
# File 'lib/quadtone/renderer.rb', line 12

def initialize(params={})
  @compress = true
  @resolution = 720
  params.each { |k, v| send("#{k}=", v) }
end

Instance Attribute Details

#compressObject

Returns the value of attribute compress.



7
8
9
# File 'lib/quadtone/renderer.rb', line 7

def compress
  @compress
end

#desired_sizeObject

Returns the value of attribute desired_size.



10
11
12
# File 'lib/quadtone/renderer.rb', line 10

def desired_size
  @desired_size
end

#gammaObject

Returns the value of attribute gamma.



5
6
7
# File 'lib/quadtone/renderer.rb', line 5

def gamma
  @gamma
end

#page_sizeObject

Returns the value of attribute page_size.



8
9
10
# File 'lib/quadtone/renderer.rb', line 8

def page_size
  @page_size
end

#resolutionObject

Returns the value of attribute resolution.



9
10
11
# File 'lib/quadtone/renderer.rb', line 9

def resolution
  @resolution
end

#rotateObject

Returns the value of attribute rotate.



6
7
8
# File 'lib/quadtone/renderer.rb', line 6

def rotate
  @rotate
end

Instance Method Details

#apply_gammaObject



91
92
93
94
95
96
# 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_16bitObject



86
87
88
89
# 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_areaObject



122
123
124
125
126
# 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_profilesObject



81
82
83
84
# File 'lib/quadtone/renderer.rb', line 81

def delete_profiles
  ;;warn "\t\t" + "Deleting profiles"
  @current_image.delete_profile('*')
end

#extend_to_pageObject



113
114
115
116
117
118
119
120
# 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_pathObject



68
69
70
71
72
73
74
75
# 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) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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

#resizeObject



108
109
110
111
# 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_infoObject



77
78
79
# File 'lib/quadtone/renderer.rb', line 77

def show_info
  ;;warn "\t\t" + @current_image.inspect
end

#write_to_fileObject



128
129
130
131
132
133
134
135
# 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