ReactOS 0.4.15-dev-7958-gcd0bb1a
jdatadst.c
Go to the documentation of this file.
1/*
2 * jdatadst.c
3 *
4 * Copyright (C) 1994-1996, Thomas G. Lane.
5 * Modified 2009-2019 by Guido Vollbeding.
6 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
8 *
9 * This file contains compression data destination routines for the case of
10 * emitting JPEG data to memory or to a file (or any stdio stream).
11 * While these routines are sufficient for most applications,
12 * some will want to use a different destination manager.
13 * IMPORTANT: we assume that fwrite() will correctly transcribe an array of
14 * JOCTETs into 8-bit-wide elements on external storage. If char is wider
15 * than 8 bits on your machine, you may need to do some tweaking.
16 */
17
18/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
19#include "jinclude.h"
20#include "jpeglib.h"
21#include "jerror.h"
22
23#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */
24extern void * malloc JPP((size_t size));
25extern void free JPP((void *ptr));
26#endif
27
28
29/* Expanded data destination object for stdio output */
30
31typedef struct {
32 struct jpeg_destination_mgr pub; /* public fields */
33
34 FILE * outfile; /* target stream */
35 JOCTET * buffer; /* start of buffer */
37
39
40#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */
41
42
43/* Expanded data destination object for memory output */
44
45typedef struct {
46 struct jpeg_destination_mgr pub; /* public fields */
47
48 unsigned char ** outbuffer; /* target buffer */
49 size_t * outsize;
50 unsigned char * newbuffer; /* newly allocated buffer */
51 JOCTET * buffer; /* start of buffer */
52 size_t bufsize;
54
56
57
58/*
59 * Initialize destination --- called by jpeg_start_compress
60 * before any data is actually written.
61 */
62
63METHODDEF(void)
65{
66 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
67
68 /* Allocate the output buffer --- it will be released when done with image */
69 dest->buffer = (JOCTET *) (*cinfo->mem->alloc_small)
71
72 dest->pub.next_output_byte = dest->buffer;
73 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
74}
75
76METHODDEF(void)
78{
79 /* no work necessary here */
80}
81
82
83/*
84 * Empty the output buffer --- called whenever buffer fills up.
85 *
86 * In typical applications, this should write the entire output buffer
87 * (ignoring the current state of next_output_byte & free_in_buffer),
88 * reset the pointer & count to the start of the buffer, and return TRUE
89 * indicating that the buffer has been dumped.
90 *
91 * In applications that need to be able to suspend compression due to output
92 * overrun, a FALSE return indicates that the buffer cannot be emptied now.
93 * In this situation, the compressor will return to its caller (possibly with
94 * an indication that it has not accepted all the supplied scanlines). The
95 * application should resume compression after it has made more room in the
96 * output buffer. Note that there are substantial restrictions on the use of
97 * suspension --- see the documentation.
98 *
99 * When suspending, the compressor will back up to a convenient restart point
100 * (typically the start of the current MCU). next_output_byte & free_in_buffer
101 * indicate where the restart point will be if the current call returns FALSE.
102 * Data beyond this point will be regenerated after resumption, so do not
103 * write it out when emptying the buffer externally.
104 */
105
106METHODDEF(boolean)
108{
109 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
110
111 if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) !=
113 ERREXIT(cinfo, JERR_FILE_WRITE);
114
115 dest->pub.next_output_byte = dest->buffer;
116 dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
117
118 return TRUE;
119}
120
121METHODDEF(boolean)
123{
124 size_t nextsize;
125 JOCTET * nextbuffer;
126 my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;
127
128 /* Try to allocate new buffer with double size */
129 nextsize = dest->bufsize * 2;
130 nextbuffer = (JOCTET *) malloc(nextsize);
131
132 if (nextbuffer == NULL)
133 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 11);
134
135 MEMCOPY(nextbuffer, dest->buffer, dest->bufsize);
136
137 if (dest->newbuffer != NULL)
138 free(dest->newbuffer);
139
140 dest->newbuffer = nextbuffer;
141
142 dest->pub.next_output_byte = nextbuffer + dest->bufsize;
143 dest->pub.free_in_buffer = dest->bufsize;
144
145 dest->buffer = nextbuffer;
146 dest->bufsize = nextsize;
147
148 return TRUE;
149}
150
151
152/*
153 * Terminate destination --- called by jpeg_finish_compress
154 * after all data has been written. Usually needs to flush buffer.
155 *
156 * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
157 * application must deal with any cleanup that should happen even
158 * for error exit.
159 */
160
161METHODDEF(void)
163{
164 my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
165 size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
166
167 /* Write any data remaining in the buffer */
168 if (datacount > 0) {
169 if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount)
170 ERREXIT(cinfo, JERR_FILE_WRITE);
171 }
172 JFFLUSH(dest->outfile);
173 /* Make sure we wrote the output file OK */
174 if (JFERROR(dest->outfile))
175 ERREXIT(cinfo, JERR_FILE_WRITE);
176}
177
178METHODDEF(void)
180{
181 my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest;
182
183 *dest->outbuffer = dest->buffer;
184 *dest->outsize = dest->bufsize - dest->pub.free_in_buffer;
185}
186
187
188/*
189 * Prepare for output to a stdio stream.
190 * The caller must have already opened the stream, and is responsible
191 * for closing it after finishing compression.
192 */
193
194GLOBAL(void)
196{
198
199 /* The destination object is made permanent so that multiple JPEG images
200 * can be written to the same file without re-executing jpeg_stdio_dest.
201 * This makes it dangerous to use this manager and a different destination
202 * manager serially with the same JPEG object, because their private object
203 * sizes may be different. Caveat programmer.
204 */
205 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
206 cinfo->dest = (struct jpeg_destination_mgr *) (*cinfo->mem->alloc_small)
208 }
209
210 dest = (my_dest_ptr) cinfo->dest;
211 dest->pub.init_destination = init_destination;
212 dest->pub.empty_output_buffer = empty_output_buffer;
213 dest->pub.term_destination = term_destination;
214 dest->outfile = outfile;
215}
216
217
218/*
219 * Prepare for output to a memory buffer.
220 * The caller may supply an own initial buffer with appropriate size.
221 * Otherwise, or when the actual data output exceeds the given size,
222 * the library adapts the buffer size as necessary.
223 * The standard library functions malloc/free are used for allocating
224 * larger memory, so the buffer is available to the application after
225 * finishing compression, and then the application is responsible for
226 * freeing the requested memory.
227 * Note: An initial buffer supplied by the caller is expected to be
228 * managed by the application. The library does not free such buffer
229 * when allocating a larger buffer.
230 */
231
232GLOBAL(void)
234 unsigned char ** outbuffer, size_t * outsize)
235{
237
238 if (outbuffer == NULL || outsize == NULL) /* sanity check */
239 ERREXIT(cinfo, JERR_BUFFER_SIZE);
240
241 /* The destination object is made permanent so that multiple JPEG images
242 * can be written to the same buffer without re-executing jpeg_mem_dest.
243 */
244 if (cinfo->dest == NULL) { /* first time for this JPEG object? */
245 cinfo->dest = (struct jpeg_destination_mgr *) (*cinfo->mem->alloc_small)
247 }
248
249 dest = (my_mem_dest_ptr) cinfo->dest;
250 dest->pub.init_destination = init_mem_destination;
251 dest->pub.empty_output_buffer = empty_mem_output_buffer;
252 dest->pub.term_destination = term_mem_destination;
253 dest->outbuffer = outbuffer;
254 dest->outsize = outsize;
255 dest->newbuffer = NULL;
256
257 if (*outbuffer == NULL || *outsize == 0) {
258 /* Allocate initial buffer */
259 dest->newbuffer = *outbuffer = (unsigned char *) malloc(OUTPUT_BUF_SIZE);
260 if (dest->newbuffer == NULL)
261 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
263 }
264
265 dest->pub.next_output_byte = dest->buffer = *outbuffer;
266 dest->pub.free_in_buffer = dest->bufsize = *outsize;
267}
#define SIZEOF(_ar)
Definition: calc.h:97
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
__kernel_size_t size_t
Definition: linux.h:237
GLsizeiptr size
Definition: glext.h:5919
term_mem_destination(j_compress_ptr cinfo)
Definition: jdatadst.c:179
term_destination(j_compress_ptr cinfo)
Definition: jdatadst.c:162
init_mem_destination(j_compress_ptr cinfo)
Definition: jdatadst.c:77
#define OUTPUT_BUF_SIZE
Definition: jdatadst.c:40
empty_mem_output_buffer(j_compress_ptr cinfo)
Definition: jdatadst.c:122
jpeg_mem_dest(j_compress_ptr cinfo, unsigned char **outbuffer, size_t *outsize)
Definition: jdatadst.c:233
my_destination_mgr * my_dest_ptr
Definition: jdatadst.c:38
init_destination(j_compress_ptr cinfo)
Definition: jdatadst.c:64
my_mem_destination_mgr * my_mem_dest_ptr
Definition: jdatadst.c:55
jpeg_stdio_dest(j_compress_ptr cinfo, FILE *outfile)
Definition: jdatadst.c:195
empty_output_buffer(j_compress_ptr cinfo)
Definition: jdatadst.c:107
#define ERREXIT1(cinfo, code, p1)
Definition: jerror.h:212
#define JFFLUSH(file)
Definition: jinclude.h:96
#define MEMCOPY(dest, src, size)
Definition: jinclude.h:69
#define JFERROR(file)
Definition: jinclude.h:97
#define JFWRITE(file, buf, sizeofbuf)
Definition: jinclude.h:94
char JOCTET
Definition: jmorecfg.h:167
#define METHODDEF(type)
Definition: jmorecfg.h:287
#define GLOBAL(type)
Definition: jmorecfg.h:291
struct jpeg_common_struct * j_common_ptr
Definition: jpeglib.h:284
#define JPP(arglist)
Definition: jpeglib.h:877
unsigned char ** outbuffer
Definition: jpeglib.h:980
#define JPOOL_PERMANENT
Definition: jpeglib.h:807
#define JPOOL_IMAGE
Definition: jpeglib.h:808
unsigned char size_t * outsize
Definition: jpeglib.h:981
if(dx< 0)
Definition: linetemp.h:194
static PVOID ptr
Definition: dispmode.c:27
static char * dest
Definition: rtl.c:135
#define ERREXIT(msg)
Definition: rdjpgcom.c:72
JOCTET * buffer
Definition: jdatadst.c:35
unsigned char ** outbuffer
Definition: jdatadst.c:48
unsigned char * newbuffer
Definition: jdatadst.c:50
static FILE * outfile
Definition: wrjpgcom.c:81