1 //---------------------------------------------------------------------------------
3 // Little Color Management System
4 // Copyright (c) 1998-2010 Marti Maria Saguer
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the Software
11 // is furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
18 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 //---------------------------------------------------------------------------------
26 // This program does apply profiles to (some) TIFF files
28 #include "lcms2_plugin.h"
35 static cmsBool BlackWhiteCompensation = FALSE;
36 static cmsBool IgnoreEmbedded = FALSE;
37 static cmsBool EmbedProfile = FALSE;
39 static cmsBool GamutCheck = FALSE;
40 static cmsBool lIsDeviceLink = FALSE;
41 static cmsBool StoreAsAlpha = FALSE;
42 static cmsBool InputLabUsingICC = FALSE;
44 static int Intent = INTENT_PERCEPTUAL;
45 static int ProofingIntent = INTENT_PERCEPTUAL;
46 static int PrecalcMode = 1;
47 static cmsFloat64Number InkLimit = 400;
49 static cmsFloat64Number ObserverAdaptationState = 1.0; // According ICC 4.3 this is the default
51 static const char *cInpProf = NULL;
52 static const char *cOutProf = NULL;
53 static const char *cProofing = NULL;
55 static const char* SaveEmbedded = NULL;
57 // Console error & warning
59 void ConsoleWarningHandler(const char* module, const char* fmt, va_list ap)
61 char e[512] = { '\0' };
63 strcat(strcpy(e, module), ": ");
65 vsprintf(e+strlen(e), fmt, ap);
69 fprintf(stderr, "\nWarning");
70 fprintf(stderr, " %s\n", e);
76 void ConsoleErrorHandler(const char* module, const char* fmt, va_list ap)
78 char e[512] = { '\0' };
81 if (strlen(module) < 500)
82 strcat(strcpy(e, module), ": ");
85 vsprintf(e+strlen(e), fmt, ap);
87 fprintf(stderr, "\nError");
88 fprintf(stderr, " %s\n", e);
95 void Warning(const char *frm, ...)
100 ConsoleWarningHandler("[tificc]", frm, args);
106 // Out of mememory is a fatal error
108 void OutOfMem(cmsUInt32Number size)
110 FatalError("Out of memory on allocating %d bytes.", size);
114 // -----------------------------------------------------------------------------------------------
116 // In TIFF, Lab is encoded in a different way, so let's use the plug-in
117 // capabilities of lcms2 to change the meaning of TYPE_Lab_8.
119 // * 0xffff / 0xff00 = (255 * 257) / (255 * 256) = 257 / 256
120 static int FromLabV2ToLabV4(int x)
124 a = ((x << 8) | x) >> 8; // * 257 / 256
125 if ( a > 0xffff) return 0xffff;
129 // * 0xf00 / 0xffff = * 256 / 257
130 static int FromLabV4ToLabV2(int x)
132 return ((x << 8) + 0x80) / 257;
136 // Formatter for 8bit Lab TIFF (photometric 8)
138 unsigned char* UnrollTIFFLab8(struct _cmstransform_struct* CMMcargo,
139 register cmsUInt16Number wIn[],
140 register cmsUInt8Number* accum,
141 register cmsUInt32Number Stride)
143 wIn[0] = (cmsUInt16Number) FromLabV2ToLabV4((accum[0]) << 8);
144 wIn[1] = (cmsUInt16Number) FromLabV2ToLabV4(((accum[1] > 127) ? (accum[1] - 128) : (accum[1] + 128)) << 8);
145 wIn[2] = (cmsUInt16Number) FromLabV2ToLabV4(((accum[2] > 127) ? (accum[2] - 128) : (accum[2] + 128)) << 8);
149 UTILS_UNUSED_PARAMETER(Stride);
150 UTILS_UNUSED_PARAMETER(CMMcargo);
154 unsigned char* PackTIFFLab8(struct _cmstransform_struct* CMMcargo,
155 register cmsUInt16Number wOut[],
156 register cmsUInt8Number* output,
157 register cmsUInt32Number Stride)
161 *output++ = (cmsUInt8Number) (FromLabV4ToLabV2(wOut[0] + 0x0080) >> 8);
163 a = (FromLabV4ToLabV2(wOut[1]) + 0x0080) >> 8;
164 b = (FromLabV4ToLabV2(wOut[2]) + 0x0080) >> 8;
166 *output++ = (cmsUInt8Number) ((a < 128) ? (a + 128) : (a - 128));
167 *output++ = (cmsUInt8Number) ((b < 128) ? (b + 128) : (b - 128));
171 UTILS_UNUSED_PARAMETER(Stride);
172 UTILS_UNUSED_PARAMETER(CMMcargo);
177 cmsFormatter TiffFormatterFactory(cmsUInt32Number Type,
178 cmsFormatterDirection Dir,
179 cmsUInt32Number dwFlags)
181 cmsFormatter Result = { NULL };
183 if (Type == TYPE_Lab_8 && !(dwFlags & CMS_PACK_FLAGS_FLOAT)) {
185 if (Dir == cmsFormatterInput)
186 Result.Fmt16 = UnrollTIFFLab8;
188 Result.Fmt16 = PackTIFFLab8;
194 static cmsPluginFormatters TiffLabPlugin = { {cmsPluginMagicNumber, 2000, cmsPluginFormattersSig, NULL}, TiffFormatterFactory };
197 // Build up the pixeltype descriptor
199 cmsUInt32Number GetInputPixelType(TIFF *Bank)
201 uint16 Photometric, bps, spp, extra, PlanarConfig, *info;
202 uint16 Compression, reverse = 0;
203 int ColorChannels, IsPlanar = 0, pt = 0, IsFlt;
205 TIFFGetField(Bank, TIFFTAG_PHOTOMETRIC, &Photometric);
206 TIFFGetFieldDefaulted(Bank, TIFFTAG_BITSPERSAMPLE, &bps);
209 FatalError("Sorry, bilevel TIFFs has nothig to do with ICC profiles");
211 if (bps != 8 && bps != 16 && bps != 32)
212 FatalError("Sorry, 8, 16 or 32 bits per sample only");
214 TIFFGetFieldDefaulted(Bank, TIFFTAG_SAMPLESPERPIXEL, &spp);
215 TIFFGetFieldDefaulted(Bank, TIFFTAG_PLANARCONFIG, &PlanarConfig);
217 switch (PlanarConfig) {
219 case PLANARCONFIG_CONTIG: IsPlanar = 0; break;
220 case PLANARCONFIG_SEPARATE: IsPlanar = 1; break;
223 FatalError("Unsupported planar configuration (=%d) ", (int) PlanarConfig);
226 // If Samples per pixel == 1, PlanarConfiguration is irrelevant and need
227 // not to be included.
229 if (spp == 1) IsPlanar = 0;
233 TIFFGetFieldDefaulted(Bank, TIFFTAG_EXTRASAMPLES, &extra, &info);
235 // Read alpha channels as colorant
243 ColorChannels = spp - extra;
245 switch (Photometric) {
247 case PHOTOMETRIC_MINISWHITE:
251 // ... fall through ...
253 case PHOTOMETRIC_MINISBLACK:
257 case PHOTOMETRIC_RGB:
262 case PHOTOMETRIC_PALETTE:
263 FatalError("Sorry, palette images not supported");
266 case PHOTOMETRIC_SEPARATED:
268 pt = PixelTypeFromChanCount(ColorChannels);
271 case PHOTOMETRIC_YCBCR:
272 TIFFGetField(Bank, TIFFTAG_COMPRESSION, &Compression);
277 TIFFGetFieldDefaulted(Bank, TIFFTAG_YCBCRSUBSAMPLING, &subx, &suby);
278 if (subx != 1 || suby != 1)
279 FatalError("Sorry, subsampled images not supported");
284 case PHOTOMETRIC_ICCLAB:
286 InputLabUsingICC = TRUE;
289 case PHOTOMETRIC_CIELAB:
291 InputLabUsingICC = FALSE;
295 case PHOTOMETRIC_LOGLUV: // CIE Log2(L) (u',v')
297 TIFFSetField(Bank, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_16BIT);
298 pt = PT_YUV; // *ICCSpace = icSigLuvData;
299 bps = 16; // 16 bits forced by LibTiff
303 FatalError("Unsupported TIFF color space (Photometric %d)", Photometric);
306 // Convert bits per sample to bytes per sample
309 IsFlt = (bps == 0) || (bps == 4);
311 return (FLOAT_SH(IsFlt)|COLORSPACE_SH(pt)|PLANAR_SH(IsPlanar)|EXTRA_SH(extra)|CHANNELS_SH(ColorChannels)|BYTES_SH(bps)|FLAVOR_SH(reverse));
316 // Rearrange pixel type to build output descriptor
318 cmsUInt32Number ComputeOutputFormatDescriptor(cmsUInt32Number dwInput, int OutColorSpace, int bps)
320 int IsPlanar = T_PLANAR(dwInput);
321 int Channels = ChanCountFromPixelType(OutColorSpace);
322 int IsFlt = (bps == 0) || (bps == 4);
324 return (FLOAT_SH(IsFlt)|COLORSPACE_SH(OutColorSpace)|PLANAR_SH(IsPlanar)|CHANNELS_SH(Channels)|BYTES_SH(bps));
329 // Tile based transforms
331 int TileBasedXform(cmsHTRANSFORM hXForm, TIFF* in, TIFF* out, int nPlanes)
333 tsize_t BufSizeIn = TIFFTileSize(in);
334 tsize_t BufSizeOut = TIFFTileSize(out);
335 unsigned char *BufferIn, *BufferOut;
336 ttile_t i, TileCount = TIFFNumberOfTiles(in) / nPlanes;
341 TIFFGetFieldDefaulted(in, TIFFTAG_TILEWIDTH, &tw);
342 TIFFGetFieldDefaulted(in, TIFFTAG_TILELENGTH, &tl);
344 PixelCount = (int) tw * tl;
346 BufferIn = (unsigned char *) _TIFFmalloc(BufSizeIn * nPlanes);
347 if (!BufferIn) OutOfMem(BufSizeIn * nPlanes);
349 BufferOut = (unsigned char *) _TIFFmalloc(BufSizeOut * nPlanes);
350 if (!BufferOut) OutOfMem(BufSizeOut * nPlanes);
353 for (i = 0; i < TileCount; i++) {
355 for (j=0; j < nPlanes; j++) {
357 if (TIFFReadEncodedTile(in, i + (j* TileCount),
358 BufferIn + (j*BufSizeIn), BufSizeIn) < 0) goto cleanup;
361 cmsDoTransform(hXForm, BufferIn, BufferOut, PixelCount);
363 for (j=0; j < nPlanes; j++) {
365 if (TIFFWriteEncodedTile(out, i + (j*TileCount),
366 BufferOut + (j*BufSizeOut), BufSizeOut) < 0) goto cleanup;
372 _TIFFfree(BufferOut);
379 _TIFFfree(BufferOut);
384 // Strip based transforms
387 int StripBasedXform(cmsHTRANSFORM hXForm, TIFF* in, TIFF* out, int nPlanes)
389 tsize_t BufSizeIn = TIFFStripSize(in);
390 tsize_t BufSizeOut = TIFFStripSize(out);
391 unsigned char *BufferIn, *BufferOut;
392 ttile_t i, StripCount = TIFFNumberOfStrips(in) / nPlanes;
399 TIFFGetFieldDefaulted(in, TIFFTAG_IMAGEWIDTH, &sw);
400 TIFFGetFieldDefaulted(in, TIFFTAG_ROWSPERSTRIP, &sl);
401 TIFFGetFieldDefaulted(in, TIFFTAG_IMAGELENGTH, &iml);
403 // It is possible to get infinite rows per strip
404 if (sl == 0 || sl > iml)
405 sl = iml; // One strip for whole image
407 BufferIn = (unsigned char *) _TIFFmalloc(BufSizeIn * nPlanes);
408 if (!BufferIn) OutOfMem(BufSizeIn * nPlanes);
410 BufferOut = (unsigned char *) _TIFFmalloc(BufSizeOut * nPlanes);
411 if (!BufferOut) OutOfMem(BufSizeOut * nPlanes);
414 for (i = 0; i < StripCount; i++) {
416 for (j=0; j < nPlanes; j++) {
418 if (TIFFReadEncodedStrip(in, i + (j * StripCount),
419 BufferIn + (j * BufSizeIn), BufSizeIn) < 0) goto cleanup;
422 PixelCount = (int) sw * (iml < sl ? iml : sl);
425 cmsDoTransform(hXForm, BufferIn, BufferOut, PixelCount);
427 for (j=0; j < nPlanes; j++) {
428 if (TIFFWriteEncodedStrip(out, i + (j * StripCount),
429 BufferOut + j * BufSizeOut, BufSizeOut) < 0) goto cleanup;
435 _TIFFfree(BufferOut);
441 _TIFFfree(BufferOut);
446 // Creates minimum required tags
448 void WriteOutputTags(TIFF *out, int Colorspace, int BytesPerSample)
450 int BitsPerSample = (8 * BytesPerSample);
451 int nChannels = ChanCountFromPixelType(Colorspace);
453 uint16 Extra[] = { EXTRASAMPLE_UNASSALPHA,
454 EXTRASAMPLE_UNASSALPHA,
455 EXTRASAMPLE_UNASSALPHA,
456 EXTRASAMPLE_UNASSALPHA,
457 EXTRASAMPLE_UNASSALPHA,
458 EXTRASAMPLE_UNASSALPHA,
459 EXTRASAMPLE_UNASSALPHA,
460 EXTRASAMPLE_UNASSALPHA,
461 EXTRASAMPLE_UNASSALPHA,
462 EXTRASAMPLE_UNASSALPHA,
463 EXTRASAMPLE_UNASSALPHA
467 switch (Colorspace) {
470 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
471 TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 1);
472 TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, BitsPerSample);
476 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
477 TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3);
478 TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, BitsPerSample);
482 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED);
483 TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3);
484 TIFFSetField(out, TIFFTAG_INKSET, 2);
485 TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, BitsPerSample);
489 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED);
490 TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 4);
491 TIFFSetField(out, TIFFTAG_INKSET, INKSET_CMYK);
492 TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, BitsPerSample);
496 if (BitsPerSample == 16)
497 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, 9);
499 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CIELAB);
500 TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3);
501 TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, BitsPerSample); // Needed by TIFF Spec
505 // Multi-ink separations
521 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED);
522 TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, nChannels);
524 if (StoreAsAlpha && nChannels >= 4) {
525 // CMYK plus extra alpha
526 TIFFSetField(out, TIFFTAG_EXTRASAMPLES, nChannels - 4, Extra);
527 TIFFSetField(out, TIFFTAG_INKSET, 1);
528 TIFFSetField(out, TIFFTAG_NUMBEROFINKS, 4);
531 TIFFSetField(out, TIFFTAG_INKSET, 2);
532 TIFFSetField(out, TIFFTAG_NUMBEROFINKS, nChannels);
535 TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, BitsPerSample);
540 FatalError("Unsupported output colorspace");
544 TIFFSetField(out, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
548 // Copies a bunch of tages
551 void CopyOtherTags(TIFF* in, TIFF* out)
553 #define CopyField(tag, v) \
554 if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
559 cmsFloat32Number floatv;
563 CopyField(TIFFTAG_SUBFILETYPE, longv);
565 TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &ow);
566 TIFFGetField(in, TIFFTAG_IMAGELENGTH, &ol);
568 TIFFSetField(out, TIFFTAG_IMAGEWIDTH, ow);
569 TIFFSetField(out, TIFFTAG_IMAGELENGTH, ol);
571 CopyField(TIFFTAG_PLANARCONFIG, shortv);
572 CopyField(TIFFTAG_COMPRESSION, shortv);
575 CopyField(TIFFTAG_PREDICTOR, shortv);
577 CopyField(TIFFTAG_THRESHHOLDING, shortv);
578 CopyField(TIFFTAG_FILLORDER, shortv);
579 CopyField(TIFFTAG_ORIENTATION, shortv);
580 CopyField(TIFFTAG_MINSAMPLEVALUE, shortv);
581 CopyField(TIFFTAG_MAXSAMPLEVALUE, shortv);
582 CopyField(TIFFTAG_XRESOLUTION, floatv);
583 CopyField(TIFFTAG_YRESOLUTION, floatv);
584 CopyField(TIFFTAG_RESOLUTIONUNIT, shortv);
585 CopyField(TIFFTAG_ROWSPERSTRIP, longv);
586 CopyField(TIFFTAG_XPOSITION, floatv);
587 CopyField(TIFFTAG_YPOSITION, floatv);
588 CopyField(TIFFTAG_IMAGEDEPTH, longv);
589 CopyField(TIFFTAG_TILEDEPTH, longv);
591 CopyField(TIFFTAG_TILEWIDTH, longv);
592 CopyField(TIFFTAG_TILELENGTH, longv);
594 CopyField(TIFFTAG_ARTIST, stringv);
595 CopyField(TIFFTAG_IMAGEDESCRIPTION, stringv);
596 CopyField(TIFFTAG_MAKE, stringv);
597 CopyField(TIFFTAG_MODEL, stringv);
599 CopyField(TIFFTAG_DATETIME, stringv);
600 CopyField(TIFFTAG_HOSTCOMPUTER, stringv);
601 CopyField(TIFFTAG_PAGENAME, stringv);
602 CopyField(TIFFTAG_DOCUMENTNAME, stringv);
606 // A replacement for (the nonstandard) filelenght
610 void DoEmbedProfile(TIFF* Out, const char* ProfileFile)
613 cmsUInt32Number size, EmbedLen;
614 cmsUInt8Number* EmbedBuffer;
616 f = fopen(ProfileFile, "rb");
617 if (f == NULL) return;
619 size = cmsfilelength(f);
620 EmbedBuffer = (cmsUInt8Number*) malloc(size + 1);
621 if (EmbedBuffer == NULL) {
626 EmbedLen = fread(EmbedBuffer, 1, size, f);
628 if (EmbedLen != size)
629 FatalError("Cannot read %ld bytes to %s", size, ProfileFile);
632 EmbedBuffer[EmbedLen] = 0;
634 TIFFSetField(Out, TIFFTAG_ICCPROFILE, EmbedLen, EmbedBuffer);
641 cmsHPROFILE GetTIFFProfile(TIFF* in)
643 cmsCIExyYTRIPLE Primaries;
644 cmsFloat32Number* chr;
645 cmsCIExyY WhitePoint;
646 cmsFloat32Number* wp;
648 cmsToneCurve* Curve[3];
649 cmsUInt16Number *gmr, *gmg, *gmb;
650 cmsHPROFILE hProfile;
651 cmsUInt32Number EmbedLen;
652 cmsUInt8Number* EmbedBuffer;
654 if (IgnoreEmbedded) return NULL;
656 if (TIFFGetField(in, TIFFTAG_ICCPROFILE, &EmbedLen, &EmbedBuffer)) {
658 hProfile = cmsOpenProfileFromMem(EmbedBuffer, EmbedLen);
660 // Print description found in the profile
663 fprintf(stdout, "\n[Embedded profile]\n");
664 PrintProfileInformation(hProfile);
668 if (hProfile != NULL && SaveEmbedded != NULL)
669 SaveMemoryBlock(EmbedBuffer, EmbedLen, SaveEmbedded);
671 if (hProfile) return hProfile;
674 // Try to see if "colorimetric" tiff
676 if (TIFFGetField(in, TIFFTAG_PRIMARYCHROMATICITIES, &chr)) {
678 Primaries.Red.x = chr[0];
679 Primaries.Red.y = chr[1];
680 Primaries.Green.x = chr[2];
681 Primaries.Green.y = chr[3];
682 Primaries.Blue.x = chr[4];
683 Primaries.Blue.y = chr[5];
685 Primaries.Red.Y = Primaries.Green.Y = Primaries.Blue.Y = 1.0;
687 if (TIFFGetField(in, TIFFTAG_WHITEPOINT, &wp)) {
689 WhitePoint.x = wp[0];
690 WhitePoint.y = wp[1];
693 // Transferfunction is a bit harder....
695 TIFFGetFieldDefaulted(in, TIFFTAG_TRANSFERFUNCTION,
700 Curve[0] = cmsBuildTabulatedToneCurve16(NULL, 256, gmr);
701 Curve[1] = cmsBuildTabulatedToneCurve16(NULL, 256, gmg);
702 Curve[2] = cmsBuildTabulatedToneCurve16(NULL, 256, gmb);
704 hProfile = cmsCreateRGBProfileTHR(NULL, &WhitePoint, &Primaries, Curve);
706 for (i=0; i < 3; i++)
707 cmsFreeToneCurve(Curve[i]);
710 fprintf(stdout, "\n[Colorimetric TIFF]\n");
722 // Transform one image
724 int TransformImage(TIFF* in, TIFF* out, const char *cDefInpProf)
726 cmsHPROFILE hIn, hOut, hProof, hInkLimit = NULL;
728 cmsUInt32Number wInput, wOutput;
729 int OutputColorSpace;
731 cmsUInt32Number dwFlags = 0;
734 // Observer adaptation state (only meaningful on absolute colorimetric intent)
736 cmsSetAdaptationState(ObserverAdaptationState);
738 if (EmbedProfile && cOutProf)
739 DoEmbedProfile(out, cOutProf);
741 if (BlackWhiteCompensation)
742 dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
745 switch (PrecalcMode) {
747 case 0: dwFlags |= cmsFLAGS_NOOPTIMIZE; break;
748 case 2: dwFlags |= cmsFLAGS_HIGHRESPRECALC; break;
749 case 3: dwFlags |= cmsFLAGS_LOWRESPRECALC; break;
752 default: FatalError("Unknown precalculation mode '%d'", PrecalcMode);
757 dwFlags |= cmsFLAGS_GAMUTCHECK;
764 hIn = cmsOpenProfileFromFile(cDefInpProf, "r");
768 hIn = GetTIFFProfile(in);
771 hIn = OpenStockProfile(NULL, cDefInpProf);
773 hOut = OpenStockProfile(NULL, cOutProf);
775 if (cProofing != NULL) {
777 hProof = OpenStockProfile(NULL, cProofing);
778 dwFlags |= cmsFLAGS_SOFTPROOFING;
782 // Take input color space
784 wInput = GetInputPixelType(in);
786 // Assure both, input profile and input TIFF are on same colorspace
788 if (_cmsLCMScolorSpace(cmsGetColorSpace(hIn)) != (int) T_COLORSPACE(wInput))
789 FatalError("Input profile is not operating in proper color space");
793 OutputColorSpace = _cmsLCMScolorSpace(cmsGetColorSpace(hOut));
795 OutputColorSpace = _cmsLCMScolorSpace(cmsGetPCS(hIn));
797 wOutput = ComputeOutputFormatDescriptor(wInput, OutputColorSpace, bps);
799 WriteOutputTags(out, OutputColorSpace, bps);
800 CopyOtherTags(in, out);
803 if (InkLimit != 400.0 &&
804 (OutputColorSpace == PT_CMYK || OutputColorSpace == PT_CMY)) {
806 cmsHPROFILE hProfiles[10];
810 hInkLimit = cmsCreateInkLimitingDeviceLink(cmsGetColorSpace(hOut), InkLimit);
812 hProfiles[nProfiles++] = hIn;
814 hProfiles[nProfiles++] = hProof;
815 hProfiles[nProfiles++] = hProof;
818 hProfiles[nProfiles++] = hOut;
819 hProfiles[nProfiles++] = hInkLimit;
821 xform = cmsCreateMultiprofileTransform(hProfiles, nProfiles,
822 wInput, wOutput, Intent, dwFlags);
827 xform = cmsCreateProofingTransform(hIn, wInput,
834 cmsCloseProfile(hIn);
835 cmsCloseProfile(hOut);
838 cmsCloseProfile(hInkLimit);
840 cmsCloseProfile(hProof);
842 if (xform == NULL) return 0;
845 if (T_PLANAR(wInput))
846 nPlanes = T_CHANNELS(wInput) + T_EXTRA(wInput);
851 // Handle tile by tile or strip by strip
852 if (TIFFIsTiled(in)) {
854 TileBasedXform(xform, in, out, nPlanes);
857 StripBasedXform(xform, in, out, nPlanes);
861 cmsDeleteTransform(xform);
863 TIFFWriteDirectory(out);
873 fprintf(stderr, "little cms ICC profile applier for TIFF - v6.1 [LittleCMS %2.2f]\n\n", LCMS_VERSION / 1000.0);
881 fprintf(stderr, "usage: tifficc [flags] input.tif output.tif\n");
883 fprintf(stderr, "\nflags:\n\n");
884 fprintf(stderr, "%cv - Verbose\n", SW);
885 fprintf(stderr, "%ci<profile> - Input profile (defaults to sRGB)\n", SW);
886 fprintf(stderr, "%co<profile> - Output profile (defaults to sRGB)\n", SW);
887 fprintf(stderr, "%cl<profile> - Transform by device-link profile\n", SW);
889 PrintRenderingIntents();
891 fprintf(stderr, "%cb - Black point compensation\n", SW);
892 fprintf(stderr, "%cd<0..1> - Observer adaptation state (abs.col. only)\n", SW);
894 fprintf(stderr, "%cc<0,1,2,3> - Precalculates transform (0=Off, 1=Normal, 2=Hi-res, 3=LoRes)\n", SW);
895 fprintf(stderr, "\n");
897 fprintf(stderr, "%cw<8,16,32> - Output depth. Use 32 for floating-point\n\n", SW);
898 fprintf(stderr, "%ca - Handle channels > 4 as alpha\n", SW);
900 fprintf(stderr, "%cn - Ignore embedded profile on input\n", SW);
901 fprintf(stderr, "%ce - Embed destination profile\n", SW);
902 fprintf(stderr, "%cs<new profile> - Save embedded profile as <new profile>\n", SW);
903 fprintf(stderr, "\n");
906 fprintf(stderr, "%cp<profile> - Soft proof profile\n", SW);
907 fprintf(stderr, "%cm<n> - Soft proof intent\n", SW);
908 fprintf(stderr, "%cg - Marks out-of-gamut colors on softproof\n", SW);
910 fprintf(stderr, "\n");
912 fprintf(stderr, "%ck<0..400> - Ink-limiting in %% (CMYK only)\n", SW);
913 fprintf(stderr, "\n");
914 fprintf(stderr, "%ch<0,1,2,3> - More help\n", SW);
919 fprintf(stderr, "Examples:\n\n"
920 "To color correct from scanner to sRGB:\n"
921 "\ttifficc %ciscanner.icm in.tif out.tif\n"
922 "To convert from monitor1 to monitor2:\n"
923 "\ttifficc %cimon1.icm %comon2.icm in.tif out.tif\n"
924 "To make a CMYK separation:\n"
925 "\ttifficc %coprinter.icm inrgb.tif outcmyk.tif\n"
926 "To recover sRGB from a CMYK separation:\n"
927 "\ttifficc %ciprinter.icm incmyk.tif outrgb.tif\n"
928 "To convert from CIELab TIFF to sRGB\n"
929 "\ttifficc %ci*Lab in.tif out.tif\n\n",
930 SW, SW, SW, SW, SW, SW);
939 fprintf(stderr, "This program is intended to be a demo of the little cms\n"
940 "engine. Both lcms and this program are freeware. You can\n"
941 "obtain both in source code at http://www.littlecms.com\n"
942 "For suggestions, comments, bug reports etc. send mail to\n"
943 "info@littlecms.com\n\n");
956 void HandleSwitches(int argc, char *argv[])
960 while ((s=xgetopt(argc,argv,"aAeEbBw:W:nNvVGgh:H:i:I:o:O:P:p:t:T:c:C:l:L:M:m:K:k:S:s:D:d:")) != EOF) {
970 BlackWhiteCompensation = TRUE;
975 PrecalcMode = atoi(xoptarg);
976 if (PrecalcMode < 0 || PrecalcMode > 3)
977 FatalError("Unknown precalc mode '%d'", PrecalcMode);
981 case 'D': ObserverAdaptationState = atof(xoptarg);
982 if (ObserverAdaptationState < 0 ||
983 ObserverAdaptationState > 1.0)
984 Warning("Adaptation state should be 0..1");
1005 FatalError("Device-link already specified");
1013 FatalError("Device-link already specified");
1020 if (cInpProf != NULL || cOutProf != NULL)
1021 FatalError("input/output profiles already specified");
1024 lIsDeviceLink = TRUE;
1029 cProofing = xoptarg;
1034 Intent = atoi(xoptarg);
1039 ProofingIntent = atoi(xoptarg);
1044 IgnoreEmbedded = TRUE;
1049 Width = atoi(xoptarg);
1050 if (Width != 8 && Width != 16 && Width != 32)
1051 FatalError("Only 8, 16 and 32 bps are supported");
1056 InkLimit = atof(xoptarg);
1057 if (InkLimit < 0.0 || InkLimit > 400.0)
1058 FatalError("Ink limit must be 0%%..400%%");
1063 case 'S': SaveEmbedded = xoptarg;
1069 int a = atoi(xoptarg);
1076 FatalError("Unknown option - run without args to see valid ones");
1085 int main(int argc, char* argv[])
1089 cmsPlugin(&TiffLabPlugin);
1091 InitUtils("tifficc");
1093 HandleSwitches(argc, argv);
1095 if ((argc - xoptind) != 2) {
1101 TIFFSetErrorHandler(ConsoleErrorHandler);
1102 TIFFSetWarningHandler(ConsoleWarningHandler);
1104 in = TIFFOpen(argv[xoptind], "r");
1105 if (in == NULL) FatalError("Unable to open '%s'", argv[xoptind]);
1107 out = TIFFOpen(argv[xoptind+1], "w");
1112 FatalError("Unable to write '%s'", argv[xoptind+1]);
1117 TransformImage(in, out, cInpProf);
1120 } while (TIFFReadDirectory(in));
1123 if (Verbose) { fprintf(stdout, "\n"); fflush(stdout); }