Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Hacking to get GDCM to work with libjpeg-turbo #5061

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Modules/ThirdParty/GDCM/itk-module.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ else()
ITKZLIB
ITKExpat
ITKOpenJPEG
ITKJPEG
DESCRIPTION
"${DOCUMENTATION}"
)
Expand Down
8 changes: 7 additions & 1 deletion Modules/ThirdParty/GDCM/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@ set(GDCM_USE_SYSTEM_ZLIB ON CACHE INTERNAL "")
set(ZLIB_INCLUDE_DIR ${ITKZLIB_INCLUDE_DIRS})
set(ZLIB_LIBRARY ${ITKZLIB_LIBRARIES})

# JPEG
set(LJPEG_LIBRARIES ${ITKJPEG_LIBRARIES})
set(LJPEG_INCLUDE_DIRS ${ITKJPEG_INCLUDE_DIRS})
message(STATUS "ITKJPEG_INCLUDE_DIRS: ${ITKJPEG_INCLUDE_DIRS}")


# Configure GDCM privately so its options do not appear to the user.
set(GDCM_LEGACY_REMOVE OFF CACHE INTERNAL "Remove all legacy code completely.")
set(GDCM_LEGACY_SILENT OFF CACHE INTERNAL "Silence all legacy code messages.")
set(GDCM_SUPPORT_BROKEN_IMPLEMENTATION ON CACHE INTERNAL "Handle broken DICOM")
set(GDCM_TEMP_DIRECTORY "${ITK_TEST_OUTPUT_DIR}" CACHE INTERNAL "Path to a valid temp directory")
set(GDCM_USE_KAKADU OFF CACHE INTERNAL "Use kakadu lib, only turn it on if you know what you are doing.")
set(GDCM_USE_PVRG OFF CACHE INTERNAL "Use pvrg lib, only turn it on if you know what you are doing.")
set(GDCM_USE_SYSTEM_LJPEG OFF CACHE INTERNAL "Use system ljpeg (ijg lib)")
set(GDCM_USE_SYSTEM_LJPEG ON CACHE INTERNAL "Use system ljpeg (ijg lib)")
set(GDCM_USE_SYSTEM_OPENSSL OFF CACHE INTERNAL "Use system OpenSSL")
set(GDCM_USE_SYSTEM_PODOFO OFF CACHE INTERNAL "Use system podofo (pdf)")
set(GDCM_USE_SYSTEM_POPPLER OFF CACHE INTERNAL "Use system poppler (pdf)")
Expand Down
2 changes: 1 addition & 1 deletion Modules/ThirdParty/GDCM/src/gdcm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ endif()
mark_as_advanced(GDCM_USE_SYSTEM_LIBXML2)

if(GDCM_USE_SYSTEM_LJPEG)
find_package(LJPEG REQUIRED)
#find_package(LJPEG REQUIRED)
set(GDCM_LJPEG_LIBRARIES ${LJPEG_LIBRARIES})
else()
set(GDCM_LJPEG_LIBRARIES gdcmjpeg8 gdcmjpeg12 gdcmjpeg16)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@

#include <limits.h>

#if !defined(LIBJPEG_TURBO_VERSION_NUMBER)
#define GDCM_JPEG_IS_LOSSLESS(CINFO) CINFO.process == JPROC_LOSSLESS
#define GDCM_JPEG_IS_SEQUENTIAL(CINFO) CINFO.process == JPROC_SEQUENTIAL
#define GDCM_JPEG_IS_PROGRESSIVE(CINFO) CINFO.process == JPROC_PROGRESSIVE
#else
// cinfo->master->lossless looks to be the right check for losslessness but the master structire is private
#define GDCM_JPEG_IS_LOSSLESS(CINFO) CINFO.data_precision == 12 || CINFO.data_precision == 16
#define GDCM_JPEG_IS_SEQUENTIAL(CINFO) CINFO.progressive_mode == FALSE
#define GDCM_JPEG_IS_PROGRESSIVE(CINFO) CINFO.progressive_mode == TRUE
#endif


/*
* jdatasrc.c
*
Expand Down Expand Up @@ -218,7 +230,7 @@ term_source (j_decompress_ptr cinfo)
* for closing it after finishing decompression.
*/

GLOBAL(void)
METHODDEF(void)
jpeg_stdio_src (j_decompress_ptr cinfo, std::istream & infile, bool flag)
{
my_src_ptr src;
Expand Down Expand Up @@ -463,8 +475,8 @@ bool JPEGBITSCodec::GetHeaderInfo(std::istream &is, TransferSyntax &ts)
{
assert( cinfo.num_components == 3 );
PI = PhotometricInterpretation::YBR_FULL_422;
if( cinfo.process == JPROC_LOSSLESS )
PI = PhotometricInterpretation::RGB; // wotsit ?
//if( cinfo.process == JPROC_LOSSLESS )
PI = PhotometricInterpretation::RGB; // wotsit ?
this->PF.SetSamplesPerPixel( 3 );
this->PlanarConfiguration = 1;
}
Expand All @@ -486,7 +498,7 @@ bool JPEGBITSCodec::GetHeaderInfo(std::istream &is, TransferSyntax &ts)
assert( 0 ); //TODO
}
}
if( cinfo.process == JPROC_LOSSLESS )
if( GDCM_JPEG_IS_LOSSLESS(cinfo) )
{
int predictor = cinfo.Ss;
/* not very user friendly... */
Expand All @@ -500,14 +512,14 @@ bool JPEGBITSCodec::GetHeaderInfo(std::istream &is, TransferSyntax &ts)
break;
}
}
else if( cinfo.process == JPROC_SEQUENTIAL )
else if( GDCM_JPEG_IS_SEQUENTIAL(cinfo) )
{
if( this->BitSample == 8 )
ts = TransferSyntax::JPEGBaselineProcess1;
else if( this->BitSample == 12 )
ts = TransferSyntax::JPEGExtendedProcess2_4;
}
else if( cinfo.process == JPROC_PROGRESSIVE )
else if( GDCM_JPEG_IS_PROGRESSIVE(cinfo) )
{
if( this->BitSample == 8 )
{
Expand All @@ -528,7 +540,7 @@ bool JPEGBITSCodec::GetHeaderInfo(std::istream &is, TransferSyntax &ts)
assert(0); // TODO
return false;
}
if( cinfo.process == JPROC_LOSSLESS )
if( GDCM_JPEG_IS_LOSSLESS(cinfo) )
{
LossyFlag = false;
}
Expand Down Expand Up @@ -764,6 +776,7 @@ bool JPEGBITSCodec::DecodeByStreams(std::istream &is, std::ostream &os)
// First of all are we using the proper JPEG decoder (correct bit sample):
if( jerr.pub.num_warnings )
{
#ifdef JWRN_MUST_DOWNSCALE
// PHILIPS_Gyroscan-12-MONO2-Jpeg_Lossless.dcm
if ( jerr.pub.msg_code == JWRN_MUST_DOWNSCALE )
{
Expand All @@ -781,6 +794,9 @@ bool JPEGBITSCodec::DecodeByStreams(std::istream &is, std::ostream &os)
{
assert( 0 );
}
#else
assert(0);
#endif
}
// Let's check the color space:
// JCS_UNKNOWN -> 0
Expand Down Expand Up @@ -824,7 +840,7 @@ bool JPEGBITSCodec::DecodeByStreams(std::istream &is, std::ostream &os)
break;
case JCS_RGB:
//assert( GetPhotometricInterpretation() == PhotometricInterpretation::RGB );
if ( cinfo.process == JPROC_LOSSLESS )
if ( GDCM_JPEG_IS_LOSSLESS(cinfo) )
{
cinfo.jpeg_color_space = JCS_UNKNOWN;
cinfo.out_color_space = JCS_UNKNOWN;
Expand All @@ -851,7 +867,7 @@ bool JPEGBITSCodec::DecodeByStreams(std::istream &is, std::ostream &os)
cinfo.jpeg_color_space = JCS_UNKNOWN;
cinfo.out_color_space = JCS_UNKNOWN;
}
if ( cinfo.process == JPROC_LOSSLESS )
if ( GDCM_JPEG_IS_LOSSLESS(cinfo) )
{
//cinfo.jpeg_color_space = JCS_UNKNOWN;
//cinfo.out_color_space = JCS_UNKNOWN;
Expand All @@ -867,14 +883,14 @@ bool JPEGBITSCodec::DecodeByStreams(std::istream &is, std::ostream &os)
break;
case JCS_CMYK:
assert( GetPhotometricInterpretation() == PhotometricInterpretation::CMYK );
if ( cinfo.process == JPROC_LOSSLESS )
if ( GDCM_JPEG_IS_LOSSLESS(cinfo) )
{
cinfo.jpeg_color_space = JCS_UNKNOWN;
cinfo.out_color_space = JCS_UNKNOWN;
}
break;
case JCS_UNKNOWN:
if ( cinfo.process == JPROC_LOSSLESS )
if ( GDCM_JPEG_IS_LOSSLESS(cinfo) )
{
cinfo.jpeg_color_space = JCS_UNKNOWN;
cinfo.out_color_space = JCS_UNKNOWN;
Expand Down Expand Up @@ -958,7 +974,7 @@ bool JPEGBITSCodec::DecodeByStreams(std::istream &is, std::ostream &os)

/* we are done decompressing the file, now is a good time to store the type
of compression used: lossless or not */
if( cinfo.process == JPROC_LOSSLESS )
if( GDCM_JPEG_IS_LOSSLESS(cinfo) )
{
LossyFlag = false;
}
Expand Down Expand Up @@ -1130,7 +1146,7 @@ term_destination (j_compress_ptr cinfo)
* for closing it after finishing compression.
*/

GLOBAL(void)
METHODDEF(void)
jpeg_stdio_dest (j_compress_ptr cinfo, /*FILE * */ std::ostream * outfile)
{
my_dest_ptr dest;
Expand Down Expand Up @@ -1281,7 +1297,11 @@ bool JPEGBITSCodec::InternalCode(const char* input, unsigned long len, std::ostr
*/
if( !LossyFlag )
{
#ifdef LIBJPEG_TURBO_VERSION_NUMBER
jpeg_enable_lossless(&cinfo, 1, 0);
#else
jpeg_simple_lossless (&cinfo, 1, 0);
#endif
//jpeg_simple_lossless (&cinfo, 7, 0);
}

Expand Down Expand Up @@ -1508,7 +1528,11 @@ bool JPEGBITSCodec::EncodeBuffer(std::ostream &os, const char *data, size_t data
{
if( !LossyFlag )
{
jpeg_simple_lossless (&cinfo, 1, 0);
#ifdef LIBJPEG_TURBO_VERSION_NUMBER
jpeg_enable_lossless(&cinfo, 1, 0);
#else
jpeg_simple_lossless (&cinfo, 1, 0);
#endif
//jpeg_simple_lossless (&cinfo, 7, 0);
}
}
Expand Down
7 changes: 4 additions & 3 deletions Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcm_ljpeg12.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@

#ifdef GDCM_USE_SYSTEM_LJPEG
extern "C" {
# include <ljpeg-62/12/jinclude.h>
# include <ljpeg-62/12/jpeglib.h>
# include <ljpeg-62/12/jerror.h>
#include "itk_jpeg.h"
#ifndef SIZEOF
#define SIZEOF(X) sizeof(X)
#endif
}
#else
extern "C" {
Expand Down
7 changes: 4 additions & 3 deletions Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcm_ljpeg16.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@

#ifdef GDCM_USE_SYSTEM_LJPEG
extern "C" {
# include <ljpeg-62/16/jinclude.h>
# include <ljpeg-62/16/jpeglib.h>
# include <ljpeg-62/16/jerror.h>
#include "itk_jpeg.h"
#ifndef SIZEOF
#define SIZEOF(X) sizeof(X)
#endif
}
#else
extern "C" {
Expand Down
7 changes: 4 additions & 3 deletions Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcm_ljpeg8.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@

#ifdef GDCM_USE_SYSTEM_LJPEG
extern "C" {
# include <ljpeg-62/8/jinclude.h>
# include <ljpeg-62/8/jpeglib.h>
# include <ljpeg-62/8/jerror.h>
#include "itk_jpeg.h"
#ifndef SIZEOF
#define SIZEOF(X) sizeof(X)
#endif
}
#else
extern "C" {
Expand Down
Loading