tif_lerc.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  1. /*
  2. * Copyright (c) 2018, Even Rouault
  3. * Author: <even.rouault at spatialys.com>
  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. * 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 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 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 LERC_SUPPORT
  26. /*
  27. * TIFF Library.
  28. *
  29. * LERC Compression Support
  30. *
  31. */
  32. #include "Lerc_c_api.h"
  33. #include "zlib.h"
  34. #ifdef ZSTD_SUPPORT
  35. #include "zstd.h"
  36. #endif
  37. #if LIBDEFLATE_SUPPORT
  38. #include "libdeflate.h"
  39. #endif
  40. #define LIBDEFLATE_MAX_COMPRESSION_LEVEL 12
  41. #include <assert.h>
  42. #define LSTATE_INIT_DECODE 0x01
  43. #define LSTATE_INIT_ENCODE 0x02
  44. #ifndef LERC_AT_LEAST_VERSION
  45. #define LERC_AT_LEAST_VERSION(maj, min, patch) 0
  46. #endif
  47. /*
  48. * State block for each open TIFF file using LERC compression/decompression.
  49. */
  50. typedef struct
  51. {
  52. double maxzerror; /* max z error */
  53. int lerc_version;
  54. int additional_compression;
  55. int zstd_compress_level; /* zstd */
  56. int zipquality; /* deflate */
  57. int state; /* state flags */
  58. uint32_t segment_width;
  59. uint32_t segment_height;
  60. unsigned int uncompressed_size;
  61. unsigned int uncompressed_alloc;
  62. uint8_t *uncompressed_buffer;
  63. unsigned int uncompressed_offset;
  64. uint8_t *uncompressed_buffer_multiband;
  65. unsigned int uncompressed_buffer_multiband_alloc;
  66. unsigned int mask_size;
  67. uint8_t *mask_buffer;
  68. unsigned int compressed_size;
  69. void *compressed_buffer;
  70. #if LIBDEFLATE_SUPPORT
  71. struct libdeflate_decompressor *libdeflate_dec;
  72. struct libdeflate_compressor *libdeflate_enc;
  73. #endif
  74. TIFFVGetMethod vgetparent; /* super-class method */
  75. TIFFVSetMethod vsetparent; /* super-class method */
  76. } LERCState;
  77. #define GetLERCState(tif) ((LERCState *)(tif)->tif_data)
  78. #define LERCDecoderState(tif) GetLERCState(tif)
  79. #define LERCEncoderState(tif) GetLERCState(tif)
  80. static int LERCDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s);
  81. static int LERCFixupTags(TIFF *tif)
  82. {
  83. (void)tif;
  84. return 1;
  85. }
  86. static int LERCSetupDecode(TIFF *tif)
  87. {
  88. LERCState *sp = LERCDecoderState(tif);
  89. assert(sp != NULL);
  90. /* if we were last encoding, terminate this mode */
  91. if (sp->state & LSTATE_INIT_ENCODE)
  92. {
  93. sp->state = 0;
  94. }
  95. sp->state |= LSTATE_INIT_DECODE;
  96. return 1;
  97. }
  98. static int GetLercDataType(TIFF *tif)
  99. {
  100. TIFFDirectory *td = &tif->tif_dir;
  101. static const char module[] = "GetLercDataType";
  102. if (td->td_sampleformat == SAMPLEFORMAT_INT && td->td_bitspersample == 8)
  103. {
  104. return 0;
  105. }
  106. if (td->td_sampleformat == SAMPLEFORMAT_UINT && td->td_bitspersample == 8)
  107. {
  108. return 1;
  109. }
  110. if (td->td_sampleformat == SAMPLEFORMAT_INT && td->td_bitspersample == 16)
  111. {
  112. return 2;
  113. }
  114. if (td->td_sampleformat == SAMPLEFORMAT_UINT && td->td_bitspersample == 16)
  115. {
  116. return 3;
  117. }
  118. if (td->td_sampleformat == SAMPLEFORMAT_INT && td->td_bitspersample == 32)
  119. {
  120. return 4;
  121. }
  122. if (td->td_sampleformat == SAMPLEFORMAT_UINT && td->td_bitspersample == 32)
  123. {
  124. return 5;
  125. }
  126. if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  127. td->td_bitspersample == 32)
  128. {
  129. return 6;
  130. }
  131. if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  132. td->td_bitspersample == 64)
  133. {
  134. return 7;
  135. }
  136. TIFFErrorExtR(
  137. tif, module,
  138. "Unsupported combination of SampleFormat and td_bitspersample");
  139. return -1;
  140. }
  141. static int SetupBuffers(TIFF *tif, LERCState *sp, const char *module)
  142. {
  143. TIFFDirectory *td = &tif->tif_dir;
  144. uint64_t new_size_64;
  145. uint64_t new_alloc_64;
  146. unsigned int new_size;
  147. unsigned int new_alloc;
  148. sp->uncompressed_offset = 0;
  149. if (isTiled(tif))
  150. {
  151. sp->segment_width = td->td_tilewidth;
  152. sp->segment_height = td->td_tilelength;
  153. }
  154. else
  155. {
  156. sp->segment_width = td->td_imagewidth;
  157. sp->segment_height = td->td_imagelength - tif->tif_row;
  158. if (sp->segment_height > td->td_rowsperstrip)
  159. sp->segment_height = td->td_rowsperstrip;
  160. }
  161. new_size_64 = (uint64_t)sp->segment_width * sp->segment_height *
  162. (td->td_bitspersample / 8);
  163. if (td->td_planarconfig == PLANARCONFIG_CONTIG)
  164. {
  165. new_size_64 *= td->td_samplesperpixel;
  166. }
  167. new_size = (unsigned int)new_size_64;
  168. sp->uncompressed_size = new_size;
  169. /* add some margin as we are going to use it also to store deflate/zstd
  170. * compressed data. We also need extra margin when writing very small
  171. * rasters with one mask per band. */
  172. new_alloc_64 = 256 + new_size_64 + new_size_64 / 3;
  173. #ifdef ZSTD_SUPPORT
  174. {
  175. size_t zstd_max = ZSTD_compressBound((size_t)new_size_64);
  176. if (new_alloc_64 < zstd_max)
  177. {
  178. new_alloc_64 = zstd_max;
  179. }
  180. }
  181. #endif
  182. new_alloc = (unsigned int)new_alloc_64;
  183. if (new_alloc != new_alloc_64)
  184. {
  185. TIFFErrorExtR(tif, module, "Too large uncompressed strip/tile");
  186. _TIFFfreeExt(tif, sp->uncompressed_buffer);
  187. sp->uncompressed_buffer = NULL;
  188. sp->uncompressed_alloc = 0;
  189. return 0;
  190. }
  191. if (sp->uncompressed_alloc < new_alloc)
  192. {
  193. _TIFFfreeExt(tif, sp->uncompressed_buffer);
  194. sp->uncompressed_buffer = (uint8_t *)_TIFFmallocExt(tif, new_alloc);
  195. if (!sp->uncompressed_buffer)
  196. {
  197. TIFFErrorExtR(tif, module, "Cannot allocate buffer");
  198. _TIFFfreeExt(tif, sp->uncompressed_buffer);
  199. sp->uncompressed_buffer = NULL;
  200. sp->uncompressed_alloc = 0;
  201. return 0;
  202. }
  203. sp->uncompressed_alloc = new_alloc;
  204. }
  205. if ((td->td_planarconfig == PLANARCONFIG_CONTIG &&
  206. td->td_extrasamples > 0 &&
  207. td->td_sampleinfo[td->td_extrasamples - 1] == EXTRASAMPLE_UNASSALPHA &&
  208. GetLercDataType(tif) == 1) ||
  209. (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  210. (td->td_bitspersample == 32 || td->td_bitspersample == 64)))
  211. {
  212. unsigned int mask_size = sp->segment_width * sp->segment_height;
  213. #if LERC_AT_LEAST_VERSION(3, 0, 0)
  214. if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  215. td->td_planarconfig == PLANARCONFIG_CONTIG)
  216. {
  217. /* We may need one mask per band */
  218. mask_size *= td->td_samplesperpixel;
  219. }
  220. #endif
  221. if (sp->mask_size < mask_size)
  222. {
  223. void *mask_buffer =
  224. _TIFFreallocExt(tif, sp->mask_buffer, mask_size);
  225. if (mask_buffer == NULL)
  226. {
  227. TIFFErrorExtR(tif, module, "Cannot allocate buffer");
  228. sp->mask_size = 0;
  229. _TIFFfreeExt(tif, sp->uncompressed_buffer);
  230. sp->uncompressed_buffer = NULL;
  231. sp->uncompressed_alloc = 0;
  232. return 0;
  233. }
  234. sp->mask_buffer = (uint8_t *)mask_buffer;
  235. sp->mask_size = mask_size;
  236. }
  237. }
  238. return 1;
  239. }
  240. /*
  241. * Setup state for decoding a strip.
  242. */
  243. static int LERCPreDecode(TIFF *tif, uint16_t s)
  244. {
  245. static const char module[] = "LERCPreDecode";
  246. lerc_status lerc_ret;
  247. TIFFDirectory *td = &tif->tif_dir;
  248. LERCState *sp = LERCDecoderState(tif);
  249. int lerc_data_type;
  250. unsigned int infoArray[9];
  251. unsigned nomask_bands = td->td_samplesperpixel;
  252. int ndims;
  253. int use_mask = 0;
  254. uint8_t *lerc_data = tif->tif_rawcp;
  255. unsigned int lerc_data_size = (unsigned int)tif->tif_rawcc;
  256. (void)s;
  257. assert(sp != NULL);
  258. if (sp->state != LSTATE_INIT_DECODE)
  259. tif->tif_setupdecode(tif);
  260. lerc_data_type = GetLercDataType(tif);
  261. if (lerc_data_type < 0)
  262. return 0;
  263. if (!SetupBuffers(tif, sp, module))
  264. return 0;
  265. if (sp->additional_compression != LERC_ADD_COMPRESSION_NONE)
  266. {
  267. if (sp->compressed_size < sp->uncompressed_alloc)
  268. {
  269. _TIFFfreeExt(tif, sp->compressed_buffer);
  270. sp->compressed_buffer = _TIFFmallocExt(tif, sp->uncompressed_alloc);
  271. if (!sp->compressed_buffer)
  272. {
  273. sp->compressed_size = 0;
  274. return 0;
  275. }
  276. sp->compressed_size = sp->uncompressed_alloc;
  277. }
  278. }
  279. if (sp->additional_compression == LERC_ADD_COMPRESSION_DEFLATE)
  280. {
  281. #if LIBDEFLATE_SUPPORT
  282. enum libdeflate_result res;
  283. size_t lerc_data_sizet = 0;
  284. if (sp->libdeflate_dec == NULL)
  285. {
  286. sp->libdeflate_dec = libdeflate_alloc_decompressor();
  287. if (sp->libdeflate_dec == NULL)
  288. {
  289. TIFFErrorExtR(tif, module, "Cannot allocate decompressor");
  290. return 0;
  291. }
  292. }
  293. res = libdeflate_zlib_decompress(
  294. sp->libdeflate_dec, tif->tif_rawcp, (size_t)tif->tif_rawcc,
  295. sp->compressed_buffer, sp->compressed_size, &lerc_data_sizet);
  296. if (res != LIBDEFLATE_SUCCESS)
  297. {
  298. TIFFErrorExtR(tif, module, "Decoding error at scanline %lu",
  299. (unsigned long)tif->tif_row);
  300. return 0;
  301. }
  302. assert(lerc_data_sizet == (unsigned int)lerc_data_sizet);
  303. lerc_data = (uint8_t *)sp->compressed_buffer;
  304. lerc_data_size = (unsigned int)lerc_data_sizet;
  305. #else
  306. z_stream strm;
  307. int zlib_ret;
  308. memset(&strm, 0, sizeof(strm));
  309. strm.zalloc = NULL;
  310. strm.zfree = NULL;
  311. strm.opaque = NULL;
  312. zlib_ret = inflateInit(&strm);
  313. if (zlib_ret != Z_OK)
  314. {
  315. TIFFErrorExtR(tif, module, "inflateInit() failed");
  316. inflateEnd(&strm);
  317. return 0;
  318. }
  319. strm.avail_in = (uInt)tif->tif_rawcc;
  320. strm.next_in = tif->tif_rawcp;
  321. strm.avail_out = sp->compressed_size;
  322. strm.next_out = (Bytef *)sp->compressed_buffer;
  323. zlib_ret = inflate(&strm, Z_FINISH);
  324. if (zlib_ret != Z_STREAM_END && zlib_ret != Z_OK)
  325. {
  326. TIFFErrorExtR(tif, module, "inflate() failed");
  327. inflateEnd(&strm);
  328. return 0;
  329. }
  330. lerc_data = (uint8_t *)sp->compressed_buffer;
  331. lerc_data_size = sp->compressed_size - strm.avail_out;
  332. inflateEnd(&strm);
  333. #endif
  334. }
  335. else if (sp->additional_compression == LERC_ADD_COMPRESSION_ZSTD)
  336. {
  337. #ifdef ZSTD_SUPPORT
  338. size_t zstd_ret;
  339. zstd_ret = ZSTD_decompress(sp->compressed_buffer, sp->compressed_size,
  340. tif->tif_rawcp, tif->tif_rawcc);
  341. if (ZSTD_isError(zstd_ret))
  342. {
  343. TIFFErrorExtR(tif, module, "Error in ZSTD_decompress(): %s",
  344. ZSTD_getErrorName(zstd_ret));
  345. return 0;
  346. }
  347. lerc_data = (uint8_t *)sp->compressed_buffer;
  348. lerc_data_size = (unsigned int)zstd_ret;
  349. #else
  350. TIFFErrorExtR(tif, module, "ZSTD support missing");
  351. return 0;
  352. #endif
  353. }
  354. else if (sp->additional_compression != LERC_ADD_COMPRESSION_NONE)
  355. {
  356. TIFFErrorExtR(tif, module, "Unhandled additional compression");
  357. return 0;
  358. }
  359. lerc_ret =
  360. lerc_getBlobInfo(lerc_data, lerc_data_size, infoArray, NULL, 9, 0);
  361. if (lerc_ret != 0)
  362. {
  363. TIFFErrorExtR(tif, module, "lerc_getBlobInfo() failed");
  364. return 0;
  365. }
  366. /* If the configuration is compatible of a LERC mask, and that the */
  367. /* LERC info has dim == samplesperpixel - 1, then there is a LERC */
  368. /* mask. */
  369. if (td->td_planarconfig == PLANARCONFIG_CONTIG && td->td_extrasamples > 0 &&
  370. td->td_sampleinfo[td->td_extrasamples - 1] == EXTRASAMPLE_UNASSALPHA &&
  371. GetLercDataType(tif) == 1 &&
  372. infoArray[2] == td->td_samplesperpixel - 1U)
  373. {
  374. use_mask = 1;
  375. nomask_bands--;
  376. }
  377. else if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP)
  378. {
  379. use_mask = 1;
  380. }
  381. ndims = td->td_planarconfig == PLANARCONFIG_CONTIG ? nomask_bands : 1;
  382. /* Info returned in infoArray is { version, dataType, nDim/nDepth, nCols,
  383. nRows, nBands, nValidPixels, blobSize,
  384. and starting with liblerc 3.0 nRequestedMasks } */
  385. if (infoArray[0] != (unsigned)sp->lerc_version)
  386. {
  387. TIFFWarningExtR(tif, module,
  388. "Unexpected version number: %d. Expected: %d",
  389. infoArray[0], sp->lerc_version);
  390. }
  391. if (infoArray[1] != (unsigned)lerc_data_type)
  392. {
  393. TIFFErrorExtR(tif, module, "Unexpected dataType: %d. Expected: %d",
  394. infoArray[1], lerc_data_type);
  395. return 0;
  396. }
  397. const unsigned nFoundDims = infoArray[2];
  398. #if LERC_AT_LEAST_VERSION(3, 0, 0)
  399. if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  400. td->td_planarconfig == PLANARCONFIG_CONTIG &&
  401. td->td_samplesperpixel > 1)
  402. {
  403. if (nFoundDims != 1 && nFoundDims != (unsigned)ndims)
  404. {
  405. TIFFErrorExtR(tif, module, "Unexpected nDim: %d. Expected: 1 or %d",
  406. nFoundDims, ndims);
  407. return 0;
  408. }
  409. }
  410. else
  411. #endif
  412. if (nFoundDims != (unsigned)ndims)
  413. {
  414. TIFFErrorExtR(tif, module, "Unexpected nDim: %d. Expected: %d",
  415. nFoundDims, ndims);
  416. return 0;
  417. }
  418. if (infoArray[3] != sp->segment_width)
  419. {
  420. TIFFErrorExtR(tif, module, "Unexpected nCols: %d. Expected: %du",
  421. infoArray[3], sp->segment_width);
  422. return 0;
  423. }
  424. if (infoArray[4] != sp->segment_height)
  425. {
  426. TIFFErrorExtR(tif, module, "Unexpected nRows: %d. Expected: %u",
  427. infoArray[4], sp->segment_height);
  428. return 0;
  429. }
  430. const unsigned nFoundBands = infoArray[5];
  431. if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  432. td->td_planarconfig == PLANARCONFIG_CONTIG &&
  433. td->td_samplesperpixel > 1 && nFoundDims == 1)
  434. {
  435. #if !LERC_AT_LEAST_VERSION(3, 0, 0)
  436. if (nFoundBands == td->td_samplesperpixel)
  437. {
  438. TIFFErrorExtR(
  439. tif, module,
  440. "Unexpected nBands: %d. This file may have been generated with "
  441. "a liblerc version >= 3.0, with one mask per band, and is not "
  442. "supported by this older version of liblerc",
  443. nFoundBands);
  444. return 0;
  445. }
  446. #endif
  447. if (nFoundBands != td->td_samplesperpixel)
  448. {
  449. TIFFErrorExtR(tif, module, "Unexpected nBands: %d. Expected: %d",
  450. nFoundBands, td->td_samplesperpixel);
  451. return 0;
  452. }
  453. }
  454. else if (nFoundBands != 1)
  455. {
  456. TIFFErrorExtR(tif, module, "Unexpected nBands: %d. Expected: %d",
  457. nFoundBands, 1);
  458. return 0;
  459. }
  460. if (infoArray[7] != lerc_data_size)
  461. {
  462. TIFFErrorExtR(tif, module, "Unexpected blobSize: %d. Expected: %u",
  463. infoArray[7], lerc_data_size);
  464. return 0;
  465. }
  466. int nRequestedMasks = use_mask ? 1 : 0;
  467. #if LERC_AT_LEAST_VERSION(3, 0, 0)
  468. const int nFoundMasks = infoArray[8];
  469. if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  470. td->td_planarconfig == PLANARCONFIG_CONTIG &&
  471. td->td_samplesperpixel > 1 && nFoundDims == 1)
  472. {
  473. if (nFoundMasks != 0 && nFoundMasks != td->td_samplesperpixel)
  474. {
  475. TIFFErrorExtR(tif, module,
  476. "Unexpected nFoundMasks: %d. Expected: 0 or %d",
  477. nFoundMasks, td->td_samplesperpixel);
  478. return 0;
  479. }
  480. nRequestedMasks = nFoundMasks;
  481. }
  482. else
  483. {
  484. if (nFoundMasks != 0 && nFoundMasks != 1)
  485. {
  486. TIFFErrorExtR(tif, module,
  487. "Unexpected nFoundMasks: %d. Expected: 0 or 1",
  488. nFoundMasks);
  489. return 0;
  490. }
  491. }
  492. if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP && nFoundMasks == 0)
  493. {
  494. nRequestedMasks = 0;
  495. use_mask = 0;
  496. }
  497. #endif
  498. const unsigned nb_pixels = sp->segment_width * sp->segment_height;
  499. #if LERC_AT_LEAST_VERSION(3, 0, 0)
  500. if (nRequestedMasks > 1)
  501. {
  502. unsigned int num_bytes_needed =
  503. nb_pixels * td->td_samplesperpixel * (td->td_bitspersample / 8);
  504. if (sp->uncompressed_buffer_multiband_alloc < num_bytes_needed)
  505. {
  506. _TIFFfreeExt(tif, sp->uncompressed_buffer_multiband);
  507. sp->uncompressed_buffer_multiband =
  508. (uint8_t *)_TIFFmallocExt(tif, num_bytes_needed);
  509. if (!sp->uncompressed_buffer_multiband)
  510. {
  511. sp->uncompressed_buffer_multiband_alloc = 0;
  512. return 0;
  513. }
  514. sp->uncompressed_buffer_multiband_alloc = num_bytes_needed;
  515. }
  516. lerc_ret = lerc_decode(lerc_data, lerc_data_size, nRequestedMasks,
  517. sp->mask_buffer, nFoundDims, sp->segment_width,
  518. sp->segment_height, nFoundBands, lerc_data_type,
  519. sp->uncompressed_buffer_multiband);
  520. }
  521. else
  522. #endif
  523. {
  524. lerc_ret =
  525. lerc_decode(lerc_data, lerc_data_size,
  526. #if LERC_AT_LEAST_VERSION(3, 0, 0)
  527. nRequestedMasks,
  528. #endif
  529. use_mask ? sp->mask_buffer : NULL, nFoundDims,
  530. sp->segment_width, sp->segment_height, nFoundBands,
  531. lerc_data_type, sp->uncompressed_buffer);
  532. }
  533. if (lerc_ret != 0)
  534. {
  535. TIFFErrorExtR(tif, module, "lerc_decode() failed");
  536. return 0;
  537. }
  538. /* Interleave alpha mask with other samples. */
  539. if (use_mask && GetLercDataType(tif) == 1)
  540. {
  541. unsigned src_stride =
  542. (td->td_samplesperpixel - 1) * (td->td_bitspersample / 8);
  543. unsigned dst_stride =
  544. td->td_samplesperpixel * (td->td_bitspersample / 8);
  545. unsigned i = sp->segment_width * sp->segment_height;
  546. /* Operate from end to begin to be able to move in place */
  547. while (i > 0 && i > nomask_bands)
  548. {
  549. i--;
  550. sp->uncompressed_buffer[i * dst_stride + td->td_samplesperpixel -
  551. 1] = 255 * sp->mask_buffer[i];
  552. memcpy(sp->uncompressed_buffer + i * dst_stride,
  553. sp->uncompressed_buffer + i * src_stride, src_stride);
  554. }
  555. /* First pixels must use memmove due to overlapping areas */
  556. while (i > 0)
  557. {
  558. i--;
  559. sp->uncompressed_buffer[i * dst_stride + td->td_samplesperpixel -
  560. 1] = 255 * sp->mask_buffer[i];
  561. memmove(sp->uncompressed_buffer + i * dst_stride,
  562. sp->uncompressed_buffer + i * src_stride, src_stride);
  563. }
  564. }
  565. else if (use_mask && td->td_sampleformat == SAMPLEFORMAT_IEEEFP)
  566. {
  567. unsigned i;
  568. #if WORDS_BIGENDIAN
  569. const unsigned char nan_bytes[] = {0x7f, 0xc0, 0, 0};
  570. #else
  571. const unsigned char nan_bytes[] = {0, 0, 0xc0, 0x7f};
  572. #endif
  573. float nan_float32;
  574. memcpy(&nan_float32, nan_bytes, 4);
  575. if (td->td_planarconfig == PLANARCONFIG_SEPARATE ||
  576. td->td_samplesperpixel == 1)
  577. {
  578. if (td->td_bitspersample == 32)
  579. {
  580. for (i = 0; i < nb_pixels; i++)
  581. {
  582. if (sp->mask_buffer[i] == 0)
  583. ((float *)sp->uncompressed_buffer)[i] = nan_float32;
  584. }
  585. }
  586. else
  587. {
  588. const double nan_float64 = nan_float32;
  589. for (i = 0; i < nb_pixels; i++)
  590. {
  591. if (sp->mask_buffer[i] == 0)
  592. ((double *)sp->uncompressed_buffer)[i] = nan_float64;
  593. }
  594. }
  595. }
  596. else if (nRequestedMasks == 1)
  597. {
  598. assert(nFoundDims == td->td_samplesperpixel);
  599. assert(nFoundBands == 1);
  600. unsigned k = 0;
  601. if (td->td_bitspersample == 32)
  602. {
  603. for (i = 0; i < nb_pixels; i++)
  604. {
  605. for (int j = 0; j < td->td_samplesperpixel; j++)
  606. {
  607. if (sp->mask_buffer[i] == 0)
  608. ((float *)sp->uncompressed_buffer)[k] = nan_float32;
  609. ++k;
  610. }
  611. }
  612. }
  613. else
  614. {
  615. const double nan_float64 = nan_float32;
  616. for (i = 0; i < nb_pixels; i++)
  617. {
  618. for (int j = 0; j < td->td_samplesperpixel; j++)
  619. {
  620. if (sp->mask_buffer[i] == 0)
  621. ((double *)sp->uncompressed_buffer)[k] =
  622. nan_float64;
  623. ++k;
  624. }
  625. }
  626. }
  627. }
  628. #if LERC_AT_LEAST_VERSION(3, 0, 0)
  629. else
  630. {
  631. assert(nRequestedMasks == td->td_samplesperpixel);
  632. assert(nFoundDims == 1);
  633. assert(nFoundBands == td->td_samplesperpixel);
  634. unsigned k = 0;
  635. if (td->td_bitspersample == 32)
  636. {
  637. for (i = 0; i < nb_pixels; i++)
  638. {
  639. for (int j = 0; j < td->td_samplesperpixel; j++)
  640. {
  641. if (sp->mask_buffer[i + j * nb_pixels] == 0)
  642. ((float *)sp->uncompressed_buffer)[k] = nan_float32;
  643. else
  644. ((float *)sp->uncompressed_buffer)[k] =
  645. ((float *)sp->uncompressed_buffer_multiband)
  646. [i + j * nb_pixels];
  647. ++k;
  648. }
  649. }
  650. }
  651. else
  652. {
  653. const double nan_float64 = nan_float32;
  654. for (i = 0; i < nb_pixels; i++)
  655. {
  656. for (int j = 0; j < td->td_samplesperpixel; j++)
  657. {
  658. if (sp->mask_buffer[i + j * nb_pixels] == 0)
  659. ((double *)sp->uncompressed_buffer)[k] =
  660. nan_float64;
  661. else
  662. ((double *)sp->uncompressed_buffer)[k] =
  663. ((double *)sp->uncompressed_buffer_multiband)
  664. [i + j * nb_pixels];
  665. ++k;
  666. }
  667. }
  668. }
  669. }
  670. #endif
  671. }
  672. return 1;
  673. }
  674. /*
  675. * Decode a strip, tile or scanline.
  676. */
  677. static int LERCDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
  678. {
  679. static const char module[] = "LERCDecode";
  680. LERCState *sp = LERCDecoderState(tif);
  681. (void)s;
  682. assert(sp != NULL);
  683. assert(sp->state == LSTATE_INIT_DECODE);
  684. if (sp->uncompressed_buffer == NULL)
  685. {
  686. memset(op, 0, (size_t)occ);
  687. TIFFErrorExtR(tif, module, "Uncompressed buffer not allocated");
  688. return 0;
  689. }
  690. if ((uint64_t)sp->uncompressed_offset + (uint64_t)occ >
  691. sp->uncompressed_size)
  692. {
  693. memset(op, 0, (size_t)occ);
  694. TIFFErrorExtR(tif, module, "Too many bytes read");
  695. return 0;
  696. }
  697. memcpy(op, sp->uncompressed_buffer + sp->uncompressed_offset, occ);
  698. sp->uncompressed_offset += (unsigned)occ;
  699. return 1;
  700. }
  701. #ifndef LERC_READ_ONLY
  702. static int LERCSetupEncode(TIFF *tif)
  703. {
  704. LERCState *sp = LERCEncoderState(tif);
  705. assert(sp != NULL);
  706. if (sp->state & LSTATE_INIT_DECODE)
  707. {
  708. sp->state = 0;
  709. }
  710. sp->state |= LSTATE_INIT_ENCODE;
  711. return 1;
  712. }
  713. /*
  714. * Reset encoding state at the start of a strip.
  715. */
  716. static int LERCPreEncode(TIFF *tif, uint16_t s)
  717. {
  718. static const char module[] = "LERCPreEncode";
  719. LERCState *sp = LERCEncoderState(tif);
  720. int lerc_data_type;
  721. (void)s;
  722. assert(sp != NULL);
  723. if (sp->state != LSTATE_INIT_ENCODE)
  724. tif->tif_setupencode(tif);
  725. lerc_data_type = GetLercDataType(tif);
  726. if (lerc_data_type < 0)
  727. return 0;
  728. if (!SetupBuffers(tif, sp, module))
  729. return 0;
  730. return 1;
  731. }
  732. /*
  733. * Encode a chunk of pixels.
  734. */
  735. static int LERCEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
  736. {
  737. static const char module[] = "LERCEncode";
  738. LERCState *sp = LERCEncoderState(tif);
  739. (void)s;
  740. assert(sp != NULL);
  741. assert(sp->state == LSTATE_INIT_ENCODE);
  742. if ((uint64_t)sp->uncompressed_offset + (uint64_t)cc >
  743. sp->uncompressed_size)
  744. {
  745. TIFFErrorExtR(tif, module, "Too many bytes written");
  746. return 0;
  747. }
  748. memcpy(sp->uncompressed_buffer + sp->uncompressed_offset, bp, cc);
  749. sp->uncompressed_offset += (unsigned)cc;
  750. return 1;
  751. }
  752. /*
  753. * Finish off an encoded strip by flushing it.
  754. */
  755. static int LERCPostEncode(TIFF *tif)
  756. {
  757. lerc_status lerc_ret;
  758. static const char module[] = "LERCPostEncode";
  759. LERCState *sp = LERCEncoderState(tif);
  760. unsigned int numBytesWritten = 0;
  761. TIFFDirectory *td = &tif->tif_dir;
  762. int use_mask = 0;
  763. unsigned dst_nbands = td->td_samplesperpixel;
  764. if (sp->uncompressed_offset != sp->uncompressed_size)
  765. {
  766. TIFFErrorExtR(tif, module, "Unexpected number of bytes in the buffer");
  767. return 0;
  768. }
  769. int mask_count = 1;
  770. const unsigned nb_pixels = sp->segment_width * sp->segment_height;
  771. /* Extract alpha mask (if containing only 0 and 255 values, */
  772. /* and compact array of regular bands */
  773. if (td->td_planarconfig == PLANARCONFIG_CONTIG && td->td_extrasamples > 0 &&
  774. td->td_sampleinfo[td->td_extrasamples - 1] == EXTRASAMPLE_UNASSALPHA &&
  775. GetLercDataType(tif) == 1)
  776. {
  777. const unsigned dst_stride =
  778. (td->td_samplesperpixel - 1) * (td->td_bitspersample / 8);
  779. const unsigned src_stride =
  780. td->td_samplesperpixel * (td->td_bitspersample / 8);
  781. unsigned i = 0;
  782. use_mask = 1;
  783. for (i = 0; i < nb_pixels; i++)
  784. {
  785. int v = sp->uncompressed_buffer[i * src_stride +
  786. td->td_samplesperpixel - 1];
  787. if (v != 0 && v != 255)
  788. {
  789. use_mask = 0;
  790. break;
  791. }
  792. }
  793. if (use_mask)
  794. {
  795. dst_nbands--;
  796. /* First pixels must use memmove due to overlapping areas */
  797. for (i = 0; i < dst_nbands && i < nb_pixels; i++)
  798. {
  799. memmove(sp->uncompressed_buffer + i * dst_stride,
  800. sp->uncompressed_buffer + i * src_stride, dst_stride);
  801. sp->mask_buffer[i] =
  802. sp->uncompressed_buffer[i * src_stride +
  803. td->td_samplesperpixel - 1];
  804. }
  805. for (; i < nb_pixels; i++)
  806. {
  807. memcpy(sp->uncompressed_buffer + i * dst_stride,
  808. sp->uncompressed_buffer + i * src_stride, dst_stride);
  809. sp->mask_buffer[i] =
  810. sp->uncompressed_buffer[i * src_stride +
  811. td->td_samplesperpixel - 1];
  812. }
  813. }
  814. }
  815. else if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  816. (td->td_bitspersample == 32 || td->td_bitspersample == 64))
  817. {
  818. /* Check for NaN values */
  819. unsigned i;
  820. if (td->td_bitspersample == 32)
  821. {
  822. if (td->td_planarconfig == PLANARCONFIG_CONTIG && dst_nbands > 1)
  823. {
  824. unsigned k = 0;
  825. for (i = 0; i < nb_pixels; i++)
  826. {
  827. int count_nan = 0;
  828. for (int j = 0; j < td->td_samplesperpixel; ++j)
  829. {
  830. const float val = ((float *)sp->uncompressed_buffer)[k];
  831. ++k;
  832. if (val != val)
  833. {
  834. ++count_nan;
  835. }
  836. }
  837. if (count_nan > 0)
  838. {
  839. use_mask = 1;
  840. if (count_nan < td->td_samplesperpixel)
  841. {
  842. mask_count = td->td_samplesperpixel;
  843. break;
  844. }
  845. }
  846. }
  847. }
  848. else
  849. {
  850. for (i = 0; i < nb_pixels; i++)
  851. {
  852. const float val = ((float *)sp->uncompressed_buffer)[i];
  853. if (val != val)
  854. {
  855. use_mask = 1;
  856. break;
  857. }
  858. }
  859. }
  860. }
  861. else
  862. {
  863. if (td->td_planarconfig == PLANARCONFIG_CONTIG && dst_nbands > 1)
  864. {
  865. unsigned k = 0;
  866. for (i = 0; i < nb_pixels; i++)
  867. {
  868. int count_nan = 0;
  869. for (int j = 0; j < td->td_samplesperpixel; ++j)
  870. {
  871. const double val =
  872. ((double *)sp->uncompressed_buffer)[k];
  873. ++k;
  874. if (val != val)
  875. {
  876. ++count_nan;
  877. }
  878. }
  879. if (count_nan > 0)
  880. {
  881. use_mask = 1;
  882. if (count_nan < td->td_samplesperpixel)
  883. {
  884. mask_count = td->td_samplesperpixel;
  885. break;
  886. }
  887. }
  888. }
  889. }
  890. else
  891. {
  892. for (i = 0; i < nb_pixels; i++)
  893. {
  894. const double val = ((double *)sp->uncompressed_buffer)[i];
  895. if (val != val)
  896. {
  897. use_mask = 1;
  898. break;
  899. }
  900. }
  901. }
  902. }
  903. if (use_mask)
  904. {
  905. if (mask_count > 1)
  906. {
  907. #if LERC_AT_LEAST_VERSION(3, 0, 0)
  908. unsigned int num_bytes_needed =
  909. nb_pixels * dst_nbands * (td->td_bitspersample / 8);
  910. if (sp->uncompressed_buffer_multiband_alloc < num_bytes_needed)
  911. {
  912. _TIFFfreeExt(tif, sp->uncompressed_buffer_multiband);
  913. sp->uncompressed_buffer_multiband =
  914. (uint8_t *)_TIFFmallocExt(tif, num_bytes_needed);
  915. if (!sp->uncompressed_buffer_multiband)
  916. {
  917. sp->uncompressed_buffer_multiband_alloc = 0;
  918. return 0;
  919. }
  920. sp->uncompressed_buffer_multiband_alloc = num_bytes_needed;
  921. }
  922. unsigned k = 0;
  923. if (td->td_bitspersample == 32)
  924. {
  925. for (i = 0; i < nb_pixels; i++)
  926. {
  927. for (int j = 0; j < td->td_samplesperpixel; ++j)
  928. {
  929. const float val =
  930. ((float *)sp->uncompressed_buffer)[k];
  931. ((float *)sp->uncompressed_buffer_multiband)
  932. [i + j * nb_pixels] = val;
  933. ++k;
  934. sp->mask_buffer[i + j * nb_pixels] =
  935. (val == val) ? 255 : 0;
  936. }
  937. }
  938. }
  939. else
  940. {
  941. for (i = 0; i < nb_pixels; i++)
  942. {
  943. for (int j = 0; j < td->td_samplesperpixel; ++j)
  944. {
  945. const double val =
  946. ((double *)sp->uncompressed_buffer)[k];
  947. ((double *)sp->uncompressed_buffer_multiband)
  948. [i + j * nb_pixels] = val;
  949. ++k;
  950. sp->mask_buffer[i + j * nb_pixels] =
  951. (val == val) ? 255 : 0;
  952. }
  953. }
  954. }
  955. #else
  956. TIFFErrorExtR(tif, module,
  957. "lerc_encode() would need to create one mask per "
  958. "sample, but this requires liblerc >= 3.0");
  959. return 0;
  960. #endif
  961. }
  962. else if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
  963. dst_nbands > 1)
  964. {
  965. if (td->td_bitspersample == 32)
  966. {
  967. for (i = 0; i < nb_pixels; i++)
  968. {
  969. const float val =
  970. ((float *)sp->uncompressed_buffer)[i * dst_nbands];
  971. sp->mask_buffer[i] = (val == val) ? 255 : 0;
  972. }
  973. }
  974. else
  975. {
  976. for (i = 0; i < nb_pixels; i++)
  977. {
  978. const double val =
  979. ((double *)sp->uncompressed_buffer)[i * dst_nbands];
  980. sp->mask_buffer[i] = (val == val) ? 255 : 0;
  981. }
  982. }
  983. }
  984. else
  985. {
  986. if (td->td_bitspersample == 32)
  987. {
  988. for (i = 0; i < nb_pixels; i++)
  989. {
  990. const float val = ((float *)sp->uncompressed_buffer)[i];
  991. sp->mask_buffer[i] = (val == val) ? 255 : 0;
  992. }
  993. }
  994. else
  995. {
  996. for (i = 0; i < nb_pixels; i++)
  997. {
  998. const double val =
  999. ((double *)sp->uncompressed_buffer)[i];
  1000. sp->mask_buffer[i] = (val == val) ? 255 : 0;
  1001. }
  1002. }
  1003. }
  1004. }
  1005. }
  1006. unsigned int estimated_compressed_size = sp->uncompressed_alloc;
  1007. #if LERC_AT_LEAST_VERSION(3, 0, 0)
  1008. if (mask_count > 1)
  1009. {
  1010. estimated_compressed_size += nb_pixels * mask_count / 8;
  1011. }
  1012. #endif
  1013. if (sp->compressed_size < estimated_compressed_size)
  1014. {
  1015. _TIFFfreeExt(tif, sp->compressed_buffer);
  1016. sp->compressed_buffer = _TIFFmallocExt(tif, estimated_compressed_size);
  1017. if (!sp->compressed_buffer)
  1018. {
  1019. sp->compressed_size = 0;
  1020. return 0;
  1021. }
  1022. sp->compressed_size = estimated_compressed_size;
  1023. }
  1024. #if LERC_AT_LEAST_VERSION(3, 0, 0)
  1025. if (mask_count > 1)
  1026. {
  1027. lerc_ret = lerc_encodeForVersion(
  1028. sp->uncompressed_buffer_multiband, sp->lerc_version,
  1029. GetLercDataType(tif), 1, sp->segment_width, sp->segment_height,
  1030. dst_nbands, dst_nbands, sp->mask_buffer, sp->maxzerror,
  1031. (unsigned char *)sp->compressed_buffer, sp->compressed_size,
  1032. &numBytesWritten);
  1033. }
  1034. else
  1035. #endif
  1036. {
  1037. lerc_ret = lerc_encodeForVersion(
  1038. sp->uncompressed_buffer, sp->lerc_version, GetLercDataType(tif),
  1039. td->td_planarconfig == PLANARCONFIG_CONTIG ? dst_nbands : 1,
  1040. sp->segment_width, sp->segment_height, 1,
  1041. #if LERC_AT_LEAST_VERSION(3, 0, 0)
  1042. use_mask ? 1 : 0,
  1043. #endif
  1044. use_mask ? sp->mask_buffer : NULL, sp->maxzerror,
  1045. (unsigned char *)sp->compressed_buffer, sp->compressed_size,
  1046. &numBytesWritten);
  1047. }
  1048. if (lerc_ret != 0)
  1049. {
  1050. TIFFErrorExtR(tif, module, "lerc_encode() failed");
  1051. return 0;
  1052. }
  1053. assert(numBytesWritten < estimated_compressed_size);
  1054. if (sp->additional_compression == LERC_ADD_COMPRESSION_DEFLATE)
  1055. {
  1056. #if LIBDEFLATE_SUPPORT
  1057. if (sp->libdeflate_enc == NULL)
  1058. {
  1059. /* To get results as good as zlib, we ask for an extra */
  1060. /* level of compression */
  1061. sp->libdeflate_enc = libdeflate_alloc_compressor(
  1062. sp->zipquality == Z_DEFAULT_COMPRESSION ? 7
  1063. : sp->zipquality >= 6 && sp->zipquality <= 9
  1064. ? sp->zipquality + 1
  1065. : sp->zipquality);
  1066. if (sp->libdeflate_enc == NULL)
  1067. {
  1068. TIFFErrorExtR(tif, module, "Cannot allocate compressor");
  1069. return 0;
  1070. }
  1071. }
  1072. /* Should not happen normally */
  1073. if (libdeflate_zlib_compress_bound(
  1074. sp->libdeflate_enc, numBytesWritten) > sp->uncompressed_alloc)
  1075. {
  1076. TIFFErrorExtR(tif, module,
  1077. "Output buffer for libdeflate too small");
  1078. return 0;
  1079. }
  1080. tif->tif_rawcc = libdeflate_zlib_compress(
  1081. sp->libdeflate_enc, sp->compressed_buffer, numBytesWritten,
  1082. sp->uncompressed_buffer, sp->uncompressed_alloc);
  1083. if (tif->tif_rawcc == 0)
  1084. {
  1085. TIFFErrorExtR(tif, module, "Encoder error at scanline %lu",
  1086. (unsigned long)tif->tif_row);
  1087. return 0;
  1088. }
  1089. #else
  1090. z_stream strm;
  1091. int zlib_ret;
  1092. int cappedQuality = sp->zipquality;
  1093. if (cappedQuality > Z_BEST_COMPRESSION)
  1094. cappedQuality = Z_BEST_COMPRESSION;
  1095. memset(&strm, 0, sizeof(strm));
  1096. strm.zalloc = NULL;
  1097. strm.zfree = NULL;
  1098. strm.opaque = NULL;
  1099. zlib_ret = deflateInit(&strm, cappedQuality);
  1100. if (zlib_ret != Z_OK)
  1101. {
  1102. TIFFErrorExtR(tif, module, "deflateInit() failed");
  1103. return 0;
  1104. }
  1105. strm.avail_in = numBytesWritten;
  1106. strm.next_in = sp->compressed_buffer;
  1107. strm.avail_out = sp->uncompressed_alloc;
  1108. strm.next_out = sp->uncompressed_buffer;
  1109. zlib_ret = deflate(&strm, Z_FINISH);
  1110. if (zlib_ret == Z_STREAM_END)
  1111. {
  1112. tif->tif_rawcc = sp->uncompressed_alloc - strm.avail_out;
  1113. }
  1114. deflateEnd(&strm);
  1115. if (zlib_ret != Z_STREAM_END)
  1116. {
  1117. TIFFErrorExtR(tif, module, "deflate() failed");
  1118. return 0;
  1119. }
  1120. #endif
  1121. {
  1122. int ret;
  1123. uint8_t *tif_rawdata_backup = tif->tif_rawdata;
  1124. tif->tif_rawdata = sp->uncompressed_buffer;
  1125. ret = TIFFFlushData1(tif);
  1126. tif->tif_rawdata = tif_rawdata_backup;
  1127. if (!ret)
  1128. {
  1129. return 0;
  1130. }
  1131. }
  1132. }
  1133. else if (sp->additional_compression == LERC_ADD_COMPRESSION_ZSTD)
  1134. {
  1135. #ifdef ZSTD_SUPPORT
  1136. size_t zstd_ret = ZSTD_compress(
  1137. sp->uncompressed_buffer, sp->uncompressed_alloc,
  1138. sp->compressed_buffer, numBytesWritten, sp->zstd_compress_level);
  1139. if (ZSTD_isError(zstd_ret))
  1140. {
  1141. TIFFErrorExtR(tif, module, "Error in ZSTD_compress(): %s",
  1142. ZSTD_getErrorName(zstd_ret));
  1143. return 0;
  1144. }
  1145. {
  1146. int ret;
  1147. uint8_t *tif_rawdata_backup = tif->tif_rawdata;
  1148. tif->tif_rawdata = sp->uncompressed_buffer;
  1149. tif->tif_rawcc = zstd_ret;
  1150. ret = TIFFFlushData1(tif);
  1151. tif->tif_rawdata = tif_rawdata_backup;
  1152. if (!ret)
  1153. {
  1154. return 0;
  1155. }
  1156. }
  1157. #else
  1158. TIFFErrorExtR(tif, module, "ZSTD support missing");
  1159. return 0;
  1160. #endif
  1161. }
  1162. else if (sp->additional_compression != LERC_ADD_COMPRESSION_NONE)
  1163. {
  1164. TIFFErrorExtR(tif, module, "Unhandled additional compression");
  1165. return 0;
  1166. }
  1167. else
  1168. {
  1169. int ret;
  1170. uint8_t *tif_rawdata_backup = tif->tif_rawdata;
  1171. tif->tif_rawdata = (uint8_t *)sp->compressed_buffer;
  1172. tif->tif_rawcc = numBytesWritten;
  1173. ret = TIFFFlushData1(tif);
  1174. tif->tif_rawdata = tif_rawdata_backup;
  1175. if (!ret)
  1176. return 0;
  1177. }
  1178. return 1;
  1179. }
  1180. #endif /* LERC_READ_ONLY */
  1181. static void LERCCleanup(TIFF *tif)
  1182. {
  1183. LERCState *sp = GetLERCState(tif);
  1184. assert(sp != NULL);
  1185. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  1186. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  1187. _TIFFfreeExt(tif, sp->uncompressed_buffer);
  1188. _TIFFfreeExt(tif, sp->uncompressed_buffer_multiband);
  1189. _TIFFfreeExt(tif, sp->compressed_buffer);
  1190. _TIFFfreeExt(tif, sp->mask_buffer);
  1191. #if LIBDEFLATE_SUPPORT
  1192. if (sp->libdeflate_dec)
  1193. libdeflate_free_decompressor(sp->libdeflate_dec);
  1194. if (sp->libdeflate_enc)
  1195. libdeflate_free_compressor(sp->libdeflate_enc);
  1196. #endif
  1197. _TIFFfreeExt(tif, sp);
  1198. tif->tif_data = NULL;
  1199. _TIFFSetDefaultCompressionState(tif);
  1200. }
  1201. static const TIFFField LERCFields[] = {
  1202. {TIFFTAG_LERC_PARAMETERS, TIFF_VARIABLE2, TIFF_VARIABLE2, TIFF_LONG, 0,
  1203. TIFF_SETGET_C32_UINT32, FIELD_CUSTOM, FALSE, TRUE,
  1204. (char *)"LercParameters", NULL},
  1205. {TIFFTAG_LERC_MAXZERROR, 0, 0, TIFF_ANY, 0, TIFF_SETGET_DOUBLE,
  1206. FIELD_PSEUDO, TRUE, FALSE, (char *)"LercMaximumError", NULL},
  1207. {TIFFTAG_LERC_VERSION, 0, 0, TIFF_ANY, 0, TIFF_SETGET_UINT32, FIELD_PSEUDO,
  1208. FALSE, FALSE, (char *)"LercVersion", NULL},
  1209. {TIFFTAG_LERC_ADD_COMPRESSION, 0, 0, TIFF_ANY, 0, TIFF_SETGET_UINT32,
  1210. FIELD_PSEUDO, FALSE, FALSE, (char *)"LercAdditionalCompression", NULL},
  1211. {TIFFTAG_ZSTD_LEVEL, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, TRUE,
  1212. FALSE, (char *)"ZSTD zstd_compress_level", NULL},
  1213. {TIFFTAG_ZIPQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, FIELD_PSEUDO, TRUE,
  1214. FALSE, (char *)"", NULL},
  1215. };
  1216. static int LERCVSetFieldBase(TIFF *tif, uint32_t tag, ...)
  1217. {
  1218. LERCState *sp = GetLERCState(tif);
  1219. int ret;
  1220. va_list ap;
  1221. va_start(ap, tag);
  1222. ret = (*sp->vsetparent)(tif, tag, ap);
  1223. va_end(ap);
  1224. return ret;
  1225. }
  1226. static int LERCVSetField(TIFF *tif, uint32_t tag, va_list ap)
  1227. {
  1228. static const char module[] = "LERCVSetField";
  1229. LERCState *sp = GetLERCState(tif);
  1230. switch (tag)
  1231. {
  1232. case TIFFTAG_LERC_PARAMETERS:
  1233. {
  1234. uint32_t count = va_arg(ap, int);
  1235. int *params = va_arg(ap, int *);
  1236. if (count < 2)
  1237. {
  1238. TIFFErrorExtR(tif, module,
  1239. "Invalid count for LercParameters: %u", count);
  1240. return 0;
  1241. }
  1242. sp->lerc_version = params[0];
  1243. sp->additional_compression = params[1];
  1244. return LERCVSetFieldBase(tif, TIFFTAG_LERC_PARAMETERS, count,
  1245. params);
  1246. }
  1247. case TIFFTAG_LERC_MAXZERROR:
  1248. sp->maxzerror = va_arg(ap, double);
  1249. return 1;
  1250. case TIFFTAG_LERC_VERSION:
  1251. {
  1252. int params[2] = {0, 0};
  1253. int version = va_arg(ap, int);
  1254. if (version != LERC_VERSION_2_4)
  1255. {
  1256. TIFFErrorExtR(tif, module, "Invalid value for LercVersion: %d",
  1257. version);
  1258. return 0;
  1259. }
  1260. sp->lerc_version = version;
  1261. params[0] = sp->lerc_version;
  1262. params[1] = sp->additional_compression;
  1263. return LERCVSetFieldBase(tif, TIFFTAG_LERC_PARAMETERS, 2, params);
  1264. }
  1265. case TIFFTAG_LERC_ADD_COMPRESSION:
  1266. {
  1267. int params[2] = {0, 0};
  1268. int additional_compression = va_arg(ap, int);
  1269. #ifndef ZSTD_SUPPORT
  1270. if (additional_compression == LERC_ADD_COMPRESSION_ZSTD)
  1271. {
  1272. TIFFErrorExtR(tif, module,
  1273. "LERC_ZSTD requested, but ZSTD not available");
  1274. return 0;
  1275. }
  1276. #endif
  1277. if (additional_compression != LERC_ADD_COMPRESSION_NONE &&
  1278. additional_compression != LERC_ADD_COMPRESSION_DEFLATE &&
  1279. additional_compression != LERC_ADD_COMPRESSION_ZSTD)
  1280. {
  1281. TIFFErrorExtR(tif, module,
  1282. "Invalid value for LercAdditionalCompression: %d",
  1283. additional_compression);
  1284. return 0;
  1285. }
  1286. sp->additional_compression = additional_compression;
  1287. params[0] = sp->lerc_version;
  1288. params[1] = sp->additional_compression;
  1289. return LERCVSetFieldBase(tif, TIFFTAG_LERC_PARAMETERS, 2, params);
  1290. }
  1291. #ifdef ZSTD_SUPPORT
  1292. case TIFFTAG_ZSTD_LEVEL:
  1293. {
  1294. sp->zstd_compress_level = (int)va_arg(ap, int);
  1295. if (sp->zstd_compress_level <= 0 ||
  1296. sp->zstd_compress_level > ZSTD_maxCLevel())
  1297. {
  1298. TIFFWarningExtR(tif, module,
  1299. "ZSTD_LEVEL should be between 1 and %d",
  1300. ZSTD_maxCLevel());
  1301. }
  1302. return 1;
  1303. }
  1304. #endif
  1305. case TIFFTAG_ZIPQUALITY:
  1306. {
  1307. sp->zipquality = (int)va_arg(ap, int);
  1308. if (sp->zipquality < Z_DEFAULT_COMPRESSION ||
  1309. sp->zipquality > LIBDEFLATE_MAX_COMPRESSION_LEVEL)
  1310. {
  1311. TIFFErrorExtR(
  1312. tif, module,
  1313. "Invalid ZipQuality value. Should be in [-1,%d] range",
  1314. LIBDEFLATE_MAX_COMPRESSION_LEVEL);
  1315. return 0;
  1316. }
  1317. #if LIBDEFLATE_SUPPORT
  1318. if (sp->libdeflate_enc)
  1319. {
  1320. libdeflate_free_compressor(sp->libdeflate_enc);
  1321. sp->libdeflate_enc = NULL;
  1322. }
  1323. #endif
  1324. return (1);
  1325. }
  1326. default:
  1327. return (*sp->vsetparent)(tif, tag, ap);
  1328. }
  1329. /*NOTREACHED*/
  1330. }
  1331. static int LERCVGetField(TIFF *tif, uint32_t tag, va_list ap)
  1332. {
  1333. LERCState *sp = GetLERCState(tif);
  1334. switch (tag)
  1335. {
  1336. case TIFFTAG_LERC_MAXZERROR:
  1337. *va_arg(ap, double *) = sp->maxzerror;
  1338. break;
  1339. case TIFFTAG_LERC_VERSION:
  1340. *va_arg(ap, int *) = sp->lerc_version;
  1341. break;
  1342. case TIFFTAG_LERC_ADD_COMPRESSION:
  1343. *va_arg(ap, int *) = sp->additional_compression;
  1344. break;
  1345. case TIFFTAG_ZSTD_LEVEL:
  1346. *va_arg(ap, int *) = sp->zstd_compress_level;
  1347. break;
  1348. case TIFFTAG_ZIPQUALITY:
  1349. *va_arg(ap, int *) = sp->zipquality;
  1350. break;
  1351. default:
  1352. return (*sp->vgetparent)(tif, tag, ap);
  1353. }
  1354. return 1;
  1355. }
  1356. int TIFFInitLERC(TIFF *tif, int scheme)
  1357. {
  1358. static const char module[] = "TIFFInitLERC";
  1359. LERCState *sp;
  1360. (void)scheme;
  1361. assert(scheme == COMPRESSION_LERC);
  1362. /*
  1363. * Merge codec-specific tag information.
  1364. */
  1365. if (!_TIFFMergeFields(tif, LERCFields, TIFFArrayCount(LERCFields)))
  1366. {
  1367. TIFFErrorExtR(tif, module, "Merging LERC codec-specific tags failed");
  1368. return 0;
  1369. }
  1370. /*
  1371. * Allocate state block so tag methods have storage to record values.
  1372. */
  1373. tif->tif_data = (uint8_t *)_TIFFcallocExt(tif, 1, sizeof(LERCState));
  1374. if (tif->tif_data == NULL)
  1375. goto bad;
  1376. sp = GetLERCState(tif);
  1377. /*
  1378. * Override parent get/set field methods.
  1379. */
  1380. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  1381. tif->tif_tagmethods.vgetfield = LERCVGetField; /* hook for codec tags */
  1382. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  1383. tif->tif_tagmethods.vsetfield = LERCVSetField; /* hook for codec tags */
  1384. /*
  1385. * Install codec methods.
  1386. */
  1387. tif->tif_fixuptags = LERCFixupTags;
  1388. tif->tif_setupdecode = LERCSetupDecode;
  1389. tif->tif_predecode = LERCPreDecode;
  1390. tif->tif_decoderow = LERCDecode;
  1391. tif->tif_decodestrip = LERCDecode;
  1392. tif->tif_decodetile = LERCDecode;
  1393. #ifndef LERC_READ_ONLY
  1394. tif->tif_setupencode = LERCSetupEncode;
  1395. tif->tif_preencode = LERCPreEncode;
  1396. tif->tif_postencode = LERCPostEncode;
  1397. tif->tif_encoderow = LERCEncode;
  1398. tif->tif_encodestrip = LERCEncode;
  1399. tif->tif_encodetile = LERCEncode;
  1400. #endif
  1401. tif->tif_cleanup = LERCCleanup;
  1402. /* Default values for codec-specific fields */
  1403. TIFFSetField(tif, TIFFTAG_LERC_VERSION, LERC_VERSION_2_4);
  1404. TIFFSetField(tif, TIFFTAG_LERC_ADD_COMPRESSION, LERC_ADD_COMPRESSION_NONE);
  1405. sp->maxzerror = 0.0;
  1406. sp->zstd_compress_level = 9; /* default comp. level */
  1407. sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
  1408. sp->state = 0;
  1409. return 1;
  1410. bad:
  1411. TIFFErrorExtR(tif, module, "No space for LERC state block");
  1412. return 0;
  1413. }
  1414. #endif /* LERC_SUPPORT */