Image Data

<< Click to Display Table of Contents >>

Navigation:  Simplygon API 7.1 User guide >

Image Data

Images can be created and/or manipulated within the Simplygon API using IImageData objects. Texture images are stored in materials as IImageData objects. The image data objects are always three dimensional but for fewer dimensional representations the non-relevant axes are set to one. A 2d image of 256x256 pixels will thereby be a 3d matrix with dimensions 256x256x1. The dimensions of an IImageData are set with Set{1|2|3}DSize.

The pixels are either stored as 8 (TYPES_ID_UCHAR) or 16 (TYPES_ID_USHORT) bits per channel and the allowed color formats are RGB, RGBA, YUV, CMYK, L and R. The format is selected with AddColors.

To be able to manipulate the image pixels, they are read to an array using GetColors. The tuple size is determined by the color format (tuple size for RGB is 3) and the tuple count is the product of the image dimensions, e.g the number of pixels. Below are two examples, the first one generates a gradient image pixel by pixel using SetItem while the other one sets all the image data in one call using SetData.

 

Example I

 
spImageData GenerateGradientImage(unsigned int image_width, unsigned int image_height)
{
   spImageData img = sg->CreateImageData();
   // Set spImageData dimensions and color field with RGB format
   img->Set2DSize(image_width, image_height);
   img->AddColors(TYPES_ID_UCHAR, "RGB");
 
   // Get the color array with tuple size 3
   // and tuple count (image_width * image_height)
   spUnsignedCharArray colors = SafeCast<IUnsignedCharArray>( img->GetColors() );
 
   // Iterate over the image pixels and set the array values
   for(unsigned int y = 0 ; y < image_height ; y++)
   {
       for(unsigned int x = 0 ; x < image_width ; x++)
       {
           // One dimensional index
           int i = image_width * y + x;
           // Gradient color
           int gradient = x * 255 / image_width ;
         
           // Colors to array
           colors->SetItem(i*3+0, gradient);
           colors->SetItem(i*3+1, gradient);
           colors->SetItem(i*3+2, gradient);
       }
   }
 
   return img;
}
 

Example II
spImageData CreateRGBImage(unsigned int image_width, unsigned int image_height, unsigned char *data)
{
   // Create an spImageData object
   spImageData img = sg->CreateImageData();
   // Set spImageData dimensions and color field with RGB format
   img->Set2DSize(image_width, image_height);
   img->AddColors(TYPES_ID_UCHAR, "RGB");
 
   // Get the color array with tuple size 3 (RGB)
   // and tuple count (image_width * image_height)
   spUnsignedCharArray colors = SafeCast<IUnsignedCharArray>( img->GetColors() );
 
   // Assign the data to the color field
   colors->SetData(data, image_width * image_height * 3);
 
   return img;
}