home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Examples / c11 / src / jpdecomp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-27  |  26.6 KB  |  881 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.   eob_run = 0 ;
  75.   noninterleaved_rows = 0 ;
  76.   noninterleaved_cols = 0 ;
  77.   data_units = NULL ;
  78.   coefficient_blocks = NULL ;
  79.   upsample_data = NULL ;
  80.   return ;
  81. }
  82.  
  83. //
  84. //  Description:
  85. //
  86. //    Class Destructor
  87. //
  88. JpegDecoderComponent::~JpegDecoderComponent ()
  89. {
  90.   delete [] data_units ; data_units = NULL ;
  91.   delete [] upsample_data ; upsample_data = NULL ;
  92.   return ;
  93. }
  94.  
  95. //
  96. //  Description:
  97. //
  98. //    This function sets the horizontal sampling frequency
  99. //    for the component.
  100. //
  101. //  Parameters:
  102. //    value: The sampling factor (1..4)
  103. //
  104. void JpegDecoderComponent::HorizontalFrequency (unsigned int value)
  105. {
  106.   if (value < JpegMinSamplingFrequency || value > JpegMaxSamplingFrequency)
  107.     throw EJpegValueOutOfRange () ;
  108.  
  109.   horizontal_frequency = value ;
  110.   return ;
  111. }
  112.  
  113. //
  114. //  Description:
  115. //
  116. //    This function sets the vertical sampling frequency
  117. //    for the component.
  118. //
  119. //  Parameters:
  120. //    value: The sampling factor (1..4)
  121. //
  122. void JpegDecoderComponent::VerticalFrequency (unsigned int value)
  123. {
  124.   if (value < JpegMinSamplingFrequency || value > JpegMaxSamplingFrequency)
  125.     throw EJpegValueOutOfRange () ;
  126.  
  127.   vertical_frequency = value ;
  128.  
  129.   return ;
  130. }
  131.  
  132. //
  133. //  Description:
  134. //
  135. //    This function associates a quantization table with the component.
  136. //
  137. //  Parameters:
  138. //    table:  The quantization table
  139. //
  140. void JpegDecoderComponent::SetQuantizationTable (
  141.                                      JpegDecoderQuantizationTable &table)
  142. {
  143.   quantization_table = &table ;
  144.   return ;
  145. }
  146.  
  147. //
  148. //  Description:
  149. //
  150. //    This function determines the dimensions for the component and allocates
  151. //    the storage to hold the component's data.
  152. //
  153. //  Parameters:
  154. //    decoder:  The jpeg decoder this component belongs to.
  155. //
  156. void JpegDecoderComponent::AllocateComponentBuffers (
  157.                                const JpegDecoder &decoder)
  158. {
  159.   if (data_units == NULL)
  160.   {
  161.     // Determine sampling for the component. This is the amount of
  162.     // stretching needed for the component.
  163.     v_sampling = decoder.MaxVFrequency () / vertical_frequency ;
  164.     h_sampling = decoder.MaxHFrequency () / horizontal_frequency ;
  165.  
  166.     // Determine the component's dimensions in a non-interleaved scan.
  167.     noninterleaved_rows = (decoder.FrameHeight ()
  168.                            + v_sampling * JpegSampleWidth - 1)
  169.                           / (v_sampling * JpegSampleWidth) ;
  170.     noninterleaved_cols = (decoder.FrameWidth ()
  171.                            + h_sampling * JpegSampleWidth - 1)
  172.                           / (h_sampling * JpegSampleWidth) ;
  173.  
  174.     du_rows = decoder.McuRows () * vertical_frequency ;
  175.     du_cols = decoder.McuCols () * horizontal_frequency ;
  176.  
  177.     data_units = new JpegDecoderDataUnit [du_rows * du_cols] ;
  178.   }
  179.  
  180.   if (decoder.IsProgressive () && coefficient_blocks == NULL)
  181.   {
  182.     unsigned int count = du_rows * du_cols ;
  183.     coefficient_blocks = new JpegDecoderCoefficientBlock [count] ;
  184.     memset (coefficient_blocks,
  185.             0,
  186.             sizeof (JpegDecoderCoefficientBlock) * count) ;
  187.   }
  188.   return ;
  189. }
  190.  
  191. //
  192. //  Description:
  193. //
  194. //    This function frees the memory allocated by the component
  195. //    during the decompression process.
  196. //
  197. void JpegDecoderComponent::FreeComponentBuffers ()
  198. {
  199.   delete [] data_units ; data_units = NULL ;
  200.   delete [] coefficient_blocks ; coefficient_blocks = NULL ;
  201.   delete [] upsample_data ; upsample_data = NULL ;
  202.   return ;
  203. }
  204.  
  205. //
  206. //  Description:
  207. //
  208. //    This function asigned Huffman tables to the component.
  209. //
  210. //  Parameters:
  211. //    dc:  The DC Huffman table
  212. //    ac:  The AC Huffman table
  213. //
  214. void JpegDecoderComponent::SetHuffmanTables (JpegHuffmanDecoder &dc,
  215.                                              JpegHuffmanDecoder &ac)
  216. {
  217.   dc_table = &dc ;
  218.   ac_table = &ac ;
  219.   return ;
  220. }
  221.  
  222. //
  223. //  Description:
  224. //
  225. //    This function ensures that this component has a defined
  226. //    AC table assigned to it. If not, it throws an exception.
  227. //
  228. void JpegDecoderComponent::CheckAcTable ()
  229. {
  230.   // If this occurs then we have a programming error.
  231.   if (ac_table == NULL)
  232.     throw EJpegFatal ("INTERNAL ERROR - AC Table Not Assigned") ;
  233.  
  234.   if (! ac_table->Defined ())
  235.     throw EJpegError ("AC Table Not Defined") ;
  236.  
  237.   return ;
  238. }
  239.  
  240. //
  241. //  Sequential and Progressive
  242. //
  243. //  This function is called before processing a scan. It ensures that the
  244. //  DC Huffman table used by the component has actually been defined.
  245. //
  246. void JpegDecoderComponent::CheckDcTable ()
  247. {
  248.   if (dc_table == NULL)
  249.     throw EJpegFatal ("INTERNAL ERROR - DC Table Not Assigned") ;
  250.  
  251.   // This condition could be caused by a corrupt JPEG stream.
  252.   if (! dc_table->Defined ())
  253.     throw EJpegFatal ("INTERNAL ERROR - DC Table Not Defined") ;
  254.  
  255.   return ;
  256. }
  257.  
  258. //
  259. //  Description:
  260. //
  261. //    Sequential and Progressive
  262. //
  263. //    This function is called before processing a scan. It ensures that the
  264. //    Quantization table used by the component has actually been defined.
  265. //
  266. void JpegDecoderComponent::CheckQuantizationTable ()
  267. {
  268.   if (quantization_table == NULL)
  269.     throw EJpegFatal ("INTERNAL ERROR - Quantization Table Not Assigned") ;
  270.  
  271.   if (! quantization_table->Defined ())
  272.     throw EJpegError ("Quantization Table Not Defined") ;
  273.  
  274.   return ;
  275. }
  276.  
  277. //
  278. //  Description:
  279. //
  280. //    This function decodes a data unit in a sequential scan.
  281. //
  282. //  Parameters:
  283. //    decoder: The decoder that owns this component
  284. //    mcurow, mcucol:  The row and column for this data unit.
  285. //
  286. void JpegDecoderComponent::DecodeSequential (JpegDecoder &decoder,
  287.                                              unsigned int mcurow,
  288.                                              unsigned int mcucol)
  289. {
  290.   JpegDecoderCoefficientBlock data ;
  291.   memset (&data, 0, sizeof (data)) ;
  292.  
  293.   // Decode the DC differce value.
  294.   // Section F.2.2.1
  295.   unsigned int count ; // called T in F.2.2.1
  296.   count = dc_table->Decode (decoder) ;
  297.   int bits = decoder.Receive (count) ;
  298.   int diff = Extend (bits, count) ;
  299.  
  300.   // Create the DC value from the difference and the previous DC value.
  301.   int dc = diff + last_dc_value ;
  302.   last_dc_value = dc ;
  303.   data [0][0] = dc ;
  304.  
  305.   // Decode the AC coefficients.
  306.   // Section F.2.2.2 Figure F.13
  307.   for (unsigned int kk = 1 ; kk < JpegSampleSize ; ++ kk)
  308.   {
  309.     UBYTE2 rs = ac_table->Decode (decoder) ;
  310.     UBYTE2 ssss = (UBYTE2) (rs & 0xF) ;
  311.     UBYTE2 rrrr = (UBYTE2) (rs >> 0x4) ;
  312.  
  313.     if (ssss == 0)
  314.     {
  315.       // ssss is zero then rrrr should either be 15 or zero according to
  316.       // Figure F.1. 0 means that the rest of the coefficients are zero
  317.       // while 15 means the next 16 coefficients are zero. We are not checking
  318.       // for other values because Figure F.13 shows values other than 15
  319.       // as being treated as zero.
  320.       if (rrrr  != 15)
  321.         break ;
  322.       kk += 15 ; // Actually 16 since one more gets added by the loop.
  323.     }
  324.     else
  325.     {
  326.       // If ssss is non-zero then rrrr gives the number of zero coefficients
  327.       // to skip.
  328.  
  329.       kk += rrrr ;
  330.       if (kk >= JpegSampleSize)
  331.         throw EJpegFatal ("Value out of range") ;
  332.  
  333.       // Receive and extend the additional bits.
  334.       // Section F2.2.2 Figure F.14
  335.       int bits = decoder.Receive (ssss) ;
  336.       int value = Extend (bits, ssss) ;
  337.       (&data [0][0])[JpegZigZagInputOrder (kk)] = value ;
  338.     }
  339.   }
  340.   data_units [mcurow * du_cols + mcucol].InverseDCT (data,
  341.                                                      *quantization_table) ;
  342.   return ;
  343. }
  344.  
  345. //
  346. //  Description:
  347. //
  348. //    This function upsamples the data for the component. Here we take
  349. //    the values from the data_units array and copy it to the
  350. //    upsample_data. If the horizontal or vertical sampling frequencies
  351. //    are less than the maximum for the image then we need to
  352. //    stretch the data during the copy.
  353. //
  354. void JpegDecoderComponent::Upsample ()
  355. {
  356.   unsigned int imagesize = du_rows * v_sampling * du_cols
  357.                               * h_sampling * JpegSampleSize ;
  358.   if (imagesize == 0)
  359.     return ;  // No data for this component yet.
  360.  
  361.   if (upsample_data == NULL)
  362.     upsample_data = new JPEGSAMPLE [imagesize] ;
  363.  
  364.   // Simple case where component does not need to be upsampled.
  365.   if (v_sampling == 1 && h_sampling == 1)
  366.   {
  367.     unsigned output = 0 ;
  368.     unsigned int startdu = 0 ;
  369.     for (unsigned int durow = 0 ; durow < du_rows ; ++ durow)
  370.     {
  371.       for (unsigned int ii = 0 ; ii < JpegSampleWidth ; ++ ii)
  372.       {
  373.          unsigned int du = startdu ;
  374.           for (unsigned int ducol = 0 ; ducol < du_cols ; ++ ducol)
  375.         {
  376.             upsample_data [output] = data_units [du][ii][0] ; ++ output ;
  377.             upsample_data [output] = data_units [du][ii][1] ; ++ output ;
  378.             upsample_data [output] = data_units [du][ii][2] ; ++ output ;
  379.             upsample_data [output] = data_units [du][ii][3] ; ++ output ;
  380.             upsample_data [output] = data_units [du][ii][4] ; ++ output ;
  381.             upsample_data [output] = data_units [du][ii][5] ; ++ output ;
  382.             upsample_data [output] = data_units [du][ii][6] ; ++ output ;
  383.             upsample_data [output] = data_units [du][ii][7] ; ++ output ;
  384.           ++ du ;
  385.           }
  386.       }
  387.       startdu += du_cols ;
  388.     }
  389.   }
  390.   else
  391.   {
  392.     unsigned output = 0 ;
  393.     unsigned int startdu = 0 ;
  394.     for (unsigned int durow = 0 ; durow < du_rows ; ++ durow)
  395.     {
  396.       for (unsigned int ii = 0 ; ii < JpegSampleWidth ; ++ ii)
  397.       {
  398.          for (unsigned int vv = 0 ; vv < v_sampling ; ++ vv)
  399.          {
  400.            unsigned int du = startdu ;
  401.               for (unsigned int ducol = 0 ; ducol < du_cols ; ++ ducol)
  402.            {
  403.              unsigned int jj ;
  404.              for (jj = 0 ; jj < h_sampling ; ++ jj)
  405.              {
  406.                upsample_data [output] = data_units [du][ii][0] ; ++ output ;
  407.              }
  408.              for (jj = 0 ; jj < h_sampling ; ++ jj)
  409.              {
  410.                upsample_data [output] = data_units [du][ii][1] ; ++ output ;
  411.              }
  412.              for (jj = 0 ; jj < h_sampling ; ++ jj)
  413.              {
  414.                upsample_data [output] = data_units [du][ii][2] ; ++ output ;
  415.              }
  416.              for (jj = 0 ; jj < h_sampling ; ++ jj)
  417.              {
  418.                upsample_data [output] = data_units [du][ii][3] ; ++ output ;
  419.              }
  420.              for (jj = 0 ; jj < h_sampling ; ++ jj)
  421.              {
  422.                upsample_data [output] = data_units [du][ii][4] ; ++ output ;
  423.              }
  424.              for (jj = 0 ; jj < h_sampling ; ++ jj)
  425.              {
  426.                upsample_data [output] = data_units [du][ii][5] ; ++ output ;
  427.              }
  428.              for (jj = 0 ; jj < h_sampling ; ++ jj)
  429.              {
  430.                upsample_data [output] = data_units [du][ii][6] ; ++ output ;
  431.              }
  432.              for (jj = 0 ; jj < h_sampling ; ++ jj)
  433.              {
  434.                upsample_data [output] = data_units [du][ii][7] ; ++ output ;
  435.              }
  436.              ++ du ;
  437.            }
  438.           }
  439.       }
  440.       startdu += du_cols ;
  441.     }
  442.   }
  443.   return ;
  444. }
  445.  
  446. //
  447. //  Description:
  448. //
  449. //    This static member function grayscale converts component
  450. //    image data in the upsample_data array and writes it to the
  451. //    the output image.  Actually for a grayscale conversion all
  452. //    we do is copy.
  453. //
  454. //  Parameters:
  455. //    cc:  The component
  456. //    image:  The output image
  457. //
  458. void JpegDecoderComponent::GrayscaleConvert (JpegDecoderComponent &cc,
  459.                                              BitmapImage &image)
  460. {
  461.   unsigned int rowstart = 0 ;
  462.   for (unsigned int ii = 0 ; ii < image.Height () ; ++ ii)
  463.   {
  464.     unsigned int offset = rowstart ;
  465.     UBYTE1 *outrow = image [ii] ;
  466.     for (unsigned int jj = 0 ; jj < image.Width () ; ++ jj)
  467.     {
  468.       outrow [jj] = cc.upsample_data [offset] ;
  469.       ++ offset ;
  470.     }
  471.     rowstart += cc.du_cols * cc.h_sampling * JpegSampleWidth ;
  472.   }
  473.   return ;
  474. }
  475.  
  476. //
  477. //  Description:
  478. //
  479. //    This static member function converts the upsample_data in three
  480. //    components from YCbCr to RGB and writes it to an image.
  481. //
  482. //  Parameters:
  483. //    c1: The component containing the Y data
  484. //    c2: Ditto for Cb
  485. //    c3: Ditto for Cr
  486. //    image: The output image
  487. //
  488. void JpegDecoderComponent::RGBConvert (JpegDecoderComponent &c1,
  489.                                        JpegDecoderComponent &c2,
  490.                                        JpegDecoderComponent &c3,
  491.                                        BitmapImage &image)
  492. {
  493.   if (c1.upsample_data == NULL
  494.       || c2.upsample_data == NULL
  495.       || c3.upsample_data == NULL)
  496.   {
  497.     // If we get here then do do not yet have data for all components.
  498.     return ;
  499.   }
  500.  
  501.   unsigned int rowstart = 0 ;
  502.   for (unsigned int ii = 0 ; ii < image.Height () ; ++ ii)
  503.   {
  504.     unsigned int offset = rowstart ;
  505.     UBYTE1 *outrow = image [ii] ;
  506.     for (unsigned int jj = 0 ; jj < 3 * image.Width () ; jj += 3)
  507.     {
  508.       JPEGSAMPLE red = YCbCrToR (c1.upsample_data [offset],
  509.                                  c2.upsample_data [offset],
  510.                                  c3.upsample_data [offset]) ;
  511.       JPEGSAMPLE green = YCbCrToG (c1.upsample_data [offset],
  512.                                   c2.upsample_data [offset],
  513.                                   c3.upsample_data [offset]) ;
  514.       JPEGSAMPLE blue = YCbCrToB (c1.upsample_data [offset],
  515.                                   c2.upsample_data [offset],
  516.                                   c3.upsample_data [offset]) ;
  517.       outrow [jj + BitmapImage::RedOffset] = red ;
  518.       outrow [jj + BitmapImage::GreenOffset] = green ;
  519.       outrow [jj + BitmapImage::BlueOffset] = blue ;
  520.       ++ offset ;
  521.     }
  522.     rowstart += c1.du_cols * c1.h_sampling * JpegSampleWidth ;
  523.   }
  524.   return ;
  525. }
  526.  
  527. //
  528. //  Description:
  529. //
  530. //    Progressive Only
  531. //
  532. //    This function decodes the DC coefficient for a data unit in the first
  533. //    DC scan for the component.
  534. //
  535. //    According to G.2 "In order to avoid repetition, detail flow diagrams
  536. //    of progressive decoder operation are not included. Decoder operation is
  537. //    defined by reversing the function of each stop described in the encoder
  538. //    flow charts, and performing the steps in reverse order."
  539. //
  540. //  Parameters:
  541. //    decoder:  The JPEG decoder
  542. //    row:  The data unit row
  543. //    col:  The data unit column
  544. //    ssa:  Successive Approximation
  545. //
  546. void JpegDecoderComponent::DecodeDcFirst (JpegDecoder &decoder,
  547.                                           unsigned int row,
  548.                                           unsigned int col,
  549.                                           unsigned int ssa)
  550. {
  551.   // We decode the first DC coeffient that same way as in a sequential
  552.   // scan except for the point transform according to G.1.2.1
  553.  
  554.   // Section F.2.2.1
  555.   unsigned int count ; // called T in F.2.2.1
  556.   count = dc_table->Decode (decoder) ;
  557.   int bits = decoder.Receive (count) ;
  558.   int diff = Extend (bits, count) ;
  559.   int value = diff + last_dc_value ;
  560.   last_dc_value = value ;
  561.   coefficient_blocks [row * du_cols + col][0][0] = (value << ssa) ;
  562.   return ;
  563. }
  564.  
  565. //
  566. //  Description:
  567. //
  568. //    Progressive Only
  569. //    
  570. //    This function decodes the DC coefficient for a data unit in refining
  571. //    DC scans for the component.
  572. //
  573. //    According to G.2 "In order to avoid repetition, detail flow diagrams
  574. //    of progressive decoder operation are not included. Decoder operation is
  575. //    defined by reversing the function of each stop described in the encoder
  576. //    flow charts, and performing the steps in reverse order."
  577. //
  578. //  Parameters:
  579. //    decoder:  The JPEG decoder
  580. //    row:  The data unit row
  581. //    col:  The data unit column
  582. //    ssa:  Successive Approximation
  583. //
  584. void JpegDecoderComponent::DecodeDcRefine (JpegDecoder &decoder,
  585.                                            unsigned int row,
  586.                                            unsigned int col,
  587.                                            unsigned int ssa)
  588. {
  589.   // Reversing G.1.2.1
  590.   if (decoder.Receive (1) != 0)
  591.   {
  592.     coefficient_blocks [row * du_cols + col][0][0] |= (1 << ssa) ;
  593.   }
  594.   return ;
  595. }
  596.  
  597. //
  598. //  Description:
  599. //
  600. //    Progressive Only
  601. //
  602. //    This function decodes the AC coefficients for a data unit in the first
  603. //    AC scans for a spectral range within the component.
  604. //
  605. //    According to G.2 "In order to avoid repetition, detail flow diagrams
  606. //    of progressive decoder operation are not included. Decoder operation is
  607. //    defined by reversing the function of each stop described in the encoder
  608. //    flow charts, and performing the steps in reverse order."
  609. //
  610. //    This function comes from reversing the steps in Figures G.3-G.5.
  611. //
  612. //  Parameters:
  613. //    decoder:  The JPEG decoder
  614. //    row:  The data unit row
  615. //    col:  The data unit column
  616. //    sss:  Spectral Selection Start
  617. //    sse:  Spectral Selection End
  618. //    ssa:  Successive Approximation
  619. //
  620. void JpegDecoderComponent::DecodeAcFirst (JpegDecoder &decoder,
  621.                                           unsigned int row,
  622.                                           unsigned int col,
  623.                                           unsigned int sss,
  624.                                           unsigned int sse,
  625.                                           unsigned int ssa)
  626. {
  627.   JpegDecoderCoefficientBlock *data =
  628.                   &coefficient_blocks [row * du_cols + col] ;
  629.   if (eob_run > 0)
  630.   {
  631.     // If a previous call created a nonzero EOB run then we decrement the
  632.     // counter and return.
  633.     -- eob_run ;
  634.   }
  635.   else
  636.   {
  637.     for (unsigned int kk = sss ; kk <= sse ; )
  638.     {
  639.       // Decode the next value in the input stream.
  640.       UBYTE2 rs = ac_table->Decode (decoder) ;
  641.       UBYTE1 ssss = (UBYTE1) (rs & 0xF) ;
  642.       UBYTE1 rrrr = (UBYTE1) (rs >> 0x4) ;
  643.  
  644.       if (ssss == 0)
  645.       {
  646.         if (rrrr == 15)
  647.         {
  648.           // A zero value ssss with rrrr == 15 means to skip
  649.           // 16 zero coefficients.
  650.             kk += 16 ;
  651.         }
  652.         else
  653.         {
  654.           // A zero value ssss with rrrr != 15 means to create
  655.           // End of Band run.
  656.  
  657.           // The EOB run includes the current block. This is why we
  658.           // do no processing for rrrr = 0 and substract one when
  659.           // rrrrr != 0.
  660.           if (rrrr != 0)
  661.           {
  662.             int bits = decoder.Receive (rrrr) ;
  663.             eob_run = (1 << rrrr) + bits - 1 ;
  664.           }
  665.           break ;
  666.         }
  667.       }
  668.       else
  669.       {
  670.         // When ssss != 0, rrrr gives the number of zero elements to skip
  671.         // before the next non-zero coefficient.
  672.         kk += rrrr ;
  673.         if (kk >= JpegSampleSize)
  674.           throw EJpegBadData ("Data out of range") ;
  675.  
  676.         // Extend the value and store.
  677.         int bits = decoder.Receive (ssss) ;
  678.         int value = Extend (bits, ssss) ;
  679.         (&((*data)[0][0]))[JpegZigZagInputOrder (kk)] = (value << ssa) ;
  680.         ++ kk ;
  681.       }
  682.     }
  683.   }
  684.   return ;
  685. }
  686.  
  687. //
  688. //  Description:
  689. //
  690. //    Progressive Only
  691. //
  692. //    This function decodes the AC coefficients for a data unit in the
  693. //    refining AC scans for a spectral range within the component.
  694. //
  695. //    According to G.2 "In order to avoid repetition, detail flow diagrams
  696. //    of progressive decoder operation are not included. Decoder operation is
  697. //    defined by reversing the function of each stop described in the encoder
  698. //    flow charts, and performing the steps in reverse order."
  699. //
  700. //    Section G.1.2.3 defines how to encode refining scans for AC
  701. //    coefficients. Unfortunately this section is vague and
  702. //    undecipherable. Reversing an undecipherable process results
  703. //    in something unimaginable. This is a "best-guess" interpretation
  704. //    that seems to work.
  705. //
  706. //    The basic process at work is that zero counts do not include nonzero
  707. //    values. Whenever we skip a value due to zero count or End of Band runs
  708. //    we have to read one bit to refine each non-zero value we skip. The
  709. //    process is ugly and it means that data is encoding out of order.
  710. //
  711. //  Parameters:
  712. //    decoder:  The JPEG decoder
  713. //    row:  The data unit row
  714. //    col:  The data unit column
  715. //    sss:  Spectral Selection Start
  716. //    sse:  Spectral Selection End
  717. //    ssa:  Successive Approximation
  718. //                                                            4
  719. void JpegDecoderComponent::DecodeAcRefine (JpegDecoder &decoder,
  720.                                            unsigned int row,
  721.                                            unsigned int col,
  722.                                            unsigned int sss,
  723.                                            unsigned int sse,
  724.                                            unsigned int ssa)
  725. {
  726.   JpegDecoderCoefficientBlock *data =
  727.                   &coefficient_blocks [row * du_cols + col] ;
  728.   // kk is incremented within the loop.
  729.   for (unsigned int kk = sss ; kk <= sse ;)
  730.   {
  731.     if (eob_run != 0)
  732.     {
  733.       // An EOB run has caused us to skip entire data units. We need
  734.       // to refine any previously non-zero coefficients.
  735.       // Notice that we do not initialize kk here. We could be using
  736.       // an EOB run to skip all the remaining coefficients in the current
  737.       // one.
  738.  
  739.       for ( ; kk <= sse ; ++ kk)
  740.       {
  741.         if ((&((*data)[0][0]))[JpegZigZagInputOrder (kk)] != 0)
  742.         {
  743.           decoder.RefineAcCoefficient (
  744.                       (&((*data)[0][0]))[JpegZigZagInputOrder (kk)],
  745.                       ssa) ;
  746.         }
  747.       }
  748.       -- eob_run ;
  749.     }
  750.     else
  751.     {
  752.       UBYTE2 rs = ac_table->Decode (decoder) ;
  753.       UBYTE1 ssss = (UBYTE1) (rs & 0xF) ;
  754.       UBYTE1 rrrr = (UBYTE1) (rs >> 0x4) ;
  755.  
  756.       if (ssss == 0)
  757.       {
  758.         // ssss == 0 means that we either have an EOB run or we need to
  759.         // 16 non-zero coefficients.
  760.  
  761.         if (rrrr == 15)
  762.         {
  763.           // ssss == 0 and rrrr == 15 => Skip over 16 zero coefficients
  764.           for (unsigned int ii = 0 ;
  765.               kk <= sse  && ii < 16 ;
  766.               ++ kk)
  767.           {
  768.             if (kk >  sse)
  769.               throw EJpegBadData ("Corrupt Scan Data") ;
  770.  
  771.             if ((&((*data)[0][0]))[JpegZigZagInputOrder (kk)] != 0)
  772.             {
  773.               decoder.RefineAcCoefficient (
  774.                             (&((*data)[0][0]))[JpegZigZagInputOrder (kk)],
  775.                             ssa) ;
  776.             }
  777.             else
  778.             {
  779.               ++ ii ;
  780.             }
  781.           }
  782.         }
  783.         else
  784.         {
  785.           // We are reading an EOB run.
  786.           if (rrrr == 0)
  787.           {
  788.             eob_run = 1 ;
  789.           }
  790.           else
  791.           {
  792.             int bits = decoder.Receive (rrrr) ;
  793.             eob_run = (1 << rrrr) + bits ;
  794.           }
  795.         }
  796.       }
  797.       else if (ssss == 1)
  798.       {
  799.         // ssss == 1 means that we are creating a new non-zero
  800.         // coefficient. rrrr gives the number of zero coefficients to
  801.         // skip before we reach this one.
  802.         // Save the value for the new coefficient. Unfortunately the data
  803.         // is stored out of order.
  804.         int newvalue = decoder.Receive (1) ;
  805.  
  806.         // Skip the zero coefficients.
  807.         for (unsigned int zerocount = 0 ;
  808.              kk <  JpegSampleSize
  809.               && (zerocount < rrrr
  810.                   || (&((*data)[0][0]))[JpegZigZagInputOrder (kk)] != 0) ;
  811.              ++ kk)
  812.         {
  813.           if (kk >  sse)
  814.             throw EJpegBadData ("Error in progressive scan") ;
  815.  
  816.           if ((&((*data)[0][0]))[JpegZigZagInputOrder (kk)] != 0)
  817.           {
  818.             decoder.RefineAcCoefficient ((&((*data)[0][0]))[JpegZigZagInputOrder (kk)],
  819.                                  ssa) ;
  820.           }
  821.           else
  822.           {
  823.             ++ zerocount ;
  824.           }
  825.         }
  826.  
  827.         if (kk >  sse)
  828.           throw EJpegBadData ("Error in progressive scan") ;
  829.  
  830.         if (newvalue)
  831.         {
  832.           (&((*data)[0][0]))[JpegZigZagInputOrder (kk)] = (1 << ssa) ;
  833.         }
  834.         else
  835.         {
  836.           (&((*data)[0][0]))[JpegZigZagInputOrder (kk)] = (-1 << ssa) ;
  837.         }
  838.         ++ kk ;
  839.       }
  840.       else
  841.       {
  842.         // The value of SSSS must be zero or one. Since we add data one
  843.         // bit at at a time data values larger than 1 make no sense.
  844.         throw EJpegBadData ("Invalid value in input stream") ;
  845.       }
  846.     }
  847.   }
  848.   return ;
  849. }
  850.  
  851. //
  852. //  Description:
  853. //
  854. //    Progressive Only
  855. //
  856. //    This function performs the IDCT on all the data in
  857. //    coefficient_blocks and stores the result in data_units.
  858. //
  859. //    This function gets called whenever the image data is written
  860. //    to the image.  For a sequential image the IDCT only needs to
  861. //    performed once no matter how often the data gets updated in the
  862. //    image but to continuously update the progressive image data
  863. //    an update after each scan gives new results.
  864. //
  865. void JpegDecoderComponent::ProgressiveInverseDct ()
  866. {
  867.   // If UpdateImage gets called before the image is completely
  868.   // decoded the these values may be NULL.
  869.   if (data_units == NULL || coefficient_blocks == NULL)
  870.     return ;
  871.  
  872.   unsigned int limit = du_cols * du_rows ;
  873.   for (unsigned int ii = 0 ; ii < limit ; ++ ii)
  874.   {
  875.     data_units [ii].InverseDCT (coefficient_blocks [ii], *quantization_table) ;
  876.   }
  877.   return ;
  878. }
  879.  
  880.  
  881.