E:/PPC/XFlib/source/xf_jpeg.c

Go to the documentation of this file.
00001 
00002 #include "xf_jpeg.h"
00003 #include "xf_bg.h"
00004 
00005 /* Setup the IWRAM-loading definitions if this has been enabled.  There are
00006  * three sections of this; the register definitions, the IWRAM end
00007  * determination, and the DMA copying functions.  Register definitions are the
00008  * same as anywhere else, only with a JPEG_IWRAM prefix.  The IWRAM end
00009  * determination uses a generated variable that DevKit Advance's linker script
00010  * creates.  Because of this, other linker scripts might not work with this
00011  * code.  Finally, the DMA copying uses DMA 3.
00012  *
00013  * Functions that are to be copied to IWRAM must obey certain restrictions.
00014  * They cannot refer to external constant data.  They must be declared static.
00015  * They must have a JPEG_FUNCTION_END(NAME) macro after them; see how it is
00016  * used in the code ahead for an example.  Finally, you should avoid external
00017  * references altogether because of how it limits your flexibility.  Instead,
00018  * pass necessary variable and function pointers in the arguments.
00019  */
00020  
00021  
00022  void XF_BgLoadJpeg(u8 number, void *jpeg){
00023         s16 width = XF_GetJpegWidth(jpeg);
00024         s16 height = XF_GetJpegHeight(jpeg);
00025         XF_BgLoadBitmap(number, width, height, 1);
00026         JPEG_DecompressImage(jpeg, xf_bg[number].Tiles, width, height);
00027 }
00028 
00029 
00030  
00031 
00032     /* This stub does absolutely nothing. */
00033     #define JPEG_FUNCTION_END(NAME)
00034 
00035 /* Converts left-to-right coefficient indices into zig-zagged indices. */
00036 const unsigned char JPEG_ToZigZag [JPEG_DCTSIZE2] =
00037 {
00038     0, 1, 8, 16, 9, 2, 3, 10,
00039     17, 24, 32, 25, 18, 11, 4, 5,
00040     12, 19, 26, 33, 40, 48, 41, 34,
00041     27, 20, 13, 6, 7, 14, 21, 28,
00042     35, 42, 49, 56, 57, 50, 43, 36,
00043     29, 22, 15, 23, 30, 37, 44, 51,
00044     58, 59, 52, 45, 38, 31, 39, 46,
00045     53, 60, 61, 54, 47, 55, 62, 63,
00046 };
00047 
00048 /* These macros are so that we can generate the AA&N multipliers at
00049  * compile-time, allowing configuration control of fixed point precision.
00050  */
00051 #define JPEG_AAN_0 1.0
00052 #define JPEG_AAN_1 1.387039845 
00053 #define JPEG_AAN_2 1.306562965
00054 #define JPEG_AAN_3 1.175875602
00055 #define JPEG_AAN_4 1.0
00056 #define JPEG_AAN_5 0.785694958
00057 #define JPEG_AAN_6 0.541196100
00058 #define JPEG_AAN_7 0.275899379
00059 
00060 #define JPEG_AAN_LINE(B) \
00061     JPEG_FTOFIX (JPEG_AAN_0 * JPEG_AAN_##B), \
00062     JPEG_FTOFIX (JPEG_AAN_1 * JPEG_AAN_##B), \
00063     JPEG_FTOFIX (JPEG_AAN_2 * JPEG_AAN_##B), \
00064     JPEG_FTOFIX (JPEG_AAN_3 * JPEG_AAN_##B), \
00065     JPEG_FTOFIX (JPEG_AAN_4 * JPEG_AAN_##B), \
00066     JPEG_FTOFIX (JPEG_AAN_5 * JPEG_AAN_##B), \
00067     JPEG_FTOFIX (JPEG_AAN_6 * JPEG_AAN_##B), \
00068     JPEG_FTOFIX (JPEG_AAN_7 * JPEG_AAN_##B)
00069 
00070 /* The AA&N scaling factors.  These should be multiplied against quantization
00071  * coefficients to determine their real value.
00072  */
00073 const JPEG_FIXED_TYPE JPEG_AANScaleFactor [JPEG_DCTSIZE2] =
00074 {
00075     JPEG_AAN_LINE (0),
00076     JPEG_AAN_LINE (1),
00077     JPEG_AAN_LINE (2),
00078     JPEG_AAN_LINE (3),
00079     JPEG_AAN_LINE (4),
00080     JPEG_AAN_LINE (5),
00081     JPEG_AAN_LINE (6),
00082     JPEG_AAN_LINE (7),
00083 };
00084 
00085 /* This converts values in the range [-32 .. 32] to [0 .. 32] by clamping
00086  * values outside of that range.  To use it, add 32 to your input.
00087  */
00088 const unsigned char JPEG_ComponentRange [32 * 3] =
00089 {
00090     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
00091         
00092     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 
00093         17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
00094         
00095     31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31
00096 };
00097 
00098 const unsigned char JPEG_ComponentRangeG [64 * 3] =
00099 {
00100     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
00101     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
00102         
00103     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 
00104         16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
00105         32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
00106         48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
00107         
00108     63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
00109     63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63,
00110 };
00111 int jpeg_width;
00112 
00113 /* Compute the columns half of the IDCT. */
00114 static void JPEG_IDCT_Columns (JPEG_FIXED_TYPE *zz)
00115 {
00116     JPEG_FIXED_TYPE tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9, tmp10, tmp11;
00117     JPEG_FIXED_TYPE *ez = zz + JPEG_DCTSIZE;
00118 
00119     /* The first column will always have a non-zero coefficient, the DC. */
00120     goto skipFirstCheckb;
00121     
00122     for ( ; zz < ez; zz ++)
00123     {
00124         /* A column containing only zeroes will output only zeroes.  Since we
00125          * output in-place, we don't need to do anything in that case.
00126          */
00127         if (!zz [0 * JPEG_DCTSIZE] && !zz [1 * JPEG_DCTSIZE]
00128         && !zz [2 * JPEG_DCTSIZE] && !zz [3 * JPEG_DCTSIZE]
00129         && !zz [4 * JPEG_DCTSIZE] && !zz [5 * JPEG_DCTSIZE]
00130         && !zz [6 * JPEG_DCTSIZE] && !zz [7 * JPEG_DCTSIZE])
00131             continue;
00132         
00133     skipFirstCheckb:
00134         tmp0 = zz [0 * JPEG_DCTSIZE];
00135         tmp1 = zz [2 * JPEG_DCTSIZE];
00136         tmp2 = zz [4 * JPEG_DCTSIZE];
00137         tmp3 = zz [6 * JPEG_DCTSIZE];
00138         
00139         tmp6 = tmp1 + tmp3;
00140         tmp7 = JPEG_FIXMUL (tmp1 - tmp3, JPEG_FTOFIX (1.414213562)) - tmp6;
00141         tmp1 = tmp0 - tmp2 + tmp7;
00142         tmp0 = tmp0 + tmp2 + tmp6;
00143         
00144         tmp3 = tmp0 - (tmp6 << 1);
00145         tmp2 = tmp1 - (tmp7 << 1);
00146         
00147         tmp4 = zz [1 * JPEG_DCTSIZE];
00148         tmp5 = zz [3 * JPEG_DCTSIZE];
00149         tmp6 = zz [5 * JPEG_DCTSIZE];
00150         tmp7 = zz [7 * JPEG_DCTSIZE];
00151         
00152         tmp10 = tmp4 - tmp7;
00153         
00154         tmp8 = tmp6 + tmp5;
00155         tmp9 = tmp4 + tmp7;
00156         tmp7 = tmp9 + tmp8;
00157         tmp11 = JPEG_FIXMUL (tmp9 - tmp8, JPEG_FTOFIX (1.414213562));
00158         
00159         tmp8 = tmp6 - tmp5;
00160         tmp9 = JPEG_FIXMUL (tmp8 + tmp10, JPEG_FTOFIX (1.847759065));
00161         
00162         tmp6 = JPEG_FIXMUL (JPEG_FTOFIX (-2.613125930), tmp8) + tmp9 - tmp7;
00163         tmp5 = tmp11 - tmp6;
00164         tmp4 = JPEG_FIXMUL (JPEG_FTOFIX (1.082392200), tmp10) - tmp9 + tmp5; 
00165         
00166         zz [0 * JPEG_DCTSIZE] = tmp0 + tmp7;
00167         zz [1 * JPEG_DCTSIZE] = tmp1 + tmp6;
00168         zz [2 * JPEG_DCTSIZE] = tmp2 + tmp5;
00169         zz [3 * JPEG_DCTSIZE] = tmp3 - tmp4;
00170         zz [4 * JPEG_DCTSIZE] = tmp3 + tmp4;
00171         zz [5 * JPEG_DCTSIZE] = tmp2 - tmp5;
00172         zz [6 * JPEG_DCTSIZE] = tmp1 - tmp6;
00173         zz [7 * JPEG_DCTSIZE] = tmp0 - tmp7;
00174     }
00175 }
00176 JPEG_FUNCTION_END (JPEG_IDCT_Columns)
00177 
00178 /* Compute the rows half of the IDCT, loading the component information into
00179  * chunk as values in the range -64 to 64, although it can go somewhat outside
00180  * of that range.  chunkStride is the number of bytes in a row in chunk.
00181  */
00182  
00183 static void JPEG_IDCT_Rows (const JPEG_FIXED_TYPE *zz, signed char *chunk, int chunkStride)
00184 {
00185     JPEG_FIXED_TYPE tmp0, tmp1, tmp2, tmp3, tmp10, tmp11, tmp12, tmp13;
00186     JPEG_FIXED_TYPE tmp4, tmp5, tmp6, tmp7, z5, z10, z11, z12, z13;
00187     int row;
00188     
00189     for (row = 0; row < JPEG_DCTSIZE; row ++, zz += JPEG_DCTSIZE, chunk += chunkStride)
00190     {
00191         tmp10 = zz [0] + zz [4];
00192         tmp11 = zz [0] - zz [4];
00193 
00194         tmp13 = zz [2] + zz [6];
00195         tmp12 = JPEG_FIXMUL (zz [2] - zz [6], JPEG_FTOFIX (1.414213562)) - tmp13;
00196 
00197         tmp0 = tmp10 + tmp13;
00198         tmp3 = tmp10 - tmp13;
00199         tmp1 = tmp11 + tmp12;
00200         tmp2 = tmp11 - tmp12;
00201         
00202         z13 = zz [5] + zz [3];
00203         z10 = zz [5] - zz [3];
00204         z11 = zz [1] + zz [7];
00205         z12 = zz [1] - zz [7];
00206 
00207         tmp7 = z11 + z13;
00208         tmp11 = JPEG_FIXMUL (z11 - z13, JPEG_FTOFIX (1.414213562));
00209 
00210         z5 = JPEG_FIXMUL (z10 + z12, JPEG_FTOFIX (1.847759065));
00211         tmp10 = JPEG_FIXMUL (JPEG_FTOFIX (1.082392200), z12) - z5;
00212         tmp12 = JPEG_FIXMUL (JPEG_FTOFIX (-2.613125930), z10) + z5;
00213         
00214         tmp6 = tmp12 - tmp7;
00215         tmp5 = tmp11 - tmp6;
00216         tmp4 = tmp10 + tmp5;
00217 
00218         /* This shifts by an extra bit to remove the need for clamping at
00219          * this point.  Thus the normative samples are in the range -64 to 63.
00220          * This requires a later bit-shift, but that comes for free with the ARM
00221          * instruction set, and has an acceptable, likely imperceptible, loss
00222          * of quality.
00223          */
00224          
00225         chunk [0] = JPEG_FIXTOI (tmp0 + tmp7) >> 4;
00226         chunk [1] = JPEG_FIXTOI (tmp1 + tmp6) >> 4;
00227         chunk [2] = JPEG_FIXTOI (tmp2 + tmp5) >> 4;
00228         chunk [3] = JPEG_FIXTOI (tmp3 - tmp4) >> 4;
00229         chunk [4] = JPEG_FIXTOI (tmp3 + tmp4) >> 4;
00230         chunk [5] = JPEG_FIXTOI (tmp2 - tmp5) >> 4;
00231         chunk [6] = JPEG_FIXTOI (tmp1 - tmp6) >> 4;
00232         chunk [7] = JPEG_FIXTOI (tmp0 - tmp7) >> 4;
00233     }
00234 }
00235 JPEG_FUNCTION_END (JPEG_IDCT_Rows)
00236 
00237 /* This function comes from jpeglib.  I feel all right about that since it comes from AA&N anyway. */
00238 void JPEG_IDCT (JPEG_FIXED_TYPE *zz, signed char *chunk, int chunkStride)
00239 {
00240     JPEG_IDCT_Columns (zz);
00241     JPEG_IDCT_Rows (zz, chunk, chunkStride);
00242 }
00243 
00244 /* Compute a signed value.  COUNT is the number of bits to read, and OUT is
00245  * where to store the result.
00246  */
00247  
00248 #define JPEG_Value(COUNT, OUT) \
00249     do { \
00250         unsigned int value = JPEG_BITS_GET (COUNT); \
00251         \
00252         if (value < (unsigned int) (1 << ((unsigned int) (COUNT - 1)))) \
00253             value += (-1 << COUNT) + 1; \
00254         (OUT) = value; \
00255     } while (0)
00256  
00257 /* Decode the coefficients from the input stream and do dequantization at the
00258  * same time.  dcLast is the previous block's DC value and is updated.  zz is
00259  * the output coefficients and will be all ready for an IDCT.  quant is the
00260  * quantization table to use, dcTable and acTable are the Huffman tables for
00261  * the DC and AC coefficients respectively, dataBase, bitsLeftBase, and
00262  * bitsDataBase are for input stream state, and toZigZag is a pointer to
00263  * JPEG_ToZigZag or to its IWRAM copy.
00264  */
00265  
00266 static void JPEG_DecodeCoefficients (
00267     JPEG_FIXED_TYPE *dcLast, JPEG_FIXED_TYPE *zz, JPEG_FIXED_TYPE *quant,
00268     JPEG_HuffmanTable *dcTable, JPEG_HuffmanTable *acTable,
00269     const unsigned char **dataBase, unsigned int *bitsLeftBase,
00270     unsigned long int *bitsDataBase, const unsigned char *toZigZag)
00271 {
00272     unsigned bits_left = *bitsLeftBase, bits_data = *bitsDataBase; /* Input stream state. */
00273     const unsigned char *data = *dataBase; /* Input stream state. */
00274     int r, s, diff; /* Various temporary data variables. */
00275     int index = 1; /* The current zig-zagged index. */
00276     
00277     /* Clear all coefficients to zero. */
00278     {
00279         JPEG_FIXED_TYPE *ez = zz + JPEG_DCTSIZE2;
00280         do *-- ez = 0;
00281         while (ez > zz);
00282     }
00283     
00284     // Read the DC coefficient. 
00285     JPEG_BITS_CHECK ();
00286     JPEG_HuffmanTable_Decode (dcTable, s);
00287     JPEG_Value (s, diff);
00288     
00289     // Store the DC coefficient. 
00290     *dcLast += diff;
00291     zz [toZigZag [0]] = *dcLast * quant [0];
00292 
00293     while (1)
00294     {
00295         // Read a bits/run-length value. 
00296         JPEG_BITS_CHECK ();
00297         JPEG_HuffmanTable_Decode (acTable, s);
00298         r = s >> 4;
00299         s &= 15;
00300     
00301         // If there is a value at this cell +r, then read it. 
00302         if (s)
00303         {
00304             index += r;
00305             JPEG_Value (s, r);
00306             zz [toZigZag [index]] = r * quant [index];
00307             if (index == JPEG_DCTSIZE2 - 1)
00308                 break;
00309             index ++;
00310         }
00311         // Otherwise we skip 16 cells or finish up. 
00312         else
00313         {
00314             if (r != 15)
00315                 break;
00316             index += 16;
00317         }
00318     }
00319     
00320     // Restore state for the caller. 
00321     *bitsDataBase = bits_data;
00322     *bitsLeftBase = bits_left;
00323     *dataBase = data;
00324 }
00325 JPEG_FUNCTION_END (JPEG_DecodeCoefficients)
00326 
00327 /* Convert a chunk of YCbCr data to the output format.  YBlock, CbBlock,
00328  * and CrBlock are the pointers to the relevant chunks; each sample is
00329  * between -64 and 64, although out-of-range values are possible.
00330  * nHorzFactor and nVertFactor, where n is Y, Cb, and Cr, hold the
00331  * multipliers for each coordinate.  Shift right by horzMax and vertMax to
00332  * get the actual point to sample data from.  M211 is true if the
00333  * component factors satisfy a 2:1:1 relationship; this leads to a much
00334  * faster conversion if JPEG_FASTER_M211 is enabled.
00335  * out and outStride are the output pointers and the number of samples
00336  * in an output row.  Finally, ComponentRange is a pointer to the
00337  * JPEG_ComponentRange array.
00338  */
00339  
00340 static void JPEG_ConvertBlock (
00341     signed char *YBlock, signed char *CbBlock, signed char *CrBlock,
00342     int YHorzFactor, int YVertFactor, int CbHorzFactor, int CbVertFactor, int CrHorzFactor, int CrVertFactor, int horzMax, int vertMax,
00343     char M211, volatile JPEG_OUTPUT_TYPE *out, int outStride, const unsigned char *ComponentRange, const unsigned char *ComponentRangeG)
00344 {
00345     int px, py;
00346     
00347     /* Since we need to offset all indices into this anyway, we might as well do it once only. */
00348     ComponentRange += 32;
00349     ComponentRangeG += 64;      
00350     
00351 /* Do the faster 2:1:1 code if JPEG_FASTER_M211 is set and the image scan satisfies that relationship. */
00352 #if JPEG_FASTER_M211                
00353     if (M211)
00354     {
00355         /* Nothing complex here.  Because of its nature, we can do Cb and Cr
00356          * conversion only once for every four pixels.  This optimization is
00357          * done implicitly, using GCC's optimizer for gleaning the actual
00358          * advantage.
00359          */
00360          
00361         for (py = 0; py < 2 * JPEG_DCTSIZE; py += 2)
00362         {
00363             volatile JPEG_OUTPUT_TYPE *row = &out [outStride * py];
00364             volatile JPEG_OUTPUT_TYPE *rowEnd = row + JPEG_DCTSIZE * 2;
00365             
00366             for ( ; row < rowEnd; row += 2, YBlock += 2, CbBlock ++, CrBlock ++)
00367             {
00368                 int Cb = *CbBlock, Cr = *CrBlock;
00369                 JPEG_Convert (row [0], YBlock [0], Cb, Cr);
00370                 JPEG_Convert (row [1], YBlock [1], Cb, Cr);
00371                 JPEG_Convert (row [jpeg_width], YBlock [2 * JPEG_DCTSIZE + 0], Cb, Cr); // 240
00372                 JPEG_Convert (row [jpeg_width+1], YBlock [2 * JPEG_DCTSIZE + 1], Cb, Cr); // 241
00373             }
00374             
00375             YBlock += JPEG_DCTSIZE * 2;
00376         }
00377     }
00378 #else
00379     if (0) { }
00380 #endif /* JPEG_FASTER_M211 */
00381 
00382 /* Otherwise we fall back on generic code, if JPEG_HANDLE_ANY_FACTORS is set.
00383  * If it is not, then this function does nothing at all!
00384  */
00385 #if JPEG_HANDLE_ANY_FACTORS
00386     else for (py = 0; py < vertMax; py ++)
00387     {
00388         signed char *YScan = YBlock + (py * YVertFactor >> 8) * (horzMax * YHorzFactor >> 8);
00389         signed char *CbScan = CbBlock + (py * CbVertFactor >> 8) * (horzMax * CbHorzFactor >> 8);
00390         signed char *CrScan = CrBlock + (py * CrVertFactor >> 8) * (horzMax * CrHorzFactor >> 8);
00391         
00392         volatile JPEG_OUTPUT_TYPE *row = &out [outStride * py];
00393         
00394         for (px = 0; px < horzMax; px ++, row ++)
00395         {
00396             int Y = YScan [px * YHorzFactor >> 8];
00397             int Cb = CbScan [px * CbHorzFactor >> 8];
00398             int Cr = CrScan [px * CrHorzFactor >> 8];
00399             
00400             JPEG_Convert (*row, Y, Cb, Cr);
00401         }
00402     }
00403 #endif /* JPEG_HANDLE_ANY_FACTORS */
00404 
00405     /* Make sure all variables are referenced. */
00406     (void) YHorzFactor; (void) YVertFactor; (void) CbHorzFactor;
00407     (void) CbVertFactor; (void) CrHorzFactor; (void) CrVertFactor;
00408     (void) horzMax; (void) vertMax; (void) px; (void) py;
00409     (void) YBlock; (void) CbBlock; (void) CrBlock;
00410     (void) M211; (void) out; (void) outStride;
00411 }
00412 JPEG_FUNCTION_END (JPEG_ConvertBlock)
00413 
00414 /* Takes information discovered in JPEG_Decoder_ReadHeaders and loads the
00415  * image.  This is a public function; see gba-jpeg.h for more information on it.
00416  */
00417 int JPEG_Decoder_ReadImage (JPEG_Decoder *decoder, const unsigned char **dataBase, volatile JPEG_OUTPUT_TYPE *out, int outWidth, int outHeight)
00418 {
00419     JPEG_FrameHeader *frame = &decoder->frame; /* Pointer to the image's frame. */
00420     JPEG_ScanHeader *scan = &decoder->scan; /* Pointer to the image's scan. */
00421     int YHorzFactor = 0, YVertFactor = 0; /* Scaling factors for the Y component. */
00422     int CbHorzFactor = 1, CbVertFactor = 1; /* Scaling factors for the Cb component.  The default is important because it is used for greyscale images. */
00423     int CrHorzFactor = 1, CrVertFactor = 1; /* Scaling factors for the Cr component.  The default is important because it is used for greyscale images. */
00424     int horzMax = 0, vertMax = 0; /* The maximum horizontal and vertical scaling factors for the components. */
00425     JPEG_FrameHeader_Component *frameComponents [JPEG_MAXIMUM_COMPONENTS]; /* Pointers translating scan header components to frame header components. */
00426     JPEG_FrameHeader_Component *item, *itemEnd = frame->componentList + frame->componentCount; /* The frame header's components for loops. */
00427     JPEG_FIXED_TYPE dcLast [JPEG_MAXIMUM_COMPONENTS]; /* The last DC coefficient computed.  This is initialized to zeroes at the start and after a restart interval. */
00428     int c, bx, by, cx, cy; /* Various loop parameters. */
00429     int horzShift = 0; /* The right shift to use after multiplying by nHorzFactor to get the actual sample. */
00430     int vertShift = 0; /* The right shift to use after multiplying by nVertFactor to get the actual sample. */
00431     char M211 = 0; /* Whether this scan satisfies the 2:1:1 relationship, which leads to faster code. */
00432     const unsigned char *data = *dataBase; /* The input data pointer; this must be right at the start of scan data. */
00433     
00434     signed char blockBase [JPEG_DCTSIZE2 * JPEG_MAXIMUM_SCAN_COMPONENT_FACTORS]; /* Blocks that have been read and are alloted to YBlock, CbBlock, and CrBlock based on their scaling factors. */
00435     signed char *YBlock; /* Y component temporary block that holds samples for the MCU currently being decompressed. */
00436     signed char *CbBlock; /* Cb component temporary block that holds samples for the MCU currently being decompressed. */
00437     signed char *CrBlock; /* Cr component temporary block that holds samples for the MCU currently being decompressed. */
00438     
00439     JPEG_HuffmanTable acTableList [2]; /* The decompressed AC Huffman tables.  JPEG Baseline allows only two AC Huffman tables in a scan. */
00440     int acTableUse [2] = { -1, -1 }; /* The indices of the decompressed AC Huffman tables, or -1 if this table hasn't been used. */
00441     JPEG_HuffmanTable dcTableList [2]; /* The decompressed DC Huffman tables.  JPEG Baseline allows only two DC Huffman tables in a scan. */
00442     int dcTableUse [2] = { -1, -1 }; /* The indices of the decompressed DC Huffman tables, or -1 if this table hasn't been used. */
00443     int restartInterval = decoder->restartInterval; /* Number of blocks until the next restart. */
00444     
00445     /* Pointer to JPEG_ConvertBlock, which might be moved to IWRAM. */
00446     void (*ConvertBlock) (signed char *, signed char *, signed char *,
00447         int, int, int, int, int, int, int, int, char,
00448         volatile JPEG_OUTPUT_TYPE *, int, const unsigned char *, const unsigned char *)
00449         = &JPEG_ConvertBlock;
00450 
00451     /* Pointer to JPEG_IDCT_Columns, which might be moved to IWRAM. */
00452     void (*IDCT_Columns) (JPEG_FIXED_TYPE *) = &JPEG_IDCT_Columns;
00453 
00454     /* Pointer to JPEG_IDCT_Rows, which might be moved to IWRAM. */
00455     void (*IDCT_Rows) (const JPEG_FIXED_TYPE *, signed char *, int) = &JPEG_IDCT_Rows;
00456     
00457     /* Pointer to JPEG_DecodeCoefficients, which might be moved to IWRAM. */
00458     void (*DecodeCoefficients) (JPEG_FIXED_TYPE *, JPEG_FIXED_TYPE *, JPEG_FIXED_TYPE *, JPEG_HuffmanTable *,
00459         JPEG_HuffmanTable *, const unsigned char **, unsigned int *,
00460         unsigned long int *, const unsigned char *) = &JPEG_DecodeCoefficients;
00461     
00462     const unsigned char *ToZigZag = JPEG_ToZigZag; /* Pointer to JPEG_ToZigZag, which might be moved to IWRAM. */
00463     const unsigned char *ComponentRange = JPEG_ComponentRange; /* Pointer to JPEG_ComponentRange, which might be moved to IWRAM. */
00464     const unsigned char *ComponentRangeG = JPEG_ComponentRangeG; /* Pointer to JPEG_ComponentRange, which might be moved to IWRAM. */
00465       
00466     /* Start decoding bits. */
00467     JPEG_BITS_START ();
00468     
00469     /* The sum of all factors in the scan; this cannot be greater than 10 in JPEG Baseline. */
00470     int factorSum = 0;
00471 
00472     /* Find the maximum factors and the factors for each component. */    
00473     for (item = frame->componentList; item < itemEnd; item ++)
00474     {
00475         /* Find the opposing scan header component. */
00476         for (c = 0; ; c ++)
00477         {
00478             JPEG_ScanHeader_Component *sc;
00479 
00480             JPEG_Assert (c < scan->componentCount);
00481             sc = &scan->componentList [c];
00482             if (sc->selector != item->selector)
00483                 continue;
00484             
00485             /* Decompress the DC table if necessary. */
00486             if (sc->dcTable != dcTableUse [0] && sc->dcTable != dcTableUse [1])
00487             {
00488                 const unsigned char *tablePointer = decoder->dcTables [sc->dcTable];
00489                 
00490                 if (dcTableUse [0] == -1)
00491                     dcTableUse [0] = sc->dcTable, JPEG_HuffmanTable_Read (&dcTableList [0], &tablePointer);
00492                 else if (dcTableUse [1] == -1)
00493                     dcTableUse [1] = sc->dcTable, JPEG_HuffmanTable_Read (&dcTableList [1], &tablePointer);
00494                 else
00495                     JPEG_Assert (0);
00496             }
00497             
00498             /* Decompress the AC table if necessary. */
00499             if (sc->acTable != acTableUse [0] && sc->acTable != acTableUse [1])
00500             {
00501                 const unsigned char *tablePointer = decoder->acTables [sc->acTable];
00502                 
00503                 if (acTableUse [0] == -1)
00504                     acTableUse [0] = sc->acTable, JPEG_HuffmanTable_Read (&acTableList [0], &tablePointer);
00505                 else if (acTableUse [1] == -1)
00506                     acTableUse [1] = sc->acTable, JPEG_HuffmanTable_Read (&acTableList [1], &tablePointer);
00507                 else
00508                     JPEG_Assert (0);
00509             }
00510             
00511             frameComponents [c] = item;
00512             break;
00513         }
00514         
00515         /* Add the sum for a later assertion test. */
00516         factorSum += item->horzFactor * item->vertFactor;
00517         
00518         /* Adjust the maximum horizontal and vertical scaling factors as necessary. */
00519         if (item->horzFactor > horzMax)
00520             horzMax = item->horzFactor;
00521         if (item->vertFactor > vertMax)
00522             vertMax = item->vertFactor;
00523             
00524         /* Update the relevant component scaling factors if necessary. */
00525         if (item->selector == 1)
00526         {
00527             YHorzFactor = item->horzFactor;
00528             YVertFactor = item->vertFactor;
00529         }
00530         else if (item->selector == 2)
00531         {
00532             CbHorzFactor = item->horzFactor;
00533             CbVertFactor = item->vertFactor;
00534         }
00535         else if (item->selector == 3)
00536         {
00537             CrHorzFactor = item->horzFactor;
00538             CrVertFactor = item->vertFactor;
00539         }
00540     }
00541    
00542     /* Ensure that we have enough memory for these factors. */
00543     JPEG_Assert (factorSum < JPEG_MAXIMUM_SCAN_COMPONENT_FACTORS);
00544      
00545     /* Split up blockBase according to the components. */
00546     YBlock = blockBase;
00547     CbBlock = YBlock + YHorzFactor * YVertFactor * JPEG_DCTSIZE2;
00548     CrBlock = CbBlock + CbHorzFactor * CbVertFactor * JPEG_DCTSIZE2;
00549     
00550     /* Compute the right shift to be done after multiplying against the scaling factor. */
00551     if (horzMax == 1) horzShift = 8;
00552     else if (horzMax == 2) horzShift = 7;
00553     else if (horzMax == 4) horzShift = 6;
00554     
00555     /* Compute the right shift to be done after multiplying against the scaling factor. */
00556     if (vertMax == 1) vertShift = 8;
00557     else if (vertMax == 2) vertShift = 7;
00558     else if (vertMax == 4) vertShift = 6;
00559 
00560     /* Adjust the scaling factors for our parameters. */    
00561     YHorzFactor <<= horzShift;
00562     YVertFactor <<= vertShift;
00563     CbHorzFactor <<= horzShift;
00564     CbVertFactor <<= vertShift;
00565     CrHorzFactor <<= horzShift;
00566     CrVertFactor <<= vertShift;
00567     
00568     /* Clear the Cb channel for potential grayscale. */
00569     {
00570         signed char *e = CbBlock + JPEG_DCTSIZE2;
00571         
00572         do *-- e = 0;
00573         while (e > CbBlock);
00574     }
00575     
00576     /* Clear the Cr channel for potential grayscale. */
00577     {
00578         signed char *e = CrBlock + JPEG_DCTSIZE2;
00579         
00580         do *-- e = 0;
00581         while (e > CrBlock);
00582     }
00583 
00584 /* Compute whether this satisfies the sped up 2:1:1 relationship. */
00585 #if JPEG_FASTER_M211
00586     if (YHorzFactor == 256 && YVertFactor == 256 && CbHorzFactor == 128 && CbVertFactor == 128 && CrHorzFactor == 128 && CrVertFactor == 128)
00587         M211 = 1;
00588 #endif /* JPEG_FASTER_M211 */
00589         
00590     /* Clear the DC parameters. */
00591     for (c = 0; c < JPEG_MAXIMUM_COMPONENTS; c ++)
00592         dcLast [c] = 0;
00593     
00594     /* Now run over each MCU horizontally, then vertically. */
00595     for (by = 0; by < frame->height; by += vertMax * JPEG_DCTSIZE)
00596     {
00597         for (bx = 0; bx < frame->width; bx += horzMax * JPEG_DCTSIZE)
00598         {
00599             /* Read the components for the MCU. */
00600             for (c = 0; c < scan->componentCount; c ++)
00601             {
00602                 JPEG_ScanHeader_Component *sc = &scan->componentList [c];
00603                 JPEG_FrameHeader_Component *fc = frameComponents [c];
00604                 JPEG_HuffmanTable *dcTable, *acTable;
00605                 JPEG_FIXED_TYPE *quant = decoder->quantTables [fc->quantTable];
00606                 int stride = fc->horzFactor * JPEG_DCTSIZE;
00607                 signed char *chunk = 0;
00608                 
00609                 dcTable = &dcTableList [sc->dcTable == dcTableUse [1] ? 1 : 0];
00610                 acTable = &acTableList [sc->acTable == acTableUse [1] ? 1 : 0];
00611                 
00612                 /* Compute the output chunk. */
00613                 if (fc->selector == 1)
00614                     chunk = YBlock;
00615                 else if (fc->selector == 2)
00616                     chunk = CbBlock;
00617                 else if (fc->selector == 3)
00618                     chunk = CrBlock;
00619                     
00620                 for (cy = 0; cy < fc->vertFactor * JPEG_DCTSIZE; cy += JPEG_DCTSIZE)
00621                 {
00622                     for (cx = 0; cx < fc->horzFactor * JPEG_DCTSIZE; cx += JPEG_DCTSIZE)
00623                     {
00624                         int start = cx + cy * stride;
00625                         JPEG_FIXED_TYPE zz [JPEG_DCTSIZE2];
00626 
00627                         /* Decode coefficients. */
00628                         DecodeCoefficients (&dcLast [c], zz, quant, dcTable, acTable, &data, &bits_left, &bits_data, ToZigZag);
00629 
00630                         /* Perform an IDCT if this component will contribute to the image. */
00631                         if (chunk)
00632                         {
00633                             IDCT_Columns (zz);
00634                             IDCT_Rows (zz, chunk + start, stride);
00635                         }
00636                     }
00637                 }
00638             }
00639             
00640             /* Check that our block will be in-range; this should actually use clamping. */
00641             if (bx + horzMax * JPEG_DCTSIZE > outWidth || by + vertMax * JPEG_DCTSIZE > outHeight)
00642                 continue;
00643                 
00644             /* Convert our block from YCbCr to the output. */
00645             ConvertBlock (YBlock, CbBlock, CrBlock,
00646                 YHorzFactor, YVertFactor, CbHorzFactor, CbVertFactor, CrHorzFactor, CrVertFactor,
00647                 horzMax * JPEG_DCTSIZE, vertMax * JPEG_DCTSIZE, M211, out + bx + by * outWidth, outWidth, ComponentRange, ComponentRangeG);
00648             
00649             /* Handle the restart interval. */
00650             if (decoder->restartInterval && --restartInterval == 0)
00651             {
00652                 restartInterval = decoder->restartInterval;
00653                 JPEG_BITS_REWIND ();
00654                 if (((data [0] << 8) | data [1]) == JPEG_Marker_EOI)
00655                     goto finish;
00656                 JPEG_Assert (data [0] == 0xFF && (data [1] >= 0xD0 && data [1] <= 0xD7));
00657                 for (c = 0; c < JPEG_MAXIMUM_COMPONENTS; c ++)
00658                     dcLast [c] = 0;
00659                 data += 2;
00660             }
00661         }
00662     }
00663    
00664 finish:
00665     /* Make sure we read an EOI marker. */ 
00666     JPEG_BITS_REWIND ();
00667     JPEG_Assert (((data [0] << 8) | data [1]) == JPEG_Marker_EOI);
00668     data += 2;
00669     
00670     /* Clear up and return success. */
00671     *dataBase = data;
00672     return 1;
00673 }
00674 
00675 /* Read an JPEG_Marker_SOFn marker into frame.  This expects to start
00676  * processing immediately after the marker.
00677  */
00678 int JPEG_FrameHeader_Read (JPEG_FrameHeader *frame, const unsigned char **dataBase, JPEG_Marker marker)
00679 {
00680     const unsigned char *data = *dataBase;
00681     unsigned short length = (data [0] << 8) | data [1];
00682     int index;
00683 
00684     (void) length;
00685     JPEG_Assert (length >= 8);        
00686     data += 2; /* Skip the length. */
00687     frame->marker = marker;
00688     frame->encoding = (marker >= 0xFFC0 && marker <= 0xFFC7) ? 0 : 1;
00689     frame->differential = !(marker >= 0xFFC0 && marker <= 0xFFC3 && marker >= 0xFFC8 && marker <= 0xFFCB);
00690     
00691     frame->precision = *data ++;
00692     frame->height = (data [0] << 8) | data [1]; data += 2;
00693     frame->width = (data [0] << 8) | data [1]; data += 2;
00694         jpeg_width = frame->width;
00695     frame->componentCount = *data ++;
00696     
00697     JPEG_Assert (frame->precision == 8);
00698     JPEG_Assert (frame->componentCount <= JPEG_MAXIMUM_COMPONENTS);
00699     JPEG_Assert (length == 8 + 3 * frame->componentCount);
00700     
00701     /* Read the frame components. */
00702     for (index = 0; index < frame->componentCount; index ++)
00703     {
00704         JPEG_FrameHeader_Component *c = &frame->componentList [index];
00705         unsigned char pair;
00706         
00707         c->selector = *data ++;
00708         pair = *data ++;
00709         c->horzFactor = pair >> 4;
00710         c->vertFactor = pair & 15;
00711         c->quantTable = *data ++;
00712         
00713         JPEG_Assert (c->horzFactor == 1 || c->horzFactor == 2 || c->horzFactor == 4);
00714         JPEG_Assert (c->vertFactor == 1 || c->vertFactor == 2 || c->vertFactor == 4);
00715         JPEG_Assert (c->quantTable <= 3);
00716     }
00717     
00718     *dataBase = data;
00719     return 1;
00720 }
00721 
00722 /* Read a JPEG_Marker_SOS marker into scan.  This expects to start processing
00723  * immediately after the marker.
00724  */
00725 int JPEG_ScanHeader_Read (JPEG_ScanHeader *scan, const unsigned char **dataBase)
00726 {
00727     const unsigned char *data = *dataBase;
00728     unsigned short length = (data [0] << 8) | data [1];
00729     JPEG_ScanHeader_Component *c, *cEnd;
00730     unsigned char pair;
00731  
00732     (void) length;
00733     JPEG_Assert (length >= 6);
00734     data += 2; /* Skip the length. */
00735     scan->componentCount = *data ++;
00736     
00737     JPEG_Assert (scan->componentCount <= JPEG_MAXIMUM_COMPONENTS);
00738     JPEG_Assert (length == 6 + 2 * scan->componentCount);
00739 
00740     /* Read the scan components. */    
00741     for (c = scan->componentList, cEnd = c + scan->componentCount; c < cEnd; c ++)
00742     {
00743         c->selector = *data ++;
00744         pair = *data ++;
00745         c->dcTable = pair >> 4;
00746         c->acTable = pair & 15;
00747         
00748         JPEG_Assert (c->dcTable < 4);
00749         JPEG_Assert (c->acTable < 4);
00750     }
00751     
00752     /* Read the spectral and approximation footers, which are used for
00753      * progressive.
00754      */
00755      
00756     scan->spectralStart = *data ++;
00757     scan->spectralEnd = *data ++;
00758     JPEG_Assert (scan->spectralStart <= 63);
00759     JPEG_Assert (scan->spectralEnd <= 63);
00760     pair = *data ++;
00761     scan->successiveApproximationBitPositionHigh = pair >> 4;
00762     scan->successiveApproximationBitPositionLow = pair & 15;
00763     JPEG_Assert (scan->successiveApproximationBitPositionHigh <= 13);
00764     JPEG_Assert (scan->successiveApproximationBitPositionLow <= 15);
00765     
00766     *dataBase = data;
00767     return 1;
00768 }
00769 
00770 /* Read all headers from the very start of the JFIF stream to right after the
00771  * SOS marker.
00772  */
00773  
00774 int JPEG_Decoder_ReadHeaders (JPEG_Decoder *decoder, const unsigned char **dataBase)
00775 {
00776     const unsigned char *data = *dataBase;
00777     JPEG_Marker marker;
00778     int c;
00779  
00780     /* Initialize state and assure that this is a JFIF file. */   
00781     decoder->restartInterval = 0;
00782     JPEG_Assert (((data [0] << 8) | data [1]) == JPEG_Marker_SOI);
00783     data += 2;
00784     
00785     /* Start reading every marker as it comes in. */
00786     while (1)
00787     {
00788         marker = (JPEG_Marker)((data [0] << 8) | data [1]);
00789         data += 2;
00790         
00791         switch (marker)
00792         {
00793             /* This block is just skipped over. */
00794             case JPEG_Marker_APP0:
00795             case JPEG_Marker_APP1:
00796             case JPEG_Marker_APP2:
00797             case JPEG_Marker_APP3:
00798             case JPEG_Marker_APP4:
00799             case JPEG_Marker_APP5:
00800             case JPEG_Marker_APP6:
00801             case JPEG_Marker_APP7:
00802             case JPEG_Marker_APP8:
00803             case JPEG_Marker_APP9:
00804             case JPEG_Marker_APP10:
00805             case JPEG_Marker_APP11:
00806             case JPEG_Marker_APP12:
00807             case JPEG_Marker_APP13:
00808             case JPEG_Marker_APP14:
00809             case JPEG_Marker_APP15:
00810             case JPEG_Marker_COM:
00811                 data += (data [0] << 8) | data [1];
00812                 break;
00813             
00814             case JPEG_Marker_DHT: /* Define Huffman table.  We just skip it for later decompression. */
00815             {
00816                 unsigned short length = (data [0] << 8) | data [1];
00817                 const unsigned char *end = data + length;
00818                 
00819                 JPEG_Assert (length >= 2);
00820                 data += 2;
00821                 while (data < end)
00822                 {
00823                     unsigned char pair, type, slot;
00824                     
00825                     pair = *data ++;
00826                     type = pair >> 4;
00827                     slot = pair & 15;
00828                     
00829                     JPEG_Assert (type == 0 || type == 1);
00830                     JPEG_Assert (slot <= 15);
00831                     
00832                     if (type == 0)
00833                         decoder->dcTables [slot] = data;
00834                     else
00835                         decoder->acTables [slot] = data;
00836                         
00837                     if (!JPEG_HuffmanTable_Skip (&data))
00838                         return 0;
00839                 }
00840                 
00841                 JPEG_Assert (data == end);
00842                 break;
00843             }
00844             
00845             case JPEG_Marker_DQT: /* Define quantization table. */
00846             {
00847                 unsigned short length = (data [0] << 8) | data [1];
00848                 const unsigned char *end = data + length;
00849                 int col, row;
00850                 JPEG_FIXED_TYPE *s;
00851                 
00852                 JPEG_Assert (length >= 2);
00853                 data += 2;
00854                 
00855                 while (data < end)
00856                 {
00857                     int pair, slot, precision;
00858                     
00859                     pair = *data ++;
00860                     precision = pair >> 4;
00861                     slot = pair & 15;
00862                     
00863                     JPEG_Assert (precision == 0); /* Only allow 8-bit. */
00864                     JPEG_Assert (slot < 4); /* Ensure the slot is in-range. */
00865                     JPEG_Assert (data + 64 <= end); /* Ensure it's the right size. */
00866                     
00867                     s = decoder->quantTables [slot];
00868                    
00869                     for (c = 0; c < JPEG_DCTSIZE2; c ++)
00870                         s [c] = JPEG_ITOFIX (*data ++);
00871                     
00872                     /* Multiply against the AAN factors. */
00873                     for (row = 0; row < JPEG_DCTSIZE; row ++)
00874                         for (col = 0; col < JPEG_DCTSIZE; col ++)
00875                         {
00876                             JPEG_FIXED_TYPE *item = &s [col + row * JPEG_DCTSIZE];
00877                             
00878                             *item = JPEG_FIXMUL (*item, JPEG_AANScaleFactor [JPEG_ToZigZag [row * JPEG_DCTSIZE + col]]);
00879                         }
00880                 }
00881                 
00882                 JPEG_Assert (data == end); /* Ensure we've finished it. */
00883                 break;
00884             }
00885         
00886             case JPEG_Marker_DRI: /* Define restart interval. */
00887                 JPEG_Assert (((data [0] << 8) | data [1]) == 4); /* Check the length. */
00888                 decoder->restartInterval = (data [2] << 8) | data [3];
00889                 data += 4;
00890                 break;
00891             
00892             case JPEG_Marker_SOF0: /* Start of Frame: Baseline Sequential Huffman. */
00893                 if (!JPEG_FrameHeader_Read (&decoder->frame, &data, marker))
00894                     return 0;
00895                 break;
00896             
00897             case JPEG_Marker_SOS: /* Start of scan, immediately followed by the image. */
00898                 if (!JPEG_ScanHeader_Read (&decoder->scan, &data))
00899                     return 0;
00900                 *dataBase = data;
00901                 return 1;
00902                 
00903             default: /* No known marker of this type. */
00904                 JPEG_Assert (0);
00905                 break;
00906         }
00907     }
00908 }
00909 
00910 /* Skip past a Huffman table section.  This expects to be called after reading
00911  * the DHT marker and the type/slot pair.
00912  */
00913 int JPEG_HuffmanTable_Skip (const unsigned char **dataBase)
00914 {
00915     const unsigned char *data = *dataBase;
00916     int c, total = 16;
00917     
00918     for (c = 0; c < 16; c ++)
00919         total += *data ++;
00920     *dataBase += total;
00921     return 1;
00922 }
00923 
00924 /* Decode a Huffman table and initialize its data.  This expects to be called
00925  * after the DHT marker and the type/slot pair.
00926  */
00927 int JPEG_HuffmanTable_Read (JPEG_HuffmanTable *huffmanTable, const unsigned char **dataBase)
00928 {
00929     const unsigned char *data = *dataBase;
00930     const unsigned char *bits;
00931     int huffcode [256];
00932     unsigned char huffsize [256];
00933     int total = 0;
00934     int c;
00935     
00936     bits = data;
00937     for (c = 0; c < 16; c ++)
00938         total += *data ++;
00939     huffmanTable->huffval = data;
00940     data += total;
00941     
00942     /*void GenerateSizeTable ()*/
00943     {
00944         int k = 0, i = 1, j = 1;
00945         
00946         do
00947         {
00948             while (j ++ <= bits [i - 1])
00949                 huffsize [k ++] = i;
00950             i ++;
00951             j = 1;
00952         }
00953         while (i <= 16);
00954             
00955         huffsize [k] = 0;
00956     }
00957     
00958     /*void GenerateCodeTable ()*/
00959     {
00960         int k = 0, code = 0, si = huffsize [0];
00961 
00962         while (1)
00963         {            
00964             do huffcode [k ++] = code ++;
00965             while (huffsize [k] == si);
00966                 
00967             if (huffsize [k] == 0)
00968                 break;
00969             
00970             do code <<= 1, si ++;
00971             while (huffsize [k] != si);
00972         }
00973     }
00974     
00975     /*void DecoderTables ()*/
00976     {
00977         int i = 0, j = 0;
00978         
00979         while (1)
00980         {
00981             if (i >= 16)
00982                 break;
00983             if (bits [i] == 0)
00984                 huffmanTable->maxcode [i] = -1;
00985             else
00986             {
00987                 huffmanTable->valptr [i] = &huffmanTable->huffval [j - huffcode [j]];
00988                 j += bits [i];
00989                 huffmanTable->maxcode [i] = huffcode [j - 1];
00990             }
00991             i ++;
00992         }
00993     }
00994     
00995     /*void GenerateLookahead ()*/
00996     {
00997         int l, i, p, c, ctr;
00998         
00999         for (c = 0; c < 256; c ++)
01000             huffmanTable->look_nbits [c] = 0;
01001             
01002         p = 0;
01003         for (l = 1; l <= 8; l ++)
01004         {
01005             for (i = 1; i <= bits [l - 1]; i ++, p ++)
01006             {
01007                 int lookbits = huffcode [p] << (8 - l);
01008                 
01009                 for (ctr = 1 << (8 - l); ctr > 0; ctr --)
01010                 {
01011                     huffmanTable->look_nbits [lookbits] = l;
01012                     huffmanTable->look_sym [lookbits] = huffmanTable->huffval [p];
01013                     lookbits ++;
01014                 }
01015             }
01016         }
01017     }
01018     
01019     *dataBase = data;
01020     return 1;
01021 }
01022 
01023 /* Perform the two steps necessary to decompress a JPEG image.
01024  * Nothing fancy about it.
01025  */
01026 int JPEG_DecompressImage (const unsigned char *data, volatile JPEG_OUTPUT_TYPE *out, int outWidth, int outHeight)
01027 {
01028     JPEG_Decoder decoder;
01029 
01030     if (!JPEG_Decoder_ReadHeaders (&decoder, &data))
01031         return 0;
01032     if (!JPEG_Decoder_ReadImage (&decoder, &data, out, outWidth, outHeight))
01033         return 0;
01034 
01035     return 1;
01036 }
01037 
01038 /* Return whether this code is a JPEG file.  Unfortunately it will incorrectly
01039  * match variants such as JPEG 2000 and JPEG-LS.  A better function would
01040  * skip known markers until it reaches an unknown marker or a handled
01041  * SOFn.
01042  */
01043  
01044 int JPEG_Match (const unsigned char *data, int length)
01045 {
01046     if (length == 0) return 0;
01047     if (data [0] != 0xFF) return 0;
01048     if (length == 1) return 1;
01049     if (data [1] != 0xD8) return 0;
01050     if (length == 2) return 1;
01051     return 1;
01052     if (data [2] != 0xFF) return 0;
01053     if (length == 3) return 1;
01054     if (data [3] < 0xC0 || data [3] > 0xCF) return 0;
01055     if (data [3] == 0xC0) return 1;
01056     return 0;
01057 }

Generated on Wed Dec 12 23:46:38 2007 for XFlib by  doxygen 1.5.4