tif_lerc.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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. #error #include "Lerc_c_api.h"
  33. #include "zlib.h"
  34. #ifdef ZSTD_SUPPORT
  35. #include "zstd.h"
  36. #endif
  37. #if LIBDEFLATE_SUPPORT
  38. #error #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. unsigned int mask_size;
  65. uint8_t *mask_buffer;
  66. unsigned int compressed_size;
  67. void *compressed_buffer;
  68. #if LIBDEFLATE_SUPPORT
  69. struct libdeflate_decompressor *libdeflate_dec;
  70. struct libdeflate_compressor *libdeflate_enc;
  71. #endif
  72. TIFFVGetMethod vgetparent; /* super-class method */
  73. TIFFVSetMethod vsetparent; /* super-class method */
  74. } LERCState;
  75. #define LState(tif) ((LERCState *)(tif)->tif_data)
  76. #define DecoderState(tif) LState(tif)
  77. #define EncoderState(tif) LState(tif)
  78. static int LERCEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s);
  79. static int LERCDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s);
  80. static int LERCFixupTags(TIFF *tif)
  81. {
  82. (void)tif;
  83. return 1;
  84. }
  85. static int LERCSetupDecode(TIFF *tif)
  86. {
  87. LERCState *sp = DecoderState(tif);
  88. assert(sp != NULL);
  89. /* if we were last encoding, terminate this mode */
  90. if (sp->state & LSTATE_INIT_ENCODE)
  91. {
  92. sp->state = 0;
  93. }
  94. sp->state |= LSTATE_INIT_DECODE;
  95. return 1;
  96. }
  97. static int GetLercDataType(TIFF *tif)
  98. {
  99. TIFFDirectory *td = &tif->tif_dir;
  100. static const char module[] = "GetLercDataType";
  101. if (td->td_sampleformat == SAMPLEFORMAT_INT && td->td_bitspersample == 8)
  102. {
  103. return 0;
  104. }
  105. if (td->td_sampleformat == SAMPLEFORMAT_UINT && td->td_bitspersample == 8)
  106. {
  107. return 1;
  108. }
  109. if (td->td_sampleformat == SAMPLEFORMAT_INT && td->td_bitspersample == 16)
  110. {
  111. return 2;
  112. }
  113. if (td->td_sampleformat == SAMPLEFORMAT_UINT && td->td_bitspersample == 16)
  114. {
  115. return 3;
  116. }
  117. if (td->td_sampleformat == SAMPLEFORMAT_INT && td->td_bitspersample == 32)
  118. {
  119. return 4;
  120. }
  121. if (td->td_sampleformat == SAMPLEFORMAT_UINT && td->td_bitspersample == 32)
  122. {
  123. return 5;
  124. }
  125. if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  126. td->td_bitspersample == 32)
  127. {
  128. return 6;
  129. }
  130. if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  131. td->td_bitspersample == 64)
  132. {
  133. return 7;
  134. }
  135. TIFFErrorExtR(
  136. tif, module,
  137. "Unsupported combination of SampleFormat and td_bitspersample");
  138. return -1;
  139. }
  140. static int SetupUncompressedBuffer(TIFF *tif, LERCState *sp, const char *module)
  141. {
  142. TIFFDirectory *td = &tif->tif_dir;
  143. uint64_t new_size_64;
  144. uint64_t new_alloc_64;
  145. unsigned int new_size;
  146. unsigned int new_alloc;
  147. sp->uncompressed_offset = 0;
  148. if (isTiled(tif))
  149. {
  150. sp->segment_width = td->td_tilewidth;
  151. sp->segment_height = td->td_tilelength;
  152. }
  153. else
  154. {
  155. sp->segment_width = td->td_imagewidth;
  156. sp->segment_height = td->td_imagelength - tif->tif_row;
  157. if (sp->segment_height > td->td_rowsperstrip)
  158. sp->segment_height = td->td_rowsperstrip;
  159. }
  160. new_size_64 = (uint64_t)sp->segment_width * sp->segment_height *
  161. (td->td_bitspersample / 8);
  162. if (td->td_planarconfig == PLANARCONFIG_CONTIG)
  163. {
  164. new_size_64 *= td->td_samplesperpixel;
  165. }
  166. new_size = (unsigned int)new_size_64;
  167. sp->uncompressed_size = new_size;
  168. /* add some margin as we are going to use it also to store deflate/zstd
  169. * compressed data */
  170. new_alloc_64 = 100 + new_size_64 + new_size_64 / 3;
  171. #ifdef ZSTD_SUPPORT
  172. {
  173. size_t zstd_max = ZSTD_compressBound((size_t)new_size_64);
  174. if (new_alloc_64 < zstd_max)
  175. {
  176. new_alloc_64 = zstd_max;
  177. }
  178. }
  179. #endif
  180. new_alloc = (unsigned int)new_alloc_64;
  181. if (new_alloc != new_alloc_64)
  182. {
  183. TIFFErrorExtR(tif, module, "Too large uncompressed strip/tile");
  184. _TIFFfreeExt(tif, sp->uncompressed_buffer);
  185. sp->uncompressed_buffer = 0;
  186. sp->uncompressed_alloc = 0;
  187. return 0;
  188. }
  189. if (sp->uncompressed_alloc < new_alloc)
  190. {
  191. _TIFFfreeExt(tif, sp->uncompressed_buffer);
  192. sp->uncompressed_buffer = _TIFFmallocExt(tif, new_alloc);
  193. if (!sp->uncompressed_buffer)
  194. {
  195. TIFFErrorExtR(tif, module, "Cannot allocate buffer");
  196. _TIFFfreeExt(tif, sp->uncompressed_buffer);
  197. sp->uncompressed_buffer = 0;
  198. sp->uncompressed_alloc = 0;
  199. return 0;
  200. }
  201. sp->uncompressed_alloc = new_alloc;
  202. }
  203. if ((td->td_planarconfig == PLANARCONFIG_CONTIG &&
  204. td->td_extrasamples > 0 &&
  205. td->td_sampleinfo[td->td_extrasamples - 1] == EXTRASAMPLE_UNASSALPHA &&
  206. GetLercDataType(tif) == 1) ||
  207. (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  208. (td->td_planarconfig == PLANARCONFIG_SEPARATE ||
  209. td->td_samplesperpixel == 1) &&
  210. (td->td_bitspersample == 32 || td->td_bitspersample == 64)))
  211. {
  212. unsigned int mask_size = sp->segment_width * sp->segment_height;
  213. if (sp->mask_size < mask_size)
  214. {
  215. void *mask_buffer =
  216. _TIFFreallocExt(tif, sp->mask_buffer, mask_size);
  217. if (mask_buffer == NULL)
  218. {
  219. TIFFErrorExtR(tif, module, "Cannot allocate buffer");
  220. sp->mask_size = 0;
  221. _TIFFfreeExt(tif, sp->uncompressed_buffer);
  222. sp->uncompressed_buffer = 0;
  223. sp->uncompressed_alloc = 0;
  224. return 0;
  225. }
  226. sp->mask_buffer = (uint8_t *)mask_buffer;
  227. sp->mask_size = mask_size;
  228. }
  229. }
  230. return 1;
  231. }
  232. /*
  233. * Setup state for decoding a strip.
  234. */
  235. static int LERCPreDecode(TIFF *tif, uint16_t s)
  236. {
  237. static const char module[] = "LERCPreDecode";
  238. lerc_status lerc_ret;
  239. TIFFDirectory *td = &tif->tif_dir;
  240. LERCState *sp = DecoderState(tif);
  241. int lerc_data_type;
  242. unsigned int infoArray[8];
  243. unsigned nomask_bands = td->td_samplesperpixel;
  244. int ndims;
  245. int use_mask = 0;
  246. uint8_t *lerc_data = tif->tif_rawcp;
  247. unsigned int lerc_data_size = (unsigned int)tif->tif_rawcc;
  248. (void)s;
  249. assert(sp != NULL);
  250. if (sp->state != LSTATE_INIT_DECODE)
  251. tif->tif_setupdecode(tif);
  252. lerc_data_type = GetLercDataType(tif);
  253. if (lerc_data_type < 0)
  254. return 0;
  255. if (!SetupUncompressedBuffer(tif, sp, module))
  256. return 0;
  257. if (sp->additional_compression != LERC_ADD_COMPRESSION_NONE)
  258. {
  259. if (sp->compressed_size < sp->uncompressed_alloc)
  260. {
  261. _TIFFfreeExt(tif, sp->compressed_buffer);
  262. sp->compressed_buffer = _TIFFmallocExt(tif, sp->uncompressed_alloc);
  263. if (!sp->compressed_buffer)
  264. {
  265. sp->compressed_size = 0;
  266. return 0;
  267. }
  268. sp->compressed_size = sp->uncompressed_alloc;
  269. }
  270. }
  271. if (sp->additional_compression == LERC_ADD_COMPRESSION_DEFLATE)
  272. {
  273. #if LIBDEFLATE_SUPPORT
  274. enum libdeflate_result res;
  275. size_t lerc_data_sizet = 0;
  276. if (sp->libdeflate_dec == NULL)
  277. {
  278. sp->libdeflate_dec = libdeflate_alloc_decompressor();
  279. if (sp->libdeflate_dec == NULL)
  280. {
  281. TIFFErrorExtR(tif, module, "Cannot allocate decompressor");
  282. return 0;
  283. }
  284. }
  285. res = libdeflate_zlib_decompress(
  286. sp->libdeflate_dec, tif->tif_rawcp, (size_t)tif->tif_rawcc,
  287. sp->compressed_buffer, sp->compressed_size, &lerc_data_sizet);
  288. if (res != LIBDEFLATE_SUCCESS)
  289. {
  290. TIFFErrorExtR(tif, module, "Decoding error at scanline %lu",
  291. (unsigned long)tif->tif_row);
  292. return 0;
  293. }
  294. assert(lerc_data_sizet == (unsigned int)lerc_data_sizet);
  295. lerc_data = sp->compressed_buffer;
  296. lerc_data_size = (unsigned int)lerc_data_sizet;
  297. #else
  298. z_stream strm;
  299. int zlib_ret;
  300. memset(&strm, 0, sizeof(strm));
  301. strm.zalloc = NULL;
  302. strm.zfree = NULL;
  303. strm.opaque = NULL;
  304. zlib_ret = inflateInit(&strm);
  305. if (zlib_ret != Z_OK)
  306. {
  307. TIFFErrorExtR(tif, module, "inflateInit() failed");
  308. inflateEnd(&strm);
  309. return 0;
  310. }
  311. strm.avail_in = (uInt)tif->tif_rawcc;
  312. strm.next_in = tif->tif_rawcp;
  313. strm.avail_out = sp->compressed_size;
  314. strm.next_out = sp->compressed_buffer;
  315. zlib_ret = inflate(&strm, Z_FINISH);
  316. if (zlib_ret != Z_STREAM_END && zlib_ret != Z_OK)
  317. {
  318. TIFFErrorExtR(tif, module, "inflate() failed");
  319. inflateEnd(&strm);
  320. return 0;
  321. }
  322. lerc_data = sp->compressed_buffer;
  323. lerc_data_size = sp->compressed_size - strm.avail_out;
  324. inflateEnd(&strm);
  325. #endif
  326. }
  327. else if (sp->additional_compression == LERC_ADD_COMPRESSION_ZSTD)
  328. {
  329. #ifdef ZSTD_SUPPORT
  330. size_t zstd_ret;
  331. zstd_ret = ZSTD_decompress(sp->compressed_buffer, sp->compressed_size,
  332. tif->tif_rawcp, tif->tif_rawcc);
  333. if (ZSTD_isError(zstd_ret))
  334. {
  335. TIFFErrorExtR(tif, module, "Error in ZSTD_decompress(): %s",
  336. ZSTD_getErrorName(zstd_ret));
  337. return 0;
  338. }
  339. lerc_data = sp->compressed_buffer;
  340. lerc_data_size = (unsigned int)zstd_ret;
  341. #else
  342. TIFFErrorExtR(tif, module, "ZSTD support missing");
  343. return 0;
  344. #endif
  345. }
  346. else if (sp->additional_compression != LERC_ADD_COMPRESSION_NONE)
  347. {
  348. TIFFErrorExtR(tif, module, "Unhandled additional compression");
  349. return 0;
  350. }
  351. lerc_ret =
  352. lerc_getBlobInfo(lerc_data, lerc_data_size, infoArray, NULL, 8, 0);
  353. if (lerc_ret != 0)
  354. {
  355. TIFFErrorExtR(tif, module, "lerc_getBlobInfo() failed");
  356. return 0;
  357. }
  358. /* If the configuration is compatible of a LERC mask, and that the */
  359. /* LERC info has dim == samplesperpixel - 1, then there is a LERC */
  360. /* mask. */
  361. if (td->td_planarconfig == PLANARCONFIG_CONTIG && td->td_extrasamples > 0 &&
  362. td->td_sampleinfo[td->td_extrasamples - 1] == EXTRASAMPLE_UNASSALPHA &&
  363. GetLercDataType(tif) == 1 &&
  364. infoArray[2] == td->td_samplesperpixel - 1U)
  365. {
  366. use_mask = 1;
  367. nomask_bands--;
  368. }
  369. else if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  370. (td->td_planarconfig == PLANARCONFIG_SEPARATE ||
  371. td->td_samplesperpixel == 1) &&
  372. (td->td_bitspersample == 32 || td->td_bitspersample == 64))
  373. {
  374. use_mask = 1;
  375. }
  376. ndims = td->td_planarconfig == PLANARCONFIG_CONTIG ? nomask_bands : 1;
  377. /* Info returned in infoArray is { version, dataType, nDim, nCols,
  378. nRows, nBands, nValidPixels, blobSize } */
  379. if (infoArray[0] != (unsigned)sp->lerc_version)
  380. {
  381. TIFFWarningExtR(tif, module,
  382. "Unexpected version number: %d. Expected: %d",
  383. infoArray[0], sp->lerc_version);
  384. }
  385. if (infoArray[1] != (unsigned)lerc_data_type)
  386. {
  387. TIFFErrorExtR(tif, module, "Unexpected dataType: %d. Expected: %d",
  388. infoArray[1], lerc_data_type);
  389. return 0;
  390. }
  391. if (infoArray[2] != (unsigned)ndims)
  392. {
  393. TIFFErrorExtR(tif, module, "Unexpected nDim: %d. Expected: %d",
  394. infoArray[2], ndims);
  395. return 0;
  396. }
  397. if (infoArray[3] != sp->segment_width)
  398. {
  399. TIFFErrorExtR(tif, module, "Unexpected nCols: %d. Expected: %du",
  400. infoArray[3], sp->segment_width);
  401. return 0;
  402. }
  403. if (infoArray[4] != sp->segment_height)
  404. {
  405. TIFFErrorExtR(tif, module, "Unexpected nRows: %d. Expected: %u",
  406. infoArray[4], sp->segment_height);
  407. return 0;
  408. }
  409. if (infoArray[5] != 1)
  410. {
  411. TIFFErrorExtR(tif, module, "Unexpected nBands: %d. Expected: %d",
  412. infoArray[5], 1);
  413. return 0;
  414. }
  415. if (infoArray[7] != lerc_data_size)
  416. {
  417. TIFFErrorExtR(tif, module, "Unexpected blobSize: %d. Expected: %u",
  418. infoArray[7], lerc_data_size);
  419. return 0;
  420. }
  421. lerc_ret = lerc_decode(lerc_data, lerc_data_size,
  422. #if LERC_AT_LEAST_VERSION(3, 0, 0)
  423. use_mask ? 1 : 0,
  424. #endif
  425. use_mask ? sp->mask_buffer : NULL, ndims,
  426. sp->segment_width, sp->segment_height, 1,
  427. lerc_data_type, sp->uncompressed_buffer);
  428. if (lerc_ret != 0)
  429. {
  430. TIFFErrorExtR(tif, module, "lerc_decode() failed");
  431. return 0;
  432. }
  433. /* Interleave alpha mask with other samples. */
  434. if (use_mask && GetLercDataType(tif) == 1)
  435. {
  436. unsigned src_stride =
  437. (td->td_samplesperpixel - 1) * (td->td_bitspersample / 8);
  438. unsigned dst_stride =
  439. td->td_samplesperpixel * (td->td_bitspersample / 8);
  440. unsigned i = sp->segment_width * sp->segment_height;
  441. /* Operate from end to begin to be able to move in place */
  442. while (i > 0 && i > nomask_bands)
  443. {
  444. i--;
  445. sp->uncompressed_buffer[i * dst_stride + td->td_samplesperpixel -
  446. 1] = 255 * sp->mask_buffer[i];
  447. memcpy(sp->uncompressed_buffer + i * dst_stride,
  448. sp->uncompressed_buffer + i * src_stride, src_stride);
  449. }
  450. /* First pixels must use memmove due to overlapping areas */
  451. while (i > 0)
  452. {
  453. i--;
  454. sp->uncompressed_buffer[i * dst_stride + td->td_samplesperpixel -
  455. 1] = 255 * sp->mask_buffer[i];
  456. memmove(sp->uncompressed_buffer + i * dst_stride,
  457. sp->uncompressed_buffer + i * src_stride, src_stride);
  458. }
  459. }
  460. else if (use_mask && td->td_sampleformat == SAMPLEFORMAT_IEEEFP)
  461. {
  462. const unsigned nb_pixels = sp->segment_width * sp->segment_height;
  463. unsigned i;
  464. #if WORDS_BIGENDIAN
  465. const unsigned char nan_bytes[] = {0x7f, 0xc0, 0, 0};
  466. #else
  467. const unsigned char nan_bytes[] = {0, 0, 0xc0, 0x7f};
  468. #endif
  469. float nan_float32;
  470. memcpy(&nan_float32, nan_bytes, 4);
  471. if (td->td_bitspersample == 32)
  472. {
  473. for (i = 0; i < nb_pixels; i++)
  474. {
  475. if (sp->mask_buffer[i] == 0)
  476. ((float *)sp->uncompressed_buffer)[i] = nan_float32;
  477. }
  478. }
  479. else
  480. {
  481. const double nan_float64 = nan_float32;
  482. for (i = 0; i < nb_pixels; i++)
  483. {
  484. if (sp->mask_buffer[i] == 0)
  485. ((double *)sp->uncompressed_buffer)[i] = nan_float64;
  486. }
  487. }
  488. }
  489. return 1;
  490. }
  491. /*
  492. * Decode a strip, tile or scanline.
  493. */
  494. static int LERCDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
  495. {
  496. static const char module[] = "LERCDecode";
  497. LERCState *sp = DecoderState(tif);
  498. (void)s;
  499. assert(sp != NULL);
  500. assert(sp->state == LSTATE_INIT_DECODE);
  501. if (sp->uncompressed_buffer == 0)
  502. {
  503. TIFFErrorExtR(tif, module, "Uncompressed buffer not allocated");
  504. return 0;
  505. }
  506. if ((uint64_t)sp->uncompressed_offset + (uint64_t)occ >
  507. sp->uncompressed_size)
  508. {
  509. TIFFErrorExtR(tif, module, "Too many bytes read");
  510. return 0;
  511. }
  512. memcpy(op, sp->uncompressed_buffer + sp->uncompressed_offset, occ);
  513. sp->uncompressed_offset += (unsigned)occ;
  514. return 1;
  515. }
  516. static int LERCSetupEncode(TIFF *tif)
  517. {
  518. LERCState *sp = EncoderState(tif);
  519. assert(sp != NULL);
  520. if (sp->state & LSTATE_INIT_DECODE)
  521. {
  522. sp->state = 0;
  523. }
  524. sp->state |= LSTATE_INIT_ENCODE;
  525. return 1;
  526. }
  527. /*
  528. * Reset encoding state at the start of a strip.
  529. */
  530. static int LERCPreEncode(TIFF *tif, uint16_t s)
  531. {
  532. static const char module[] = "LERCPreEncode";
  533. LERCState *sp = EncoderState(tif);
  534. int lerc_data_type;
  535. (void)s;
  536. assert(sp != NULL);
  537. if (sp->state != LSTATE_INIT_ENCODE)
  538. tif->tif_setupencode(tif);
  539. lerc_data_type = GetLercDataType(tif);
  540. if (lerc_data_type < 0)
  541. return 0;
  542. if (!SetupUncompressedBuffer(tif, sp, module))
  543. return 0;
  544. return 1;
  545. }
  546. /*
  547. * Encode a chunk of pixels.
  548. */
  549. static int LERCEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
  550. {
  551. static const char module[] = "LERCEncode";
  552. LERCState *sp = EncoderState(tif);
  553. (void)s;
  554. assert(sp != NULL);
  555. assert(sp->state == LSTATE_INIT_ENCODE);
  556. if ((uint64_t)sp->uncompressed_offset + (uint64_t)cc >
  557. sp->uncompressed_size)
  558. {
  559. TIFFErrorExtR(tif, module, "Too many bytes written");
  560. return 0;
  561. }
  562. memcpy(sp->uncompressed_buffer + sp->uncompressed_offset, bp, cc);
  563. sp->uncompressed_offset += (unsigned)cc;
  564. return 1;
  565. }
  566. /*
  567. * Finish off an encoded strip by flushing it.
  568. */
  569. static int LERCPostEncode(TIFF *tif)
  570. {
  571. lerc_status lerc_ret;
  572. static const char module[] = "LERCPostEncode";
  573. LERCState *sp = EncoderState(tif);
  574. unsigned int numBytes = 0;
  575. unsigned int numBytesWritten = 0;
  576. TIFFDirectory *td = &tif->tif_dir;
  577. int use_mask = 0;
  578. unsigned dst_nbands = td->td_samplesperpixel;
  579. if (sp->uncompressed_offset != sp->uncompressed_size)
  580. {
  581. TIFFErrorExtR(tif, module, "Unexpected number of bytes in the buffer");
  582. return 0;
  583. }
  584. /* Extract alpha mask (if containing only 0 and 255 values, */
  585. /* and compact array of regular bands */
  586. if (td->td_planarconfig == PLANARCONFIG_CONTIG && td->td_extrasamples > 0 &&
  587. td->td_sampleinfo[td->td_extrasamples - 1] == EXTRASAMPLE_UNASSALPHA &&
  588. GetLercDataType(tif) == 1)
  589. {
  590. const unsigned dst_stride =
  591. (td->td_samplesperpixel - 1) * (td->td_bitspersample / 8);
  592. const unsigned src_stride =
  593. td->td_samplesperpixel * (td->td_bitspersample / 8);
  594. unsigned i = 0;
  595. const unsigned nb_pixels = sp->segment_width * sp->segment_height;
  596. use_mask = 1;
  597. for (i = 0; i < nb_pixels; i++)
  598. {
  599. int v = sp->uncompressed_buffer[i * src_stride +
  600. td->td_samplesperpixel - 1];
  601. if (v != 0 && v != 255)
  602. {
  603. use_mask = 0;
  604. break;
  605. }
  606. }
  607. if (use_mask)
  608. {
  609. dst_nbands--;
  610. /* First pixels must use memmove due to overlapping areas */
  611. for (i = 0; i < dst_nbands && i < nb_pixels; i++)
  612. {
  613. memmove(sp->uncompressed_buffer + i * dst_stride,
  614. sp->uncompressed_buffer + i * src_stride, dst_stride);
  615. sp->mask_buffer[i] =
  616. sp->uncompressed_buffer[i * src_stride +
  617. td->td_samplesperpixel - 1];
  618. }
  619. for (; i < nb_pixels; i++)
  620. {
  621. memcpy(sp->uncompressed_buffer + i * dst_stride,
  622. sp->uncompressed_buffer + i * src_stride, dst_stride);
  623. sp->mask_buffer[i] =
  624. sp->uncompressed_buffer[i * src_stride +
  625. td->td_samplesperpixel - 1];
  626. }
  627. }
  628. }
  629. else if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  630. (td->td_planarconfig == PLANARCONFIG_SEPARATE ||
  631. dst_nbands == 1) &&
  632. (td->td_bitspersample == 32 || td->td_bitspersample == 64))
  633. {
  634. /* Check for NaN values */
  635. unsigned i;
  636. const unsigned nb_pixels = sp->segment_width * sp->segment_height;
  637. if (td->td_bitspersample == 32)
  638. {
  639. for (i = 0; i < nb_pixels; i++)
  640. {
  641. const float val = ((float *)sp->uncompressed_buffer)[i];
  642. if (val != val)
  643. {
  644. use_mask = 1;
  645. break;
  646. }
  647. }
  648. }
  649. else
  650. {
  651. for (i = 0; i < nb_pixels; i++)
  652. {
  653. const double val = ((double *)sp->uncompressed_buffer)[i];
  654. if (val != val)
  655. {
  656. use_mask = 1;
  657. break;
  658. }
  659. }
  660. }
  661. if (use_mask)
  662. {
  663. if (td->td_bitspersample == 32)
  664. {
  665. for (i = 0; i < nb_pixels; i++)
  666. {
  667. const float val = ((float *)sp->uncompressed_buffer)[i];
  668. sp->mask_buffer[i] = (val == val) ? 255 : 0;
  669. }
  670. }
  671. else
  672. {
  673. for (i = 0; i < nb_pixels; i++)
  674. {
  675. const double val = ((double *)sp->uncompressed_buffer)[i];
  676. sp->mask_buffer[i] = (val == val) ? 255 : 0;
  677. }
  678. }
  679. }
  680. }
  681. #if 0
  682. lerc_ret = lerc_computeCompressedSize(
  683. sp->uncompressed_buffer,
  684. sp->lerc_version,
  685. GetLercDataType(tif),
  686. td->td_planarconfig == PLANARCONFIG_CONTIG ?
  687. dst_nbands : 1,
  688. sp->segment_width,
  689. sp->segment_height,
  690. 1,
  691. use_mask ? sp->mask_buffer : NULL,
  692. sp->maxzerror,
  693. &numBytes);
  694. if( lerc_ret != 0 )
  695. {
  696. TIFFErrorExtR(tif, module,
  697. "lerc_computeCompressedSize() failed");
  698. return 0;
  699. }
  700. #else
  701. numBytes = sp->uncompressed_alloc;
  702. #endif
  703. if (sp->compressed_size < numBytes)
  704. {
  705. _TIFFfreeExt(tif, sp->compressed_buffer);
  706. sp->compressed_buffer = _TIFFmallocExt(tif, numBytes);
  707. if (!sp->compressed_buffer)
  708. {
  709. sp->compressed_size = 0;
  710. return 0;
  711. }
  712. sp->compressed_size = numBytes;
  713. }
  714. lerc_ret = lerc_encodeForVersion(
  715. sp->uncompressed_buffer, sp->lerc_version, GetLercDataType(tif),
  716. td->td_planarconfig == PLANARCONFIG_CONTIG ? dst_nbands : 1,
  717. sp->segment_width, sp->segment_height, 1,
  718. #if LERC_AT_LEAST_VERSION(3, 0, 0)
  719. use_mask ? 1 : 0,
  720. #endif
  721. use_mask ? sp->mask_buffer : NULL, sp->maxzerror, sp->compressed_buffer,
  722. sp->compressed_size, &numBytesWritten);
  723. if (lerc_ret != 0)
  724. {
  725. TIFFErrorExtR(tif, module, "lerc_encode() failed");
  726. return 0;
  727. }
  728. assert(numBytesWritten < numBytes);
  729. if (sp->additional_compression == LERC_ADD_COMPRESSION_DEFLATE)
  730. {
  731. #if LIBDEFLATE_SUPPORT
  732. if (sp->libdeflate_enc == NULL)
  733. {
  734. /* To get results as good as zlib, we ask for an extra */
  735. /* level of compression */
  736. sp->libdeflate_enc = libdeflate_alloc_compressor(
  737. sp->zipquality == Z_DEFAULT_COMPRESSION ? 7
  738. : sp->zipquality >= 6 && sp->zipquality <= 9
  739. ? sp->zipquality + 1
  740. : sp->zipquality);
  741. if (sp->libdeflate_enc == NULL)
  742. {
  743. TIFFErrorExtR(tif, module, "Cannot allocate compressor");
  744. return 0;
  745. }
  746. }
  747. /* Should not happen normally */
  748. if (libdeflate_zlib_compress_bound(
  749. sp->libdeflate_enc, numBytesWritten) > sp->uncompressed_alloc)
  750. {
  751. TIFFErrorExtR(tif, module,
  752. "Output buffer for libdeflate too small");
  753. return 0;
  754. }
  755. tif->tif_rawcc = libdeflate_zlib_compress(
  756. sp->libdeflate_enc, sp->compressed_buffer, numBytesWritten,
  757. sp->uncompressed_buffer, sp->uncompressed_alloc);
  758. if (tif->tif_rawcc == 0)
  759. {
  760. TIFFErrorExtR(tif, module, "Encoder error at scanline %lu",
  761. (unsigned long)tif->tif_row);
  762. return 0;
  763. }
  764. #else
  765. z_stream strm;
  766. int zlib_ret;
  767. int cappedQuality = sp->zipquality;
  768. if (cappedQuality > Z_BEST_COMPRESSION)
  769. cappedQuality = Z_BEST_COMPRESSION;
  770. memset(&strm, 0, sizeof(strm));
  771. strm.zalloc = NULL;
  772. strm.zfree = NULL;
  773. strm.opaque = NULL;
  774. zlib_ret = deflateInit(&strm, cappedQuality);
  775. if (zlib_ret != Z_OK)
  776. {
  777. TIFFErrorExtR(tif, module, "deflateInit() failed");
  778. return 0;
  779. }
  780. strm.avail_in = numBytesWritten;
  781. strm.next_in = sp->compressed_buffer;
  782. strm.avail_out = sp->uncompressed_alloc;
  783. strm.next_out = sp->uncompressed_buffer;
  784. zlib_ret = deflate(&strm, Z_FINISH);
  785. if (zlib_ret == Z_STREAM_END)
  786. {
  787. tif->tif_rawcc = sp->uncompressed_alloc - strm.avail_out;
  788. }
  789. deflateEnd(&strm);
  790. if (zlib_ret != Z_STREAM_END)
  791. {
  792. TIFFErrorExtR(tif, module, "deflate() failed");
  793. return 0;
  794. }
  795. #endif
  796. {
  797. int ret;
  798. uint8_t *tif_rawdata_backup = tif->tif_rawdata;
  799. tif->tif_rawdata = sp->uncompressed_buffer;
  800. ret = TIFFFlushData1(tif);
  801. tif->tif_rawdata = tif_rawdata_backup;
  802. if (!ret)
  803. {
  804. return 0;
  805. }
  806. }
  807. }
  808. else if (sp->additional_compression == LERC_ADD_COMPRESSION_ZSTD)
  809. {
  810. #ifdef ZSTD_SUPPORT
  811. size_t zstd_ret = ZSTD_compress(
  812. sp->uncompressed_buffer, sp->uncompressed_alloc,
  813. sp->compressed_buffer, numBytesWritten, sp->zstd_compress_level);
  814. if (ZSTD_isError(zstd_ret))
  815. {
  816. TIFFErrorExtR(tif, module, "Error in ZSTD_compress(): %s",
  817. ZSTD_getErrorName(zstd_ret));
  818. return 0;
  819. }
  820. {
  821. int ret;
  822. uint8_t *tif_rawdata_backup = tif->tif_rawdata;
  823. tif->tif_rawdata = sp->uncompressed_buffer;
  824. tif->tif_rawcc = zstd_ret;
  825. ret = TIFFFlushData1(tif);
  826. tif->tif_rawdata = tif_rawdata_backup;
  827. if (!ret)
  828. {
  829. return 0;
  830. }
  831. }
  832. #else
  833. TIFFErrorExtR(tif, module, "ZSTD support missing");
  834. return 0;
  835. #endif
  836. }
  837. else if (sp->additional_compression != LERC_ADD_COMPRESSION_NONE)
  838. {
  839. TIFFErrorExtR(tif, module, "Unhandled additional compression");
  840. return 0;
  841. }
  842. else
  843. {
  844. int ret;
  845. uint8_t *tif_rawdata_backup = tif->tif_rawdata;
  846. tif->tif_rawdata = sp->compressed_buffer;
  847. tif->tif_rawcc = numBytesWritten;
  848. ret = TIFFFlushData1(tif);
  849. tif->tif_rawdata = tif_rawdata_backup;
  850. if (!ret)
  851. return 0;
  852. }
  853. return 1;
  854. }
  855. static void LERCCleanup(TIFF *tif)
  856. {
  857. LERCState *sp = LState(tif);
  858. assert(sp != 0);
  859. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  860. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  861. _TIFFfreeExt(tif, sp->uncompressed_buffer);
  862. _TIFFfreeExt(tif, sp->compressed_buffer);
  863. _TIFFfreeExt(tif, sp->mask_buffer);
  864. #if LIBDEFLATE_SUPPORT
  865. if (sp->libdeflate_dec)
  866. libdeflate_free_decompressor(sp->libdeflate_dec);
  867. if (sp->libdeflate_enc)
  868. libdeflate_free_compressor(sp->libdeflate_enc);
  869. #endif
  870. _TIFFfreeExt(tif, sp);
  871. tif->tif_data = NULL;
  872. _TIFFSetDefaultCompressionState(tif);
  873. }
  874. static const TIFFField LERCFields[] = {
  875. {TIFFTAG_LERC_PARAMETERS, TIFF_VARIABLE2, TIFF_VARIABLE2, TIFF_LONG, 0,
  876. TIFF_SETGET_C32_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, FALSE, TRUE,
  877. "LercParameters", NULL},
  878. {TIFFTAG_LERC_MAXZERROR, 0, 0, TIFF_ANY, 0, TIFF_SETGET_DOUBLE,
  879. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "LercMaximumError",
  880. NULL},
  881. {TIFFTAG_LERC_VERSION, 0, 0, TIFF_ANY, 0, TIFF_SETGET_UINT32,
  882. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "LercVersion", NULL},
  883. {TIFFTAG_LERC_ADD_COMPRESSION, 0, 0, TIFF_ANY, 0, TIFF_SETGET_UINT32,
  884. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE,
  885. "LercAdditionalCompression", NULL},
  886. {TIFFTAG_ZSTD_LEVEL, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
  887. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE,
  888. "ZSTD zstd_compress_level", NULL},
  889. {TIFFTAG_ZIPQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
  890. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "", NULL},
  891. };
  892. static int LERCVSetFieldBase(TIFF *tif, uint32_t tag, ...)
  893. {
  894. LERCState *sp = LState(tif);
  895. int ret;
  896. va_list ap;
  897. va_start(ap, tag);
  898. ret = (*sp->vsetparent)(tif, tag, ap);
  899. va_end(ap);
  900. return ret;
  901. }
  902. static int LERCVSetField(TIFF *tif, uint32_t tag, va_list ap)
  903. {
  904. static const char module[] = "LERCVSetField";
  905. LERCState *sp = LState(tif);
  906. switch (tag)
  907. {
  908. case TIFFTAG_LERC_PARAMETERS:
  909. {
  910. uint32_t count = va_arg(ap, int);
  911. int *params = va_arg(ap, int *);
  912. if (count < 2)
  913. {
  914. TIFFErrorExtR(tif, module,
  915. "Invalid count for LercParameters: %u", count);
  916. return 0;
  917. }
  918. sp->lerc_version = params[0];
  919. sp->additional_compression = params[1];
  920. return LERCVSetFieldBase(tif, TIFFTAG_LERC_PARAMETERS, count,
  921. params);
  922. }
  923. case TIFFTAG_LERC_MAXZERROR:
  924. sp->maxzerror = va_arg(ap, double);
  925. return 1;
  926. case TIFFTAG_LERC_VERSION:
  927. {
  928. int params[2] = {0, 0};
  929. int version = va_arg(ap, int);
  930. if (version != LERC_VERSION_2_4)
  931. {
  932. TIFFErrorExtR(tif, module, "Invalid value for LercVersion: %d",
  933. version);
  934. return 0;
  935. }
  936. sp->lerc_version = version;
  937. params[0] = sp->lerc_version;
  938. params[1] = sp->additional_compression;
  939. return LERCVSetFieldBase(tif, TIFFTAG_LERC_PARAMETERS, 2, params);
  940. }
  941. case TIFFTAG_LERC_ADD_COMPRESSION:
  942. {
  943. int params[2] = {0, 0};
  944. int additional_compression = va_arg(ap, int);
  945. #ifndef ZSTD_SUPPORT
  946. if (additional_compression == LERC_ADD_COMPRESSION_ZSTD)
  947. {
  948. TIFFErrorExtR(tif, module,
  949. "LERC_ZSTD requested, but ZSTD not available");
  950. return 0;
  951. }
  952. #endif
  953. if (additional_compression != LERC_ADD_COMPRESSION_NONE &&
  954. additional_compression != LERC_ADD_COMPRESSION_DEFLATE &&
  955. additional_compression != LERC_ADD_COMPRESSION_ZSTD)
  956. {
  957. TIFFErrorExtR(tif, module,
  958. "Invalid value for LercAdditionalCompression: %d",
  959. additional_compression);
  960. return 0;
  961. }
  962. sp->additional_compression = additional_compression;
  963. params[0] = sp->lerc_version;
  964. params[1] = sp->additional_compression;
  965. return LERCVSetFieldBase(tif, TIFFTAG_LERC_PARAMETERS, 2, params);
  966. }
  967. #ifdef ZSTD_SUPPORT
  968. case TIFFTAG_ZSTD_LEVEL:
  969. {
  970. sp->zstd_compress_level = (int)va_arg(ap, int);
  971. if (sp->zstd_compress_level <= 0 ||
  972. sp->zstd_compress_level > ZSTD_maxCLevel())
  973. {
  974. TIFFWarningExtR(tif, module,
  975. "ZSTD_LEVEL should be between 1 and %d",
  976. ZSTD_maxCLevel());
  977. }
  978. return 1;
  979. }
  980. #endif
  981. case TIFFTAG_ZIPQUALITY:
  982. {
  983. sp->zipquality = (int)va_arg(ap, int);
  984. if (sp->zipquality < Z_DEFAULT_COMPRESSION ||
  985. sp->zipquality > LIBDEFLATE_MAX_COMPRESSION_LEVEL)
  986. {
  987. TIFFErrorExtR(
  988. tif, module,
  989. "Invalid ZipQuality value. Should be in [-1,%d] range",
  990. LIBDEFLATE_MAX_COMPRESSION_LEVEL);
  991. return 0;
  992. }
  993. #if LIBDEFLATE_SUPPORT
  994. if (sp->libdeflate_enc)
  995. {
  996. libdeflate_free_compressor(sp->libdeflate_enc);
  997. sp->libdeflate_enc = NULL;
  998. }
  999. #endif
  1000. return (1);
  1001. }
  1002. default:
  1003. return (*sp->vsetparent)(tif, tag, ap);
  1004. }
  1005. /*NOTREACHED*/
  1006. }
  1007. static int LERCVGetField(TIFF *tif, uint32_t tag, va_list ap)
  1008. {
  1009. LERCState *sp = LState(tif);
  1010. switch (tag)
  1011. {
  1012. case TIFFTAG_LERC_MAXZERROR:
  1013. *va_arg(ap, double *) = sp->maxzerror;
  1014. break;
  1015. case TIFFTAG_LERC_VERSION:
  1016. *va_arg(ap, int *) = sp->lerc_version;
  1017. break;
  1018. case TIFFTAG_LERC_ADD_COMPRESSION:
  1019. *va_arg(ap, int *) = sp->additional_compression;
  1020. break;
  1021. case TIFFTAG_ZSTD_LEVEL:
  1022. *va_arg(ap, int *) = sp->zstd_compress_level;
  1023. break;
  1024. case TIFFTAG_ZIPQUALITY:
  1025. *va_arg(ap, int *) = sp->zipquality;
  1026. break;
  1027. default:
  1028. return (*sp->vgetparent)(tif, tag, ap);
  1029. }
  1030. return 1;
  1031. }
  1032. int TIFFInitLERC(TIFF *tif, int scheme)
  1033. {
  1034. static const char module[] = "TIFFInitLERC";
  1035. LERCState *sp;
  1036. (void)scheme;
  1037. assert(scheme == COMPRESSION_LERC);
  1038. /*
  1039. * Merge codec-specific tag information.
  1040. */
  1041. if (!_TIFFMergeFields(tif, LERCFields, TIFFArrayCount(LERCFields)))
  1042. {
  1043. TIFFErrorExtR(tif, module, "Merging LERC codec-specific tags failed");
  1044. return 0;
  1045. }
  1046. /*
  1047. * Allocate state block so tag methods have storage to record values.
  1048. */
  1049. tif->tif_data = (uint8_t *)_TIFFcallocExt(tif, 1, sizeof(LERCState));
  1050. if (tif->tif_data == NULL)
  1051. goto bad;
  1052. sp = LState(tif);
  1053. /*
  1054. * Override parent get/set field methods.
  1055. */
  1056. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  1057. tif->tif_tagmethods.vgetfield = LERCVGetField; /* hook for codec tags */
  1058. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  1059. tif->tif_tagmethods.vsetfield = LERCVSetField; /* hook for codec tags */
  1060. /*
  1061. * Install codec methods.
  1062. */
  1063. tif->tif_fixuptags = LERCFixupTags;
  1064. tif->tif_setupdecode = LERCSetupDecode;
  1065. tif->tif_predecode = LERCPreDecode;
  1066. tif->tif_decoderow = LERCDecode;
  1067. tif->tif_decodestrip = LERCDecode;
  1068. tif->tif_decodetile = LERCDecode;
  1069. tif->tif_setupencode = LERCSetupEncode;
  1070. tif->tif_preencode = LERCPreEncode;
  1071. tif->tif_postencode = LERCPostEncode;
  1072. tif->tif_encoderow = LERCEncode;
  1073. tif->tif_encodestrip = LERCEncode;
  1074. tif->tif_encodetile = LERCEncode;
  1075. tif->tif_cleanup = LERCCleanup;
  1076. /* Default values for codec-specific fields */
  1077. TIFFSetField(tif, TIFFTAG_LERC_VERSION, LERC_VERSION_2_4);
  1078. TIFFSetField(tif, TIFFTAG_LERC_ADD_COMPRESSION, LERC_ADD_COMPRESSION_NONE);
  1079. sp->maxzerror = 0.0;
  1080. sp->zstd_compress_level = 9; /* default comp. level */
  1081. sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
  1082. sp->state = 0;
  1083. return 1;
  1084. bad:
  1085. TIFFErrorExtR(tif, module, "No space for LERC state block");
  1086. return 0;
  1087. }
  1088. #endif /* LERC_SUPPORT */