Class: Morandi::ImageBorder

Inherits:
ImageOp
  • Object
show all
Defined in:
lib/morandi/image-ops.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ImageOp

new_from_hash, #priority

Constructor Details

#initialize(style = 'none', colour = 'white') ⇒ ImageBorder

Returns a new instance of ImageBorder.



158
159
160
161
162
# File 'lib/morandi/image-ops.rb', line 158

def initialize(style='none', colour='white')
  super()
  @style = style
  @colour = colour
end

Instance Attribute Details

#border_sizeObject

Returns the value of attribute border_size.



157
158
159
# File 'lib/morandi/image-ops.rb', line 157

def border_size
  @border_size
end

#colourObject

Returns the value of attribute colour.



157
158
159
# File 'lib/morandi/image-ops.rb', line 157

def colour
  @colour
end

#cropObject

Returns the value of attribute crop.



157
158
159
# File 'lib/morandi/image-ops.rb', line 157

def crop
  @crop
end

Returns the value of attribute print_size.



157
158
159
# File 'lib/morandi/image-ops.rb', line 157

def print_size
  @print_size
end

#shrinkObject

Returns the value of attribute shrink.



157
158
159
# File 'lib/morandi/image-ops.rb', line 157

def shrink
  @shrink
end

#sizeObject

Returns the value of attribute size.



157
158
159
# File 'lib/morandi/image-ops.rb', line 157

def size
  @size
end

#styleObject

Returns the value of attribute style.



157
158
159
# File 'lib/morandi/image-ops.rb', line 157

def style
  @style
end

Instance Method Details

#call(image, pixbuf) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/morandi/image-ops.rb', line 164

def call(image, pixbuf)
  return pixbuf unless %w[square retro].include? @style
  surface = Cairo::ImageSurface.new(:rgb24, pixbuf.width, pixbuf.height)
  cr = Cairo::Context.new(surface)

  img_width = pixbuf.width
  img_height = pixbuf.height

  cr.save do
    if @crop
      if @crop[0] < 0 || @crop[1] < 0
        img_width = size[0]
        img_height = size[1]
        cr.translate( - @crop[0], - @crop[1])
      end
    end

    cr.save do
      cr.set_operator :source
      cr.set_source_rgb 1, 1, 1
      cr.paint

      cr.rectangle(0, 0, img_width, img_height)
      case colour
      when 'dominant'
        pixbuf.scale_max(400).save(fn="/tmp/hist-#{$$}.#{Time.now.to_i}", 'jpeg')
        hgram = Colorscore::Histogram.new(fn)
        File.unlink(fn) rescue nil
        col =  hgram.scores.first[1]
        cr.set_source_rgb col.red/256.0, col.green/256.0, col.blue/256.0
      when 'retro'
        cr.set_source_rgb 1, 1, 0.8
      when 'black'
        cr.set_source_rgb 0, 0, 0
      else
        cr.set_source_rgb 1, 1, 1
      end
      cr.fill
    end
  end

  border_scale =  [img_width,img_height].max.to_f / print_size.max.to_i
  size = @border_size
  size *= border_scale
  x, y = size, size

  # This biggest impact will be on the smallest side, so to avoid white
  # edges between photo and border scale by the longest changed side.
  longest_side = [pixbuf.width, pixbuf.height].max.to_f

  # Should be less than 1
  pb_scale = (longest_side - (size * 2)) / longest_side

  if @crop
    if @crop[0] < 0 || @crop[1] < 0
      x -= @crop[0]
      y -= @crop[1]
    end
  end

  case style
  when 'retro'
    CairoUtils.rounded_rectangle(cr, x, y,
                                 img_width + x - (size*2), img_height+y-(size*2), size)
  when 'square'
      cr.rectangle(x, y, img_width - (size*2), img_height - (size*2))
  end
  cr.clip()

  if @shrink
    cr.translate(size, size)
    cr.scale(pb_scale, pb_scale)
  end
  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