📄 355-370.html
字号:
* 1 -1, 1* 2 3 to -2, 2 to 3* 3 -7 to -4, 4 to 7* 4 -15 to -8, 8 to 15* 5 -31 to -16, 16 to 31* 6 -63 to -32, 32 to 64* 7 -127 to -64, 64 to 127* 8 -255 to -128, 128 to 255* 9 -511 to -256, 256 to 511* 10 -1023 to -512, 512 to 1023**/int InputCode( input_file )BIT_FILE *input_file;{ int bit_count; int result; if ( InputRunLength > 0 ) { InputRunLength--; return( 0 ); } bit_count = (int) InputBits( input_file, 2 ); if ( bit_count == 0 ) { InputRunLength = (int) InputBits( input_file, 4); return( 0 ); } if ( bit_count == 1 ) bit_count = (int) InputBits( input_file, 1 ) + 1; else bit_count = (int) InputBits( input_file, 2 ) + ( bit_count << 2 ) - 5; result = (int) InputBits( input_file, bit_count ); if ( result & ( 1 << ( bit_count - 1 ) ) ) return( result ); return( result - ( 1 << bit_count ) + 1 );}/** This routine reads in a block of encoded DCT data from a compressed* file. The routine reorders it in row major format and dequantizes it* using the quantization matrix.*/void ReadDCTData( input_file, input_data )BIT_FILE *input_file;int input_data[ N ][ N ];{ int i; int row; int col; for ( i = 0 ; i < ( N * N ) ; i++ ) { row = ZigZag[ i ].row; col = ZigZag[ i ].col; input_data[ row ][ col ] = InputCode( input_file ) * Quantum[ row ][ col ]; }}/** This routine outputs a code to the compressed DCT file. For specs* on the exact format, see the comments that go with InputCode, shown* earlier in this file.*/void OutputCode( output_file, code ) BIT_FILE *output_file;int code;{ int top_of_range; int abs_code; int bit_count; if ( code == 0 ) { OutputRunLength++; return; } if ( OutputRunLength != 0 ) { while ( OutputRunLength > 0 ) { if ( OutputRunLength <= 16 ) { OutputBits( output_file, (unsigned long) (OutputRunLength - 1 ), 4 ); OutputRunLength = 0; } else { OutputBits( output_file, 15L, 4 ); OutputRunLength -= 16; } }}if ( code < 0 ) abs_code = -code;else abs_code = code;top_of_range = 1;bit_count = 1;while ( abs_code > top_of_range ) { bit_count++; top_of_range = ( ( top_of_range + 1 ) * 2 ) - 1; } if ( bit_count < 3 ) OutputBits( output_file, (unsigned long) ( bit_count + 1 ), 3 ); else OutputBits( output_file, (unsigned long) ( bit_count + 5 ), 4 ); if (code > 0 ) OutputBits( output_file, (unsigned long) code, bit_count ); else OutputBits( output_file, (unsigned long) ( code + top_of_range ) , bit_count );}/** This routine takes DCT data, puts it in zigzag order, quantizes* it, and outputs the code.*/void WriteDCTData( output_file, output_data )BIT_FILE *output_file;int output_data[ N ][ N ];{ int i; int row; int col; double result; for ( i = 0 ; i < ( N * N ) ; i++ ) { row = ZigZag[ i ].row; col = ZigZag[ i ].col; result = output_data[ row ][ col ] / Quantum[ row ][ col ]; OutputCode( output_file, ROUND( result ) ): }}/** This routine writes out a strip of pixel data to a GS format file.*/void WritePixelStrip( output, strip )FILE *output;unsigned char strip[ N ][ COLS ];{ int row; int col; for ( row = 0 ; row < N ; row++ ) for ( col = 0 ; col < COLS ; col++ ) putc( strip[ row ][ col ], output ); }/** The Forward DCT routine implements the matrix function:** DCT = C * pixels * Ct*/void ForwardDCT( input, output )unsigned char *input[ N ];int output[ N ][ N ];{ double temp[ N ][ N ]; double temp1; int i; int j; int k;/* MatrixMultiply( temp, input, Ct ); */ for ( i = 0 ; i < N ; i++ ) { for ( j = 0 ; j < N ; j++ ) { temp[ i ][ j ] = 0.0; for ( k = 0 ; k < N ; k++ ) temp[ i ][ j ] += ( (int) input [ i ][ k ] - 128 ) * Ct[ k ][ j ]; } }/* MatrixMultiply( output, C, temp ); */ for ( i = 0 ; i < N ; i++ ) { for ( j = 0 ; j < N ; j++ ) { temp1 = 0.0; for ( k = 0 ; k < N ; k++ ) temp1 += C[ i ][ k ] * temp[ k ][ j ]; output[ i ][ j ] = ROUND( temp1 ); } }}/** The Inverse DCT routine implements the matrix function:** pixels = C * DCT * Ct*/void InverseDCT( input, output )int input[ N ][ N ];unsigned char *output[ N ];{ double temp[ N ][ N ]; double temp1; int i; int j; int k;/* MatrixMultiply( temp, input, C ); */ for ( i = 0 ; i < N ; i++ ) { for ( j = 0 ; j < N ; j++ ) { temp[ i ][ j ] = 0.0; for ( k = 0 ; k < N ; k++ ) temp[ i ][ j ] += input[ i ][ k ] * C[ k ][ j ]; } }/* MatrixMultiply( output, Ct, temp ); */ for ( i = 0 ; i < N ; i++ ) { for ( j = 0 ; j < N ; j++ ) { temp1 = 0.0; for ( k = 0 ; k < N ; k++ ) temp1 += Ct[ i ][ k ] * temp[ k ][ j ]; temp1 += 128.0; if ( temp1 < 0 ) output[ i ][ j ] = 0; else if ( temp1 > 255 ) output[ i ][ j ] = 255; else output[ i ][ j ] = (unsigned char) ROUND( temp1 ); } }}/** This is the main compression routine. By the time it gets called,* the input and output files have been properly opened, so all it has to* do is the compression. Note that the compression routine expects an* additional parameter, the quality value, ranging from 0 to 25.*/void CompressFile( input, output, argc, argv )FILE *input;BIT_FILE *output;int argc;char *argv[];{ int row; int col; int i; unsigned char *input_array[ N ]; int output_array[ N ][ N ]; int quality; if ( argc-- > 0 ) quality = atoi( argv[ 0 ] ); else quality = 3; if ( quality < 0 || quality > 50 ) fatal_error( "Illegal quality factor of %d\n", quality ); printf( "Using quality factor of %d\n", quality ); Initialize( quality ); OutputBits( output, (unsigned long) quality, 8 ); for ( row = 0 ; row < ROWS ; row += N ) { ReadPixelStrip( input, PixelStrip ); for ( col = 0 ; col < COLS ; col += N ) { for ( i = 0 ; i < N ; i++ ) input_array[ i ] = PixelStrip[ i ] + col; ForwardDCT( input_array, output_array ); WriteDCTData( output, output_array ); } } OutputCode( output, 1); while ( argc-- > 0 ) printf( "Unused argument: %s\n", *argv++ );}/* The expansion routine reads in the compressed data from the DCT file,* then writes out the decompressed grey-scale file.*/void ExpandFile( input, output, argc, argv)BIT_FILE *input;FILE *output;int argc;char *argv[];{ int row; int col; int i; int input_array[ N ][ N ]; unsigned char *output_array[ N ]; int quality; quality = (int) InputBits( input, 8 ); printf( "\rUsing quality factor of %d\n", quality ); Initialized( quality ); for ( row = 0 ; row < ROWS ; row += N ) { for ( col = 0 ; col < COLS ; col += N ) { for ( i = 0 ; i < N ; i++ ) output_array[ i ] = PixelStrip[ i ] + col; ReadDCTData( input, input_array ); InverseDCT( input_array, output_array ); } WritePixelStrip( output, PixelStrip ); } while ( argc-- > 0 ) printf( "Unused argument: %s\n", *argv++ );}/**************************** End of DCT.C *****************************/</PRE><!-- END CODE //--><P><BR></P><CENTER><TABLE BORDER><TR><TD><A HREF="351-355.html">Previous</A></TD><TD><A HREF="../ewtoc.html">Table of Contents</A></TD><TD><A HREF="370-375.html">Next</A></TD></TR></TABLE></CENTER></TD></TR></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -