Blame libtiff/tif_jbig.c

Packit 7838c8
/* $Id: tif_jbig.c,v 1.16 2017-06-26 15:20:00 erouault Exp $ */
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Copyright (c) 1988-1997 Sam Leffler
Packit 7838c8
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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
/*
Packit 7838c8
 * TIFF Library.
Packit 7838c8
 *
Packit 7838c8
 * JBIG Compression Algorithm Support.
Packit 7838c8
 * Contributed by Lee Howard <faxguy@deanox.com>
Packit 7838c8
 *
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
#include "tiffiop.h"
Packit 7838c8
Packit 7838c8
#ifdef JBIG_SUPPORT
Packit 7838c8
#include "jbig.h"
Packit 7838c8
Packit 7838c8
static int JBIGSetupDecode(TIFF* tif)
Packit 7838c8
{
Packit 7838c8
	if (TIFFNumberOfStrips(tif) != 1)
Packit 7838c8
	{
Packit 7838c8
		TIFFErrorExt(tif->tif_clientdata, "JBIG", "Multistrip images not supported in decoder");
Packit 7838c8
		return 0;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	return 1;
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static int JBIGDecode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
Packit 7838c8
{
Packit 7838c8
	struct jbg_dec_state decoder;
Packit 7838c8
	int decodeStatus = 0;
Packit 7838c8
	unsigned char* pImage = NULL;
Packit 7838c8
	(void) size, (void) s;
Packit 7838c8
Packit 7838c8
	if (isFillOrder(tif, tif->tif_dir.td_fillorder))
Packit 7838c8
	{
Packit 7838c8
		TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdatasize);
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	jbg_dec_init(&decoder);
Packit 7838c8
Packit 7838c8
#if defined(HAVE_JBG_NEWLEN)
Packit 7838c8
	jbg_newlen(tif->tif_rawdata, (size_t)tif->tif_rawdatasize);
Packit 7838c8
	/*
Packit 7838c8
	 * I do not check the return status of jbg_newlen because even if this
Packit 7838c8
	 * function fails it does not necessarily mean that decoding the image
Packit 7838c8
	 * will fail.  It is generally only needed for received fax images
Packit 7838c8
	 * that do not contain the actual length of the image in the BIE
Packit 7838c8
	 * header.  I do not log when an error occurs because that will cause
Packit 7838c8
	 * problems when converting JBIG encoded TIFF's to
Packit 7838c8
	 * PostScript.  As long as the actual image length is contained in the
Packit 7838c8
	 * BIE header jbg_dec_in should succeed.
Packit 7838c8
	 */
Packit 7838c8
#endif /* HAVE_JBG_NEWLEN */
Packit 7838c8
Packit 7838c8
	decodeStatus = jbg_dec_in(&decoder, (unsigned char*)tif->tif_rawdata,
Packit 7838c8
				  (size_t)tif->tif_rawdatasize, NULL);
Packit 7838c8
	if (JBG_EOK != decodeStatus)
Packit 7838c8
	{
Packit 7838c8
		/*
Packit 7838c8
		 * XXX: JBG_EN constant was defined in pre-2.0 releases of the
Packit 7838c8
		 * JBIG-KIT. Since the 2.0 the error reporting functions were
Packit 7838c8
		 * changed. We will handle both cases here.
Packit 7838c8
		 */
Packit 7838c8
		TIFFErrorExt(tif->tif_clientdata,
Packit 7838c8
			     "JBIG", "Error (%d) decoding: %s",
Packit 7838c8
			     decodeStatus,
Packit 7838c8
#if defined(JBG_EN)
Packit 7838c8
			     jbg_strerror(decodeStatus, JBG_EN)
Packit 7838c8
#else
Packit 7838c8
			     jbg_strerror(decodeStatus)
Packit 7838c8
#endif
Packit 7838c8
			     );
Packit 7838c8
		jbg_dec_free(&decoder);
Packit 7838c8
		return 0;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	pImage = jbg_dec_getimage(&decoder, 0);
Packit 7838c8
	_TIFFmemcpy(buffer, pImage, jbg_dec_getsize(&decoder));
Packit 7838c8
	jbg_dec_free(&decoder);
Packit 7838c8
	return 1;
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static int JBIGSetupEncode(TIFF* tif)
Packit 7838c8
{
Packit 7838c8
	if (TIFFNumberOfStrips(tif) != 1)
Packit 7838c8
	{
Packit 7838c8
		TIFFErrorExt(tif->tif_clientdata, "JBIG", "Multistrip images not supported in encoder");
Packit 7838c8
		return 0;
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	return 1;
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static int JBIGCopyEncodedData(TIFF* tif, unsigned char* pp, size_t cc, uint16 s)
Packit 7838c8
{
Packit 7838c8
	(void) s;
Packit 7838c8
	while (cc > 0)
Packit 7838c8
	{
Packit 7838c8
		tmsize_t n = (tmsize_t)cc;
Packit 7838c8
Packit 7838c8
		if (tif->tif_rawcc + n > tif->tif_rawdatasize)
Packit 7838c8
		{
Packit 7838c8
			n = tif->tif_rawdatasize - tif->tif_rawcc;
Packit 7838c8
		}
Packit 7838c8
Packit 7838c8
		assert(n > 0);
Packit 7838c8
		_TIFFmemcpy(tif->tif_rawcp, pp, n);
Packit 7838c8
		tif->tif_rawcp += n;
Packit 7838c8
		tif->tif_rawcc += n;
Packit 7838c8
		pp += n;
Packit 7838c8
		cc -= (size_t)n;
Packit 7838c8
		if (tif->tif_rawcc >= tif->tif_rawdatasize &&
Packit 7838c8
		    !TIFFFlushData1(tif))
Packit 7838c8
		{
Packit 7838c8
			return (-1);
Packit 7838c8
		}
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	return (1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static void JBIGOutputBie(unsigned char* buffer, size_t len, void* userData)
Packit 7838c8
{
Packit 7838c8
	TIFF* tif = (TIFF*)userData;
Packit 7838c8
Packit 7838c8
	if (isFillOrder(tif, tif->tif_dir.td_fillorder))
Packit 7838c8
	{
Packit 7838c8
		TIFFReverseBits(buffer, (tmsize_t)len);
Packit 7838c8
	}
Packit 7838c8
Packit 7838c8
	JBIGCopyEncodedData(tif, buffer, len, 0);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
static int JBIGEncode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
Packit 7838c8
{
Packit 7838c8
	TIFFDirectory* dir = &tif->tif_dir;
Packit 7838c8
	struct jbg_enc_state encoder;
Packit 7838c8
Packit 7838c8
	(void) size, (void) s;
Packit 7838c8
Packit 7838c8
	jbg_enc_init(&encoder,
Packit 7838c8
		     dir->td_imagewidth,
Packit 7838c8
		     dir->td_imagelength,
Packit 7838c8
		     1,
Packit 7838c8
		     &buffer,
Packit 7838c8
		     JBIGOutputBie,
Packit 7838c8
		     tif);
Packit 7838c8
	/*
Packit 7838c8
	 * jbg_enc_out does the "real" encoding.  As data is encoded,
Packit 7838c8
	 * JBIGOutputBie is called, which writes the data to the directory.
Packit 7838c8
	 */
Packit 7838c8
	jbg_enc_out(&encoder);
Packit 7838c8
	jbg_enc_free(&encoder);
Packit 7838c8
Packit 7838c8
	return 1;
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
int TIFFInitJBIG(TIFF* tif, int scheme)
Packit 7838c8
{
Packit 7838c8
	assert(scheme == COMPRESSION_JBIG);
Packit 7838c8
Packit 7838c8
	/*
Packit 7838c8
	 * These flags are set so the JBIG Codec can control when to reverse
Packit 7838c8
	 * bits and when not to and to allow the jbig decoder and bit reverser
Packit 7838c8
	 * to write to memory when necessary.
Packit 7838c8
	 */
Packit 7838c8
	tif->tif_flags |= TIFF_NOBITREV;
Packit 7838c8
	tif->tif_flags &= ~TIFF_MAPPED;
Packit 7838c8
Packit 7838c8
	/* Setup the function pointers for encode, decode, and cleanup. */
Packit 7838c8
	tif->tif_setupdecode = JBIGSetupDecode;
Packit 7838c8
	tif->tif_decodestrip = JBIGDecode;
Packit 7838c8
Packit 7838c8
	tif->tif_setupencode = JBIGSetupEncode;
Packit 7838c8
	tif->tif_encodestrip = JBIGEncode;
Packit 7838c8
Packit 7838c8
	return 1;
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
#endif /* JBIG_SUPPORT */
Packit 7838c8
Packit 7838c8
/* vim: set ts=8 sts=8 sw=8 noet: */
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Local Variables:
Packit 7838c8
 * mode: c
Packit 7838c8
 * c-basic-offset: 8
Packit 7838c8
 * fill-column: 78
Packit 7838c8
 * End:
Packit 7838c8
 */