home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Examples / c10 / src / jpdequan.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-17  |  5.7 KB  |  172 lines

  1. //
  2. // Copyright (c) 1997,1998 Colosseum Builders, Inc.
  3. // All rights reserved.
  4. //
  5. // Colosseum Builders, Inc. makes no warranty, expressed or implied
  6. // with regards to this software. It is provided as is.
  7. //
  8. // See the README.TXT file that came with this software for restrictions
  9. // on the use and redistribution of this file or send E-mail to
  10. // info@colosseumbuilders.com
  11. //
  12.  
  13. //
  14. // JPEG Decoder Quantization Table Class Implementation
  15. //
  16. // Author:  John M. Miano  miano@colosseumbuilders.com
  17. //
  18.  
  19. #include <iostream>
  20. #include "jpdequan.h"
  21. #include "jpegdeco.h"
  22. #include "jpgexcep.h"
  23.  
  24. using namespace std ;
  25.  
  26. #pragma comment (exestr, "Copyright 1997 Colosseum Builders Inc.")
  27.  
  28. //
  29. // This table consists of the values
  30. //
  31. //   F (i, j) = X (i) X (j) / 8 
  32. // 
  33. // where
  34. // 
  35. //  X (n) = 1, n = 0, 4
  36. //  X (n) = 1 / sqrt(2) / cos (n*PI/16) 
  37. //  
  38.  
  39. static const double floatscaling [JpegSampleWidth][JpegSampleWidth] = 
  40. {
  41. { 0.125,                  0.09011997775086849627, 0.09567085809127244544, 0.1063037618459070632,  0.125,                  0.159094822571604233,  0.2309698831278216846, 0.4530637231764438333, },
  42. { 0.09011997775086849627, 0.0649728831185362593,  0.0689748448207357645,  0.07664074121909414394, 0.09011997775086849627, 0.1147009749634507608, 0.1665200058287998886, 0.3266407412190940884, },
  43. { 0.09567085809127244544, 0.0689748448207357645,  0.0732233047033631207,  0.08136137691302557096, 0.09567085809127244544, 0.1217659055464329343, 0.1767766952966368932, 0.3467599613305368256, },
  44. { 0.1063037618459070632,  0.07664074121909414394, 0.08136137691302557096, 0.09040391826073060355, 0.1063037618459070632,  0.135299025036549253,  0.1964237395967755595, 0.3852990250365491698, },
  45. { 0.125,                  0.09011997775086849627, 0.09567085809127244544, 0.1063037618459070632,  0.125,                  0.159094822571604233,  0.2309698831278216846, 0.4530637231764438333, },
  46. { 0.159094822571604233,   0.1147009749634507608,  0.1217659055464329343,  0.135299025036549253,   0.159094822571604233,   0.2024893005527218515, 0.2939689006048396558, 0.5766407412190940329, },
  47. { 0.2309698831278216846,  0.1665200058287998886,  0.1767766952966368932,  0.1964237395967755595,  0.2309698831278216846,  0.2939689006048396558, 0.4267766952966368654, 0.8371526015321518744, },
  48. { 0.4530637231764438333,  0.3266407412190940884,  0.3467599613305368256,  0.3852990250365491698,  0.4530637231764438333,  0.5766407412190940329, 0.8371526015321518744, 1.642133898068010689,  },
  49. } ;
  50.  
  51. //
  52. //  Description:
  53. //
  54. //    Class Default Constructor
  55. //
  56. JpegDecoderQuantizationTable::JpegDecoderQuantizationTable ()
  57. {
  58.   table_defined = false ;
  59.   memset (data_values, 0, sizeof (data_values)) ;
  60.   return ;
  61. }
  62.  
  63. //
  64. //  Description:
  65. //
  66. //    This function reads a quantization table from a JPEG stream.
  67. //
  68. //  Parameters:
  69. //    decoder:  The JPEG decoder that owns the table and the JPEG stream.
  70. //    precision: The quantization table precision
  71. //
  72. void JpegDecoderQuantizationTable::ReadTable (JpegDecoder &decoder,
  73.                                               unsigned int precision)
  74. {
  75.   // B 2.4.1
  76.   // Determine if 16-bit or 8-bit precision is used for the quantization
  77.   // values in the file.
  78.   if (precision == 1)
  79.   {
  80. // Our source code only allows 8-bit data. The standard says
  81. // 16-bit quantization tables are not allowed with 8-bit data.
  82. // The commented code shows how 16-bit tables would be implemented.
  83. //
  84. //    // Read 16-bit values.
  85. //    for (unsigned int ii = 0 ; ii < SampleSize ; ++ ii)
  86. //    {
  87. //      data_values[ii] = decoder.ReadWord () ;
  88. //      if (data_values[ii] == 0)
  89. //        throw EJpegBadData ("Zero value in quantization table") ;
  90. //    }
  91.     throw EJpegBadData ("Only 8-bit Data is Supported") ;
  92.   }
  93.   else if (precision == 0)
  94.   {
  95.     // Read 8-bit values.
  96.     for (unsigned int ii = 0 ; ii < JpegSampleSize ; ++ ii)
  97.     {
  98.       data_values[ii] = decoder.ReadByte () ;
  99.       if (data_values[ii] == 0)
  100.         throw EJpegBadData ("Zero value in quantization table") ;
  101.     }
  102.   }
  103.   else
  104.   {
  105.     throw EJpegBadData ("Invalid Quantization Table Precision") ;
  106.   }
  107.  
  108.   BuildScaledTables () ;
  109.  
  110.   table_defined = true ;
  111.   return ;
  112. }
  113.  
  114. //
  115. //  Description:
  116. //
  117. //    This function creates scaled quantization tables that
  118. //    allow quantization to be merged with the IDCT process.
  119. //    We factor the DCT matrix so that the first step in the
  120. //    IDCT is to multiply each value by a constant. Here we
  121. //    merge that constant with the quantization table valus.
  122. //
  123. void JpegDecoderQuantizationTable::BuildScaledTables ()
  124. {
  125.   unsigned int ii ;  // Overcome MSVC++ Wierdness
  126.  
  127.   for (ii = 0 ; ii < JpegSampleWidth ; ++ ii)
  128.   {
  129.     for (int jj = 0 ; jj < JpegSampleWidth ; ++ jj)
  130.     {
  131.       float_scaling [ii][jj] = data_values [JpegZigZagOutputOrder (ii * 8 + jj)]
  132.                              * floatscaling [ii][jj] ;
  133.     }
  134.   }
  135.  
  136.   for (ii = 0 ; ii < JpegSampleWidth ; ++ ii)
  137.   {
  138.     for (int jj = 0 ; jj < JpegSampleWidth ; ++ jj)
  139.     {
  140.       integer_scaling [ii][jj] = (long) ((1 << QuantizationIntegerScale)
  141.                                * floatscaling [ii][jj]
  142.                                * data_values  [JpegZigZagOutputOrder (ii * 8 + jj)]) ;
  143.     }
  144.   }
  145.   return ;
  146. }
  147.  
  148. //
  149. //  Description:
  150. //
  151. //    This is a debugging function that prints the contents
  152. //    of the quantization table to a stream.
  153. //
  154. //  Parameters:
  155. //
  156. //    strm:  The output stream
  157. //
  158. void JpegDecoderQuantizationTable::Print (std::ostream &strm) const
  159. {
  160.   for (unsigned int ii = 0 ; ii < JpegSampleWidth ; ++ ii)
  161.   {
  162.     strm << endl << "        "  ;
  163.     for (unsigned int jj = 0 ; jj < JpegSampleWidth ; ++ jj)
  164.     {
  165.       strm << dec
  166.            << data_values [ii * JpegSampleWidth + jj]
  167.            << " " ;
  168.     }
  169.   }
  170.   return ;
  171. }
  172.