Changeset 38326
- Timestamp:
- May 26, 2015, 4:29:20 PM (11 years ago)
- Location:
- branches/eam_branches/ohana.20150429/src/libfits
- Files:
-
- 4 edited
-
matrix/F_compress_M.c (modified) (5 diffs)
-
matrix/F_compress_utils.c (modified) (1 diff)
-
table/F_table_format.c (modified) (2 diffs)
-
table/F_table_varlength.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_M.c
r38323 r38326 26 26 // zcmptype 27 27 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 39 int 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); 55 77 // 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); 57 84 58 85 // add ZIMAGE from output header … … 60 87 61 88 // 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"); 64 92 65 93 for (i = 0; i < header->Naxes; i++) { … … 149 177 MOD_KEYWORD ("ZHECKSUM", "CHECKSUM", "%s", checksum, checksum); 150 178 MOD_KEYWORD ("ZDATASUM", "DATASUM", "%s", datasum, datasum); 151 # endif152 179 153 180 zscale = 1; … … 162 189 zzero = 0; 163 190 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; 178 195 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 } 198 239 199 240 // this takes place in three major steps: … … 207 248 // idata.pixsize : header.bitpix 208 249 209 // size of an element in the vartable heap section210 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 routine216 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 table221 // each tile -> 1 row of the output table222 int done = FALSE;223 while (!done) {224 225 // size of the current tile226 Nraw = gfits_tile_size (matrix, otile, ztile);227 228 // copy the raw pixels from their native matrix locations to the temporary output buffer229 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 > 2GB232 233 Nzdata = raw_pixsize*max_tile_size; // available space, replaced with actual output size on compression234 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 activate239 if (FALSE) gfits_byteswap_zdata (zdata, Nzdata, zdata_pixsize) return (FALSE);240 241 // update the tile counters, carrying to the next dimension if needed242 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 }257 250 258 251 // raw_pixsize is the bytes / pixel for the input matrix -
branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_utils.c
r38322 r38326 13 13 // XXX inconsistency between data element size between compressed data, uncompressed data, 14 14 // and output pixel data..... 15 16 // need a structure to describe tiles, current tile? 17 18 typedef struct { 19 int *ztile; // max size of a tile (edge tiles may be smaller) 20 int *otile; // counter for current tile (eg, for (2,3,0) otile[0] = 2, otile[1] = 3, otile[2] = 0) 21 int *ntile; // number of tiles in dimension [i] 22 } ImageTile; 23 24 // advance the otile counter. 25 int gfits_imtile_next (Matrix *matrix, int *ztile, int *ntile, int *otile) { 26 27 int i; 28 29 // update the tile counters, carrying to the next dimension if needed 30 for (i = 0; i < matrix->Naxes; i++) { 31 otile[i] ++; 32 if (otile[i] == ntile[i]) { 33 if (i == matrix->Naxes - 1) return FALSE; 34 otile[i] = 0; 35 } else { 36 return TRUE; 37 } 38 } 39 return TRUE; 40 } 41 42 // how many tiles needed for this image (given ztile[]) 43 int gfits_imtile_start (Matrix *matrix, int *otile) { 44 45 int i; 46 47 // update the tile counters, carrying to the next dimension if needed 48 for (i = 0; i < matrix->Naxes; i++) { 49 otile[i] = 0; 50 } 51 } 52 53 // how many tiles needed for this image (given ztile[]) 54 int gfits_imtile_count (Matrix *matrix, int *ztile, int *ntile) { 55 56 int i; 57 58 int Ntiles = 1; 59 for (i = 0; i < matrix->Naxes; i++) { 60 ntile[i] = (matrix->Naxis[i] % ztile[i]) ? (matrix->Naxis[i] / ztile[i] + 1) : (matrix->Naxis[i] / ztile[i]); 61 Ntiles *= ntile[i]; 62 } 63 64 return (Ntiles); 65 } 66 67 // how many tiles needed for this image (given ztile[]) 68 int gfits_imtile_maxsize (Matrix *matrix, int *ztile) { 69 70 int i; 71 72 int max_tile_size = 1; 73 for (i = 0; i < matrix->Naxes; i++) { 74 max_tile_size *= ztile[i]; 75 } 76 77 return (max_tile_size); 78 } 15 79 16 80 // true sizes of this tile (in pixels) -
branches/eam_branches/ohana.20150429/src/libfits/table/F_table_format.c
r37039 r38326 52 52 break; 53 53 case 'P': 54 { *Nbytes = 8; strcpy (type, "var"); *Nval = 2*Nv; } 54 { *Nbytes = 4; strcpy (type, "var"); *Nval = 2*Nv; } 55 break; 56 case 'Q': 57 { *Nbytes = 8; strcpy (type, "var64"); *Nval = 2*Nv; } 55 58 break; 56 59 case 'C': … … 80 83 C - complex float 81 84 M - complex double 82 P - var length array descrpt (64 bit) 85 P - var length array descrpt (32 bit pointer) 86 Q - var length array descrpt (64 bit pointer) 83 87 84 88 all can be preceeded by integer N which specified number 85 89 of entries in array 86 87 90 */ 88 91 -
branches/eam_branches/ohana.20150429/src/libfits/table/F_table_varlength.c
r38324 r38326 123 123 // Here are operations we need to do: 124 124 125 int gfits_varlength_column_add_data (FTable *table, char *data, off_t Ndata, int row ) {125 int gfits_varlength_column_add_data (FTable *table, char *data, off_t Ndata, int row, VarLengthColumn *column) { 126 126 127 127 // find the current starting point for new data (end of current buffer main data + current heap … … 161 161 column->maxlen = MAX (column->maxlen, Ndata); 162 162 163 off_t pcount; 164 gfits_scan (ftable->header, "PCOUNT", OFF_T_FMT, 1, &pcount); 165 166 pcount += Ndata; 167 gfits_modify (header, "PCOUNT", OFF_T_FMT, 1, pcount); 168 163 169 return TRUE; 164 170 }
Note:
See TracChangeset
for help on using the changeset viewer.
