2021-04-30 20:12:10 +02:00
// Copyright (C) 2019-2021 Jakub Melka
2019-12-24 17:29:40 +01:00
//
2020-12-20 19:03:58 +01:00
// This file is part of Pdf4Qt.
2019-12-24 17:29:40 +01:00
//
2020-12-20 19:03:58 +01:00
// Pdf4Qt is free software: you can redistribute it and/or modify
2019-12-24 17:29:40 +01:00
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
2021-04-30 20:12:10 +02:00
// with the written consent of the copyright owner, any later version.
2019-12-24 17:29:40 +01:00
//
2020-12-20 19:03:58 +01:00
// Pdf4Qt is distributed in the hope that it will be useful,
2019-12-24 17:29:40 +01:00
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
2020-12-20 19:03:58 +01:00
// along with Pdf4Qt. If not, see <https://www.gnu.org/licenses/>.
2019-12-24 17:29:40 +01:00
# include "pdfcms.h"
2021-03-04 19:39:14 +01:00
# include "pdfdocument.h"
2021-02-13 17:09:57 +01:00
# include "pdfexecutionpolicy.h"
2019-12-24 17:29:40 +01:00
2019-12-25 14:34:37 +01:00
# include <QApplication>
2019-12-26 16:28:46 +01:00
# include <QReadWriteLock>
2019-12-25 14:34:37 +01:00
2019-12-24 17:29:40 +01:00
# pragma warning(push)
# pragma warning(disable:5033)
2021-03-06 18:13:21 +01:00
# define CMS_NO_REGISTER_KEYWORD
2019-12-24 17:29:40 +01:00
# include <lcms2.h>
# pragma warning(pop)
2019-12-26 16:28:46 +01:00
# include <unordered_map>
2019-12-24 17:29:40 +01:00
namespace pdf
{
2019-12-26 16:28:46 +01:00
class PDFLittleCMS : public PDFCMS
{
public :
explicit PDFLittleCMS ( const PDFCMSManager * manager , const PDFCMSSettings & settings ) ;
virtual ~ PDFLittleCMS ( ) override ;
virtual bool isCompatible ( const PDFCMSSettings & settings ) const override ;
virtual QColor getPaperColor ( ) const override ;
virtual QColor getColorFromDeviceGray ( const PDFColor & color , RenderingIntent intent , PDFRenderErrorReporter * reporter ) const override ;
virtual QColor getColorFromDeviceRGB ( const PDFColor & color , RenderingIntent intent , PDFRenderErrorReporter * reporter ) const override ;
virtual QColor getColorFromDeviceCMYK ( const PDFColor & color , RenderingIntent intent , PDFRenderErrorReporter * reporter ) const override ;
virtual QColor getColorFromXYZ ( const PDFColor3 & whitePoint , const PDFColor3 & color , RenderingIntent intent , PDFRenderErrorReporter * reporter ) const override ;
2019-12-26 17:57:45 +01:00
virtual QColor getColorFromICC ( const PDFColor & color , RenderingIntent renderingIntent , const QByteArray & iccID , const QByteArray & iccData , PDFRenderErrorReporter * reporter ) const override ;
2019-12-30 16:08:48 +01:00
virtual bool fillRGBBufferFromDeviceGray ( const std : : vector < float > & colors , RenderingIntent intent , unsigned char * outputBuffer , PDFRenderErrorReporter * reporter ) const override ;
virtual bool fillRGBBufferFromDeviceRGB ( const std : : vector < float > & colors , RenderingIntent intent , unsigned char * outputBuffer , PDFRenderErrorReporter * reporter ) const override ;
virtual bool fillRGBBufferFromDeviceCMYK ( const std : : vector < float > & colors , RenderingIntent intent , unsigned char * outputBuffer , PDFRenderErrorReporter * reporter ) const override ;
virtual bool fillRGBBufferFromXYZ ( const PDFColor3 & whitePoint , const std : : vector < float > & colors , RenderingIntent intent , unsigned char * outputBuffer , PDFRenderErrorReporter * reporter ) const override ;
virtual bool fillRGBBufferFromICC ( const std : : vector < float > & colors , RenderingIntent renderingIntent , unsigned char * outputBuffer , const QByteArray & iccID , const QByteArray & iccData , PDFRenderErrorReporter * reporter ) const override ;
2021-01-02 18:27:22 +01:00
virtual bool transformColorSpace ( const ColorSpaceTransformParams & params ) const override ;
2019-12-26 16:28:46 +01:00
private :
void init ( ) ;
enum Profile
{
Output ,
Gray ,
RGB ,
CMYK ,
XYZ ,
2020-12-25 19:01:08 +01:00
SoftProofing ,
2019-12-26 16:28:46 +01:00
ProfileCount
} ;
2020-12-25 19:01:08 +01:00
/// Returns true, if we are doing soft-proofing
bool isSoftProofing ( ) const ;
2019-12-26 16:28:46 +01:00
/// Creates a profile using provided id and a list of profile descriptors.
2021-03-04 19:39:14 +01:00
/// If profile can't be created, then null handle is returned. If \p preferOutputProfile
/// is set to true, and given profile is not output profile, then first output profile
/// is being selected.
2019-12-26 16:28:46 +01:00
/// \param id Id of color profile
/// \param profileDescriptors Profile descriptor list
2021-03-04 19:39:14 +01:00
/// \param preferOutputProfile
cmsHPROFILE createProfile ( const QString & id , const PDFColorProfileIdentifiers & profileDescriptors , bool preferOutputProfile ) const ;
2019-12-26 16:28:46 +01:00
/// Gets transform from cache. If transform doesn't exist, then it is created.
/// \param profile Color profile
/// \param intent Rendering intent
2019-12-30 16:08:48 +01:00
/// \param isRGB888Buffer If true, 8-bit RGB output buffer is used, otherwise FLOAT RGB output buffer is used
cmsHTRANSFORM getTransform ( Profile profile , RenderingIntent intent , bool isRGB888Buffer ) const ;
/// Gets transform for ICC profile from cache. If transform doesn't exist, then it is created.
/// \param iccData Data of icc profile
/// \param iccID Icc profile id
/// \param renderingIntent Rendering intent
/// \param isRGB888Buffer If true, 8-bit RGB output buffer is used, otherwise FLOAT RGB output buffer is used
cmsHTRANSFORM getTransformFromICCProfile ( const QByteArray & iccData , const QByteArray & iccID , RenderingIntent renderingIntent , bool isRGB888Buffer ) const ;
2019-12-26 16:28:46 +01:00
2020-08-08 18:18:08 +02:00
/// Returns transformation flags according to the current settings
2019-12-26 16:28:46 +01:00
cmsUInt32Number getTransformationFlags ( ) const ;
/// Calculates effective rendering intent. If rendering intent is auto,
/// then \p intent is used, otherwise intent is overriden.
RenderingIntent getEffectiveRenderingIntent ( RenderingIntent intent ) const ;
/// Gets transform from cache key.
/// \param profile Color profile
/// \param intent Rendering intent
2019-12-30 16:08:48 +01:00
/// \param isRGB888Buffer If true, 8-bit RGB output buffer is used, otherwise FLOAT RGB output buffer is used
static constexpr int getCacheKey ( Profile profile , RenderingIntent intent , bool isRGB888Buffer ) { return ( ( int ( intent ) * ProfileCount + profile ) < < 1 ) + ( isRGB888Buffer ? 1 : 0 ) ; }
2019-12-26 16:28:46 +01:00
/// Returns little CMS rendering intent
/// \param intent Rendering intent
static cmsUInt32Number getLittleCMSRenderingIntent ( RenderingIntent intent ) ;
/// Returns little CMS data format for profile
/// \param profile Color profile handle
static cmsUInt32Number getProfileDataFormat ( cmsHPROFILE profile ) ;
/// Returns color from output color. Clamps invalid rgb output values to range [0.0, 1.0].
/// \param color01 Rgb color (range 0-1 is assumed).
static QColor getColorFromOutputColor ( std : : array < float , 3 > color01 ) ;
2021-01-07 18:46:44 +01:00
/// Returns transform key for transformation between various color spaces
static QByteArray getTransformColorSpaceKey ( const ColorSpaceTransformParams & params ) ;
cmsHTRANSFORM getTransformBetweenColorSpaces ( const ColorSpaceTransformParams & params ) const ;
2019-12-26 16:28:46 +01:00
const PDFCMSManager * m_manager ;
PDFCMSSettings m_settings ;
QColor m_paperColor ;
std : : array < cmsHPROFILE , ProfileCount > m_profiles ;
mutable QReadWriteLock m_transformationCacheLock ;
mutable std : : unordered_map < int , cmsHTRANSFORM > m_transformationCache ;
2019-12-26 17:57:45 +01:00
mutable QReadWriteLock m_customIccProfileCacheLock ;
mutable std : : map < std : : pair < QByteArray , RenderingIntent > , cmsHTRANSFORM > m_customIccProfileCache ;
2021-01-07 18:46:44 +01:00
mutable QReadWriteLock m_transformColorSpaceCacheLock ;
mutable std : : map < QByteArray , cmsHTRANSFORM > m_transformColorSpaceCache ;
2019-12-26 16:28:46 +01:00
} ;
2019-12-30 16:08:48 +01:00
bool PDFLittleCMS : : fillRGBBufferFromDeviceGray ( const std : : vector < float > & colors ,
RenderingIntent intent ,
unsigned char * outputBuffer ,
PDFRenderErrorReporter * reporter ) const
{
cmsHTRANSFORM transform = getTransform ( Gray , getEffectiveRenderingIntent ( intent ) , true ) ;
if ( ! transform )
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from gray to output device using CMS failed. " ) ) ;
return false ;
}
if ( cmsGetTransformInputFormat ( transform ) = = TYPE_GRAY_FLT )
{
Q_ASSERT ( cmsGetTransformOutputFormat ( transform ) = = TYPE_RGB_8 ) ;
cmsDoTransform ( transform , colors . data ( ) , outputBuffer , static_cast < cmsUInt32Number > ( colors . size ( ) ) ) ;
return true ;
}
else
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from gray to output device using CMS failed - invalid data format. " ) ) ;
}
return false ;
}
bool PDFLittleCMS : : fillRGBBufferFromDeviceRGB ( const std : : vector < float > & colors , RenderingIntent intent , unsigned char * outputBuffer , PDFRenderErrorReporter * reporter ) const
{
cmsHTRANSFORM transform = getTransform ( RGB , getEffectiveRenderingIntent ( intent ) , true ) ;
if ( ! transform )
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from RGB to output device using CMS failed. " ) ) ;
return false ;
}
const cmsUInt32Number inputFormat = cmsGetTransformInputFormat ( transform ) ;
if ( inputFormat = = TYPE_RGB_FLT & & ( colors . size ( ) ) % T_CHANNELS ( inputFormat ) = = 0 )
{
Q_ASSERT ( cmsGetTransformOutputFormat ( transform ) = = TYPE_RGB_8 ) ;
cmsDoTransform ( transform , colors . data ( ) , outputBuffer , static_cast < cmsUInt32Number > ( colors . size ( ) ) / T_CHANNELS ( inputFormat ) ) ;
return true ;
}
else
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from RGB to output device using CMS failed - invalid data format. " ) ) ;
}
return false ;
}
bool PDFLittleCMS : : fillRGBBufferFromDeviceCMYK ( const std : : vector < float > & colors , RenderingIntent intent , unsigned char * outputBuffer , PDFRenderErrorReporter * reporter ) const
{
cmsHTRANSFORM transform = getTransform ( CMYK , getEffectiveRenderingIntent ( intent ) , true ) ;
if ( ! transform )
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from CMYK to output device using CMS failed. " ) ) ;
return false ;
}
const cmsUInt32Number inputFormat = cmsGetTransformInputFormat ( transform ) ;
if ( inputFormat = = TYPE_CMYK_FLT & & ( colors . size ( ) ) % T_CHANNELS ( inputFormat ) = = 0 )
{
Q_ASSERT ( cmsGetTransformOutputFormat ( transform ) = = TYPE_RGB_8 ) ;
std : : vector < float > fixedColors = colors ;
for ( size_t i = 0 , count = fixedColors . size ( ) ; i < count ; + + i )
{
fixedColors [ i ] = fixedColors [ i ] * 100.0f ;
}
cmsDoTransform ( transform , fixedColors . data ( ) , outputBuffer , static_cast < cmsUInt32Number > ( colors . size ( ) ) / T_CHANNELS ( inputFormat ) ) ;
return true ;
}
else
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from CMYK to output device using CMS failed - invalid data format. " ) ) ;
}
return false ;
}
bool PDFLittleCMS : : fillRGBBufferFromXYZ ( const PDFColor3 & whitePoint , const std : : vector < float > & colors , RenderingIntent intent , unsigned char * outputBuffer , PDFRenderErrorReporter * reporter ) const
{
cmsHTRANSFORM transform = getTransform ( XYZ , getEffectiveRenderingIntent ( intent ) , true ) ;
if ( ! transform )
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from XYZ to output device using CMS failed. " ) ) ;
return false ;
}
const cmsUInt32Number inputFormat = cmsGetTransformInputFormat ( transform ) ;
if ( inputFormat = = TYPE_XYZ_FLT & & ( colors . size ( ) ) % T_CHANNELS ( inputFormat ) = = 0 )
{
Q_ASSERT ( cmsGetTransformOutputFormat ( transform ) = = TYPE_RGB_8 ) ;
2021-05-08 18:16:28 +02:00
PDFColorComponentMatrix_3x3 adaptationMatrix = PDFChromaticAdaptationXYZ : : createWhitepointChromaticAdaptation ( getDefaultXYZWhitepoint ( ) , whitePoint , m_settings . colorAdaptationXYZ ) ;
2019-12-30 16:08:48 +01:00
std : : vector < float > fixedColors = colors ;
2021-05-08 18:16:28 +02:00
const size_t count = fixedColors . size ( ) / 3 ;
for ( size_t i = 0 ; i < count ; + + i )
2019-12-30 16:08:48 +01:00
{
2021-05-08 18:16:28 +02:00
const size_t indexX = i * 3 ;
const size_t indexY = i * 3 + 1 ;
const size_t indexZ = i * 3 + 2 ;
const PDFColor3 sourceXYZ = { fixedColors [ indexX ] , fixedColors [ indexY ] , fixedColors [ indexZ ] } ;
const PDFColor3 adaptedXYZ = adaptationMatrix * sourceXYZ ;
fixedColors [ indexX ] = adaptedXYZ [ 0 ] ;
fixedColors [ indexY ] = adaptedXYZ [ 1 ] ;
fixedColors [ indexZ ] = adaptedXYZ [ 2 ] ;
2019-12-30 16:08:48 +01:00
}
cmsDoTransform ( transform , fixedColors . data ( ) , outputBuffer , static_cast < cmsUInt32Number > ( colors . size ( ) ) / T_CHANNELS ( inputFormat ) ) ;
return true ;
}
else
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from XYZ to output device using CMS failed - invalid data format. " ) ) ;
}
return false ;
}
bool PDFLittleCMS : : fillRGBBufferFromICC ( const std : : vector < float > & colors , RenderingIntent renderingIntent , unsigned char * outputBuffer , const QByteArray & iccID , const QByteArray & iccData , PDFRenderErrorReporter * reporter ) const
{
cmsHTRANSFORM transform = getTransformFromICCProfile ( iccData , iccID , renderingIntent , true ) ;
if ( ! transform )
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from icc profile space to output device using CMS failed. " ) ) ;
return false ;
}
const cmsUInt32Number format = cmsGetTransformInputFormat ( transform ) ;
const cmsUInt32Number channels = T_CHANNELS ( format ) ;
const cmsUInt32Number colorSpace = T_COLORSPACE ( format ) ;
const bool isCMYK = colorSpace = = PT_CMYK ;
const float * inputColors = colors . data ( ) ;
std : : vector < float > cmykColors ;
if ( isCMYK )
{
cmykColors = colors ;
for ( size_t i = 0 ; i < cmykColors . size ( ) ; + + i )
{
2021-03-05 19:21:20 +01:00
cmykColors [ i ] = cmykColors [ i ] * 100.0f ;
2019-12-30 16:08:48 +01:00
}
inputColors = cmykColors . data ( ) ;
}
2021-01-07 18:46:44 +01:00
if ( colors . size ( ) % channels = = 0 )
2019-12-30 16:08:48 +01:00
{
const cmsUInt32Number pixels = static_cast < cmsUInt32Number > ( colors . size ( ) ) / channels ;
cmsDoTransform ( transform , inputColors , outputBuffer , pixels ) ;
return true ;
}
else
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from icc profile space to output device using CMS failed - invalid data format. " ) ) ;
}
return false ;
}
2021-01-02 18:27:22 +01:00
bool PDFLittleCMS : : transformColorSpace ( const PDFCMS : : ColorSpaceTransformParams & params ) const
{
2021-01-07 18:46:44 +01:00
PDFCMS : : ColorSpaceTransformParams transformedParams = params ;
transformedParams . intent = getEffectiveRenderingIntent ( transformedParams . intent ) ;
cmsHTRANSFORM transform = getTransformBetweenColorSpaces ( transformedParams ) ;
if ( ! transform )
{
return false ;
}
const cmsUInt32Number inputProfileFormat = cmsGetTransformInputFormat ( transform ) ;
const cmsUInt32Number inputChannels = T_CHANNELS ( inputProfileFormat ) ;
const cmsUInt32Number inputColorSpace = T_COLORSPACE ( inputProfileFormat ) ;
const bool isInputCMYK = inputColorSpace = = PT_CMYK ;
const float * inputColors = params . input . begin ( ) ;
std : : vector < float > cmykColors ;
const cmsUInt32Number outputProfileFormat = cmsGetTransformOutputFormat ( transform ) ;
const cmsUInt32Number outputChannels = T_CHANNELS ( outputProfileFormat ) ;
const cmsUInt32Number outputColorSpace = T_COLORSPACE ( outputProfileFormat ) ;
const bool isOutputCMYK = outputColorSpace = = PT_CMYK ;
if ( isInputCMYK )
{
cmykColors = std : : vector < float > ( params . input . cbegin ( ) , params . input . cend ( ) ) ;
for ( size_t i = 0 ; i < cmykColors . size ( ) ; + + i )
{
2021-03-05 19:21:20 +01:00
cmykColors [ i ] = cmykColors [ i ] * 100.0f ;
2021-01-07 18:46:44 +01:00
}
inputColors = cmykColors . data ( ) ;
}
const cmsUInt32Number inputPixelCount = static_cast < cmsUInt32Number > ( params . input . size ( ) ) / inputChannels ;
const cmsUInt32Number outputPixelCount = static_cast < cmsUInt32Number > ( params . output . size ( ) ) / outputChannels ;
if ( params . input . size ( ) % inputChannels = = 0 & &
params . output . size ( ) % outputChannels = = 0 & &
inputPixelCount = = outputPixelCount )
{
PDFColorBuffer outputBuffer = params . output ;
2021-02-13 17:09:57 +01:00
if ( inputPixelCount > params . multithreadingThreshold )
{
struct TransformInfo
{
inline TransformInfo ( const float * source , float * target , cmsUInt32Number pixelCount ) :
source ( source ) ,
target ( target ) ,
pixelCount ( pixelCount )
{
}
const float * source = nullptr ;
float * target = nullptr ;
cmsUInt32Number pixelCount = 0 ;
} ;
const cmsUInt32Number blockSize = 4096 ;
std : : vector < TransformInfo > infos ;
infos . reserve ( inputPixelCount / blockSize + 1 ) ;
const float * sourcePointer = inputColors ;
float * targetPointer = outputBuffer . begin ( ) ;
cmsUInt32Number remainingPixelCount = inputPixelCount ;
while ( remainingPixelCount > 0 )
{
const cmsUInt32Number currentPixelCount = qMin ( blockSize , remainingPixelCount ) ;
infos . emplace_back ( sourcePointer , targetPointer , currentPixelCount ) ;
sourcePointer + = currentPixelCount * inputChannels ;
targetPointer + = currentPixelCount * outputChannels ;
remainingPixelCount - = currentPixelCount ;
}
auto processEntry = [ transform ] ( const TransformInfo & info )
{
cmsDoTransform ( transform , info . source , info . target , info . pixelCount ) ;
} ;
PDFExecutionPolicy : : execute ( PDFExecutionPolicy : : Scope : : Content , infos . begin ( ) , infos . end ( ) , processEntry ) ;
}
else
{
// Single-threaded transform
cmsDoTransform ( transform , inputColors , outputBuffer . begin ( ) , inputPixelCount ) ;
}
2021-01-07 18:46:44 +01:00
if ( isOutputCMYK )
{
const PDFColorComponent colorQuotient = 1.0f / 100.0f ;
for ( PDFColorComponent & color : outputBuffer )
{
color * = colorQuotient ;
}
}
return true ;
}
else
{
return false ;
}
2021-01-02 18:27:22 +01:00
return false ;
}
2019-12-26 16:28:46 +01:00
PDFLittleCMS : : PDFLittleCMS ( const PDFCMSManager * manager , const PDFCMSSettings & settings ) :
m_manager ( manager ) ,
m_settings ( settings ) ,
m_paperColor ( Qt : : white ) ,
m_profiles ( )
{
init ( ) ;
}
PDFLittleCMS : : ~ PDFLittleCMS ( )
{
for ( const auto & transformItem : m_transformationCache )
{
cmsHTRANSFORM transform = transformItem . second ;
if ( transform )
{
cmsDeleteTransform ( transform ) ;
}
}
2019-12-26 17:57:45 +01:00
for ( const auto & transformItem : m_customIccProfileCache )
{
cmsHTRANSFORM transform = transformItem . second ;
if ( transform )
{
cmsDeleteTransform ( transform ) ;
}
}
2021-01-07 18:46:44 +01:00
for ( const auto & transformItem : m_transformColorSpaceCache )
{
cmsHTRANSFORM transform = transformItem . second ;
if ( transform )
{
cmsDeleteTransform ( transform ) ;
}
}
2019-12-26 16:28:46 +01:00
for ( cmsHPROFILE profile : m_profiles )
{
if ( profile )
{
cmsCloseProfile ( profile ) ;
}
}
}
bool PDFLittleCMS : : isCompatible ( const PDFCMSSettings & settings ) const
{
return m_settings = = settings ;
}
QColor PDFLittleCMS : : getPaperColor ( ) const
{
return m_paperColor ;
}
QColor PDFLittleCMS : : getColorFromDeviceGray ( const PDFColor & color , RenderingIntent intent , PDFRenderErrorReporter * reporter ) const
{
2019-12-30 16:08:48 +01:00
cmsHTRANSFORM transform = getTransform ( Gray , getEffectiveRenderingIntent ( intent ) , false ) ;
2019-12-26 16:28:46 +01:00
if ( ! transform )
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from gray to output device using CMS failed. " ) ) ;
return QColor ( ) ;
}
if ( cmsGetTransformInputFormat ( transform ) = = TYPE_GRAY_FLT & & color . size ( ) = = 1 )
{
Q_ASSERT ( cmsGetTransformOutputFormat ( transform ) = = TYPE_RGB_FLT ) ;
const float grayColor = color [ 0 ] ;
std : : array < float , 3 > rgbOutputColor = { } ;
cmsDoTransform ( transform , & grayColor , rgbOutputColor . data ( ) , 1 ) ;
return getColorFromOutputColor ( rgbOutputColor ) ;
}
else
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from gray to output device using CMS failed - invalid data format. " ) ) ;
}
return QColor ( ) ;
}
QColor PDFLittleCMS : : getColorFromDeviceRGB ( const PDFColor & color , RenderingIntent intent , PDFRenderErrorReporter * reporter ) const
{
2019-12-30 16:08:48 +01:00
cmsHTRANSFORM transform = getTransform ( RGB , getEffectiveRenderingIntent ( intent ) , false ) ;
2019-12-26 16:28:46 +01:00
if ( ! transform )
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from RGB to output device using CMS failed. " ) ) ;
return QColor ( ) ;
}
if ( cmsGetTransformInputFormat ( transform ) = = TYPE_RGB_FLT & & color . size ( ) = = 3 )
{
Q_ASSERT ( cmsGetTransformOutputFormat ( transform ) = = TYPE_RGB_FLT ) ;
std : : array < float , 3 > rgbInputColor = { color [ 0 ] , color [ 1 ] , color [ 2 ] } ;
std : : array < float , 3 > rgbOutputColor = { } ;
cmsDoTransform ( transform , rgbInputColor . data ( ) , rgbOutputColor . data ( ) , 1 ) ;
return getColorFromOutputColor ( rgbOutputColor ) ;
}
else
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from RGB to output device using CMS failed - invalid data format. " ) ) ;
}
return QColor ( ) ;
}
QColor PDFLittleCMS : : getColorFromDeviceCMYK ( const PDFColor & color , RenderingIntent intent , PDFRenderErrorReporter * reporter ) const
{
2019-12-30 16:08:48 +01:00
cmsHTRANSFORM transform = getTransform ( CMYK , getEffectiveRenderingIntent ( intent ) , false ) ;
2019-12-26 16:28:46 +01:00
if ( ! transform )
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from CMYK to output device using CMS failed. " ) ) ;
return QColor ( ) ;
}
if ( cmsGetTransformInputFormat ( transform ) = = TYPE_CMYK_FLT & & color . size ( ) = = 4 )
{
Q_ASSERT ( cmsGetTransformOutputFormat ( transform ) = = TYPE_RGB_FLT ) ;
std : : array < float , 4 > cmykInputColor = { color [ 0 ] * 100.0f , color [ 1 ] * 100.0f , color [ 2 ] * 100.0f , color [ 3 ] * 100.0f } ;
std : : array < float , 3 > rgbOutputColor = { } ;
cmsDoTransform ( transform , cmykInputColor . data ( ) , rgbOutputColor . data ( ) , 1 ) ;
return getColorFromOutputColor ( rgbOutputColor ) ;
}
else
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from CMYK to output device using CMS failed - invalid data format. " ) ) ;
}
return QColor ( ) ;
}
QColor PDFLittleCMS : : getColorFromXYZ ( const PDFColor3 & whitePoint , const PDFColor3 & color , RenderingIntent intent , PDFRenderErrorReporter * reporter ) const
{
2019-12-30 16:08:48 +01:00
cmsHTRANSFORM transform = getTransform ( XYZ , getEffectiveRenderingIntent ( intent ) , false ) ;
2019-12-26 16:28:46 +01:00
if ( ! transform )
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from XYZ to output device using CMS failed. " ) ) ;
return QColor ( ) ;
}
if ( cmsGetTransformInputFormat ( transform ) = = TYPE_XYZ_FLT & & color . size ( ) = = 3 )
{
Q_ASSERT ( cmsGetTransformOutputFormat ( transform ) = = TYPE_RGB_FLT ) ;
2021-05-08 18:16:28 +02:00
const PDFColorComponentMatrix_3x3 adaptationMatrix = PDFChromaticAdaptationXYZ : : createWhitepointChromaticAdaptation ( getDefaultXYZWhitepoint ( ) , whitePoint , m_settings . colorAdaptationXYZ ) ;
const PDFColor3 xyzInputColor = adaptationMatrix * color ;
2019-12-26 16:28:46 +01:00
std : : array < float , 3 > rgbOutputColor = { } ;
cmsDoTransform ( transform , xyzInputColor . data ( ) , rgbOutputColor . data ( ) , 1 ) ;
return getColorFromOutputColor ( rgbOutputColor ) ;
}
else
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from XYZ to output device using CMS failed - invalid data format. " ) ) ;
}
return QColor ( ) ;
}
2019-12-30 16:08:48 +01:00
cmsHTRANSFORM PDFLittleCMS : : getTransformFromICCProfile ( const QByteArray & iccData , const QByteArray & iccID , RenderingIntent renderingIntent , bool isRGB888Buffer ) const
2019-12-26 16:28:46 +01:00
{
2019-12-30 16:08:48 +01:00
RenderingIntent effectiveRenderingIntent = getEffectiveRenderingIntent ( renderingIntent ) ;
const auto key = std : : make_pair ( iccID + ( isRGB888Buffer ? " RGB_888 " : " FLT " ) , effectiveRenderingIntent ) ;
QReadLocker lock ( & m_customIccProfileCacheLock ) ;
auto it = m_customIccProfileCache . find ( key ) ;
if ( it = = m_customIccProfileCache . cend ( ) )
2019-12-26 17:57:45 +01:00
{
2019-12-30 16:08:48 +01:00
lock . unlock ( ) ;
QWriteLocker writeLock ( & m_customIccProfileCacheLock ) ;
// Now, we have locked cache for writing. We must find out,
// if some other thread doesn't created the transformation already.
it = m_customIccProfileCache . find ( key ) ;
2019-12-26 17:57:45 +01:00
if ( it = = m_customIccProfileCache . cend ( ) )
{
2019-12-30 16:08:48 +01:00
cmsHTRANSFORM transform = cmsHTRANSFORM ( ) ;
cmsHPROFILE profile = cmsOpenProfileFromMem ( iccData . data ( ) , iccData . size ( ) ) ;
if ( profile )
2019-12-26 17:57:45 +01:00
{
2019-12-30 16:08:48 +01:00
if ( const cmsUInt32Number inputDataFormat = getProfileDataFormat ( profile ) )
2019-12-26 17:57:45 +01:00
{
2019-12-30 16:08:48 +01:00
cmsUInt32Number lcmsIntent = getLittleCMSRenderingIntent ( effectiveRenderingIntent ) ;
2020-12-25 19:01:08 +01:00
if ( isSoftProofing ( ) )
{
cmsHPROFILE proofingProfile = m_profiles [ SoftProofing ] ;
RenderingIntent proofingIntent = m_settings . proofingIntent ;
if ( m_settings . proofingIntent = = RenderingIntent : : Auto )
{
proofingIntent = effectiveRenderingIntent ;
}
transform = cmsCreateProofingTransform ( profile , inputDataFormat , m_profiles [ Output ] , isRGB888Buffer ? TYPE_RGB_8 : TYPE_RGB_FLT , proofingProfile ,
lcmsIntent , getLittleCMSRenderingIntent ( proofingIntent ) , getTransformationFlags ( ) ) ;
}
else
{
transform = cmsCreateTransform ( profile , inputDataFormat , m_profiles [ Output ] , isRGB888Buffer ? TYPE_RGB_8 : TYPE_RGB_FLT , lcmsIntent , getTransformationFlags ( ) ) ;
}
2019-12-26 17:57:45 +01:00
}
2019-12-30 16:08:48 +01:00
cmsCloseProfile ( profile ) ;
2019-12-26 17:57:45 +01:00
}
2019-12-30 16:08:48 +01:00
it = m_customIccProfileCache . insert ( std : : make_pair ( key , transform ) ) . first ;
2019-12-26 17:57:45 +01:00
}
2019-12-30 16:08:48 +01:00
return it - > second ;
2019-12-26 17:57:45 +01:00
}
2019-12-30 16:08:48 +01:00
else
{
return it - > second ;
}
return cmsHTRANSFORM ( ) ;
}
QColor PDFLittleCMS : : getColorFromICC ( const PDFColor & color , RenderingIntent renderingIntent , const QByteArray & iccID , const QByteArray & iccData , PDFRenderErrorReporter * reporter ) const
{
cmsHTRANSFORM transform = getTransformFromICCProfile ( iccData , iccID , renderingIntent , false ) ;
2019-12-26 17:57:45 +01:00
if ( ! transform )
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from icc profile space to output device using CMS failed. " ) ) ;
return QColor ( ) ;
}
std : : array < float , 4 > inputBuffer = { } ;
2019-12-27 14:17:33 +01:00
const cmsUInt32Number format = cmsGetTransformInputFormat ( transform ) ;
const cmsUInt32Number channels = T_CHANNELS ( format ) ;
const cmsUInt32Number colorSpace = T_COLORSPACE ( format ) ;
const bool isCMYK = colorSpace = = PT_CMYK ;
2019-12-26 17:57:45 +01:00
if ( channels = = color . size ( ) & & channels < = inputBuffer . size ( ) )
{
for ( size_t i = 0 ; i < color . size ( ) ; + + i )
{
2021-03-05 19:21:20 +01:00
inputBuffer [ i ] = isCMYK ? color [ i ] * 100.0f : color [ i ] ;
2019-12-26 17:57:45 +01:00
}
std : : array < float , 3 > rgbOutputColor = { } ;
cmsDoTransform ( transform , inputBuffer . data ( ) , rgbOutputColor . data ( ) , 1 ) ;
return getColorFromOutputColor ( rgbOutputColor ) ;
}
else
{
reporter - > reportRenderErrorOnce ( RenderErrorType : : Error , PDFTranslationContext : : tr ( " Conversion from icc profile space to output device using CMS failed - invalid data format. " ) ) ;
}
2019-12-26 16:28:46 +01:00
return QColor ( ) ;
}
void PDFLittleCMS : : init ( )
{
// Jakub Melka: initialize all color profiles
2021-03-04 19:39:14 +01:00
m_profiles [ Output ] = createProfile ( m_settings . outputCS , m_manager - > getOutputProfiles ( ) , false ) ;
m_profiles [ Gray ] = createProfile ( m_settings . deviceGray , m_manager - > getGrayProfiles ( ) , m_settings . isConsiderOutputIntent ) ;
m_profiles [ RGB ] = createProfile ( m_settings . deviceRGB , m_manager - > getRGBProfiles ( ) , m_settings . isConsiderOutputIntent ) ;
m_profiles [ CMYK ] = createProfile ( m_settings . deviceCMYK , m_manager - > getCMYKProfiles ( ) , m_settings . isConsiderOutputIntent ) ;
m_profiles [ SoftProofing ] = createProfile ( m_settings . softProofingProfile , m_manager - > getCMYKProfiles ( ) , false ) ;
2019-12-26 16:28:46 +01:00
m_profiles [ XYZ ] = cmsCreateXYZProfile ( ) ;
2020-12-26 12:55:29 +01:00
cmsUInt16Number outOfGamutR = m_settings . outOfGamutColor . redF ( ) * 0xFFFF ;
cmsUInt16Number outOfGamutG = m_settings . outOfGamutColor . greenF ( ) * 0xFFFF ;
cmsUInt16Number outOfGamutB = m_settings . outOfGamutColor . blueF ( ) * 0xFFFF ;
cmsUInt16Number alarmCodes [ cmsMAXCHANNELS ] = { outOfGamutR , outOfGamutG , outOfGamutB } ;
2020-12-25 19:01:08 +01:00
cmsSetAlarmCodes ( alarmCodes ) ;
2019-12-26 16:28:46 +01:00
if ( m_settings . isWhitePaperColorTransformed )
{
m_paperColor = getColorFromDeviceRGB ( PDFColor ( 1.0f , 1.0f , 1.0f ) , RenderingIntent : : AbsoluteColorimetric , nullptr ) ;
// We must check color of the paper, it can be invalid, if error occurs...
if ( ! m_paperColor . isValid ( ) )
{
m_paperColor = QColor ( Qt : : white ) ;
}
}
// 64 should be enough, because we can have 4 input color spaces (gray, RGB, CMYK and XYZ),
// and 4 rendering intents. We have 4 * 4 = 16 input tables, so 64 will suffice enough
// (because we then have 25% load factor).
m_transformationCache . reserve ( 64 ) ;
}
2020-12-25 19:01:08 +01:00
bool PDFLittleCMS : : isSoftProofing ( ) const
{
return ( m_settings . isSoftProofing | | m_settings . isGamutChecking ) & & m_profiles [ SoftProofing ] ;
}
2021-03-04 19:39:14 +01:00
cmsHPROFILE PDFLittleCMS : : createProfile ( const QString & id , const PDFColorProfileIdentifiers & profileDescriptors , bool preferOutputProfile ) const
2019-12-26 16:28:46 +01:00
{
auto it = std : : find_if ( profileDescriptors . cbegin ( ) , profileDescriptors . cend ( ) , [ & id ] ( const PDFColorProfileIdentifier & identifier ) { return identifier . id = = id ; } ) ;
2021-03-04 19:39:14 +01:00
if ( preferOutputProfile & & it ! = profileDescriptors . end ( ) )
{
const PDFColorProfileIdentifier & identifier = * it ;
if ( ! identifier . isOutputIntentProfile )
{
// Find first output intent color profile
2021-03-06 18:13:21 +01:00
auto itOutputIntentColorProfile = std : : find_if ( profileDescriptors . cbegin ( ) , profileDescriptors . cend ( ) , [ ] ( const PDFColorProfileIdentifier & identifier ) { return identifier . isOutputIntentProfile ; } ) ;
2021-03-04 19:39:14 +01:00
if ( itOutputIntentColorProfile ! = profileDescriptors . end ( ) )
{
it = itOutputIntentColorProfile ;
}
}
}
2019-12-26 16:28:46 +01:00
if ( it ! = profileDescriptors . cend ( ) )
{
const PDFColorProfileIdentifier & identifier = * it ;
switch ( identifier . type )
{
case PDFColorProfileIdentifier : : Type : : Gray :
{
cmsCIExyY whitePoint { } ;
if ( cmsWhitePointFromTemp ( & whitePoint , identifier . temperature ) )
{
cmsToneCurve * gammaCurve = cmsBuildGamma ( cmsContext ( ) , identifier . gamma ) ;
cmsHPROFILE profile = cmsCreateGrayProfile ( & whitePoint , gammaCurve ) ;
cmsFreeToneCurve ( gammaCurve ) ;
return profile ;
}
break ;
}
case PDFColorProfileIdentifier : : Type : : sRGB :
return cmsCreate_sRGBProfile ( ) ;
case PDFColorProfileIdentifier : : Type : : RGB :
{
cmsCIExyY whitePoint { } ;
if ( cmsWhitePointFromTemp ( & whitePoint , identifier . temperature ) )
{
cmsCIExyYTRIPLE primaries ;
primaries . Red = { identifier . primaryR . x ( ) , identifier . primaryR . y ( ) , 1.0 } ;
primaries . Green = { identifier . primaryG . x ( ) , identifier . primaryG . y ( ) , 1.0 } ;
primaries . Blue = { identifier . primaryB . x ( ) , identifier . primaryB . y ( ) , 1.0 } ;
cmsToneCurve * gammaCurve = cmsBuildGamma ( cmsContext ( ) , identifier . gamma ) ;
cmsToneCurve * toneCurves [ 3 ] = { gammaCurve , cmsDupToneCurve ( gammaCurve ) , cmsDupToneCurve ( gammaCurve ) } ;
cmsHPROFILE profile = cmsCreateRGBProfile ( & whitePoint , & primaries , toneCurves ) ;
cmsFreeToneCurveTriple ( toneCurves ) ;
return profile ;
}
break ;
}
case PDFColorProfileIdentifier : : Type : : FileGray :
case PDFColorProfileIdentifier : : Type : : FileRGB :
case PDFColorProfileIdentifier : : Type : : FileCMYK :
{
QFile file ( identifier . id ) ;
if ( file . open ( QFile : : ReadOnly ) )
{
QByteArray fileContent = file . readAll ( ) ;
file . close ( ) ;
return cmsOpenProfileFromMem ( fileContent . data ( ) , fileContent . size ( ) ) ;
}
break ;
}
2021-03-04 19:39:14 +01:00
case PDFColorProfileIdentifier : : Type : : MemoryGray :
case PDFColorProfileIdentifier : : Type : : MemoryRGB :
case PDFColorProfileIdentifier : : Type : : MemoryCMYK :
return cmsOpenProfileFromMem ( identifier . profileMemoryData . data ( ) , identifier . profileMemoryData . size ( ) ) ;
2019-12-26 16:28:46 +01:00
default :
Q_ASSERT ( false ) ;
break ;
}
}
return cmsHPROFILE ( ) ;
}
2019-12-30 16:08:48 +01:00
cmsHTRANSFORM PDFLittleCMS : : getTransform ( Profile profile , RenderingIntent intent , bool isRGB888Buffer ) const
2019-12-26 16:28:46 +01:00
{
2019-12-30 16:08:48 +01:00
const int key = getCacheKey ( profile , intent , isRGB888Buffer ) ;
2019-12-26 16:28:46 +01:00
QReadLocker lock ( & m_transformationCacheLock ) ;
auto it = m_transformationCache . find ( key ) ;
if ( it = = m_transformationCache . cend ( ) )
{
lock . unlock ( ) ;
QWriteLocker writeLock ( & m_transformationCacheLock ) ;
// Now, we have locked cache for writing. We must find out,
// if some other thread doesn't created the transformation already.
it = m_transformationCache . find ( key ) ;
if ( it = = m_transformationCache . cend ( ) )
{
cmsHTRANSFORM transform = cmsHTRANSFORM ( ) ;
cmsHPROFILE input = m_profiles [ profile ] ;
cmsHPROFILE output = m_profiles [ Output ] ;
if ( input & & output )
{
2020-12-25 19:01:08 +01:00
if ( isSoftProofing ( ) )
{
cmsHPROFILE proofingProfile = m_profiles [ SoftProofing ] ;
RenderingIntent proofingIntent = m_settings . proofingIntent ;
if ( m_settings . proofingIntent = = RenderingIntent : : Auto )
{
proofingIntent = intent ;
}
transform = cmsCreateProofingTransform ( input , getProfileDataFormat ( input ) , output , isRGB888Buffer ? TYPE_RGB_8 : TYPE_RGB_FLT , proofingProfile ,
getLittleCMSRenderingIntent ( intent ) , getLittleCMSRenderingIntent ( proofingIntent ) , getTransformationFlags ( ) ) ;
}
else
{
transform = cmsCreateTransform ( input , getProfileDataFormat ( input ) , output , isRGB888Buffer ? TYPE_RGB_8 : TYPE_RGB_FLT , getLittleCMSRenderingIntent ( intent ) , getTransformationFlags ( ) ) ;
}
2019-12-26 16:28:46 +01:00
}
it = m_transformationCache . insert ( std : : make_pair ( key , transform ) ) . first ;
}
// We must return it here to avoid race condition (after current block,
// lock is not locked, because we unlocked lock for reading).
return it - > second ;
}
return it - > second ;
}
cmsUInt32Number PDFLittleCMS : : getTransformationFlags ( ) const
{
2021-03-05 19:21:20 +01:00
// Flag cmsFLAGS_NONEGATIVES is used here to avoid invalid transformation
// between CMYK color space and RGB color space in the Ghent output suite examples.
cmsUInt32Number flags = cmsFLAGS_NOCACHE | cmsFLAGS_NONEGATIVES ;
2019-12-26 16:28:46 +01:00
if ( m_settings . isBlackPointCompensationActive )
{
flags | = cmsFLAGS_BLACKPOINTCOMPENSATION ;
}
switch ( m_settings . accuracy )
{
case PDFCMSSettings : : Accuracy : : Low :
flags | = cmsFLAGS_LOWRESPRECALC ;
break ;
case PDFCMSSettings : : Accuracy : : Medium :
break ;
case PDFCMSSettings : : Accuracy : : High :
flags | = cmsFLAGS_HIGHRESPRECALC ;
break ;
default :
Q_ASSERT ( false ) ;
break ;
}
2020-12-25 19:01:08 +01:00
if ( m_settings . isGamutChecking )
{
flags | = cmsFLAGS_GAMUTCHECK ;
}
if ( m_settings . isSoftProofing )
{
flags | = cmsFLAGS_SOFTPROOFING ;
}
2019-12-26 16:28:46 +01:00
return flags ;
}
RenderingIntent PDFLittleCMS : : getEffectiveRenderingIntent ( RenderingIntent intent ) const
{
if ( m_settings . intent ! = RenderingIntent : : Auto )
{
return m_settings . intent ;
}
return intent ;
}
cmsUInt32Number PDFLittleCMS : : getLittleCMSRenderingIntent ( RenderingIntent intent )
{
switch ( intent )
{
case RenderingIntent : : Perceptual :
return INTENT_PERCEPTUAL ;
case RenderingIntent : : AbsoluteColorimetric :
return INTENT_ABSOLUTE_COLORIMETRIC ;
case RenderingIntent : : RelativeColorimetric :
return INTENT_RELATIVE_COLORIMETRIC ;
case RenderingIntent : : Saturation :
return INTENT_SATURATION ;
default :
Q_ASSERT ( false ) ;
break ;
}
return INTENT_PERCEPTUAL ;
}
cmsUInt32Number PDFLittleCMS : : getProfileDataFormat ( cmsHPROFILE profile )
{
cmsColorSpaceSignature signature = cmsGetColorSpace ( profile ) ;
switch ( signature )
{
case cmsSigGrayData :
return TYPE_GRAY_FLT ;
case cmsSigRgbData :
return TYPE_RGB_FLT ;
case cmsSigCmykData :
return TYPE_CMYK_FLT ;
case cmsSigXYZData :
return TYPE_XYZ_FLT ;
default :
break ;
}
return 0 ;
}
QColor PDFLittleCMS : : getColorFromOutputColor ( std : : array < float , 3 > color01 )
{
QColor color ( QColor : : Rgb ) ;
color . setRgbF ( qBound ( 0.0f , color01 [ 0 ] , 1.0f ) , qBound ( 0.0f , color01 [ 1 ] , 1.0f ) , qBound ( 0.0f , color01 [ 2 ] , 1.0f ) ) ;
return color ;
}
2021-01-07 18:46:44 +01:00
QByteArray PDFLittleCMS : : getTransformColorSpaceKey ( const PDFCMS : : ColorSpaceTransformParams & params )
{
QByteArray key ;
QBuffer buffer ( & key ) ;
buffer . open ( QBuffer : : WriteOnly ) ;
QDataStream stream ( & buffer ) ;
stream < < params . sourceType ;
stream < < params . sourceIccId ;
stream < < params . targetType ;
stream < < params . targetIccId ;
stream < < params . intent ;
buffer . close ( ) ;
return key ;
}
cmsHTRANSFORM PDFLittleCMS : : getTransformBetweenColorSpaces ( const PDFCMS : : ColorSpaceTransformParams & params ) const
{
QByteArray key = getTransformColorSpaceKey ( params ) ;
QReadLocker lock ( & m_transformColorSpaceCacheLock ) ;
auto it = m_transformColorSpaceCache . find ( key ) ;
if ( it = = m_transformColorSpaceCache . cend ( ) )
{
lock . unlock ( ) ;
QWriteLocker writeLock ( & m_transformColorSpaceCacheLock ) ;
// Now, we have locked cache for writing. We must find out,
// if some other thread doesn't created the transformation already.
it = m_transformColorSpaceCache . find ( key ) ;
if ( it = = m_transformColorSpaceCache . cend ( ) )
{
cmsHPROFILE inputProfile = cmsHPROFILE ( ) ;
cmsHPROFILE outputProfile = cmsHPROFILE ( ) ;
cmsHTRANSFORM transform = cmsHTRANSFORM ( ) ;
switch ( params . sourceType )
{
case ColorSpaceType : : DeviceGray :
inputProfile = m_profiles [ Gray ] ;
break ;
case ColorSpaceType : : DeviceRGB :
inputProfile = m_profiles [ RGB ] ;
break ;
case ColorSpaceType : : DeviceCMYK :
inputProfile = m_profiles [ CMYK ] ;
break ;
case ColorSpaceType : : XYZ :
inputProfile = m_profiles [ XYZ ] ;
break ;
case ColorSpaceType : : ICC :
inputProfile = cmsOpenProfileFromMem ( params . sourceIccData . data ( ) , params . sourceIccData . size ( ) ) ;
break ;
default :
Q_ASSERT ( false ) ;
break ;
}
switch ( params . targetType )
{
case ColorSpaceType : : DeviceGray :
outputProfile = m_profiles [ Gray ] ;
break ;
case ColorSpaceType : : DeviceRGB :
outputProfile = m_profiles [ RGB ] ;
break ;
case ColorSpaceType : : DeviceCMYK :
outputProfile = m_profiles [ CMYK ] ;
break ;
case ColorSpaceType : : XYZ :
outputProfile = m_profiles [ XYZ ] ;
break ;
case ColorSpaceType : : ICC :
outputProfile = cmsOpenProfileFromMem ( params . targetIccData . data ( ) , params . targetIccData . size ( ) ) ;
break ;
default :
Q_ASSERT ( false ) ;
break ;
}
if ( inputProfile & & outputProfile )
{
transform = cmsCreateTransform ( inputProfile , getProfileDataFormat ( inputProfile ) , outputProfile , getProfileDataFormat ( outputProfile ) , getLittleCMSRenderingIntent ( params . intent ) , getTransformationFlags ( ) ) ;
}
if ( params . sourceType = = ColorSpaceType : : ICC )
{
cmsCloseProfile ( inputProfile ) ;
}
if ( params . targetType = = ColorSpaceType : : ICC )
{
cmsCloseProfile ( outputProfile ) ;
}
it = m_transformColorSpaceCache . insert ( std : : make_pair ( key , transform ) ) . first ;
}
return it - > second ;
}
else
{
return it - > second ;
}
return cmsHTRANSFORM ( ) ;
}
2019-12-25 14:34:37 +01:00
QString getInfoFromProfile ( cmsHPROFILE profile , cmsInfoType infoType )
{
QLocale locale ;
QString country = QLocale : : countryToString ( locale . country ( ) ) ;
QString language = QLocale : : languageToString ( locale . language ( ) ) ;
char countryCode [ 3 ] = { } ;
char languageCode [ 3 ] = { } ;
if ( country . size ( ) = = 2 )
{
countryCode [ 0 ] = country [ 0 ] . toLatin1 ( ) ;
countryCode [ 1 ] = country [ 1 ] . toLatin1 ( ) ;
}
if ( language . size ( ) = = 2 )
{
languageCode [ 0 ] = language [ 0 ] . toLatin1 ( ) ;
languageCode [ 1 ] = language [ 1 ] . toLatin1 ( ) ;
}
// Jakub Melka: try to get profile info from current language/country.
// If it fails, then pick any language/any country.
cmsUInt32Number bufferSize = cmsGetProfileInfo ( profile , infoType , languageCode , countryCode , nullptr , 0 ) ;
if ( bufferSize )
{
std : : vector < wchar_t > buffer ( bufferSize , 0 ) ;
cmsGetProfileInfo ( profile , infoType , languageCode , countryCode , buffer . data ( ) , static_cast < cmsUInt32Number > ( buffer . size ( ) ) ) ;
return QString : : fromWCharArray ( buffer . data ( ) ) ;
}
bufferSize = cmsGetProfileInfo ( profile , infoType , cmsNoLanguage , cmsNoCountry , nullptr , 0 ) ;
if ( bufferSize )
{
std : : vector < wchar_t > buffer ( bufferSize , 0 ) ;
cmsGetProfileInfo ( profile , infoType , cmsNoLanguage , cmsNoCountry , buffer . data ( ) , static_cast < cmsUInt32Number > ( buffer . size ( ) ) ) ;
return QString : : fromWCharArray ( buffer . data ( ) ) ;
}
return QString ( ) ;
}
2019-12-24 17:29:40 +01:00
bool PDFCMSGeneric : : isCompatible ( const PDFCMSSettings & settings ) const
{
return settings . system = = PDFCMSSettings : : System : : Generic ;
}
2019-12-25 17:56:17 +01:00
QColor PDFCMSGeneric : : getPaperColor ( ) const
{
return QColor ( Qt : : white ) ;
}
2019-12-24 17:29:40 +01:00
QColor PDFCMSGeneric : : getColorFromDeviceGray ( const PDFColor & color , RenderingIntent intent , PDFRenderErrorReporter * reporter ) const
{
Q_UNUSED ( color ) ;
Q_UNUSED ( intent ) ;
Q_UNUSED ( reporter ) ;
return QColor ( ) ;
}
QColor PDFCMSGeneric : : getColorFromDeviceRGB ( const PDFColor & color , RenderingIntent intent , PDFRenderErrorReporter * reporter ) const
{
Q_UNUSED ( color ) ;
Q_UNUSED ( intent ) ;
Q_UNUSED ( reporter ) ;
return QColor ( ) ;
}
QColor PDFCMSGeneric : : getColorFromDeviceCMYK ( const PDFColor & color , RenderingIntent intent , PDFRenderErrorReporter * reporter ) const
{
Q_UNUSED ( color ) ;
Q_UNUSED ( intent ) ;
Q_UNUSED ( reporter ) ;
return QColor ( ) ;
}
2019-12-25 17:56:17 +01:00
QColor PDFCMSGeneric : : getColorFromXYZ ( const PDFColor3 & whitePoint , const PDFColor3 & color , RenderingIntent intent , PDFRenderErrorReporter * reporter ) const
2019-12-24 17:29:40 +01:00
{
Q_UNUSED ( color ) ;
Q_UNUSED ( intent ) ;
Q_UNUSED ( reporter ) ;
Q_UNUSED ( whitePoint ) ;
return QColor ( ) ;
}
2019-12-26 17:57:45 +01:00
QColor PDFCMSGeneric : : getColorFromICC ( const PDFColor & color ,
RenderingIntent renderingIntent ,
const QByteArray & iccID ,
const QByteArray & iccData ,
PDFRenderErrorReporter * reporter ) const
2019-12-25 18:42:54 +01:00
{
Q_UNUSED ( color ) ;
2019-12-26 17:57:45 +01:00
Q_UNUSED ( renderingIntent ) ;
2019-12-25 18:42:54 +01:00
Q_UNUSED ( iccID ) ;
Q_UNUSED ( iccData ) ;
2019-12-26 17:57:45 +01:00
Q_UNUSED ( reporter ) ;
2019-12-25 18:42:54 +01:00
return QColor ( ) ;
}
2019-12-30 16:08:48 +01:00
bool PDFCMSGeneric : : fillRGBBufferFromDeviceGray ( const std : : vector < float > & colors , RenderingIntent intent , unsigned char * outputBuffer , PDFRenderErrorReporter * reporter ) const
{
Q_UNUSED ( colors ) ;
Q_UNUSED ( intent ) ;
Q_UNUSED ( outputBuffer ) ;
Q_UNUSED ( reporter ) ;
return false ;
}
bool PDFCMSGeneric : : fillRGBBufferFromDeviceRGB ( const std : : vector < float > & colors , RenderingIntent intent , unsigned char * outputBuffer , PDFRenderErrorReporter * reporter ) const
{
Q_UNUSED ( colors ) ;
Q_UNUSED ( intent ) ;
Q_UNUSED ( outputBuffer ) ;
Q_UNUSED ( reporter ) ;
return false ;
}
bool PDFCMSGeneric : : fillRGBBufferFromDeviceCMYK ( const std : : vector < float > & colors , RenderingIntent intent , unsigned char * outputBuffer , PDFRenderErrorReporter * reporter ) const
{
Q_UNUSED ( colors ) ;
Q_UNUSED ( intent ) ;
Q_UNUSED ( outputBuffer ) ;
Q_UNUSED ( reporter ) ;
return false ;
}
bool PDFCMSGeneric : : fillRGBBufferFromXYZ ( const PDFColor3 & whitePoint , const std : : vector < float > & colors , RenderingIntent intent , unsigned char * outputBuffer , PDFRenderErrorReporter * reporter ) const
{
Q_UNUSED ( whitePoint ) ;
Q_UNUSED ( colors ) ;
Q_UNUSED ( intent ) ;
Q_UNUSED ( outputBuffer ) ;
Q_UNUSED ( reporter ) ;
return false ;
}
bool PDFCMSGeneric : : fillRGBBufferFromICC ( const std : : vector < float > & colors , RenderingIntent renderingIntent , unsigned char * outputBuffer , const QByteArray & iccID , const QByteArray & iccData , PDFRenderErrorReporter * reporter ) const
{
Q_UNUSED ( colors ) ;
Q_UNUSED ( renderingIntent ) ;
Q_UNUSED ( outputBuffer ) ;
Q_UNUSED ( iccID ) ;
Q_UNUSED ( iccData ) ;
Q_UNUSED ( reporter ) ;
return false ;
}
2021-01-02 18:27:22 +01:00
bool PDFCMSGeneric : : transformColorSpace ( const PDFCMS : : ColorSpaceTransformParams & params ) const
{
2021-01-23 19:03:12 +01:00
Q_UNUSED ( params ) ;
2021-01-02 18:27:22 +01:00
return false ;
}
2019-12-24 17:29:40 +01:00
PDFCMSManager : : PDFCMSManager ( QObject * parent ) :
2019-12-26 16:28:46 +01:00
BaseClass ( parent ) ,
2021-03-06 18:13:21 +01:00
m_document ( nullptr ) ,
m_mutex ( QMutex : : Recursive )
2019-12-24 17:29:40 +01:00
{
}
2019-12-25 17:56:17 +01:00
PDFCMSPointer PDFCMSManager : : getCurrentCMS ( ) const
{
QMutexLocker lock ( & m_mutex ) ;
return m_CMS . get ( this , & PDFCMSManager : : getCurrentCMSImpl ) ;
}
2019-12-25 14:34:37 +01:00
void PDFCMSManager : : setSettings ( const PDFCMSSettings & settings )
{
if ( m_settings ! = settings )
{
// We must ensure, that mutex is not locked, while we are
// sending signal about CMS change.
{
QMutexLocker lock ( & m_mutex ) ;
m_settings = settings ;
2021-03-04 19:39:14 +01:00
clearCache ( ) ;
2019-12-25 14:34:37 +01:00
}
emit colorManagementSystemChanged ( ) ;
}
}
const PDFColorProfileIdentifiers & PDFCMSManager : : getOutputProfiles ( ) const
2019-12-24 17:29:40 +01:00
{
QMutexLocker lock ( & m_mutex ) ;
return m_outputProfiles . get ( this , & PDFCMSManager : : getOutputProfilesImpl ) ;
}
2019-12-25 14:34:37 +01:00
const PDFColorProfileIdentifiers & PDFCMSManager : : getGrayProfiles ( ) const
2019-12-24 17:29:40 +01:00
{
QMutexLocker lock ( & m_mutex ) ;
return m_grayProfiles . get ( this , & PDFCMSManager : : getGrayProfilesImpl ) ;
}
2019-12-25 14:34:37 +01:00
const PDFColorProfileIdentifiers & PDFCMSManager : : getRGBProfiles ( ) const
2019-12-24 17:29:40 +01:00
{
QMutexLocker lock ( & m_mutex ) ;
return m_RGBProfiles . get ( this , & PDFCMSManager : : getRGBProfilesImpl ) ;
}
2019-12-25 14:34:37 +01:00
const PDFColorProfileIdentifiers & PDFCMSManager : : getCMYKProfiles ( ) const
2019-12-24 17:29:40 +01:00
{
QMutexLocker lock ( & m_mutex ) ;
return m_CMYKProfiles . get ( this , & PDFCMSManager : : getCMYKProfilesImpl ) ;
}
2019-12-25 14:34:37 +01:00
const PDFColorProfileIdentifiers & PDFCMSManager : : getExternalProfiles ( ) const
{
// Jakub Melka: do not protect this by mutex, this function is private
// and must be called only from mutex-protected code.
return m_externalProfiles . get ( this , & PDFCMSManager : : getExternalProfilesImpl ) ;
}
2019-12-24 17:29:40 +01:00
PDFCMSSettings PDFCMSManager : : getDefaultSettings ( ) const
{
PDFCMSSettings settings ;
2019-12-25 14:34:37 +01:00
auto getFirstProfileId = [ ] ( const PDFColorProfileIdentifiers & identifiers )
2019-12-24 17:29:40 +01:00
{
if ( ! identifiers . empty ( ) )
{
return identifiers . front ( ) . id ;
}
return QString ( ) ;
} ;
settings . system = PDFCMSSettings : : System : : LittleCMS2 ;
settings . outputCS = getFirstProfileId ( getOutputProfiles ( ) ) ;
settings . deviceGray = getFirstProfileId ( getGrayProfiles ( ) ) ;
settings . deviceRGB = getFirstProfileId ( getRGBProfiles ( ) ) ;
settings . deviceCMYK = getFirstProfileId ( getCMYKProfiles ( ) ) ;
return settings ;
}
2021-03-04 19:39:14 +01:00
void PDFCMSManager : : setDocument ( const PDFDocument * document )
{
std : : optional < QMutexLocker > lock ;
lock . emplace ( & m_mutex ) ;
if ( m_document = = document )
{
return ;
}
m_document = document ;
int i = 0 ;
PDFColorProfileIdentifiers outputIntentProfiles ;
if ( m_document )
{
for ( const PDFOutputIntent & outputIntent : m_document - > getCatalog ( ) - > getOutputIntents ( ) )
{
QByteArray content ;
try
{
// Try to read the profile from the output intent stream. If it fails, then do nothing.
PDFObject outputProfileObject = m_document - > getObject ( outputIntent . getOutputProfile ( ) ) ;
if ( outputProfileObject . isStream ( ) )
{
content = m_document - > getDecodedStream ( outputProfileObject . getStream ( ) ) ;
}
}
catch ( PDFException )
{
continue ;
}
if ( content . isEmpty ( ) )
{
// Decoding of output profile failed. Continue
// with next output profile.
continue ;
}
cmsHPROFILE profile = cmsOpenProfileFromMem ( content . data ( ) , content . size ( ) ) ;
if ( profile )
{
PDFColorProfileIdentifier : : Type csiType = PDFColorProfileIdentifier : : Type : : Invalid ;
const cmsColorSpaceSignature colorSpace = cmsGetColorSpace ( profile ) ;
switch ( colorSpace )
{
case cmsSigGrayData :
csiType = PDFColorProfileIdentifier : : Type : : MemoryGray ;
break ;
case cmsSigRgbData :
csiType = PDFColorProfileIdentifier : : Type : : MemoryRGB ;
break ;
case cmsSigCmykData :
csiType = PDFColorProfileIdentifier : : Type : : MemoryCMYK ;
break ;
default :
break ;
}
QString description = getInfoFromProfile ( profile , cmsInfoDescription ) ;
cmsCloseProfile ( profile ) ;
// If we have a valid profile, then add it
if ( csiType ! = PDFColorProfileIdentifier : : Type : : Invalid )
{
outputIntentProfiles . emplace_back ( PDFColorProfileIdentifier : : createOutputIntent ( csiType , qMove ( description ) , QString ( " @@OUTPUT_INTENT_PROFILE_%1 " ) . arg ( + + i ) , qMove ( content ) ) ) ;
}
}
}
}
bool outputIntentProfilesChanged = false ;
if ( m_outputIntentProfiles ! = outputIntentProfiles )
{
m_outputIntentProfiles = qMove ( outputIntentProfiles ) ;
clearCache ( ) ;
outputIntentProfilesChanged = true ;
}
if ( outputIntentProfilesChanged )
{
lock = std : : nullopt ;
emit colorManagementSystemChanged ( ) ;
}
}
2019-12-24 17:29:40 +01:00
QString PDFCMSManager : : getSystemName ( PDFCMSSettings : : System system )
{
switch ( system )
{
case PDFCMSSettings : : System : : Generic :
return tr ( " Generic " ) ;
case PDFCMSSettings : : System : : LittleCMS2 :
{
const int major = LCMS_VERSION / 1000 ;
const int minor = ( LCMS_VERSION % 1000 ) / 10 ;
return tr ( " Little CMS %1.%2 " ) . arg ( major ) . arg ( minor ) ;
}
default :
{
Q_ASSERT ( false ) ;
break ;
}
}
return QString ( ) ;
}
2019-12-25 17:56:17 +01:00
PDFCMSPointer PDFCMSManager : : getCurrentCMSImpl ( ) const
{
2019-12-26 16:28:46 +01:00
switch ( m_settings . system )
{
case PDFCMSSettings : : System : : Generic :
return PDFCMSPointer ( new PDFCMSGeneric ( ) ) ;
case PDFCMSSettings : : System : : LittleCMS2 :
return PDFCMSPointer ( new PDFLittleCMS ( this , m_settings ) ) ;
default :
Q_ASSERT ( false ) ;
break ;
}
2019-12-25 17:56:17 +01:00
return PDFCMSPointer ( new PDFCMSGeneric ( ) ) ;
}
2021-03-04 19:39:14 +01:00
void PDFCMSManager : : clearCache ( )
{
QMutexLocker lock ( & m_mutex ) ;
m_CMS . dirty ( ) ;
m_outputProfiles . dirty ( ) ;
m_grayProfiles . dirty ( ) ;
m_RGBProfiles . dirty ( ) ;
m_CMYKProfiles . dirty ( ) ;
m_externalProfiles . dirty ( ) ;
}
2019-12-25 14:34:37 +01:00
PDFColorProfileIdentifiers PDFCMSManager : : getOutputProfilesImpl ( ) const
2019-12-24 17:29:40 +01:00
{
// Currently, we only support sRGB output color profile.
2019-12-25 14:34:37 +01:00
return { PDFColorProfileIdentifier : : createSRGB ( ) } ;
2019-12-24 17:29:40 +01:00
}
2019-12-25 14:34:37 +01:00
PDFColorProfileIdentifiers PDFCMSManager : : getGrayProfilesImpl ( ) const
2019-12-24 17:29:40 +01:00
{
// Jakub Melka: We create gray profiles for temperature 5000K, 6500K and 9300K.
// We also use linear gamma and gamma value 2.2.
2019-12-25 14:34:37 +01:00
PDFColorProfileIdentifiers result =
{
PDFColorProfileIdentifier : : createGray ( tr ( " Gray D65, γ = 2.2 " ) , " @GENERIC_Gray_D65_g22 " , 6500.0 , 2.2 ) ,
PDFColorProfileIdentifier : : createGray ( tr ( " Gray D50, γ = 2.2 " ) , " @GENERIC_Gray_D50_g22 " , 5000.0 , 2.2 ) ,
PDFColorProfileIdentifier : : createGray ( tr ( " Gray D93, γ = 2.2 " ) , " @GENERIC_Gray_D93_g22 " , 9300.0 , 2.2 ) ,
PDFColorProfileIdentifier : : createGray ( tr ( " Gray D65, γ = 1.0 (linear) " ) , " @GENERIC_Gray_D65_g10 " , 6500.0 , 1.0 ) ,
PDFColorProfileIdentifier : : createGray ( tr ( " Gray D50, γ = 1.0 (linear) " ) , " @GENERIC_Gray_D50_g10 " , 5000.0 , 1.0 ) ,
PDFColorProfileIdentifier : : createGray ( tr ( " Gray D93, γ = 1.0 (linear) " ) , " @GENERIC_Gray_D93_g10 " , 9300.0 , 1.0 )
2019-12-24 17:29:40 +01:00
} ;
2019-12-25 14:34:37 +01:00
2021-03-04 19:39:14 +01:00
PDFColorProfileIdentifiers externalGrayProfiles = getFilteredExternalProfiles ( PDFColorProfileIdentifier : : Type : : FileGray ) ;
result . insert ( result . end ( ) , std : : make_move_iterator ( externalGrayProfiles . begin ( ) ) , std : : make_move_iterator ( externalGrayProfiles . end ( ) ) ) ;
PDFColorProfileIdentifiers outputIntentRGBProfiles = getFilteredOutputIntentProfiles ( PDFColorProfileIdentifier : : Type : : MemoryGray ) ;
result . insert ( result . end ( ) , std : : make_move_iterator ( outputIntentRGBProfiles . begin ( ) ) , std : : make_move_iterator ( outputIntentRGBProfiles . end ( ) ) ) ;
2019-12-25 14:34:37 +01:00
return result ;
2019-12-24 17:29:40 +01:00
}
2019-12-25 14:34:37 +01:00
PDFColorProfileIdentifiers PDFCMSManager : : getRGBProfilesImpl ( ) const
2019-12-24 17:29:40 +01:00
{
2021-03-04 19:39:14 +01:00
// Jakub Melka: We create RGB profiles for common standards and also for
2019-12-24 17:29:40 +01:00
// default standard sRGB. See https://en.wikipedia.org/wiki/Color_spaces_with_RGB_primaries.
2019-12-25 14:34:37 +01:00
PDFColorProfileIdentifiers result =
{
PDFColorProfileIdentifier : : createSRGB ( ) ,
PDFColorProfileIdentifier : : createRGB ( tr ( " HDTV (ITU-R BT.709) " ) , " @GENERIC_RGB_HDTV " , 6500 , QPointF ( 0.64 , 0.33 ) , QPointF ( 0.30 , 0.60 ) , QPointF ( 0.15 , 0.06 ) , 20.0 / 9.0 ) ,
PDFColorProfileIdentifier : : createRGB ( tr ( " Adobe RGB 1998 " ) , " @GENERIC_RGB_Adobe1998 " , 6500 , QPointF ( 0.64 , 0.33 ) , QPointF ( 0.30 , 0.60 ) , QPointF ( 0.15 , 0.06 ) , 563.0 / 256.0 ) ,
PDFColorProfileIdentifier : : createRGB ( tr ( " PAL / SECAM " ) , " @GENERIC_RGB_PalSecam " , 6500 , QPointF ( 0.64 , 0.33 ) , QPointF ( 0.29 , 0.60 ) , QPointF ( 0.15 , 0.06 ) , 14.0 / 5.0 ) ,
PDFColorProfileIdentifier : : createRGB ( tr ( " NTSC " ) , " @GENERIC_RGB_NTSC " , 6500 , QPointF ( 0.64 , 0.34 ) , QPointF ( 0.31 , 0.595 ) , QPointF ( 0.155 , 0.07 ) , 20.0 / 9.0 ) ,
PDFColorProfileIdentifier : : createRGB ( tr ( " Adobe Wide Gamut RGB " ) , " @GENERIC_RGB_AdobeWideGamut " , 5000 , QPointF ( 0.735 , 0.265 ) , QPointF ( 0.115 , 0.826 ) , QPointF ( 0.157 , 0.018 ) , 563.0 / 256.0 ) ,
PDFColorProfileIdentifier : : createRGB ( tr ( " ProPhoto RGB " ) , " @GENERIC_RGB_ProPhoto " , 5000 , QPointF ( 0.7347 , 0.2653 ) , QPointF ( 0.1596 , 0.8404 ) , QPointF ( 0.0366 , 0.0001 ) , 9.0 / 5.0 )
2019-12-24 17:29:40 +01:00
} ;
2019-12-25 14:34:37 +01:00
PDFColorProfileIdentifiers externalRGBProfiles = getFilteredExternalProfiles ( PDFColorProfileIdentifier : : Type : : FileRGB ) ;
result . insert ( result . end ( ) , externalRGBProfiles . begin ( ) , externalRGBProfiles . end ( ) ) ;
2021-03-04 19:39:14 +01:00
PDFColorProfileIdentifiers outputIntentRGBProfiles = getFilteredOutputIntentProfiles ( PDFColorProfileIdentifier : : Type : : MemoryRGB ) ;
result . insert ( result . end ( ) , std : : make_move_iterator ( outputIntentRGBProfiles . begin ( ) ) , std : : make_move_iterator ( outputIntentRGBProfiles . end ( ) ) ) ;
2019-12-25 14:34:37 +01:00
return result ;
2019-12-24 17:29:40 +01:00
}
2019-12-25 14:34:37 +01:00
PDFColorProfileIdentifiers PDFCMSManager : : getCMYKProfilesImpl ( ) const
2019-12-24 17:29:40 +01:00
{
2021-03-04 19:39:14 +01:00
PDFColorProfileIdentifiers result ;
PDFColorProfileIdentifiers externalCMYKProfiles = getFilteredExternalProfiles ( PDFColorProfileIdentifier : : Type : : FileCMYK ) ;
result . insert ( result . end ( ) , externalCMYKProfiles . begin ( ) , externalCMYKProfiles . end ( ) ) ;
PDFColorProfileIdentifiers outputIntentCMYKProfiles = getFilteredOutputIntentProfiles ( PDFColorProfileIdentifier : : Type : : MemoryCMYK ) ;
result . insert ( result . end ( ) , std : : make_move_iterator ( outputIntentCMYKProfiles . begin ( ) ) , std : : make_move_iterator ( outputIntentCMYKProfiles . end ( ) ) ) ;
return result ;
2019-12-25 14:34:37 +01:00
}
2019-12-24 17:29:40 +01:00
2019-12-25 14:34:37 +01:00
PDFColorProfileIdentifiers PDFCMSManager : : getExternalColorProfiles ( QString profileDirectory ) const
{
PDFColorProfileIdentifiers result ;
QDir directory ( profileDirectory ) ;
2020-05-03 19:14:53 +02:00
QDir applicationDirectory ( QApplication : : applicationDirPath ( ) ) ;
2019-12-25 14:34:37 +01:00
if ( ! profileDirectory . isEmpty ( ) & & directory . exists ( ) )
{
QStringList iccProfiles = directory . entryList ( { " *.icc " } , QDir : : Files | QDir : : Readable | QDir : : NoDotAndDotDot , QDir : : NoSort ) ;
for ( const QString & fileName : iccProfiles )
{
2020-05-03 19:14:53 +02:00
QString filePath = QDir : : cleanPath ( applicationDirectory . relativeFilePath ( directory . absoluteFilePath ( fileName ) ) ) ;
2019-12-25 14:34:37 +01:00
// Try to read the profile from the file. If it fails, then do nothing.
QFile file ( filePath ) ;
if ( file . open ( QFile : : ReadOnly ) )
{
QByteArray content = file . readAll ( ) ;
file . close ( ) ;
cmsHPROFILE profile = cmsOpenProfileFromMem ( content . data ( ) , content . size ( ) ) ;
if ( profile )
{
PDFColorProfileIdentifier : : Type csiType = PDFColorProfileIdentifier : : Type : : Invalid ;
const cmsColorSpaceSignature colorSpace = cmsGetColorSpace ( profile ) ;
switch ( colorSpace )
{
case cmsSigGrayData :
csiType = PDFColorProfileIdentifier : : Type : : FileGray ;
break ;
case cmsSigRgbData :
csiType = PDFColorProfileIdentifier : : Type : : FileRGB ;
break ;
case cmsSigCmykData :
csiType = PDFColorProfileIdentifier : : Type : : FileCMYK ;
break ;
default :
break ;
}
QString description = getInfoFromProfile ( profile , cmsInfoDescription ) ;
cmsCloseProfile ( profile ) ;
// If we have a valid profile, then add it
if ( csiType ! = PDFColorProfileIdentifier : : Type : : Invalid )
{
result . emplace_back ( PDFColorProfileIdentifier : : createFile ( csiType , qMove ( description ) , filePath ) ) ;
}
}
}
}
}
return result ;
}
PDFColorProfileIdentifiers PDFCMSManager : : getExternalProfilesImpl ( ) const
{
PDFColorProfileIdentifiers result ;
QStringList directories ( m_settings . profileDirectory ) ;
QDir applicationDirectory ( QApplication : : applicationDirPath ( ) ) ;
if ( applicationDirectory . cd ( " colorprofiles " ) )
{
directories < < applicationDirectory . absolutePath ( ) ;
}
for ( const QString & directory : directories )
{
PDFColorProfileIdentifiers externalProfiles = getExternalColorProfiles ( directory ) ;
result . insert ( result . end ( ) , externalProfiles . begin ( ) , externalProfiles . end ( ) ) ;
}
return result ;
}
PDFColorProfileIdentifiers PDFCMSManager : : getFilteredExternalProfiles ( PDFColorProfileIdentifier : : Type type ) const
{
PDFColorProfileIdentifiers result ;
const PDFColorProfileIdentifiers & externalProfiles = getExternalProfiles ( ) ;
std : : copy_if ( externalProfiles . cbegin ( ) , externalProfiles . cend ( ) , std : : back_inserter ( result ) , [ type ] ( const PDFColorProfileIdentifier & identifier ) { return identifier . type = = type ; } ) ;
return result ;
2019-12-24 17:29:40 +01:00
}
2021-03-04 19:39:14 +01:00
PDFColorProfileIdentifiers PDFCMSManager : : getFilteredOutputIntentProfiles ( PDFColorProfileIdentifier : : Type type ) const
{
PDFColorProfileIdentifiers result ;
std : : copy_if ( m_outputIntentProfiles . cbegin ( ) , m_outputIntentProfiles . cend ( ) , std : : back_inserter ( result ) , [ type ] ( const PDFColorProfileIdentifier & identifier ) { return identifier . type = = type ; } ) ;
return result ;
}
2019-12-25 14:34:37 +01:00
PDFColorProfileIdentifier PDFColorProfileIdentifier : : createGray ( QString name , QString id , PDFReal temperature , PDFReal gamma )
2019-12-24 17:29:40 +01:00
{
2019-12-25 14:34:37 +01:00
PDFColorProfileIdentifier result ;
2019-12-24 17:29:40 +01:00
result . type = Type : : Gray ;
result . name = qMove ( name ) ;
result . id = qMove ( id ) ;
result . temperature = temperature ;
result . gamma = gamma ;
return result ;
}
2019-12-25 14:34:37 +01:00
PDFColorProfileIdentifier PDFColorProfileIdentifier : : createSRGB ( )
2019-12-24 17:29:40 +01:00
{
2019-12-25 14:34:37 +01:00
PDFColorProfileIdentifier result ;
2019-12-24 17:29:40 +01:00
result . type = Type : : sRGB ;
result . name = PDFCMSManager : : tr ( " sRGB " ) ;
result . id = " @GENERIC_sRGB " ;
return result ;
}
2019-12-25 14:34:37 +01:00
PDFColorProfileIdentifier PDFColorProfileIdentifier : : createRGB ( QString name , QString id , PDFReal temperature , QPointF primaryR , QPointF primaryG , QPointF primaryB , PDFReal gamma )
2019-12-24 17:29:40 +01:00
{
2019-12-25 14:34:37 +01:00
PDFColorProfileIdentifier result ;
2019-12-24 17:29:40 +01:00
result . type = Type : : RGB ;
result . name = qMove ( name ) ;
result . id = qMove ( id ) ;
result . temperature = temperature ;
result . primaryR = primaryR ;
result . primaryG = primaryG ;
result . primaryB = primaryB ;
result . gamma = gamma ;
return result ;
}
2019-12-25 14:34:37 +01:00
PDFColorProfileIdentifier PDFColorProfileIdentifier : : createFile ( Type type , QString name , QString id )
{
PDFColorProfileIdentifier result ;
result . type = type ;
result . name = qMove ( name ) ;
result . id = qMove ( id ) ;
return result ;
}
2021-03-04 19:39:14 +01:00
PDFColorProfileIdentifier PDFColorProfileIdentifier : : createOutputIntent ( PDFColorProfileIdentifier : : Type type , QString name , QString id , QByteArray profileData )
{
PDFColorProfileIdentifier result ;
result . type = type ;
result . name = qMove ( name ) ;
result . id = qMove ( id ) ;
result . profileMemoryData = qMove ( profileData ) ;
result . isOutputIntentProfile = true ;
return result ;
}
2021-01-02 18:27:22 +01:00
PDFColor3 PDFCMS : : getDefaultXYZWhitepoint ( )
{
const cmsCIEXYZ * whitePoint = cmsD50_XYZ ( ) ;
return PDFColor3 { PDFColorComponent ( whitePoint - > X ) , PDFColorComponent ( whitePoint - > Y ) , PDFColorComponent ( whitePoint - > Z ) } ;
}
2021-05-08 18:16:28 +02:00
PDFColorComponentMatrix_3x3 PDFChromaticAdaptationXYZ : : createWhitepointChromaticAdaptation ( const PDFColor3 & targetWhitePoint ,
const PDFColor3 & sourceWhitePoint ,
PDFCMSSettings : : ColorAdaptationXYZ method )
{
PDFColorComponentMatrix_3x3 matrix ;
matrix . makeIdentity ( ) ;
switch ( method )
{
case pdf : : PDFCMSSettings : : ColorAdaptationXYZ : : None :
// No scaling performed, just return identity matrix
break ;
case pdf : : PDFCMSSettings : : ColorAdaptationXYZ : : XYZScaling :
matrix . makeDiagonal ( std : : array { targetWhitePoint [ 0 ] / sourceWhitePoint [ 0 ] ,
targetWhitePoint [ 1 ] / sourceWhitePoint [ 1 ] ,
targetWhitePoint [ 2 ] / sourceWhitePoint [ 2 ]
} ) ;
break ;
case pdf : : PDFCMSSettings : : ColorAdaptationXYZ : : CAT97 :
{
// CAT97 matrix, as defined in https://en.wikipedia.org/wiki/LMS_color_space
constexpr PDFColorComponentMatrix_3x3 cat97Matrix (
0.8562f , 0.3372f , - 0.1934f ,
- 0.8360f , 1.8327f , 0.0033f ,
0.0357f , - 0.0469f , 1.0112f ) ;
// Inverse of CAT97 matrix (using wxMaxima to compute it)
constexpr PDFColorComponentMatrix_3x3 inverseCat97Matrix (
0.9873999149199271f , - 0.1768250198556842f , 0.1894251049357571f ,
0.4504351090445315f , 0.464932897752711f , 0.08463199320275755f ,
- 0.01396832510725165f , 0.027806572501434f , 0.9861617526058175f ) ;
PDFColor3 adaptedTargetWhitePoint = cat97Matrix * targetWhitePoint ;
PDFColor3 adaptedSourceWhitePoint = cat97Matrix * sourceWhitePoint ;
PDFColor3 gain = {
adaptedTargetWhitePoint [ 0 ] / adaptedSourceWhitePoint [ 0 ] ,
adaptedTargetWhitePoint [ 1 ] / adaptedSourceWhitePoint [ 1 ] ,
adaptedTargetWhitePoint [ 2 ] / adaptedSourceWhitePoint [ 2 ]
} ;
PDFColorComponentMatrix_3x3 gainMatrix ;
gainMatrix . makeDiagonal ( gain ) ;
matrix = inverseCat97Matrix * gainMatrix * cat97Matrix ;
break ;
}
case pdf : : PDFCMSSettings : : ColorAdaptationXYZ : : CAT02 :
{
// CAT02 matrix, as defined in https://en.wikipedia.org/wiki/LMS_color_space
constexpr PDFColorComponentMatrix_3x3 cat02Matrix (
0.7328f , 0.4296f , - 0.1624f ,
- 0.7036f , 1.6975f , 0.0061f ,
0.0030f , 0.0136f , 0.9834f ) ;
// Inverse of CAT02 matrix (using wxMaxima to compute it)
constexpr PDFColorComponentMatrix_3x3 inverseCat02Matrix (
1.096123820835514f , - 0.2788690002182872f , 0.182745179382773f ,
0.4543690419753592f , 0.4735331543074117f , 0.0720978037172291f ,
- 0.009627608738429352f , - 0.005698031216113419f , 1.015325639954543f ) ;
PDFColor3 adaptedTargetWhitePoint = cat02Matrix * targetWhitePoint ;
PDFColor3 adaptedSourceWhitePoint = cat02Matrix * sourceWhitePoint ;
PDFColor3 gain = {
adaptedTargetWhitePoint [ 0 ] / adaptedSourceWhitePoint [ 0 ] ,
adaptedTargetWhitePoint [ 1 ] / adaptedSourceWhitePoint [ 1 ] ,
adaptedTargetWhitePoint [ 2 ] / adaptedSourceWhitePoint [ 2 ]
} ;
PDFColorComponentMatrix_3x3 gainMatrix ;
gainMatrix . makeDiagonal ( gain ) ;
matrix = inverseCat02Matrix * gainMatrix * cat02Matrix ;
break ;
}
case pdf : : PDFCMSSettings : : ColorAdaptationXYZ : : Bradford :
{
// Bradford matrix, as defined in https://en.wikipedia.org/wiki/LMS_color_space
constexpr PDFColorComponentMatrix_3x3 bradfordMatrix (
0.8951f , 0.2264f , - 0.1614f ,
- 0.7502f , 1.7135f , 0.0367f ,
0.0389f , - 0.0685f , 1.0296f ) ;
// Inverse of bradford matrix (using wxMaxima to compute it)
constexpr PDFColorComponentMatrix_3x3 inverseBradfordMatrix (
1.004360519274085f , - 0.1262294327613208f , 0.1619428982062721f ,
0.4399123264001572f , 0.527481594455384f , 0.05015858096782513f ,
- 0.008678739162151443f , 0.03986287311053728f , 0.9684695843590444f ) ;
PDFColor3 adaptedTargetWhitePoint = bradfordMatrix * targetWhitePoint ;
PDFColor3 adaptedSourceWhitePoint = bradfordMatrix * sourceWhitePoint ;
PDFColor3 gain = {
adaptedTargetWhitePoint [ 0 ] / adaptedSourceWhitePoint [ 0 ] ,
adaptedTargetWhitePoint [ 1 ] / adaptedSourceWhitePoint [ 1 ] ,
adaptedTargetWhitePoint [ 2 ] / adaptedSourceWhitePoint [ 2 ]
} ;
PDFColorComponentMatrix_3x3 gainMatrix ;
gainMatrix . makeDiagonal ( gain ) ;
matrix = inverseBradfordMatrix * gainMatrix * bradfordMatrix ;
break ;
}
default :
Q_ASSERT ( false ) ;
break ;
}
return matrix ;
}
2019-12-24 17:29:40 +01:00
} // namespace pdf