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