| Class | RSILViewer |
| In: |
rsil/examples/rsilviewer.rb
|
| Parent: | TkRoot |
| Class: | RSILViewer |
| File: | rsilviewer.rb |
| Purpose: | RSILViewer represents a full working tool with GUI to demonstrate module RSIL. |
| Created by: | Mario Pehle, 2006/06/10 |
| Required modules: | - |
| Offers functions: | - |
| FILE_TYPES | = | %w(bmp jpg jpeg tif tiff gif png) | holds the file types of graphic files which can be processed | |
| FILTERS | = | ["CIAffineTransform", "CIBloom", "CIBumpDistortion", "CICircularScreen", "CICircularWrap", "CIColorControls", "CIColorCube", "CIDisintegrateWithMaskTransition", "CIDissolveTransition", "CIGaussianBlur", "CIGlassLozenge", "CIGloom", "CIHatchedScreen", "CIHeightFieldFromMask", "CIHoleDistortion", "CIHueAdjust", "CIKaleidoscope", "CILanczosScaleTransform", "CILineScreen", "CIMedianFilter", "CINoiseReduction", "CIPerspectiveTransform", "CIPinchDistortion", "CIPixellate", "CIPointillize", "CISepiaTone", "CISharpenLuminance", "CISourceOverCompositing", "CISpotLight", "CITorusLensDistortion", "CITwirlDistortion", "CIUnsharpMask", "CIVortexDistortion", "CIWhitePointAdjust"] | holds the filters which can be processed without modifying their attributes |
| frame | [RW] | hold the contents of the GUI |
| Description: | Cares for the GUI, that when a image is processed, the mouse shows a "working" cursor and is updated. |
| Precondition: | - |
| Postcondition: | - |
| Exceptions: | - |
| Uses: | self.cursor, self.update, yield |
| Returns: | self |
| Parameters: | Name | i/o/io | default | Meaning |
| : | (anonymous) | i | - | Block, the code to proceed |
# File rsil/examples/rsilviewer.rb, line 87
87: def busy
88: begin
89: self.cursor "watch"
90: self.update
91: yield
92: self.update
93: ensure
94: self.cursor ""
95: self.update
96: end
97: self
98: end
| Description: | Applys a given filter to a given file. Takes only the file data, if file is not the actual displayed file. |
| Precondition: | - |
| Postcondition: | - |
| Exceptions: | - |
| Uses: | RSFilter, RSImage, RSImageProperties |
| Returns: | String |
| Parameters: | Name | i/o/io | default | Meaning |
| : | filename | i | - | String, the name an path of the file to filter |
| : | filter | i | - | String, the system name of the filter to use (i.e. "CISepiaTone") |
# File rsil/examples/rsilviewer.rb, line 244
244: def filter_image filename, filter
245: rsfilter = RS::Graphics::RSFilter.new filter
246: rsfilter.set_defaults
247: unless filename == @filename
248: @filename = filename
249: @rsimage = RS::Graphics::RSImage.new @filename
250: end
251: rsfilter.inputImage = @rsimage
252: @rsimage.apply_filters! rsfilter
253: rsfilter = nil
254: return @rsimage.bytes(RS::Utils::RSImageProperties::FILE_TYPES[:jpg])
255: end
| Description: | Crawls a given directory for valid graphic files an returns their names within a sorted array. |
| Precondition: | - |
| Postcondition: | - |
| Exceptions: | - |
| Uses: | Array, Dir |
| Returns: | Array |
| Parameters: | Name | i/o/io | default | Meaning |
| : | (anonymous) | i | - | Block, the code to proceed |
# File rsil/examples/rsilviewer.rb, line 113
113: def get_files dir
114: files = []
115: FILE_TYPES.each { |t| files.concat Dir["#{dir}/*.#{t}"] }
116: files.sort
117: end
| Description: | Determines the screen size. |
| Precondition: | - |
| Postcondition: | - |
| Exceptions: | - |
| Uses: | TkWinfo |
| Returns: | Array |
| Parameters: | Name | i/o/io | default | Meaning |
# File rsil/examples/rsilviewer.rb, line 131
131: def screen_size
132: [TkWinfo.screenwidth(self), TkWinfo.screenheight(self)]
133: end
| Description: | Creates the GUI, adds listeners to its components. |
| Precondition: | - |
| Postcondition: | - |
| Exceptions: | - |
| Uses: | TkLabel, FILTERS, TkMenuBar, TkListBox, TkButton |
| Returns: | self |
| Parameters: | Name | i/o/io | default | Meaning |
# File rsil/examples/rsilviewer.rb, line 147
147: def setContent
148: @dir = '.'
149: title "RSILviewer v0.12"
150: geometry "#{(screen_size[0]*0.75).to_i}x#{(screen_size[1]*0.75).to_i}"
151:
152: # Build Folder-Chooser
153: frame_dir = TkFrame.new(self).pack(:side=>'top', :fill=>'x', :expand=>false)
154: TkLabel.new(frame_dir, :text=>'Directory:').pack(:side=>'left')
155: entry = TkEntry.new(frame_dir).pack(:side=>'left', :fill=>'x', :expand=>true)
156: button = TkButton.new(frame_dir, :text=>'...')
157: button.pack(:side=>'right')
158:
159: frame_list = TkFrame.new(self).pack(:side=>'left', :fill=> 'both')
160: list_w = TkListbox.new(frame_list, :selectmode=>'single')
161: list_w.yscrollbar(TkScrollbar.new(frame_list).pack(:side=>'right', :fill=>'y'))
162: list_w.pack(:side=>'left', :fill=>'y', :expand=>true)
163:
164: frame_image = TkFrame.new(self)
165: image = TkPhotoImage.new
166: label = TkLabel.new(frame_image, 'image'=>image, :anchor=>'w', :justify=>'left')
167: list_contents = self.get_files @dir
168:
169: filters = [['Filter', 0]]
170: FILTERS.each { |f|
171: filters << [RS::Graphics::RSFilter.localized_name(f), proc{ busy {
172: bin_data = self.filter_image list_contents[list_w.curselection[0]], f
173: tmp_img = TkPhotoImage.new(:data => Tk::BinaryString(bin_data))
174: bin_data = nil
175: scale_x = (tmp_img.width.to_f / label.winfo_width.to_f).ceil
176: scale_y = (tmp_img.height.to_f / label.winfo_height.to_f).ceil
177: scale = scale_x > scale_y ? scale_x : scale_y
178: scale = 1 if scale < 1
179: image.blank
180: image.copy(tmp_img, :subsample => [scale, scale])
181: tmp_img = nil
182: GC.start
183: } } ]
184: }
185: filtermenu = [filters]
186: menu TkMenubar.new(self, filtermenu, :tearoff=>false).pack(:fill=>'x', :side=>'top')
187: savebutton = TkButton.new(menu, :text=>'Save...')
188: savebutton.pack(:side=>'left')
189: label.pack(:side=>'left', :anchor=>'w', :fill=>'both', :expand=>true)
190: frame_image.pack(:side=>'left', :fill=>'both', :expand=>true)
191:
192: savebutton.bind('ButtonRelease-1') {
193: unless @rsimage.nil?
194: savefilename = Tk.getSaveFile(:initialfile=>"filtered.jpg")
195: @rsimage.save savefilename
196: end
197: }
198:
199: button.bind('ButtonRelease-1') {
200: busy {
201: dir = @dir.nil? ? chooseDirectory(:parent=>self) : chooseDirectory(:parent=>self, :initialdir=>@dir)
202: unless dir.nil? or dir.empty?
203: @dir = dir
204: entry.set @dir
205: list_contents = self.get_files @dir
206: list_w.clear
207: list_contents.each { |x| list_w.insert('end', x) }
208: unless list_contents.empty?
209: list_w.bind("ButtonRelease-1") {
210: busy {
211: tmp_img = TkPhotoImage.new('file' => list_contents[list_w.curselection[0]])
212: @rcimage = nil
213: scale_x = (tmp_img.width.to_f / label.winfo_width.to_f).ceil
214: scale_y = (tmp_img.height.to_f / label.winfo_height.to_f).ceil
215: scale = scale_x > scale_y ? scale_x : scale_y
216: scale = 1 if scale < 1
217: image.blank
218: image.copy(tmp_img, 'subsample' => [scale, scale])
219: tmp_img = nil
220: GC.start
221: }
222: }
223: end
224: end
225: }
226: }
227: self
228: end