OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimJpegMemSrc.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------
2 // License: See top level LICENSE.txt file.
3 //
4 // Author: David Burken, original code from Thomas G. Lane (memsrc.c)
5 //
6 // Description:
7 // Code to use jpeg-6b library to read jpeg image from memory.
8 //----------------------------------------------------------------------------
9 // $Id$
10 
11 //---
12 // Original code from Thomas G. Lane, header comment follows.
13 //---
14 
15 /*
16  * memsrc.c
17  *
18  * Copyright (C) 1994-1996, Thomas G. Lane.
19  * This file is part of the Independent JPEG Group's software.
20  * For conditions of distribution and use, see the accompanying README file.
21  *
22  * This file contains decompression data source routines for the case of
23  * reading JPEG data from a memory buffer that is preloaded with the entire
24  * JPEG file. This would not seem especially useful at first sight, but
25  * a number of people have asked for it.
26  * This is really just a stripped-down version of jdatasrc.c. Comparison
27  * of this code with jdatasrc.c may be helpful in seeing how to make
28  * custom source managers for other purposes.
29  */
30 
31 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
32 /* #include <jinclude.h> */
33 
35 #include <cstdio> /* FILE* */
36 #include <csetjmp>
37 #include <jpeglib.h>
38 #include <jerror.h>
39 
40 extern "C"
41 {
42 
43 /* Expanded data source object for memory input */
44 
46 {
47  struct jpeg_source_mgr pub; /* public fields */
48  JOCTET eoi_buffer[2]; /* a place to put a dummy EOI */
49 };
51 
52 
53 /*
54  * Initialize source --- called by jpeg_read_header
55  * before any data is actually read.
56  */
57 
58 void init_source (j_decompress_ptr /* cinfo */)
59 {
60  /* No work, since jpeg_memory_src set up the buffer pointer and count.
61  * Indeed, if we want to read multiple JPEG images from one buffer,
62  * this *must* not do anything to the pointer.
63  */
64 }
65 
66 
67 /*
68  * Fill the input buffer --- called whenever buffer is emptied.
69  *
70  * In this application, this routine should never be called; if it is called,
71  * the decompressor has overrun the end of the input buffer, implying we
72  * supplied an incomplete or corrupt JPEG datastream. A simple error exit
73  * might be the most appropriate response.
74  *
75  * But what we choose to do in this code is to supply dummy EOI markers
76  * in order to force the decompressor to finish processing and supply
77  * some sort of output image, no matter how corrupted.
78  */
79 boolean fill_input_buffer (j_decompress_ptr cinfo)
80 {
82 
83  WARNMS(cinfo, JWRN_JPEG_EOF);
84 
85  /* Create a fake EOI marker */
86  src->eoi_buffer[0] = (JOCTET) 0xFF;
87  src->eoi_buffer[1] = (JOCTET) JPEG_EOI;
88  src->pub.next_input_byte = src->eoi_buffer;
89  src->pub.bytes_in_buffer = 2;
90 
91  return TRUE;
92 }
93 
94 
95 /*
96  * Skip data --- used to skip over a potentially large amount of
97  * uninteresting data (such as an APPn marker).
98  *
99  * If we overrun the end of the buffer, we let fill_input_buffer deal with
100  * it. An extremely large skip could cause some time-wasting here, but
101  * it really isn't supposed to happen ... and the decompressor will never
102  * skip more than 64K anyway.
103  */
104 void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
105 {
106  ossimJpegSourceMgrPtr src = (ossimJpegSourceMgrPtr) cinfo->src;
107 
108  if (num_bytes > 0) {
109  while (num_bytes > (long) src->pub.bytes_in_buffer) {
110  num_bytes -= (long) src->pub.bytes_in_buffer;
111  (void) fill_input_buffer(cinfo);
112  /* note we assume that fill_input_buffer will never return FALSE,
113  * so suspension need not be handled.
114  */
115  }
116  src->pub.next_input_byte += (size_t) num_bytes;
117  src->pub.bytes_in_buffer -= (size_t) num_bytes;
118  }
119 }
120 
121 
122 /*
123  * An additional method that can be provided by data source modules is the
124  * resync_to_restart method for error recovery in the presence of RST markers.
125  * For the moment, this source module just uses the default resync method
126  * provided by the JPEG library. That method assumes that no backtracking
127  * is possible.
128  */
129 
130 
131 /*
132  * Terminate source --- called by jpeg_finish_decompress
133  * after all data has been read. Often a no-op.
134  *
135  * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
136  * application must deal with any cleanup that should happen even
137  * for error exit.
138  */
139 void term_source (j_decompress_ptr /* cinfo */)
140 {
141  /* no work necessary here */
142 }
143 
144 
145 /*
146  * Prepare for input from a memory buffer.
147  */
148 void ossimJpegMemorySrc (jpeg_decompress_struct* cinfo,
149  const ossim_uint8* buffer,
150  std::size_t bufsize)
151 {
153 
154  /* The source object is made permanent so that a series of JPEG images
155  * can be read from a single buffer by calling jpeg_memory_src
156  * only before the first one.
157  * This makes it unsafe to use this manager and a different source
158  * manager serially with the same JPEG object. Caveat programmer.
159  */
160  if (cinfo->src == NULL) { /* first time for this JPEG object? */
161  cinfo->src = (struct jpeg_source_mgr *)
162  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
163  sizeof(ossimJpegSourceMgr));
164  }
165 
166  src = (ossimJpegSourceMgrPtr) cinfo->src;
167  src->pub.init_source = init_source;
168  src->pub.fill_input_buffer = fill_input_buffer;
169  src->pub.skip_input_data = skip_input_data;
170  src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
171  src->pub.term_source = term_source;
172 
173  src->pub.next_input_byte = buffer;
174  src->pub.bytes_in_buffer = bufsize;
175 }
176 }
177 
ossimJpegSourceMgr * ossimJpegSourceMgrPtr
void init_source(j_decompress_ptr)
void term_source(j_decompress_ptr)
void ossimJpegMemorySrc(jpeg_decompress_struct *cinfo, const ossim_uint8 *buffer, std::size_t bufsize)
Method which uses memory instead of a FILE* to read from.
boolean fill_input_buffer(j_decompress_ptr cinfo)
if(yy_init)
void skip_input_data(j_decompress_ptr cinfo, long num_bytes)
struct jpeg_source_mgr pub
unsigned char ossim_uint8