tif_luv.c 53 KB

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