2 * Copyright (c) 1988-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 * JBIG Compression Algorithm Support.
29 * Contributed by Lee Howard <faxguy@deanox.com>
38 static int JBIGSetupDecode(TIFF
* tif
)
40 if (TIFFNumberOfStrips(tif
) != 1)
42 TIFFErrorExt(tif
->tif_clientdata
, "JBIG", "Multistrip images not supported in decoder");
49 static int JBIGDecode(TIFF
* tif
, uint8
* buffer
, tmsize_t size
, uint16 s
)
51 struct jbg_dec_state decoder
;
53 unsigned char* pImage
= NULL
;
54 unsigned long decodedSize
;
57 if (isFillOrder(tif
, tif
->tif_dir
.td_fillorder
))
59 TIFFReverseBits(tif
->tif_rawcp
, tif
->tif_rawcc
);
62 jbg_dec_init(&decoder
);
64 #if defined(HAVE_JBG_NEWLEN)
65 jbg_newlen(tif
->tif_rawcp
, (size_t)tif
->tif_rawcc
);
67 * I do not check the return status of jbg_newlen because even if this
68 * function fails it does not necessarily mean that decoding the image
69 * will fail. It is generally only needed for received fax images
70 * that do not contain the actual length of the image in the BIE
71 * header. I do not log when an error occurs because that will cause
72 * problems when converting JBIG encoded TIFF's to
73 * PostScript. As long as the actual image length is contained in the
74 * BIE header jbg_dec_in should succeed.
76 #endif /* HAVE_JBG_NEWLEN */
78 decodeStatus
= jbg_dec_in(&decoder
, (unsigned char*)tif
->tif_rawcp
,
79 (size_t)tif
->tif_rawcc
, NULL
);
80 if (JBG_EOK
!= decodeStatus
)
83 * XXX: JBG_EN constant was defined in pre-2.0 releases of the
84 * JBIG-KIT. Since the 2.0 the error reporting functions were
85 * changed. We will handle both cases here.
87 TIFFErrorExt(tif
->tif_clientdata
,
88 "JBIG", "Error (%d) decoding: %s",
91 jbg_strerror(decodeStatus
, JBG_EN
)
93 jbg_strerror(decodeStatus
)
96 jbg_dec_free(&decoder
);
100 decodedSize
= jbg_dec_getsize(&decoder
);
101 if( (tmsize_t
)decodedSize
< size
)
103 TIFFWarningExt(tif
->tif_clientdata
, "JBIG",
104 "Only decoded %lu bytes, whereas %lu requested",
105 decodedSize
, (unsigned long)size
);
107 else if( (tmsize_t
)decodedSize
> size
)
109 TIFFErrorExt(tif
->tif_clientdata
, "JBIG",
110 "Decoded %lu bytes, whereas %lu were requested",
111 decodedSize
, (unsigned long)size
);
112 jbg_dec_free(&decoder
);
115 pImage
= jbg_dec_getimage(&decoder
, 0);
116 _TIFFmemcpy(buffer
, pImage
, decodedSize
);
117 jbg_dec_free(&decoder
);
119 tif
->tif_rawcp
+= tif
->tif_rawcc
;
125 static int JBIGSetupEncode(TIFF
* tif
)
127 if (TIFFNumberOfStrips(tif
) != 1)
129 TIFFErrorExt(tif
->tif_clientdata
, "JBIG", "Multistrip images not supported in encoder");
136 static int JBIGCopyEncodedData(TIFF
* tif
, unsigned char* pp
, size_t cc
, uint16 s
)
141 tmsize_t n
= (tmsize_t
)cc
;
143 if (tif
->tif_rawcc
+ n
> tif
->tif_rawdatasize
)
145 n
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
149 _TIFFmemcpy(tif
->tif_rawcp
, pp
, n
);
154 if (tif
->tif_rawcc
>= tif
->tif_rawdatasize
&&
155 !TIFFFlushData1(tif
))
164 static void JBIGOutputBie(unsigned char* buffer
, size_t len
, void* userData
)
166 TIFF
* tif
= (TIFF
*)userData
;
168 if (isFillOrder(tif
, tif
->tif_dir
.td_fillorder
))
170 TIFFReverseBits(buffer
, (tmsize_t
)len
);
173 JBIGCopyEncodedData(tif
, buffer
, len
, 0);
176 static int JBIGEncode(TIFF
* tif
, uint8
* buffer
, tmsize_t size
, uint16 s
)
178 TIFFDirectory
* dir
= &tif
->tif_dir
;
179 struct jbg_enc_state encoder
;
181 (void) size
, (void) s
;
183 jbg_enc_init(&encoder
,
191 * jbg_enc_out does the "real" encoding. As data is encoded,
192 * JBIGOutputBie is called, which writes the data to the directory.
194 jbg_enc_out(&encoder
);
195 jbg_enc_free(&encoder
);
200 int TIFFInitJBIG(TIFF
* tif
, int scheme
)
202 assert(scheme
== COMPRESSION_JBIG
);
205 * These flags are set so the JBIG Codec can control when to reverse
206 * bits and when not to and to allow the jbig decoder and bit reverser
207 * to write to memory when necessary.
209 tif
->tif_flags
|= TIFF_NOBITREV
;
210 tif
->tif_flags
&= ~TIFF_MAPPED
;
212 /* Setup the function pointers for encode, decode, and cleanup. */
213 tif
->tif_setupdecode
= JBIGSetupDecode
;
214 tif
->tif_decodestrip
= JBIGDecode
;
216 tif
->tif_setupencode
= JBIGSetupEncode
;
217 tif
->tif_encodestrip
= JBIGEncode
;
222 #endif /* JBIG_SUPPORT */
224 /* vim: set ts=8 sts=8 sw=8 noet: */