tif_lerc.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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. 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 LERCEncode(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s);
  81. static int LERCDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s);
  82. static int LERCFixupTags(TIFF *tif)
  83. {
  84. (void)tif;
  85. return 1;
  86. }
  87. static int LERCSetupDecode(TIFF *tif)
  88. {
  89. LERCState *sp = LERCDecoderState(tif);
  90. assert(sp != NULL);
  91. /* if we were last encoding, terminate this mode */
  92. if (sp->state & LSTATE_INIT_ENCODE)
  93. {
  94. sp->state = 0;
  95. }
  96. sp->state |= LSTATE_INIT_DECODE;
  97. return 1;
  98. }
  99. static int GetLercDataType(TIFF *tif)
  100. {
  101. TIFFDirectory *td = &tif->tif_dir;
  102. static const char module[] = "GetLercDataType";
  103. if (td->td_sampleformat == SAMPLEFORMAT_INT && td->td_bitspersample == 8)
  104. {
  105. return 0;
  106. }
  107. if (td->td_sampleformat == SAMPLEFORMAT_UINT && td->td_bitspersample == 8)
  108. {
  109. return 1;
  110. }
  111. if (td->td_sampleformat == SAMPLEFORMAT_INT && td->td_bitspersample == 16)
  112. {
  113. return 2;
  114. }
  115. if (td->td_sampleformat == SAMPLEFORMAT_UINT && td->td_bitspersample == 16)
  116. {
  117. return 3;
  118. }
  119. if (td->td_sampleformat == SAMPLEFORMAT_INT && td->td_bitspersample == 32)
  120. {
  121. return 4;
  122. }
  123. if (td->td_sampleformat == SAMPLEFORMAT_UINT && td->td_bitspersample == 32)
  124. {
  125. return 5;
  126. }
  127. if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  128. td->td_bitspersample == 32)
  129. {
  130. return 6;
  131. }
  132. if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  133. td->td_bitspersample == 64)
  134. {
  135. return 7;
  136. }
  137. TIFFErrorExtR(
  138. tif, module,
  139. "Unsupported combination of SampleFormat and td_bitspersample");
  140. return -1;
  141. }
  142. static int SetupBuffers(TIFF *tif, LERCState *sp, const char *module)
  143. {
  144. TIFFDirectory *td = &tif->tif_dir;
  145. uint64_t new_size_64;
  146. uint64_t new_alloc_64;
  147. unsigned int new_size;
  148. unsigned int new_alloc;
  149. sp->uncompressed_offset = 0;
  150. if (isTiled(tif))
  151. {
  152. sp->segment_width = td->td_tilewidth;
  153. sp->segment_height = td->td_tilelength;
  154. }
  155. else
  156. {
  157. sp->segment_width = td->td_imagewidth;
  158. sp->segment_height = td->td_imagelength - tif->tif_row;
  159. if (sp->segment_height > td->td_rowsperstrip)
  160. sp->segment_height = td->td_rowsperstrip;
  161. }
  162. new_size_64 = (uint64_t)sp->segment_width * sp->segment_height *
  163. (td->td_bitspersample / 8);
  164. if (td->td_planarconfig == PLANARCONFIG_CONTIG)
  165. {
  166. new_size_64 *= td->td_samplesperpixel;
  167. }
  168. new_size = (unsigned int)new_size_64;
  169. sp->uncompressed_size = new_size;
  170. /* add some margin as we are going to use it also to store deflate/zstd
  171. * compressed data. We also need extra margin when writing very small
  172. * rasters with one mask per band. */
  173. new_alloc_64 = 256 + new_size_64 + new_size_64 / 3;
  174. #ifdef ZSTD_SUPPORT
  175. {
  176. size_t zstd_max = ZSTD_compressBound((size_t)new_size_64);
  177. if (new_alloc_64 < zstd_max)
  178. {
  179. new_alloc_64 = zstd_max;
  180. }
  181. }
  182. #endif
  183. new_alloc = (unsigned int)new_alloc_64;
  184. if (new_alloc != new_alloc_64)
  185. {
  186. TIFFErrorExtR(tif, module, "Too large uncompressed strip/tile");
  187. _TIFFfreeExt(tif, sp->uncompressed_buffer);
  188. sp->uncompressed_buffer = 0;
  189. sp->uncompressed_alloc = 0;
  190. return 0;
  191. }
  192. if (sp->uncompressed_alloc < new_alloc)
  193. {
  194. _TIFFfreeExt(tif, sp->uncompressed_buffer);
  195. sp->uncompressed_buffer = _TIFFmallocExt(tif, new_alloc);
  196. if (!sp->uncompressed_buffer)
  197. {
  198. TIFFErrorExtR(tif, module, "Cannot allocate buffer");
  199. _TIFFfreeExt(tif, sp->uncompressed_buffer);
  200. sp->uncompressed_buffer = 0;
  201. sp->uncompressed_alloc = 0;
  202. return 0;
  203. }
  204. sp->uncompressed_alloc = new_alloc;
  205. }
  206. if ((td->td_planarconfig == PLANARCONFIG_CONTIG &&
  207. td->td_extrasamples > 0 &&
  208. td->td_sampleinfo[td->td_extrasamples - 1] == EXTRASAMPLE_UNASSALPHA &&
  209. GetLercDataType(tif) == 1) ||
  210. (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  211. (td->td_bitspersample == 32 || td->td_bitspersample == 64)))
  212. {
  213. unsigned int mask_size = sp->segment_width * sp->segment_height;
  214. #if LERC_AT_LEAST_VERSION(3, 0, 0)
  215. if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  216. td->td_planarconfig == PLANARCONFIG_CONTIG)
  217. {
  218. /* We may need one mask per band */
  219. mask_size *= td->td_samplesperpixel;
  220. }
  221. #endif
  222. if (sp->mask_size < mask_size)
  223. {
  224. void *mask_buffer =
  225. _TIFFreallocExt(tif, sp->mask_buffer, mask_size);
  226. if (mask_buffer == NULL)
  227. {
  228. TIFFErrorExtR(tif, module, "Cannot allocate buffer");
  229. sp->mask_size = 0;
  230. _TIFFfreeExt(tif, sp->uncompressed_buffer);
  231. sp->uncompressed_buffer = 0;
  232. sp->uncompressed_alloc = 0;
  233. return 0;
  234. }
  235. sp->mask_buffer = (uint8_t *)mask_buffer;
  236. sp->mask_size = mask_size;
  237. }
  238. }
  239. return 1;
  240. }
  241. /*
  242. * Setup state for decoding a strip.
  243. */
  244. static int LERCPreDecode(TIFF *tif, uint16_t s)
  245. {
  246. static const char module[] = "LERCPreDecode";
  247. lerc_status lerc_ret;
  248. TIFFDirectory *td = &tif->tif_dir;
  249. LERCState *sp = LERCDecoderState(tif);
  250. int lerc_data_type;
  251. unsigned int infoArray[9];
  252. unsigned nomask_bands = td->td_samplesperpixel;
  253. int ndims;
  254. int use_mask = 0;
  255. uint8_t *lerc_data = tif->tif_rawcp;
  256. unsigned int lerc_data_size = (unsigned int)tif->tif_rawcc;
  257. (void)s;
  258. assert(sp != NULL);
  259. if (sp->state != LSTATE_INIT_DECODE)
  260. tif->tif_setupdecode(tif);
  261. lerc_data_type = GetLercDataType(tif);
  262. if (lerc_data_type < 0)
  263. return 0;
  264. if (!SetupBuffers(tif, sp, module))
  265. return 0;
  266. if (sp->additional_compression != LERC_ADD_COMPRESSION_NONE)
  267. {
  268. if (sp->compressed_size < sp->uncompressed_alloc)
  269. {
  270. _TIFFfreeExt(tif, sp->compressed_buffer);
  271. sp->compressed_buffer = _TIFFmallocExt(tif, sp->uncompressed_alloc);
  272. if (!sp->compressed_buffer)
  273. {
  274. sp->compressed_size = 0;
  275. return 0;
  276. }
  277. sp->compressed_size = sp->uncompressed_alloc;
  278. }
  279. }
  280. if (sp->additional_compression == LERC_ADD_COMPRESSION_DEFLATE)
  281. {
  282. #if LIBDEFLATE_SUPPORT
  283. enum libdeflate_result res;
  284. size_t lerc_data_sizet = 0;
  285. if (sp->libdeflate_dec == NULL)
  286. {
  287. sp->libdeflate_dec = libdeflate_alloc_decompressor();
  288. if (sp->libdeflate_dec == NULL)
  289. {
  290. TIFFErrorExtR(tif, module, "Cannot allocate decompressor");
  291. return 0;
  292. }
  293. }
  294. res = libdeflate_zlib_decompress(
  295. sp->libdeflate_dec, tif->tif_rawcp, (size_t)tif->tif_rawcc,
  296. sp->compressed_buffer, sp->compressed_size, &lerc_data_sizet);
  297. if (res != LIBDEFLATE_SUCCESS)
  298. {
  299. TIFFErrorExtR(tif, module, "Decoding error at scanline %lu",
  300. (unsigned long)tif->tif_row);
  301. return 0;
  302. }
  303. assert(lerc_data_sizet == (unsigned int)lerc_data_sizet);
  304. lerc_data = sp->compressed_buffer;
  305. lerc_data_size = (unsigned int)lerc_data_sizet;
  306. #else
  307. z_stream strm;
  308. int zlib_ret;
  309. memset(&strm, 0, sizeof(strm));
  310. strm.zalloc = NULL;
  311. strm.zfree = NULL;
  312. strm.opaque = NULL;
  313. zlib_ret = inflateInit(&strm);
  314. if (zlib_ret != Z_OK)
  315. {
  316. TIFFErrorExtR(tif, module, "inflateInit() failed");
  317. inflateEnd(&strm);
  318. return 0;
  319. }
  320. strm.avail_in = (uInt)tif->tif_rawcc;
  321. strm.next_in = tif->tif_rawcp;
  322. strm.avail_out = sp->compressed_size;
  323. strm.next_out = sp->compressed_buffer;
  324. zlib_ret = inflate(&strm, Z_FINISH);
  325. if (zlib_ret != Z_STREAM_END && zlib_ret != Z_OK)
  326. {
  327. TIFFErrorExtR(tif, module, "inflate() failed");
  328. inflateEnd(&strm);
  329. return 0;
  330. }
  331. lerc_data = sp->compressed_buffer;
  332. lerc_data_size = sp->compressed_size - strm.avail_out;
  333. inflateEnd(&strm);
  334. #endif
  335. }
  336. else if (sp->additional_compression == LERC_ADD_COMPRESSION_ZSTD)
  337. {
  338. #ifdef ZSTD_SUPPORT
  339. size_t zstd_ret;
  340. zstd_ret = ZSTD_decompress(sp->compressed_buffer, sp->compressed_size,
  341. tif->tif_rawcp, tif->tif_rawcc);
  342. if (ZSTD_isError(zstd_ret))
  343. {
  344. TIFFErrorExtR(tif, module, "Error in ZSTD_decompress(): %s",
  345. ZSTD_getErrorName(zstd_ret));
  346. return 0;
  347. }
  348. lerc_data = sp->compressed_buffer;
  349. lerc_data_size = (unsigned int)zstd_ret;
  350. #else
  351. TIFFErrorExtR(tif, module, "ZSTD support missing");
  352. return 0;
  353. #endif
  354. }
  355. else if (sp->additional_compression != LERC_ADD_COMPRESSION_NONE)
  356. {
  357. TIFFErrorExtR(tif, module, "Unhandled additional compression");
  358. return 0;
  359. }
  360. lerc_ret =
  361. lerc_getBlobInfo(lerc_data, lerc_data_size, infoArray, NULL, 9, 0);
  362. if (lerc_ret != 0)
  363. {
  364. TIFFErrorExtR(tif, module, "lerc_getBlobInfo() failed");
  365. return 0;
  366. }
  367. /* If the configuration is compatible of a LERC mask, and that the */
  368. /* LERC info has dim == samplesperpixel - 1, then there is a LERC */
  369. /* mask. */
  370. if (td->td_planarconfig == PLANARCONFIG_CONTIG && td->td_extrasamples > 0 &&
  371. td->td_sampleinfo[td->td_extrasamples - 1] == EXTRASAMPLE_UNASSALPHA &&
  372. GetLercDataType(tif) == 1 &&
  373. infoArray[2] == td->td_samplesperpixel - 1U)
  374. {
  375. use_mask = 1;
  376. nomask_bands--;
  377. }
  378. else if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP)
  379. {
  380. use_mask = 1;
  381. }
  382. ndims = td->td_planarconfig == PLANARCONFIG_CONTIG ? nomask_bands : 1;
  383. /* Info returned in infoArray is { version, dataType, nDim/nDepth, nCols,
  384. nRows, nBands, nValidPixels, blobSize,
  385. and starting with liblerc 3.0 nRequestedMasks } */
  386. if (infoArray[0] != (unsigned)sp->lerc_version)
  387. {
  388. TIFFWarningExtR(tif, module,
  389. "Unexpected version number: %d. Expected: %d",
  390. infoArray[0], sp->lerc_version);
  391. }
  392. if (infoArray[1] != (unsigned)lerc_data_type)
  393. {
  394. TIFFErrorExtR(tif, module, "Unexpected dataType: %d. Expected: %d",
  395. infoArray[1], lerc_data_type);
  396. return 0;
  397. }
  398. const unsigned nFoundDims = infoArray[2];
  399. #if LERC_AT_LEAST_VERSION(3, 0, 0)
  400. if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  401. td->td_planarconfig == PLANARCONFIG_CONTIG &&
  402. td->td_samplesperpixel > 1)
  403. {
  404. if (nFoundDims != 1 && nFoundDims != (unsigned)ndims)
  405. {
  406. TIFFErrorExtR(tif, module, "Unexpected nDim: %d. Expected: 1 or %d",
  407. nFoundDims, ndims);
  408. return 0;
  409. }
  410. }
  411. else
  412. #endif
  413. if (nFoundDims != (unsigned)ndims)
  414. {
  415. TIFFErrorExtR(tif, module, "Unexpected nDim: %d. Expected: %d",
  416. nFoundDims, ndims);
  417. return 0;
  418. }
  419. if (infoArray[3] != sp->segment_width)
  420. {
  421. TIFFErrorExtR(tif, module, "Unexpected nCols: %d. Expected: %du",
  422. infoArray[3], sp->segment_width);
  423. return 0;
  424. }
  425. if (infoArray[4] != sp->segment_height)
  426. {
  427. TIFFErrorExtR(tif, module, "Unexpected nRows: %d. Expected: %u",
  428. infoArray[4], sp->segment_height);
  429. return 0;
  430. }
  431. const unsigned nFoundBands = infoArray[5];
  432. if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  433. td->td_planarconfig == PLANARCONFIG_CONTIG &&
  434. td->td_samplesperpixel > 1 && nFoundDims == 1)
  435. {
  436. #if !LERC_AT_LEAST_VERSION(3, 0, 0)
  437. if (nFoundBands == td->td_samplesperpixel)
  438. {
  439. TIFFErrorExtR(
  440. tif, module,
  441. "Unexpected nBands: %d. This file may have been generated with "
  442. "a liblerc version >= 3.0, with one mask per band, and is not "
  443. "supported by this older version of liblerc",
  444. nFoundBands);
  445. return 0;
  446. }
  447. #endif
  448. if (nFoundBands != td->td_samplesperpixel)
  449. {
  450. TIFFErrorExtR(tif, module, "Unexpected nBands: %d. Expected: %d",
  451. nFoundBands, td->td_samplesperpixel);
  452. return 0;
  453. }
  454. }
  455. else if (nFoundBands != 1)
  456. {
  457. TIFFErrorExtR(tif, module, "Unexpected nBands: %d. Expected: %d",
  458. nFoundBands, 1);
  459. return 0;
  460. }
  461. if (infoArray[7] != lerc_data_size)
  462. {
  463. TIFFErrorExtR(tif, module, "Unexpected blobSize: %d. Expected: %u",
  464. infoArray[7], lerc_data_size);
  465. return 0;
  466. }
  467. int nRequestedMasks = use_mask ? 1 : 0;
  468. #if LERC_AT_LEAST_VERSION(3, 0, 0)
  469. const int nFoundMasks = infoArray[8];
  470. if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP &&
  471. td->td_planarconfig == PLANARCONFIG_CONTIG &&
  472. td->td_samplesperpixel > 1 && nFoundDims == 1)
  473. {
  474. if (nFoundMasks != 0 && nFoundMasks != td->td_samplesperpixel)
  475. {
  476. TIFFErrorExtR(tif, module,
  477. "Unexpected nFoundMasks: %d. Expected: 0 or %d",
  478. nFoundMasks, td->td_samplesperpixel);
  479. return 0;
  480. }
  481. nRequestedMasks = nFoundMasks;
  482. }
  483. else
  484. {
  485. if (nFoundMasks != 0 && nFoundMasks != 1)
  486. {
  487. TIFFErrorExtR(tif, module,
  488. "Unexpected nFoundMasks: %d. Expected: 0 or 1",
  489. nFoundMasks);
  490. return 0;
  491. }
  492. }
  493. if (td->td_sampleformat == SAMPLEFORMAT_IEEEFP && nFoundMasks == 0)
  494. {
  495. nRequestedMasks = 0;
  496. use_mask = 0;
  497. }
  498. #endif
  499. const unsigned nb_pixels = sp->segment_width * sp->segment_height;
  500. #if LERC_AT_LEAST_VERSION(3, 0, 0)
  501. if (nRequestedMasks > 1)
  502. {
  503. unsigned int num_bytes_needed =
  504. nb_pixels * td->td_samplesperpixel * (td->td_bitspersample / 8);
  505. if (sp->uncompressed_buffer_multiband_alloc < num_bytes_needed)
  506. {
  507. _TIFFfreeExt(tif, sp->uncompressed_buffer_multiband);
  508. sp->uncompressed_buffer_multiband =
  509. _TIFFmallocExt(tif, num_bytes_needed);
  510. if (!sp->uncompressed_buffer_multiband)
  511. {
  512. sp->uncompressed_buffer_multiband_alloc = 0;
  513. return 0;
  514. }
  515. sp->uncompressed_buffer_multiband_alloc = num_bytes_needed;
  516. }
  517. lerc_ret = lerc_decode(lerc_data, lerc_data_size, nRequestedMasks,
  518. sp->mask_buffer, nFoundDims, sp->segment_width,
  519. sp->segment_height, nFoundBands, lerc_data_type,
  520. sp->uncompressed_buffer_multiband);
  521. }
  522. else
  523. #endif
  524. {
  525. lerc_ret =
  526. lerc_decode(lerc_data, lerc_data_size,
  527. #if LERC_AT_LEAST_VERSION(3, 0, 0)
  528. nRequestedMasks,
  529. #endif
  530. use_mask ? sp->mask_buffer : NULL, nFoundDims,
  531. sp->segment_width, sp->segment_height, nFoundBands,
  532. lerc_data_type, sp->uncompressed_buffer);
  533. }
  534. if (lerc_ret != 0)
  535. {
  536. TIFFErrorExtR(tif, module, "lerc_decode() failed");
  537. return 0;
  538. }
  539. /* Interleave alpha mask with other samples. */
  540. if (use_mask && GetLercDataType(tif) == 1)
  541. {
  542. unsigned src_stride =
  543. (td->td_samplesperpixel - 1) * (td->td_bitspersample / 8);
  544. unsigned dst_stride =
  545. td->td_samplesperpixel * (td->td_bitspersample / 8);
  546. unsigned i = sp->segment_width * sp->segment_height;
  547. /* Operate from end to begin to be able to move in place */
  548. while (i > 0 && i > nomask_bands)
  549. {
  550. i--;
  551. sp->uncompressed_buffer[i * dst_stride + td->td_samplesperpixel -
  552. 1] = 255 * sp->mask_buffer[i];
  553. memcpy(sp->uncompressed_buffer + i * dst_stride,
  554. sp->uncompressed_buffer + i * src_stride, src_stride);
  555. }
  556. /* First pixels must use memmove due to overlapping areas */
  557. while (i > 0)
  558. {
  559. i--;
  560. sp->uncompressed_buffer[i * dst_stride + td->td_samplesperpixel -
  561. 1] = 255 * sp->mask_buffer[i];
  562. memmove(sp->uncompressed_buffer + i * dst_stride,
  563. sp->uncompressed_buffer + i * src_stride, src_stride);
  564. }
  565. }
  566. else if (use_mask && td->td_sampleformat == SAMPLEFORMAT_IEEEFP)
  567. {
  568. unsigned i;
  569. #if WORDS_BIGENDIAN
  570. const unsigned char nan_bytes[] = {0x7f, 0xc0, 0, 0};
  571. #else
  572. const unsigned char nan_bytes[] = {0, 0, 0xc0, 0x7f};
  573. #endif
  574. float nan_float32;
  575. memcpy(&nan_float32, nan_bytes, 4);
  576. if (td->td_planarconfig == PLANARCONFIG_SEPARATE ||
  577. td->td_samplesperpixel == 1)
  578. {
  579. if (td->td_bitspersample == 32)
  580. {
  581. for (i = 0; i < nb_pixels; i++)
  582. {
  583. if (sp->mask_buffer[i] == 0)
  584. ((float *)sp->uncompressed_buffer)[i] = nan_float32;
  585. }
  586. }
  587. else
  588. {
  589. const double nan_float64 = nan_float32;
  590. for (i = 0; i < nb_pixels; i++)
  591. {
  592. if (sp->mask_buffer[i] == 0)
  593. ((double *)sp->uncompressed_buffer)[i] = nan_float64;
  594. }
  595. }
  596. }
  597. else if (nRequestedMasks == 1)
  598. {
  599. assert(nFoundDims == td->td_samplesperpixel);
  600. assert(nFoundBands == 1);
  601. unsigned k = 0;
  602. if (td->td_bitspersample == 32)
  603. {
  604. for (i = 0; i < nb_pixels; i++)
  605. {
  606. for (int j = 0; j < td->td_samplesperpixel; j++)
  607. {
  608. if (sp->mask_buffer[i] == 0)
  609. ((float *)sp->uncompressed_buffer)[k] = nan_float32;
  610. ++k;
  611. }
  612. }
  613. }
  614. else
  615. {
  616. const double nan_float64 = nan_float32;
  617. for (i = 0; i < nb_pixels; i++)
  618. {
  619. for (int j = 0; j < td->td_samplesperpixel; j++)
  620. {
  621. if (sp->mask_buffer[i] == 0)
  622. ((double *)sp->uncompressed_buffer)[k] =
  623. nan_float64;
  624. ++k;
  625. }
  626. }
  627. }
  628. }
  629. #if LERC_AT_LEAST_VERSION(3, 0, 0)
  630. else
  631. {
  632. assert(nRequestedMasks == td->td_samplesperpixel);
  633. assert(nFoundDims == 1);
  634. assert(nFoundBands == td->td_samplesperpixel);
  635. unsigned k = 0;
  636. if (td->td_bitspersample == 32)
  637. {
  638. for (i = 0; i < nb_pixels; i++)
  639. {
  640. for (int j = 0; j < td->td_samplesperpixel; j++)
  641. {
  642. if (sp->mask_buffer[i + j * nb_pixels] == 0)
  643. ((float *)sp->uncompressed_buffer)[k] = nan_float32;
  644. else
  645. ((float *)sp->uncompressed_buffer)[k] =
  646. ((float *)sp->uncompressed_buffer_multiband)
  647. [i + j * nb_pixels];
  648. ++k;
  649. }
  650. }
  651. }
  652. else
  653. {
  654. const double nan_float64 = nan_float32;
  655. for (i = 0; i < nb_pixels; i++)
  656. {
  657. for (int j = 0; j < td->td_samplesperpixel; j++)
  658. {
  659. if (sp->mask_buffer[i + j * nb_pixels] == 0)
  660. ((double *)sp->uncompressed_buffer)[k] =
  661. nan_float64;
  662. else
  663. ((double *)sp->uncompressed_buffer)[k] =
  664. ((double *)sp->uncompressed_buffer_multiband)
  665. [i + j * nb_pixels];
  666. ++k;
  667. }
  668. }
  669. }
  670. }
  671. #endif
  672. }
  673. return 1;
  674. }
  675. /*
  676. * Decode a strip, tile or scanline.
  677. */
  678. static int LERCDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
  679. {
  680. static const char module[] = "LERCDecode";
  681. LERCState *sp = LERCDecoderState(tif);
  682. (void)s;
  683. assert(sp != NULL);
  684. assert(sp->state == LSTATE_INIT_DECODE);
  685. if (sp->uncompressed_buffer == 0)
  686. {
  687. memset(op, 0, (size_t)occ);
  688. TIFFErrorExtR(tif, module, "Uncompressed buffer not allocated");
  689. return 0;
  690. }
  691. if ((uint64_t)sp->uncompressed_offset + (uint64_t)occ >
  692. sp->uncompressed_size)
  693. {
  694. memset(op, 0, (size_t)occ);
  695. TIFFErrorExtR(tif, module, "Too many bytes read");
  696. return 0;
  697. }
  698. memcpy(op, sp->uncompressed_buffer + sp->uncompressed_offset, occ);
  699. sp->uncompressed_offset += (unsigned)occ;
  700. return 1;
  701. }
  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. _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. sp->compressed_buffer, sp->compressed_size, &numBytesWritten);
  1032. }
  1033. else
  1034. #endif
  1035. {
  1036. lerc_ret = lerc_encodeForVersion(
  1037. sp->uncompressed_buffer, sp->lerc_version, GetLercDataType(tif),
  1038. td->td_planarconfig == PLANARCONFIG_CONTIG ? dst_nbands : 1,
  1039. sp->segment_width, sp->segment_height, 1,
  1040. #if LERC_AT_LEAST_VERSION(3, 0, 0)
  1041. use_mask ? 1 : 0,
  1042. #endif
  1043. use_mask ? sp->mask_buffer : NULL, sp->maxzerror,
  1044. sp->compressed_buffer, sp->compressed_size, &numBytesWritten);
  1045. }
  1046. if (lerc_ret != 0)
  1047. {
  1048. TIFFErrorExtR(tif, module, "lerc_encode() failed");
  1049. return 0;
  1050. }
  1051. assert(numBytesWritten < estimated_compressed_size);
  1052. if (sp->additional_compression == LERC_ADD_COMPRESSION_DEFLATE)
  1053. {
  1054. #if LIBDEFLATE_SUPPORT
  1055. if (sp->libdeflate_enc == NULL)
  1056. {
  1057. /* To get results as good as zlib, we ask for an extra */
  1058. /* level of compression */
  1059. sp->libdeflate_enc = libdeflate_alloc_compressor(
  1060. sp->zipquality == Z_DEFAULT_COMPRESSION ? 7
  1061. : sp->zipquality >= 6 && sp->zipquality <= 9
  1062. ? sp->zipquality + 1
  1063. : sp->zipquality);
  1064. if (sp->libdeflate_enc == NULL)
  1065. {
  1066. TIFFErrorExtR(tif, module, "Cannot allocate compressor");
  1067. return 0;
  1068. }
  1069. }
  1070. /* Should not happen normally */
  1071. if (libdeflate_zlib_compress_bound(
  1072. sp->libdeflate_enc, numBytesWritten) > sp->uncompressed_alloc)
  1073. {
  1074. TIFFErrorExtR(tif, module,
  1075. "Output buffer for libdeflate too small");
  1076. return 0;
  1077. }
  1078. tif->tif_rawcc = libdeflate_zlib_compress(
  1079. sp->libdeflate_enc, sp->compressed_buffer, numBytesWritten,
  1080. sp->uncompressed_buffer, sp->uncompressed_alloc);
  1081. if (tif->tif_rawcc == 0)
  1082. {
  1083. TIFFErrorExtR(tif, module, "Encoder error at scanline %lu",
  1084. (unsigned long)tif->tif_row);
  1085. return 0;
  1086. }
  1087. #else
  1088. z_stream strm;
  1089. int zlib_ret;
  1090. int cappedQuality = sp->zipquality;
  1091. if (cappedQuality > Z_BEST_COMPRESSION)
  1092. cappedQuality = Z_BEST_COMPRESSION;
  1093. memset(&strm, 0, sizeof(strm));
  1094. strm.zalloc = NULL;
  1095. strm.zfree = NULL;
  1096. strm.opaque = NULL;
  1097. zlib_ret = deflateInit(&strm, cappedQuality);
  1098. if (zlib_ret != Z_OK)
  1099. {
  1100. TIFFErrorExtR(tif, module, "deflateInit() failed");
  1101. return 0;
  1102. }
  1103. strm.avail_in = numBytesWritten;
  1104. strm.next_in = sp->compressed_buffer;
  1105. strm.avail_out = sp->uncompressed_alloc;
  1106. strm.next_out = sp->uncompressed_buffer;
  1107. zlib_ret = deflate(&strm, Z_FINISH);
  1108. if (zlib_ret == Z_STREAM_END)
  1109. {
  1110. tif->tif_rawcc = sp->uncompressed_alloc - strm.avail_out;
  1111. }
  1112. deflateEnd(&strm);
  1113. if (zlib_ret != Z_STREAM_END)
  1114. {
  1115. TIFFErrorExtR(tif, module, "deflate() failed");
  1116. return 0;
  1117. }
  1118. #endif
  1119. {
  1120. int ret;
  1121. uint8_t *tif_rawdata_backup = tif->tif_rawdata;
  1122. tif->tif_rawdata = sp->uncompressed_buffer;
  1123. ret = TIFFFlushData1(tif);
  1124. tif->tif_rawdata = tif_rawdata_backup;
  1125. if (!ret)
  1126. {
  1127. return 0;
  1128. }
  1129. }
  1130. }
  1131. else if (sp->additional_compression == LERC_ADD_COMPRESSION_ZSTD)
  1132. {
  1133. #ifdef ZSTD_SUPPORT
  1134. size_t zstd_ret = ZSTD_compress(
  1135. sp->uncompressed_buffer, sp->uncompressed_alloc,
  1136. sp->compressed_buffer, numBytesWritten, sp->zstd_compress_level);
  1137. if (ZSTD_isError(zstd_ret))
  1138. {
  1139. TIFFErrorExtR(tif, module, "Error in ZSTD_compress(): %s",
  1140. ZSTD_getErrorName(zstd_ret));
  1141. return 0;
  1142. }
  1143. {
  1144. int ret;
  1145. uint8_t *tif_rawdata_backup = tif->tif_rawdata;
  1146. tif->tif_rawdata = sp->uncompressed_buffer;
  1147. tif->tif_rawcc = zstd_ret;
  1148. ret = TIFFFlushData1(tif);
  1149. tif->tif_rawdata = tif_rawdata_backup;
  1150. if (!ret)
  1151. {
  1152. return 0;
  1153. }
  1154. }
  1155. #else
  1156. TIFFErrorExtR(tif, module, "ZSTD support missing");
  1157. return 0;
  1158. #endif
  1159. }
  1160. else if (sp->additional_compression != LERC_ADD_COMPRESSION_NONE)
  1161. {
  1162. TIFFErrorExtR(tif, module, "Unhandled additional compression");
  1163. return 0;
  1164. }
  1165. else
  1166. {
  1167. int ret;
  1168. uint8_t *tif_rawdata_backup = tif->tif_rawdata;
  1169. tif->tif_rawdata = sp->compressed_buffer;
  1170. tif->tif_rawcc = numBytesWritten;
  1171. ret = TIFFFlushData1(tif);
  1172. tif->tif_rawdata = tif_rawdata_backup;
  1173. if (!ret)
  1174. return 0;
  1175. }
  1176. return 1;
  1177. }
  1178. static void LERCCleanup(TIFF *tif)
  1179. {
  1180. LERCState *sp = GetLERCState(tif);
  1181. assert(sp != 0);
  1182. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  1183. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  1184. _TIFFfreeExt(tif, sp->uncompressed_buffer);
  1185. _TIFFfreeExt(tif, sp->uncompressed_buffer_multiband);
  1186. _TIFFfreeExt(tif, sp->compressed_buffer);
  1187. _TIFFfreeExt(tif, sp->mask_buffer);
  1188. #if LIBDEFLATE_SUPPORT
  1189. if (sp->libdeflate_dec)
  1190. libdeflate_free_decompressor(sp->libdeflate_dec);
  1191. if (sp->libdeflate_enc)
  1192. libdeflate_free_compressor(sp->libdeflate_enc);
  1193. #endif
  1194. _TIFFfreeExt(tif, sp);
  1195. tif->tif_data = NULL;
  1196. _TIFFSetDefaultCompressionState(tif);
  1197. }
  1198. static const TIFFField LERCFields[] = {
  1199. {TIFFTAG_LERC_PARAMETERS, TIFF_VARIABLE2, TIFF_VARIABLE2, TIFF_LONG, 0,
  1200. TIFF_SETGET_C32_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, FALSE, TRUE,
  1201. "LercParameters", NULL},
  1202. {TIFFTAG_LERC_MAXZERROR, 0, 0, TIFF_ANY, 0, TIFF_SETGET_DOUBLE,
  1203. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "LercMaximumError",
  1204. NULL},
  1205. {TIFFTAG_LERC_VERSION, 0, 0, TIFF_ANY, 0, TIFF_SETGET_UINT32,
  1206. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE, "LercVersion", NULL},
  1207. {TIFFTAG_LERC_ADD_COMPRESSION, 0, 0, TIFF_ANY, 0, TIFF_SETGET_UINT32,
  1208. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, FALSE, FALSE,
  1209. "LercAdditionalCompression", NULL},
  1210. {TIFFTAG_ZSTD_LEVEL, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
  1211. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE,
  1212. "ZSTD zstd_compress_level", NULL},
  1213. {TIFFTAG_ZIPQUALITY, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
  1214. TIFF_SETGET_UNDEFINED, FIELD_PSEUDO, TRUE, FALSE, "", 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. tif->tif_setupencode = LERCSetupEncode;
  1394. tif->tif_preencode = LERCPreEncode;
  1395. tif->tif_postencode = LERCPostEncode;
  1396. tif->tif_encoderow = LERCEncode;
  1397. tif->tif_encodestrip = LERCEncode;
  1398. tif->tif_encodetile = LERCEncode;
  1399. tif->tif_cleanup = LERCCleanup;
  1400. /* Default values for codec-specific fields */
  1401. TIFFSetField(tif, TIFFTAG_LERC_VERSION, LERC_VERSION_2_4);
  1402. TIFFSetField(tif, TIFFTAG_LERC_ADD_COMPRESSION, LERC_ADD_COMPRESSION_NONE);
  1403. sp->maxzerror = 0.0;
  1404. sp->zstd_compress_level = 9; /* default comp. level */
  1405. sp->zipquality = Z_DEFAULT_COMPRESSION; /* default comp. level */
  1406. sp->state = 0;
  1407. return 1;
  1408. bad:
  1409. TIFFErrorExtR(tif, module, "No space for LERC state block");
  1410. return 0;
  1411. }
  1412. #endif /* LERC_SUPPORT */