tif_webp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /*
  2. * Copyright (c) 2018, Mapbox
  3. * Author: <norman.barker at mapbox.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 WEBP_SUPPORT
  26. /*
  27. * TIFF Library.
  28. *
  29. * WEBP Compression Support
  30. *
  31. */
  32. #include "webp/decode.h"
  33. #include "webp/encode.h"
  34. #include <stdio.h>
  35. #define LSTATE_INIT_DECODE 0x01
  36. #define LSTATE_INIT_ENCODE 0x02
  37. /*
  38. * State block for each open TIFF
  39. * file using WEBP compression/decompression.
  40. */
  41. typedef struct {
  42. uint16 nSamples; /* number of samples per pixel */
  43. int lossless; /* lossy/lossless compression */
  44. int quality_level; /* compression level */
  45. WebPPicture sPicture; /* WebP Picture */
  46. WebPConfig sEncoderConfig; /* WebP encoder config */
  47. uint8* pBuffer; /* buffer to hold raw data on encoding */
  48. unsigned int buffer_offset; /* current offset into the buffer */
  49. unsigned int buffer_size;
  50. WebPIDecoder* psDecoder; /* WebPIDecoder */
  51. WebPDecBuffer sDecBuffer; /* Decoder buffer */
  52. int last_y; /* Last row decoded */
  53. int state; /* state flags */
  54. TIFFVGetMethod vgetparent; /* super-class method */
  55. TIFFVSetMethod vsetparent; /* super-class method */
  56. } WebPState;
  57. #define LState(tif) ((WebPState*) (tif)->tif_data)
  58. #define DecoderState(tif) LState(tif)
  59. #define EncoderState(tif) LState(tif)
  60. static int TWebPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
  61. static int TWebPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
  62. static
  63. int TWebPDatasetWriter(const uint8_t* data, size_t data_size,
  64. const WebPPicture* const picture)
  65. {
  66. static const char module[] = "TWebPDatasetWriter";
  67. TIFF* tif = (TIFF*)(picture->custom_ptr);
  68. if ( (tif->tif_rawcc + (tmsize_t)data_size) > tif->tif_rawdatasize ) {
  69. TIFFErrorExt(tif->tif_clientdata, module,
  70. "Buffer too small by " TIFF_SIZE_FORMAT " bytes.",
  71. (size_t) (tif->tif_rawcc + data_size - tif->tif_rawdatasize));
  72. return 0;
  73. } else {
  74. _TIFFmemcpy(tif->tif_rawcp, data, data_size);
  75. tif->tif_rawcc += data_size;
  76. tif->tif_rawcp += data_size;
  77. return 1;
  78. }
  79. }
  80. /*
  81. * Encode a chunk of pixels.
  82. */
  83. static int
  84. TWebPEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  85. {
  86. static const char module[] = "TWebPEncode";
  87. WebPState *sp = EncoderState(tif);
  88. (void) s;
  89. assert(sp != NULL);
  90. assert(sp->state == LSTATE_INIT_ENCODE);
  91. if( (uint64)sp->buffer_offset +
  92. (uint64)cc > sp->buffer_size )
  93. {
  94. TIFFErrorExt(tif->tif_clientdata, module,
  95. "Too many bytes to be written");
  96. return 0;
  97. }
  98. memcpy(sp->pBuffer + sp->buffer_offset,
  99. bp, cc);
  100. sp->buffer_offset += (unsigned)cc;
  101. return 1;
  102. }
  103. static int
  104. TWebPDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
  105. {
  106. static const char module[] = "WebPDecode";
  107. VP8StatusCode status = VP8_STATUS_OK;
  108. WebPState *sp = DecoderState(tif);
  109. (void) s;
  110. assert(sp != NULL);
  111. assert(sp->state == LSTATE_INIT_DECODE);
  112. if (occ % sp->sDecBuffer.u.RGBA.stride)
  113. {
  114. TIFFErrorExt(tif->tif_clientdata, module,
  115. "Fractional scanlines cannot be read");
  116. return 0;
  117. }
  118. status = WebPIAppend(sp->psDecoder, tif->tif_rawcp, tif->tif_rawcc);
  119. if (status != VP8_STATUS_OK && status != VP8_STATUS_SUSPENDED) {
  120. if (status == VP8_STATUS_INVALID_PARAM) {
  121. TIFFErrorExt(tif->tif_clientdata, module,
  122. "Invalid parameter used.");
  123. } else if (status == VP8_STATUS_OUT_OF_MEMORY) {
  124. TIFFErrorExt(tif->tif_clientdata, module,
  125. "Out of memory.");
  126. } else {
  127. TIFFErrorExt(tif->tif_clientdata, module,
  128. "Unrecognized error.");
  129. }
  130. return 0;
  131. } else {
  132. int current_y, stride;
  133. uint8_t* buf;
  134. /* Returns the RGB/A image decoded so far */
  135. buf = WebPIDecGetRGB(sp->psDecoder, &current_y, NULL, NULL, &stride);
  136. if ((buf != NULL) &&
  137. (occ <= stride * (current_y - sp->last_y))) {
  138. memcpy(op,
  139. buf + (sp->last_y * stride),
  140. occ);
  141. tif->tif_rawcp += tif->tif_rawcc;
  142. tif->tif_rawcc = 0;
  143. sp->last_y += occ / sp->sDecBuffer.u.RGBA.stride;
  144. return 1;
  145. } else {
  146. TIFFErrorExt(tif->tif_clientdata, module, "Unable to decode WebP data.");
  147. return 0;
  148. }
  149. }
  150. }
  151. static int
  152. TWebPFixupTags(TIFF* tif)
  153. {
  154. (void) tif;
  155. if (tif->tif_dir.td_planarconfig != PLANARCONFIG_CONTIG) {
  156. static const char module[] = "TWebPFixupTags";
  157. TIFFErrorExt(tif->tif_clientdata, module,
  158. "TIFF WEBP requires data to be stored contiguously in RGB e.g. RGBRGBRGB "
  159. #if WEBP_ENCODER_ABI_VERSION >= 0x0100
  160. "or RGBARGBARGBA"
  161. #endif
  162. );
  163. return 0;
  164. }
  165. return 1;
  166. }
  167. static int
  168. TWebPSetupDecode(TIFF* tif)
  169. {
  170. static const char module[] = "WebPSetupDecode";
  171. uint16 nBitsPerSample = tif->tif_dir.td_bitspersample;
  172. uint16 sampleFormat = tif->tif_dir.td_sampleformat;
  173. WebPState* sp = DecoderState(tif);
  174. assert(sp != NULL);
  175. sp->nSamples = tif->tif_dir.td_samplesperpixel;
  176. /* check band count */
  177. if ( sp->nSamples != 3
  178. #if WEBP_ENCODER_ABI_VERSION >= 0x0100
  179. && sp->nSamples != 4
  180. #endif
  181. )
  182. {
  183. TIFFErrorExt(tif->tif_clientdata, module,
  184. "WEBP driver doesn't support %d bands. Must be 3 (RGB) "
  185. #if WEBP_ENCODER_ABI_VERSION >= 0x0100
  186. "or 4 (RGBA) "
  187. #endif
  188. "bands.",
  189. sp->nSamples );
  190. return 0;
  191. }
  192. /* check bits per sample and data type */
  193. if ((nBitsPerSample != 8) && (sampleFormat != 1)) {
  194. TIFFErrorExt(tif->tif_clientdata, module,
  195. "WEBP driver requires 8 bit unsigned data");
  196. return 0;
  197. }
  198. /* if we were last encoding, terminate this mode */
  199. if (sp->state & LSTATE_INIT_ENCODE) {
  200. WebPPictureFree(&sp->sPicture);
  201. if (sp->pBuffer != NULL) {
  202. _TIFFfree(sp->pBuffer);
  203. sp->pBuffer = NULL;
  204. }
  205. sp->buffer_offset = 0;
  206. sp->state = 0;
  207. }
  208. sp->state |= LSTATE_INIT_DECODE;
  209. return 1;
  210. }
  211. /*
  212. * Setup state for decoding a strip.
  213. */
  214. static int
  215. TWebPPreDecode(TIFF* tif, uint16 s)
  216. {
  217. static const char module[] = "TWebPPreDecode";
  218. uint32 segment_width, segment_height;
  219. WebPState* sp = DecoderState(tif);
  220. TIFFDirectory* td = &tif->tif_dir;
  221. (void) s;
  222. assert(sp != NULL);
  223. if (isTiled(tif)) {
  224. segment_width = td->td_tilewidth;
  225. segment_height = td->td_tilelength;
  226. } else {
  227. segment_width = td->td_imagewidth;
  228. segment_height = td->td_imagelength - tif->tif_row;
  229. if (segment_height > td->td_rowsperstrip)
  230. segment_height = td->td_rowsperstrip;
  231. }
  232. if( segment_width > 16383 || segment_height > 16383 ) {
  233. TIFFErrorExt(tif->tif_clientdata, module,
  234. "WEBP maximum image dimensions are 16383 x 16383.");
  235. return 0;
  236. }
  237. if( (sp->state & LSTATE_INIT_DECODE) == 0 )
  238. tif->tif_setupdecode(tif);
  239. if (sp->psDecoder != NULL) {
  240. WebPIDelete(sp->psDecoder);
  241. WebPFreeDecBuffer(&sp->sDecBuffer);
  242. sp->psDecoder = NULL;
  243. }
  244. sp->last_y = 0;
  245. WebPInitDecBuffer(&sp->sDecBuffer);
  246. sp->sDecBuffer.is_external_memory = 0;
  247. sp->sDecBuffer.width = segment_width;
  248. sp->sDecBuffer.height = segment_height;
  249. sp->sDecBuffer.u.RGBA.stride = segment_width * sp->nSamples;
  250. sp->sDecBuffer.u.RGBA.size = segment_width * sp->nSamples * segment_height;
  251. if (sp->nSamples > 3) {
  252. sp->sDecBuffer.colorspace = MODE_RGBA;
  253. } else {
  254. sp->sDecBuffer.colorspace = MODE_RGB;
  255. }
  256. sp->psDecoder = WebPINewDecoder(&sp->sDecBuffer);
  257. if (sp->psDecoder == NULL) {
  258. TIFFErrorExt(tif->tif_clientdata, module,
  259. "Unable to allocate WebP decoder.");
  260. return 0;
  261. }
  262. return 1;
  263. }
  264. static int
  265. TWebPSetupEncode(TIFF* tif)
  266. {
  267. static const char module[] = "WebPSetupEncode";
  268. uint16 nBitsPerSample = tif->tif_dir.td_bitspersample;
  269. uint16 sampleFormat = tif->tif_dir.td_sampleformat;
  270. WebPState* sp = EncoderState(tif);
  271. assert(sp != NULL);
  272. sp->nSamples = tif->tif_dir.td_samplesperpixel;
  273. /* check band count */
  274. if ( sp->nSamples != 3
  275. #if WEBP_ENCODER_ABI_VERSION >= 0x0100
  276. && sp->nSamples != 4
  277. #endif
  278. )
  279. {
  280. TIFFErrorExt(tif->tif_clientdata, module,
  281. "WEBP driver doesn't support %d bands. Must be 3 (RGB) "
  282. #if WEBP_ENCODER_ABI_VERSION >= 0x0100
  283. "or 4 (RGBA) "
  284. #endif
  285. "bands.",
  286. sp->nSamples );
  287. return 0;
  288. }
  289. /* check bits per sample and data type */
  290. if ((nBitsPerSample != 8) || (sampleFormat != SAMPLEFORMAT_UINT)) {
  291. TIFFErrorExt(tif->tif_clientdata, module,
  292. "WEBP driver requires 8 bit unsigned data");
  293. return 0;
  294. }
  295. if (sp->state & LSTATE_INIT_DECODE) {
  296. WebPIDelete(sp->psDecoder);
  297. WebPFreeDecBuffer(&sp->sDecBuffer);
  298. sp->psDecoder = NULL;
  299. sp->last_y = 0;
  300. sp->state = 0;
  301. }
  302. sp->state |= LSTATE_INIT_ENCODE;
  303. if (!WebPPictureInit(&sp->sPicture)) {
  304. TIFFErrorExt(tif->tif_clientdata, module,
  305. "Error initializing WebP picture.");
  306. return 0;
  307. }
  308. if (!WebPConfigInitInternal(&sp->sEncoderConfig, WEBP_PRESET_DEFAULT,
  309. (float)sp->quality_level,
  310. WEBP_ENCODER_ABI_VERSION)) {
  311. TIFFErrorExt(tif->tif_clientdata, module,
  312. "Error creating WebP encoder configuration.");
  313. return 0;
  314. }
  315. // WebPConfigInitInternal above sets lossless to false
  316. #if WEBP_ENCODER_ABI_VERSION >= 0x0100
  317. sp->sEncoderConfig.lossless = sp->lossless;
  318. if (sp->lossless) {
  319. sp->sPicture.use_argb = 1;
  320. }
  321. #endif
  322. if (!WebPValidateConfig(&sp->sEncoderConfig)) {
  323. TIFFErrorExt(tif->tif_clientdata, module,
  324. "Error with WebP encoder configuration.");
  325. return 0;
  326. }
  327. return 1;
  328. }
  329. /*
  330. * Reset encoding state at the start of a strip.
  331. */
  332. static int
  333. TWebPPreEncode(TIFF* tif, uint16 s)
  334. {
  335. static const char module[] = "TWebPPreEncode";
  336. uint32 segment_width, segment_height;
  337. WebPState *sp = EncoderState(tif);
  338. TIFFDirectory* td = &tif->tif_dir;
  339. (void) s;
  340. assert(sp != NULL);
  341. if( sp->state != LSTATE_INIT_ENCODE )
  342. tif->tif_setupencode(tif);
  343. /*
  344. * Set encoding parameters for this strip/tile.
  345. */
  346. if (isTiled(tif)) {
  347. segment_width = td->td_tilewidth;
  348. segment_height = td->td_tilelength;
  349. } else {
  350. segment_width = td->td_imagewidth;
  351. segment_height = td->td_imagelength - tif->tif_row;
  352. if (segment_height > td->td_rowsperstrip)
  353. segment_height = td->td_rowsperstrip;
  354. }
  355. if( segment_width > 16383 || segment_height > 16383 ) {
  356. TIFFErrorExt(tif->tif_clientdata, module,
  357. "WEBP maximum image dimensions are 16383 x 16383.");
  358. return 0;
  359. }
  360. /* set up buffer for raw data */
  361. /* given above check and that nSamples <= 4, buffer_size is <= 1 GB */
  362. sp->buffer_size = segment_width * segment_height * sp->nSamples;
  363. if (sp->pBuffer != NULL) {
  364. _TIFFfree(sp->pBuffer);
  365. sp->pBuffer = NULL;
  366. }
  367. sp->pBuffer = _TIFFmalloc(sp->buffer_size);
  368. if( !sp->pBuffer) {
  369. TIFFErrorExt(tif->tif_clientdata, module, "Cannot allocate buffer");
  370. return 0;
  371. }
  372. sp->buffer_offset = 0;
  373. sp->sPicture.width = segment_width;
  374. sp->sPicture.height = segment_height;
  375. sp->sPicture.writer = TWebPDatasetWriter;
  376. sp->sPicture.custom_ptr = tif;
  377. return 1;
  378. }
  379. /*
  380. * Finish off an encoded strip by flushing it.
  381. */
  382. static int
  383. TWebPPostEncode(TIFF* tif)
  384. {
  385. static const char module[] = "WebPPostEncode";
  386. int64_t stride;
  387. WebPState *sp = EncoderState(tif);
  388. assert(sp != NULL);
  389. assert(sp->state == LSTATE_INIT_ENCODE);
  390. stride = (int64_t)sp->sPicture.width * sp->nSamples;
  391. #if WEBP_ENCODER_ABI_VERSION >= 0x0100
  392. if (sp->nSamples == 4) {
  393. if (!WebPPictureImportRGBA(&sp->sPicture, sp->pBuffer, (int)stride)) {
  394. TIFFErrorExt(tif->tif_clientdata, module,
  395. "WebPPictureImportRGBA() failed" );
  396. return 0;
  397. }
  398. }
  399. else
  400. #endif
  401. if (!WebPPictureImportRGB(&sp->sPicture, sp->pBuffer, (int)stride)) {
  402. TIFFErrorExt(tif->tif_clientdata, module,
  403. "WebPPictureImportRGB() failed");
  404. return 0;
  405. }
  406. if (!WebPEncode(&sp->sEncoderConfig, &sp->sPicture)) {
  407. #if WEBP_ENCODER_ABI_VERSION >= 0x0100
  408. const char* pszErrorMsg = NULL;
  409. switch(sp->sPicture.error_code) {
  410. case VP8_ENC_ERROR_OUT_OF_MEMORY:
  411. pszErrorMsg = "Out of memory"; break;
  412. case VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY:
  413. pszErrorMsg = "Out of memory while flushing bits"; break;
  414. case VP8_ENC_ERROR_NULL_PARAMETER:
  415. pszErrorMsg = "A pointer parameter is NULL"; break;
  416. case VP8_ENC_ERROR_INVALID_CONFIGURATION:
  417. pszErrorMsg = "Configuration is invalid"; break;
  418. case VP8_ENC_ERROR_BAD_DIMENSION:
  419. pszErrorMsg = "Picture has invalid width/height"; break;
  420. case VP8_ENC_ERROR_PARTITION0_OVERFLOW:
  421. pszErrorMsg = "Partition is bigger than 512k. Try using less "
  422. "SEGMENTS, or increase PARTITION_LIMIT value";
  423. break;
  424. case VP8_ENC_ERROR_PARTITION_OVERFLOW:
  425. pszErrorMsg = "Partition is bigger than 16M";
  426. break;
  427. case VP8_ENC_ERROR_BAD_WRITE:
  428. pszErrorMsg = "Error while fludshing bytes"; break;
  429. case VP8_ENC_ERROR_FILE_TOO_BIG:
  430. pszErrorMsg = "File is bigger than 4G"; break;
  431. case VP8_ENC_ERROR_USER_ABORT:
  432. pszErrorMsg = "User interrupted";
  433. break;
  434. default:
  435. TIFFErrorExt(tif->tif_clientdata, module,
  436. "WebPEncode returned an unknown error code: %d",
  437. sp->sPicture.error_code);
  438. pszErrorMsg = "Unknown WebP error type.";
  439. break;
  440. }
  441. TIFFErrorExt(tif->tif_clientdata, module,
  442. "WebPEncode() failed : %s", pszErrorMsg);
  443. #else
  444. TIFFErrorExt(tif->tif_clientdata, module,
  445. "Error in WebPEncode()");
  446. #endif
  447. return 0;
  448. }
  449. sp->sPicture.custom_ptr = NULL;
  450. if (!TIFFFlushData1(tif))
  451. {
  452. TIFFErrorExt(tif->tif_clientdata, module,
  453. "Error flushing TIFF WebP encoder.");
  454. return 0;
  455. }
  456. return 1;
  457. }
  458. static void
  459. TWebPCleanup(TIFF* tif)
  460. {
  461. WebPState* sp = LState(tif);
  462. assert(sp != 0);
  463. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  464. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  465. if (sp->state & LSTATE_INIT_ENCODE) {
  466. WebPPictureFree(&sp->sPicture);
  467. }
  468. if (sp->psDecoder != NULL) {
  469. WebPIDelete(sp->psDecoder);
  470. WebPFreeDecBuffer(&sp->sDecBuffer);
  471. sp->psDecoder = NULL;
  472. sp->last_y = 0;
  473. }
  474. if (sp->pBuffer != NULL) {
  475. _TIFFfree(sp->pBuffer);
  476. sp->pBuffer = NULL;
  477. }
  478. _TIFFfree(tif->tif_data);
  479. tif->tif_data = NULL;
  480. _TIFFSetDefaultCompressionState(tif);
  481. }
  482. static int
  483. TWebPVSetField(TIFF* tif, uint32 tag, va_list ap)
  484. {
  485. static const char module[] = "WebPVSetField";
  486. WebPState* sp = LState(tif);
  487. switch (tag) {
  488. case TIFFTAG_WEBP_LEVEL:
  489. sp->quality_level = (int) va_arg(ap, int);
  490. if( sp->quality_level <= 0 ||
  491. sp->quality_level > 100.0f ) {
  492. TIFFWarningExt(tif->tif_clientdata, module,
  493. "WEBP_LEVEL should be between 1 and 100");
  494. }
  495. return 1;
  496. case TIFFTAG_WEBP_LOSSLESS:
  497. #if WEBP_ENCODER_ABI_VERSION >= 0x0100
  498. sp->lossless = va_arg(ap, int);
  499. if (sp->lossless){
  500. sp->quality_level = 100;
  501. }
  502. return 1;
  503. #else
  504. TIFFErrorExt(tif->tif_clientdata, module,
  505. "Need to upgrade WEBP driver, this version doesn't support "
  506. "lossless compression.");
  507. return 0;
  508. #endif
  509. default:
  510. return (*sp->vsetparent)(tif, tag, ap);
  511. }
  512. /*NOTREACHED*/
  513. }
  514. static int
  515. TWebPVGetField(TIFF* tif, uint32 tag, va_list ap)
  516. {
  517. WebPState* sp = LState(tif);
  518. switch (tag) {
  519. case TIFFTAG_WEBP_LEVEL:
  520. *va_arg(ap, int*) = sp->quality_level;
  521. break;
  522. case TIFFTAG_WEBP_LOSSLESS:
  523. *va_arg(ap, int*) = sp->lossless;
  524. break;
  525. default:
  526. return (*sp->vgetparent)(tif, tag, ap);
  527. }
  528. return 1;
  529. }
  530. static const TIFFField TWebPFields[] = {
  531. { TIFFTAG_WEBP_LEVEL, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
  532. TIFF_SETGET_UNDEFINED,
  533. FIELD_PSEUDO, TRUE, FALSE, "WEBP quality", NULL },
  534. { TIFFTAG_WEBP_LOSSLESS, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT,
  535. TIFF_SETGET_UNDEFINED,
  536. FIELD_PSEUDO, TRUE, FALSE, "WEBP lossless/lossy", NULL
  537. },
  538. };
  539. int
  540. TIFFInitWebP(TIFF* tif, int scheme)
  541. {
  542. static const char module[] = "TIFFInitWebP";
  543. WebPState* sp;
  544. (void)scheme;
  545. assert( scheme == COMPRESSION_WEBP );
  546. /*
  547. * Merge codec-specific tag information.
  548. */
  549. if ( !_TIFFMergeFields(tif, TWebPFields, TIFFArrayCount(TWebPFields)) ) {
  550. TIFFErrorExt(tif->tif_clientdata, module,
  551. "Merging WebP codec-specific tags failed");
  552. return 0;
  553. }
  554. /*
  555. * Allocate state block so tag methods have storage to record values.
  556. */
  557. tif->tif_data = (uint8*) _TIFFmalloc(sizeof(WebPState));
  558. if (tif->tif_data == NULL)
  559. goto bad;
  560. sp = LState(tif);
  561. /*
  562. * Override parent get/set field methods.
  563. */
  564. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  565. tif->tif_tagmethods.vgetfield = TWebPVGetField; /* hook for codec tags */
  566. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  567. tif->tif_tagmethods.vsetfield = TWebPVSetField; /* hook for codec tags */
  568. /* Default values for codec-specific fields */
  569. sp->quality_level = 75; /* default comp. level */
  570. sp->lossless = 0; /* default to false */
  571. sp->state = 0;
  572. sp->nSamples = 0;
  573. sp->psDecoder = NULL;
  574. sp->last_y = 0;
  575. sp->buffer_offset = 0;
  576. sp->pBuffer = NULL;
  577. /*
  578. * Install codec methods.
  579. * Notes:
  580. * encoderow is not supported
  581. */
  582. tif->tif_fixuptags = TWebPFixupTags;
  583. tif->tif_setupdecode = TWebPSetupDecode;
  584. tif->tif_predecode = TWebPPreDecode;
  585. tif->tif_decoderow = TWebPDecode;
  586. tif->tif_decodestrip = TWebPDecode;
  587. tif->tif_decodetile = TWebPDecode;
  588. tif->tif_setupencode = TWebPSetupEncode;
  589. tif->tif_preencode = TWebPPreEncode;
  590. tif->tif_postencode = TWebPPostEncode;
  591. tif->tif_encoderow = TWebPEncode;
  592. tif->tif_encodestrip = TWebPEncode;
  593. tif->tif_encodetile = TWebPEncode;
  594. tif->tif_cleanup = TWebPCleanup;
  595. return 1;
  596. bad:
  597. TIFFErrorExt(tif->tif_clientdata, module,
  598. "No space for WebP state block");
  599. return 0;
  600. }
  601. #endif /* WEBP_SUPPORT */