author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 0 | 1918ee327afb |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* jdatadst.c |
|
3 |
* |
|
4 |
* Copyright (C) 1994-1996, Thomas G. Lane. |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
5 |
* Modified 2009 by Guido Vollbeding. |
0 | 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 |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
10 |
* emitting JPEG data to memory or to a file (or any stdio stream). |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
11 |
* While these routines are sufficient for most applications, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
12 |
* some will want to use a different destination manager. |
0 | 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 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
23 |
#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare malloc(),free() */ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
24 |
extern void * malloc JPP((size_t size)); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
25 |
extern void free JPP((void *ptr)); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
26 |
#endif |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
27 |
|
0 | 28 |
|
29 |
/* Expanded data destination object for stdio output */ |
|
30 |
||
31 |
typedef struct { |
|
32 |
struct jpeg_destination_mgr pub; /* public fields */ |
|
33 |
||
34 |
FILE * outfile; /* target stream */ |
|
35 |
JOCTET * buffer; /* start of buffer */ |
|
36 |
} my_destination_mgr; |
|
37 |
||
38 |
typedef my_destination_mgr * my_dest_ptr; |
|
39 |
||
40 |
#define OUTPUT_BUF_SIZE 4096 /* choose an efficiently fwrite'able size */ |
|
41 |
||
42 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
43 |
/* Expanded data destination object for memory output */ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
44 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
45 |
typedef struct { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
46 |
struct jpeg_destination_mgr pub; /* public fields */ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
47 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
48 |
unsigned char ** outbuffer; /* target buffer */ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
49 |
unsigned long * outsize; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
50 |
unsigned char * newbuffer; /* newly allocated buffer */ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
51 |
JOCTET * buffer; /* start of buffer */ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
52 |
size_t bufsize; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
53 |
} my_mem_destination_mgr; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
54 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
55 |
typedef my_mem_destination_mgr * my_mem_dest_ptr; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
56 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
57 |
|
0 | 58 |
/* |
59 |
* Initialize destination --- called by jpeg_start_compress |
|
60 |
* before any data is actually written. |
|
61 |
*/ |
|
62 |
||
63 |
METHODDEF(void) |
|
64 |
init_destination (j_compress_ptr cinfo) |
|
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 *) |
|
70 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
|
71 |
OUTPUT_BUF_SIZE * SIZEOF(JOCTET)); |
|
72 |
||
73 |
dest->pub.next_output_byte = dest->buffer; |
|
74 |
dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; |
|
75 |
} |
|
76 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
77 |
METHODDEF(void) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
78 |
init_mem_destination (j_compress_ptr cinfo) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
79 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
80 |
/* no work necessary here */ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
81 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
82 |
|
0 | 83 |
|
84 |
/* |
|
85 |
* Empty the output buffer --- called whenever buffer fills up. |
|
86 |
* |
|
87 |
* In typical applications, this should write the entire output buffer |
|
88 |
* (ignoring the current state of next_output_byte & free_in_buffer), |
|
89 |
* reset the pointer & count to the start of the buffer, and return TRUE |
|
90 |
* indicating that the buffer has been dumped. |
|
91 |
* |
|
92 |
* In applications that need to be able to suspend compression due to output |
|
93 |
* overrun, a FALSE return indicates that the buffer cannot be emptied now. |
|
94 |
* In this situation, the compressor will return to its caller (possibly with |
|
95 |
* an indication that it has not accepted all the supplied scanlines). The |
|
96 |
* application should resume compression after it has made more room in the |
|
97 |
* output buffer. Note that there are substantial restrictions on the use of |
|
98 |
* suspension --- see the documentation. |
|
99 |
* |
|
100 |
* When suspending, the compressor will back up to a convenient restart point |
|
101 |
* (typically the start of the current MCU). next_output_byte & free_in_buffer |
|
102 |
* indicate where the restart point will be if the current call returns FALSE. |
|
103 |
* Data beyond this point will be regenerated after resumption, so do not |
|
104 |
* write it out when emptying the buffer externally. |
|
105 |
*/ |
|
106 |
||
107 |
METHODDEF(boolean) |
|
108 |
empty_output_buffer (j_compress_ptr cinfo) |
|
109 |
{ |
|
110 |
my_dest_ptr dest = (my_dest_ptr) cinfo->dest; |
|
111 |
||
112 |
if (JFWRITE(dest->outfile, dest->buffer, OUTPUT_BUF_SIZE) != |
|
113 |
(size_t) OUTPUT_BUF_SIZE) |
|
114 |
ERREXIT(cinfo, JERR_FILE_WRITE); |
|
115 |
||
116 |
dest->pub.next_output_byte = dest->buffer; |
|
117 |
dest->pub.free_in_buffer = OUTPUT_BUF_SIZE; |
|
118 |
||
119 |
return TRUE; |
|
120 |
} |
|
121 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
122 |
METHODDEF(boolean) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
123 |
empty_mem_output_buffer (j_compress_ptr cinfo) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
124 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
125 |
size_t nextsize; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
126 |
JOCTET * nextbuffer; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
127 |
my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
128 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
129 |
/* Try to allocate new buffer with double size */ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
130 |
nextsize = dest->bufsize * 2; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
131 |
nextbuffer = malloc(nextsize); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
132 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
133 |
if (nextbuffer == NULL) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
134 |
ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
135 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
136 |
MEMCOPY(nextbuffer, dest->buffer, dest->bufsize); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
137 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
138 |
if (dest->newbuffer != NULL) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
139 |
free(dest->newbuffer); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
140 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
141 |
dest->newbuffer = nextbuffer; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
142 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
143 |
dest->pub.next_output_byte = nextbuffer + dest->bufsize; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
144 |
dest->pub.free_in_buffer = dest->bufsize; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
145 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
146 |
dest->buffer = nextbuffer; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
147 |
dest->bufsize = nextsize; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
148 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
149 |
return TRUE; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
150 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
151 |
|
0 | 152 |
|
153 |
/* |
|
154 |
* Terminate destination --- called by jpeg_finish_compress |
|
155 |
* after all data has been written. Usually needs to flush buffer. |
|
156 |
* |
|
157 |
* NB: *not* called by jpeg_abort or jpeg_destroy; surrounding |
|
158 |
* application must deal with any cleanup that should happen even |
|
159 |
* for error exit. |
|
160 |
*/ |
|
161 |
||
162 |
METHODDEF(void) |
|
163 |
term_destination (j_compress_ptr cinfo) |
|
164 |
{ |
|
165 |
my_dest_ptr dest = (my_dest_ptr) cinfo->dest; |
|
166 |
size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer; |
|
167 |
||
168 |
/* Write any data remaining in the buffer */ |
|
169 |
if (datacount > 0) { |
|
170 |
if (JFWRITE(dest->outfile, dest->buffer, datacount) != datacount) |
|
171 |
ERREXIT(cinfo, JERR_FILE_WRITE); |
|
172 |
} |
|
173 |
fflush(dest->outfile); |
|
174 |
/* Make sure we wrote the output file OK */ |
|
175 |
if (ferror(dest->outfile)) |
|
176 |
ERREXIT(cinfo, JERR_FILE_WRITE); |
|
177 |
} |
|
178 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
179 |
METHODDEF(void) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
180 |
term_mem_destination (j_compress_ptr cinfo) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
181 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
182 |
my_mem_dest_ptr dest = (my_mem_dest_ptr) cinfo->dest; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
183 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
184 |
*dest->outbuffer = dest->buffer; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
185 |
*dest->outsize = dest->bufsize - dest->pub.free_in_buffer; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
186 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
187 |
|
0 | 188 |
|
189 |
/* |
|
190 |
* Prepare for output to a stdio stream. |
|
191 |
* The caller must have already opened the stream, and is responsible |
|
192 |
* for closing it after finishing compression. |
|
193 |
*/ |
|
194 |
||
195 |
GLOBAL(void) |
|
196 |
jpeg_stdio_dest (j_compress_ptr cinfo, FILE * outfile) |
|
197 |
{ |
|
198 |
my_dest_ptr dest; |
|
199 |
||
200 |
/* The destination object is made permanent so that multiple JPEG images |
|
201 |
* can be written to the same file without re-executing jpeg_stdio_dest. |
|
202 |
* This makes it dangerous to use this manager and a different destination |
|
203 |
* manager serially with the same JPEG object, because their private object |
|
204 |
* sizes may be different. Caveat programmer. |
|
205 |
*/ |
|
206 |
if (cinfo->dest == NULL) { /* first time for this JPEG object? */ |
|
207 |
cinfo->dest = (struct jpeg_destination_mgr *) |
|
208 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, |
|
209 |
SIZEOF(my_destination_mgr)); |
|
210 |
} |
|
211 |
||
212 |
dest = (my_dest_ptr) cinfo->dest; |
|
213 |
dest->pub.init_destination = init_destination; |
|
214 |
dest->pub.empty_output_buffer = empty_output_buffer; |
|
215 |
dest->pub.term_destination = term_destination; |
|
216 |
dest->outfile = outfile; |
|
217 |
} |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
218 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
219 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
220 |
/* |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
221 |
* Prepare for output to a memory buffer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
222 |
* The caller may supply an own initial buffer with appropriate size. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
223 |
* Otherwise, or when the actual data output exceeds the given size, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
224 |
* the library adapts the buffer size as necessary. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
225 |
* The standard library functions malloc/free are used for allocating |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
226 |
* larger memory, so the buffer is available to the application after |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
227 |
* finishing compression, and then the application is responsible for |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
228 |
* freeing the requested memory. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
229 |
*/ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
230 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
231 |
GLOBAL(void) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
232 |
jpeg_mem_dest (j_compress_ptr cinfo, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
233 |
unsigned char ** outbuffer, unsigned long * outsize) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
234 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
235 |
my_mem_dest_ptr dest; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
236 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
237 |
if (outbuffer == NULL || outsize == NULL) /* sanity check */ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
238 |
ERREXIT(cinfo, JERR_BUFFER_SIZE); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
239 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
240 |
/* The destination object is made permanent so that multiple JPEG images |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
241 |
* can be written to the same buffer without re-executing jpeg_mem_dest. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
242 |
*/ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
243 |
if (cinfo->dest == NULL) { /* first time for this JPEG object? */ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
244 |
cinfo->dest = (struct jpeg_destination_mgr *) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
245 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
246 |
SIZEOF(my_mem_destination_mgr)); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
247 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
248 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
249 |
dest = (my_mem_dest_ptr) cinfo->dest; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
250 |
dest->pub.init_destination = init_mem_destination; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
251 |
dest->pub.empty_output_buffer = empty_mem_output_buffer; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
252 |
dest->pub.term_destination = term_mem_destination; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
253 |
dest->outbuffer = outbuffer; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
254 |
dest->outsize = outsize; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
255 |
dest->newbuffer = NULL; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
256 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
257 |
if (*outbuffer == NULL || *outsize == 0) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
258 |
/* Allocate initial buffer */ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
259 |
dest->newbuffer = *outbuffer = malloc(OUTPUT_BUF_SIZE); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
260 |
if (dest->newbuffer == NULL) |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
261 |
ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
262 |
*outsize = OUTPUT_BUF_SIZE; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
263 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
264 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
265 |
dest->pub.next_output_byte = dest->buffer = *outbuffer; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
266 |
dest->pub.free_in_buffer = dest->bufsize = *outsize; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
267 |
} |