2
0

tif_pixarlog.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. /* $Id: tif_pixarlog.c,v 1.37 2012-05-24 23:21:45 fwarmerdam 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 int
  594. PixarLogFixupTags(TIFF* tif)
  595. {
  596. (void) tif;
  597. return (1);
  598. }
  599. static int
  600. PixarLogSetupDecode(TIFF* tif)
  601. {
  602. static const char module[] = "PixarLogSetupDecode";
  603. TIFFDirectory *td = &tif->tif_dir;
  604. PixarLogState* sp = DecoderState(tif);
  605. tmsize_t tbuf_size;
  606. assert(sp != NULL);
  607. /* Make sure no byte swapping happens on the data
  608. * after decompression. */
  609. tif->tif_postdecode = _TIFFNoPostDecode;
  610. /* for some reason, we can't do this in TIFFInitPixarLog */
  611. sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
  612. td->td_samplesperpixel : 1);
  613. tbuf_size = multiply_ms(multiply_ms(multiply_ms(sp->stride, td->td_imagewidth),
  614. td->td_rowsperstrip), sizeof(uint16));
  615. if (tbuf_size == 0)
  616. return (0); /* TODO: this is an error return without error report through TIFFErrorExt */
  617. sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size+sizeof(uint16));
  618. if (sp->tbuf == NULL)
  619. return (0);
  620. if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
  621. sp->user_datafmt = PixarLogGuessDataFmt(td);
  622. if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) {
  623. TIFFErrorExt(tif->tif_clientdata, module,
  624. "PixarLog compression can't handle bits depth/data format combination (depth: %d)",
  625. td->td_bitspersample);
  626. return (0);
  627. }
  628. if (inflateInit(&sp->stream) != Z_OK) {
  629. TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg);
  630. return (0);
  631. } else {
  632. sp->state |= PLSTATE_INIT;
  633. return (1);
  634. }
  635. }
  636. /*
  637. * Setup state for decoding a strip.
  638. */
  639. static int
  640. PixarLogPreDecode(TIFF* tif, uint16 s)
  641. {
  642. static const char module[] = "PixarLogPreDecode";
  643. PixarLogState* sp = DecoderState(tif);
  644. (void) s;
  645. assert(sp != NULL);
  646. sp->stream.next_in = tif->tif_rawdata;
  647. assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
  648. we need to simplify this code to reflect a ZLib that is likely updated
  649. to deal with 8byte memory sizes, though this code will respond
  650. apropriately even before we simplify it */
  651. sp->stream.avail_in = (uInt) tif->tif_rawcc;
  652. if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc)
  653. {
  654. TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
  655. return (0);
  656. }
  657. return (inflateReset(&sp->stream) == Z_OK);
  658. }
  659. static int
  660. PixarLogDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
  661. {
  662. static const char module[] = "PixarLogDecode";
  663. TIFFDirectory *td = &tif->tif_dir;
  664. PixarLogState* sp = DecoderState(tif);
  665. tmsize_t i;
  666. tmsize_t nsamples;
  667. int llen;
  668. uint16 *up;
  669. switch (sp->user_datafmt) {
  670. case PIXARLOGDATAFMT_FLOAT:
  671. nsamples = occ / sizeof(float); /* XXX float == 32 bits */
  672. break;
  673. case PIXARLOGDATAFMT_16BIT:
  674. case PIXARLOGDATAFMT_12BITPICIO:
  675. case PIXARLOGDATAFMT_11BITLOG:
  676. nsamples = occ / sizeof(uint16); /* XXX uint16 == 16 bits */
  677. break;
  678. case PIXARLOGDATAFMT_8BIT:
  679. case PIXARLOGDATAFMT_8BITABGR:
  680. nsamples = occ;
  681. break;
  682. default:
  683. TIFFErrorExt(tif->tif_clientdata, module,
  684. "%d bit input not supported in PixarLog",
  685. td->td_bitspersample);
  686. return 0;
  687. }
  688. llen = sp->stride * td->td_imagewidth;
  689. (void) s;
  690. assert(sp != NULL);
  691. sp->stream.next_out = (unsigned char *) sp->tbuf;
  692. assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
  693. we need to simplify this code to reflect a ZLib that is likely updated
  694. to deal with 8byte memory sizes, though this code will respond
  695. apropriately even before we simplify it */
  696. sp->stream.avail_out = (uInt) (nsamples * sizeof(uint16));
  697. if (sp->stream.avail_out != nsamples * sizeof(uint16))
  698. {
  699. TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
  700. return (0);
  701. }
  702. do {
  703. int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
  704. if (state == Z_STREAM_END) {
  705. break; /* XXX */
  706. }
  707. if (state == Z_DATA_ERROR) {
  708. TIFFErrorExt(tif->tif_clientdata, module,
  709. "Decoding error at scanline %lu, %s",
  710. (unsigned long) tif->tif_row, sp->stream.msg);
  711. if (inflateSync(&sp->stream) != Z_OK)
  712. return (0);
  713. continue;
  714. }
  715. if (state != Z_OK) {
  716. TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
  717. sp->stream.msg);
  718. return (0);
  719. }
  720. } while (sp->stream.avail_out > 0);
  721. /* hopefully, we got all the bytes we needed */
  722. if (sp->stream.avail_out != 0) {
  723. TIFFErrorExt(tif->tif_clientdata, module,
  724. "Not enough data at scanline %lu (short " TIFF_UINT64_FORMAT " bytes)",
  725. (unsigned long) tif->tif_row, (TIFF_UINT64_T) sp->stream.avail_out);
  726. return (0);
  727. }
  728. up = sp->tbuf;
  729. /* Swap bytes in the data if from a different endian machine. */
  730. if (tif->tif_flags & TIFF_SWAB)
  731. TIFFSwabArrayOfShort(up, nsamples);
  732. /*
  733. * if llen is not an exact multiple of nsamples, the decode operation
  734. * may overflow the output buffer, so truncate it enough to prevent
  735. * that but still salvage as much data as possible.
  736. */
  737. if (nsamples % llen) {
  738. TIFFWarningExt(tif->tif_clientdata, module,
  739. "stride %lu is not a multiple of sample count, "
  740. "%lu, data truncated.", (unsigned long) llen, (unsigned long) nsamples);
  741. nsamples -= nsamples % llen;
  742. }
  743. for (i = 0; i < nsamples; i += llen, up += llen) {
  744. switch (sp->user_datafmt) {
  745. case PIXARLOGDATAFMT_FLOAT:
  746. horizontalAccumulateF(up, llen, sp->stride,
  747. (float *)op, sp->ToLinearF);
  748. op += llen * sizeof(float);
  749. break;
  750. case PIXARLOGDATAFMT_16BIT:
  751. horizontalAccumulate16(up, llen, sp->stride,
  752. (uint16 *)op, sp->ToLinear16);
  753. op += llen * sizeof(uint16);
  754. break;
  755. case PIXARLOGDATAFMT_12BITPICIO:
  756. horizontalAccumulate12(up, llen, sp->stride,
  757. (int16 *)op, sp->ToLinearF);
  758. op += llen * sizeof(int16);
  759. break;
  760. case PIXARLOGDATAFMT_11BITLOG:
  761. horizontalAccumulate11(up, llen, sp->stride,
  762. (uint16 *)op);
  763. op += llen * sizeof(uint16);
  764. break;
  765. case PIXARLOGDATAFMT_8BIT:
  766. horizontalAccumulate8(up, llen, sp->stride,
  767. (unsigned char *)op, sp->ToLinear8);
  768. op += llen * sizeof(unsigned char);
  769. break;
  770. case PIXARLOGDATAFMT_8BITABGR:
  771. horizontalAccumulate8abgr(up, llen, sp->stride,
  772. (unsigned char *)op, sp->ToLinear8);
  773. op += llen * sizeof(unsigned char);
  774. break;
  775. default:
  776. TIFFErrorExt(tif->tif_clientdata, module,
  777. "Unsupported bits/sample: %d",
  778. td->td_bitspersample);
  779. return (0);
  780. }
  781. }
  782. return (1);
  783. }
  784. static int
  785. PixarLogSetupEncode(TIFF* tif)
  786. {
  787. static const char module[] = "PixarLogSetupEncode";
  788. TIFFDirectory *td = &tif->tif_dir;
  789. PixarLogState* sp = EncoderState(tif);
  790. tmsize_t tbuf_size;
  791. assert(sp != NULL);
  792. /* for some reason, we can't do this in TIFFInitPixarLog */
  793. sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
  794. td->td_samplesperpixel : 1);
  795. tbuf_size = multiply_ms(multiply_ms(multiply_ms(sp->stride, td->td_imagewidth),
  796. td->td_rowsperstrip), sizeof(uint16));
  797. if (tbuf_size == 0)
  798. return (0); /* TODO: this is an error return without error report through TIFFErrorExt */
  799. sp->tbuf = (uint16 *) _TIFFmalloc(tbuf_size);
  800. if (sp->tbuf == NULL)
  801. return (0);
  802. if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
  803. sp->user_datafmt = PixarLogGuessDataFmt(td);
  804. if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN) {
  805. TIFFErrorExt(tif->tif_clientdata, module, "PixarLog compression can't handle %d bit linear encodings", td->td_bitspersample);
  806. return (0);
  807. }
  808. if (deflateInit(&sp->stream, sp->quality) != Z_OK) {
  809. TIFFErrorExt(tif->tif_clientdata, module, "%s", sp->stream.msg);
  810. return (0);
  811. } else {
  812. sp->state |= PLSTATE_INIT;
  813. return (1);
  814. }
  815. }
  816. /*
  817. * Reset encoding state at the start of a strip.
  818. */
  819. static int
  820. PixarLogPreEncode(TIFF* tif, uint16 s)
  821. {
  822. static const char module[] = "PixarLogPreEncode";
  823. PixarLogState *sp = EncoderState(tif);
  824. (void) s;
  825. assert(sp != NULL);
  826. sp->stream.next_out = tif->tif_rawdata;
  827. assert(sizeof(sp->stream.avail_out)==4); /* if this assert gets raised,
  828. we need to simplify this code to reflect a ZLib that is likely updated
  829. to deal with 8byte memory sizes, though this code will respond
  830. apropriately even before we simplify it */
  831. sp->stream.avail_out = tif->tif_rawdatasize;
  832. if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
  833. {
  834. TIFFErrorExt(tif->tif_clientdata, module, "ZLib cannot deal with buffers this size");
  835. return (0);
  836. }
  837. return (deflateReset(&sp->stream) == Z_OK);
  838. }
  839. static void
  840. horizontalDifferenceF(float *ip, int n, int stride, uint16 *wp, uint16 *FromLT2)
  841. {
  842. int32 r1, g1, b1, a1, r2, g2, b2, a2, mask;
  843. float fltsize = Fltsize;
  844. #define CLAMP(v) ( (v<(float)0.) ? 0 \
  845. : (v<(float)2.) ? FromLT2[(int)(v*fltsize)] \
  846. : (v>(float)24.2) ? 2047 \
  847. : LogK1*log(v*LogK2) + 0.5 )
  848. mask = CODE_MASK;
  849. if (n >= stride) {
  850. if (stride == 3) {
  851. r2 = wp[0] = (uint16) CLAMP(ip[0]);
  852. g2 = wp[1] = (uint16) CLAMP(ip[1]);
  853. b2 = wp[2] = (uint16) CLAMP(ip[2]);
  854. n -= 3;
  855. while (n > 0) {
  856. n -= 3;
  857. wp += 3;
  858. ip += 3;
  859. r1 = (int32) CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
  860. g1 = (int32) CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
  861. b1 = (int32) CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
  862. }
  863. } else if (stride == 4) {
  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. a2 = wp[3] = (uint16) CLAMP(ip[3]);
  868. n -= 4;
  869. while (n > 0) {
  870. n -= 4;
  871. wp += 4;
  872. ip += 4;
  873. r1 = (int32) CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
  874. g1 = (int32) CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
  875. b1 = (int32) CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
  876. a1 = (int32) CLAMP(ip[3]); wp[3] = (a1-a2) & mask; a2 = a1;
  877. }
  878. } else {
  879. ip += n - 1; /* point to last one */
  880. wp += n - 1; /* point to last one */
  881. n -= stride;
  882. while (n > 0) {
  883. REPEAT(stride, wp[0] = (uint16) CLAMP(ip[0]);
  884. wp[stride] -= wp[0];
  885. wp[stride] &= mask;
  886. wp--; ip--)
  887. n -= stride;
  888. }
  889. REPEAT(stride, wp[0] = (uint16) CLAMP(ip[0]); wp--; ip--)
  890. }
  891. }
  892. }
  893. static void
  894. horizontalDifference16(unsigned short *ip, int n, int stride,
  895. unsigned short *wp, uint16 *From14)
  896. {
  897. register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
  898. /* assumption is unsigned pixel values */
  899. #undef CLAMP
  900. #define CLAMP(v) From14[(v) >> 2]
  901. mask = CODE_MASK;
  902. if (n >= stride) {
  903. if (stride == 3) {
  904. r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
  905. b2 = wp[2] = CLAMP(ip[2]);
  906. n -= 3;
  907. while (n > 0) {
  908. n -= 3;
  909. wp += 3;
  910. ip += 3;
  911. r1 = CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
  912. g1 = CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
  913. b1 = CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
  914. }
  915. } else if (stride == 4) {
  916. r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
  917. b2 = wp[2] = CLAMP(ip[2]); a2 = wp[3] = CLAMP(ip[3]);
  918. n -= 4;
  919. while (n > 0) {
  920. n -= 4;
  921. wp += 4;
  922. ip += 4;
  923. r1 = CLAMP(ip[0]); wp[0] = (r1-r2) & mask; r2 = r1;
  924. g1 = CLAMP(ip[1]); wp[1] = (g1-g2) & mask; g2 = g1;
  925. b1 = CLAMP(ip[2]); wp[2] = (b1-b2) & mask; b2 = b1;
  926. a1 = CLAMP(ip[3]); wp[3] = (a1-a2) & mask; a2 = a1;
  927. }
  928. } else {
  929. ip += n - 1; /* point to last one */
  930. wp += n - 1; /* point to last one */
  931. n -= stride;
  932. while (n > 0) {
  933. REPEAT(stride, wp[0] = CLAMP(ip[0]);
  934. wp[stride] -= wp[0];
  935. wp[stride] &= mask;
  936. wp--; ip--)
  937. n -= stride;
  938. }
  939. REPEAT(stride, wp[0] = CLAMP(ip[0]); wp--; ip--)
  940. }
  941. }
  942. }
  943. static void
  944. horizontalDifference8(unsigned char *ip, int n, int stride,
  945. unsigned short *wp, uint16 *From8)
  946. {
  947. register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
  948. #undef CLAMP
  949. #define CLAMP(v) (From8[(v)])
  950. mask = CODE_MASK;
  951. if (n >= stride) {
  952. if (stride == 3) {
  953. r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
  954. b2 = wp[2] = CLAMP(ip[2]);
  955. n -= 3;
  956. while (n > 0) {
  957. n -= 3;
  958. r1 = CLAMP(ip[3]); wp[3] = (r1-r2) & mask; r2 = r1;
  959. g1 = CLAMP(ip[4]); wp[4] = (g1-g2) & mask; g2 = g1;
  960. b1 = CLAMP(ip[5]); wp[5] = (b1-b2) & mask; b2 = b1;
  961. wp += 3;
  962. ip += 3;
  963. }
  964. } else if (stride == 4) {
  965. r2 = wp[0] = CLAMP(ip[0]); g2 = wp[1] = CLAMP(ip[1]);
  966. b2 = wp[2] = CLAMP(ip[2]); a2 = wp[3] = CLAMP(ip[3]);
  967. n -= 4;
  968. while (n > 0) {
  969. n -= 4;
  970. r1 = CLAMP(ip[4]); wp[4] = (r1-r2) & mask; r2 = r1;
  971. g1 = CLAMP(ip[5]); wp[5] = (g1-g2) & mask; g2 = g1;
  972. b1 = CLAMP(ip[6]); wp[6] = (b1-b2) & mask; b2 = b1;
  973. a1 = CLAMP(ip[7]); wp[7] = (a1-a2) & mask; a2 = a1;
  974. wp += 4;
  975. ip += 4;
  976. }
  977. } else {
  978. wp += n + stride - 1; /* point to last one */
  979. ip += n + stride - 1; /* point to last one */
  980. n -= stride;
  981. while (n > 0) {
  982. REPEAT(stride, wp[0] = CLAMP(ip[0]);
  983. wp[stride] -= wp[0];
  984. wp[stride] &= mask;
  985. wp--; ip--)
  986. n -= stride;
  987. }
  988. REPEAT(stride, wp[0] = CLAMP(ip[0]); wp--; ip--)
  989. }
  990. }
  991. }
  992. /*
  993. * Encode a chunk of pixels.
  994. */
  995. static int
  996. PixarLogEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  997. {
  998. static const char module[] = "PixarLogEncode";
  999. TIFFDirectory *td = &tif->tif_dir;
  1000. PixarLogState *sp = EncoderState(tif);
  1001. tmsize_t i;
  1002. tmsize_t n;
  1003. int llen;
  1004. unsigned short * up;
  1005. (void) s;
  1006. switch (sp->user_datafmt) {
  1007. case PIXARLOGDATAFMT_FLOAT:
  1008. n = cc / sizeof(float); /* XXX float == 32 bits */
  1009. break;
  1010. case PIXARLOGDATAFMT_16BIT:
  1011. case PIXARLOGDATAFMT_12BITPICIO:
  1012. case PIXARLOGDATAFMT_11BITLOG:
  1013. n = cc / sizeof(uint16); /* XXX uint16 == 16 bits */
  1014. break;
  1015. case PIXARLOGDATAFMT_8BIT:
  1016. case PIXARLOGDATAFMT_8BITABGR:
  1017. n = cc;
  1018. break;
  1019. default:
  1020. TIFFErrorExt(tif->tif_clientdata, module,
  1021. "%d bit input not supported in PixarLog",
  1022. td->td_bitspersample);
  1023. return 0;
  1024. }
  1025. llen = sp->stride * td->td_imagewidth;
  1026. for (i = 0, up = sp->tbuf; i < n; i += llen, up += llen) {
  1027. switch (sp->user_datafmt) {
  1028. case PIXARLOGDATAFMT_FLOAT:
  1029. horizontalDifferenceF((float *)bp, llen,
  1030. sp->stride, up, sp->FromLT2);
  1031. bp += llen * sizeof(float);
  1032. break;
  1033. case PIXARLOGDATAFMT_16BIT:
  1034. horizontalDifference16((uint16 *)bp, llen,
  1035. sp->stride, up, sp->From14);
  1036. bp += llen * sizeof(uint16);
  1037. break;
  1038. case PIXARLOGDATAFMT_8BIT:
  1039. horizontalDifference8((unsigned char *)bp, llen,
  1040. sp->stride, up, sp->From8);
  1041. bp += llen * sizeof(unsigned char);
  1042. break;
  1043. default:
  1044. TIFFErrorExt(tif->tif_clientdata, module,
  1045. "%d bit input not supported in PixarLog",
  1046. td->td_bitspersample);
  1047. return 0;
  1048. }
  1049. }
  1050. sp->stream.next_in = (unsigned char *) sp->tbuf;
  1051. assert(sizeof(sp->stream.avail_in)==4); /* if this assert gets raised,
  1052. we need to simplify this code to reflect a ZLib that is likely updated
  1053. to deal with 8byte memory sizes, though this code will respond
  1054. apropriately even before we simplify it */
  1055. sp->stream.avail_in = (uInt) (n * sizeof(uint16));
  1056. if ((sp->stream.avail_in / sizeof(uint16)) != (uInt) n)
  1057. {
  1058. TIFFErrorExt(tif->tif_clientdata, module,
  1059. "ZLib cannot deal with buffers this size");
  1060. return (0);
  1061. }
  1062. do {
  1063. if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK) {
  1064. TIFFErrorExt(tif->tif_clientdata, module, "Encoder error: %s",
  1065. sp->stream.msg);
  1066. return (0);
  1067. }
  1068. if (sp->stream.avail_out == 0) {
  1069. tif->tif_rawcc = tif->tif_rawdatasize;
  1070. TIFFFlushData1(tif);
  1071. sp->stream.next_out = tif->tif_rawdata;
  1072. sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in PixarLogPreEncode */
  1073. }
  1074. } while (sp->stream.avail_in > 0);
  1075. return (1);
  1076. }
  1077. /*
  1078. * Finish off an encoded strip by flushing the last
  1079. * string and tacking on an End Of Information code.
  1080. */
  1081. static int
  1082. PixarLogPostEncode(TIFF* tif)
  1083. {
  1084. static const char module[] = "PixarLogPostEncode";
  1085. PixarLogState *sp = EncoderState(tif);
  1086. int state;
  1087. sp->stream.avail_in = 0;
  1088. do {
  1089. state = deflate(&sp->stream, Z_FINISH);
  1090. switch (state) {
  1091. case Z_STREAM_END:
  1092. case Z_OK:
  1093. if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) {
  1094. tif->tif_rawcc =
  1095. tif->tif_rawdatasize - sp->stream.avail_out;
  1096. TIFFFlushData1(tif);
  1097. sp->stream.next_out = tif->tif_rawdata;
  1098. sp->stream.avail_out = (uInt) tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in PixarLogPreEncode */
  1099. }
  1100. break;
  1101. default:
  1102. TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
  1103. sp->stream.msg);
  1104. return (0);
  1105. }
  1106. } while (state != Z_STREAM_END);
  1107. return (1);
  1108. }
  1109. static void
  1110. PixarLogClose(TIFF* tif)
  1111. {
  1112. TIFFDirectory *td = &tif->tif_dir;
  1113. /* In a really sneaky (and really incorrect, and untruthfull, and
  1114. * troublesome, and error-prone) maneuver that completely goes against
  1115. * the spirit of TIFF, and breaks TIFF, on close, we covertly
  1116. * modify both bitspersample and sampleformat in the directory to
  1117. * indicate 8-bit linear. This way, the decode "just works" even for
  1118. * readers that don't know about PixarLog, or how to set
  1119. * the PIXARLOGDATFMT pseudo-tag.
  1120. */
  1121. td->td_bitspersample = 8;
  1122. td->td_sampleformat = SAMPLEFORMAT_UINT;
  1123. }
  1124. static void
  1125. PixarLogCleanup(TIFF* tif)
  1126. {
  1127. PixarLogState* sp = (PixarLogState*) tif->tif_data;
  1128. assert(sp != 0);
  1129. (void)TIFFPredictorCleanup(tif);
  1130. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  1131. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  1132. if (sp->FromLT2) _TIFFfree(sp->FromLT2);
  1133. if (sp->From14) _TIFFfree(sp->From14);
  1134. if (sp->From8) _TIFFfree(sp->From8);
  1135. if (sp->ToLinearF) _TIFFfree(sp->ToLinearF);
  1136. if (sp->ToLinear16) _TIFFfree(sp->ToLinear16);
  1137. if (sp->ToLinear8) _TIFFfree(sp->ToLinear8);
  1138. if (sp->state&PLSTATE_INIT) {
  1139. if (tif->tif_mode == O_RDONLY)
  1140. inflateEnd(&sp->stream);
  1141. else
  1142. deflateEnd(&sp->stream);
  1143. }
  1144. if (sp->tbuf)
  1145. _TIFFfree(sp->tbuf);
  1146. _TIFFfree(sp);
  1147. tif->tif_data = NULL;
  1148. _TIFFSetDefaultCompressionState(tif);
  1149. }
  1150. static int
  1151. PixarLogVSetField(TIFF* tif, uint32 tag, va_list ap)
  1152. {
  1153. static const char module[] = "PixarLogVSetField";
  1154. PixarLogState *sp = (PixarLogState *)tif->tif_data;
  1155. int result;
  1156. switch (tag) {
  1157. case TIFFTAG_PIXARLOGQUALITY:
  1158. sp->quality = (int) va_arg(ap, int);
  1159. if (tif->tif_mode != O_RDONLY && (sp->state&PLSTATE_INIT)) {
  1160. if (deflateParams(&sp->stream,
  1161. sp->quality, Z_DEFAULT_STRATEGY) != Z_OK) {
  1162. TIFFErrorExt(tif->tif_clientdata, module, "ZLib error: %s",
  1163. sp->stream.msg);
  1164. return (0);
  1165. }
  1166. }
  1167. return (1);
  1168. case TIFFTAG_PIXARLOGDATAFMT:
  1169. sp->user_datafmt = (int) va_arg(ap, int);
  1170. /* Tweak the TIFF header so that the rest of libtiff knows what
  1171. * size of data will be passed between app and library, and
  1172. * assume that the app knows what it is doing and is not
  1173. * confused by these header manipulations...
  1174. */
  1175. switch (sp->user_datafmt) {
  1176. case PIXARLOGDATAFMT_8BIT:
  1177. case PIXARLOGDATAFMT_8BITABGR:
  1178. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
  1179. TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
  1180. break;
  1181. case PIXARLOGDATAFMT_11BITLOG:
  1182. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
  1183. TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
  1184. break;
  1185. case PIXARLOGDATAFMT_12BITPICIO:
  1186. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
  1187. TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
  1188. break;
  1189. case PIXARLOGDATAFMT_16BIT:
  1190. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
  1191. TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
  1192. break;
  1193. case PIXARLOGDATAFMT_FLOAT:
  1194. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32);
  1195. TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_IEEEFP);
  1196. break;
  1197. }
  1198. /*
  1199. * Must recalculate sizes should bits/sample change.
  1200. */
  1201. tif->tif_tilesize = isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);
  1202. tif->tif_scanlinesize = TIFFScanlineSize(tif);
  1203. result = 1; /* NB: pseudo tag */
  1204. break;
  1205. default:
  1206. result = (*sp->vsetparent)(tif, tag, ap);
  1207. }
  1208. return (result);
  1209. }
  1210. static int
  1211. PixarLogVGetField(TIFF* tif, uint32 tag, va_list ap)
  1212. {
  1213. PixarLogState *sp = (PixarLogState *)tif->tif_data;
  1214. switch (tag) {
  1215. case TIFFTAG_PIXARLOGQUALITY:
  1216. *va_arg(ap, int*) = sp->quality;
  1217. break;
  1218. case TIFFTAG_PIXARLOGDATAFMT:
  1219. *va_arg(ap, int*) = sp->user_datafmt;
  1220. break;
  1221. default:
  1222. return (*sp->vgetparent)(tif, tag, ap);
  1223. }
  1224. return (1);
  1225. }
  1226. static const TIFFField pixarlogFields[] = {
  1227. {TIFFTAG_PIXARLOGDATAFMT, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL},
  1228. {TIFFTAG_PIXARLOGQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL}
  1229. };
  1230. int
  1231. TIFFInitPixarLog(TIFF* tif, int scheme)
  1232. {
  1233. static const char module[] = "TIFFInitPixarLog";
  1234. PixarLogState* sp;
  1235. assert(scheme == COMPRESSION_PIXARLOG);
  1236. /*
  1237. * Merge codec-specific tag information.
  1238. */
  1239. if (!_TIFFMergeFields(tif, pixarlogFields,
  1240. TIFFArrayCount(pixarlogFields))) {
  1241. TIFFErrorExt(tif->tif_clientdata, module,
  1242. "Merging PixarLog codec-specific tags failed");
  1243. return 0;
  1244. }
  1245. /*
  1246. * Allocate state block so tag methods have storage to record values.
  1247. */
  1248. tif->tif_data = (uint8*) _TIFFmalloc(sizeof (PixarLogState));
  1249. if (tif->tif_data == NULL)
  1250. goto bad;
  1251. sp = (PixarLogState*) tif->tif_data;
  1252. _TIFFmemset(sp, 0, sizeof (*sp));
  1253. sp->stream.data_type = Z_BINARY;
  1254. sp->user_datafmt = PIXARLOGDATAFMT_UNKNOWN;
  1255. /*
  1256. * Install codec methods.
  1257. */
  1258. tif->tif_fixuptags = PixarLogFixupTags;
  1259. tif->tif_setupdecode = PixarLogSetupDecode;
  1260. tif->tif_predecode = PixarLogPreDecode;
  1261. tif->tif_decoderow = PixarLogDecode;
  1262. tif->tif_decodestrip = PixarLogDecode;
  1263. tif->tif_decodetile = PixarLogDecode;
  1264. tif->tif_setupencode = PixarLogSetupEncode;
  1265. tif->tif_preencode = PixarLogPreEncode;
  1266. tif->tif_postencode = PixarLogPostEncode;
  1267. tif->tif_encoderow = PixarLogEncode;
  1268. tif->tif_encodestrip = PixarLogEncode;
  1269. tif->tif_encodetile = PixarLogEncode;
  1270. tif->tif_close = PixarLogClose;
  1271. tif->tif_cleanup = PixarLogCleanup;
  1272. /* Override SetField so we can handle our private pseudo-tag */
  1273. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  1274. tif->tif_tagmethods.vgetfield = PixarLogVGetField; /* hook for codec tags */
  1275. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  1276. tif->tif_tagmethods.vsetfield = PixarLogVSetField; /* hook for codec tags */
  1277. /* Default values for codec-specific fields */
  1278. sp->quality = Z_DEFAULT_COMPRESSION; /* default comp. level */
  1279. sp->state = 0;
  1280. /* we don't wish to use the predictor,
  1281. * the default is none, which predictor value 1
  1282. */
  1283. (void) TIFFPredictorInit(tif);
  1284. /*
  1285. * build the companding tables
  1286. */
  1287. PixarLogMakeTables(sp);
  1288. return (1);
  1289. bad:
  1290. TIFFErrorExt(tif->tif_clientdata, module,
  1291. "No space for PixarLog state block");
  1292. return (0);
  1293. }
  1294. #endif /* PIXARLOG_SUPPORT */
  1295. /* vim: set ts=8 sts=8 sw=8 noet: */
  1296. /*
  1297. * Local Variables:
  1298. * mode: c
  1299. * c-basic-offset: 8
  1300. * fill-column: 78
  1301. * End:
  1302. */