2005-10-12 00:55:29 +08:00
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
2007-02-07 22:56:24 +08:00
|
|
|
* Copyright by The HDF Group. *
|
2005-10-12 00:55:29 +08:00
|
|
|
* Copyright by the Board of Trustees of the University of Illinois. *
|
|
|
|
* All rights reserved. *
|
|
|
|
* *
|
|
|
|
* This file is part of HDF5. The full HDF5 copyright notice, including *
|
|
|
|
* terms governing use, modification, and redistribution, is contained in *
|
|
|
|
* the files COPYING and Copyright.html. COPYING can be found at the root *
|
|
|
|
* of the source code distribution tree; Copyright.html can be found at the *
|
|
|
|
* root level of an installed copy of the electronic HDF5 document set and *
|
|
|
|
* is linked from the top-level documents page. It can also be found at *
|
2007-02-07 22:56:24 +08:00
|
|
|
* http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *
|
|
|
|
* access to either file, you may request a copy from help@hdfgroup.org. *
|
2005-10-12 00:55:29 +08:00
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
2004-11-23 03:17:36 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2006-04-29 14:20:50 +08:00
|
|
|
#include "h5hltest.h"
|
|
|
|
#include "H5LTpublic.h"
|
|
|
|
#include "H5IMpublic.h"
|
|
|
|
#include "pal_rgb.h"
|
2005-03-28 23:24:10 +08:00
|
|
|
|
|
|
|
#define FILE1 "test_image1.h5"
|
|
|
|
#define FILE2 "test_image2.h5"
|
|
|
|
#define FILE3 "test_image3.h5"
|
|
|
|
#define DATA_FILE1 "image8.txt"
|
|
|
|
#define DATA_FILE2 "image24pixel.txt"
|
|
|
|
#define DATA_FILE3 "image24plane.txt"
|
|
|
|
#define DATA_FILE4 "usa.wri"
|
2005-03-30 05:47:53 +08:00
|
|
|
#define PAL2_FILE "sepia.pal"
|
|
|
|
#define PAL3_FILE "earth.pal"
|
2005-03-28 23:24:10 +08:00
|
|
|
#define IMAGE1_NAME "image8bit"
|
|
|
|
#define IMAGE2_NAME "image24bitpixel"
|
|
|
|
#define IMAGE3_NAME "image24bitplane"
|
2005-03-30 05:47:53 +08:00
|
|
|
#define PAL1_NAME "rainbow"
|
|
|
|
#define PAL2_NAME "sepia"
|
|
|
|
#define PAL3_NAME "earth"
|
2005-05-17 03:08:41 +08:00
|
|
|
#define PAL4_NAME "blue-red"
|
2005-03-28 23:24:10 +08:00
|
|
|
|
|
|
|
|
2005-05-17 03:08:41 +08:00
|
|
|
#define WIDTH (hsize_t)500
|
|
|
|
#define HEIGHT (hsize_t)200
|
|
|
|
#define PAL_ENTRIES 9
|
2005-03-30 05:47:53 +08:00
|
|
|
|
|
|
|
/* struct to store RGB values read from a .pal file */
|
|
|
|
typedef struct rgb_t {
|
2005-04-06 04:26:26 +08:00
|
|
|
unsigned char r;
|
2005-03-30 05:47:53 +08:00
|
|
|
unsigned char g;
|
|
|
|
unsigned char b;
|
|
|
|
} rgb_t;
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* prototypes */
|
|
|
|
static int test_simple(void);
|
|
|
|
static int test_data(void);
|
|
|
|
static int test_generate(void);
|
2005-03-30 05:47:53 +08:00
|
|
|
static int read_data(const char* file_name, hsize_t *width, hsize_t *height );
|
2006-04-29 14:20:50 +08:00
|
|
|
static int read_palette(const char* file_name, rgb_t *palette, size_t palette_size);
|
2005-03-30 05:47:53 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* globals */
|
2005-08-14 04:53:35 +08:00
|
|
|
unsigned char *image_data = NULL;
|
2004-11-23 03:17:36 +08:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* the main program
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2005-03-28 23:24:10 +08:00
|
|
|
|
|
|
|
int main(void)
|
2004-11-23 03:17:36 +08:00
|
|
|
{
|
2005-03-28 23:24:10 +08:00
|
|
|
int nerrors=0;
|
|
|
|
|
|
|
|
nerrors += test_simple()<0 ?1:0;
|
|
|
|
nerrors += test_data()<0 ?1:0;
|
|
|
|
nerrors += test_generate()<0 ?1:0;
|
|
|
|
|
|
|
|
if (nerrors) goto error;
|
|
|
|
printf("All image tests passed.\n");
|
|
|
|
return 0;
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
error:
|
|
|
|
printf("***** %d IMAGE TEST%s FAILED! *****\n",nerrors, 1 == nerrors ? "" : "S");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* a simple test that generates images and palettes
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int test_simple(void)
|
|
|
|
{
|
|
|
|
hid_t fid;
|
|
|
|
hsize_t width;
|
|
|
|
hsize_t height;
|
|
|
|
hsize_t planes;
|
2005-05-17 03:08:41 +08:00
|
|
|
hsize_t pal_dims[] = {PAL_ENTRIES,3};
|
2004-11-23 03:17:36 +08:00
|
|
|
hsize_t pal_dims_out[2];
|
|
|
|
char interlace[20];
|
|
|
|
hssize_t npals;
|
2007-03-27 11:06:48 +08:00
|
|
|
size_t i, j;
|
2005-03-28 23:24:10 +08:00
|
|
|
herr_t is_image;
|
|
|
|
herr_t is_pal;
|
|
|
|
unsigned char image_in1 [ WIDTH*HEIGHT ];
|
|
|
|
unsigned char image_out1[ WIDTH*HEIGHT ];
|
|
|
|
unsigned char image_in2 [ WIDTH*HEIGHT*3 ];
|
|
|
|
unsigned char image_out2[ WIDTH*HEIGHT*3 ];
|
2005-05-17 03:08:41 +08:00
|
|
|
unsigned char pal_data_out[PAL_ENTRIES*3];
|
|
|
|
unsigned char n;
|
2006-04-29 14:20:50 +08:00
|
|
|
hsize_t space;
|
2005-05-17 03:08:41 +08:00
|
|
|
/* create a PAL_ENTRIES entry palette */
|
|
|
|
unsigned char pal_data_in[PAL_ENTRIES*3] = {
|
|
|
|
0,0,168, /* dark blue */
|
|
|
|
0,0,252, /* blue */
|
|
|
|
0,168,252, /* ocean blue */
|
|
|
|
84,252,252, /* light blue */
|
|
|
|
168,252,168, /* light green */
|
|
|
|
0,252,168, /* green */
|
|
|
|
252,252,84, /* yellow */
|
|
|
|
252,168,0, /* orange */
|
|
|
|
252,0,0}; /* red */
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-05-17 03:08:41 +08:00
|
|
|
/* create an image of 9 values divided evenly by the array */
|
|
|
|
space = WIDTH*HEIGHT / PAL_ENTRIES;
|
2007-03-27 11:06:48 +08:00
|
|
|
for (i=0, j=0, n=0; i < (size_t)(WIDTH*HEIGHT); i++, j++ )
|
2005-05-17 03:08:41 +08:00
|
|
|
{
|
|
|
|
image_in1[i] = n;
|
2007-03-27 11:06:48 +08:00
|
|
|
if ( j > (size_t)space )
|
2005-05-17 03:08:41 +08:00
|
|
|
{
|
|
|
|
n++;
|
|
|
|
j=0;
|
|
|
|
}
|
|
|
|
if (n>PAL_ENTRIES-1) n=0;
|
|
|
|
}
|
|
|
|
/* create an image 3 byte RGB image */
|
2007-03-27 11:06:48 +08:00
|
|
|
for (i=0, j=0, n=0; i < (size_t)(WIDTH*HEIGHT*3); i++, j++)
|
2005-05-17 03:08:41 +08:00
|
|
|
{
|
|
|
|
image_in2[i] = n;
|
2005-08-14 04:53:35 +08:00
|
|
|
if (j==3)
|
2005-05-17 03:08:41 +08:00
|
|
|
{
|
|
|
|
n++;
|
|
|
|
j=0;
|
|
|
|
}
|
|
|
|
}
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* create a file using default properties */
|
|
|
|
if ((fid=H5Fcreate(FILE1,H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT))<0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
|
|
|
|
printf("Testing API functions\n");
|
2004-11-23 03:17:36 +08:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
2005-03-28 23:24:10 +08:00
|
|
|
* indexed image test
|
2004-11-23 03:17:36 +08:00
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2005-05-17 03:08:41 +08:00
|
|
|
TESTING2("indexed images");
|
2004-11-23 03:17:36 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* write image */
|
|
|
|
if (H5IMmake_image_8bit(fid,"image1",WIDTH,HEIGHT,image_in1)<0)
|
2004-11-23 03:17:36 +08:00
|
|
|
goto out;
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* make a palette */
|
|
|
|
if (H5IMmake_palette(fid,"palette",pal_dims,pal_data_in)<0)
|
2004-11-23 03:17:36 +08:00
|
|
|
goto out;
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* attach the palette to the image dataset */
|
|
|
|
if (H5IMlink_palette(fid,"image1","palette")<0)
|
2004-11-23 03:17:36 +08:00
|
|
|
goto out;
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* get info */
|
|
|
|
if (H5IMget_image_info(fid,"image1",&width,&height,&planes,interlace,&npals)<0)
|
2004-11-23 03:17:36 +08:00
|
|
|
goto out;
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
if (width!=WIDTH)
|
|
|
|
goto out;
|
|
|
|
if (height!=HEIGHT)
|
|
|
|
goto out;
|
|
|
|
if (planes!=1)
|
|
|
|
goto out;
|
|
|
|
if (npals!=1)
|
2004-11-23 03:17:36 +08:00
|
|
|
goto out;
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* read image */
|
|
|
|
if (H5IMread_image(fid,"image1",image_out1)<0)
|
|
|
|
goto out;
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* check */
|
2007-03-27 11:06:48 +08:00
|
|
|
for (i = 0; i < (size_t)(height*width*planes); i++)
|
2005-03-28 23:24:10 +08:00
|
|
|
{
|
2005-08-14 04:53:35 +08:00
|
|
|
if ( image_in1[i] != image_out1[i] )
|
2005-03-28 23:24:10 +08:00
|
|
|
{
|
|
|
|
goto out;
|
2004-11-23 03:17:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-17 03:08:41 +08:00
|
|
|
|
2004-11-23 03:17:36 +08:00
|
|
|
PASSED();
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
2005-03-28 23:24:10 +08:00
|
|
|
* true color image test
|
2004-11-23 03:17:36 +08:00
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
TESTING2("true color image");
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* write image */
|
|
|
|
if (H5IMmake_image_24bit(fid,"image2",WIDTH,HEIGHT,"INTERLACE_PIXEL",image_in2))
|
2004-11-23 03:17:36 +08:00
|
|
|
goto out;
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* get info */
|
|
|
|
if (H5IMget_image_info(fid,"image2",&width,&height,&planes,interlace,&npals)<0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (width!=WIDTH)
|
|
|
|
goto out;
|
|
|
|
if (height!=HEIGHT)
|
|
|
|
goto out;
|
|
|
|
if (planes!=3)
|
2004-11-23 03:17:36 +08:00
|
|
|
goto out;
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* read image */
|
|
|
|
if (H5IMread_image(fid,"image2",image_out2)<0)
|
2004-11-23 03:17:36 +08:00
|
|
|
goto out;
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* check */
|
2007-03-27 11:06:48 +08:00
|
|
|
for (i = 0; i < (size_t)(height*width*planes); i++)
|
2005-03-28 23:24:10 +08:00
|
|
|
{
|
2005-08-14 04:53:35 +08:00
|
|
|
if ( image_in2[i] != image_out2[i] )
|
2005-03-28 23:24:10 +08:00
|
|
|
{
|
2004-11-23 03:17:36 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PASSED();
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* H5IMget_npalettes test
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
TESTING2("palette functions");
|
|
|
|
|
|
|
|
if (H5IMget_npalettes(fid,"image1",&npals)<0)
|
|
|
|
goto out;
|
2004-11-23 03:17:36 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
if (npals!=1)
|
2004-11-23 03:17:36 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* H5IMget_palette_info test
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
if (H5IMget_palette_info(fid,"image1",0,pal_dims_out)<0)
|
2004-11-23 03:17:36 +08:00
|
|
|
goto out;
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* check */
|
2005-08-14 04:53:35 +08:00
|
|
|
for (i = 0; i < 2; i++)
|
2005-03-28 23:24:10 +08:00
|
|
|
{
|
2005-08-14 04:53:35 +08:00
|
|
|
if ( pal_dims[i] != pal_dims_out[i] )
|
2005-03-28 23:24:10 +08:00
|
|
|
{
|
|
|
|
goto out;
|
2004-11-23 03:17:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* H5IMget_palette test
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
if (H5IMget_palette(fid,"image1",0,pal_data_out)<0)
|
2004-11-23 03:17:36 +08:00
|
|
|
goto out;
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* check */
|
2005-08-14 04:53:35 +08:00
|
|
|
for (i = 0; i < PAL_ENTRIES*3; i++)
|
2005-03-28 23:24:10 +08:00
|
|
|
{
|
2005-08-14 04:53:35 +08:00
|
|
|
if ( pal_data_in[i] != pal_data_out[i] )
|
2005-03-28 23:24:10 +08:00
|
|
|
{
|
2004-11-23 03:17:36 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* H5IMis_image test
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
if ((is_image=H5IMis_image(fid,"image1"))<0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (is_image!=1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if ((is_image=H5IMis_image(fid,"image2"))<0)
|
2004-11-23 03:17:36 +08:00
|
|
|
goto out;
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
if (is_image!=1)
|
2004-11-23 03:17:36 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* H5IMis_palette test
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
if ((is_pal=H5IMis_palette(fid,"palette"))<0)
|
2004-11-23 03:17:36 +08:00
|
|
|
goto out;
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
if (is_pal!=1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
PASSED();
|
|
|
|
|
2004-11-23 03:17:36 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
2005-08-14 04:53:35 +08:00
|
|
|
* close
|
2005-03-28 23:24:10 +08:00
|
|
|
*-------------------------------------------------------------------------
|
2005-08-14 04:53:35 +08:00
|
|
|
*/
|
2005-03-28 23:24:10 +08:00
|
|
|
if (H5Fclose(fid)<0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
return 0;
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* error zone, gracefully close */
|
|
|
|
out:
|
|
|
|
H5E_BEGIN_TRY {
|
|
|
|
H5Fclose(fid);
|
|
|
|
} H5E_END_TRY;
|
|
|
|
H5_FAILED();
|
|
|
|
return FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* read sample realistic image data from ASCII files
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int test_data(void)
|
|
|
|
{
|
2005-03-30 05:47:53 +08:00
|
|
|
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 */
|
2005-04-06 04:26:26 +08:00
|
|
|
rgb_t rgb[256]; /* buffer to hold a .pal file palette */
|
2005-03-30 05:47:53 +08:00
|
|
|
int i, n;
|
2005-03-28 23:24:10 +08:00
|
|
|
|
|
|
|
/* 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");
|
|
|
|
|
2005-08-14 04:53:35 +08:00
|
|
|
/* read first data file */
|
2005-03-30 05:47:53 +08:00
|
|
|
if (read_data(DATA_FILE1,&width,&height)<0)
|
2005-03-28 23:24:10 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* make an image */
|
2005-03-30 05:47:53 +08:00
|
|
|
if (H5IMmake_image_8bit(fid,IMAGE1_NAME,width,height,image_data)<0)
|
2005-03-28 23:24:10 +08:00
|
|
|
goto out;
|
2005-03-30 05:47:53 +08:00
|
|
|
|
|
|
|
PASSED();
|
|
|
|
|
|
|
|
|
|
|
|
TESTING2("attaching palettes");
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* palette #1. rainbow palette. data is contained in "pal_rgb.h"
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-30 05:47:53 +08:00
|
|
|
/* initialize the palette data */
|
2005-03-28 23:24:10 +08:00
|
|
|
pal_dims[0] = 256;
|
|
|
|
pal_dims[1] = 3;
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* make a palette */
|
2005-03-30 05:47:53 +08:00
|
|
|
if (H5IMmake_palette(fid,PAL1_NAME,pal_dims,pal_rgb)<0)
|
2005-03-28 23:24:10 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* attach a palette to the image dataset */
|
2005-03-30 05:47:53 +08:00
|
|
|
if (H5IMlink_palette(fid,IMAGE1_NAME,PAL1_NAME)<0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
2005-08-14 04:53:35 +08:00
|
|
|
* palette #2. sepia palette.
|
2005-03-30 05:47:53 +08:00
|
|
|
* read a PAL file and attach the palette to the HDF5 file
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2005-04-06 04:26:26 +08:00
|
|
|
/* read a PAL file */
|
2005-03-30 05:47:53 +08:00
|
|
|
if (read_palette(PAL2_FILE, rgb, sizeof(rgb))<0)
|
|
|
|
goto out;
|
|
|
|
|
2005-04-06 04:26:26 +08:00
|
|
|
/* 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;
|
|
|
|
}
|
2005-03-30 05:47:53 +08:00
|
|
|
|
2005-04-06 04:26:26 +08:00
|
|
|
/* make a palette */
|
2005-03-30 05:47:53 +08:00
|
|
|
if (H5IMmake_palette(fid,PAL2_NAME,pal_dims,pal)<0)
|
|
|
|
goto out;
|
|
|
|
|
2005-04-06 04:26:26 +08:00
|
|
|
/* attach the palette to the image dataset */
|
2005-03-30 05:47:53 +08:00
|
|
|
if (H5IMlink_palette(fid,IMAGE1_NAME,PAL2_NAME)<0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
2005-08-14 04:53:35 +08:00
|
|
|
* palette #3. earth palette.
|
2005-03-30 05:47:53 +08:00
|
|
|
* read a PAL file and attach the palette to the HDF5 file
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2005-04-06 04:26:26 +08:00
|
|
|
/* read a PAL file */
|
2005-03-30 05:47:53 +08:00
|
|
|
if (read_palette(PAL3_FILE, rgb, sizeof(rgb))<0)
|
|
|
|
goto out;
|
|
|
|
|
2005-04-06 04:26:26 +08:00
|
|
|
/* 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;
|
|
|
|
}
|
2005-03-30 05:47:53 +08:00
|
|
|
|
2005-04-06 04:26:26 +08:00
|
|
|
/* make a palette */
|
2005-03-30 05:47:53 +08:00
|
|
|
if (H5IMmake_palette(fid,PAL3_NAME,pal_dims,pal)<0)
|
|
|
|
goto out;
|
|
|
|
|
2005-04-06 04:26:26 +08:00
|
|
|
/* attach the palette to the image dataset */
|
2005-03-30 05:47:53 +08:00
|
|
|
if (H5IMlink_palette(fid,IMAGE1_NAME,PAL3_NAME)<0)
|
2005-03-28 23:24:10 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
PASSED();
|
|
|
|
|
|
|
|
|
2005-05-17 03:08:41 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
2005-08-14 04:53:35 +08:00
|
|
|
* palette #4. blue-red
|
2005-05-17 03:08:41 +08:00
|
|
|
* make a palette whith blue to red colors
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
for ( i=0, n=0; i<256*3; i+=3, n++)
|
|
|
|
{
|
|
|
|
pal[i] =n;
|
|
|
|
pal[i+1]=0;
|
|
|
|
pal[i+2]=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;
|
|
|
|
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* true color image example with pixel interlace
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
TESTING2("make true color image with pixel interlace");
|
|
|
|
|
2005-08-14 04:53:35 +08:00
|
|
|
/* read second data file */
|
2005-03-30 05:47:53 +08:00
|
|
|
if ((read_data(DATA_FILE2,&width,&height))<0)
|
2005-03-28 23:24:10 +08:00
|
|
|
goto out;
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* make image */
|
|
|
|
if ((H5IMmake_image_24bit(fid,IMAGE2_NAME,width,height,"INTERLACE_PIXEL",image_data))<0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
PASSED();
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* True color image example with plane interlace
|
2004-11-23 03:17:36 +08:00
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
TESTING2("make true color image with plane interlace");
|
|
|
|
|
2005-08-14 04:53:35 +08:00
|
|
|
/* read third data file */
|
2005-03-30 05:47:53 +08:00
|
|
|
if ((read_data(DATA_FILE3,&width,&height))<0)
|
2005-03-28 23:24:10 +08:00
|
|
|
goto out;
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* make image */
|
|
|
|
if ((H5IMmake_image_24bit(fid,IMAGE3_NAME,width,height,"INTERLACE_PLANE",image_data))<0)
|
|
|
|
goto out;
|
2004-11-23 03:17:36 +08:00
|
|
|
|
|
|
|
PASSED();
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
2005-08-14 04:53:35 +08:00
|
|
|
* close
|
2005-03-28 23:24:10 +08:00
|
|
|
*-------------------------------------------------------------------------
|
2005-08-14 04:53:35 +08:00
|
|
|
*/
|
2005-03-28 23:24:10 +08:00
|
|
|
if (H5Fclose(fid)<0)
|
|
|
|
goto out;
|
|
|
|
|
2004-11-23 03:17:36 +08:00
|
|
|
return 0;
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* error zone, gracefully close */
|
|
|
|
out:
|
|
|
|
H5E_BEGIN_TRY {
|
|
|
|
H5Fclose(fid);
|
|
|
|
} H5E_END_TRY;
|
|
|
|
H5_FAILED();
|
|
|
|
return FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2005-08-14 04:53:35 +08:00
|
|
|
The following test provides an examples of how to generate HDF5 image data from
|
|
|
|
floating point data. In the example we use real life topographic data
|
|
|
|
(from the North American hemisphere). In the dataset sea values are represented
|
|
|
|
as negative numbers and land values are represented as positive numbers.
|
|
|
|
The example generates 3 HDF5 images, one that generates an image from all the values,
|
|
|
|
another that generates an image from the land values and another that generates an
|
2005-03-28 23:24:10 +08:00
|
|
|
image from the sea values.
|
|
|
|
For the example we used data from MODB, the Mediterranean Oceanic Data Base
|
|
|
|
http://modb.oce.ulg.ac.be/
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
static int test_generate(void)
|
|
|
|
{
|
|
|
|
hid_t fid;
|
|
|
|
hsize_t pal_dims[2] = { 256, 3 };
|
2005-08-14 04:53:35 +08:00
|
|
|
float *data;
|
2005-03-28 23:24:10 +08:00
|
|
|
int imax, jmax, kmax;
|
|
|
|
float valex, xmin, xmax, value;
|
|
|
|
FILE *f;
|
|
|
|
char *srcdir = getenv("srcdir"); /* the source directory */
|
|
|
|
char data_file[512]=""; /* buffer to hold name of existing data file */
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* create a file using default properties */
|
|
|
|
if ((fid=H5Fcreate(FILE3,H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT))<0)
|
|
|
|
goto out;
|
2004-11-23 03:17:36 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
printf("Testing read and process data and make indexed images\n");
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
2005-03-30 05:47:53 +08:00
|
|
|
* compose the name of the file to open, using the srcdir, if appropriate
|
2005-03-28 23:24:10 +08:00
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
if ( srcdir )
|
|
|
|
{
|
|
|
|
strcpy(data_file, srcdir);
|
|
|
|
strcat(data_file, "/");
|
|
|
|
}
|
|
|
|
strcat(data_file,DATA_FILE4);
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-30 05:47:53 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* read data; the file data format is described below
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
f = fopen( data_file, "r" ) ;
|
|
|
|
if ( f == NULL )
|
|
|
|
{
|
2005-03-30 05:47:53 +08:00
|
|
|
printf( "Could not find file %s. Try set $srcdir \n", data_file );
|
2005-03-28 23:24:10 +08:00
|
|
|
H5Fclose(fid);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-08-14 04:53:35 +08:00
|
|
|
!The first line of the ASCII file contains the dimension of the array :
|
|
|
|
! IMAX, JMAX, KMAX. The integers are stored with the FORTRAN format I5.
|
|
|
|
!The second line contains the exclusion value, the minimum and the maximum value of this
|
|
|
|
! file. These numbers are stored with the FORTRAN format E12.5.
|
|
|
|
! The remaining lines contains the data of the array, with 5 numbers per line
|
|
|
|
! (except the last line for each I-line).
|
|
|
|
! The array is stored in horizontal slices from sea surface to sea bottom and from
|
|
|
|
! north to south. So the indexes go from :
|
2005-03-28 23:24:10 +08:00
|
|
|
!
|
|
|
|
! DO K = KMAX to 1
|
|
|
|
! DO J = JMAX to 1
|
|
|
|
! DO I = 1 to IMAX
|
|
|
|
! read
|
|
|
|
! OD
|
|
|
|
! OD
|
|
|
|
! OD
|
2005-08-14 04:53:35 +08:00
|
|
|
!
|
2005-03-28 23:24:10 +08:00
|
|
|
! ____________________________
|
|
|
|
! / /| (imax,jmax,kmax)
|
|
|
|
! / sea surface / |
|
|
|
|
! / / |
|
|
|
|
! /__________________________ / |
|
|
|
|
! | | |
|
|
|
|
! | | | (imax,jmax,1) n
|
|
|
|
! | | / /
|
|
|
|
! | | / /
|
|
|
|
! ^ j | | / w <-----o-----> e
|
|
|
|
! k | / |__________________________|/ /
|
|
|
|
! | / (imax,1,1) /
|
|
|
|
! |----------> s
|
|
|
|
! i
|
|
|
|
!
|
|
|
|
*/
|
2005-08-14 04:53:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
fscanf( f, "%d %d %d", &imax, &jmax, &kmax );
|
|
|
|
fscanf( f, "%f %f %f", &valex, &xmin, &xmax );
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
data = (float*) malloc ( imax * jmax * kmax * sizeof( float ));
|
|
|
|
image_data = (unsigned char*) malloc ( imax * jmax * kmax * sizeof( unsigned char ));
|
2005-08-14 04:53:35 +08:00
|
|
|
|
|
|
|
for ( i = 0; i < imax * jmax * kmax; i++ )
|
2005-03-28 23:24:10 +08:00
|
|
|
{
|
2005-08-14 04:53:35 +08:00
|
|
|
fscanf( f, "%f ", &value );
|
2005-03-28 23:24:10 +08:00
|
|
|
data[i] = value;
|
|
|
|
}
|
|
|
|
fclose( f );
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* transform the data from floating point to unsigend char
|
|
|
|
* we are processing all the data here
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
TESTING2("make indexed image from all the data");
|
2005-08-14 04:53:35 +08:00
|
|
|
|
|
|
|
for ( i = 0; i < imax * jmax * kmax; i++ )
|
2005-03-28 23:24:10 +08:00
|
|
|
{
|
|
|
|
image_data[i] = (unsigned char)(( 255 * (data[i] - xmin ) ) / (xmax - xmin ));
|
|
|
|
}
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* Make the image */
|
|
|
|
if ((H5IMmake_image_8bit(fid,"All data",(hsize_t)imax,(hsize_t)jmax,image_data))<0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
PASSED();
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* transform the data from floating point to unsigend char
|
|
|
|
* here we just process the land data
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
TESTING2("make indexed image from land data");
|
2005-08-14 04:53:35 +08:00
|
|
|
|
|
|
|
for ( i = 0; i < imax * jmax * kmax; i++ )
|
2005-03-28 23:24:10 +08:00
|
|
|
{
|
|
|
|
if ( data[i] < 0 )
|
|
|
|
image_data[i] = 0;
|
|
|
|
else
|
|
|
|
image_data[i] = (unsigned char)(( 255 * (data[i] ) ) / xmax );
|
|
|
|
}
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* make the image */
|
|
|
|
if ((H5IMmake_image_8bit(fid,"Land data",(hsize_t)imax,(hsize_t)jmax,image_data))<0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
PASSED();
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* transform the data from floating point to unsigend char
|
|
|
|
* here we just process the sea data
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
TESTING2("make indexed image from sea data");
|
2005-08-14 04:53:35 +08:00
|
|
|
|
|
|
|
for ( i = 0; i < imax * jmax * kmax; i++ )
|
2005-03-28 23:24:10 +08:00
|
|
|
{
|
|
|
|
if ( data[i] > 0 )
|
|
|
|
image_data[i] = 0;
|
|
|
|
else
|
|
|
|
image_data[i] = (unsigned char)(( 255 * (data[i] - xmin ) ) / xmin );
|
|
|
|
}
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* make the image */
|
|
|
|
if ((H5IMmake_image_8bit(fid,"Sea data",(hsize_t)imax,(hsize_t)jmax,image_data))<0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
PASSED();
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* make a palette and attach it to the datasets
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
TESTING2("attaching palettes");
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* make a palette */
|
2005-03-30 05:47:53 +08:00
|
|
|
if ((H5IMmake_palette(fid,PAL1_NAME,pal_dims,pal_rgb))<0)
|
2005-03-28 23:24:10 +08:00
|
|
|
goto out;
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-04-06 04:26:26 +08:00
|
|
|
/* attach the palette to the image datasets */
|
2005-03-30 05:47:53 +08:00
|
|
|
if ((H5IMlink_palette(fid,"All data",PAL1_NAME))<0)
|
2005-03-28 23:24:10 +08:00
|
|
|
goto out;
|
2005-03-30 05:47:53 +08:00
|
|
|
if ((H5IMlink_palette(fid,"Land data",PAL1_NAME))<0)
|
2005-03-28 23:24:10 +08:00
|
|
|
goto out;
|
2005-03-30 05:47:53 +08:00
|
|
|
if ((H5IMlink_palette(fid,"Sea data",PAL1_NAME))<0)
|
2005-03-28 23:24:10 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
PASSED();
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
2005-08-14 04:53:35 +08:00
|
|
|
* close
|
2005-03-28 23:24:10 +08:00
|
|
|
*-------------------------------------------------------------------------
|
2005-08-14 04:53:35 +08:00
|
|
|
*/
|
2005-03-28 23:24:10 +08:00
|
|
|
if (H5Fclose(fid)<0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
return 0;
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
/* error zone, gracefully close */
|
2004-11-23 03:17:36 +08:00
|
|
|
out:
|
2005-03-28 23:24:10 +08:00
|
|
|
H5E_BEGIN_TRY {
|
|
|
|
H5Fclose(fid);
|
|
|
|
} H5E_END_TRY;
|
2004-11-23 03:17:36 +08:00
|
|
|
H5_FAILED();
|
2005-03-28 23:24:10 +08:00
|
|
|
return FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* read_data
|
|
|
|
* utility function to read ASCII image data
|
|
|
|
* the files have a header of the type
|
|
|
|
*
|
|
|
|
* components
|
|
|
|
* n
|
|
|
|
* height
|
|
|
|
* n
|
|
|
|
* width
|
|
|
|
* n
|
2005-08-14 04:53:35 +08:00
|
|
|
*
|
2005-03-28 23:24:10 +08:00
|
|
|
* followed by the image data
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2005-03-30 05:47:53 +08:00
|
|
|
static int read_data( const char* fname, /*IN*/
|
2005-03-28 23:24:10 +08:00
|
|
|
hsize_t *width, /*OUT*/
|
|
|
|
hsize_t *height /*OUT*/ )
|
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
int color_planes;
|
|
|
|
char str[20];
|
|
|
|
FILE *f;
|
|
|
|
int w, h;
|
2005-03-30 05:47:53 +08:00
|
|
|
char *srcdir = getenv("srcdir"); /* the source directory */
|
|
|
|
char data_file[512]=""; /* buffer to hold name of existing data file */
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* compose the name of the file to open, using "srcdir", if appropriate
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
strcpy(data_file, "");
|
|
|
|
if (srcdir)
|
|
|
|
{
|
|
|
|
strcpy(data_file, srcdir);
|
|
|
|
strcat(data_file, "/");
|
|
|
|
}
|
|
|
|
strcat(data_file,fname);
|
2005-03-28 23:24:10 +08:00
|
|
|
|
2005-03-30 05:47:53 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* read
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2005-03-28 23:24:10 +08:00
|
|
|
|
2005-03-30 05:47:53 +08:00
|
|
|
f = fopen(data_file, "r");
|
2005-03-28 23:24:10 +08:00
|
|
|
if ( f == NULL )
|
|
|
|
{
|
2005-03-30 05:47:53 +08:00
|
|
|
printf( "Could not open file %s. Try set $srcdir \n", data_file );
|
2005-03-28 23:24:10 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fscanf( f, "%s", str );
|
|
|
|
fscanf( f, "%d", &color_planes );
|
|
|
|
fscanf( f, "%s", str );
|
2005-08-14 04:53:35 +08:00
|
|
|
fscanf( f, "%d", &h);
|
2005-03-28 23:24:10 +08:00
|
|
|
fscanf( f, "%s", str );
|
2005-08-14 04:53:35 +08:00
|
|
|
fscanf( f, "%d", &w);
|
|
|
|
|
2005-03-28 23:24:10 +08:00
|
|
|
*width = (hsize_t)w;
|
|
|
|
*height = (hsize_t)h;
|
|
|
|
|
|
|
|
if ( image_data )
|
|
|
|
{
|
|
|
|
free( image_data );
|
|
|
|
image_data=NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
image_data = (unsigned char*) malloc (w * h * 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);
|
|
|
|
|
2004-11-23 03:17:36 +08:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
}
|
2005-03-30 05:47:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* read_palette
|
|
|
|
* Read an ASCII palette file .PAL into an array
|
|
|
|
* the files have a header of the type
|
|
|
|
*
|
2005-08-14 04:53:35 +08:00
|
|
|
* Parameters:
|
2005-04-06 04:26:26 +08:00
|
|
|
* fname - name of file to read.
|
|
|
|
* palette - array of rgb_t to store the read palette.
|
|
|
|
* palette_size - number of elements in 'palette' array
|
2005-03-30 05:47:53 +08:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#define STRING_JASC "JASC-PAL"
|
|
|
|
#define VERSION_JASC "0100"
|
|
|
|
#define STRING_CWPAL "CWPAL"
|
|
|
|
#define VERSION_CWPAL "100"
|
|
|
|
|
2005-08-14 04:53:35 +08:00
|
|
|
static int read_palette(const char* fname,
|
|
|
|
rgb_t *palette,
|
2006-04-29 14:20:50 +08:00
|
|
|
size_t palette_size)
|
2005-03-30 05:47:53 +08:00
|
|
|
{
|
2005-04-06 04:26:26 +08:00
|
|
|
FILE *file;
|
2005-03-30 05:47:53 +08:00
|
|
|
char buffer[80];
|
2006-10-09 11:51:03 +08:00
|
|
|
unsigned u;
|
2005-03-30 05:47:53 +08:00
|
|
|
unsigned int red;
|
|
|
|
unsigned int green;
|
|
|
|
unsigned int blue;
|
2006-04-29 14:20:50 +08:00
|
|
|
unsigned nentries;
|
2005-03-30 05:47:53 +08:00
|
|
|
char *srcdir = getenv("srcdir"); /* the source directory */
|
|
|
|
char data_file[512]; /* buffer to hold name of existing data file */
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
* compose the name of the file to open, using "srcdir", if appropriate
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
strcpy(data_file, "");
|
|
|
|
if (srcdir)
|
|
|
|
{
|
|
|
|
strcpy(data_file, srcdir);
|
|
|
|
strcat(data_file, "/");
|
|
|
|
}
|
|
|
|
strcat(data_file,fname);
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-30 05:47:53 +08:00
|
|
|
/* ensure the given palette is valid */
|
|
|
|
if (!palette)
|
2005-04-06 04:26:26 +08:00
|
|
|
return -1;
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-30 05:47:53 +08:00
|
|
|
/* open the input file */
|
2005-04-06 04:26:26 +08:00
|
|
|
if (!(file = fopen(data_file, "r")))
|
2005-03-30 05:47:53 +08:00
|
|
|
{
|
|
|
|
printf( "Could not open file %s. Try set $srcdir \n", data_file );
|
|
|
|
return -1;
|
|
|
|
}
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-30 05:47:53 +08:00
|
|
|
/* read the file ident string */
|
2005-04-06 04:26:26 +08:00
|
|
|
if (fgets(buffer, sizeof(buffer), file) == NULL)
|
|
|
|
{
|
|
|
|
fclose(file);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-03-30 05:47:53 +08:00
|
|
|
/* ensure it matches the palette file ident string */
|
2005-08-14 04:53:35 +08:00
|
|
|
if ( strncmp(buffer, STRING_JASC, sizeof(STRING_JASC) - 1) != 0 &&
|
2005-04-06 04:26:26 +08:00
|
|
|
strncmp(buffer, STRING_CWPAL, sizeof(STRING_CWPAL) - 1) != 0 )
|
|
|
|
{
|
|
|
|
fclose(file);
|
2005-03-30 05:47:53 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-04-06 04:26:26 +08:00
|
|
|
/* read the version string */
|
|
|
|
if (fgets(buffer, sizeof(buffer), file) == NULL)
|
2005-03-30 05:47:53 +08:00
|
|
|
{
|
2005-04-06 04:26:26 +08:00
|
|
|
fclose(file);
|
2005-03-30 05:47:53 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2005-08-14 04:53:35 +08:00
|
|
|
|
|
|
|
/* ensure it matches the palette file version string */
|
|
|
|
if ( strncmp(buffer, VERSION_JASC, sizeof(VERSION_JASC) - 1) != 0 &&
|
2005-04-06 04:26:26 +08:00
|
|
|
strncmp(buffer, VERSION_CWPAL, sizeof(VERSION_CWPAL) - 1) != 0 )
|
2005-03-30 05:47:53 +08:00
|
|
|
{
|
2005-04-06 04:26:26 +08:00
|
|
|
fclose(file);
|
2005-03-30 05:47:53 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-04-06 04:26:26 +08:00
|
|
|
/* read the number of colors */
|
2005-03-30 05:47:53 +08:00
|
|
|
if (fgets(buffer, sizeof(buffer), file) == NULL)
|
|
|
|
{
|
2005-04-06 04:26:26 +08:00
|
|
|
fclose(file);
|
2005-03-30 05:47:53 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2005-08-14 04:53:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* extract the number of colors.
|
2005-04-06 04:26:26 +08:00
|
|
|
check for missing version or number of colors
|
|
|
|
in this case it reads the first entry
|
2005-03-30 05:47:53 +08:00
|
|
|
*/
|
2005-08-14 04:53:35 +08:00
|
|
|
if ( strlen( buffer ) > 4 )
|
2005-03-30 05:47:53 +08:00
|
|
|
{
|
2005-04-06 04:26:26 +08:00
|
|
|
fclose(file);
|
2005-03-30 05:47:53 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2006-04-29 14:20:50 +08:00
|
|
|
if (sscanf(buffer, "%u", &nentries) != 1)
|
2005-03-30 05:47:53 +08:00
|
|
|
{
|
2005-04-06 04:26:26 +08:00
|
|
|
fclose(file);
|
2005-03-30 05:47:53 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2005-08-14 04:53:35 +08:00
|
|
|
|
|
|
|
/* ensure there are a sensible number of colors in the palette */
|
2006-10-09 11:51:03 +08:00
|
|
|
if ((nentries > 256) || (nentries > palette_size))
|
2005-03-30 05:47:53 +08:00
|
|
|
{
|
2005-04-06 04:26:26 +08:00
|
|
|
fclose(file);
|
|
|
|
return(-1);
|
|
|
|
}
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-04-06 04:26:26 +08:00
|
|
|
/* read the palette entries */
|
2006-10-09 11:51:03 +08:00
|
|
|
for (u = 0; u < nentries; u++)
|
2005-04-06 04:26:26 +08:00
|
|
|
{
|
|
|
|
/* extract the red, green and blue color components. */
|
|
|
|
if (fscanf(file, "%u %u %u", &red, &green, &blue) != 3)
|
|
|
|
{
|
|
|
|
fclose(file);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* store this palette entry */
|
2006-10-09 11:51:03 +08:00
|
|
|
palette[u].r = (unsigned char)red;
|
|
|
|
palette[u].g = (unsigned char)green;
|
|
|
|
palette[u].b = (unsigned char)blue;
|
2005-03-30 05:47:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* close file */
|
2005-04-06 04:26:26 +08:00
|
|
|
fclose(file);
|
2005-08-14 04:53:35 +08:00
|
|
|
|
2005-04-06 04:26:26 +08:00
|
|
|
return nentries;
|
2005-03-30 05:47:53 +08:00
|
|
|
}
|
|
|
|
|