📄 sfjcjpeg.c
字号:
/*sfjjpeg.c is reedited from IJG code by Fujian Shi(fieagle@yahoo.com.cn). *This file is used to finish the compression of a gray image *sfjcpeg.c ,quanhuff.c,jum.c,jcmarker.c ,jfdctint.c and jchuff.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 i){ int j,k,input_lines; JSAMPARRAY in_array=cinfo->inbuffer; input_lines=(i==(cinfo->block_height-1))?(8-cinfo->height_blank):8; for(j=0;j<input_lines;j++){ JFREAD(cinfo->inputfile,in_array[j],cinfo->image_width); for(k=0;k<cinfo->width_blank;k++) in_array[j][cinfo->image_width+k]=0; /*produce dump colume*/ } if (input_lines<8){ MEMZERO(in_array[input_lines],cinfo->image_width); /*produce dump line*/ input_lines++; }} /*the following function acomplish the DCT and entropy coding*/voidcompress_data(j_compress_ptr cinfo){ int i; DCTELEM * divisors=cinfo->divisors ; DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */ JDIMENSION bi; JDIMENSION start_col=0; JCOEF coef_blocks[DCTSIZE2]; /*dipose a row of block*/ for (bi = 0; bi < cinfo->block_width; bi++, start_col += DCTSIZE) { /* Load data into workspace, applying unsigned->signed conversion */ register DCTELEM *workspaceptr; register JSAMPROW elemptr; register int elemr; workspaceptr = workspace; for (elemr = 0; elemr < 8; elemr++) { elemptr = cinfo->inbuffer[elemr] + start_col; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE; } /* Perform the DCT */ jpeg_fdct_islow (workspace); /* Quantize/descale the coefficients */ { register DCTELEM temp, qval; register int i; register JCOEFPTR output_ptr = coef_blocks; for (i = 0; i < DCTSIZE2; i++) { qval = divisors[i]; temp = workspace[i]; /* Divide the coefficient value by qval, ensuring proper rounding. * Since C does not specify the direction of rounding for negative * quotients, we have to force the dividend positive for portability. * * In most files, at least half of the output values will be zero * (at default quantization settings, more like three-quarters...) * so we should ensure that this case is fast. On many machines, * a comparison is enough cheaper than a divide to make a special test * a win. Since both inputs will be nonnegative, we need only test * for a < b to discover whether a/b is 0. * If your machine's division is fast enough, define FAST_DIVIDE. */#ifdef FAST_DIVIDE#define DIVIDE_BY(a,b) a /= b#else#define DIVIDE_BY(a,b) if (a >= b) a /= b; else a = 0#endif if (temp < 0) { temp = -temp; temp += qval>>1; /* for rounding */ DIVIDE_BY(temp, qval); temp = -temp; } else { temp += qval>>1; /* for rounding */ DIVIDE_BY(temp, qval); } output_ptr[i] = (JCOEF) temp; } /*this marker is for the defination of variaty*/ encode_one_block(cinfo,output_ptr,cinfo->saved.last_dc_val, cinfo->d_dc_huff_tbl_ptr,cinfo->d_ac_huff_tbl_ptr); } } /*finish a row of block*/}main(int argc,char *argv[]){ j_compress_struct main_cinfo; int i,p,quality; char c; jpeg_destination_mgr main_dest; unsigned char fileheader[4]; /*the compact image data include only height and width*/ j_compress_ptr cinfo=&main_cinfo; cinfo->dest=&main_dest; cinfo->buffer_count=0; main_cinfo.outbuffer=(JOCTET *) alloc_one_row(cinfo,(size_t) (OUTPUT_BUF_SIZE)); main_dest.next_output_byte =cinfo->outbuffer; main_dest.free_in_buffer = OUTPUT_BUF_SIZE; cinfo->saved.put_buffer=cinfo->saved.put_bits=\ cinfo->saved.last_dc_val=0; 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->block_width=cinfo->image_width = (UINT16) GET_2B(fileheader,0); cinfo->block_height=cinfo->image_height = (UINT16) GET_2B(fileheader,2); cinfo->width_blank=cinfo->height_blank=0; while(cinfo->block_width & 7){ cinfo->block_width++; cinfo->width_blank++; } cinfo->block_width>>=3; while(cinfo->block_height & 7){ cinfo->block_height++; cinfo->height_blank++; } cinfo->block_height>>=3; cinfo->inbuffer=alloc_sarray((JDIMENSION) (8*cinfo->block_width), 8,cinfo); /*the inbuffer is a row of block*/ /*create the quality and huff table*/ if (sscanf(argv[3],"%d",&quality)!=1) exit(0); create_quality (cinfo,quality); cinfo->divisors = (DCTELEM *)\ alloc_one_row (cinfo,DCTSIZE2 * SIZEOF(DCTELEM)); for (i = 0; i < DCTSIZE2; i++) cinfo->divisors[i] = ((DCTELEM) cinfo->quant_tbl_ptrs[i]) << 3; /* mutitude by 8 is for couteract scale in DCT transform*/ create_huff (cinfo); /*write the proper file ,frame,scan and table header*/ write_file_header (cinfo); write_frame_header (cinfo); write_scan_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)); /*DCT transform and write to the JPEG file*/ for (i=0;i<(cinfo->block_height);i++){ input_image_data(cinfo,i); compress_data(cinfo); /*include DCT and entropy coding*/ } /*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 + -