IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 24, 2021, 3:37:52 PM (5 years ago)
Author:
eugene
Message:

clarify in comments the buffers and pixel sizes used in the different stages of compression/uncompression; catch unimplemented compression cases; fix error in pixel size for RICE1; cleanup verbose code; use correct sscanf format code in ASCII table reading code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/libfits/matrix/F_uncompress_M.c

    r40549 r41474  
    4040    gfits_modify (header, NAME, TYPE, 1, OUT); }
    4141
     42# define DUMP_ROWBYTES(BUF, INFO)              \
     43  if (VERBOSE_DUMP && (row == 0)) {            \
     44    int k;                                     \
     45    fprintf (stderr, "%s: ", INFO);            \
     46    for (k = 0; k < 64; k++) {                 \
     47      fprintf (stderr, "%02hhx", BUF[k]);    \
     48      if (k % 4 == 3) fprintf (stderr, " ");   \
     49    } fprintf (stderr, "\n"); }
     50
    4251int gfits_uncompress_image (Header *header, Matrix *matrix, FTable *ftable) {
    4352
     
    97106  } else {
    98107    gfits_delete (header, "ZTILE1", 1);
     108    // if ZTILE1 exists, all ZTILEn must exist:
    99109    for (axis = 1; axis < header->Naxes; axis++) {
    100110      snprintf (key, 10, "ZTILE%d", axis + 1);
     
    127137  }
    128138
    129   // XXX get ZMASKCMP
    130   int zsimple, have_zsimple;
    131   int have_ztension;
     139  // check for ZMASKCMP (not yet supported 2021.01.23)
     140  char zmaskcmp[81];
     141  int have_zmaskcmp = gfits_scan (header, "ZMASKCMP", "%s", 1, zmaskcmp);
     142  if (have_zmaskcmp) {
     143    fprintf (stderr, "null pixels were compressed with %s, not implemented in Ohana\n", zmaskcmp);
     144    ESCAPE;
     145  }
    132146
    133147  // NOTE target of %t must be int length
    134   have_zsimple  = gfits_scan_alt (header, "ZSIMPLE", "%t", 1, &zsimple);
    135   have_ztension = gfits_scan (header, "ZTENSION", "%s", 1, exttype);
    136 
    137   // this is a very bogus case: we cannot have both keywords
     148  int zsimple;
     149  int have_zsimple  = gfits_scan_alt (header, "ZSIMPLE", "%t", 1, &zsimple);
     150  int have_ztension = gfits_scan (header, "ZTENSION", "%s", 1, exttype);
     151
     152  // this is a bogus case: we cannot have both keywords
    138153  if (have_zsimple && have_ztension) ESCAPE;
    139154
     
    179194    }
    180195  } else {
     196    // if this is a PHU (whether or not ZSIMPLE is present), PCOUNT & GCOUNT must be basic (0,1)
    181197    header->pcount = 0;
    182198    header->gcount = 1;
     
    188204  MOD_KEYWORD ("ZDATASUM", "DATASUM",  "%s", datasum,         datasum);
    189205
    190   zscale = 1;
    191   gfits_scan (header, "ZSCALE", "%f", 1, &zscale);
    192 
    193   zblank = 32767;
    194   gfits_scan (header, "ZBLANK", "%d", 1, &zblank);
    195 
    196   oblank = 32767;
    197   gfits_scan (header, "BLANK", "%d", 1, &oblank);
    198 
    199   zzero = 0;
    200   gfits_scan (header, "ZZERO", "%f", 1, &zzero);
     206  // get other basic keywords (default values supplied)
     207  zscale = 1;      gfits_scan (header, "ZSCALE", "%f", 1, &zscale);
     208  zblank = 32767;  gfits_scan (header, "ZBLANK", "%d", 1, &zblank);
     209  oblank = 32767;  gfits_scan (header, "BLANK", "%d", 1, &oblank);
     210  zzero = 0;       gfits_scan (header, "ZZERO", "%f", 1, &zzero);
    201211
    202212  // find the COMPRESSED_DATA column (format should be 1PB, 1PI, 1PJ)
    203   // is it required that this be the only column?
     213  // XXX is it required that this be the only column? -- not if ZMASKCMP exists (not yet supported)
    204214  int colnum;
    205215  for (colnum = 1; TRUE; colnum++) {
     
    242252
    243253  // heapdata.pixsize : zdef.format
    244   // zdata.pixsize : depends on compression
    245   // odata.pixsize : depends on compression
    246   // idata.pixsize : header.bitpix
     254  // zdata.pixsize : depends on compression method (cmp_pixsize below)
     255  // odata.pixsize : depends on compression method (raw_pixsize below)
     256  // idata.pixsize : depends on header.bitpix
    247257
    248258  // size of an element in the vartable heap section
    249259  // zdata_pixsize = gfits_vartable_heap_pixsize (zdef.format); XXX
    250260
    251   // size of a pixel in the output from the decompression routine
     261  // depending on the compression method, the data may need to be byteswapped either before
     262  // or after uncompression:
     263
     264  // size of a pixel in the compressed data (cmp_pixsize), used for byteswap below
    252265  int cmp_pixsize = gfits_compressed_data_pixsize (cmptype, header[0].bitpix, optname, optvalue, Noptions);
    253   int raw_bitpix  = gfits_uncompressed_data_bitpix (cmptype, header[0].bitpix, optname, optvalue, Noptions);
    254   int raw_pixsize = gfits_uncompressed_data_pixsize (cmptype, header[0].bitpix, optname, optvalue, Noptions);
     266
     267  // size of a pixel in the raw uncomprssed buffer (raw_pixsize) after uncompression (last option says RICE ignores bitpix)
     268  int raw_pixsize = gfits_uncompressed_data_pixsize (cmptype, header[0].bitpix, optname, optvalue, Noptions, FALSE);
     269
     270  // we also need the bitpix value for the raw uncompressed buffer to distinguish int and floats
     271  int raw_bitpix  = gfits_uncompressed_data_bitpix (cmptype, header[0].bitpix, optname, optvalue, Noptions, FALSE);
    255272
    256273  // size of a pixel in the final image (not needed)
     
    261278  ALLOCATE (out, char, Nout_alloc);
    262279
    263   off_t row;
    264 
    265   if (VERBOSE_DUMP) {
    266     int k;
    267     fprintf (stderr, "unc tbl: ");
    268     for (k = 0; k < 64; k++) { fprintf (stderr, "%02hhx", ftable->buffer[k]); if (k % 4 == 3) fprintf (stderr, " "); }
    269     fprintf (stderr, "\n");
    270   }
    271 
    272280  // uncompress the data
    273281  Nzrows = ftable->header->Naxis[1];
    274   for (row = 0; row < Nzrows; row++) {
     282  for (off_t row = 0; row < Nzrows; row++) {
     283
     284    DUMP_ROWBYTES (ftable->buffer, "tbl dat");
    275285
    276286    // expected output size for this tile
     
    280290    if (!zdata) ESCAPE;
    281291
    282     if (VERBOSE_DUMP && (row == 0)) {
    283       int k;
    284       fprintf (stderr, "unc dat: ");
    285       for (k = 0; k < 64; k++) { fprintf (stderr, "%02hhx", zdata[k]); if (k % 4 == 3) fprintf (stderr, " "); }
    286       fprintf (stderr, "\n");
    287     }
    288 
    289     if (strcasecmp(cmptype, "NONE_2") && // strcasecmp(cmptype, "NONE") &&
    290         strcasecmp(cmptype, "GZIP_1") &&
    291         strcasecmp(cmptype, "GZIP_2") &&
    292         strcasecmp(cmptype, "RICE_1") &&
    293         strcasecmp(cmptype, "RICE_ONE")) {
    294       // Nzdata is number of bytes
    295       if (!gfits_byteswap_zdata (zdata, Nzdata, cmp_pixsize)) ESCAPE;
    296     }
    297 
    298     if (VERBOSE_DUMP && (row == 0)) {
    299       int k;
    300       fprintf (stderr, "unc SWP: ");
    301       for (k = 0; k < 64; k++) { fprintf (stderr, "%02hhx", zdata[k]); if (k % 4 == 3) fprintf (stderr, " "); }
    302       fprintf (stderr, "\n");
    303       fprintf (stderr, "Nout  : %lu -> ", Nout);
    304     }
     292    DUMP_ROWBYTES (zdata, "cmp dat");
     293
     294    // byteswap the compressed data before uncompressing.
     295    // compression algorithms which do not need to byteswap here return 1 for cmp_pixsize
     296    if (!gfits_byteswap_zdata (zdata, Nzdata, cmp_pixsize)) ESCAPE;
     297
     298    DUMP_ROWBYTES (zdata, "cmp swp");
    305299   
    306300    // gfits_uncompress_data uncompresses from zdata to the temporary output buffer which must be allocated
    307     // XXX the tile must not be > 2GB
    308     // Nout going in is number of bytes
     301    // Note: the tile must not be > 2GB
     302    // Nout going in is the expected number of bytes
    309303    if (!gfits_uncompress_data ((char *)zdata, Nzdata, cmptype, optname, optvalue, Noptions, out, &Nout, Nout_alloc, raw_pixsize)) ESCAPE;
    310304
    311     if (VERBOSE_DUMP && (row == 0)) {
    312       int k;
    313       fprintf (stderr, "%lu\n", Nout);
    314       fprintf (stderr, "unc swp: ");
    315       for (k = 0; k < 64; k++) { fprintf (stderr, "%02hhx", out[k]); if (k % 4 == 3) fprintf (stderr, " "); }
    316       fprintf (stderr, "\n");
    317     }
     305    DUMP_ROWBYTES (out, "unc swp");
    318306   
    319307    if (!strcasecmp(cmptype, "GZIP_1")) {
     
    322310    }
    323311   
    324     if (VERBOSE_DUMP && (row == 0)) {
    325       int k;
    326       fprintf (stderr, "unc raw: ");
    327       for (k = 0; k < 64; k++) { fprintf (stderr, "%02hhx", out[k]); if (k % 4 == 3) fprintf (stderr, " "); }
    328       fprintf (stderr, "\n");
    329     }
     312    DUMP_ROWBYTES (out, "unc raw");
    330313
    331314    // copy the uncompressed pixels into their correct locations           
     
    336319    }
    337320
    338     if (VERBOSE_DUMP && (row == 0)) {
    339       int k;
    340       fprintf (stderr, "unc mat: ");
    341       for (k = 0; k < 64; k++) { fprintf (stderr, "%02hhx", matrix->buffer[k]); if (k % 4 == 3) fprintf (stderr, " "); }
    342       fprintf (stderr, "\n");
    343     }
     321    DUMP_ROWBYTES (matrix->buffer, "out raw");
    344322
    345323    // update the tile counters, carrying to the next dimension if needed
     
    370348}
    371349
    372 // bitpix is the input data size/type
     350// raw_bitpix is the data size/type for the temporary buffer of uncompressed data
     351// matrix->bitpix is the data size/type of the output image data
    373352int gfits_distribute_data (Matrix *matrix, char *raw, unsigned long int Nraw, int raw_bitpix, int *otile, int oblank, unsigned long int *ztile, int zblank, float zscale, float zzero) {
    374353
     
    514493}
    515494
    516 // bitpix is the input data size/type
     495// raw_bitpix is the data size/type for the temporary buffer of uncompressed data
     496// matrix->bitpix is the data size/type of the output image data
     497// The gzip2 algorithm organizes the compressed data in groups by significant byte sequence (most significant byte of all pixels first, etc).
     498// gfits_distribute_gzp2 needs to shuffle the bytes back into the correct location in the tile.
    517499int gfits_distribute_gzp2 (Matrix *matrix, char *raw, unsigned long int Nraw, int raw_bitpix, int *otile, int oblank, unsigned long int *ztile, int zblank, float zscale, float zzero) {
     500  // unused parameters are supplied to make this function have the same API as gfits_distribute_data()
    518501  OHANA_UNUSED_PARAM(oblank);
    519502  OHANA_UNUSED_PARAM(zblank);
Note: See TracChangeset for help on using the changeset viewer.