📄 jpeg.c
字号:
int k = 0, i = 1, j = 1;
do
{
while (j ++ <= bits [i - 1])
huffsize [k ++] = i;
i ++;
j = 1;
}
while (i <= 16);
huffsize [k] = 0;
}
/*void GenerateCodeTable ()*/
{
int k = 0, code = 0, si = huffsize [0];
while (1)
{
do huffcode [k ++] = code ++;
while (huffsize [k] == si);
if (huffsize [k] == 0)
break;
do code <<= 1, si ++;
while (huffsize [k] != si);
}
}
/*void DecoderTables ()*/
{
int i = 0, j = 0;
while (1)
{
if (i >= 16)
break;
if (bits [i] == 0)
huffmanTable->maxcode [i] = -1;
else
{
huffmanTable->valptr [i] = &huffmanTable->huffval [j - huffcode [j]];
j += bits [i];
huffmanTable->maxcode [i] = huffcode [j - 1];
}
i ++;
}
}
/*void GenerateLookahead ()*/
{
int l, i, p, c, ctr;
for (c = 0; c < 256; c ++)
huffmanTable->look_nbits [c] = 0;
p = 0;
for (l = 1; l <= 8; l ++)
{
for (i = 1; i <= bits [l - 1]; i ++, p ++)
{
int lookbits = huffcode [p] << (8 - l);
for (ctr = 1 << (8 - l); ctr > 0; ctr --)
{
huffmanTable->look_nbits [lookbits] = l;
huffmanTable->look_sym [lookbits] = huffmanTable->huffval [p];
lookbits ++;
}
}
}
}
*dataBase = data;
return 1;
}
/* Skip past a Huffman table section. This expects to be called after reading
* the DHT marker and the type/slot pair.
*/
int JPEG_HuffmanTable_Skip (const unsigned char **dataBase)
{
const unsigned char *data = *dataBase;
int c, total = 16;
for (c = 0; c < 16; c ++)
total += *data ++;
*dataBase += total;
return 1;
}
/* Takes information discovered in JPEG_Decoder_ReadHeaders and loads the
* image. This is a public function; see gba-jpeg.h for more information on it.
*/
int JPEG_Decoder_ReadImage (JPEG_Decoder *decoder, const unsigned char **dataBase, signed char *out, int outWidth, int outHeight)
{
JPEG_FrameHeader *frame = &decoder->frame; /* Pointer to the image's frame. */
JPEG_ScanHeader *scan = &decoder->scan; /* Pointer to the image's scan. */
int YHorzFactor = 0, YVertFactor = 0; /* Scaling factors for the Y component. */
int CbHorzFactor = 1, CbVertFactor = 1; /* Scaling factors for the Cb component. The default is important because it is used for greyscale images. */
int CrHorzFactor = 1, CrVertFactor = 1; /* Scaling factors for the Cr component. The default is important because it is used for greyscale images. */
int horzMax = 0, vertMax = 0; /* The maximum horizontal and vertical scaling factors for the components. */
JPEG_FrameHeader_Component *frameComponents [JPEG_MAXIMUM_COMPONENTS]; /* Pointers translating scan header components to frame header components. */
JPEG_FrameHeader_Component *item, *itemEnd = frame->componentList + frame->componentCount; /* The frame header's components for loops. */
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. */
int c, bx, by, cx, cy, ix, iy, ii; /* Various loop parameters. */
int horzShift = 0; /* The right shift to use after multiplying by nHorzFactor to get the actual sample. */
int vertShift = 0; /* The right shift to use after multiplying by nVertFactor to get the actual sample. */
const unsigned char *data = *dataBase; /* The input data pointer; this must be right at the start of scan data. */
static 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. */
signed char *YBlock; /* Y component temporary block that holds samples for the MCU currently being decompressed. */
signed char *CbBlock; /* Cb component temporary block that holds samples for the MCU currently being decompressed. */
signed char *CrBlock; /* Cr component temporary block that holds samples for the MCU currently being decompressed. */
static JPEG_HuffmanTable acTableList [2]; /* The decompressed AC Huffman tables. JPEG Baseline allows only two AC Huffman tables in a scan. */
static JPEG_HuffmanTable dcTableList [2]; /* The decompressed DC Huffman tables. JPEG Baseline allows only two DC Huffman tables in a scan. */
int acTableUse [2] = { -1, -1 }; /* The indices of the decompressed AC Huffman tables, or -1 if this table hasn't been used. */
int dcTableUse [2] = { -1, -1 }; /* The indices of the decompressed DC Huffman tables, or -1 if this table hasn't been used. */
int restartInterval = decoder->restartInterval; /* Number of blocks until the next restart. */
/* Pointer to JPEG_IDCT_Columns*/
void (*IDCT_Columns) (JPEG_FIXED_TYPE *) = &JPEG_IDCT_Columns;
/* Pointer to JPEG_IDCT_Rows */
void (*IDCT_Rows) (const JPEG_FIXED_TYPE *, signed char *, int) = &JPEG_IDCT_Rows;
/* Pointer to JPEG_DecodeCoefficients*/
void (*DecodeCoefficients) (JPEG_FIXED_TYPE *, JPEG_FIXED_TYPE *, JPEG_FIXED_TYPE *, JPEG_HuffmanTable *,
JPEG_HuffmanTable *, const unsigned char **, unsigned int *,
unsigned long int *, const unsigned char *) = &JPEG_DecodeCoefficients;
const unsigned char *ToZigZag = JPEG_ToZigZag; /* Pointer to JPEG_ToZigZag*/
/* Start decoding bits. */
JPEG_BITS_START ();
/* The sum of all factors in the scan; this cannot be greater than 10 in JPEG Baseline. */
// int factorSum = 0;
/* Find the maximum factors and the factors for each component. */
for (item = frame->componentList; item < itemEnd; item ++)
{
/* Find the opposing scan header component. */
for (c = 0; ; c ++)
{
JPEG_ScanHeader_Component *sc;
sc = &scan->componentList [c];
if (sc->selector != item->selector)
continue;
/* Decompress the DC table if necessary. */
if (sc->dcTable != dcTableUse [0] && sc->dcTable != dcTableUse [1])
{
const unsigned char *tablePointer = decoder->dcTables [sc->dcTable];
if (dcTableUse [0] == -1)
dcTableUse [0] = sc->dcTable, JPEG_HuffmanTable_Read (&dcTableList [0], &tablePointer);
else if (dcTableUse [1] == -1)
dcTableUse [1] = sc->dcTable, JPEG_HuffmanTable_Read (&dcTableList [1], &tablePointer);
}
/* Decompress the AC table if necessary. */
if (sc->acTable != acTableUse [0] && sc->acTable != acTableUse [1])
{
const unsigned char *tablePointer = decoder->acTables [sc->acTable];
if (acTableUse [0] == -1)
acTableUse [0] = sc->acTable, JPEG_HuffmanTable_Read (&acTableList [0], &tablePointer);
else if (acTableUse [1] == -1)
acTableUse [1] = sc->acTable, JPEG_HuffmanTable_Read (&acTableList [1], &tablePointer);
}
frameComponents [c] = item;
break;
}
/* Add the sum for a later assertion test. */
// factorSum += item->horzFactor * item->vertFactor;
/* Adjust the maximum horizontal and vertical scaling factors as necessary. */
if (item->horzFactor > horzMax)
horzMax = item->horzFactor;
if (item->vertFactor > vertMax)
vertMax = item->vertFactor;
/* Update the relevant component scaling factors if necessary. */
if (item->selector == 1)
{
YHorzFactor = item->horzFactor;
YVertFactor = item->vertFactor;
}
else if (item->selector == 2)
{
CbHorzFactor = item->horzFactor;
CbVertFactor = item->vertFactor;
}
else if (item->selector == 3)
{
CrHorzFactor = item->horzFactor;
CrVertFactor = item->vertFactor;
}
}
/* Split up blockBase according to the components. */
YBlock = blockBase;
CbBlock = YBlock + YHorzFactor * YVertFactor * JPEG_DCTSIZE2;
CrBlock = CbBlock + CbHorzFactor * CbVertFactor * JPEG_DCTSIZE2;
/* Compute the right shift to be done after multiplying against the scaling factor. */
if (horzMax == 1) horzShift = 8;
else if (horzMax == 2) horzShift = 7;
else if (horzMax == 4) horzShift = 6;
/* Compute the right shift to be done after multiplying against the scaling factor. */
if (vertMax == 1) vertShift = 8;
else if (vertMax == 2) vertShift = 7;
else if (vertMax == 4) vertShift = 6;
/* Adjust the scaling factors for our parameters. */
YHorzFactor <<= horzShift;
YVertFactor <<= vertShift;
CbHorzFactor <<= horzShift;
CbVertFactor <<= vertShift;
CrHorzFactor <<= horzShift;
CrVertFactor <<= vertShift;
/* Clear the Cb channel for potential grayscale. */
{
signed char *e = CbBlock + JPEG_DCTSIZE2;
do *-- e = 0;
while (e > CbBlock);
}
/* Clear the Cr channel for potential grayscale. */
{
signed char *e = CrBlock + JPEG_DCTSIZE2;
do *-- e = 0;
while (e > CrBlock);
}
/* Clear the DC parameters. */
for (c = 0; c < JPEG_MAXIMUM_COMPONENTS; c ++)
dcLast [c] = 0;
/* Now run over each MCU horizontally, then vertically. */
for (by = 0; by < frame->height; by += vertMax * JPEG_DCTSIZE)
{
for (bx = 0; bx < frame->width; bx += horzMax * JPEG_DCTSIZE)
{
/* Read the components for the MCU. */
for (c = 0; c < scan->componentCount; c ++)
{
JPEG_ScanHeader_Component *sc = &scan->componentList [c];
JPEG_FrameHeader_Component *fc = frameComponents [c];
JPEG_HuffmanTable *dcTable, *acTable;
JPEG_FIXED_TYPE *quant = decoder->quantTables [fc->quantTable];
int stride = fc->horzFactor * JPEG_DCTSIZE;
signed char *chunk = 0;
dcTable = &dcTableList [sc->dcTable == dcTableUse [1] ? 1 : 0];
acTable = &acTableList [sc->acTable == acTableUse [1] ? 1 : 0];
/* Compute the output chunk. */
if (fc->selector == 1)
chunk = YBlock;
else if (fc->selector == 2)
chunk = CbBlock;
else if (fc->selector == 3)
chunk = CrBlock;
for (cy = 0; cy < fc->vertFactor * JPEG_DCTSIZE; cy += JPEG_DCTSIZE)
{
for (cx = 0; cx < fc->horzFactor * JPEG_DCTSIZE; cx += JPEG_DCTSIZE)
{
int start = cx + cy * stride;
JPEG_FIXED_TYPE zz [JPEG_DCTSIZE2];
/* Decode coefficients. */
DecodeCoefficients (&dcLast [c], zz, quant, dcTable, acTable, &data, &bits_left, &bits_data, ToZigZag);
/* Perform an IDCT if this component will contribute to the image. */
if (chunk)
{
IDCT_Columns (zz);
IDCT_Rows (zz, chunk + start, stride);
}
}
}
}
/* Check that our block will be in-range; this should actually use clamping. */
if (bx + horzMax * JPEG_DCTSIZE > outWidth || by + vertMax * JPEG_DCTSIZE > outHeight)
continue;
//for (ix=0; ix< 384; ix++)
// *out++ = blockBase[ix];
/* YUV decode where Y is subsampled to create YUV 1:1:1 */
unsigned int oo = outWidth * outHeight / 4;
unsigned int o2 = outWidth / 2;
for (iy=0; iy<16; iy+=2)
{
for (ix=0; ix<16; ix+=2)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -