C Example - gr_pal.c



#include "mfgr.h"

#define X_LENGTH 15 
#define Y_LENGTH 10 

main( )
{
	int32 gr_id, ri_id, file_id, pal_id, status, num_entries, ri_idx;
	int32 data_type, ncomp, num_comp, interlace_mode; 
	uint8 palette_data[256*3];
	uint8 r_palette_data[256*3];
	intn i, j;

	/* Create and open the file. */
	file_id = Hopen("gr1.hdf", DFACC_WRITE, 0);

	/* Initiate the GR interface. */
	gr_id = GRstart(file_id);

	ri_idx = GRnametoindex(gr_id, "Image_1");

	ri_id = GRselect (gr_id, ri_idx);
 
	/* Initialize the palette to grayscale. */
	for (i = 0; i < 256; i++) {
	 	palette_data[i * 3] = i;
	 	palette_data[i * 3 + 1] = i;
	 	palette_data[i * 3 + 2] = i;
	}

	/* Set palette characteristics. */
	data_type = DFNT_UINT8;
	num_entries = 256;
	num_comp = 3;

	/* Get the id for the palette. */
	pal_id = GRgetlutid(ri_id, ri_idx);

	/* Write the palette to file. */
	status = GRwritelut(pal_id, num_comp, data_type, interlace_mode, num_entries, (VOIDP)palette_data);

	/* Read the palette data. */
	status = GRreadlut(pal_id, (VOIDP)r_palette_data);

	printf ("  Data: \n");

	for (i=0; i< 768; i++)
	{
		printf ("%i ",r_palette_data[i]);
		if (i==256) printf ("\n");
		if (i==512) printf ("\n");
	}
	printf ("\n");

	/* Terminate access to the image. */
	status = GRendaccess(ri_id);

	/* Terminate access to the GR interface. */
	status = GRend(gr_id);

	/* Close the file. */
	status = Hclose(file_id);

}