New comit of SDL2
[supertux.git] / src / SDL2 / external / tiff-4.0.3 / libtiff / tif_jbig.c
1 /* $Id: tif_jbig.c,v 1.15 2010-03-10 18:56:48 bfriesen Exp $ */
2
3 /*
4  * Copyright (c) 1988-1997 Sam Leffler
5  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and
8  * its documentation for any purpose is hereby granted without fee, provided
9  * that (i) the above copyright notices and this permission notice appear in
10  * all copies of the software and related documentation, and (ii) the names of
11  * Sam Leffler and Silicon Graphics may not be used in any advertising or
12  * publicity relating to the software without the specific, prior written
13  * permission of Sam Leffler and Silicon Graphics.
14  *
15  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18  *
19  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  */
26
27 /*
28  * TIFF Library.
29  *
30  * JBIG Compression Algorithm Support.
31  * Contributed by Lee Howard <faxguy@deanox.com>
32  *
33  */
34
35 #include "tiffiop.h"
36
37 #ifdef JBIG_SUPPORT
38 #include "jbig.h"
39
40 static int JBIGSetupDecode(TIFF* tif)
41 {
42         if (TIFFNumberOfStrips(tif) != 1)
43         {
44                 TIFFErrorExt(tif->tif_clientdata, "JBIG", "Multistrip images not supported in decoder");
45                 return 0;
46         }
47
48         return 1;
49 }
50
51 static int JBIGDecode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
52 {
53         struct jbg_dec_state decoder;
54         int decodeStatus = 0;
55         unsigned char* pImage = NULL;
56         (void) size, (void) s;
57
58         if (isFillOrder(tif, tif->tif_dir.td_fillorder))
59         {
60                 TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdatasize);
61         }
62
63         jbg_dec_init(&decoder);
64
65 #if defined(HAVE_JBG_NEWLEN)
66         jbg_newlen(tif->tif_rawdata, (size_t)tif->tif_rawdatasize);
67         /*
68          * I do not check the return status of jbg_newlen because even if this
69          * function fails it does not necessarily mean that decoding the image
70          * will fail.  It is generally only needed for received fax images
71          * that do not contain the actual length of the image in the BIE
72          * header.  I do not log when an error occurs because that will cause
73          * problems when converting JBIG encoded TIFF's to
74          * PostScript.  As long as the actual image length is contained in the
75          * BIE header jbg_dec_in should succeed.
76          */
77 #endif /* HAVE_JBG_NEWLEN */
78
79         decodeStatus = jbg_dec_in(&decoder, (unsigned char*)tif->tif_rawdata,
80                                   (size_t)tif->tif_rawdatasize, NULL);
81         if (JBG_EOK != decodeStatus)
82         {
83                 /*
84                  * XXX: JBG_EN constant was defined in pre-2.0 releases of the
85                  * JBIG-KIT. Since the 2.0 the error reporting functions were
86                  * changed. We will handle both cases here.
87                  */
88                 TIFFErrorExt(tif->tif_clientdata,
89                              "JBIG", "Error (%d) decoding: %s",
90                              decodeStatus,
91 #if defined(JBG_EN)
92                              jbg_strerror(decodeStatus, JBG_EN)
93 #else
94                              jbg_strerror(decodeStatus)
95 #endif
96                              );
97                 return 0;
98         }
99
100         pImage = jbg_dec_getimage(&decoder, 0);
101         _TIFFmemcpy(buffer, pImage, jbg_dec_getsize(&decoder));
102         jbg_dec_free(&decoder);
103         return 1;
104 }
105
106 static int JBIGSetupEncode(TIFF* tif)
107 {
108         if (TIFFNumberOfStrips(tif) != 1)
109         {
110                 TIFFErrorExt(tif->tif_clientdata, "JBIG", "Multistrip images not supported in encoder");
111                 return 0;
112         }
113
114         return 1;
115 }
116
117 static int JBIGCopyEncodedData(TIFF* tif, unsigned char* pp, size_t cc, uint16 s)
118 {
119         (void) s;
120         while (cc > 0)
121         {
122                 tmsize_t n = (tmsize_t)cc;
123
124                 if (tif->tif_rawcc + n > tif->tif_rawdatasize)
125                 {
126                         n = tif->tif_rawdatasize - tif->tif_rawcc;
127                 }
128
129                 assert(n > 0);
130                 _TIFFmemcpy(tif->tif_rawcp, pp, n);
131                 tif->tif_rawcp += n;
132                 tif->tif_rawcc += n;
133                 pp += n;
134                 cc -= (size_t)n;
135                 if (tif->tif_rawcc >= tif->tif_rawdatasize &&
136                     !TIFFFlushData1(tif))
137                 {
138                         return (-1);
139                 }
140         }
141
142         return (1);
143 }
144
145 static void JBIGOutputBie(unsigned char* buffer, size_t len, void* userData)
146 {
147         TIFF* tif = (TIFF*)userData;
148
149         if (isFillOrder(tif, tif->tif_dir.td_fillorder))
150         {
151                 TIFFReverseBits(buffer, (tmsize_t)len);
152         }
153
154         JBIGCopyEncodedData(tif, buffer, len, 0);
155 }
156
157 static int JBIGEncode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
158 {
159         TIFFDirectory* dir = &tif->tif_dir;
160         struct jbg_enc_state encoder;
161
162         (void) size, (void) s;
163
164         jbg_enc_init(&encoder,
165                      dir->td_imagewidth,
166                      dir->td_imagelength,
167                      1,
168                      &buffer,
169                      JBIGOutputBie,
170                      tif);
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          */
175         jbg_enc_out(&encoder);
176         jbg_enc_free(&encoder);
177
178         return 1;
179 }
180
181 int TIFFInitJBIG(TIFF* tif, int scheme)
182 {
183         assert(scheme == COMPRESSION_JBIG);
184
185         /*
186          * These flags are set so the JBIG Codec can control when to reverse
187          * bits and when not to and to allow the jbig decoder and bit reverser
188          * to write to memory when necessary.
189          */
190         tif->tif_flags |= TIFF_NOBITREV;
191         tif->tif_flags &= ~TIFF_MAPPED;
192
193         /* Setup the function pointers for encode, decode, and cleanup. */
194         tif->tif_setupdecode = JBIGSetupDecode;
195         tif->tif_decodestrip = JBIGDecode;
196
197         tif->tif_setupencode = JBIGSetupEncode;
198         tif->tif_encodestrip = JBIGEncode;
199
200         return 1;
201 }
202
203 #endif /* JBIG_SUPPORT */
204
205 /* vim: set ts=8 sts=8 sw=8 noet: */
206
207 /*
208  * Local Variables:
209  * mode: c
210  * c-basic-offset: 8
211  * fill-column: 78
212  * End:
213  */