1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Copyright by The HDF Group. *
3 * Copyright by the Board of Trustees of the University of Illinois. *
4 * All rights reserved. *
5 * *
6 * This file is part of HDF5. The full HDF5 copyright notice, including *
7 * terms governing use, modification, and redistribution, is contained in *
8 * the COPYING file, which can be found at the root of the source code *
9 * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases. *
10 * If you do not have access to either file, you may request a copy from *
11 * help@hdfgroup.org. *
12 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13
14 #include "hdf5.h"
15 #include "hdf5_hl.h"
16 #include <stdlib.h>
17 #include <string.h>
18
19 #define DATA_FILE1 "image8.txt"
20 #define DATA_FILE2 "image24pixel.txt"
21 #define IMAGE1_NAME "image8bit"
22 #define IMAGE2_NAME "image24bitpixel"
23 #define PAL_NAME "palette"
24 #define PAL_ENTRIES 256
25
26 static int read_data(const char* file_name, hsize_t *width, hsize_t *height );
27 unsigned char *gbuf = NULL; /* global buffer for image data */
28
main(void)29 int main( void )
30 {
31 hid_t file_id; /* HDF5 file identifier */
32 hsize_t width; /* width of image */
33 hsize_t height; /* height of image */
34 unsigned char pal[ PAL_ENTRIES * 3 ]; /* palette array */
35 hsize_t pal_dims[2] = {PAL_ENTRIES,3}; /* palette dimensions */
36 herr_t i, n;
37
38 /* create a new HDF5 file using default properties. */
39 file_id = H5Fcreate( "ex_image2.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT );
40
41 /* read first data file */
42 if (read_data(DATA_FILE1,&width,&height)<0)
43 goto out;
44
45 /* make the image */
46 H5IMmake_image_8bit( file_id, IMAGE1_NAME, width, height, gbuf );
47 if (gbuf) {
48 free(gbuf);
49 gbuf = NULL;
50 }
51
52 /*-------------------------------------------------------------------------
53 * define a palette, blue to red tones
54 *-------------------------------------------------------------------------
55 */
56 for ( i=0, n=0; i<PAL_ENTRIES*3; i+=3, n++)
57 {
58 pal[i] =n; /* red */
59 pal[i+1]=0; /* green */
60 pal[i+2]=255-n; /* blue */
61 }
62
63 /* make a palette */
64 H5IMmake_palette( file_id, PAL_NAME, pal_dims, pal );
65
66 /* attach the palette to the image */
67 H5IMlink_palette( file_id, IMAGE1_NAME, PAL_NAME );
68
69 /*-------------------------------------------------------------------------
70 * True color image example with pixel interlace
71 *-------------------------------------------------------------------------
72 */
73
74 /* read second data file */
75 if (read_data(DATA_FILE2,&width,&height)<0)
76 goto out;
77
78 /* make dataset */
79 H5IMmake_image_24bit( file_id, IMAGE2_NAME, width, height, "INTERLACE_PIXEL", gbuf );
80
81 /* close the file. */
82 H5Fclose( file_id );
83
84 if(gbuf) {
85 free(gbuf);
86 gbuf = NULL;
87 }
88
89 return 0;
90
91 out:
92 printf("Error on return function...Exiting\n");
93
94 if(gbuf) {
95 free(gbuf);
96 gbuf = NULL;
97 }
98
99 return 1;
100 }
101
102
103 /*-------------------------------------------------------------------------
104 * read_data
105 * utility function to read ASCII image data
106 * the files have a header of the type
107 *
108 * components
109 * n
110 * height
111 * n
112 * width
113 * n
114 *
115 * followed by the image data
116 *
117 *-------------------------------------------------------------------------
118 */
119
read_data(const char * fname,hsize_t * width,hsize_t * height)120 static int read_data( const char* fname, /*IN*/
121 hsize_t *width, /*OUT*/
122 hsize_t *height /*OUT*/ )
123 {
124 int i, n;
125 int color_planes;
126 char str[20];
127 FILE *f;
128 int w, h;
129 char *srcdir = getenv("srcdir"); /* the source directory */
130 char data_file[512]=""; /* buffer to hold name of existing data file */
131
132 /*-------------------------------------------------------------------------
133 * compose the name of the file to open, using "srcdir", if appropriate
134 *-------------------------------------------------------------------------
135 */
136 strcpy(data_file, "");
137 if (srcdir)
138 {
139 strcpy(data_file, srcdir);
140 strcat(data_file, "/");
141 }
142 strcat(data_file,fname);
143
144 /*-------------------------------------------------------------------------
145 * read
146 *-------------------------------------------------------------------------
147 */
148
149 f = fopen(data_file, "r");
150 if ( f == NULL )
151 {
152 printf( "Could not open file %s. Try set $srcdir \n", data_file );
153 return -1;
154 }
155
156 fscanf( f, "%s", str );
157 fscanf( f, "%d", &color_planes );
158 fscanf( f, "%s", str );
159 fscanf( f, "%d", &h);
160 fscanf( f, "%s", str );
161 fscanf( f, "%d", &w);
162
163 *width = (hsize_t)w;
164 *height = (hsize_t)h;
165
166 if ( gbuf )
167 {
168 free( gbuf );
169 gbuf=NULL;
170 }
171
172 gbuf = (unsigned char*) malloc (w * h * color_planes * sizeof( unsigned char ));
173
174 for (i = 0; i < h * w * color_planes ; i++)
175 {
176 fscanf( f, "%d",&n );
177 gbuf[i] = (unsigned char)n;
178 }
179 fclose(f);
180
181 return 1;
182
183 }
184
185