1 /************************************************************************************
2 Partially based on TerraLib, from https://svn.dpi.inpe.br/terralib
4 TerraLib - a library for developing GIS applications.
5 Copyright © 2001-2007 INPE and Tecgraf/PUC-Rio.
7 This code is part of the TerraLib library.
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 You should have received a copy of the GNU Lesser General Public
14 License along with this library.
16 The authors reassure the license terms regarding the warranties.
17 They specifically disclaim any warranties, including, but not limited to,
18 the implied warranties of merchantability and fitness for a particular purpose.
19 The library provided hereunder is on an "as is" basis, and the authors have no
20 obligation to provide maintenance, support, updates, enhancements, or modifications.
21 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
22 indirect, special, incidental, or consequential damages arising out of the use
23 of this library and its documentation.
24 *************************************************************************************/
29 guint8 * src; // pointer to jpeg in mem
30 int size; // length of jpeg in mem
31 guint8 * dest; // pointer to output image row
33 init_jpeg_src(sparrow); //once only
36 begin_reading_jpeg(sparrow, src, size);
38 read_one_line(sparrow, dest);
40 finish_reading_jpeg(sparrow);
43 finalise_jpeg_src(sparrow); // once only (optional really)
50 #include "gstsparrow.h"
53 // Expanded data source object for memory buffer input
56 struct jpeg_source_mgr pub
;
57 unsigned char* buffer
;
61 #define COLOURSPACE JCS_EXT_BGRX
65 Initialize source --- Nothing to do
68 init_source (j_decompress_ptr cinfo
)
72 Fill the input buffer --- called whenever buffer is emptied.
75 fill_input_buffer (j_decompress_ptr cinfo
)
77 sparrow_src_mgr
*src
= (sparrow_src_mgr
*) cinfo
->src
;
78 src
->pub
.next_input_byte
= src
->buffer
;
79 src
->pub
.bytes_in_buffer
= src
->bufsize
;
85 Skip data --- used to skip over a potentially large amount of
89 skip_input_data (j_decompress_ptr cinfo
, long num_bytes
)
91 sparrow_src_mgr
* src
= (sparrow_src_mgr
*) cinfo
->src
;
93 /* just move the ptr */
94 src
->pub
.next_input_byte
+= num_bytes
;
95 src
->pub
.bytes_in_buffer
-= num_bytes
;
99 Terminate source --- called by jpeg_finish_decompress
102 term_source (j_decompress_ptr cinfo
)
106 Prepare for input from a memory buffer.
109 jpeg_mem_src (j_decompress_ptr cinfo
, unsigned char* buffer
, unsigned int bufsize
)
111 sparrow_src_mgr
*src
;
113 if (cinfo
->src
== NULL
) {
114 cinfo
->src
= (struct jpeg_source_mgr
*)
115 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
, sizeof(sparrow_src_mgr
));
118 src
= (sparrow_src_mgr
*) cinfo
->src
;
119 src
->pub
.init_source
= init_source
;
120 src
->pub
.fill_input_buffer
= fill_input_buffer
;
121 src
->pub
.skip_input_data
= skip_input_data
;
122 src
->pub
.resync_to_restart
= jpeg_resync_to_restart
;
123 src
->pub
.term_source
= term_source
;
124 src
->pub
.bytes_in_buffer
= 0;
125 src
->pub
.next_input_byte
= NULL
;
127 src
->buffer
= buffer
;
128 src
->bufsize
= bufsize
;
131 #define ROWS_PER_CYCLE 1 /*I think this needs to be 1 in anycase*/
133 /* the complete decompress function, not actually used in sparrow */
135 decompress_buffer(struct jpeg_decompress_struct
*cinfo
, guint8
* src
, int size
, guint8
* dest
,
136 int *width
, int *height
)
138 jpeg_create_decompress(cinfo
);
139 jpeg_mem_src(cinfo
, src
, size
);
141 jpeg_read_header(cinfo
, TRUE
);
142 jpeg_start_decompress(cinfo
);
143 cinfo
->out_color_space
= COLOURSPACE
;
145 *width
= cinfo
->output_width
;
146 *height
= cinfo
->output_height
;
148 /*use 4 here, not cinfo->num_channels, because libjpeg_turbo is doing the
149 RGBx colour space conversion */
150 int stride
= 4 * cinfo
->output_width
;
152 while (cinfo
->output_scanline
< cinfo
->output_height
){
153 int read
= jpeg_read_scanlines(cinfo
, &row
, ROWS_PER_CYCLE
);
154 row
+= stride
* read
;
156 jpeg_finish_decompress(cinfo
);
163 begin_reading_jpeg(GstSparrow
*sparrow
, guint8
* src
, int size
){
164 struct jpeg_decompress_struct
*cinfo
= sparrow
->cinfo
;
165 GST_DEBUG("cinfo is %p, src %p, size %d\n", cinfo
, src
, size
);
167 jpeg_create_decompress(cinfo
);
168 jpeg_mem_src(cinfo
, src
, size
);
170 jpeg_read_header(cinfo
, TRUE
);
171 jpeg_start_decompress(cinfo
);
172 cinfo
->out_color_space
= COLOURSPACE
;
173 if (cinfo
->output_width
!= (guint
)sparrow
->out
.width
||
174 cinfo
->output_height
!= (guint
)sparrow
->out
.height
){
175 GST_ERROR("jpeg sizes are wrong! %dx%d, should be %dx%d.\n"
176 "Not doing anything: this is probably goodbye.\n",
177 cinfo
->output_width
, cinfo
->output_height
,
178 sparrow
->out
.width
, sparrow
->out
.height
);
184 read_one_line(GstSparrow
*sparrow
, guint8
* dest
){
185 struct jpeg_decompress_struct
*cinfo
= sparrow
->cinfo
;
186 if (cinfo
->output_scanline
< cinfo
->output_height
){
187 jpeg_read_scanlines(cinfo
, &dest
, 1);
190 GST_WARNING("wanted to read line %d of jpeg that thinks it is %d lines high!",
191 cinfo
->output_scanline
, cinfo
->output_height
);
196 finish_reading_jpeg(GstSparrow
*sparrow
){
197 jpeg_finish_decompress(sparrow
->cinfo
);
202 init_jpeg_src(GstSparrow
*sparrow
){
203 sparrow
->cinfo
= zalloc_or_die(sizeof(struct jpeg_decompress_struct
));
204 struct jpeg_error_mgr
*jerr
= zalloc_or_die(sizeof(struct jpeg_error_mgr
));
205 sparrow
->cinfo
->err
= jpeg_std_error(jerr
);
206 sparrow
->cinfo
->out_color_space
= COLOURSPACE
;
210 finalise_jpeg_src(GstSparrow
*sparrow
){
211 jpeg_destroy_decompress(sparrow
->cinfo
);
212 free(sparrow
->cinfo
->err
);
213 free(sparrow
->cinfo
);