tif_pixarlog.c 43 KB

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