in memory jpeg decoding, using libjpeg_turbo
[sparrow.git] / jpeg_src.c
bloba45d859f9335cf856dfea31d27bb7afead05926c
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 *************************************************************************************/
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include "jpeglib.h"
28 #include "gstsparrow.h"
29 #include "sparrow.h"
31 // Expanded data source object for memory buffer input
32 typedef struct
34 struct jpeg_source_mgr pub;
35 unsigned char* buffer;
36 unsigned int bufsize;
37 } sparrow_src_mgr;
39 #define COLOURSPACE JCS_EXT_XBGR
43 Initialize source --- Nothing to do
45 static void
46 init_source (j_decompress_ptr cinfo)
50 Fill the input buffer --- called whenever buffer is emptied.
52 static gboolean
53 fill_input_buffer (j_decompress_ptr cinfo)
55 sparrow_src_mgr *src = (sparrow_src_mgr *) cinfo->src;
56 src->pub.next_input_byte = src->buffer;
57 src->pub.bytes_in_buffer = src->bufsize;
59 return TRUE;
63 Skip data --- used to skip over a potentially large amount of
64 uninteresting data.
66 static void
67 skip_input_data (j_decompress_ptr cinfo, long num_bytes)
69 sparrow_src_mgr * src = (sparrow_src_mgr *) cinfo->src;
71 /* just move the ptr */
72 src->pub.next_input_byte += num_bytes;
73 src->pub.bytes_in_buffer -= num_bytes;
77 Terminate source --- called by jpeg_finish_decompress
79 static void
80 term_source (j_decompress_ptr cinfo)
84 Prepare for input from a memory buffer.
86 void
87 jpeg_mem_src (j_decompress_ptr cinfo, unsigned char* buffer, unsigned int bufsize)
89 sparrow_src_mgr *src;
91 if (cinfo->src == NULL) {
92 cinfo->src = (struct jpeg_source_mgr *)
93 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(sparrow_src_mgr));
96 src = (sparrow_src_mgr *) cinfo->src;
97 src->pub.init_source = init_source;
98 src->pub.fill_input_buffer = fill_input_buffer;
99 src->pub.skip_input_data = skip_input_data;
100 src->pub.resync_to_restart = jpeg_resync_to_restart;
101 src->pub.term_source = term_source;
102 src->pub.bytes_in_buffer = 0;
103 src->pub.next_input_byte = NULL;
105 src->buffer = buffer;
106 src->bufsize = bufsize;
109 #define ROWS_PER_CYCLE 1
111 void
112 decompress_buffer(struct jpeg_decompress_struct *cinfo, guint8* src, int size, guint8* dest,
113 int *width, int *height)
115 jpeg_create_decompress(cinfo);
116 jpeg_mem_src(cinfo, src, size);
118 jpeg_read_header(cinfo, TRUE);
119 jpeg_start_decompress(cinfo);
120 cinfo->out_color_space = COLOURSPACE;
122 *width = cinfo->output_width;
123 *height = cinfo->output_height;
125 /*use 4 here, not cinfo->num_channels, because libjpeg_turbo is doing the
126 RGBx colour space conversion */
127 int stride = 4 * cinfo->output_width;
128 guint8* row = dest;
129 while (cinfo->output_scanline < cinfo->output_height){
130 int read = jpeg_read_scanlines(cinfo, &row, ROWS_PER_CYCLE);
131 row += stride * read;
133 jpeg_finish_decompress(cinfo);
136 void
137 init_jpeg_src(GstSparrow *sparrow){
138 sparrow->cinfo = zalloc_or_die(sizeof(struct jpeg_decompress_struct));
139 struct jpeg_error_mgr *jerr = zalloc_or_die(sizeof(struct jpeg_error_mgr));
140 sparrow->cinfo->err = jpeg_std_error(jerr);
141 sparrow->cinfo->out_color_space = COLOURSPACE;
144 void
145 finalise_jpeg_src(GstSparrow *sparrow){
146 jpeg_destroy_decompress(sparrow->cinfo);
147 free(sparrow->cinfo->err);
148 free(sparrow->cinfo);