[package] vips: update to 7.24.5
[openwrt/svn-archive/archive.git] / libs / vips / patches / 002-im_bufjpeg2vips.patch
1 Index: libs/vips/patches/002-im_bufjpeg2vips.patch
2 ===================================================================
3 --- libs/vips/patches/002-im_bufjpeg2vips.patch (revision 0)
4 +++ libs/vips/patches/002-im_bufjpeg2vips.patch (revision 0)
5 @@ -0,0 +1,288 @@
6 +diff -u --recursive vips-7.24.5-vanilla/libvips/format/im_jpeg2vips.c vips-7.24.5/libvips/format/im_jpeg2vips.c
7 +--- vips-7.24.5-vanilla/libvips/format/im_jpeg2vips.c 2011-07-04 09:23:04.437730278 -0500
8 ++++ vips-7.24.5/libvips/format/im_jpeg2vips.c 2011-07-04 09:27:46.972274128 -0500
9 +@@ -30,6 +30,8 @@
10 + * - gtkdoc
11 + * 4/12/10
12 + * - attach the jpeg thumbnail and multiscan fields (thanks Mike)
13 ++ * 20/4/2011
14 ++ * - added im_bufjpeg2vips()
15 + */
16 +
17 + /*
18 +@@ -683,7 +685,7 @@
19 + fail_on_warn = TRUE;
20 + }
21 +
22 +- /* Make jpeg compression object.
23 ++ /* Make jpeg dcompression object.
24 + */
25 + cinfo.err = jpeg_std_error( &eman.pub );
26 + eman.pub.error_exit = new_error_exit;
27 +@@ -737,6 +739,227 @@
28 + return( result );
29 + }
30 +
31 ++/* Just like the above, but we read from a memory buffer.
32 ++ */
33 ++typedef struct {
34 ++ /* Public jpeg fields.
35 ++ */
36 ++ struct jpeg_source_mgr pub;
37 ++
38 ++ /* Private stuff during read.
39 ++ */
40 ++ gboolean start_of_file; /* have we gotten any data yet? */
41 ++ JOCTET *buf;
42 ++ size_t len;
43 ++} InputBuffer;
44 ++
45 ++/*
46 ++ * Initialize source --- called by jpeg_read_header
47 ++ * before any data is actually read.
48 ++ */
49 ++
50 ++static void
51 ++init_source (j_decompress_ptr cinfo)
52 ++{
53 ++ InputBuffer *src = (InputBuffer *) cinfo->src;
54 ++
55 ++ /* We reset the empty-input-file flag for each image,
56 ++ * but we don't clear the input buffer.
57 ++ * This is correct behavior for reading a series of images from one source.
58 ++ */
59 ++ src->start_of_file = TRUE;
60 ++}
61 ++
62 ++/*
63 ++ * Fill the input buffer --- called whenever buffer is emptied.
64 ++ *
65 ++ * In typical applications, this should read fresh data into the buffer
66 ++ * (ignoring the current state of next_input_byte & bytes_in_buffer),
67 ++ * reset the pointer & count to the start of the buffer, and return TRUE
68 ++ * indicating that the buffer has been reloaded. It is not necessary to
69 ++ * fill the buffer entirely, only to obtain at least one more byte.
70 ++ *
71 ++ * There is no such thing as an EOF return. If the end of the file has been
72 ++ * reached, the routine has a choice of ERREXIT() or inserting fake data into
73 ++ * the buffer. In most cases, generating a warning message and inserting a
74 ++ * fake EOI marker is the best course of action --- this will allow the
75 ++ * decompressor to output however much of the image is there. However,
76 ++ * the resulting error message is misleading if the real problem is an empty
77 ++ * input file, so we handle that case specially.
78 ++ *
79 ++ * In applications that need to be able to suspend compression due to input
80 ++ * not being available yet, a FALSE return indicates that no more data can be
81 ++ * obtained right now, but more may be forthcoming later. In this situation,
82 ++ * the decompressor will return to its caller (with an indication of the
83 ++ * number of scanlines it has read, if any). The application should resume
84 ++ * decompression after it has loaded more data into the input buffer. Note
85 ++ * that there are substantial restrictions on the use of suspension --- see
86 ++ * the documentation.
87 ++ *
88 ++ * When suspending, the decompressor will back up to a convenient restart point
89 ++ * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
90 ++ * indicate where the restart point will be if the current call returns FALSE.
91 ++ * Data beyond this point must be rescanned after resumption, so move it to
92 ++ * the front of the buffer rather than discarding it.
93 ++ */
94 ++
95 ++static boolean
96 ++fill_input_buffer (j_decompress_ptr cinfo)
97 ++{
98 ++ InputBuffer *src = (InputBuffer *) cinfo->src;
99 ++ size_t nbytes;
100 ++
101 ++ if (src->start_of_file) {
102 ++ nbytes = src->len;
103 ++ }
104 ++ else {
105 ++ WARNMS(cinfo, JWRN_JPEG_EOF);
106 ++ /* Insert a fake EOI marker */
107 ++ src->buf[0] = (JOCTET) 0xFF;
108 ++ src->buf[1] = (JOCTET) JPEG_EOI;
109 ++ nbytes = 2;
110 ++ }
111 ++
112 ++ src->pub.next_input_byte = src->buf;
113 ++ src->pub.bytes_in_buffer = nbytes;
114 ++ src->start_of_file = FALSE;
115 ++
116 ++ return TRUE;
117 ++}
118 ++
119 ++/*
120 ++ * Skip data --- used to skip over a potentially large amount of
121 ++ * uninteresting data (such as an APPn marker).
122 ++ *
123 ++ * Writers of suspendable-input applications must note that skip_input_data
124 ++ * is not granted the right to give a suspension return. If the skip extends
125 ++ * beyond the data currently in the buffer, the buffer can be marked empty so
126 ++ * that the next read will cause a fill_input_buffer call that can suspend.
127 ++ * Arranging for additional bytes to be discarded before reloading the input
128 ++ * buffer is the application writer's problem.
129 ++ */
130 ++
131 ++static void
132 ++skip_input_data (j_decompress_ptr cinfo, long num_bytes)
133 ++{
134 ++ InputBuffer *src = (InputBuffer *) cinfo->src;
135 ++
136 ++ /* Just skip fwd.
137 ++ */
138 ++ if (num_bytes > 0) {
139 ++ src->pub.next_input_byte += (size_t) num_bytes;
140 ++ src->pub.bytes_in_buffer -= (size_t) num_bytes;
141 ++ }
142 ++}
143 ++
144 ++/*
145 ++ * An additional method that can be provided by data source modules is the
146 ++ * resync_to_restart method for error recovery in the presence of RST markers.
147 ++ * For the moment, this source module just uses the default resync method
148 ++ * provided by the JPEG library. That method assumes that no backtracking
149 ++ * is possible.
150 ++ */
151 ++
152 ++/*
153 ++ * Terminate source --- called by jpeg_finish_decompress
154 ++ * after all data has been read. Often a no-op.
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 ++
161 ++static void
162 ++term_source (j_decompress_ptr cinfo)
163 ++{
164 ++ /* no work necessary here */
165 ++}
166 ++
167 ++/*
168 ++ * Prepare for input from a memory buffer. The caller needs to free the
169 ++ * buffer after decompress is done, we don't take ownership.
170 ++ */
171 ++
172 ++static void
173 ++buf_source (j_decompress_ptr cinfo, void *buf, size_t len)
174 ++{
175 ++ InputBuffer *src;
176 ++
177 ++ /* The source object and input buffer are made permanent so that a series
178 ++ * of JPEG images can be read from the same file by calling jpeg_stdio_src
179 ++ * only before the first one. (If we discarded the buffer at the end of
180 ++ * one image, we'd likely lose the start of the next one.)
181 ++ * This makes it unsafe to use this manager and a different source
182 ++ * manager serially with the same JPEG object. Caveat programmer.
183 ++ */
184 ++ if (cinfo->src == NULL) { /* first time for this JPEG object? */
185 ++ cinfo->src = (struct jpeg_source_mgr *)
186 ++ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
187 ++ sizeof(InputBuffer));
188 ++ src = (InputBuffer *) cinfo->src;
189 ++ src->buf = buf;
190 ++ src->len = len;
191 ++ }
192 ++
193 ++ src = (InputBuffer *) cinfo->src;
194 ++ src->pub.init_source = init_source;
195 ++ src->pub.fill_input_buffer = fill_input_buffer;
196 ++ src->pub.skip_input_data = skip_input_data;
197 ++ src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
198 ++ src->pub.term_source = term_source;
199 ++ src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
200 ++ src->pub.next_input_byte = NULL; /* until buffer loaded */
201 ++}
202 ++
203 ++/* Read a JPEG memory buffer into a VIPS image.
204 ++ */
205 ++static int
206 ++bufjpeg2vips( void *buf, size_t len, IMAGE *out )
207 ++{
208 ++ char *p, *q;
209 ++ struct jpeg_decompress_struct cinfo;
210 ++ ErrorManager eman;
211 ++ int result;
212 ++ gboolean invert_pels;
213 ++ gboolean header_only = FALSE;
214 ++
215 ++ /* Make jpeg dcompression object.
216 ++ */
217 ++ cinfo.err = jpeg_std_error( &eman.pub );
218 ++ eman.pub.error_exit = new_error_exit;
219 ++ eman.pub.output_message = new_output_message;
220 ++ eman.fp = NULL;
221 ++ if( setjmp( eman.jmp ) ) {
222 ++ /* Here for longjmp() from new_error_exit().
223 ++ */
224 ++ jpeg_destroy_decompress( &cinfo );
225 ++
226 ++ return( -1 );
227 ++ }
228 ++ jpeg_create_decompress( &cinfo );
229 ++
230 ++ /* Make input.
231 ++ */
232 ++ buf_source( &cinfo, buf, len );
233 ++
234 ++ /* Need to read in APP1 (EXIF metadata) and APP2 (ICC profile).
235 ++ */
236 ++ jpeg_save_markers( &cinfo, JPEG_APP0 + 1, 0xffff );
237 ++ jpeg_save_markers( &cinfo, JPEG_APP0 + 2, 0xffff );
238 ++
239 ++ /* Convert!
240 ++ */
241 ++ result = read_jpeg_header( &cinfo, out, &invert_pels, 1 );
242 ++ if( !header_only && !result )
243 ++ result = read_jpeg_image( &cinfo, out, invert_pels );
244 ++
245 ++ /* Close and tidy.
246 ++ */
247 ++ jpeg_destroy_decompress( &cinfo );
248 ++
249 ++ return( result );
250 ++}
251 ++
252 + /**
253 + * im_jpeg2vips:
254 + * @filename: file to load
255 +@@ -803,6 +1026,27 @@
256 + return( jpeg2vips( filename, out, FALSE ) );
257 + }
258 +
259 ++/**
260 ++ * im_bufjpeg2vips:
261 ++ * @buf: memory area to load
262 ++ * @len: size of memory area
263 ++ * @out: image to write
264 ++ *
265 ++ * Read a JPEG-formatted memory block into a VIPS image. It can read most
266 ++ * 8-bit JPEG images, including CMYK and YCbCr.
267 ++ *
268 ++ * This function is handy for processing JPEG image thumbnails.
269 ++ *
270 ++ * See also: #VipsFormat, im_jpeg2vips().
271 ++ *
272 ++ * Returns: 0 on success, -1 on error.
273 ++ */
274 ++int
275 ++im_bufjpeg2vips( void *buf, size_t len, IMAGE *out )
276 ++{
277 ++ return( bufjpeg2vips( buf, len, out ) );
278 ++}
279 ++
280 + static int
281 + isjpeg( const char *filename )
282 + {
283 +diff -u --recursive vips-7.24.5-vanilla/libvips/include/vips/format.h vips-7.24.5/libvips/include/vips/format.h
284 +--- vips-7.24.5-vanilla/libvips/include/vips/format.h 2011-07-04 09:23:04.643721945 -0500
285 ++++ vips-7.24.5/libvips/include/vips/format.h 2011-07-04 09:26:21.870724494 -0500
286 +@@ -122,6 +122,7 @@
287 + /* Low-level read/write operations.
288 + */
289 + int im_jpeg2vips( const char *filename, IMAGE *out );
290 ++int im_bufjpeg2vips( void *buf, size_t len, IMAGE *out );
291 + int im_vips2jpeg( IMAGE *in, const char *filename );
292 + int im_vips2mimejpeg( IMAGE *in, int qfac );
293 + int im_vips2bufjpeg( IMAGE *in, IMAGE *out, int qfac, char **obuf, int *olen );