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.
240 241 242 243 |
# File 'lib/devil.rb', line 240 def initialize(name, file) @name = name @file = file end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
238 239 240 |
# File 'lib/devil.rb', line 238 def file @file end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
238 239 240 |
# File 'lib/devil.rb', line 238 def name @name end |
Instance Method Details
#alienify ⇒ Object
applies a strange color distortion effect to the image giving a preternatural feel
423 424 425 426 |
# File 'lib/devil.rb', line 423 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.
376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/devil.rb', line 376 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.
429 430 431 432 |
# File 'lib/devil.rb', line 429 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])
513 514 515 516 |
# File 'lib/devil.rb', line 513 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.
482 483 484 485 |
# File 'lib/devil.rb', line 482 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.
353 354 355 356 |
# File 'lib/devil.rb', line 353 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.
341 342 343 344 |
# File 'lib/devil.rb', line 341 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)
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/devil.rb', line 400 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.
417 418 419 420 |
# File 'lib/devil.rb', line 417 def emboss action { ILU.Emboss } self end |
#enlarge_canvas(width, height) ⇒ Object
enlarge the canvas of current image to width
and height
.
359 360 361 362 363 364 365 366 |
# File 'lib/devil.rb', line 359 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.
489 490 491 492 |
# File 'lib/devil.rb', line 489 def equalize action { ILU.Equalize } self end |
#flip ⇒ Object
flip the image about its x axis.
501 502 503 504 |
# File 'lib/devil.rb', line 501 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.
247 248 249 250 251 252 |
# File 'lib/devil.rb', line 247 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.
465 466 467 468 |
# File 'lib/devil.rb', line 465 def gamma_correct(factor) action { ILU.GammaCorrect(factor) } self end |
#height ⇒ Object Also known as: rows
returns the height of the image.
265 266 267 |
# File 'lib/devil.rb', line 265 def height action { IL.GetInteger(IL::IMAGE_HEIGHT) } end |
#mirror ⇒ Object
reflect image about its y axis.
392 393 394 395 |
# File 'lib/devil.rb', line 392 def mirror action { ILU.Mirror } self end |
#negate ⇒ Object Also known as: negative
invert the color of every pixel in the image.
471 472 473 474 |
# File 'lib/devil.rb', line 471 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.
442 443 444 445 |
# File 'lib/devil.rb', line 442 def noisify(factor) action { ILU.Noisify(factor) } self end |
#pixelize(pixel_size) ⇒ Object
‘pixelize’ the image using a pixel size of pixel_size
.
435 436 437 438 |
# File 'lib/devil.rb', line 435 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)
290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/devil.rb', line 290 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 :filter hash parameter that maps to a valid scale filter Optional :gauss parameter - number of times to apply gaussian filter before resizing (default is 2) (see: Devil.set_options :scale_filter)
307 308 309 310 311 312 |
# File 'lib/devil.rb', line 307 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).
507 508 509 510 |
# File 'lib/devil.rb', line 507 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.
274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/devil.rb', line 274 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.
455 456 457 458 |
# File 'lib/devil.rb', line 455 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 Optional :gauss parameter - number of times to apply gaussian filter before resizing (default is 2) (see: Devil.set_options :scale_filter)
319 320 321 322 323 324 325 326 |
# File 'lib/devil.rb', line 319 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 :filter hash parameter that maps to a valid scale filter (see: Devil.set_options :scale_filter)
333 334 335 336 337 338 |
# File 'lib/devil.rb', line 333 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.
496 497 498 |
# File 'lib/devil.rb', line 496 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.
258 259 260 |
# File 'lib/devil.rb', line 258 def width action { IL.GetInteger(IL::IMAGE_WIDTH) } end |