Class: ImageProcessor

Inherits:
Object
  • Object
show all
Includes:
Fox
Defined in:
lib/piggy-gui/image_processor.rb

Overview

Utilizes image processing via FXRuby.

Instance Method Summary collapse

Constructor Details

#initialize(application) ⇒ ImageProcessor

Returns a new instance of ImageProcessor.



10
11
12
# File 'lib/piggy-gui/image_processor.rb', line 10

def initialize application
  @app = application
end

Instance Method Details

#basic_load(img, rubyfile, create) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/piggy-gui/image_processor.rb', line 28

def basic_load(img, rubyfile, create)
  return nil if img == nil
  file = ruby_2_fox(rubyfile)
  begin
    FXFileStream.open(file, FXStreamLoad) do
      |stream|
      img.loadPixels stream
    end
    img.create if create
  rescue FXStreamNoReadError
    puts "File not found: #{file}"
  end
  return img
end

#destroy_all(imageCache) ⇒ Object



56
57
58
59
# File 'lib/piggy-gui/image_processor.rb', line 56

def destroy_all(imageCache)
  imageCache.each { |i| uncreate_image(i) }
  imageCache.clear
end

#fit_image(img, maxW, maxH, quality = 1, widescreen_w = -1)) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/piggy-gui/image_processor.rb', line 66

def fit_image(img, maxW, maxH, quality = 1, widescreen_w = -1)
  w = img.getWidth
  h = img.getHeight
  used_width = (widescreen_w > 0 and h.to_f > 0 and w.to_f/h.to_f > 1.6) ? widescreen_w : maxW
  ratios = Array.new(2)
  ratios[0] = used_width.to_f / w.to_f
  ratios[1] = maxH.to_f / h.to_f
  ratio = ratios.min
  new_w = (w * ratio).to_i
  new_h = (h * ratio).to_i
  if new_w < 10 or new_h < 10
    return false
  end
  img.scale(new_w, new_h, quality)
  return true
end

#garbage_image(img) ⇒ Object



61
62
63
64
# File 'lib/piggy-gui/image_processor.rb', line 61

def garbage_image(img)
  uncreate_image(img)
  GC.start
end

#is_supported_image_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/piggy-gui/image_processor.rb', line 14

def is_supported_image_file? file
  return get_image_class_for_file(file) != nil
end

#load_icon(file, create = true) ⇒ Object



18
19
20
21
# File 'lib/piggy-gui/image_processor.rb', line 18

def load_icon(file, create = true)
  img = get_icon_instance_for_file file
  return basic_load(img, file, create)
end

#load_image(file, create = true) ⇒ Object



23
24
25
26
# File 'lib/piggy-gui/image_processor.rb', line 23

def load_image(file, create = true)
  img = get_image_instance_for_file file
  return basic_load(img, file, create)
end

#rotate_image(img, degree) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/piggy-gui/image_processor.rb', line 83

def rotate_image(img, degree)
  fox_degree = case degree
  when 0 then 0
  when 90 then 270
  when 180 then 180		  
  when 270 then 90
  else
    return error('Illegal degree')
  end
  img.rotate(fox_degree)
end

#save_image(img, rubyfile) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/piggy-gui/image_processor.rb', line 43

def save_image(img, rubyfile)
  file = ruby_2_fox(rubyfile)
  FXFileStream.open(file, FXStreamSave) do
    |stream|
    img.savePixels stream
  end
end

#uncreate_image(img) ⇒ Object



51
52
53
54
# File 'lib/piggy-gui/image_processor.rb', line 51

def uncreate_image(img)
  img.destroy
  img.release
end