00001
00002 #include "xf_jpeg.h"
00003 #include "xf_bg.h"
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00033 #define JPEG_FUNCTION_END(NAME)
00034
00035
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
00049
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
00071
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
00086
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
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
00120 goto skipFirstCheckb;
00121
00122 for ( ; zz < ez; zz ++)
00123 {
00124
00125
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
00179
00180
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
00219
00220
00221
00222
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
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
00245
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
00258
00259
00260
00261
00262
00263
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;
00273 const unsigned char *data = *dataBase;
00274 int r, s, diff;
00275 int index = 1;
00276
00277
00278 {
00279 JPEG_FIXED_TYPE *ez = zz + JPEG_DCTSIZE2;
00280 do *-- ez = 0;
00281 while (ez > zz);
00282 }
00283
00284
00285 JPEG_BITS_CHECK ();
00286 JPEG_HuffmanTable_Decode (dcTable, s);
00287 JPEG_Value (s, diff);
00288
00289
00290 *dcLast += diff;
00291 zz [toZigZag [0]] = *dcLast * quant [0];
00292
00293 while (1)
00294 {
00295
00296 JPEG_BITS_CHECK ();
00297 JPEG_HuffmanTable_Decode (acTable, s);
00298 r = s >> 4;
00299 s &= 15;
00300
00301
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
00312 else
00313 {
00314 if (r != 15)
00315 break;
00316 index += 16;
00317 }
00318 }
00319
00320
00321 *bitsDataBase = bits_data;
00322 *bitsLeftBase = bits_left;
00323 *dataBase = data;
00324 }
00325 JPEG_FUNCTION_END (JPEG_DecodeCoefficients)
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
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
00348 ComponentRange += 32;
00349 ComponentRangeG += 64;
00350
00351
00352 #if JPEG_FASTER_M211
00353 if (M211)
00354 {
00355
00356
00357
00358
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);
00372 JPEG_Convert (row [jpeg_width+1], YBlock [2 * JPEG_DCTSIZE + 1], Cb, Cr);
00373 }
00374
00375 YBlock += JPEG_DCTSIZE * 2;
00376 }
00377 }
00378 #else
00379 if (0) { }
00380 #endif
00381
00382
00383
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
00404
00405
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
00415
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;
00420 JPEG_ScanHeader *scan = &decoder->scan;
00421 int YHorzFactor = 0, YVertFactor = 0;
00422 int CbHorzFactor = 1, CbVertFactor = 1;
00423 int CrHorzFactor = 1, CrVertFactor = 1;
00424 int horzMax = 0, vertMax = 0;
00425 JPEG_FrameHeader_Component *frameComponents [JPEG_MAXIMUM_COMPONENTS];
00426 JPEG_FrameHeader_Component *item, *itemEnd = frame->componentList + frame->componentCount;
00427 JPEG_FIXED_TYPE dcLast [JPEG_MAXIMUM_COMPONENTS];
00428 int c, bx, by, cx, cy;
00429 int horzShift = 0;
00430 int vertShift = 0;
00431 char M211 = 0;
00432 const unsigned char *data = *dataBase;
00433
00434 signed char blockBase [JPEG_DCTSIZE2 * JPEG_MAXIMUM_SCAN_COMPONENT_FACTORS];
00435 signed char *YBlock;
00436 signed char *CbBlock;
00437 signed char *CrBlock;
00438
00439 JPEG_HuffmanTable acTableList [2];
00440 int acTableUse [2] = { -1, -1 };
00441 JPEG_HuffmanTable dcTableList [2];
00442 int dcTableUse [2] = { -1, -1 };
00443 int restartInterval = decoder->restartInterval;
00444
00445
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
00452 void (*IDCT_Columns) (JPEG_FIXED_TYPE *) = &JPEG_IDCT_Columns;
00453
00454
00455 void (*IDCT_Rows) (const JPEG_FIXED_TYPE *, signed char *, int) = &JPEG_IDCT_Rows;
00456
00457
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;
00463 const unsigned char *ComponentRange = JPEG_ComponentRange;
00464 const unsigned char *ComponentRangeG = JPEG_ComponentRangeG;
00465
00466
00467 JPEG_BITS_START ();
00468
00469
00470 int factorSum = 0;
00471
00472
00473 for (item = frame->componentList; item < itemEnd; item ++)
00474 {
00475
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
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
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
00516 factorSum += item->horzFactor * item->vertFactor;
00517
00518
00519 if (item->horzFactor > horzMax)
00520 horzMax = item->horzFactor;
00521 if (item->vertFactor > vertMax)
00522 vertMax = item->vertFactor;
00523
00524
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
00543 JPEG_Assert (factorSum < JPEG_MAXIMUM_SCAN_COMPONENT_FACTORS);
00544
00545
00546 YBlock = blockBase;
00547 CbBlock = YBlock + YHorzFactor * YVertFactor * JPEG_DCTSIZE2;
00548 CrBlock = CbBlock + CbHorzFactor * CbVertFactor * JPEG_DCTSIZE2;
00549
00550
00551 if (horzMax == 1) horzShift = 8;
00552 else if (horzMax == 2) horzShift = 7;
00553 else if (horzMax == 4) horzShift = 6;
00554
00555
00556 if (vertMax == 1) vertShift = 8;
00557 else if (vertMax == 2) vertShift = 7;
00558 else if (vertMax == 4) vertShift = 6;
00559
00560
00561 YHorzFactor <<= horzShift;
00562 YVertFactor <<= vertShift;
00563 CbHorzFactor <<= horzShift;
00564 CbVertFactor <<= vertShift;
00565 CrHorzFactor <<= horzShift;
00566 CrVertFactor <<= vertShift;
00567
00568
00569 {
00570 signed char *e = CbBlock + JPEG_DCTSIZE2;
00571
00572 do *-- e = 0;
00573 while (e > CbBlock);
00574 }
00575
00576
00577 {
00578 signed char *e = CrBlock + JPEG_DCTSIZE2;
00579
00580 do *-- e = 0;
00581 while (e > CrBlock);
00582 }
00583
00584
00585 #if JPEG_FASTER_M211
00586 if (YHorzFactor == 256 && YVertFactor == 256 && CbHorzFactor == 128 && CbVertFactor == 128 && CrHorzFactor == 128 && CrVertFactor == 128)
00587 M211 = 1;
00588 #endif
00589
00590
00591 for (c = 0; c < JPEG_MAXIMUM_COMPONENTS; c ++)
00592 dcLast [c] = 0;
00593
00594
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
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
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
00628 DecodeCoefficients (&dcLast [c], zz, quant, dcTable, acTable, &data, &bits_left, &bits_data, ToZigZag);
00629
00630
00631 if (chunk)
00632 {
00633 IDCT_Columns (zz);
00634 IDCT_Rows (zz, chunk + start, stride);
00635 }
00636 }
00637 }
00638 }
00639
00640
00641 if (bx + horzMax * JPEG_DCTSIZE > outWidth || by + vertMax * JPEG_DCTSIZE > outHeight)
00642 continue;
00643
00644
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
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
00666 JPEG_BITS_REWIND ();
00667 JPEG_Assert (((data [0] << 8) | data [1]) == JPEG_Marker_EOI);
00668 data += 2;
00669
00670
00671 *dataBase = data;
00672 return 1;
00673 }
00674
00675
00676
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;
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
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
00723
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;
00735 scan->componentCount = *data ++;
00736
00737 JPEG_Assert (scan->componentCount <= JPEG_MAXIMUM_COMPONENTS);
00738 JPEG_Assert (length == 6 + 2 * scan->componentCount);
00739
00740
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
00753
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
00771
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
00781 decoder->restartInterval = 0;
00782 JPEG_Assert (((data [0] << 8) | data [1]) == JPEG_Marker_SOI);
00783 data += 2;
00784
00785
00786 while (1)
00787 {
00788 marker = (JPEG_Marker)((data [0] << 8) | data [1]);
00789 data += 2;
00790
00791 switch (marker)
00792 {
00793
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:
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:
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);
00864 JPEG_Assert (slot < 4);
00865 JPEG_Assert (data + 64 <= end);
00866
00867 s = decoder->quantTables [slot];
00868
00869 for (c = 0; c < JPEG_DCTSIZE2; c ++)
00870 s [c] = JPEG_ITOFIX (*data ++);
00871
00872
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);
00883 break;
00884 }
00885
00886 case JPEG_Marker_DRI:
00887 JPEG_Assert (((data [0] << 8) | data [1]) == 4);
00888 decoder->restartInterval = (data [2] << 8) | data [3];
00889 data += 4;
00890 break;
00891
00892 case JPEG_Marker_SOF0:
00893 if (!JPEG_FrameHeader_Read (&decoder->frame, &data, marker))
00894 return 0;
00895 break;
00896
00897 case JPEG_Marker_SOS:
00898 if (!JPEG_ScanHeader_Read (&decoder->scan, &data))
00899 return 0;
00900 *dataBase = data;
00901 return 1;
00902
00903 default:
00904 JPEG_Assert (0);
00905 break;
00906 }
00907 }
00908 }
00909
00910
00911
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
00925
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
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
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
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
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
01024
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
01039
01040
01041
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 }