Namespaces

Variants
Actions

Javascript in Max 14 - the Image Object

From Cycling '74 Wiki
Jump to: navigation, search

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.).

Contents

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], get/set)

size[0] is width size[1] is height.

Image Methods

adjustchannel (channel, scale, bias)

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 (red, green, blue, tolerance, fade, minkey, maxkey)

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 (source_object, alpha, source_channel, destination_channel)

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 (source_object, alpha, destination_x, destination_y, source_x, source_y, width, height)

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 (red, green, blue, alpha)

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 (channel, value)

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 (source_object, source_channel, destination_channel)

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 (source_object, destination_x, destination_y, source_x, source_y, width, height)

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 (horizontal_flip, vertical_flip)

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 (matrixname)

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

getpixel (x, y)

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 (x, y, red, green, blue, alpha)

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 (matrixname)

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