This source file includes following definitions.
- JBIGSetupDecode
- JBIGDecode
- JBIGSetupEncode
- JBIGCopyEncodedData
- JBIGOutputBie
- JBIGEncode
- TIFFInitJBIG
#include "tiffiop.h"
#ifdef JBIG_SUPPORT
#include "jbig.h"
static int JBIGSetupDecode(TIFF* tif)
{
if (TIFFNumberOfStrips(tif) != 1)
{
TIFFErrorExt(tif->tif_clientdata, "JBIG", "Multistrip images not supported in decoder");
return 0;
}
return 1;
}
static int JBIGDecode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
{
struct jbg_dec_state decoder;
int decodeStatus = 0;
unsigned char* pImage = NULL;
(void) size, (void) s;
if (isFillOrder(tif, tif->tif_dir.td_fillorder))
{
TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdatasize);
}
jbg_dec_init(&decoder);
#if defined(HAVE_JBG_NEWLEN)
jbg_newlen(tif->tif_rawdata, (size_t)tif->tif_rawdatasize);
#endif
decodeStatus = jbg_dec_in(&decoder, (unsigned char*)tif->tif_rawdata,
(size_t)tif->tif_rawdatasize, NULL);
if (JBG_EOK != decodeStatus)
{
TIFFErrorExt(tif->tif_clientdata,
"JBIG", "Error (%d) decoding: %s",
decodeStatus,
#if defined(JBG_EN)
jbg_strerror(decodeStatus, JBG_EN)
#else
jbg_strerror(decodeStatus)
#endif
);
return 0;
}
pImage = jbg_dec_getimage(&decoder, 0);
_TIFFmemcpy(buffer, pImage, jbg_dec_getsize(&decoder));
jbg_dec_free(&decoder);
return 1;
}
static int JBIGSetupEncode(TIFF* tif)
{
if (TIFFNumberOfStrips(tif) != 1)
{
TIFFErrorExt(tif->tif_clientdata, "JBIG", "Multistrip images not supported in encoder");
return 0;
}
return 1;
}
static int JBIGCopyEncodedData(TIFF* tif, unsigned char* pp, size_t cc, uint16 s)
{
(void) s;
while (cc > 0)
{
tmsize_t n = (tmsize_t)cc;
if (tif->tif_rawcc + n > tif->tif_rawdatasize)
{
n = tif->tif_rawdatasize - tif->tif_rawcc;
}
assert(n > 0);
_TIFFmemcpy(tif->tif_rawcp, pp, n);
tif->tif_rawcp += n;
tif->tif_rawcc += n;
pp += n;
cc -= (size_t)n;
if (tif->tif_rawcc >= tif->tif_rawdatasize &&
!TIFFFlushData1(tif))
{
return (-1);
}
}
return (1);
}
static void JBIGOutputBie(unsigned char* buffer, size_t len, void* userData)
{
TIFF* tif = (TIFF*)userData;
if (isFillOrder(tif, tif->tif_dir.td_fillorder))
{
TIFFReverseBits(buffer, (tmsize_t)len);
}
JBIGCopyEncodedData(tif, buffer, len, 0);
}
static int JBIGEncode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
{
TIFFDirectory* dir = &tif->tif_dir;
struct jbg_enc_state encoder;
(void) size, (void) s;
jbg_enc_init(&encoder,
dir->td_imagewidth,
dir->td_imagelength,
1,
&buffer,
JBIGOutputBie,
tif);
jbg_enc_out(&encoder);
jbg_enc_free(&encoder);
return 1;
}
int TIFFInitJBIG(TIFF* tif, int scheme)
{
assert(scheme == COMPRESSION_JBIG);
tif->tif_flags |= TIFF_NOBITREV;
tif->tif_flags &= ~TIFF_MAPPED;
tif->tif_setupdecode = JBIGSetupDecode;
tif->tif_decodestrip = JBIGDecode;
tif->tif_setupencode = JBIGSetupEncode;
tif->tif_encodestrip = JBIGEncode;
return 1;
}
#endif