tif_luv.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  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. register int vi, ui;
  884. /* check for NaN */
  885. if (u != u || v != v)
  886. {
  887. u = U_NEU;
  888. v = V_NEU;
  889. }
  890. if (v < UV_VSTART)
  891. return oog_encode(u, v);
  892. vi = tiff_itrunc((v - UV_VSTART) * (1. / UV_SQSIZ), em);
  893. if (vi >= UV_NVS)
  894. return oog_encode(u, v);
  895. if (u < uv_row[vi].ustart)
  896. return oog_encode(u, v);
  897. ui = tiff_itrunc((u - uv_row[vi].ustart) * (1. / UV_SQSIZ), em);
  898. if (ui >= uv_row[vi].nus)
  899. return oog_encode(u, v);
  900. return (uv_row[vi].ncum + ui);
  901. }
  902. #if !LOGLUV_PUBLIC
  903. static
  904. #endif
  905. int
  906. uv_decode(double *up, double *vp, int c) /* decode (u',v') index */
  907. {
  908. int upper, lower;
  909. register int ui, vi;
  910. if (c < 0 || c >= UV_NDIVS)
  911. return (-1);
  912. lower = 0; /* binary search */
  913. upper = UV_NVS;
  914. while (upper - lower > 1)
  915. {
  916. vi = (lower + upper) >> 1;
  917. ui = c - uv_row[vi].ncum;
  918. if (ui > 0)
  919. lower = vi;
  920. else if (ui < 0)
  921. upper = vi;
  922. else
  923. {
  924. lower = vi;
  925. break;
  926. }
  927. }
  928. vi = lower;
  929. ui = c - uv_row[vi].ncum;
  930. *up = uv_row[vi].ustart + (ui + .5) * UV_SQSIZ;
  931. *vp = UV_VSTART + (vi + .5) * UV_SQSIZ;
  932. return (0);
  933. }
  934. #if !LOGLUV_PUBLIC
  935. static
  936. #endif
  937. void
  938. LogLuv24toXYZ(uint32_t p, float *XYZ)
  939. {
  940. int Ce;
  941. double L, u, v, s, x, y;
  942. /* decode luminance */
  943. L = LogL10toY(p >> 14 & 0x3ff);
  944. if (L <= 0.)
  945. {
  946. XYZ[0] = XYZ[1] = XYZ[2] = 0.;
  947. return;
  948. }
  949. /* decode color */
  950. Ce = p & 0x3fff;
  951. if (uv_decode(&u, &v, Ce) < 0)
  952. {
  953. u = U_NEU;
  954. v = V_NEU;
  955. }
  956. s = 1. / (6. * u - 16. * v + 12.);
  957. x = 9. * u * s;
  958. y = 4. * v * s;
  959. /* convert to XYZ */
  960. XYZ[0] = (float)(x / y * L);
  961. XYZ[1] = (float)L;
  962. XYZ[2] = (float)((1. - x - y) / y * L);
  963. }
  964. #if !LOGLUV_PUBLIC
  965. static
  966. #endif
  967. uint32_t
  968. LogLuv24fromXYZ(float *XYZ, int em)
  969. {
  970. int Le, Ce;
  971. double u, v, s;
  972. /* encode luminance */
  973. Le = LogL10fromY(XYZ[1], em);
  974. /* encode color */
  975. s = XYZ[0] + 15. * XYZ[1] + 3. * XYZ[2];
  976. if (!Le || s <= 0.)
  977. {
  978. u = U_NEU;
  979. v = V_NEU;
  980. }
  981. else
  982. {
  983. u = 4. * XYZ[0] / s;
  984. v = 9. * XYZ[1] / s;
  985. }
  986. Ce = uv_encode(u, v, em);
  987. if (Ce < 0) /* never happens */
  988. Ce = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER);
  989. /* combine encodings */
  990. return (Le << 14 | Ce);
  991. }
  992. static void Luv24toXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
  993. {
  994. uint32_t *luv = (uint32_t *)sp->tbuf;
  995. float *xyz = (float *)op;
  996. while (n-- > 0)
  997. {
  998. LogLuv24toXYZ(*luv, xyz);
  999. xyz += 3;
  1000. luv++;
  1001. }
  1002. }
  1003. static void Luv24toLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1004. {
  1005. uint32_t *luv = (uint32_t *)sp->tbuf;
  1006. int16_t *luv3 = (int16_t *)op;
  1007. while (n-- > 0)
  1008. {
  1009. double u, v;
  1010. *luv3++ = (int16_t)((*luv >> 12 & 0xffd) + 13314);
  1011. if (uv_decode(&u, &v, *luv & 0x3fff) < 0)
  1012. {
  1013. u = U_NEU;
  1014. v = V_NEU;
  1015. }
  1016. *luv3++ = (int16_t)(u * (1L << 15));
  1017. *luv3++ = (int16_t)(v * (1L << 15));
  1018. luv++;
  1019. }
  1020. }
  1021. static void Luv24toRGB(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1022. {
  1023. uint32_t *luv = (uint32_t *)sp->tbuf;
  1024. uint8_t *rgb = (uint8_t *)op;
  1025. while (n-- > 0)
  1026. {
  1027. float xyz[3];
  1028. LogLuv24toXYZ(*luv++, xyz);
  1029. XYZtoRGB24(xyz, rgb);
  1030. rgb += 3;
  1031. }
  1032. }
  1033. static void Luv24fromXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1034. {
  1035. uint32_t *luv = (uint32_t *)sp->tbuf;
  1036. float *xyz = (float *)op;
  1037. while (n-- > 0)
  1038. {
  1039. *luv++ = LogLuv24fromXYZ(xyz, sp->encode_meth);
  1040. xyz += 3;
  1041. }
  1042. }
  1043. static void Luv24fromLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1044. {
  1045. uint32_t *luv = (uint32_t *)sp->tbuf;
  1046. int16_t *luv3 = (int16_t *)op;
  1047. while (n-- > 0)
  1048. {
  1049. int Le, Ce;
  1050. if (luv3[0] <= 0)
  1051. Le = 0;
  1052. else if (luv3[0] >= (1 << 12) + 3314)
  1053. Le = (1 << 10) - 1;
  1054. else if (sp->encode_meth == SGILOGENCODE_NODITHER)
  1055. Le = (luv3[0] - 3314) >> 2;
  1056. else
  1057. Le = tiff_itrunc(.25 * (luv3[0] - 3314.), sp->encode_meth);
  1058. Ce = uv_encode((luv3[1] + .5) / (1 << 15), (luv3[2] + .5) / (1 << 15),
  1059. sp->encode_meth);
  1060. if (Ce < 0) /* never happens */
  1061. Ce = uv_encode(U_NEU, V_NEU, SGILOGENCODE_NODITHER);
  1062. *luv++ = (uint32_t)Le << 14 | Ce;
  1063. luv3 += 3;
  1064. }
  1065. }
  1066. #if !LOGLUV_PUBLIC
  1067. static
  1068. #endif
  1069. void
  1070. LogLuv32toXYZ(uint32_t p, float *XYZ)
  1071. {
  1072. double L, u, v, s, x, y;
  1073. /* decode luminance */
  1074. L = LogL16toY((int)p >> 16);
  1075. if (L <= 0.)
  1076. {
  1077. XYZ[0] = XYZ[1] = XYZ[2] = 0.;
  1078. return;
  1079. }
  1080. /* decode color */
  1081. u = 1. / UVSCALE * ((p >> 8 & 0xff) + .5);
  1082. v = 1. / UVSCALE * ((p & 0xff) + .5);
  1083. s = 1. / (6. * u - 16. * v + 12.);
  1084. x = 9. * u * s;
  1085. y = 4. * v * s;
  1086. /* convert to XYZ */
  1087. XYZ[0] = (float)(x / y * L);
  1088. XYZ[1] = (float)L;
  1089. XYZ[2] = (float)((1. - x - y) / y * L);
  1090. }
  1091. #if !LOGLUV_PUBLIC
  1092. static
  1093. #endif
  1094. uint32_t
  1095. LogLuv32fromXYZ(float *XYZ, int em)
  1096. {
  1097. unsigned int Le, ue, ve;
  1098. double u, v, s;
  1099. /* encode luminance */
  1100. Le = (unsigned int)LogL16fromY(XYZ[1], em);
  1101. /* encode color */
  1102. s = XYZ[0] + 15. * XYZ[1] + 3. * XYZ[2];
  1103. if (!Le || s <= 0.)
  1104. {
  1105. u = U_NEU;
  1106. v = V_NEU;
  1107. }
  1108. else
  1109. {
  1110. u = 4. * XYZ[0] / s;
  1111. v = 9. * XYZ[1] / s;
  1112. }
  1113. if (u <= 0.)
  1114. ue = 0;
  1115. else
  1116. ue = tiff_itrunc(UVSCALE * u, em);
  1117. if (ue > 255)
  1118. ue = 255;
  1119. if (v <= 0.)
  1120. ve = 0;
  1121. else
  1122. ve = tiff_itrunc(UVSCALE * v, em);
  1123. if (ve > 255)
  1124. ve = 255;
  1125. /* combine encodings */
  1126. return (Le << 16 | ue << 8 | ve);
  1127. }
  1128. static void Luv32toXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1129. {
  1130. uint32_t *luv = (uint32_t *)sp->tbuf;
  1131. float *xyz = (float *)op;
  1132. while (n-- > 0)
  1133. {
  1134. LogLuv32toXYZ(*luv++, xyz);
  1135. xyz += 3;
  1136. }
  1137. }
  1138. static void Luv32toLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1139. {
  1140. uint32_t *luv = (uint32_t *)sp->tbuf;
  1141. int16_t *luv3 = (int16_t *)op;
  1142. while (n-- > 0)
  1143. {
  1144. double u, v;
  1145. *luv3++ = (int16_t)(*luv >> 16);
  1146. u = 1. / UVSCALE * ((*luv >> 8 & 0xff) + .5);
  1147. v = 1. / UVSCALE * ((*luv & 0xff) + .5);
  1148. *luv3++ = (int16_t)(u * (1L << 15));
  1149. *luv3++ = (int16_t)(v * (1L << 15));
  1150. luv++;
  1151. }
  1152. }
  1153. static void Luv32toRGB(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1154. {
  1155. uint32_t *luv = (uint32_t *)sp->tbuf;
  1156. uint8_t *rgb = (uint8_t *)op;
  1157. while (n-- > 0)
  1158. {
  1159. float xyz[3];
  1160. LogLuv32toXYZ(*luv++, xyz);
  1161. XYZtoRGB24(xyz, rgb);
  1162. rgb += 3;
  1163. }
  1164. }
  1165. static void Luv32fromXYZ(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1166. {
  1167. uint32_t *luv = (uint32_t *)sp->tbuf;
  1168. float *xyz = (float *)op;
  1169. while (n-- > 0)
  1170. {
  1171. *luv++ = LogLuv32fromXYZ(xyz, sp->encode_meth);
  1172. xyz += 3;
  1173. }
  1174. }
  1175. static void Luv32fromLuv48(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1176. {
  1177. uint32_t *luv = (uint32_t *)sp->tbuf;
  1178. int16_t *luv3 = (int16_t *)op;
  1179. if (sp->encode_meth == SGILOGENCODE_NODITHER)
  1180. {
  1181. while (n-- > 0)
  1182. {
  1183. *luv++ = (uint32_t)luv3[0] << 16 |
  1184. (luv3[1] * (uint32_t)(UVSCALE + .5) >> 7 & 0xff00) |
  1185. (luv3[2] * (uint32_t)(UVSCALE + .5) >> 15 & 0xff);
  1186. luv3 += 3;
  1187. }
  1188. return;
  1189. }
  1190. while (n-- > 0)
  1191. {
  1192. *luv++ =
  1193. (uint32_t)luv3[0] << 16 |
  1194. (tiff_itrunc(luv3[1] * (UVSCALE / (1 << 15)), sp->encode_meth)
  1195. << 8 &
  1196. 0xff00) |
  1197. (tiff_itrunc(luv3[2] * (UVSCALE / (1 << 15)), sp->encode_meth) &
  1198. 0xff);
  1199. luv3 += 3;
  1200. }
  1201. }
  1202. static void _logLuvNop(LogLuvState *sp, uint8_t *op, tmsize_t n)
  1203. {
  1204. (void)sp;
  1205. (void)op;
  1206. (void)n;
  1207. }
  1208. static int LogL16GuessDataFmt(TIFFDirectory *td)
  1209. {
  1210. #define PACK(s, b, f) (((b) << 6) | ((s) << 3) | (f))
  1211. switch (
  1212. PACK(td->td_samplesperpixel, td->td_bitspersample, td->td_sampleformat))
  1213. {
  1214. case PACK(1, 32, SAMPLEFORMAT_IEEEFP):
  1215. return (SGILOGDATAFMT_FLOAT);
  1216. case PACK(1, 16, SAMPLEFORMAT_VOID):
  1217. case PACK(1, 16, SAMPLEFORMAT_INT):
  1218. case PACK(1, 16, SAMPLEFORMAT_UINT):
  1219. return (SGILOGDATAFMT_16BIT);
  1220. case PACK(1, 8, SAMPLEFORMAT_VOID):
  1221. case PACK(1, 8, SAMPLEFORMAT_UINT):
  1222. return (SGILOGDATAFMT_8BIT);
  1223. }
  1224. #undef PACK
  1225. return (SGILOGDATAFMT_UNKNOWN);
  1226. }
  1227. static tmsize_t multiply_ms(tmsize_t m1, tmsize_t m2)
  1228. {
  1229. return _TIFFMultiplySSize(NULL, m1, m2, NULL);
  1230. }
  1231. static int LogL16InitState(TIFF *tif)
  1232. {
  1233. static const char module[] = "LogL16InitState";
  1234. TIFFDirectory *td = &tif->tif_dir;
  1235. LogLuvState *sp = DecoderState(tif);
  1236. assert(sp != NULL);
  1237. assert(td->td_photometric == PHOTOMETRIC_LOGL);
  1238. if (td->td_samplesperpixel != 1)
  1239. {
  1240. TIFFErrorExtR(tif, module,
  1241. "Sorry, can not handle LogL image with %s=%" PRIu16,
  1242. "Samples/pixel", td->td_samplesperpixel);
  1243. return 0;
  1244. }
  1245. /* for some reason, we can't do this in TIFFInitLogL16 */
  1246. if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN)
  1247. sp->user_datafmt = LogL16GuessDataFmt(td);
  1248. switch (sp->user_datafmt)
  1249. {
  1250. case SGILOGDATAFMT_FLOAT:
  1251. sp->pixel_size = sizeof(float);
  1252. break;
  1253. case SGILOGDATAFMT_16BIT:
  1254. sp->pixel_size = sizeof(int16_t);
  1255. break;
  1256. case SGILOGDATAFMT_8BIT:
  1257. sp->pixel_size = sizeof(uint8_t);
  1258. break;
  1259. default:
  1260. TIFFErrorExtR(tif, module,
  1261. "No support for converting user data format to LogL");
  1262. return (0);
  1263. }
  1264. if (isTiled(tif))
  1265. sp->tbuflen = multiply_ms(td->td_tilewidth, td->td_tilelength);
  1266. else if (td->td_rowsperstrip < td->td_imagelength)
  1267. sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_rowsperstrip);
  1268. else
  1269. sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_imagelength);
  1270. if (multiply_ms(sp->tbuflen, sizeof(int16_t)) == 0 ||
  1271. (sp->tbuf = (uint8_t *)_TIFFmallocExt(
  1272. tif, sp->tbuflen * sizeof(int16_t))) == NULL)
  1273. {
  1274. TIFFErrorExtR(tif, module, "No space for SGILog translation buffer");
  1275. return (0);
  1276. }
  1277. return (1);
  1278. }
  1279. static int LogLuvGuessDataFmt(TIFFDirectory *td)
  1280. {
  1281. int guess;
  1282. /*
  1283. * If the user didn't tell us their datafmt,
  1284. * take our best guess from the bitspersample.
  1285. */
  1286. #define PACK(a, b) (((a) << 3) | (b))
  1287. switch (PACK(td->td_bitspersample, td->td_sampleformat))
  1288. {
  1289. case PACK(32, SAMPLEFORMAT_IEEEFP):
  1290. guess = SGILOGDATAFMT_FLOAT;
  1291. break;
  1292. case PACK(32, SAMPLEFORMAT_VOID):
  1293. case PACK(32, SAMPLEFORMAT_UINT):
  1294. case PACK(32, SAMPLEFORMAT_INT):
  1295. guess = SGILOGDATAFMT_RAW;
  1296. break;
  1297. case PACK(16, SAMPLEFORMAT_VOID):
  1298. case PACK(16, SAMPLEFORMAT_INT):
  1299. case PACK(16, SAMPLEFORMAT_UINT):
  1300. guess = SGILOGDATAFMT_16BIT;
  1301. break;
  1302. case PACK(8, SAMPLEFORMAT_VOID):
  1303. case PACK(8, SAMPLEFORMAT_UINT):
  1304. guess = SGILOGDATAFMT_8BIT;
  1305. break;
  1306. default:
  1307. guess = SGILOGDATAFMT_UNKNOWN;
  1308. break;
  1309. #undef PACK
  1310. }
  1311. /*
  1312. * Double-check samples per pixel.
  1313. */
  1314. switch (td->td_samplesperpixel)
  1315. {
  1316. case 1:
  1317. if (guess != SGILOGDATAFMT_RAW)
  1318. guess = SGILOGDATAFMT_UNKNOWN;
  1319. break;
  1320. case 3:
  1321. if (guess == SGILOGDATAFMT_RAW)
  1322. guess = SGILOGDATAFMT_UNKNOWN;
  1323. break;
  1324. default:
  1325. guess = SGILOGDATAFMT_UNKNOWN;
  1326. break;
  1327. }
  1328. return (guess);
  1329. }
  1330. static int LogLuvInitState(TIFF *tif)
  1331. {
  1332. static const char module[] = "LogLuvInitState";
  1333. TIFFDirectory *td = &tif->tif_dir;
  1334. LogLuvState *sp = DecoderState(tif);
  1335. assert(sp != NULL);
  1336. assert(td->td_photometric == PHOTOMETRIC_LOGLUV);
  1337. /* for some reason, we can't do this in TIFFInitLogLuv */
  1338. if (td->td_planarconfig != PLANARCONFIG_CONTIG)
  1339. {
  1340. TIFFErrorExtR(tif, module,
  1341. "SGILog compression cannot handle non-contiguous data");
  1342. return (0);
  1343. }
  1344. if (sp->user_datafmt == SGILOGDATAFMT_UNKNOWN)
  1345. sp->user_datafmt = LogLuvGuessDataFmt(td);
  1346. switch (sp->user_datafmt)
  1347. {
  1348. case SGILOGDATAFMT_FLOAT:
  1349. sp->pixel_size = 3 * sizeof(float);
  1350. break;
  1351. case SGILOGDATAFMT_16BIT:
  1352. sp->pixel_size = 3 * sizeof(int16_t);
  1353. break;
  1354. case SGILOGDATAFMT_RAW:
  1355. sp->pixel_size = sizeof(uint32_t);
  1356. break;
  1357. case SGILOGDATAFMT_8BIT:
  1358. sp->pixel_size = 3 * sizeof(uint8_t);
  1359. break;
  1360. default:
  1361. TIFFErrorExtR(
  1362. tif, module,
  1363. "No support for converting user data format to LogLuv");
  1364. return (0);
  1365. }
  1366. if (isTiled(tif))
  1367. sp->tbuflen = multiply_ms(td->td_tilewidth, td->td_tilelength);
  1368. else if (td->td_rowsperstrip < td->td_imagelength)
  1369. sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_rowsperstrip);
  1370. else
  1371. sp->tbuflen = multiply_ms(td->td_imagewidth, td->td_imagelength);
  1372. if (multiply_ms(sp->tbuflen, sizeof(uint32_t)) == 0 ||
  1373. (sp->tbuf = (uint8_t *)_TIFFmallocExt(
  1374. tif, sp->tbuflen * sizeof(uint32_t))) == NULL)
  1375. {
  1376. TIFFErrorExtR(tif, module, "No space for SGILog translation buffer");
  1377. return (0);
  1378. }
  1379. return (1);
  1380. }
  1381. static int LogLuvFixupTags(TIFF *tif)
  1382. {
  1383. (void)tif;
  1384. return (1);
  1385. }
  1386. static int LogLuvSetupDecode(TIFF *tif)
  1387. {
  1388. static const char module[] = "LogLuvSetupDecode";
  1389. LogLuvState *sp = DecoderState(tif);
  1390. TIFFDirectory *td = &tif->tif_dir;
  1391. tif->tif_postdecode = _TIFFNoPostDecode;
  1392. switch (td->td_photometric)
  1393. {
  1394. case PHOTOMETRIC_LOGLUV:
  1395. if (!LogLuvInitState(tif))
  1396. break;
  1397. if (td->td_compression == COMPRESSION_SGILOG24)
  1398. {
  1399. tif->tif_decoderow = LogLuvDecode24;
  1400. switch (sp->user_datafmt)
  1401. {
  1402. case SGILOGDATAFMT_FLOAT:
  1403. sp->tfunc = Luv24toXYZ;
  1404. break;
  1405. case SGILOGDATAFMT_16BIT:
  1406. sp->tfunc = Luv24toLuv48;
  1407. break;
  1408. case SGILOGDATAFMT_8BIT:
  1409. sp->tfunc = Luv24toRGB;
  1410. break;
  1411. }
  1412. }
  1413. else
  1414. {
  1415. tif->tif_decoderow = LogLuvDecode32;
  1416. switch (sp->user_datafmt)
  1417. {
  1418. case SGILOGDATAFMT_FLOAT:
  1419. sp->tfunc = Luv32toXYZ;
  1420. break;
  1421. case SGILOGDATAFMT_16BIT:
  1422. sp->tfunc = Luv32toLuv48;
  1423. break;
  1424. case SGILOGDATAFMT_8BIT:
  1425. sp->tfunc = Luv32toRGB;
  1426. break;
  1427. }
  1428. }
  1429. return (1);
  1430. case PHOTOMETRIC_LOGL:
  1431. if (!LogL16InitState(tif))
  1432. break;
  1433. tif->tif_decoderow = LogL16Decode;
  1434. switch (sp->user_datafmt)
  1435. {
  1436. case SGILOGDATAFMT_FLOAT:
  1437. sp->tfunc = L16toY;
  1438. break;
  1439. case SGILOGDATAFMT_8BIT:
  1440. sp->tfunc = L16toGry;
  1441. break;
  1442. }
  1443. return (1);
  1444. default:
  1445. TIFFErrorExtR(tif, module,
  1446. "Inappropriate photometric interpretation %" PRIu16
  1447. " for SGILog compression; %s",
  1448. td->td_photometric, "must be either LogLUV or LogL");
  1449. break;
  1450. }
  1451. return (0);
  1452. }
  1453. static int LogLuvSetupEncode(TIFF *tif)
  1454. {
  1455. static const char module[] = "LogLuvSetupEncode";
  1456. LogLuvState *sp = EncoderState(tif);
  1457. TIFFDirectory *td = &tif->tif_dir;
  1458. switch (td->td_photometric)
  1459. {
  1460. case PHOTOMETRIC_LOGLUV:
  1461. if (!LogLuvInitState(tif))
  1462. return (0);
  1463. if (td->td_compression == COMPRESSION_SGILOG24)
  1464. {
  1465. tif->tif_encoderow = LogLuvEncode24;
  1466. switch (sp->user_datafmt)
  1467. {
  1468. case SGILOGDATAFMT_FLOAT:
  1469. sp->tfunc = Luv24fromXYZ;
  1470. break;
  1471. case SGILOGDATAFMT_16BIT:
  1472. sp->tfunc = Luv24fromLuv48;
  1473. break;
  1474. case SGILOGDATAFMT_RAW:
  1475. break;
  1476. default:
  1477. goto notsupported;
  1478. }
  1479. }
  1480. else
  1481. {
  1482. tif->tif_encoderow = LogLuvEncode32;
  1483. switch (sp->user_datafmt)
  1484. {
  1485. case SGILOGDATAFMT_FLOAT:
  1486. sp->tfunc = Luv32fromXYZ;
  1487. break;
  1488. case SGILOGDATAFMT_16BIT:
  1489. sp->tfunc = Luv32fromLuv48;
  1490. break;
  1491. case SGILOGDATAFMT_RAW:
  1492. break;
  1493. default:
  1494. goto notsupported;
  1495. }
  1496. }
  1497. break;
  1498. case PHOTOMETRIC_LOGL:
  1499. if (!LogL16InitState(tif))
  1500. return (0);
  1501. tif->tif_encoderow = LogL16Encode;
  1502. switch (sp->user_datafmt)
  1503. {
  1504. case SGILOGDATAFMT_FLOAT:
  1505. sp->tfunc = L16fromY;
  1506. break;
  1507. case SGILOGDATAFMT_16BIT:
  1508. break;
  1509. default:
  1510. goto notsupported;
  1511. }
  1512. break;
  1513. default:
  1514. TIFFErrorExtR(tif, module,
  1515. "Inappropriate photometric interpretation %" PRIu16
  1516. " for SGILog compression; %s",
  1517. td->td_photometric, "must be either LogLUV or LogL");
  1518. return (0);
  1519. }
  1520. sp->encoder_state = 1;
  1521. return (1);
  1522. notsupported:
  1523. TIFFErrorExtR(tif, module,
  1524. "SGILog compression supported only for %s, or raw data",
  1525. td->td_photometric == PHOTOMETRIC_LOGL ? "Y, L" : "XYZ, Luv");
  1526. return (0);
  1527. }
  1528. static void LogLuvClose(TIFF *tif)
  1529. {
  1530. LogLuvState *sp = (LogLuvState *)tif->tif_data;
  1531. TIFFDirectory *td = &tif->tif_dir;
  1532. assert(sp != 0);
  1533. /*
  1534. * For consistency, we always want to write out the same
  1535. * bitspersample and sampleformat for our TIFF file,
  1536. * regardless of the data format being used by the application.
  1537. * Since this routine is called after tags have been set but
  1538. * before they have been recorded in the file, we reset them here.
  1539. * Note: this is really a nasty approach. See PixarLogClose
  1540. */
  1541. if (sp->encoder_state)
  1542. {
  1543. /* See PixarLogClose. Might avoid issues with tags whose size depends
  1544. * on those below, but not completely sure this is enough. */
  1545. td->td_samplesperpixel =
  1546. (td->td_photometric == PHOTOMETRIC_LOGL) ? 1 : 3;
  1547. td->td_bitspersample = 16;
  1548. td->td_sampleformat = SAMPLEFORMAT_INT;
  1549. }
  1550. }
  1551. static void LogLuvCleanup(TIFF *tif)
  1552. {
  1553. LogLuvState *sp = (LogLuvState *)tif->tif_data;
  1554. assert(sp != 0);
  1555. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  1556. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  1557. if (sp->tbuf)
  1558. _TIFFfreeExt(tif, sp->tbuf);
  1559. _TIFFfreeExt(tif, sp);
  1560. tif->tif_data = NULL;
  1561. _TIFFSetDefaultCompressionState(tif);
  1562. }
  1563. static int LogLuvVSetField(TIFF *tif, uint32_t tag, va_list ap)
  1564. {
  1565. static const char module[] = "LogLuvVSetField";
  1566. LogLuvState *sp = DecoderState(tif);
  1567. int bps, fmt;
  1568. switch (tag)
  1569. {
  1570. case TIFFTAG_SGILOGDATAFMT:
  1571. sp->user_datafmt = (int)va_arg(ap, int);
  1572. /*
  1573. * Tweak the TIFF header so that the rest of libtiff knows what
  1574. * size of data will be passed between app and library, and
  1575. * assume that the app knows what it is doing and is not
  1576. * confused by these header manipulations...
  1577. */
  1578. switch (sp->user_datafmt)
  1579. {
  1580. case SGILOGDATAFMT_FLOAT:
  1581. bps = 32;
  1582. fmt = SAMPLEFORMAT_IEEEFP;
  1583. break;
  1584. case SGILOGDATAFMT_16BIT:
  1585. bps = 16;
  1586. fmt = SAMPLEFORMAT_INT;
  1587. break;
  1588. case SGILOGDATAFMT_RAW:
  1589. bps = 32;
  1590. fmt = SAMPLEFORMAT_UINT;
  1591. TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
  1592. break;
  1593. case SGILOGDATAFMT_8BIT:
  1594. bps = 8;
  1595. fmt = SAMPLEFORMAT_UINT;
  1596. break;
  1597. default:
  1598. TIFFErrorExtR(
  1599. tif, tif->tif_name,
  1600. "Unknown data format %d for LogLuv compression",
  1601. sp->user_datafmt);
  1602. return (0);
  1603. }
  1604. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bps);
  1605. TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, fmt);
  1606. /*
  1607. * Must recalculate sizes should bits/sample change.
  1608. */
  1609. tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)-1;
  1610. tif->tif_scanlinesize = TIFFScanlineSize(tif);
  1611. return (1);
  1612. case TIFFTAG_SGILOGENCODE:
  1613. sp->encode_meth = (int)va_arg(ap, int);
  1614. if (sp->encode_meth != SGILOGENCODE_NODITHER &&
  1615. sp->encode_meth != SGILOGENCODE_RANDITHER)
  1616. {
  1617. TIFFErrorExtR(tif, module,
  1618. "Unknown encoding %d for LogLuv compression",
  1619. sp->encode_meth);
  1620. return (0);
  1621. }
  1622. return (1);
  1623. default:
  1624. return (*sp->vsetparent)(tif, tag, ap);
  1625. }
  1626. }
  1627. static int LogLuvVGetField(TIFF *tif, uint32_t tag, va_list ap)
  1628. {
  1629. LogLuvState *sp = (LogLuvState *)tif->tif_data;
  1630. switch (tag)
  1631. {
  1632. case TIFFTAG_SGILOGDATAFMT:
  1633. *va_arg(ap, int *) = sp->user_datafmt;
  1634. return (1);
  1635. default:
  1636. return (*sp->vgetparent)(tif, tag, ap);
  1637. }
  1638. }
  1639. static const TIFFField LogLuvFields[] = {
  1640. {TIFFTAG_SGILOGDATAFMT, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_INT,
  1641. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "SGILogDataFmt", NULL},
  1642. {TIFFTAG_SGILOGENCODE, 0, 0, TIFF_SHORT, 0, TIFF_SETGET_INT,
  1643. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "SGILogEncode", NULL}};
  1644. int TIFFInitSGILog(TIFF *tif, int scheme)
  1645. {
  1646. static const char module[] = "TIFFInitSGILog";
  1647. LogLuvState *sp;
  1648. assert(scheme == COMPRESSION_SGILOG24 || scheme == COMPRESSION_SGILOG);
  1649. /*
  1650. * Merge codec-specific tag information.
  1651. */
  1652. if (!_TIFFMergeFields(tif, LogLuvFields, TIFFArrayCount(LogLuvFields)))
  1653. {
  1654. TIFFErrorExtR(tif, module, "Merging SGILog codec-specific tags failed");
  1655. return 0;
  1656. }
  1657. /*
  1658. * Allocate state block so tag methods have storage to record values.
  1659. */
  1660. tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(LogLuvState));
  1661. if (tif->tif_data == NULL)
  1662. goto bad;
  1663. sp = (LogLuvState *)tif->tif_data;
  1664. _TIFFmemset((void *)sp, 0, sizeof(*sp));
  1665. sp->user_datafmt = SGILOGDATAFMT_UNKNOWN;
  1666. sp->encode_meth = (scheme == COMPRESSION_SGILOG24) ? SGILOGENCODE_RANDITHER
  1667. : SGILOGENCODE_NODITHER;
  1668. sp->tfunc = _logLuvNop;
  1669. /*
  1670. * Install codec methods.
  1671. * NB: tif_decoderow & tif_encoderow are filled
  1672. * in at setup time.
  1673. */
  1674. tif->tif_fixuptags = LogLuvFixupTags;
  1675. tif->tif_setupdecode = LogLuvSetupDecode;
  1676. tif->tif_decodestrip = LogLuvDecodeStrip;
  1677. tif->tif_decodetile = LogLuvDecodeTile;
  1678. tif->tif_setupencode = LogLuvSetupEncode;
  1679. tif->tif_encodestrip = LogLuvEncodeStrip;
  1680. tif->tif_encodetile = LogLuvEncodeTile;
  1681. tif->tif_close = LogLuvClose;
  1682. tif->tif_cleanup = LogLuvCleanup;
  1683. /*
  1684. * Override parent get/set field methods.
  1685. */
  1686. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  1687. tif->tif_tagmethods.vgetfield = LogLuvVGetField; /* hook for codec tags */
  1688. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  1689. tif->tif_tagmethods.vsetfield = LogLuvVSetField; /* hook for codec tags */
  1690. return (1);
  1691. bad:
  1692. TIFFErrorExtR(tif, module, "%s: No space for LogLuv state block",
  1693. tif->tif_name);
  1694. return (0);
  1695. }
  1696. #endif /* LOGLUV_SUPPORT */