⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 355-370.html

📁 The primary purpose of this book is to explain various data-compression techniques using the C progr
💻 HTML
📖 第 1 页 / 共 2 页
字号:
*         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 &gt; 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 &lt;&lt; 2 ) - 5; result = (int) InputBits( input_file, bit_count ); if ( result &amp; ( 1 &lt;&lt; ( bit_count - 1 ) ) )  return( result ); return( result - ( 1 &lt;&lt; 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 &lt; ( 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 &gt; 0 ) {  if ( OutputRunLength &lt;= 16 ) {    OutputBits( output_file,         (unsigned long) (OutputRunLength - 1 ), 4 );    OutputRunLength = 0;  } else {   OutputBits( output_file, 15L, 4 );   OutputRunLength -= 16;  } }}if ( code &lt; 0 ) abs_code = -code;else abs_code = code;top_of_range = 1;bit_count = 1;while ( abs_code &gt; top_of_range ) { bit_count++; top_of_range = ( ( top_of_range + 1 ) * 2 ) - 1; } if ( bit_count &lt; 3 )   OutputBits( output_file, (unsigned long) ( bit_count + 1 ), 3 ); else   OutputBits( output_file, (unsigned long) ( bit_count + 5 ), 4 ); if (code &gt; 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 &lt; ( 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 &lt; N ; row++ )  for ( col = 0 ; col &lt; 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 &lt; N ; i++ ) {  for ( j = 0 ; j &lt; N ; j++ ) {   temp[ i ][ j ] = 0.0;   for ( k = 0 ; k &lt; N ; k++ )    temp[ i ][ j ] += ( (int) input [ i ][ k ] - 128 ) *               Ct[ k ][ j ];  } }/* MatrixMultiply( output, C, temp ); */ for ( i = 0 ; i &lt; N ; i++ ) {  for ( j = 0 ; j &lt; N ; j++ ) {   temp1 = 0.0;   for ( k = 0 ; k &lt; 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 &lt; N ; i++ ) {  for ( j = 0 ; j &lt; N ; j++ ) {   temp[ i ][ j ] = 0.0;   for ( k = 0 ; k &lt; N ; k++ )    temp[ i ][ j ] += input[ i ][ k ] * C[ k ][ j ];  } }/* MatrixMultiply( output, Ct, temp ); */ for ( i = 0 ; i &lt; N ; i++ ) {  for ( j = 0 ; j &lt; N ; j++ ) {   temp1 = 0.0;   for ( k = 0 ; k &lt; N ; k++ )    temp1 += Ct[ i ][ k ] * temp[ k ][ j ];   temp1 += 128.0;   if ( temp1 &lt; 0 )    output[ i ][ j ] = 0;   else if ( temp1 &gt; 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-- &gt; 0 )  quality = atoi( argv[ 0 ] ); else  quality = 3; if ( quality &lt; 0 || quality &gt; 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 &lt; ROWS ; row += N ) {  ReadPixelStrip( input, PixelStrip );  for ( col = 0 ; col &lt; COLS ; col += N ) {    for ( i = 0 ; i &lt; N ; i++ )     input_array[ i ] = PixelStrip[ i ] + col;    ForwardDCT( input_array, output_array );    WriteDCTData( output, output_array );   } } OutputCode( output, 1); while ( argc-- &gt; 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 &lt; ROWS ; row += N ) {  for ( col = 0 ; col &lt; COLS ; col += N ) {   for ( i = 0 ; i &lt; N ; i++ )    output_array[ i ] = PixelStrip[ i ] + col;   ReadDCTData( input, input_array );   InverseDCT( input_array, output_array );  }  WritePixelStrip( output, PixelStrip ); } while ( argc-- &gt; 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 + -