00001
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #include <stdbool.h>
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048 #include <setjmp.h>
00049
00050 #include "compiler.h"
00051 #include "board.h"
00052
00053 #include "et024006dhu.h"
00054 #include "jpeg_decoder.h"
00055 #include "conf_jpeg_decoder.h"
00056
00057
00058 #include "jpeglib.h"
00059 #include "jinclude.h"
00060
00061 struct jpeg_lib
00062 {
00063 uint8_t *output_image;
00064 struct jpeg_decompress_struct *cinfo;
00065
00066 };
00067 static struct jpeg_lib jpeg_lib_data;
00068
00069
00070
00071
00072 #define DEFAULT_MAX_IMAGE_WIDTH 128
00073 #define DEFAULT_MAX_IMAGE_HEIGHT 128
00074 #define DEFAULT_PIXEL_SIZE_IN_BYTES 2
00075 #define JPEG_INPUT_BUF_SIZE 1024
00076
00077 #if !defined(JPEG_DECODER_MAX_IMAGE_WIDTH)
00078 #define JPEG_DECODER_MAX_IMAGE_WIDTH DEFAULT_MAX_IMAGE_WIDTH
00079 #endif
00080 #if !defined(JPEG_DECODER_MAX_IMAGE_HEIGHT)
00081 #define JPEG_DECODER_MAX_IMAGE_HEIGHT DEFAULT_MAX_IMAGE_HEIGHT
00082 #endif
00083 #if !defined(JPEG_DECODER_PIXEL_SIZE_IN_BYTES)
00084 #define JPEG_DECODER_PIXEL_SIZE_IN_BYTES DEFAULT_PIXEL_SIZE_IN_BYTES
00085 #endif
00086
00087 extern const U8 *stream_jpeg_src_ptr;
00088 extern U16 stream_src_size;
00089 static size_t stream_offset;
00090
00091 void stream_open(void)
00092 {
00093 stream_offset = 0;
00094 }
00095
00096 size_t stream_read(JOCTET * buffer, size_t nb_byte)
00097 {
00098 if ((stream_offset+nb_byte) > (size_t)stream_src_size)
00099 {
00100 nb_byte = (size_t)stream_src_size - stream_offset;
00101 }
00102 memcpy (buffer, (stream_jpeg_src_ptr + stream_offset), nb_byte);
00103 stream_offset += nb_byte;
00104 return nb_byte;
00105 }
00106
00107 void stream_close()
00108 {
00109 }
00110
00111 void stream_seek(int offset)
00112 {
00113 stream_offset += offset;
00114 }
00115
00116
00117
00118
00119
00120 typedef struct {
00121 struct jpeg_source_mgr pub;
00122
00123 FILE * infile;
00124 JOCTET * buffer;
00125 boolean start_of_file;
00126 } my_source_mgr;
00127
00128 typedef my_source_mgr * my_src_ptr;
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 struct extended_error_mgr {
00143 struct jpeg_error_mgr pub;
00144
00145 jmp_buf setjmp_buffer;
00146 };
00147
00148 typedef struct extended_error_mgr * extended_error_ptr;
00149
00150
00151
00152
00153
00154 METHODDEF(void)
00155 extended_error_exit (j_common_ptr cinfo)
00156 {
00157
00158 extended_error_ptr extended_err = (extended_error_ptr) cinfo->err;
00159
00160
00161
00162 (*cinfo->err->output_message) (cinfo);
00163
00164
00165 longjmp(extended_err->setjmp_buffer, 1);
00166 }
00167
00168
00169
00170 static boolean fill_input_buffer(j_decompress_ptr cinfo)
00171 {
00172 my_src_ptr src = (my_src_ptr) cinfo->src;
00173 size_t nbytes;
00174
00175 nbytes = stream_read(src->buffer, JPEG_INPUT_BUF_SIZE);
00176
00177 if(nbytes <= 0)
00178 {
00179 if (src->start_of_file)
00180 {
00181 return FALSE;
00182 }
00183
00184 src->buffer[0] = (JOCTET) 0xFF;
00185 src->buffer[1] = (JOCTET) JPEG_EOI;
00186 nbytes = 2;
00187 }
00188 src->pub.next_input_byte = src->buffer;
00189 src->pub.bytes_in_buffer = nbytes;
00190 src->start_of_file = FALSE;
00191
00192 return TRUE;
00193 }
00194
00195
00196
00197 static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
00198 {
00199 my_src_ptr src = (my_src_ptr) cinfo->src;
00200
00201
00202
00203
00204
00205 if (num_bytes > 0) {
00206 while (num_bytes > (long) src->pub.bytes_in_buffer) {
00207 num_bytes -= (long) src->pub.bytes_in_buffer;
00208 (void) fill_input_buffer(cinfo);
00209
00210
00211
00212 }
00213 src->pub.next_input_byte += (size_t) num_bytes;
00214 src->pub.bytes_in_buffer -= (size_t) num_bytes;
00215 }
00216 }
00217
00218
00219
00220 static void init_source (j_decompress_ptr cinfo)
00221 {
00222 my_src_ptr src = (my_src_ptr) cinfo->src;
00223
00224
00225
00226
00227
00228 src->start_of_file = TRUE;
00229 }
00230
00231
00232
00233 static void term_source (j_decompress_ptr cinfo)
00234 {
00235
00236 }
00237
00238
00239
00240 void jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile)
00241 {
00242 my_src_ptr src;
00243
00244
00245
00246
00247
00248
00249
00250
00251 if (cinfo->src == NULL) {
00252 cinfo->src = (struct jpeg_source_mgr *)
00253 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00254 SIZEOF(my_source_mgr));
00255 src = (my_src_ptr) cinfo->src;
00256
00257 src->buffer = (JOCTET *)
00258 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
00259 JPEG_INPUT_BUF_SIZE * SIZEOF(JOCTET));
00260 }
00261
00262 src = (my_src_ptr) cinfo->src;
00263 src->pub.init_source = init_source;
00264 src->pub.fill_input_buffer = fill_input_buffer;
00265 src->pub.skip_input_data = skip_input_data;
00266 src->pub.resync_to_restart = jpeg_resync_to_restart;
00267 src->pub.term_source = term_source;
00268 src->infile = infile;
00269 src->pub.bytes_in_buffer = 0;
00270 src->pub.next_input_byte = NULL;
00271 }
00272
00273
00274 uint16_t *jpeg_out_buffer_pos;
00275
00276 void *jpeg_lib_decode_ex(int offset, U16 *width, U16 *height)
00277 {
00278 struct jpeg_decompress_struct *cinfo = (struct jpeg_decompress_struct *)
00279 jpeg_lib_data.cinfo;
00280
00281 cinfo->output_width = *width;
00282 cinfo->output_height = *height;
00283
00284 if (!jpeg_lib_decode(offset))
00285 return NULL;
00286
00287 *width = cinfo->output_width;
00288 *height = cinfo->output_height;
00289
00290 return (void *) jpeg_lib_data.output_image;
00291 }
00292
00293 Bool jpeg_lib_decode(int offset)
00294 {
00295 struct jpeg_decompress_struct *cinfo = (struct jpeg_decompress_struct *) jpeg_lib_data.cinfo;
00296 struct extended_error_mgr *jerr = (struct extended_error_mgr *) cinfo->err;
00297 uint16_t max_lines = 1;
00298 uint16_t scale_denom;
00299 uint16_t max_width, max_height;
00300
00301 max_width = cinfo->output_width;
00302 max_height = cinfo->output_height;
00303
00304
00305 jpeg_out_buffer_pos = (uint16_t *) jpeg_lib_data.output_image;
00306
00307
00308 stream_open();
00309 if(offset)
00310 stream_seek(offset);
00311
00312
00313 jpeg_stdio_src(cinfo, 0);
00314
00315
00316 if (setjmp(jerr->setjmp_buffer))
00317 {
00318
00319
00320
00321 jpeg_abort_decompress(cinfo);
00322 stream_close();
00323 return false;
00324 }
00325
00326 if(JPEG_HEADER_OK != jpeg_read_header(cinfo, TRUE))
00327 {
00328 jpeg_abort_decompress(cinfo);
00329 stream_close();
00330 return false;
00331 }
00332
00333
00334 scale_denom = 0;
00335
00336
00337 while(((cinfo->image_width >> scale_denom) > max_width)
00338 || ((cinfo->image_height >> scale_denom) > max_height))
00339 {
00340 scale_denom++;
00341 }
00342 scale_denom = 1 << scale_denom;
00343
00344 if(scale_denom > 8)
00345 {
00346 stream_close();
00347 return false;
00348 }
00349
00350 cinfo->scale_denom = scale_denom;
00351
00352
00353 cinfo->dct_method = JDCT_FASTEST;
00354 cinfo->do_fancy_upsampling = false;
00355 cinfo->out_color_space = JCS_RGB565;
00356
00357
00358
00359 if( jpeg_start_decompress(cinfo) == false)
00360 {
00361 jpeg_abort_decompress(cinfo);
00362 stream_close();
00363 return false;
00364 }
00365
00366
00367
00368 while(cinfo->output_scanline < cinfo->output_height)
00369 {
00370
00371 jpeg_read_scanlines(cinfo, NULL, max_lines);
00372 }
00373
00374 jpeg_finish_decompress(cinfo);
00375
00376 stream_close();
00377 return true;
00378 }
00379
00380
00381
00382 Bool jpeg_lib_init(void)
00383 {
00384
00385 jpeg_lib_data.cinfo = malloc(sizeof(struct jpeg_decompress_struct ));
00386
00387 if(!jpeg_lib_data.cinfo)
00388 return false;
00389
00390 jpeg_lib_data.cinfo->err = (struct jpeg_error_mgr *) malloc(sizeof(struct extended_error_mgr));
00391
00392 if(!jpeg_lib_data.cinfo->err)
00393 {
00394 free(jpeg_lib_data.cinfo);
00395 return false;
00396 }
00397
00398 jpeg_lib_data.output_image = malloc(
00399 JPEG_DECODER_MAX_IMAGE_WIDTH * JPEG_DECODER_MAX_IMAGE_HEIGHT * JPEG_DECODER_PIXEL_SIZE_IN_BYTES);
00400
00401 if(!jpeg_lib_data.output_image)
00402 {
00403 free(jpeg_lib_data.cinfo->err);
00404 free(jpeg_lib_data.cinfo);
00405 return false;
00406 }
00407
00408 jpeg_std_error(jpeg_lib_data.cinfo->err);
00409 jpeg_lib_data.cinfo->err->error_exit = extended_error_exit;
00410
00411
00412 jpeg_create_decompress(jpeg_lib_data.cinfo);
00413
00414 return true;
00415 }
00416
00417 void jpeg_lib_exit(void)
00418 {
00419 if(jpeg_lib_data.output_image)
00420 free(jpeg_lib_data.output_image);
00421
00422 if(jpeg_lib_data.cinfo->err)
00423 free(jpeg_lib_data.cinfo->err);
00424
00425 if(jpeg_lib_data.cinfo)
00426 {
00427
00428 jpeg_destroy_decompress(jpeg_lib_data.cinfo);
00429 free(jpeg_lib_data.cinfo);
00430 }
00431 }