fix a few more int-isms around coord_t
[sparrow.git] / jpeg_src.c
blobb48ed5e0649640ba445c38b300d58ef2ebd0faa9
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 *************************************************************************************/
26 /* SYNOPSIS
28 GstSparrow *sparrow;
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
35 FOR EACH jpeg {
36 begin_reading_jpeg(sparrow, src, size);
37 FOR EACH line {
38 read_one_line(sparrow, dest);
40 finish_reading_jpeg(sparrow);
43 finalise_jpeg_src(sparrow); // once only (optional really)
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include "jpeglib.h"
50 #include "gstsparrow.h"
51 #include "sparrow.h"
53 // Expanded data source object for memory buffer input
54 typedef struct
56 struct jpeg_source_mgr pub;
57 unsigned char* buffer;
58 unsigned int bufsize;
59 } sparrow_src_mgr;
61 #define COLOURSPACE JCS_EXT_BGRX
65 Initialize source --- Nothing to do
67 static void
68 init_source (j_decompress_ptr cinfo)
72 Fill the input buffer --- called whenever buffer is emptied.
74 static gboolean
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;
81 return TRUE;
85 Skip data --- used to skip over a potentially large amount of
86 uninteresting data.
88 static void
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
101 static void
102 term_source (j_decompress_ptr cinfo)
106 Prepare for input from a memory buffer.
108 static void
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 */
134 INVISIBLE void
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;
151 guint8* row = dest;
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);
162 INVISIBLE void
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);
183 INVISIBLE void
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);
189 else {
190 GST_WARNING("wanted to read line %d of jpeg that thinks it is %d lines high!",
191 cinfo->output_scanline, cinfo->output_height);
195 INVISIBLE void
196 finish_reading_jpeg(GstSparrow *sparrow){
197 jpeg_finish_decompress(sparrow->cinfo);
201 INVISIBLE void
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;
209 INVISIBLE void
210 finalise_jpeg_src(GstSparrow *sparrow){
211 jpeg_destroy_decompress(sparrow->cinfo);
212 free(sparrow->cinfo->err);
213 free(sparrow->cinfo);