CImage:newarray
The newarray method constructs a new CImage object from a lua array (table). Since this method creates a new CImage object, it is an alternative to the standard constructor CImage:new. You can optionally set the image data type to convert the table values to a different image data type and also specify the image dimensionality to load the 1-dimensional table as an n-dimensional image.
CImageNew = CImage:newarray( tArray ) CImageNew = CImage:newarray( tArray, nDim[] ) CImageNew = CImage:newarray( tArray, sDataType ) CImageNew = CImage:newarray( tArray, nDim[], sDataType ) where |
tArray is a lua array (table) of values. This table should not contain other tables.
nDim[] is an array containing the lengths of the image dimensions. The number of dimensions, or image axes, is set by the number of elements in the table. The number of axes must be between 1 and the maximum number of axes supported by Mira.
sDataType is a string specifying the data type of the image (e.g., "int", "float", etc.). If not specified, the data type is "double".
CImageNew is the return value, a new CImage object.
The nAxis[] argument specifies the number ofimage dimensions and sets the lengths of the axes. The 1-dimensional lua table is then packed into the CImage array of specified dimension. For example, {1024,512} creates a 2-D image having 1024 columns and 512 rows. If you do not pass nAxis[], the image will have 1 dimension with a length equal to the number of elements in the table.
If the sDataType string is not specified then the image has "double" data type. If the nAxis[] argument specifies more elements than exist in the table, then the extra elements are set to 0. If nAxis[] specifies fewer elements than exist in the table, the extra elements are discarded from the image.
The following script creates a CImage from a 1-dimensional table, subtracts 0.5 from every element of the table, and then uses the Array method to save the results back into the table:
|
|
|
-- make an array (lua table) of data |
|
-- create a new CImage object |
|
-- subtract 0.5 from every pixel (array element) |
|
-- save to the table |
|
-- loop over every table element |
|
-- Print each table element |
|
-- result: 1:0.5 2:2.5 3:4.5 4:6.5 5:8.5 |
Note that the for loop can be compacted as follows:
for i,v in pairs( I:Array() ) do
However in this form, note that the data are not stored because the lua table is not explicitly assigned.
The following script creates a 2-D image having 4 columns, 3 rows, and "int" data type:
|
|
|
-- create a new 2-D CImage |
|
-- prints "Axes=2, 4x3" |