class Morandi::Straighten

Attributes

angle[RW]

Public Class Methods

new(angle=0) click to toggle source
Calls superclass method Morandi::ImageOp::new
# File lib/morandi/image-ops.rb, line 67
def initialize(angle=0)
  super()
  @angle = angle
end

Public Instance Methods

call(image, pixbuf) click to toggle source
# File lib/morandi/image-ops.rb, line 71
def call(image, pixbuf)
  if @angle.zero?
    pixbuf
  else
    surface = Cairo::ImageSurface.new(:rgb24, pixbuf.width, pixbuf.height)

    rotationValueRad = @angle * (Math::PI/180)

    ratio = pixbuf.width.to_f/pixbuf.height
    rh = (pixbuf.height) / ((ratio * Math.sin(rotationValueRad.abs)) + Math.cos(rotationValueRad.abs))
    scale = pixbuf.height / rh.to_f.abs

    a_ratio = pixbuf.height.to_f/pixbuf.width
    a_rh = (pixbuf.width) / ((a_ratio * Math.sin(rotationValueRad.abs)) + Math.cos(rotationValueRad.abs))
    a_scale = pixbuf.width / a_rh.to_f.abs

    scale = a_scale if a_scale > scale

    cr = Cairo::Context.new(surface)
    #p [@angle, rotationValueRad, rh, scale, pixbuf.height]

    cr.translate(pixbuf.width / 2.0, pixbuf.height / 2.0)
    cr.rotate(rotationValueRad)
    cr.scale(scale, scale)
    cr.translate(pixbuf.width / -2.0, pixbuf.height / - 2.0)
    cr.set_source_pixbuf(pixbuf)

    cr.rectangle(0, 0, pixbuf.width, pixbuf.height)
    cr.paint(1.0)
    final_pb = surface.to_pixbuf
    cr.destroy
    surface.destroy
    return final_pb
  end
end