2
0

tif_luv.c 42 KB

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