tificc.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. //---------------------------------------------------------------------------------
  2. //
  3. // Little Color Management System
  4. // Copyright (c) 1998-2023 Marti Maria Saguer
  5. //
  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:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  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.
  23. //
  24. //---------------------------------------------------------------------------------
  25. // This program does apply profiles to (some) TIFF files
  26. #include "lcms2_plugin.h"
  27. #include "tiffio.h"
  28. #include "utils.h"
  29. // Fix broken libtiff 4.3.0, thanks to Bob Friesenhahn for uncovering this
  30. #if defined(HAVE_STDINT_H) && (TIFFLIB_VERSION >= 20201219)
  31. #include <stdint.h>
  32. # undef cmsUInt16Number
  33. # define cmsUInt16Number uint16_t
  34. # undef cmsUInt32Number
  35. # define cmsUInt32Number uint32_t
  36. #endif /* TIFFLIB_VERSION */
  37. // Flags
  38. static cmsBool BlackWhiteCompensation = FALSE;
  39. static cmsBool IgnoreEmbedded = FALSE;
  40. static cmsBool EmbedProfile = FALSE;
  41. static int PixelDepth = 8;
  42. static cmsBool GamutCheck = FALSE;
  43. static cmsBool lIsDeviceLink = FALSE;
  44. static cmsBool lIsCUBE = FALSE;
  45. static cmsBool StoreAsAlpha = FALSE;
  46. static int Intent = INTENT_PERCEPTUAL;
  47. static int ProofingIntent = INTENT_PERCEPTUAL;
  48. static int PrecalcMode = 1;
  49. static cmsFloat64Number InkLimit = 400;
  50. 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;
  54. static const char* SaveEmbedded = NULL;
  55. // Console error & warning
  56. static
  57. void ConsoleWarningHandler(const char* module, const char* fmt, va_list ap)
  58. {
  59. if (Verbose) {
  60. fprintf(stderr, "Warning: ");
  61. if (module != NULL)
  62. fprintf(stderr, "[%s] ", module);
  63. vfprintf(stderr, fmt, ap);
  64. fprintf(stderr, "\n");
  65. fflush(stderr);
  66. }
  67. }
  68. static
  69. void ConsoleErrorHandler(const char* module, const char* fmt, va_list ap)
  70. {
  71. if (Verbose) {
  72. fprintf(stderr, "Error: ");
  73. if (module != NULL)
  74. fprintf(stderr, "[%s] ", module);
  75. vfprintf(stderr, fmt, ap);
  76. fprintf(stderr, "\n");
  77. fflush(stderr);
  78. }
  79. }
  80. // Issue a warning
  81. static
  82. void Warning(const char *frm, ...)
  83. {
  84. va_list args;
  85. va_start(args, frm);
  86. ConsoleWarningHandler("tificc", frm, args);
  87. va_end(args);
  88. }
  89. // Out of memory is a fatal error
  90. static
  91. void OutOfMem(cmsUInt32Number size)
  92. {
  93. FatalError("Out of memory on allocating %d bytes.", size);
  94. }
  95. // -----------------------------------------------------------------------------------------------
  96. // Lab plug-in
  97. // In TIFF, Lab is encoded in a different way, so let's use the plug-in
  98. // capabilities of lcms2 to change the meaning of TYPE_Lab_8.
  99. #define LABTIFF_SH(m) ((m) << 30)
  100. #define T_LABTIFF(m) (((m)>>30)&1)
  101. // * 0xffff / 0xff00 = (255 * 257) / (255 * 256) = 257 / 256
  102. static
  103. int FromLabV2ToLabV4(int x)
  104. {
  105. int a;
  106. a = ((x << 8) | x) >> 8; // * 257 / 256
  107. if ( a > 0xffff) return 0xffff;
  108. return a;
  109. }
  110. // * 0xf00 / 0xffff = * 256 / 257
  111. static
  112. int FromLabV4ToLabV2(int x)
  113. {
  114. return ((x << 8) + 0x80) / 257;
  115. }
  116. // Formatter for 8bit Lab TIFF (photometric 8)
  117. static
  118. unsigned char* UnrollTIFFLab8(struct _cmstransform_struct* CMMcargo,
  119. CMSREGISTER cmsUInt16Number wIn[],
  120. CMSREGISTER cmsUInt8Number* accum,
  121. CMSREGISTER cmsUInt32Number Stride)
  122. {
  123. wIn[0] = (cmsUInt16Number) FromLabV2ToLabV4((accum[0]) << 8);
  124. wIn[1] = (cmsUInt16Number) FromLabV2ToLabV4(((accum[1] > 127) ? (accum[1] - 128) : (accum[1] + 128)) << 8);
  125. wIn[2] = (cmsUInt16Number) FromLabV2ToLabV4(((accum[2] > 127) ? (accum[2] - 128) : (accum[2] + 128)) << 8);
  126. return accum + 3;
  127. UTILS_UNUSED_PARAMETER(Stride);
  128. UTILS_UNUSED_PARAMETER(CMMcargo);
  129. }
  130. // Formatter for 16bit Lab TIFF (photometric 8)
  131. static
  132. unsigned char* UnrollTIFFLab16(struct _cmstransform_struct* CMMcargo,
  133. CMSREGISTER cmsUInt16Number wIn[],
  134. CMSREGISTER cmsUInt8Number* accum,
  135. CMSREGISTER cmsUInt32Number Stride )
  136. {
  137. cmsUInt16Number* accum16 = (cmsUInt16Number*) accum;
  138. wIn[0] = (cmsUInt16Number) FromLabV2ToLabV4(accum16[0]);
  139. wIn[1] = (cmsUInt16Number) FromLabV2ToLabV4(((accum16[1] > 0x7f00) ? (accum16[1] - 0x8000) : (accum16[1] + 0x8000)) );
  140. wIn[2] = (cmsUInt16Number) FromLabV2ToLabV4(((accum16[2] > 0x7f00) ? (accum16[2] - 0x8000) : (accum16[2] + 0x8000)) );
  141. return accum + 3 * sizeof(cmsUInt16Number);
  142. UTILS_UNUSED_PARAMETER(Stride);
  143. UTILS_UNUSED_PARAMETER(CMMcargo);
  144. }
  145. static
  146. unsigned char* PackTIFFLab8(struct _cmstransform_struct* CMMcargo,
  147. CMSREGISTER cmsUInt16Number wOut[],
  148. CMSREGISTER cmsUInt8Number* output,
  149. CMSREGISTER cmsUInt32Number Stride)
  150. {
  151. int a, b;
  152. *output++ = (cmsUInt8Number) (FromLabV4ToLabV2(wOut[0] + 0x0080) >> 8);
  153. a = (FromLabV4ToLabV2(wOut[1]) + 0x0080) >> 8;
  154. b = (FromLabV4ToLabV2(wOut[2]) + 0x0080) >> 8;
  155. *output++ = (cmsUInt8Number) ((a < 128) ? (a + 128) : (a - 128));
  156. *output++ = (cmsUInt8Number) ((b < 128) ? (b + 128) : (b - 128));
  157. return output;
  158. UTILS_UNUSED_PARAMETER(Stride);
  159. UTILS_UNUSED_PARAMETER(CMMcargo);
  160. }
  161. static
  162. unsigned char* PackTIFFLabA8(struct _cmstransform_struct* CMMcargo,
  163. CMSREGISTER cmsUInt16Number wOut[],
  164. CMSREGISTER cmsUInt8Number* output,
  165. CMSREGISTER cmsUInt32Number Stride)
  166. {
  167. int a, b;
  168. *output++ = (cmsUInt8Number) (FromLabV4ToLabV2(wOut[0] + 0x0080) >> 8);
  169. a = (FromLabV4ToLabV2(wOut[1]) + 0x0080) >> 8;
  170. b = (FromLabV4ToLabV2(wOut[2]) + 0x0080) >> 8;
  171. *output++ = (cmsUInt8Number) ((a < 128) ? (a + 128) : (a - 128));
  172. *output++ = (cmsUInt8Number) ((b < 128) ? (b + 128) : (b - 128));
  173. output++; // Alpha
  174. return output;
  175. UTILS_UNUSED_PARAMETER(Stride);
  176. UTILS_UNUSED_PARAMETER(CMMcargo);
  177. }
  178. static
  179. unsigned char* PackTIFFLab16(struct _cmstransform_struct* CMMcargo,
  180. CMSREGISTER cmsUInt16Number wOut[],
  181. CMSREGISTER cmsUInt8Number* output,
  182. CMSREGISTER cmsUInt32Number Stride)
  183. {
  184. int a, b;
  185. cmsUInt16Number* output16 = (cmsUInt16Number*) output;
  186. *output16++ = (cmsUInt16Number) FromLabV4ToLabV2(wOut[0]);
  187. a = FromLabV4ToLabV2(wOut[1]);
  188. b = FromLabV4ToLabV2(wOut[2]);
  189. *output16++ = (cmsUInt16Number) ((a < 0x7f00) ? (a + 0x8000) : (a - 0x8000));
  190. *output16++ = (cmsUInt16Number) ((b < 0x7f00) ? (b + 0x8000) : (b - 0x8000));
  191. return (cmsUInt8Number*) output16;
  192. UTILS_UNUSED_PARAMETER(Stride);
  193. UTILS_UNUSED_PARAMETER(CMMcargo);
  194. }
  195. static
  196. unsigned char* PackTIFFLabA16(struct _cmstransform_struct* CMMcargo,
  197. CMSREGISTER cmsUInt16Number wOut[],
  198. CMSREGISTER cmsUInt8Number* output,
  199. CMSREGISTER cmsUInt32Number Stride)
  200. {
  201. int a, b;
  202. cmsUInt16Number* output16 = (cmsUInt16Number*) output;
  203. *output16++ = (cmsUInt16Number) FromLabV4ToLabV2(wOut[0]);
  204. a = FromLabV4ToLabV2(wOut[1]);
  205. b = FromLabV4ToLabV2(wOut[2]);
  206. *output16++ = (cmsUInt16Number) ((a < 0x7f00) ? (a + 0x8000) : (a - 0x8000));
  207. *output16++ = (cmsUInt16Number) ((b < 0x7f00) ? (b + 0x8000) : (b - 0x8000));
  208. output16++; // Alpha
  209. return (cmsUInt8Number*) output16;
  210. UTILS_UNUSED_PARAMETER(Stride);
  211. UTILS_UNUSED_PARAMETER(CMMcargo);
  212. }
  213. static
  214. cmsFormatter TiffFormatterFactory(cmsUInt32Number Type,
  215. cmsFormatterDirection Dir,
  216. cmsUInt32Number dwFlags)
  217. {
  218. cmsFormatter Result = { NULL };
  219. int bps = T_BYTES(Type);
  220. int IsTiffSpecial = T_LABTIFF(Type);
  221. if (IsTiffSpecial && !(dwFlags & CMS_PACK_FLAGS_FLOAT))
  222. {
  223. if (Dir == cmsFormatterInput)
  224. {
  225. Result.Fmt16 = (bps == 1) ? UnrollTIFFLab8 : UnrollTIFFLab16;
  226. }
  227. else
  228. {
  229. if (T_EXTRA(Type) == 1)
  230. Result.Fmt16 = (bps == 1) ? PackTIFFLabA8 : PackTIFFLabA16;
  231. else
  232. if (T_EXTRA(Type) == 0)
  233. Result.Fmt16 = (bps == 1) ? PackTIFFLab8 : PackTIFFLab16;
  234. }
  235. }
  236. return Result;
  237. }
  238. static cmsPluginFormatters TiffLabPlugin = { {cmsPluginMagicNumber, 2000, cmsPluginFormattersSig, NULL}, TiffFormatterFactory };
  239. // -----------------------------------------------------------------------------------------------
  240. // Build up the pixeltype descriptor
  241. static
  242. cmsUInt32Number GetInputPixelType(TIFF *Bank)
  243. {
  244. cmsUInt16Number Photometric, bps, spp, extra, PlanarConfig, *info;
  245. cmsUInt16Number Compression;
  246. int ColorChannels;
  247. int IsPremul = FALSE, IsPlanar = FALSE, IsFlt = FALSE, IsReverse = FALSE;
  248. int labTiffSpecial = FALSE;
  249. int pt = PT_ANY;
  250. TIFFGetFieldDefaulted(Bank, TIFFTAG_BITSPERSAMPLE, &bps);
  251. if (bps == 1)
  252. FatalError("Sorry, bilevel TIFFs has nothing to do with ICC profiles");
  253. if (bps != 8 && bps != 16 && bps != 32)
  254. FatalError("Sorry, 8, 16 or 32 bits per sample only");
  255. TIFFGetFieldDefaulted(Bank, TIFFTAG_PLANARCONFIG, &PlanarConfig);
  256. switch (PlanarConfig) {
  257. case PLANARCONFIG_CONTIG: IsPlanar = 0; break;
  258. case PLANARCONFIG_SEPARATE: IsPlanar = 1; break;
  259. default:
  260. FatalError("Unsupported planar configuration (=%d) ", (int) PlanarConfig);
  261. }
  262. TIFFGetFieldDefaulted(Bank, TIFFTAG_SAMPLESPERPIXEL, &spp);
  263. // If Samples per pixel == 1, PlanarConfiguration is irrelevant and need not to be included.
  264. if (spp == 1) IsPlanar = 0;
  265. // Any alpha?
  266. TIFFGetFieldDefaulted(Bank, TIFFTAG_EXTRASAMPLES, &extra, &info);
  267. // Read alpha channels as colorant?
  268. if (StoreAsAlpha) {
  269. ColorChannels = spp;
  270. extra = 0;
  271. }
  272. else
  273. ColorChannels = spp - extra;
  274. // Is alpha premultiplied ?
  275. IsPremul = ((extra == 1) && (info[0] == EXTRASAMPLE_ASSOCALPHA));
  276. // Get photometric interpretation and proceed accordly
  277. TIFFGetField(Bank, TIFFTAG_PHOTOMETRIC, &Photometric);
  278. switch (Photometric) {
  279. case PHOTOMETRIC_MINISWHITE:
  280. IsReverse = 1;
  281. // ... fall through ...
  282. case PHOTOMETRIC_MINISBLACK:
  283. pt = PT_GRAY;
  284. break;
  285. case PHOTOMETRIC_RGB:
  286. pt = PT_RGB;
  287. if (ColorChannels < 3)
  288. FatalError("Sorry, RGB needs at least 3 samples per pixel");
  289. break;
  290. case PHOTOMETRIC_PALETTE:
  291. FatalError("Sorry, palette images not supported");
  292. break;
  293. case PHOTOMETRIC_SEPARATED:
  294. pt = PixelTypeFromChanCount(ColorChannels);
  295. break;
  296. case PHOTOMETRIC_YCBCR:
  297. TIFFGetField(Bank, TIFFTAG_COMPRESSION, &Compression);
  298. {
  299. cmsUInt16Number subx, suby;
  300. pt = PT_YCbCr;
  301. TIFFGetFieldDefaulted(Bank, TIFFTAG_YCBCRSUBSAMPLING, &subx, &suby);
  302. if (subx != 1 || suby != 1)
  303. FatalError("Sorry, subsampled images are not supported");
  304. }
  305. break;
  306. // Two Lab flavours
  307. case PHOTOMETRIC_ICCLAB:
  308. pt = PT_Lab;
  309. break;
  310. case PHOTOMETRIC_CIELAB:
  311. pt = PT_Lab;
  312. labTiffSpecial = TRUE;
  313. break;
  314. // CIE Log2(L) (u',v')
  315. case PHOTOMETRIC_LOGLUV:
  316. TIFFSetField(Bank, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_16BIT);
  317. pt = PT_YUV; // *ICCSpace = icSigLuvData;
  318. bps = 16; // 16 bits forced by LibTiff
  319. break;
  320. default:
  321. FatalError("Unsupported TIFF color space (Photometric %d)", Photometric);
  322. }
  323. // Convert bits per sample to bytes per sample
  324. bps >>= 3;
  325. IsFlt = (bps == 0) || (bps == 4);
  326. return (FLOAT_SH(IsFlt) | COLORSPACE_SH(pt) | PLANAR_SH(IsPlanar) | EXTRA_SH(extra) | PREMUL_SH(IsPremul) |
  327. CHANNELS_SH(ColorChannels) | BYTES_SH(bps) | FLAVOR_SH(IsReverse) | LABTIFF_SH(labTiffSpecial));
  328. }
  329. // Rearrange pixel type to build output descriptor
  330. static
  331. cmsUInt32Number ComputeOutputFormatDescriptor(cmsUInt32Number dwInput, int OutColorSpace, int bps)
  332. {
  333. int IsPlanar = T_PLANAR(dwInput);
  334. int Channels = ChanCountFromPixelType(OutColorSpace);
  335. int IsFlt = (bps == 0) || (bps == 4);
  336. int labTiffSpecial = FALSE;
  337. int Extra = T_EXTRA(dwInput);
  338. int IsPremul = T_PREMUL(dwInput);
  339. if (OutColorSpace == PT_Lab)
  340. labTiffSpecial = TRUE;
  341. return (FLOAT_SH(IsFlt) | COLORSPACE_SH(OutColorSpace) | PLANAR_SH(IsPlanar) |
  342. CHANNELS_SH(Channels) | BYTES_SH(bps) | EXTRA_SH(Extra) | PREMUL_SH(IsPremul) |
  343. LABTIFF_SH(labTiffSpecial));
  344. }
  345. // Tile based transforms
  346. static
  347. int TileBasedXform(cmsHTRANSFORM hXForm, TIFF* in, TIFF* out, int nPlanes)
  348. {
  349. tsize_t BufSizeIn = TIFFTileSize(in);
  350. tsize_t BufSizeOut = TIFFTileSize(out);
  351. unsigned char *BufferIn, *BufferOut;
  352. ttile_t i, TileCount = TIFFNumberOfTiles(in) / nPlanes;
  353. cmsUInt32Number tw, tl;
  354. int PixelCount, j;
  355. // Check for bad tiffs
  356. if (BufSizeIn > INT_MAX || BufSizeOut > INT_MAX)
  357. FatalError("Probably corrupted TIFF, tile too big.");
  358. TIFFGetFieldDefaulted(in, TIFFTAG_TILEWIDTH, &tw);
  359. TIFFGetFieldDefaulted(in, TIFFTAG_TILELENGTH, &tl);
  360. PixelCount = (int) tw * tl;
  361. BufferIn = (unsigned char *) _TIFFmalloc(BufSizeIn * nPlanes);
  362. if (!BufferIn) OutOfMem((cmsUInt32Number) BufSizeIn * nPlanes);
  363. BufferOut = (unsigned char *) _TIFFmalloc(BufSizeOut * nPlanes);
  364. if (!BufferOut) OutOfMem((cmsUInt32Number) BufSizeOut * nPlanes);
  365. for (i = 0; i < TileCount; i++) {
  366. for (j=0; j < nPlanes; j++) {
  367. if (TIFFReadEncodedTile(in, i + (j* TileCount),
  368. BufferIn + (j*BufSizeIn), BufSizeIn) < 0) goto cleanup;
  369. }
  370. if (PixelCount < 0)
  371. FatalError("TIFF is corrupted");
  372. cmsDoTransform(hXForm, BufferIn, BufferOut, PixelCount);
  373. for (j=0; j < nPlanes; j++) {
  374. if (TIFFWriteEncodedTile(out, i + (j*TileCount),
  375. BufferOut + (j*BufSizeOut), BufSizeOut) < 0) goto cleanup;
  376. }
  377. }
  378. _TIFFfree(BufferIn);
  379. _TIFFfree(BufferOut);
  380. return 1;
  381. cleanup:
  382. _TIFFfree(BufferIn);
  383. _TIFFfree(BufferOut);
  384. return 0;
  385. }
  386. // Strip based transforms
  387. static
  388. int StripBasedXform(cmsHTRANSFORM hXForm, TIFF* in, TIFF* out, int nPlanes)
  389. {
  390. tsize_t BufSizeIn = TIFFStripSize(in);
  391. tsize_t BufSizeOut = TIFFStripSize(out);
  392. unsigned char *BufferIn, *BufferOut;
  393. ttile_t i, StripCount = TIFFNumberOfStrips(in) / nPlanes;
  394. cmsUInt32Number sw;
  395. cmsUInt32Number sl;
  396. cmsUInt32Number iml;
  397. int j;
  398. int PixelCount;
  399. // Check for bad tiffs
  400. if (BufSizeIn > INT_MAX || BufSizeOut > INT_MAX)
  401. FatalError("Probably corrupted TIFF, strip too big.");
  402. TIFFGetFieldDefaulted(in, TIFFTAG_IMAGEWIDTH, &sw);
  403. TIFFGetFieldDefaulted(in, TIFFTAG_ROWSPERSTRIP, &sl);
  404. TIFFGetFieldDefaulted(in, TIFFTAG_IMAGELENGTH, &iml);
  405. // It is possible to get infinite rows per strip
  406. if (sl == 0 || sl > iml)
  407. sl = iml; // One strip for whole image
  408. BufferIn = (unsigned char *) _TIFFmalloc(BufSizeIn * nPlanes);
  409. if (!BufferIn) OutOfMem((cmsUInt32Number) BufSizeIn * nPlanes);
  410. BufferOut = (unsigned char *) _TIFFmalloc(BufSizeOut * nPlanes);
  411. if (!BufferOut) OutOfMem((cmsUInt32Number) BufSizeOut * nPlanes);
  412. for (i = 0; i < StripCount; i++) {
  413. for (j=0; j < nPlanes; j++) {
  414. if (TIFFReadEncodedStrip(in, i + (j * StripCount),
  415. BufferIn + (j * BufSizeIn), BufSizeIn) < 0) goto cleanup;
  416. }
  417. PixelCount = (int) sw * (iml < sl ? iml : sl);
  418. iml -= sl;
  419. if (PixelCount < 0)
  420. FatalError("TIFF is corrupted");
  421. cmsDoTransform(hXForm, BufferIn, BufferOut, PixelCount);
  422. for (j=0; j < nPlanes; j++) {
  423. if (TIFFWriteEncodedStrip(out, i + (j * StripCount),
  424. BufferOut + j * BufSizeOut, BufSizeOut) < 0) goto cleanup;
  425. }
  426. }
  427. _TIFFfree(BufferIn);
  428. _TIFFfree(BufferOut);
  429. return 1;
  430. cleanup:
  431. _TIFFfree(BufferIn);
  432. _TIFFfree(BufferOut);
  433. return 0;
  434. }
  435. // Fill the array with a short value
  436. static
  437. void fillArray(cmsInt16Number array[], cmsInt16Number val, cmsUInt32Number size)
  438. {
  439. cmsUInt32Number i;
  440. size /= sizeof(cmsInt16Number);
  441. for (i = 0; i < size; i++)
  442. array[i] = val;
  443. }
  444. // Creates minimum required tags
  445. static
  446. void WriteOutputTags(TIFF* out, int Colorspace, int BytesPerSample, int AlphaChannels, int IsPremul)
  447. {
  448. int BitsPerSample = (8 * BytesPerSample);
  449. int nChannels = ChanCountFromPixelType(Colorspace);
  450. TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, BitsPerSample);
  451. TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, nChannels + AlphaChannels);
  452. if (AlphaChannels > 0)
  453. {
  454. cmsInt16Number Extra[20];
  455. if (IsPremul)
  456. fillArray(Extra, EXTRASAMPLE_ASSOCALPHA, sizeof(Extra));
  457. else
  458. fillArray(Extra, EXTRASAMPLE_UNASSALPHA, sizeof(Extra));
  459. TIFFSetField(out, TIFFTAG_EXTRASAMPLES, AlphaChannels, Extra);
  460. }
  461. switch (Colorspace) {
  462. case PT_GRAY:
  463. TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
  464. break;
  465. case PT_RGB:
  466. TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
  467. break;
  468. case PT_CMY:
  469. TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED);
  470. TIFFSetField(out, TIFFTAG_INKSET, INKSET_MULTIINK);
  471. break;
  472. case PT_CMYK:
  473. TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED);
  474. TIFFSetField(out, TIFFTAG_INKSET, INKSET_CMYK);
  475. break;
  476. case PT_Lab:
  477. if (BitsPerSample == 16)
  478. TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CIELAB);
  479. else
  480. TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CIELAB);
  481. break;
  482. // Multi-ink separations
  483. case PT_MCH2:
  484. case PT_MCH3:
  485. case PT_MCH4:
  486. case PT_MCH5:
  487. case PT_MCH6:
  488. case PT_MCH7:
  489. case PT_MCH8:
  490. case PT_MCH9:
  491. case PT_MCH10:
  492. case PT_MCH11:
  493. case PT_MCH12:
  494. case PT_MCH13:
  495. case PT_MCH14:
  496. case PT_MCH15:
  497. TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED);
  498. if (StoreAsAlpha && nChannels >= 4) {
  499. cmsInt16Number Extra[20];
  500. fillArray(Extra, EXTRASAMPLE_UNASSALPHA, sizeof(Extra));
  501. // CMYK plus extra alpha
  502. TIFFSetField(out, TIFFTAG_EXTRASAMPLES, nChannels - 4, Extra);
  503. TIFFSetField(out, TIFFTAG_INKSET, INKSET_CMYK);
  504. TIFFSetField(out, TIFFTAG_NUMBEROFINKS, 4);
  505. }
  506. else {
  507. TIFFSetField(out, TIFFTAG_INKSET, INKSET_MULTIINK);
  508. TIFFSetField(out, TIFFTAG_NUMBEROFINKS, nChannels);
  509. }
  510. break;
  511. default:
  512. FatalError("Unsupported output colorspace");
  513. }
  514. if (PixelDepth == 32)
  515. TIFFSetField(out, TIFFTAG_SAMPLEFORMAT,
  516. SAMPLEFORMAT_IEEEFP,
  517. SAMPLEFORMAT_IEEEFP,
  518. SAMPLEFORMAT_IEEEFP,
  519. SAMPLEFORMAT_IEEEFP);
  520. }
  521. // Copies a bunch of tags
  522. static
  523. void CopyOtherTags(TIFF* in, TIFF* out)
  524. {
  525. #define CopyField(tag, v) \
  526. if (TIFFGetField(in, tag, &v)) TIFFSetField(out, tag, v)
  527. short shortv;
  528. unsigned short compression;
  529. cmsUInt32Number ow, ol;
  530. cmsFloat32Number floatv;
  531. char *stringv;
  532. cmsUInt32Number longv;
  533. CopyField(TIFFTAG_SUBFILETYPE, longv);
  534. TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &ow);
  535. TIFFGetField(in, TIFFTAG_IMAGELENGTH, &ol);
  536. TIFFSetField(out, TIFFTAG_IMAGEWIDTH, ow);
  537. TIFFSetField(out, TIFFTAG_IMAGELENGTH, ol);
  538. CopyField(TIFFTAG_PLANARCONFIG, shortv);
  539. CopyField(TIFFTAG_COMPRESSION, compression);
  540. // This is tricky, libtiff would access predictor in a wrong way
  541. // if the codec is none of those
  542. if (compression == COMPRESSION_LZW ||
  543. compression == 34925 /*COMPRESSION_LZMA*/ ||
  544. compression == COMPRESSION_PIXARLOG ||
  545. compression == COMPRESSION_DEFLATE ||
  546. compression == COMPRESSION_ADOBE_DEFLATE ||
  547. compression == 50000 /*COMPRESSION_ZSTD*/)
  548. {
  549. if (PixelDepth != 32)
  550. CopyField(TIFFTAG_PREDICTOR, shortv);
  551. }
  552. CopyField(TIFFTAG_THRESHHOLDING, shortv);
  553. CopyField(TIFFTAG_FILLORDER, shortv);
  554. CopyField(TIFFTAG_ORIENTATION, shortv);
  555. CopyField(TIFFTAG_MINSAMPLEVALUE, shortv);
  556. CopyField(TIFFTAG_MAXSAMPLEVALUE, shortv);
  557. CopyField(TIFFTAG_XRESOLUTION, floatv);
  558. CopyField(TIFFTAG_YRESOLUTION, floatv);
  559. CopyField(TIFFTAG_RESOLUTIONUNIT, shortv);
  560. CopyField(TIFFTAG_ROWSPERSTRIP, longv);
  561. CopyField(TIFFTAG_XPOSITION, floatv);
  562. CopyField(TIFFTAG_YPOSITION, floatv);
  563. CopyField(TIFFTAG_IMAGEDEPTH, longv);
  564. CopyField(TIFFTAG_TILEDEPTH, longv);
  565. CopyField(TIFFTAG_TILEWIDTH, longv);
  566. CopyField(TIFFTAG_TILELENGTH, longv);
  567. CopyField(TIFFTAG_ARTIST, stringv);
  568. CopyField(TIFFTAG_IMAGEDESCRIPTION, stringv);
  569. CopyField(TIFFTAG_MAKE, stringv);
  570. CopyField(TIFFTAG_MODEL, stringv);
  571. CopyField(TIFFTAG_DATETIME, stringv);
  572. CopyField(TIFFTAG_HOSTCOMPUTER, stringv);
  573. CopyField(TIFFTAG_PAGENAME, stringv);
  574. CopyField(TIFFTAG_DOCUMENTNAME, stringv);
  575. }
  576. static
  577. void DoEmbedProfile(TIFF* Out, const char* ProfileFile)
  578. {
  579. FILE* f;
  580. cmsInt32Number size;
  581. cmsUInt32Number EmbedLen;
  582. cmsUInt8Number* EmbedBuffer;
  583. f = fopen(ProfileFile, "rb");
  584. if (f == NULL) return;
  585. size = cmsfilelength(f);
  586. if (size < 0) return;
  587. EmbedBuffer = (cmsUInt8Number*) malloc((size_t) size + 1);
  588. if (EmbedBuffer == NULL) {
  589. OutOfMem(size+1);
  590. return;
  591. }
  592. EmbedLen = (cmsUInt32Number) fread(EmbedBuffer, 1, (size_t) size, f);
  593. if (EmbedLen != (cmsUInt32Number) size)
  594. FatalError("Cannot read %ld bytes to %s", (long) size, ProfileFile);
  595. fclose(f);
  596. EmbedBuffer[EmbedLen] = 0;
  597. TIFFSetField(Out, TIFFTAG_ICCPROFILE, EmbedLen, EmbedBuffer);
  598. free(EmbedBuffer);
  599. }
  600. // Read or create a ICC profile from the TIFF data
  601. static
  602. cmsHPROFILE GetTIFFProfile(TIFF* in)
  603. {
  604. cmsCIExyYTRIPLE Primaries;
  605. cmsFloat32Number* chr;
  606. cmsCIExyY WhitePoint;
  607. cmsFloat32Number* wp;
  608. int i;
  609. cmsToneCurve* Curve[3];
  610. cmsUInt16Number *gmr, *gmg, *gmb;
  611. cmsHPROFILE hProfile;
  612. cmsUInt32Number EmbedLen;
  613. cmsUInt8Number* EmbedBuffer;
  614. if (IgnoreEmbedded) return NULL;
  615. if (TIFFGetField(in, TIFFTAG_ICCPROFILE, &EmbedLen, &EmbedBuffer)) {
  616. hProfile = cmsOpenProfileFromMem(EmbedBuffer, EmbedLen);
  617. // Print description found in the profile
  618. if (Verbose && (hProfile != NULL)) {
  619. fprintf(stdout, "\n[Embedded profile]\n");
  620. PrintProfileInformation(hProfile);
  621. fflush(stdout);
  622. }
  623. if (hProfile != NULL && SaveEmbedded != NULL)
  624. SaveMemoryBlock(EmbedBuffer, EmbedLen, SaveEmbedded);
  625. if (hProfile) return hProfile;
  626. }
  627. // Try to see if "colorimetric" tiff
  628. if (TIFFGetField(in, TIFFTAG_PRIMARYCHROMATICITIES, &chr)) {
  629. Primaries.Red.x = chr[0];
  630. Primaries.Red.y = chr[1];
  631. Primaries.Green.x = chr[2];
  632. Primaries.Green.y = chr[3];
  633. Primaries.Blue.x = chr[4];
  634. Primaries.Blue.y = chr[5];
  635. Primaries.Red.Y = Primaries.Green.Y = Primaries.Blue.Y = 1.0;
  636. if (TIFFGetField(in, TIFFTAG_WHITEPOINT, &wp)) {
  637. WhitePoint.x = wp[0];
  638. WhitePoint.y = wp[1];
  639. WhitePoint.Y = 1.0;
  640. // Transfer function is a bit harder....
  641. TIFFGetFieldDefaulted(in, TIFFTAG_TRANSFERFUNCTION,
  642. &gmr,
  643. &gmg,
  644. &gmb);
  645. Curve[0] = cmsBuildTabulatedToneCurve16(NULL, 256, gmr);
  646. Curve[1] = cmsBuildTabulatedToneCurve16(NULL, 256, gmg);
  647. Curve[2] = cmsBuildTabulatedToneCurve16(NULL, 256, gmb);
  648. hProfile = cmsCreateRGBProfileTHR(NULL, &WhitePoint, &Primaries, Curve);
  649. for (i=0; i < 3; i++)
  650. cmsFreeToneCurve(Curve[i]);
  651. if (Verbose) {
  652. fprintf(stdout, "\n[Colorimetric TIFF]\n");
  653. }
  654. return hProfile;
  655. }
  656. }
  657. return NULL;
  658. }
  659. // Transform one image
  660. static
  661. int TransformImage(TIFF* in, TIFF* out, const char *cDefInpProf)
  662. {
  663. cmsHPROFILE hIn, hOut, hProof, hInkLimit = NULL;
  664. cmsHTRANSFORM xform;
  665. cmsUInt32Number wInput, wOutput;
  666. int OutputColorSpace;
  667. int BytesPerSample = PixelDepth / 8;
  668. cmsUInt32Number dwFlags;
  669. int nPlanes;
  670. // Default options
  671. dwFlags = cmsFLAGS_COPY_ALPHA;
  672. // Observer adaptation state (only meaningful on absolute colorimetric intent)
  673. cmsSetAdaptationState(ObserverAdaptationState);
  674. if (EmbedProfile && cOutProf)
  675. DoEmbedProfile(out, cOutProf);
  676. if (BlackWhiteCompensation)
  677. dwFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
  678. switch (PrecalcMode) {
  679. case 0: dwFlags |= cmsFLAGS_NOOPTIMIZE; break;
  680. case 2: dwFlags |= cmsFLAGS_HIGHRESPRECALC; break;
  681. case 3: dwFlags |= cmsFLAGS_LOWRESPRECALC; break;
  682. case 1: break;
  683. default: FatalError("Unknown precalculation mode '%d'", PrecalcMode);
  684. }
  685. if (GamutCheck)
  686. dwFlags |= cmsFLAGS_GAMUTCHECK;
  687. hProof = NULL;
  688. hOut = NULL;
  689. if (lIsDeviceLink) {
  690. if (lIsCUBE)
  691. hIn = cmsCreateDeviceLinkFromCubeFile(cDefInpProf);
  692. else
  693. hIn = cmsOpenProfileFromFile(cDefInpProf, "r");
  694. }
  695. else {
  696. hIn = GetTIFFProfile(in);
  697. if (hIn == NULL)
  698. hIn = OpenStockProfile(NULL, cDefInpProf);
  699. hOut = OpenStockProfile(NULL, cOutProf);
  700. if (cProofing != NULL) {
  701. hProof = OpenStockProfile(NULL, cProofing);
  702. dwFlags |= cmsFLAGS_SOFTPROOFING;
  703. }
  704. }
  705. // Take input color space
  706. wInput = GetInputPixelType(in);
  707. // Assure both, input profile and input TIFF are on same colorspace
  708. if (_cmsLCMScolorSpace(cmsGetColorSpace(hIn)) != (int) T_COLORSPACE(wInput))
  709. FatalError("Input profile is not operating in proper color space (%d)", T_COLORSPACE(wInput));
  710. if (!lIsDeviceLink)
  711. OutputColorSpace = _cmsLCMScolorSpace(cmsGetColorSpace(hOut));
  712. else
  713. OutputColorSpace = _cmsLCMScolorSpace(cmsGetPCS(hIn));
  714. wOutput = ComputeOutputFormatDescriptor(wInput, OutputColorSpace, BytesPerSample);
  715. WriteOutputTags(out, OutputColorSpace, BytesPerSample, T_EXTRA(wOutput), T_PREMUL(wOutput));
  716. CopyOtherTags(in, out);
  717. // Ink limit
  718. if (InkLimit != 400.0 &&
  719. (OutputColorSpace == PT_CMYK || OutputColorSpace == PT_CMY)) {
  720. cmsHPROFILE hProfiles[10];
  721. int nProfiles = 0;
  722. hInkLimit = cmsCreateInkLimitingDeviceLink(cmsGetColorSpace(hOut), InkLimit);
  723. hProfiles[nProfiles++] = hIn;
  724. if (hProof) {
  725. hProfiles[nProfiles++] = hProof;
  726. hProfiles[nProfiles++] = hProof;
  727. }
  728. hProfiles[nProfiles++] = hOut;
  729. hProfiles[nProfiles++] = hInkLimit;
  730. xform = cmsCreateMultiprofileTransform(hProfiles, nProfiles,
  731. wInput, wOutput, Intent, dwFlags);
  732. }
  733. else {
  734. xform = cmsCreateProofingTransform(hIn, wInput,
  735. hOut, wOutput,
  736. hProof, Intent,
  737. ProofingIntent,
  738. dwFlags);
  739. }
  740. cmsCloseProfile(hIn);
  741. cmsCloseProfile(hOut);
  742. if (hInkLimit)
  743. cmsCloseProfile(hInkLimit);
  744. if (hProof)
  745. cmsCloseProfile(hProof);
  746. if (xform == NULL) return 0;
  747. // Planar stuff
  748. if (T_PLANAR(wInput))
  749. nPlanes = T_CHANNELS(wInput) + T_EXTRA(wInput);
  750. else
  751. nPlanes = 1;
  752. // Handle tile by tile or strip by strip
  753. if (TIFFIsTiled(in)) {
  754. TileBasedXform(xform, in, out, nPlanes);
  755. }
  756. else {
  757. StripBasedXform(xform, in, out, nPlanes);
  758. }
  759. cmsDeleteTransform(xform);
  760. TIFFWriteDirectory(out);
  761. return 1;
  762. }
  763. // Print help
  764. static
  765. void Help(int level)
  766. {
  767. UTILS_UNUSED_PARAMETER(level);
  768. fprintf(stderr, "Little CMS ICC profile applier for TIFF - v8.0 [LittleCMS %2.2f]\n", cmsGetEncodedCMMversion() / 1000.0);
  769. fprintf(stderr, "Copyright (c) 1998-2023 Marti Maria Saguer. See COPYING file for details.\n");
  770. fflush(stderr);
  771. fprintf(stderr, "usage: tificc [flags] input.tif output.tif\n");
  772. fprintf(stderr, "\nflags:\n\n");
  773. fprintf(stderr, "-v - Verbose\n");
  774. fprintf(stderr, "-i<profile> - Input profile (defaults to sRGB)\n");
  775. fprintf(stderr, "-o<profile> - Output profile (defaults to sRGB)\n");
  776. fprintf(stderr, "-l<profile> - Transform by device-link profile\n");
  777. fprintf(stderr, "-u<profile> - Transform by CUBE colormap\n");
  778. PrintBuiltins();
  779. PrintRenderingIntents();
  780. fprintf(stderr, "-b - Black point compensation\n");
  781. fprintf(stderr, "-d<0..1> - Observer adaptation state (abs.col. only)\n");
  782. fprintf(stderr, "-c<0,1,2,3> - Precalculates transform (0=Off, 1=Normal, 2=Hi-res, 3=LoRes)\n");
  783. fprintf(stderr, "\n");
  784. fprintf(stderr, "-w<8,16,32> - Output depth. Use 32 for floating-point\n\n");
  785. fprintf(stderr, "-a - Handle channels > 4 as alpha\n");
  786. fprintf(stderr, "-n - Ignore embedded profile on input\n");
  787. fprintf(stderr, "-e - Embed destination profile\n");
  788. fprintf(stderr, "-s<new profile> - Save embedded profile as <new profile>\n");
  789. fprintf(stderr, "\n");
  790. fprintf(stderr, "-p<profile> - Soft proof profile\n");
  791. fprintf(stderr, "-m<n> - Soft proof intent\n");
  792. fprintf(stderr, "\tThat is the intent used to translate the simulation to the output device.\n\tNote that the simulated intent is set by using -t not by -m\n");
  793. fprintf(stderr, "-g - Marks out-of-gamut colors on softproof\n");
  794. fprintf(stderr, "\n");
  795. fprintf(stderr, "-k<0..400> - Ink-limiting in %% (CMYK only)\n");
  796. fprintf(stderr, "\n");
  797. fprintf(stderr, "Examples:\n\n"
  798. "To color correct from scanner to sRGB:\n"
  799. "\ttificc -iscanner.icm in.tif out.tif\n"
  800. "To convert from monitor1 to monitor2:\n"
  801. "\ttificc -imon1.icm -omon2.icm in.tif out.tif\n"
  802. "To make a CMYK separation:\n"
  803. "\ttificc -oprinter.icm inrgb.tif outcmyk.tif\n"
  804. "To recover sRGB from a CMYK separation:\n"
  805. "\ttificc -iprinter.icm incmyk.tif outrgb.tif\n"
  806. "To soft-proof how behaves Probev1_ICCv4.icc on perceptual:\n"
  807. "\ttifficc -t0 -p Probev1_ICCv4.icc -m1 infile.tif out.tif\n"
  808. "To convert from CIELab TIFF to sRGB\n"
  809. "\ttificc -i*Lab in.tif out.tif\n\n");
  810. fprintf(stderr, "This program is intended to be a demo of the Little CMS\n"
  811. "color engine. Both lcms and this program are open source.\n"
  812. "You can obtain both in source code at https://www.littlecms.com\n"
  813. "For suggestions, comments, bug reports etc. send mail to\n"
  814. "info@littlecms.com\n\n");
  815. exit(0);
  816. }
  817. // The toggles stuff
  818. static
  819. void HandleSwitches(int argc, char *argv[])
  820. {
  821. int s;
  822. while ((s=xgetopt(argc,argv,"aAeEbBw:W:nNvVGgh:H:i:I:o:O:P:p:t:T:c:C:l:L:u:U:M:m:K:k:S:s:D:d:-:")) != EOF) {
  823. switch (s) {
  824. case '-':
  825. if (strcmp(xoptarg, "help") == 0)
  826. {
  827. Help(0);
  828. }
  829. else
  830. {
  831. FatalError("Unknown option - run without args to see valid ones.\n");
  832. }
  833. break;
  834. case 'a':
  835. case 'A':
  836. StoreAsAlpha = TRUE;
  837. break;
  838. case 'b':
  839. case 'B':
  840. BlackWhiteCompensation = TRUE;
  841. break;
  842. case 'c':
  843. case 'C':
  844. PrecalcMode = atoi(xoptarg);
  845. if (PrecalcMode < 0 || PrecalcMode > 3)
  846. FatalError("Unknown precalc mode '%d'", PrecalcMode);
  847. break;
  848. case 'd':
  849. case 'D': ObserverAdaptationState = atof(xoptarg);
  850. if (ObserverAdaptationState < 0 ||
  851. ObserverAdaptationState > 1.0)
  852. Warning("Adaptation state should be 0..1");
  853. break;
  854. case 'e':
  855. case 'E':
  856. EmbedProfile = TRUE;
  857. break;
  858. case 'g':
  859. case 'G':
  860. GamutCheck = TRUE;
  861. break;
  862. case 'v':
  863. case 'V':
  864. Verbose = TRUE;
  865. break;
  866. case 'i':
  867. case 'I':
  868. if (lIsDeviceLink)
  869. FatalError("Device-link already specified");
  870. cInpProf = xoptarg;
  871. break;
  872. case 'o':
  873. case 'O':
  874. if (lIsDeviceLink)
  875. FatalError("Device-link already specified");
  876. cOutProf = xoptarg;
  877. break;
  878. case 'l':
  879. case 'L':
  880. if (cInpProf != NULL || cOutProf != NULL)
  881. FatalError("input/output profiles already specified");
  882. cInpProf = xoptarg;
  883. lIsDeviceLink = TRUE;
  884. lIsCUBE = FALSE;
  885. break;
  886. case 'u':
  887. case 'U':
  888. if (cInpProf != NULL || cOutProf != NULL)
  889. FatalError("input/output profiles already specified");
  890. cInpProf = xoptarg;
  891. lIsDeviceLink = TRUE;
  892. lIsCUBE = TRUE;
  893. break;
  894. case 'p':
  895. case 'P':
  896. cProofing = xoptarg;
  897. break;
  898. case 't':
  899. case 'T':
  900. Intent = atoi(xoptarg);
  901. break;
  902. case 'm':
  903. case 'M':
  904. ProofingIntent = atoi(xoptarg);
  905. break;
  906. case 'N':
  907. case 'n':
  908. IgnoreEmbedded = TRUE;
  909. break;
  910. case 'W':
  911. case 'w':
  912. PixelDepth = atoi(xoptarg);
  913. if (PixelDepth != 8 && PixelDepth != 16 && PixelDepth != 32)
  914. FatalError("Only 8, 16 and 32 bps are supported");
  915. break;
  916. case 'k':
  917. case 'K':
  918. InkLimit = atof(xoptarg);
  919. if (InkLimit < 0.0 || InkLimit > 400.0)
  920. FatalError("Ink limit must be 0%%..400%%");
  921. break;
  922. case 's':
  923. case 'S': SaveEmbedded = xoptarg;
  924. break;
  925. case 'H':
  926. case 'h': {
  927. int a = atoi(xoptarg);
  928. Help(a);
  929. }
  930. break;
  931. default:
  932. FatalError("Unknown option - run without args to see valid ones");
  933. }
  934. }
  935. }
  936. // The main sink
  937. int main(int argc, char* argv[])
  938. {
  939. TIFF *in, *out;
  940. cmsPlugin(&TiffLabPlugin);
  941. InitUtils("tificc");
  942. HandleSwitches(argc, argv);
  943. if ((argc - xoptind) != 2) {
  944. Help(0);
  945. }
  946. TIFFSetErrorHandler(ConsoleErrorHandler);
  947. TIFFSetWarningHandler(ConsoleWarningHandler);
  948. in = TIFFOpen(argv[xoptind], "r");
  949. if (in == NULL) FatalError("Unable to open '%s'", argv[xoptind]);
  950. out = TIFFOpen(argv[xoptind+1], "w");
  951. if (out == NULL) {
  952. TIFFClose(in);
  953. FatalError("Unable to write '%s'", argv[xoptind+1]);
  954. }
  955. do {
  956. TransformImage(in, out, cInpProf);
  957. } while (TIFFReadDirectory(in));
  958. if (Verbose) { fprintf(stdout, "\n"); fflush(stdout); }
  959. TIFFClose(in);
  960. TIFFClose(out);
  961. return 0;
  962. }