tif_luv.c 44 KB

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