tif_pixarlog.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  1. /* $Id: tif_pixarlog.c,v 1.39 2012-12-10 17:27:13 tgl Exp $ */
  2. /*
  3. * Copyright (c) 1996-1997 Sam Leffler
  4. * Copyright (c) 1996 Pixar
  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. * Pixar, Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Pixar, Sam Leffler 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 PIXAR, SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * 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 PIXARLOG_SUPPORT
  27. /*
  28. * TIFF Library.
  29. * PixarLog Compression Support
  30. *
  31. * Contributed by Dan McCoy.
  32. *
  33. * PixarLog film support uses the TIFF library to store companded
  34. * 11 bit values into a tiff file, which are compressed using the
  35. * zip compressor.
  36. *
  37. * The codec can take as input and produce as output 32-bit IEEE float values
  38. * as well as 16-bit or 8-bit unsigned integer values.
  39. *
  40. * On writing any of the above are converted into the internal
  41. * 11-bit log format. In the case of 8 and 16 bit values, the
  42. * input is assumed to be unsigned linear color values that represent
  43. * the range 0-1. In the case of IEEE values, the 0-1 range is assumed to
  44. * be the normal linear color range, in addition over 1 values are
  45. * accepted up to a value of about 25.0 to encode "hot" hightlights and such.
  46. * The encoding is lossless for 8-bit values, slightly lossy for the
  47. * other bit depths. The actual color precision should be better
  48. * than the human eye can perceive with extra room to allow for
  49. * error introduced by further image computation. As with any quantized
  50. * color format, it is possible to perform image calculations which
  51. * expose the quantization error. This format should certainly be less
  52. * susceptable to such errors than standard 8-bit encodings, but more
  53. * susceptable than straight 16-bit or 32-bit encodings.
  54. *
  55. * On reading the internal format is converted to the desired output format.
  56. * The program can request which format it desires by setting the internal
  57. * pseudo tag TIFFTAG_PIXARLOGDATAFMT to one of these possible values:
  58. * PIXARLOGDATAFMT_FLOAT = provide IEEE float values.
  59. * PIXARLOGDATAFMT_16BIT = provide unsigned 16-bit integer values
  60. * PIXARLOGDATAFMT_8BIT = provide unsigned 8-bit integer values
  61. *
  62. * alternately PIXARLOGDATAFMT_8BITABGR provides unsigned 8-bit integer
  63. * values with the difference that if there are exactly three or four channels
  64. * (rgb or rgba) it swaps the channel order (bgr or abgr).
  65. *
  66. * PIXARLOGDATAFMT_11BITLOG provides the internal encoding directly
  67. * packed in 16-bit values. However no tools are supplied for interpreting
  68. * these values.
  69. *
  70. * "hot" (over 1.0) areas written in floating point get clamped to
  71. * 1.0 in the integer data types.
  72. *
  73. * When the file is closed after writing, the bit depth and sample format
  74. * are set always to appear as if 8-bit data has been written into it.
  75. * That way a naive program unaware of the particulars of the encoding
  76. * gets the format it is most likely able to handle.
  77. *
  78. * The codec does it's own horizontal differencing step on the coded
  79. * values so the libraries predictor stuff should be turned off.
  80. * The codec also handle byte swapping the encoded values as necessary
  81. * since the library does not have the information necessary
  82. * to know the bit depth of the raw unencoded buffer.
  83. *
  84. * NOTE: This decoder does not appear to update tif_rawcp, and tif_rawcc.
  85. * This can cause problems with the implementation of CHUNKY_STRIP_READ_SUPPORT
  86. * as noted in http://trac.osgeo.org/gdal/ticket/3894. FrankW - Jan'11
  87. */
  88. #include "tif_predict.h"
  89. #include "zlib.h"
  90. #include <stdio.h>
  91. #include <stdlib.h>
  92. #include <math.h>
  93. /* Tables for converting to/from 11 bit coded values */
  94. #define TSIZE 2048 /* decode table size (11-bit tokens) */
  95. #define TSIZEP1 2049 /* Plus one for slop */
  96. #define ONE 1250 /* token value of 1.0 exactly */
  97. #define RATIO 1.004 /* nominal ratio for log part */
  98. #define CODE_MASK 0x7ff /* 11 bits. */
  99. static float Fltsize;
  100. static float LogK1, LogK2;
  101. #define REPEAT(n, op) { int i; i=n; do { i--; op; } while (i>0); }
  102. static void
  103. horizontalAccumulateF(uint16 *wp, int n, int stride, float *op,
  104. float *ToLinearF)
  105. {
  106. register unsigned int cr, cg, cb, ca, mask;
  107. register float t0, t1, t2, t3;
  108. if (n >= stride) {
  109. mask = CODE_MASK;
  110. if (stride == 3) {
  111. t0 = ToLinearF[cr = (wp[0] & mask)];
  112. t1 = ToLinearF[cg = (wp[1] & mask)];
  113. t2 = ToLinearF[cb = (wp[2] & mask)];
  114. op[0] = t0;
  115. op[1] = t1;
  116. op[2] = t2;
  117. n -= 3;
  118. while (n > 0) {
  119. wp += 3;
  120. op += 3;
  121. n -= 3;
  122. t0 = ToLinearF[(cr += wp[0]) & mask];
  123. t1 = ToLinearF[(cg += wp[1]) & mask];
  124. t2 = ToLinearF[(cb += wp[2]) & mask];
  125. op[0] = t0;
  126. op[1] = t1;
  127. op[2] = t2;
  128. }
  129. } else if (stride == 4) {
  130. t0 = ToLinearF[cr = (wp[0] & mask)];
  131. t1 = ToLinearF[cg = (wp[1] & mask)];
  132. t2 = ToLinearF[cb = (wp[2] & mask)];
  133. t3 = ToLinearF[ca = (wp[3] & mask)];
  134. op[0] = t0;
  135. op[1] = t1;
  136. op[2] = t2;
  137. op[3] = t3;
  138. n -= 4;
  139. while (n > 0) {
  140. wp += 4;
  141. op += 4;
  142. n -= 4;
  143. t0 = ToLinearF[(cr += wp[0]) & mask];
  144. t1 = ToLinearF[(cg += wp[1]) & mask];
  145. t2 = ToLinearF[(cb += wp[2]) & mask];
  146. t3 = ToLinearF[(ca += wp[3]) & mask];
  147. op[0] = t0;
  148. op[1] = t1;
  149. op[2] = t2;
  150. op[3] = t3;
  151. }
  152. } else {
  153. REPEAT(stride, *op = ToLinearF[*wp&mask]; wp++; op++)
  154. n -= stride;
  155. while (n > 0) {
  156. REPEAT(stride,
  157. wp[stride] += *wp; *op = ToLinearF[*wp&mask]; wp++; op++)
  158. n -= stride;
  159. }
  160. }
  161. }
  162. }
  163. static void
  164. horizontalAccumulate12(uint16 *wp, int n, int stride, int16 *op,
  165. float *ToLinearF)
  166. {
  167. register unsigned int cr, cg, cb, ca, mask;
  168. register float t0, t1, t2, t3;
  169. #define SCALE12 2048.0F
  170. #define CLAMP12(t) (((t) < 3071) ? (uint16) (t) : 3071)
  171. if (n >= stride) {
  172. mask = CODE_MASK;
  173. if (stride == 3) {
  174. t0 = ToLinearF[cr = (wp[0] & mask)] * SCALE12;
  175. t1 = ToLinearF[cg = (wp[1] & mask)] * SCALE12;
  176. t2 = ToLinearF[cb = (wp[2] & mask)] * SCALE12;
  177. op[0] = CLAMP12(t0);
  178. op[1] = CLAMP12(t1);
  179. op[2] = CLAMP12(t2);
  180. n -= 3;
  181. while (n > 0) {
  182. wp += 3;
  183. op += 3;
  184. n -= 3;
  185. t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
  186. t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
  187. t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
  188. op[0] = CLAMP12(t0);
  189. op[1] = CLAMP12(t1);
  190. op[2] = CLAMP12(t2);
  191. }
  192. } else if (stride == 4) {
  193. t0 = ToLinearF[cr = (wp[0] & mask)] * SCALE12;
  194. t1 = ToLinearF[cg = (wp[1] & mask)] * SCALE12;
  195. t2 = ToLinearF[cb = (wp[2] & mask)] * SCALE12;
  196. t3 = ToLinearF[ca = (wp[3] & mask)] * SCALE12;
  197. op[0] = CLAMP12(t0);
  198. op[1] = CLAMP12(t1);
  199. op[2] = CLAMP12(t2);
  200. op[3] = CLAMP12(t3);
  201. n -= 4;
  202. while (n > 0) {
  203. wp += 4;
  204. op += 4;
  205. n -= 4;
  206. t0 = ToLinearF[(cr += wp[0]) & mask] * SCALE12;
  207. t1 = ToLinearF[(cg += wp[1]) & mask] * SCALE12;
  208. t2 = ToLinearF[(cb += wp[2]) & mask] * SCALE12;
  209. t3 = ToLinearF[(ca += wp[3]) & mask] * SCALE12;
  210. op[0] = CLAMP12(t0);
  211. op[1] = CLAMP12(t1);
  212. op[2] = CLAMP12(t2);
  213. op[3] = CLAMP12(t3);
  214. }
  215. } else {
  216. REPEAT(stride, t0 = ToLinearF[*wp&mask] * SCALE12;
  217. *op = CLAMP12(t0); wp++; op++)
  218. n -= stride;
  219. while (n > 0) {
  220. REPEAT(stride,
  221. wp[stride] += *wp; t0 = ToLinearF[wp[stride]&mask]*SCALE12;
  222. *op = CLAMP12(t0); wp++; op++)
  223. n -= stride;
  224. }
  225. }
  226. }
  227. }
  228. static void
  229. horizontalAccumulate16(uint16 *wp, int n, int stride, uint16 *op,
  230. uint16 *ToLinear16)
  231. {
  232. register unsigned int cr, cg, cb, ca, mask;
  233. if (n >= stride) {
  234. mask = CODE_MASK;
  235. if (stride == 3) {
  236. op[0] = ToLinear16[cr = (wp[0] & mask)];
  237. op[1] = ToLinear16[cg = (wp[1] & mask)];
  238. op[2] = ToLinear16[cb = (wp[2] & mask)];
  239. n -= 3;
  240. while (n > 0) {
  241. wp += 3;
  242. op += 3;
  243. n -= 3;
  244. op[0] = ToLinear16[(cr += wp[0]) & mask];
  245. op[1] = ToLinear16[(cg += wp[1]) & mask];
  246. op[2] = ToLinear16[(cb += wp[2]) & mask];
  247. }
  248. } else if (stride == 4) {
  249. op[0] = ToLinear16[cr = (wp[0] & mask)];
  250. op[1] = ToLinear16[cg = (wp[1] & mask)];
  251. op[2] = ToLinear16[cb = (wp[2] & mask)];
  252. op[3] = ToLinear16[ca = (wp[3] & mask)];
  253. n -= 4;
  254. while (n > 0) {
  255. wp += 4;
  256. op += 4;
  257. n -= 4;
  258. op[0] = ToLinear16[(cr += wp[0]) & mask];
  259. op[1] = ToLinear16[(cg += wp[1]) & mask];
  260. op[2] = ToLinear16[(cb += wp[2]) & mask];
  261. op[3] = ToLinear16[(ca += wp[3]) & mask];
  262. }
  263. } else {
  264. REPEAT(stride, *op = ToLinear16[*wp&mask]; wp++; op++)
  265. n -= stride;
  266. while (n > 0) {
  267. REPEAT(stride,
  268. wp[stride] += *wp; *op = ToLinear16[*wp&mask]; wp++; op++)
  269. n -= stride;
  270. }
  271. }
  272. }
  273. }
  274. /*
  275. * Returns the log encoded 11-bit values with the horizontal
  276. * differencing undone.
  277. */
  278. static void
  279. horizontalAccumulate11(uint16 *wp, int n, int stride, uint16 *op)
  280. {
  281. register unsigned int cr, cg, cb, ca, mask;
  282. if (n >= stride) {
  283. mask = CODE_MASK;
  284. if (stride == 3) {
  285. op[0] = cr = wp[0]; op[1] = cg = wp[1]; op[2] = cb = wp[2];
  286. n -= 3;
  287. while (n > 0) {
  288. wp += 3;
  289. op += 3;
  290. n -= 3;
  291. op[0] = (cr += wp[0]) & mask;
  292. op[1] = (cg += wp[1]) & mask;
  293. op[2] = (cb += wp[2]) & mask;
  294. }
  295. } else if (stride == 4) {
  296. op[0] = cr = wp[0]; op[1] = cg = wp[1];
  297. op[2] = cb = wp[2]; op[3] = ca = wp[3];
  298. n -= 4;
  299. while (n > 0) {
  300. wp += 4;
  301. op += 4;
  302. n -= 4;
  303. op[0] = (cr += wp[0]) & mask;
  304. op[1] = (cg += wp[1]) & mask;
  305. op[2] = (cb += wp[2]) & mask;
  306. op[3] = (ca += wp[3]) & mask;
  307. }
  308. } else {
  309. REPEAT(stride, *op = *wp&mask; wp++; op++)
  310. n -= stride;
  311. while (n > 0) {
  312. REPEAT(stride,
  313. wp[stride] += *wp; *op = *wp&mask; wp++; op++)
  314. n -= stride;
  315. }
  316. }
  317. }
  318. }
  319. static void
  320. horizontalAccumulate8(uint16 *wp, int n, int stride, unsigned char *op,
  321. unsigned char *ToLinear8)
  322. {
  323. register unsigned int cr, cg, cb, ca, mask;
  324. if (n >= stride) {
  325. mask = CODE_MASK;
  326. if (stride == 3) {
  327. op[0] = ToLinear8[cr = (wp[0] & mask)];
  328. op[1] = ToLinear8[cg = (wp[1] & mask)];
  329. op[2] = ToLinear8[cb = (wp[2] & mask)];
  330. n -= 3;
  331. while (n > 0) {
  332. n -= 3;
  333. wp += 3;
  334. op += 3;
  335. op[0] = ToLinear8[(cr += wp[0]) & mask];
  336. op[1] = ToLinear8[(cg += wp[1]) & mask];
  337. op[2] = ToLinear8[(cb += wp[2]) & mask];
  338. }
  339. } else if (stride == 4) {
  340. op[0] = ToLinear8[cr = (wp[0] & mask)];
  341. op[1] = ToLinear8[cg = (wp[1] & mask)];
  342. op[2] = ToLinear8[cb = (wp[2] & mask)];
  343. op[3] = ToLinear8[ca = (wp[3] & mask)];
  344. n -= 4;
  345. while (n > 0) {
  346. n -= 4;
  347. wp += 4;
  348. op += 4;
  349. op[0] = ToLinear8[(cr += wp[0]) & mask];
  350. op[1] = ToLinear8[(cg += wp[1]) & mask];
  351. op[2] = ToLinear8[(cb += wp[2]) & mask];
  352. op[3] = ToLinear8[(ca += wp[3]) & mask];
  353. }
  354. } else {
  355. REPEAT(stride, *op = ToLinear8[*wp&mask]; wp++; op++)
  356. n -= stride;
  357. while (n > 0) {
  358. REPEAT(stride,
  359. wp[stride] += *wp; *op = ToLinear8[*wp&mask]; wp++; op++)
  360. n -= stride;
  361. }
  362. }
  363. }
  364. }
  365. static void
  366. horizontalAccumulate8abgr(uint16 *wp, int n, int stride, unsigned char *op,
  367. unsigned char *ToLinear8)
  368. {
  369. register unsigned int cr, cg, cb, ca, mask;
  370. register unsigned char t0, t1, t2, t3;
  371. if (n >= stride) {
  372. mask = CODE_MASK;
  373. if (stride == 3) {
  374. op[0] = 0;
  375. t1 = ToLinear8[cb = (wp[2] & mask)];
  376. t2 = ToLinear8[cg = (wp[1] & mask)];
  377. t3 = ToLinear8[cr = (wp[0] & mask)];
  378. op[1] = t1;
  379. op[2] = t2;
  380. op[3] = t3;
  381. n -= 3;
  382. while (n > 0) {
  383. n -= 3;
  384. wp += 3;
  385. op += 4;
  386. op[0] = 0;
  387. t1 = ToLinear8[(cb += wp[2]) & mask];
  388. t2 = ToLinear8[(cg += wp[1]) & mask];
  389. t3 = ToLinear8[(cr += wp[0]) & mask];
  390. op[1] = t1;
  391. op[2] = t2;
  392. op[3] = t3;
  393. }
  394. } else if (stride == 4) {
  395. t0 = ToLinear8[ca = (wp[3] & mask)];
  396. t1 = ToLinear8[cb = (wp[2] & mask)];
  397. t2 = ToLinear8[cg = (wp[1] & mask)];
  398. t3 = ToLinear8[cr = (wp[0] & mask)];
  399. op[0] = t0;
  400. op[1] = t1;
  401. op[2] = t2;
  402. op[3] = t3;
  403. n -= 4;
  404. while (n > 0) {
  405. n -= 4;
  406. wp += 4;
  407. op += 4;
  408. t0 = ToLinear8[(ca += wp[3]) & mask];
  409. t1 = ToLinear8[(cb += wp[2]) & mask];
  410. t2 = ToLinear8[(cg += wp[1]) & mask];
  411. t3 = ToLinear8[(cr += wp[0]) & mask];
  412. op[0] = t0;
  413. op[1] = t1;
  414. op[2] = t2;
  415. op[3] = t3;
  416. }
  417. } else {
  418. REPEAT(stride, *op = ToLinear8[*wp&mask]; wp++; op++)
  419. n -= stride;
  420. while (n > 0) {
  421. REPEAT(stride,
  422. wp[stride] += *wp; *op = ToLinear8[*wp&mask]; wp++; op++)
  423. n -= stride;
  424. }
  425. }
  426. }
  427. }
  428. /*
  429. * State block for each open TIFF
  430. * file using PixarLog compression/decompression.
  431. */
  432. typedef struct {
  433. TIFFPredictorState predict;
  434. z_stream stream;
  435. uint16 *tbuf;
  436. uint16 stride;
  437. int state;
  438. int user_datafmt;
  439. int quality;
  440. #define PLSTATE_INIT 1
  441. TIFFVSetMethod vgetparent; /* super-class method */
  442. TIFFVSetMethod vsetparent; /* super-class method */
  443. float *ToLinearF;
  444. uint16 *ToLinear16;
  445. unsigned char *ToLinear8;
  446. uint16 *FromLT2;
  447. uint16 *From14; /* Really for 16-bit data, but we shift down 2 */
  448. uint16 *From8;
  449. } PixarLogState;
  450. static int
  451. PixarLogMakeTables(PixarLogState *sp)
  452. {
  453. /*
  454. * We make several tables here to convert between various external
  455. * representations (float, 16-bit, and 8-bit) and the internal
  456. * 11-bit companded representation. The 11-bit representation has two
  457. * distinct regions. A linear bottom end up through .018316 in steps
  458. * of about .000073, and a region of constant ratio up to about 25.
  459. * These floating point numbers are stored in the main table ToLinearF.
  460. * All other tables are derived from this one. The tables (and the
  461. * ratios) are continuous at the internal seam.
  462. */
  463. int nlin, lt2size;
  464. int i, j;
  465. double b, c, linstep, v;
  466. float *ToLinearF;
  467. uint16 *ToLinear16;
  468. unsigned char *ToLinear8;
  469. uint16 *FromLT2;
  470. uint16 *From14; /* Really for 16-bit data, but we shift down 2 */
  471. uint16 *From8;
  472. c = log(RATIO);
  473. nlin = (int)(1./c); /* nlin must be an integer */
  474. c = 1./nlin;
  475. b = exp(-c*ONE); /* multiplicative scale factor [b*exp(c*ONE) = 1] */
  476. linstep = b*c*exp(1.);
  477. LogK1 = (float)(1./c); /* if (v >= 2) token = k1*log(v*k2) */
  478. LogK2 = (float)(1./b);
  479. lt2size = (int)(2./linstep) + 1;
  480. FromLT2 = (uint16 *)_TIFFmalloc(lt2size*sizeof(uint16));
  481. From14 = (uint16 *)_TIFFmalloc(16384*sizeof(uint16));
  482. From8 = (uint16 *)_TIFFmalloc(256*sizeof(uint16));
  483. ToLinearF = (float *)_TIFFmalloc(TSIZEP1 * sizeof(float));
  484. ToLinear16 = (uint16 *)_TIFFmalloc(TSIZEP1 * sizeof(uint16));
  485. ToLinear8 = (unsigned char *)_TIFFmalloc(TSIZEP1 * sizeof(unsigned char));
  486. if (FromLT2 == NULL || From14 == NULL || From8 == NULL ||
  487. ToLinearF == NULL || ToLinear16 == NULL || ToLinear8 == NULL) {
  488. if (FromLT2) _TIFFfree(FromLT2);
  489. if (From14) _TIFFfree(From14);
  490. if (From8) _TIFFfree(From8);
  491. if (ToLinearF) _TIFFfree(ToLinearF);
  492. if (ToLinear16) _TIFFfree(ToLinear16);
  493. if (ToLinear8) _TIFFfree(ToLinear8);
  494. sp->FromLT2 = NULL;
  495. sp->From14 = NULL;
  496. sp->From8 = NULL;
  497. sp->ToLinearF = NULL;
  498. sp->ToLinear16 = NULL;
  499. sp->ToLinear8 = NULL;
  500. return 0;
  501. }
  502. j = 0;
  503. for (i = 0; i < nlin; i++) {
  504. v = i * linstep;
  505. ToLinearF[j++] = (float)v;
  506. }
  507. for (i = nlin; i < TSIZE; i++)
  508. ToLinearF[j++] = (float)(b*exp(c*i));
  509. ToLinearF[2048] = ToLinearF[2047];
  510. for (i = 0; i < TSIZEP1; i++) {
  511. v = ToLinearF[i]*65535.0 + 0.5;
  512. ToLinear16[i] = (v > 65535.0) ? 65535 : (uint16)v;
  513. v = ToLinearF[i]*255.0 + 0.5;
  514. ToLinear8[i] = (v > 255.0) ? 255 : (unsigned char)v;
  515. }
  516. j = 0;
  517. for (i = 0; i < lt2size; i++) {
  518. if ((i*linstep)*(i*linstep) > ToLinearF[j]*ToLinearF[j+1])
  519. j++;
  520. FromLT2[i] = j;
  521. }
  522. /*
  523. * Since we lose info anyway on 16-bit data, we set up a 14-bit
  524. * table and shift 16-bit values down two bits on input.
  525. * saves a little table space.
  526. */
  527. j = 0;
  528. for (i = 0; i < 16384; i++) {
  529. while ((i/16383.)*(i/16383.) > ToLinearF[j]*ToLinearF[j+1])
  530. j++;
  531. From14[i] = j;
  532. }
  533. j = 0;
  534. for (i = 0; i < 256; i++) {
  535. while ((i/255.)*(i/255.) > ToLinearF[j]*ToLinearF[j+1])
  536. j++;
  537. From8[i] = j;
  538. }
  539. Fltsize = (float)(lt2size/2);
  540. sp->ToLinearF = ToLinearF;
  541. sp->ToLinear16 = ToLinear16;
  542. sp->ToLinear8 = ToLinear8;
  543. sp->FromLT2 = FromLT2;
  544. sp->From14 = From14;
  545. sp->From8 = From8;
  546. return 1;
  547. }
  548. #define DecoderState(tif) ((PixarLogState*) (tif)->tif_data)
  549. #define EncoderState(tif) ((PixarLogState*) (tif)->tif_data)
  550. static int PixarLogEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
  551. static int PixarLogDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
  552. #define PIXARLOGDATAFMT_UNKNOWN -1
  553. static int
  554. PixarLogGuessDataFmt(TIFFDirectory *td)
  555. {
  556. int guess = PIXARLOGDATAFMT_UNKNOWN;
  557. int format = td->td_sampleformat;
  558. /* If the user didn't tell us his datafmt,
  559. * take our best guess from the bitspersample.
  560. */
  561. switch (td->td_bitspersample) {
  562. case 32:
  563. if (format == SAMPLEFORMAT_IEEEFP)
  564. guess = PIXARLOGDATAFMT_FLOAT;
  565. break;
  566. case 16:
  567. if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
  568. guess = PIXARLOGDATAFMT_16BIT;
  569. break;
  570. case 12:
  571. if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_INT)
  572. guess = PIXARLOGDATAFMT_12BITPICIO;
  573. break;
  574. case 11:
  575. if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
  576. guess = PIXARLOGDATAFMT_11BITLOG;
  577. break;
  578. case 8:
  579. if (format == SAMPLEFORMAT_VOID || format == SAMPLEFORMAT_UINT)
  580. guess = PIXARLOGDATAFMT_8BIT;
  581. break;
  582. }
  583. return guess;
  584. }
  585. static tmsize_t
  586. multiply_ms(tmsize_t m1, tmsize_t m2)
  587. {
  588. tmsize_t bytes = m1 * m2;
  589. if (m1 && bytes / m1 != m2)
  590. bytes = 0;
  591. return bytes;
  592. }
  593. static tmsize_t
  594. add_ms(tmsize_t m1, tmsize_t m2)
  595. {
  596. tmsize_t bytes = m1 + m2;
  597. /* if either input is zero, assume overflow already occurred */
  598. if (m1 == 0 || m2 == 0)
  599. bytes = 0;
  600. else if (bytes <= m1 || bytes <= m2)
  601. bytes = 0;
  602. return bytes;
  603. }
  604. static int
  605. PixarLogFixupTags(TIFF* tif)
  606. {
  607. (void) tif;
  608. return (1);
  609. }
  610. static int
  611. PixarLogSetupDecode(TIFF* tif)
  612. {
  613. static const char module[] = "PixarLogSetupDecode";
  614. TIFFDirectory *td = &tif->tif_dir;
  615. PixarLogState* sp = DecoderState(tif);
  616. tmsize_t tbuf_size;
  617. assert(sp != NULL);
  618. /* Make sure no byte swapping happens on the data
  619. * after decompression. */
  620. tif->tif_postdecode = _TIFFNoPostDecode;
  621. /* for some reason, we can't do this in TIFFInitPixarLog */
  622. sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
  623. td->td_samplesperpixel : 1);
  624. tbuf_size = multiply_ms(multiply_ms(multiply_ms(sp->stride, td->td_imagewidth),
  625. td->td_rowsperstrip), sizeof(uint16));
  626. /* add one more stride in case input ends mid-stride */
  627. tbuf_size = add_ms(tbuf_size, sizeof(uint16) * sp->stride);
  628. if (tbuf_size == 0)
  629. return (0); /* TODO: this is an error return without error report through TIFFErrorExt */
  630. sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size);
  631. if (sp->tbuf == NULL)
  632. return (0);
  633. if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
  634. sp->user_datafmt = PixarLogGuessDataFmt(td);
  635. if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) {
  636. TIFFErrorExt(tif->tif_clientdata, module,
  637. "PixarLog compression can't handle bits depth/data format combination (depth: %d)",
  638. td->td_bitspersample);
  639. return (0);
  640. }
  641. if (inflateInit(&sp->stream) != Z_OK) {
  642. TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg);
  643. return (0);
  644. } else {
  645. sp->state |= PLSTATE_INIT;
  646. return (1);
  647. }
  648. }
  649. /*
  650. * Setup state for decoding a strip.
  651. */
  652. static int
  653. PixarLogPreDecode(TIFF* tif, uint16 s)
  654. {
  655. static const char module[] = "PixarLogPreDecode";
  656. PixarLogState* sp = DecoderState(tif);
  657. (void) s;
  658. assert(sp != NULL);
  659. sp->stream.next_in = tif->tif_rawdata;
  660. assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
  661. we need to simplify this code to reflect a ZLib that is likely updated
  662. to deal with 8byte memory sizes, though this code will respond
  663. apropriately even before we simplify it */
  664. sp->stream.avail_in = (uInt) tif->tif_rawcc;
  665. if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc)
  666. {
  667. TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
  668. return (0);
  669. }
  670. return (inflateReset(&sp->stream) == Z_OK);
  671. }
  672. static int
  673. PixarLogDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
  674. {
  675. static const char module[] = "PixarLogDecode";
  676. TIFFDirectory *td = &tif->tif_dir;
  677. PixarLogState* sp = DecoderState(tif);
  678. tmsize_t i;
  679. tmsize_t nsamples;
  680. int llen;
  681. uint16 *up;
  682. switch (sp->user_datafmt) {
  683. case PIXARLOGDATAFMT_FLOAT:
  684. nsamples = occ / sizeof(float); /* XXX float == 32 bits */
  685. break;
  686. case PIXARLOGDATAFMT_16BIT:
  687. case PIXARLOGDATAFMT_12BITPICIO:
  688. case PIXARLOGDATAFMT_11BITLOG:
  689. nsamples = occ / sizeof(uint16); /* XXX uint16 == 16 bits */
  690. break;
  691. case PIXARLOGDATAFMT_8BIT:
  692. case PIXARLOGDATAFMT_8BITABGR:
  693. nsamples = occ;
  694. break;
  695. default:
  696. TIFFErrorExt(tif->tif_clientdata, module,
  697. "%d bit input not supported in PixarLog",
  698. td->td_bitspersample);
  699. return 0;
  700. }
  701. llen = sp->stride * td->td_imagewidth;
  702. (void) s;
  703. assert(sp != NULL);
  704. sp->stream.next_out = (unsigned char *) sp->tbuf;
  705. assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
  706. we need to simplify this code to reflect a ZLib that is likely updated
  707. to deal with 8byte memory sizes, though this code will respond
  708. apropriately even before we simplify it */
  709. sp->stream.avail_out = (uInt) (nsamples * sizeof(uint16));
  710. if (sp->stream.avail_out != nsamples * sizeof(uint16))
  711. {
  712. TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
  713. return (0);
  714. }
  715. do {
  716. int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
  717. if (state == Z_STREAM_END) {
  718. break; /* XXX */
  719. }
  720. if (state == Z_DATA_ERROR) {
  721. TIFFErrorExt(tif->tif_clientdata, module,
  722. "Decoding error at scanline %lu, %s",
  723. (unsigned long) tif->tif_row, sp->stream.msg);
  724. if (inflateSync(&sp->stream) != Z_OK)
  725. return (0);
  726. continue;
  727. }
  728. if (state != Z_OK) {
  729. TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
  730. sp->stream.msg);
  731. return (0);
  732. }
  733. } while (sp->stream.avail_out > 0);
  734. /* hopefully, we got all the bytes we needed */
  735. if (sp->stream.avail_out != 0) {
  736. TIFFErrorExt(tif->tif_clientdata, module,
  737. "Not enough data at scanline %lu (short " TIFF_UINT64_FORMAT " bytes)",
  738. (unsigned long) tif->tif_row, (TIFF_UINT64_T) sp->stream.avail_out);
  739. return (0);
  740. }
  741. up = sp->tbuf;
  742. /* Swap bytes in the data if from a different endian machine. */
  743. if (tif->tif_flags & TIFF_SWAB)
  744. TIFFSwabArrayOfShort(up, nsamples);
  745. /*
  746. * if llen is not an exact multiple of nsamples, the decode operation
  747. * may overflow the output buffer, so truncate it enough to prevent
  748. * that but still salvage as much data as possible.
  749. */
  750. if (nsamples % llen) {
  751. TIFFWarningExt(tif->tif_clientdata, module,
  752. "stride %lu is not a multiple of sample count, "
  753. "%lu, data truncated.", (unsigned long) llen, (unsigned long) nsamples);
  754. nsamples -= nsamples % llen;
  755. }
  756. for (i = 0; i < nsamples; i += llen, up += llen) {
  757. switch (sp->user_datafmt) {
  758. case PIXARLOGDATAFMT_FLOAT:
  759. horizontalAccumulateF(up, llen, sp->stride,
  760. (float *)op, sp->ToLinearF);
  761. op += llen * sizeof(float);
  762. break;
  763. case PIXARLOGDATAFMT_16BIT:
  764. horizontalAccumulate16(up, llen, sp->stride,
  765. (uint16 *)op, sp->ToLinear16);
  766. op += llen * sizeof(uint16);
  767. break;
  768. case PIXARLOGDATAFMT_12BITPICIO:
  769. horizontalAccumulate12(up, llen, sp->stride,
  770. (int16 *)op, sp->ToLinearF);
  771. op += llen * sizeof(int16);
  772. break;
  773. case PIXARLOGDATAFMT_11BITLOG:
  774. horizontalAccumulate11(up, llen, sp->stride,
  775. (uint16 *)op);
  776. op += llen * sizeof(uint16);
  777. break;
  778. case PIXARLOGDATAFMT_8BIT:
  779. horizontalAccumulate8(up, llen, sp->stride,
  780. (unsigned char *)op, sp->ToLinear8);
  781. op += llen * sizeof(unsigned char);
  782. break;
  783. case PIXARLOGDATAFMT_8BITABGR:
  784. horizontalAccumulate8abgr(up, llen, sp->stride,
  785. (unsigned char *)op, sp->ToLinear8);
  786. op += llen * sizeof(unsigned char);
  787. break;
  788. default:
  789. TIFFErrorExt(tif->tif_clientdata, module,
  790. "Unsupported bits/sample: %d",
  791. td->td_bitspersample);
  792. return (0);
  793. }
  794. }
  795. return (1);
  796. }
  797. static int
  798. PixarLogSetupEncode(TIFF* tif)
  799. {
  800. static const char module[] = "PixarLogSetupEncode";
  801. TIFFDirectory *td = &tif->tif_dir;
  802. PixarLogState* sp = EncoderState(tif);
  803. tmsize_t tbuf_size;
  804. assert(sp != NULL);
  805. /* for some reason, we can't do this in TIFFInitPixarLog */
  806. sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
  807. td->td_samplesperpixel : 1);
  808. tbuf_size = multiply_ms(multiply_ms(multiply_ms(sp->stride, td->td_imagewidth),
  809. td->td_rowsperstrip), sizeof(uint16));
  810. if (tbuf_size == 0)
  811. return (0); /* TODO: this is an error return without error report through TIFFErrorExt */
  812. sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size);
  813. if (sp->tbuf == NULL)
  814. return (0);
  815. if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
  816. sp->user_datafmt = PixarLogGuessDataFmt(td);
  817. if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) {
  818. TIFFErrorExt(tif->tif_clientdata, module, "PixarLog compression can't handle %d bit linear encodings", td->td_bitspersample);
  819. return (0);
  820. }
  821. if (deflateInit(&sp->stream, sp->quality) != Z_OK) {
  822. TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg);
  823. return (0);
  824. } else {
  825. sp->state |= PLSTATE_INIT;
  826. return (1);
  827. }
  828. }
  829. /*
  830. * Reset encoding state at the start of a strip.
  831. */
  832. static int
  833. PixarLogPreEncode(TIFF* tif, uint16 s)
  834. {
  835. static const char module[] = "PixarLogPreEncode";
  836. PixarLogState *sp = EncoderState(tif);
  837. (void) s;
  838. assert(sp != NULL);
  839. sp->stream.next_out = tif->tif_rawdata;
  840. assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
  841. we need to simplify this code to reflect a ZLib that is likely updated
  842. to deal with 8byte memory sizes, though this code will respond
  843. apropriately even before we simplify it */
  844. sp->stream.avail_out = tif->tif_rawdatasize;
  845. if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
  846. {
  847. TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
  848. return (0);
  849. }
  850. return (deflateReset(&sp->stream) == Z_OK);
  851. }
  852. static void
  853. horizontalDifferenceF(float *ip, int n, int stride, uint16 *wp, uint16 *FromLT2)
  854. {
  855. int32 r1, g1, b1, a1, r2, g2, b2, a2, mask;
  856. float fltsize = Fltsize;
  857. #define CLAMP(v) ( (v<(float)0.) ? 0 \
  858. : (v<(float)2.) ? FromLT2[(int)(v*fltsize)] \
  859. : (v>(float)24.2) ? 2047 \
  860. : LogK1*log(v*LogK2) + 0.5 )
  861. mask = CODE_MASK;
  862. if (n >= stride) {
  863. if (stride == 3) {
  864. r2 = wp[0] = (uint16) CLAMP(ip[0]);
  865. g2 = wp[1] = (uint16) CLAMP(ip[1]);
  866. b2 = wp[2] = (uint16) CLAMP(ip[2]);
  867. n -= 3;
  868. while (n > 0) {
  869. n -= 3;
  870. wp += 3;
  871. ip += 3;
  872. r1 = (int32) CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
  873. g1 = (int32) CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
  874. b1 = (int32) CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
  875. }
  876. } else if (stride == 4) {
  877. r2 = wp[0] = (uint16) CLAMP(ip[0]);
  878. g2 = wp[1] = (uint16) CLAMP(ip[1]);
  879. b2 = wp[2] = (uint16) CLAMP(ip[2]);
  880. a2 = wp[3] = (uint16) CLAMP(ip[3]);
  881. n -= 4;
  882. while (n > 0) {
  883. n -= 4;
  884. wp += 4;
  885. ip += 4;
  886. r1 = (int32) CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
  887. g1 = (int32) CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
  888. b1 = (int32) CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
  889. a1 = (int32) CLAMP(ip[3]); wp[3] = (a1-a2) & mask; a2 = a1;
  890. }
  891. } else {
  892. ip += n - 1; /* point to last one */
  893. wp += n - 1; /* point to last one */
  894. n -= stride;
  895. while (n > 0) {
  896. REPEAT(stride, wp[0] = (uint16) CLAMP(ip[0]);
  897. wp[stride] -= wp[0];
  898. wp[stride] &= mask;
  899. wp--; ip--)
  900. n -= stride;
  901. }
  902. REPEAT(stride, wp[0] = (uint16) CLAMP(ip[0]); wp--; ip--)
  903. }
  904. }
  905. }
  906. static void
  907. horizontalDifference16(unsigned short *ip, int n, int stride,
  908. unsigned short *wp, uint16 *From14)
  909. {
  910. register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
  911. /* assumption is unsigned pixel values */
  912. #undef CLAMP
  913. #define CLAMP(v) From14[(v) >> 2]
  914. mask = CODE_MASK;
  915. if (n >= stride) {
  916. if (stride == 3) {
  917. r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
  918. b2 = wp[2] = CLAMP(ip[2]);
  919. n -= 3;
  920. while (n > 0) {
  921. n -= 3;
  922. wp += 3;
  923. ip += 3;
  924. r1 = CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
  925. g1 = CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
  926. b1 = CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
  927. }
  928. } else if (stride == 4) {
  929. r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
  930. b2 = wp[2] = CLAMP(ip[2]); a2 = wp[3] = CLAMP(ip[3]);
  931. n -= 4;
  932. while (n > 0) {
  933. n -= 4;
  934. wp += 4;
  935. ip += 4;
  936. r1 = CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
  937. g1 = CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
  938. b1 = CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
  939. a1 = CLAMP(ip[3]); wp[3] = (a1-a2) & mask; a2 = a1;
  940. }
  941. } else {
  942. ip += n - 1; /* point to last one */
  943. wp += n - 1; /* point to last one */
  944. n -= stride;
  945. while (n > 0) {
  946. REPEAT(stride, wp[0] = CLAMP(ip[0]);
  947. wp[stride] -= wp[0];
  948. wp[stride] &= mask;
  949. wp--; ip--)
  950. n -= stride;
  951. }
  952. REPEAT(stride, wp[0] = CLAMP(ip[0]); wp--; ip--)
  953. }
  954. }
  955. }
  956. static void
  957. horizontalDifference8(unsigned char *ip, int n, int stride,
  958. unsigned short *wp, uint16 *From8)
  959. {
  960. register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
  961. #undef CLAMP
  962. #define CLAMP(v) (From8[(v)])
  963. mask = CODE_MASK;
  964. if (n >= stride) {
  965. if (stride == 3) {
  966. r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
  967. b2 = wp[2] = CLAMP(ip[2]);
  968. n -= 3;
  969. while (n > 0) {
  970. n -= 3;
  971. r1 = CLAMP(ip[3]); wp[3] = (r1-r2) & mask; r2 = r1;
  972. g1 = CLAMP(ip[4]); wp[4] = (g1-g2) & mask; g2 = g1;
  973. b1 = CLAMP(ip[5]); wp[5] = (b1-b2) & mask; b2 = b1;
  974. wp += 3;
  975. ip += 3;
  976. }
  977. } else if (stride == 4) {
  978. r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
  979. b2 = wp[2] = CLAMP(ip[2]); a2 = wp[3] = CLAMP(ip[3]);
  980. n -= 4;
  981. while (n > 0) {
  982. n -= 4;
  983. r1 = CLAMP(ip[4]); wp[4] = (r1-r2) & mask; r2 = r1;
  984. g1 = CLAMP(ip[5]); wp[5] = (g1-g2) & mask; g2 = g1;
  985. b1 = CLAMP(ip[6]); wp[6] = (b1-b2) & mask; b2 = b1;
  986. a1 = CLAMP(ip[7]); wp[7] = (a1-a2) & mask; a2 = a1;
  987. wp += 4;
  988. ip += 4;
  989. }
  990. } else {
  991. wp += n + stride - 1; /* point to last one */
  992. ip += n + stride - 1; /* point to last one */
  993. n -= stride;
  994. while (n > 0) {
  995. REPEAT(stride, wp[0] = CLAMP(ip[0]);
  996. wp[stride] -= wp[0];
  997. wp[stride] &= mask;
  998. wp--; ip--)
  999. n -= stride;
  1000. }
  1001. REPEAT(stride, wp[0] = CLAMP(ip[0]); wp--; ip--)
  1002. }
  1003. }
  1004. }
  1005. /*
  1006. * Encode a chunk of pixels.
  1007. */
  1008. static int
  1009. PixarLogEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  1010. {
  1011. static const char module[] = "PixarLogEncode";
  1012. TIFFDirectory *td = &tif->tif_dir;
  1013. PixarLogState *sp = EncoderState(tif);
  1014. tmsize_t i;
  1015. tmsize_t n;
  1016. int llen;
  1017. unsigned short * up;
  1018. (void) s;
  1019. switch (sp->user_datafmt) {
  1020. case PIXARLOGDATAFMT_FLOAT:
  1021. n = cc / sizeof(float); /* XXX float == 32 bits */
  1022. break;
  1023. case PIXARLOGDATAFMT_16BIT:
  1024. case PIXARLOGDATAFMT_12BITPICIO:
  1025. case PIXARLOGDATAFMT_11BITLOG:
  1026. n = cc / sizeof(uint16); /* XXX uint16 == 16 bits */
  1027. break;
  1028. case PIXARLOGDATAFMT_8BIT:
  1029. case PIXARLOGDATAFMT_8BITABGR:
  1030. n = cc;
  1031. break;
  1032. default:
  1033. TIFFErrorExt(tif->tif_clientdata, module,
  1034. "%d bit input not supported in PixarLog",
  1035. td->td_bitspersample);
  1036. return 0;
  1037. }
  1038. llen = sp->stride * td->td_imagewidth;
  1039. for (i = 0, up = sp->tbuf; i < n; i += llen, up += llen) {
  1040. switch (sp->user_datafmt) {
  1041. case PIXARLOGDATAFMT_FLOAT:
  1042. horizontalDifferenceF((float *)bp, llen,
  1043. sp->stride, up, sp->FromLT2);
  1044. bp += llen * sizeof(float);
  1045. break;
  1046. case PIXARLOGDATAFMT_16BIT:
  1047. horizontalDifference16((uint16 *)bp, llen,
  1048. sp->stride, up, sp->From14);
  1049. bp += llen * sizeof(uint16);
  1050. break;
  1051. case PIXARLOGDATAFMT_8BIT:
  1052. horizontalDifference8((unsigned char *)bp, llen,
  1053. sp->stride, up, sp->From8);
  1054. bp += llen * sizeof(unsigned char);
  1055. break;
  1056. default:
  1057. TIFFErrorExt(tif->tif_clientdata, module,
  1058. "%d bit input not supported in PixarLog",
  1059. td->td_bitspersample);
  1060. return 0;
  1061. }
  1062. }
  1063. sp->stream.next_in = (unsigned char *) sp->tbuf;
  1064. assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
  1065. we need to simplify this code to reflect a ZLib that is likely updated
  1066. to deal with 8byte memory sizes, though this code will respond
  1067. apropriately even before we simplify it */
  1068. sp->stream.avail_in = (uInt) (n * sizeof(uint16));
  1069. if ((sp->stream.avail_in / sizeof(uint16)) != (uInt) n)
  1070. {
  1071. TIFFErrorExt(tif->tif_clientdata, module,
  1072. "ZLib cannot deal with buffers this size");
  1073. return (0);
  1074. }
  1075. do {
  1076. if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
  1077. TIFFErrorExt(tif->tif_clientdata, module, "Encoder error: %s",
  1078. sp->stream.msg);
  1079. return (0);
  1080. }
  1081. if (sp->stream.avail_out == 0) {
  1082. tif->tif_rawcc = tif->tif_rawdatasize;
  1083. TIFFFlushData1(tif);
  1084. sp->stream.next_out = tif->tif_rawdata;
  1085. sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in PixarLogPreEncode */
  1086. }
  1087. } while (sp->stream.avail_in > 0);
  1088. return (1);
  1089. }
  1090. /*
  1091. * Finish off an encoded strip by flushing the last
  1092. * string and tacking on an End Of Information code.
  1093. */
  1094. static int
  1095. PixarLogPostEncode(TIFF* tif)
  1096. {
  1097. static const char module[] = "PixarLogPostEncode";
  1098. PixarLogState *sp = EncoderState(tif);
  1099. int state;
  1100. sp->stream.avail_in = 0;
  1101. do {
  1102. state = deflate(&sp->stream, Z_FINISH);
  1103. switch (state) {
  1104. case Z_STREAM_END:
  1105. case Z_OK:
  1106. if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) {
  1107. tif->tif_rawcc =
  1108. tif->tif_rawdatasize - sp->stream.avail_out;
  1109. TIFFFlushData1(tif);
  1110. sp->stream.next_out = tif->tif_rawdata;
  1111. sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in PixarLogPreEncode */
  1112. }
  1113. break;
  1114. default:
  1115. TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
  1116. sp->stream.msg);
  1117. return (0);
  1118. }
  1119. } while (state != Z_STREAM_END);
  1120. return (1);
  1121. }
  1122. static void
  1123. PixarLogClose(TIFF* tif)
  1124. {
  1125. TIFFDirectory *td = &tif->tif_dir;
  1126. /* In a really sneaky (and really incorrect, and untruthfull, and
  1127. * troublesome, and error-prone) maneuver that completely goes against
  1128. * the spirit of TIFF, and breaks TIFF, on close, we covertly
  1129. * modify both bitspersample and sampleformat in the directory to
  1130. * indicate 8-bit linear. This way, the decode "just works" even for
  1131. * readers that don't know about PixarLog, or how to set
  1132. * the PIXARLOGDATFMT pseudo-tag.
  1133. */
  1134. td->td_bitspersample = 8;
  1135. td->td_sampleformat = SAMPLEFORMAT_UINT;
  1136. }
  1137. static void
  1138. PixarLogCleanup(TIFF* tif)
  1139. {
  1140. PixarLogState* sp = (PixarLogState*) tif->tif_data;
  1141. assert(sp != 0);
  1142. (void)TIFFPredictorCleanup(tif);
  1143. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  1144. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  1145. if (sp->FromLT2) _TIFFfree(sp->FromLT2);
  1146. if (sp->From14) _TIFFfree(sp->From14);
  1147. if (sp->From8) _TIFFfree(sp->From8);
  1148. if (sp->ToLinearF) _TIFFfree(sp->ToLinearF);
  1149. if (sp->ToLinear16) _TIFFfree(sp->ToLinear16);
  1150. if (sp->ToLinear8) _TIFFfree(sp->ToLinear8);
  1151. if (sp->state&PLSTATE_INIT) {
  1152. if (tif->tif_mode == O_RDONLY)
  1153. inflateEnd(&sp->stream);
  1154. else
  1155. deflateEnd(&sp->stream);
  1156. }
  1157. if (sp->tbuf)
  1158. _TIFFfree(sp->tbuf);
  1159. _TIFFfree(sp);
  1160. tif->tif_data = NULL;
  1161. _TIFFSetDefaultCompressionState(tif);
  1162. }
  1163. static int
  1164. PixarLogVSetField(TIFF* tif, uint32 tag, va_list ap)
  1165. {
  1166. static const char module[] = "PixarLogVSetField";
  1167. PixarLogState *sp = (PixarLogState *)tif->tif_data;
  1168. int result;
  1169. switch (tag) {
  1170. case TIFFTAG_PIXARLOGQUALITY:
  1171. sp->quality = (int) va_arg(ap, int);
  1172. if (tif->tif_mode != O_RDONLY && (sp->state&PLSTATE_INIT)) {
  1173. if (deflateParams(&sp->stream,
  1174. sp->quality, Z_DEFAULT_STRATEGY) != Z_OK) {
  1175. TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
  1176. sp->stream.msg);
  1177. return (0);
  1178. }
  1179. }
  1180. return (1);
  1181. case TIFFTAG_PIXARLOGDATAFMT:
  1182. sp->user_datafmt = (int) va_arg(ap, int);
  1183. /* Tweak the TIFF header so that the rest of libtiff knows what
  1184. * size of data will be passed between app and library, and
  1185. * assume that the app knows what it is doing and is not
  1186. * confused by these header manipulations...
  1187. */
  1188. switch (sp->user_datafmt) {
  1189. case PIXARLOGDATAFMT_8BIT:
  1190. case PIXARLOGDATAFMT_8BITABGR:
  1191. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
  1192. TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
  1193. break;
  1194. case PIXARLOGDATAFMT_11BITLOG:
  1195. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
  1196. TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
  1197. break;
  1198. case PIXARLOGDATAFMT_12BITPICIO:
  1199. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
  1200. TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
  1201. break;
  1202. case PIXARLOGDATAFMT_16BIT:
  1203. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
  1204. TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
  1205. break;
  1206. case PIXARLOGDATAFMT_FLOAT:
  1207. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32);
  1208. TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
  1209. break;
  1210. }
  1211. /*
  1212. * Must recalculate sizes should bits/sample change.
  1213. */
  1214. tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);
  1215. tif->tif_scanlinesize = TIFFScanlineSize(tif);
  1216. result = 1; /* NB: pseudo tag */
  1217. break;
  1218. default:
  1219. result = (*sp->vsetparent)(tif, tag, ap);
  1220. }
  1221. return (result);
  1222. }
  1223. static int
  1224. PixarLogVGetField(TIFF* tif, uint32 tag, va_list ap)
  1225. {
  1226. PixarLogState *sp = (PixarLogState *)tif->tif_data;
  1227. switch (tag) {
  1228. case TIFFTAG_PIXARLOGQUALITY:
  1229. *va_arg(ap, int*) = sp->quality;
  1230. break;
  1231. case TIFFTAG_PIXARLOGDATAFMT:
  1232. *va_arg(ap, int*) = sp->user_datafmt;
  1233. break;
  1234. default:
  1235. return (*sp->vgetparent)(tif, tag, ap);
  1236. }
  1237. return (1);
  1238. }
  1239. static const TIFFField pixarlogFields[] = {
  1240. {TIFFTAG_PIXARLOGDATAFMT, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL},
  1241. {TIFFTAG_PIXARLOGQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL}
  1242. };
  1243. int
  1244. TIFFInitPixarLog(TIFF* tif, int scheme)
  1245. {
  1246. static const char module[] = "TIFFInitPixarLog";
  1247. PixarLogState* sp;
  1248. assert(scheme == COMPRESSION_PIXARLOG);
  1249. /*
  1250. * Merge codec-specific tag information.
  1251. */
  1252. if (!_TIFFMergeFields(tif, pixarlogFields,
  1253. TIFFArrayCount(pixarlogFields))) {
  1254. TIFFErrorExt(tif->tif_clientdata, module,
  1255. "Merging PixarLog codec-specific tags failed");
  1256. return 0;
  1257. }
  1258. /*
  1259. * Allocate state block so tag methods have storage to record values.
  1260. */
  1261. tif->tif_data = (uint8*) _TIFFmalloc(sizeof (PixarLogState));
  1262. if (tif->tif_data == NULL)
  1263. goto bad;
  1264. sp = (PixarLogState*) tif->tif_data;
  1265. _TIFFmemset(sp, 0, sizeof (*sp));
  1266. sp->stream.data_type = Z_BINARY;
  1267. sp->user_datafmt = PIXARLOGDATAFMT_UNKNOWN;
  1268. /*
  1269. * Install codec methods.
  1270. */
  1271. tif->tif_fixuptags = PixarLogFixupTags;
  1272. tif->tif_setupdecode = PixarLogSetupDecode;
  1273. tif->tif_predecode = PixarLogPreDecode;
  1274. tif->tif_decoderow = PixarLogDecode;
  1275. tif->tif_decodestrip = PixarLogDecode;
  1276. tif->tif_decodetile = PixarLogDecode;
  1277. tif->tif_setupencode = PixarLogSetupEncode;
  1278. tif->tif_preencode = PixarLogPreEncode;
  1279. tif->tif_postencode = PixarLogPostEncode;
  1280. tif->tif_encoderow = PixarLogEncode;
  1281. tif->tif_encodestrip = PixarLogEncode;
  1282. tif->tif_encodetile = PixarLogEncode;
  1283. tif->tif_close = PixarLogClose;
  1284. tif->tif_cleanup = PixarLogCleanup;
  1285. /* Override SetField so we can handle our private pseudo-tag */
  1286. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  1287. tif->tif_tagmethods.vgetfield = PixarLogVGetField; /* hook for codec tags */
  1288. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  1289. tif->tif_tagmethods.vsetfield = PixarLogVSetField; /* hook for codec tags */
  1290. /* Default values for codec-specific fields */
  1291. sp->quality = Z_DEFAULT_COMPRESSION; /* default comp. level */
  1292. sp->state = 0;
  1293. /* we don't wish to use the predictor,
  1294. * the default is none, which predictor value 1
  1295. */
  1296. (void) TIFFPredictorInit(tif);
  1297. /*
  1298. * build the companding tables
  1299. */
  1300. PixarLogMakeTables(sp);
  1301. return (1);
  1302. bad:
  1303. TIFFErrorExt(tif->tif_clientdata, module,
  1304. "No space for PixarLog state block");
  1305. return (0);
  1306. }
  1307. #endif /* PIXARLOG_SUPPORT */
  1308. /* vim: set ts=8 sts=8 sw=8 noet: */
  1309. /*
  1310. * Local Variables:
  1311. * mode: c
  1312. * c-basic-offset: 8
  1313. * fill-column: 78
  1314. * End:
  1315. */