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