Module: Wuffl::ImageActions

Defined in:
lib/wuffl/image_actions.rb

Class Method Summary collapse

Class Method Details

.img_dimensions_fi(filename) ⇒ Object

determine the dimensions of the given image with FastImage-gem



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/wuffl/image_actions.rb', line 5

def self.img_dimensions_fi (filename) 
  img_dim = FastImage.size("#{filename}") 
  img_orig_width_fi = img_dim[0]
  img_orig_height_fi = img_dim[1]

  if img_orig_width_fi > img_orig_height_fi
    is_orig_landscape = true
  else
    is_orig_landscape = false
  end

  fastimage_infos = [is_orig_landscape, img_orig_width_fi.to_i, img_orig_height_fi.to_i]
end

.landscape_pic(pb_orig, img_width, img_height, img_max_w, img_max_h, reduction_factor) ⇒ Object

resize the image if too big - for landscape mode



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wuffl/image_actions.rb', line 20

def self.landscape_pic(pb_orig, img_width, img_height, img_max_w, img_max_h, reduction_factor)
  if img_width > img_max_w || img_height > img_max_h
      while img_width > img_max_w || img_height > img_max_h do
        img_width *= reduction_factor
        img_height *= reduction_factor
      end
  end

  img_width = img_width.to_i
  img_height = img_height.to_i
  pb_orig = pb_orig.scale(img_width, img_height, :bilinear)
  return pb_orig
end

.portrait_pic(pb_orig, rotate_pic, img_width, img_height, img_max_h, reduction_factor) ⇒ Object

resize the image if too big - for portrait mode



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/wuffl/image_actions.rb', line 35

def self.portrait_pic(pb_orig, rotate_pic, img_width, img_height, img_max_h, reduction_factor)
  if rotate_pic == true
    pb_orig = pb_orig.rotate(:clockwise)
    z = img_width
    img_width = img_height
    img_height = z
  end

  if img_height > img_max_h
    while img_height > img_max_h do
      img_width *= reduction_factor
      img_height *= reduction_factor
    end
  end 
  
    img_height = img_height.to_i
    img_width = img_width.to_i

    pb_orig = pb_orig.scale(img_width, img_height, :bilinear)
    return pb_orig
end

.prepare_pixbuf(filename, array_of_orig_pixbufs, img_max_w, img_max_h, reduction_factor) ⇒ Object

prepare image for loading



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
# File 'lib/wuffl/image_actions.rb', line 58

def self.prepare_pixbuf(filename, array_of_orig_pixbufs, img_max_w, img_max_h, reduction_factor)
  fi_infos = img_dimensions_fi(filename)
  pixbuf_infos = GdkPixbuf::Pixbuf.get_file_info(filename)
  is_landscape = fi_infos[0]
  pixbuf_width = pixbuf_infos[1]
  pixbuf_height = pixbuf_infos[2]

  img_width = pixbuf_width
  img_height = pixbuf_height

  pb_orig = GdkPixbuf::Pixbuf.new :file => filename, :width => img_width, :height => img_height
  if fi_infos[1] > fi_infos[2] #landscape format
    pb_orig = landscape_pic(pb_orig, img_width, img_height, img_max_w, img_max_h, reduction_factor)
  else #portrait format
    if (fi_infos[1] == pixbuf_infos[1]) && (fi_infos[2] == pixbuf_infos[2])
      rotate_pic = false
    else
      rotate_pic = true
    end 
    pb_orig = portrait_pic(pb_orig, rotate_pic, img_width, img_height, img_max_h, reduction_factor)
  end

  array_of_orig_pixbufs << pb_orig
  return [array_of_orig_pixbufs, is_landscape]
end

.show_img(img_curr, pb_curr) ⇒ Object

display the current image



85
86
87
88
# File 'lib/wuffl/image_actions.rb', line 85

def self.show_img(img_curr, pb_curr)
  img_curr.set_pixbuf(pb_curr)
  
end