int main(int argc , char **argv) { BYTE *Image; /* compression structs */ CHAR *HDFName = NULL; CHAR *GIFName = NULL; BYTE* b; BYTE GlobalPalette[256][3]; BYTE Red[256]; BYTE Green[256]; BYTE Blue[256]; int RWidth, RHeight; int ColorMapSize, InitCodeSize, Background, BitsPerPixel; int j,nc; int i; int numcols; BYTE pc2nc[256] , r1[256] , g1[256] , b1[256]; int arg_index = 2; int bool_is_image = 0; /* 0 = false , 1 = true */ char *image_name = NULL; int idx; if ( argv[1] && (strcmp("-V",argv[1])==0) ) { print_version("gif2h5"); exit(EXIT_SUCCESS); } if (argc < 4) { /* they didn't supply at least one image -- bail */ usage(); return 1; } HDFName = argv[1]; GIFName = argv[2]; /* get the options */ while (arg_index++ < argc - 1) { if (!strcmp(argv[arg_index] , "-i")) { bool_is_image = 1; continue; } if (bool_is_image) { /* allocate space to store the image name */ size_t len = strlen(argv[arg_index]); image_name = (CHAR*) malloc( len + 1); strcpy(image_name , argv[arg_index]); bool_is_image = 0; continue; } /* oops. This was not meant to happen */ usage(); goto out; } /* Do Endian Order testing and set Endian Order */ idx = 0x0001; b = (BYTE *) &idx; EndianOrder = (b[0] ? 1:0); if (!(fpGif = fopen(GIFName , "wb"))) { printf("Error opening gif file for output. Aborting.\n"); goto out; } Background = 0; { hsize_t width, height, planes; hid_t fid; char interlace[20]; hssize_t npals; hsize_t pal_dims[2]; unsigned char *pal; if ((fid = H5Fopen(HDFName , H5F_ACC_RDONLY , H5P_DEFAULT)) < 0) { fprintf(stderr , "Unable to open HDF file for input. Aborting.\n"); goto out; } /* read image */ if ( H5IMget_image_info( fid, image_name, &width, &height, &planes, interlace, &npals ) < 0 ) goto out; Image = (BYTE*) malloc( (size_t) width * (size_t) height ); if ( H5IMread_image( fid, image_name, Image ) < 0 ) goto out; if (npals) { if ( H5IMget_palette_info( fid, image_name, 0, pal_dims ) < 0 ) goto out; pal = (BYTE*) malloc( (size_t) pal_dims[0] * (size_t) pal_dims[1] ); if ( H5IMget_palette( fid, image_name, 0, pal ) < 0 ) goto out; numcols = (int) pal_dims[0]; for (i = 0, j = 0 ; i < numcols ; j+=3, i++) { GlobalPalette[i][0] = pal[j]; GlobalPalette[i][1] = pal[j+1]; GlobalPalette[i][2] = pal[j+2]; } free(pal); } H5Fclose(fid); RWidth = (int)width; RHeight = (int)height; /* * If the first image does not have a palette, I make my own global * color table Obviously this is not the best thing to do, better * steps would be: * * 1. Check for either a global palette or a global attribute called * palette * 2. Check for palettes in any of the other images. */ if (!npals) { numcols = 256; for (i = 0 ; i < numcols ; i++) { Red[i] = 255 - i; Green[i] = 255 - i; Blue[i] = 255 - i; } } else { for (i = 0 ; i < numcols ; i++) { Red[i] = GlobalPalette[i][0]; Green[i] = GlobalPalette[i][1]; Blue[i] = GlobalPalette[i][2]; } } for (i = 0; i < numcols; i++) { pc2nc[i] = r1[i] = g1[i] = b1[i] = 0; } /* compute number of unique colors */ nc = 0; for (i = 0; i < numcols; i++) { /* see if color #i is already used */ for (j = 0; j < i; j++) { if (Red[i] == Red[j] && Green[i] == Green[j] && Blue[i] == Blue[j]) break; } if (j==i) { /* wasn't found */ pc2nc[i] = nc; r1[nc] = Red[i]; g1[nc] = Green[i]; b1[nc] = Blue[i]; nc++; } else { pc2nc[i] = pc2nc[j]; } } /* figure out 'BitsPerPixel' */ for (i = 1; i < 8; i++) { if ((1<<i) >= nc) break; } BitsPerPixel = i; ColorMapSize = 1 << BitsPerPixel; if (BitsPerPixel <= 1) InitCodeSize = 2; else InitCodeSize = BitsPerPixel; if (!fpGif) { fprintf(stderr, "WriteGIF: file not open for writing\n" ); goto out; } fwrite("GIF87a", sizeof( char ), 6, fpGif); /* the GIF magic number */ putword(RWidth, fpGif); /* screen descriptor */ putword(RHeight, fpGif); i = 0x00; /* No, there is no color map */ i |= (8-1)<<4; /* OR in the color resolution (hardwired 8) */ i |= (BitsPerPixel - 1); /* OR in the # of bits per pixel */ fputc(i,fpGif); fputc(Background,fpGif); /* background color */ fputc(0, fpGif); /* future expansion byte */ /* * Put Image Descriptor * Hardwiring Left Offset and Top Offset to 0x00 */ fputc(0x2c , fpGif); putword(0x00 , fpGif); putword(0x00 , fpGif); putword(RWidth , fpGif); putword(RHeight , fpGif); /* since we always have a local color palette ... */ fputc((0x80 | (BitsPerPixel - 1)) , fpGif); for (i = 0; i < ColorMapSize; i++) { /* write out Global colormap */ fputc(r1[i], fpGif); fputc(g1[i], fpGif); fputc(b1[i], fpGif); } fputc(InitCodeSize , fpGif); i = hdfWriteGIF(fpGif , Image , 0 , RHeight , RWidth , r1, g1 , b1 , pc2nc , 256 , 8 , BitsPerPixel); fputc(0x00, fpGif); free(Image); } if (fputc(';',fpGif) == EOF) { /* Write GIF file terminator */ fprintf(stderr , "Error!"); goto out; } if (fpGif != NULL) fclose(fpGif); if (image_name != NULL) free(image_name); return 0; out: if (fpGif != NULL) fclose(fpGif); if (image_name != NULL) free(image_name); return 1; }
static int test_simple(void) { hsize_t width = WIDTH; hsize_t height = HEIGHT; hsize_t planes; hid_t fid; int i, j, n, space; hsize_t u; char interlace[20]; hssize_t npals; /* 8-bit image */ unsigned char *buf1 = NULL; unsigned char pal[ PAL_ENTRIES * 3 ]; /* palette array */ hsize_t pal_dims[2] = {PAL_ENTRIES,3}; /* palette dimensions */ /* 24-bit image */ unsigned char *buf2 = NULL; /* read data */ unsigned char *buf1_out = NULL; unsigned char *buf2_out = NULL; unsigned char pal_out[ PAL_ENTRIES * 3 ]; /* palette array */ hsize_t pal_dims_out[2]; /* palette dimensions */ /* Allocate image buffers */ buf1 = (unsigned char *)HDmalloc(WIDTH * HEIGHT); HDassert(buf1); buf2 = (unsigned char *)HDmalloc(WIDTH * HEIGHT * 3); HDassert(buf2); buf1_out = (unsigned char *)HDmalloc(WIDTH * HEIGHT); HDassert(buf1_out); buf2_out = (unsigned char *)HDmalloc(WIDTH * HEIGHT * 3); HDassert(buf2_out); /* create an image */ space = WIDTH*HEIGHT / PAL_ENTRIES; for (i=0, j=0, n=0; i < WIDTH*HEIGHT; i++, j++ ) { buf1[i] = (unsigned char)n; if ( j > space ) { n++; j=0; } } /* create an image */ space = WIDTH*HEIGHT / 256; for (i=0, j=0, n=0; i < WIDTH*HEIGHT*3; i+=3, j++ ) { buf2[i] = (unsigned char)n; buf2[i+1] = 0; buf2[i+2] = (unsigned char)(255 - n); if ( j > space ) { n++; j=0; } } /*------------------------------------------------------------------------- * define a palette, blue to red tones *------------------------------------------------------------------------- */ for ( i=0, n=0; i<PAL_ENTRIES*3; i+=3, n++) { pal[i] =(unsigned char)n; /* red */ pal[i+1]=0; /* green */ pal[i+2]=(unsigned char)(255 - n); /* blue */ } /* Create a new HDF5 file using default properties. */ fid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT ); /*------------------------------------------------------------------------- * Indexed image test *------------------------------------------------------------------------- */ TESTING("indexed image"); /* Write image */ if ( H5IMmake_image_8bit( fid, IMAGE1_NAME, width, height, buf1 ) < 0 ) goto out; /* Make a palette */ if ( H5IMmake_palette( fid, PAL_NAME, pal_dims, pal ) < 0 ) goto out; /* Attach a palette to the image dataset */ if ( H5IMlink_palette( fid, IMAGE1_NAME, PAL_NAME ) < 0 ) goto out; /* Read image */ if ( H5IMget_image_info( fid, IMAGE1_NAME, &width, &height, &planes, interlace, &npals ) < 0 ) goto out; if ( H5IMread_image( fid, IMAGE1_NAME, buf1_out ) < 0 ) goto out; for (u = 0; u < height*width*planes; u++) { if ( buf1[u] != buf1_out[u] ) goto out; } PASSED(); /*------------------------------------------------------------------------- * True color image test *------------------------------------------------------------------------- */ TESTING("true color image"); /* Write image */ if ( H5IMmake_image_24bit( fid, IMAGE2_NAME, width, height, "INTERLACE_PIXEL", buf2 ) ) goto out; /* Read image */ if ( H5IMget_image_info( fid, IMAGE2_NAME, &width, &height, &planes, interlace, &npals ) < 0 ) goto out; if ( H5IMread_image( fid, IMAGE2_NAME, buf2_out ) < 0 ) goto out; for (u = 0; u < height*width*planes; u++) { if ( buf2[u] != buf2_out[u] ) goto out; } PASSED(); /*------------------------------------------------------------------------- * H5IMget_npalettes test *------------------------------------------------------------------------- */ TESTING("pallete functions"); if ( H5IMget_npalettes( fid, IMAGE1_NAME, &npals ) < 0 ) goto out; /*------------------------------------------------------------------------- * H5IMget_palette_info test *------------------------------------------------------------------------- */ if ( H5IMget_palette_info( fid, IMAGE1_NAME, 0, pal_dims_out ) < 0 ) goto out; for (i = 0; i < 2; i++) { if ( pal_dims[i] != pal_dims_out[i] ) goto out; } /*------------------------------------------------------------------------- * H5IMget_palette test *------------------------------------------------------------------------- */ if ( H5IMget_palette( fid, IMAGE1_NAME, 0, pal_out ) < 0 ) goto out; for (i = 0; i < PAL_ENTRIES * 3; i++) { if ( pal[i] != pal_out[i] ) goto out; } /*------------------------------------------------------------------------- * H5IMis_image test *------------------------------------------------------------------------- */ if ( H5IMis_image( fid, IMAGE1_NAME ) < 0 ) goto out; if ( H5IMis_image( fid, IMAGE2_NAME ) < 0 ) goto out; /*------------------------------------------------------------------------- * H5IMis_palette test *------------------------------------------------------------------------- */ if ( H5IMis_palette( fid, PAL_NAME ) < 0 ) goto out; /*------------------------------------------------------------------------- * end tests *------------------------------------------------------------------------- */ if(buf1) HDfree(buf1); if(buf2) HDfree(buf2); if(buf1_out) HDfree(buf1_out); if(buf2_out) HDfree(buf2_out); /* Close the file. */ if(H5Fclose( fid ) < 0) goto out; PASSED(); return 0; /* error zone, gracefully close */ out: if(buf1) HDfree(buf1); if(buf2) HDfree(buf2); if(buf1_out) HDfree(buf1_out); if(buf2_out) HDfree(buf2_out); H5E_BEGIN_TRY { H5Fclose(fid); } H5E_END_TRY; H5_FAILED(); return FAIL; }