From 87d2142fff79f5d956f69f1b9f865cab2314c89b Mon Sep 17 00:00:00 2001 From: Chris Rorden Date: Mon, 24 Jul 2017 08:41:15 -0400 Subject: [PATCH] Fix for https://github.com/neurolabusc/dcm_qa/issues/5 Update OpenJPEG seek to work with modern versions of OpenJPEG, see https://github.com/uclouvain/openjpeg/issues/962 --- COMPILE.md | 11 ++++++++--- console/nii_dicom.cpp | 27 +++++++++++++++------------ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/COMPILE.md b/COMPILE.md index d79846cb..c5c8e06e 100644 --- a/COMPILE.md +++ b/COMPILE.md @@ -64,14 +64,19 @@ You should then be able to run then run: g++ -O3 -dead_strip -I. main_console.cpp nii_dicom.cpp nifti1_io_core.cpp nii_ortho.cpp nii_dicom_batch.cpp jpg_0XC3.cpp ujpeg.cpp nii_foreign.cpp -o dcm2niix -lopenjp2 ``` -But in my experience this works best if you explicitly tell the software how to find the libraries, so your compile will probably look like one of these two options: +But in my experience this works best if you explicitly tell the software how to find the libraries, so your compile will probably look like one of these options: ``` +#for MacOS g++ -O3 -dead_strip -I. main_console.cpp nii_dicom.cpp nifti1_io_core.cpp nii_ortho.cpp nii_dicom_batch.cpp jpg_0XC3.cpp ujpeg.cpp nii_foreign.cpp -o dcm2niix -I/usr/local/include/openjpeg-2.1 /usr/local/lib/libopenjp2.a ``` - ``` -g++ -O3 -dead_strip -I. main_console.cpp nii_dicom.cpp nifti1_io_core.cpp nii_ortho.cpp nii_dicom_batch.cpp jpg_0XC3.cpp ujpeg.cpp nii_foreign.cpp -o dcm2niix -I/usr/local/lib /usr/local/lib/libopenjp2.a +#For older Linux +g++ -O3 -I. main_console.cpp nii_dicom.cpp nifti1_io_core.cpp nii_ortho.cpp nii_dicom_batch.cpp jpg_0XC3.cpp ujpeg.cpp nii_foreign.cpp -o dcm2niix -I/usr/local/lib /usr/local/lib/libopenjp2.a +``` +``` +#For modern Linux +g++ -O3 -s -I. main_console.cpp nii_dicom.cpp nifti1_io_core.cpp nii_ortho.cpp nii_dicom_batch.cpp jpg_0XC3.cpp ujpeg.cpp nii_foreign.cpp -lpthread -o dcm2niix -I/usr/local/include/openjpeg-2.2 ~/openjpeg-master/build/bin/libopenjp2.a ``` If you want to build this with JPEG2000 decompression support using Jasper: You will need to have the Jasper (http://www.ece.uvic.ca/~frodo/jasper/) and libjpeg (http://www.ijg.org) libraries installed which for Linux users may be as easy as running 'sudo apt-get install libjasper-dev' (otherwise, see http://www.ece.uvic.ca/~frodo/jasper/#doc). You can then run: diff --git a/console/nii_dicom.cpp b/console/nii_dicom.cpp index 4bfd0e3b..2a017e4a 100644 --- a/console/nii_dicom.cpp +++ b/console/nii_dicom.cpp @@ -169,15 +169,26 @@ static OPJ_SIZE_T opj_skip_from_buffer(OPJ_SIZE_T p_nb_bytes, BufInfo * p_file) return (OPJ_SIZE_T)-1; } //opj_skip_from_buffer() +//fix for https://github.com/neurolabusc/dcm_qa/issues/5 static OPJ_BOOL opj_seek_from_buffer(OPJ_SIZE_T p_nb_bytes, BufInfo * p_file) { - if(p_file->cur + p_nb_bytes < p_file->buf + p_file->len ) { - p_file->cur += p_nb_bytes; + //printf("opj_seek_from_buffer %d + %d -> %d + %d\n", p_file->cur , p_nb_bytes, p_file->buf, p_file->len); + if ((p_file->buf + p_nb_bytes < p_file->buf + p_file->len ) && (p_nb_bytes >= 0)){ + p_file->cur = p_file->buf + p_nb_bytes; return OPJ_TRUE; } p_file->cur = p_file->buf + p_file->len; return OPJ_FALSE; } //opj_seek_from_buffer() +/*static OPJ_BOOL opj_seek_from_buffer(OPJ_SIZE_T p_nb_bytes, BufInfo * p_file) { + if((p_file->cur + p_nb_bytes) < (p_file->buf + p_file->len) ) { + p_file->cur += p_nb_bytes; + return OPJ_TRUE; + } + p_file->cur = p_file->buf + p_file->len; + return OPJ_FALSE; +} //opj_seek_from_buffer()*/ + opj_stream_t* opj_stream_create_buffer_stream(BufInfo* p_file, OPJ_UINT32 p_size, OPJ_BOOL p_is_read_stream) { opj_stream_t* l_stream; if(! p_file) return NULL; @@ -214,13 +225,6 @@ unsigned char * nii_loadImgCoreOpenJPEG(char* imgname, struct nifti_1_header hdr //DICOM JPEG2k is SUPPOSED to start with codestream, but some vendors include a header if (data[0] == 0xFF && data[1] == 0x4F && data[2] == 0xFF && data[3] == 0x51) format = OPJ_CODEC_J2K; opj_set_default_decoder_parameters(¶ms); - #if OPJ_VERSION_MAJOR == 2 - #if OPJ_VERSION_MINOR >= 1 - #if OPJ_VERSION_BUILD > 0 - #pragma message "\n\nYour OpenJPEG library version > 2.1.0, please make sure it's custom compiled with: -DOPJ_DISABLE_TPSOT_FIX=ON.\n" - #endif - #endif - #endif BufInfo dx; dx.buf = data; dx.cur = data; @@ -518,7 +522,6 @@ mat44 set_nii_header_x(struct TDICOMdata d, struct TDICOMdata d2, struct nifti_1 double nRowCol = ceil(sqrt((double) d.CSA.mosaicSlices)); double lFactorX = (d.xyzDim[1] -(d.xyzDim[1]/nRowCol) )/2.0; double lFactorY = (d.xyzDim[2] -(d.xyzDim[2]/nRowCol) )/2.0; - //printf("%g %g\n", lFactorX, lFactorY); Q44.m[0][3] =(float)((Q44.m[0][0]*lFactorX)+(Q44.m[0][1]*lFactorY)+Q44.m[0][3]); Q44.m[1][3] = (float)((Q44.m[1][0] * lFactorX) + (Q44.m[1][1] * lFactorY) + Q44.m[1][3]); Q44.m[2][3] = (float)((Q44.m[2][0] * lFactorX) + (Q44.m[2][1] * lFactorY) + Q44.m[2][3]); @@ -2256,7 +2259,7 @@ unsigned char * nii_loadImgJPEGC3(char* imgname, struct nifti_1_header hdr, stru // https://github.com/chafey/cornerstoneWADOImageLoader //I have never seen these segmented images in the wild, so we will simply warn the user if we encounter such a file //int Sz = JPEG_SOF_0XC3_sz (imgname, (dcm.imageStart - 4), dcm.isLittleEndian); - //printf("Sz %d %d\n", Sz, dcm.imageBytes ); + //printMessage("Sz %d %d\n", Sz, dcm.imageBytes ); //This behavior is legal but appears extremely rare //ftp://medical.nema.org/medical/dicom/final/cp900_ft.pdf if (65536 == dcm.imageBytes) @@ -3136,7 +3139,7 @@ struct TDICOMdata readDICOMv(char * fname, int isVerbose, int compressFlag, stru break; /*case kStackSliceNumber: { //https://github.com/Kevin-Mattheus-Moerman/GIBBON/blob/master/dicomDict/PMS-R32-dict.txt int stackSliceNumber = dcmInt(lLength,&buffer[lPos],d.isLittleEndian); - printf("%d\n",stackSliceNumber); + printMessage("StackSliceNumber %d\n",stackSliceNumber); break; }*/ case kNumberOfDynamicScans: