tif_pixarlog.c 54 KB

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