Changeset 38355
- Timestamp:
- Jun 3, 2015, 4:10:26 AM (11 years ago)
- Location:
- branches/eam_branches/ohana.20150429/src/libfits
- Files:
-
- 6 edited
-
include/gfitsio.h (modified) (2 diffs)
-
matrix/F_compress_M.c (modified) (8 diffs)
-
matrix/F_compress_data.c (modified) (5 diffs)
-
matrix/F_compress_utils.c (modified) (3 diffs)
-
matrix/F_uncompress_M.c (modified) (7 diffs)
-
table/F_compress_T.c (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ohana.20150429/src/libfits/include/gfitsio.h
r38350 r38355 192 192 int gfits_uncompress_image PROTO((Header *header, Matrix *matrix, FTable *ftable)); 193 193 int gfits_uncompress_data PROTO((char *zdata, int Nzdata, char *cmptype, char **optname, char **optvalue, int Nopt, char *outdata, int *Nout, int out_pixsize)); 194 int gfits_distribute_data PROTO((Matrix *matrix, int bitpix, char *data, int Ndata, int *otile, int oblank, int *ztile, int zblank, float zscale, float zzero));195 196 int gfits_compress_data PROTO((char *zdata, int *Nzdata, char *cmptype, char **optname, char **optvalue, int Nopt, char *rawdata, int Nraw, int raw_pixsize ));194 int gfits_distribute_data PROTO((Matrix *matrix, char *data, int Ndata, int bitpix, int *otile, int oblank, int *ztile, int zblank, float zscale, float zzero)); 195 196 int gfits_compress_data PROTO((char *zdata, int *Nzdata, char *cmptype, char **optname, char **optvalue, int Nopt, char *rawdata, int Nraw, int raw_pixsize, int Nx, int Ny)); 197 197 int gfits_compress_image PROTO((Header *header, Matrix *matrix, FTable *ftable, int *Ztile, char *zcmptype)); 198 198 int gfits_collect_data PROTO((Matrix *matrix, char *raw, int Nraw, int raw_pixsize, int *otile, int oblank, int *ztile, int zblank, float zscale, float zzero)); … … 205 205 int gfits_imtile_next PROTO((Matrix *matrix, int *ztile, int *ntile, int *otile)); 206 206 207 int gfits_byteswap_zdata PROTO((char *zdata, int Nzdata, int bitpix));208 207 int gfits_extension_is_compressed_image PROTO((Header *header)); 209 208 int gfits_extension_is_compressed_table PROTO((Header *header)); 209 int gfits_byteswap_zdata PROTO((char *zdata, int Nzdata, int pixsize)); 210 int gfits_compressed_data_pixsize PROTO((char *cmptype, int out_bitpix, char **optname, char **optvalue, int Noptions)); 210 211 int gfits_uncompressed_data_pixsize PROTO((char *cmptype, int out_bitpix, char **optname, char **optvalue, int Noptions)); 211 int gfits_vartable_heap_pixsize PROTO((char format)); 212 int gfits_uncompressed_data_bitpix PROTO((char *cmptype, int out_bitpix, char **optname, char **optvalue, int Noptions)); 213 int gfits_vartable_heap_pixsize PROTO((char format)); 212 214 213 215 int gfits_varlength_column_add_data PROTO((FTable *table, char *data, off_t Ndata, int row, VarLengthColumn *column)); -
branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_M.c
r38340 r38355 18 18 // we do not try to support compression of an image with an associated table (not valid) 19 19 20 # define ESCAPE (A){ fprintf (stderr, "error in %s @ line %d\n", __func__, __LINE__); goto escape; }20 # define ESCAPE { fprintf (stderr, "error in %s @ line %d\n", __func__, __LINE__); goto escape; } 21 21 22 22 int gfits_compress_image (Header *header, Matrix *matrix, FTable *ftable, int *Ztile, char *zcmptype) { … … 46 46 47 47 // creates an empty (Naxes = 2, Naxis[i] = 0) table header with XTENSION = BINTABLE, EXTNAME = COMPRESSED_IMAGE 48 if (!gfits_create_table_header (theader, "BINTABLE", "COMPRESSED_IMAGE")) ESCAPE (A);48 if (!gfits_create_table_header (theader, "BINTABLE", "COMPRESSED_IMAGE")) ESCAPE; 49 49 50 50 // by using "P" format, we are limited to 32bit pointers (2GB heap) 51 51 // XXX how is the "B" format used? 52 if (!gfits_define_bintable_column (theader, "1PB(0)", "COMPRESSED_DATA", "compressed image data", "none", 1.0, 0.0)) ESCAPE (A);53 if (!gfits_delete (theader, "TUNIT1", 1)) ESCAPE (A);52 if (!gfits_define_bintable_column (theader, "1PB(0)", "COMPRESSED_DATA", "compressed image data", "none", 1.0, 0.0)) ESCAPE; 53 if (!gfits_delete (theader, "TUNIT1", 1)) ESCAPE; 54 54 55 55 // allocates the default data array (ftable->buffer) 56 if (!gfits_create_table (theader, ftable)) ESCAPE (A);56 if (!gfits_create_table (theader, ftable)) ESCAPE; 57 57 58 58 // copy original header to output header (XXX this needs to be finished) 59 if (!gfits_copy_keywords_compress (header, theader)) ESCAPE (A);59 if (!gfits_copy_keywords_compress (header, theader)) ESCAPE; 60 60 61 61 // this allocates the pointer data array for the full set of tiles (NOT the heap) 62 62 char *tmpbuffer = NULL; 63 63 ALLOCATE_ZERO (tmpbuffer, char, Ntile); 64 if (!gfits_set_bintable_column (theader, ftable, "COMPRESSED_DATA", tmpbuffer, Ntile)) ESCAPE (A);64 if (!gfits_set_bintable_column (theader, ftable, "COMPRESSED_DATA", tmpbuffer, Ntile)) ESCAPE; 65 65 free (tmpbuffer); 66 66 67 67 // add ZIMAGE from output header 68 if (!gfits_modify_alt (theader, "ZIMAGE", "%t", 1, TRUE)) ESCAPE (A);68 if (!gfits_modify_alt (theader, "ZIMAGE", "%t", 1, TRUE)) ESCAPE; 69 69 70 70 // define compression-specific keywords, update header as needed. 71 if (!gfits_modify (theader, "ZCMPTYPE", "%s", 1, zcmptype)) ESCAPE (A);72 73 if (!gfits_modify_alt (theader, "ZSIMPLE", "%t", 1, TRUE)) ESCAPE (A);71 if (!gfits_modify (theader, "ZCMPTYPE", "%s", 1, zcmptype)) ESCAPE; 72 73 if (!gfits_modify_alt (theader, "ZSIMPLE", "%t", 1, TRUE)) ESCAPE; 74 74 // gfits_modify (header, "ZTENSION", "%s", 1, exttype); 75 75 76 76 // supply the Z* header values from the source image header 77 if (!gfits_modify (theader, "ZBITPIX", "%d", 1, header[0].bitpix)) ESCAPE (A);78 79 if (!gfits_modify (theader, "ZNAXIS", "%d", 1, header[0].Naxes)) ESCAPE (A);77 if (!gfits_modify (theader, "ZBITPIX", "%d", 1, header[0].bitpix)) ESCAPE; 78 79 if (!gfits_modify (theader, "ZNAXIS", "%d", 1, header[0].Naxes)) ESCAPE; 80 80 81 81 char keyword[11]; 82 82 for (i = 0; i < header[0].Naxes; i++) { 83 83 snprintf (keyword, 10, "ZNAXIS%d", i + 1); 84 if (!gfits_modify (theader, keyword, OFF_T_FMT, 1, header[0].Naxis[i])) ESCAPE (A);84 if (!gfits_modify (theader, keyword, OFF_T_FMT, 1, header[0].Naxis[i])) ESCAPE; 85 85 } 86 86 87 87 for (i = 0; i < header->Naxes; i++) { 88 88 snprintf (keyword, 10, "ZTILE%d", i + 1); 89 if (!gfits_modify (theader, keyword, "%d", 1, ztile[i])) ESCAPE (A);90 } 91 92 if (!gfits_modify (theader, "BSCALE", "%lf", 1, header[0].bscale)) ESCAPE (A);93 if (!gfits_modify (theader, "BZERO", "%lf", 1, header[0].bzero)) ESCAPE (A);89 if (!gfits_modify (theader, keyword, "%d", 1, ztile[i])) ESCAPE; 90 } 91 92 if (!gfits_modify (theader, "BSCALE", "%lf", 1, header[0].bscale)) ESCAPE; 93 if (!gfits_modify (theader, "BZERO", "%lf", 1, header[0].bzero)) ESCAPE; 94 94 95 95 // other keywords to define: … … 128 128 129 129 VarLengthColumn zdef; 130 if (!gfits_varlength_column_define (ftable, &zdef, 1)) ESCAPE(A); 131 132 // allocate the working buffers to the max tile size 130 if (!gfits_varlength_column_define (ftable, &zdef, 1)) ESCAPE; 131 132 // pixel sizes and tile sizes: 133 // * We have Ntile tiles, each of Nx * Ny * Nz ... tile pixels 134 // -> A pixel in the tile (in the image) has size tile_pixsize = f(BITPIX) 135 // * We copy the pixels in the tile to a continuous buffer to be compressed. 136 // During this copy, we can scale the data (eg, floats -> ints), in which 137 // case the raw buffer may have a different pixel size (and type) than the 138 // image tile 139 // -> A pixel in the raw buffer is f(zcmptype,BITPIX): 140 // -- GZIP_1 : raw_pixsize = tile_pixsize (no scaling is performed) 141 // -- GZIP_2 : raw_pixsize = tile_pixsize (no scaling is performed) 142 // -- RICE_1 : raw pixels must be integers. raw_pixsize is user specified? (ZNAMEn,ZVALn : BYTEPIX = 1,2,4,8) 143 // -- PLIO_1 : raw_pixsize = 2 bytes 144 // -- HCOMPRESS_1 : raw pixels must be integers. 145 146 // size (in pixels) of the largest tile 133 147 int max_tile_size = gfits_imtile_maxsize (matrix, ztile); 134 148 135 // size of a pixel in the raw image tile: 136 int raw_pixsize = abs(header[0].bitpix) / 8; 137 138 // size of a pixel in the raw image tile: 139 int tile_pixsize = gfits_uncompressed_data_pixsize (zcmptype, header[0].bitpix, NULL, NULL, 0); 140 149 // size of a pixel in the image tile -- this is probably not needed 150 int tile_pixsize = abs(header[0].bitpix) / 8; 151 152 // size of a pixel in the raw pixel buffer (before compression) 153 int raw_pixsize = gfits_uncompressed_data_pixsize (zcmptype, header[0].bitpix, NULL, NULL, 0); 154 int raw_bitpix = gfits_uncompressed_data_bitpix (zcmptype, header[0].bitpix, NULL, NULL, 0); 155 int cmp_pixsize = gfits_compressed_data_pixsize (zcmptype, header[0].bitpix, NULL, NULL, 0); 156 157 fprintf (stderr, "raw_pixsize: %d, cmp_pixsize: %d, tile_pixsize: %d, raw_bitpix: %d\n", 158 raw_pixsize, cmp_pixsize, tile_pixsize, raw_bitpix); 159 160 // allocate the buffer for compression work 141 161 int Nzdata_alloc = raw_pixsize*max_tile_size + 100; 142 162 ALLOCATE (raw, char, raw_pixsize*max_tile_size); … … 144 164 145 165 // init the otile[] counters 146 if (!gfits_imtile_start (matrix, otile)) ESCAPE (A);166 if (!gfits_imtile_start (matrix, otile)) ESCAPE; 147 167 148 168 // compress the data : copy into a tile, compress the tile, then add to the output table … … 150 170 for (i = 0; i < Ntile; i++) { 151 171 152 // size of the current tile 172 // size of the current tile in pixels 153 173 int Nraw = gfits_tile_size (matrix, otile, ztile); 154 174 155 175 // copy the raw pixels from their native matrix locations to the temporary output buffer 156 if (!gfits_collect_data (matrix, raw, Nraw, raw_pixsize, otile, oblank, ztile, zblank, zscale, zzero)) ESCAPE(A); 157 176 // for float -> int scaling by zscale, zzero may be applied 177 if (!gfits_collect_data (matrix, raw, Nraw, raw_bitpix, otile, oblank, ztile, zblank, zscale, zzero)) ESCAPE; 178 179 if (0 && (i == 0)) { 180 int k; 181 fprintf (stderr, "cmp mat: "); 182 for (k = 0; k < 32; k++) { fprintf (stderr, "0x%02hhx ", matrix->buffer[k]); } 183 fprintf (stderr, "\n"); 184 fprintf (stderr, "cmp raw: "); 185 for (k = 0; k < 32; k++) { fprintf (stderr, "0x%02hhx ", raw[k]); } 186 fprintf (stderr, "\n"); 187 } 188 189 // gzip compresses bytes which are in BIG-ENDIAN order 158 190 if (!strcasecmp(zcmptype, "GZIP_1")) { 159 if (!gfits_byteswap_zdata (raw, Nraw, raw_pixsize)) ESCAPE(A); 160 } 161 162 // XXX the tile must not be > 2GB 163 164 // optname, optvalue = NULL, Noptions = 0 191 if (!gfits_byteswap_zdata (raw, Nraw, raw_pixsize)) ESCAPE; 192 } 193 if (0 && (i == 0)) { 194 int k; 195 fprintf (stderr, "cmp swp: "); 196 for (k = 0; k < 32; k++) { fprintf (stderr, "0x%02hhx ", raw[k]); } 197 fprintf (stderr, "\n"); 198 } 199 200 // optname, optvalue = NULL, Noptions = 0 : defaults for RICE_1, HCOMPRESS_1 165 201 166 202 int Nzdata = Nzdata_alloc; // available space, replaced with actual output size on compression 167 if (!gfits_compress_data (zdata, &Nzdata, zcmptype, NULL, NULL, 0, raw, Nraw, tile_pixsize)) ESCAPE(A); 168 169 if (!gfits_varlength_column_add_data (ftable, zdata, Nzdata, i, &zdef)) ESCAPE(A); 170 171 // XXX need to activate 172 // if (FALSE) gfits_byteswap_zdata (zdata, Nzdata, zdata_pixsize) ESCAPE(A); 203 if (!gfits_compress_data (zdata, &Nzdata, zcmptype, NULL, NULL, 0, raw, Nraw, raw_pixsize, ztile[1], ztile[0])) ESCAPE; 204 if (0 && (i == 0)) { 205 int k; 206 fprintf (stderr, "cmp dat: "); 207 for (k = 0; k < 32; k++) { fprintf (stderr, "0x%02hhx ", zdata[k]); } 208 fprintf (stderr, "\n"); 209 } 210 211 if (!gfits_varlength_column_add_data (ftable, zdata, Nzdata, i, &zdef)) ESCAPE; 212 213 // all other compression modes require swapping after compression 214 // (compresssion & decompression operate on native ENDIAN) 215 if (strcasecmp(zcmptype, "GZIP_1")) { 216 if (!gfits_byteswap_zdata (zdata, Nzdata, cmp_pixsize)) ESCAPE; 217 } 173 218 174 219 // update the otile[] counters, carrying to the next dimension if needed … … 176 221 } 177 222 178 if (!gfits_varlength_column_finish (ftable, &zdef)) ESCAPE(A); 223 if (!gfits_varlength_column_finish (ftable, &zdef)) ESCAPE; 224 if (0) { 225 int k; 226 fprintf (stderr, "cmp tbl: "); 227 for (k = 0; k < 32; k++) { fprintf (stderr, "0x%02hhx ", ftable->buffer[k]); } 228 fprintf (stderr, "\n"); 229 } 179 230 180 231 free (raw); … … 209 260 // raw_pixsize is the bytes / pixel for the input matrix 210 261 // place the raw image bytes for the current tile into the tile buffer 211 int gfits_collect_data (Matrix *matrix, char *raw, int Nraw, int raw_pixsize, int *otile, int oblank, int *ztile, int zblank, float zscale, float zzero) { 262 // * otile is the counter for the current output tile 263 // * ztile gives the size of the i-th dimension of the current tile 264 // * raw_bitpix defines the size and type of the raw buffer 265 int gfits_collect_data (Matrix *matrix, char *raw, int Nraw, int raw_bitpix, int *otile, int oblank, int *ztile, int zblank, float zscale, float zzero) { 212 266 213 267 int i, j, start, offset, coord, Nline; … … 246 300 // we need to set up switches for all of the possible combinations: 247 301 // this macro is used at the inner switch to run the actual loop 248 // note we copy Optr to Iptr 249 # define SCALE_AND_DIST(TYPE, SIZE) { \ 250 TYPE *Optr = (TYPE *) &matrix->buffer[SIZE*(offset + start)]; \ 251 for (j = 0; j < Ztile[0]; j++, Iptr++, Optr++) { \ 252 if (*Optr == oblank) { \ 253 *Iptr = zblank; \ 302 # define SCALE_AND_DIST_INT(TYPE, SIZE) { \ 303 TYPE *TILEptr = (TYPE *) &matrix->buffer[SIZE*(offset + start)]; \ 304 for (j = 0; j < Ztile[0]; j++, TILEptr++, RAWptr++) { \ 305 if (*TILEptr == oblank) { \ 306 *RAWptr = zblank; \ 254 307 } else { \ 255 * Iptr = (*Optr - zzero) / zscale; \308 *RAWptr = (*TILEptr - zzero) / zscale; \ 256 309 } } } 257 310 258 311 // we need to set up switches for all of the possible combinations: 259 312 // this macro is used at the inner switch to run the actual loop 260 // note we copy Optr to Iptr 261 # define SCALE_AND_DIST_FLOAT(TYPE, SIZE) { \ 262 TYPE *Optr = (TYPE *) &matrix->buffer[SIZE*(offset + start)]; \ 263 for (j = 0; j < Ztile[0]; j++, Iptr++, Optr++) { \ 264 if (!isfinite(*Optr)) { \ 265 *Iptr = zblank; \ 313 # define SCALE_AND_DIST_FLOAT(TYPE, SIZE) { \ 314 TYPE *TILEptr = (TYPE *) &matrix->buffer[SIZE*(offset + start)]; \ 315 for (j = 0; j < Ztile[0]; j++, TILEptr++, RAWptr++) { \ 316 if (!isfinite(*TILEptr)) { \ 317 *RAWptr = zblank; \ 266 318 } else { \ 267 * Iptr = (*Optr - zzero) / zscale; \319 *RAWptr = (*TILEptr - zzero) / zscale; \ 268 320 } } } 269 321 270 // this macro sets up the outer switch and calls above macro with all output bitpix options 271 # define SETUP_INSIZE(TYPE, SIZE) { \ 272 TYPE *Iptr = (TYPE *) &raw[i*SIZE*Ztile[0]]; \ 322 // this macro sets up the outer switch and calls above the macro with all RAW buffer bitpix options 323 // 324 # define SETUP_RAWSIZE(TYPE, SIZE) { \ 325 TYPE *RAWptr = (TYPE *) &raw[i*SIZE*Ztile[0]]; \ 273 326 switch (matrix->bitpix) { \ 274 case 8: \ 275 SCALE_AND_DIST (char, 1); \ 276 break; \ 277 case 16: \ 278 SCALE_AND_DIST (short, 2); \ 279 break; \ 280 case 32: \ 281 SCALE_AND_DIST (int, 4); \ 282 break; \ 283 case -32: \ 284 SCALE_AND_DIST_FLOAT (float, 4); \ 285 break; \ 286 case -64: \ 287 SCALE_AND_DIST_FLOAT (double, 8); \ 288 break; \ 289 default: \ 290 abort(); \ 327 case 8: SCALE_AND_DIST_INT (char, 1); break; \ 328 case 16: SCALE_AND_DIST_INT (short, 2); break; \ 329 case 32: SCALE_AND_DIST_INT (int, 4); break; \ 330 case -32: SCALE_AND_DIST_FLOAT (float, 4); break; \ 331 case -64: SCALE_AND_DIST_FLOAT (double, 8); break; \ 332 default: abort(); \ 291 333 } } 292 334 293 335 // loop over lines in the tile 294 336 for (i = 0; i < Nline; i++) { 295 switch (raw_pixsize) { 296 case 1: 297 SETUP_INSIZE (char, 1); 298 break; 299 case 2: 300 SETUP_INSIZE (short, 2); 301 break; 302 case 4: 303 SETUP_INSIZE (int, 4); 304 break; 305 case 8: 306 SETUP_INSIZE (double, 8); 307 break; 308 default: 309 abort(); 337 switch (raw_bitpix) { 338 case 8: SETUP_RAWSIZE (char, 1); break; 339 case 16: SETUP_RAWSIZE (short, 2); break; 340 case 32: SETUP_RAWSIZE (int, 4); break; 341 case -32: SETUP_RAWSIZE (float, 4); break; 342 case -64: SETUP_RAWSIZE (double, 8); break; 343 default: abort(); 310 344 } 311 345 -
branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_data.c
r38340 r38355 5 5 /* functions defined in ricecomp.c */ 6 6 int fits_rcomp(int a[], int nx, unsigned char *c, int clen, int nblock); 7 int fits_rcomp_short(short a[], int nx, unsigned char *c, int clen, int nblock); 8 int fits_rcomp_byte(char a[], int nx, unsigned char *c, int clen, int nblock); 7 9 int fits_rdecomp (unsigned char *c, int clen, unsigned int array[], int nx, int nblock); 8 10 int fits_rdecomp_short (unsigned char *c, int clen, unsigned short array[], int nx, int nblock); … … 29 31 # define ESCAPE(RET) { fprintf (stderr, "gzip error in %s @ %s:%d\n", __func__, __FILE__, __LINE__); return (RET); } 30 32 33 // XXX I'm putting Nx and Ny on the argument list -- only used by hcompress 34 // with a little work, these could go on the option list 31 35 int gfits_compress_data (char *zdata, int *Nzdata, char *cmptype, 32 36 char **optname, char **optvalue, int Nopt, 33 char *rawdata, int Nrawpix, int rawpix_size) { 37 char *rawdata, int Nrawpix, int rawpix_size, 38 int Nx, int Ny) { 34 39 40 int Nout; 35 41 int status; 36 42 … … 59 65 } 60 66 61 # if (0)62 67 if (!strcasecmp(cmptype, "RICE_1")) { 63 68 int i, blocksize; … … 74 79 } 75 80 76 int Npix; 77 // Npix = *Nout * (out_pixsize / 4.0); 78 Npix = *Nout; 79 80 Ninsum += Nzdata; 81 Noutsum += *Nout; 81 // Ninsum += Nzdata; 82 // Noutsum += *Nout; 82 83 // fprintf (stderr, "%d comp bytes; %d uncomp 'pixels', totals: %d %d\n", Nzdata, Npix, Ninsum, Noutsum); 83 84 84 switch ( out_pixsize) {85 switch (rawpix_size) { 85 86 case 4: 86 // rice decompression from the CFITSIO source tree : we need to tell it the expected number of pixels 87 // is also REQUIRES 4byte output, which is fairly stupid. 88 if (fits_rdecomp ((unsigned char *) zdata, Nzdata, (unsigned int *) outdata, Npix, blocksize)) { 89 fprintf (stderr, "error in rice decompression\n"); 90 return (FALSE); 91 } 92 return (TRUE); 87 // rice compression from the CFITSIO source tree 88 Nout = fits_rcomp ((int *) rawdata, Nrawpix, (unsigned char *) zdata, *Nzdata, blocksize); 89 break; 93 90 94 91 case 2: 95 if (fits_rdecomp_short ((unsigned char *) zdata, Nzdata, (unsigned short *) outdata, Npix, blocksize)) { 96 fprintf (stderr, "error in rice decompression\n"); 97 return (FALSE); 98 } 99 return (TRUE); 92 Nout = fits_rcomp_short ((short *) rawdata, Nrawpix, (unsigned char *) zdata, *Nzdata, blocksize); 93 break; 100 94 101 95 case 1: 102 if (fits_rdecomp_byte ((unsigned char *) zdata, Nzdata, (unsigned char *) outdata, Npix, blocksize)) { 103 fprintf (stderr, "error in rice decompression\n"); 104 return (FALSE); 105 } 106 return (TRUE); 96 Nout = fits_rcomp_byte ((char *) rawdata, Nrawpix, (unsigned char *) zdata, *Nzdata, blocksize); 97 break; 107 98 108 99 default: 109 fprintf (stderr, "invalid output pixel size %d\n", out_pixsize); 100 fprintf (stderr, "invalid output pixel size %d\n", rawpix_size); 101 *Nzdata = 0; 110 102 return (FALSE); 111 103 } 112 104 if (Nout < 0) { 105 fprintf (stderr, "error in rice decompression\n"); 106 *Nzdata = 0; 107 return (FALSE); 108 } 109 *Nzdata = Nout; 110 return TRUE; 113 111 } 114 112 115 113 if (!strcasecmp(cmptype, "PLIO_1")) { 116 int Npix; 117 Npix = pl_l2pi ((short *) zdata, 1, (int *) outdata, *Nout); 118 if (Npix != *Nout) { 119 fprintf (stderr, "error in plio decompression\n"); 120 return (FALSE); 121 } 114 // note the fortan starting point: zdata is decremented at start 115 Nout = pl_p2li ((int *) rawdata, 1, (short *) zdata, Nrawpix); 116 *Nzdata = Nout; 117 if (!Nout) return (FALSE); 122 118 return (TRUE); 123 119 } 124 120 125 121 if (!strcasecmp(cmptype, "HCOMPRESS_1")) { 126 int Nx, Ny, scale; 122 long Nbytes = Nrawpix * rawpix_size; 123 int scale = 0; 127 124 status = 0; 128 125 // call hdecompress without smoothing 129 fits_hdecompress ((unsigned char *) zdata, FALSE, (int *) outdata, &Nx, &Ny, &scale, &status); 126 127 if (rawpix_size == 4) { 128 fits_hcompress ((int *) rawdata, Nx, Ny, scale, (char *) zdata, &Nbytes, &status); 129 } 130 if (rawpix_size == 8) { 131 fits_hcompress64 ((long long *) rawdata, Nx, Ny, scale, (char *) zdata, &Nbytes, &status); 132 } 133 130 134 if (status) { 131 135 fprintf (stderr, "error in hdecompress\n"); … … 134 138 // fprintf (stderr, "decompression yields image %d x %d (scale: %d)\n", Nx, Ny, scale); 135 139 136 if (Nx * Ny != *Nout) { 137 fprintf (stderr, "error in hdecompress: mismatched output size\n"); 138 return (FALSE); 139 } 140 *Nzdata = Nbytes / rawpix_size; 140 141 return (TRUE); 141 142 } 142 # endif143 143 144 144 fprintf (stderr, "unknown compression %s\n", cmptype); -
branches/eam_branches/ohana.20150429/src/libfits/matrix/F_compress_utils.c
r38340 r38355 174 174 int i, Nbyte; 175 175 176 if (!strcasecmp(cmptype, "GZIP_1") || !strcasecmp(cmptype, "NONE")) { 177 if (out_bitpix == 8) return (1); 178 if (out_bitpix == 16) return (2); 179 if (out_bitpix == 32) return (4); 176 // GZIP_1, GZIP_2 use matched input & output pixels 177 if (!strcasecmp(cmptype, "GZIP_1") || 178 !strcasecmp(cmptype, "GZIP_2") || 179 !strcasecmp(cmptype, "NONE")) { 180 if (out_bitpix == 8) return (1); 181 if (out_bitpix == 16) return (2); 182 if (out_bitpix == 32) return (4); 180 183 if (out_bitpix == -32) return (4); 181 184 if (out_bitpix == -64) return (8); 182 185 return (1); 183 186 } 184 if (!strcasecmp(cmptype, "RICE_1")) { 185 187 188 // RICE_1 must operate on integer pixels 189 if (!strcasecmp(cmptype, "RICE_1") || 190 !strcasecmp(cmptype, "RICE_ONE")) { 186 191 // if BYTEPIX option is specified, use that for Nbyte 187 192 for (i = 0; i < Noptions; i++) { … … 191 196 } 192 197 } 198 if (out_bitpix == 8) return (1); 199 if (out_bitpix == 16) return (2); 200 if (out_bitpix == 32) return (4); 193 201 return (4); 194 202 } 203 204 // PLIO_1 always results in 4-byte ints (not 2 byte ints?) 195 205 if (!strcasecmp(cmptype, "PLIO_1")) { 196 206 return (4); 197 207 } 208 198 209 if (!strcasecmp(cmptype, "HCOMPRESS_1")) { 199 210 if (out_bitpix == 8) return (4); … … 204 215 } 205 216 217 int gfits_compressed_data_pixsize (char *cmptype, int out_bitpix, char **optname, char **optvalue, int Noptions) { 218 219 int i, Nbyte; 220 221 // GZIP_1, GZIP_2 should not be swapped after compression 222 if (!strcasecmp(cmptype, "GZIP_1") || 223 !strcasecmp(cmptype, "GZIP_2") || 224 !strcasecmp(cmptype, "NONE")) { 225 if (out_bitpix == 8) return (1); 226 if (out_bitpix == 16) return (1); 227 if (out_bitpix == 32) return (1); 228 if (out_bitpix == -32) return (1); 229 if (out_bitpix == -64) return (1); 230 return (1); 231 } 232 233 // RICE_1 must operate on integer pixels 234 if (!strcasecmp(cmptype, "RICE_1") || 235 !strcasecmp(cmptype, "RICE_ONE")) { 236 // if BYTEPIX option is specified, use that for Nbyte 237 for (i = 0; i < Noptions; i++) { 238 if (!strcmp(optname[i], "BYTEPIX")) { 239 Nbyte = atoi (optvalue[i]); 240 return (Nbyte); 241 } 242 } 243 if (out_bitpix == 8) return (1); 244 if (out_bitpix == 16) return (2); 245 if (out_bitpix == 32) return (4); 246 return (4); 247 } 248 249 // PLIO_1 always results in 4-byte ints (not 2 byte ints?) 250 if (!strcasecmp(cmptype, "PLIO_1")) { 251 return (4); 252 } 253 254 if (!strcasecmp(cmptype, "HCOMPRESS_1")) { 255 if (out_bitpix == 8) return (4); 256 if (out_bitpix == 16) return (4); 257 return (8); 258 } 259 return (0); 260 } 261 262 int gfits_uncompressed_data_bitpix (char *cmptype, int out_bitpix, char **optname, char **optvalue, int Noptions) { 263 264 int i, Nbyte; 265 266 // GZIP_1, GZIP_2 use matched input & output pixels 267 if (!strcasecmp(cmptype, "GZIP_1") || 268 !strcasecmp(cmptype, "GZIP_2") || 269 !strcasecmp(cmptype, "NONE")) { 270 return (out_bitpix); 271 } 272 273 // RICE_1 must operate on integer pixels 274 if (!strcasecmp(cmptype, "RICE_1") || 275 !strcasecmp(cmptype, "RICE_ONE")) { 276 // if BYTEPIX option is specified, use that for Nbyte 277 for (i = 0; i < Noptions; i++) { 278 if (!strcmp(optname[i], "BYTEPIX")) { 279 Nbyte = atoi (optvalue[i]); 280 return (Nbyte*8); 281 } 282 } 283 if (out_bitpix == 8) return (out_bitpix); 284 if (out_bitpix == 16) return (out_bitpix); 285 if (out_bitpix == 32) return (out_bitpix); 286 return (32); 287 } 288 289 // PLIO_1 always results in 4-byte ints (not 2 byte ints?) 290 if (!strcasecmp(cmptype, "PLIO_1")) { 291 return (32); 292 } 293 294 if (!strcasecmp(cmptype, "HCOMPRESS_1")) { 295 if (out_bitpix == 8) return (32); 296 if (out_bitpix == 16) return (32); 297 return (64); 298 } 299 return (0); 300 } 301 206 302 int gfits_vartable_heap_pixsize (char format) { 207 303 -
branches/eam_branches/ohana.20150429/src/libfits/matrix/F_uncompress_M.c
r38340 r38355 46 46 float zscale, zzero; 47 47 48 int zdata_pixsize, odata_pixsize;49 50 48 int *ztile = NULL; 51 49 int *otile = NULL; … … 242 240 243 241 // size of an element in the vartable heap section 244 zdata_pixsize = gfits_vartable_heap_pixsize (zdef.format);242 // zdata_pixsize = gfits_vartable_heap_pixsize (zdef.format); XXX 245 243 246 244 // size of a pixel in the final image (not needed) … … 248 246 249 247 // size of a pixel in the output from the decompression routine 250 odata_pixsize = gfits_uncompressed_data_pixsize (cmptype, header[0].bitpix, optname, optvalue, Noptions); 251 252 int Nout_alloc = odata_pixsize*max_tile_size; 248 int cmp_pixsize = gfits_compressed_data_pixsize (cmptype, header[0].bitpix, optname, optvalue, Noptions); 249 int raw_bitpix = gfits_uncompressed_data_bitpix (cmptype, header[0].bitpix, optname, optvalue, Noptions); 250 int raw_pixsize = gfits_uncompressed_data_pixsize (cmptype, header[0].bitpix, optname, optvalue, Noptions); 251 252 int Nout_alloc = raw_pixsize*max_tile_size; 253 253 ALLOCATE (out, char, Nout_alloc); 254 254 255 255 off_t row; 256 257 if (0) { 258 int k; 259 fprintf (stderr, "unc tbl: "); 260 for (k = 0; k < 32; k++) { fprintf (stderr, "0x%02hhx ", ftable->buffer[k]); } 261 fprintf (stderr, "\n"); 262 } 256 263 257 264 // uncompress the data … … 266 273 if (!zdata) ESCAPE; 267 274 268 // XXX not certain this is the correct place to do the swap (or if zdata_pixsize is269 // the correct size to guide the swap)270 271 275 if (strcasecmp(cmptype, "GZIP_1")) { 272 if (!gfits_byteswap_zdata (zdata, Nzdata, zdata_pixsize)) ESCAPE; 276 if (!gfits_byteswap_zdata (zdata, Nzdata, cmp_pixsize)) ESCAPE; 277 } 278 279 if (0 && (row == 0)) { 280 int k; 281 fprintf (stderr, "unc dat: "); 282 for (k = 0; k < 32; k++) { fprintf (stderr, "0x%02hhx ", zdata[k]); } 283 fprintf (stderr, "\n"); 273 284 } 274 285 275 286 // gfits_uncompress_data uncompresses from zdata to the temporary output buffer which must be allocated 276 287 // XXX the tile must not be > 2GB 277 if (!gfits_uncompress_data ((char *)zdata, Nzdata, cmptype, optname, optvalue, Noptions, out, &Nout, odata_pixsize)) ESCAPE; 278 288 if (!gfits_uncompress_data ((char *)zdata, Nzdata, cmptype, optname, optvalue, Noptions, out, &Nout, raw_pixsize)) ESCAPE; 289 290 if (0 && (row == 0)) { 291 int k; 292 fprintf (stderr, "unc swp: "); 293 for (k = 0; k < 32; k++) { fprintf (stderr, "0x%02hhx ", out[k]); } 294 fprintf (stderr, "\n"); 295 } 296 279 297 if (!strcasecmp(cmptype, "GZIP_1")) { 280 if (!gfits_byteswap_zdata (out, Nout, odata_pixsize)) ESCAPE; 298 if (!gfits_byteswap_zdata (out, Nout, raw_pixsize)) ESCAPE; 299 } 300 301 if (0 && (row == 0)) { 302 int k; 303 fprintf (stderr, "unc raw: "); 304 for (k = 0; k < 32; k++) { fprintf (stderr, "0x%02hhx ", out[k]); } 305 fprintf (stderr, "\n"); 281 306 } 282 307 283 308 // copy the uncompressed pixels into their correct locations 284 if (!gfits_distribute_data (matrix, odata_pixsize, out, Nout, otile, oblank, ztile, zblank, zscale, zzero)) ESCAPE; 309 if (!gfits_distribute_data (matrix, out, Nout, raw_bitpix, otile, oblank, ztile, zblank, zscale, zzero)) ESCAPE; 310 311 if (0 && (row == 0)) { 312 int k; 313 fprintf (stderr, "unc mat: "); 314 for (k = 0; k < 32; k++) { fprintf (stderr, "0x%02hhx ", matrix->buffer[k]); } 315 fprintf (stderr, "\n"); 316 } 285 317 286 318 // update the tile counters, carrying to the next dimension if needed … … 311 343 312 344 // bitpix is the input data size/type 313 int gfits_distribute_data (Matrix *matrix, int bitpix, char *data, int Ndata, int *otile, int oblank, int *ztile, int zblank, float zscale, float zzero) {345 int gfits_distribute_data (Matrix *matrix, char *raw, int Nraw, int raw_bitpix, int *otile, int oblank, int *ztile, int zblank, float zscale, float zzero) { 314 346 315 347 int i, j, start, offset, coord, Nline; … … 334 366 335 367 // double check reported size (needs the Isize) 336 assert (N data== Ztile[0]*Nline);368 assert (Nraw == Ztile[0]*Nline); 337 369 338 370 // set the starting point of the tile: … … 350 382 // we need to set up switches for all of the possible combinations: 351 383 // this macro is used at the inner switch to run the actual loop 352 # define SCALE_AND_DIST (TYPE, SIZE) {\353 TYPE * Optr = (TYPE *) &matrix->buffer[SIZE*(offset + start)];\354 for (j = 0; j < Ztile[0]; j++, Iptr++, Optr++) {\355 if (* Iptr == zblank) {\356 *Optr = oblank;\357 } else { \358 *Optr = *Iptr * zscale + zzero;\384 # define SCALE_AND_DIST_INT(TYPE, SIZE) { \ 385 TYPE *TILEptr = (TYPE *) &matrix->buffer[SIZE*(offset + start)]; \ 386 for (j = 0; j < Ztile[0]; j++, TILEptr++, RAWptr++) { \ 387 if (*TILEptr == zblank) { \ 388 *TILEptr = oblank; \ 389 } else { \ 390 *TILEptr = *RAWptr * zscale + zzero; \ 359 391 } } } 360 392 393 // we need to set up switches for all of the possible combinations: 394 // this macro is used at the inner switch to run the actual loop 395 # define SCALE_AND_DIST_FLOAT(TYPE, SIZE) { \ 396 TYPE *TILEptr = (TYPE *) &matrix->buffer[SIZE*(offset + start)]; \ 397 for (j = 0; j < Ztile[0]; j++, TILEptr++, RAWptr++) { \ 398 if (!isfinite(*TILEptr)) { \ 399 *TILEptr = oblank; \ 400 } else { \ 401 *TILEptr = *RAWptr * zscale + zzero; \ 402 } } } 403 361 404 // this macro sets up the outer switch and calls above macro with all output bitpix options 362 # define SETUP_INSIZE(TYPE, SIZE) { \ 363 TYPE *Iptr = (TYPE *) &data[i*SIZE*Ztile[0]]; \ 364 switch (matrix->bitpix) { \ 365 case 8: \ 366 SCALE_AND_DIST (char, 1); \ 367 break; \ 368 case 16: \ 369 SCALE_AND_DIST (short, 2); \ 370 break; \ 371 case 32: \ 372 SCALE_AND_DIST (int, 4); \ 373 break; \ 374 case -32: \ 375 SCALE_AND_DIST (float, 4); \ 376 break; \ 377 case -64: \ 378 SCALE_AND_DIST (double, 8); \ 379 break; \ 380 default: \ 381 abort(); \ 405 # define SETUP_RAWSIZE(TYPE, SIZE) { \ 406 TYPE *RAWptr = (TYPE *) &raw[i*SIZE*Ztile[0]]; \ 407 switch (matrix->bitpix) { \ 408 case 8: SCALE_AND_DIST_INT (char, 1); break; \ 409 case 16: SCALE_AND_DIST_INT (short, 2); break; \ 410 case 32: SCALE_AND_DIST_INT (int, 4); break; \ 411 case -32: SCALE_AND_DIST_FLOAT (float, 4); break; \ 412 case -64: SCALE_AND_DIST_FLOAT (double, 8); break; \ 413 default: abort(); \ 382 414 } } 383 415 384 416 // loop over lines in the tile 385 417 for (i = 0; i < Nline; i++) { 386 switch (bitpix) { 387 case 1: 388 SETUP_INSIZE (char, 1); 389 break; 390 case 2: 391 SETUP_INSIZE (short, 2); 392 break; 393 case 4: 394 SETUP_INSIZE (int, 4); 395 break; 396 case 8: 397 SETUP_INSIZE (double, 8); 398 break; 399 default: 400 abort(); 418 switch (raw_bitpix) { 419 case 8: SETUP_RAWSIZE (char, 1); break; 420 case 16: SETUP_RAWSIZE (short, 2); break; 421 case 32: SETUP_RAWSIZE (int, 4); break; 422 case -32: SETUP_RAWSIZE (float, 4); break; 423 case -64: SETUP_RAWSIZE (double, 8); break; 424 default: abort(); 401 425 } 402 426 -
branches/eam_branches/ohana.20150429/src/libfits/table/F_compress_T.c
r38348 r38355 161 161 162 162 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 )) ESCAPE(A);163 if (!gfits_compress_data (zdata, &Nzdata, fields[j].zctype, NULL, NULL, 0, raw, Nraw, fields[j].pixsize, 0, 0)) ESCAPE(A); 164 164 165 165 if (!gfits_varlength_column_add_data (tgttable, zdata, Nzdata, i, &fields[j].zdef)) ESCAPE(A);
Note:
See TracChangeset
for help on using the changeset viewer.
