blob: eb11777cef503444cecbd24b9f94c3490a6abe41 [file] [log] [blame]
//========================================================================
//
// GfxState.cc
//
// Copyright 1996-2003 Glyph & Cog, LLC
//
//========================================================================
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2005 Kristian Høgsberg <krh@redhat.com>
// Copyright (C) 2006, 2007 Jeff Muizelaar <jeff@infidigm.net>
// Copyright (C) 2006, 2010 Carlos Garcia Campos <carlosgc@gnome.org>
// Copyright (C) 2006-2014 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2009, 2012 Koji Otani <sho@bbr.jp>
// Copyright (C) 2009, 2011-2013 Thomas Freitag <Thomas.Freitag@alfa.de>
// Copyright (C) 2009 Christian Persch <chpe@gnome.org>
// Copyright (C) 2010 Paweł Wiejacha <pawel.wiejacha@gmail.com>
// Copyright (C) 2010 Christian Feuersänger <cfeuersaenger@googlemail.com>
// Copyright (C) 2011 Andrea Canciani <ranma42@gmail.com>
// Copyright (C) 2012 William Bader <williambader@hotmail.com>
// Copyright (C) 2013 Lu Wang <coolwanglu@gmail.com>
// Copyright (C) 2013 Hib Eris <hib@hiberis.nl>
// Copyright (C) 2013 Fabio D'Urso <fabiodurso@hotmail.it>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================
#include <config.h>
#ifdef USE_GCC_PRAGMAS
#pragma implementation
#endif
#include <algorithm>
#include <stddef.h>
#include <math.h>
#include <string.h>
#include "goo/gmem.h"
#include "Error.h"
#include "Object.h"
#include "Array.h"
#include "Page.h"
#include "Gfx.h"
#include "GfxState.h"
#include "GfxState_helpers.h"
#include "GfxFont.h"
#include "GlobalParams.h"
#include "PopplerCache.h"
#include "OutputDev.h"
#include "splash/SplashTypes.h"
//------------------------------------------------------------------------
// Max depth of nested color spaces. This is used to catch infinite
// loops in the color space object structure.
#define colorSpaceRecursionLimit 8
//------------------------------------------------------------------------
GBool Matrix::invertTo(Matrix *other) const
{
double det;
det = 1 / determinant();
other->m[0] = m[3] * det;
other->m[1] = -m[1] * det;
other->m[2] = -m[2] * det;
other->m[3] = m[0] * det;
other->m[4] = (m[2] * m[5] - m[3] * m[4]) * det;
other->m[5] = (m[1] * m[4] - m[0] * m[5]) * det;
return gTrue;
}
void Matrix::transform(double x, double y, double *tx, double *ty) const
{
double temp_x, temp_y;
temp_x = m[0] * x + m[2] * y + m[4];
temp_y = m[1] * x + m[3] * y + m[5];
*tx = temp_x;
*ty = temp_y;
}
// Matrix norm, taken from _cairo_matrix_transformed_circle_major_axis
double Matrix::norm() const
{
double f, g, h, i, j;
i = m[0]*m[0] + m[1]*m[1];
j = m[2]*m[2] + m[3]*m[3];
f = 0.5 * (i + j);
g = 0.5 * (i - j);
h = m[0]*m[2] + m[1]*m[3];
return sqrt (f + hypot (g, h));
}
//------------------------------------------------------------------------
struct GfxBlendModeInfo {
const char *name;
GfxBlendMode mode;
};
static const GfxBlendModeInfo gfxBlendModeNames[] = {
{ "Normal", gfxBlendNormal },
{ "Compatible", gfxBlendNormal },
{ "Multiply", gfxBlendMultiply },
{ "Screen", gfxBlendScreen },
{ "Overlay", gfxBlendOverlay },
{ "Darken", gfxBlendDarken },
{ "Lighten", gfxBlendLighten },
{ "ColorDodge", gfxBlendColorDodge },
{ "ColorBurn", gfxBlendColorBurn },
{ "HardLight", gfxBlendHardLight },
{ "SoftLight", gfxBlendSoftLight },
{ "Difference", gfxBlendDifference },
{ "Exclusion", gfxBlendExclusion },
{ "Hue", gfxBlendHue },
{ "Saturation", gfxBlendSaturation },
{ "Color", gfxBlendColor },
{ "Luminosity", gfxBlendLuminosity }
};
#define nGfxBlendModeNames \
((int)((sizeof(gfxBlendModeNames) / sizeof(GfxBlendModeInfo))))
//------------------------------------------------------------------------
//
// NB: This must match the GfxColorSpaceMode enum defined in
// GfxState.h
static const char *gfxColorSpaceModeNames[] = {
"DeviceGray",
"CalGray",
"DeviceRGB",
"CalRGB",
"DeviceCMYK",
"Lab",
"ICCBased",
"Indexed",
"Separation",
"DeviceN",
"Pattern"
};
#define nGfxColorSpaceModes ((sizeof(gfxColorSpaceModeNames) / sizeof(char *)))
#ifdef USE_CMS
static const std::map<unsigned int, unsigned int>::size_type CMSCACHE_LIMIT = 2048;
#ifdef USE_LCMS1
#include <lcms.h>
#define cmsColorSpaceSignature icColorSpaceSignature
#define cmsSetLogErrorHandler cmsSetErrorHandler
#define cmsSigXYZData icSigXYZData
#define cmsSigLuvData icSigLuvData
#define cmsSigLabData icSigLabData
#define cmsSigYCbCrData icSigYCbCrData
#define cmsSigYxyData icSigYxyData
#define cmsSigRgbData icSigRgbData
#define cmsSigHsvData icSigHsvData
#define cmsSigHlsData icSigHlsData
#define cmsSigCmyData icSigCmyData
#define cmsSig3colorData icSig3colorData
#define cmsSigGrayData icSigGrayData
#define cmsSigCmykData icSigCmykData
#define cmsSig4colorData icSig4colorData
#define cmsSig2colorData icSig2colorData
#define cmsSig5colorData icSig5colorData
#define cmsSig6colorData icSig6colorData
#define cmsSig7colorData icSig7colorData
#define cmsSig8colorData icSig8colorData
#define cmsSig9colorData icSig9colorData
#define cmsSig10colorData icSig10colorData
#define cmsSig11colorData icSig11colorData
#define cmsSig12colorData icSig12colorData
#define cmsSig13colorData icSig13colorData
#define cmsSig14colorData icSig14colorData
#define cmsSig15colorData icSig15colorData
#define LCMS_FLAGS 0
#else
#include <lcms2.h>
#define LCMS_FLAGS cmsFLAGS_NOOPTIMIZE
#endif
#define COLOR_PROFILE_DIR "/ColorProfiles/"
#define GLOBAL_COLOR_PROFILE_DIR POPPLER_DATADIR COLOR_PROFILE_DIR
void GfxColorTransform::doTransform(void *in, void *out, unsigned int size) {
cmsDoTransform(transform, in, out, size);
}
// transformA should be a cmsHTRANSFORM
GfxColorTransform::GfxColorTransform(void *transformA, int cmsIntentA, unsigned int transformPixelTypeA) {
transform = transformA;
refCount = 1;
cmsIntent = cmsIntentA;
transformPixelType = transformPixelTypeA;
}
GfxColorTransform::~GfxColorTransform() {
cmsDeleteTransform(transform);
}
void GfxColorTransform::ref() {
refCount++;
}
unsigned int GfxColorTransform::unref() {
return --refCount;
}
static cmsHPROFILE RGBProfile = NULL;
static GooString *displayProfileName = NULL; // display profile file Name
static cmsHPROFILE displayProfile = NULL; // display profile
static unsigned int displayPixelType = 0;
static GfxColorTransform *XYZ2DisplayTransform = NULL;
// convert color space signature to cmsColor type
static unsigned int getCMSColorSpaceType(cmsColorSpaceSignature cs);
static unsigned int getCMSNChannels(cmsColorSpaceSignature cs);
static cmsHPROFILE loadColorProfile(const char *fileName);
void GfxColorSpace::setDisplayProfile(void *displayProfileA) {
displayProfile = displayProfileA;
if (displayProfile != NULL) {
cmsHTRANSFORM transform;
unsigned int nChannels;
displayPixelType = getCMSColorSpaceType(cmsGetColorSpace(displayProfile));
nChannels = getCMSNChannels(cmsGetColorSpace(displayProfile));
// create transform from XYZ
cmsHPROFILE XYZProfile = cmsCreateXYZProfile();
if ((transform = cmsCreateTransform(XYZProfile, TYPE_XYZ_DBL,
displayProfile,
COLORSPACE_SH(displayPixelType) |
CHANNELS_SH(nChannels) | BYTES_SH(1),
INTENT_RELATIVE_COLORIMETRIC,LCMS_FLAGS)) == 0) {
error(errSyntaxWarning, -1, "Can't create Lab transform");
} else {
XYZ2DisplayTransform = new GfxColorTransform(transform, INTENT_RELATIVE_COLORIMETRIC, displayPixelType);
}
cmsCloseProfile(XYZProfile);
}
}
void GfxColorSpace::setDisplayProfileName(GooString *name) {
displayProfileName = name->copy();
}
cmsHPROFILE GfxColorSpace::getRGBProfile() {
return RGBProfile;
}
cmsHPROFILE GfxColorSpace::getDisplayProfile() {
return displayProfile;
}
#endif
//------------------------------------------------------------------------
// GfxColorSpace
//------------------------------------------------------------------------
GfxColorSpace::GfxColorSpace() {
overprintMask = 0x0f;
mapping = NULL;
}
GfxColorSpace::~GfxColorSpace() {
}
GfxColorSpace *GfxColorSpace::parse(Object *csObj, OutputDev *out, GfxState *state, int recursion) {
GfxColorSpace *cs;
Object obj1;
if (recursion > colorSpaceRecursionLimit) {
error(errSyntaxError, -1, "Loop detected in color space objects");
return NULL;
}
cs = NULL;
if (csObj->isName()) {
if (csObj->isName("DeviceGray") || csObj->isName("G")) {
cs = new GfxDeviceGrayColorSpace();
} else if (csObj->isName("DeviceRGB") || csObj->isName("RGB")) {
cs = new GfxDeviceRGBColorSpace();
} else if (csObj->isName("DeviceCMYK") || csObj->isName("CMYK")) {
cs = new GfxDeviceCMYKColorSpace();
} else if (csObj->isName("Pattern")) {
cs = new GfxPatternColorSpace(NULL);
} else {
error(errSyntaxWarning, -1, "Bad color space '{0:s}'", csObj->getName());
}
} else if (csObj->isArray() && csObj->arrayGetLength() > 0) {
csObj->arrayGet(0, &obj1);
if (obj1.isName("DeviceGray") || obj1.isName("G")) {
cs = new GfxDeviceGrayColorSpace();
} else if (obj1.isName("DeviceRGB") || obj1.isName("RGB")) {
cs = new GfxDeviceRGBColorSpace();
} else if (obj1.isName("DeviceCMYK") || obj1.isName("CMYK")) {
cs = new GfxDeviceCMYKColorSpace();
} else if (obj1.isName("CalGray")) {
cs = GfxCalGrayColorSpace::parse(csObj->getArray(), state);
} else if (obj1.isName("CalRGB")) {
cs = GfxCalRGBColorSpace::parse(csObj->getArray(), state);
} else if (obj1.isName("Lab")) {
cs = GfxLabColorSpace::parse(csObj->getArray(), state);
} else if (obj1.isName("ICCBased")) {
cs = GfxICCBasedColorSpace::parse(csObj->getArray(), out, state, recursion);
} else if (obj1.isName("Indexed") || obj1.isName("I")) {
cs = GfxIndexedColorSpace::parse(csObj->getArray(), out, state, recursion);
} else if (obj1.isName("Separation")) {
cs = GfxSeparationColorSpace::parse(csObj->getArray(), out, state, recursion);
} else if (obj1.isName("DeviceN")) {
cs = GfxDeviceNColorSpace::parse(csObj->getArray(), out, state, recursion);
} else if (obj1.isName("Pattern")) {
cs = GfxPatternColorSpace::parse(csObj->getArray(), out, state, recursion);
} else {
error(errSyntaxWarning, -1, "Bad color space");
}
obj1.free();
} else if (csObj->isDict()) {
csObj->dictLookup("ColorSpace", &obj1);
if (obj1.isName("DeviceGray")) {
cs = new GfxDeviceGrayColorSpace();
} else if (obj1.isName("DeviceRGB")) {
cs = new GfxDeviceRGBColorSpace();
} else if (obj1.isName("DeviceCMYK")) {
cs = new GfxDeviceCMYKColorSpace();
} else {
error(errSyntaxWarning, -1, "Bad color space dict'");
}
obj1.free();
} else {
error(errSyntaxWarning, -1, "Bad color space - expected name or array or dict");
}
return cs;
}
void GfxColorSpace::createMapping(GooList *separationList, int maxSepComps) {
return;
}
void GfxColorSpace::getDefaultRanges(double *decodeLow, double *decodeRange,
int maxImgPixel) {
int i;
for (i = 0; i < getNComps(); ++i) {
decodeLow[i] = 0;
decodeRange[i] = 1;
}
}
int GfxColorSpace::getNumColorSpaceModes() {
return nGfxColorSpaceModes;
}
const char *GfxColorSpace::getColorSpaceModeName(int idx) {
return gfxColorSpaceModeNames[idx];
}
#ifdef USE_CMS
cmsHPROFILE loadColorProfile(const char *fileName)
{
cmsHPROFILE hp = NULL;
FILE *fp;
if (fileName[0] == '/') {
// full path
// check if open the file
if ((fp = fopen(fileName,"r")) != NULL) {
fclose(fp);
hp = cmsOpenProfileFromFile(fileName,"r");
}
return hp;
}
// try to load from global directory
GooString *path = new GooString(GLOBAL_COLOR_PROFILE_DIR);
path->append(fileName);
// check if open the file
if ((fp = fopen(path->getCString(),"r")) != NULL) {
fclose(fp);
hp = cmsOpenProfileFromFile(path->getCString(),"r");
}
delete path;
return hp;
}
#ifdef USE_LCMS1
static int CMSError(int ecode, const char *msg)
{
error(errSyntaxWarning, -1, "{0:s}", msg);
return 1;
}
#else
static void CMSError(cmsContext /*contextId*/, cmsUInt32Number /*ecode*/, const char *text)
{
error(errSyntaxWarning, -1, "{0:s}", text);
}
#endif
int GfxColorSpace::setupColorProfiles()
{
static GBool initialized = gFalse;
cmsHTRANSFORM transform;
unsigned int nChannels;
// do only once
if (initialized) return 0;
initialized = gTrue;
// set error handlor
cmsSetLogErrorHandler(CMSError);
if (displayProfile == NULL) {
// load display profile if it was not already loaded.
if (displayProfileName == NULL) {
displayProfile = loadColorProfile("display.icc");
} else if (displayProfileName->getLength() > 0) {
displayProfile = loadColorProfile(displayProfileName->getCString());
}
}
// load RGB profile
RGBProfile = loadColorProfile("RGB.icc");
if (RGBProfile == NULL) {
/* use built in sRGB profile */
RGBProfile = cmsCreate_sRGBProfile();
}
// create transforms
if (displayProfile != NULL) {
displayPixelType = getCMSColorSpaceType(cmsGetColorSpace(displayProfile));
nChannels = getCMSNChannels(cmsGetColorSpace(displayProfile));
// create transform from XYZ
cmsHPROFILE XYZProfile = cmsCreateXYZProfile();
if ((transform = cmsCreateTransform(XYZProfile, TYPE_XYZ_DBL,
displayProfile,
COLORSPACE_SH(displayPixelType) |
CHANNELS_SH(nChannels) | BYTES_SH(1),
INTENT_RELATIVE_COLORIMETRIC,LCMS_FLAGS)) == 0) {
error(errSyntaxWarning, -1, "Can't create Lab transform");
} else {
XYZ2DisplayTransform = new GfxColorTransform(transform, INTENT_RELATIVE_COLORIMETRIC, displayPixelType);
}
cmsCloseProfile(XYZProfile);
}
return 0;
}
unsigned int getCMSColorSpaceType(cmsColorSpaceSignature cs)
{
switch (cs) {
case cmsSigXYZData:
return PT_XYZ;
break;
case cmsSigLabData:
return PT_Lab;
break;
case cmsSigLuvData:
return PT_YUV;
break;
case cmsSigYCbCrData:
return PT_YCbCr;
break;
case cmsSigYxyData:
return PT_Yxy;
break;
case cmsSigRgbData:
return PT_RGB;
break;
case cmsSigGrayData:
return PT_GRAY;
break;
case cmsSigHsvData:
return PT_HSV;
break;
case cmsSigHlsData:
return PT_HLS;
break;
case cmsSigCmykData:
return PT_CMYK;
break;
case cmsSigCmyData:
return PT_CMY;
break;
case cmsSig2colorData:
case cmsSig3colorData:
case cmsSig4colorData:
case cmsSig5colorData:
case cmsSig6colorData:
case cmsSig7colorData:
case cmsSig8colorData:
case cmsSig9colorData:
case cmsSig10colorData:
case cmsSig11colorData:
case cmsSig12colorData:
case cmsSig13colorData:
case cmsSig14colorData:
case cmsSig15colorData:
default:
break;
}
return PT_RGB;
}
unsigned int getCMSNChannels(cmsColorSpaceSignature cs)
{
switch (cs) {
case cmsSigXYZData:
case cmsSigLuvData:
case cmsSigLabData:
case cmsSigYCbCrData:
case cmsSigYxyData:
case cmsSigRgbData:
case cmsSigHsvData:
case cmsSigHlsData:
case cmsSigCmyData:
case cmsSig3colorData:
return 3;
break;
case cmsSigGrayData:
return 1;
break;
case cmsSigCmykData:
case cmsSig4colorData:
return 4;
break;
case cmsSig2colorData:
return 2;
break;
case cmsSig5colorData:
return 5;
break;
case cmsSig6colorData:
return 6;
break;
case cmsSig7colorData:
return 7;
break;
case cmsSig8colorData:
return 8;
break;
case cmsSig9colorData:
return 9;
break;
case cmsSig10colorData:
return 10;
break;
case cmsSig11colorData:
return 11;
break;
case cmsSig12colorData:
return 12;
break;
case cmsSig13colorData:
return 13;
break;
case cmsSig14colorData:
return 14;
break;
case cmsSig15colorData:
return 15;
default:
break;
}
return 3;
}
#endif
//------------------------------------------------------------------------
// GfxDeviceGrayColorSpace
//------------------------------------------------------------------------
GfxDeviceGrayColorSpace::GfxDeviceGrayColorSpace() {
}
GfxDeviceGrayColorSpace::~GfxDeviceGrayColorSpace() {
}
GfxColorSpace *GfxDeviceGrayColorSpace::copy() {
return new GfxDeviceGrayColorSpace();
}
void GfxDeviceGrayColorSpace::getGray(GfxColor *color, GfxGray *gray) {
*gray = clip01(color->c[0]);
}
void GfxDeviceGrayColorSpace::getGrayLine(Guchar *in, Guchar *out, int length) {
memcpy (out, in, length);
}
void GfxDeviceGrayColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
rgb->r = rgb->g = rgb->b = clip01(color->c[0]);
}
void GfxDeviceGrayColorSpace::getRGBLine(Guchar *in, unsigned int *out,
int length) {
int i;
for (i = 0; i < length; i++)
out[i] = (in[i] << 16) | (in[i] << 8) | (in[i] << 0);
}
void GfxDeviceGrayColorSpace::getRGBLine(Guchar *in, Guchar *out, int length) {
for (int i = 0; i < length; i++) {
*out++ = in[i];
*out++ = in[i];
*out++ = in[i];
}
}
void GfxDeviceGrayColorSpace::getRGBXLine(Guchar *in, Guchar *out, int length) {
for (int i = 0; i < length; i++) {
*out++ = in[i];
*out++ = in[i];
*out++ = in[i];
*out++ = 255;
}
}
void GfxDeviceGrayColorSpace::getCMYKLine(Guchar *in, Guchar *out, int length) {
for (int i = 0; i < length; i++) {
*out++ = 0;
*out++ = 0;
*out++ = 0;
*out++ = in[i];
}
}
void GfxDeviceGrayColorSpace::getDeviceNLine(Guchar *in, Guchar *out, int length) {
for (int i = 0; i < length; i++) {
for (int j = 0; j < SPOT_NCOMPS+4; j++)
out[j] = 0;
out[4] = in[i];
out += (SPOT_NCOMPS+4);
}
}
void GfxDeviceGrayColorSpace::getCMYK(GfxColor *color, GfxCMYK *cmyk) {
cmyk->c = cmyk->m = cmyk->y = 0;
cmyk->k = clip01(gfxColorComp1 - color->c[0]);
}
void GfxDeviceGrayColorSpace::getDeviceN(GfxColor *color, GfxColor *deviceN) {
for (int i = 0; i < gfxColorMaxComps; i++)
deviceN->c[i] = 0;
deviceN->c[3] = clip01(gfxColorComp1 - color->c[0]);
}
void GfxDeviceGrayColorSpace::getDefaultColor(GfxColor *color) {
color->c[0] = 0;
}
//------------------------------------------------------------------------
// GfxCalGrayColorSpace
//------------------------------------------------------------------------
GfxCalGrayColorSpace::GfxCalGrayColorSpace() {
whiteX = whiteY = whiteZ = 1;
blackX = blackY = blackZ = 0;
gamma = 1;
}
GfxCalGrayColorSpace::~GfxCalGrayColorSpace() {
#ifdef USE_CMS
if (transform != NULL) {
if (transform->unref() == 0) delete transform;
}
#endif
}
GfxColorSpace *GfxCalGrayColorSpace::copy() {
GfxCalGrayColorSpace *cs;
cs = new GfxCalGrayColorSpace();
cs->whiteX = whiteX;
cs->whiteY = whiteY;
cs->whiteZ = whiteZ;
cs->blackX = blackX;
cs->blackY = blackY;
cs->blackZ = blackZ;
cs->gamma = gamma;
#ifdef USE_CMS
cs->transform = transform;
if (transform != NULL) transform->ref();
#endif
return cs;
}
// This is the inverse of MatrixLMN in Example 4.10 from the PostScript
// Language Reference, Third Edition.
static const double xyzrgb[3][3] = {
{ 3.240449, -1.537136, -0.498531 },
{ -0.969265, 1.876011, 0.041556 },
{ 0.055643, -0.204026, 1.057229 }
};
GfxColorSpace *GfxCalGrayColorSpace::parse(Array *arr, GfxState *state) {
GfxCalGrayColorSpace *cs;
Object obj1, obj2, obj3;
arr->get(1, &obj1);
if (!obj1.isDict()) {
error(errSyntaxWarning, -1, "Bad CalGray color space");
obj1.free();
return NULL;
}
cs = new GfxCalGrayColorSpace();
if (obj1.dictLookup("WhitePoint", &obj2)->isArray() &&
obj2.arrayGetLength() == 3) {
obj2.arrayGet(0, &obj3);
if (likely(obj3.isNum()))
cs->whiteX = obj3.getNum();
obj3.free();
obj2.arrayGet(1, &obj3);
if (likely(obj3.isNum()))
cs->whiteY = obj3.getNum();
obj3.free();
obj2.arrayGet(2, &obj3);
if (likely(obj3.isNum()))
cs->whiteZ = obj3.getNum();
obj3.free();
}
obj2.free();
if (obj1.dictLookup("BlackPoint", &obj2)->isArray() &&
obj2.arrayGetLength() == 3) {
obj2.arrayGet(0, &obj3);
if (likely(obj3.isNum()))
cs->blackX = obj3.getNum();
obj3.free();
obj2.arrayGet(1, &obj3);
if (likely(obj3.isNum()))
cs->blackY = obj3.getNum();
obj3.free();
obj2.arrayGet(2, &obj3);
if (likely(obj3.isNum()))
cs->blackZ = obj3.getNum();
obj3.free();
}
obj2.free();
if (obj1.dictLookup("Gamma", &obj2)->isNum()) {
cs->gamma = obj2.getNum();
}
obj2.free();
obj1.free();
cs->kr = 1 / (xyzrgb[0][0] * cs->whiteX +
xyzrgb[0][1] * cs->whiteY +
xyzrgb[0][2] * cs->whiteZ);
cs->kg = 1 / (xyzrgb[1][0] * cs->whiteX +
xyzrgb[1][1] * cs->whiteY +
xyzrgb[1][2] * cs->whiteZ);
cs->kb = 1 / (xyzrgb[2][0] * cs->whiteX +
xyzrgb[2][1] * cs->whiteY +
xyzrgb[2][2] * cs->whiteZ);
#ifdef USE_CMS
cs->transform = (state != NULL) ? state->getXYZ2DisplayTransform() : XYZ2DisplayTransform;
if (cs->transform != NULL) cs->transform->ref();
#endif
return cs;
}
// convert CalGray to media XYZ color space
// (not multiply by the white point)
void GfxCalGrayColorSpace::getXYZ(GfxColor *color,
double *pX, double *pY, double *pZ) {
const double A = colToDbl(color->c[0]);
const double xyzColor = pow(A,gamma);
*pX = xyzColor;
*pY = xyzColor;
*pZ = xyzColor;
}
void GfxCalGrayColorSpace::getGray(GfxColor *color, GfxGray *gray) {
GfxRGB rgb;
#ifdef USE_CMS
if (transform != NULL && transform->getTransformPixelType() == PT_GRAY) {
Guchar out[gfxColorMaxComps];
double in[gfxColorMaxComps];
double X, Y, Z;
getXYZ(color,&X,&Y,&Z);
in[0] = clip01(X);
in[1] = clip01(Y);
in[2] = clip01(Z);
transform->doTransform(in,out,1);
*gray = byteToCol(out[0]);
return;
}
#endif
getRGB(color, &rgb);
*gray = clip01((GfxColorComp)(0.299 * rgb.r +
0.587 * rgb.g +
0.114 * rgb.b + 0.5));
}
void GfxCalGrayColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
double X, Y, Z;
double r, g, b;
getXYZ(color,&X,&Y,&Z);
#ifdef USE_CMS
if (transform != NULL && transform->getTransformPixelType() == PT_RGB) {
Guchar out[gfxColorMaxComps];
double in[gfxColorMaxComps];
in[0] = clip01(X);
in[1] = clip01(Y);
in[2] = clip01(Z);
transform->doTransform(in,out,1);
rgb->r = byteToCol(out[0]);
rgb->g = byteToCol(out[1]);
rgb->b = byteToCol(out[2]);
return;
}
#endif
X *= whiteX;
Y *= whiteY;
Z *= whiteZ;
// convert XYZ to RGB, including gamut mapping and gamma correction
r = xyzrgb[0][0] * X + xyzrgb[0][1] * Y + xyzrgb[0][2] * Z;
g = xyzrgb[1][0] * X + xyzrgb[1][1] * Y + xyzrgb[1][2] * Z;
b = xyzrgb[2][0] * X + xyzrgb[2][1] * Y + xyzrgb[2][2] * Z;
rgb->r = dblToCol(sqrt(clip01(r * kr)));
rgb->g = dblToCol(sqrt(clip01(g * kg)));
rgb->b = dblToCol(sqrt(clip01(b * kb)));
}
void GfxCalGrayColorSpace::getCMYK(GfxColor *color, GfxCMYK *cmyk) {
GfxRGB rgb;
GfxColorComp c, m, y, k;
#ifdef USE_CMS
if (transform != NULL && transform->getTransformPixelType() == PT_CMYK) {
double in[gfxColorMaxComps];
Guchar out[gfxColorMaxComps];
double X, Y, Z;
getXYZ(color,&X,&Y,&Z);
in[0] = clip01(X);
in[1] = clip01(Y);
in[2] = clip01(Z);
transform->doTransform(in,out,1);
cmyk->c = byteToCol(out[0]);
cmyk->m = byteToCol(out[1]);
cmyk->y = byteToCol(out[2]);
cmyk->k = byteToCol(out[3]);
return;
}
#endif
getRGB(color, &rgb);
c = clip01(gfxColorComp1 - rgb.r);
m = clip01(gfxColorComp1 - rgb.g);
y = clip01(gfxColorComp1 - rgb.b);
k = c;
if (m < k) {
k = m;
}
if (y < k) {
k = y;
}
cmyk->c = c - k;
cmyk->m = m - k;
cmyk->y = y - k;
cmyk->k = k;
}
void GfxCalGrayColorSpace::getDeviceN(GfxColor *color, GfxColor *deviceN) {
GfxCMYK cmyk;
for (int i = 0; i < gfxColorMaxComps; i++)
deviceN->c[i] = 0;
getCMYK(color, &cmyk);
deviceN->c[0] = cmyk.c;
deviceN->c[1] = cmyk.m;
deviceN->c[2] = cmyk.y;
deviceN->c[3] = cmyk.k;
}
void GfxCalGrayColorSpace::getDefaultColor(GfxColor *color) {
color->c[0] = 0;
}
//------------------------------------------------------------------------
// GfxDeviceRGBColorSpace
//------------------------------------------------------------------------
GfxDeviceRGBColorSpace::GfxDeviceRGBColorSpace() {
}
GfxDeviceRGBColorSpace::~GfxDeviceRGBColorSpace() {
}
GfxColorSpace *GfxDeviceRGBColorSpace::copy() {
return new GfxDeviceRGBColorSpace();
}
void GfxDeviceRGBColorSpace::getGray(GfxColor *color, GfxGray *gray) {
*gray = clip01((GfxColorComp)(0.3 * color->c[0] +
0.59 * color->c[1] +
0.11 * color->c[2] + 0.5));
}
void GfxDeviceRGBColorSpace::getGrayLine(Guchar *in, Guchar *out, int length) {
int i;
for (i = 0; i < length; i++) {
out[i] =
(in[i * 3 + 0] * 19595 +
in[i * 3 + 1] * 38469 +
in[i * 3 + 2] * 7472) / 65536;
}
}
void GfxDeviceRGBColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
rgb->r = clip01(color->c[0]);
rgb->g = clip01(color->c[1]);
rgb->b = clip01(color->c[2]);
}
void GfxDeviceRGBColorSpace::getRGBLine(Guchar *in, unsigned int *out,
int length) {
Guchar *p;
int i;
for (i = 0, p = in; i < length; i++, p += 3)
out[i] = (p[0] << 16) | (p[1] << 8) | (p[2] << 0);
}
void GfxDeviceRGBColorSpace::getRGBLine(Guchar *in, Guchar *out, int length) {
for (int i = 0; i < length; i++) {
*out++ = *in++;
*out++ = *in++;
*out++ = *in++;
}
}
void GfxDeviceRGBColorSpace::getRGBXLine(Guchar *in, Guchar *out, int length) {
for (int i = 0; i < length; i++) {
*out++ = *in++;
*out++ = *in++;
*out++ = *in++;
*out++ = 255;
}
}
void GfxDeviceRGBColorSpace::getCMYKLine(Guchar *in, Guchar *out, int length) {
GfxColorComp c, m, y, k;
for (int i = 0; i < length; i++) {
c = byteToCol(255 - *in++);
m = byteToCol(255 - *in++);
y = byteToCol(255 - *in++);
k = c;
if (m < k) {
k = m;
}
if (y < k) {
k = y;
}
*out++ = colToByte(c - k);
*out++ = colToByte(m - k);
*out++ = colToByte(y - k);
*out++ = colToByte(k);
}
}
void GfxDeviceRGBColorSpace::getDeviceNLine(Guchar *in, Guchar *out, int length) {
GfxColorComp c, m, y, k;
for (int i = 0; i < length; i++) {
for (int j = 0; j < SPOT_NCOMPS+4; j++)
out[j] = 0;
c = byteToCol(255 - *in++);
m = byteToCol(255 - *in++);
y = byteToCol(255 - *in++);
k = c;
if (m < k) {
k = m;
}
if (y < k) {
k = y;
}
out[0] = colToByte(c - k);
out[1] = colToByte(m - k);
out[2] = colToByte(y - k);
out[3] = colToByte(k);
out += (SPOT_NCOMPS+4);
}
}
void GfxDeviceRGBColorSpace::getCMYK(GfxColor *color, GfxCMYK *cmyk) {
GfxColorComp c, m, y, k;
c = clip01(gfxColorComp1 - color->c[0]);
m = clip01(gfxColorComp1 - color->c[1]);
y = clip01(gfxColorComp1 - color->c[2]);
k = c;
if (m < k) {
k = m;
}
if (y < k) {
k = y;
}
cmyk->c = c - k;
cmyk->m = m - k;
cmyk->y = y - k;
cmyk->k = k;
}
void GfxDeviceRGBColorSpace::getDeviceN(GfxColor *color, GfxColor *deviceN) {
GfxCMYK cmyk;
for (int i = 0; i < gfxColorMaxComps; i++)
deviceN->c[i] = 0;
getCMYK(color, &cmyk);
deviceN->c[0] = cmyk.c;
deviceN->c[1] = cmyk.m;
deviceN->c[2] = cmyk.y;
deviceN->c[3] = cmyk.k;
}
void GfxDeviceRGBColorSpace::getDefaultColor(GfxColor *color) {
color->c[0] = 0;
color->c[1] = 0;
color->c[2] = 0;
}
//------------------------------------------------------------------------
// GfxCalRGBColorSpace
//------------------------------------------------------------------------
GfxCalRGBColorSpace::GfxCalRGBColorSpace() {
whiteX = whiteY = whiteZ = 1;
blackX = blackY = blackZ = 0;
gammaR = gammaG = gammaB = 1;
mat[0] = 1; mat[1] = 0; mat[2] = 0;
mat[3] = 0; mat[4] = 1; mat[5] = 0;
mat[6] = 0; mat[7] = 0; mat[8] = 1;
}
GfxCalRGBColorSpace::~GfxCalRGBColorSpace() {
#ifdef USE_CMS
if (transform != NULL) {
if (transform->unref() == 0) delete transform;
}
#endif
}
GfxColorSpace *GfxCalRGBColorSpace::copy() {
GfxCalRGBColorSpace *cs;
int i;
cs = new GfxCalRGBColorSpace();
cs->whiteX = whiteX;
cs->whiteY = whiteY;
cs->whiteZ = whiteZ;
cs->blackX = blackX;
cs->blackY = blackY;
cs->blackZ = blackZ;
cs->gammaR = gammaR;
cs->gammaG = gammaG;
cs->gammaB = gammaB;
for (i = 0; i < 9; ++i) {
cs->mat[i] = mat[i];
}
#ifdef USE_CMS
cs->transform = transform;
if (transform != NULL) transform->ref();
#endif
return cs;
}
GfxColorSpace *GfxCalRGBColorSpace::parse(Array *arr, GfxState *state) {
GfxCalRGBColorSpace *cs;
Object obj1, obj2, obj3;
int i;
arr->get(1, &obj1);
if (!obj1.isDict()) {
error(errSyntaxWarning, -1, "Bad CalRGB color space");
obj1.free();
return NULL;
}
cs = new GfxCalRGBColorSpace();
if (obj1.dictLookup("WhitePoint", &obj2)->isArray() &&
obj2.arrayGetLength() == 3) {
obj2.arrayGet(0, &obj3);
if (likely(obj3.isNum()))
cs->whiteX = obj3.getNum();
obj3.free();
obj2.arrayGet(1, &obj3);
if (likely(obj3.isNum()))
cs->whiteY = obj3.getNum();
obj3.free();
obj2.arrayGet(2, &obj3);
if (likely(obj3.isNum()))
cs->whiteZ = obj3.getNum();
obj3.free();
}
obj2.free();
if (obj1.dictLookup("BlackPoint", &obj2)->isArray() &&
obj2.arrayGetLength() == 3) {
obj2.arrayGet(0, &obj3);
if (likely(obj3.isNum()))
cs->blackX = obj3.getNum();
obj3.free();
obj2.arrayGet(1, &obj3);
if (likely(obj3.isNum()))
cs->blackY = obj3.getNum();
obj3.free();
obj2.arrayGet(2, &obj3);
if (likely(obj3.isNum()))
cs->blackZ = obj3.getNum();
obj3.free();
}
obj2.free();
if (obj1.dictLookup("Gamma", &obj2)->isArray() &&
obj2.arrayGetLength() == 3) {
obj2.arrayGet(0, &obj3);
if (likely(obj3.isNum()))
cs->gammaR = obj3.getNum();
obj3.free();
obj2.arrayGet(1, &obj3);
if (likely(obj3.isNum()))
cs->gammaG = obj3.getNum();
obj3.free();
obj2.arrayGet(2, &obj3);
if (likely(obj3.isNum()))
cs->gammaB = obj3.getNum();
obj3.free();
}
obj2.free();
if (obj1.dictLookup("Matrix", &obj2)->isArray() &&
obj2.arrayGetLength() == 9) {
for (i = 0; i < 9; ++i) {
obj2.arrayGet(i, &obj3);
if (likely(obj3.isNum()))
cs->mat[i] = obj3.getNum();
obj3.free();
}
}
obj2.free();
obj1.free();
cs->kr = 1 / (xyzrgb[0][0] * cs->whiteX +
xyzrgb[0][1] * cs->whiteY +
xyzrgb[0][2] * cs->whiteZ);
cs->kg = 1 / (xyzrgb[1][0] * cs->whiteX +
xyzrgb[1][1] * cs->whiteY +
xyzrgb[1][2] * cs->whiteZ);
cs->kb = 1 / (xyzrgb[2][0] * cs->whiteX +
xyzrgb[2][1] * cs->whiteY +
xyzrgb[2][2] * cs->whiteZ);
#ifdef USE_CMS
cs->transform = (state != NULL) ? state->getXYZ2DisplayTransform() : XYZ2DisplayTransform;
if (cs->transform != NULL) cs->transform->ref();
#endif
return cs;
}
// convert CalRGB to XYZ color space
void GfxCalRGBColorSpace::getXYZ(GfxColor *color,
double *pX, double *pY, double *pZ) {
double A, B, C;
A = pow(colToDbl(color->c[0]), gammaR);
B = pow(colToDbl(color->c[1]), gammaG);
C = pow(colToDbl(color->c[2]), gammaB);
*pX = mat[0] * A + mat[3] * B + mat[6] * C;
*pY = mat[1] * A + mat[4] * B + mat[7] * C;
*pZ = mat[2] * A + mat[5] * B + mat[8] * C;
}
void GfxCalRGBColorSpace::getGray(GfxColor *color, GfxGray *gray) {
GfxRGB rgb;
#ifdef USE_CMS
if (transform != NULL && transform->getTransformPixelType() == PT_GRAY) {
Guchar out[gfxColorMaxComps];
double in[gfxColorMaxComps];
double X, Y, Z;
getXYZ(color,&X,&Y,&Z);
in[0] = clip01(X);
in[1] = clip01(Y);
in[2] = clip01(Z);
transform->doTransform(in,out,1);
*gray = byteToCol(out[0]);
return;
}
#endif
getRGB(color, &rgb);
*gray = clip01((GfxColorComp)(0.299 * rgb.r +
0.587 * rgb.g +
0.114 * rgb.b + 0.5));
}
void GfxCalRGBColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
double X, Y, Z;
double r, g, b;
getXYZ(color,&X,&Y,&Z);
#ifdef USE_CMS
if (transform != NULL && transform->getTransformPixelType() == PT_RGB) {
Guchar out[gfxColorMaxComps];
double in[gfxColorMaxComps];
in[0] = clip01(X/whiteX);
in[1] = clip01(Y/whiteY);
in[2] = clip01(Z/whiteZ);
transform->doTransform(in,out,1);
rgb->r = byteToCol(out[0]);
rgb->g = byteToCol(out[1]);
rgb->b = byteToCol(out[2]);
return;
}
#endif
// convert XYZ to RGB, including gamut mapping and gamma correction
r = xyzrgb[0][0] * X + xyzrgb[0][1] * Y + xyzrgb[0][2] * Z;
g = xyzrgb[1][0] * X + xyzrgb[1][1] * Y + xyzrgb[1][2] * Z;
b = xyzrgb[2][0] * X + xyzrgb[2][1] * Y + xyzrgb[2][2] * Z;
rgb->r = dblToCol(sqrt(clip01(r)));
rgb->g = dblToCol(sqrt(clip01(g)));
rgb->b = dblToCol(sqrt(clip01(b)));
}
void GfxCalRGBColorSpace::getCMYK(GfxColor *color, GfxCMYK *cmyk) {
GfxRGB rgb;
GfxColorComp c, m, y, k;
#ifdef USE_CMS
if (transform != NULL && transform->getTransformPixelType() == PT_CMYK) {
double in[gfxColorMaxComps];
Guchar out[gfxColorMaxComps];
double X, Y, Z;
getXYZ(color,&X,&Y,&Z);
in[0] = clip01(X);
in[1] = clip01(Y);
in[2] = clip01(Z);
transform->doTransform(in,out,1);
cmyk->c = byteToCol(out[0]);
cmyk->m = byteToCol(out[1]);
cmyk->y = byteToCol(out[2]);
cmyk->k = byteToCol(out[3]);
return;
}
#endif
getRGB(color, &rgb);
c = clip01(gfxColorComp1 - rgb.r);
m = clip01(gfxColorComp1 - rgb.g);
y = clip01(gfxColorComp1 - rgb.b);
k = c;
if (m < k) {
k = m;
}
if (y < k) {
k = y;
}
cmyk->c = c - k;
cmyk->m = m - k;
cmyk->y = y - k;
cmyk->k = k;
}
void GfxCalRGBColorSpace::getDeviceN(GfxColor *color, GfxColor *deviceN) {
GfxCMYK cmyk;
for (int i = 0; i < gfxColorMaxComps; i++)
deviceN->c[i] = 0;
getCMYK(color, &cmyk);
deviceN->c[0] = cmyk.c;
deviceN->c[1] = cmyk.m;
deviceN->c[2] = cmyk.y;
deviceN->c[3] = cmyk.k;
}
void GfxCalRGBColorSpace::getDefaultColor(GfxColor *color) {
color->c[0] = 0;
color->c[1] = 0;
color->c[2] = 0;
}
//------------------------------------------------------------------------
// GfxDeviceCMYKColorSpace
//------------------------------------------------------------------------
GfxDeviceCMYKColorSpace::GfxDeviceCMYKColorSpace() {
}
GfxDeviceCMYKColorSpace::~GfxDeviceCMYKColorSpace() {
}
GfxColorSpace *GfxDeviceCMYKColorSpace::copy() {
return new GfxDeviceCMYKColorSpace();
}
void GfxDeviceCMYKColorSpace::getGray(GfxColor *color, GfxGray *gray) {
*gray = clip01((GfxColorComp)(gfxColorComp1 - color->c[3]
- 0.3 * color->c[0]
- 0.59 * color->c[1]
- 0.11 * color->c[2] + 0.5));
}
void GfxDeviceCMYKColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
double c, m, y, k, c1, m1, y1, k1, r, g, b;
c = colToDbl(color->c[0]);
m = colToDbl(color->c[1]);
y = colToDbl(color->c[2]);
k = colToDbl(color->c[3]);
c1 = 1 - c;
m1 = 1 - m;
y1 = 1 - y;
k1 = 1 - k;
cmykToRGBMatrixMultiplication(c, m, y, k, c1, m1, y1, k1, r, g, b);
rgb->r = clip01(dblToCol(r));
rgb->g = clip01(dblToCol(g));
rgb->b = clip01(dblToCol(b));
}
static inline void GfxDeviceCMYKColorSpacegetRGBLineHelper(Guchar *&in, double &r, double &g, double &b)
{
double c, m, y, k, c1, m1, y1, k1;
c = byteToDbl(*in++);
m = byteToDbl(*in++);
y = byteToDbl(*in++);
k = byteToDbl(*in++);
c1 = 1 - c;
m1 = 1 - m;
y1 = 1 - y;
k1 = 1 - k;
cmykToRGBMatrixMultiplication(c, m, y, k, c1, m1, y1, k1, r, g, b);
}
void GfxDeviceCMYKColorSpace::getRGBLine(Guchar *in, unsigned int *out, int length)
{
double r, g, b;
for (int i = 0; i < length; i++) {
GfxDeviceCMYKColorSpacegetRGBLineHelper(in, r, g, b);
*out++ = (dblToByte(clip01(r)) << 16) | (dblToByte(clip01(g)) << 8) | dblToByte(clip01(b));
}
}
void GfxDeviceCMYKColorSpace::getRGBLine(Guchar *in, Guchar *out, int length)
{
double r, g, b;
for (int i = 0; i < length; i++) {
GfxDeviceCMYKColorSpacegetRGBLineHelper(in, r, g, b);
*out++ = dblToByte(clip01(r));
*out++ = dblToByte(clip01(g));
*out++ = dblToByte(clip01(b));
}
}
void GfxDeviceCMYKColorSpace::getRGBXLine(Guchar *in, Guchar *out, int length)
{
double r, g, b;
for (int i = 0; i < length; i++) {
GfxDeviceCMYKColorSpacegetRGBLineHelper(in, r, g, b);
*out++ = dblToByte(clip01(r));
*out++ = dblToByte(clip01(g));
*out++ = dblToByte(clip01(b));
*out++ = 255;
}
}
void GfxDeviceCMYKColorSpace::getCMYKLine(Guchar *in, Guchar *out, int length)
{
for (int i = 0; i < length; i++) {
*out++ = *in++;
*out++ = *in++;
*out++ = *in++;
*out++ = *in++;
}
}
void GfxDeviceCMYKColorSpace::getDeviceNLine(Guchar *in, Guchar *out, int length)
{
for (int i = 0; i < length; i++) {
for (int j = 0; j < SPOT_NCOMPS+4; j++)
out[j] = 0;
out[0] = *in++;
out[1] = *in++;
out[2] = *in++;
out[3] = *in++;
out += (SPOT_NCOMPS+4);
}
}
void GfxDeviceCMYKColorSpace::getCMYK(GfxColor *color, GfxCMYK *cmyk) {
cmyk->c = clip01(color->c[0]);
cmyk->m = clip01(color->c[1]);
cmyk->y = clip01(color->c[2]);
cmyk->k = clip01(color->c[3]);
}
void GfxDeviceCMYKColorSpace::getDeviceN(GfxColor *color, GfxColor *deviceN) {
for (int i = 0; i < gfxColorMaxComps; i++)
deviceN->c[i] = 0;
deviceN->c[0] = clip01(color->c[0]);
deviceN->c[1] = clip01(color->c[1]);
deviceN->c[2] = clip01(color->c[2]);
deviceN->c[3] = clip01(color->c[3]);
}
void GfxDeviceCMYKColorSpace::getDefaultColor(GfxColor *color) {
color->c[0] = 0;
color->c[1] = 0;
color->c[2] = 0;
color->c[3] = gfxColorComp1;
}
//------------------------------------------------------------------------
// GfxLabColorSpace
//------------------------------------------------------------------------
GfxLabColorSpace::GfxLabColorSpace() {
whiteX = whiteY = whiteZ = 1;
blackX = blackY = blackZ = 0;
aMin = bMin = -100;
aMax = bMax = 100;
}
GfxLabColorSpace::~GfxLabColorSpace() {
#ifdef USE_CMS
if (transform != NULL) {
if (transform->unref() == 0) delete transform;
}
#endif
}
GfxColorSpace *GfxLabColorSpace::copy() {
GfxLabColorSpace *cs;
cs = new GfxLabColorSpace();
cs->whiteX = whiteX;
cs->whiteY = whiteY;
cs->whiteZ = whiteZ;
cs->blackX = blackX;
cs->blackY = blackY;
cs->blackZ = blackZ;
cs->aMin = aMin;
cs->aMax = aMax;
cs->bMin = bMin;
cs->bMax = bMax;
cs->kr = kr;
cs->kg = kg;
cs->kb = kb;
#ifdef USE_CMS
cs->transform = transform;
if (transform != NULL) transform->ref();
#endif
return cs;
}
GfxColorSpace *GfxLabColorSpace::parse(Array *arr, GfxState *state) {
GfxLabColorSpace *cs;
Object obj1, obj2, obj3;
arr->get(1, &obj1);
if (!obj1.isDict()) {
error(errSyntaxWarning, -1, "Bad Lab color space");
obj1.free();
return NULL;
}
cs = new GfxLabColorSpace();
if (obj1.dictLookup("WhitePoint", &obj2)->isArray() &&
obj2.arrayGetLength() == 3) {
obj2.arrayGet(0, &obj3);
cs->whiteX = obj3.getNum();
obj3.free();
obj2.arrayGet(1, &obj3);
cs->whiteY = obj3.getNum();
obj3.free();
obj2.arrayGet(2, &obj3);
cs->whiteZ = obj3.getNum();
obj3.free();
}
obj2.free();
if (obj1.dictLookup("BlackPoint", &obj2)->isArray() &&
obj2.arrayGetLength() == 3) {
obj2.arrayGet(0, &obj3);
cs->blackX = obj3.getNum();
obj3.free();
obj2.arrayGet(1, &obj3);
cs->blackY = obj3.getNum();
obj3.free();
obj2.arrayGet(2, &obj3);
cs->blackZ = obj3.getNum();
obj3.free();
}
obj2.free();
if (obj1.dictLookup("Range", &obj2)->isArray() &&
obj2.arrayGetLength() == 4) {
obj2.arrayGet(0, &obj3);
cs->aMin = obj3.getNum();
obj3.free();
obj2.arrayGet(1, &obj3);
cs->aMax = obj3.getNum();
obj3.free();
obj2.arrayGet(2, &obj3);
cs->bMin = obj3.getNum();
obj3.free();
obj2.arrayGet(3, &obj3);
cs->bMax = obj3.getNum();
obj3.free();
}
obj2.free();
obj1.free();
cs->kr = 1 / (xyzrgb[0][0] * cs->whiteX +
xyzrgb[0][1] * cs->whiteY +
xyzrgb[0][2] * cs->whiteZ);
cs->kg = 1 / (xyzrgb[1][0] * cs->whiteX +
xyzrgb[1][1] * cs->whiteY +
xyzrgb[1][2] * cs->whiteZ);
cs->kb = 1 / (xyzrgb[2][0] * cs->whiteX +
xyzrgb[2][1] * cs->whiteY +
xyzrgb[2][2] * cs->whiteZ);
#ifdef USE_CMS
cs->transform = (state != NULL) ? state->getXYZ2DisplayTransform() : XYZ2DisplayTransform;
if (cs->transform != NULL) cs->transform->ref();
#endif
return cs;
}
void GfxLabColorSpace::getGray(GfxColor *color, GfxGray *gray) {
GfxRGB rgb;
#ifdef USE_CMS
if (transform != NULL && transform->getTransformPixelType() == PT_GRAY) {
Guchar out[gfxColorMaxComps];
double in[gfxColorMaxComps];
getXYZ(color, &in[0], &in[1], &in[2]);
transform->doTransform(in,out,1);
*gray = byteToCol(out[0]);
return;
}
#endif
getRGB(color, &rgb);
*gray = clip01((GfxColorComp)(0.299 * rgb.r +
0.587 * rgb.g +
0.114 * rgb.b + 0.5));
}
// convert L*a*b* to media XYZ color space
// (not multiply by the white point)
void GfxLabColorSpace::getXYZ(GfxColor *color,
double *pX, double *pY, double *pZ) {
double X, Y, Z;
double t1, t2;
t1 = (colToDbl(color->c[0]) + 16) / 116;
t2 = t1 + colToDbl(color->c[1]) / 500;
if (t2 >= (6.0 / 29.0)) {
X = t2 * t2 * t2;
} else {
X = (108.0 / 841.0) * (t2 - (4.0 / 29.0));
}
if (t1 >= (6.0 / 29.0)) {
Y = t1 * t1 * t1;
} else {
Y = (108.0 / 841.0) * (t1 - (4.0 / 29.0));
}
t2 = t1 - colToDbl(color->c[2]) / 200;
if (t2 >= (6.0 / 29.0)) {
Z = t2 * t2 * t2;
} else {
Z = (108.0 / 841.0) * (t2 - (4.0 / 29.0));
}
*pX = X;
*pY = Y;
*pZ = Z;
}
void GfxLabColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
double X, Y, Z;
double r, g, b;
getXYZ(color, &X, &Y, &Z);
#ifdef USE_CMS
if (transform != NULL && transform->getTransformPixelType() == PT_RGB) {
Guchar out[gfxColorMaxComps];
double in[gfxColorMaxComps];
in[0] = clip01(X);
in[1] = clip01(Y);
in[2] = clip01(Z);
transform->doTransform(in,out,1);
rgb->r = byteToCol(out[0]);
rgb->g = byteToCol(out[1]);
rgb->b = byteToCol(out[2]);
return;
} else if (transform != NULL && transform->getTransformPixelType() == PT_CMYK) {
Guchar out[gfxColorMaxComps];
double in[gfxColorMaxComps];
double c, m, y, k, c1, m1, y1, k1, r, g, b;
in[0] = clip01(X);
in[1] = clip01(Y);
in[2] = clip01(Z);
transform->doTransform(in,out,1);
c = byteToDbl(out[0]);
m = byteToDbl(out[1]);
y = byteToDbl(out[2]);
k = byteToDbl(out[3]);
c1 = 1 - c;
m1 = 1 - m;
y1 = 1 - y;
k1 = 1 - k;
cmykToRGBMatrixMultiplication(c, m, y, k, c1, m1, y1, k1, r, g, b);
rgb->r = clip01(dblToCol(r));
rgb->g = clip01(dblToCol(g));
rgb->b = clip01(dblToCol(b));
return;
}
#endif
X *= whiteX;
Y *= whiteY;
Z *= whiteZ;
// convert XYZ to RGB, including gamut mapping and gamma correction
r = xyzrgb[0][0] * X + xyzrgb[0][1] * Y + xyzrgb[0][2] * Z;
g = xyzrgb[1][0] * X + xyzrgb[1][1] * Y + xyzrgb[1][2] * Z;
b = xyzrgb[2][0] * X + xyzrgb[2][1] * Y + xyzrgb[2][2] * Z;
rgb->r = dblToCol(sqrt(clip01(r * kr)));
rgb->g = dblToCol(sqrt(clip01(g * kg)));
rgb->b = dblToCol(sqrt(clip01(b * kb)));
}
void GfxLabColorSpace::getCMYK(GfxColor *color, GfxCMYK *cmyk) {
GfxRGB rgb;
GfxColorComp c, m, y, k;
#ifdef USE_CMS
if (transform != NULL && transform->getTransformPixelType() == PT_CMYK) {
double in[gfxColorMaxComps];
Guchar out[gfxColorMaxComps];
getXYZ(color, &in[0], &in[1], &in[2]);
transform->doTransform(in,out,1);
cmyk->c = byteToCol(out[0]);
cmyk->m = byteToCol(out[1]);
cmyk->y = byteToCol(out[2]);
cmyk->k = byteToCol(out[3]);
return;
}
#endif
getRGB(color, &rgb);
c = clip01(gfxColorComp1 - rgb.r);
m = clip01(gfxColorComp1 - rgb.g);
y = clip01(gfxColorComp1 - rgb.b);
k = c;
if (m < k) {
k = m;
}
if (y < k) {
k = y;
}
cmyk->c = c - k;
cmyk->m = m - k;
cmyk->y = y - k;
cmyk->k = k;
}
void GfxLabColorSpace::getDeviceN(GfxColor *color, GfxColor *deviceN) {
GfxCMYK cmyk;
for (int i = 0; i < gfxColorMaxComps; i++)
deviceN->c[i] = 0;
getCMYK(color, &cmyk);
deviceN->c[0] = cmyk.c;
deviceN->c[1] = cmyk.m;
deviceN->c[2] = cmyk.y;
deviceN->c[3] = cmyk.k;
}
void GfxLabColorSpace::getDefaultColor(GfxColor *color) {
color->c[0] = 0;
if (aMin > 0) {
color->c[1] = dblToCol(aMin);
} else if (aMax < 0) {
color->c[1] = dblToCol(aMax);
} else {
color->c[1] = 0;
}
if (bMin > 0) {
color->c[2] = dblToCol(bMin);
} else if (bMax < 0) {
color->c[2] = dblToCol(bMax);
} else {
color->c[2] = 0;
}
}
void GfxLabColorSpace::getDefaultRanges(double *decodeLow, double *decodeRange,
int maxImgPixel) {
decodeLow[0] = 0;
decodeRange[0] = 100;
decodeLow[1] = aMin;
decodeRange[1] = aMax - aMin;
decodeLow[2] = bMin;
decodeRange[2] = bMax - bMin;
}
//------------------------------------------------------------------------
// GfxICCBasedColorSpace
//------------------------------------------------------------------------
class GfxICCBasedColorSpaceKey : public PopplerCacheKey
{
public:
GfxICCBasedColorSpaceKey(int numA, int genA) : num(numA), gen(genA)
{
}
bool operator==(const PopplerCacheKey &key) const
{
const GfxICCBasedColorSpaceKey *k = static_cast<const GfxICCBasedColorSpaceKey*>(&key);
return k->num == num && k->gen == gen;
}
int num, gen;
};
class GfxICCBasedColorSpaceItem : public PopplerCacheItem
{
public:
GfxICCBasedColorSpaceItem(GfxICCBasedColorSpace *csA)
{
cs = static_cast<GfxICCBasedColorSpace*>(csA->copy());
}
~GfxICCBasedColorSpaceItem()
{
delete cs;
}
GfxICCBasedColorSpace *cs;
};
GfxICCBasedColorSpace::GfxICCBasedColorSpace(int nCompsA, GfxColorSpace *altA,
Ref *iccProfileStreamA) {
nComps = nCompsA;
alt = altA;
iccProfileStream = *iccProfileStreamA;
rangeMin[0] = rangeMin[1] = rangeMin[2] = rangeMin[3] = 0;
rangeMax[0] = rangeMax[1] = rangeMax[2] = rangeMax[3] = 1;
#ifdef USE_CMS
transform = NULL;
lineTransform = NULL;
#endif
}
GfxICCBasedColorSpace::~GfxICCBasedColorSpace() {
delete alt;
#ifdef USE_CMS
if (transform != NULL) {
if (transform->unref() == 0) delete transform;
}
if (lineTransform != NULL) {
if (lineTransform->unref() == 0) delete lineTransform;
}
#endif
}
GfxColorSpace *GfxICCBasedColorSpace::copy() {
GfxICCBasedColorSpace *cs;
int i;
cs = new GfxICCBasedColorSpace(nComps, alt->copy(), &iccProfileStream);
for (i = 0; i < 4; ++i) {
cs->rangeMin[i] = rangeMin[i];
cs->rangeMax[i] = rangeMax[i];
}
#ifdef USE_CMS
cs->transform = transform;
if (transform != NULL) transform->ref();
cs->lineTransform = lineTransform;
if (lineTransform != NULL) lineTransform->ref();
#endif
return cs;
}
GfxColorSpace *GfxICCBasedColorSpace::parse(Array *arr, OutputDev *out, GfxState *state, int recursion) {
GfxICCBasedColorSpace *cs;
Ref iccProfileStreamA;
int nCompsA;
GfxColorSpace *altA;
Dict *dict;
Object obj1, obj2, obj3;
int i;
if (arr->getLength() < 2) {
error(errSyntaxError, -1, "Bad ICCBased color space");
return NULL;
}
arr->getNF(1, &obj1);
if (obj1.isRef()) {
iccProfileStreamA = obj1.getRef();
} else {
iccProfileStreamA.num = 0;
iccProfileStreamA.gen = 0;
}
obj1.free();
#ifdef USE_CMS
// check cache
if (out && iccProfileStreamA.num > 0) {
GfxICCBasedColorSpaceKey k(iccProfileStreamA.num, iccProfileStreamA.gen);
GfxICCBasedColorSpaceItem *item = static_cast<GfxICCBasedColorSpaceItem *>(out->getIccColorSpaceCache()->lookup(k));
if (item != NULL)
{
cs = static_cast<GfxICCBasedColorSpace*>(item->cs->copy());
int transformIntent = cs->getIntent();
int cmsIntent = INTENT_RELATIVE_COLORIMETRIC;
if (state != NULL) {
const char *intent = state->getRenderingIntent();
if (intent != NULL) {
if (strcmp(intent, "AbsoluteColorimetric") == 0) {
cmsIntent = INTENT_ABSOLUTE_COLORIMETRIC;
} else if (strcmp(intent, "Saturation") == 0) {
cmsIntent = INTENT_SATURATION;
} else if (strcmp(intent, "Perceptual") == 0) {
cmsIntent = INTENT_PERCEPTUAL;
}
}
}
if (transformIntent == cmsIntent) {
return cs;
}
delete cs;
}
}
#endif
arr->get(1, &obj1);
if (!obj1.isStream()) {
error(errSyntaxWarning, -1, "Bad ICCBased color space (stream)");
obj1.free();
return NULL;
}
dict = obj1.streamGetDict();
if (!dict->lookup("N", &obj2)->isInt()) {
error(errSyntaxWarning, -1, "Bad ICCBased color space (N)");
obj2.free();
obj1.free();
return NULL;
}
nCompsA = obj2.getInt();
obj2.free();
if (nCompsA > 4) {
error(errSyntaxError, -1,
"ICCBased color space with too many ({0:d} > 4) components",
nCompsA);
nCompsA = 4;
}
if (dict->lookup("Alternate", &obj2)->isNull() ||
!(altA = GfxColorSpace::parse(&obj2, out, state, recursion + 1))) {
switch (nCompsA) {
case 1:
altA = new GfxDeviceGrayColorSpace();
break;
case 3:
altA = new GfxDeviceRGBColorSpace();
break;
case 4:
altA = new GfxDeviceCMYKColorSpace();
break;
default:
error(errSyntaxWarning, -1, "Bad ICCBased color space - invalid N");
obj2.free();
obj1.free();
return NULL;
}
}
obj2.free();
cs = new GfxICCBasedColorSpace(nCompsA, altA, &iccProfileStreamA);
if (dict->lookup("Range", &obj2)->isArray() &&
obj2.arrayGetLength() == 2 * nCompsA) {
Object obj4;
for (i = 0; i < nCompsA; ++i) {
obj2.arrayGet(2*i, &obj3);
obj2.arrayGet(2*i+1, &obj4);
if (obj3.isNum() && obj4.isNum()) {
cs->rangeMin[i] = obj3.getNum();
cs->rangeMax[i] = obj4.getNum();
}
obj3.free();
obj4.free();
}
}
obj2.free();
obj1.free();
#ifdef USE_CMS
arr->get(1, &obj1);
dict = obj1.streamGetDict();
Guchar *profBuf;
Stream *iccStream = obj1.getStream();
int length = 0;
profBuf = iccStream->toUnsignedChars(&length, 65536, 65536);
cmsHPROFILE hp = cmsOpenProfileFromMem(profBuf,length);
gfree(profBuf);
if (hp == 0) {
error(errSyntaxWarning, -1, "read ICCBased color space profile error");
} else {
cmsHPROFILE dhp = (state != NULL && state->getDisplayProfile() != NULL) ? state->getDisplayProfile() : displayProfile;
if (dhp == NULL) dhp = RGBProfile;
unsigned int cst = getCMSColorSpaceType(cmsGetColorSpace(hp));
unsigned int dNChannels = getCMSNChannels(cmsGetColorSpace(dhp));
unsigned int dcst = getCMSColorSpaceType(cmsGetColorSpace(dhp));
cmsHTRANSFORM transform;
int cmsIntent = INTENT_RELATIVE_COLORIMETRIC;
if (state != NULL) {
const char *intent = state->getRenderingIntent();
if (intent != NULL) {
if (strcmp(intent, "AbsoluteColorimetric") == 0) {
cmsIntent = INTENT_ABSOLUTE_COLORIMETRIC;
} else if (strcmp(intent, "Saturation") == 0) {
cmsIntent = INTENT_SATURATION;
} else if (strcmp(intent, "Perceptual") == 0) {
cmsIntent = INTENT_PERCEPTUAL;
}
}
}
if ((transform = cmsCreateTransform(hp,
COLORSPACE_SH(cst) |CHANNELS_SH(nCompsA) | BYTES_SH(1),
dhp,
COLORSPACE_SH(dcst) |
CHANNELS_SH(dNChannels) | BYTES_SH(1),
cmsIntent, LCMS_FLAGS)) == 0) {
error(errSyntaxWarning, -1, "Can't create transform");
cs->transform = NULL;
} else {
cs->transform = new GfxColorTransform(transform, cmsIntent, dcst);
}
if (dcst == PT_RGB || dcst == PT_CMYK) {
// create line transform only when the display is RGB type color space
if ((transform = cmsCreateTransform(hp,
CHANNELS_SH(nCompsA) | BYTES_SH(1),dhp,
(dcst == PT_RGB) ? TYPE_RGB_8 : TYPE_CMYK_8, cmsIntent, LCMS_FLAGS)) == 0) {
error(errSyntaxWarning, -1, "Can't create transform");
cs->lineTransform = NULL;
} else {
cs->lineTransform = new GfxColorTransform(transform, cmsIntent, dcst);
}
}
cmsCloseProfile(hp);
}
obj1.free();
// put this colorSpace into cache
if (out && iccProfileStreamA.num > 0) {
GfxICCBasedColorSpaceKey *k = new GfxICCBasedColorSpaceKey(iccProfileStreamA.num, iccProfileStreamA.gen);
GfxICCBasedColorSpaceItem *item = new GfxICCBasedColorSpaceItem(cs);
out->getIccColorSpaceCache()->put(k, item);
}
#endif
return cs;
}
void GfxICCBasedColorSpace::getGray(GfxColor *color, GfxGray *gray) {
#ifdef USE_CMS
if (transform != 0 && transform->getTransformPixelType() == PT_GRAY) {
Guchar in[gfxColorMaxComps];
Guchar out[gfxColorMaxComps];
for (int i = 0;i < nComps;i++) {
in[i] = colToByte(color->c[i]);
}
if (nComps <= 4) {
unsigned int key = 0;
for (int j = 0; j < nComps; j++) {
key = (key << 8) + in[j];
}
std::map<unsigned int, unsigned int>::iterator it = cmsCache.find(key);
if (it != cmsCache.end()) {
unsigned int value = it->second;
*gray = byteToCol(value & 0xff);
return;
}
}
transform->doTransform(in,out,1);
*gray = byteToCol(out[0]);
if (nComps <= 4 && cmsCache.size() <= CMSCACHE_LIMIT) {
unsigned int key = 0;
for (int j = 0; j < nComps; j++) {
key = (key << 8) + in[j];
}
unsigned int value = out[0];
cmsCache.insert(std::pair<unsigned int, unsigned int>(key, value));
}
} else {
GfxRGB rgb;
getRGB(color,&rgb);
*gray = clip01((GfxColorComp)(0.3 * rgb.r +
0.59 * rgb.g +
0.11 * rgb.b + 0.5));
}
#else
alt->getGray(color, gray);
#endif
}
void GfxICCBasedColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
#ifdef USE_CMS
if (transform != 0 && transform->getTransformPixelType() == PT_RGB) {
Guchar in[gfxColorMaxComps];
Guchar out[gfxColorMaxComps];
for (int i = 0;i < nComps;i++) {
in[i] = colToByte(color->c[i]);
}
if (nComps <= 4) {
unsigned int key = 0;
for (int j = 0; j < nComps; j++) {
key = (key << 8) + in[j];
}
std::map<unsigned int, unsigned int>::iterator it = cmsCache.find(key);
if (it != cmsCache.end()) {
unsigned int value = it->second;
rgb->r = byteToCol(value >> 16);
rgb->g = byteToCol((value >> 8) & 0xff);
rgb->b = byteToCol(value & 0xff);
return;
}
}
transform->doTransform(in,out,1);
rgb->r = byteToCol(out[0]);
rgb->g = byteToCol(out[1]);
rgb->b = byteToCol(out[2]);
if (nComps <= 4 && cmsCache.size() <= CMSCACHE_LIMIT) {
unsigned int key = 0;
for (int j = 0; j < nComps; j++) {
key = (key << 8) + in[j];
}
unsigned int value = (out[0] << 16) + (out[1] << 8) + out[2];
cmsCache.insert(std::pair<unsigned int, unsigned int>(key, value));
}
} else if (transform != NULL && transform->getTransformPixelType() == PT_CMYK) {
Guchar in[gfxColorMaxComps];
Guchar out[gfxColorMaxComps];
double c, m, y, k, c1, m1, y1, k1, r, g, b;
for (int i = 0;i < nComps;i++) {
in[i] = colToByte(color->c[i]);
}
if (nComps <= 4) {
unsigned int key = 0;
for (int j = 0; j < nComps; j++) {
key = (key << 8) + in[j];
}
std::map<unsigned int, unsigned int>::iterator it = cmsCache.find(key);
if (it != cmsCache.end()) {
unsigned int value = it->second;
rgb->r = byteToCol(value >> 16);
rgb->g = byteToCol((value >> 8) & 0xff);
rgb->b = byteToCol(value & 0xff);
return;
}
}
transform->doTransform(in,out,1);
c = byteToDbl(out[0]);
m = byteToDbl(out[1]);
y = byteToDbl(out[2]);
k = byteToDbl(out[3]);
c1 = 1 - c;
m1 = 1 - m;
y1 = 1 - y;
k1 = 1 - k;
cmykToRGBMatrixMultiplication(c, m, y, k, c1, m1, y1, k1, r, g, b);
rgb->r = clip01(dblToCol(r));
rgb->g = clip01(dblToCol(g));
rgb->b = clip01(dblToCol(b));
if (nComps <= 4 && cmsCache.size() <= CMSCACHE_LIMIT) {
unsigned int key = 0;
for (int j = 0; j < nComps; j++) {
key = (key << 8) + in[j];
}
unsigned int value = (dblToByte(r) << 16) + (dblToByte(g) << 8) + dblToByte(b);
cmsCache.insert(std::pair<unsigned int, unsigned int>(key, value));
}
} else {
alt->getRGB(color, rgb);
}
#else
alt->getRGB(color, rgb);
#endif
}
void GfxICCBasedColorSpace::getRGBLine(Guchar *in, unsigned int *out,
int length) {
#ifdef USE_CMS
if (lineTransform != 0 && lineTransform->getTransformPixelType() == PT_RGB) {
Guchar* tmp = (Guchar *)gmallocn(3 * length, sizeof(Guchar));
lineTransform->doTransform(in, tmp, length);
for (int i = 0; i < length; ++i) {
Guchar *current = tmp + (i * 3);
out[i] = (current[0] << 16) | (current[1] << 8) | current[2];
}
gfree(tmp);
} else {
alt->getRGBLine(in, out, length);
}
#else
alt->getRGBLine(in, out, length);
#endif
}
void GfxICCBasedColorSpace::getRGBLine(Guchar *in, Guchar *out, int length) {
#ifdef USE_CMS
if (lineTransform != 0 && lineTransform->getTransformPixelType() == PT_RGB) {
Guchar* tmp = (Guchar *)gmallocn(3 * length, sizeof(Guchar));
lineTransform->doTransform(in, tmp, length);
Guchar *current = tmp;
for (int i = 0; i < length; ++i) {
*out++ = *current++;
*out++ = *current++;
*out++ = *current++;
}
gfree(tmp);
} else if (lineTransform != NULL && lineTransform->getTransformPixelType() == PT_CMYK) {
Guchar* tmp = (Guchar *)gmallocn(4 * length, sizeof(Guchar));
lineTransform->doTransform(in, tmp, length);
Guchar *current = tmp;
double c, m, y, k, c1, m1, y1, k1, r, g, b;
for (int i = 0; i < length; ++i) {
c = byteToDbl(*current++);
m = byteToDbl(*current++);
y = byteToDbl(*current++);
k = byteToDbl(*current++);
c1 = 1 - c;
m1 = 1 - m;
y1 = 1 - y;
k1 = 1 - k;
cmykToRGBMatrixMultiplication(c, m, y, k, c1, m1, y1, k1, r, g, b);
*out++ = dblToByte(r);
*out++ = dblToByte(g);
*out++ = dblToByte(b);
}
gfree(tmp);
} else {
alt->getRGBLine(in, out, length);
}
#else
alt->getRGBLine(in, out, length);
#endif
}
void GfxICCBasedColorSpace::getRGBXLine(Guchar *in, Guchar *out, int length) {
#ifdef USE_CMS
if (lineTransform != 0 && lineTransform->getTransformPixelType() == PT_RGB) {
Guchar* tmp = (Guchar *)gmallocn(3 * length, sizeof(Guchar));
lineTransform->doTransform(in, tmp, length);
Guchar *current = tmp;
for (int i = 0; i < length; ++i) {
*out++ = *current++;
*out++ = *current++;
*out++ = *current++;
*out++ = 255;
}
gfree(tmp);
} else {
alt->getRGBXLine(in, out, length);
}
#else
alt->getRGBXLine(in, out, length);
#endif
}
void GfxICCBasedColorSpace::getCMYKLine(Guchar *in, Guchar *out, int length) {
#ifdef USE_CMS
if (lineTransform != NULL && lineTransform->getTransformPixelType() == PT_CMYK) {
transform->doTransform(in,out,length);
} else if (lineTransform != NULL) {
GfxColorComp c, m, y, k;
Guchar* tmp = (Guchar *)gmallocn(3 * length, sizeof(Guchar));
getRGBLine(in, tmp, length);
Guchar *p = tmp;
for (int i = 0; i < length; i++) {
c = byteToCol(255 - *p++);
m = byteToCol(255 - *p++);
y = byteToCol(255 - *p++);
k = c;
if (m < k) {
k = m;
}
if (y < k) {
k = y;
}
*out++ = colToByte(c - k);
*out++ = colToByte(m - k);
*out++ = colToByte(y - k);
*out++ = colToByte(k);
}
gfree(tmp);
} else {
alt->getCMYKLine(in, out, length);
}
#else
alt->getCMYKLine(in, out, length);
#endif
}
void GfxICCBasedColorSpace::getDeviceNLine(Guchar *in, Guchar *out, int length) {
#ifdef USE_CMS
if (lineTransform != NULL && lineTransform->getTransformPixelType() == PT_CMYK) {
Guchar* tmp = (Guchar *)gmallocn(4 * length, sizeof(Guchar));
transform->doTransform(in,tmp,length);
Guchar *p = tmp;
for (int i = 0; i < length; i++) {
for (int j = 0; j < 4; j++)
*out++ = *p++;
for (int j = 4; j < SPOT_NCOMPS+4; j++)
*out++ = 0;
}
gfree(tmp);
} else if (lineTransform != NULL) {
GfxColorComp c, m, y, k;
Guchar* tmp = (Guchar *)gmallocn(3 * length, sizeof(Guchar));
getRGBLine(in, tmp, length);
Guchar *p = tmp;
for (int i = 0; i < length; i++) {
for (int j = 0; j < SPOT_NCOMPS+4; j++)
out[j] = 0;
c = byteToCol(255 - *p++);
m = byteToCol(255 - *p++);
y = byteToCol(255 - *p++);
k = c;
if (m < k) {
k = m;
}
if (y < k) {
k = y;
}
out[0] = colToByte(c - k);
out[1] = colToByte(m - k);
out[2] = colToByte(y - k);
out[3] = colToByte(k);
out += (SPOT_NCOMPS+4);
}
gfree(tmp);
} else {
alt->getDeviceNLine(in, out, length);
}
#else
alt->getDeviceNLine(in, out, length);
#endif
}
void GfxICCBasedColorSpace::getCMYK(GfxColor *color, GfxCMYK *cmyk) {
#ifdef USE_CMS
if (transform != NULL && transform->getTransformPixelType() == PT_CMYK) {
Guchar in[gfxColorMaxComps];
Guchar out[gfxColorMaxComps];
for (int i = 0;i < nComps;i++) {
in[i] = colToByte(color->c[i]);
}
if (nComps <= 4) {
unsigned int key = 0;
for (int j = 0; j < nComps; j++) {
key = (key << 8) + in[j];
}
std::map<unsigned int, unsigned int>::iterator it = cmsCache.find(key);
if (it != cmsCache.end()) {
unsigned int value = it->second;
cmyk->c = byteToCol(value >> 24);
cmyk->m = byteToCol((value >> 16) & 0xff);
cmyk->y = byteToCol((value >> 8) & 0xff);
cmyk->k = byteToCol(value & 0xff);
return;
}
}
transform->doTransform(in,out,1);
cmyk->c = byteToCol(out[0]);
cmyk->m = byteToCol(out[1]);
cmyk->y = byteToCol(out[2]);
cmyk->k = byteToCol(out[3]);
if (nComps <= 4 && cmsCache.size() <= CMSCACHE_LIMIT) {
unsigned int key = 0;
for (int j = 0; j < nComps; j++) {
key = (key << 8) + in[j];
}
unsigned int value = (out[0] << 24) + (out[1] << 16) + (out[2] << 8) + out[3];
cmsCache.insert(std::pair<unsigned int, unsigned int>(key, value));
}
} else {
GfxRGB rgb;
GfxColorComp c, m, y, k;
getRGB(color,&rgb);
c = clip01(gfxColorComp1 - rgb.r);
m = clip01(gfxColorComp1 - rgb.g);
y = clip01(gfxColorComp1 - rgb.b);
k = c;
if (m < k) {
k = m;
}
if (y < k) {
k = y;
}
cmyk->c = c - k;
cmyk->m = m - k;
cmyk->y = y - k;
cmyk->k = k;
}
#else
alt->getCMYK(color, cmyk);
#endif
}
GBool GfxICCBasedColorSpace::useGetRGBLine() {
#ifdef USE_CMS
return lineTransform != NULL || alt->useGetRGBLine();
#else
return alt->useGetRGBLine();
#endif
}
GBool GfxICCBasedColorSpace::useGetCMYKLine() {
#ifdef USE_CMS
return lineTransform != NULL || alt->useGetCMYKLine();
#else
return alt->useGetCMYKLine();
#endif
}
GBool GfxICCBasedColorSpace::useGetDeviceNLine() {
#ifdef USE_CMS
return lineTransform != NULL || alt->useGetDeviceNLine();
#else
return alt->useGetDeviceNLine();
#endif
}
void GfxICCBasedColorSpace::getDeviceN(GfxColor *color, GfxColor *deviceN) {
GfxCMYK cmyk;
for (int i = 0; i < gfxColorMaxComps; i++)
deviceN->c[i] = 0;
getCMYK(color, &cmyk);
deviceN->c[0] = cmyk.c;
deviceN->c[1] = cmyk.m;
deviceN->c[2] = cmyk.y;
deviceN->c[3] = cmyk.k;
}
void GfxICCBasedColorSpace::getDefaultColor(GfxColor *color) {
int i;
for (i = 0; i < nComps; ++i) {
if (rangeMin[i] > 0) {
color->c[i] = dblToCol(rangeMin[i]);
} else if (rangeMax[i] < 0) {
color->c[i] = dblToCol(rangeMax[i]);
} else {
color->c[i] = 0;
}
}
}
void GfxICCBasedColorSpace::getDefaultRanges(double *decodeLow,
double *decodeRange,
int maxImgPixel) {
alt->getDefaultRanges(decodeLow, decodeRange, maxImgPixel);
#if 0
// this is nominally correct, but some PDF files don't set the
// correct ranges in the ICCBased dict
int i;
for (i = 0; i < nComps; ++i) {
decodeLow[i] = rangeMin[i];
decodeRange[i] = rangeMax[i] - rangeMin[i];
}
#endif
}
//------------------------------------------------------------------------
// GfxIndexedColorSpace
//------------------------------------------------------------------------
GfxIndexedColorSpace::GfxIndexedColorSpace(GfxColorSpace *baseA,
int indexHighA) {
base = baseA;
indexHigh = indexHighA;
lookup = (Guchar *)gmallocn((indexHigh + 1) * base->getNComps(),
sizeof(Guchar));
overprintMask = base->getOverprintMask();
}
GfxIndexedColorSpace::~GfxIndexedColorSpace() {
delete base;
gfree(lookup);
}
GfxColorSpace *GfxIndexedColorSpace::copy() {
GfxIndexedColorSpace *cs;
cs = new GfxIndexedColorSpace(base->copy(), indexHigh);
memcpy(cs->lookup, lookup,
(indexHigh + 1) * base->getNComps() * sizeof(Guchar));
return cs;
}
GfxColorSpace *GfxIndexedColorSpace::parse(Array *arr, OutputDev *out, GfxState *state, int recursion) {
GfxIndexedColorSpace *cs;
GfxColorSpace *baseA;
int indexHighA;
Object obj1;
char *s;
int n, i, j;
if (arr->getLength() != 4) {
error(errSyntaxWarning, -1, "Bad Indexed color space");
goto err1;
}
arr->get(1, &obj1);
if (!(baseA = GfxColorSpace::parse(&obj1, out, state, recursion + 1))) {
error(errSyntaxWarning, -1, "Bad Indexed color space (base color space)");
goto err2;
}
obj1.free();
if (!arr->get(2, &obj1)->isInt()) {
error(errSyntaxWarning, -1, "Bad Indexed color space (hival)");
delete baseA;
goto err2;
}
indexHighA = obj1.getInt();
if (indexHighA < 0 || indexHighA > 255) {
// the PDF spec requires indexHigh to be in [0,255] -- allowing
// values larger than 255 creates a security hole: if nComps *
// indexHigh is greater than 2^31, the loop below may overwrite
// past the end of the array
int previousValue = indexHighA;
if (indexHighA < 0) indexHighA = 0;
else indexHighA = 255;
error(errSyntaxWarning, -1, "Bad Indexed color space (invalid indexHigh value, was {0:d} using {1:d} to try to recover)", previousValue, indexHighA);
}
obj1.free();
cs = new GfxIndexedColorSpace(baseA, indexHighA);
arr->get(3, &obj1);
n = baseA->getNComps();
if (obj1.isStream()) {
obj1.streamReset();
for (i = 0; i <= indexHighA; ++i) {
const int readChars = obj1.streamGetChars(n, &cs->lookup[i*n]);
for (j = readChars; j < n; ++j) {
error(errSyntaxWarning, -1, "Bad Indexed color space (lookup table stream too short) padding with zeroes");
cs->lookup[i*n + j] = 0;
}
}
obj1.streamClose();
} else if (obj1.isString()) {
if (obj1.getString()->getLength() < (indexHighA + 1) * n) {
error(errSyntaxWarning, -1, "Bad Indexed color space (lookup table string too short)");
goto err3;
}
s = obj1.getString()->getCString();
for (i = 0; i <= indexHighA; ++i) {
for (j = 0; j < n; ++j) {
cs->lookup[i*n + j] = (Guchar)*s++;
}
}
} else {
error(errSyntaxWarning, -1, "Bad Indexed color space (lookup table)");
goto err3;
}
obj1.free();
return cs;
err3:
delete cs;
err2:
obj1.free();
err1:
return NULL;
}
GfxColor *GfxIndexedColorSpace::mapColorToBase(GfxColor *color,
GfxColor *baseColor) {
Guchar *p;
double low[gfxColorMaxComps], range[gfxColorMaxComps];
int n, i;
n = base->getNComps();
base->getDefaultRanges(low, range, indexHigh);
const int idx = (int)(colToDbl(color->c[0]) + 0.5) * n;
if (likely((idx + n < (indexHigh + 1) * base->getNComps()) && idx >= 0)) {
p = &lookup[idx];
for (i = 0; i < n; ++i) {
baseColor->c[i] = dblToCol(low[i] + (p[i] / 255.0) * range[i]);
}
} else {
for (i = 0; i < n; ++i) {
baseColor->c[i] = 0;
}
}
return baseColor;
}
void GfxIndexedColorSpace::getGray(GfxColor *color, GfxGray *gray) {
GfxColor color2;
base->getGray(mapColorToBase(color, &color2), gray);
}
void GfxIndexedColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
GfxColor color2;
base->getRGB(mapColorToBase(color, &color2), rgb);
}
void GfxIndexedColorSpace::getRGBLine(Guchar *in, unsigned int *out, int length) {
Guchar *line;
int i, j, n;
n = base->getNComps();
line = (Guchar *) gmallocn (length, n);
for (i = 0; i < length; i++)
for (j = 0; j < n; j++)
line[i * n + j] = lookup[in[i] * n + j];
base->getRGBLine(line, out, length);
gfree (line);
}
void GfxIndexedColorSpace::getRGBLine(Guchar *in, Guchar *out, int length)
{
Guchar *line;
int i, j, n;
n = base->getNComps();
line = (Guchar *) gmallocn (length, n);
for (i = 0; i < length; i++)
for (j = 0; j < n; j++)
line[i * n + j] = lookup[in[i] * n + j];
base->getRGBLine(line, out, length);
gfree (line);
}
void GfxIndexedColorSpace::getRGBXLine(Guchar *in, Guchar *out, int length)
{
Guchar *line;
int i, j, n;
n = base->getNComps();
line = (Guchar *) gmallocn (length, n);
for (i = 0; i < length; i++)
for (j = 0; j < n; j++)
line[i * n + j] = lookup[in[i] * n + j];
base->getRGBXLine(line, out, length);
gfree (line);
}
void GfxIndexedColorSpace::getCMYKLine(Guchar *in, Guchar *out, int length) {
Guchar *line;
int i, j, n;
n = base->getNComps();
line = (Guchar *) gmallocn (length, n);
for (i = 0; i < length; i++)
for (j = 0; j < n; j++)
line[i * n + j] = lookup[in[i] * n + j];
base->getCMYKLine(line, out, length);
gfree (line);
}
void GfxIndexedColorSpace::getDeviceNLine(Guchar *in, Guchar *out, int length) {
Guchar *line;
int i, j, n;
n = base->getNComps();
line =