Image Rendering Script (JavaScript)
In this article:
We recommend reading Scripting in Application (JavaScript) first to learn the basics of scripting.
JavaScript is by far the slowest of the 3 languages. Running the same script, it is roughly 100 times slower than C# and 5 to 10 times slower than Python. The script runs again every time the image needs to be rendered, so please consider C# for a script which processes a large image.
Rendering Script
There is only one script in each image rendering script, and it will be invoked one time every time when the image needs to be rendered. The script reads raw pixel data from context.inputStream and writes BGRA pixels into context.outputStream. The type of returned value is ImageRenderingResult. Return ImageRenderingResult.default after rendering the image. The following is sample script to render an image with single plane of 8-bit luminance to Bgra32 output color format:var planeOptions = context.planeOptions[0];
var sourceRow = context.createByteArray(planeOptions.rowStride);
var outputRow = context.createByteArray(context.outputRowBytes);
for (var y = 0; y < context.outputHeight; ++y) {
context.inputStream.readExactly(sourceRow, 0, sourceRow.length);
for (var x = 0; x < context.outputWidth; ++x) {
var luminance = sourceRow[x * planeOptions.pixelStride];
outputRow[x * 4] = luminance;
outputRow[x * 4 + 1] = luminance;
outputRow[x * 4 + 2] = luminance;
outputRow[x * 4 + 3] = 255;
}
context.outputStream.write(outputRow, 0, outputRow.length);
}
return ImageRenderingResult.default;
The offset to the image data in the file has already been applied to context.inputStream, so the script can read raw pixel data from the beginning of the stream directly.
Samples
The following is sample script to render an image of NV21 (YUV 4:2:0, semi-planar, V before U) format into Bgra32 output color format: You can import the downloaded file through the dialog of managing image rendering scripts.Context
The following are members of Context other than basic members:-
createByteArray(count: number): Array
Create an array of bytes for bulk reading from inputStream or writing to outputStream.
-
inputStream: System.IO.Stream
Stream to read source image data. The offset to the image data has already been applied.
-
outputFormat: BitmapFormat
Format of the output bitmap buffer. The following are values defined in BitmapFormat:
- bgra32
- bgra64
-
outputHeight: number
Height of the output bitmap buffer in pixels.
-
outputRowBytes: number
Number of bytes of each row of the output bitmap buffer.
-
outputStream: System.IO.Stream
Stream to write rendered image into the output bitmap buffer. The stream is valid only during the rendering call.
-
outputWidth: number
Width of the output bitmap buffer in pixels.
-
planeOptions: Array
Options of each plane of the image. The following are members of ImagePlaneOptions:
-
blackLevel: System.Nullable<uint>
Black level of the plane, or Null if black level is not defined.
-
effectiveBits: number
Effective bits of each pixel of the plane.
-
pixelStride: number
Number of bytes of each pixel of the plane.
-
rowStride: number
Number of bytes of each row of the plane.
-
whiteLevel: System.Nullable<uint>
White level of the plane, or Null if white level is not defined.
-
blackLevel: System.Nullable<uint>
-
renderingOptions: ImageRenderingOptions
Options of rendering the image. The following are members of ImageRenderingOptions:
- blueGain: number
-
byteOrdering: ByteOrdering
Byte ordering of source image data. The following are values defined in ByteOrdering:
- bigEndian
- littleEndian
- greenGain: number
- redGain: number
Interacting with user is not supported inside an image rendering script, because the script runs on a background thread every time when the image needs to be rendered.