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