JScript RasterImage

Log-in or register.

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

  • uint GetPixelColor(uint posX, uint posY, uint posZ, uint posW) - returns color of a pixel at the given coordinates in RGB format (8 bits per channel).
  • uint GetPixelAlpha(uint posX, uint posY, uint posZ, uint posW)
  • uint GetPixelRed(uint posX, uint posY, uint posZ, uint posW)
  • uint GetPixelGreen(uint posX, uint posY, uint posZ, uint posW)
  • uint GetPixelBlue(uint posX, uint posY, uint posZ, uint posW) - retrieve only a single color channel or alpha value of the pixel at given coordinates.
  • uint GetPixel(uint posX, uint posY, uint posZ, uint posW) - return the color channels and alpha of a pixel as a packed ARGB value.
  • void SetPixelColor(uint posX, uint posY, uint posZ, uint posW, uint pixelColor)
  • void SetPixelAlpha(uint posX, uint posY, uint posZ, uint posW, uint pixelAlpha)
  • void SetPixelRed(uint posX, uint posY, uint posZ, uint posW, uint pixelRed)
  • void SetPixelGreen(uint posX, uint posY, uint posZ, uint posW, uint pixelGreen)
  • void SetPixelBlue(uint posX, uint posY, uint posZ, uint posW, uint pixelBlue)
  • void SetPixel(uint posX, uint posY, uint posZ, uint posW, uint packedPixel)
  • uint sizeX (get) - width of the image.
  • uint sizeY (get) - height of the image.
  • uint sizeZ (get) - depth of the image.
  • uint sizeW (get) - size of the 4-th dimension of the image.
  • void Resize(uint sizeX, uint sizeY, uint sizeZ, uint sizeW, int offsetX, int offsetY, int offsetZ, int offsetW)
  • void FillResize(uint sizeX, uint sizeY, uint sizeZ, uint sizeW, int offsetX, int offsetY, int offsetZ, int offsetW, uint fillColor)
  • float GetPixelH(uint posX, uint posY, uint posZ, uint posW)
  • float GetPixelL(uint posX, uint posY, uint posZ, uint posW)
  • float GetPixelS(uint posX, uint posY, uint posZ, uint posW)
  • void SetPixelHLS(uint posX, uint posY, uint posZ, uint posW, float pixelH, float pixelL, float pixelS)
  • bool HasAlpha()
  • void SetAlpha(bool alpha)
  • uint resolutionXNum (get)
  • uint resolutionXDenom (get)
  • uint resolutionYNum (get)
  • uint resolutionYDenom (get)
  • uint resolutionZNum (get)
  • uint resolutionZDenom (get)
  • uint resolutionWNum (get)
  • uint resolutionWDenom (get)
  • void SetResolution(uint resXNum, uint resXDenom, uint resYNum, uint resYDenom, uint resZNum, uint resZDenom, uint resWNum, uint resWDenom)
  • uint previewScale (get)

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

Recent comments

user icon PAEz contributing user on November 7th 2013

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 icon Vlasta site administrator on November 7th 2013

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 icon Sree registered user on November 20th 2020

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 icon Anonymous
Select background
What about ICL files?