Image Rendering Script (C#)
In this article:
We recommend reading Scripting in Application (C#) first to learn the basics of scripting.
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 = new byte[planeOptions.RowStride];
var outputRow = new byte[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: int): byte[]
Create an array of bytes for bulk reading from InputStream or writing to OutputStream. A C# script has no need for it: create the array with new directly, for example new byte[count]. It is provided for JavaScript and Python, which cannot construct an array of the CLR themselves.
-
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: int
Height of the output bitmap buffer in pixels.
-
OutputRowBytes: int
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: int
Width of the output bitmap buffer in pixels.
-
PlaneOptions: IList<ImagePlaneOptions>
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: int
Effective bits of each pixel of the plane.
-
PixelStride: int
Number of bytes of each pixel of the plane.
-
RowStride: int
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: double
-
ByteOrdering: ByteOrdering
Byte ordering of source image data. The following are values defined in ByteOrdering:
- BigEndian
- LittleEndian
- GreenGain: double
- RedGain: double
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.