Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygentif_jbig.c
Go to the documentation of this file.
00001 /* $Id: tif_jbig.c,v 1.2.2.3 2010-06-08 18:50:42 bfriesen Exp $ */ 00002 00003 /* 00004 * Copyright (c) 1988-1997 Sam Leffler 00005 * Copyright (c) 1991-1997 Silicon Graphics, Inc. 00006 * 00007 * Permission to use, copy, modify, distribute, and sell this software and 00008 * its documentation for any purpose is hereby granted without fee, provided 00009 * that (i) the above copyright notices and this permission notice appear in 00010 * all copies of the software and related documentation, and (ii) the names of 00011 * Sam Leffler and Silicon Graphics may not be used in any advertising or 00012 * publicity relating to the software without the specific, prior written 00013 * permission of Sam Leffler and Silicon Graphics. 00014 * 00015 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 00016 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 00017 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 00018 * 00019 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR 00020 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, 00021 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 00022 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 00023 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 00024 * OF THIS SOFTWARE. 00025 */ 00026 00027 /* 00028 * TIFF Library. 00029 * 00030 * JBIG Compression Algorithm Support. 00031 * Contributed by Lee Howard <faxguy@deanox.com> 00032 * 00033 */ 00034 00035 #include "tiffiop.h" 00036 00037 #ifdef JBIG_SUPPORT 00038 #include "jbig.h" 00039 00040 typedef struct 00041 { 00042 uint32 recvparams; /* encoded Class 2 session params */ 00043 char* subaddress; /* subaddress string */ 00044 uint32 recvtime; /* time spend receiving in seconds */ 00045 char* faxdcs; /* encoded fax parameters (DCS, Table 2/T.30) */ 00046 00047 TIFFVGetMethod vgetparent; 00048 TIFFVSetMethod vsetparent; 00049 } JBIGState; 00050 00051 #define GetJBIGState(tif) ((JBIGState*)(tif)->tif_data) 00052 00053 #define FIELD_RECVPARAMS (FIELD_CODEC+0) 00054 #define FIELD_SUBADDRESS (FIELD_CODEC+1) 00055 #define FIELD_RECVTIME (FIELD_CODEC+2) 00056 #define FIELD_FAXDCS (FIELD_CODEC+3) 00057 00058 static const TIFFFieldInfo jbigFieldInfo[] = 00059 { 00060 {TIFFTAG_FAXRECVPARAMS, 1, 1, TIFF_LONG, FIELD_RECVPARAMS, TRUE, FALSE, "FaxRecvParams"}, 00061 {TIFFTAG_FAXSUBADDRESS, -1, -1, TIFF_ASCII, FIELD_SUBADDRESS, TRUE, FALSE, "FaxSubAddress"}, 00062 {TIFFTAG_FAXRECVTIME, 1, 1, TIFF_LONG, FIELD_RECVTIME, TRUE, FALSE, "FaxRecvTime"}, 00063 {TIFFTAG_FAXDCS, -1, -1, TIFF_ASCII, FIELD_FAXDCS, TRUE, FALSE, "FaxDcs"}, 00064 }; 00065 00066 static int JBIGSetupDecode(TIFF* tif) 00067 { 00068 if (TIFFNumberOfStrips(tif) != 1) 00069 { 00070 TIFFError("JBIG", "Multistrip images not supported in decoder"); 00071 return 0; 00072 } 00073 00074 return 1; 00075 } 00076 00077 static int JBIGDecode(TIFF* tif, tidata_t buffer, tsize_t size, tsample_t s) 00078 { 00079 struct jbg_dec_state decoder; 00080 int decodeStatus = 0; 00081 unsigned char* pImage = NULL; 00082 (void) size, (void) s; 00083 00084 if (isFillOrder(tif, tif->tif_dir.td_fillorder)) 00085 { 00086 TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdatasize); 00087 } 00088 00089 jbg_dec_init(&decoder); 00090 00091 #if defined(HAVE_JBG_NEWLEN) 00092 jbg_newlen(tif->tif_rawdata, tif->tif_rawdatasize); 00093 /* 00094 * I do not check the return status of jbg_newlen because even if this 00095 * function fails it does not necessarily mean that decoding the image 00096 * will fail. It is generally only needed for received fax images 00097 * that do not contain the actual length of the image in the BIE 00098 * header. I do not log when an error occurs because that will cause 00099 * problems when converting JBIG encoded TIFF's to 00100 * PostScript. As long as the actual image length is contained in the 00101 * BIE header jbg_dec_in should succeed. 00102 */ 00103 #endif /* HAVE_JBG_NEWLEN */ 00104 00105 decodeStatus = jbg_dec_in(&decoder, tif->tif_rawdata, 00106 tif->tif_rawdatasize, NULL); 00107 if (JBG_EOK != decodeStatus) 00108 { 00109 /* 00110 * XXX: JBG_EN constant was defined in pre-2.0 releases of the 00111 * JBIG-KIT. Since the 2.0 the error reporting functions were 00112 * changed. We will handle both cases here. 00113 */ 00114 TIFFError("JBIG", "Error (%d) decoding: %s", decodeStatus, 00115 #if defined(JBG_EN) 00116 jbg_strerror(decodeStatus, JBG_EN) 00117 #else 00118 jbg_strerror(decodeStatus) 00119 #endif 00120 ); 00121 return 0; 00122 } 00123 00124 pImage = jbg_dec_getimage(&decoder, 0); 00125 _TIFFmemcpy(buffer, pImage, jbg_dec_getsize(&decoder)); 00126 jbg_dec_free(&decoder); 00127 return 1; 00128 } 00129 00130 static int JBIGSetupEncode(TIFF* tif) 00131 { 00132 if (TIFFNumberOfStrips(tif) != 1) 00133 { 00134 TIFFError("JBIG", "Multistrip images not supported in encoder"); 00135 return 0; 00136 } 00137 00138 return 1; 00139 } 00140 00141 static int JBIGCopyEncodedData(TIFF* tif, tidata_t pp, tsize_t cc, tsample_t s) 00142 { 00143 (void) s; 00144 while (cc > 0) 00145 { 00146 tsize_t n = cc; 00147 00148 if (tif->tif_rawcc + n > tif->tif_rawdatasize) 00149 { 00150 n = tif->tif_rawdatasize - tif->tif_rawcc; 00151 } 00152 00153 assert(n > 0); 00154 _TIFFmemcpy(tif->tif_rawcp, pp, n); 00155 tif->tif_rawcp += n; 00156 tif->tif_rawcc += n; 00157 pp += n; 00158 cc -= n; 00159 if (tif->tif_rawcc >= tif->tif_rawdatasize && 00160 !TIFFFlushData1(tif)) 00161 { 00162 return (-1); 00163 } 00164 } 00165 00166 return (1); 00167 } 00168 00169 static void JBIGOutputBie(unsigned char* buffer, size_t len, void *userData) 00170 { 00171 TIFF* tif = (TIFF*)userData; 00172 00173 if (isFillOrder(tif, tif->tif_dir.td_fillorder)) 00174 { 00175 TIFFReverseBits(buffer, len); 00176 } 00177 00178 JBIGCopyEncodedData(tif, buffer, len, 0); 00179 } 00180 00181 static int JBIGEncode(TIFF* tif, tidata_t buffer, tsize_t size, tsample_t s) 00182 { 00183 TIFFDirectory* dir = &tif->tif_dir; 00184 struct jbg_enc_state encoder; 00185 00186 (void) size, (void) s; 00187 00188 jbg_enc_init(&encoder, 00189 dir->td_imagewidth, 00190 dir->td_imagelength, 00191 1, 00192 &buffer, 00193 JBIGOutputBie, 00194 tif); 00195 /* 00196 * jbg_enc_out does the "real" encoding. As data is encoded, 00197 * JBIGOutputBie is called, which writes the data to the directory. 00198 */ 00199 jbg_enc_out(&encoder); 00200 jbg_enc_free(&encoder); 00201 00202 return 1; 00203 } 00204 00205 static void JBIGCleanup(TIFF* tif) 00206 { 00207 JBIGState *sp = GetJBIGState(tif); 00208 00209 assert(sp != 0); 00210 00211 tif->tif_tagmethods.vgetfield = sp->vgetparent; 00212 tif->tif_tagmethods.vsetfield = sp->vsetparent; 00213 00214 _TIFFfree(tif->tif_data); 00215 tif->tif_data = NULL; 00216 00217 _TIFFSetDefaultCompressionState(tif); 00218 } 00219 00220 static void JBIGPrintDir(TIFF* tif, FILE* fd, long flags) 00221 { 00222 JBIGState* codec = GetJBIGState(tif); 00223 (void)flags; 00224 00225 if (TIFFFieldSet(tif, FIELD_RECVPARAMS)) 00226 { 00227 fprintf(fd, 00228 " Fax Receive Parameters: %08lx\n", 00229 (unsigned long)codec->recvparams); 00230 } 00231 00232 if (TIFFFieldSet(tif, FIELD_SUBADDRESS)) 00233 { 00234 fprintf(fd, 00235 " Fax SubAddress: %s\n", 00236 codec->subaddress); 00237 } 00238 00239 if (TIFFFieldSet(tif, FIELD_RECVTIME)) 00240 { 00241 fprintf(fd, 00242 " Fax Receive Time: %lu secs\n", 00243 (unsigned long)codec->recvtime); 00244 } 00245 00246 if (TIFFFieldSet(tif, FIELD_FAXDCS)) 00247 { 00248 fprintf(fd, 00249 " Fax DCS: %s\n", 00250 codec->faxdcs); 00251 } 00252 } 00253 00254 static int JBIGVGetField(TIFF* tif, ttag_t tag, va_list ap) 00255 { 00256 JBIGState* codec = GetJBIGState(tif); 00257 00258 switch (tag) 00259 { 00260 case TIFFTAG_FAXRECVPARAMS: 00261 *va_arg(ap, uint32*) = codec->recvparams; 00262 break; 00263 00264 case TIFFTAG_FAXSUBADDRESS: 00265 *va_arg(ap, char**) = codec->subaddress; 00266 break; 00267 00268 case TIFFTAG_FAXRECVTIME: 00269 *va_arg(ap, uint32*) = codec->recvtime; 00270 break; 00271 00272 case TIFFTAG_FAXDCS: 00273 *va_arg(ap, char**) = codec->faxdcs; 00274 break; 00275 00276 default: 00277 return (*codec->vgetparent)(tif, tag, ap); 00278 } 00279 00280 return 1; 00281 } 00282 00283 static int JBIGVSetField(TIFF* tif, ttag_t tag, va_list ap) 00284 { 00285 JBIGState* codec = GetJBIGState(tif); 00286 00287 switch (tag) 00288 { 00289 case TIFFTAG_FAXRECVPARAMS: 00290 codec->recvparams = va_arg(ap, uint32); 00291 break; 00292 00293 case TIFFTAG_FAXSUBADDRESS: 00294 _TIFFsetString(&codec->subaddress, va_arg(ap, char*)); 00295 break; 00296 00297 case TIFFTAG_FAXRECVTIME: 00298 codec->recvtime = va_arg(ap, uint32); 00299 break; 00300 00301 case TIFFTAG_FAXDCS: 00302 _TIFFsetString(&codec->faxdcs, va_arg(ap, char*)); 00303 break; 00304 00305 default: 00306 return (*codec->vsetparent)(tif, tag, ap); 00307 } 00308 00309 TIFFSetFieldBit(tif, _TIFFFieldWithTag(tif, tag)->field_bit); 00310 tif->tif_flags |= TIFF_DIRTYDIRECT; 00311 return 1; 00312 } 00313 00314 int TIFFInitJBIG(TIFF* tif, int scheme) 00315 { 00316 JBIGState* codec = NULL; 00317 00318 assert(scheme == COMPRESSION_JBIG); 00319 00320 /* 00321 * Merge codec-specific tag information. 00322 */ 00323 if (!_TIFFMergeFieldInfo(tif, jbigFieldInfo, 00324 TIFFArrayCount(jbigFieldInfo))) { 00325 TIFFErrorExt(tif->tif_clientdata, "TIFFInitJBIG", 00326 "Merging JBIG codec-specific tags failed"); 00327 return 0; 00328 } 00329 00330 /* Allocate memory for the JBIGState structure.*/ 00331 tif->tif_data = (tdata_t)_TIFFmalloc(sizeof(JBIGState)); 00332 if (tif->tif_data == NULL) 00333 { 00334 TIFFError("TIFFInitJBIG", "Not enough memory for JBIGState"); 00335 return 0; 00336 } 00337 _TIFFmemset(tif->tif_data, 0, sizeof(JBIGState)); 00338 codec = GetJBIGState(tif); 00339 00340 /* Initialize codec private fields */ 00341 codec->recvparams = 0; 00342 codec->subaddress = NULL; 00343 codec->faxdcs = NULL; 00344 codec->recvtime = 0; 00345 00346 /* 00347 * Override parent get/set field methods. 00348 */ 00349 codec->vgetparent = tif->tif_tagmethods.vgetfield; 00350 codec->vsetparent = tif->tif_tagmethods.vsetfield; 00351 tif->tif_tagmethods.vgetfield = JBIGVGetField; 00352 tif->tif_tagmethods.vsetfield = JBIGVSetField; 00353 tif->tif_tagmethods.printdir = JBIGPrintDir; 00354 00355 /* 00356 * These flags are set so the JBIG Codec can control when to reverse 00357 * bits and when not to and to allow the jbig decoder and bit reverser 00358 * to write to memory when necessary. 00359 */ 00360 tif->tif_flags |= TIFF_NOBITREV; 00361 tif->tif_flags &= ~TIFF_MAPPED; 00362 00363 /* Setup the function pointers for encode, decode, and cleanup. */ 00364 tif->tif_setupdecode = JBIGSetupDecode; 00365 tif->tif_decodestrip = JBIGDecode; 00366 00367 tif->tif_setupencode = JBIGSetupEncode; 00368 tif->tif_encodestrip = JBIGEncode; 00369 00370 tif->tif_cleanup = JBIGCleanup; 00371 00372 return 1; 00373 } 00374 00375 #endif /* JBIG_SUPPORT */ 00376 00377 /* vim: set ts=8 sts=8 sw=8 noet: */ 00378 00379 /* 00380 * Local Variables: 00381 * mode: c 00382 * c-basic-offset: 8 00383 * fill-column: 78 00384 * End: 00385 */ Generated on Sat May 26 2012 04:18:23 for ReactOS by
1.7.6.1
|