|
Packit |
7838c8 |
/* $Id: tif_lzma.c,v 1.6 2016-09-17 09:18:59 erouault Exp $ */
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
/*
|
|
Packit |
7838c8 |
* Copyright (c) 2010, Andrey Kiselev <dron@ak4719.spb.edu>
|
|
Packit |
7838c8 |
*
|
|
Packit |
7838c8 |
* Permission to use, copy, modify, distribute, and sell this software and
|
|
Packit |
7838c8 |
* its documentation for any purpose is hereby granted without fee, provided
|
|
Packit |
7838c8 |
* that (i) the above copyright notices and this permission notice appear in
|
|
Packit |
7838c8 |
* all copies of the software and related documentation, and (ii) the names of
|
|
Packit |
7838c8 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or
|
|
Packit |
7838c8 |
* publicity relating to the software without the specific, prior written
|
|
Packit |
7838c8 |
* permission of Sam Leffler and Silicon Graphics.
|
|
Packit |
7838c8 |
*
|
|
Packit |
7838c8 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
|
|
Packit |
7838c8 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
|
|
Packit |
7838c8 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
|
|
Packit |
7838c8 |
*
|
|
Packit |
7838c8 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
|
|
Packit |
7838c8 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
|
|
Packit |
7838c8 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
|
Packit |
7838c8 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
|
|
Packit |
7838c8 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
|
Packit |
7838c8 |
* OF THIS SOFTWARE.
|
|
Packit |
7838c8 |
*/
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
#include "tiffiop.h"
|
|
Packit |
7838c8 |
#ifdef LZMA_SUPPORT
|
|
Packit |
7838c8 |
/*
|
|
Packit |
7838c8 |
* TIFF Library.
|
|
Packit |
7838c8 |
*
|
|
Packit |
7838c8 |
* LZMA2 Compression Support
|
|
Packit |
7838c8 |
*
|
|
Packit |
7838c8 |
* You need an LZMA2 SDK to link with. See http://tukaani.org/xz/ for details.
|
|
Packit |
7838c8 |
*
|
|
Packit |
7838c8 |
* The codec is derived from ZLIB codec (tif_zip.c).
|
|
Packit |
7838c8 |
*/
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
#include "tif_predict.h"
|
|
Packit |
7838c8 |
#include "lzma.h"
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
#include <stdio.h>
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
/*
|
|
Packit |
7838c8 |
* State block for each open TIFF file using LZMA2 compression/decompression.
|
|
Packit |
7838c8 |
*/
|
|
Packit |
7838c8 |
typedef struct {
|
|
Packit |
7838c8 |
TIFFPredictorState predict;
|
|
Packit |
7838c8 |
lzma_stream stream;
|
|
Packit |
7838c8 |
lzma_filter filters[LZMA_FILTERS_MAX + 1];
|
|
Packit |
7838c8 |
lzma_options_delta opt_delta; /* delta filter options */
|
|
Packit |
7838c8 |
lzma_options_lzma opt_lzma; /* LZMA2 filter options */
|
|
Packit |
7838c8 |
int preset; /* compression level */
|
|
Packit |
7838c8 |
lzma_check check; /* type of the integrity check */
|
|
Packit |
7838c8 |
int state; /* state flags */
|
|
Packit |
7838c8 |
#define LSTATE_INIT_DECODE 0x01
|
|
Packit |
7838c8 |
#define LSTATE_INIT_ENCODE 0x02
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
TIFFVGetMethod vgetparent; /* super-class method */
|
|
Packit |
7838c8 |
TIFFVSetMethod vsetparent; /* super-class method */
|
|
Packit |
7838c8 |
} LZMAState;
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
#define LState(tif) ((LZMAState*) (tif)->tif_data)
|
|
Packit |
7838c8 |
#define DecoderState(tif) LState(tif)
|
|
Packit |
7838c8 |
#define EncoderState(tif) LState(tif)
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
static int LZMAEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
|
|
Packit |
7838c8 |
static int LZMADecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s);
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
static const char *
|
|
Packit |
7838c8 |
LZMAStrerror(lzma_ret ret)
|
|
Packit |
7838c8 |
{
|
|
Packit |
7838c8 |
switch (ret) {
|
|
Packit |
7838c8 |
case LZMA_OK:
|
|
Packit |
7838c8 |
return "operation completed successfully";
|
|
Packit |
7838c8 |
case LZMA_STREAM_END:
|
|
Packit |
7838c8 |
return "end of stream was reached";
|
|
Packit |
7838c8 |
case LZMA_NO_CHECK:
|
|
Packit |
7838c8 |
return "input stream has no integrity check";
|
|
Packit |
7838c8 |
case LZMA_UNSUPPORTED_CHECK:
|
|
Packit |
7838c8 |
return "cannot calculate the integrity check";
|
|
Packit |
7838c8 |
case LZMA_GET_CHECK:
|
|
Packit |
7838c8 |
return "integrity check type is now available";
|
|
Packit |
7838c8 |
case LZMA_MEM_ERROR:
|
|
Packit |
7838c8 |
return "cannot allocate memory";
|
|
Packit |
7838c8 |
case LZMA_MEMLIMIT_ERROR:
|
|
Packit |
7838c8 |
return "memory usage limit was reached";
|
|
Packit |
7838c8 |
case LZMA_FORMAT_ERROR:
|
|
Packit |
7838c8 |
return "file format not recognized";
|
|
Packit |
7838c8 |
case LZMA_OPTIONS_ERROR:
|
|
Packit |
7838c8 |
return "invalid or unsupported options";
|
|
Packit |
7838c8 |
case LZMA_DATA_ERROR:
|
|
Packit |
7838c8 |
return "data is corrupt";
|
|
Packit |
7838c8 |
case LZMA_BUF_ERROR:
|
|
Packit |
7838c8 |
return "no progress is possible (stream is truncated or corrupt)";
|
|
Packit |
7838c8 |
case LZMA_PROG_ERROR:
|
|
Packit |
7838c8 |
return "programming error";
|
|
Packit |
7838c8 |
default:
|
|
Packit |
7838c8 |
return "unidentified liblzma error";
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
static int
|
|
Packit |
7838c8 |
LZMAFixupTags(TIFF* tif)
|
|
Packit |
7838c8 |
{
|
|
Packit |
7838c8 |
(void) tif;
|
|
Packit |
7838c8 |
return 1;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
static int
|
|
Packit |
7838c8 |
LZMASetupDecode(TIFF* tif)
|
|
Packit |
7838c8 |
{
|
|
Packit |
7838c8 |
LZMAState* sp = DecoderState(tif);
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
assert(sp != NULL);
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
/* if we were last encoding, terminate this mode */
|
|
Packit |
7838c8 |
if (sp->state & LSTATE_INIT_ENCODE) {
|
|
Packit |
7838c8 |
lzma_end(&sp->stream);
|
|
Packit |
7838c8 |
sp->state = 0;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
sp->state |= LSTATE_INIT_DECODE;
|
|
Packit |
7838c8 |
return 1;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
/*
|
|
Packit |
7838c8 |
* Setup state for decoding a strip.
|
|
Packit |
7838c8 |
*/
|
|
Packit |
7838c8 |
static int
|
|
Packit |
7838c8 |
LZMAPreDecode(TIFF* tif, uint16 s)
|
|
Packit |
7838c8 |
{
|
|
Packit |
7838c8 |
static const char module[] = "LZMAPreDecode";
|
|
Packit |
7838c8 |
LZMAState* sp = DecoderState(tif);
|
|
Packit |
7838c8 |
lzma_ret ret;
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
(void) s;
|
|
Packit |
7838c8 |
assert(sp != NULL);
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
if( (sp->state & LSTATE_INIT_DECODE) == 0 )
|
|
Packit |
7838c8 |
tif->tif_setupdecode(tif);
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
sp->stream.next_in = tif->tif_rawdata;
|
|
Packit |
7838c8 |
sp->stream.avail_in = (size_t) tif->tif_rawcc;
|
|
Packit |
7838c8 |
if ((tmsize_t)sp->stream.avail_in != tif->tif_rawcc) {
|
|
Packit |
7838c8 |
TIFFErrorExt(tif->tif_clientdata, module,
|
|
Packit |
7838c8 |
"Liblzma cannot deal with buffers this size");
|
|
Packit |
7838c8 |
return 0;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
/*
|
|
Packit |
7838c8 |
* Disable memory limit when decoding. UINT64_MAX is a flag to disable
|
|
Packit |
7838c8 |
* the limit, we are passing (uint64_t)-1 which should be the same.
|
|
Packit |
7838c8 |
*/
|
|
Packit |
7838c8 |
ret = lzma_stream_decoder(&sp->stream, (uint64_t)-1, 0);
|
|
Packit |
7838c8 |
if (ret != LZMA_OK) {
|
|
Packit |
7838c8 |
TIFFErrorExt(tif->tif_clientdata, module,
|
|
Packit |
7838c8 |
"Error initializing the stream decoder, %s",
|
|
Packit |
7838c8 |
LZMAStrerror(ret));
|
|
Packit |
7838c8 |
return 0;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
return 1;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
static int
|
|
Packit |
7838c8 |
LZMADecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
|
|
Packit |
7838c8 |
{
|
|
Packit |
7838c8 |
static const char module[] = "LZMADecode";
|
|
Packit |
7838c8 |
LZMAState* sp = DecoderState(tif);
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
(void) s;
|
|
Packit |
7838c8 |
assert(sp != NULL);
|
|
Packit |
7838c8 |
assert(sp->state == LSTATE_INIT_DECODE);
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
sp->stream.next_in = tif->tif_rawcp;
|
|
Packit |
7838c8 |
sp->stream.avail_in = (size_t) tif->tif_rawcc;
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
sp->stream.next_out = op;
|
|
Packit |
7838c8 |
sp->stream.avail_out = (size_t) occ;
|
|
Packit |
7838c8 |
if ((tmsize_t)sp->stream.avail_out != occ) {
|
|
Packit |
7838c8 |
TIFFErrorExt(tif->tif_clientdata, module,
|
|
Packit |
7838c8 |
"Liblzma cannot deal with buffers this size");
|
|
Packit |
7838c8 |
return 0;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
do {
|
|
Packit |
7838c8 |
/*
|
|
Packit |
7838c8 |
* Save the current stream state to properly recover from the
|
|
Packit |
7838c8 |
* decoding errors later.
|
|
Packit |
7838c8 |
*/
|
|
Packit |
7838c8 |
const uint8_t *next_in = sp->stream.next_in;
|
|
Packit |
7838c8 |
size_t avail_in = sp->stream.avail_in;
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
lzma_ret ret = lzma_code(&sp->stream, LZMA_RUN);
|
|
Packit |
7838c8 |
if (ret == LZMA_STREAM_END)
|
|
Packit |
7838c8 |
break;
|
|
Packit |
7838c8 |
if (ret == LZMA_MEMLIMIT_ERROR) {
|
|
Packit |
7838c8 |
lzma_ret r = lzma_stream_decoder(&sp->stream,
|
|
Packit |
7838c8 |
lzma_memusage(&sp->stream), 0);
|
|
Packit |
7838c8 |
if (r != LZMA_OK) {
|
|
Packit |
7838c8 |
TIFFErrorExt(tif->tif_clientdata, module,
|
|
Packit |
7838c8 |
"Error initializing the stream decoder, %s",
|
|
Packit |
7838c8 |
LZMAStrerror(r));
|
|
Packit |
7838c8 |
break;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
sp->stream.next_in = next_in;
|
|
Packit |
7838c8 |
sp->stream.avail_in = avail_in;
|
|
Packit |
7838c8 |
continue;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
if (ret != LZMA_OK) {
|
|
Packit |
7838c8 |
TIFFErrorExt(tif->tif_clientdata, module,
|
|
Packit |
7838c8 |
"Decoding error at scanline %lu, %s",
|
|
Packit |
7838c8 |
(unsigned long) tif->tif_row, LZMAStrerror(ret));
|
|
Packit |
7838c8 |
break;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
} while (sp->stream.avail_out > 0);
|
|
Packit |
7838c8 |
if (sp->stream.avail_out != 0) {
|
|
Packit |
7838c8 |
TIFFErrorExt(tif->tif_clientdata, module,
|
|
Packit |
7838c8 |
"Not enough data at scanline %lu (short %lu bytes)",
|
|
Packit |
7838c8 |
(unsigned long) tif->tif_row, (unsigned long) sp->stream.avail_out);
|
|
Packit |
7838c8 |
return 0;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
tif->tif_rawcp = (uint8 *)sp->stream.next_in; /* cast away const */
|
|
Packit |
7838c8 |
tif->tif_rawcc = sp->stream.avail_in;
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
return 1;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
static int
|
|
Packit |
7838c8 |
LZMASetupEncode(TIFF* tif)
|
|
Packit |
7838c8 |
{
|
|
Packit |
7838c8 |
LZMAState* sp = EncoderState(tif);
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
assert(sp != NULL);
|
|
Packit |
7838c8 |
if (sp->state & LSTATE_INIT_DECODE) {
|
|
Packit |
7838c8 |
lzma_end(&sp->stream);
|
|
Packit |
7838c8 |
sp->state = 0;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
sp->state |= LSTATE_INIT_ENCODE;
|
|
Packit |
7838c8 |
return 1;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
/*
|
|
Packit |
7838c8 |
* Reset encoding state at the start of a strip.
|
|
Packit |
7838c8 |
*/
|
|
Packit |
7838c8 |
static int
|
|
Packit |
7838c8 |
LZMAPreEncode(TIFF* tif, uint16 s)
|
|
Packit |
7838c8 |
{
|
|
Packit |
7838c8 |
static const char module[] = "LZMAPreEncode";
|
|
Packit |
7838c8 |
LZMAState *sp = EncoderState(tif);
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
(void) s;
|
|
Packit |
7838c8 |
assert(sp != NULL);
|
|
Packit |
7838c8 |
if( sp->state != LSTATE_INIT_ENCODE )
|
|
Packit |
7838c8 |
tif->tif_setupencode(tif);
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
sp->stream.next_out = tif->tif_rawdata;
|
|
Packit |
7838c8 |
sp->stream.avail_out = (size_t)tif->tif_rawdatasize;
|
|
Packit |
7838c8 |
if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) {
|
|
Packit |
7838c8 |
TIFFErrorExt(tif->tif_clientdata, module,
|
|
Packit |
7838c8 |
"Liblzma cannot deal with buffers this size");
|
|
Packit |
7838c8 |
return 0;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
return (lzma_stream_encoder(&sp->stream, sp->filters, sp->check) == LZMA_OK);
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
/*
|
|
Packit |
7838c8 |
* Encode a chunk of pixels.
|
|
Packit |
7838c8 |
*/
|
|
Packit |
7838c8 |
static int
|
|
Packit |
7838c8 |
LZMAEncode(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
|
|
Packit |
7838c8 |
{
|
|
Packit |
7838c8 |
static const char module[] = "LZMAEncode";
|
|
Packit |
7838c8 |
LZMAState *sp = EncoderState(tif);
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
assert(sp != NULL);
|
|
Packit |
7838c8 |
assert(sp->state == LSTATE_INIT_ENCODE);
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
(void) s;
|
|
Packit |
7838c8 |
sp->stream.next_in = bp;
|
|
Packit |
7838c8 |
sp->stream.avail_in = (size_t) cc;
|
|
Packit |
7838c8 |
if ((tmsize_t)sp->stream.avail_in != cc) {
|
|
Packit |
7838c8 |
TIFFErrorExt(tif->tif_clientdata, module,
|
|
Packit |
7838c8 |
"Liblzma cannot deal with buffers this size");
|
|
Packit |
7838c8 |
return 0;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
do {
|
|
Packit |
7838c8 |
lzma_ret ret = lzma_code(&sp->stream, LZMA_RUN);
|
|
Packit |
7838c8 |
if (ret != LZMA_OK) {
|
|
Packit |
7838c8 |
TIFFErrorExt(tif->tif_clientdata, module,
|
|
Packit |
7838c8 |
"Encoding error at scanline %lu, %s",
|
|
Packit |
7838c8 |
(unsigned long) tif->tif_row, LZMAStrerror(ret));
|
|
Packit |
7838c8 |
return 0;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
if (sp->stream.avail_out == 0) {
|
|
Packit |
7838c8 |
tif->tif_rawcc = tif->tif_rawdatasize;
|
|
Packit |
7838c8 |
TIFFFlushData1(tif);
|
|
Packit |
7838c8 |
sp->stream.next_out = tif->tif_rawdata;
|
|
Packit |
7838c8 |
sp->stream.avail_out = (size_t)tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in LZMAPreEncode */
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
} while (sp->stream.avail_in > 0);
|
|
Packit |
7838c8 |
return 1;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
/*
|
|
Packit |
7838c8 |
* Finish off an encoded strip by flushing the last
|
|
Packit |
7838c8 |
* string and tacking on an End Of Information code.
|
|
Packit |
7838c8 |
*/
|
|
Packit |
7838c8 |
static int
|
|
Packit |
7838c8 |
LZMAPostEncode(TIFF* tif)
|
|
Packit |
7838c8 |
{
|
|
Packit |
7838c8 |
static const char module[] = "LZMAPostEncode";
|
|
Packit |
7838c8 |
LZMAState *sp = EncoderState(tif);
|
|
Packit |
7838c8 |
lzma_ret ret;
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
sp->stream.avail_in = 0;
|
|
Packit |
7838c8 |
do {
|
|
Packit |
7838c8 |
ret = lzma_code(&sp->stream, LZMA_FINISH);
|
|
Packit |
7838c8 |
switch (ret) {
|
|
Packit |
7838c8 |
case LZMA_STREAM_END:
|
|
Packit |
7838c8 |
case LZMA_OK:
|
|
Packit |
7838c8 |
if ((tmsize_t)sp->stream.avail_out != tif->tif_rawdatasize) {
|
|
Packit |
7838c8 |
tif->tif_rawcc =
|
|
Packit |
7838c8 |
tif->tif_rawdatasize - sp->stream.avail_out;
|
|
Packit |
7838c8 |
TIFFFlushData1(tif);
|
|
Packit |
7838c8 |
sp->stream.next_out = tif->tif_rawdata;
|
|
Packit |
7838c8 |
sp->stream.avail_out = (size_t)tif->tif_rawdatasize; /* this is a safe typecast, as check is made already in ZIPPreEncode */
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
break;
|
|
Packit |
7838c8 |
default:
|
|
Packit |
7838c8 |
TIFFErrorExt(tif->tif_clientdata, module, "Liblzma error: %s",
|
|
Packit |
7838c8 |
LZMAStrerror(ret));
|
|
Packit |
7838c8 |
return 0;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
} while (ret != LZMA_STREAM_END);
|
|
Packit |
7838c8 |
return 1;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
static void
|
|
Packit |
7838c8 |
LZMACleanup(TIFF* tif)
|
|
Packit |
7838c8 |
{
|
|
Packit |
7838c8 |
LZMAState* sp = LState(tif);
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
assert(sp != 0);
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
(void)TIFFPredictorCleanup(tif);
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
tif->tif_tagmethods.vgetfield = sp->vgetparent;
|
|
Packit |
7838c8 |
tif->tif_tagmethods.vsetfield = sp->vsetparent;
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
if (sp->state) {
|
|
Packit |
7838c8 |
lzma_end(&sp->stream);
|
|
Packit |
7838c8 |
sp->state = 0;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
_TIFFfree(sp);
|
|
Packit |
7838c8 |
tif->tif_data = NULL;
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
_TIFFSetDefaultCompressionState(tif);
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
static int
|
|
Packit |
7838c8 |
LZMAVSetField(TIFF* tif, uint32 tag, va_list ap)
|
|
Packit |
7838c8 |
{
|
|
Packit |
7838c8 |
static const char module[] = "LZMAVSetField";
|
|
Packit |
7838c8 |
LZMAState* sp = LState(tif);
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
switch (tag) {
|
|
Packit |
7838c8 |
case TIFFTAG_LZMAPRESET:
|
|
Packit |
7838c8 |
sp->preset = (int) va_arg(ap, int);
|
|
Packit |
7838c8 |
lzma_lzma_preset(&sp->opt_lzma, sp->preset);
|
|
Packit |
7838c8 |
if (sp->state & LSTATE_INIT_ENCODE) {
|
|
Packit |
7838c8 |
lzma_ret ret = lzma_stream_encoder(&sp->stream,
|
|
Packit |
7838c8 |
sp->filters,
|
|
Packit |
7838c8 |
sp->check);
|
|
Packit |
7838c8 |
if (ret != LZMA_OK) {
|
|
Packit |
7838c8 |
TIFFErrorExt(tif->tif_clientdata, module,
|
|
Packit |
7838c8 |
"Liblzma error: %s",
|
|
Packit |
7838c8 |
LZMAStrerror(ret));
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
return 1;
|
|
Packit |
7838c8 |
default:
|
|
Packit |
7838c8 |
return (*sp->vsetparent)(tif, tag, ap);
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
/*NOTREACHED*/
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
static int
|
|
Packit |
7838c8 |
LZMAVGetField(TIFF* tif, uint32 tag, va_list ap)
|
|
Packit |
7838c8 |
{
|
|
Packit |
7838c8 |
LZMAState* sp = LState(tif);
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
switch (tag) {
|
|
Packit |
7838c8 |
case TIFFTAG_LZMAPRESET:
|
|
Packit |
7838c8 |
*va_arg(ap, int*) = sp->preset;
|
|
Packit |
7838c8 |
break;
|
|
Packit |
7838c8 |
default:
|
|
Packit |
7838c8 |
return (*sp->vgetparent)(tif, tag, ap);
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
return 1;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
static const TIFFField lzmaFields[] = {
|
|
Packit |
7838c8 |
{ TIFFTAG_LZMAPRESET, 0, 0, TIFF_ANY, 0, TIFF_SETGET_INT, TIFF_SETGET_UNDEFINED,
|
|
Packit |
7838c8 |
FIELD_PSEUDO, TRUE, FALSE, "LZMA2 Compression Preset", NULL },
|
|
Packit |
7838c8 |
};
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
int
|
|
Packit |
7838c8 |
TIFFInitLZMA(TIFF* tif, int scheme)
|
|
Packit |
7838c8 |
{
|
|
Packit |
7838c8 |
static const char module[] = "TIFFInitLZMA";
|
|
Packit |
7838c8 |
LZMAState* sp;
|
|
Packit |
7838c8 |
lzma_stream tmp_stream = LZMA_STREAM_INIT;
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
assert( scheme == COMPRESSION_LZMA );
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
/*
|
|
Packit |
7838c8 |
* Merge codec-specific tag information.
|
|
Packit |
7838c8 |
*/
|
|
Packit |
7838c8 |
if (!_TIFFMergeFields(tif, lzmaFields, TIFFArrayCount(lzmaFields))) {
|
|
Packit |
7838c8 |
TIFFErrorExt(tif->tif_clientdata, module,
|
|
Packit |
7838c8 |
"Merging LZMA2 codec-specific tags failed");
|
|
Packit |
7838c8 |
return 0;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
/*
|
|
Packit |
7838c8 |
* Allocate state block so tag methods have storage to record values.
|
|
Packit |
7838c8 |
*/
|
|
Packit |
7838c8 |
tif->tif_data = (uint8*) _TIFFmalloc(sizeof(LZMAState));
|
|
Packit |
7838c8 |
if (tif->tif_data == NULL)
|
|
Packit |
7838c8 |
goto bad;
|
|
Packit |
7838c8 |
sp = LState(tif);
|
|
Packit |
7838c8 |
memcpy(&sp->stream, &tmp_stream, sizeof(lzma_stream));
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
/*
|
|
Packit |
7838c8 |
* Override parent get/set field methods.
|
|
Packit |
7838c8 |
*/
|
|
Packit |
7838c8 |
sp->vgetparent = tif->tif_tagmethods.vgetfield;
|
|
Packit |
7838c8 |
tif->tif_tagmethods.vgetfield = LZMAVGetField; /* hook for codec tags */
|
|
Packit |
7838c8 |
sp->vsetparent = tif->tif_tagmethods.vsetfield;
|
|
Packit |
7838c8 |
tif->tif_tagmethods.vsetfield = LZMAVSetField; /* hook for codec tags */
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
/* Default values for codec-specific fields */
|
|
Packit |
7838c8 |
sp->preset = LZMA_PRESET_DEFAULT; /* default comp. level */
|
|
Packit |
7838c8 |
sp->check = LZMA_CHECK_NONE;
|
|
Packit |
7838c8 |
sp->state = 0;
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
/* Data filters. So far we are using delta and LZMA2 filters only. */
|
|
Packit |
7838c8 |
sp->opt_delta.type = LZMA_DELTA_TYPE_BYTE;
|
|
Packit |
7838c8 |
/*
|
|
Packit |
7838c8 |
* The sample size in bytes seems to be reasonable distance for delta
|
|
Packit |
7838c8 |
* filter.
|
|
Packit |
7838c8 |
*/
|
|
Packit |
7838c8 |
sp->opt_delta.dist = (tif->tif_dir.td_bitspersample % 8) ?
|
|
Packit |
7838c8 |
1 : tif->tif_dir.td_bitspersample / 8;
|
|
Packit |
7838c8 |
sp->filters[0].id = LZMA_FILTER_DELTA;
|
|
Packit |
7838c8 |
sp->filters[0].options = &sp->opt_delta;
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
lzma_lzma_preset(&sp->opt_lzma, sp->preset);
|
|
Packit |
7838c8 |
sp->filters[1].id = LZMA_FILTER_LZMA2;
|
|
Packit |
7838c8 |
sp->filters[1].options = &sp->opt_lzma;
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
sp->filters[2].id = LZMA_VLI_UNKNOWN;
|
|
Packit |
7838c8 |
sp->filters[2].options = NULL;
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
/*
|
|
Packit |
7838c8 |
* Install codec methods.
|
|
Packit |
7838c8 |
*/
|
|
Packit |
7838c8 |
tif->tif_fixuptags = LZMAFixupTags;
|
|
Packit |
7838c8 |
tif->tif_setupdecode = LZMASetupDecode;
|
|
Packit |
7838c8 |
tif->tif_predecode = LZMAPreDecode;
|
|
Packit |
7838c8 |
tif->tif_decoderow = LZMADecode;
|
|
Packit |
7838c8 |
tif->tif_decodestrip = LZMADecode;
|
|
Packit |
7838c8 |
tif->tif_decodetile = LZMADecode;
|
|
Packit |
7838c8 |
tif->tif_setupencode = LZMASetupEncode;
|
|
Packit |
7838c8 |
tif->tif_preencode = LZMAPreEncode;
|
|
Packit |
7838c8 |
tif->tif_postencode = LZMAPostEncode;
|
|
Packit |
7838c8 |
tif->tif_encoderow = LZMAEncode;
|
|
Packit |
7838c8 |
tif->tif_encodestrip = LZMAEncode;
|
|
Packit |
7838c8 |
tif->tif_encodetile = LZMAEncode;
|
|
Packit |
7838c8 |
tif->tif_cleanup = LZMACleanup;
|
|
Packit |
7838c8 |
/*
|
|
Packit |
7838c8 |
* Setup predictor setup.
|
|
Packit |
7838c8 |
*/
|
|
Packit |
7838c8 |
(void) TIFFPredictorInit(tif);
|
|
Packit |
7838c8 |
return 1;
|
|
Packit |
7838c8 |
bad:
|
|
Packit |
7838c8 |
TIFFErrorExt(tif->tif_clientdata, module,
|
|
Packit |
7838c8 |
"No space for LZMA2 state block");
|
|
Packit |
7838c8 |
return 0;
|
|
Packit |
7838c8 |
}
|
|
Packit |
7838c8 |
#endif /* LZMA_SUPPORT */
|
|
Packit |
7838c8 |
|
|
Packit |
7838c8 |
/* vim: set ts=8 sts=8 sw=8 noet: */
|