tif_luv.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. /*
  2. * Copyright (c) 1997 Greg Ward Larson
  3. * Copyright (c) 1997 Silicon Graphics, Inc.
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and
  6. * its documentation for any purpose is hereby granted without fee, provided
  7. * that (i) the above copyright notices and this permission notice appear in
  8. * all copies of the software and related documentation, and (ii) the names of
  9. * Sam Leffler, Greg Larson and Silicon Graphics may not be used in any
  10. * advertising or publicity relating to the software without the specific,
  11. * prior written permission of Sam Leffler, Greg Larson and Silicon Graphics.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SAM LEFFLER, GREG LARSON OR SILICON GRAPHICS BE LIABLE
  18. * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. #include "tiffiop.h"
  25. #ifdef LOGLUV_SUPPORT
  26. /*
  27. * TIFF Library.
  28. * LogLuv compression support for high dynamic range images.
  29. *
  30. * Contributed by Greg Larson.
  31. *
  32. * LogLuv image support uses the TIFF library to store 16 or 10-bit
  33. * log luminance values with 8 bits each of u and v or a 14-bit index.
  34. *
  35. * The codec can take as input and produce as output 32-bit IEEE float values
  36. * as well as 16-bit integer values. A 16-bit luminance is interpreted
  37. * as a sign bit followed by a 15-bit integer that is converted
  38. * to and from a linear magnitude using the transformation:
  39. *
  40. * L = 2^( (Le+.5)/256 - 64 ) # real from 15-bit
  41. *
  42. * Le = floor( 256*(log2(L) + 64) ) # 15-bit from real
  43. *
  44. * The actual conversion to world luminance units in candelas per sq. meter
  45. * requires an additional multiplier, which is stored in the TIFFTAG_STONITS.
  46. * This value is usually set such that a reasonable exposure comes from
  47. * clamping decoded luminances above 1 to 1 in the displayed image.
  48. *
  49. * The 16-bit values for u and v may be converted to real values by dividing
  50. * each by 32768. (This allows for negative values, which aren't useful as
  51. * far as we know, but are left in case of future improvements in human
  52. * color vision.)
  53. *
  54. * Conversion from (u,v), which is actually the CIE (u',v') system for
  55. * you color scientists, is accomplished by the following transformation:
  56. *
  57. * u = 4*x / (-2*x + 12*y + 3)
  58. * v = 9*y / (-2*x + 12*y + 3)
  59. *
  60. * x = 9*u / (6*u - 16*v + 12)
  61. * y = 4*v / (6*u - 16*v + 12)
  62. *
  63. * This process is greatly simplified by passing 32-bit IEEE floats
  64. * for each of three CIE XYZ coordinates. The codec then takes care
  65. * of conversion to and from LogLuv, though the application is still
  66. * responsible for interpreting the TIFFTAG_STONITS calibration factor.
  67. *
  68. * By definition, a CIE XYZ vector of [1 1 1] corresponds to a neutral white
  69. * point of (x,y)=(1/3,1/3). However, most color systems assume some other
  70. * white point, such as D65, and an absolute color conversion to XYZ then
  71. * to another color space with a different white point may introduce an
  72. * unwanted color cast to the image. It is often desirable, therefore, to
  73. * perform a white point conversion that maps the input white to [1 1 1]
  74. * in XYZ, then record the original white point using the TIFFTAG_WHITEPOINT
  75. * tag value. A decoder that demands absolute color calibration may use
  76. * this white point tag to get back the original colors, but usually it
  77. * will be ignored and the new white point will be used instead that
  78. * matches the output color space.
  79. *
  80. * Pixel information is compressed into one of two basic encodings, depending
  81. * on the setting of the compression tag, which is one of COMPRESSION_SGILOG
  82. * or COMPRESSION_SGILOG24. For COMPRESSION_SGILOG, greyscale data is
  83. * stored as:
  84. *
  85. * 1 15
  86. * |-+---------------|
  87. *
  88. * COMPRESSION_SGILOG color data is stored as:
  89. *
  90. * 1 15 8 8
  91. * |-+---------------|--------+--------|
  92. * S Le ue ve
  93. *
  94. * For the 24-bit COMPRESSION_SGILOG24 color format, the data is stored as:
  95. *
  96. * 10 14
  97. * |----------|--------------|
  98. * Le' Ce
  99. *
  100. * There is no sign bit in the 24-bit case, and the (u,v) chromaticity is
  101. * encoded as an index for optimal color resolution. The 10 log bits are
  102. * defined by the following conversions:
  103. *
  104. * L = 2^((Le'+.5)/64 - 12) # real from 10-bit
  105. *
  106. * Le' = floor( 64*(log2(L) + 12) ) # 10-bit from real
  107. *
  108. * The 10 bits of the smaller format may be converted into the 15 bits of
  109. * the larger format by multiplying by 4 and adding 13314. Obviously,
  110. * a smaller range of magnitudes is covered (about 5 orders of magnitude
  111. * instead of 38), and the lack of a sign bit means that negative luminances
  112. * are not allowed. (Well, they aren't allowed in the real world, either,
  113. * but they are useful for certain types of image processing.)
  114. *
  115. * The desired user format is controlled by the setting the internal
  116. * pseudo tag TIFFTAG_SGILOGDATAFMT to one of:
  117. * SGILOGDATAFMT_FLOAT = IEEE 32-bit float XYZ values
  118. * SGILOGDATAFMT_16BIT = 16-bit integer encodings of logL, u and v
  119. * Raw data i/o is also possible using:
  120. * SGILOGDATAFMT_RAW = 32-bit unsigned integer with encoded pixel
  121. * In addition, the following decoding is provided for ease of display:
  122. * SGILOGDATAFMT_8BIT = 8-bit default RGB gamma-corrected values
  123. *
  124. * For grayscale images, we provide the following data formats:
  125. * SGILOGDATAFMT_FLOAT = IEEE 32-bit float Y values
  126. * SGILOGDATAFMT_16BIT = 16-bit integer w/ encoded luminance
  127. * SGILOGDATAFMT_8BIT = 8-bit gray monitor values
  128. *
  129. * Note that the COMPRESSION_SGILOG applies a simple run-length encoding
  130. * scheme by separating the logL, u and v bytes for each row and applying
  131. * a PackBits type of compression. Since the 24-bit encoding is not
  132. * adaptive, the 32-bit color format takes less space in many cases.
  133. *
  134. * Further control is provided over the conversion from higher-resolution
  135. * formats to final encoded values through the pseudo tag
  136. * TIFFTAG_SGILOGENCODE:
  137. * SGILOGENCODE_NODITHER = do not dither encoded values
  138. * SGILOGENCODE_RANDITHER = apply random dithering during encoding
  139. *
  140. * The default value of this tag is SGILOGENCODE_NODITHER for
  141. * COMPRESSION_SGILOG to maximize run-length encoding and
  142. * SGILOGENCODE_RANDITHER for COMPRESSION_SGILOG24 to turn
  143. * quantization errors into noise.
  144. */
  145. #include <math.h>
  146. #include <stdio.h>
  147. #include <stdlib.h>
  148. /*
  149. * State block for each open TIFF
  150. * file using LogLuv compression/decompression.
  151. */
  152. typedef struct logLuvState LogLuvState;
  153. struct logLuvState
  154. {
  155. int encoder_state; /* 1 if encoder correctly initialized */
  156. int user_datafmt; /* user data format */
  157. int encode_meth; /* encoding method */
  158. int pixel_size; /* bytes per pixel */
  159. uint8_t *tbuf; /* translation buffer */
  160. tmsize_t tbuflen; /* buffer length */
  161. void (*tfunc)(LogLuvState *, uint8_t *, tmsize_t);
  162. TIFFVSetMethod vgetparent; /* super-class method */
  163. TIFFVSetMethod vsetparent; /* super-class method */
  164. };
  165. #define DecoderState(tif) ((LogLuvState *)(tif)->tif_data)
  166. #define EncoderState(tif) ((LogLuvState *)(tif)->tif_data)
  167. #define SGILOGDATAFMT_UNKNOWN -1
  168. #define MINRUN 4 /* minimum run length */
  169. /*
  170. * Decode a string of 16-bit gray pixels.
  171. */
  172. static int LogL16Decode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
  173. {
  174. static const char module[] = "LogL16Decode";
  175. LogLuvState *sp = DecoderState(tif);
  176. int shft;
  177. tmsize_t i;
  178. tmsize_t npixels;
  179. unsigned char *bp;
  180. int16_t *tp;
  181. int16_t b;
  182. tmsize_t cc;
  183. int rc;
  184. (void)s;
  185. assert(s == 0);
  186. assert(sp != NULL);
  187. npixels = occ / sp->pixel_size;
  188. if (sp->user_datafmt == SGILOGDATAFMT_16BIT)
  189. tp = (int16_t *)op;
  190. else
  191. {
  192. if (sp->tbuflen < npixels)
  193. {
  194. TIFFErrorExtR(tif, module, "Translation buffer too short");
  195. return (0);
  196. }
  197. tp = (int16_t *)sp->tbuf;
  198. }
  199. _TIFFmemset((void *)tp, 0, npixels * sizeof(tp[0]));
  200. bp = (unsigned char *)tif->tif_rawcp;
  201. cc = tif->tif_rawcc;
  202. /* get each byte string */
  203. for (shft = 8; shft >= 0; shft -= 8)
  204. {
  205. for (i = 0; i < npixels && cc > 0;)
  206. {
  207. if (*bp >= 128)
  208. { /* run */
  209. if (cc < 2)
  210. break;
  211. rc = *bp++ + (2 - 128);
  212. b = (int16_t)(*bp++ << shft);
  213. cc -= 2;
  214. while (rc-- && i < npixels)
  215. tp[i++] |= b;
  216. }
  217. else
  218. { /* non-run */
  219. rc = *bp++; /* nul is noop */
  220. while (--cc && rc-- && i < npixels)
  221. tp[i++] |= (int16_t)*bp++ << shft;
  222. }
  223. }
  224. if (i != npixels)
  225. {
  226. TIFFErrorExtR(tif, module,
  227. "Not enough data at row %" PRIu32
  228. " (short %" TIFF_SSIZE_FORMAT " pixels)",
  229. tif->tif_row, npixels - i);
  230. tif->tif_rawcp = (uint8_t *)bp;
  231. tif->tif_rawcc = cc;
  232. return (0);
  233. }
  234. }
  235. (*sp->tfunc)(sp, op, npixels);
  236. tif->tif_rawcp = (uint8_t *)bp;
  237. tif->tif_rawcc = cc;
  238. return (1);
  239. }
  240. /*
  241. * Decode a string of 24-bit pixels.
  242. */
  243. static int LogLuvDecode24(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
  244. {
  245. static const char module[] = "LogLuvDecode24";
  246. LogLuvState *sp = DecoderState(tif);
  247. tmsize_t cc;
  248. tmsize_t i;
  249. tmsize_t npixels;
  250. unsigned char *bp;
  251. uint32_t *tp;
  252. (void)s;
  253. assert(s == 0);
  254. assert(sp != NULL);
  255. npixels = occ / sp->pixel_size;
  256. if (sp->user_datafmt == SGILOGDATAFMT_RAW)
  257. tp = (uint32_t *)op;
  258. else
  259. {
  260. if (sp->tbuflen < npixels)
  261. {
  262. TIFFErrorExtR(tif, module, "Translation buffer too short");
  263. return (0);
  264. }
  265. tp = (uint32_t *)sp->tbuf;
  266. }
  267. /* copy to array of uint32_t */
  268. bp = (unsigned char *)tif->tif_rawcp;
  269. cc = tif->tif_rawcc;
  270. for (i = 0; i < npixels && cc >= 3; i++)
  271. {
  272. tp[i] = bp[0] << 16 | bp[1] << 8 | bp[2];
  273. bp += 3;
  274. cc -= 3;
  275. }
  276. tif->tif_rawcp = (uint8_t *)bp;
  277. tif->tif_rawcc = cc;
  278. if (i != npixels)
  279. {
  280. TIFFErrorExtR(tif, module,
  281. "Not enough data at row %" PRIu32
  282. " (short %" TIFF_SSIZE_FORMAT " pixels)",
  283. tif->tif_row, npixels - i);
  284. return (0);
  285. }
  286. (*sp->tfunc)(sp, op, npixels);
  287. return (1);
  288. }
  289. /*
  290. * Decode a string of 32-bit pixels.
  291. */
  292. static int LogLuvDecode32(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
  293. {
  294. static const char module[] = "LogLuvDecode32";
  295. LogLuvState *sp;
  296. int shft;
  297. tmsize_t i;
  298. tmsize_t npixels;
  299. unsigned char *bp;
  300. uint32_t *tp;
  301. uint32_t b;
  302. tmsize_t cc;
  303. int rc;
  304. (void)s;
  305. assert(s == 0);
  306. sp = DecoderState(tif);
  307. assert(sp != NULL);
  308. npixels = occ / sp->pixel_size;
  309. if (sp->user_datafmt == SGILOGDATAFMT_RAW)
  310. tp = (uint32_t *)op;
  311. else
  312. {
  313. if (sp->tbuflen < npixels)
  314. {
  315. TIFFErrorExtR(tif, module, "Translation buffer too short");
  316. return (0);
  317. }
  318. tp = (uint32_t *)sp->tbuf;
  319. }
  320. _TIFFmemset((void *)tp, 0, npixels * sizeof(tp[0]));
  321. bp = (unsigned char *)tif->tif_rawcp;
  322. cc = tif->tif_rawcc;
  323. /* get each byte string */
  324. for (shft = 24; shft >= 0; shft -= 8)
  325. {
  326. for (i = 0; i < npixels && cc > 0;)
  327. {
  328. if (*bp >= 128)
  329. { /* run */
  330. if (cc < 2)
  331. break;
  332. rc = *bp++ + (2 - 128);
  333. b = (uint32_t)*bp++ << shft;
  334. cc -= 2;
  335. while (rc-- && i < npixels)
  336. tp[i++] |= b;
  337. }
  338. else
  339. { /* non-run */
  340. rc = *bp++; /* nul is noop */
  341. while (--cc && rc-- && i < npixels)
  342. tp[i++] |= (uint32_t)*bp++ << shft;
  343. }
  344. }
  345. if (i != npixels)
  346. {
  347. TIFFErrorExtR(tif, module,
  348. "Not enough data at row %" PRIu32
  349. " (short %" TIFF_SSIZE_FORMAT " pixels)",
  350. tif->tif_row, npixels - i);
  351. tif->tif_rawcp = (uint8_t *)bp;
  352. tif->tif_rawcc = cc;
  353. return (0);
  354. }
  355. }
  356. (*sp->tfunc)(sp, op, npixels);
  357. tif->tif_rawcp = (uint8_t *)bp;
  358. tif->tif_rawcc = cc;
  359. return (1);
  360. }
  361. /*
  362. * Decode a strip of pixels. We break it into rows to
  363. * maintain synchrony with the encode algorithm, which
  364. * is row by row.
  365. */
  366. static int LogLuvDecodeStrip(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
  367. {
  368. tmsize_t rowlen = TIFFScanlineSize(tif);
  369. if (rowlen == 0)
  370. return 0;
  371. assert(cc % rowlen == 0);
  372. while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s))
  373. {
  374. bp += rowlen;
  375. cc -= rowlen;
  376. }
  377. return (cc == 0);
  378. }
  379. /*
  380. * Decode a tile of pixels. We break it into rows to
  381. * maintain synchrony with the encode algorithm, which
  382. * is row by row.
  383. */
  384. static int LogLuvDecodeTile(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
  385. {
  386. tmsize_t rowlen = TIFFTileRowSize(tif);
  387. if (rowlen == 0)
  388. return 0;
  389. assert(cc % rowlen == 0);
  390. while (cc && (*tif->tif_decoderow)(tif, bp, rowlen, s))
  391. {
  392. bp += rowlen;
  393. cc -= rowlen;
  394. }
  395. return (cc == 0);
  396. }
  397. /*
  398. * Encode a row of 16-bit pixels.
  399. */
  400. static int LogL16Encode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
  401. {
  402. static const char module[] = "LogL16Encode";
  403. LogLuvState *sp = EncoderState(tif);
  404. int shft;
  405. tmsize_t i;
  406. tmsize_t j;
  407. tmsize_t npixels;
  408. uint8_t *op;
  409. int16_t *tp;
  410. int16_t b;
  411. tmsize_t occ;
  412. int rc = 0, mask;
  413. tmsize_t beg;
  414. (void)s;
  415. assert(s == 0);
  416. assert(sp != NULL);
  417. npixels = cc / sp->pixel_size;
  418. if (sp->user_datafmt == SGILOGDATAFMT_16BIT)
  419. tp = (int16_t *)bp;
  420. else
  421. {
  422. tp = (int16_t *)sp->tbuf;
  423. if (sp->tbuflen < npixels)
  424. {
  425. TIFFErrorExtR(tif, module, "Translation buffer too short");
  426. return (0);
  427. }
  428. (*sp->tfunc)(sp, bp, npixels);
  429. }
  430. /* compress each byte string */
  431. op = tif->tif_rawcp;
  432. occ = tif->tif_rawdatasize - tif->tif_rawcc;
  433. for (shft = 8; shft >= 0; shft -= 8)
  434. {
  435. for (i = 0; i < npixels; i += rc)
  436. {
  437. if (occ < 4)
  438. {
  439. tif->tif_rawcp = op;
  440. tif->tif_rawcc = tif->tif_rawdatasize - occ;
  441. if (!TIFFFlushData1(tif))
  442. return (0);
  443. op = tif->tif_rawcp;
  444. occ = tif->tif_rawdatasize - tif->tif_rawcc;
  445. }
  446. mask = 0xff << shft; /* find next run */
  447. for (beg = i; beg < npixels; beg += rc)
  448. {
  449. b = (int16_t)(tp[beg] & mask);
  450. rc = 1;
  451. while (rc < 127 + 2 && beg + rc < npixels &&
  452. (tp[beg + rc] & mask) == b)
  453. rc++;
  454. if (rc >= MINRUN)
  455. break; /* long enough */
  456. }
  457. if (beg - i > 1 && beg - i < MINRUN)
  458. {
  459. b = (int16_t)(tp[i] & mask); /*check short run */
  460. j = i + 1;
  461. while ((tp[j++] & mask) == b)
  462. if (j == beg)
  463. {
  464. *op++ = (uint8_t)(128 - 2 + j - i);
  465. *op++ = (uint8_t)(b >> shft);
  466. occ -= 2;
  467. i = beg;
  468. break;
  469. }
  470. }
  471. while (i < beg)
  472. { /* write out non-run */
  473. if ((j = beg - i) > 127)
  474. j = 127;
  475. if (occ < j + 3)
  476. {
  477. tif->tif_rawcp = op;
  478. tif->tif_rawcc = tif->tif_rawdatasize - occ;
  479. if (!TIFFFlushData1(tif))
  480. return (0);
  481. op = tif->tif_rawcp;
  482. occ = tif->tif_rawdatasize - tif->tif_rawcc;
  483. }
  484. *op++ = (uint8_t)j;
  485. occ--;
  486. while (j--)
  487. {
  488. *op++ = (uint8_t)(tp[i++] >> shft & 0xff);
  489. occ--;
  490. }
  491. }
  492. if (rc >= MINRUN)
  493. { /* write out run */
  494. *op++ = (uint8_t)(128 - 2 + rc);
  495. *op++ = (uint8_t)(tp[beg] >> shft & 0xff);
  496. occ -= 2;
  497. }
  498. else
  499. rc = 0;
  500. }
  501. }
  502. tif->tif_rawcp = op;
  503. tif->tif_rawcc = tif->tif_rawdatasize - occ;
  504. return (1);
  505. }
  506. /*
  507. * Encode a row of 24-bit pixels.
  508. */
  509. static int LogLuvEncode24(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
  510. {
  511. static const char module[] = "LogLuvEncode24";
  512. LogLuvState *sp = EncoderState(tif);
  513. tmsize_t i;
  514. tmsize_t npixels;
  515. tmsize_t occ;
  516. uint8_t *op;
  517. uint32_t *tp;
  518. (void)s;
  519. assert(s == 0);
  520. assert(sp != NULL);
  521. npixels = cc / sp->pixel_size;
  522. if (sp->user_datafmt == SGILOGDATAFMT_RAW)
  523. tp = (uint32_t *)bp;
  524. else
  525. {
  526. tp = (uint32_t *)sp->tbuf;
  527. if (sp->tbuflen < npixels)
  528. {
  529. TIFFErrorExtR(tif, module, "Translation buffer too short");
  530. return (0);
  531. }
  532. (*sp->tfunc)(sp, bp, npixels);
  533. }
  534. /* write out encoded pixels */
  535. op = tif->tif_rawcp;
  536. occ = tif->tif_rawdatasize - tif->tif_rawcc;
  537. for (i = npixels; i--;)
  538. {
  539. if (occ < 3)
  540. {
  541. tif->tif_rawcp = op;
  542. tif->tif_rawcc = tif->tif_rawdatasize - occ;
  543. if (!TIFFFlushData1(tif))
  544. return (0);
  545. op = tif->tif_rawcp;
  546. occ = tif->tif_rawdatasize - tif->tif_rawcc;
  547. }
  548. *op++ = (uint8_t)(*tp >> 16);
  549. *op++ = (uint8_t)(*tp >> 8 & 0xff);
  550. *op++ = (uint8_t)(*tp++ & 0xff);
  551. occ -= 3;
  552. }
  553. tif->tif_rawcp = op;
  554. tif->tif_rawcc = tif->tif_rawdatasize - occ;
  555. return (1);
  556. }
  557. /*
  558. * Encode a row of 32-bit pixels.
  559. */
  560. static int LogLuvEncode32(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
  561. {
  562. static const char module[] = "LogLuvEncode32";
  563. LogLuvState *sp = EncoderState(tif);
  564. int shft;
  565. tmsize_t i;
  566. tmsize_t j;
  567. tmsize_t npixels;
  568. uint8_t *op;
  569. uint32_t *tp;
  570. uint32_t b;
  571. tmsize_t occ;
  572. int rc = 0;
  573. tmsize_t beg;
  574. (void)s;
  575. assert(s == 0);
  576. assert(sp != NULL);
  577. npixels = cc / sp->pixel_size;
  578. if (sp->user_datafmt == SGILOGDATAFMT_RAW)
  579. tp = (uint32_t *)bp;
  580. else
  581. {
  582. tp = (uint32_t *)sp->tbuf;
  583. if (sp->tbuflen < npixels)
  584. {
  585. TIFFErrorExtR(tif, module, "Translation buffer too short");
  586. return (0);
  587. }
  588. (*sp->tfunc)(sp, bp, npixels);
  589. }
  590. /* compress each byte string */
  591. op = tif->tif_rawcp;
  592. occ = tif->tif_rawdatasize - tif->tif_rawcc;
  593. for (shft = 24; shft >= 0; shft -= 8)
  594. {
  595. const uint32_t mask = 0xffU << shft; /* find next run */
  596. for (i = 0; i < npixels; i += rc)
  597. {
  598. if (occ < 4)
  599. {
  600. tif->tif_rawcp = op;
  601. tif->tif_rawcc = tif->tif_rawdatasize - occ;
  602. if (!TIFFFlushData1(tif))
  603. return (0);
  604. op = tif->tif_rawcp;
  605. occ = tif->tif_rawdatasize - tif->tif_rawcc;
  606. }
  607. for (beg = i; beg < npixels; beg += rc)
  608. {
  609. b = tp[beg] & mask;
  610. rc = 1;
  611. while (rc < 127 + 2 && beg + rc < npixels &&
  612. (tp[beg + rc] & mask) == b)
  613. rc++;
  614. if (rc >= MINRUN)
  615. break; /* long enough */
  616. }
  617. if (beg - i > 1 && beg - i < MINRUN)
  618. {
  619. b = tp[i] & mask; /* check short run */
  620. j = i + 1;
  621. while ((tp[j++] & mask) == b)
  622. if (j == beg)
  623. {
  624. *op++ = (uint8_t)(128 - 2 + j - i);
  625. *op++ = (uint8_t)(b >> shft);
  626. occ -= 2;
  627. i = beg;
  628. break;
  629. }
  630. }
  631. while (i < beg)
  632. { /* write out non-run */
  633. if ((j = beg - i) > 127)
  634. j = 127;
  635. if (occ < j + 3)
  636. {
  637. tif->tif_rawcp = op;
  638. tif->tif_rawcc = tif->tif_rawdatasize - occ;
  639. if (!TIFFFlushData1(tif))
  640. return (0);
  641. op = tif->tif_rawcp;
  642. occ = tif->tif_rawdatasize - tif->tif_rawcc;
  643. }
  644. *op++ = (uint8_t)j;
  645. occ--;
  646. while (j--)
  647. {
  648. *op++ = (uint8_t)(tp[i++] >> shft & 0xff);
  649. occ--;
  650. }
  651. }
  652. if (rc >= MINRUN)
  653. { /* write out run */
  654. *op++ = (uint8_t)(128 - 2 + rc);
  655. *op++ = (uint8_t)(tp[beg] >> shft & 0xff);
  656. occ -= 2;
  657. }
  658. else
  659. rc = 0;
  660. }
  661. }
  662. tif->tif_rawcp = op;
  663. tif->tif_rawcc = tif->tif_rawdatasize - occ;
  664. return (1);
  665. }
  666. /*
  667. * Encode a strip of pixels. We break it into rows to
  668. * avoid encoding runs across row boundaries.
  669. */
  670. static int LogLuvEncodeStrip(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
  671. {
  672. tmsize_t rowlen = TIFFScanlineSize(tif);
  673. if (rowlen == 0)
  674. return 0;
  675. assert(cc % rowlen == 0);
  676. while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1)
  677. {
  678. bp += rowlen;
  679. cc -= rowlen;
  680. }
  681. return (cc == 0);
  682. }
  683. /*
  684. * Encode a tile of pixels. We break it into rows to
  685. * avoid encoding runs across row boundaries.
  686. */
  687. static int LogLuvEncodeTile(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
  688. {
  689. tmsize_t rowlen = TIFFTileRowSize(tif);
  690. if (rowlen == 0)
  691. return 0;
  692. assert(cc % rowlen == 0);
  693. while (cc && (*tif->tif_encoderow)(tif, bp, rowlen, s) == 1)
  694. {
  695. bp += rowlen;
  696. cc -= rowlen;
  697. }
  698. return (cc == 0);
  699. }
  700. /*
  701. * Encode/Decode functions for converting to and from user formats.
  702. */
  703. #include "uvcode.h"
  704. #ifndef UVSCALE
  705. #define U_NEU 0.210526316
  706. #define V_NEU 0.473684211
  707. #define UVSCALE 410.
  708. #endif
  709. #ifndef M_LN2
  710. #define M_LN2 0.69314718055994530942
  711. #endif
  712. #ifndef M_PI
  713. #define M_PI 3.14159265358979323846
  714. #endif
  715. #undef log2 /* Conflict with C'99 function */
  716. #define log2(x) ((1. / M_LN2) * log(x))
  717. #undef exp2 /* Conflict with C'99 function */
  718. #define exp2(x) exp(M_LN2 *(x))
  719. static int tiff_itrunc(double x, int m)
  720. {
  721. if (m == SGILOGENCODE_NODITHER)
  722. return (int)x;
  723. /* Silence CoverityScan warning about bad crypto function */
  724. /* coverity[dont_call] */
  725. return (int)(x + rand() * (1. / RAND_MAX) - .5);
  726. }
  727. #if !LOGLUV_PUBLIC
  728. static
  729. #endif
  730. double
  731. LogL16toY(int p16) /* compute luminance from 16-bit LogL */
  732. {
  733. int Le = p16 & 0x7fff;
  734. double Y;
  735. if (!Le)
  736. return (0.);
  737. Y = exp(M_LN2 / 256. * (Le + .5) - M_LN2 * 64.);
  738. return (!(p16 & 0x8000) ? Y : -Y);
  739. }
  740. #if !LOGLUV_PUBLIC
  741. static
  742. #endif
  743. int
  744. LogL16fromY(double Y, int em) /* get 16-bit LogL from Y */
  745. {
  746. if (Y >= 1.8371976e19)
  747. return (0x7fff);
  748. if (Y <= -1.8371976e19)
  749. return (0xffff);
  750. if (Y > 5.4136769e-20)
  751. return tiff_itrunc(256. * (log2(Y) + 64.), em);
  752. if (Y < -5.4136769e-20)
  753. return (~0x7fff | tiff_itrunc(256. * (log2(-Y) + 64.), em));
  754. return (0);
  755. }
  756. static void L16toY(LogLuvState *sp, uint8_t *op, tmsize_t n)
  757. {
  758. int16_t *l16 = (int16_t *)sp->tbuf;
  759. float *yp = (float *)op;
  760. while (n-- > 0)
  761. *yp++ = (float)LogL16toY(*l16++);
  762. }
  763. static void L16toGry(LogLuvState *sp, uint8_t *op, tmsize_t n)
  764. {
  765. int16_t *l16 = (int16_t *)sp->tbuf;
  766. uint8_t *gp = (uint8_t *)op;
  767. while (n-- > 0)
  768. {
  769. double Y = LogL16toY(*l16++);
  770. *gp++ = (uint8_t)((Y <= 0.) ? 0
  771. : (Y >= 1.) ? 255
  772. : (int)(256. * sqrt(Y)));
  773. }
  774. }
  775. static void L16fromY(LogLuvState *sp, uint8_t *op, tmsize_t n)
  776. {
  777. int16_t *l16 = (int16_t *)sp->tbuf;
  778. float *yp = (float *)op;
  779. while (n-- > 0)
  780. *l16++ = (int16_t)(LogL16fromY(*yp++, sp->encode_meth));
  781. }
  782. #if !LOGLUV_PUBLIC
  783. static
  784. #endif
  785. void
  786. XYZtoRGB24(float *xyz, uint8_t *rgb)
  787. {
  788. double r, g, b;
  789. /* assume CCIR-709 primaries */
  790. r = 2.690 * xyz[0] + -1.276 * xyz[1] + -0.414 * xyz[2];
  791. g = -1.022 * xyz[0] + 1.978 * xyz[1] + 0.044 * xyz[2];
  792. b = 0.061 * xyz[0] + -0.224 * xyz[1] + 1.163 * xyz[2];
  793. /* assume 2.0 gamma for speed */
  794. /* could use integer sqrt approx., but this is probably faster */
  795. rgb[0] = (uint8_t)((r <= 0.) ? 0 : (r >= 1.) ? 255 : (int)(256. * sqrt(r)));
  796. rgb[1] = (uint8_t)((g <= 0.) ? 0 : (g >= 1.) ? 255 : (int)(256. * sqrt(g)));
  797. rgb[2] = (uint8_t)((b <= 0.) ? 0 : (b >= 1.) ? 255 : (int)(256. * sqrt(b)));
  798. }
  799. #if !LOGLUV_PUBLIC
  800. static
  801. #endif
  802. double
  803. LogL10toY(int p10) /* compute luminance from 10-bit LogL */
  804. {
  805. if (p10 == 0)
  806. return (0.);
  807. return (exp(M_LN2 / 64. * (p10 + .5) - M_LN2 * 12.));
  808. }
  809. #if !LOGLUV_PUBLIC
  810. static
  811. #endif
  812. int
  813. LogL10fromY(double Y, int em) /* get 10-bit LogL from Y */
  814. {
  815. if (Y >= 15.742)
  816. return (0x3ff);
  817. else if (Y <= .00024283)
  818. return (0);
  819. else
  820. return tiff_itrunc(64. * (log2(Y) + 12.), em);
  821. }
  822. #define NANGLES 100
  823. #define uv2ang(u, v) \
  824. ((NANGLES * .499999999 / M_PI) * atan2((v)-V_NEU, (u)-U_NEU) + .5 * NANGLES)
  825. static int oog_encode(double u, double v) /* encode out-of-gamut chroma */
  826. {
  827. static int oog_table[NANGLES];
  828. static int initialized = 0;
  829. register int i;
  830. if (!initialized)
  831. { /* set up perimeter table */
  832. double eps[NANGLES], ua, va, ang, epsa;
  833. int ui, vi, ustep;
  834. for (i = NANGLES; i--;)
  835. eps[i] = 2.;
  836. for (vi = UV_NVS; vi--;)
  837. {
  838. va = UV_VSTART + (vi + .5) * UV_SQSIZ;
  839. ustep = uv_row[vi].nus - 1;
  840. if (vi == UV_NVS - 1 || vi == 0 || ustep <= 0)
  841. ustep = 1;
  842. for (ui = uv_row[vi].nus - 1; ui >= 0; ui -= ustep)
  843. {
  844. ua = uv_row[vi].ustart + (ui + .5) * UV_SQSIZ;
  845. ang = uv2ang(ua, va);
  846. i = (int)ang;
  847. epsa = fabs(ang - (i + .5));
  848. if (epsa < eps[i])
  849. {
  850. oog_table[i] = uv_row[vi].ncum + ui;
  851. eps[i] = epsa;
  852. }
  853. }
  854. }
  855. for (i = NANGLES; i--;) /* fill any holes */
  856. if (eps[i] > 1.5)
  857. {
  858. int i1, i2;
  859. for (i1 = 1; i1 < NANGLES / 2; i1++)
  860. if (eps[(i + i1) % NANGLES] < 1.5)
  861. break;
  862. for (i2 = 1; i2 < NANGLES / 2; i2++)
  863. if (eps[(i + NANGLES - i2) % NANGLES] < 1.5)
  864. break;
  865. if (i1 < i2)
  866. oog_table[i] = oog_table[(i + i1) % NANGLES];
  867. else
  868. oog_table[i] = oog_table[(i + NANGLES - i2) % NANGLES];
  869. }
  870. initialized = 1;
  871. }
  872. i = (int)uv2ang(u, v); /* look up hue angle */
  873. return (oog_table[i]);
  874. }
  875. #undef uv2ang
  876. #undef NANGLES
  877. #if !LOGLUV_PUBLIC
  878. static
  879. #endif
  880. int
  881. uv_encode(double u, double v, int em) /* encode (u',v') coordinates */
  882. {
  883. unsigned int vi;
  884. int ui;
  885. /* check for NaN */
  886. if (u != u || v != v)
  887. {
  888. u = U_NEU;
  889. v = V_NEU;
  890. }
  891. if (v < UV_VSTART)
  892. return oog_encode(u, v);
  893. vi = tiff_itrunc((v - UV_VSTART) * (1. / UV_SQSIZ), em);
  894. if (vi >= UV_NVS)
  895. return oog_encode(u, v);
  896. if (u < uv_row[vi].ustart)
  897. return oog_encode(u, v);
  898. ui = tiff_itrunc((u - uv_row[vi].ustart) * (1. / UV_SQSIZ), em);
  899. if (ui >= uv_row[vi].nus)
  900. return oog_encode(u, v);
  901. return (uv_row[vi].ncum + ui);
  902. }
  903. #if !LOGLUV_PUBLIC
  904. static
  905. #endif
  906. int
  907. uv_decode(double *up, double *vp, int c) /* decode (u',v') index */
  908. {
  909. unsigned int upper, lower;
  910. int ui;
  911. unsigned int vi;
  912. if (c < 0 || c >= UV_NDIVS)
  913. return (-1);
  914. lower = 0; /* binary search */
  915. upper = UV_NVS;
  916. while (upper - lower > 1)
  917. {
  918. vi = (lower + upper) >> 1;
  919. ui = c - uv_row[vi].ncum;
  920. if (ui > 0)
  921. lower = vi;
  922. else if (ui < 0)
  923. upper = vi;
  924. else
  925. {
  926. lower = vi;
  927. break;
  928. }
  929. }
  930. vi = lower;
  931. ui = c - uv_row[vi].ncum;
  932. *up = uv_row[vi].ustart + (ui + .5) * UV_SQSIZ;
  933. *vp = UV_VSTART + (vi + .5) * UV_SQSIZ;
  934. return (0);
  935. }
  936. #if !LOGLUV_PUBLIC
  937. static
  938. #endif
  939. void
  940. LogLuv24toXYZ(uint32_t p, float *XYZ)
  941. {
  942. int Ce;
  943. double L, u, v, s, x, y;
  944. /* decode luminance */
  945. L = LogL10toY(p >> 14 & 0x3ff);
  946. if (L <= 0.)
  947. {
  948. XYZ[0] = XYZ[1] = XYZ[2] = 0.;
  949. return;
  950. }
  951. /* decode color */
  952. Ce = p & 0x3fff;
  953. if (uv_decode(&u, &v, Ce) < 0)
  954. {
  955. u = U_NEU;
  956. v = V_NEU;
  957. }
  958. s = 1. / (6. * u - 16. * v + 12.);
  959. x = 9. * u * s;
  960. y = 4. * v * s;
  961. /* convert to XYZ */
  962. XYZ[0] = (float)(x / y * L);
  963. XYZ[1] = (float)L;
  964. XYZ[2] = (float)((1. - x - y) / y * L);
  965. }
  966. #if !LOGLUV_PUBLIC
  967. static
  968. #endif
  969. uint32_t
  970. LogLuv24fromXYZ(float *XYZ, int em)
  971. {
  972. int Le, Ce;
  973. double u, v, s;
  974. /* encode luminance */
  975. Le = LogL10fromY(XYZ[1], em);
  976. /* encode color */
  977. s = XYZ[0] + 15. * XYZ[1] + 3. * XYZ[2];
  978. if (!Le || s <= 0.)
  979. {
  980. u = U_NEU;
  981. v = V_NEU;
  982. }
  983. else
  984. {
  985. u = 4. * XYZ[0] / s;
  986. v = 9. * XYZ[1] / s;
  987. }
  988. Ce = uv_encode(u, v, em);
  989. if (Ce < 0) /* never happens */
  990. Ce = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER);
  991. /* combine encodings */
  992. return (Le << 14 | Ce);
  993. }
  994. static void Luv24toXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
  995. {
  996. uint32_t *luv = (uint32_t *)sp->tbuf;
  997. float *xyz = (float *)op;
  998. while (n-- > 0)
  999. {
  1000. LogLuv24toXYZ(*luv, xyz);
  1001. xyz += 3;
  1002. luv++;
  1003. }
  1004. }
  1005. static void Luv24toLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1006. {
  1007. uint32_t *luv = (uint32_t *)sp->tbuf;
  1008. int16_t *luv3 = (int16_t *)op;
  1009. while (n-- > 0)
  1010. {
  1011. double u, v;
  1012. *luv3++ = (int16_t)((*luv >> 12 & 0xffd) + 13314);
  1013. if (uv_decode(&u, &v, *luv & 0x3fff) < 0)
  1014. {
  1015. u = U_NEU;
  1016. v = V_NEU;
  1017. }
  1018. *luv3++ = (int16_t)(u * (1L << 15));
  1019. *luv3++ = (int16_t)(v * (1L << 15));
  1020. luv++;
  1021. }
  1022. }
  1023. static void Luv24toRGB(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1024. {
  1025. uint32_t *luv = (uint32_t *)sp->tbuf;
  1026. uint8_t *rgb = (uint8_t *)op;
  1027. while (n-- > 0)
  1028. {
  1029. float xyz[3];
  1030. LogLuv24toXYZ(*luv++, xyz);
  1031. XYZtoRGB24(xyz, rgb);
  1032. rgb += 3;
  1033. }
  1034. }
  1035. static void Luv24fromXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1036. {
  1037. uint32_t *luv = (uint32_t *)sp->tbuf;
  1038. float *xyz = (float *)op;
  1039. while (n-- > 0)
  1040. {
  1041. *luv++ = LogLuv24fromXYZ(xyz, sp->encode_meth);
  1042. xyz += 3;
  1043. }
  1044. }
  1045. static void Luv24fromLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1046. {
  1047. uint32_t *luv = (uint32_t *)sp->tbuf;
  1048. int16_t *luv3 = (int16_t *)op;
  1049. while (n-- > 0)
  1050. {
  1051. int Le, Ce;
  1052. if (luv3[0] <= 0)
  1053. Le = 0;
  1054. else if (luv3[0] >= (1 << 12) + 3314)
  1055. Le = (1 << 10) - 1;
  1056. else if (sp->encode_meth == SGILOGENCODE_NODITHER)
  1057. Le = (luv3[0] - 3314) >> 2;
  1058. else
  1059. Le = tiff_itrunc(.25 * (luv3[0] - 3314.), sp->encode_meth);
  1060. Ce = uv_encode((luv3[1] + .5) / (1 << 15), (luv3[2] + .5) / (1 << 15),
  1061. sp->encode_meth);
  1062. if (Ce < 0) /* never happens */
  1063. Ce = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER);
  1064. *luv++ = (uint32_t)Le << 14 | Ce;
  1065. luv3 += 3;
  1066. }
  1067. }
  1068. #if !LOGLUV_PUBLIC
  1069. static
  1070. #endif
  1071. void
  1072. LogLuv32toXYZ(uint32_t p, float *XYZ)
  1073. {
  1074. double L, u, v, s, x, y;
  1075. /* decode luminance */
  1076. L = LogL16toY((int)p >> 16);
  1077. if (L <= 0.)
  1078. {
  1079. XYZ[0] = XYZ[1] = XYZ[2] = 0.;
  1080. return;
  1081. }
  1082. /* decode color */
  1083. u = 1. / UVSCALE * ((p >> 8 & 0xff) + .5);
  1084. v = 1. / UVSCALE * ((p & 0xff) + .5);
  1085. s = 1. / (6. * u - 16. * v + 12.);
  1086. x = 9. * u * s;
  1087. y = 4. * v * s;
  1088. /* convert to XYZ */
  1089. XYZ[0] = (float)(x / y * L);
  1090. XYZ[1] = (float)L;
  1091. XYZ[2] = (float)((1. - x - y) / y * L);
  1092. }
  1093. #if !LOGLUV_PUBLIC
  1094. static
  1095. #endif
  1096. uint32_t
  1097. LogLuv32fromXYZ(float *XYZ, int em)
  1098. {
  1099. unsigned int Le, ue, ve;
  1100. double u, v, s;
  1101. /* encode luminance */
  1102. Le = (unsigned int)LogL16fromY(XYZ[1], em);
  1103. /* encode color */
  1104. s = XYZ[0] + 15. * XYZ[1] + 3. * XYZ[2];
  1105. if (!Le || s <= 0.)
  1106. {
  1107. u = U_NEU;
  1108. v = V_NEU;
  1109. }
  1110. else
  1111. {
  1112. u = 4. * XYZ[0] / s;
  1113. v = 9. * XYZ[1] / s;
  1114. }
  1115. if (u <= 0.)
  1116. ue = 0;
  1117. else
  1118. ue = tiff_itrunc(UVSCALE * u, em);
  1119. if (ue > 255)
  1120. ue = 255;
  1121. if (v <= 0.)
  1122. ve = 0;
  1123. else
  1124. ve = tiff_itrunc(UVSCALE * v, em);
  1125. if (ve > 255)
  1126. ve = 255;
  1127. /* combine encodings */
  1128. return (Le << 16 | ue << 8 | ve);
  1129. }
  1130. static void Luv32toXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1131. {
  1132. uint32_t *luv = (uint32_t *)sp->tbuf;
  1133. float *xyz = (float *)op;
  1134. while (n-- > 0)
  1135. {
  1136. LogLuv32toXYZ(*luv++, xyz);
  1137. xyz += 3;
  1138. }
  1139. }
  1140. static void Luv32toLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1141. {
  1142. uint32_t *luv = (uint32_t *)sp->tbuf;
  1143. int16_t *luv3 = (int16_t *)op;
  1144. while (n-- > 0)
  1145. {
  1146. double u, v;
  1147. *luv3++ = (int16_t)(*luv >> 16);
  1148. u = 1. / UVSCALE * ((*luv >> 8 & 0xff) + .5);
  1149. v = 1. / UVSCALE * ((*luv & 0xff) + .5);
  1150. *luv3++ = (int16_t)(u * (1L << 15));
  1151. *luv3++ = (int16_t)(v * (1L << 15));
  1152. luv++;
  1153. }
  1154. }
  1155. static void Luv32toRGB(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1156. {
  1157. uint32_t *luv = (uint32_t *)sp->tbuf;
  1158. uint8_t *rgb = (uint8_t *)op;
  1159. while (n-- > 0)
  1160. {
  1161. float xyz[3];
  1162. LogLuv32toXYZ(*luv++, xyz);
  1163. XYZtoRGB24(xyz, rgb);
  1164. rgb += 3;
  1165. }
  1166. }
  1167. static void Luv32fromXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1168. {
  1169. uint32_t *luv = (uint32_t *)sp->tbuf;
  1170. float *xyz = (float *)op;
  1171. while (n-- > 0)
  1172. {
  1173. *luv++ = LogLuv32fromXYZ(xyz, sp->encode_meth);
  1174. xyz += 3;
  1175. }
  1176. }
  1177. static void Luv32fromLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1178. {
  1179. uint32_t *luv = (uint32_t *)sp->tbuf;
  1180. int16_t *luv3 = (int16_t *)op;
  1181. if (sp->encode_meth == SGILOGENCODE_NODITHER)
  1182. {
  1183. while (n-- > 0)
  1184. {
  1185. *luv++ = (uint32_t)luv3[0] << 16 |
  1186. (luv3[1] * (uint32_t)(UVSCALE + .5) >> 7 & 0xff00) |
  1187. (luv3[2] * (uint32_t)(UVSCALE + .5) >> 15 & 0xff);
  1188. luv3 += 3;
  1189. }
  1190. return;
  1191. }
  1192. while (n-- > 0)
  1193. {
  1194. *luv++ =
  1195. (uint32_t)luv3[0] << 16 |
  1196. (tiff_itrunc(luv3[1] * (UVSCALE / (1 << 15)), sp->encode_meth)
  1197. << 8 &
  1198. 0xff00) |
  1199. (tiff_itrunc(luv3[2] * (UVSCALE / (1 << 15)), sp->encode_meth) &
  1200. 0xff);
  1201. luv3 += 3;
  1202. }
  1203. }
  1204. static void _logLuvNop(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1205. {
  1206. (void)sp;
  1207. (void)op;
  1208. (void)n;
  1209. }
  1210. static int LogL16GuessDataFmt(TIFFDirectory *td)
  1211. {
  1212. #define PACK(s, b, f) (((b) << 6) | ((s) << 3) | (f))
  1213. switch (
  1214. PACK(td->td_samplesperpixel, td->td_bitspersample, td->td_sampleformat))
  1215. {
  1216. case PACK(1, 32, SAMPLEFORMAT_IEEEFP):
  1217. return (SGILOGDATAFMT_FLOAT);
  1218. case PACK(1, 16, SAMPLEFORMAT_VOID):
  1219. case PACK(1, 16, SAMPLEFORMAT_INT):
  1220. case PACK(1, 16, SAMPLEFORMAT_UINT):
  1221. return (SGILOGDATAFMT_16BIT);
  1222. case PACK(1, 8, SAMPLEFORMAT_VOID):
  1223. case PACK(1, 8, SAMPLEFORMAT_UINT):
  1224. return (SGILOGDATAFMT_8BIT);
  1225. }
  1226. #undef PACK
  1227. return (SGILOGDATAFMT_UNKNOWN);
  1228. }
  1229. static tmsize_t multiply_ms(tmsize_t m1, tmsize_t m2)
  1230. {
  1231. return _TIFFMultiplySSize(NULL, m1, m2, NULL);
  1232. }
  1233. static int LogL16InitState(TIFF *tif)
  1234. {
  1235. static const char module[] = "LogL16InitState";
  1236. TIFFDirectory *td = &tif->tif_dir;
  1237. LogLuvState *sp = DecoderState(tif);
  1238. assert(sp != NULL);
  1239. assert(td->td_photometric == PHOTOMETRIC_LOGL);
  1240. if (td->td_samplesperpixel != 1)
  1241. {
  1242. TIFFErrorExtR(tif, module,
  1243. "Sorry, can not handle LogL image with %s=%" PRIu16,
  1244. "Samples/pixel", td->td_samplesperpixel);
  1245. return 0;
  1246. }
  1247. /* for some reason, we can't do this in TIFFInitLogL16 */
  1248. if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN)
  1249. sp->user_datafmt = LogL16GuessDataFmt(td);
  1250. switch (sp->user_datafmt)
  1251. {
  1252. case SGILOGDATAFMT_FLOAT:
  1253. sp->pixel_size = sizeof(float);
  1254. break;
  1255. case SGILOGDATAFMT_16BIT:
  1256. sp->pixel_size = sizeof(int16_t);
  1257. break;
  1258. case SGILOGDATAFMT_8BIT:
  1259. sp->pixel_size = sizeof(uint8_t);
  1260. break;
  1261. default:
  1262. TIFFErrorExtR(tif, module,
  1263. "No support for converting user data format to LogL");
  1264. return (0);
  1265. }
  1266. if (isTiled(tif))
  1267. sp->tbuflen = multiply_ms(td->td_tilewidth, td->td_tilelength);
  1268. else if (td->td_rowsperstrip < td->td_imagelength)
  1269. sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_rowsperstrip);
  1270. else
  1271. sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_imagelength);
  1272. if (multiply_ms(sp->tbuflen, sizeof(int16_t)) == 0 ||
  1273. (sp->tbuf = (uint8_t *)_TIFFmallocExt(
  1274. tif, sp->tbuflen * sizeof(int16_t))) == NULL)
  1275. {
  1276. TIFFErrorExtR(tif, module, "No space for SGILog translation buffer");
  1277. return (0);
  1278. }
  1279. return (1);
  1280. }
  1281. static int LogLuvGuessDataFmt(TIFFDirectory *td)
  1282. {
  1283. int guess;
  1284. /*
  1285. * If the user didn't tell us their datafmt,
  1286. * take our best guess from the bitspersample.
  1287. */
  1288. #define PACK(a, b) (((a) << 3) | (b))
  1289. switch (PACK(td->td_bitspersample, td->td_sampleformat))
  1290. {
  1291. case PACK(32, SAMPLEFORMAT_IEEEFP):
  1292. guess = SGILOGDATAFMT_FLOAT;
  1293. break;
  1294. case PACK(32, SAMPLEFORMAT_VOID):
  1295. case PACK(32, SAMPLEFORMAT_UINT):
  1296. case PACK(32, SAMPLEFORMAT_INT):
  1297. guess = SGILOGDATAFMT_RAW;
  1298. break;
  1299. case PACK(16, SAMPLEFORMAT_VOID):
  1300. case PACK(16, SAMPLEFORMAT_INT):
  1301. case PACK(16, SAMPLEFORMAT_UINT):
  1302. guess = SGILOGDATAFMT_16BIT;
  1303. break;
  1304. case PACK(8, SAMPLEFORMAT_VOID):
  1305. case PACK(8, SAMPLEFORMAT_UINT):
  1306. guess = SGILOGDATAFMT_8BIT;
  1307. break;
  1308. default:
  1309. guess = SGILOGDATAFMT_UNKNOWN;
  1310. break;
  1311. #undef PACK
  1312. }
  1313. /*
  1314. * Double-check samples per pixel.
  1315. */
  1316. switch (td->td_samplesperpixel)
  1317. {
  1318. case 1:
  1319. if (guess != SGILOGDATAFMT_RAW)
  1320. guess = SGILOGDATAFMT_UNKNOWN;
  1321. break;
  1322. case 3:
  1323. if (guess == SGILOGDATAFMT_RAW)
  1324. guess = SGILOGDATAFMT_UNKNOWN;
  1325. break;
  1326. default:
  1327. guess = SGILOGDATAFMT_UNKNOWN;
  1328. break;
  1329. }
  1330. return (guess);
  1331. }
  1332. static int LogLuvInitState(TIFF *tif)
  1333. {
  1334. static const char module[] = "LogLuvInitState";
  1335. TIFFDirectory *td = &tif->tif_dir;
  1336. LogLuvState *sp = DecoderState(tif);
  1337. assert(sp != NULL);
  1338. assert(td->td_photometric == PHOTOMETRIC_LOGLUV);
  1339. /* for some reason, we can't do this in TIFFInitLogLuv */
  1340. if (td->td_planarconfig != PLANARCONFIG_CONTIG)
  1341. {
  1342. TIFFErrorExtR(tif, module,
  1343. "SGILog compression cannot handle non-contiguous data");
  1344. return (0);
  1345. }
  1346. if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN)
  1347. sp->user_datafmt = LogLuvGuessDataFmt(td);
  1348. switch (sp->user_datafmt)
  1349. {
  1350. case SGILOGDATAFMT_FLOAT:
  1351. sp->pixel_size = 3 * sizeof(float);
  1352. break;
  1353. case SGILOGDATAFMT_16BIT:
  1354. sp->pixel_size = 3 * sizeof(int16_t);
  1355. break;
  1356. case SGILOGDATAFMT_RAW:
  1357. sp->pixel_size = sizeof(uint32_t);
  1358. break;
  1359. case SGILOGDATAFMT_8BIT:
  1360. sp->pixel_size = 3 * sizeof(uint8_t);
  1361. break;
  1362. default:
  1363. TIFFErrorExtR(
  1364. tif, module,
  1365. "No support for converting user data format to LogLuv");
  1366. return (0);
  1367. }
  1368. if (isTiled(tif))
  1369. sp->tbuflen = multiply_ms(td->td_tilewidth, td->td_tilelength);
  1370. else if (td->td_rowsperstrip < td->td_imagelength)
  1371. sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_rowsperstrip);
  1372. else
  1373. sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_imagelength);
  1374. if (multiply_ms(sp->tbuflen, sizeof(uint32_t)) == 0 ||
  1375. (sp->tbuf = (uint8_t *)_TIFFmallocExt(
  1376. tif, sp->tbuflen * sizeof(uint32_t))) == NULL)
  1377. {
  1378. TIFFErrorExtR(tif, module, "No space for SGILog translation buffer");
  1379. return (0);
  1380. }
  1381. return (1);
  1382. }
  1383. static int LogLuvFixupTags(TIFF *tif)
  1384. {
  1385. (void)tif;
  1386. return (1);
  1387. }
  1388. static int LogLuvSetupDecode(TIFF *tif)
  1389. {
  1390. static const char module[] = "LogLuvSetupDecode";
  1391. LogLuvState *sp = DecoderState(tif);
  1392. TIFFDirectory *td = &tif->tif_dir;
  1393. tif->tif_postdecode = _TIFFNoPostDecode;
  1394. switch (td->td_photometric)
  1395. {
  1396. case PHOTOMETRIC_LOGLUV:
  1397. if (!LogLuvInitState(tif))
  1398. break;
  1399. if (td->td_compression == COMPRESSION_SGILOG24)
  1400. {
  1401. tif->tif_decoderow = LogLuvDecode24;
  1402. switch (sp->user_datafmt)
  1403. {
  1404. case SGILOGDATAFMT_FLOAT:
  1405. sp->tfunc = Luv24toXYZ;
  1406. break;
  1407. case SGILOGDATAFMT_16BIT:
  1408. sp->tfunc = Luv24toLuv48;
  1409. break;
  1410. case SGILOGDATAFMT_8BIT:
  1411. sp->tfunc = Luv24toRGB;
  1412. break;
  1413. }
  1414. }
  1415. else
  1416. {
  1417. tif->tif_decoderow = LogLuvDecode32;
  1418. switch (sp->user_datafmt)
  1419. {
  1420. case SGILOGDATAFMT_FLOAT:
  1421. sp->tfunc = Luv32toXYZ;
  1422. break;
  1423. case SGILOGDATAFMT_16BIT:
  1424. sp->tfunc = Luv32toLuv48;
  1425. break;
  1426. case SGILOGDATAFMT_8BIT:
  1427. sp->tfunc = Luv32toRGB;
  1428. break;
  1429. }
  1430. }
  1431. return (1);
  1432. case PHOTOMETRIC_LOGL:
  1433. if (!LogL16InitState(tif))
  1434. break;
  1435. tif->tif_decoderow = LogL16Decode;
  1436. switch (sp->user_datafmt)
  1437. {
  1438. case SGILOGDATAFMT_FLOAT:
  1439. sp->tfunc = L16toY;
  1440. break;
  1441. case SGILOGDATAFMT_8BIT:
  1442. sp->tfunc = L16toGry;
  1443. break;
  1444. }
  1445. return (1);
  1446. default:
  1447. TIFFErrorExtR(tif, module,
  1448. "Inappropriate photometric interpretation %" PRIu16
  1449. " for SGILog compression; %s",
  1450. td->td_photometric, "must be either LogLUV or LogL");
  1451. break;
  1452. }
  1453. return (0);
  1454. }
  1455. static int LogLuvSetupEncode(TIFF *tif)
  1456. {
  1457. static const char module[] = "LogLuvSetupEncode";
  1458. LogLuvState *sp = EncoderState(tif);
  1459. TIFFDirectory *td = &tif->tif_dir;
  1460. switch (td->td_photometric)
  1461. {
  1462. case PHOTOMETRIC_LOGLUV:
  1463. if (!LogLuvInitState(tif))
  1464. return (0);
  1465. if (td->td_compression == COMPRESSION_SGILOG24)
  1466. {
  1467. tif->tif_encoderow = LogLuvEncode24;
  1468. switch (sp->user_datafmt)
  1469. {
  1470. case SGILOGDATAFMT_FLOAT:
  1471. sp->tfunc = Luv24fromXYZ;
  1472. break;
  1473. case SGILOGDATAFMT_16BIT:
  1474. sp->tfunc = Luv24fromLuv48;
  1475. break;
  1476. case SGILOGDATAFMT_RAW:
  1477. break;
  1478. default:
  1479. goto notsupported;
  1480. }
  1481. }
  1482. else
  1483. {
  1484. tif->tif_encoderow = LogLuvEncode32;
  1485. switch (sp->user_datafmt)
  1486. {
  1487. case SGILOGDATAFMT_FLOAT:
  1488. sp->tfunc = Luv32fromXYZ;
  1489. break;
  1490. case SGILOGDATAFMT_16BIT:
  1491. sp->tfunc = Luv32fromLuv48;
  1492. break;
  1493. case SGILOGDATAFMT_RAW:
  1494. break;
  1495. default:
  1496. goto notsupported;
  1497. }
  1498. }
  1499. break;
  1500. case PHOTOMETRIC_LOGL:
  1501. if (!LogL16InitState(tif))
  1502. return (0);
  1503. tif->tif_encoderow = LogL16Encode;
  1504. switch (sp->user_datafmt)
  1505. {
  1506. case SGILOGDATAFMT_FLOAT:
  1507. sp->tfunc = L16fromY;
  1508. break;
  1509. case SGILOGDATAFMT_16BIT:
  1510. break;
  1511. default:
  1512. goto notsupported;
  1513. }
  1514. break;
  1515. default:
  1516. TIFFErrorExtR(tif, module,
  1517. "Inappropriate photometric interpretation %" PRIu16
  1518. " for SGILog compression; %s",
  1519. td->td_photometric, "must be either LogLUV or LogL");
  1520. return (0);
  1521. }
  1522. sp->encoder_state = 1;
  1523. return (1);
  1524. notsupported:
  1525. TIFFErrorExtR(tif, module,
  1526. "SGILog compression supported only for %s, or raw data",
  1527. td->td_photometric == PHOTOMETRIC_LOGL ? "Y, L" : "XYZ, Luv");
  1528. return (0);
  1529. }
  1530. static void LogLuvClose(TIFF *tif)
  1531. {
  1532. LogLuvState *sp = (LogLuvState *)tif->tif_data;
  1533. TIFFDirectory *td = &tif->tif_dir;
  1534. assert(sp != 0);
  1535. /*
  1536. * For consistency, we always want to write out the same
  1537. * bitspersample and sampleformat for our TIFF file,
  1538. * regardless of the data format being used by the application.
  1539. * Since this routine is called after tags have been set but
  1540. * before they have been recorded in the file, we reset them here.
  1541. * Note: this is really a nasty approach. See PixarLogClose
  1542. */
  1543. if (sp->encoder_state)
  1544. {
  1545. /* See PixarLogClose. Might avoid issues with tags whose size depends
  1546. * on those below, but not completely sure this is enough. */
  1547. td->td_samplesperpixel =
  1548. (td->td_photometric == PHOTOMETRIC_LOGL) ? 1 : 3;
  1549. td->td_bitspersample = 16;
  1550. td->td_sampleformat = SAMPLEFORMAT_INT;
  1551. }
  1552. }
  1553. static void LogLuvCleanup(TIFF *tif)
  1554. {
  1555. LogLuvState *sp = (LogLuvState *)tif->tif_data;
  1556. assert(sp != 0);
  1557. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  1558. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  1559. if (sp->tbuf)
  1560. _TIFFfreeExt(tif, sp->tbuf);
  1561. _TIFFfreeExt(tif, sp);
  1562. tif->tif_data = NULL;
  1563. _TIFFSetDefaultCompressionState(tif);
  1564. }
  1565. static int LogLuvVSetField(TIFF *tif, uint32_t tag, va_list ap)
  1566. {
  1567. static const char module[] = "LogLuvVSetField";
  1568. LogLuvState *sp = DecoderState(tif);
  1569. int bps, fmt;
  1570. switch (tag)
  1571. {
  1572. case TIFFTAG_SGILOGDATAFMT:
  1573. sp->user_datafmt = (int)va_arg(ap, int);
  1574. /*
  1575. * Tweak the TIFF header so that the rest of libtiff knows what
  1576. * size of data will be passed between app and library, and
  1577. * assume that the app knows what it is doing and is not
  1578. * confused by these header manipulations...
  1579. */
  1580. switch (sp->user_datafmt)
  1581. {
  1582. case SGILOGDATAFMT_FLOAT:
  1583. bps = 32;
  1584. fmt = SAMPLEFORMAT_IEEEFP;
  1585. break;
  1586. case SGILOGDATAFMT_16BIT:
  1587. bps = 16;
  1588. fmt = SAMPLEFORMAT_INT;
  1589. break;
  1590. case SGILOGDATAFMT_RAW:
  1591. bps = 32;
  1592. fmt = SAMPLEFORMAT_UINT;
  1593. TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
  1594. break;
  1595. case SGILOGDATAFMT_8BIT:
  1596. bps = 8;
  1597. fmt = SAMPLEFORMAT_UINT;
  1598. break;
  1599. default:
  1600. TIFFErrorExtR(
  1601. tif, tif->tif_name,
  1602. "Unknown data format %d for LogLuv compression",
  1603. sp->user_datafmt);
  1604. return (0);
  1605. }
  1606. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
  1607. TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, fmt);
  1608. /*
  1609. * Must recalculate sizes should bits/sample change.
  1610. */
  1611. tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)-1;
  1612. tif->tif_scanlinesize = TIFFScanlineSize(tif);
  1613. return (1);
  1614. case TIFFTAG_SGILOGENCODE:
  1615. sp->encode_meth = (int)va_arg(ap, int);
  1616. if (sp->encode_meth != SGILOGENCODE_NODITHER &&
  1617. sp->encode_meth != SGILOGENCODE_RANDITHER)
  1618. {
  1619. TIFFErrorExtR(tif, module,
  1620. "Unknown encoding %d for LogLuv compression",
  1621. sp->encode_meth);
  1622. return (0);
  1623. }
  1624. return (1);
  1625. default:
  1626. return (*sp->vsetparent)(tif, tag, ap);
  1627. }
  1628. }
  1629. static int LogLuvVGetField(TIFF *tif, uint32_t tag, va_list ap)
  1630. {
  1631. LogLuvState *sp = (LogLuvState *)tif->tif_data;
  1632. switch (tag)
  1633. {
  1634. case TIFFTAG_SGILOGDATAFMT:
  1635. *va_arg(ap, int *) = sp->user_datafmt;
  1636. return (1);
  1637. default:
  1638. return (*sp->vgetparent)(tif, tag, ap);
  1639. }
  1640. }
  1641. static const TIFFField LogLuvFields[] = {
  1642. {TIFFTAG_SGILOGDATAFMT, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_INT,
  1643. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "SGILogDataFmt", NULL},
  1644. {TIFFTAG_SGILOGENCODE, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_INT,
  1645. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "SGILogEncode", NULL}};
  1646. int TIFFInitSGILog(TIFF *tif, int scheme)
  1647. {
  1648. static const char module[] = "TIFFInitSGILog";
  1649. LogLuvState *sp;
  1650. assert(scheme == COMPRESSION_SGILOG24 || scheme == COMPRESSION_SGILOG);
  1651. /*
  1652. * Merge codec-specific tag information.
  1653. */
  1654. if (!_TIFFMergeFields(tif, LogLuvFields, TIFFArrayCount(LogLuvFields)))
  1655. {
  1656. TIFFErrorExtR(tif, module, "Merging SGILog codec-specific tags failed");
  1657. return 0;
  1658. }
  1659. /*
  1660. * Allocate state block so tag methods have storage to record values.
  1661. */
  1662. tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(LogLuvState));
  1663. if (tif->tif_data == NULL)
  1664. goto bad;
  1665. sp = (LogLuvState *)tif->tif_data;
  1666. _TIFFmemset((void *)sp, 0, sizeof(*sp));
  1667. sp->user_datafmt = SGILOGDATAFMT_UNKNOWN;
  1668. sp->encode_meth = (scheme == COMPRESSION_SGILOG24) ? SGILOGENCODE_RANDITHER
  1669. : SGILOGENCODE_NODITHER;
  1670. sp->tfunc = _logLuvNop;
  1671. /*
  1672. * Install codec methods.
  1673. * NB: tif_decoderow & tif_encoderow are filled
  1674. * in at setup time.
  1675. */
  1676. tif->tif_fixuptags = LogLuvFixupTags;
  1677. tif->tif_setupdecode = LogLuvSetupDecode;
  1678. tif->tif_decodestrip = LogLuvDecodeStrip;
  1679. tif->tif_decodetile = LogLuvDecodeTile;
  1680. tif->tif_setupencode = LogLuvSetupEncode;
  1681. tif->tif_encodestrip = LogLuvEncodeStrip;
  1682. tif->tif_encodetile = LogLuvEncodeTile;
  1683. tif->tif_close = LogLuvClose;
  1684. tif->tif_cleanup = LogLuvCleanup;
  1685. /*
  1686. * Override parent get/set field methods.
  1687. */
  1688. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  1689. tif->tif_tagmethods.vgetfield = LogLuvVGetField; /* hook for codec tags */
  1690. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  1691. tif->tif_tagmethods.vsetfield = LogLuvVSetField; /* hook for codec tags */
  1692. return (1);
  1693. bad:
  1694. TIFFErrorExtR(tif, module, "%s: No space for LogLuv state block",
  1695. tif->tif_name);
  1696. return (0);
  1697. }
  1698. #endif /* LOGLUV_SUPPORT */