home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Examples / c08 / src / jpdecomp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-23  |  14.8 KB  |  516 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 Component Class Implementation
  15. //
  16. // Author:  John M. Miano  miano@colosseumbuilders.com
  17. //
  18.  
  19.  
  20. #include "jpdecomp.h"
  21. #include "jpgexcep.h"
  22. #include "jpdequan.h"
  23. #include "jpegdeco.h"
  24. #include "jpdecobk.h"
  25. #include "jpeg.h"
  26. #include "bitimage.h"
  27.  
  28.  
  29. //
  30. //  Description:
  31. //
  32. //    SequentialOnly
  33. //
  34. //    This function extends the sign bit of a decoded value.
  35. //
  36. //  Parameters:
  37. //    vv: The bit value
  38. //    tt: The length of the bit value
  39. //
  40.  
  41. static inline int Extend (int vv, int tt)
  42. {
  43.   // Extend function defined in Section F.2.2.1 Figure F.12
  44.    // The tt'th bit of vv is the sign bit. One is for
  45.    // positive values and zero is for negative values.
  46.    int vt = 1 << (tt - 1) ;
  47.    if (vv < vt)
  48.    {
  49.       vt = (-1 << tt) + 1 ;
  50.       return vv + vt ;
  51.    }
  52.    else
  53.    {
  54.       return vv ;
  55.    }
  56. }
  57.  
  58. //
  59. //  Description:
  60. //
  61. //    Class default constructor
  62. //
  63. JpegDecoderComponent::JpegDecoderComponent ()
  64. {
  65.   component_id = 0 ;
  66.   horizontal_frequency = 0 ;
  67.   vertical_frequency = 0 ;
  68.   v_sampling = 0 ;
  69.   h_sampling = 0 ;
  70.   last_dc_value = 0 ;
  71.   ac_table = NULL ;
  72.   dc_table = NULL ;
  73.   quantization_table = NULL ;
  74.   noninterleaved_rows = 0 ;
  75.   noninterleaved_cols = 0 ;
  76.   data_units = NULL ;
  77.   upsample_data = NULL ;
  78.   return ;
  79. }
  80.  
  81. //
  82. //  Description:
  83. //
  84. //    Class Destructor
  85. //
  86. JpegDecoderComponent::~JpegDecoderComponent ()
  87. {
  88.   delete [] data_units ; data_units = NULL ;
  89.   delete [] upsample_data ; upsample_data = NULL ;
  90.   return ;
  91. }
  92.  
  93. //
  94. //  Description:
  95. //
  96. //    This function sets the horizontal sampling frequency
  97. //    for the component.
  98. //
  99. //  Parameters:
  100. //    value: The sampling factor (1..4)
  101. //
  102. void JpegDecoderComponent::HorizontalFrequency (unsigned int value)
  103. {
  104.   if (value < JpegMinSamplingFrequency || value > JpegMaxSamplingFrequency)
  105.     throw EJpegValueOutOfRange () ;
  106.  
  107.   horizontal_frequency = value ;
  108.   return ;
  109. }
  110.  
  111. //
  112. //  Description:
  113. //
  114. //    This function sets the vertical sampling frequency
  115. //    for the component.
  116. //
  117. //  Parameters:
  118. //    value: The sampling factor (1..4)
  119. //
  120. void JpegDecoderComponent::VerticalFrequency (unsigned int value)
  121. {
  122.   if (value < JpegMinSamplingFrequency || value > JpegMaxSamplingFrequency)
  123.     throw EJpegValueOutOfRange () ;
  124.  
  125.   vertical_frequency = value ;
  126.  
  127.   return ;
  128. }
  129.  
  130. //
  131. //  Description:
  132. //
  133. //    This function associates a quantization table with the component.
  134. //
  135. //  Parameters:
  136. //    table:  The quantization table
  137. //
  138. void JpegDecoderComponent::SetQuantizationTable (
  139.                                      JpegDecoderQuantizationTable &table)
  140. {
  141.   quantization_table = &table ;
  142.   return ;
  143. }
  144.  
  145. //
  146. //  Description:
  147. //
  148. //    This function determines the dimensions for the component and allocates
  149. //    the storage to hold the component's data.
  150. //
  151. //  Parameters:
  152. //    decoder:  The jpeg decoder this component belongs to.
  153. //
  154. void JpegDecoderComponent::AllocateComponentBuffers (
  155.                                const JpegDecoder &decoder)
  156. {
  157.   if (data_units == NULL)
  158.   {
  159.     // Determine sampling for the component. This is the amount of
  160.     // stretching needed for the component.
  161.     v_sampling = decoder.MaxVFrequency () / vertical_frequency ;
  162.     h_sampling = decoder.MaxHFrequency () / horizontal_frequency ;
  163.  
  164.     // Determine the component's dimensions in a non-interleaved scan.
  165.     noninterleaved_rows = (decoder.FrameHeight ()
  166.                            + v_sampling * JpegSampleWidth - 1)
  167.                           / (v_sampling * JpegSampleWidth) ;
  168.     noninterleaved_cols = (decoder.FrameWidth ()
  169.                            + h_sampling * JpegSampleWidth - 1)
  170.                           / (h_sampling * JpegSampleWidth) ;
  171.  
  172.     du_rows = decoder.McuRows () * vertical_frequency ;
  173.     du_cols = decoder.McuCols () * horizontal_frequency ;
  174.  
  175.     data_units = new JpegDecoderDataUnit [du_rows * du_cols] ;
  176.   }
  177.  
  178.   return ;
  179. }
  180.  
  181. //
  182. //  Description:
  183. //
  184. //    This function frees the memory allocated by the component
  185. //    during the decompression process.
  186. //
  187. void JpegDecoderComponent::FreeComponentBuffers ()
  188. {
  189.   delete [] data_units ; data_units = NULL ;
  190.   delete [] upsample_data ; upsample_data = NULL ;
  191.   return ;
  192. }
  193.  
  194. //
  195. //  Description:
  196. //
  197. //    This function asigned Huffman tables to the component.
  198. //
  199. //  Parameters:
  200. //    dc:  The DC Huffman table
  201. //    ac:  The AC Huffman table
  202. //
  203. void JpegDecoderComponent::SetHuffmanTables (JpegHuffmanDecoder &dc,
  204.                                              JpegHuffmanDecoder &ac)
  205. {
  206.   dc_table = &dc ;
  207.   ac_table = &ac ;
  208.   return ;
  209. }
  210.  
  211. //
  212. //  Description:
  213. //
  214. //    This function ensures that this component has a defined
  215. //    AC table assigned to it. If not, it throws an exception.
  216. //
  217. void JpegDecoderComponent::CheckAcTable ()
  218. {
  219.   // If this occurs then we have a programming error.
  220.   if (ac_table == NULL)
  221.     throw EJpegFatal ("INTERNAL ERROR - AC Table Not Assigned") ;
  222.  
  223.   if (! ac_table->Defined ())
  224.     throw EJpegError ("AC Table Not Defined") ;
  225.  
  226.   return ;
  227. }
  228.  
  229. //
  230. //  Sequential and Progressive
  231. //
  232. //  This function is called before processing a scan. It ensures that the
  233. //  DC Huffman table used by the component has actually been defined.
  234. //
  235. void JpegDecoderComponent::CheckDcTable ()
  236. {
  237.   if (dc_table == NULL)
  238.     throw EJpegFatal ("INTERNAL ERROR - DC Table Not Assigned") ;
  239.  
  240.   // This condition could be caused by a corrupt JPEG stream.
  241.   if (! dc_table->Defined ())
  242.     throw EJpegFatal ("INTERNAL ERROR - DC Table Not Defined") ;
  243.  
  244.   return ;
  245. }
  246.  
  247. //
  248. //  Description:
  249. //
  250. //    Sequential and Progressive
  251. //
  252. //    This function is called before processing a scan. It ensures that the
  253. //    Quantization table used by the component has actually been defined.
  254. //
  255. void JpegDecoderComponent::CheckQuantizationTable ()
  256. {
  257.   if (quantization_table == NULL)
  258.     throw EJpegFatal ("INTERNAL ERROR - Quantization Table Not Assigned") ;
  259.  
  260.   if (! quantization_table->Defined ())
  261.     throw EJpegError ("Quantization Table Not Defined") ;
  262.  
  263.   return ;
  264. }
  265.  
  266. //
  267. //  Description:
  268. //
  269. //    This function decodes a data unit in a sequential scan.
  270. //
  271. //  Parameters:
  272. //    decoder: The decoder that owns this component
  273. //    mcurow, mcucol:  The row and column for this data unit.
  274. //
  275. void JpegDecoderComponent::DecodeSequential (JpegDecoder &decoder,
  276.                                              unsigned int mcurow,
  277.                                              unsigned int mcucol)
  278. {
  279.   JpegDecoderCoefficientBlock data ;
  280.   memset (&data, 0, sizeof (data)) ;
  281.  
  282.   // Decode the DC differce value.
  283.   // Section F.2.2.1
  284.   unsigned int count ; // called T in F.2.2.1
  285.   count = dc_table->Decode (decoder) ;
  286.   int bits = decoder.Receive (count) ;
  287.   int diff = Extend (bits, count) ;
  288.  
  289.   // Create the DC value from the difference and the previous DC value.
  290.   int dc = diff + last_dc_value ;
  291.   last_dc_value = dc ;
  292.   data [0][0] = dc ;
  293.  
  294.   // Decode the AC coefficients.
  295.   // Section F.2.2.2 Figure F.13
  296.   for (unsigned int kk = 1 ; kk < JpegSampleSize ; ++ kk)
  297.   {
  298.     UBYTE2 rs = ac_table->Decode (decoder) ;
  299.     UBYTE2 ssss = (UBYTE2) (rs & 0xF) ;
  300.     UBYTE2 rrrr = (UBYTE2) (rs >> 0x4) ;
  301.  
  302.     if (ssss == 0)
  303.     {
  304.       // ssss is zero then rrrr should either be 15 or zero according to
  305.       // Figure F.1. 0 means that the rest of the coefficients are zero
  306.       // while 15 means the next 16 coefficients are zero. We are not checking
  307.       // for other values because Figure F.13 shows values other than 15
  308.       // as being treated as zero.
  309.       if (rrrr  != 15)
  310.         break ;
  311.       kk += 15 ; // Actually 16 since one more gets added by the loop.
  312.     }
  313.     else
  314.     {
  315.       // If ssss is non-zero then rrrr gives the number of zero coefficients
  316.       // to skip.
  317.  
  318.       kk += rrrr ;
  319.       if (kk >= JpegSampleSize)
  320.         throw EJpegFatal ("Value out of range") ;
  321.  
  322.       // Receive and extend the additional bits.
  323.       // Section F2.2.2 Figure F.14
  324.       int bits = decoder.Receive (ssss) ;
  325.       int value = Extend (bits, ssss) ;
  326.       (&data [0][0])[JpegZigZagInputOrder (kk)] = value ;
  327.     }
  328.   }
  329.   data_units [mcurow * du_cols + mcucol].InverseDCT (data,
  330.                                                      *quantization_table) ;
  331.   return ;
  332. }
  333.  
  334. //
  335. //  Description:
  336. //
  337. //    This function upsamples the data for the component. Here we take
  338. //    the values from the data_units array and copy it to the
  339. //    upsample_data. If the horizontal or vertical sampling frequencies
  340. //    are less than the maximum for the image then we need to
  341. //    stretch the data during the copy.
  342. //
  343. void JpegDecoderComponent::Upsample ()
  344. {
  345.   unsigned int imagesize = du_rows * v_sampling * du_cols
  346.                               * h_sampling * JpegSampleSize ;
  347.   if (imagesize == 0)
  348.     return ;  // No data for this component yet.
  349.  
  350.   if (upsample_data == NULL)
  351.     upsample_data = new JPEGSAMPLE [imagesize] ;
  352.  
  353.   // Simple case where component does not need to be upsampled.
  354.   if (v_sampling == 1 && h_sampling == 1)
  355.   {
  356.     unsigned output = 0 ;
  357.     unsigned int startdu = 0 ;
  358.     for (unsigned int durow = 0 ; durow < du_rows ; ++ durow)
  359.     {
  360.       for (unsigned int ii = 0 ; ii < JpegSampleWidth ; ++ ii)
  361.       {
  362.          unsigned int du = startdu ;
  363.           for (unsigned int ducol = 0 ; ducol < du_cols ; ++ ducol)
  364.         {
  365.             upsample_data [output] = data_units [du][ii][0] ; ++ output ;
  366.             upsample_data [output] = data_units [du][ii][1] ; ++ output ;
  367.             upsample_data [output] = data_units [du][ii][2] ; ++ output ;
  368.             upsample_data [output] = data_units [du][ii][3] ; ++ output ;
  369.             upsample_data [output] = data_units [du][ii][4] ; ++ output ;
  370.             upsample_data [output] = data_units [du][ii][5] ; ++ output ;
  371.             upsample_data [output] = data_units [du][ii][6] ; ++ output ;
  372.             upsample_data [output] = data_units [du][ii][7] ; ++ output ;
  373.           ++ du ;
  374.           }
  375.       }
  376.       startdu += du_cols ;
  377.     }
  378.   }
  379.   else
  380.   {
  381.     unsigned output = 0 ;
  382.     unsigned int startdu = 0 ;
  383.     for (unsigned int durow = 0 ; durow < du_rows ; ++ durow)
  384.     {
  385.       for (unsigned int ii = 0 ; ii < JpegSampleWidth ; ++ ii)
  386.       {
  387.          for (unsigned int vv = 0 ; vv < v_sampling ; ++ vv)
  388.          {
  389.            unsigned int du = startdu ;
  390.               for (unsigned int ducol = 0 ; ducol < du_cols ; ++ ducol)
  391.            {
  392.              unsigned int jj ;
  393.              for (jj = 0 ; jj < h_sampling ; ++ jj)
  394.              {
  395.                upsample_data [output] = data_units [du][ii][0] ; ++ output ;
  396.              }
  397.              for (jj = 0 ; jj < h_sampling ; ++ jj)
  398.              {
  399.                upsample_data [output] = data_units [du][ii][1] ; ++ output ;
  400.              }
  401.              for (jj = 0 ; jj < h_sampling ; ++ jj)
  402.              {
  403.                upsample_data [output] = data_units [du][ii][2] ; ++ output ;
  404.              }
  405.              for (jj = 0 ; jj < h_sampling ; ++ jj)
  406.              {
  407.                upsample_data [output] = data_units [du][ii][3] ; ++ output ;
  408.              }
  409.              for (jj = 0 ; jj < h_sampling ; ++ jj)
  410.              {
  411.                upsample_data [output] = data_units [du][ii][4] ; ++ output ;
  412.              }
  413.              for (jj = 0 ; jj < h_sampling ; ++ jj)
  414.              {
  415.                upsample_data [output] = data_units [du][ii][5] ; ++ output ;
  416.              }
  417.              for (jj = 0 ; jj < h_sampling ; ++ jj)
  418.              {
  419.                upsample_data [output] = data_units [du][ii][6] ; ++ output ;
  420.              }
  421.              for (jj = 0 ; jj < h_sampling ; ++ jj)
  422.              {
  423.                upsample_data [output] = data_units [du][ii][7] ; ++ output ;
  424.              }
  425.              ++ du ;
  426.            }
  427.           }
  428.       }
  429.       startdu += du_cols ;
  430.     }
  431.   }
  432.   return ;
  433. }
  434.  
  435. //
  436. //  Description:
  437. //
  438. //    This static member function grayscale converts component
  439. //    image data in the upsample_data array and writes it to the
  440. //    the output image.  Actually for a grayscale conversion all
  441. //    we do is copy.
  442. //
  443. //  Parameters:
  444. //    cc:  The component
  445. //    image:  The output image
  446. //
  447. void JpegDecoderComponent::GrayscaleConvert (JpegDecoderComponent &cc,
  448.                                              BitmapImage &image)
  449. {
  450.   unsigned int rowstart = 0 ;
  451.   for (unsigned int ii = 0 ; ii < image.Height () ; ++ ii)
  452.   {
  453.     unsigned int offset = rowstart ;
  454.     UBYTE1 *outrow = image [ii] ;
  455.     for (unsigned int jj = 0 ; jj < image.Width () ; ++ jj)
  456.     {
  457.       outrow [jj] = cc.upsample_data [offset] ;
  458.       ++ offset ;
  459.     }
  460.     rowstart += cc.du_cols * cc.h_sampling * JpegSampleWidth ;
  461.   }
  462.   return ;
  463. }
  464.  
  465. //
  466. //  Description:
  467. //
  468. //    This static member function converts the upsample_data in three
  469. //    components from YCbCr to RGB and writes it to an image.
  470. //
  471. //  Parameters:
  472. //    c1: The component containing the Y data
  473. //    c2: Ditto for Cb
  474. //    c3: Ditto for Cr
  475. //    image: The output image
  476. //
  477. void JpegDecoderComponent::RGBConvert (JpegDecoderComponent &c1,
  478.                                        JpegDecoderComponent &c2,
  479.                                        JpegDecoderComponent &c3,
  480.                                        BitmapImage &image)
  481. {
  482.   if (c1.upsample_data == NULL
  483.       || c2.upsample_data == NULL
  484.       || c3.upsample_data == NULL)
  485.   {
  486.     // If we get here then do do not yet have data for all components.
  487.     return ;
  488.   }
  489.  
  490.   unsigned int rowstart = 0 ;
  491.   for (unsigned int ii = 0 ; ii < image.Height () ; ++ ii)
  492.   {
  493.     unsigned int offset = rowstart ;
  494.     UBYTE1 *outrow = image [ii] ;
  495.     for (unsigned int jj = 0 ; jj < 3 * image.Width () ; jj += 3)
  496.     {
  497.       JPEGSAMPLE red = YCbCrToR (c1.upsample_data [offset],
  498.                                  c2.upsample_data [offset],
  499.                                  c3.upsample_data [offset]) ;
  500.       JPEGSAMPLE green = YCbCrToG (c1.upsample_data [offset],
  501.                                   c2.upsample_data [offset],
  502.                                   c3.upsample_data [offset]) ;
  503.       JPEGSAMPLE blue = YCbCrToB (c1.upsample_data [offset],
  504.                                   c2.upsample_data [offset],
  505.                                   c3.upsample_data [offset]) ;
  506.       outrow [jj + BitmapImage::RedOffset] = red ;
  507.       outrow [jj + BitmapImage::GreenOffset] = green ;
  508.       outrow [jj + BitmapImage::BlueOffset] = blue ;
  509.       ++ offset ;
  510.     }
  511.     rowstart += c1.du_cols * c1.h_sampling * JpegSampleWidth ;
  512.   }
  513.   return ;
  514. }
  515.  
  516.