OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimJpegStdIOSrc.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16  =========================================================================*/
17 
18 /*
19  * jdatasrc.c
20  *
21  * Copyright (C) 1994-1996, Thomas G. Lane.
22  * This file is part of the Independent JPEG Group's software.
23  * For conditions of distribution and use, see the accompanying README file.
24  *
25  * This file contains decompression data source routines for the case of
26  * reading JPEG data from a file (or any stdio stream). While these routines
27  * are sufficient for most applications, some will want to use a different
28  * source manager.
29  * IMPORTANT: we assume that fread() will correctly transcribe an array of
30  * JOCTETs from 8-bit-wide elements on external storage. If char is wider
31  * than 8 bits on your machine, you may need to do some tweaking.
32  */
33 
34 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
35 
37 
38 #include <csetjmp>
39 #include <jpeglib.h>
40 #include <jerror.h>
41 
42 
43 #define INPUT_BUF_SIZE 4096 /* choose an efficiently fread'able size */
44 
45 #define JFREAD(file,buf,sizeofbuf) \
46 ((size_t) fread((void *) (buf), (size_t) 1, (size_t) (sizeofbuf), (file)))
47 
48 /*
49  * In ANSI C, and indeed any rational implementation, size_t is also the
50  * type returned by sizeof(). However, it seems there are some irrational
51  * implementations out there, in which sizeof() returns an int even though
52  * size_t is defined as long or unsigned long. To ensure consistent results
53  * we always use this SIZEOF() macro in place of using sizeof() directly.
54  */
55 
56 #define SIZEOF(object) ((size_t) sizeof(object))
57 
58 extern "C"
59 {
60 
61  /* Expanded data source object for stdio input */
62 
63  typedef struct {
64  struct jpeg_source_mgr pub; /* public fields */
65 
66  FILE * infile; /* source stream */
67  JOCTET * buffer; /* start of buffer */
68  boolean start_of_file; /* have we gotten any data yet? */
70 
72 
73 
74 
75  /*
76  * Initialize source --- called by jpeg_read_header
77  * before any data is actually read.
78  */
79 
80  void ossimJpegStdIOSrc_init_source (j_decompress_ptr cinfo)
81  {
83 
84  /* We reset the empty-input-file flag for each image,
85  * but we don't clear the input buffer.
86  * This is correct behavior for reading a series of images from one source.
87  */
88  src->start_of_file = TRUE;
89  }
90 
91 
92  /*
93  * Fill the input buffer --- called whenever buffer is emptied.
94  *
95  * In typical applications, this should read fresh data into the buffer
96  * (ignoring the current state of next_input_byte & bytes_in_buffer),
97  * reset the pointer & count to the start of the buffer, and return TRUE
98  * indicating that the buffer has been reloaded. It is not necessary to
99  * fill the buffer entirely, only to obtain at least one more byte.
100  *
101  * There is no such thing as an EOF return. If the end of the file has been
102  * reached, the routine has a choice of ERREXIT() or inserting fake data into
103  * the buffer. In most cases, generating a warning message and inserting a
104  * fake EOI marker is the best course of action --- this will allow the
105  * decompressor to output however much of the image is there. However,
106  * the resulting error message is misleading if the real problem is an empty
107  * input file, so we handle that case specially.
108  *
109  * In applications that need to be able to suspend compression due to input
110  * not being available yet, a FALSE return indicates that no more data can be
111  * obtained right now, but more may be forthcoming later. In this situation,
112  * the decompressor will return to its caller (with an indication of the
113  * number of scanlines it has read, if any). The application should resume
114  * decompression after it has loaded more data into the input buffer. Note
115  * that there are substantial restrictions on the use of suspension --- see
116  * the documentation.
117  *
118  * When suspending, the decompressor will back up to a convenient restart point
119  * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
120  * indicate where the restart point will be if the current call returns FALSE.
121  * Data beyond this point must be rescanned after resumption, so move it to
122  * the front of the buffer rather than discarding it.
123  */
124 
125  boolean
126  ossimJpegStdIOSrc_fill_input_buffer (j_decompress_ptr cinfo)
127  {
129  size_t nbytes;
130 
131  nbytes = JFREAD(src->infile, src->buffer, INPUT_BUF_SIZE);
132 
133  if (nbytes <= 0) {
134  if (src->start_of_file) /* Treat empty input file as fatal error */
135  ERREXIT(cinfo, JERR_INPUT_EMPTY);
136  WARNMS(cinfo, JWRN_JPEG_EOF);
137  /* Insert a fake EOI marker */
138  src->buffer[0] = (JOCTET) 0xFF;
139  src->buffer[1] = (JOCTET) JPEG_EOI;
140  nbytes = 2;
141  }
142 
143  src->pub.next_input_byte = src->buffer;
144  src->pub.bytes_in_buffer = nbytes;
145  src->start_of_file = FALSE;
146 
147  return TRUE;
148  }
149 
150 
151  /*
152  * Skip data --- used to skip over a potentially large amount of
153  * uninteresting data (such as an APPn marker).
154  *
155  * Writers of suspendable-input applications must note that skip_input_data
156  * is not granted the right to give a suspension return. If the skip extends
157  * beyond the data currently in the buffer, the buffer can be marked empty so
158  * that the next read will cause a fill_input_buffer call that can suspend.
159  * Arranging for additional bytes to be discarded before reloading the input
160  * buffer is the application writer's problem.
161  */
162 
163  void
164  ossimJpegStdIOSrc_skip_input_data (j_decompress_ptr cinfo, long num_bytes)
165  {
167 
168  /* Just a dumb implementation for now. Could use fseek() except
169  * it doesn't work on pipes. Not clear that being smart is worth
170  * any trouble anyway --- large skips are infrequent.
171  */
172  if (num_bytes > 0) {
173  while (num_bytes > (long) src->pub.bytes_in_buffer) {
174  num_bytes -= (long) src->pub.bytes_in_buffer;
176  /* note we assume that fill_input_buffer will never return FALSE,
177  * so suspension need not be handled.
178  */
179  }
180  src->pub.next_input_byte += (size_t) num_bytes;
181  src->pub.bytes_in_buffer -= (size_t) num_bytes;
182  }
183  }
184 
185 
186  /*
187  * An additional method that can be provided by data source modules is the
188  * resync_to_restart method for error recovery in the presence of RST markers.
189  * For the moment, this source module just uses the default resync method
190  * provided by the JPEG library. That method assumes that no backtracking
191  * is possible.
192  */
193 
194 
195  /*
196  * Terminate source --- called by jpeg_finish_decompress
197  * after all data has been read. Often a no-op.
198  *
199  * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
200  * application must deal with any cleanup that should happen even
201  * for error exit.
202  */
203 
204  void
205  ossimJpegStdIOSrc_term_source (j_decompress_ptr cinfo)
206  {
207  (void)cinfo;
208  /* no work necessary here */
209  }
210 
211 
212  /*
213  * Prepare for input from a stdio stream.
214  * The caller must have already opened the stream, and is responsible
215  * for closing it after finishing decompression.
216  */
217 
218  /* void ossimJpegStdIOSrc (j_decompress_ptr cinfo, FILE * infile) */
219  void ossimJpegStdIOSrc (jpeg_decompress_struct* cinfo, FILE * infile)
220  {
222 
223  /* The source object and input buffer are made permanent so that a series
224  * of JPEG images can be read from the same file by calling jpeg_stdio_src
225  * only before the first one. (If we discarded the buffer at the end of
226  * one image, we'd likely lose the start of the next one.)
227  * This makes it unsafe to use this manager and a different source
228  * manager serially with the same JPEG object. Caveat programmer.
229  */
230  if (cinfo->src == NULL) { /* first time for this JPEG object? */
231  cinfo->src = (struct jpeg_source_mgr *)
232  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
234  src = (ossimJpegStdIOSourceMgrPtr) cinfo->src;
235  src->buffer = (JOCTET *)
236  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
237  INPUT_BUF_SIZE * SIZEOF(JOCTET));
238  }
239 
240  src = (ossimJpegStdIOSourceMgrPtr) cinfo->src;
241  src->pub.init_source = ossimJpegStdIOSrc_init_source;
242  src->pub.fill_input_buffer = ossimJpegStdIOSrc_fill_input_buffer;
243  src->pub.skip_input_data = ossimJpegStdIOSrc_skip_input_data;
244  src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
245  src->pub.term_source = ossimJpegStdIOSrc_term_source;
246  src->infile = infile;
247  src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
248  src->pub.next_input_byte = NULL; /* until buffer loaded */
249  }
250 }
#define SIZEOF(object)
#define INPUT_BUF_SIZE
for jmp_buf
void ossimJpegStdIOSrc_term_source(j_decompress_ptr cinfo)
ossimJpegStdIOSourceMgr * ossimJpegStdIOSourceMgrPtr
struct jpeg_source_mgr pub
void ossimJpegStdIOSrc_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
#define JFREAD(file, buf, sizeofbuf)
void ossimJpegStdIOSrc(jpeg_decompress_struct *cinfo, FILE *infile)
Method which uses memory instead of a FILE* to read from.
if(yy_init)
void ossimJpegStdIOSrc_init_source(j_decompress_ptr cinfo)
boolean ossimJpegStdIOSrc_fill_input_buffer(j_decompress_ptr cinfo)