1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* 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 *
* 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. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "H5IMcc.h"
#include <string.h>
#include <stdlib.h>
/*-------------------------------------------------------------------------
* private functions
*-------------------------------------------------------------------------
*/
herr_t H5IM_get_palette(hid_t loc_id,
const char *image_name,
int pal_number,
hid_t tid,
void *pal_data);
/*-------------------------------------------------------------------------
* Function: H5IMmake_image_8bitf
*
* Purpose: Creates and writes an image an 8 bit image
*
* Return: Success: 0, Failure: -1
*
* Programmer: Pedro Vicente Nunes, pvn@ncsa.uiuc.edu
*
* Date: May 10, 2005
*
* Comments:
* This function allows the creation and writing of an 8bit image on disk.
* The memory datatype is H5T_NATIVE_INT. It is supposed to be called from
* the FORTRAN interface where the image buffer is defined as type "integer"
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
herr_t H5IMmake_image_8bitf(hid_t loc_id,
const char *dset_name,
hsize_t width,
hsize_t height,
int_f *buf)
{
hid_t did; /* dataset ID */
hid_t sid; /* space ID */
hsize_t dims[IMAGE8_RANK]; /* dimensions */
/* initialize the image dimensions */
dims[0] = height;
dims[1] = width;
/*-------------------------------------------------------------------------
* create and write the dataset
*-------------------------------------------------------------------------
*/
/* create the data space for the dataset. */
if((sid = H5Screate_simple(IMAGE8_RANK, dims, NULL)) < 0)
return -1;
/* create the dataset as H5T_NATIVE_UCHAR */
if((did = H5Dcreate2(loc_id, dset_name, H5T_NATIVE_UINT8, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
return -1;
/* write with memory type H5T_NATIVE_INT */
/* Use long type if Fortran integer is 8 bytes and C long long is also 8 bytes*/
/* Fail if otherwise */
if(buf) {
if(sizeof(int_f) == sizeof(int)) {
if(H5Dwrite(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0)
return -1;
} else if(sizeof(int_f) == sizeof(long)) {
if(H5Dwrite(did, H5T_NATIVE_LONG, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0)
return -1;
} else if(sizeof(int_f) == sizeof(long long)) {
if(H5Dwrite(did, H5T_NATIVE_LLONG, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0)
return -1;
} else
return -1;
}
/* close */
if(H5Dclose(did) < 0)
return -1;
if(H5Sclose(sid) < 0)
return -1;
/*-------------------------------------------------------------------------
* attach the specification attributes
*-------------------------------------------------------------------------
*/
/* attach the CLASS attribute */
if(H5LTset_attribute_string(loc_id, dset_name, "CLASS", IMAGE_CLASS) < 0)
return -1;
/* attach the VERSION attribute */
if(H5LTset_attribute_string(loc_id, dset_name, "IMAGE_VERSION", IMAGE_VERSION) < 0)
return -1;
/* attach the IMAGE_SUBCLASS attribute */
if(H5LTset_attribute_string(loc_id, dset_name, "IMAGE_SUBCLASS", "IMAGE_INDEXED") < 0)
return -1;
return 0;
}
/*-------------------------------------------------------------------------
* Function: H5IMmake_image_24bitf
*
* Purpose:
*
* Return: Success: 0, Failure: -1
*
* Programmer: Pedro Vicente Nunes, pvn@ncsa.uiuc.edu
*
* Date: May 10, 2005
*
* Comments:
* This function allows the creation and writing of an 8bit image on disk.
* The memory datatype is H5T_NATIVE_INT. It is supposed to be called from
* the FORTRAN interface where the image buffer is defined as type "integer"
*
* Interlace Mode Dimensions in the Dataspace
* INTERLACE_PIXEL [height][width][pixel components]
* INTERLACE_PLANE [pixel components][height][width]
*
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
herr_t H5IMmake_image_24bitf(hid_t loc_id,
const char *dset_name,
hsize_t width,
hsize_t height,
const char *interlace,
int_f *buf)
{
hid_t did; /* dataset ID */
hid_t sid; /* space ID */
hsize_t dims[IMAGE24_RANK]; /* dimensions */
/*-------------------------------------------------------------------------
* attach the image dimensions according to the interlace mode
*-------------------------------------------------------------------------
*/
if(strcmp(interlace, "INTERLACE_PIXEL") == 0) {
/* Number of color planes is defined as the third dimension */
dims[0] = height;
dims[1] = width;
dims[2] = IMAGE24_RANK;
}
else
if(strcmp(interlace, "INTERLACE_PLANE") == 0) {
/* Number of color planes is defined as the first dimension */
dims[0] = IMAGE24_RANK;
dims[1] = height;
dims[2] = width;
}
else
return -1;
/*-------------------------------------------------------------------------
* create and write the dataset
*-------------------------------------------------------------------------
*/
/* create the data space for the dataset. */
if((sid = H5Screate_simple(IMAGE24_RANK, dims, NULL)) < 0)
return -1;
/* create the dataset as H5T_NATIVE_UCHAR */
if((did = H5Dcreate2(loc_id, dset_name, H5T_NATIVE_UCHAR, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
return -1;
/* write with memory type H5T_NATIVE_INT */
if(buf) {
if(sizeof(int_f) == sizeof(int)) {
if(H5Dwrite(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0)
return -1;
} else if(sizeof(int_f) == sizeof(long)) {
if(H5Dwrite(did, H5T_NATIVE_LONG, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0)
return -1;
} else if(sizeof(int_f) == sizeof(long long)) {
if(H5Dwrite(did, H5T_NATIVE_LLONG, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0)
return -1;
} else
return -1;
}
/* close */
if(H5Dclose(did) < 0)
return -1;
if(H5Sclose(sid) < 0)
return -1;
/*-------------------------------------------------------------------------
* attach the specification attributes
*-------------------------------------------------------------------------
*/
/* Attach the CLASS attribute */
if(H5LTset_attribute_string(loc_id, dset_name, "CLASS", IMAGE_CLASS) < 0)
return -1;
/* Attach the VERSION attribute */
if(H5LTset_attribute_string(loc_id, dset_name, "IMAGE_VERSION", IMAGE_VERSION) < 0)
return -1;
/* Attach the IMAGE_SUBCLASS attribute */
if(H5LTset_attribute_string(loc_id, dset_name, "IMAGE_SUBCLASS", "IMAGE_TRUECOLOR") < 0)
return -1;
/* Attach the INTERLACE_MODE attribute. This attributes is only for true color images */
if(H5LTset_attribute_string(loc_id, dset_name, "INTERLACE_MODE", interlace) < 0)
return -1;
return 0;
}
/*-------------------------------------------------------------------------
* Function: H5IMread_imagef
*
* Purpose: Reads image data from disk.
*
* Return: Success: 0, Failure: -1
*
* Programmer: Pedro Vicente Nunes, pvn@ncsa.uiuc.edu
*
* Date: May 10, 2005
*
* Comments:
* This function allows reading of an 8bit image on disk.
* The memory datatype is H5T_NATIVE_INT. It is supposed to be called from
* the FORTRAN interface where the image buffer is defined as type "integer"
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
herr_t H5IMread_imagef(hid_t loc_id,
const char *dset_name,
int_f *buf)
{
hid_t did;
hid_t tid;
/* open the dataset */
if((did = H5Dopen2(loc_id, dset_name, H5P_DEFAULT)) < 0)
return -1;
/* determine appropriate datatype to use */
if(sizeof(int_f) == sizeof(int))
tid = H5T_NATIVE_INT;
else if(sizeof(int_f) == sizeof(long))
tid = H5T_NATIVE_LONG;
else if(sizeof(int_f) == sizeof(long long))
tid = H5T_NATIVE_LLONG;
else
goto out;
/* read to memory */
if(H5Dread(did, tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0)
goto out;
/* close */
if(H5Dclose(did))
return -1;
return 0;
out:
H5Dclose(did);
return -1;
}
/*-------------------------------------------------------------------------
* Function: H5IMmake_palettef
*
* Purpose: Creates and writes a palette.
*
* Return: Success: 0, Failure: -1
*
* Programmer: Pedro Vicente Nunes, pvn@ncsa.uiuc.edu
*
* Date: May 10, 2005
*
* Comments:
* This function allows writing of an 8bit palette to disk.
* The memory datatype is H5T_NATIVE_INT. It is supposed to be called from
* the FORTRAN interface where the image buffer is defined as type "integer"
*
* based on HDF5 Image and Palette Specification
* http://hdf.ncsa.uiuc.edu/HDF5/H5Image/ImageSpec.html
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
herr_t H5IMmake_palettef(hid_t loc_id,
const char *pal_name,
const hsize_t *pal_dims,
int_f *pal_data)
{
hid_t did; /* dataset ID */
hid_t sid; /* space ID */
int has_pal;
/* Check if the dataset already exists */
has_pal = H5LTfind_dataset(loc_id, pal_name);
/* It exists. Return */
if(has_pal == 1)
return 0;
/*-------------------------------------------------------------------------
* create and write the dataset
*-------------------------------------------------------------------------
*/
/* create the data space for the dataset. */
if((sid = H5Screate_simple(2, pal_dims, NULL)) < 0)
return -1;
/* create the dataset as H5T_NATIVE_UCHAR */
if((did = H5Dcreate2(loc_id, pal_name, H5T_NATIVE_UCHAR, sid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
return -1;
/* write with memory type H5T_NATIVE_INT */
if(pal_data) {
if(sizeof(int_f) == sizeof(int)) {
if(H5Dwrite(did, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, pal_data) < 0)
return -1;
} else if(sizeof(int_f) == sizeof(long)) {
if(H5Dwrite(did, H5T_NATIVE_LONG, H5S_ALL, H5S_ALL, H5P_DEFAULT, pal_data) < 0)
return -1;
} else if(sizeof(int_f) == sizeof(long long)) {
if(H5Dwrite(did, H5T_NATIVE_LLONG, H5S_ALL, H5S_ALL, H5P_DEFAULT, pal_data) < 0)
return -1;
} else
return -1;
}
/* close */
if(H5Dclose(did) < 0)
return -1;
if(H5Sclose(sid) < 0)
return -1;
/*-------------------------------------------------------------------------
* attach the specification attributes
*-------------------------------------------------------------------------
*/
/* Attach the attribute "CLASS" to the >>palette<< dataset*/
if(H5LTset_attribute_string(loc_id, pal_name, "CLASS", PALETTE_CLASS) < 0)
return -1;
/* Attach the attribute "PAL_VERSION" to the >>palette<< dataset*/
if(H5LTset_attribute_string(loc_id, pal_name, "PAL_VERSION", "1.2") < 0)
return -1;
return 0;
}
/*-------------------------------------------------------------------------
* Function: H5IMget_palettef
*
* Purpose: Read palette
*
* Return: Success: 0, Failure: -1
*
* Programmer: Pedro Vicente Nunes, pvn@ncsa.uiuc.edu
*
* Date: May 10, 2005
*
* Comments:
* This function allows reading of an 8bit palette from disk.
* The memory datatype is H5T_NATIVE_INT. It is supposed to be called from
* the FORTRAN interface where the image buffer is defined as type "integer"
*
* based on HDF5 Image and Palette Specification
* http://hdf.ncsa.uiuc.edu/HDF5/H5Image/ImageSpec.html
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
herr_t H5IMget_palettef(hid_t loc_id,
const char *image_name,
int pal_number,
int_f *pal_data)
{
if(sizeof(int_f) == sizeof(int))
return H5IM_get_palette(loc_id,image_name,pal_number,H5T_NATIVE_INT,pal_data);
else if(sizeof(int_f) == sizeof(long))
return H5IM_get_palette(loc_id,image_name,pal_number,H5T_NATIVE_LONG,pal_data);
else if(sizeof(int_f) == sizeof(long long))
return H5IM_get_palette(loc_id,image_name,pal_number,H5T_NATIVE_LLONG,pal_data);
else
return -1;
}
/*-------------------------------------------------------------------------
* Function: H5IM_get_palette
*
* Purpose: private function that reads a palette to memory type TID
*
* Return: Success: 0, Failure: -1
*
* Programmer: Pedro Vicente Nunes, pvn@ncsa.uiuc.edu
*
* Date: May 10, 2005
*
* Comments:
* This function allows reading of an 8bit palette from disk disk
* to memory type TID
* The memory datatype can be H5T_NATIVE_INT or H5T_NATIVE_UCHAR currently.
* the H5T_NATIVE_INT is supposed to be called from
* the FORTRAN interface where the image buffer is defined as type "integer"
*
* Comments:
* based on HDF5 Image and Palette Specification
* http://hdf.ncsa.uiuc.edu/HDF5/H5Image/ImageSpec.html
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
herr_t H5IM_get_palette(hid_t loc_id,
const char *image_name,
int pal_number,
hid_t tid,
void *pal_data)
{
hid_t image_id;
int has_pal;
hid_t attr_type;
hid_t attr_id;
hid_t attr_space_id;
hid_t attr_class;
hssize_t n_refs;
size_t dim_ref;
hobj_ref_t *refbuf; /* buffer to read references */
hid_t pal_id;
/* Open the dataset. */
if((image_id = H5Dopen2(loc_id, image_name, H5P_DEFAULT)) < 0)
return -1;
/* Try to find the attribute "PALETTE" on the >>image<< dataset */
has_pal = H5IM_find_palette(image_id);
if(has_pal == 1)
{
if((attr_id = H5Aopen(image_id, "PALETTE", H5P_DEFAULT)) < 0)
goto out;
if((attr_type = H5Aget_type(attr_id)) < 0)
goto out;
if((attr_class = H5Tget_class(attr_type)) < 0)
goto out;
/* Check if it is really a reference */
if(attr_class == H5T_REFERENCE)
{
/* Get the reference(s) */
if((attr_space_id = H5Aget_space(attr_id)) < 0)
goto out;
n_refs = H5Sget_simple_extent_npoints(attr_space_id);
dim_ref = (size_t)n_refs;
refbuf = (hobj_ref_t *)HDmalloc(sizeof(hobj_ref_t) * dim_ref);
if(H5Aread(attr_id, attr_type, refbuf) < 0)
goto out;
/* Get the palette id */
if((pal_id = H5Rdereference(image_id, H5R_OBJECT, &refbuf[pal_number])) < 0)
goto out;
/* Read the palette dataset using the memory type TID */
if(H5Dread(pal_id, tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, pal_data) < 0)
goto out;
if(H5Sclose(attr_space_id) < 0)
goto out;
/* close the dereferenced dataset */
if(H5Dclose(pal_id) < 0)
goto out;
free(refbuf);
} /* H5T_REFERENCE */
if(H5Tclose(attr_type) < 0)
goto out;
/* Close the attribute. */
if(H5Aclose(attr_id) < 0)
goto out;
}
/* Close the image dataset. */
if(H5Dclose(image_id) < 0)
return -1;
return 0;
out:
H5Dclose(image_id);
return -1;
}
|