jdatasrc.c File Reference


Detailed Description

IJG JPEG library data input module.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file EXAMPLE/jdatasrc.c.

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include "compiler.h"
#include "board.h"
#include "et024006dhu.h"
#include "jpeg_decoder.h"
#include "conf_jpeg_decoder.h"
#include "jpeglib.h"
#include "jinclude.h"

Go to the source code of this file.

Data Structures

struct  extended_error_mgr
struct  jpeg_lib
struct  my_source_mgr

Defines

#define DEFAULT_MAX_IMAGE_HEIGHT   128
#define DEFAULT_MAX_IMAGE_WIDTH   128
#define DEFAULT_PIXEL_SIZE_IN_BYTES   2
#define JPEG_INPUT_BUF_SIZE   1024

Typedefs

typedef struct extended_error_mgrextended_error_ptr
typedef my_source_mgrmy_src_ptr

Functions

 extended_error_exit (j_common_ptr cinfo)
static boolean fill_input_buffer (j_decompress_ptr cinfo)
static void init_source (j_decompress_ptr cinfo)
Bool jpeg_lib_decode (int offset)
void * jpeg_lib_decode_ex (int offset, U16 *width, U16 *height)
void jpeg_lib_exit (void)
Bool jpeg_lib_init (void)
void jpeg_stdio_src (j_decompress_ptr cinfo, FILE *infile)
static void skip_input_data (j_decompress_ptr cinfo, long num_bytes)
void stream_close ()
void stream_open (void)
size_t stream_read (JOCTET *buffer, size_t nb_byte)
void stream_seek (int offset)
static void term_source (j_decompress_ptr cinfo)

Variables

static struct jpeg_lib jpeg_lib_data
uint16_t * jpeg_out_buffer_pos
const U8 * stream_jpeg_src_ptr
static size_t stream_offset
U16 stream_src_size


Define Documentation

#define DEFAULT_MAX_IMAGE_HEIGHT   128

Definition at line 73 of file EXAMPLE/jdatasrc.c.

#define DEFAULT_MAX_IMAGE_WIDTH   128

Definition at line 72 of file EXAMPLE/jdatasrc.c.

#define DEFAULT_PIXEL_SIZE_IN_BYTES   2

Definition at line 74 of file EXAMPLE/jdatasrc.c.

#define JPEG_INPUT_BUF_SIZE   1024

Definition at line 75 of file EXAMPLE/jdatasrc.c.

Referenced by fill_input_buffer(), and jpeg_stdio_src().


Typedef Documentation

Definition at line 148 of file EXAMPLE/jdatasrc.c.

Definition at line 128 of file EXAMPLE/jdatasrc.c.


Function Documentation

extended_error_exit ( j_common_ptr  cinfo  ) 

Definition at line 155 of file EXAMPLE/jdatasrc.c.

References extended_error_mgr::setjmp_buffer.

Referenced by jpeg_lib_init().

00156 {
00157   /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
00158   extended_error_ptr extended_err = (extended_error_ptr) cinfo->err;
00159 
00160   /* Always display the message. */
00161   /* We could postpone this until after returning, if we chose. */
00162   (*cinfo->err->output_message) (cinfo);
00163 
00164   /* Return control to the setjmp point */
00165   longjmp(extended_err->setjmp_buffer, 1);
00166 }

static boolean fill_input_buffer ( j_decompress_ptr  cinfo  )  [static]

Definition at line 170 of file EXAMPLE/jdatasrc.c.

References my_source_mgr::buffer, jpeg_source_mgr::bytes_in_buffer, FALSE, JPEG_EOI, JPEG_INPUT_BUF_SIZE, jpeg_source_mgr::next_input_byte, my_source_mgr::pub, jpeg_decompress_struct::src, my_source_mgr::start_of_file, stream_read(), and TRUE.

Referenced by jpeg_stdio_src(), and skip_input_data().

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);     //  nbytes = file_read_buf(src->buffer, JPEG_INPUT_BUF_SIZE);
00176 
00177     if(nbytes <= 0)
00178     {
00179         if (src->start_of_file)
00180         { /* Treat empty input file as fatal error */
00181             return FALSE;//ERREXIT(cinfo, JERR_INPUT_EMPTY);
00182         }
00183         //WARNMS(cinfo, JWRN_JPEG_EOF);
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 }

static void init_source ( j_decompress_ptr  cinfo  )  [static]

Definition at line 220 of file EXAMPLE/jdatasrc.c.

References jpeg_decompress_struct::src, my_source_mgr::start_of_file, and TRUE.

Referenced by jpeg_stdio_src().

00221 {
00222   my_src_ptr src = (my_src_ptr) cinfo->src;
00223 
00224   /* We reset the empty-input-file flag for each image,
00225    * but we don't clear the input buffer.
00226    * This is correct behavior for reading a series of images from one source.
00227    */
00228   src->start_of_file = TRUE;
00229 }

Bool jpeg_lib_decode ( int  offset  ) 

Definition at line 293 of file EXAMPLE/jdatasrc.c.

References jpeg_lib::cinfo, jpeg_decompress_struct::dct_method, jpeg_decompress_struct::do_fancy_upsampling, jpeg_decompress_struct::image_height, jpeg_decompress_struct::image_width, JCS_RGB565, JDCT_FASTEST, jpeg_abort_decompress(), jpeg_finish_decompress(), JPEG_HEADER_OK, jpeg_lib_data, jpeg_out_buffer_pos, jpeg_read_header(), jpeg_read_scanlines(), jpeg_stdio_src(), max_lines, jpeg_decompress_struct::out_color_space, jpeg_decompress_struct::output_height, jpeg_lib::output_image, jpeg_decompress_struct::output_scanline, jpeg_decompress_struct::output_width, jpeg_decompress_struct::scale_denom, stream_close(), stream_open(), stream_seek(), and TRUE.

Referenced by jpeg_lib_decode_ex().

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     // set output image position for the JPEG library
00305     jpeg_out_buffer_pos = (uint16_t *) jpeg_lib_data.output_image;
00306 
00307     //file_ptr = 0;
00308     stream_open();              //  file_open(FOPEN_MODE_R);
00309     if(offset)
00310         stream_seek(offset);    //  file_seek(offset, FS_SEEK_SET);
00311 
00312     // set file to read from
00313     jpeg_stdio_src(cinfo, 0);
00314 
00315     /* Establish the setjmp return context for my_error_exit to use. */
00316     if (setjmp(jerr->setjmp_buffer))
00317     {
00318         /* If we get here, the JPEG code has signaled an error.
00319          * We need to clean up the JPEG object, close the input file, and return.
00320          */
00321         jpeg_abort_decompress(cinfo);
00322         stream_close();     //  file_close();
00323         return false;
00324     }
00325     // read the file header
00326     if(JPEG_HEADER_OK != jpeg_read_header(cinfo, TRUE))
00327     {
00328         jpeg_abort_decompress(cinfo);
00329         stream_close();     //  file_close();
00330         return false;
00331     }
00332 
00333     // set decompression configuration
00334     scale_denom = 0;
00335 
00336     // set correct scaling to fit the reserved space
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     // any scaling above 1/8 is not possible
00344     if(scale_denom > 8)
00345     {
00346         stream_close();     //  file_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     // submit the requested decompression parameters
00358     // this call will also adjust invalid settings
00359     if( jpeg_start_decompress(cinfo) == false)
00360     {
00361         jpeg_abort_decompress(cinfo);
00362         stream_close();     //  file_close();
00363         return false;
00364     }
00365 
00366     // read scanlines
00367     //
00368     while(cinfo->output_scanline < cinfo->output_height)
00369     {
00370         // get decoded scanlines
00371         jpeg_read_scanlines(cinfo, NULL, max_lines);
00372     }
00373 
00374     jpeg_finish_decompress(cinfo);
00375 
00376     stream_close();     //  file_close();
00377     return true;
00378 }

void* jpeg_lib_decode_ex ( int  offset,
U16 *  width,
U16 *  height 
)

Definition at line 276 of file EXAMPLE/jdatasrc.c.

References jpeg_lib::cinfo, jpeg_lib_data, jpeg_lib_decode(), jpeg_decompress_struct::output_height, jpeg_lib::output_image, and jpeg_decompress_struct::output_width.

Referenced by main().

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 }

void jpeg_lib_exit ( void   ) 

Definition at line 417 of file EXAMPLE/jdatasrc.c.

References jpeg_lib::cinfo, free(), jpeg_destroy_decompress(), jpeg_lib_data, and jpeg_lib::output_image.

Referenced by main().

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         // release internal memory
00428         jpeg_destroy_decompress(jpeg_lib_data.cinfo);
00429         free(jpeg_lib_data.cinfo);
00430     }
00431 }

Bool jpeg_lib_init ( void   ) 

Definition at line 382 of file EXAMPLE/jdatasrc.c.

References jpeg_lib::cinfo, extended_error_exit(), free(), jpeg_create_decompress, JPEG_DECODER_MAX_IMAGE_HEIGHT, JPEG_DECODER_MAX_IMAGE_WIDTH, JPEG_DECODER_PIXEL_SIZE_IN_BYTES, jpeg_lib_data, jpeg_std_error(), malloc(), and jpeg_lib::output_image.

Referenced by main().

00383 {
00384     // allocate the decompression structure
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     // allocate the buffer for the decompressed image
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     // initialize error handler
00408     jpeg_std_error(jpeg_lib_data.cinfo->err);
00409     jpeg_lib_data.cinfo->err->error_exit = extended_error_exit;
00410 
00411     // initialize the decompression struct
00412     jpeg_create_decompress(jpeg_lib_data.cinfo);
00413 
00414     return true;
00415 }

void jpeg_stdio_src ( j_decompress_ptr  cinfo,
FILE *  infile 
)

Definition at line 240 of file EXAMPLE/jdatasrc.c.

References my_source_mgr::buffer, jpeg_source_mgr::bytes_in_buffer, fill_input_buffer(), my_source_mgr::infile, init_source(), JPEG_INPUT_BUF_SIZE, jpeg_resync_to_restart(), JPOOL_PERMANENT, jpeg_source_mgr::next_input_byte, my_source_mgr::pub, SIZEOF, skip_input_data(), jpeg_decompress_struct::src, and term_source().

Referenced by jpeg_lib_decode(), main(), and read_JPEG_file().

00241 {
00242   my_src_ptr src;
00243 
00244   /* The source object and input buffer are made permanent so that a series
00245    * of JPEG images can be read from the same file by calling jpeg_stdio_src
00246    * only before the first one.  (If we discarded the buffer at the end of
00247    * one image, we'd likely lose the start of the next one.)
00248    * This makes it unsafe to use this manager and a different source
00249    * manager serially with the same JPEG object.  Caveat programmer.
00250    */
00251   if (cinfo->src == NULL) {     /* first time for this JPEG object? */
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     // allocate input buffer
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; /* use default method */
00267   src->pub.term_source = term_source;
00268   src->infile = infile;
00269   src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
00270   src->pub.next_input_byte = NULL; /* until buffer loaded */
00271 }

static void skip_input_data ( j_decompress_ptr  cinfo,
long  num_bytes 
) [static]

Definition at line 197 of file EXAMPLE/jdatasrc.c.

References jpeg_source_mgr::bytes_in_buffer, fill_input_buffer(), jpeg_source_mgr::next_input_byte, my_source_mgr::pub, and jpeg_decompress_struct::src.

Referenced by jpeg_stdio_src().

00198 {
00199   my_src_ptr src = (my_src_ptr) cinfo->src;
00200 
00201   /* Just a dumb implementation for now.  Could use fseek() except
00202    * it doesn't work on pipes.  Not clear that being smart is worth
00203    * any trouble anyway --- large skips are infrequent.
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       /* note we assume that fill_input_buffer will never return FALSE,
00210        * so suspension need not be handled.
00211        */
00212     }
00213     src->pub.next_input_byte += (size_t) num_bytes;
00214     src->pub.bytes_in_buffer -= (size_t) num_bytes;
00215   }
00216 }

void stream_close (  ) 

Definition at line 107 of file EXAMPLE/jdatasrc.c.

Referenced by jpeg_lib_decode().

00108 {
00109 }

void stream_open ( void   ) 

Definition at line 91 of file EXAMPLE/jdatasrc.c.

References stream_offset.

Referenced by jpeg_lib_decode().

00092 {
00093   stream_offset = 0;
00094 }

size_t stream_read ( JOCTET buffer,
size_t  nb_byte 
)

Definition at line 96 of file EXAMPLE/jdatasrc.c.

References stream_jpeg_src_ptr, stream_offset, and stream_src_size.

Referenced by fill_input_buffer().

00097 { // assume nb_byte never goes beyond the stream buffer size
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 }

void stream_seek ( int  offset  ) 

Definition at line 111 of file EXAMPLE/jdatasrc.c.

References stream_offset.

Referenced by jpeg_lib_decode().

00112 { // assume offset is always > 0 and inside the JPEG stream buffer
00113     stream_offset += offset;
00114 }

static void term_source ( j_decompress_ptr  cinfo  )  [static]

Definition at line 233 of file EXAMPLE/jdatasrc.c.

Referenced by jpeg_stdio_src().

00234 {
00235   /* no work necessary here */
00236 }


Variable Documentation

struct jpeg_lib jpeg_lib_data [static]

Definition at line 274 of file EXAMPLE/jdatasrc.c.

Referenced by jpeg_lib_decode(), and ycc_rgb565_convert().

Definition at line 101 of file ijg_example.c.

Referenced by main(), and stream_read().

size_t stream_offset [static]

Definition at line 89 of file EXAMPLE/jdatasrc.c.

Referenced by stream_open(), stream_read(), and stream_seek().

Definition at line 102 of file ijg_example.c.

Referenced by main(), and stream_read().


Generated on Fri Feb 19 02:31:02 2010 for AVR32 - IJG JPEG Decoder Example by  doxygen 1.5.5