Module: Imlib2Processor

Defined in:
lib/dimension/processors/imlib2.rb

Instance Method Summary collapse

Instance Method Details

#closeObject



46
47
48
49
50
# File 'lib/dimension/processors/imlib2.rb', line 46

def close
  log "Closing image."
  FileUtils.rm(@temp_file) if @temp_file
  image.delete!(true) # free image, and de-cache it too
end

#crop(width, height, x, y, gravity) ⇒ Object



88
89
90
91
# File 'lib/dimension/processors/imlib2.rb', line 88

def crop(width, height, x, y, gravity)
  rect = [(x || 0).to_i, (y || 0).to_i, width.to_i, height.to_i]
  image.crop!(rect)
end

#crop_scaled(x, y, new_w, new_h) ⇒ Object



93
94
95
96
# File 'lib/dimension/processors/imlib2.rb', line 93

def crop_scaled(x, y, new_w, new_h)
  log "Resizing #{image.w}x#{image.h} to #{new_w}x#{new_h}. Offset at #{x},#{y}"
  image.crop_scaled!(x, y, image.w, image.h, new_w.to_i, new_h.to_i)
end

#formatObject



13
14
15
16
17
# File 'lib/dimension/processors/imlib2.rb', line 13

def format
  image.format # .gsub('jpeg', 'jpg')
rescue ArgumentError
  File.extname(file).sub('.', '')
end

#geometryObject



9
10
11
# File 'lib/dimension/processors/imlib2.rb', line 9

def geometry
  [image.w, image.h]
end

#get_center_offset(w, h) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/dimension/processors/imlib2.rb', line 106

def get_center_offset(w, h)
  w_diff = image.w - w.to_i
  h_diff = image.h - h.to_i
  log "Width diff: #{w_diff}"
  log "Height diff: #{h_diff}"

  if w_diff == h_diff
    return [0, 0]
  elsif w_diff > h_diff
    return [w_diff.to_f/2, 0]
  else
    return [0, h_diff.to_f/2]
  end
end

#get_new_geometryObject



52
53
54
# File 'lib/dimension/processors/imlib2.rb', line 52

def get_new_geometry
  geometry
end

#get_offset(w, h, gravity = 'c') ⇒ Object



98
99
100
101
102
103
104
# File 'lib/dimension/processors/imlib2.rb', line 98

def get_offset(w, h, gravity = 'c')
  if gravity.nil? or gravity == '' or gravity == 'c'
    return get_center_offset(w, h)
  else
    raise 'Not implemented'
  end
end

#get_resize_geometry(w, h, to_longer = true) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/dimension/processors/imlib2.rb', line 121

def get_resize_geometry(w, h, to_longer = true)
  if to_longer or h.nil?
    if image.w < image.h
      new_h = ((w.to_f / image.w) * image.h).round
      return w.to_i, new_h
    else
      new_w = ((h.to_f / image.h) * image.w).round
      return new_w, h.to_i
    end
  else
    if w && image.w >= image.h
      new_h = ((w.to_f / image.w) * image.h).round
      return w.to_i, new_h
    else
      new_w = ((h.to_f / image.h) * image.w).round
      return new_w, h.to_i
    end
  end
end

#imageObject



5
6
7
# File 'lib/dimension/processors/imlib2.rb', line 5

def image
  @image ||= Imlib2::Image.load(@file)
end

#image_dataObject



30
31
32
33
34
35
36
# File 'lib/dimension/processors/imlib2.rb', line 30

def image_data
  unless @temp_file
    @temp_file = "/tmp/#{$$}.#{File.basename(file)}"
    save_as(@temp_file)
  end
  IO.read(@temp_file)
end

#resize(w, h) ⇒ Object

pablotron.org/software/imlib2-ruby/doc/

iw, ih = old_image.width, old_image.height
new_w, new_h = iw - 20, ih - 20
values = [10, 10, iw - 10, iw - 10, new_w, new_h]
new_image = old_image.crop_scaled values


63
64
65
66
# File 'lib/dimension/processors/imlib2.rb', line 63

def resize(w, h)
  new_w, new_h = get_resize_geometry(w, h, false)
  crop_scaled(0, 0, new_w, new_h)
end

#resize_and_crop(w, h, gravity) ⇒ Object

def resize_and_fill(w, h)

  new_w, new_h = get_resize_geometry(w, h, false)
  x = (w.to_i - new_w)
  y = (h.to_i - new_h)
  image.crop_scaled!(x*-1, y*-1, image.w + x*2, image.h + y*2, w.to_i, h.to_i)
end


75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dimension/processors/imlib2.rb', line 75

def resize_and_crop(w, h, gravity)
  new_w, new_h = get_resize_geometry(w, h, true)
  crop_scaled(0, 0, new_w, new_h)
  # crop_scaled(0, 0, w, h)

  offset = get_offset(w, h, gravity)
  # original_height = image.h
  # original_width  = image.w

  image.crop!(offset[0], offset[1], w.to_i, h.to_i)
  # crop_scaled(offset[0], offset[1], new_w, new_h)
end

#save!Object



42
43
44
# File 'lib/dimension/processors/imlib2.rb', line 42

def save!
  image.save(file)
end

#save_as(new_file_path) ⇒ Object



38
39
40
# File 'lib/dimension/processors/imlib2.rb', line 38

def save_as(new_file_path)
  image.save(new_file_path)
end

#to_rgbObject

transforms data (RGBA buffer) into a array of RGB values



25
26
27
28
# File 'lib/dimension/processors/imlib2.rb', line 25

def to_rgb
  bytes = data.bytes
  (1..bytes.length).step(4).map { |i| [bytes[i-1],bytes[i],bytes[i+1]] }.flatten
end

#to_rgbaObject



19
20
21
22
# File 'lib/dimension/processors/imlib2.rb', line 19

def to_rgba
  bytes = data.bytes
  (1..bytes.length).step(4).map { |i| bytes[i..i+2] << bytes[i-1] }.flatten
end