📄 jpeglole.c
字号:
/*This programe is reedited from IJG code by Fujian Shi(fieagle@yahoo.com.cn). *This file is used to finish the compression of a gray image *jpeglole.c ,jcmarker.c ,jarithc.c,jaricom.c predic.c need to combined together. */#include "commondecls.h"void *alloc_one_row(j_compress_ptr cinfo,size_t size_object){ void *buffer_ptr; buffer_ptr=cinfo->buffer[cinfo->buffer_count++]=(void *) malloc(size_object); return buffer_ptr;}/*arrage several rows buffer,that is a array*/JSAMPARRAYalloc_sarray (JDIMENSION samplesperrow, JDIMENSION numrows,j_compress_ptr cinfo)/* Allocate a 2-D sample array */{ JSAMPARRAY result; JSAMPROW workspace; int currow; /* Get space for row pointers (small object) */ result = (JSAMPARRAY) alloc_one_row(cinfo,((size_t) numrows) * SIZEOF(JSAMPROW)); /* Get the rows themselves (large objects) */ currow = 0; while (currow < numrows) { workspace = (JSAMPROW) alloc_one_row(cinfo,((size_t) (samplesperrow) * SIZEOF(JSAMPLE))); result[currow++] = workspace; } return result;}LOCAL(void)free_mem(j_compress_ptr cinfo){ int i; for (i=0;i<cinfo->buffer_count;i++) free(cinfo->buffer[i]); }/*the fowllowing function is used to input image data to the inbuffer*/voidinput_image_data(j_compress_ptr cinfo){ int j,input_lines; JSAMPARRAY in_array=cinfo->inbuffer; input_lines=1; for(j=0;j<input_lines;j++) JFREAD(cinfo->inputfile,in_array[j],cinfo->image_width); } /*the following function acomplish the arithmetic entropy coding*/voidcompress_data(j_compress_ptr cinfo,int i){ JDIMENSION bi; JSAMPARRAY in_array=cinfo->inbuffer; /*dipose a row of image*/ encode_row (cinfo,in_array[0],i); }main(int argc,char *argv[]){ j_compress_struct main_cinfo; int i,p; char c; jpeg_destination_mgr main_dest; unsigned char fileheader[4]; /*the compact image data include only width and height*/ j_compress_ptr cinfo=&main_cinfo; arith_entropy_encoder entropy; cinfo->dest=&main_dest; cinfo->buffer_count=0; cinfo->entropy=&entropy; main_cinfo.outbuffer=(JOCTET *) alloc_one_row(cinfo,(size_t) (OUTPUT_BUF_SIZE)); /*sizeof(JOCTET) is 1 */ main_dest.next_output_byte =cinfo->outbuffer; main_dest.free_in_buffer = OUTPUT_BUF_SIZE; cinfo->arith_dc_L=1; cinfo->first_line=0; MEMZERO(cinfo->entropy->dc_stats,160); MEMZERO(cinfo->entropy->context_b,1000); MEMZERO(cinfo->entropy->val_b,1000); MEMZERO(cinfo->pre.B,4*365); MEMZERO(cinfo->pre.N,4*365); MEMZERO(cinfo->pre.C,4*365); cinfo->pre.reset=64; cinfo->entropy->last_val=0; cinfo->entropy->context=0; cinfo->arith_dc_U=2; cinfo->entropy->c = 0; cinfo->entropy->a = 0x10000L; cinfo->entropy->sc = 0; cinfo->entropy->zc = 0; cinfo->entropy->ct = 11; cinfo->entropy->buffer = -1; /* empty */ if((cinfo->inputfile=fopen(argv[1],"rb"))==NULL){ printf("The %s can't be opended\n",argv[1]); exit(0); } if((cinfo->outputfile=fopen(argv[2],"wb+"))==NULL){ printf("The %s can't be created\n",argv[2]); exit(0); } #define UCH(x) ((int)(x)) #define GET_2B(array,offset) ((unsigned int) UCH(array[offset]) + \ (((unsigned int) UCH(array[offset+1])) << 8)) if (! ReadOK(cinfo->inputfile, fileheader, 4)){ printf("error when read the head of file"); exit(0); } cinfo->image_width = (UINT16) GET_2B(fileheader,0); cinfo->image_height = (UINT16) GET_2B(fileheader,2); cinfo->inbuffer=alloc_sarray((JDIMENSION) (cinfo->image_width), 1,cinfo); /*the inbuffer is a row of image elements now for* *one demintion prediction. */ /*create the quality and huff table*/ /*create_huff (cinfo); It is keeped for later compareration*/ /*write the proper file header,for simplisity we only reserve the SOI and EOI symbole*/ write_file_header (cinfo); /*prepare for the compresion*/ /*jpeg_make_c_derived_tbl (cinfo, 1, * &(cinfo->d_dc_huff_tbl_ptr)); *jpeg_make_c_derived_tbl (cinfo, 0, * &(cinfo->d_ac_huff_tbl_ptr)); */ /*Enthopy coding and write to the JPEG file*/ for (i=0;i<(cinfo->image_height);i++){ input_image_data(cinfo); compress_data(cinfo,i); /*include DCT and entropy coding*/ if (i==0) cinfo->first_line=0; } /*kill all the buffer and output EOI marker*/ flush_bits (cinfo); /*out put the patial image data*/ write_file_trailer (cinfo); JFWRITE(cinfo->outputfile, cinfo->outbuffer, (((size_t) (OUTPUT_BUF_SIZE))-cinfo->dest->free_in_buffer)); free_mem(cinfo); /*fseek(cinfo->inputfile,-2,2);*/ p=0; while(getc(cinfo->inputfile)!=EOF) p++; printf("the mumber is%d\n",p); fclose(cinfo->inputfile); fclose(cinfo->outputfile);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -