#ifndef DATAINFO_TESTER #define DATAINFO_TESTER /* to include hdatainfo.h */ #endif #include "hdf.h" #define WIDTH 5 #define LENGTH 5 #define N_IMAGES 4 static void test_getpalinfo(); intn check_dds(char *fname, char *msg) { int32 fid, grid; intn n_pals = 0; hdf_ddinfo_t *palinfo_array = NULL; uint8 *inbuf; intn status; /* status returned from routines */ intn ii, jj; /* indices */ fprintf(stderr, "\n>>>> %s", msg); /* Re-open the file and initialize the GR interface */ fid = Hopen (fname, DFACC_RDWR, 0); grid = GRstart (fid); n_pals = 0; n_pals = GRgetpalinfo(grid, 0, NULL); fprintf(stderr, "GRgetpalinfo return pal count = %d\n", n_pals); palinfo_array = (hdf_ddinfo_t *) HDmalloc(n_pals * sizeof(hdf_ddinfo_t)); n_pals = GRgetpalinfo(grid, n_pals, palinfo_array); fprintf(stderr, "GRgetpalinfo return pal count = %d\n", n_pals); fprintf(stderr, "tag ref offset length \n"); for (ii = 0; ii < n_pals; ii++) fprintf(stderr, "%d %d %d %d\n", palinfo_array[ii].tag, palinfo_array[ii].ref, palinfo_array[ii].offset, palinfo_array[ii].length); /* Terminate access to the GR interface and close the file */ status = GRend(grid); status = Hclose(fid); return 0; } /************************************************************************* test_getpalinfo() - tests GRgetpalinfo **************************************************************************/ #define IMAGE_DFPAL_FILE "tdatainfo_pal.hdf" #define IMAGE_WITH_PAL "GR Image with Palette" #define IMAGE2_WITH_PAL "Second GR Image w/pal" #define LASTIMAGE_NOPAL "Last GR Image: no pal" #define N_COMPS_IMG 2 /* number of image components */ #define N_ENTRIES 256 /* number of entries in the palette */ #define N_COMPS_PAL 3 /* number of palette's components */ static void test_getpalinfo() { int32 fid, grid, /* file ID and GR interface ID */ riid; /* raster image ID */ intn status; /* status returned from routines */ intn ii, jj; /* indices */ int32 n_images, n_fattrs; int32 pal_id, interlace_mode, start[2], /* holds where to start to write for each dimension */ edges[2], /* holds how long to write for each dimension */ dim_sizes[2]; /* sizes of the two dimensions of the image array */ uint8 image_buf[WIDTH][LENGTH][N_COMPS_IMG]; /* data of first image */ uint8 palette_buf[N_ENTRIES][N_COMPS_PAL]; uint8 paletteA[256*3], paletteB[256*3]; uint16 last_ref =0; intn n_pals = 0; /* Palettes are added in the following means and order: paletteA (DFPputpal) paletteB (DFPputpal) paletteA (DFR8setpalette/DFR8addimage) palette_buf (GRwritelut) for image named IMAGE_WITH_PAL */ /* Add several palettes. */ status = DFPputpal(IMAGE_DFPAL_FILE, paletteA, 0, "w"); fprintf(stderr, "DFPputpal paletteA returns %d\n", status); status = DFPputpal(IMAGE_DFPAL_FILE, paletteB, 0, "a"); fprintf(stderr, "DFPputpal paletteB returns %d\n", status); n_pals = DFPnpals(IMAGE_DFPAL_FILE); fprintf(stderr, "DFPnpals returns %d palettes\n", n_pals); /* Initialize the 8-bit image array */ static uint8 raster_data[WIDTH][LENGTH] = { 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 6, 4, 2, 0, 2 }; status = DFR8setpalette(paletteA); /* Write an 8-bit raster image to file */ status = DFR8addimage(IMAGE_DFPAL_FILE, raster_data, WIDTH, LENGTH, COMP_RLE); n_pals = DFPnpals(IMAGE_DFPAL_FILE); fprintf(stderr, "DFPnpals returns %d palettes\n", n_pals); /* Write another 8-bit raster image to file */ status = DFR8addimage(IMAGE_DFPAL_FILE, raster_data, WIDTH, LENGTH, COMP_RLE); n_pals = DFPnpals(IMAGE_DFPAL_FILE); fprintf(stderr, "DFPnpals returns %d palettes\n", n_pals); check_dds(IMAGE_DFPAL_FILE, "before going to GR API \n"); /**************************************************************** Re-open the file in GR interface, add a few images with palettes, then test GRgetpalinfo on the palettes ****************************************************************/ fprintf(stderr, "\nreopen file in GR\n"); /* Re-open the file and initialize the GR interface */ fid = Hopen (IMAGE_DFPAL_FILE, DFACC_RDWR, 0); grid = GRstart (fid); /* Get the number of images in the file */ status = GRfileinfo(grid, &n_images, &n_fattrs); fprintf(stderr, "GRfileinfo returns n_images = %d\n", n_images); /* Define the dimensions and interlace mode of the image. */ dim_sizes[0] = LENGTH; dim_sizes[1] = WIDTH; interlace_mode = MFGR_INTERLACE_PIXEL; /* Create the image named NEW_IMAGE_NAME. */ riid = GRcreate (grid, IMAGE_WITH_PAL, N_COMPS_IMG, DFNT_UINT8, interlace_mode, dim_sizes); /* Fill the image data buffer with values. */ for (ii = 0; ii < WIDTH; ii++) { for (jj = 0; jj < LENGTH; jj++) { image_buf[ii][jj][0] = (ii + jj) + 1; image_buf[ii][jj][1] = (ii + jj) + 2; } } /* Define the size of the data to be written, i.e., start from the origin * and go as long as the length of each dimension. */ start[0] = start[1] = 0; edges[0] = WIDTH; edges[1] = LENGTH; /* Write the data in the buffer into the image array. */ status = GRwriteimage (riid, start, NULL, edges, (VOIDP)image_buf); /* Initialize the palette to grayscale. */ for (ii = 0; ii < N_ENTRIES; ii++) { palette_buf[ii][0] = ii; palette_buf[ii][1] = 0; palette_buf[ii][2] = 8; } check_dds(IMAGE_DFPAL_FILE, "after re-opened file in GR API, GRwriteimage\n"); /* Define palette interlace mode. */ interlace_mode = MFGR_INTERLACE_PIXEL; /* Get the identifier of the palette attached to the image NEW_IMAGE_NAME. */ pal_id = GRgetlutid (riid, 0); /* Write data to the palette. */ status = GRwritelut (pal_id, N_COMPS_PAL, DFNT_UINT8, interlace_mode, N_ENTRIES, (VOIDP)palette_buf); fprintf(stderr, "LUT for image %s had been written\n", IMAGE_WITH_PAL); n_pals = DFPnpals(IMAGE_DFPAL_FILE); fprintf(stderr, "DFPnpals returns %d palettes\n", n_pals); check_dds(IMAGE_DFPAL_FILE, "after re-opened file in GR API, GRwriteimage, GRwritelut\n"); /* Terminate access to the first image. */ status = GRendaccess (riid); /* Read some palettes */ { hdf_ddinfo_t *palinfo_array = NULL; uint8 *inbuf; /* Call GRgetpalinfo the first time, passing in NULL for the palette array, to get the number of palettes in the file */ n_pals = 0; n_pals = GRgetpalinfo(grid, 0, NULL); fprintf(stderr, "GRgetpalinfo return pal count = %d\n", n_pals); palinfo_array = (hdf_ddinfo_t *) HDmalloc(n_pals * sizeof(hdf_ddinfo_t)); n_pals = GRgetpalinfo(grid, n_pals, palinfo_array); fprintf(stderr, "GRgetpalinfo return pal count = %d\n", n_pals); fprintf(stderr, "tag ref offset length \n"); for (ii = 0; ii < n_pals; ii++) fprintf(stderr, "%d %d %d %d\n", palinfo_array[ii].tag, palinfo_array[ii].ref, palinfo_array[ii].offset, palinfo_array[ii].length); inbuf = (uint8 *) HDmalloc(palinfo_array[0].length * sizeof(uint8)); fprintf(stderr, "palinfo_array[0].length = %d, sizeof(uint8) = %d\n", palinfo_array[0].length, sizeof(uint8)); status = Hgetelement(fid, palinfo_array[0].tag, palinfo_array[0].ref, inbuf); if (HDmemcmp(inbuf, paletteA, palinfo_array[0].length * sizeof(uint8))!=0) fprintf(stderr, "inbuf differs paletteA\n"); else fprintf(stderr, "inbuf is same as paletteA\n"); } /* Terminate access to the GR interface and close the file */ status = GRend(grid); status = Hclose(fid); check_dds(IMAGE_DFPAL_FILE, "Final:\n"); } /* test_getpalinfo */ int main() { test_getpalinfo(); return 0; }