Module: Morandi::Utils
- Defined in:
- lib/morandi/utils.rb
Class Method Summary collapse
- .apply_crop(pixbuf, x, y, w, h, fill_col = 0xffffffff) ⇒ Object
- .autocrop_coords(iw, ih, width, height) ⇒ Object
- .constrain(val, min, max) ⇒ Object
Class Method Details
.apply_crop(pixbuf, x, y, w, h, fill_col = 0xffffffff) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/morandi/utils.rb', line 45 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 = Gdk::Pixbuf.new(Gdk::Pixbuf::ColorSpace::RGB, false, 8, w, 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 args = [pixbuf, paste_x, paste_y, copy_w, copy_h, paste_x - offset_x, paste_y - offset_y, 1, 1, Gdk::Pixbuf::INTERP_HYPER, 255] #p args base_pixbuf.composite!(*args) 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 = Gdk::Pixbuf.new(pixbuf, x, y, w, h) end pixbuf end |
.autocrop_coords(iw, ih, width, height) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/morandi/utils.rb', line 4 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) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/morandi/utils.rb', line 35 def constrain(val,min,max) if val < min min elsif val > max max else val end end |