📄 jpeg.c
字号:
if (xindex >= Image.Cols)
{
xindex = 0; yindex += 8; mcu = 1;
}
if ((mcu == 1) && (Restart != 0)) // execute the restart interval
{
curByte = jpgdata[findex++]; curByte = jpgdata[findex++]; curByte = jpgdata[findex++];
curBits = 128;
dcY = 0; dcCb = 0; dcCr = 0; mcu = 0;
}
}
while (yindex < Image.Rows);
break;
case 1: // 1 pixel to 1
do
{
JPGGetBlock (YVector1, Image.HuffDCTableY, Image.HuffACTableY, Image.QuantTableY, &dcY);
JPGGetBlock (CbVector, Image.HuffDCTableCbCr, Image.HuffACTableCbCr, Image.QuantTableCbCr, &dcCb);
JPGGetBlock (CrVector, Image.HuffDCTableCbCr, Image.HuffACTableCbCr, Image.QuantTableCbCr, &dcCr);
// YCbCr vectors have been obtained
loc=(y0+yindex)*240+(xindex+x0);//计算地址
if (yindex+8>=Image.Rows) ystep=Image.Rows-yindex;
else ystep=8;
for (i=0; i<ystep; i++) // Draw 8x8 pixels
{
if (xindex+8>=Image.Cols) xstep=Image.Cols-xindex;
else xstep=8;
for (j=0; j<xstep; j++)
{
y = YVector1[i][j];
i2 = i >> 1;
j2 = j >> 1;
cb = CbVector[i2][j2];
cr = CrVector[i2][j2];
vram[loc+j]=JRGB(y,cb,cr);
}
loc+=240;
}
xindex += 8;
if (xindex >= Image.Cols)
{
xindex = 0; yindex += 8; mcu = 1;
}
if ((mcu == 1) && (Restart != 0)) // execute the restart interval
{
curByte = jpgdata[findex++]; curByte = jpgdata[findex++]; curByte = jpgdata[findex++];
curBits = 128;
dcY = 0; dcCb = 0; dcCr = 0; mcu = 0;
}
}
while (yindex < Image.Rows);
break;
} // Ratio
}
case 1:
do
{
JPGGetBlock (YVector1, Image.HuffDCTableY, Image.HuffACTableY, Image.QuantTableY, &dcY);
// Y vector has been obtained
loc=(y0+yindex)*240+(xindex+x0);
if (yindex+8>=Image.Rows) ystep=Image.Rows-yindex;
else ystep=8;
for (i=0; i<ystep; i++) // Draw 8x8 pixels
{
if (xindex+8>=Image.Cols) xstep=Image.Cols-xindex;
else xstep=8;
for (j=0; j<xstep; j++)
{
y = YVector1[i][j];
if (y < 0) y = 0;
if (y > 255) y = 255;
vram[loc+j]=((r>>3)<<10)+((g>>3)<<5)+(b>>3);
}
loc+=240;
}
// Get setup to draw next block
xindex += 8;
if (xindex >= Image.Cols)
{
xindex = 0; yindex += 8; mcu = 1;
}
if ((mcu == 1) && (Restart != 0)) // execute the restart interval
{
curByte = jpgdata[findex++]; curByte = jpgdata[findex++]; curByte = jpgdata[findex++];
curBits = 128;
dcY = 0; mcu = 0;
}
}
while (yindex < Image.Rows);
break;
}
return(1);
}
u16 JRGB(s16 y0,s16 cr0,s16 cb0)
{
s16 r,g,b;
cr0-=128;
cb0-=128;
r= (y0) + ((cr0 * 45) >> 5);
g= (y0) - ((cb0 * 11) >> 5) - ((cr0 * 23) >> 5);
b= (y0) + ((cb0 * 57) >> 5);
if (r > 255) r = 255;
if (r < 0) r = 0;
if (g > 255) g = 255;
if (g < 0) g = 0;
if (b > 255) b = 255;
if (b < 0) b = 0;
return ((r>>3)<<10)|((g>>3)<<5)|(b>>3);
}
u16 JPGGetWord (void)
{
u16 i = jpgdata[findex++] << 8;
i += jpgdata[findex++];
return (i);
}
u8 JPGNextBit (void)
{
u8 NextBit;
curBits >>= 1;
NextBit = (curByte >> 7) & 1;
curByte <<= 1;
if (curBits == 0)
{
curBits = 128;
curByte = jpgdata[findex++];
if (curByte == 255)
if (jpgdata[findex++] == 0xD9)
{
EOI = 1;
return(0);
}
}
return(NextBit);
}
s16 JPGDecode(struct JPGHuffmanEntry inArray[256])
{
u16 n1; u16 n2; u16 i; u16 l;
s32 CurVal;
s16 MatchFound;
if (jpgdata[findex++] == 255)
{
n1 = jpgdata[findex++];
findex -= 2;
if ((n1 >= 0xd0) && (n1 <= 0xd7))
{
n2 = curBits - 1;
if ((curByte & n2) == n2) // if the remaining bits are 1
{
EOI = 1;
return(0);
}
}
}
else findex--;
CurVal = 0;
MatchFound = -1;
for (l=1; l<16+1; l++) // cycle through 16 possible Huffman lengths
{
CurVal = (CurVal << 1) + JPGNextBit();
if (EOI) return(0);
for (i=0; i<256; i++) // look for a match in the Huffman table
{
if (inArray[i].Length > l) break;
if (inArray[i].Length == l && inArray[i].Index == CurVal)
//if (inArray[i].Index == CurVal)
{
MatchFound = i;
break;
}
}
if (MatchFound > -1) break;
}
if (MatchFound == -1) return(-1);
return(inArray[MatchFound].Code); // return the appropriate code
}
void jpeg_idct_ifast (s16 inarray[8][8], s16 outarray[8][8], u16 QuantNum)
{
s32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
s32 tmp10, tmp11, tmp12, tmp13;
s32 z5, z10, z11, z12, z13;
u32 ctr;
s16 warray[8][8];
for (ctr = 0; ctr < 8; ctr++)
{
if (inarray[1][ctr] == 0 && inarray[2][ctr] == 0 &&
inarray[3][ctr] == 0 && inarray[4][ctr] == 0 &&
inarray[5][ctr] == 0 && inarray[6][ctr] == 0 &&
inarray[7][ctr] == 0)
{
s16 dcval = inarray[0][ctr] * QuantTable[QuantNum][0][ctr];
warray[0][ctr] = dcval;
warray[1][ctr] = dcval;
warray[2][ctr] = dcval;
warray[3][ctr] = dcval;
warray[4][ctr] = dcval;
warray[5][ctr] = dcval;
warray[6][ctr] = dcval;
warray[7][ctr] = dcval;
continue;
}
tmp0 = inarray[0][ctr] * QuantTable[QuantNum][0][ctr];
tmp1 = inarray[2][ctr] * QuantTable[QuantNum][2][ctr];
tmp2 = inarray[4][ctr] * QuantTable[QuantNum][4][ctr];
tmp3 = inarray[6][ctr] * QuantTable[QuantNum][6][ctr];
tmp10 = tmp0 + tmp2; /* phase 3 */
tmp11 = tmp0 - tmp2;
tmp13 = tmp1 + tmp3; /* phases 5-3 */
tmp12 = (((tmp1 - tmp3)*( FIX_1_414213562)) >> 8) - tmp13; /* 2*c4 */
tmp0 = tmp10 + tmp13; /* phase 2 */
tmp3 = tmp10 - tmp13;
tmp1 = tmp11 + tmp12;
tmp2 = tmp11 - tmp12;
tmp4 = inarray[1][ctr] * QuantTable[QuantNum][1][ctr];
tmp5 = inarray[3][ctr] * QuantTable[QuantNum][3][ctr];
tmp6 = inarray[5][ctr] * QuantTable[QuantNum][5][ctr];
tmp7 = inarray[7][ctr] * QuantTable[QuantNum][7][ctr];
z13 = tmp6 + tmp5; /* phase 6 */
z10 = tmp6 - tmp5;
z11 = tmp4 + tmp7;
z12 = tmp4 - tmp7;
tmp7 = z11 + z13; /* phase 5 */
tmp11 = ((z11 - z13) * FIX_1_414213562) >> 8; /* 2*c4 */
z5 = ((z10 + z12) * FIX_1_847759065) >> 8; /* 2*c2 */
tmp10 = ((z12 * FIX_1_082392200) >> 8) - z5; /* 2*(c2-c6) */
tmp12 = ((z10 * - FIX_2_613125930) >> 8) + z5; /* -2*(c2+c6) */
tmp6 = tmp12 - tmp7; /* phase 2 */
tmp5 = tmp11 - tmp6;
tmp4 = tmp10 + tmp5;
warray[0][ctr] = (tmp0 + tmp7);
warray[7][ctr] = (tmp0 - tmp7);
warray[1][ctr] = (tmp1 + tmp6);
warray[6][ctr] = (tmp1 - tmp6);
warray[2][ctr] = (tmp2 + tmp5);
warray[5][ctr] = (tmp2 - tmp5);
warray[4][ctr] = (tmp3 + tmp4);
warray[3][ctr] = (tmp3 - tmp4);
}
for (ctr = 0; ctr < 8; ctr++)
{
if (warray[ctr][1] == 0 && warray[ctr][2] == 0 && warray[ctr][3] == 0 && warray[ctr][4] == 0
&& warray[ctr][5] == 0 && warray[ctr][6] == 0 && warray[ctr][7] == 0)
{
s16 dcval = (warray[ctr][0] >> 5)+128;
if (dcval<0) dcval = 0;
if (dcval>255) dcval = 255;
outarray[ctr][0] = dcval;
outarray[ctr][1] = dcval;
outarray[ctr][2] = dcval;
outarray[ctr][3] = dcval;
outarray[ctr][4] = dcval;
outarray[ctr][5] = dcval;
outarray[ctr][6] = dcval;
outarray[ctr][7] = dcval;
continue;
}
tmp10 = warray[ctr][0] + warray[ctr][4];
tmp11 = warray[ctr][0] - warray[ctr][4];
tmp13 = warray[ctr][2] + warray[ctr][6];
tmp12 = (((warray[ctr][2] - warray[ctr][6]) * FIX_1_414213562) >> 8) - tmp13;
tmp0 = tmp10 + tmp13;
tmp3 = tmp10 - tmp13;
tmp1 = tmp11 + tmp12;
tmp2 = tmp11 - tmp12;
z13 = warray[ctr][5] + warray[ctr][3];
z10 = warray[ctr][5] - warray[ctr][3];
z11 = warray[ctr][1] + warray[ctr][7];
z12 = warray[ctr][1] - warray[ctr][7];
tmp7 = z11 + z13; /* phase 5 */
tmp11 = ((z11 - z13) * FIX_1_414213562) >> 8; /* 2*c4 */
z5 = ((z10 + z12) * FIX_1_847759065) >> 8; /* 2*c2 */
tmp10 = ((z12 * FIX_1_082392200) >> 8) - z5; /* 2*(c2-c6) */
tmp12 = ((z10 * - FIX_2_613125930) >> 8) + z5; /* -2*(c2+c6) */
tmp6 = tmp12 - tmp7; /* phase 2 */
tmp5 = tmp11 - tmp6;
tmp4 = tmp10 + tmp5;
/* Final output stage: scale down by a factor of 8 and range-limit */
outarray[ctr][0] = ((tmp0 + tmp7) >> 5)+128;
if ((outarray[ctr][0])<0) outarray[ctr][0] = 0;
if ((outarray[ctr][0])>255) outarray[ctr][0] = 255;
outarray[ctr][7] = ((tmp0 - tmp7) >> 5)+128;
if ((outarray[ctr][7])<0) outarray[ctr][7] = 0;
if ((outarray[ctr][7])>255) outarray[ctr][7] = 255;
outarray[ctr][1] = ((tmp1 + tmp6) >> 5)+128;
if ((outarray[ctr][1])<0) outarray[ctr][1] = 0;
if ((outarray[ctr][1])>255) outarray[ctr][1] = 255;
outarray[ctr][6] = ((tmp1 - tmp6) >> 5)+128;
if ((outarray[ctr][6])<0) outarray[ctr][6] = 0;
if ((outarray[ctr][6])>255) outarray[ctr][6] = 255;
outarray[ctr][2] = ((tmp2 + tmp5) >> 5)+128;
if ((outarray[ctr][2])<0) outarray[ctr][2] = 0;
if ((outarray[ctr][2])>255) outarray[ctr][2] = 255;
outarray[ctr][5] = ((tmp2 - tmp5) >> 5)+128;
if ((outarray[ctr][5])<0) outarray[ctr][5] = 0;
if ((outarray[ctr][5])>255) outarray[ctr][5] = 255;
outarray[ctr][4] = ((tmp3 + tmp4) >> 5)+128;
if ((outarray[ctr][4])<0) outarray[ctr][4] = 0;
if ((outarray[ctr][4])>255) outarray[ctr][4] = 255;
outarray[ctr][3] = ((tmp3 - tmp4) >> 5)+128;
if ((outarray[ctr][3])<0) outarray[ctr][3] = 0;
if ((outarray[ctr][3])>255) outarray[ctr][3] = 255;
}
}
void JPGGetBlock (s16 vector[8][8], u16 HuffDCNum, u16 HuffACNum, u16 QuantNum, s16 *dcCoef)
{
s16 array2[8][8];
s32 d; u16 XPos; u16 YPos;
u16 bits; u16 zeros; s32 bitVal; u16 ACCount;
u16 x; u16 y;
s16 temp0;
u16 ZigIndex;
EOI = 0;
for (x=0; x<8; x++)
for (y=0; y<8; y++) array2[x][y] = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -