Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update OpenJPEG seek to work with modern versions of OpenJPEG, see uclouvain/openjpeg#962
  • Loading branch information
neurolabusc committed Jul 24, 2017
1 parent 23a32ce commit 87d2142
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
11 changes: 8 additions & 3 deletions COMPILE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 15 additions & 12 deletions console/nii_dicom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(&params);
#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;
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 87d2142

Please sign in to comment.