home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Examples / c07 / src / jpdedu.cpp next >
Encoding:
C/C++ Source or Header  |  1998-06-25  |  2.9 KB  |  123 lines

  1. #include <math.h>
  2. #include "matrix.h"
  3. #include "jpdedu.h"
  4.  
  5. using namespace std ;
  6.  
  7. //
  8. //  Description:
  9. //
  10. //    The IDCT process can produce rounding errors that result in sample
  11. //    values being outside the legal range.  This function clamps
  12. //    sample value into the legal range.
  13. //
  14. //    Unclamped values give strange results when converted to bytes.
  15. //     -1 (0xFFFFFFFF) would be converted to 255 (0xFF) instead of 0 and
  16. //    256 (0x00000100) gets converted to 0.
  17. //
  18. //  Parameters:
  19. //    value: The value to clamp
  20. //
  21. //  Return Value:
  22. //    "value" clamped to MinSampleValue..MaxSampleValue
  23. //
  24.  
  25. static inline JPEGSAMPLE SampleRange (long value)
  26. {
  27.   if (value < JpegMinSampleValue)
  28.     return JpegMinSampleValue ;
  29.   else if (value > JpegMaxSampleValue)
  30.     return JpegMaxSampleValue ;
  31.   else
  32.     return (JPEGSAMPLE) value ;
  33. }
  34.  
  35. //
  36. //  Description:
  37. //
  38. //    This function descales a scaled integer value.
  39. //
  40. //    This implementation is simplay a shift operation. We
  41. //    use an inline function to give one place to change in case
  42. //    we want to switch to a rounded scale.
  43. //
  44. //  Parameters:
  45. //    value: The value to descale
  46. //    amount:  The amount to descale
  47. //
  48. //  Return Value:
  49. //    The descaled value.
  50. //
  51. static inline long Descale (long value, int amount)
  52. {
  53.   // A more precise value would be
  54.   // result = (value + (1 << (amount - 1))) >> amount ;
  55.   return value >> amount ;
  56. }
  57.  
  58. //
  59. //  Description:
  60. //
  61. //    Class Copy Constructor
  62. //
  63. JpegDecoderDataUnit::JpegDecoderDataUnit(const JpegDecoderDataUnit &du)
  64. {
  65.   memcpy (values, du.values, sizeof (values)) ;
  66.   return ;
  67. }
  68.  
  69. //
  70. //  Description:
  71. //
  72. //    Class assignment operator
  73. //
  74. JpegDecoderDataUnit &JpegDecoderDataUnit::operator=(const JpegDecoderDataUnit&du)
  75. {
  76.   memcpy (values, du.values, sizeof (values)) ;
  77.   return *this ;
  78. }
  79.  
  80. JpegDecoderDataUnit &JpegDecoderDataUnit::InverseDCT (
  81.                             const JpegDecoderCoefficientBlock data,
  82.                             const JpegDecoderQuantizationTable &qt)
  83. {
  84.   InitializeDctMatrix () ;
  85.  
  86.   MATRIX source, temp, destination ;
  87.   unsigned int ii, jj ;
  88.   for (ii = 0 ; ii < JpegSampleWidth ; ++ ii)
  89.   {
  90.     for (jj = 0 ; jj < JpegSampleWidth ; ++ jj)
  91.     {
  92.       source [ii][jj] = data [ii][jj]    
  93.                       * qt [JpegZigZagOutputOrder (ii * JpegSampleWidth + jj)]  ;
  94.     }
  95.   }
  96.   Multiply (IDctMatrix, source, temp) ;
  97.   Multiply (temp, DctMatrix, destination) ;
  98.   for (ii = 0 ; ii < JpegSampleWidth ; ++ ii)
  99.   {
  100.     for (jj = 0 ; jj < JpegSampleWidth ; ++ jj)
  101.     {
  102.       values [ii][jj] = SampleRange (destination [ii][jj] + 0.5 + JpegMidpointSampleValue) ;
  103.     }
  104.   }
  105.   return *this ;
  106. }
  107.  
  108. void JpegDecoderDataUnit::Print (std::ostream &strm) const
  109. {
  110.   for (int ii = 0 ; ii < JpegSampleWidth ; ++ ii)
  111.   {
  112.     for (int jj = 0 ; jj < JpegSampleWidth ; ++ jj)
  113.     {
  114.       strm << (int) values [ii][jj] << " " ;
  115.     }
  116.     strm << endl ;
  117.   }
  118.   return ;
  119. }
  120.  
  121.  
  122.  
  123.