SAMV71 Xplained Ultra Software Package 1.4

libjpeg.c

00001 /* ----------------------------------------------------------------------------
00002  *         ATMEL Microcontroller Software Support
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2009, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
00028  */
00029 
00030 /*----------------------------------------------------------------------------
00031  *        Headers
00032  *----------------------------------------------------------------------------*/
00033 #include "libjpeg.h"
00034 #include "include/cdjpeg.h"     /* Common decls for cjpeg/djpeg applications */
00035 
00036 #include <stdint.h>
00037 #include <stdbool.h>
00038 #include <assert.h>
00039 
00040 /*----------------------------------------------------------------------------
00041  *        Local functions
00042  *----------------------------------------------------------------------------*/
00043 /**
00044   * \brief allocate memory for mcu row from heap
00045   * \param image pointer to mcu row
00046   * \param cinfo compress infomation especially components info
00047   */
00048 static void _alloc_sarray_imcu_row(JSAMPIMAGE *image,j_compress_ptr cinfo)
00049 {
00050     int ci;
00051     jpeg_component_info *compptr;
00052     assert( image != NULL ) ;
00053     *image = malloc( cinfo->num_components * sizeof(JSAMPARRAY));
00054     /* Allocate a strip buffer for each component */
00055     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00056      ci++, compptr++) {
00057       (*image)[ci] = (*cinfo->mem->alloc_sarray)
00058     ((j_common_ptr) cinfo, JPOOL_IMAGE,
00059      compptr->width_in_blocks * compptr->DCT_h_scaled_size,
00060      (JDIMENSION) (compptr->v_samp_factor * compptr->DCT_v_scaled_size));
00061     }
00062 }
00063 
00064 /**
00065   * \brief initial mcu rows with yuv420 data
00066   * \param image pointer to mcu row
00067   * \param src pointer of source with yuv422 format
00068   * \prarm y_pos the start position of the mcu rows in a whole image
00069   * \param width width of the image
00070   * \param rows total number of concerned rows in the mcu
00071   */
00072 static void _init_sarray_imcu_row(JSAMPIMAGE *image,JSAMPLE *src,uint32_t y_pos,uint32_t width,uint32_t rows)
00073 {
00074     JSAMPLE *r0;
00075     JSAMPLE *r1;
00076     JSAMPARRAY ay = (*image)[0];
00077     JSAMPARRAY au = (*image)[1];
00078     JSAMPARRAY av = (*image)[2];
00079     JSAMPROW ry0,ru,rv,ry1;    
00080     
00081     int i,j;
00082     for( i = 0; i < rows/2 ;i++)
00083     {
00084         r0 = & src[ (i * 2 + y_pos) * width * 2];
00085         r1 = & src[ (i * 2 + y_pos + 1) * width * 2];
00086         
00087         ry0 = ay[i*2];
00088         ry1 = ay[i*2+1];
00089         
00090         ru = au[i];
00091         rv = av[i];
00092         
00093         for(j = 0; j < width /2; j++)
00094         {
00095             ry0[j * 2] = r0[j*4];
00096             ry1[j * 2] = r1[j*4];
00097             ry0[j * 2 + 1] = r0[j*4+2];
00098             ry1[j * 2 +1] = r1[j*4+2];
00099             
00100             ru[j] = (r0[ j * 4 + 1] + r1[j*4+1])/2;
00101             rv[j] = (r0[ j * 4 + 3] + r1[j*4+3])/2;
00102             
00103         }
00104         
00105         
00106     }
00107 }
00108 
00109 
00110 /*----------------------------------------------------------------------------
00111  *        Exported functions
00112  *----------------------------------------------------------------------------*/
00113 /**
00114   * \brief do comperession from raw yuv420 data
00115   * \param pData pointer to jpeg information gaved by main application
00116   */
00117 extern uint32_t ijg_compress_raw_no_padding( SJpegData* pData )
00118 {
00119     struct jpeg_compress_struct cinfo ;
00120     struct jpeg_error_mgr       jerr ;
00121     
00122     JSAMPIMAGE  raw_image;          /* pointer to raw pointer */
00123     
00124     uint32_t lines;
00125 
00126     assert( pData != NULL ) ;
00127 
00128     cinfo.err = jpeg_std_error( &jerr ) ;
00129     jpeg_create_compress( &cinfo ) ;
00130     jpeg_mem_dest( &cinfo, &(pData->pucDst), (unsigned long*)&(pData->dwDstLength) ) ;
00131 
00132     cinfo.image_width      = pData->dwWidth ;
00133     cinfo.image_height     = pData->dwHeight ;
00134     cinfo.input_components = pData->dwBPP ;
00135     cinfo.in_color_space   = JCS_YCbCr ;
00136 
00137     jpeg_set_defaults( &cinfo ) ;
00138     /* set this for raw data*/
00139     cinfo.raw_data_in = TRUE;
00140     cinfo.do_fancy_downsampling = FALSE;
00141     /* color space is yuv*/
00142     jpeg_set_colorspace(&cinfo,JCS_YCbCr);
00143     /* factors for 3 components horizontally and vertically*/
00144     cinfo.comp_info[0].h_samp_factor = 2;
00145     cinfo.comp_info[0].v_samp_factor = 2;
00146     cinfo.comp_info[1].h_samp_factor = 1;
00147     cinfo.comp_info[1].v_samp_factor = 1;
00148     cinfo.comp_info[2].h_samp_factor = 1;
00149     cinfo.comp_info[2].v_samp_factor = 1;
00150     
00151     cinfo.dct_method = pData->eMethod ;
00152     jpeg_set_quality( &cinfo, pData->dwQuality, true ) ;
00153     jpeg_start_compress( &cinfo, true ) ;
00154     
00155     lines = cinfo.max_v_samp_factor * DCTSIZE;
00156     
00157     /* allocate memory for raw_image*/
00158     _alloc_sarray_imcu_row(&raw_image,&cinfo);
00159 
00160     while ( cinfo.next_scanline < cinfo.image_height )
00161     {  
00162         _init_sarray_imcu_row(&raw_image,pData->pucSrc,cinfo.next_scanline,cinfo.image_width,lines);
00163         jpeg_write_raw_data( &cinfo, raw_image, lines ) ;
00164         
00165     }
00166     
00167     /* free allocated memory*/
00168     free(raw_image);
00169     jpeg_finish_compress( &cinfo ) ;
00170     jpeg_destroy_compress(&cinfo);
00171 
00172     return 0 ;
00173 }
00174 /**
00175   * \brief ijg compress of recommened method with pre-processing
00176   * \param pData pointer to jpeg information gaved by main application
00177   */
00178 extern uint32_t ijg_compress( SJpegData* pData )
00179 {
00180     struct jpeg_compress_struct cinfo ;
00181     struct jpeg_error_mgr       jerr ;
00182     JSAMPROW row_pointer ;          /* pointer to a single row */
00183 
00184     assert( pData != NULL ) ;
00185 
00186     cinfo.err = jpeg_std_error( &jerr ) ;
00187     jpeg_create_compress( &cinfo ) ;
00188     jpeg_mem_dest( &cinfo, &(pData->pucDst), (unsigned long*)&(pData->dwDstLength) ) ;
00189 
00190     cinfo.image_width      = pData->dwWidth ;
00191     cinfo.image_height     = pData->dwHeight ;
00192     cinfo.input_components = pData->dwBPP ;
00193     cinfo.in_color_space   = pData->eInput ;
00194 
00195     jpeg_set_defaults( &cinfo ) ;
00196     cinfo.dct_method = pData->eMethod ;
00197     jpeg_set_quality( &cinfo, pData->dwQuality, true ) ;
00198     jpeg_start_compress( &cinfo, true ) ;
00199 
00200     while ( cinfo.next_scanline < cinfo.image_height )
00201     {
00202         row_pointer = (JSAMPROW) &pData->pucSrc[cinfo.next_scanline*cinfo.image_width*cinfo.input_components] ;
00203         jpeg_write_scanlines( &cinfo, &row_pointer, 1 ) ;
00204     }
00205 
00206     jpeg_finish_compress( &cinfo ) ;
00207     jpeg_destroy_compress(&cinfo);
00208 
00209     return 0 ;
00210 }
00211 
00212 /** entry for decompression */
00213 extern uint32_t ijg_decompress( SJpegData* pData )
00214 {
00215 //        Allocate and initialize a JPEG decompression object
00216 //        Specify the source of the compressed data (eg, a file)
00217 //        Call jpeg_read_header() to obtain image info
00218 //        Set parameters for decompression
00219 //        jpeg_start_decompress(...);
00220 //        while (scan lines remain to be read)
00221 //                jpeg_read_scanlines(...);
00222 //        jpeg_finish_decompress(...);
00223 //        Release the JPEG decompression object
00224 
00225     struct jpeg_decompress_struct cinfo ;
00226     struct jpeg_error_mgr jerr ;
00227     uint32_t dwSourceLength ;
00228     JSAMPROW aLines[2]={ pData->pucDst, pData->pucDst+pData->dwWidth*pData->dwBPP } ;
00229 
00230     assert( pData != NULL ) ;
00231 
00232     cinfo.err=jpeg_std_error( &jerr ) ;
00233 
00234     jpeg_create_decompress( &cinfo ) ;
00235     dwSourceLength=pData->dwHeight*pData->dwWidth*pData->dwBPP ;
00236     jpeg_mem_src( &cinfo, (uint8_t*)pData->pucSrc, dwSourceLength ) ;
00237     jpeg_read_header( &cinfo, TRUE ) ;
00238     cinfo.dct_method = pData->eMethod ;
00239     jpeg_start_decompress( &cinfo ) ;
00240 
00241 //    dwLines=dwDstLength/(dwWidth*3) ;
00242     for ( ; cinfo.output_scanline < cinfo.image_height ; )
00243     {
00244         jpeg_read_scanlines( &cinfo, aLines, 1 ) ;
00245 //        jpeg_read_scanlines( &cinfo, aLines, dwLines ) ;
00246 
00247         if ( pData->cbk )
00248         {
00249             if ( pData->cbk( aLines[0], pData->dwWidth*pData->dwBPP ) != 0 )
00250             {
00251                 break ;
00252             }
00253         }
00254 
00255     }
00256 
00257     jpeg_finish_decompress( &cinfo ) ;
00258     jpeg_destroy_decompress( &cinfo ) ;
00259 
00260     return 0 ;
00261 }
00262 
00263 /** initialization of jpeg information*/
00264 extern uint32_t JpegData_Init( SJpegData* pData )
00265 {
00266     assert( pData != NULL ) ;
00267 
00268     memset( pData, 0, sizeof( SJpegData ) ) ;
00269 
00270     return 0 ;
00271 }
00272 
00273 /** set the source data of the jpeg compression*/
00274 extern uint32_t JpegData_SetSource( SJpegData* pData, uint8_t* pucSrc, uint32_t dwSrcLength )
00275 {
00276     assert( pData != NULL ) ;
00277 
00278     pData->pucSrc=pucSrc ;
00279     pData->dwSrcLength=dwSrcLength ;
00280 
00281     return 0 ;
00282 }
00283 
00284 /** set destination for jpeg data*/
00285 extern uint32_t JpegData_SetDestination( SJpegData* pData, uint8_t* pucDst, uint32_t dwDstLength )
00286 {
00287     assert( pData != NULL ) ;
00288 
00289     pData->pucDst=pucDst ;
00290     pData->dwDstLength=dwDstLength ;
00291 
00292     return 0 ;
00293 }
00294 
00295 /** set dimensions of image*/
00296 extern uint32_t JpegData_SetDimensions( SJpegData* pData, uint32_t dwWidth, uint32_t dwHeight, uint32_t dwBPP )
00297 {
00298     assert( pData != NULL ) ;
00299 
00300     pData->dwWidth=dwWidth ;
00301     pData->dwHeight=dwHeight ;
00302     pData->dwBPP=dwBPP ;
00303 
00304     return 0 ;
00305 }
00306 
00307 /** set parameters like quality, compression method and input format*/
00308 extern uint32_t JpegData_SetParameters( SJpegData* pData, uint32_t dwQuality, EJpegInput eInput, EJpegMethod eMethod )
00309 {
00310     assert( pData != NULL ) ;
00311 
00312     pData->dwQuality=dwQuality ;
00313     pData->eInput=eInput ;
00314     pData->eMethod=eMethod ;
00315 
00316     return 0 ;
00317 }
00318 
00319 /** set callback function for using in main application*/
00320 extern uint32_t JpegData_SetCallback( SJpegData* pData, uint32_t (*cbk)( uint8_t*, uint32_t ) )
00321 {
00322     assert( pData != NULL ) ;
00323 
00324     pData->cbk=cbk ;
00325 
00326     return 0 ;
00327 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines