home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Examples / c07 / src / jpdequan.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-23  |  2.3 KB  |  93 lines

  1. #include <iostream>
  2. #include "jpdequan.h"
  3. #include "jpegdeco.h"
  4. #include "jpgexcep.h"
  5.  
  6. using namespace std ;
  7.  
  8. //
  9. //  Description:
  10. //
  11. //    Class Default Constructor
  12. //
  13. JpegDecoderQuantizationTable::JpegDecoderQuantizationTable ()
  14. {
  15.   table_defined = false ;
  16.   memset (data_values, 0, sizeof (data_values)) ;
  17.   return ;
  18. }
  19.  
  20. //
  21. //  Description:
  22. //
  23. //    This function reads a quantization table from a JPEG stream.
  24. //
  25. //  Parameters:
  26. //    decoder:  The JPEG decoder that owns the table and the JPEG stream.
  27. //    precision: The quantization table precision
  28. //
  29. void JpegDecoderQuantizationTable::ReadTable (JpegDecoder &decoder,
  30.                                               unsigned int precision)
  31. {
  32.   // B 2.4.1
  33.   // Determine if 16-bit or 8-bit precision is used for the quantization
  34.   // values in the file.
  35.   if (precision == 1)
  36.   {
  37. // Our source code only allows 8-bit data. The standard says
  38. // 16-bit quantization tables are not allowed with 8-bit data.
  39. // The commented code shows how 16-bit tables would be implemented.
  40. //
  41. //    // Read 16-bit values.
  42. //    for (unsigned int ii = 0 ; ii < SampleSize ; ++ ii)
  43. //    {
  44. //      data_values[ii] = decoder.ReadWord () ;
  45. //      if (data_values[ii] == 0)
  46. //        throw EJpegBadData ("Zero value in quantization table") ;
  47. //    }
  48.     throw EJpegBadData ("Only 8-bit Data is Supported") ;
  49.   }
  50.   else if (precision == 0)
  51.   {
  52.     // Read 8-bit values.
  53.     for (unsigned int ii = 0 ; ii < JpegSampleSize ; ++ ii)
  54.     {
  55.       data_values[ii] = decoder.ReadByte () ;
  56.       if (data_values[ii] == 0)
  57.         throw EJpegBadData ("Zero value in quantization table") ;
  58.     }
  59.   }
  60.   else
  61.   {
  62.     throw EJpegBadData ("Invalid Quantization Table Precision") ;
  63.   }
  64.  
  65.   table_defined = true ;
  66.   return ;
  67. }
  68.  
  69. //
  70. //  Description:
  71. //
  72. //    This is a debugging function that prints the contents
  73. //    of the quantization table to a stream.
  74. //
  75. //  Parameters:
  76. //
  77. //    strm:  The output stream
  78. //
  79. void JpegDecoderQuantizationTable::Print (std::ostream &strm) const
  80. {
  81.   for (unsigned int ii = 0 ; ii < JpegSampleWidth ; ++ ii)
  82.   {
  83.     strm << endl << "        "  ;
  84.     for (unsigned int jj = 0 ; jj < JpegSampleWidth ; ++ jj)
  85.     {
  86.       strm << dec
  87.            << data_values [ii * JpegSampleWidth + jj]
  88.            << " " ;
  89.     }
  90.   }
  91.   return ;
  92. }
  93.