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