Esempio n. 1
0
int append_hdf5_image(const char *filename, const char * img_name, unsigned char *img, int width, int height)
{
  hid_t  file_id;
  herr_t status;
  file_id = H5Fopen(filename, H5F_ACC_RDWR, H5P_DEFAULT );
  status  = H5IMmake_image_24bit(file_id, img_name, width, height, "INTERLACE_PIXEL", img );
  H5Fclose( file_id );
  return status;
}
Esempio n. 2
0
static int test_simple(void)
{
    hsize_t       width    = WIDTH;
    hsize_t       height   = HEIGHT;
    hsize_t       planes;
    hid_t         fid;
    int           i, j, n, space;
    hsize_t       u;
    char          interlace[20];
    hssize_t      npals;

    /* 8-bit image */
    unsigned char *buf1 = NULL;
    unsigned char pal[ PAL_ENTRIES * 3 ];        /* palette array */
    hsize_t       pal_dims[2] = {PAL_ENTRIES,3}; /* palette dimensions */

    /* 24-bit image */
    unsigned char *buf2 = NULL;

    /* read data */
    unsigned char *buf1_out = NULL;
    unsigned char *buf2_out = NULL;
    unsigned char pal_out[ PAL_ENTRIES * 3 ];    /* palette array */
    hsize_t       pal_dims_out[2];               /* palette dimensions */

    /* Allocate image buffers */
    buf1 = (unsigned char *)HDmalloc(WIDTH * HEIGHT);
    HDassert(buf1);
    buf2  = (unsigned char *)HDmalloc(WIDTH * HEIGHT * 3);
    HDassert(buf2);
    buf1_out = (unsigned char *)HDmalloc(WIDTH * HEIGHT);
    HDassert(buf1_out);
    buf2_out  = (unsigned char *)HDmalloc(WIDTH * HEIGHT * 3);
    HDassert(buf2_out);

    /* create an image */
    space = WIDTH*HEIGHT / PAL_ENTRIES;
    for (i=0, j=0, n=0; i < WIDTH*HEIGHT; i++, j++ )
    {
        buf1[i] = (unsigned char)n;
        if ( j > space )
        {
            n++;
            j=0;
        }

    }

    /* create an image */
    space = WIDTH*HEIGHT / 256;
    for (i=0, j=0, n=0; i < WIDTH*HEIGHT*3; i+=3, j++ )
    {
        buf2[i]   = (unsigned char)n;
        buf2[i+1] = 0;
        buf2[i+2] = (unsigned char)(255 - n);
        if ( j > space )
        {
            n++;
            j=0;
        }
    }

    /*-------------------------------------------------------------------------
    * define a palette, blue to red tones
    *-------------------------------------------------------------------------
    */
    for ( i=0, n=0; i<PAL_ENTRIES*3; i+=3, n++)
    {
        pal[i]  =(unsigned char)n;      /* red */
        pal[i+1]=0;      /* green */
        pal[i+2]=(unsigned char)(255 - n);  /* blue */
    }

    /* Create a new HDF5 file using default properties. */
    fid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT );

    /*-------------------------------------------------------------------------
    * Indexed image test
    *-------------------------------------------------------------------------
    */

    TESTING("indexed image");

    /* Write image */
    if ( H5IMmake_image_8bit( fid, IMAGE1_NAME, width, height, buf1 ) < 0 )
        goto out;

    /* Make a palette */
    if ( H5IMmake_palette( fid, PAL_NAME, pal_dims, pal ) < 0 )
        goto out;

    /* Attach a palette to the image dataset */
    if ( H5IMlink_palette( fid, IMAGE1_NAME, PAL_NAME ) < 0 )
        goto out;

    /* Read image */
    if ( H5IMget_image_info( fid, IMAGE1_NAME, &width, &height, &planes, interlace, &npals ) < 0 )
        goto out;

    if ( H5IMread_image( fid, IMAGE1_NAME, buf1_out ) < 0 )
        goto out;

    for (u = 0; u < height*width*planes; u++)
    {
        if ( buf1[u] != buf1_out[u] )
            goto out;

    }


    PASSED();

    /*-------------------------------------------------------------------------
    * True color image test
    *-------------------------------------------------------------------------
    */

    TESTING("true color image");

    /* Write image */
    if ( H5IMmake_image_24bit( fid, IMAGE2_NAME, width, height, "INTERLACE_PIXEL", buf2 ) )
        goto out;

    /* Read image */
    if ( H5IMget_image_info( fid, IMAGE2_NAME, &width, &height, &planes, interlace, &npals ) < 0 )
        goto out;

    if ( H5IMread_image( fid, IMAGE2_NAME, buf2_out ) < 0 )
        goto out;

    for (u = 0; u < height*width*planes; u++)
    {
        if ( buf2[u] != buf2_out[u] )
            goto out;
    }


    PASSED();

    /*-------------------------------------------------------------------------
    * H5IMget_npalettes test
    *-------------------------------------------------------------------------
    */

    TESTING("pallete functions");

    if ( H5IMget_npalettes( fid, IMAGE1_NAME, &npals ) < 0 )
        goto out;

    /*-------------------------------------------------------------------------
    * H5IMget_palette_info test
    *-------------------------------------------------------------------------
    */

    if ( H5IMget_palette_info( fid, IMAGE1_NAME, 0, pal_dims_out ) < 0 )
        goto out;

    for (i = 0; i < 2; i++)
    {
        if ( pal_dims[i] != pal_dims_out[i] )
            goto out;
    }

    /*-------------------------------------------------------------------------
    * H5IMget_palette test
    *-------------------------------------------------------------------------
    */

    if ( H5IMget_palette( fid, IMAGE1_NAME, 0, pal_out ) < 0 )
        goto out;

    for (i = 0; i < PAL_ENTRIES * 3; i++)
    {
        if ( pal[i] != pal_out[i] )
            goto out;
    }

    /*-------------------------------------------------------------------------
    * H5IMis_image test
    *-------------------------------------------------------------------------
    */

    if ( H5IMis_image( fid, IMAGE1_NAME ) < 0 )
        goto out;

    if ( H5IMis_image( fid, IMAGE2_NAME ) < 0 )
        goto out;

    /*-------------------------------------------------------------------------
    * H5IMis_palette test
    *-------------------------------------------------------------------------
    */

    if ( H5IMis_palette( fid, PAL_NAME ) < 0 )
        goto out;

    /*-------------------------------------------------------------------------
    * end tests
    *-------------------------------------------------------------------------
    */

    if(buf1)
        HDfree(buf1);
    if(buf2)
        HDfree(buf2);
    if(buf1_out)
        HDfree(buf1_out);
    if(buf2_out)
        HDfree(buf2_out);

    /* Close the file. */
    if(H5Fclose( fid ) < 0)
        goto out;


    PASSED();

    return 0;

    /* error zone, gracefully close */
out:
    if(buf1)
        HDfree(buf1);
    if(buf2)
        HDfree(buf2);
    if(buf1_out)
        HDfree(buf1_out);
    if(buf2_out)
        HDfree(buf2_out);
    H5E_BEGIN_TRY {
        H5Fclose(fid);
    } H5E_END_TRY;
    H5_FAILED();
    return FAIL;
}
Esempio n. 3
0
static int test_data(void)
{
    hid_t          fid;
    hsize_t        pal_dims[2];
    hsize_t        width;
    hsize_t        height;
    unsigned char  pal[256*3]; /* buffer to hold an HDF5 palette */
    rgb_t          rgb[256];   /* buffer to hold a .pal file palette */
    int            i, n;

    /* create a file using default properties */
    if ((fid=H5Fcreate(FILE2,H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT))<0)
        goto out;

    printf("Testing read ascii image data and generate images\n");

    /*-------------------------------------------------------------------------
    * read 8bit image data
    *-------------------------------------------------------------------------
    */

    TESTING2("make indexed image");

    /* read first data file */
    if (read_data(DATA_FILE1,&width,&height)<0)
        goto out;

    /* make an image */
    if (H5IMmake_image_8bit(fid,IMAGE1_NAME,width,height,image_data)<0)
        goto out;

    PASSED();


    TESTING2("attaching palettes");

    /*-------------------------------------------------------------------------
    * palette #1. rainbow palette. data is contained in "pal_rgb.h"
    *-------------------------------------------------------------------------
    */

    /* initialize the palette data */
    pal_dims[0] = 256;
    pal_dims[1] = 3;

    /* make a palette */
    if (H5IMmake_palette(fid,PAL1_NAME,pal_dims,pal_rgb)<0)
        goto out;

    /* attach a palette to the image dataset */
    if (H5IMlink_palette(fid,IMAGE1_NAME,PAL1_NAME)<0)
        goto out;

    /*-------------------------------------------------------------------------
    * palette #2. sepia palette.
    * read a PAL file and attach the palette to the HDF5 file
    *-------------------------------------------------------------------------
    */

    /* read a PAL file */
    if (read_palette(PAL2_FILE, rgb, (sizeof(rgb) / sizeof(rgb[0]))) < 0)
        goto out;

    /* transfer to the HDF5 buffer */
    for ( i=0, n=0; i<256*3; i+=3, n++)
    {
        pal[i]  =rgb[n].r;
        pal[i+1]=rgb[n].g;
        pal[i+2]=rgb[n].b;
    }

    /* make a palette */
    if (H5IMmake_palette(fid,PAL2_NAME,pal_dims,pal)<0)
        goto out;

    /* attach the palette to the image dataset */
    if (H5IMlink_palette(fid,IMAGE1_NAME,PAL2_NAME)<0)
        goto out;

    /*-------------------------------------------------------------------------
    * palette #3. earth palette.
    * read a PAL file and attach the palette to the HDF5 file
    *-------------------------------------------------------------------------
    */

    /* read a PAL file */
    if (read_palette(PAL3_FILE, rgb, (sizeof(rgb) / sizeof(rgb[0]))) < 0)
        goto out;

    /* transfer to the HDF5 buffer */
    for ( i=0, n=0; i<256*3; i+=3, n++)
    {
        pal[i]  =rgb[n].r;
        pal[i+1]=rgb[n].g;
        pal[i+2]=rgb[n].b;
    }

    /* make a palette */
    if (H5IMmake_palette(fid,PAL3_NAME,pal_dims,pal)<0)
        goto out;

    /* attach the palette to the image dataset */
    if (H5IMlink_palette(fid,IMAGE1_NAME,PAL3_NAME)<0)
        goto out;

    PASSED();


    /*-------------------------------------------------------------------------
    * palette #4. blue-red
    * make a palette whith blue to red colors
    *-------------------------------------------------------------------------
    */
    for ( i=0, n=0; i<256*3; i+=3, n++)
    {
        pal[i]  =(unsigned char)n;
        pal[i+1]=0;
        pal[i+2]=(unsigned char)(255 - n);
    }

    /* make a palette */
    if (H5IMmake_palette(fid,PAL4_NAME,pal_dims,pal)<0)
        goto out;

    /* attach the palette to the image dataset */
    if (H5IMlink_palette(fid,IMAGE1_NAME,PAL4_NAME)<0)
        goto out;


    /*-------------------------------------------------------------------------
    * true color image example with pixel interlace
    *-------------------------------------------------------------------------
    */

    TESTING2("make true color image with pixel interlace");

    /* read second data file */
    if ((read_data(DATA_FILE2,&width,&height))<0)
        goto out;

    /* make image */
    if ((H5IMmake_image_24bit(fid,IMAGE2_NAME,width,height,"INTERLACE_PIXEL",image_data))<0)
        goto out;

    PASSED();

    /*-------------------------------------------------------------------------
    * True color image example with plane interlace
    *-------------------------------------------------------------------------
    */

    TESTING2("make true color image with plane interlace");

    /* read third data file */
    if ((read_data(DATA_FILE3,&width,&height))<0)
        goto out;

    /* make image */
    if ((H5IMmake_image_24bit(fid,IMAGE3_NAME,width,height,"INTERLACE_PLANE",image_data))<0)
        goto out;

    PASSED();


    /*-------------------------------------------------------------------------
    * close
    *-------------------------------------------------------------------------
    */
    if (H5Fclose(fid)<0)
        goto out;

    /* Release memory buffer */
    HDfree(image_data);

    return 0;

    /* error zone, gracefully close */
out:
    /* Release memory buffer */
    if(image_data)
        HDfree(image_data);

    H5E_BEGIN_TRY {
        H5Fclose(fid);
    } H5E_END_TRY;

    H5_FAILED();
    return FAIL;
}