tif_luv.c 42 KB

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