
/****************************************************************************
 * NCSA HDF                                                                 *
 * Scientific Data Technologies                                             *
 * National Center for Supercomputing Applications                          *
 * University of Illinois at Urbana-Champaign                               *
 * 605 E. Springfield, Champaign IL 61820                                   *
 *                                                                          *
 * For conditions of distribution and use, see the accompanying             *
 * hdf/COPYING f.                                                        *
 *                                                                          *
 ****************************************************************************/

#include "H5IM.h"
#include "pal_rgb.h"
#include <stdlib.h>


#define DATA_FILE1  "image8.txt"
#define DATA_FILE2  "image24pixel.txt"
#define DATA_FILE3  "image24plane.txt"


#define IMAGE1_NAME  "Image8bit"
#define IMAGE2_NAME  "Image24bitpixel"
#define IMAGE3_NAME  "Image24bitplane"


int      read_data( const char* file_name );
hsize_t  width, height;
unsigned char *image_data = 0;            


int main( void )
{
 hid_t      file_id;
 hsize_t    pal_dims[2];
 herr_t     status; 
	char       *srcdir = getenv("srcdir"); /* The source directory */
	char       data_file[512]="";          /* Buffer to hold name of existing data file */

	EXAMPLE("read ascii data and make indexed image");

	/* Compose the name of the file to open, using the srcdir, if appropriate */
	if ( srcdir )
	{
	 strcpy(data_file, srcdir);
	 strcat(data_file, "/");
 }

 /* Create a new HDF5 file using default properties. */
 file_id = H5Fcreate( "ex_image2.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT );

 /* Read first data file */   
	strcat( data_file, DATA_FILE1);
 read_data( data_file );
   
 /* Initialize the palette data */
 pal_dims[0] = 256;
 pal_dims[1] = 3;
 
 /* Make dataset */
 status=H5IMmake_image_8bit( file_id, IMAGE1_NAME, width, height, image_data );

 /* Make a palette */
 status=H5IMmake_palette( file_id, "Rainbow pallete", pal_dims, pal_rgb );

 /* Attach a palette to the image dataset */
 status=H5IMlink_palette( file_id, IMAGE1_NAME, "Rainbow pallete" );

	PASSED();


/*-------------------------------------------------------------------------
 * True color image example with pixel interlace
 *-------------------------------------------------------------------------
 */

	EXAMPLE("read ascii data and make true color image with pixel interlace");

	/* Read second data file */  
	strcpy(data_file, "");
	/* Compose the name of the file to open, using the srcdir, if appropriate */
	if ( srcdir )
	{
	 strcpy(data_file, srcdir);
	 strcat(data_file, "/");
 }
	strcat( data_file, DATA_FILE2);
 read_data( data_file );
 
 /* Make dataset */
 status=H5IMmake_image_24bit( file_id, IMAGE2_NAME, width, height, "INTERLACE_PIXEL", image_data );

	PASSED();
 
/*-------------------------------------------------------------------------
 * True color image example with plane interlace
 *-------------------------------------------------------------------------
 */

	EXAMPLE("read ascii data and make true color image with plane interlace");

 /* Read third data file */  
	strcpy(data_file, "");
	/* Compose the name of the file to open, using the srcdir, if appropriate */
	if ( srcdir )
	{
	 strcpy(data_file, srcdir);
	 strcat(data_file, "/");
 }
	strcat( data_file, DATA_FILE3);
 read_data( data_file );
 
 /* Make dataset */
 status=H5IMmake_image_24bit( file_id, IMAGE3_NAME, width, height, "INTERLACE_PLANE", image_data );
 
 /* Close the file. */
 H5Fclose( file_id );

	PASSED();
 
 return 0;
}


/*-------------------------------------------------------------------------
 * read_data
 * utility function to read ASCII image data
 * the files have a header of the type
 *
 *   components
 *   n
 *   height
 *   n
 *   width
 *   n
 * 
 * followed by the image data
 *
 *-------------------------------------------------------------------------
 */

int read_data( const char* file_name )
{
 int    i, n;
 int    color_planes;
 char   str[20];
 FILE   *f;
 int    w, h;

 f = fopen( file_name, "r");

 if ( f == NULL )
 {
  printf( "Could not open file %s. Try set $srcdir \n", file_name );
		return -1;
	}


 fscanf( f, "%s", str );
 fscanf( f, "%d", &color_planes );
 fscanf( f, "%s", str );
 fscanf( f, "%d", &h); height = (hsize_t)h;
 fscanf( f, "%s", str );
 fscanf( f, "%d", &w); width = (hsize_t)w;

 if ( image_data )
  free( image_data );

 image_data = (unsigned char*) malloc ( (size_t)width * (size_t)height * color_planes * sizeof( unsigned char ));

 for (i = 0; i < h * w * color_planes ; i++)
 {
  fscanf( f, "%d",&n );
  image_data[i] = (unsigned char)n;
 }
 fclose(f);

 return 1;

}

