/* $Id: tif_jbig.c,v 1.16 2017-06-26 15:20:00 erouault Exp $ */12/*3* Copyright (c) 1988-1997 Sam Leffler4* Copyright (c) 1991-1997 Silicon Graphics, Inc.5*6* Permission to use, copy, modify, distribute, and sell this software and7* its documentation for any purpose is hereby granted without fee, provided8* that (i) the above copyright notices and this permission notice appear in9* all copies of the software and related documentation, and (ii) the names of10* Sam Leffler and Silicon Graphics may not be used in any advertising or11* publicity relating to the software without the specific, prior written12* permission of Sam Leffler and Silicon Graphics.13*14* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,15* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY16* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.17*18* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR19* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,20* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,21* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF22* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE23* OF THIS SOFTWARE.24*/2526/*27* TIFF Library.28*29* JBIG Compression Algorithm Support.30* Contributed by Lee Howard <[email protected]>31*32*/3334#include "tiffiop.h"3536#ifdef JBIG_SUPPORT37#include "jbig.h"3839static int JBIGSetupDecode(TIFF* tif)40{41if (TIFFNumberOfStrips(tif) != 1)42{43TIFFErrorExt(tif->tif_clientdata, "JBIG", "Multistrip images not supported in decoder");44return 0;45}4647return 1;48}4950static int JBIGDecode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)51{52struct jbg_dec_state decoder;53int decodeStatus = 0;54unsigned char* pImage = NULL;55(void) size, (void) s;5657if (isFillOrder(tif, tif->tif_dir.td_fillorder))58{59TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdatasize);60}6162jbg_dec_init(&decoder);6364#if defined(HAVE_JBG_NEWLEN)65jbg_newlen(tif->tif_rawdata, (size_t)tif->tif_rawdatasize);66/*67* I do not check the return status of jbg_newlen because even if this68* function fails it does not necessarily mean that decoding the image69* will fail. It is generally only needed for received fax images70* that do not contain the actual length of the image in the BIE71* header. I do not log when an error occurs because that will cause72* problems when converting JBIG encoded TIFF's to73* PostScript. As long as the actual image length is contained in the74* BIE header jbg_dec_in should succeed.75*/76#endif /* HAVE_JBG_NEWLEN */7778decodeStatus = jbg_dec_in(&decoder, (unsigned char*)tif->tif_rawdata,79(size_t)tif->tif_rawdatasize, NULL);80if (JBG_EOK != decodeStatus)81{82/*83* XXX: JBG_EN constant was defined in pre-2.0 releases of the84* JBIG-KIT. Since the 2.0 the error reporting functions were85* changed. We will handle both cases here.86*/87TIFFErrorExt(tif->tif_clientdata,88"JBIG", "Error (%d) decoding: %s",89decodeStatus,90#if defined(JBG_EN)91jbg_strerror(decodeStatus, JBG_EN)92#else93jbg_strerror(decodeStatus)94#endif95);96jbg_dec_free(&decoder);97return 0;98}99100pImage = jbg_dec_getimage(&decoder, 0);101_TIFFmemcpy(buffer, pImage, jbg_dec_getsize(&decoder));102jbg_dec_free(&decoder);103return 1;104}105106static int JBIGSetupEncode(TIFF* tif)107{108if (TIFFNumberOfStrips(tif) != 1)109{110TIFFErrorExt(tif->tif_clientdata, "JBIG", "Multistrip images not supported in encoder");111return 0;112}113114return 1;115}116117static int JBIGCopyEncodedData(TIFF* tif, unsigned char* pp, size_t cc, uint16 s)118{119(void) s;120while (cc > 0)121{122tmsize_t n = (tmsize_t)cc;123124if (tif->tif_rawcc + n > tif->tif_rawdatasize)125{126n = tif->tif_rawdatasize - tif->tif_rawcc;127}128129assert(n > 0);130_TIFFmemcpy(tif->tif_rawcp, pp, n);131tif->tif_rawcp += n;132tif->tif_rawcc += n;133pp += n;134cc -= (size_t)n;135if (tif->tif_rawcc >= tif->tif_rawdatasize &&136!TIFFFlushData1(tif))137{138return (-1);139}140}141142return (1);143}144145static void JBIGOutputBie(unsigned char* buffer, size_t len, void* userData)146{147TIFF* tif = (TIFF*)userData;148149if (isFillOrder(tif, tif->tif_dir.td_fillorder))150{151TIFFReverseBits(buffer, (tmsize_t)len);152}153154JBIGCopyEncodedData(tif, buffer, len, 0);155}156157static int JBIGEncode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)158{159TIFFDirectory* dir = &tif->tif_dir;160struct jbg_enc_state encoder;161162(void) size, (void) s;163164jbg_enc_init(&encoder,165dir->td_imagewidth,166dir->td_imagelength,1671,168&buffer,169JBIGOutputBie,170tif);171/*172* jbg_enc_out does the "real" encoding. As data is encoded,173* JBIGOutputBie is called, which writes the data to the directory.174*/175jbg_enc_out(&encoder);176jbg_enc_free(&encoder);177178return 1;179}180181int TIFFInitJBIG(TIFF* tif, int scheme)182{183assert(scheme == COMPRESSION_JBIG);184185/*186* These flags are set so the JBIG Codec can control when to reverse187* bits and when not to and to allow the jbig decoder and bit reverser188* to write to memory when necessary.189*/190tif->tif_flags |= TIFF_NOBITREV;191tif->tif_flags &= ~TIFF_MAPPED;192193/* Setup the function pointers for encode, decode, and cleanup. */194tif->tif_setupdecode = JBIGSetupDecode;195tif->tif_decodestrip = JBIGDecode;196197tif->tif_setupencode = JBIGSetupEncode;198tif->tif_encodestrip = JBIGEncode;199200return 1;201}202203#endif /* JBIG_SUPPORT */204205/* vim: set ts=8 sts=8 sw=8 noet: */206207/*208* Local Variables:209* mode: c210* c-basic-offset: 8211* fill-column: 78212* End:213*/214215216