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