Image Utility

Availability LightWave 6.0 | Component Layout, Modeler | Header lwimage.h

This global provides functions for creating and saving still images. Also see the Image List global.

Pixmaps, used by this global and identified by LWPixmapID, differ from the images in the image list, which are identified by LWImageID. Pixmaps are stills, and you can draw on them and save them. Once saved to a file, the image can be loaded into LightWave using the Image List load function. The pixmap returned by the Image List evaluate function is a copy of the image, and drawing on this copy does not change the original image.

Global Call

   LWImageUtil *imgutil;
   imgutil = global( LWIMAGEUTIL_GLOBAL, GFUSE_TRANSIENT );

The global function returns a pointer to an LWImageUtil.

   typedef struct st_LWImageUtil {
      LWPixmapID   (*create)    (int w, int h, LWImageType);
      void         (*destroy)   (LWPixmapID);
      int          (*save)      (LWPixmapID, int saver, const char *name);
      void         (*setPixel)  (LWPixmapID, int x, int y, void *pix);
      void         (*getPixel)  (LWPixmapID, int x, int y, void *pix);
      void         (*getInfo)   (LWPixmapID, int *w, int *h, int *type);
      LWPixmapID   (*resample)  (LWPixmapID, int w, int h, int mode);
      int          (*saverCount)(void);
      const char * (*saverName) (int saver);
   } LWImageUtil;
image = create( w, h, type )
Create a new image. The type specifies the organization of the pixel data and may be any of the image I/O pixel types.
destroy( image )
Release resources allocated by create. The image ID is no longer valid after this is called.
result = save( image, saver_index, filename )
Save the image to a file using the specified format. The format is determined by the choice of image saver, which can be one of Layout's built-in image savers or any of the installed ImageSaver class plug-ins. Use the saverCount and saverName functions to determine what formats are available and which saver index to use.
setPixel( image, x, y, pixel )
Set the value of a pixel in the image. The format of the pixel data depends on the pixel type of the image.
getPixel( image, x, y, pixel )
Get the value of a pixel in the image.
getInfo( image, w, h, type )
Get the width, height and pixel type of an image.
image2 = resample( image, w, h, mode )
Create a new image by resizing an existing image. The mode determines how the existing pixels will be resampled and can be one of the following values.
LWISM_SUBSAMPLING
LWISM_MEDIAN
LWISM_SUPERSAMPLING
LWISM_BILINEAR
LWISM_BSPLINE
LWISM_BICUBIC
count = saverCount()
Returns the number of available image savers.
name = saverName( saver_index )
Returns the name of an image saver.

Example

This example creates a rainbow image, saves it, and loads it into Layout's internal image list using the image list global.

   #include <lwserver.h>
   #include <lwimage.h>
   #include <lwhost.h>

   LWMessageFuncs *msg;
   LWImageUtil *imgutil;
   LWImageList *imglist;
   LWImageID image;
   LWPixmapID pixmap;
   int x, y, saver, nsavers;
   unsigned char rgb[ 3 ];
   char *filename = "rainbow.tga";

   imgutil = global( LWIMAGEUTIL_GLOBAL, GFUSE_TRANSIENT );
   imglist = global( LWIMAGELIST_GLOBAL, GFUSE_TRANSIENT );
   msg = global( LWMESSAGEFUNCS_GLOBAL, GFUSE_TRANSIENT );
   if ( !imgutil || !imglist || !msg )
      return AFUNC_BADGLOBAL;

   pixmap = imgutil->create( 256, 20, LWIMTYP_RGB24 );
   if ( !pixmap ) {
      msg->error( "Couldn't create the image.", NULL );
      return AFUNC_OK;
   } 

   for ( x = 0; x < 256; x++ )
      for ( y = 0; y < 20; y++ ) {
         hsv2rgb( 359.0f * x / 255.0f, y / 20.0f, 1.0f, rgb );
         imgutil->setPixel( pixmap, x, y, rgb );
      }
   
   nsavers = imgutil->saverCount();
   for ( saver = 0; saver < nsavers; saver++ )
      if ( !strcmp( "Targa Format (.tga)",
         imgutil->saverName( saver ))) break;
   
   if ( saver == nsavers )
      msg->error( "Couldn't find the Targa saver.", NULL );
   else   
      imgutil->save( pixmap, saver, filename );
      
   imgutil->destroy( pixmap );

   image = imglist->load( filename );

The hsv2rgb function looks like this.

   void hsv2rgb( float h, float s, float v, char rgb[] )
   {
      float r, g, b, p, q, f, t;
      int i;

      if ( s == 0 ) {
         rgb[ 0 ] = rgb[ 1 ] = rgb[ 2 ] = ( int )( v * 255 );
         return;
      }

      h /= 60.0f;
      i = ( int ) h;
      f = h - i;
      p = v * ( 1.0f - s );
      q = v * ( 1.0f - ( s * f ));
      t = v * ( 1.0f - ( s * ( 1.0f - f )));

      switch ( i ) {
         case 0:  r = v;  g = t;  b = p; break;
         case 1:  r = q;  g = v;  b = p; break;
         case 2:  r = p;  g = v;  b = t; break;
         case 3:  r = p;  g = q;  b = v; break;
         case 4:  r = t;  g = p;  b = v; break;
         case 5:  r = v;  g = p;  b = q; break;
      }

      rgb[ 0 ] = ( int )( r * 255 ); 
      rgb[ 1 ] = ( int )( g * 255 ); 
      rgb[ 2 ] = ( int )( b * 255 ); 
   }