Last modified: 2012-12-5
Class Image
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 in Cycling '74 Reference
Constructor Attributes | Constructor Name and Description |
---|---|
Image(param1, param2)
The Image object can be used to draw images in an instance of the Sketch.
|
Field Attributes | Field Name and Description |
---|---|
Integer Array[2] get/set |
size[0] is width size[1] is height.
|
Method Attributes | Method Name and Description |
---|---|
void |
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.
|
void |
alphachroma(red, green, blue, tolerance, fade, minkey, maxkey)
Generates an alpha channel based on the chromatic distance from the specified RGB
target color.
|
void |
blendchannel(source_object, alpha, source_channel, destination_channel)
Similar to the copychannel method, except supports a blend amount specified by the alpha
argument.
|
void |
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.
|
void |
clear(red, green, blue, alpha)
Sets all pixels in the image to be the value specified by the red, green, blue, and alpha
arguments.
|
void |
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.
|
void |
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.
|
void |
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.
|
void |
flip(hotizontal_flip, vertical_flip)
Flips the image horizontally and or vertically.
|
void |
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.
|
void |
fromnamedmatrix(matrixname)
Copies the pixels from the jit.matrix object specified by matrixname to the image.
|
Float Array |
getpixel(x, y)
Returns an array containing the pixel value at the specified location.
|
void |
setpixel(x, y, red, green, blue, alpha)
Sets the pixel value at the specified location.
|
void |
swapxy()
Swaps the axes of the image so that width becomes height and vice versa.
|
void |
tonamedmatrix(matrixname)
Copy the pixels from the image to the jit.matrix object specified by matrixname.
|
Image
(param1, param2)- Example:
// create a new Image instance with default width + height var myimg = new Image(); // create a new Image instance with the specified witdth + height var myimg = new Image(width,height); // create a new Image instance from a file from disk var myimg = new Image(filename); // create a new Image instance from another instance of Image var myimg = new Image(imageobject); // create a new Image instance from an instance of Sketch var myimg = new Image(sketchobject);
- Parameters:
- {mixed optional} param1
- can be empty, width, filename, imageobject, sketchobject
- {mixed optional} param2
- if first parameter is width, this should be height
size
- Example:
myimg = new Image("IMG_0032.JPG"); width = myimg.size[0]; height = myimg.size[1]; myimg.size = [400,300];
adjustchannel
(channel, scale, bias)- Example:
function bang(){ myimg = new Image("IMG_0032.JPG"); myimg.adjustchannel("red",0.1,0.5); mat = new JitterMatrix(); myimg.tonamedmatrix(mat.name); outlet(0,"jit_matrix",mat.name); }
- Parameters:
- {String} channel
- "red", "green", "blue", or "alpha"
- {Float} scale
- {Float} bias
alphachroma
(red, green, blue, tolerance, fade, minkey, maxkey)- Example:
function bang(){ myimg = new Image("IMG_0032.JPG"); myimg.alphachroma(1.,0.5,0.,0.5,0.9); mat = new JitterMatrix(); myimg.tonamedmatrix(mat.name); outlet(0,"jit_matrix",mat.name); }
- Parameters:
- {Float} red
- {Float} green
- {Float} blue
- {Float} tolerance
- {Float} fade
- {Float} minkey
- {Float} maxkey
blendchannel
(source_object, alpha, source_channel, destination_channel)- Example:
function bang(){ myimg = new Image("IMG_0032.JPG"); myimg2 = new Image("IMG_0032.JPG"); myimg.flip(1,0); myimg.blendchannel(myimg2, 0.5, "red", "green"); mat = new JitterMatrix(); myimg.tonamedmatrix(mat.name); outlet(0,"jit_matrix",mat.name); }
- Parameters:
- {Maxobj} source_object
- {Float} alpha
- {String} source_channel
- {String} destination_channel
blendpixels
(source_object, alpha, destination_x, destination_y, source_x, source_y, width, height)- Example:
function bang(){ myimg = new Image("IMG_0032.JPG"); myimg2 = new Image("IMG_0032.JPG"); myimg.flip(1,0); width = myimg.size[0]; height = myimg.size[1]; post("width: " + width + "\n"); post("height: " + height + "\n"); for(var y=0;y < height;y++){ for(var x = 0; x < width;x++){ myimg.blendpixels(myimg2, 0.9, x, y); } } mat = new JitterMatrix(); myimg.tonamedmatrix(mat.name); outlet(0,"jit_matrix",mat.name); }
- Parameters:
- {Maxobj} source_object
- {Float} alpha
- {Integer} destination_x
- {Integer} destination_y
- {Integer} source_x
- {Integer} source_y
- {Integer} width
- {Integer} height
clear
(red, green, blue, alpha)- Example:
function bang(){ myimg = new Image("IMG_0032.JPG"); myimg.clear(1.,.5,0.,0.5); mat = new JitterMatrix(); myimg.tonamedmatrix(mat.name); outlet(0,"jit_matrix",mat.name); }
- Parameters:
- {Float} red
- {Float} green
- {Float} blue
- {Float} alpha
clearchannel
(channel, value)- Example:
function bang(){ myimg = new Image("IMG_0032.JPG"); myimg.clearchannel("red",0.5); mat = new JitterMatrix(); myimg.tonamedmatrix(mat.name); outlet(0,"jit_matrix",mat.name); }
- Parameters:
- {String} channel
- {Float} value
copychannel
(source_object, source_channel, destination_channel)- Example:
function bang(){ myimg = new Image("IMG_0032.JPG"); myimg2 = new Image("IMG_0032.JPG"); myimg.flip(1,0); myimg.copychannel(myimg2,"red","green"); mat = new JitterMatrix(); myimg.tonamedmatrix(mat.name); outlet(0,"jit_matrix",mat.name); }
- Parameters:
- {Maxobj} source_object
- {String} source_channel
- {String} destination_channel
copypixels
(source_object, destination_x, destination_y, source_x, source_y, width, height)- Example:
function bang(){ myimg = new Image("IMG_0032.JPG"); myimg2 = new Image("IMG_0032.JPG"); myimg.flip(1,0); width = myimg.size[0]; height = myimg.size[1]; post("width: " + width + "\n"); post("height: " + height + "\n"); for(var y=0;y < height;y++){ for(var x = 0; x < width;x++){ myimg.copypixels(myimg2, x, y); } } mat = new JitterMatrix(); myimg.tonamedmatrix(mat.name); outlet(0,"jit_matrix",mat.name); }
- Parameters:
- {Maxobj} source_object
- {Integer} destination_x
- {Integer} destination_y
- {Integer} source_x
- {Integer} source_y
- {Integer} width
- {Integer} height
flip
(hotizontal_flip, vertical_flip)- Example:
function bang(){ myimg = new Image("IMG_0032.JPG"); myimg.flip(1,0); mat = new JitterMatrix(); myimg.tonamedmatrix(mat.name); outlet(0,"jit_matrix",mat.name); }
- Parameters:
- {Boolean} hotizontal_flip
- {Boolean} vertical_flip
freepeer
()- Example:
function bang(){ myimg = new Image("IMG_0032.JPG"); mat = new JitterMatrix(); myimg.tonamedmatrix(mat.name); outlet(0,"jit_matrix",mat.name); myimg.freepeer(); }
fromnamedmatrix
(matrixname)- Example:
function bang(){ mat = new JitterMatrix(); myimg = new Image("IMG_0032.JPG"); myimg.tonamedmatrix(mat.name); myimg2 = new Image(); myimg2.fromnamedmatrix(mat.name); myimg2.flip(0,1); myimg2.tonamedmatrix(mat.name); outlet(0,"jit_matrix",mat.name); }
- Parameters:
- {String} matrixname
getpixel
(x, y)- Example:
function bang(){ myimg = new Image("IMG_0032.JPG"); width = myimg.size[0]; height = myimg.size[1]; pixel = myimg.getpixel(width/2,height/2); myimg.clear(pixel[0],pixel[1],pixel[2],pixel[3]); mat = new JitterMatrix(); myimg.tonamedmatrix(mat.name); outlet(0,"jit_matrix",mat.name); }
- Parameters:
- {Integer} x
- {Integer} y
setpixel
(x, y, red, green, blue, alpha)- Example:
function bang(){ myimg = new Image("IMG_0032.JPG"); width = myimg.size[0]; height = myimg.size[1]; post("width: " + width + "\n"); post("height: " + height + "\n"); for(var y=0;y < height;y+=2){ for(var x = 0; x < width;x+=2){ pixel = myimg.getpixel(width/2,height/2); myimg.setpixel(x,y,pixel[0]/2,pixel[1]/2,pixel[2]/2,.5); } } mat = new JitterMatrix(); myimg.tonamedmatrix(mat.name); outlet(0,"jit_matrix",mat.name); }
- Parameters:
- {Integer} x
- {Integer} y
- {Float} red
- {Float} green
- {Float} blue
- {Float} alpha
swapxy
()- Example:
function bang(){ myimg = new Image("IMG_0032.JPG"); myimg.swapxy(); mat = new JitterMatrix(); myimg.tonamedmatrix(mat.name); outlet(0,"jit_matrix",mat.name); }
tonamedmatrix
(matrixname)- Example:
function bang(){ myimg = new Image("IMG_0032.JPG"); mat = new JitterMatrix(); myimg.tonamedmatrix(mat.name); outlet(0,"jit_matrix",mat.name); }
- Parameters:
- {String} matrixname