summaryrefslogtreecommitdiffstats
path: root/hl/test/test_image.c
blob: 8b446b8dac5283d5676b4b30432251042058203f (plain)
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691

/****************************************************************************
 * NCSA HDF                                                                 *
 * Scientific Data Technologies                                             *
 * National Center for Supercomputing Applications                          *
 * University of Illinois at Urbana-Champaign                               *
 * 605 E. Springfield, Champaign IL 61820                                   *
 *                                                                          *
 * For conditions of distribution and use, see the accompanying             *
 * hdf/COPYING f.                                                        *
 *                                                                          *
 ****************************************************************************/



#include "H5IM.h"
#include "pal_rgb.h"
#include <stdlib.h>
#include <string.h>

#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"
#define IMAGE1_NAME "image8bit"
#define IMAGE2_NAME "image24bitpixel"
#define IMAGE3_NAME "image24bitplane"
#define PAL_NAME    "rainbow pallete"

#define WIDTH  (hsize_t)50
#define HEIGHT (hsize_t)20

/* 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 );

/* globals */
unsigned char *image_data = NULL;            


/*-------------------------------------------------------------------------
 * the main program
 *-------------------------------------------------------------------------
 */

int main(void)
{
 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;
 
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;
 hsize_t       pal_dims[] = {9,3};
 hsize_t       pal_dims_out[2];
 char          interlace[20];
 hssize_t      npals;
 hsize_t       i;
 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 ];
 unsigned char pal_data_out[9*3];
 /* create a 9 entry grey palette */
 unsigned char pal_data_in[9*3] = {0,0,0,
 25,25,25,
 50,50,50,
 75,75,75,
 100,100,100,
 125,125,125,
 150,150,150,
 175,175,175,
 200,200,200};
    
 for (i = 0; i < WIDTH*HEIGHT; i++ )
  image_in1[i] = (unsigned char)i;
 for (i = 0; i < WIDTH*HEIGHT*3; i++)
  image_in2[i] = (unsigned char)i;
 
 /* 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");

/*-------------------------------------------------------------------------
 * indexed image test
 *-------------------------------------------------------------------------
 */

 TESTING2("indexed image");

 /* write image */
 if (H5IMmake_image_8bit(fid,"image1",WIDTH,HEIGHT,image_in1)<0)
  goto out;

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

 /* attach the palette to the image dataset */
 if (H5IMlink_palette(fid,"image1","palette")<0)
  goto out;
 
 /* get info */
 if (H5IMget_image_info(fid,"image1",&width,&height,&planes,interlace,&npals)<0)
  goto out;

 if (width!=WIDTH)
  goto out;
 if (height!=HEIGHT)
  goto out;
 if (planes!=1)
  goto out;
 if (npals!=1)
  goto out;

 /* read image */
 if (H5IMread_image(fid,"image1",image_out1)<0)
  goto out;
 
 /* check */
 for (i = 0; i < height*width*planes; i++) 
 {
  if ( image_in1[i] != image_out1[i] ) 
  {
   goto out;
  }
 }

 PASSED();

/*-------------------------------------------------------------------------
 * true color image test
 *-------------------------------------------------------------------------
 */

 TESTING2("true color image");
 
 /* write image */
 if (H5IMmake_image_24bit(fid,"image2",WIDTH,HEIGHT,"INTERLACE_PIXEL",image_in2))
  goto out;

 /* 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)
  goto out;

 /* read image */
 if (H5IMread_image(fid,"image2",image_out2)<0)
  goto out;

 /* check */
 for (i = 0; i < height*width*planes; i++) 
 {
  if ( image_in2[i] != image_out2[i] ) 
  {
    goto out;
  }
 }

 PASSED();

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

 TESTING2("palette functions");

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

 if (npals!=1)
  goto out;

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

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

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

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

 if (H5IMget_palette(fid,"image1",0,pal_data_out)<0)
  goto out;

 /* check */
 for (i = 0; i < 9*3; i++) 
 {
  if ( pal_data_in[i] != pal_data_out[i] ) 
  {
   goto out;
  }
 }

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

 if ((is_image=H5IMis_image(fid,"image1"))<0)
  goto out;

 if (is_image!=1)
  goto out;

 if ((is_image=H5IMis_image(fid,"image2"))<0)
  goto out;

 if (is_image!=1)
  goto out;

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

 if ((is_pal=H5IMis_palette(fid,"palette"))<0)
  goto out;

 if (is_pal!=1)
  goto out;

 PASSED();

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

 return 0;
  
 /* 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)
{
 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;

 /* 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");

 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)
  goto out;

 /* make an image */
 if ((H5IMmake_image_8bit(fid,IMAGE1_NAME,width,height,image_data))<0)
  goto out;
    
 /* initialize the palette data; pal_rgb data is contained in "pal_rgb.h" */
 pal_dims[0] = 256;
 pal_dims[1] = 3;
 
 /* make a palette */
 if ((H5IMmake_palette(fid,PAL_NAME,pal_dims,pal_rgb))<0)
  goto out;

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

 PASSED();


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

 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)
  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 */  
 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)
  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;

 return 0;
  
 /* error zone, gracefully close */
out:
 H5E_BEGIN_TRY {
  H5Fclose(fid);
 } H5E_END_TRY;
 H5_FAILED();
 return FAIL;
}


 /*
 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 
 image from the sea values.
   For the example we used data from MODB, the Mediterranean Oceanic Data Base
   http://modb.oce.ulg.ac.be/
   
*/

static int test_generate(void)
{
 hid_t    fid;
 hsize_t  pal_dims[2] = { 256, 3 };
 float    *data; 
 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;

 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 */
 if ( srcdir )
 {
  strcpy(data_file, srcdir);
  strcat(data_file, "/");
 }
 strcat(data_file,DATA_FILE4);
 
 /* Read  data file */   
 f  = fopen( data_file, "r" ) ;
 
 if ( f == NULL )
 {
  printf( "Could not find file %s. Try set $srcdir \n", DATA_FILE4 );
  H5Fclose(fid);
  return -1;
 }

/*
!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 : 
!
!   DO K = KMAX to 1
!       DO J = JMAX to 1
!           DO I = 1 to IMAX
!              read
!           OD
!       OD
!   OD
! 
!              ____________________________
!             /                           /| (imax,jmax,kmax)
!            /        sea surface        / |
!           /                           /  |
!          /__________________________ /   |
!          |                          |    |
!          |                          |    | (imax,jmax,1)        n
!          |                          |   /                      /
!          |                          |  /                      /
!     ^  j |                          | /             w  <-----o-----> e
!  k  |  / |__________________________|/                      /
!     | /                           (imax,1,1)               /
!     |---------->                                          s
!     i
!
*/
 
 
 fscanf( f, "%d %d %d", &imax, &jmax, &kmax ); 
 fscanf( f, "%f %f %f", &valex, &xmin, &xmax ); 
 
 data = (float*) malloc ( imax * jmax * kmax * sizeof( float ));
 image_data = (unsigned char*) malloc ( imax * jmax * kmax * sizeof( unsigned char ));
 
 for ( i = 0; i < imax * jmax * kmax; i++ ) 
 {
  fscanf( f, "%f ", &value ); 
  data[i] = value;
 }
 fclose( f );
  
/*-------------------------------------------------------------------------
 * transform the data from floating point to unsigend char
 * we are processing all the data here
 *-------------------------------------------------------------------------
 */

 TESTING2("make indexed image from all the data");
 
 for ( i = 0; i < imax * jmax * kmax; i++ ) 
 {
  image_data[i] = (unsigned char)(( 255 * (data[i] - xmin ) ) / (xmax - xmin ));
 }
 
 /* Make the image */
 if ((H5IMmake_image_8bit(fid,"All data",(hsize_t)imax,(hsize_t)jmax,image_data))<0)
  goto out;

 PASSED();
 
/*-------------------------------------------------------------------------
 * transform the data from floating point to unsigend char
 * here we just process the land data
 *-------------------------------------------------------------------------
 */

 TESTING2("make indexed image from land data");
 
 for ( i = 0; i < imax * jmax * kmax; i++ ) 
 {
  if ( data[i] < 0 )
   image_data[i] = 0;
  else
   image_data[i] = (unsigned char)(( 255 * (data[i] ) ) / xmax );
 }
 
 /* make the image */
 if ((H5IMmake_image_8bit(fid,"Land data",(hsize_t)imax,(hsize_t)jmax,image_data))<0)
  goto out;

 PASSED();
 
/*-------------------------------------------------------------------------
 * transform the data from floating point to unsigend char
 * here we just process the sea data
 *-------------------------------------------------------------------------
 */

 TESTING2("make indexed image from sea data");
 
 for ( i = 0; i < imax * jmax * kmax; i++ ) 
 {
  if ( data[i] > 0 )
   image_data[i] = 0;
  else
   image_data[i] = (unsigned char)(( 255 * (data[i] - xmin ) ) / xmin );
 }
 
 /* make the image */
 if ((H5IMmake_image_8bit(fid,"Sea data",(hsize_t)imax,(hsize_t)jmax,image_data))<0)
  goto out;

 PASSED();
 
/*-------------------------------------------------------------------------
 * make a palette and attach it to the datasets
 *-------------------------------------------------------------------------
 */

 TESTING2("attaching palettes");
 
 /* make a palette */
 if ((H5IMmake_palette(fid,PAL_NAME,pal_dims,pal_rgb))<0)
  goto out;
 
 /* Attach the palette to the image datasets */
 if ((H5IMlink_palette(fid,"All data",PAL_NAME))<0)
  goto out;
 if ((H5IMlink_palette(fid,"Land data",PAL_NAME))<0)
  goto out;
 if ((H5IMlink_palette(fid,"Sea data",PAL_NAME))<0)
  goto out;

 PASSED();
 

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

 return 0;
  
 /* error zone, gracefully close */
out:
 H5E_BEGIN_TRY {
  H5Fclose(fid);
 } H5E_END_TRY;
 H5_FAILED();
 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
 * 
 * followed by the image data
 *
 *-------------------------------------------------------------------------
 */

static int read_data( const char* file_name, /*IN*/
                      hsize_t *width, /*OUT*/
                      hsize_t *height /*OUT*/ )
{
 int    i, n;
 int    color_planes;
 char   str[20];
 FILE   *f;
 int    w, h;

 f = fopen( file_name, "r");

 if ( f == NULL )
 {
  printf( "Could not open file %s. Try set $srcdir \n", file_name );
  return -1;
 }

 fscanf( f, "%s", str );
 fscanf( f, "%d", &color_planes );
 fscanf( f, "%s", str );
 fscanf( f, "%d", &h); 
 fscanf( f, "%s", str );
 fscanf( f, "%d", &w); 
 
 *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);

 return 1;

}