Blame libtiff/tif_jbig.c

Packit 85355f
/* $Id: tif_jbig.c,v 1.16 2017-06-26 15:20:00 erouault Exp $ */
Packit 85355f
Packit 85355f
/*
Packit 85355f
 * Copyright (c) 1988-1997 Sam Leffler
Packit 85355f
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
Packit 85355f
 *
Packit 85355f
 * Permission to use, copy, modify, distribute, and sell this software and
Packit 85355f
 * its documentation for any purpose is hereby granted without fee, provided
Packit 85355f
 * that (i) the above copyright notices and this permission notice appear in
Packit 85355f
 * all copies of the software and related documentation, and (ii) the names of
Packit 85355f
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
Packit 85355f
 * publicity relating to the software without the specific, prior written
Packit 85355f
 * permission of Sam Leffler and Silicon Graphics.
Packit 85355f
 *
Packit 85355f
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
Packit 85355f
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
Packit 85355f
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
Packit 85355f
 *
Packit 85355f
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
Packit 85355f
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
Packit 85355f
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
Packit 85355f
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
Packit 85355f
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
Packit 85355f
 * OF THIS SOFTWARE.
Packit 85355f
 */
Packit 85355f
Packit 85355f
/*
Packit 85355f
 * TIFF Library.
Packit 85355f
 *
Packit 85355f
 * JBIG Compression Algorithm Support.
Packit 85355f
 * Contributed by Lee Howard <faxguy@deanox.com>
Packit 85355f
 *
Packit 85355f
 */
Packit 85355f
Packit 85355f
#include "tiffiop.h"
Packit 85355f
Packit 85355f
#ifdef JBIG_SUPPORT
Packit 85355f
#include "jbig.h"
Packit 85355f
Packit 85355f
static int JBIGSetupDecode(TIFF* tif)
Packit 85355f
{
Packit 85355f
	if (TIFFNumberOfStrips(tif) != 1)
Packit 85355f
	{
Packit 85355f
		TIFFErrorExt(tif->tif_clientdata, "JBIG", "Multistrip images not supported in decoder");
Packit 85355f
		return 0;
Packit 85355f
	}
Packit 85355f
Packit 85355f
	return 1;
Packit 85355f
}
Packit 85355f
Packit 85355f
static int JBIGDecode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
Packit 85355f
{
Packit 85355f
	struct jbg_dec_state decoder;
Packit 85355f
	int decodeStatus = 0;
Packit 85355f
	unsigned char* pImage = NULL;
Packit Service bac201
	unsigned long decodedSize;
Packit Service bac201
	(void) s;
Packit 85355f
Packit 85355f
	if (isFillOrder(tif, tif->tif_dir.td_fillorder))
Packit 85355f
	{
Packit Service bac201
		TIFFReverseBits(tif->tif_rawcp, tif->tif_rawcc);
Packit 85355f
	}
Packit 85355f
Packit 85355f
	jbg_dec_init(&decoder);
Packit 85355f
Packit 85355f
#if defined(HAVE_JBG_NEWLEN)
Packit Service bac201
	jbg_newlen(tif->tif_rawcp, (size_t)tif->tif_rawcc);
Packit 85355f
	/*
Packit 85355f
	 * I do not check the return status of jbg_newlen because even if this
Packit 85355f
	 * function fails it does not necessarily mean that decoding the image
Packit 85355f
	 * will fail.  It is generally only needed for received fax images
Packit 85355f
	 * that do not contain the actual length of the image in the BIE
Packit 85355f
	 * header.  I do not log when an error occurs because that will cause
Packit 85355f
	 * problems when converting JBIG encoded TIFF's to
Packit 85355f
	 * PostScript.  As long as the actual image length is contained in the
Packit 85355f
	 * BIE header jbg_dec_in should succeed.
Packit 85355f
	 */
Packit 85355f
#endif /* HAVE_JBG_NEWLEN */
Packit 85355f
Packit Service bac201
	decodeStatus = jbg_dec_in(&decoder, (unsigned char*)tif->tif_rawcp,
Packit Service bac201
				  (size_t)tif->tif_rawcc, NULL);
Packit 85355f
	if (JBG_EOK != decodeStatus)
Packit 85355f
	{
Packit 85355f
		/*
Packit 85355f
		 * XXX: JBG_EN constant was defined in pre-2.0 releases of the
Packit 85355f
		 * JBIG-KIT. Since the 2.0 the error reporting functions were
Packit 85355f
		 * changed. We will handle both cases here.
Packit 85355f
		 */
Packit 85355f
		TIFFErrorExt(tif->tif_clientdata,
Packit 85355f
			     "JBIG", "Error (%d) decoding: %s",
Packit 85355f
			     decodeStatus,
Packit 85355f
#if defined(JBG_EN)
Packit 85355f
			     jbg_strerror(decodeStatus, JBG_EN)
Packit 85355f
#else
Packit 85355f
			     jbg_strerror(decodeStatus)
Packit 85355f
#endif
Packit 85355f
			     );
Packit 85355f
		jbg_dec_free(&decoder);
Packit 85355f
		return 0;
Packit 85355f
	}
Packit 85355f
Packit Service bac201
	decodedSize = jbg_dec_getsize(&decoder);
Packit Service bac201
	if( (tmsize_t)decodedSize < size )
Packit Service bac201
	{
Packit Service bac201
	    TIFFWarningExt(tif->tif_clientdata, "JBIG",
Packit Service bac201
	                   "Only decoded %lu bytes, whereas %lu requested",
Packit Service bac201
	                   decodedSize, (unsigned long)size);
Packit Service bac201
	}
Packit Service bac201
	else if( (tmsize_t)decodedSize > size )
Packit Service bac201
	{
Packit Service bac201
	    TIFFErrorExt(tif->tif_clientdata, "JBIG",
Packit Service bac201
	                 "Decoded %lu bytes, whereas %lu were requested",
Packit Service bac201
	                 decodedSize, (unsigned long)size);
Packit Service bac201
	    jbg_dec_free(&decoder);
Packit Service bac201
	    return 0;
Packit Service bac201
	}
Packit 85355f
	pImage = jbg_dec_getimage(&decoder, 0);
Packit Service bac201
	_TIFFmemcpy(buffer, pImage, decodedSize);
Packit 85355f
	jbg_dec_free(&decoder);
Packit Service bac201
Packit Service bac201
        tif->tif_rawcp += tif->tif_rawcc;
Packit Service bac201
        tif->tif_rawcc = 0;
Packit Service bac201
Packit 85355f
	return 1;
Packit 85355f
}
Packit 85355f
Packit 85355f
static int JBIGSetupEncode(TIFF* tif)
Packit 85355f
{
Packit 85355f
	if (TIFFNumberOfStrips(tif) != 1)
Packit 85355f
	{
Packit 85355f
		TIFFErrorExt(tif->tif_clientdata, "JBIG", "Multistrip images not supported in encoder");
Packit 85355f
		return 0;
Packit 85355f
	}
Packit 85355f
Packit 85355f
	return 1;
Packit 85355f
}
Packit 85355f
Packit 85355f
static int JBIGCopyEncodedData(TIFF* tif, unsigned char* pp, size_t cc, uint16 s)
Packit 85355f
{
Packit 85355f
	(void) s;
Packit 85355f
	while (cc > 0)
Packit 85355f
	{
Packit 85355f
		tmsize_t n = (tmsize_t)cc;
Packit 85355f
Packit 85355f
		if (tif->tif_rawcc + n > tif->tif_rawdatasize)
Packit 85355f
		{
Packit 85355f
			n = tif->tif_rawdatasize - tif->tif_rawcc;
Packit 85355f
		}
Packit 85355f
Packit 85355f
		assert(n > 0);
Packit 85355f
		_TIFFmemcpy(tif->tif_rawcp, pp, n);
Packit 85355f
		tif->tif_rawcp += n;
Packit 85355f
		tif->tif_rawcc += n;
Packit 85355f
		pp += n;
Packit 85355f
		cc -= (size_t)n;
Packit 85355f
		if (tif->tif_rawcc >= tif->tif_rawdatasize &&
Packit 85355f
		    !TIFFFlushData1(tif))
Packit 85355f
		{
Packit 85355f
			return (-1);
Packit 85355f
		}
Packit 85355f
	}
Packit 85355f
Packit 85355f
	return (1);
Packit 85355f
}
Packit 85355f
Packit 85355f
static void JBIGOutputBie(unsigned char* buffer, size_t len, void* userData)
Packit 85355f
{
Packit 85355f
	TIFF* tif = (TIFF*)userData;
Packit 85355f
Packit 85355f
	if (isFillOrder(tif, tif->tif_dir.td_fillorder))
Packit 85355f
	{
Packit 85355f
		TIFFReverseBits(buffer, (tmsize_t)len);
Packit 85355f
	}
Packit 85355f
Packit 85355f
	JBIGCopyEncodedData(tif, buffer, len, 0);
Packit 85355f
}
Packit 85355f
Packit 85355f
static int JBIGEncode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
Packit 85355f
{
Packit 85355f
	TIFFDirectory* dir = &tif->tif_dir;
Packit 85355f
	struct jbg_enc_state encoder;
Packit 85355f
Packit 85355f
	(void) size, (void) s;
Packit 85355f
Packit 85355f
	jbg_enc_init(&encoder,
Packit 85355f
		     dir->td_imagewidth,
Packit 85355f
		     dir->td_imagelength,
Packit 85355f
		     1,
Packit 85355f
		     &buffer,
Packit 85355f
		     JBIGOutputBie,
Packit 85355f
		     tif);
Packit 85355f
	/*
Packit 85355f
	 * jbg_enc_out does the "real" encoding.  As data is encoded,
Packit 85355f
	 * JBIGOutputBie is called, which writes the data to the directory.
Packit 85355f
	 */
Packit 85355f
	jbg_enc_out(&encoder);
Packit 85355f
	jbg_enc_free(&encoder);
Packit 85355f
Packit 85355f
	return 1;
Packit 85355f
}
Packit 85355f
Packit 85355f
int TIFFInitJBIG(TIFF* tif, int scheme)
Packit 85355f
{
Packit 85355f
	assert(scheme == COMPRESSION_JBIG);
Packit 85355f
Packit 85355f
	/*
Packit 85355f
	 * These flags are set so the JBIG Codec can control when to reverse
Packit 85355f
	 * bits and when not to and to allow the jbig decoder and bit reverser
Packit 85355f
	 * to write to memory when necessary.
Packit 85355f
	 */
Packit 85355f
	tif->tif_flags |= TIFF_NOBITREV;
Packit 85355f
	tif->tif_flags &= ~TIFF_MAPPED;
Packit 85355f
Packit 85355f
	/* Setup the function pointers for encode, decode, and cleanup. */
Packit 85355f
	tif->tif_setupdecode = JBIGSetupDecode;
Packit 85355f
	tif->tif_decodestrip = JBIGDecode;
Packit 85355f
Packit 85355f
	tif->tif_setupencode = JBIGSetupEncode;
Packit 85355f
	tif->tif_encodestrip = JBIGEncode;
Packit 85355f
Packit 85355f
	return 1;
Packit 85355f
}
Packit 85355f
Packit 85355f
#endif /* JBIG_SUPPORT */
Packit 85355f
Packit 85355f
/* vim: set ts=8 sts=8 sw=8 noet: */
Packit 85355f
Packit 85355f
/*
Packit 85355f
 * Local Variables:
Packit 85355f
 * mode: c
Packit 85355f
 * c-basic-offset: 8
Packit 85355f
 * fill-column: 78
Packit 85355f
 * End:
Packit 85355f
 */