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
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#alienify ⇒ Object
applies a strange color distortion effect to the image giving a preternatural feel.
-
#blit(source, x, y, options = {}) ⇒ Object
(also: #composite)
splice the
source
image into current image at positionx
andy
. -
#blur(iter) ⇒ Object
performs a gaussian blur on the image.
-
#clear ⇒ Object
simply clears the image to the ‘clear color’ (specified using Devil.set_options(:clear_color => [r, g, b, a]).
-
#contrast(factor) ⇒ Object
factor
describes desired contrast to use A value of 1.0 has no effect on the image. -
#crop(xoff, yoff, width, height) ⇒ Object
crop the current image.
-
#dup ⇒ Object
(also: #clone)
return a deep copy of the current image.
-
#edge_detect(options = {}) ⇒ Object
use prewitt or sobel filters to detect the edges in the current image.
-
#emboss ⇒ Object
embosses an image, causing it to have a “relief” feel to it using a convolution filter.
-
#enlarge_canvas(width, height) ⇒ Object
enlarge the canvas of current image to
width
andheight
. -
#equalize ⇒ Object
darkens the bright colours and lightens the dark colours, reducing the contrast in an image or ‘equalizing’ it.
-
#flip ⇒ Object
flip the image about its x axis.
-
#free ⇒ Object
(also: #close, #delete)
Frees the memory associated with the image.
-
#gamma_correct(factor) ⇒ Object
applies gamma correction to an image using an exponential curve.
-
#height ⇒ Object
(also: #rows)
returns the height of the image.
-
#initialize(name, file) ⇒ Image
constructor
A new instance of Image.
-
#mirror ⇒ Object
reflect image about its y axis.
-
#negate ⇒ Object
(also: #negative)
invert the color of every pixel in the image.
-
#noisify(factor) ⇒ Object
add random noise to the image.
-
#pixelize(pixel_size) ⇒ Object
‘pixelize’ the image using a pixel size of
pixel_size
. -
#resize(width, height, options = {}) ⇒ Object
resize the image to
width
andheight
. -
#resize2(width, height, options = {}) ⇒ Object
resize the image to
width
andheight
. -
#rotate(angle) ⇒ Object
rotate an image about its central point by
angle
degrees (counter clockwise). -
#save(file = @file, options = {}) ⇒ Object
saves the image to
file
. -
#sharpen(factor, iter) ⇒ Object
The sharpening
factor
must be in the range of 0.0 - 2.5. -
#show(x = Devil.get_options[:window_size][0] / 2, y = Devil.get_options[:window_size][1] / 2) ⇒ Object
display the Devil images on screen utilizing the Gosu library for visualization if
x
andy
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. -
#thumbnail(size, options = {}) ⇒ Object
Creates a proportional thumbnail of the image scaled so its longest edge is resized to
size
. -
#thumbnail2(size, options = {}) ⇒ Object
Creates a proportional thumbnail of the image scaled so its longest edge is resized to
size
. -
#to_blob ⇒ Object
returns the image data in the form of a ruby string.
-
#to_gosu(window) ⇒ Object
convert a Devil::Image to a Gosu::Image.
-
#width ⇒ Object
(also: #columns)
returns the width of the image.
Constructor Details
#initialize(name, file) ⇒ Image
Returns 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
#file ⇒ Object (readonly)
Returns the value of attribute file.
243 244 245 |
# File 'lib/devil.rb', line 243 def file @file end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
243 244 245 |
# File 'lib/devil.rb', line 243 def name @name end |
Instance Method Details
#alienify ⇒ Object
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 |
#blit(source, x, y, options = {}) ⇒ Object 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 |
#blur(iter) ⇒ Object
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 |
#clear ⇒ Object
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 |
#contrast(factor) ⇒ Object
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 |
#crop(xoff, yoff, width, height) ⇒ Object
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 |
#dup ⇒ Object 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 |
#edge_detect(options = {}) ⇒ Object
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 |
#emboss ⇒ Object
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 |
#enlarge_canvas(width, height) ⇒ Object
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 |
#equalize ⇒ Object
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 |
#flip ⇒ Object
flip the image about its x axis.
506 507 508 509 |
# File 'lib/devil.rb', line 506 def flip action { ILU.FlipImage } self end |
#free ⇒ Object 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 |
#gamma_correct(factor) ⇒ Object
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 |
#height ⇒ Object 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 |
#mirror ⇒ Object
reflect image about its y axis.
397 398 399 400 |
# File 'lib/devil.rb', line 397 def mirror action { ILU.Mirror } self end |
#negate ⇒ Object 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 |
#noisify(factor) ⇒ Object
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 |
#pixelize(pixel_size) ⇒ Object
‘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 |
#resize(width, height, options = {}) ⇒ Object
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 |
#resize2(width, height, options = {}) ⇒ Object
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 |
#rotate(angle) ⇒ Object
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 |
#save(file = @file, options = {}) ⇒ Object
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 |
#sharpen(factor, iter) ⇒ Object
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 |
#show(x = Devil.get_options[:window_size][0] / 2, y = Devil.get_options[:window_size][1] / 2) ⇒ Object
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 |
#thumbnail(size, options = {}) ⇒ Object
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 |
#thumbnail2(size, options = {}) ⇒ Object
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 |
#to_blob ⇒ Object
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 |
#to_gosu(window) ⇒ Object
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 |
#width ⇒ Object 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 |