Skip to content

Commit

Permalink
Force gzip (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
neurolabusc committed Dec 21, 2024
1 parent 9ec8fff commit 85078ae
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
6 changes: 5 additions & 1 deletion src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ int nii_otsu(int* H, int nBin, int mode, int *dark, int *mid, int *bright) {
return thresh;
}

int nifti_save(nifti_image *nim, const char *postfix) {
int nifti_save(nifti_image *nim, const char *postfix, gzModes gzMode) {
char extnii[5] = ".nii"; /* modifiable, for possible uppercase */
char exthdr[5] = ".hdr";
char extimg[5] = ".img";
Expand Down Expand Up @@ -200,6 +200,10 @@ int nifti_save(nifti_image *nim, const char *postfix) {
#ifdef HAVE_ZLIB // if compression is requested, make sure of suffix
if ((value == NULL) || strstr(value, gzKey))
isGz = 1; //NIFTI2_GZ, NIFTI2_PAIR_GZ, NIFTI_GZ, NIFTI_PAIR_GZ
if (gzMode == GZ_FALSE)
isGz = 0;
if (gzMode == GZ_TRUE)
isGz = 1;
#endif
if ((value != NULL) && strstr(value, pairKey)) {
strcat(hname, exthdr);
Expand Down
7 changes: 6 additions & 1 deletion src/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ extern "C" {
//#include <immintrin.h>
#include <limits.h>

typedef enum {
GZ_ENVIRONMENT, // Use the environment settings
GZ_FALSE, // Uncompressed output
GZ_TRUE // Gzipped output
} gzModes;

//CORE32 and CORE64 handle Float32 and Float64 operations, CORE handles shared code

Expand Down Expand Up @@ -119,7 +124,7 @@ typedef struct { /** x4 vector struct **/
float v[4] ;
} vec4 ;

int nifti_save(nifti_image * nim, const char *postfix);
int nifti_save(nifti_image * nim, const char *postfix, gzModes gzMode);
nifti_image *nifti_image_read2( const char *hname , int read_data );
int * make_kernel_file(nifti_image * nim, int * nkernel, char * fin);
mat44 xform(nifti_image * nim);
Expand Down
40 changes: 25 additions & 15 deletions src/coreFLT.c
Original file line number Diff line number Diff line change
Expand Up @@ -2476,7 +2476,7 @@ staticx int nifti_tensor_2(nifti_image *nim, int lower2upper) {
return 0;
} // nifti_tensor_2()

staticx int nifti_tensor_decomp(nifti_image *nim, int isUpperTriangle) {
staticx int nifti_tensor_decomp(nifti_image *nim, int isUpperTriangle, gzModes gzMode) {
// MD= (Dxx+Dyy+Dzz)/3
//https://github.com/ANTsX/ANTs/wiki/Importing-diffusion-tensor-data-from-other-software
// dtifit produces upper-triangular order: xx xy xz yy yz zz
Expand Down Expand Up @@ -2561,17 +2561,17 @@ staticx int nifti_tensor_decomp(nifti_image *nim, int isUpperTriangle) {
//save V1
outv = out32 + (nvox3D * 3);
nim->data = (void *)outv;
nifti_save(nim, "_V1");
nifti_save(nim, "_V1", gzMode);
//save V2
outv = out32 + (nvox3D * 6);
//xmemcpy(fa32, outv, 3*nvox3D*sizeof(flt));
nim->data = (void *)outv;
nifti_save(nim, "_V2");
nifti_save(nim, "_V2", gzMode);
//save V3
outv = out32 + (nvox3D * 9);
//xmemcpy(fa32, outv, 3*nvox3D*sizeof(flt));
nim->data = (void *)outv;
nifti_save(nim, "_V3");
nifti_save(nim, "_V3", gzMode);
//release 4D memory
//free(dat);
//save 3D images
Expand All @@ -2587,26 +2587,26 @@ staticx int nifti_tensor_decomp(nifti_image *nim, int isUpperTriangle) {
//xmemcpy(fa32, outv, nvox3D*sizeof(flt));
nim->data = (void *)outv;
nim->cal_max = calmax(nim);
nifti_save(nim, "_L1");
nifti_save(nim, "_L1", gzMode);
//save L2
outv = out32 + (nvox3D * 1);
//xmemcpy(fa32, outv, nvox3D*sizeof(flt));
nim->data = (void *)outv;
nim->cal_max = calmax(nim);
nifti_save(nim, "_L2");
nifti_save(nim, "_L2", gzMode);
//save L3
outv = out32 + (nvox3D * 2);
//xmemcpy(fa32, outv, nvox3D*sizeof(flt));
nim->data = (void *)outv;
nim->cal_max = calmax(nim);
nifti_save(nim, "_L3");
nifti_save(nim, "_L3", gzMode);
//save MD
outv = out32 + (nvox3D * 13);
//xmemcpy(fa32, outv, nvox3D*sizeof(flt));
nim->data = (void *)outv;
nim->cal_min = calmin(nim);
nim->cal_max = calmax(nim);
nifti_save(nim, "_MD");
nifti_save(nim, "_MD", gzMode);
//single volume data
//void *dat = (void *)calloc(1, nvox3D * sizeof(flt));
void *dat = (void *)aligned_calloc(nvox3D * sizeof(flt));
Expand Down Expand Up @@ -2644,13 +2644,13 @@ staticx int nifti_tensor_decomp(nifti_image *nim, int isUpperTriangle) {
d = MAX(d, -1.0);
fa32[i] = d;
}
nifti_save(nim, "_MO");
nifti_save(nim, "_MO", gzMode);
//save FA
outv = out32 + (nvox3D * 12);
xmemcpy(fa32, outv, nvox3D * sizeof(flt));
nim->cal_min = 0;
nim->cal_max = 1;
nifti_save(nim, "_FA");
nifti_save(nim, "_FA", gzMode);
//keep FA in memory
nim->cal_max = 0;
_mm_free(out32);
Expand Down Expand Up @@ -4995,6 +4995,7 @@ staticx void nifti_compare(nifti_image *nim, char *fin, double thresh) {
exit(1);
#endif
}
gzModes gzMode = GZ_ENVIRONMENT;
int dtCalc = DT_FLOAT32; //data type for calculation
int dtOut = DT_FLOAT32; //data type for calculation
int ac = 1;
Expand Down Expand Up @@ -5022,7 +5023,7 @@ staticx void nifti_compare(nifti_image *nim, char *fin, double thresh) {
ac++;
if (nifti_set_filenames(nim, fout, 0, 1))
return 1;
nifti_save(nim, ""); //nifti_image_write( nim );
nifti_save(nim, "", gzMode); //nifti_image_write( nim );
nifti_image_free(nim);
return 0;
} //end pass through
Expand Down Expand Up @@ -5402,6 +5403,15 @@ staticx void nifti_compare(nifti_image *nim, char *fin, double thresh) {
ok = EXIT_FAILURE;
#endif
#endif
} else if (!strcmp(argv[ac], "-gz")) {
ac ++;
int mode = atoi(argv[ac]);
if (mode == 0)
gzMode = GZ_FALSE;
else if (mode == 1)
gzMode = GZ_TRUE;
else
gzMode = GZ_ENVIRONMENT;
} else if (!strcmp(argv[ac], "-h2c"))
ok = nifti_h2c(nim, false);
else if (!strcmp(argv[ac], "-c2h"))
Expand Down Expand Up @@ -5546,16 +5556,16 @@ staticx void nifti_compare(nifti_image *nim, char *fin, double thresh) {
} else if (!strcmp(argv[ac], "-tensor_2upper")) {
ok = nifti_tensor_2(nim, 1);
} else if (!strcmp(argv[ac], "-tensor_decomp")) {
ok = nifti_tensor_decomp(nim, 1);
ok = nifti_tensor_decomp(nim, 1, gzMode);
} else if (!strcmp(argv[ac], "-tensor_decomp_lower")) {
ok = nifti_tensor_decomp(nim, 0);
ok = nifti_tensor_decomp(nim, 0, gzMode);
} else if (!strcmp(argv[ac], "-save")) {
ac++;
char *fout2 = argv[ac];
if (nifti_set_filenames(nim, fout2, 1, 1))
ok = 1;
else {
nifti_save(nim, ""); //nifti_image_write( nim );
nifti_save(nim, "", gzMode); //nifti_image_write( nim );
nifti_set_filenames(nim, fout, 1, 1);
}
}
Expand Down Expand Up @@ -5691,7 +5701,7 @@ staticx void nifti_compare(nifti_image *nim, char *fin, double thresh) {
if (nifti_image_change_datatype(nim, dtOut, &ihdr) != 0)
return 1;
// if we get here, write the output dataset
nifti_save(nim, ""); //nifti_image_write( nim );
nifti_save(nim, "", gzMode); //nifti_image_write( nim );
// and clean up memory
nifti_image_free(nim);
if (kernel != NULL)
Expand Down
1 change: 1 addition & 0 deletions src/niimath.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ int show_help( void ) {
printf(" -edt : estimate Euler Distance Transform (distance field). Assumes isotropic input\n");
printf(" -close <thr> <dx1> <dx2> : morphological close that binarizes with `thr`, dilates with `dx1` and erodes with `dx2` (fills bubbles with `thr`)\n");
printf(" -floor : round voxels downwards to the nearest integer\n");
printf(" -gz <mode> : NIfTI gzip mode (0=uncompressed, 1=compressed, else FSL environment; default -1)\n");
printf(" -h2c : convert CT scans from 'Hounsfield' to 'Cormack' units to emphasize soft tissue contrast\n");
#ifdef NII2MESH
printf("\n");
Expand Down

0 comments on commit 85078ae

Please sign in to comment.