module CrImage::Image

Overview

Image is a finite rectangular grid of Color::Color values taken from a color model An Image maps every grid square in a Rectangle to a Color from a Model. "The pixel at (x, y)" refers to the color of the grid square defined by the points (x, y), (x+1, y), (x+1, y+1) and (x, y+1). A common mistake is assuming that an Image's bounds start at (0, 0). For example, an animated GIF contains a sequence of Images, and each Image after the first typically only holds pixel data for the area that changed, and that area doesn't necessarily start at (0, 0). The correct way to iterate over an Image m's pixels looks like:

b = m.bounds
b.min.y.upto(b.max.y - 1) do |y|
  b.min.x.upto(b.max.x - 1) do |x|
    do_stuff_with(m[x, y]) # or call `m.at(x,y)`
  end
end

Image implementations do not have to be based on an in-memory slice of pixel data. For example, a CrImage::Uniform is an Image of enormous bounds and uniform color, whose in-memory representation is simply that color. Typically, though, programs will want an image based on a Slice. Classes types like CrImage::RGBA and CrImage::Gray etc (Look below into Including Types) hold slices of pixel data and implement the Image interface. These types also provide a set(x, y , c : Color::Color) method that allows modifying the image one pixel at a time.

m = CrImage::RGBA.new(CrImage.rect(0,0,640,480))
m[5,5] = Color::RGBA.new(255,0,0,255)) #Or call set method like  `m.set(5,5, Color::RGBA.new(255,0,0,255))`

If you're reading or writing a lot of pixel data, it can be more efficient, but more complicated, to access these classes pix field directly. The slice-based Image implementations also provide a sub_image method, which returns an Image backed by the same array. Modifying the pixels of a sub-image will affect the pixels of the original image, analogous to how modifying the contents of a sub-slice s[i0..i1] will affect the contents of the original slice s.

m0 = CrImage::RGBA.new(CrImage.rect(0, 0, 8, 5))
m1 = m0.sub_image(CrImage.rect(1, 2, 5, 5)).as(CrImage::RGBA)
pp "#{m0.bounds.width}, #{m1.bounds.width}" # => 8,4
pp m0.stride == m1.stride                   # => true

For low-level code that works on an image's Pix field, be aware that ranging over Pix can affect pixels outside an image's bounds. In the example above, the pixels covered by m1.pix are shaded in blue. Higher-level code, such as the at and set methods or the CrImage::Draw package, will clip their operations to the image's bounds.

Defined in:

lib/crimage/src/crimage/clip.cr
lib/crimage/src/crimage/image.cr
lib/crimage/src/crimage/image/extensions.cr
lib/crimage/src/crimage/pipeline.cr
lib/crimage/src/crimage/util/border.cr
lib/crimage/src/crimage/util/channels.cr
lib/crimage/src/crimage/util/noise.cr
lib/crimage/src/crimage/util/palette_extractor.cr
lib/crimage/src/crimage/util/selection.cr
lib/crimage/src/crimage/util/smart_crop.cr
lib/crimage/src/crimage/util/tiling.cr
lib/crimage/src/crimage/util/visual_diff.cr
uing/crimage.cr

Instance Method Summary

Instance Method Detail

def to_uing_image : UIng::Image #

Converts to an owned UIng image. Call free when it is no longer in use.


[View source]