00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "libjpeg.h"
00034 #include "include/cdjpeg.h"
00035
00036 #include <stdint.h>
00037 #include <stdbool.h>
00038 #include <assert.h>
00039
00040
00041
00042
00043
00044
00045
00046
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
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
00066
00067
00068
00069
00070
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
00112
00113
00114
00115
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;
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
00139 cinfo.raw_data_in = TRUE;
00140 cinfo.do_fancy_downsampling = FALSE;
00141
00142 jpeg_set_colorspace(&cinfo,JCS_YCbCr);
00143
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
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
00168 free(raw_image);
00169 jpeg_finish_compress( &cinfo ) ;
00170 jpeg_destroy_compress(&cinfo);
00171
00172 return 0 ;
00173 }
00174
00175
00176
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 ;
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
00213 extern uint32_t ijg_decompress( SJpegData* pData )
00214 {
00215
00216
00217
00218
00219
00220
00221
00222
00223
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
00242 for ( ; cinfo.output_scanline < cinfo.image_height ; )
00243 {
00244 jpeg_read_scanlines( &cinfo, aLines, 1 ) ;
00245
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
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
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
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
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
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
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 }