A newer version of Max is available. Click here to access the latest version of the Max documentation

The Image Object

The Image Object

The Image object can be used to draw images in an instance of the Sketch. It is possible to load image files from disk, create images from instances of Sketch, or generate them manually. The Image object has several methods to assist in manipulating images once generated. Note that alphablending is on by default in sketch. Certain file formats which contain alpha channels such as PICT or TIFF may have their alpha channel set all off. File formats which do not contain an alpha channel such as JPEG, by default have an alpha channel of all on. If you are having trouble seeing an image when attempting to draw in an instance of Sketch, you may want to either turn off blending with gldisable("blend"), or set the alpha channel to be all on with clearchannel("alpha",1.).

Image Constructor

var myimg = new Image() // create a new Image instance with default width + height
var myimg = new Image(width, height) // create a new Image instance with the specified witdth + height
var myimg = new Image(filename) // create a new Image instance from a file from disk
var myimg = new Image(imageobject) // create a new Image instance from another instance of Image
var myimg = new Image(sketchobject) // create a new Image instance from an instance of Sketch

Image Properties

size [Array[2]]

Access or set the size of an image. size[0] is width size[1] is height.

Image Methods

adjustchannel

Arguments

channel [string]
scale [float]
bias [float]

Adjusts all channel values in the image channel specified by the channel argument, by multiplying the channel value by the value specified by the scale argument and then adding the value specified by the bias argument. The resulting channel is clipped to the range 0.-1. Acceptable values for the channel argument are the strings: "red", "green", "blue", or "alpha".

alphachroma

Arguments

red [number]
green [number]
blue [number]
tolerance [number]
fade [number]
minkey [number]
maxkey [number]

Generates an alpha channel based on the chromatic distance from the specified RGB target color. If no tolerance, fade or minkey arguments are specified they are assumed to be 0. If no maxkey argument is specified, it is assumed to be 1.

blendchannel

Arguments

source_object [string]
alpha [number]
source channel [string]
destination channel [string]

Similar to the copychannel method, except supports a blend amount specified by the alpha argument. The source object can only be an instance of Image (not Sketch). If the source object is not the same size as the destination object, then rectangle composed of the minimum width and height of each, is the rectangle of values which will be blended. Acceptable values for the channel arguments are the strings: "red", "green", "blue", or "alpha".

blendpixels

Arguments

source_object [string]
alpha [number]
destination x [number]
destination y [number]
source x [number]
source y [number]
width [number]
height [number]

Similar to the copypixels method, except supports alpha blending, including a global alpha value specified by the alpha argument. This global alpha value is multiplied by the source object's alpha channel at each pixel. Instances of Sketch do not contain an alpha channel, which is assumed to be all on. The source object can either be an instance of Image, or Sketch.

clear

Arguments

red [number]
green [number]
blue [number]
alpha [number]

Sets all pixels in the image to be the value specified by the red, green, blue, and alpha arguments. If no arguments are specified, these values are assumed to be (0, 0, 0, 1) respectively.

clearchannel

Arguments

channel [string]
value [number]

Sets all channel values in the image channel specified by the channel argument to be the value specified by the value argument. If no value argument is specified, it is assumed to be 0. Acceptable values for the channel argument are the strings: "red", "green", "blue", or "alpha".

copychannel

Arguments

source object [string]
source channel [string]
destination channel [string]

Copies the channel values from the source object's channel specified by the source_channel argument to the destination object's channel specified by the destination_channel argument. The source object can only be an instance of Image (not Sketch). If the source object is not the same size as the destination object, then rectangle composed of the minimum width and height of each, is the rectangle of values which will be copied. Acceptable values for the channel arguments are the strings: "red", "green", "blue", or "alpha".

copypixels

Arguments

source_object [string]
destination x [number]
destination y [number]
source x [number]
source y [number]
width [number]
height [number]

Copies pixels from the source object to the location specified by the destination_x and destination_y arguments. The initial x and y offset into the source and size of the rectangle copied can be speified by the source_x, source_y, width and height arguments. If these are not present an x and y offset of zero and width and height equal to the source image is assumed. No scaling of pixels is supported. The source object can either be an instance of Image, or Sketch.

flip

Arguments

horizontal_flip [number]
vertical_flip [number]

Flips the image horizontally and or vertically. Arguments can be 0 or 1, where 0 is no flip, and 1 is flip.

freepeer

Frees the image data from the native c peer, which is not considered by the JavaScript garbage collector, and may consume lots of memory until the garbage collector decides to run based on JS allocated memory. Once called, the Image object is not available for any other use.

fromnamedmatrix

Arguments

matrixname [string]

Copies the pixels from the jit.matrix object specified by matrixname to the image.

getpixel

Arguments

x [number]
y [number]

Returns an array containing the pixel value at the specified location. This array is ordered RGBA, i.e. array element 0 is red, 1, green, 2, blue, 3 alpha. Color values are floating point numbers in the range 0.-1.

setpixel

Arguments

x [number]
y [number]
red [number]
green [number]
blue [number]
alpha [number]

Sets the pixel value at the specified location. Color values are floating point numbers in the range 0.-1.

swapxy

Swaps the axes of the image so that width becomes height and vice versa. The effective result is that the image is rotated 90 degrees counter clockwise, and then flipped vertically.

tonamedmatrix

Arguments

matrixname [string]

Copy the pixels from the image to the jit.matrix object specified by matrixname.

See Also

Name Description
JavaScript Usage JavaScript Usage