📄 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 by PDCM *jpeglole.c ,jcmarker.c ,jarithc.c,jaricom.c , dpredic.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]); }main(int argc,char *argv[]){ j_compress_struct main_cinfo; int i,p; char c; jpeg_destination_mgr main_dest; unsigned char fileheader[8]; /*the compact image data include only width and height*/ j_compress_ptr cinfo=&main_cinfo; arith_entropy_encoder entropy; jpeg_source_mgr src; cinfo->src=&src; 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; cinfo->unread_marker=0; MEMZERO(cinfo->entropy->dc_stats,160); MEMZERO(cinfo->entropy->context_b,1000); MEMZERO(cinfo->entropy->val_b,1000); MEMZERO(cinfo->pre.N,365*4); MEMZERO(cinfo->pre.B,365*4); MEMZERO(cinfo->pre.C,365*4); cinfo->pre.reset=64; cinfo->arith_dc_U=2; cinfo->entropy->c = 0; cinfo->entropy->a = 0x10000; cinfo->entropy->sc = 0; cinfo->entropy->zc = 0; cinfo->entropy->ct = 0; cinfo->entropy->last_val=0; cinfo->entropy->context=0; 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); } sscanf(argv[3],"%d",&cinfo->pre.near); #define UCH(x) ((int)(x)) #define GET_2B(array,offset) ((unsigned int) UCH(array[offset+1]) + \ (((unsigned int) UCH(array[offset])) << 8)) if (! ReadOK(cinfo->inputfile, fileheader, 6)){ printf("error when read the head of file"); exit(0); } cinfo->image_width = (UINT16) GET_2B(fileheader,2); cinfo->image_height = (UINT16) GET_2B(fileheader,4); cinfo->inbuffer=alloc_sarray((JDIMENSION) (4096), 1,cinfo); /*the inbuffer is a row of image elements now for* *one demintion prediction. */ src.next_input_byte=cinfo->inbuffer[0]; src.bytes_in_buffer=0; /*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*/ initial_decoder(cinfo); for (i=0;i<(cinfo->image_height);i++){ decode_row(cinfo,i); } /*kill all the buffer and output EOI marker*/ /*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 + -