IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 26, 2015, 4:29:20 PM (11 years ago)
Author:
eugene
Message:

updating compression functions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_M.c

    r38323 r38326  
    2626// zcmptype
    2727
    28 int gfits_compress_image (Header *header, Matrix *matrix, FTable *ftable) {
    29 
    30   off_t Nzdata, Nzrows, zcol;
    31   int i, j, status, zimage, Nout, max_tile_size;
    32   char cmptype[80];
    33   char zaxis[10], naxis[10], key[10], word[81], exttype[81], checksum[81], datasum[81];
    34   int Noptions, NOPTIONS, zblank, oblank;
    35   VarLengthColumn zdef;
    36   float zscale, zzero;
    37 
    38   int zdata_pixsize, odata_pixsize;
    39 
    40   int *ztile = NULL;
    41   int *otile = NULL;
    42   int *ntile = NULL;
    43   char **optname = NULL;
    44   char **optvalue = NULL;
    45   char *out = NULL;
    46   char *zdata = NULL;
    47 
    48   Noptions = 0;
    49 
    50   // is ZIMAGE present?
    51   // NOTE target of %t must be int length
    52   status = gfits_scan_alt (ftable->header, "ZIMAGE", "%t", 1, &zimage);
    53   if (!status || !zimage) ESCAPE;
    54 
     28// steps to convert an image to a compressed table:
     29// * determine the number of tiles (from ztile[] and image size)
     30// * construct an empty table with the right dimensions (1 column, Ntile rows)
     31// * loop over tiles
     32// * extract tile data to a buffer
     33// * compress the buffer data
     34// * insert in table heap
     35// * update header keywords
     36
     37// XXX do not try to support compression of an image with an associated table (not valid)
     38
     39int gfits_compress_image (Header *header, Matrix *matrix, FTable *ftable, int *ztile, char *zcmptype) {
     40
     41  // off_t Nzdata, Nzrows, zcol;
     42  // int i, j, status, zimage, Nout, max_tile_size;
     43  // char cmptype[80];
     44  // char zaxis[10], naxis[10], key[10], word[81], exttype[81], checksum[81], datasum[81];
     45  // int Noptions, NOPTIONS, zblank, oblank;
     46  // VarLengthColumn zdef;
     47  // float zscale, zzero;
     48
     49  // int zdata_pixsize, odata_pixsize;
     50  // int *otile = NULL;
     51  // int *ntile = NULL;
     52  // char **optname = NULL;
     53  // char **optvalue = NULL;
     54  // char *out = NULL;
     55  // char *zdata = NULL;
     56
     57  // determine the number of tiles (from ztile[] and image size)
     58  if (!ztile) {
     59    ALLOCATE (ztile, int, matrix->Naxes);
     60    ztile[0] = matrix->Naxis[0];
     61    for (i = 1; i < matrix->Naxes; i++) ztile[i] = 1;
     62  }
     63  ALLOCATE (ntile, int, matrix->Naxes);
     64  ALLOCATE (otile, int, matrix->Naxes);
     65
     66  int Ntile = gfits_imtile_count (matrix, ztile, ntile);
     67
     68  // creates an empty (Naxes = 2, Naxis[i] = 0) table header with XTENSION = BINTABLE, EXTNAME = COMPRESSED_IMAGE
     69  gfits_create_table_header (&theader, "BINTABLE", "COMPRESSED_IMAGE");
     70
     71  // by using "P" format, we are limited to 32bit pointers (2GB heap)
     72  // XXX how is the "B" format used?
     73  gfits_define_bintable_column (&theader, "1PB(0)", "COMPRESSED_DATA", "compressed image data", "none", 1.0, 0.0);
     74
     75  // XXX I need to copy the bulk of the header from the matrix header -> ftable.header
     76  gfits_create_table (&theader, ftable);
    5577  // copy original header to output header
    56   gfits_copy_header (header, theader);
     78  // XXX gfits_copy_header (header, theader); -- I need to copy only the desired keywords.
     79 
     80  // this allocates the data array
     81  char *tmpbuffer = NULL;
     82  ALLOCATE_ZERO (tmpbuffer, char, Ntile);
     83  gfits_set_bintable_column (&theader, &ftable, "COMPRESSED_DATA", tmpbuffer, Ntile);
    5784
    5885  // add ZIMAGE from output header
     
    6087
    6188  // translate image-specific keywords to Z versions (updates the Z version, removes the old version)
    62   MOD_KEYWORD ("ZBITPIX", "BITPIX", "%d", header->bitpix);
    63   MOD_KEYWORD ("ZNAXIS",  "NAXIS",  "%d", header->Naxes);
     89  // XXX use the header or the structure as authoratative?
     90  MOVE_KEYWORD ("ZBITPIX", "BITPIX", "%d");
     91  MOVE_KEYWORD ("ZNAXIS",  "NAXIS",  "%d");
    6492
    6593  for (i = 0; i < header->Naxes; i++) {
     
    149177  MOD_KEYWORD ("ZHECKSUM", "CHECKSUM", "%s", checksum,        checksum);
    150178  MOD_KEYWORD ("ZDATASUM", "DATASUM",  "%s", datasum,         datasum);
    151 # endif
    152179
    153180  zscale = 1;
     
    162189  zzero = 0;
    163190  gfits_scan (header, "ZZERO", "%f", 1, &zzero);
    164 
    165   // XXX do not try to support compression of an image with an associated table (not valid)
    166 
    167   // create a table with a column called COMPRESSED_DATA
    168   // output format may be 1PB, 1PI, 1PJ
    169 
    170   // find the COMPRESSED_DATA column (format should be 1PB, 1PI, 1PJ)
    171   for (i = 1; TRUE; i++) {
    172     snprintf (key, 10, "TTYPE%d", i);
    173     if (!gfits_scan (ftable->header, key, "%s", 1, word)) ESCAPE;
    174     if (!strcmp (word, "COMPRESSED_DATA")) break;
    175   }
    176   zcol = i;
    177 
     191# endif
     192
     193  // XXX how to define zdef?
     194  VarLengthColumn zdef;
    178195  if (!gfits_varlength_column_define (ftable, &zdef, zcol)) ESCAPE;
    179   gfits_delete (header, "TFIELDS", 1);
    180   snprintf (key, 10, "TTYPE"OFF_T_FMT,  zcol);
    181   gfits_delete (header, key, 1);
    182   snprintf (key, 10, "TFORM"OFF_T_FMT,  zcol);
    183   gfits_delete (header, key, 1);
    184 
    185   // counters for tile numbers
    186   ALLOCATE (otile, int, matrix->Naxes);
    187   ALLOCATE (ntile, int, matrix->Naxes);
    188   max_tile_size = 1;
    189   for (i = 0; i < matrix->Naxes; i++) {
    190     otile[i] = 0;
    191     ntile[i] = (matrix->Naxis[i] % ztile[i]) ? (matrix->Naxis[i] / ztile[i] + 1) : (matrix->Naxis[i] / ztile[i]);
    192     max_tile_size *= ztile[i];
    193 
    194     // ztile[i] is the default (or max) tile size in the i-th dimension
    195     // ntile[i] is the number of tiles in the i-th dimension
    196     // otile[i] is the current output tile counter in the i-th dimension
    197   }
     196
     197  max_tile_size = gfits_imtile_maxsize (matrix, ztile);
     198
     199  // size of an element in the vartable heap section
     200  zdata_pixsize = gfits_vartable_heap_pixsize (zdef.format);
     201
     202  // size of a pixel in the final image (not needed)
     203  // raw_pixsize = gfits_uncompressed_data_pixsize (cmptype, header[0].bitpix, optname, optvalue, Noptions)
     204
     205  // size of a pixel in the raw image tile:
     206  raw_pixsize = abs(header[0].bitpix) / 8;
     207
     208  ALLOCATE (raw,   char, raw_pixsize*max_tile_size);
     209  ALLOCATE (zdata, char, raw_pixsize*max_tile_size); //
     210
     211  // init the otile[] counters
     212  gfits_imfile_start (matrix, otile);
     213
     214  // compress the data : copy into a tile, compress the tile, then add to the output table
     215  // each tile -> 1 row of the output table
     216  for (i = 0; i < Ntile; i++) {
     217
     218    // size of the current tile
     219    Nraw = gfits_tile_size (matrix, otile, ztile);
     220
     221    // copy the raw pixels from their native matrix locations to the temporary output buffer
     222    if (!gfits_collect_data (matrix, raw, Nraw, raw_pixsize, otile, oblank, ztile, zblank, zscale, zzero)) return (FALSE);
     223
     224    // XXX the tile must not be > 2GB
     225
     226    Nzdata = raw_pixsize*max_tile_size; // available space, replaced with actual output size on compression
     227    if (!gfits_compress_data (zdata, &Nzdata, zcmptype, optname, optvalue, Noptions, raw, Nraw, raw_pixsize)) return (FALSE);
     228
     229    if (!gfits_varlength_column_add_data (ftable, zdata, Nzdata, i, &zdef)) return FALSE;
     230
     231    // XXX need to activate
     232    if (FALSE) gfits_byteswap_zdata (zdata, Nzdata, zdata_pixsize) return (FALSE);
     233
     234    // update the otile[] counters, carrying to the next dimension if needed
     235    gfits_imfile_next (matrix, ztile, ntile, otile);
     236  }
     237  return (TRUE);
     238}
    198239
    199240  // this takes place in three major steps:
     
    207248  // idata.pixsize : header.bitpix
    208249
    209   // size of an element in the vartable heap section
    210   zdata_pixsize = gfits_vartable_heap_pixsize (zdef.format);
    211 
    212   // size of a pixel in the final image (not needed)
    213   // idata_pixsize = abs(header[0].bitpix) / 8;
    214 
    215   // size of a pixel in the output from the decompression routine
    216   raw_pixsize = gfits_uncompressed_data_pixsize (cmptype, header[0].bitpix, optname, optvalue, Noptions)
    217   ALLOCATE (raw_buffer, char, raw_pixsize*max_tile_size);
    218   ALLOCATE (zdata, char, raw_pixsize*max_tile_size); //
    219 
    220   // compress the data : copy into a tile, compress the tile, then add to the output table
    221   // each tile -> 1 row of the output table
    222   int done = FALSE;
    223   while (!done) {
    224 
    225     // size of the current tile
    226     Nraw = gfits_tile_size (matrix, otile, ztile);
    227 
    228     // copy the raw pixels from their native matrix locations to the temporary output buffer
    229     if (!gfits_collect_data (matrix, raw, Nraw, raw_pixsize, otile, oblank, ztile, zblank, zscale, zzero)) return (FALSE);
    230 
    231     // XXX the tile must not be > 2GB
    232 
    233     Nzdata = raw_pixsize*max_tile_size; // available space, replaced with actual output size on compression
    234     if (!gfits_compress_data (zdata, &Nzdata, cmptype, optname, optvalue, Noptions, raw, Nraw, raw_pixsize)) return (FALSE);
    235 
    236     if (!gfits_varlength_column_addrow (ftable, zdata, Nzdata, &zdef)) return FALSE;
    237 
    238     // XXX need to activate
    239     if (FALSE) gfits_byteswap_zdata (zdata, Nzdata, zdata_pixsize) return (FALSE);
    240 
    241     // update the tile counters, carrying to the next dimension if needed
    242     for (j = 0; j < matrix->Naxes; j++) {
    243       otile[j] ++;
    244       if (otile[j] == ntile[j]) {
    245         if (j == matrix->Naxes - 1) {
    246           done = TRUE;
    247           break;
    248         }
    249         otile[j] = 0;
    250       } else {
    251         break;
    252       }
    253     }
    254   }
    255   return (TRUE);
    256 }
    257250
    258251// raw_pixsize is the bytes / pixel for the input matrix
Note: See TracChangeset for help on using the changeset viewer.