IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 4, 2015, 8:49:25 AM (11 years ago)
Author:
eugene
Message:

rice and gzip2 both work for images; working on tables

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ohana.20150429/src/libfits/table/F_compress_T.c

    r38356 r38363  
    1818// we do not try to support compression of an image with an associated table (not valid)
    1919
    20 # define ESCAPE(A) { fprintf (stderr, "error in %s @ line %d\n", __func__, __LINE__); goto escape; }
    21 
    22 int gfits_collect_table_data (FTable *table, char *raw, int col, int row_start, int Nrows, int rowsize);
     20# define ESCAPE { fprintf (stderr, "error in %s @ line %d\n", __func__, __LINE__); goto escape; }
     21
     22int gfits_collect_table_data (FTable *table, TableField *field, char *raw, int row_start, int Nrows);
     23int gfits_collect_table_gzp2 (FTable *table, TableField *field, char *raw, int row_start, int Nrows);
    2324
    2425int gfits_compress_table (FTable *srctable, FTable *tgttable, int ztilelen, char *zcmptype) {
     
    5051
    5152  // creates an empty (Naxes = 2, Naxis[i] = 0) table header with XTENSION = BINTABLE, EXTNAME = COMPRESSED_TABLE
    52   if (!gfits_create_table_header (tgtheader, "BINTABLE", extname)) ESCAPE(A);
     53  if (!gfits_create_table_header (tgtheader, "BINTABLE", extname)) ESCAPE;
    5354
    5455  int Nfields;
    55   if (!gfits_scan (srcheader, "TFIELDS", "%d", 1, &Nfields)) ESCAPE(A);
     56  if (!gfits_scan (srcheader, "TFIELDS", "%d", 1, &Nfields)) ESCAPE;
    5657
    5758  TableField *fields = NULL;
     
    5960  for (i = 0; i < Nfields; i++) {
    6061    snprintf (keyword, 80, "TTYPE%d", i+1);
    61     if (!gfits_scan (srcheader, keyword, "%s", 1, fields[i].ttype)) ESCAPE(A);
    62     if (!gfits_scan_alt (srcheader, keyword, "%C", 1, fields[i].ttype_cmt)) ESCAPE(A);
     62    if (!gfits_scan (srcheader, keyword, "%s", 1, fields[i].ttype)) ESCAPE;
     63    if (!gfits_scan_alt (srcheader, keyword, "%C", 1, fields[i].ttype_cmt)) ESCAPE;
    6364
    6465    // TUNIT is not mandatory
     
    6768      fields[i].tunit[0] = 0;
    6869    } else {
    69       if (!gfits_scan_alt (srcheader, keyword, "%C", 1, fields[i].tunit_cmt)) ESCAPE(A);
     70      if (!gfits_scan_alt (srcheader, keyword, "%C", 1, fields[i].tunit_cmt)) ESCAPE;
    7071    }
    7172   
    7273    snprintf (keyword, 80, "TFORM%d", i+1);
    73     if (!gfits_scan (srcheader, keyword, "%s", 1, fields[i].tformat)) ESCAPE(A);
    74     if (!gfits_scan_alt (srcheader, keyword, "%C", 1, fields[i].tformat_cmt)) ESCAPE(A);
     74    if (!gfits_scan (srcheader, keyword, "%s", 1, fields[i].tformat)) ESCAPE;
     75    if (!gfits_scan_alt (srcheader, keyword, "%C", 1, fields[i].tformat_cmt)) ESCAPE;
    7576
    7677    // XXX this should depend on the field type, but for now just use a single type (GZIP_1)
     
    7879
    7980    // by using "P" format, we are limited to 32bit pointers (2GB heap)
    80     if (!gfits_define_bintable_column (tgtheader, "1QB(0)", fields[i].ttype, fields[i].ttype_cmt, fields[i].tunit, 1.0, 0.0)) ESCAPE (A);
     81    if (!gfits_define_bintable_column (tgtheader, "1QB(0)", fields[i].ttype, fields[i].ttype_cmt, fields[i].tunit, 1.0, 0.0)) ESCAPE;
    8182  }   
    8283
    8384  // allocates the default data array (tgttable->buffer)
    84   if (!gfits_create_table (tgtheader, tgttable)) ESCAPE (A);
     85  if (!gfits_create_table (tgtheader, tgttable)) ESCAPE;
    8586
    8687  // copy original header to output header (XXX this needs to be finished)
    87   if (!gfits_copy_keywords_compress (srcheader, tgtheader)) ESCAPE (A);
     88  if (!gfits_copy_keywords_compress (srcheader, tgtheader)) ESCAPE;
    8889 
    8990  // this allocates the pointer data array for the full set of tiles (NOT the heap)
     
    9192  ALLOCATE_ZERO (tmpbuffer, char, 16*Ntile); // need space for Ntile entries, each of width 16 bytes (2 long)
    9293  for (i = 0; i < Nfields; i++) {
    93     if (!gfits_set_bintable_column (tgtheader, tgttable, fields[i].ttype, tmpbuffer, Ntile)) ESCAPE (A);
     94    if (!gfits_set_bintable_column (tgtheader, tgttable, fields[i].ttype, tmpbuffer, Ntile)) ESCAPE;
    9495  }
    9596  free (tmpbuffer);
    9697   
    9798  // add ZIMAGE from output header
    98   if (!gfits_modify_alt (tgtheader, "ZTABLE", "%t", 1, TRUE)) ESCAPE (A);
     99  if (!gfits_modify_alt (tgtheader, "ZTABLE", "%t", 1, TRUE)) ESCAPE;
    99100
    100101  // define compression-specific keywords, update header as needed.
    101   if (!gfits_modify (tgtheader, "ZTILELEN", "%d", 1, ztilelen)) ESCAPE(A);
    102 
    103   if (!gfits_modify (tgtheader, "ZNAXIS1", OFF_T_FMT, 1, srcheader->Naxis[0])) ESCAPE (A);
    104   if (!gfits_modify (tgtheader, "ZNAXIS2", OFF_T_FMT, 1, srcheader->Naxis[1])) ESCAPE (A);
     102  if (!gfits_modify (tgtheader, "ZTILELEN", "%d", 1, ztilelen)) ESCAPE;
     103
     104  if (!gfits_modify (tgtheader, "ZNAXIS1", OFF_T_FMT, 1, srcheader->Naxis[0])) ESCAPE;
     105  if (!gfits_modify (tgtheader, "ZNAXIS2", OFF_T_FMT, 1, srcheader->Naxis[1])) ESCAPE;
    105106
    106107  off_t pcount;
    107   if (!gfits_scan (srcheader, "PCOUNT", OFF_T_FMT, 1, &pcount)) ESCAPE (A);
    108   if (!gfits_modify (tgtheader, "ZPCOUNT", OFF_T_FMT, 1, pcount)) ESCAPE (A);
     108  if (!gfits_scan (srcheader, "PCOUNT", OFF_T_FMT, 1, &pcount)) ESCAPE;
     109  if (!gfits_modify (tgtheader, "ZPCOUNT", OFF_T_FMT, 1, pcount)) ESCAPE;
    109110
    110111  off_t theap;
    111112  if (gfits_scan (srcheader, "THEAP", OFF_T_FMT, 1, &theap)) {
    112     if (!gfits_modify (tgtheader, "ZTHEAP", OFF_T_FMT, 1, theap)) ESCAPE (A);
     113    if (!gfits_modify (tgtheader, "ZTHEAP", OFF_T_FMT, 1, theap)) ESCAPE;
    113114  }
    114115 
     
    118119  for (i = 0; i < Nfields; i++) {
    119120    snprintf (keyword, 81, "ZFORM%d", i+1);
    120     if (!gfits_modify (tgtheader, keyword, "%s", 1, fields[i].tformat)) ESCAPE (A);
    121     if (!gfits_varlength_column_define (tgttable, &fields[i].zdef, i+1)) ESCAPE(A);
    122     if (!gfits_bintable_format (fields[i].tformat, fields[i].datatype, &fields[i].Nvalues, &fields[i].pixsize)) ESCAPE(A); //
     121    if (!gfits_modify (tgtheader, keyword, "%s", 1, fields[i].tformat)) ESCAPE;
     122    if (!gfits_varlength_column_define (tgttable, &fields[i].zdef, i+1)) ESCAPE;
     123    if (!gfits_bintable_format (fields[i].tformat, fields[i].datatype, &fields[i].Nvalues, &fields[i].pixsize)) ESCAPE; //
    123124    fields[i].rowsize = fields[i].Nvalues*fields[i].pixsize;
    124125    max_width = MAX(max_width, fields[i].rowsize);
     
    126127    // compression type per column (XXX for now we are just using zcmptype, as set above)
    127128    snprintf (keyword, 81, "ZCTYP%d", i+1);
    128     if (!gfits_modify (tgtheader, keyword, "%s", 1, fields[i].zctype)) ESCAPE (A);
     129    if (!gfits_modify (tgtheader, keyword, "%s", 1, fields[i].zctype)) ESCAPE;
    129130
    130131    fields[i].offset = offset;
     
    151152
    152153      // copy the raw pixels from their native matrix locations to the temporary output buffer
    153       if (!gfits_collect_table_data (srctable, raw, fields[j].offset, row_start, Nrows, fields[j].rowsize)) ESCAPE(A);
     154      if (!strcasecmp(fields[i].zctype, "GZIP_2")) {
     155        if (!gfits_collect_table_gzp2 (srctable, &fields[j], raw, row_start, Nrows)) ESCAPE;
     156      } else {
     157        if (!gfits_collect_table_data (srctable, &fields[j], raw, row_start, Nrows)) ESCAPE;
     158      }
    154159     
    155160      // optname, optvalue = NULL, Noptions = 0
     
    157162      int Nraw = Nrows*fields[j].Nvalues; // number of pixels
    158163      if (!strcmp(fields[j].zctype, "GZIP_1")) {
    159         if (!gfits_byteswap_zdata (raw, Nraw * fields[j].pixsize, fields[j].pixsize)) ESCAPE(A);
     164        if (!gfits_byteswap_zdata (raw, Nraw * fields[j].pixsize, fields[j].pixsize)) ESCAPE;
    160165      }
    161166
    162167      int Nzdata = Nzdata_alloc; // available space, replaced with actual output size on compression
    163       if (!gfits_compress_data (zdata, &Nzdata, fields[j].zctype, NULL, NULL, 0, raw, Nraw, fields[j].pixsize, 0, 0)) ESCAPE(A);
     168      if (!gfits_compress_data (zdata, &Nzdata, fields[j].zctype, NULL, NULL, 0, raw, Nraw, fields[j].pixsize, 0, 0)) ESCAPE;
    164169     
    165       if (!gfits_varlength_column_add_data (tgttable, zdata, Nzdata, i, &fields[j].zdef)) ESCAPE(A);
    166      
    167       // XXX need to activate
    168       // if (FALSE) gfits_byteswap_zdata (zdata, Nzdata, zdata_pixsize) ESCAPE(A);
     170      if (strcasecmp(fields[j].zctype, "GZIP_1") && strcasecmp(fields[j].zctype, "GZIP_2") && strcasecmp(fields[j].zctype, "RICE_1")) {
     171        if (!gfits_byteswap_zdata (zdata, Nzdata, fields[j].pixsize)) ESCAPE;
     172      }
     173
     174      if (!gfits_varlength_column_add_data (tgttable, zdata, Nzdata, i, &fields[j].zdef)) ESCAPE;
    169175    }
    170176  }
    171177
    172178  for (i = 0; i < Nfields; i++) {
    173     if (!gfits_varlength_column_finish (tgttable, &fields[i].zdef)) ESCAPE(A);
     179    if (!gfits_varlength_column_finish (tgttable, &fields[i].zdef)) ESCAPE;
    174180  }
    175181
     
    190196// raw_pixsize is the bytes / pixel for the input matrix
    191197// place the raw image bytes for the current tile into the tile buffer
    192 int gfits_collect_table_data (FTable *table, char *raw, int col, int row_start, int Nrows, int rowsize) {
     198int gfits_collect_table_data (FTable *table, TableField *field, char *raw, int row_start, int Nrows) {
    193199
    194200  off_t i;
     
    201207  for (i = 0; i < Nrows; i++) {
    202208    int row = row_start + i;
    203     memcpy (&raw[i*rowsize], &table->buffer[Nx*row + col], rowsize);
     209    memcpy (&raw[i*field->rowsize], &table->buffer[Nx*row + field->offset], field->rowsize);
    204210# if (VERBOSE)
    205     int j; for (j = 0; j < rowsize; j++) fprintf (stderr, "0x%02hhx ", table->buffer[Nx*row + col + j]);
     211    int j; for (j = 0; j < field->rowsize; j++) fprintf (stderr, "0x%02hhx ", table->buffer[Nx*row + field->offset + j]);
    206212# endif
    207213  }
     
    210216  return (TRUE);
    211217}
     218
     219int gfits_collect_table_gzp2 (FTable *table, TableField *field, char *raw, int row_start, int Nrows) {
     220
     221  off_t i, j, k;
     222
     223  // we are copying NN rows into the column which starts at XX and has MM bytes per row
     224
     225  off_t Nx = table->header->Naxis[0];
     226
     227  for (k = 0; k < field->pixsize; k++) {
     228    for (i = 0; i < Nrows; i++) {
     229      int row = row_start + i;
     230      char *tgt = &raw[i*field->rowsize + k*Nrows*field->Nvalues];
     231      char *src = &table->buffer[Nx*row + field->offset + k];
     232      for (j = 0; j < field->Nvalues; j++, tgt+=field->pixsize, src++) {
     233        *tgt = *src;
     234        myAssert (src - raw < field->
     235      }
     236    }
     237  }
     238  return (TRUE);
     239}
Note: See TracChangeset for help on using the changeset viewer.