[svn-r10507] Purpose:

add a new feature for the test image API, read a palette from an ASCII file and attach several
palettes to an image

Description:

Solution:

Platforms tested:
linux
solaris
AIX
IRIX

Misc. update:
This commit is contained in:
Pedro Vicente Nunes 2005-03-29 16:47:53 -05:00
parent a9d5fc42eb
commit 7f98ee5db8
4 changed files with 791 additions and 52 deletions

View File

@ -1614,6 +1614,8 @@
./hl/test/dsdata.txt
./hl/test/dslat.txt
./hl/test/dslon.txt
./hl/test/sepia.pal
./hl/test/earth.pal
# hl fortran
./hl/fortran/src/H5f90i.h

260
hl/test/earth.pal Normal file
View File

@ -0,0 +1,260 @@
CWPAL
100
256
255 255 255
255 222 54
255 214 54
255 204 54
255 196 54
255 189 54
255 179 54
255 171 54
255 163 51
255 153 51
255 145 51
255 135 51
255 128 51
255 120 51
255 110 51
255 102 51
255 102 51
255 102 51
255 102 51
255 102 51
255 102 51
255 102 51
255 102 51
255 102 51
255 102 51
255 102 51
255 102 51
255 102 51
255 102 51
255 102 51
255 102 51
255 102 51
250 99 51
250 99 48
247 99 48
247 97 48
245 97 48
245 97 48
242 97 48
242 97 46
240 94 46
237 94 46
237 94 46
235 94 46
232 92 46
232 92 46
230 92 43
227 89 43
227 89 43
224 89 43
224 89 43
222 89 43
222 87 43
219 87 41
217 87 41
217 84 41
214 84 41
214 84 41
212 84 41
209 84 41
209 82 41
207 82 38
204 79 38
204 79 38
201 79 38
201 79 38
199 79 38
199 79 38
196 77 38
194 77 38
194 77 38
191 77 38
191 74 38
189 74 38
186 74 38
186 74 38
184 82 41
181 89 43
179 99 48
176 107 54
173 115 59
171 125 61
171 135 66
168 143 71
166 150 74
163 161 79
161 168 84
158 176 89
158 186 92
156 194 97
176 204 102
148 199 102
145 196 102
143 194 102
138 189 102
135 186 102
133 184 102
128 181 102
125 176 102
122 173 102
117 168 102
115 166 102
112 158 102
107 158 102
105 156 102
102 153 102
102 153 97
102 153 94
102 153 92
102 153 89
102 153 84
102 153 82
102 153 79
102 153 74
102 153 71
102 153 66
102 153 64
102 153 61
102 153 56
102 153 54
102 153 51
102 153 51
102 153 54
102 150 54
105 150 56
105 150 59
105 150 59
107 150 61
107 150 61
107 148 64
107 148 66
107 148 66
110 148 69
110 148 69
110 148 71
112 148 74
102 138 74
97 128 74
89 117 77
82 107 77
74 97 77
66 89 79
59 79 79
51 69 79
43 59 79
36 48 82
28 38 82
23 28 82
13 18 84
8 10 84
0 0 84
0 0 87
0 0 89
0 0 89
0 0 92
0 0 92
0 0 94
0 0 94
0 0 97
0 0 97
0 0 99
0 0 102
0 0 102
0 0 105
0 0 107
0 0 107
0 3 110
0 8 112
0 10 115
0 13 117
0 18 120
0 23 122
0 26 125
0 28 128
0 33 130
0 36 135
0 41 135
0 43 140
0 46 140
0 51 143
0 56 148
5 54 138
10 51 128
13 48 117
18 46 107
23 46 97
28 43 89
33 41 79
36 41 69
41 38 59
46 36 48
51 33 38
56 33 28
59 31 18
64 28 10
69 26 0
66 26 0
64 23 0
64 23 0
61 23 0
61 23 0
59 23 0
56 23 0
56 20 0
54 20 0
54 20 0
51 20 0
51 18 0
48 18 0
46 18 0
46 18 0
43 15 0
41 15 0
41 15 0
38 13 0
38 13 0
36 13 0
33 13 0
33 13 0
31 13 0
31 10 0
28 10 0
28 10 0
26 10 0
23 10 0
23 8 0
36 23 15
51 38 33
64 54 48
79 69 64
94 84 79
110 99 97
122 115 112
138 130 128
153 148 143
166 163 161
181 179 176
196 194 191
209 209 209
224 224 224
240 240 240
224 224 224
209 209 209
191 191 191
176 176 176
161 161 161
143 143 143
128 128 128
112 112 112
97 97 97
79 79 79
64 64 64
48 48 48
33 33 33
15 15 15
0 0 0


260
hl/test/sepia.pal Normal file
View File

@ -0,0 +1,260 @@
CWPAL
100
256
0 0 0
43 28 14
57 35 16
65 35 6
66 36 7
67 37 8
68 38 9
69 39 10
70 40 11
71 41 12
72 42 13
73 43 14
74 44 15
75 45 16
76 46 17
77 47 18
78 48 19
79 49 20
80 50 21
81 51 22
82 52 23
83 53 24
83 53 24
84 54 25
85 55 26
86 56 27
87 57 28
88 58 29
89 59 30
90 60 31
91 61 32
92 62 33
93 63 34
94 64 35
95 65 36
96 66 37
97 67 38
98 68 39
99 69 40
100 70 41
101 71 42
102 72 43
103 73 44
104 74 45
105 75 46
106 76 47
107 77 48
107 77 48
108 78 49
109 79 50
110 80 51
111 81 52
112 82 53
113 83 54
114 84 55
115 85 56
116 86 57
117 87 58
118 88 59
119 89 60
120 90 61
121 91 62
122 92 63
123 93 64
124 94 65
125 95 66
125 95 66
126 96 67
127 97 68
128 98 69
129 99 70
130 100 71
131 101 72
132 102 73
133 103 74
134 104 75
135 105 76
136 106 77
137 107 78
138 108 79
139 109 80
140 110 81
141 111 82
142 112 83
143 113 84
144 114 85
145 115 86
146 116 87
147 117 88
148 118 89
149 119 90
150 120 91
151 121 92
152 122 93
153 123 94
154 124 95
155 125 96
156 126 97
157 127 98
158 128 99
159 129 100
160 130 101
161 131 102
162 132 103
163 133 104
164 134 105
165 135 106
166 136 107
167 137 108
168 138 109
169 139 110
170 140 111
171 141 112
172 142 113
173 143 114
174 144 115
175 145 116
176 146 117
177 147 118
178 148 119
179 149 120
180 150 121
181 151 122
182 152 123
183 153 124
184 154 125
185 155 126
186 156 127
187 157 128
187 157 128
188 158 129
189 159 130
190 160 131
191 161 132
192 162 133
193 163 134
194 164 135
195 165 136
196 166 137
197 167 138
198 168 139
199 169 140
200 170 141
201 171 142
202 172 143
203 173 144
204 174 145
205 175 146
206 176 147
207 177 148
208 178 149
209 179 150
210 180 151
211 181 152
212 182 153
213 183 154
214 184 155
215 185 156
216 186 157
217 187 158
218 188 159
219 189 160
220 190 161
221 191 162
222 192 163
223 193 164
224 194 165
225 195 166
226 196 167
227 197 168
228 198 169
229 199 170
230 200 171
231 201 172
232 202 173
233 203 174
234 204 175
235 205 176
236 206 177
237 207 178
238 208 179
239 209 180
240 210 181
241 211 182
242 212 183
243 213 184
244 214 185
245 215 186
246 216 187
247 217 188
248 218 189
249 219 190
250 220 191
251 221 192
252 222 193
253 223 194
254 224 195
255 225 196
255 225 196
255 225 196
255 225 196
255 225 196
255 225 197
255 225 197
255 225 197
255 225 197
255 226 198
255 226 198
255 226 198
255 226 198
255 226 198
255 226 199
255 226 199
255 226 199
255 226 199
255 226 199
255 227 200
255 227 200
255 227 200
255 227 200
255 227 200
255 227 201
255 227 201
255 227 201
255 227 201
255 227 201
255 228 202
255 228 202
255 228 202
255 228 202
255 228 202
255 228 202
255 228 203
255 228 203
255 228 203
255 228 203
255 228 203
255 229 204
255 229 204
255 229 204
255 229 204
255 229 204
255 229 204
255 229 205
255 229 205
255 229 205
255 229 205
255 229 205
255 230 205
255 230 205
255 229 205
255 231 209
255 233 214
255 233 217
255 242 233
255 255 255


View File

@ -25,19 +25,33 @@
#define DATA_FILE2 "image24pixel.txt"
#define DATA_FILE3 "image24plane.txt"
#define DATA_FILE4 "usa.wri"
#define PAL2_FILE "sepia.pal"
#define PAL3_FILE "earth.pal"
#define IMAGE1_NAME "image8bit"
#define IMAGE2_NAME "image24bitpixel"
#define IMAGE3_NAME "image24bitplane"
#define PAL_NAME "rainbow pallete"
#define PAL1_NAME "rainbow"
#define PAL2_NAME "sepia"
#define PAL3_NAME "earth"
#define WIDTH (hsize_t)50
#define HEIGHT (hsize_t)20
/* struct to store RGB values read from a .pal file */
typedef struct rgb_t {
unsigned char r;
unsigned char g;
unsigned char b;
} rgb_t;
/* prototypes */
static int test_simple(void);
static int test_data(void);
static int test_generate(void);
static int read_data( const char* file_name, hsize_t *width, hsize_t *height );
static int read_data(const char* file_name, hsize_t *width, hsize_t *height );
static int read_palette(const char* file_name, rgb_t *palette, int palette_size);
/* globals */
unsigned char *image_data = NULL;
@ -298,12 +312,13 @@ out:
static int test_data(void)
{
hid_t fid;
hsize_t pal_dims[2];
char *srcdir = getenv("srcdir"); /* the source directory */
char data_file[512]=""; /* buffer to hold name of existing data file */
hsize_t width;
hsize_t height;
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)
@ -318,32 +333,87 @@ static int test_data(void)
TESTING2("make indexed image");
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, "/");
}
/* read first data file */
strcat(data_file,DATA_FILE1);
if ((read_data(data_file,&width,&height))<0)
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)
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_rgb 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,PAL_NAME,pal_dims,pal_rgb))<0)
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,PAL_NAME))<0)
if (H5IMlink_palette(fid,IMAGE1_NAME,PAL1_NAME)<0)
goto out;
/*-------------------------------------------------------------------------
* palette #1. 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))<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 #1. 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))<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();
@ -357,15 +427,7 @@ static int test_data(void)
TESTING2("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);
if ((read_data(data_file,&width,&height))<0)
if ((read_data(DATA_FILE2,&width,&height))<0)
goto out;
/* make image */
@ -382,15 +444,7 @@ static int test_data(void)
TESTING2("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);
if ((read_data(data_file,&width,&height))<0)
if ((read_data(DATA_FILE3,&width,&height))<0)
goto out;
/* make image */
@ -451,11 +505,9 @@ static int test_generate(void)
printf("Testing read and process data and make indexed images\n");
/*-------------------------------------------------------------------------
* read data; the file data format is described below
* compose the name of the file to open, using the srcdir, if appropriate
*-------------------------------------------------------------------------
*/
/* compose the name of the file to open, using the srcdir, if appropriate */
if ( srcdir )
{
strcpy(data_file, srcdir);
@ -463,12 +515,15 @@ static int test_generate(void)
}
strcat(data_file,DATA_FILE4);
/* Read data file */
/*-------------------------------------------------------------------------
* read data; the file data format is described below
*-------------------------------------------------------------------------
*/
f = fopen( data_file, "r" ) ;
if ( f == NULL )
{
printf( "Could not find file %s. Try set $srcdir \n", DATA_FILE4 );
printf( "Could not find file %s. Try set $srcdir \n", data_file );
H5Fclose(fid);
return -1;
}
@ -593,15 +648,15 @@ static int test_generate(void)
TESTING2("attaching palettes");
/* make a palette */
if ((H5IMmake_palette(fid,PAL_NAME,pal_dims,pal_rgb))<0)
if ((H5IMmake_palette(fid,PAL1_NAME,pal_dims,pal_rgb))<0)
goto out;
/* Attach the palette to the image datasets */
if ((H5IMlink_palette(fid,"All data",PAL_NAME))<0)
if ((H5IMlink_palette(fid,"All data",PAL1_NAME))<0)
goto out;
if ((H5IMlink_palette(fid,"Land data",PAL_NAME))<0)
if ((H5IMlink_palette(fid,"Land data",PAL1_NAME))<0)
goto out;
if ((H5IMlink_palette(fid,"Sea data",PAL_NAME))<0)
if ((H5IMlink_palette(fid,"Sea data",PAL1_NAME))<0)
goto out;
PASSED();
@ -643,7 +698,7 @@ out:
*-------------------------------------------------------------------------
*/
static int read_data( const char* file_name, /*IN*/
static int read_data( const char* fname, /*IN*/
hsize_t *width, /*OUT*/
hsize_t *height /*OUT*/ )
{
@ -652,12 +707,30 @@ static int read_data( const char* file_name, /*IN*/
char str[20];
FILE *f;
int w, h;
char *srcdir = getenv("srcdir"); /* the source directory */
char data_file[512]=""; /* buffer to hold name of existing data file */
f = fopen( file_name, "r");
/*-------------------------------------------------------------------------
* 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);
/*-------------------------------------------------------------------------
* read
*-------------------------------------------------------------------------
*/
f = fopen(data_file, "r");
if ( f == NULL )
{
printf( "Could not open file %s. Try set $srcdir \n", file_name );
printf( "Could not open file %s. Try set $srcdir \n", data_file );
return -1;
}
@ -689,3 +762,147 @@ static int read_data( const char* file_name, /*IN*/
return 1;
}
/*-------------------------------------------------------------------------
* read_palette
* Read an ASCII palette file .PAL into an array
* the files have a header of the type
*
* Parameters: filename - name of file to read.
* palette - array of RwPalleteEntry to store the
* read palette.
* palette_size - number of elements in 'palette' array
*
* Return Value: Returns number of entries read or 0 on error
* palette contains palette.
*
*-------------------------------------------------------------------------
*/
#define STRING_JASC "JASC-PAL"
#define VERSION_JASC "0100"
#define STRING_CWPAL "CWPAL"
#define VERSION_CWPAL "100"
static int read_palette(const char* fname, rgb_t *palette, int palette_size)
{
FILE *file;
char buffer[80];
int i;
unsigned int red;
unsigned int green;
unsigned int blue;
int numEntries;
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);
/* ensure the given palette is valid */
if (!palette)
return -1;
/* open the input file */
if (!(file = fopen(data_file, "r")))
{
printf( "Could not open file %s. Try set $srcdir \n", data_file );
return -1;
}
/* read the file ident string */
if (fgets(buffer, sizeof(buffer), file) == NULL)
{
fclose(file);
return -1;
}
/* ensure it matches the palette file ident string */
if ( strncmp(buffer, STRING_JASC, sizeof(STRING_JASC) - 1) != 0 &&
strncmp(buffer, STRING_CWPAL, sizeof(STRING_CWPAL) - 1) != 0 )
{
fclose(file);
return -1;
}
/* read the version string */
if (fgets(buffer, sizeof(buffer), file) == NULL)
{
fclose(file);
return -1;
}
/* ensure it matches the palette file version string */
if ( strncmp(buffer, VERSION_JASC, sizeof(VERSION_JASC) - 1) != 0 &&
strncmp(buffer, VERSION_CWPAL, sizeof(VERSION_CWPAL) - 1) != 0 )
{
fclose(file);
return -1;
}
/* read the number of colors */
if (fgets(buffer, sizeof(buffer), file) == NULL)
{
fclose(file);
return -1;
}
/* extract the number of colors.
check for missing version or number of colors
in this case it reads the first entry
*/
if ( strlen( buffer ) > 4 )
{
fclose(file);
return -1;
}
if (sscanf(buffer, "%d", &numEntries) != 1)
{
fclose(file);
return -1;
}
/* ensure there are a sensible number of colors in the palette */
if ((numEntries < 0) || (numEntries > 256) || (numEntries > palette_size))
{
fclose(file);
return(0);
}
/* read the palette entries */
for (i = 0; i < numEntries; i++)
{
/* Extract the red, green and blue color components. */
if (fscanf(file, "%u %u %u", &red, &green, &blue) != 3)
{
fclose(file);
return -1;
}
/* sore this palette entry */
palette[i].r = (unsigned char)red;
palette[i].g = (unsigned char)green;
palette[i].b = (unsigned char)blue;
}
/* close file */
fclose(file);
return numEntries;
}