Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/3rdparty/libtiff/tif_jbig.c
16337 views
1
/* $Id: tif_jbig.c,v 1.16 2017-06-26 15:20:00 erouault 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 <[email protected]>
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
jbg_dec_free(&decoder);
98
return 0;
99
}
100
101
pImage = jbg_dec_getimage(&decoder, 0);
102
_TIFFmemcpy(buffer, pImage, jbg_dec_getsize(&decoder));
103
jbg_dec_free(&decoder);
104
return 1;
105
}
106
107
static int JBIGSetupEncode(TIFF* tif)
108
{
109
if (TIFFNumberOfStrips(tif) != 1)
110
{
111
TIFFErrorExt(tif->tif_clientdata, "JBIG", "Multistrip images not supported in encoder");
112
return 0;
113
}
114
115
return 1;
116
}
117
118
static int JBIGCopyEncodedData(TIFF* tif, unsigned char* pp, size_t cc, uint16 s)
119
{
120
(void) s;
121
while (cc > 0)
122
{
123
tmsize_t n = (tmsize_t)cc;
124
125
if (tif->tif_rawcc + n > tif->tif_rawdatasize)
126
{
127
n = tif->tif_rawdatasize - tif->tif_rawcc;
128
}
129
130
assert(n > 0);
131
_TIFFmemcpy(tif->tif_rawcp, pp, n);
132
tif->tif_rawcp += n;
133
tif->tif_rawcc += n;
134
pp += n;
135
cc -= (size_t)n;
136
if (tif->tif_rawcc >= tif->tif_rawdatasize &&
137
!TIFFFlushData1(tif))
138
{
139
return (-1);
140
}
141
}
142
143
return (1);
144
}
145
146
static void JBIGOutputBie(unsigned char* buffer, size_t len, void* userData)
147
{
148
TIFF* tif = (TIFF*)userData;
149
150
if (isFillOrder(tif, tif->tif_dir.td_fillorder))
151
{
152
TIFFReverseBits(buffer, (tmsize_t)len);
153
}
154
155
JBIGCopyEncodedData(tif, buffer, len, 0);
156
}
157
158
static int JBIGEncode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
159
{
160
TIFFDirectory* dir = &tif->tif_dir;
161
struct jbg_enc_state encoder;
162
163
(void) size, (void) s;
164
165
jbg_enc_init(&encoder,
166
dir->td_imagewidth,
167
dir->td_imagelength,
168
1,
169
&buffer,
170
JBIGOutputBie,
171
tif);
172
/*
173
* jbg_enc_out does the "real" encoding. As data is encoded,
174
* JBIGOutputBie is called, which writes the data to the directory.
175
*/
176
jbg_enc_out(&encoder);
177
jbg_enc_free(&encoder);
178
179
return 1;
180
}
181
182
int TIFFInitJBIG(TIFF* tif, int scheme)
183
{
184
assert(scheme == COMPRESSION_JBIG);
185
186
/*
187
* These flags are set so the JBIG Codec can control when to reverse
188
* bits and when not to and to allow the jbig decoder and bit reverser
189
* to write to memory when necessary.
190
*/
191
tif->tif_flags |= TIFF_NOBITREV;
192
tif->tif_flags &= ~TIFF_MAPPED;
193
194
/* Setup the function pointers for encode, decode, and cleanup. */
195
tif->tif_setupdecode = JBIGSetupDecode;
196
tif->tif_decodestrip = JBIGDecode;
197
198
tif->tif_setupencode = JBIGSetupEncode;
199
tif->tif_encodestrip = JBIGEncode;
200
201
return 1;
202
}
203
204
#endif /* JBIG_SUPPORT */
205
206
/* vim: set ts=8 sts=8 sw=8 noet: */
207
208
/*
209
* Local Variables:
210
* mode: c
211
* c-basic-offset: 8
212
* fill-column: 78
213
* End:
214
*/
215
216