module Morandi::Utils

Public Instance Methods

apply_crop(pixbuf, x, y, w, h, fill_col = 0xffffffff) click to toggle source
# File lib/morandi/utils.rb, line 47
def apply_crop(pixbuf, x, y, w, h, fill_col = 0xffffffff)
  if (x < 0) or (y < 0) || ((x+w) > pixbuf.width) || ((y+h) > pixbuf.height)
    #tw, th = [w-x,w].max, [h-y,h].max
    base_pixbuf = GdkPixbuf::Pixbuf.new(
      colorspace: GdkPixbuf::Colorspace::RGB,
      has_alpha: false,
      bits_per_sample: 8,
      width: w,
      height: h
    )
    base_pixbuf.fill!(fill_col)
    dest_x = [x, 0].min
    dest_y = [y, 0].min
    #src_x = [x,0].max
    #src_y = [y,0].max
    dest_x = [-x,0].max
    dest_y = [-y,0].max

    #if x < 0
    #else
    #end
    #if y < 0
    #  dest_h = [h-dest_y, pixbuf.height, base_pixbuf.height-dest_y].min
    #else
    #       dest_h = [h,pixbuf.height].min
    #end
    #  dest_w  = [w-dest_x, pixbuf.width, base_pixbuf.width-dest_x].min

    offset_x = [x,0].max
    offset_y = [y,0].max
    copy_w = [w, pixbuf.width - offset_x].min
    copy_h = [h, pixbuf.height - offset_y].min

    paste_x = [x, 0].min * -1
    paste_y = [y, 0].min * -1

    if copy_w + paste_x > base_pixbuf.width
      copy_w = base_pixbuf.width - paste_x
    end
    if copy_h + paste_y > base_pixbuf.height
      copy_h = base_pixbuf.height - paste_y
    end
    base_pixbuf.composite! pixbuf, {
      dest_x: paste_x,
      dest_y: paste_y,
      dest_width: copy_w,
      dest_height: copy_h,
      offset_x: paste_x - offset_x,
      offset_y: paste_y - offset_y,
      scale_x: 1,
      scale_y: 1,
      interpolation_type: :hyper,
      overall_alpha: 255
    }
    pixbuf = base_pixbuf
  else
    x = constrain(x, 0, pixbuf.width)
    y = constrain(y, 0, pixbuf.height)
    w = constrain(w, 1, pixbuf.width - x)
    h = constrain(h, 1, pixbuf.height - y)
    #p [pixbuf, x, y, w, h]
    pixbuf = pixbuf.subpixbuf(x, y, w, h)
  end
  pixbuf
end
autocrop_coords(iw, ih, width, height) click to toggle source
# File lib/morandi/utils.rb, line 6
def autocrop_coords(iw, ih, width, height)
  return nil unless width
  aspect = width.to_f / height.to_f
  iaspect = iw.to_f / ih.to_f

  if ih > iw
    # Portrait image
    # Check whether the aspect ratio is greater or smaller
    # ie. where constraints will hit
    aspect = height.to_f / width.to_f
  end

  # Landscape
  if aspect > iaspect
    # Width constraint - aspect-rect wider
    crop_width  = iw
    crop_height = (crop_width / aspect).to_i
  else
    # Height constraint - aspect-rect wider
    crop_height = ih
    crop_width  = (crop_height * aspect).to_i
  end

  [
    ((iw - crop_width)>>1),
    ((ih - crop_height)>>1),
    crop_width,
    crop_height
  ].map { |i| i.to_i }
end
constrain(val,min,max) click to toggle source
# File lib/morandi/utils.rb, line 37
def constrain(val,min,max)
  if val < min
    min
  elsif val > max
    max
  else
    val
  end
end