• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kviewshell

JPEGDecoder.cpp

Go to the documentation of this file.
00001 //C-  -*- C++ -*-
00002 //C- -------------------------------------------------------------------
00003 //C- DjVuLibre-3.5
00004 //C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
00005 //C- Copyright (c) 2001  AT&T
00006 //C-
00007 //C- This software is subject to, and may be distributed under, the
00008 //C- GNU General Public License, Version 2. The license should have
00009 //C- accompanied the software or you may obtain a copy of the license
00010 //C- from the Free Software Foundation at http://www.fsf.org .
00011 //C-
00012 //C- This program is distributed in the hope that it will be useful,
00013 //C- but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //C- GNU General Public License for more details.
00016 //C- 
00017 //C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
00018 //C- distributed by Lizardtech Software.  On July 19th 2002, Lizardtech 
00019 //C- Software authorized us to replace the original DjVu(r) Reference 
00020 //C- Library notice by the following text (see doc/lizard2002.djvu):
00021 //C-
00022 //C-  ------------------------------------------------------------------
00023 //C- | DjVu (r) Reference Library (v. 3.5)
00024 //C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
00025 //C- | The DjVu Reference Library is protected by U.S. Pat. No.
00026 //C- | 6,058,214 and patents pending.
00027 //C- |
00028 //C- | This software is subject to, and may be distributed under, the
00029 //C- | GNU General Public License, Version 2. The license should have
00030 //C- | accompanied the software or you may obtain a copy of the license
00031 //C- | from the Free Software Foundation at http://www.fsf.org .
00032 //C- |
00033 //C- | The computer code originally released by LizardTech under this
00034 //C- | license and unmodified by other parties is deemed "the LIZARDTECH
00035 //C- | ORIGINAL CODE."  Subject to any third party intellectual property
00036 //C- | claims, LizardTech grants recipient a worldwide, royalty-free, 
00037 //C- | non-exclusive license to make, use, sell, or otherwise dispose of 
00038 //C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 
00039 //C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 
00040 //C- | General Public License.   This grant only confers the right to 
00041 //C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 
00042 //C- | the extent such infringement is reasonably necessary to enable 
00043 //C- | recipient to make, have made, practice, sell, or otherwise dispose 
00044 //C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 
00045 //C- | any greater extent that may be necessary to utilize further 
00046 //C- | modifications or combinations.
00047 //C- |
00048 //C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
00049 //C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
00050 //C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
00051 //C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
00052 //C- +------------------------------------------------------------------
00053 // 
00054 // $Id: JPEGDecoder.cpp,v 1.8 2003/11/07 22:08:22 leonb Exp $
00055 // $Name: release_3_5_15 $
00056 
00057 #ifdef HAVE_CONFIG_H
00058 # include "config.h"
00059 #endif
00060 #if NEED_GNUG_PRAGMAS
00061 # pragma implementation
00062 #endif
00063 
00064 #ifdef NEED_JPEG_DECODER
00065 
00066 #include "JPEGDecoder.h"
00067 
00068 #ifdef __cplusplus
00069 extern "C" {
00070 #endif
00071 #undef HAVE_STDLIB_H
00072 #undef HAVE_STDDEF_H
00073 #include <stdio.h>
00074 #include <jconfig.h>
00075 #include <jpeglib.h>
00076 #include <jerror.h>
00077 #ifdef __cplusplus
00078 }
00079 #endif
00080 
00081 #include "ByteStream.h"
00082 #include "GPixmap.h"
00083 #ifdef LIBJPEGNAME
00084 #include "DjVuDynamic.h"
00085 #include "GString.h"
00086 #endif // LIBJPEGNAME
00087 
00088 
00089 
00090 #ifdef HAVE_NAMESPACES
00091 namespace DJVU {
00092 # ifdef NOT_DEFINED // Just to fool emacs c++ mode
00093 }
00094 #endif
00095 #endif
00096 
00097 
00098 class JPEGDecoder::Impl : public JPEGDecoder
00099 {
00100 public:
00101   static void jpeg_byte_stream_src(j_decompress_ptr, ByteStream &);
00102 };
00103 
00104 extern "C"
00105 {
00106 
00107 struct djvu_error_mgr
00108 {
00109   struct jpeg_error_mgr pub;  /* "public" fields */
00110 
00111   jmp_buf setjmp_buffer;  /* for return to caller */
00112 };
00113 
00114 typedef struct djvu_error_mgr * djvu_error_ptr;
00115 
00116 METHODDEF(void)
00117 djvu_error_exit (j_common_ptr cinfo)
00118 {
00119   /* cinfo->err really points to a djvu_error_mgr struct, so coerce pointer */
00120   djvu_error_ptr djvuerr = (djvu_error_ptr) cinfo->err;
00121 
00122   /* Always display the message. */
00123   /* We could postpone this until after returning, if we chose. */
00124   (*cinfo->err->output_message) (cinfo);
00125 
00126   /* Return control to the setjmp point */
00127   longjmp(djvuerr->setjmp_buffer, 1);
00128 }
00129 
00130 }
00131 
00132 GP<GPixmap>
00133 JPEGDecoder::decode(ByteStream & bs )
00134 {
00135   GP<GPixmap> retval=GPixmap::create();
00136   G_TRY
00137   {
00138     decode(bs,*retval);
00139   } G_CATCH_ALL
00140   {
00141     retval=0;
00142   }
00143   G_ENDCATCH;
00144   return retval;
00145 }
00146 
00147 void
00148 JPEGDecoder::decode(ByteStream & bs,GPixmap &pix)
00149 {
00150   struct jpeg_decompress_struct cinfo;
00151 
00152   /* We use our private extension JPEG error handler. */
00153   struct djvu_error_mgr jerr;
00154 
00155   JSAMPARRAY buffer;    /* Output row buffer */
00156   int row_stride;   /* physical row width in output buffer */
00157   int full_buf_size;
00158   int isGrey,i;
00159 
00160   cinfo.err = jpeg_std_error(&jerr.pub);
00161 
00162   jerr.pub.error_exit = djvu_error_exit;
00163 
00164   if (setjmp(jerr.setjmp_buffer))
00165   {
00166 
00167     jpeg_destroy_decompress(&cinfo);
00168     G_THROW( ERR_MSG("GPixmap.unk_PPM") );
00169   }
00170 
00171   jpeg_create_decompress(&cinfo);
00172 
00173   Impl::jpeg_byte_stream_src(&cinfo, bs);
00174 
00175   (void) jpeg_read_header(&cinfo, TRUE);
00176 
00177   jpeg_start_decompress(&cinfo);
00178   
00179   /* We may need to do some setup of our own at this point before reading
00180    * the data.  After jpeg_start_decompress() we have the correct scaled
00181    * output image dimensions available, as well as the output colormap
00182    * if we asked for color quantization.
00183    * In this example, we need to make an output work buffer of the right size.
00184    */
00185 
00186   /* JSAMPLEs per row in output buffer */
00187   row_stride = cinfo.output_width * cinfo.output_components;
00188   full_buf_size = row_stride * cinfo.output_height;
00189 
00190   /* Make a one-row-high sample array that will go away when done with image */
00191   buffer = (*cinfo.mem->alloc_sarray)
00192     ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
00193 
00194   GP<ByteStream> goutputBlock=ByteStream::create();
00195   ByteStream &outputBlock=*goutputBlock;
00196   outputBlock.format("P6\n%d %d\n%d\n",cinfo.output_width, 
00197                                  cinfo.output_height,255);
00198 
00199   isGrey = ( cinfo.out_color_space == JCS_GRAYSCALE) ? 1 : 0; 
00200 
00201   while (cinfo.output_scanline < cinfo.output_height)
00202   {
00203     (void) jpeg_read_scanlines(&cinfo, buffer, 1);
00204 
00205     if ( isGrey == 1 )
00206     {
00207       for (i=0; i<row_stride; i++)
00208       {
00209         outputBlock.write8((char)buffer[0][i]); 
00210         outputBlock.write8((char)buffer[0][i]); 
00211         outputBlock.write8((char)buffer[0][i]); 
00212       }
00213     }else
00214     {
00215       for (i=0; i<row_stride; i++) 
00216         outputBlock.write8((char)buffer[0][i]); 
00217     }
00218   }
00219 
00220   (void) jpeg_finish_decompress(&cinfo);   
00221 
00222   jpeg_destroy_decompress(&cinfo);
00223   
00224   outputBlock.seek(0,SEEK_SET);
00225 
00226   pix.init(outputBlock);
00227 }         
00228 
00229 /*** From here onwards code is to make ByteStream as the data
00230      source for the JPEG library */
00231 
00232 extern "C"
00233 {
00234 
00235 typedef struct
00236 {
00237   struct jpeg_source_mgr pub; /* public fields */
00238 
00239   ByteStream * byteStream;    /* source stream */
00240   JOCTET * buffer;    /* start of buffer */
00241   boolean start_of_stream;  
00242 } byte_stream_src_mgr;
00243                 
00244 
00245 typedef byte_stream_src_mgr * byte_stream_src_ptr; 
00246 
00247 #define INPUT_BUF_SIZE   4096
00248 
00249 METHODDEF(void)
00250 init_source (j_decompress_ptr cinfo)
00251 {
00252   byte_stream_src_ptr src = (byte_stream_src_ptr) cinfo->src;
00253 
00254   src->start_of_stream = TRUE;
00255 }
00256 
00257 METHODDEF(boolean)
00258 fill_input_buffer (j_decompress_ptr cinfo)
00259 {
00260   byte_stream_src_ptr src = (byte_stream_src_ptr) cinfo->src;
00261   size_t nbytes;
00262 
00263   nbytes = src->byteStream->readall(src->buffer, INPUT_BUF_SIZE);
00264 
00265   if (nbytes <= 0)
00266   {
00267     if (src->start_of_stream) /* Treat empty input as fatal error */
00268       ERREXIT(cinfo, JERR_INPUT_EMPTY);
00269     WARNMS(cinfo, JWRN_JPEG_EOF);
00270     /* Insert a fake EOI marker */
00271     src->buffer[0] = (JOCTET) 0xFF;
00272     src->buffer[1] = (JOCTET) JPEG_EOI;
00273     nbytes = 2;
00274   }
00275 
00276   src->pub.next_input_byte = src->buffer;
00277   src->pub.bytes_in_buffer = nbytes;
00278   src->start_of_stream = FALSE; 
00279 
00280   return TRUE;
00281 }
00282 
00283 
00284 METHODDEF(void)
00285 skip_input_data (j_decompress_ptr cinfo, long num_bytes)
00286 {
00287   byte_stream_src_ptr src = (byte_stream_src_ptr) cinfo->src;
00288 
00289   if (num_bytes > (long) src->pub.bytes_in_buffer)
00290   {
00291     src->byteStream->seek((num_bytes - src->pub.bytes_in_buffer), SEEK_CUR);
00292     (void) fill_input_buffer(cinfo);
00293   }else
00294   {
00295     src->pub.bytes_in_buffer -= num_bytes;
00296     src->pub.next_input_byte += num_bytes;
00297   }
00298 }
00299                  
00300 METHODDEF(void)
00301 term_source (j_decompress_ptr cinfo)
00302 {
00303   /* no work necessary here */
00304 }
00305 
00306 }
00307 
00308 void
00309 JPEGDecoder::Impl::jpeg_byte_stream_src(j_decompress_ptr cinfo,ByteStream &bs)
00310 {
00311   byte_stream_src_ptr src;
00312 
00313   if (cinfo->src == NULL)
00314   { /* first time for this JPEG object? */
00315     cinfo->src = (struct jpeg_source_mgr *)      
00316       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00317           sizeof(byte_stream_src_mgr));
00318     src = (byte_stream_src_ptr) cinfo->src;
00319     src->buffer = (JOCTET *)
00320       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00321           INPUT_BUF_SIZE * sizeof(JOCTET));
00322   }
00323 
00324   src = (byte_stream_src_ptr) cinfo->src;
00325   src->pub.init_source = init_source;
00326   src->pub.fill_input_buffer = fill_input_buffer;
00327   src->pub.skip_input_data = skip_input_data;
00328   src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
00329   src->pub.term_source = term_source;
00330   src->byteStream = &bs;
00331   src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
00332   src->pub.next_input_byte = NULL; /* until buffer loaded */
00333 }
00334 
00335 #ifdef LIBJPEGNAME
00336 void *
00337 JPEGDecoder::jpeg_lookup(const GUTF8String &name)
00338 {
00339   static DjVuDynamic lib(GUTF8String(LIBJPEGNAME));
00340   void *sym=lib.lookup(name);
00341   if(!sym)
00342     G_THROW(ERR_MSG("DjVuFile.JPEG_bg2"));
00343   return sym;
00344 }
00345 
00346 jpeg_error_mgr *
00347 JPEGDecoder::jpeg_std_error(jpeg_error_mgr *x)
00348 {
00349   static void *sym=jpeg_lookup("jpeg_std_error");
00350   return ((jpeg_error_mgr *(*)(jpeg_error_mgr *))sym)(x);
00351 }
00352 
00353 void
00354 JPEGDecoder::jpeg_CreateDecompress(jpeg_decompress_struct *x,int v, size_t s)
00355 {
00356   static void *sym=jpeg_lookup("jpeg_CreateDecompress");
00357   ((void (*)(jpeg_decompress_struct *,int,size_t))sym)(x,v,s);
00358 }
00359 
00360 void
00361 JPEGDecoder::jpeg_destroy_decompress(j_decompress_ptr x)
00362 {
00363   static void *sym=jpeg_lookup("jpeg_destroy_decompress");
00364   ((void (*)(j_decompress_ptr))sym)(x);
00365 }
00366 
00367 int
00368 JPEGDecoder::jpeg_read_header(j_decompress_ptr x,boolean y)
00369 {
00370   static void *sym=jpeg_lookup("jpeg_read_header");
00371   return ((int (*)(j_decompress_ptr,boolean))sym)(x,y);
00372 }
00373 
00374 JDIMENSION
00375 JPEGDecoder::jpeg_read_scanlines(j_decompress_ptr x,JSAMPARRAY y,JDIMENSION z)
00376 {
00377   static void *sym=jpeg_lookup("jpeg_read_scanlines");
00378   return ((JDIMENSION (*)(j_decompress_ptr,JSAMPARRAY,JDIMENSION))sym)(x,y,z);
00379 }
00380 
00381 boolean
00382 JPEGDecoder::jpeg_finish_decompress(j_decompress_ptr x)
00383 {
00384   static void *sym=jpeg_lookup("jpeg_finish_decompress");
00385   return ((boolean (*)(j_decompress_ptr))sym)(x);
00386 }
00387 
00388 boolean
00389 JPEGDecoder::jpeg_resync_to_restart(jpeg_decompress_struct *x,int d)
00390 {
00391   static void *sym=jpeg_lookup("jpeg_resync_to_restart");
00392   return ((boolean (*)(jpeg_decompress_struct *,int))sym)(x,d);
00393 }
00394 
00395 boolean
00396 JPEGDecoder::jpeg_start_decompress(j_decompress_ptr x)
00397 {
00398   static void *sym=jpeg_lookup("jpeg_start_decompress");
00399   return ((boolean (*)(j_decompress_ptr))sym)(x);
00400 }
00401 
00402 #endif // LIBJPEGNAME
00403 
00404 
00405 #ifdef HAVE_NAMESPACES
00406 }
00407 # ifndef NOT_USING_DJVU_NAMESPACE
00408 using namespace DJVU;
00409 # endif
00410 #endif
00411 
00412 #endif
00413 

kviewshell

Skip menu "kviewshell"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • kviewshell
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal