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.

Syntax

  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.

Examples

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:

 

t = { 1, 3, 5, 7, 9 }

-- make an array (lua table) of data

I = CImage:newarray( t )

-- create a new CImage object

I = I - 0.5

-- subtract 0.5 from every pixel (array element)

t = I:Array()

-- save to the table

for i,v in pairs( t ) do

-- loop over every table element

  Printf("%d:%lg ", i, v )

-- Print each table element

end

-- 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:

t = {1,2,3,4,5,6,7,8,9,10,11,12}

 

I = CImage:newarray( t, {4,3}, "int" )

-- create a new 2-D CImage

Printf("Axes=%d, %dx%d", I:Axes(),I:Len(1),I:Len(2))

-- prints "Axes=2, 4x3"

Related Topics

new

Array

Copy

Dup

Create

delete

Math Operators

CImage class