JScript RasterImage

RasterImage is an interface wrapper accessible as a property of the Document object and can be used to access raster images at pixel level and to change the canvas size of an image.

Methods and properties

Example

// cache often used properties in local variables
var image = Document.RasterImage;
var sizeX = image.sizeX;
var sizeY = image.sizeY;

// iterate through pixels and erase those with even sum of X and Y coordinates
for (x=0; x<sizeX; x++)
    for (y=0; y<sizeY; y++)
        if ((x+y)&1)
            image.SetPixel(x, y, 0, 0, 0);

speech bubble iconRecent comments

user iconPAEz on November 7th 2013 0

You know what would be awesome, if GetPixel could take float for x and y and bilinear interpolate....just so I dont have to add it to my scripts 😉 Would help speed them up a little.

user iconVlasta on November 7th 2013 0

That is an interesting idea, but maybe it would be better if it were a separate method.

Also, this is a very old interface, it has been here since the beginning of scripting in RW tools many years ago and there is some outdated stuff, for example the Z and W coordinates, which are not supported anymore. Perhaps a new interface (RasterImage2 or something better) would be even better. I'll put a proposal on the forum.

user iconSree on November 20th 2020 0

That's amazing! I just made this quick little code to make a red-blue gradient sort of thing!

// cache often used properties in local variables
var bmp = Document.RasterImage;
var w = bmp.sizeX;
var h = bmp.sizeY;
// the below lines are to just cache the values instead of calculating them every time!
var widthmap = [], heightmap = [];
// bitshifting for uint is not possible and 2**24, 2**16 are not recognized! :(
// Multiply with:
//   65536 to change R component
//   256 to change G component
//   1 to change B component
// The square root is there to simulate gamma correction or something, watch this for explanation: youtu.be/LKnqECcg6Gw
for (var x = w-1; x >= 0; x--) {widthmap[x] = parseInt(Math.sqrt(x/w)*255)*65536;}
for (var y = h-1; y >= 0; y--) {heightmap[y] = parseInt(Math.sqrt(y/h)*255)*1;}

for (var y = h-1; y >= 0; y--) {
    for (var x = w-1; x >= 0; x--) {
        bmp.SetPixel(x, y, 0, 0, 255*(16777216) + widthmap[x] + heightmap[y]);
    }
}
user iconLoganDark on November 15th 2021 0

Okay how in the world does this end up making the image completely transparent?

I'm literally just unpacking each pixel and then repacking it

var image = Document.RasterImage;
var sizeX = image.sizeX;
var sizeY = image.sizeY;
for (x=0; x < sizeX; x++) {
	for (y=0; y < sizeY; y++) {
		var argb = image.GetPixel(x, y, 0, 0);
		var a = argb >> 24 & 0xFF;
		var r = argb >> 16 & 0xFF;
		var g = argb >> 8 & 0xFF;
		var b = argb & 0xFF;
		image.SetPixel(x, y, 0, 0, (a << 24) & (r << 16) & (g << 8) & b);
	}
}

Makes no sense...

user iconVlasta on November 15th 2021 0

You are using & instead of |, I suppose.

user iconLoganDark on November 15th 2021 0

Oh whoops, thanks for catching that! I don't do bitwise math often xD

user icon