ScinImageBuffer

Represents a server-side graphics memory region for sampling static images.
See also: VTexPos VSampler

Description


The ScinImageBuffer class is designed to be analagous to the SuperCollider audio Buffer class, but for reading static images for sampling. ScinImageBuffers are most commonly used inside of ScinthDef functions as arguments to VSampler instances. Currently ScinImageBuffer support is limited to reading static images, but more may be added in the future.

Class Methods


ScinImageBuffer.read(server, path, width, height, action, bufnum)

Attempts to open the image file at the provided path and read the metadata. If that is successful the server will decode the image file, optionally resize it to the provided width and height, and upload it to the graphics hardware so it is ready for use.

The width and height arguments are both by default -1, which is an instruction to the server to respect the original dimensions of the image. If only one of the dimension arguments is supplied by -1, the server will scale the image to the non-negative dimension and then preserve the aspect ratio of the image by scaling the negative dimension to the appropriate size. Of course, if both dimensions are non-negative then the server will scale the image to the provided dimensions, disregarding the aspect ratio of the input image.

For example, for a source image that is 200 pixels wide and 100 pixels tall:

width requested

height requested

buffer width

buffer height

notes

400

100

400

100

Server will disregard aspect ratio of source image if both requested dimensions are nonnegative.

-1

50

100

50

In order to maintain 2:1 aspect ratio server has computed a width of 50 px.

400

-1

400

200

In order to maintain 2:1 aspect ratio server has computed a height of 400 px.

-1

-1

200

100

Server has allocated width and height of source image.

Arguments

server

The ScinServer on which to read the image and allocate the buffer. If nil, will use ScinServer.default.

path

A string containing the path to the image file to read.

width

An optional integer describing a desired width to scale the image to, or -1.

height

An optional integer describing a desired height to scale the image to, or -1.

action

An optional function to be evaluated once the image has been decoded, uploaded, and this ImageBuffer’s instance variables have been updated. The function will be passed this ImageBuffer as an argument.

bufnum

An explicitly specified buffer number. While buffer numbers for ImageBuffer are set on the client, and not allocated on the server, any load or delete operations on an ImageBuffer will clobber any exising ImageBuffer with the same buffer number. So, like Buffer , the best practice is to leave this unspecified.

Instance Methods


.width

Returns the width of the ImageBuffer in pixels.

.server

The ScinServer that owns the associated buffer.

.bufnum

The integer buffer number that uniquely identifies this buffer on the server.

.height

Returns the height of the ImageBuffer in pixels.

Examples


// Creates a chromakey-like effect using the VecMix VGen to choose between two different
// image buffers based on distance of the first image from the provided target rgb color.
(
~v = ScinServer.new;
~v.options.width = 400;
~v.options.height = 300;
~v.boot;
)

(
~molly = ScinImageBuffer.read(path: "~/src/TestGoldImages/sourceMedia/molly.png".standardizePath);
~storm = ScinImageBuffer.read(path: "~/src/TestGoldImages/sourceMedia/storm.png".standardizePath);
)

(
~f = ScinthDef.new(\chromaKey, { |r, g, b, key = 0.25|
    var m = VSampler.pr(~molly, VTexPos.pr);
    var s = VSampler.pr(~storm, VTexPos.pr);
    var dist = VLength.pr(m - VVec4.pr(r, g, b, 1.0));
    var pick = VStep.pr(key, dist);
    VMix.pr(s, m, pick);
}).add;
)

(
~t = Scinth.new(\chromaKey);
)

// Now vary the parameters of the ScinthDef to pick up a brighter color and with a larger key
// threshold.
(
~t.set(\r, 0.64, \g, 0.64, \b, 0.64, \key, 0.5);
)