]>
git.saurik.com Git - wxWidgets.git/blob - src/tiff/libtiff/tif_jbig.c
3 * Copyright (c) 1988-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that (i) the above copyright notices and this permission notice appear in
9 * all copies of the software and related documentation, and (ii) the names of
10 * Sam Leffler and Silicon Graphics may not be used in any advertising or
11 * publicity relating to the software without the specific, prior written
12 * permission of Sam Leffler and Silicon Graphics.
14 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19 * 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 OF
22 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
29 * JBIG Compression Algorithm Support.
30 * Contributed by Lee Howard <faxguy@deanox.com>
39 static int JBIGSetupDecode(TIFF
* tif
)
41 if (TIFFNumberOfStrips(tif
) != 1)
43 TIFFErrorExt(tif
->tif_clientdata
, "JBIG", "Multistrip images not supported in decoder");
50 static int JBIGDecode(TIFF
* tif
, uint8
* buffer
, tmsize_t size
, uint16 s
)
52 struct jbg_dec_state decoder
;
54 unsigned char* pImage
= NULL
;
55 (void) size
, (void) s
;
57 if (isFillOrder(tif
, tif
->tif_dir
.td_fillorder
))
59 TIFFReverseBits(tif
->tif_rawdata
, tif
->tif_rawdatasize
);
62 jbg_dec_init(&decoder
);
64 #if defined(HAVE_JBG_NEWLEN)
65 jbg_newlen(tif
->tif_rawdata
, (size_t)tif
->tif_rawdatasize
);
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_rawdata
,
79 (size_t)tif
->tif_rawdatasize
, 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
)
99 pImage
= jbg_dec_getimage(&decoder
, 0);
100 _TIFFmemcpy(buffer
, pImage
, jbg_dec_getsize(&decoder
));
101 jbg_dec_free(&decoder
);
105 static int JBIGSetupEncode(TIFF
* tif
)
107 if (TIFFNumberOfStrips(tif
) != 1)
109 TIFFErrorExt(tif
->tif_clientdata
, "JBIG", "Multistrip images not supported in encoder");
116 static int JBIGCopyEncodedData(TIFF
* tif
, unsigned char* pp
, size_t cc
, uint16 s
)
121 tmsize_t n
= (tmsize_t
)cc
;
123 if (tif
->tif_rawcc
+ n
> tif
->tif_rawdatasize
)
125 n
= tif
->tif_rawdatasize
- tif
->tif_rawcc
;
129 _TIFFmemcpy(tif
->tif_rawcp
, pp
, n
);
134 if (tif
->tif_rawcc
>= tif
->tif_rawdatasize
&&
135 !TIFFFlushData1(tif
))
144 static void JBIGOutputBie(unsigned char* buffer
, size_t len
, void* userData
)
146 TIFF
* tif
= (TIFF
*)userData
;
148 if (isFillOrder(tif
, tif
->tif_dir
.td_fillorder
))
150 TIFFReverseBits(buffer
, (tmsize_t
)len
);
153 JBIGCopyEncodedData(tif
, buffer
, len
, 0);
156 static int JBIGEncode(TIFF
* tif
, uint8
* buffer
, tmsize_t size
, uint16 s
)
158 TIFFDirectory
* dir
= &tif
->tif_dir
;
159 struct jbg_enc_state encoder
;
161 (void) size
, (void) s
;
163 jbg_enc_init(&encoder
,
171 * jbg_enc_out does the "real" encoding. As data is encoded,
172 * JBIGOutputBie is called, which writes the data to the directory.
174 jbg_enc_out(&encoder
);
175 jbg_enc_free(&encoder
);
180 int TIFFInitJBIG(TIFF
* tif
, int scheme
)
182 assert(scheme
== COMPRESSION_JBIG
);
185 * These flags are set so the JBIG Codec can control when to reverse
186 * bits and when not to and to allow the jbig decoder and bit reverser
187 * to write to memory when necessary.
189 tif
->tif_flags
|= TIFF_NOBITREV
;
190 tif
->tif_flags
&= ~TIFF_MAPPED
;
192 /* Setup the function pointers for encode, decode, and cleanup. */
193 tif
->tif_setupdecode
= JBIGSetupDecode
;
194 tif
->tif_decodestrip
= JBIGDecode
;
196 tif
->tif_setupencode
= JBIGSetupEncode
;
197 tif
->tif_encodestrip
= JBIGEncode
;
202 #endif /* JBIG_SUPPORT */
204 /* vim: set ts=8 sts=8 sw=8 noet: */