OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimJpegMemSrc12.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 // Specialized for 12 bit compiled jpeg library.
9 //----------------------------------------------------------------------------
10 // $Id$
11 
12 //---
13 // Original code from Thomas G. Lane, header comment follows.
14 //---
15 
16 /*
17  * memsrc.c
18  *
19  * Copyright (C) 1994-1996, Thomas G. Lane.
20  * This file is part of the Independent JPEG Group's software.
21  * For conditions of distribution and use, see the accompanying README file.
22  *
23  * This file contains decompression data source routines for the case of
24  * reading JPEG data from a memory buffer that is preloaded with the entire
25  * JPEG file. This would not seem especially useful at first sight, but
26  * a number of people have asked for it.
27  * This is really just a stripped-down version of jdatasrc.c. Comparison
28  * of this code with jdatasrc.c may be helpful in seeing how to make
29  * custom source managers for other purposes.
30  */
31 
32 #include "ossimJpegMemSrc12.h" \
33  \
37 #include <cstdio>
38 #include <csetjmp>
39 #include <jpeg12/jpeglib.h>
40 #include <jpeg12/jerror.h>
41 
42 extern "C"
43 {
44 /* Expanded data source object for memory input */
46 {
47  struct jpeg12_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_source12 (j12_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_buffer12 (j12_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  * Skip data --- used to skip over a potentially large amount of
96  * uninteresting data (such as an APPn marker).
97  *
98  * If we overrun the end of the buffer, we let fill_input_buffer deal with
99  * it. An extremely large skip could cause some time-wasting here, but
100  * it really isn't supposed to happen ... and the decompressor will never
101  * skip more than 64K anyway.
102  */
103 void skip_input_data12 (j12_decompress_ptr cinfo, long num_bytes)
104 {
106 
107  if (num_bytes > 0)
108  {
109  while (num_bytes > (long) src->pub.bytes_in_buffer)
110  {
111  num_bytes -= (long) src->pub.bytes_in_buffer;
112  (void) fill_input_buffer12(cinfo);
113  /* note we assume that fill_input_buffer will never return FALSE,
114  * so suspension need not be handled.
115  */
116  }
117  src->pub.next_input_byte += (size_t) num_bytes;
118  src->pub.bytes_in_buffer -= (size_t) num_bytes;
119  }
120 }
121 
122 
123 /*
124  * An additional method that can be provided by data source modules is the
125  * resync_to_restart method for error recovery in the presence of RST markers.
126  * For the moment, this source module just uses the default resync method
127  * provided by the JPEG library. That method assumes that no backtracking
128  * is possible.
129  */
130 
131 
132 /*
133  * Terminate source --- called by jpeg_finish_decompress
134  * after all data has been read. Often a no-op.
135  *
136  * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
137  * application must deal with any cleanup that should happen even
138  * for error exit.
139  */
140 void term_source12 (j12_decompress_ptr /* cinfo */)
141 {
142  /* no work necessary here */
143 }
144 
145 
146 /*
147  * Prepare for input from a memory buffer.
148  */
149 void ossimJpegMemorySrc12(j12_decompress_ptr cinfo,
150  const ossim_uint8* buffer,
151  std::size_t bufsize)
152 {
154 
155  /* The source object is made permanent so that a series of JPEG images
156  * can be read from a single buffer by calling jpeg_memory_src
157  * only before the first one.
158  * This makes it unsafe to use this manager and a different source
159  * manager serially with the same JPEG object. Caveat programmer.
160  */
161  if (cinfo->src == NULL)
162  { /* first time for this JPEG object? */
163  cinfo->src = (struct jpeg12_source_mgr *)
164  (*cinfo->mem->alloc_small) ((j12_common_ptr) cinfo, JPOOL_PERMANENT,
165  sizeof(ossimJpegSourceMgr12));
166  }
167 
168  src = (ossimJpegSourceMgrPtr12) cinfo->src;
169  src->pub.init_source = init_source12;
170  src->pub.fill_input_buffer = fill_input_buffer12;
171  src->pub.skip_input_data = skip_input_data12;
172  src->pub.resync_to_restart = jpeg12_resync_to_restart; /* use default method */
173  src->pub.term_source = term_source12;
174 
175  src->pub.next_input_byte = buffer;
176  src->pub.bytes_in_buffer = bufsize;
177 }
178 
179 } /* extern "C" */
180 
void init_source12(j12_decompress_ptr)
boolean fill_input_buffer12(j12_decompress_ptr cinfo)
void ossimJpegMemorySrc12(j12_decompress_ptr cinfo, const ossim_uint8 *buffer, std::size_t bufsize)
void term_source12(j12_decompress_ptr)
void skip_input_data12(j12_decompress_ptr cinfo, long num_bytes)
struct jpeg12_source_mgr pub
ossimJpegSourceMgr12 * ossimJpegSourceMgrPtr12
jpeg library includes missing dependent includes.
if(yy_init)
unsigned char ossim_uint8