tif_pixarlog.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670
  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 DecoderState(tif) ((PixarLogState *)(tif)->tif_data)
  631. #define EncoderState(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 = DecoderState(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 = DecoderState(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 = DecoderState(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. return 0;
  799. }
  800. llen = sp->stride * td->td_imagewidth;
  801. (void)s;
  802. assert(sp != NULL);
  803. sp->stream.next_in = tif->tif_rawcp;
  804. sp->stream.avail_in = (uInt)tif->tif_rawcc;
  805. sp->stream.next_out = (unsigned char *)sp->tbuf;
  806. assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised,
  807. we need to simplify this code to reflect a ZLib that is likely updated
  808. to deal with 8byte memory sizes, though this code will respond
  809. appropriately even before we simplify it */
  810. sp->stream.avail_out = (uInt)(nsamples * sizeof(uint16_t));
  811. if (sp->stream.avail_out != nsamples * sizeof(uint16_t))
  812. {
  813. TIFFErrorExtR(tif, module, "ZLib cannot deal with buffers this size");
  814. return (0);
  815. }
  816. /* Check that we will not fill more than what was allocated */
  817. if ((tmsize_t)sp->stream.avail_out > sp->tbuf_size)
  818. {
  819. TIFFErrorExtR(tif, module, "sp->stream.avail_out > sp->tbuf_size");
  820. return (0);
  821. }
  822. do
  823. {
  824. int state = inflate(&sp->stream, Z_PARTIAL_FLUSH);
  825. if (state == Z_STREAM_END)
  826. {
  827. break; /* XXX */
  828. }
  829. if (state == Z_DATA_ERROR)
  830. {
  831. TIFFErrorExtR(
  832. tif, module, "Decoding error at scanline %" PRIu32 ", %s",
  833. tif->tif_row, sp->stream.msg ? sp->stream.msg : "(null)");
  834. return (0);
  835. }
  836. if (state != Z_OK)
  837. {
  838. TIFFErrorExtR(tif, module, "ZLib error: %s",
  839. sp->stream.msg ? sp->stream.msg : "(null)");
  840. return (0);
  841. }
  842. } while (sp->stream.avail_out > 0);
  843. /* hopefully, we got all the bytes we needed */
  844. if (sp->stream.avail_out != 0)
  845. {
  846. TIFFErrorExtR(tif, module,
  847. "Not enough data at scanline %" PRIu32
  848. " (short %u bytes)",
  849. tif->tif_row, sp->stream.avail_out);
  850. return (0);
  851. }
  852. tif->tif_rawcp = sp->stream.next_in;
  853. tif->tif_rawcc = sp->stream.avail_in;
  854. up = sp->tbuf;
  855. /* Swap bytes in the data if from a different endian machine. */
  856. if (tif->tif_flags & TIFF_SWAB)
  857. TIFFSwabArrayOfShort(up, nsamples);
  858. /*
  859. * if llen is not an exact multiple of nsamples, the decode operation
  860. * may overflow the output buffer, so truncate it enough to prevent
  861. * that but still salvage as much data as possible.
  862. */
  863. if (nsamples % llen)
  864. {
  865. TIFFWarningExtR(tif, module,
  866. "stride %d is not a multiple of sample count, "
  867. "%" TIFF_SSIZE_FORMAT ", data truncated.",
  868. llen, nsamples);
  869. nsamples -= nsamples % llen;
  870. }
  871. for (i = 0; i < nsamples; i += llen, up += llen)
  872. {
  873. switch (sp->user_datafmt)
  874. {
  875. case PIXARLOGDATAFMT_FLOAT:
  876. horizontalAccumulateF(up, llen, sp->stride, (float *)op,
  877. sp->ToLinearF);
  878. op += llen * sizeof(float);
  879. break;
  880. case PIXARLOGDATAFMT_16BIT:
  881. horizontalAccumulate16(up, llen, sp->stride, (uint16_t *)op,
  882. sp->ToLinear16);
  883. op += llen * sizeof(uint16_t);
  884. break;
  885. case PIXARLOGDATAFMT_12BITPICIO:
  886. horizontalAccumulate12(up, llen, sp->stride, (int16_t *)op,
  887. sp->ToLinearF);
  888. op += llen * sizeof(int16_t);
  889. break;
  890. case PIXARLOGDATAFMT_11BITLOG:
  891. horizontalAccumulate11(up, llen, sp->stride, (uint16_t *)op);
  892. op += llen * sizeof(uint16_t);
  893. break;
  894. case PIXARLOGDATAFMT_8BIT:
  895. horizontalAccumulate8(up, llen, sp->stride, (unsigned char *)op,
  896. sp->ToLinear8);
  897. op += llen * sizeof(unsigned char);
  898. break;
  899. case PIXARLOGDATAFMT_8BITABGR:
  900. horizontalAccumulate8abgr(up, llen, sp->stride,
  901. (unsigned char *)op, sp->ToLinear8);
  902. op += llen * sizeof(unsigned char);
  903. break;
  904. default:
  905. TIFFErrorExtR(tif, module, "Unsupported bits/sample: %" PRIu16,
  906. td->td_bitspersample);
  907. return (0);
  908. }
  909. }
  910. return (1);
  911. }
  912. static int PixarLogSetupEncode(TIFF *tif)
  913. {
  914. static const char module[] = "PixarLogSetupEncode";
  915. TIFFDirectory *td = &tif->tif_dir;
  916. PixarLogState *sp = EncoderState(tif);
  917. tmsize_t tbuf_size;
  918. assert(sp != NULL);
  919. /* for some reason, we can't do this in TIFFInitPixarLog */
  920. sp->stride =
  921. (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel
  922. : 1);
  923. tbuf_size =
  924. multiply_ms(multiply_ms(multiply_ms(sp->stride, td->td_imagewidth),
  925. td->td_rowsperstrip),
  926. sizeof(uint16_t));
  927. if (tbuf_size == 0)
  928. return (0); /* TODO: this is an error return without error report
  929. through TIFFErrorExt */
  930. sp->tbuf = (uint16_t *)_TIFFmallocExt(tif, tbuf_size);
  931. if (sp->tbuf == NULL)
  932. return (0);
  933. if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
  934. sp->user_datafmt = PixarLogGuessDataFmt(td);
  935. if (sp->user_datafmt == PIXARLOGDATAFMT_UNKNOWN)
  936. {
  937. TIFFErrorExtR(tif, module,
  938. "PixarLog compression can't handle %" PRIu16
  939. " bit linear encodings",
  940. td->td_bitspersample);
  941. return (0);
  942. }
  943. if (deflateInit(&sp->stream, sp->quality) != Z_OK)
  944. {
  945. TIFFErrorExtR(tif, module, "%s",
  946. sp->stream.msg ? sp->stream.msg : "(null)");
  947. return (0);
  948. }
  949. else
  950. {
  951. sp->state |= PLSTATE_INIT;
  952. return (1);
  953. }
  954. }
  955. /*
  956. * Reset encoding state at the start of a strip.
  957. */
  958. static int PixarLogPreEncode(TIFF *tif, uint16_t s)
  959. {
  960. static const char module[] = "PixarLogPreEncode";
  961. PixarLogState *sp = EncoderState(tif);
  962. (void)s;
  963. assert(sp != NULL);
  964. sp->stream.next_out = tif->tif_rawdata;
  965. assert(sizeof(sp->stream.avail_out) == 4); /* if this assert gets raised,
  966. we need to simplify this code to reflect a ZLib that is likely updated
  967. to deal with 8byte memory sizes, though this code will respond
  968. appropriately even before we simplify it */
  969. sp->stream.avail_out = (uInt)tif->tif_rawdatasize;
  970. if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
  971. {
  972. TIFFErrorExtR(tif, module, "ZLib cannot deal with buffers this size");
  973. return (0);
  974. }
  975. return (deflateReset(&sp->stream) == Z_OK);
  976. }
  977. static void horizontalDifferenceF(float *ip, int n, int stride, uint16_t *wp,
  978. uint16_t *FromLT2)
  979. {
  980. int32_t r1, g1, b1, a1, r2, g2, b2, a2, mask;
  981. float fltsize = Fltsize;
  982. #define CLAMP(v) \
  983. ((v < (float)0.) ? 0 \
  984. : (v < (float)2.) ? FromLT2[(int)(v * fltsize)] \
  985. : (v > (float)24.2) ? 2047 \
  986. : LogK1 * log(v * LogK2) + 0.5)
  987. mask = CODE_MASK;
  988. if (n >= stride)
  989. {
  990. if (stride == 3)
  991. {
  992. r2 = wp[0] = (uint16_t)CLAMP(ip[0]);
  993. g2 = wp[1] = (uint16_t)CLAMP(ip[1]);
  994. b2 = wp[2] = (uint16_t)CLAMP(ip[2]);
  995. n -= 3;
  996. while (n > 0)
  997. {
  998. n -= 3;
  999. wp += 3;
  1000. ip += 3;
  1001. r1 = (int32_t)CLAMP(ip[0]);
  1002. wp[0] = (uint16_t)((r1 - r2) & mask);
  1003. r2 = r1;
  1004. g1 = (int32_t)CLAMP(ip[1]);
  1005. wp[1] = (uint16_t)((g1 - g2) & mask);
  1006. g2 = g1;
  1007. b1 = (int32_t)CLAMP(ip[2]);
  1008. wp[2] = (uint16_t)((b1 - b2) & mask);
  1009. b2 = b1;
  1010. }
  1011. }
  1012. else if (stride == 4)
  1013. {
  1014. r2 = wp[0] = (uint16_t)CLAMP(ip[0]);
  1015. g2 = wp[1] = (uint16_t)CLAMP(ip[1]);
  1016. b2 = wp[2] = (uint16_t)CLAMP(ip[2]);
  1017. a2 = wp[3] = (uint16_t)CLAMP(ip[3]);
  1018. n -= 4;
  1019. while (n > 0)
  1020. {
  1021. n -= 4;
  1022. wp += 4;
  1023. ip += 4;
  1024. r1 = (int32_t)CLAMP(ip[0]);
  1025. wp[0] = (uint16_t)((r1 - r2) & mask);
  1026. r2 = r1;
  1027. g1 = (int32_t)CLAMP(ip[1]);
  1028. wp[1] = (uint16_t)((g1 - g2) & mask);
  1029. g2 = g1;
  1030. b1 = (int32_t)CLAMP(ip[2]);
  1031. wp[2] = (uint16_t)((b1 - b2) & mask);
  1032. b2 = b1;
  1033. a1 = (int32_t)CLAMP(ip[3]);
  1034. wp[3] = (uint16_t)((a1 - a2) & mask);
  1035. a2 = a1;
  1036. }
  1037. }
  1038. else
  1039. {
  1040. REPEAT(stride, wp[0] = (uint16_t)CLAMP(ip[0]); wp++; ip++)
  1041. n -= stride;
  1042. while (n > 0)
  1043. {
  1044. REPEAT(stride,
  1045. wp[0] = (uint16_t)(((int32_t)CLAMP(ip[0]) -
  1046. (int32_t)CLAMP(ip[-stride])) &
  1047. mask);
  1048. wp++; ip++)
  1049. n -= stride;
  1050. }
  1051. }
  1052. }
  1053. }
  1054. static void horizontalDifference16(unsigned short *ip, int n, int stride,
  1055. unsigned short *wp, uint16_t *From14)
  1056. {
  1057. register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
  1058. /* assumption is unsigned pixel values */
  1059. #undef CLAMP
  1060. #define CLAMP(v) From14[(v) >> 2]
  1061. mask = CODE_MASK;
  1062. if (n >= stride)
  1063. {
  1064. if (stride == 3)
  1065. {
  1066. r2 = wp[0] = CLAMP(ip[0]);
  1067. g2 = wp[1] = CLAMP(ip[1]);
  1068. b2 = wp[2] = CLAMP(ip[2]);
  1069. n -= 3;
  1070. while (n > 0)
  1071. {
  1072. n -= 3;
  1073. wp += 3;
  1074. ip += 3;
  1075. r1 = CLAMP(ip[0]);
  1076. wp[0] = (uint16_t)((r1 - r2) & mask);
  1077. r2 = r1;
  1078. g1 = CLAMP(ip[1]);
  1079. wp[1] = (uint16_t)((g1 - g2) & mask);
  1080. g2 = g1;
  1081. b1 = CLAMP(ip[2]);
  1082. wp[2] = (uint16_t)((b1 - b2) & mask);
  1083. b2 = b1;
  1084. }
  1085. }
  1086. else if (stride == 4)
  1087. {
  1088. r2 = wp[0] = CLAMP(ip[0]);
  1089. g2 = wp[1] = CLAMP(ip[1]);
  1090. b2 = wp[2] = CLAMP(ip[2]);
  1091. a2 = wp[3] = CLAMP(ip[3]);
  1092. n -= 4;
  1093. while (n > 0)
  1094. {
  1095. n -= 4;
  1096. wp += 4;
  1097. ip += 4;
  1098. r1 = CLAMP(ip[0]);
  1099. wp[0] = (uint16_t)((r1 - r2) & mask);
  1100. r2 = r1;
  1101. g1 = CLAMP(ip[1]);
  1102. wp[1] = (uint16_t)((g1 - g2) & mask);
  1103. g2 = g1;
  1104. b1 = CLAMP(ip[2]);
  1105. wp[2] = (uint16_t)((b1 - b2) & mask);
  1106. b2 = b1;
  1107. a1 = CLAMP(ip[3]);
  1108. wp[3] = (uint16_t)((a1 - a2) & mask);
  1109. a2 = a1;
  1110. }
  1111. }
  1112. else
  1113. {
  1114. REPEAT(stride, wp[0] = CLAMP(ip[0]); wp++; ip++)
  1115. n -= stride;
  1116. while (n > 0)
  1117. {
  1118. REPEAT(stride,
  1119. wp[0] = (uint16_t)((CLAMP(ip[0]) - CLAMP(ip[-stride])) &
  1120. mask);
  1121. wp++; ip++)
  1122. n -= stride;
  1123. }
  1124. }
  1125. }
  1126. }
  1127. static void horizontalDifference8(unsigned char *ip, int n, int stride,
  1128. unsigned short *wp, uint16_t *From8)
  1129. {
  1130. register int r1, g1, b1, a1, r2, g2, b2, a2, mask;
  1131. #undef CLAMP
  1132. #define CLAMP(v) (From8[(v)])
  1133. mask = CODE_MASK;
  1134. if (n >= stride)
  1135. {
  1136. if (stride == 3)
  1137. {
  1138. r2 = wp[0] = CLAMP(ip[0]);
  1139. g2 = wp[1] = CLAMP(ip[1]);
  1140. b2 = wp[2] = CLAMP(ip[2]);
  1141. n -= 3;
  1142. while (n > 0)
  1143. {
  1144. n -= 3;
  1145. r1 = CLAMP(ip[3]);
  1146. wp[3] = (uint16_t)((r1 - r2) & mask);
  1147. r2 = r1;
  1148. g1 = CLAMP(ip[4]);
  1149. wp[4] = (uint16_t)((g1 - g2) & mask);
  1150. g2 = g1;
  1151. b1 = CLAMP(ip[5]);
  1152. wp[5] = (uint16_t)((b1 - b2) & mask);
  1153. b2 = b1;
  1154. wp += 3;
  1155. ip += 3;
  1156. }
  1157. }
  1158. else if (stride == 4)
  1159. {
  1160. r2 = wp[0] = CLAMP(ip[0]);
  1161. g2 = wp[1] = CLAMP(ip[1]);
  1162. b2 = wp[2] = CLAMP(ip[2]);
  1163. a2 = wp[3] = CLAMP(ip[3]);
  1164. n -= 4;
  1165. while (n > 0)
  1166. {
  1167. n -= 4;
  1168. r1 = CLAMP(ip[4]);
  1169. wp[4] = (uint16_t)((r1 - r2) & mask);
  1170. r2 = r1;
  1171. g1 = CLAMP(ip[5]);
  1172. wp[5] = (uint16_t)((g1 - g2) & mask);
  1173. g2 = g1;
  1174. b1 = CLAMP(ip[6]);
  1175. wp[6] = (uint16_t)((b1 - b2) & mask);
  1176. b2 = b1;
  1177. a1 = CLAMP(ip[7]);
  1178. wp[7] = (uint16_t)((a1 - a2) & mask);
  1179. a2 = a1;
  1180. wp += 4;
  1181. ip += 4;
  1182. }
  1183. }
  1184. else
  1185. {
  1186. REPEAT(stride, wp[0] = CLAMP(ip[0]); wp++; ip++)
  1187. n -= stride;
  1188. while (n > 0)
  1189. {
  1190. REPEAT(stride,
  1191. wp[0] = (uint16_t)((CLAMP(ip[0]) - CLAMP(ip[-stride])) &
  1192. mask);
  1193. wp++; ip++)
  1194. n -= stride;
  1195. }
  1196. }
  1197. }
  1198. }
  1199. /*
  1200. * Encode a chunk of pixels.
  1201. */
  1202. static int PixarLogEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
  1203. {
  1204. static const char module[] = "PixarLogEncode";
  1205. TIFFDirectory *td = &tif->tif_dir;
  1206. PixarLogState *sp = EncoderState(tif);
  1207. tmsize_t i;
  1208. tmsize_t n;
  1209. int llen;
  1210. unsigned short *up;
  1211. (void)s;
  1212. switch (sp->user_datafmt)
  1213. {
  1214. case PIXARLOGDATAFMT_FLOAT:
  1215. n = cc / sizeof(float); /* XXX float == 32 bits */
  1216. break;
  1217. case PIXARLOGDATAFMT_16BIT:
  1218. case PIXARLOGDATAFMT_12BITPICIO:
  1219. case PIXARLOGDATAFMT_11BITLOG:
  1220. n = cc / sizeof(uint16_t); /* XXX uint16_t == 16 bits */
  1221. break;
  1222. case PIXARLOGDATAFMT_8BIT:
  1223. case PIXARLOGDATAFMT_8BITABGR:
  1224. n = cc;
  1225. break;
  1226. default:
  1227. TIFFErrorExtR(tif, module,
  1228. "%" PRIu16 " bit input not supported in PixarLog",
  1229. td->td_bitspersample);
  1230. return 0;
  1231. }
  1232. llen = sp->stride * td->td_imagewidth;
  1233. /* Check against the number of elements (of size uint16_t) of sp->tbuf */
  1234. if (n > ((tmsize_t)td->td_rowsperstrip * llen))
  1235. {
  1236. TIFFErrorExtR(tif, module, "Too many input bytes provided");
  1237. return 0;
  1238. }
  1239. for (i = 0, up = sp->tbuf; i < n; i += llen, up += llen)
  1240. {
  1241. switch (sp->user_datafmt)
  1242. {
  1243. case PIXARLOGDATAFMT_FLOAT:
  1244. horizontalDifferenceF((float *)bp, llen, sp->stride, up,
  1245. sp->FromLT2);
  1246. bp += llen * sizeof(float);
  1247. break;
  1248. case PIXARLOGDATAFMT_16BIT:
  1249. horizontalDifference16((uint16_t *)bp, llen, sp->stride, up,
  1250. sp->From14);
  1251. bp += llen * sizeof(uint16_t);
  1252. break;
  1253. case PIXARLOGDATAFMT_8BIT:
  1254. horizontalDifference8((unsigned char *)bp, llen, sp->stride, up,
  1255. sp->From8);
  1256. bp += llen * sizeof(unsigned char);
  1257. break;
  1258. default:
  1259. TIFFErrorExtR(tif, module,
  1260. "%" PRIu16 " bit input not supported in PixarLog",
  1261. td->td_bitspersample);
  1262. return 0;
  1263. }
  1264. }
  1265. sp->stream.next_in = (unsigned char *)sp->tbuf;
  1266. assert(sizeof(sp->stream.avail_in) == 4); /* if this assert gets raised,
  1267. we need to simplify this code to reflect a ZLib that is likely updated
  1268. to deal with 8byte memory sizes, though this code will respond
  1269. appropriately even before we simplify it */
  1270. sp->stream.avail_in = (uInt)(n * sizeof(uint16_t));
  1271. if ((sp->stream.avail_in / sizeof(uint16_t)) != (uInt)n)
  1272. {
  1273. TIFFErrorExtR(tif, module, "ZLib cannot deal with buffers this size");
  1274. return (0);
  1275. }
  1276. do
  1277. {
  1278. if (deflate(&sp->stream, Z_NO_FLUSH) != Z_OK)
  1279. {
  1280. TIFFErrorExtR(tif, module, "Encoder error: %s",
  1281. sp->stream.msg ? sp->stream.msg : "(null)");
  1282. return (0);
  1283. }
  1284. if (sp->stream.avail_out == 0)
  1285. {
  1286. tif->tif_rawcc = tif->tif_rawdatasize;
  1287. if (!TIFFFlushData1(tif))
  1288. return 0;
  1289. sp->stream.next_out = tif->tif_rawdata;
  1290. sp->stream.avail_out =
  1291. (uInt)tif
  1292. ->tif_rawdatasize; /* this is a safe typecast, as check is
  1293. made already in PixarLogPreEncode */
  1294. }
  1295. } while (sp->stream.avail_in > 0);
  1296. return (1);
  1297. }
  1298. /*
  1299. * Finish off an encoded strip by flushing the last
  1300. * string and tacking on an End Of Information code.
  1301. */
  1302. static int PixarLogPostEncode(TIFF *tif)
  1303. {
  1304. static const char module[] = "PixarLogPostEncode";
  1305. PixarLogState *sp = EncoderState(tif);
  1306. int state;
  1307. sp->stream.avail_in = 0;
  1308. do
  1309. {
  1310. state = deflate(&sp->stream, Z_FINISH);
  1311. switch (state)
  1312. {
  1313. case Z_STREAM_END:
  1314. case Z_OK:
  1315. if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize)
  1316. {
  1317. tif->tif_rawcc =
  1318. tif->tif_rawdatasize - sp->stream.avail_out;
  1319. if (!TIFFFlushData1(tif))
  1320. return 0;
  1321. sp->stream.next_out = tif->tif_rawdata;
  1322. sp->stream.avail_out =
  1323. (uInt)tif->tif_rawdatasize; /* this is a safe typecast,
  1324. as check is made already
  1325. in PixarLogPreEncode */
  1326. }
  1327. break;
  1328. default:
  1329. TIFFErrorExtR(tif, module, "ZLib error: %s",
  1330. sp->stream.msg ? sp->stream.msg : "(null)");
  1331. return (0);
  1332. }
  1333. } while (state != Z_STREAM_END);
  1334. return (1);
  1335. }
  1336. static void PixarLogClose(TIFF *tif)
  1337. {
  1338. PixarLogState *sp = (PixarLogState *)tif->tif_data;
  1339. TIFFDirectory *td = &tif->tif_dir;
  1340. assert(sp != 0);
  1341. /* In a really sneaky (and really incorrect, and untruthful, and
  1342. * troublesome, and error-prone) maneuver that completely goes against
  1343. * the spirit of TIFF, and breaks TIFF, on close, we covertly
  1344. * modify both bitspersample and sampleformat in the directory to
  1345. * indicate 8-bit linear. This way, the decode "just works" even for
  1346. * readers that don't know about PixarLog, or how to set
  1347. * the PIXARLOGDATFMT pseudo-tag.
  1348. */
  1349. if (sp->state & PLSTATE_INIT)
  1350. {
  1351. /* We test the state to avoid an issue such as in
  1352. * http://bugzilla.maptools.org/show_bug.cgi?id=2604
  1353. * What appends in that case is that the bitspersample is 1 and
  1354. * a TransferFunction is set. The size of the TransferFunction
  1355. * depends on 1<<bitspersample. So if we increase it, an access
  1356. * out of the buffer will happen at directory flushing.
  1357. * Another option would be to clear those targs.
  1358. */
  1359. td->td_bitspersample = 8;
  1360. td->td_sampleformat = SAMPLEFORMAT_UINT;
  1361. }
  1362. }
  1363. static void PixarLogCleanup(TIFF *tif)
  1364. {
  1365. PixarLogState *sp = (PixarLogState *)tif->tif_data;
  1366. assert(sp != 0);
  1367. (void)TIFFPredictorCleanup(tif);
  1368. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  1369. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  1370. if (sp->FromLT2)
  1371. _TIFFfreeExt(tif, sp->FromLT2);
  1372. if (sp->From14)
  1373. _TIFFfreeExt(tif, sp->From14);
  1374. if (sp->From8)
  1375. _TIFFfreeExt(tif, sp->From8);
  1376. if (sp->ToLinearF)
  1377. _TIFFfreeExt(tif, sp->ToLinearF);
  1378. if (sp->ToLinear16)
  1379. _TIFFfreeExt(tif, sp->ToLinear16);
  1380. if (sp->ToLinear8)
  1381. _TIFFfreeExt(tif, sp->ToLinear8);
  1382. if (sp->state & PLSTATE_INIT)
  1383. {
  1384. if (tif->tif_mode == O_RDONLY)
  1385. inflateEnd(&sp->stream);
  1386. else
  1387. deflateEnd(&sp->stream);
  1388. }
  1389. if (sp->tbuf)
  1390. _TIFFfreeExt(tif, sp->tbuf);
  1391. _TIFFfreeExt(tif, sp);
  1392. tif->tif_data = NULL;
  1393. _TIFFSetDefaultCompressionState(tif);
  1394. }
  1395. static int PixarLogVSetField(TIFF *tif, uint32_t tag, va_list ap)
  1396. {
  1397. static const char module[] = "PixarLogVSetField";
  1398. PixarLogState *sp = (PixarLogState *)tif->tif_data;
  1399. int result;
  1400. switch (tag)
  1401. {
  1402. case TIFFTAG_PIXARLOGQUALITY:
  1403. sp->quality = (int)va_arg(ap, int);
  1404. if (tif->tif_mode != O_RDONLY && (sp->state & PLSTATE_INIT))
  1405. {
  1406. if (deflateParams(&sp->stream, sp->quality,
  1407. Z_DEFAULT_STRATEGY) != Z_OK)
  1408. {
  1409. TIFFErrorExtR(tif, module, "ZLib error: %s",
  1410. sp->stream.msg ? sp->stream.msg : "(null)");
  1411. return (0);
  1412. }
  1413. }
  1414. return (1);
  1415. case TIFFTAG_PIXARLOGDATAFMT:
  1416. sp->user_datafmt = (int)va_arg(ap, int);
  1417. /* Tweak the TIFF header so that the rest of libtiff knows what
  1418. * size of data will be passed between app and library, and
  1419. * assume that the app knows what it is doing and is not
  1420. * confused by these header manipulations...
  1421. */
  1422. switch (sp->user_datafmt)
  1423. {
  1424. case PIXARLOGDATAFMT_8BIT:
  1425. case PIXARLOGDATAFMT_8BITABGR:
  1426. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
  1427. TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
  1428. break;
  1429. case PIXARLOGDATAFMT_11BITLOG:
  1430. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
  1431. TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
  1432. break;
  1433. case PIXARLOGDATAFMT_12BITPICIO:
  1434. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
  1435. TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_INT);
  1436. break;
  1437. case PIXARLOGDATAFMT_16BIT:
  1438. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
  1439. TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
  1440. break;
  1441. case PIXARLOGDATAFMT_FLOAT:
  1442. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 32);
  1443. TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT,
  1444. SAMPLEFORMAT_IEEEFP);
  1445. break;
  1446. }
  1447. /*
  1448. * Must recalculate sizes should bits/sample change.
  1449. */
  1450. tif->tif_tilesize =
  1451. isTiled(tif) ? TIFFTileSize(tif) : (tmsize_t)(-1);
  1452. tif->tif_scanlinesize = TIFFScanlineSize(tif);
  1453. result = 1; /* NB: pseudo tag */
  1454. break;
  1455. default:
  1456. result = (*sp->vsetparent)(tif, tag, ap);
  1457. }
  1458. return (result);
  1459. }
  1460. static int PixarLogVGetField(TIFF *tif, uint32_t tag, va_list ap)
  1461. {
  1462. PixarLogState *sp = (PixarLogState *)tif->tif_data;
  1463. switch (tag)
  1464. {
  1465. case TIFFTAG_PIXARLOGQUALITY:
  1466. *va_arg(ap, int *) = sp->quality;
  1467. break;
  1468. case TIFFTAG_PIXARLOGDATAFMT:
  1469. *va_arg(ap, int *) = sp->user_datafmt;
  1470. break;
  1471. default:
  1472. return (*sp->vgetparent)(tif, tag, ap);
  1473. }
  1474. return (1);
  1475. }
  1476. static const TIFFField pixarlogFields[] = {
  1477. {TIFFTAG_PIXARLOGDATAFMT, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
  1478. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL},
  1479. {TIFFTAG_PIXARLOGQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
  1480. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "", NULL}};
  1481. int TIFFInitPixarLog(TIFF *tif, int scheme)
  1482. {
  1483. static const char module[] = "TIFFInitPixarLog";
  1484. PixarLogState *sp;
  1485. (void)scheme;
  1486. assert(scheme == COMPRESSION_PIXARLOG);
  1487. /*
  1488. * Merge codec-specific tag information.
  1489. */
  1490. if (!_TIFFMergeFields(tif, pixarlogFields, TIFFArrayCount(pixarlogFields)))
  1491. {
  1492. TIFFErrorExtR(tif, module,
  1493. "Merging PixarLog codec-specific tags failed");
  1494. return 0;
  1495. }
  1496. /*
  1497. * Allocate state block so tag methods have storage to record values.
  1498. */
  1499. tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(PixarLogState));
  1500. if (tif->tif_data == NULL)
  1501. goto bad;
  1502. sp = (PixarLogState *)tif->tif_data;
  1503. _TIFFmemset(sp, 0, sizeof(*sp));
  1504. sp->stream.data_type = Z_BINARY;
  1505. sp->user_datafmt = PIXARLOGDATAFMT_UNKNOWN;
  1506. /*
  1507. * Install codec methods.
  1508. */
  1509. tif->tif_fixuptags = PixarLogFixupTags;
  1510. tif->tif_setupdecode = PixarLogSetupDecode;
  1511. tif->tif_predecode = PixarLogPreDecode;
  1512. tif->tif_decoderow = PixarLogDecode;
  1513. tif->tif_decodestrip = PixarLogDecode;
  1514. tif->tif_decodetile = PixarLogDecode;
  1515. tif->tif_setupencode = PixarLogSetupEncode;
  1516. tif->tif_preencode = PixarLogPreEncode;
  1517. tif->tif_postencode = PixarLogPostEncode;
  1518. tif->tif_encoderow = PixarLogEncode;
  1519. tif->tif_encodestrip = PixarLogEncode;
  1520. tif->tif_encodetile = PixarLogEncode;
  1521. tif->tif_close = PixarLogClose;
  1522. tif->tif_cleanup = PixarLogCleanup;
  1523. /* Override SetField so we can handle our private pseudo-tag */
  1524. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  1525. tif->tif_tagmethods.vgetfield = PixarLogVGetField; /* hook for codec tags */
  1526. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  1527. tif->tif_tagmethods.vsetfield = PixarLogVSetField; /* hook for codec tags */
  1528. /* Default values for codec-specific fields */
  1529. sp->quality = Z_DEFAULT_COMPRESSION; /* default comp. level */
  1530. sp->state = 0;
  1531. /* we don't wish to use the predictor,
  1532. * the default is none, which predictor value 1
  1533. */
  1534. (void)TIFFPredictorInit(tif);
  1535. /*
  1536. * build the companding tables
  1537. */
  1538. PixarLogMakeTables(tif, sp);
  1539. return (1);
  1540. bad:
  1541. TIFFErrorExtR(tif, module, "No space for PixarLog state block");
  1542. return (0);
  1543. }
  1544. #endif /* PIXARLOG_SUPPORT */