Class: Devil::Image
- Inherits:
-
Object
- Object
- Devil::Image
- Defined in:
- lib/devil.rb,
lib/devil/gosu.rb
Overview
wraps a DevIL image
Instance Attribute Summary (collapse)
-
- (Object) file
readonly
Returns the value of attribute file.
-
- (Object) name
readonly
Returns the value of attribute name.
Instance Method Summary (collapse)
-
- (Object) alienify
applies a strange color distortion effect to the image giving a preternatural feel.
-
- (Object) blit(source, x, y, options = {})
(also: #composite)
splice the source image into current image at position x and y.
-
- (Object) blur(iter)
performs a gaussian blur on the image.
-
- (Object) clear
simply clears the image to the 'clear color' (specified using Devil.set_options(:clear_color => [r, g, b, a]).
-
- (Object) contrast(factor)
factor describes desired contrast to use A value of 1.0 has no effect on the image.
-
- (Object) crop(xoff, yoff, width, height)
crop the current image.
-
- (Object) dup
(also: #clone)
return a deep copy of the current image.
-
- (Object) edge_detect(options = {})
use prewitt or sobel filters to detect the edges in the current image.
-
- (Object) emboss
embosses an image, causing it to have a "relief" feel to it using a convolution filter.
-
- (Object) enlarge_canvas(width, height)
enlarge the canvas of current image to width and height.
-
- (Object) equalize
darkens the bright colours and lightens the dark colours, reducing the contrast in an image or 'equalizing' it.
-
- (Object) flip
flip the image about its x axis.
-
- (Object) free
(also: #close, #delete)
Frees the memory associated with the image.
-
- (Object) gamma_correct(factor)
applies gamma correction to an image using an exponential curve.
-
- (Object) height
(also: #rows)
returns the height of the image.
-
- (Image) initialize(name, file)
constructor
A new instance of Image.
-
- (Object) mirror
reflect image about its y axis.
-
- (Object) negate
(also: #negative)
invert the color of every pixel in the image.
-
- (Object) noisify(factor)
add random noise to the image.
-
- (Object) pixelize(pixel_size)
'pixelize' the image using a pixel size of pixel_size.
-
- (Object) resize(width, height, options = {})
resize the image to width and height.
-
- (Object) resize2(width, height, options = {})
resize the image to width and height.
-
- (Object) rotate(angle)
rotate an image about its central point by angle degrees (counter clockwise).
-
- (Object) save(file = @file, options = {})
saves the image to file.
-
- (Object) sharpen(factor, iter)
The sharpening factor must be in the range of 0.0 - 2.5.
-
- (Object) show(x = Devil.get_options[:window_size][0] / 2, y = Devil.get_options[:window_size][1] / 2)
display the Devil images on screen utilizing the Gosu library for visualization if x and y are specified then show the image centered at this location, otherwise draw the image at the center of the screen This method is only available if require 'devil/gosu' is used.
-
- (Object) thumbnail(size, options = {})
Creates a proportional thumbnail of the image scaled so its longest edge is resized to size.
-
- (Object) thumbnail2(size, options = {})
Creates a proportional thumbnail of the image scaled so its longest edge is resized to size.
-
- (Object) to_blob
returns the image data in the form of a ruby string.
-
- (Object) to_gosu(window)
convert a Devil::Image to a Gosu::Image.
-
- (Object) width
(also: #columns)
returns the width of the image.
Constructor Details
- (Image) initialize(name, file)
A new instance of Image
245 246 247 248 |
# File 'lib/devil.rb', line 245 def initialize(name, file) @name = name @file = file end |
Instance Attribute Details
- (Object) file (readonly)
Returns the value of attribute file
243 244 245 |
# File 'lib/devil.rb', line 243 def file @file end |
- (Object) name (readonly)
Returns the value of attribute name
243 244 245 |
# File 'lib/devil.rb', line 243 def name @name end |
Instance Method Details
- (Object) alienify
applies a strange color distortion effect to the image giving a preternatural feel
428 429 430 431 |
# File 'lib/devil.rb', line 428 def alienify action { ILU.Alienify } self end |
- (Object) blit(source, x, y, options = {}) Also known as: composite
splice the source image into current image at position x and y. Takes an optional :crop hash parameter that has the following format: :crop => [sx, sy, width, height] sx, sy, +width, height crop the source image to be spliced. sx is how many pixels to skip in x direction of source image. sy is how many pixels to skip in y direction of source image. width number of pixels to preserve in x direction of source image. height number of pixels to preserve in y direction of source image. if no :crop parameter is provided then the whole image is spliced in.
381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/devil.rb', line 381 def blit(source, x, y, = {}) = { :crop => [0, 0, source.width, source.height] }.merge!() action do IL.Blit(source.name, x, y, 0, [:crop][0], [:crop][1], 0, [:crop][2], [:crop][3], 1) end self end |
- (Object) blur(iter)
performs a gaussian blur on the image. The blur is performed iter times.
434 435 436 437 |
# File 'lib/devil.rb', line 434 def blur(iter) action { ILU.BlurGaussian(iter) } self end |
- (Object) clear
simply clears the image to the 'clear color' (specified using Devil.set_options(:clear_color => [r, g, b, a])
518 519 520 521 |
# File 'lib/devil.rb', line 518 def clear action { IL.ClearImage } self end |
- (Object) contrast(factor)
factor describes desired contrast to use A value of 1.0 has no effect on the image. Values between 1.0 and 1.7 increase the amount of contrast (values above 1.7 have no effect) Valid range of factor is 0.0 - 1.7.
487 488 489 490 |
# File 'lib/devil.rb', line 487 def contrast(factor) action { ILU.Contrast(factor) } self end |
- (Object) crop(xoff, yoff, width, height)
crop the current image. xoff number of pixels to skip in x direction. yoff number of pixels to skip in y direction. width number of pixels to preserve in x direction. height number of pixels to preserve in y direction.
358 359 360 361 |
# File 'lib/devil.rb', line 358 def crop(xoff, yoff, width, height) action { ILU.Crop(xoff, yoff, 1, width, height, 1) } self end |
- (Object) dup Also known as: clone
return a deep copy of the current image.
346 347 348 349 |
# File 'lib/devil.rb', line 346 def dup new_image_name = action { IL.CloneCurImage } Devil::Image.new(new_image_name, nil) end |
- (Object) edge_detect(options = {})
use prewitt or sobel filters to detect the edges in the current image. Optional :filter hash parameter selects filter to use (:prewitt or :sobel). (see: Devil.set_options :edge_filter)
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/devil.rb', line 405 def edge_detect(={}) = { :filter => Devil.[:edge_filter] }.merge!() case [:filter] when :prewitt action { ILU.EdgeDetectP } when :sobel action { ILU.EdgeDetectS } else raise "No such edge filter #{[:filter]}. Use :prewitt or :sobel" end self end |
- (Object) emboss
embosses an image, causing it to have a "relief" feel to it using a convolution filter.
422 423 424 425 |
# File 'lib/devil.rb', line 422 def emboss action { ILU.Emboss } self end |
- (Object) enlarge_canvas(width, height)
enlarge the canvas of current image to width and height.
364 365 366 367 368 369 370 371 |
# File 'lib/devil.rb', line 364 def enlarge_canvas(width, height) if width < self.width || height < self.height raise "width and height parameters must be larger than current image width and height" end action { ILU.EnlargeCanvas(width, height, 1) } self end |
- (Object) equalize
darkens the bright colours and lightens the dark colours, reducing the contrast in an image or 'equalizing' it.
494 495 496 497 |
# File 'lib/devil.rb', line 494 def equalize action { ILU.Equalize } self end |
- (Object) flip
flip the image about its x axis.
506 507 508 509 |
# File 'lib/devil.rb', line 506 def flip action { ILU.FlipImage } self end |
- (Object) free Also known as: close, delete
Frees the memory associated with the image. Must be called explictly if load_image or create_image is invoked without a block.
252 253 254 255 256 257 |
# File 'lib/devil.rb', line 252 def free raise "Error: calling 'free' on already freed image! #{self}" if !@name IL.DeleteImages([@name]) error_check @name = nil end |
- (Object) gamma_correct(factor)
applies gamma correction to an image using an exponential curve. factor is gamma correction factor to use. A value of 1.0 leaves the image unmodified. Values in the range 0.0 - 1.0 darken the image Values above 1.0 brighten the image.
470 471 472 473 |
# File 'lib/devil.rb', line 470 def gamma_correct(factor) action { ILU.GammaCorrect(factor) } self end |
- (Object) height Also known as: rows
returns the height of the image.
270 271 272 |
# File 'lib/devil.rb', line 270 def height action { IL.GetInteger(IL::IMAGE_HEIGHT) } end |
- (Object) mirror
reflect image about its y axis.
397 398 399 400 |
# File 'lib/devil.rb', line 397 def mirror action { ILU.Mirror } self end |
- (Object) negate Also known as: negative
invert the color of every pixel in the image.
476 477 478 479 |
# File 'lib/devil.rb', line 476 def negate action { ILU.Negative } self end |
- (Object) noisify(factor)
add random noise to the image. factor is the tolerance to use. accepeted values range from 0.0 - 1.0.
447 448 449 450 |
# File 'lib/devil.rb', line 447 def noisify(factor) action { ILU.Noisify(factor) } self end |
- (Object) pixelize(pixel_size)
'pixelize' the image using a pixel size of pixel_size.
440 441 442 443 |
# File 'lib/devil.rb', line 440 def pixelize(pixel_size) action { ILU.Pixelize(pixel_size) } self end |
- (Object) resize(width, height, options = {})
resize the image to width and height. Aspect ratios of the image do not have to be the same. Optional :filter hash parameter that maps to a valid scale filter (see: Devil.set_options :scale_filter)
295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/devil.rb', line 295 def resize(width, height, = {}) filter = [:filter] action do ILU.ImageParameter(ILU::FILTER, filter) if filter ILU.Scale(width, height, 1) ILU.ImageParameter(ILU::FILTER, Devil.[:scale_filter]) if filter end self end |
- (Object) resize2(width, height, options = {})
resize the image to width and height. Aspect ratios of the image do not have to be the same. Applies a gaussian filter before rescaling to reduce aliasing. Optional :gauss parameter - number of times to apply gaussian filter before resizing (default is 2) Optional :filter hash parameter that maps to a valid scale filter (see: Devil.set_options :scale_filter)
312 313 314 315 316 317 |
# File 'lib/devil.rb', line 312 def resize2(width, height, = {}) gauss = [:gauss] ||= 2 blur(gauss) if (width < self.width && height < self.height) resize(width, height, ) self end |
- (Object) rotate(angle)
rotate an image about its central point by angle degrees (counter clockwise).
512 513 514 515 |
# File 'lib/devil.rb', line 512 def rotate(angle) action { ILU.Rotate(angle) } self end |
- (Object) save(file = @file, options = {})
saves the image to file. If no file is provided default to the opened file. Optional :quality hash parameter (only applies to JPEG images). Valid values are in the 0-99 range, with 99 being the best quality.
279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/devil.rb', line 279 def save(file = @file, = {}) quality = [:quality] raise "This image does not have an associated file. Please provide an explicit file name when saving." if !file action do IL.SetInteger(IL::JPG_QUALITY, quality) if quality IL.SaveImage(file) IL.SetInteger(IL::JPG_QUALITY, Devil.[:jpg_quality]) if quality end self end |
- (Object) sharpen(factor, iter)
The sharpening factor must be in the range of 0.0 - 2.5. A value of 1.0 for the sharpening. factor will have no effect on the image. Values in the range 1.0 - 2.5 will sharpen the image, with 2.5 having the most pronounced sharpening effect. Values from 0.0 to 1.0 do a type of reverse sharpening, blurring the image. Values outside of the 0.0 - 2.5 range produce undefined results.
The number of iter (iterations) to perform will usually be 1, but to achieve more sharpening, increase the number of iterations.
460 461 462 463 |
# File 'lib/devil.rb', line 460 def sharpen(factor, iter) action { ILU.Sharpen(factor, iter) } self end |
- (Object) show(x = Devil.get_options[:window_size][0] / 2, y = Devil.get_options[:window_size][1] / 2)
display the Devil images on screen utilizing the Gosu library for visualization if x and y are specified then show the image centered at this location, otherwise draw the image at the center of the screen This method is only available if require 'devil/gosu' is used
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/devil/gosu.rb', line 86 def show(x = Devil.[:window_size][0] / 2, y = Devil.[:window_size][1] / 2) if !Devil.const_defined?(:Window) c = Class.new(Gosu::Window) do attr_accessor :show_list def initialize super(Devil.[:window_size][0], Devil.[:window_size][1], false) @show_list = [] end def draw # :nodoc: @show_list.each { |v| v[:image].draw_rot(v[:x], v[:y], 1, 0) } exit if (Gosu::KbEscape) end end Devil.const_set :Window, c end if !defined? @@window @@window ||= Devil::Window.new at_exit { @@window.show } end # note we dup the image so the displayed image is a snapshot taken at the time #show is invoked img = self.dup.flip @@window.show_list.push :image => Gosu::Image.new(@@window, img), :x => x, :y => y img.free self end |
- (Object) thumbnail(size, options = {})
Creates a proportional thumbnail of the image scaled so its longest edge is resized to size. Optional :filter hash parameter that maps to a valid scale filter (see: Devil.set_options :scale_filter)
323 324 325 326 327 328 329 330 |
# File 'lib/devil.rb', line 323 def thumbnail(size, = {}) # this thumbnail code from image_science.rb w, h = width, height scale = size.to_f / (w > h ? w : h) resize((w * scale).to_i, (h * scale).to_i, ) self end |
- (Object) thumbnail2(size, options = {})
Creates a proportional thumbnail of the image scaled so its longest edge is resized to size. Applies a gaussian filter before rescaling to reduce aliasing. Optional :gauss parameter - number of times to apply gaussian filter before resizing (default is 2) Optional :filter hash parameter that maps to a valid scale filter (see: Devil.set_options :scale_filter)
338 339 340 341 342 343 |
# File 'lib/devil.rb', line 338 def thumbnail2(size, = {}) gauss = [:gauss] ||= 2 blur(gauss) thumbnail(size, ) self end |
- (Object) to_blob
returns the image data in the form of a ruby string. The image data is formatted to RGBA / UNSIGNED BYTE.
501 502 503 |
# File 'lib/devil.rb', line 501 def to_blob action { IL.ToBlob } end |
- (Object) to_gosu(window)
convert a Devil::Image to a Gosu::Image. Must provide a window parameter, as per Gosu::Image#new() This method is only available if require 'devil/gosu' is used
78 79 80 |
# File 'lib/devil/gosu.rb', line 78 def to_gosu(window) Gosu::Image.new(window, self) end |
- (Object) width Also known as: columns
returns the width of the image.
263 264 265 |
# File 'lib/devil.rb', line 263 def width action { IL.GetInteger(IL::IMAGE_WIDTH) } end |