1 //========================================================================
5 // Copyright 1996-2003 Glyph & Cog, LLC
7 //========================================================================
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
17 #include <string.h> // for memcpy()
25 //------------------------------------------------------------------------
27 static inline double clip01(double x) {
28 return (x < 0) ? 0 : ((x > 1) ? 1 : x);
31 //------------------------------------------------------------------------
33 static char *gfxColorSpaceModeNames[] = {
47 #define nGfxColorSpaceModes ((sizeof(gfxColorSpaceModeNames) / sizeof(char *)))
49 //------------------------------------------------------------------------
51 //------------------------------------------------------------------------
53 GfxColorSpace::GfxColorSpace() {
56 GfxColorSpace::~GfxColorSpace() {
59 GfxColorSpace *GfxColorSpace::parse(Object *csObj) {
64 if (csObj->isName()) {
65 if (csObj->isName("DeviceGray") || csObj->isName("G")) {
66 cs = new GfxDeviceGrayColorSpace();
67 } else if (csObj->isName("DeviceRGB") || csObj->isName("RGB")) {
68 cs = new GfxDeviceRGBColorSpace();
69 } else if (csObj->isName("DeviceCMYK") || csObj->isName("CMYK")) {
70 cs = new GfxDeviceCMYKColorSpace();
71 } else if (csObj->isName("Pattern")) {
72 cs = new GfxPatternColorSpace(NULL);
74 error(-1, "Bad color space '%s'", csObj->getName());
76 } else if (csObj->isArray()) {
77 csObj->arrayGet(0, &obj1);
78 if (obj1.isName("DeviceGray") || obj1.isName("G")) {
79 cs = new GfxDeviceGrayColorSpace();
80 } else if (obj1.isName("DeviceRGB") || obj1.isName("RGB")) {
81 cs = new GfxDeviceRGBColorSpace();
82 } else if (obj1.isName("DeviceCMYK") || obj1.isName("CMYK")) {
83 cs = new GfxDeviceCMYKColorSpace();
84 } else if (obj1.isName("CalGray")) {
85 cs = GfxCalGrayColorSpace::parse(csObj->getArray());
86 } else if (obj1.isName("CalRGB")) {
87 cs = GfxCalRGBColorSpace::parse(csObj->getArray());
88 } else if (obj1.isName("Lab")) {
89 cs = GfxLabColorSpace::parse(csObj->getArray());
90 } else if (obj1.isName("ICCBased")) {
91 cs = GfxICCBasedColorSpace::parse(csObj->getArray());
92 } else if (obj1.isName("Indexed") || obj1.isName("I")) {
93 cs = GfxIndexedColorSpace::parse(csObj->getArray());
94 } else if (obj1.isName("Separation")) {
95 cs = GfxSeparationColorSpace::parse(csObj->getArray());
96 } else if (obj1.isName("DeviceN")) {
97 cs = GfxDeviceNColorSpace::parse(csObj->getArray());
98 } else if (obj1.isName("Pattern")) {
99 cs = GfxPatternColorSpace::parse(csObj->getArray());
101 error(-1, "Bad color space");
105 error(-1, "Bad color space - expected name or array");
110 void GfxColorSpace::getDefaultRanges(double *decodeLow, double *decodeRange,
114 for (i = 0; i < getNComps(); ++i) {
120 int GfxColorSpace::getNumColorSpaceModes() {
121 return nGfxColorSpaceModes;
124 char *GfxColorSpace::getColorSpaceModeName(int idx) {
125 return gfxColorSpaceModeNames[idx];
128 //------------------------------------------------------------------------
129 // GfxDeviceGrayColorSpace
130 //------------------------------------------------------------------------
132 GfxDeviceGrayColorSpace::GfxDeviceGrayColorSpace() {
135 GfxDeviceGrayColorSpace::~GfxDeviceGrayColorSpace() {
138 GfxColorSpace *GfxDeviceGrayColorSpace::copy() {
139 return new GfxDeviceGrayColorSpace();
142 void GfxDeviceGrayColorSpace::getGray(GfxColor *color, double *gray) {
143 *gray = clip01(color->c[0]);
146 void GfxDeviceGrayColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
147 rgb->r = rgb->g = rgb->b = clip01(color->c[0]);
150 void GfxDeviceGrayColorSpace::getCMYK(GfxColor *color, GfxCMYK *cmyk) {
151 cmyk->c = cmyk->m = cmyk->y = 0;
152 cmyk->k = clip01(1 - color->c[0]);
155 //------------------------------------------------------------------------
156 // GfxCalGrayColorSpace
157 //------------------------------------------------------------------------
159 GfxCalGrayColorSpace::GfxCalGrayColorSpace() {
160 whiteX = whiteY = whiteZ = 1;
161 blackX = blackY = blackZ = 0;
165 GfxCalGrayColorSpace::~GfxCalGrayColorSpace() {
168 GfxColorSpace *GfxCalGrayColorSpace::copy() {
169 GfxCalGrayColorSpace *cs;
171 cs = new GfxCalGrayColorSpace();
182 GfxColorSpace *GfxCalGrayColorSpace::parse(Array *arr) {
183 GfxCalGrayColorSpace *cs;
184 Object obj1, obj2, obj3;
187 if (!obj1.isDict()) {
188 error(-1, "Bad CalGray color space");
192 cs = new GfxCalGrayColorSpace();
193 if (obj1.dictLookup("WhitePoint", &obj2)->isArray() &&
194 obj2.arrayGetLength() == 3) {
195 obj2.arrayGet(0, &obj3);
196 cs->whiteX = obj3.getNum();
198 obj2.arrayGet(1, &obj3);
199 cs->whiteY = obj3.getNum();
201 obj2.arrayGet(2, &obj3);
202 cs->whiteZ = obj3.getNum();
206 if (obj1.dictLookup("BlackPoint", &obj2)->isArray() &&
207 obj2.arrayGetLength() == 3) {
208 obj2.arrayGet(0, &obj3);
209 cs->blackX = obj3.getNum();
211 obj2.arrayGet(1, &obj3);
212 cs->blackY = obj3.getNum();
214 obj2.arrayGet(2, &obj3);
215 cs->blackZ = obj3.getNum();
219 if (obj1.dictLookup("Gamma", &obj2)->isNum()) {
220 cs->gamma = obj2.getNum();
227 void GfxCalGrayColorSpace::getGray(GfxColor *color, double *gray) {
228 *gray = clip01(color->c[0]);
231 void GfxCalGrayColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
232 rgb->r = rgb->g = rgb->b = clip01(color->c[0]);
235 void GfxCalGrayColorSpace::getCMYK(GfxColor *color, GfxCMYK *cmyk) {
236 cmyk->c = cmyk->m = cmyk->y = 0;
237 cmyk->k = clip01(1 - color->c[0]);
240 //------------------------------------------------------------------------
241 // GfxDeviceRGBColorSpace
242 //------------------------------------------------------------------------
244 GfxDeviceRGBColorSpace::GfxDeviceRGBColorSpace() {
247 GfxDeviceRGBColorSpace::~GfxDeviceRGBColorSpace() {
250 GfxColorSpace *GfxDeviceRGBColorSpace::copy() {
251 return new GfxDeviceRGBColorSpace();
254 void GfxDeviceRGBColorSpace::getGray(GfxColor *color, double *gray) {
255 *gray = clip01(0.299 * color->c[0] +
256 0.587 * color->c[1] +
257 0.114 * color->c[2]);
260 void GfxDeviceRGBColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
261 rgb->r = clip01(color->c[0]);
262 rgb->g = clip01(color->c[1]);
263 rgb->b = clip01(color->c[2]);
266 void GfxDeviceRGBColorSpace::getCMYK(GfxColor *color, GfxCMYK *cmyk) {
269 c = clip01(1 - color->c[0]);
270 m = clip01(1 - color->c[1]);
271 y = clip01(1 - color->c[2]);
285 //------------------------------------------------------------------------
286 // GfxCalRGBColorSpace
287 //------------------------------------------------------------------------
289 GfxCalRGBColorSpace::GfxCalRGBColorSpace() {
290 whiteX = whiteY = whiteZ = 1;
291 blackX = blackY = blackZ = 0;
292 gammaR = gammaG = gammaB = 1;
293 mat[0] = 1; mat[1] = 0; mat[2] = 0;
294 mat[3] = 0; mat[4] = 1; mat[5] = 0;
295 mat[6] = 0; mat[7] = 0; mat[8] = 1;
298 GfxCalRGBColorSpace::~GfxCalRGBColorSpace() {
301 GfxColorSpace *GfxCalRGBColorSpace::copy() {
302 GfxCalRGBColorSpace *cs;
305 cs = new GfxCalRGBColorSpace();
315 for (i = 0; i < 9; ++i) {
321 GfxColorSpace *GfxCalRGBColorSpace::parse(Array *arr) {
322 GfxCalRGBColorSpace *cs;
323 Object obj1, obj2, obj3;
327 if (!obj1.isDict()) {
328 error(-1, "Bad CalRGB color space");
332 cs = new GfxCalRGBColorSpace();
333 if (obj1.dictLookup("WhitePoint", &obj2)->isArray() &&
334 obj2.arrayGetLength() == 3) {
335 obj2.arrayGet(0, &obj3);
336 cs->whiteX = obj3.getNum();
338 obj2.arrayGet(1, &obj3);
339 cs->whiteY = obj3.getNum();
341 obj2.arrayGet(2, &obj3);
342 cs->whiteZ = obj3.getNum();
346 if (obj1.dictLookup("BlackPoint", &obj2)->isArray() &&
347 obj2.arrayGetLength() == 3) {
348 obj2.arrayGet(0, &obj3);
349 cs->blackX = obj3.getNum();
351 obj2.arrayGet(1, &obj3);
352 cs->blackY = obj3.getNum();
354 obj2.arrayGet(2, &obj3);
355 cs->blackZ = obj3.getNum();
359 if (obj1.dictLookup("Gamma", &obj2)->isArray() &&
360 obj2.arrayGetLength() == 3) {
361 obj2.arrayGet(0, &obj3);
362 cs->gammaR = obj3.getNum();
364 obj2.arrayGet(1, &obj3);
365 cs->gammaG = obj3.getNum();
367 obj2.arrayGet(2, &obj3);
368 cs->gammaB = obj3.getNum();
372 if (obj1.dictLookup("Matrix", &obj2)->isArray() &&
373 obj2.arrayGetLength() == 9) {
374 for (i = 0; i < 9; ++i) {
375 obj2.arrayGet(i, &obj3);
376 cs->mat[i] = obj3.getNum();
385 void GfxCalRGBColorSpace::getGray(GfxColor *color, double *gray) {
386 *gray = clip01(0.299 * color->c[0] +
387 0.587 * color->c[1] +
388 0.114 * color->c[2]);
391 void GfxCalRGBColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
392 rgb->r = clip01(color->c[0]);
393 rgb->g = clip01(color->c[1]);
394 rgb->b = clip01(color->c[2]);
397 void GfxCalRGBColorSpace::getCMYK(GfxColor *color, GfxCMYK *cmyk) {
400 c = clip01(1 - color->c[0]);
401 m = clip01(1 - color->c[1]);
402 y = clip01(1 - color->c[2]);
416 //------------------------------------------------------------------------
417 // GfxDeviceCMYKColorSpace
418 //------------------------------------------------------------------------
420 GfxDeviceCMYKColorSpace::GfxDeviceCMYKColorSpace() {
423 GfxDeviceCMYKColorSpace::~GfxDeviceCMYKColorSpace() {
426 GfxColorSpace *GfxDeviceCMYKColorSpace::copy() {
427 return new GfxDeviceCMYKColorSpace();
430 void GfxDeviceCMYKColorSpace::getGray(GfxColor *color, double *gray) {
431 *gray = clip01(1 - color->c[3]
432 - 0.299 * color->c[0]
433 - 0.587 * color->c[1]
434 - 0.114 * color->c[2]);
437 void GfxDeviceCMYKColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
438 double c, m, y, aw, ac, am, ay, ar, ag, ab;
440 /* FIXME ask Derek */
441 if (color->c[0] == 0.0 && color->c[1] == 0 && color->c[2] == 0) {
442 rgb->r = rgb->g = rgb->b = 1 - color->c[3];
446 c = clip01(color->c[0] + color->c[3]);
447 m = clip01(color->c[1] + color->c[3]);
448 y = clip01(color->c[2] + color->c[3]);
449 aw = (1-c) * (1-m) * (1-y);
450 ac = c * (1-m) * (1-y);
451 am = (1-c) * m * (1-y);
452 ay = (1-c) * (1-m) * y;
456 rgb->r = clip01(aw + 0.9137*am + 0.9961*ay + 0.9882*ar);
457 rgb->g = clip01(aw + 0.6196*ac + ay + 0.5176*ag);
458 rgb->b = clip01(aw + 0.7804*ac + 0.5412*am + 0.0667*ar + 0.2118*ag +
462 void GfxDeviceCMYKColorSpace::getCMYK(GfxColor *color, GfxCMYK *cmyk) {
463 cmyk->c = clip01(color->c[0]);
464 cmyk->m = clip01(color->c[1]);
465 cmyk->y = clip01(color->c[2]);
466 cmyk->k = clip01(color->c[3]);
469 //------------------------------------------------------------------------
471 //------------------------------------------------------------------------
473 // This is the inverse of MatrixLMN in Example 4.10 from the PostScript
474 // Language Reference, Third Edition.
475 static double xyzrgb[3][3] = {
476 { 3.240449, -1.537136, -0.498531 },
477 { -0.969265, 1.876011, 0.041556 },
478 { 0.055643, -0.204026, 1.057229 }
481 GfxLabColorSpace::GfxLabColorSpace() {
482 whiteX = whiteY = whiteZ = 1;
483 blackX = blackY = blackZ = 0;
488 GfxLabColorSpace::~GfxLabColorSpace() {
491 GfxColorSpace *GfxLabColorSpace::copy() {
492 GfxLabColorSpace *cs;
494 cs = new GfxLabColorSpace();
511 GfxColorSpace *GfxLabColorSpace::parse(Array *arr) {
512 GfxLabColorSpace *cs;
513 Object obj1, obj2, obj3;
516 if (!obj1.isDict()) {
517 error(-1, "Bad Lab color space");
521 cs = new GfxLabColorSpace();
522 if (obj1.dictLookup("WhitePoint", &obj2)->isArray() &&
523 obj2.arrayGetLength() == 3) {
524 obj2.arrayGet(0, &obj3);
525 cs->whiteX = obj3.getNum();
527 obj2.arrayGet(1, &obj3);
528 cs->whiteY = obj3.getNum();
530 obj2.arrayGet(2, &obj3);
531 cs->whiteZ = obj3.getNum();
535 if (obj1.dictLookup("BlackPoint", &obj2)->isArray() &&
536 obj2.arrayGetLength() == 3) {
537 obj2.arrayGet(0, &obj3);
538 cs->blackX = obj3.getNum();
540 obj2.arrayGet(1, &obj3);
541 cs->blackY = obj3.getNum();
543 obj2.arrayGet(2, &obj3);
544 cs->blackZ = obj3.getNum();
548 if (obj1.dictLookup("Range", &obj2)->isArray() &&
549 obj2.arrayGetLength() == 4) {
550 obj2.arrayGet(0, &obj3);
551 cs->aMin = obj3.getNum();
553 obj2.arrayGet(1, &obj3);
554 cs->aMax = obj3.getNum();
556 obj2.arrayGet(2, &obj3);
557 cs->bMin = obj3.getNum();
559 obj2.arrayGet(3, &obj3);
560 cs->bMax = obj3.getNum();
566 cs->kr = 1 / (xyzrgb[0][0] * cs->whiteX +
567 xyzrgb[0][1] * cs->whiteY +
568 xyzrgb[0][2] * cs->whiteZ);
569 cs->kg = 1 / (xyzrgb[1][0] * cs->whiteX +
570 xyzrgb[1][1] * cs->whiteY +
571 xyzrgb[1][2] * cs->whiteZ);
572 cs->kb = 1 / (xyzrgb[2][0] * cs->whiteX +
573 xyzrgb[2][1] * cs->whiteY +
574 xyzrgb[2][2] * cs->whiteZ);
579 void GfxLabColorSpace::getGray(GfxColor *color, double *gray) {
583 *gray = clip01(0.299 * rgb.r +
588 void GfxLabColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
593 // convert L*a*b* to CIE 1931 XYZ color space
594 t1 = (color->c[0] + 16) / 116;
595 t2 = t1 + color->c[1] / 500;
596 if (t2 >= (6.0 / 29.0)) {
599 X = (108.0 / 841.0) * (t2 - (4.0 / 29.0));
602 if (t1 >= (6.0 / 29.0)) {
605 Y = (108.0 / 841.0) * (t1 - (4.0 / 29.0));
608 t2 = t1 - color->c[2] / 200;
609 if (t2 >= (6.0 / 29.0)) {
612 Z = (108.0 / 841.0) * (t2 - (4.0 / 29.0));
616 // convert XYZ to RGB, including gamut mapping and gamma correction
617 r = xyzrgb[0][0] * X + xyzrgb[0][1] * Y + xyzrgb[0][2] * Z;
618 g = xyzrgb[1][0] * X + xyzrgb[1][1] * Y + xyzrgb[1][2] * Z;
619 b = xyzrgb[2][0] * X + xyzrgb[2][1] * Y + xyzrgb[2][2] * Z;
620 rgb->r = pow(clip01(r * kr), 0.5);
621 rgb->g = pow(clip01(g * kg), 0.5);
622 rgb->b = pow(clip01(b * kb), 0.5);
625 void GfxLabColorSpace::getCMYK(GfxColor *color, GfxCMYK *cmyk) {
630 c = clip01(1 - rgb.r);
631 m = clip01(1 - rgb.g);
632 y = clip01(1 - rgb.b);
646 void GfxLabColorSpace::getDefaultRanges(double *decodeLow, double *decodeRange,
649 decodeRange[0] = 100;
651 decodeRange[1] = aMax - aMin;
653 decodeRange[2] = bMax - bMin;
656 //------------------------------------------------------------------------
657 // GfxICCBasedColorSpace
658 //------------------------------------------------------------------------
660 GfxICCBasedColorSpace::GfxICCBasedColorSpace(int nCompsA, GfxColorSpace *altA,
661 Ref *iccProfileStreamA) {
664 iccProfileStream = *iccProfileStreamA;
665 rangeMin[0] = rangeMin[1] = rangeMin[2] = rangeMin[3] = 0;
666 rangeMax[0] = rangeMax[1] = rangeMax[2] = rangeMax[3] = 1;
669 GfxICCBasedColorSpace::~GfxICCBasedColorSpace() {
673 GfxColorSpace *GfxICCBasedColorSpace::copy() {
674 GfxICCBasedColorSpace *cs;
677 cs = new GfxICCBasedColorSpace(nComps, alt->copy(), &iccProfileStream);
678 for (i = 0; i < 4; ++i) {
679 cs->rangeMin[i] = rangeMin[i];
680 cs->rangeMax[i] = rangeMax[i];
685 GfxColorSpace *GfxICCBasedColorSpace::parse(Array *arr) {
686 GfxICCBasedColorSpace *cs;
687 Ref iccProfileStreamA;
691 Object obj1, obj2, obj3;
694 arr->getNF(1, &obj1);
696 iccProfileStreamA = obj1.getRef();
698 iccProfileStreamA.num = 0;
699 iccProfileStreamA.gen = 0;
703 if (!obj1.isStream()) {
704 error(-1, "Bad ICCBased color space (stream)");
708 dict = obj1.streamGetDict();
709 if (!dict->lookup("N", &obj2)->isInt()) {
710 error(-1, "Bad ICCBased color space (N)");
715 nCompsA = obj2.getInt();
717 if (dict->lookup("Alternate", &obj2)->isNull() ||
718 !(altA = GfxColorSpace::parse(&obj2))) {
721 altA = new GfxDeviceGrayColorSpace();
724 altA = new GfxDeviceRGBColorSpace();
727 altA = new GfxDeviceCMYKColorSpace();
730 error(-1, "Bad ICCBased color space - invalid N");
737 cs = new GfxICCBasedColorSpace(nCompsA, altA, &iccProfileStreamA);
738 if (dict->lookup("Range", &obj2)->isArray() &&
739 obj2.arrayGetLength() == 2 * nCompsA) {
740 for (i = 0; i < nCompsA; ++i) {
741 obj2.arrayGet(2*i, &obj3);
742 cs->rangeMin[i] = obj3.getNum();
744 obj2.arrayGet(2*i+1, &obj3);
745 cs->rangeMax[i] = obj3.getNum();
754 void GfxICCBasedColorSpace::getGray(GfxColor *color, double *gray) {
755 alt->getGray(color, gray);
758 void GfxICCBasedColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
759 alt->getRGB(color, rgb);
762 void GfxICCBasedColorSpace::getCMYK(GfxColor *color, GfxCMYK *cmyk) {
763 alt->getCMYK(color, cmyk);
766 void GfxICCBasedColorSpace::getDefaultRanges(double *decodeLow,
769 alt->getDefaultRanges(decodeLow, decodeRange, maxImgPixel);
772 // this is nominally correct, but some PDF files don't set the
773 // correct ranges in the ICCBased dict
776 for (i = 0; i < nComps; ++i) {
777 decodeLow[i] = rangeMin[i];
778 decodeRange[i] = rangeMax[i] - rangeMin[i];
783 //------------------------------------------------------------------------
784 // GfxIndexedColorSpace
785 //------------------------------------------------------------------------
787 GfxIndexedColorSpace::GfxIndexedColorSpace(GfxColorSpace *baseA,
790 indexHigh = indexHighA;
791 lookup = (Guchar *)gmalloc((indexHigh + 1) * base->getNComps() *
795 GfxIndexedColorSpace::~GfxIndexedColorSpace() {
800 GfxColorSpace *GfxIndexedColorSpace::copy() {
801 GfxIndexedColorSpace *cs;
803 cs = new GfxIndexedColorSpace(base->copy(), indexHigh);
804 memcpy(cs->lookup, lookup,
805 (indexHigh + 1) * base->getNComps() * sizeof(Guchar));
809 GfxColorSpace *GfxIndexedColorSpace::parse(Array *arr) {
810 GfxIndexedColorSpace *cs;
811 GfxColorSpace *baseA;
818 if (arr->getLength() != 4) {
819 error(-1, "Bad Indexed color space");
823 if (!(baseA = GfxColorSpace::parse(&obj1))) {
824 error(-1, "Bad Indexed color space (base color space)");
828 if (!arr->get(2, &obj1)->isInt()) {
829 error(-1, "Bad Indexed color space (hival)");
833 indexHighA = obj1.getInt();
834 if (indexHighA < 0 || indexHighA > 255) {
835 // the PDF spec requires indexHigh to be in [0,255] -- allowing
836 // values larger than 255 creates a security hole: if nComps *
837 // indexHigh is greater than 2^31, the loop below may overwrite
838 // past the end of the array
839 error(-1, "Bad Indexed color space (invalid indexHigh value)");
844 cs = new GfxIndexedColorSpace(baseA, indexHighA);
846 n = baseA->getNComps();
847 if (obj1.isStream()) {
849 for (i = 0; i <= indexHighA; ++i) {
850 for (j = 0; j < n; ++j) {
851 if ((x = obj1.streamGetChar()) == EOF) {
852 error(-1, "Bad Indexed color space (lookup table stream too short)");
855 cs->lookup[i*n + j] = (Guchar)x;
859 } else if (obj1.isString()) {
860 if (obj1.getString()->getLength() < (indexHighA + 1) * n) {
861 error(-1, "Bad Indexed color space (lookup table string too short)");
864 s = obj1.getString()->getCString();
865 for (i = 0; i <= indexHighA; ++i) {
866 for (j = 0; j < n; ++j) {
867 cs->lookup[i*n + j] = (Guchar)*s++;
871 error(-1, "Bad Indexed color space (lookup table)");
885 GfxColor *GfxIndexedColorSpace::mapColorToBase(GfxColor *color,
886 GfxColor *baseColor) {
888 double low[gfxColorMaxComps], range[gfxColorMaxComps];
891 n = base->getNComps();
892 base->getDefaultRanges(low, range, indexHigh);
893 p = &lookup[(int)(color->c[0] + 0.5) * n];
894 for (i = 0; i < n; ++i) {
895 baseColor->c[i] = low[i] + (p[i] / 255.0) * range[i];
900 void GfxIndexedColorSpace::getGray(GfxColor *color, double *gray) {
903 base->getGray(mapColorToBase(color, &color2), gray);
906 void GfxIndexedColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
909 base->getRGB(mapColorToBase(color, &color2), rgb);
912 void GfxIndexedColorSpace::getCMYK(GfxColor *color, GfxCMYK *cmyk) {
915 base->getCMYK(mapColorToBase(color, &color2), cmyk);
918 void GfxIndexedColorSpace::getDefaultRanges(double *decodeLow,
922 decodeRange[0] = maxImgPixel;
925 //------------------------------------------------------------------------
926 // GfxSeparationColorSpace
927 //------------------------------------------------------------------------
929 GfxSeparationColorSpace::GfxSeparationColorSpace(GString *nameA,
937 GfxSeparationColorSpace::~GfxSeparationColorSpace() {
943 GfxColorSpace *GfxSeparationColorSpace::copy() {
944 return new GfxSeparationColorSpace(name->copy(), alt->copy(), func->copy());
947 //~ handle the 'All' and 'None' colorants
948 GfxColorSpace *GfxSeparationColorSpace::parse(Array *arr) {
949 GfxSeparationColorSpace *cs;
955 if (arr->getLength() != 4) {
956 error(-1, "Bad Separation color space");
959 if (!arr->get(1, &obj1)->isName()) {
960 error(-1, "Bad Separation color space (name)");
963 nameA = new GString(obj1.getName());
966 if (!(altA = GfxColorSpace::parse(&obj1))) {
967 error(-1, "Bad Separation color space (alternate color space)");
972 if (!(funcA = Function::parse(&obj1))) {
976 cs = new GfxSeparationColorSpace(nameA, altA, funcA);
989 void GfxSeparationColorSpace::getGray(GfxColor *color, double *gray) {
992 func->transform(color->c, color2.c);
993 alt->getGray(&color2, gray);
996 void GfxSeparationColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
999 func->transform(color->c, color2.c);
1000 alt->getRGB(&color2, rgb);
1003 void GfxSeparationColorSpace::getCMYK(GfxColor *color, GfxCMYK *cmyk) {
1006 func->transform(color->c, color2.c);
1007 alt->getCMYK(&color2, cmyk);
1010 //------------------------------------------------------------------------
1011 // GfxDeviceNColorSpace
1012 //------------------------------------------------------------------------
1014 GfxDeviceNColorSpace::GfxDeviceNColorSpace(int nCompsA,
1015 GfxColorSpace *altA,
1022 GfxDeviceNColorSpace::~GfxDeviceNColorSpace() {
1025 for (i = 0; i < nComps; ++i) {
1032 GfxColorSpace *GfxDeviceNColorSpace::copy() {
1033 GfxDeviceNColorSpace *cs;
1036 cs = new GfxDeviceNColorSpace(nComps, alt->copy(), func->copy());
1037 for (i = 0; i < nComps; ++i) {
1038 cs->names[i] = names[i]->copy();
1043 //~ handle the 'None' colorant
1044 GfxColorSpace *GfxDeviceNColorSpace::parse(Array *arr) {
1045 GfxDeviceNColorSpace *cs;
1047 GString *namesA[gfxColorMaxComps];
1048 GfxColorSpace *altA;
1053 if (arr->getLength() != 4 && arr->getLength() != 5) {
1054 error(-1, "Bad DeviceN color space");
1057 if (!arr->get(1, &obj1)->isArray()) {
1058 error(-1, "Bad DeviceN color space (names)");
1061 nCompsA = obj1.arrayGetLength();
1062 if (nCompsA > gfxColorMaxComps) {
1063 error(-1, "DeviceN color space with more than %d > %d components",
1064 nCompsA, gfxColorMaxComps);
1065 nCompsA = gfxColorMaxComps;
1067 for (i = 0; i < nCompsA; ++i) {
1068 if (!obj1.arrayGet(i, &obj2)->isName()) {
1069 error(-1, "Bad DeviceN color space (names)");
1073 namesA[i] = new GString(obj2.getName());
1078 if (!(altA = GfxColorSpace::parse(&obj1))) {
1079 error(-1, "Bad DeviceN color space (alternate color space)");
1084 if (!(funcA = Function::parse(&obj1))) {
1088 cs = new GfxDeviceNColorSpace(nCompsA, altA, funcA);
1089 for (i = 0; i < nCompsA; ++i) {
1090 cs->names[i] = namesA[i];
1097 for (i = 0; i < nCompsA; ++i) {
1106 void GfxDeviceNColorSpace::getGray(GfxColor *color, double *gray) {
1109 func->transform(color->c, color2.c);
1110 alt->getGray(&color2, gray);
1113 void GfxDeviceNColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
1116 func->transform(color->c, color2.c);
1117 alt->getRGB(&color2, rgb);
1120 void GfxDeviceNColorSpace::getCMYK(GfxColor *color, GfxCMYK *cmyk) {
1123 func->transform(color->c, color2.c);
1124 alt->getCMYK(&color2, cmyk);
1127 //------------------------------------------------------------------------
1128 // GfxPatternColorSpace
1129 //------------------------------------------------------------------------
1131 GfxPatternColorSpace::GfxPatternColorSpace(GfxColorSpace *underA) {
1135 GfxPatternColorSpace::~GfxPatternColorSpace() {
1141 GfxColorSpace *GfxPatternColorSpace::copy() {
1142 return new GfxPatternColorSpace(under ? under->copy() :
1143 (GfxColorSpace *)NULL);
1146 GfxColorSpace *GfxPatternColorSpace::parse(Array *arr) {
1147 GfxPatternColorSpace *cs;
1148 GfxColorSpace *underA;
1151 if (arr->getLength() != 1 && arr->getLength() != 2) {
1152 error(-1, "Bad Pattern color space");
1156 if (arr->getLength() == 2) {
1158 if (!(underA = GfxColorSpace::parse(&obj1))) {
1159 error(-1, "Bad Pattern color space (underlying color space)");
1165 cs = new GfxPatternColorSpace(underA);
1169 void GfxPatternColorSpace::getGray(GfxColor *color, double *gray) {
1173 void GfxPatternColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
1174 rgb->r = rgb->g = rgb->b = 0;
1177 void GfxPatternColorSpace::getCMYK(GfxColor *color, GfxCMYK *cmyk) {
1178 cmyk->c = cmyk->m = cmyk->y = 0;
1182 //------------------------------------------------------------------------
1184 //------------------------------------------------------------------------
1186 GfxPattern::GfxPattern(int typeA) {
1190 GfxPattern::~GfxPattern() {
1193 GfxPattern *GfxPattern::parse(Object *obj) {
1194 GfxPattern *pattern;
1197 if (obj->isDict()) {
1198 obj->dictLookup("PatternType", &obj1);
1199 } else if (obj->isStream()) {
1200 obj->streamGetDict()->lookup("PatternType", &obj1);
1205 if (obj1.isInt() && obj1.getInt() == 1) {
1206 pattern = GfxTilingPattern::parse(obj);
1207 } else if (obj1.isInt() && obj1.getInt() == 2) {
1208 pattern = GfxShadingPattern::parse(obj);
1214 //------------------------------------------------------------------------
1216 //------------------------------------------------------------------------
1218 GfxTilingPattern *GfxTilingPattern::parse(Object *patObj) {
1219 GfxTilingPattern *pat;
1221 int paintTypeA, tilingTypeA;
1222 double bboxA[4], matrixA[6];
1223 double xStepA, yStepA;
1228 if (!patObj->isStream()) {
1231 dict = patObj->streamGetDict();
1233 if (dict->lookup("PaintType", &obj1)->isInt()) {
1234 paintTypeA = obj1.getInt();
1237 error(-1, "Invalid or missing PaintType in pattern");
1240 if (dict->lookup("TilingType", &obj1)->isInt()) {
1241 tilingTypeA = obj1.getInt();
1244 error(-1, "Invalid or missing TilingType in pattern");
1247 bboxA[0] = bboxA[1] = 0;
1248 bboxA[2] = bboxA[3] = 1;
1249 if (dict->lookup("BBox", &obj1)->isArray() &&
1250 obj1.arrayGetLength() == 4) {
1251 for (i = 0; i < 4; ++i) {
1252 if (obj1.arrayGet(i, &obj2)->isNum()) {
1253 bboxA[i] = obj2.getNum();
1258 error(-1, "Invalid or missing BBox in pattern");
1261 if (dict->lookup("XStep", &obj1)->isNum()) {
1262 xStepA = obj1.getNum();
1265 error(-1, "Invalid or missing XStep in pattern");
1268 if (dict->lookup("YStep", &obj1)->isNum()) {
1269 yStepA = obj1.getNum();
1272 error(-1, "Invalid or missing YStep in pattern");
1275 if (!dict->lookup("Resources", &resDictA)->isDict()) {
1277 resDictA.initNull();
1278 error(-1, "Invalid or missing Resources in pattern");
1280 matrixA[0] = 1; matrixA[1] = 0;
1281 matrixA[2] = 0; matrixA[3] = 1;
1282 matrixA[4] = 0; matrixA[5] = 0;
1283 if (dict->lookup("Matrix", &obj1)->isArray() &&
1284 obj1.arrayGetLength() == 6) {
1285 for (i = 0; i < 6; ++i) {
1286 if (obj1.arrayGet(i, &obj2)->isNum()) {
1287 matrixA[i] = obj2.getNum();
1294 pat = new GfxTilingPattern(paintTypeA, tilingTypeA, bboxA, xStepA, yStepA,
1295 &resDictA, matrixA, patObj);
1300 GfxTilingPattern::GfxTilingPattern(int paintTypeA, int tilingTypeA,
1301 double *bboxA, double xStepA, double yStepA,
1302 Object *resDictA, double *matrixA,
1303 Object *contentStreamA):
1308 paintType = paintTypeA;
1309 tilingType = tilingTypeA;
1310 for (i = 0; i < 4; ++i) {
1315 resDictA->copy(&resDict);
1316 for (i = 0; i < 6; ++i) {
1317 matrix[i] = matrixA[i];
1319 contentStreamA->copy(&contentStream);
1322 GfxTilingPattern::~GfxTilingPattern() {
1324 contentStream.free();
1327 GfxPattern *GfxTilingPattern::copy() {
1328 return new GfxTilingPattern(paintType, tilingType, bbox, xStep, yStep,
1329 &resDict, matrix, &contentStream);
1332 //------------------------------------------------------------------------
1333 // GfxShadingPattern
1334 //------------------------------------------------------------------------
1336 GfxShadingPattern *GfxShadingPattern::parse(Object *patObj) {
1338 GfxShading *shadingA;
1343 if (!patObj->isDict()) {
1346 dict = patObj->getDict();
1348 dict->lookup("Shading", &obj1);
1349 shadingA = GfxShading::parse(&obj1);
1355 matrixA[0] = 1; matrixA[1] = 0;
1356 matrixA[2] = 0; matrixA[3] = 1;
1357 matrixA[4] = 0; matrixA[5] = 0;
1358 if (dict->lookup("Matrix", &obj1)->isArray() &&
1359 obj1.arrayGetLength() == 6) {
1360 for (i = 0; i < 6; ++i) {
1361 if (obj1.arrayGet(i, &obj2)->isNum()) {
1362 matrixA[i] = obj2.getNum();
1369 return new GfxShadingPattern(shadingA, matrixA);
1372 GfxShadingPattern::GfxShadingPattern(GfxShading *shadingA, double *matrixA):
1378 for (i = 0; i < 6; ++i) {
1379 matrix[i] = matrixA[i];
1383 GfxShadingPattern::~GfxShadingPattern() {
1387 GfxPattern *GfxShadingPattern::copy() {
1388 return new GfxShadingPattern(shading->copy(), matrix);
1391 //------------------------------------------------------------------------
1393 //------------------------------------------------------------------------
1395 GfxShading::GfxShading(int typeA) {
1400 GfxShading::GfxShading(GfxShading *shading) {
1403 type = shading->type;
1404 colorSpace = shading->colorSpace->copy();
1405 for (i = 0; i < gfxColorMaxComps; ++i) {
1406 background.c[i] = shading->background.c[i];
1408 hasBackground = shading->hasBackground;
1409 xMin = shading->xMin;
1410 yMin = shading->yMin;
1411 xMax = shading->xMax;
1412 yMax = shading->yMax;
1413 hasBBox = shading->hasBBox;
1416 GfxShading::~GfxShading() {
1422 GfxShading *GfxShading::parse(Object *obj) {
1423 GfxShading *shading;
1428 if (obj->isDict()) {
1429 dict = obj->getDict();
1430 } else if (obj->isStream()) {
1431 dict = obj->streamGetDict();
1436 if (!dict->lookup("ShadingType", &obj1)->isInt()) {
1437 error(-1, "Invalid ShadingType in shading dictionary");
1441 typeA = obj1.getInt();
1446 shading = GfxFunctionShading::parse(dict);
1449 shading = GfxAxialShading::parse(dict);
1452 shading = GfxRadialShading::parse(dict);
1455 error(-1, "Unimplemented shading type %d", typeA);
1465 GBool GfxShading::init(Dict *dict) {
1469 dict->lookup("ColorSpace", &obj1);
1470 if (!(colorSpace = GfxColorSpace::parse(&obj1))) {
1471 error(-1, "Bad color space in shading dictionary");
1477 for (i = 0; i < gfxColorMaxComps; ++i) {
1478 background.c[i] = 0;
1480 hasBackground = gFalse;
1481 if (dict->lookup("Background", &obj1)->isArray()) {
1482 if (obj1.arrayGetLength() == colorSpace->getNComps()) {
1483 hasBackground = gTrue;
1484 for (i = 0; i < colorSpace->getNComps(); ++i) {
1485 background.c[i] = obj1.arrayGet(i, &obj2)->getNum();
1489 error(-1, "Bad Background in shading dictionary");
1494 xMin = yMin = xMax = yMax = 0;
1496 if (dict->lookup("BBox", &obj1)->isArray()) {
1497 if (obj1.arrayGetLength() == 4) {
1499 xMin = obj1.arrayGet(0, &obj2)->getNum();
1501 yMin = obj1.arrayGet(1, &obj2)->getNum();
1503 xMax = obj1.arrayGet(2, &obj2)->getNum();
1505 yMax = obj1.arrayGet(3, &obj2)->getNum();
1508 error(-1, "Bad BBox in shading dictionary");
1516 //------------------------------------------------------------------------
1517 // GfxFunctionShading
1518 //------------------------------------------------------------------------
1520 GfxFunctionShading::GfxFunctionShading(double x0A, double y0A,
1521 double x1A, double y1A,
1523 Function **funcsA, int nFuncsA):
1532 for (i = 0; i < 6; ++i) {
1533 matrix[i] = matrixA[i];
1536 for (i = 0; i < nFuncs; ++i) {
1537 funcs[i] = funcsA[i];
1541 GfxFunctionShading::GfxFunctionShading(GfxFunctionShading *shading):
1550 for (i = 0; i < 6; ++i) {
1551 matrix[i] = shading->matrix[i];
1553 nFuncs = shading->nFuncs;
1554 for (i = 0; i < nFuncs; ++i) {
1555 funcs[i] = shading->funcs[i]->copy();
1559 GfxFunctionShading::~GfxFunctionShading() {
1562 for (i = 0; i < nFuncs; ++i) {
1567 GfxFunctionShading *GfxFunctionShading::parse(Dict *dict) {
1568 GfxFunctionShading *shading;
1569 double x0A, y0A, x1A, y1A;
1571 Function *funcsA[gfxColorMaxComps];
1578 if (dict->lookup("Domain", &obj1)->isArray() &&
1579 obj1.arrayGetLength() == 4) {
1580 x0A = obj1.arrayGet(0, &obj2)->getNum();
1582 y0A = obj1.arrayGet(1, &obj2)->getNum();
1584 x1A = obj1.arrayGet(2, &obj2)->getNum();
1586 y1A = obj1.arrayGet(3, &obj2)->getNum();
1591 matrixA[0] = 1; matrixA[1] = 0;
1592 matrixA[2] = 0; matrixA[3] = 1;
1593 matrixA[4] = 0; matrixA[5] = 0;
1594 if (dict->lookup("Matrix", &obj1)->isArray() &&
1595 obj1.arrayGetLength() == 6) {
1596 matrixA[0] = obj1.arrayGet(0, &obj2)->getNum();
1598 matrixA[1] = obj1.arrayGet(1, &obj2)->getNum();
1600 matrixA[2] = obj1.arrayGet(2, &obj2)->getNum();
1602 matrixA[3] = obj1.arrayGet(3, &obj2)->getNum();
1604 matrixA[4] = obj1.arrayGet(4, &obj2)->getNum();
1606 matrixA[5] = obj1.arrayGet(5, &obj2)->getNum();
1611 dict->lookup("Function", &obj1);
1612 if (obj1.isArray()) {
1613 nFuncsA = obj1.arrayGetLength();
1614 if (nFuncsA > gfxColorMaxComps) {
1615 error(-1, "Invalid Function array in shading dictionary");
1618 for (i = 0; i < nFuncsA; ++i) {
1619 obj1.arrayGet(i, &obj2);
1620 if (!(funcsA[i] = Function::parse(&obj2))) {
1627 if (!(funcsA[0] = Function::parse(&obj1))) {
1633 shading = new GfxFunctionShading(x0A, y0A, x1A, y1A, matrixA,
1635 if (!shading->init(dict)) {
1648 GfxShading *GfxFunctionShading::copy() {
1649 return new GfxFunctionShading(this);
1652 void GfxFunctionShading::getColor(double x, double y, GfxColor *color) {
1658 for (i = 0; i < nFuncs; ++i) {
1659 funcs[i]->transform(in, &color->c[i]);
1663 //------------------------------------------------------------------------
1665 //------------------------------------------------------------------------
1667 GfxAxialShading::GfxAxialShading(double x0A, double y0A,
1668 double x1A, double y1A,
1669 double t0A, double t1A,
1670 Function **funcsA, int nFuncsA,
1671 GBool extend0A, GBool extend1A):
1683 for (i = 0; i < nFuncs; ++i) {
1684 funcs[i] = funcsA[i];
1690 GfxAxialShading::GfxAxialShading(GfxAxialShading *shading):
1701 nFuncs = shading->nFuncs;
1702 for (i = 0; i < nFuncs; ++i) {
1703 funcs[i] = shading->funcs[i]->copy();
1705 extend0 = shading->extend0;
1706 extend1 = shading->extend1;
1709 GfxAxialShading::~GfxAxialShading() {
1712 for (i = 0; i < nFuncs; ++i) {
1717 GfxAxialShading *GfxAxialShading::parse(Dict *dict) {
1718 GfxAxialShading *shading;
1719 double x0A, y0A, x1A, y1A;
1721 Function *funcsA[gfxColorMaxComps];
1723 GBool extend0A, extend1A;
1727 x0A = y0A = x1A = y1A = 0;
1728 if (dict->lookup("Coords", &obj1)->isArray() &&
1729 obj1.arrayGetLength() == 4) {
1730 x0A = obj1.arrayGet(0, &obj2)->getNum();
1732 y0A = obj1.arrayGet(1, &obj2)->getNum();
1734 x1A = obj1.arrayGet(2, &obj2)->getNum();
1736 y1A = obj1.arrayGet(3, &obj2)->getNum();
1739 error(-1, "Missing or invalid Coords in shading dictionary");
1746 if (dict->lookup("Domain", &obj1)->isArray() &&
1747 obj1.arrayGetLength() == 2) {
1748 t0A = obj1.arrayGet(0, &obj2)->getNum();
1750 t1A = obj1.arrayGet(1, &obj2)->getNum();
1755 dict->lookup("Function", &obj1);
1756 if (obj1.isArray()) {
1757 nFuncsA = obj1.arrayGetLength();
1758 if (nFuncsA > gfxColorMaxComps) {
1759 error(-1, "Invalid Function array in shading dictionary");
1762 for (i = 0; i < nFuncsA; ++i) {
1763 obj1.arrayGet(i, &obj2);
1764 if (!(funcsA[i] = Function::parse(&obj2))) {
1773 if (!(funcsA[0] = Function::parse(&obj1))) {
1780 extend0A = extend1A = gFalse;
1781 if (dict->lookup("Extend", &obj1)->isArray() &&
1782 obj1.arrayGetLength() == 2) {
1783 extend0A = obj1.arrayGet(0, &obj2)->getBool();
1785 extend1A = obj1.arrayGet(1, &obj2)->getBool();
1790 shading = new GfxAxialShading(x0A, y0A, x1A, y1A, t0A, t1A,
1791 funcsA, nFuncsA, extend0A, extend1A);
1792 if (!shading->init(dict)) {
1802 GfxShading *GfxAxialShading::copy() {
1803 return new GfxAxialShading(this);
1806 void GfxAxialShading::getColor(double t, GfxColor *color) {
1809 // NB: there can be one function with n outputs or n functions with
1810 // one output each (where n = number of color components)
1811 for (i = 0; i < nFuncs; ++i) {
1812 funcs[i]->transform(&t, &color->c[i]);
1816 //------------------------------------------------------------------------
1818 //------------------------------------------------------------------------
1820 GfxRadialShading::GfxRadialShading(double x0A, double y0A, double r0A,
1821 double x1A, double y1A, double r1A,
1822 double t0A, double t1A,
1823 Function **funcsA, int nFuncsA,
1824 GBool extend0A, GBool extend1A):
1838 for (i = 0; i < nFuncs; ++i) {
1839 funcs[i] = funcsA[i];
1845 GfxRadialShading::GfxRadialShading(GfxRadialShading *shading):
1858 nFuncs = shading->nFuncs;
1859 for (i = 0; i < nFuncs; ++i) {
1860 funcs[i] = shading->funcs[i]->copy();
1862 extend0 = shading->extend0;
1863 extend1 = shading->extend1;
1866 GfxRadialShading::~GfxRadialShading() {
1869 for (i = 0; i < nFuncs; ++i) {
1874 GfxRadialShading *GfxRadialShading::parse(Dict *dict) {
1875 GfxRadialShading *shading;
1876 double x0A, y0A, r0A, x1A, y1A, r1A;
1878 Function *funcsA[gfxColorMaxComps];
1880 GBool extend0A, extend1A;
1884 x0A = y0A = r0A = x1A = y1A = r1A = 0;
1885 if (dict->lookup("Coords", &obj1)->isArray() &&
1886 obj1.arrayGetLength() == 6) {
1887 x0A = obj1.arrayGet(0, &obj2)->getNum();
1889 y0A = obj1.arrayGet(1, &obj2)->getNum();
1891 r0A = obj1.arrayGet(2, &obj2)->getNum();
1893 x1A = obj1.arrayGet(3, &obj2)->getNum();
1895 y1A = obj1.arrayGet(4, &obj2)->getNum();
1897 r1A = obj1.arrayGet(5, &obj2)->getNum();
1900 error(-1, "Missing or invalid Coords in shading dictionary");
1907 if (dict->lookup("Domain", &obj1)->isArray() &&
1908 obj1.arrayGetLength() == 2) {
1909 t0A = obj1.arrayGet(0, &obj2)->getNum();
1911 t1A = obj1.arrayGet(1, &obj2)->getNum();
1916 dict->lookup("Function", &obj1);
1917 if (obj1.isArray()) {
1918 nFuncsA = obj1.arrayGetLength();
1919 if (nFuncsA > gfxColorMaxComps) {
1920 error(-1, "Invalid Function array in shading dictionary");
1923 for (i = 0; i < nFuncsA; ++i) {
1924 obj1.arrayGet(i, &obj2);
1925 if (!(funcsA[i] = Function::parse(&obj2))) {
1934 if (!(funcsA[0] = Function::parse(&obj1))) {
1941 extend0A = extend1A = gFalse;
1942 if (dict->lookup("Extend", &obj1)->isArray() &&
1943 obj1.arrayGetLength() == 2) {
1944 extend0A = obj1.arrayGet(0, &obj2)->getBool();
1946 extend1A = obj1.arrayGet(1, &obj2)->getBool();
1951 shading = new GfxRadialShading(x0A, y0A, r0A, x1A, y1A, r1A, t0A, t1A,
1952 funcsA, nFuncsA, extend0A, extend1A);
1953 if (!shading->init(dict)) {
1963 GfxShading *GfxRadialShading::copy() {
1964 return new GfxRadialShading(this);
1967 void GfxRadialShading::getColor(double t, GfxColor *color) {
1970 // NB: there can be one function with n outputs or n functions with
1971 // one output each (where n = number of color components)
1972 for (i = 0; i < nFuncs; ++i) {
1973 funcs[i]->transform(&t, &color->c[i]);
1977 //------------------------------------------------------------------------
1979 //------------------------------------------------------------------------
1981 GfxImageColorMap::GfxImageColorMap(int bitsA, Object *decode,
1982 GfxColorSpace *colorSpaceA) {
1983 GfxIndexedColorSpace *indexedCS;
1984 GfxSeparationColorSpace *sepCS;
1985 int maxPixel, indexHigh;
1989 double x[gfxColorMaxComps];
1990 double y[gfxColorMaxComps];
1995 // bits per component and color space
1997 maxPixel = (1 << bits) - 1;
1998 colorSpace = colorSpaceA;
2001 if (decode->isNull()) {
2002 nComps = colorSpace->getNComps();
2003 colorSpace->getDefaultRanges(decodeLow, decodeRange, maxPixel);
2004 } else if (decode->isArray()) {
2005 nComps = decode->arrayGetLength() / 2;
2006 if (nComps != colorSpace->getNComps()) {
2009 for (i = 0; i < nComps; ++i) {
2010 decode->arrayGet(2*i, &obj);
2014 decodeLow[i] = obj.getNum();
2016 decode->arrayGet(2*i+1, &obj);
2020 decodeRange[i] = obj.getNum() - decodeLow[i];
2027 // Construct a lookup table -- this stores pre-computed decoded
2028 // values for each component, i.e., the result of applying the
2029 // decode mapping to each possible image pixel component value.
2031 // Optimization: for Indexed and Separation color spaces (which have
2032 // only one component), we store color values in the lookup table
2033 // rather than component values.
2036 if (colorSpace->getMode() == csIndexed) {
2037 // Note that indexHigh may not be the same as maxPixel --
2038 // Distiller will remove unused palette entries, resulting in
2039 // indexHigh < maxPixel.
2040 indexedCS = (GfxIndexedColorSpace *)colorSpace;
2041 colorSpace2 = indexedCS->getBase();
2042 indexHigh = indexedCS->getIndexHigh();
2043 nComps2 = colorSpace2->getNComps();
2044 lookup = (double *)gmalloc((maxPixel + 1) * nComps2 * sizeof(double));
2045 lookup2 = indexedCS->getLookup();
2046 colorSpace2->getDefaultRanges(x, y, indexHigh);
2047 for (i = 0; i <= maxPixel; ++i) {
2048 j = (int)(decodeLow[0] + (i * decodeRange[0]) / maxPixel + 0.5);
2051 } else if (j > indexHigh) {
2054 for (k = 0; k < nComps2; ++k) {
2055 lookup[i*nComps2 + k] = x[k] + (lookup2[j*nComps2 + k] / 255.0) * y[k];
2058 } else if (colorSpace->getMode() == csSeparation) {
2059 sepCS = (GfxSeparationColorSpace *)colorSpace;
2060 colorSpace2 = sepCS->getAlt();
2061 nComps2 = colorSpace2->getNComps();
2062 lookup = (double *)gmalloc((maxPixel + 1) * nComps2 * sizeof(double));
2063 sepFunc = sepCS->getFunc();
2064 for (i = 0; i <= maxPixel; ++i) {
2065 x[0] = decodeLow[0] + (i * decodeRange[0]) / maxPixel;
2066 sepFunc->transform(x, y);
2067 for (k = 0; k < nComps2; ++k) {
2068 lookup[i*nComps2 + k] = y[k];
2072 lookup = (double *)gmalloc((maxPixel + 1) * nComps * sizeof(double));
2073 for (i = 0; i <= maxPixel; ++i) {
2074 for (k = 0; k < nComps; ++k) {
2075 lookup[i*nComps + k] = decodeLow[k] +
2076 (i * decodeRange[k]) / maxPixel;
2089 GfxImageColorMap::GfxImageColorMap(GfxImageColorMap *colorMap) {
2092 colorSpace = colorMap->colorSpace->copy();
2093 bits = colorMap->bits;
2094 nComps = colorMap->nComps;
2095 nComps2 = colorMap->nComps2;
2099 if (colorSpace->getMode() == csIndexed) {
2100 colorSpace2 = ((GfxIndexedColorSpace *)colorSpace)->getBase();
2101 n = n * nComps2 * sizeof(double);
2102 } else if (colorSpace->getMode() == csSeparation) {
2103 colorSpace2 = ((GfxSeparationColorSpace *)colorSpace)->getAlt();
2104 n = n * nComps2 * sizeof(double);
2106 n = n * nComps * sizeof(double);
2108 lookup = (double *)gmalloc(n);
2109 memcpy(lookup, colorMap->lookup, n);
2110 for (i = 0; i < nComps; ++i) {
2111 decodeLow[i] = colorMap->decodeLow[i];
2112 decodeRange[i] = colorMap->decodeRange[i];
2117 GfxImageColorMap::~GfxImageColorMap() {
2122 void GfxImageColorMap::getGray(Guchar *x, double *gray) {
2128 p = &lookup[x[0] * nComps2];
2129 for (i = 0; i < nComps2; ++i) {
2132 colorSpace2->getGray(&color, gray);
2134 for (i = 0; i < nComps; ++i) {
2135 color.c[i] = lookup[x[i] * nComps + i];
2137 colorSpace->getGray(&color, gray);
2141 void GfxImageColorMap::getRGB(Guchar *x, GfxRGB *rgb) {
2147 p = &lookup[x[0] * nComps2];
2148 for (i = 0; i < nComps2; ++i) {
2151 colorSpace2->getRGB(&color, rgb);
2153 for (i = 0; i < nComps; ++i) {
2154 color.c[i] = lookup[x[i] * nComps + i];
2156 colorSpace->getRGB(&color, rgb);
2160 void GfxImageColorMap::getCMYK(Guchar *x, GfxCMYK *cmyk) {
2166 p = &lookup[x[0] * nComps2];
2167 for (i = 0; i < nComps2; ++i) {
2170 colorSpace2->getCMYK(&color, cmyk);
2172 for (i = 0; i < nComps; ++i) {
2173 color.c[i] = lookup[x[i] * nComps + i];
2175 colorSpace->getCMYK(&color, cmyk);
2179 void GfxImageColorMap::getColor(Guchar *x, GfxColor *color) {
2182 maxPixel = (1 << bits) - 1;
2183 for (i = 0; i < nComps; ++i) {
2184 color->c[i] = decodeLow[i] + (x[i] * decodeRange[i]) / maxPixel;
2188 //------------------------------------------------------------------------
2189 // GfxSubpath and GfxPath
2190 //------------------------------------------------------------------------
2192 GfxSubpath::GfxSubpath(double x1, double y1) {
2194 x = (double *)gmalloc(size * sizeof(double));
2195 y = (double *)gmalloc(size * sizeof(double));
2196 curve = (GBool *)gmalloc(size * sizeof(GBool));
2204 GfxSubpath::~GfxSubpath() {
2211 GfxSubpath::GfxSubpath(GfxSubpath *subpath) {
2212 size = subpath->size;
2214 x = (double *)gmalloc(size * sizeof(double));
2215 y = (double *)gmalloc(size * sizeof(double));
2216 curve = (GBool *)gmalloc(size * sizeof(GBool));
2217 memcpy(x, subpath->x, n * sizeof(double));
2218 memcpy(y, subpath->y, n * sizeof(double));
2219 memcpy(curve, subpath->curve, n * sizeof(GBool));
2220 closed = subpath->closed;
2223 void GfxSubpath::lineTo(double x1, double y1) {
2226 x = (double *)grealloc(x, size * sizeof(double));
2227 y = (double *)grealloc(y, size * sizeof(double));
2228 curve = (GBool *)grealloc(curve, size * sizeof(GBool));
2236 void GfxSubpath::curveTo(double x1, double y1, double x2, double y2,
2237 double x3, double y3) {
2240 x = (double *)grealloc(x, size * sizeof(double));
2241 y = (double *)grealloc(y, size * sizeof(double));
2242 curve = (GBool *)grealloc(curve, size * sizeof(GBool));
2250 curve[n] = curve[n+1] = gTrue;
2251 curve[n+2] = gFalse;
2255 void GfxSubpath::close() {
2256 if (x[n-1] != x[0] || y[n-1] != y[0]) {
2262 void GfxSubpath::offset(double dx, double dy) {
2265 for (i = 0; i < n; ++i) {
2271 GfxPath::GfxPath() {
2275 firstX = firstY = 0;
2276 subpaths = (GfxSubpath **)gmalloc(size * sizeof(GfxSubpath *));
2279 GfxPath::~GfxPath() {
2282 for (i = 0; i < n; ++i)
2288 GfxPath::GfxPath(GBool justMoved1, double firstX1, double firstY1,
2289 GfxSubpath **subpaths1, int n1, int size1) {
2292 justMoved = justMoved1;
2297 subpaths = (GfxSubpath **)gmalloc(size * sizeof(GfxSubpath *));
2298 for (i = 0; i < n; ++i)
2299 subpaths[i] = subpaths1[i]->copy();
2302 void GfxPath::moveTo(double x, double y) {
2308 void GfxPath::lineTo(double x, double y) {
2312 subpaths = (GfxSubpath **)
2313 grealloc(subpaths, size * sizeof(GfxSubpath *));
2315 subpaths[n] = new GfxSubpath(firstX, firstY);
2319 subpaths[n-1]->lineTo(x, y);
2322 void GfxPath::curveTo(double x1, double y1, double x2, double y2,
2323 double x3, double y3) {
2327 subpaths = (GfxSubpath **)
2328 grealloc(subpaths, size * sizeof(GfxSubpath *));
2330 subpaths[n] = new GfxSubpath(firstX, firstY);
2334 subpaths[n-1]->curveTo(x1, y1, x2, y2, x3, y3);
2337 void GfxPath::close() {
2338 // this is necessary to handle the pathological case of
2339 // moveto/closepath/clip, which defines an empty clipping region
2343 subpaths = (GfxSubpath **)
2344 grealloc(subpaths, size * sizeof(GfxSubpath *));
2346 subpaths[n] = new GfxSubpath(firstX, firstY);
2350 subpaths[n-1]->close();
2353 void GfxPath::append(GfxPath *path) {
2356 if (n + path->n > size) {
2358 subpaths = (GfxSubpath **)
2359 grealloc(subpaths, size * sizeof(GfxSubpath *));
2361 for (i = 0; i < path->n; ++i) {
2362 subpaths[n++] = path->subpaths[i]->copy();
2367 void GfxPath::offset(double dx, double dy) {
2370 for (i = 0; i < n; ++i) {
2371 subpaths[i]->offset(dx, dy);
2375 //------------------------------------------------------------------------
2377 //------------------------------------------------------------------------
2379 GfxState::GfxState(double hDPI, double vDPI, PDFRectangle *pageBox,
2380 int rotate, GBool upsideDown) {
2391 ctm[1] = upsideDown ? ky : -ky;
2395 ctm[5] = ky * (upsideDown ? -px1 : px2);
2396 pageWidth = kx * (py2 - py1);
2397 pageHeight = ky * (px2 - px1);
2398 } else if (rotate == 180) {
2402 ctm[3] = upsideDown ? ky : -ky;
2404 ctm[5] = ky * (upsideDown ? -py1 : py2);
2405 pageWidth = kx * (px2 - px1);
2406 pageHeight = ky * (py2 - py1);
2407 } else if (rotate == 270) {
2409 ctm[1] = upsideDown ? -ky : ky;
2413 ctm[5] = ky * (upsideDown ? px2 : -px1);
2414 pageWidth = kx * (py2 - py1);
2415 pageHeight = ky * (px2 - px1);
2420 ctm[3] = upsideDown ? -ky : ky;
2422 ctm[5] = ky * (upsideDown ? py2 : -py1);
2423 pageWidth = kx * (px2 - px1);
2424 pageHeight = ky * (py2 - py1);
2427 fillColorSpace = new GfxDeviceGrayColorSpace();
2428 strokeColorSpace = new GfxDeviceGrayColorSpace();
2430 strokeColor.c[0] = 0;
2432 strokePattern = NULL;
2447 textMat[0] = 1; textMat[1] = 0;
2448 textMat[2] = 0; textMat[3] = 1;
2449 textMat[4] = 0; textMat[5] = 0;
2457 path = new GfxPath();
2463 clipXMax = pageWidth;
2464 clipYMax = pageHeight;
2469 GfxState::~GfxState() {
2470 if (fillColorSpace) {
2471 delete fillColorSpace;
2473 if (strokeColorSpace) {
2474 delete strokeColorSpace;
2479 if (strokePattern) {
2480 delete strokePattern;
2484 // this gets set to NULL by restore()
2493 GfxState::GfxState(GfxState *state) {
2494 memcpy(this, state, sizeof(GfxState));
2495 if (fillColorSpace) {
2496 fillColorSpace = state->fillColorSpace->copy();
2498 if (strokeColorSpace) {
2499 strokeColorSpace = state->strokeColorSpace->copy();
2502 fillPattern = state->fillPattern->copy();
2504 if (strokePattern) {
2505 strokePattern = state->strokePattern->copy();
2507 if (lineDashLength > 0) {
2508 lineDash = (double *)gmalloc(lineDashLength * sizeof(double));
2509 memcpy(lineDash, state->lineDash, lineDashLength * sizeof(double));
2514 void GfxState::setPath(GfxPath *pathA) {
2519 void GfxState::getUserClipBBox(double *xMin, double *yMin,
2520 double *xMax, double *yMax) {
2522 double xMin1, yMin1, xMax1, yMax1, det, tx, ty;
2525 det = 1 / (ctm[0] * ctm[3] - ctm[1] * ctm[2]);
2526 ictm[0] = ctm[3] * det;
2527 ictm[1] = -ctm[1] * det;
2528 ictm[2] = -ctm[2] * det;
2529 ictm[3] = ctm[0] * det;
2530 ictm[4] = (ctm[2] * ctm[5] - ctm[3] * ctm[4]) * det;
2531 ictm[5] = (ctm[1] * ctm[4] - ctm[0] * ctm[5]) * det;
2533 // transform all four corners of the clip bbox; find the min and max
2535 xMin1 = xMax1 = clipXMin * ictm[0] + clipYMin * ictm[2] + ictm[4];
2536 yMin1 = yMax1 = clipXMin * ictm[1] + clipYMin * ictm[3] + ictm[5];
2537 tx = clipXMin * ictm[0] + clipYMax * ictm[2] + ictm[4];
2538 ty = clipXMin * ictm[1] + clipYMax * ictm[3] + ictm[5];
2541 } else if (tx > xMax1) {
2546 } else if (ty > yMax1) {
2549 tx = clipXMax * ictm[0] + clipYMin * ictm[2] + ictm[4];
2550 ty = clipXMax * ictm[1] + clipYMin * ictm[3] + ictm[5];
2553 } else if (tx > xMax1) {
2558 } else if (ty > yMax1) {
2561 tx = clipXMax * ictm[0] + clipYMax * ictm[2] + ictm[4];
2562 ty = clipXMax * ictm[1] + clipYMax * ictm[3] + ictm[5];
2565 } else if (tx > xMax1) {
2570 } else if (ty > yMax1) {
2580 double GfxState::transformWidth(double w) {
2583 x = ctm[0] + ctm[2];
2584 y = ctm[1] + ctm[3];
2585 return w * sqrt(0.5 * (x * x + y * y));
2588 double GfxState::getTransformedFontSize() {
2589 double x1, y1, x2, y2;
2591 x1 = textMat[2] * fontSize;
2592 y1 = textMat[3] * fontSize;
2593 x2 = ctm[0] * x1 + ctm[2] * y1;
2594 y2 = ctm[1] * x1 + ctm[3] * y1;
2595 return sqrt(x2 * x2 + y2 * y2);
2598 void GfxState::getFontTransMat(double *m11, double *m12,
2599 double *m21, double *m22) {
2600 *m11 = (textMat[0] * ctm[0] + textMat[1] * ctm[2]) * fontSize;
2601 *m12 = (textMat[0] * ctm[1] + textMat[1] * ctm[3]) * fontSize;
2602 *m21 = (textMat[2] * ctm[0] + textMat[3] * ctm[2]) * fontSize;
2603 *m22 = (textMat[2] * ctm[1] + textMat[3] * ctm[3]) * fontSize;
2606 void GfxState::setCTM(double a, double b, double c,
2607 double d, double e, double f) {
2617 // avoid FP exceptions on badly messed up PDF files
2618 for (i = 0; i < 6; ++i) {
2619 if (ctm[i] > 1e10) {
2621 } else if (ctm[i] < -1e10) {
2627 void GfxState::concatCTM(double a, double b, double c,
2628 double d, double e, double f) {
2635 ctm[0] = a * a1 + b * c1;
2636 ctm[1] = a * b1 + b * d1;
2637 ctm[2] = c * a1 + d * c1;
2638 ctm[3] = c * b1 + d * d1;
2639 ctm[4] = e * a1 + f * c1 + ctm[4];
2640 ctm[5] = e * b1 + f * d1 + ctm[5];
2642 // avoid FP exceptions on badly messed up PDF files
2643 for (i = 0; i < 6; ++i) {
2644 if (ctm[i] > 1e10) {
2646 } else if (ctm[i] < -1e10) {
2652 void GfxState::setFillColorSpace(GfxColorSpace *colorSpace) {
2653 if (fillColorSpace) {
2654 delete fillColorSpace;
2656 fillColorSpace = colorSpace;
2659 void GfxState::setStrokeColorSpace(GfxColorSpace *colorSpace) {
2660 if (strokeColorSpace) {
2661 delete strokeColorSpace;
2663 strokeColorSpace = colorSpace;
2666 void GfxState::setFillPattern(GfxPattern *pattern) {
2670 fillPattern = pattern;
2673 void GfxState::setStrokePattern(GfxPattern *pattern) {
2674 if (strokePattern) {
2675 delete strokePattern;
2677 strokePattern = pattern;
2680 void GfxState::setLineDash(double *dash, int length, double start) {
2684 lineDashLength = length;
2685 lineDashStart = start;
2688 void GfxState::clearPath() {
2690 path = new GfxPath();
2693 void GfxState::clip() {
2694 double xMin, yMin, xMax, yMax, x, y;
2695 GfxSubpath *subpath;
2698 xMin = xMax = yMin = yMax = 0; // make gcc happy
2699 for (i = 0; i < path->getNumSubpaths(); ++i) {
2700 subpath = path->getSubpath(i);
2701 for (j = 0; j < subpath->getNumPoints(); ++j) {
2702 transform(subpath->getX(j), subpath->getY(j), &x, &y);
2703 if (i == 0 && j == 0) {
2709 } else if (x > xMax) {
2714 } else if (y > yMax) {
2720 if (xMin > clipXMin) {
2723 if (yMin > clipYMin) {
2726 if (xMax < clipXMax) {
2729 if (yMax < clipYMax) {
2734 void GfxState::textShift(double tx, double ty) {
2737 textTransformDelta(tx, ty, &dx, &dy);
2742 void GfxState::shift(double dx, double dy) {
2747 GfxState *GfxState::save() {
2751 newState->saved = this;
2755 GfxState *GfxState::restore() {
2761 // these attributes aren't saved/restored by the q/Q operators
2762 oldState->path = path;
2763 oldState->curX = curX;
2764 oldState->curY = curY;
2765 oldState->lineX = lineX;
2766 oldState->lineY = lineY;