| 1 |
/***************************************************************************** |
| 2 |
jpegdatasource.c |
| 3 |
****************************************************************************** |
| 4 |
This is the data source manager that Jpegtopnm supplies to the JPEG |
| 5 |
library. The Jpeg library uses this object to get JPEG input. |
| 6 |
|
| 7 |
This data source manager is the same as the Jpeg library's built in |
| 8 |
"stdio" one, except that it looks ahead and one can query it to see |
| 9 |
if there is any data in the stream that the Jpeg library hasn't seen |
| 10 |
yet. Thus, you can use it in a program that reads multiple JPEG |
| 11 |
images and know when to stop. You can also nicely handle completely |
| 12 |
empty input files more gracefully than just crying input error. |
| 13 |
|
| 14 |
This data source manager does 4K fread() reads and passes 4K buffers |
| 15 |
to the Jpeg library. It reads one 4K block ahead, so there is up to |
| 16 |
8K of image buffered at any time. |
| 17 |
|
| 18 |
By Bryan Henderson, San Jose CA 2002.10.13 |
| 19 |
*****************************************************************************/ |
| 20 |
|
| 21 |
#include <ctype.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <errno.h> |
| 25 |
/* Note: jpeglib.h prerequires stdlib.h and ctype.h. It should include them |
| 26 |
itself, but doesn't. |
| 27 |
*/ |
| 28 |
#include <jpeglib.h> |
| 29 |
|
| 30 |
#include "pm_c_util.h" |
| 31 |
#include "mallocvar.h" |
| 32 |
#include "pnm.h" |
| 33 |
|
| 34 |
#include "jpegdatasource.h" |
| 35 |
|
| 36 |
#define BUFFER_SIZE 4096 |
| 37 |
|
| 38 |
struct sourceManager { |
| 39 |
/* The following structure _must_ be the first thing in this structure, |
| 40 |
because the methods below assume the address of the struct |
| 41 |
jpeg_source_mgr is the same as the address of the struct sourceManager. |
| 42 |
The former address is the only information the Jpeg library gives |
| 43 |
these methods to tell them on what source manager object they are |
| 44 |
supposed to operate. |
| 45 |
*/ |
| 46 |
struct jpeg_source_mgr jpegSourceMgr; |
| 47 |
FILE * ifP; |
| 48 |
bool prematureEof; |
| 49 |
/* We have been asked for data and were unable to comply because |
| 50 |
the file had no more to give (so we supplied EOI markers |
| 51 |
instead). |
| 52 |
*/ |
| 53 |
JOCTET * currentBuffer; |
| 54 |
JOCTET * nextBuffer; |
| 55 |
unsigned int bytesInNextBuffer; |
| 56 |
JOCTET buffer1[BUFFER_SIZE]; |
| 57 |
JOCTET buffer2[BUFFER_SIZE]; |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
static void |
| 62 |
dsInitSource(j_decompress_ptr const cinfoP) { |
| 63 |
/*---------------------------------------------------------------------------- |
| 64 |
This is the init_source method for the data source manager the Jpeg |
| 65 |
library uses. |
| 66 |
-----------------------------------------------------------------------------*/ |
| 67 |
/* The object was created ready to go and remains ready from one image |
| 68 |
to the next. No per-image initialization is necessary. |
| 69 |
*/ |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
static const JOCTET jfifEoiMarker[] = {0xff, JPEG_EOI}; |
| 75 |
/* An EOI (end of image) marker */ |
| 76 |
|
| 77 |
|
| 78 |
static boolean |
| 79 |
dsFillInputBuffer(j_decompress_ptr const cinfoP) { |
| 80 |
/*---------------------------------------------------------------------------- |
| 81 |
This is the fill_input_buffer method for the data source manager the Jpeg |
| 82 |
library uses. |
| 83 |
-----------------------------------------------------------------------------*/ |
| 84 |
struct sourceManager * const srcP = (struct sourceManager *) cinfoP->src; |
| 85 |
|
| 86 |
if (srcP->bytesInNextBuffer == 0) { |
| 87 |
/* The decompressor expects more bytes, but there aren't any, so |
| 88 |
the file is corrupted -- probably truncated. We want the |
| 89 |
decompressor to decompress whatever it's read so far, so we |
| 90 |
synthesize an EOI marker here, but we also set error state |
| 91 |
in the source manager. The decompressor will recognize the |
| 92 |
truncation and pad out the image with gray. |
| 93 |
*/ |
| 94 |
srcP->prematureEof = TRUE; |
| 95 |
|
| 96 |
srcP->jpegSourceMgr.next_input_byte = jfifEoiMarker; |
| 97 |
srcP->jpegSourceMgr.bytes_in_buffer = sizeof(jfifEoiMarker); |
| 98 |
} else { |
| 99 |
/* Rotate the buffers */ |
| 100 |
srcP->jpegSourceMgr.next_input_byte = srcP->nextBuffer; |
| 101 |
srcP->jpegSourceMgr.bytes_in_buffer = srcP->bytesInNextBuffer; |
| 102 |
{ |
| 103 |
JOCTET * tmp; |
| 104 |
tmp = srcP->nextBuffer; |
| 105 |
srcP->nextBuffer = srcP->currentBuffer; |
| 106 |
srcP->currentBuffer = tmp; |
| 107 |
} |
| 108 |
|
| 109 |
/* Fill the new "next" buffer */ |
| 110 |
srcP->bytesInNextBuffer = |
| 111 |
fread(srcP->nextBuffer, 1, BUFFER_SIZE, srcP->ifP); |
| 112 |
} |
| 113 |
return TRUE; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
static void |
| 119 |
dsSkipInputData(j_decompress_ptr const cinfoP, long const num_bytes) { |
| 120 |
/*---------------------------------------------------------------------------- |
| 121 |
This is the skip_input_data method for the data source manager the Jpeg |
| 122 |
library uses. |
| 123 |
-----------------------------------------------------------------------------*/ |
| 124 |
struct jpeg_source_mgr * const jpegSourceMgrP = cinfoP->src; |
| 125 |
|
| 126 |
long i; |
| 127 |
|
| 128 |
for (i = 0; i < num_bytes; ++i) { |
| 129 |
if (jpegSourceMgrP->bytes_in_buffer == 0) |
| 130 |
dsFillInputBuffer(cinfoP); |
| 131 |
++jpegSourceMgrP->next_input_byte; |
| 132 |
--jpegSourceMgrP->bytes_in_buffer; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
static void |
| 139 |
dsTermSource(j_decompress_ptr const cinfoP) { |
| 140 |
/*---------------------------------------------------------------------------- |
| 141 |
This is the term_source method for the data source manager the Jpeg |
| 142 |
library uses. |
| 143 |
-----------------------------------------------------------------------------*/ |
| 144 |
/* We couldn't care less that the Jpeg library is done reading an |
| 145 |
image. The source manager object remains active and ready for the |
| 146 |
Jpeg library to read the next image. |
| 147 |
*/ |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
bool |
| 153 |
dsDataLeft(struct sourceManager * const srcP) { |
| 154 |
|
| 155 |
return((srcP->jpegSourceMgr.bytes_in_buffer > 0 || |
| 156 |
srcP->bytesInNextBuffer > 0)); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
bool |
| 162 |
dsPrematureEof(struct sourceManager * const srcP) { |
| 163 |
|
| 164 |
return srcP->prematureEof; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
struct sourceManager * |
| 170 |
dsCreateSource(const char * const fileName) { |
| 171 |
|
| 172 |
struct sourceManager * srcP; |
| 173 |
|
| 174 |
MALLOCVAR(srcP); |
| 175 |
if (srcP == NULL) |
| 176 |
pm_error("Unable to get memory for the Jpeg library source manager."); |
| 177 |
|
| 178 |
srcP->ifP = pm_openr(fileName); |
| 179 |
|
| 180 |
srcP->jpegSourceMgr.init_source = dsInitSource; |
| 181 |
srcP->jpegSourceMgr.fill_input_buffer = dsFillInputBuffer; |
| 182 |
srcP->jpegSourceMgr.skip_input_data = dsSkipInputData; |
| 183 |
srcP->jpegSourceMgr.resync_to_restart = jpeg_resync_to_restart; |
| 184 |
srcP->jpegSourceMgr.term_source = dsTermSource; |
| 185 |
|
| 186 |
srcP->prematureEof = FALSE; |
| 187 |
srcP->currentBuffer = srcP->buffer1; |
| 188 |
srcP->nextBuffer = srcP->buffer2; |
| 189 |
srcP->jpegSourceMgr.bytes_in_buffer = |
| 190 |
fread(srcP->currentBuffer, 1, BUFFER_SIZE, srcP->ifP); |
| 191 |
srcP->jpegSourceMgr.next_input_byte = srcP->currentBuffer; |
| 192 |
srcP->bytesInNextBuffer = |
| 193 |
fread(srcP->nextBuffer, 1, BUFFER_SIZE, srcP->ifP); |
| 194 |
|
| 195 |
return srcP; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
void |
| 201 |
dsDestroySource(struct sourceManager * const srcP) { |
| 202 |
|
| 203 |
pm_close(srcP->ifP); |
| 204 |
free(srcP); |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
struct jpeg_source_mgr * |
| 211 |
dsJpegSourceMgr(struct sourceManager * const srcP) { |
| 212 |
return &srcP->jpegSourceMgr; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|