📄 jpeg.c
字号:
//DmaClear(3,0,array2,128,16);//改用DMA
if (HuffDCNum) temp0 = JPGDecode(HuffmanDC1); // Get the DC coefficient
else temp0 = JPGDecode(HuffmanDC0); // Get the DC coefficient
*dcCoef = *dcCoef + JPGReceiveBits(temp0);
array2[0][0] = *dcCoef; //* Quant[QuantNum][0][0];
XPos = 0; YPos = 0;
ZigIndex = 1;
ACCount = 1;
do
{
if (HuffACNum) d = JPGDecode(HuffmanAC1);
else d = JPGDecode(HuffmanAC0);
zeros = d >> 4;
bits = d & 15;
bitVal = JPGReceiveBits(bits);
if (bits)
{
ZigIndex += zeros;
ACCount += zeros;
if (ACCount >= 64) break;
XPos = JPGZig1[ZigIndex];
YPos = JPGZig2[ZigIndex];
ZigIndex++;
array2[XPos][YPos] = bitVal; // * Quant[QuantNum][XPos][YPos];
ACCount++;
}
else
{
if (zeros != 15) break;
ZigIndex += 15;
ACCount += 16;
}
} while (ACCount < 64);
jpeg_idct_ifast (array2, vector, QuantNum);
}
u16 JPGGetHuffTables (void)
{
u16 HuffAmount[17]; //1-16
u32 l0;
u16 c0;
u16 temp0;
s16 temp1;
u16 total;
u16 i;
u16 t0;
s32 CurNum;
u16 CurIndex;
u16 j;
//l0 = JPGGetWord();
l0 = jpgdata[findex++] << 8;
l0+= jpgdata[findex++];
c0 = 2;
do
{
temp0 = jpgdata[findex++];
c0++;
t0 = (temp0 & 16) >> 4;
temp0 &= 15;
switch (t0)
{
case 0: // DC Table
total = 0;
for (i=1; i<16+1; i++)
{
temp1 = jpgdata[findex++];
c0++;
total += temp1;
HuffAmount[i] = temp1;
}
for (i=0; i<total; i++)
{
if (temp0) HuffmanDC1[i].Code =jpgdata[findex++];
else HuffmanDC0[i].Code = jpgdata[findex++];
c0++;
}
CurNum = 0;
CurIndex = -1;
for (i=1; i<16+1; i++)
{
for (j=1; j<HuffAmount[i]+1; j++)
{
CurIndex++;
if (temp0)
{
HuffmanDC1[CurIndex].Index = CurNum;
HuffmanDC1[CurIndex].Length = i;
}
else
{
HuffmanDC0[CurIndex].Index = CurNum;
HuffmanDC0[CurIndex].Length = i;
}
CurNum++;
}
CurNum *= 2;
}
DCTables++;
break;
case 1:
total = 0;
for (i=1; i<16+1; i++)
{
temp1 = jpgdata[findex++];
c0++;
total += temp1;
HuffAmount[i] = temp1;
}
for (i=0; i<total; i++)
{
if (temp0) HuffmanAC1[i].Code = jpgdata[findex++];
else HuffmanAC0[i].Code = jpgdata[findex++];
c0++;
}
CurNum = 0;
CurIndex = -1;
for (i=1; i<16+1; i++)
{
for (j=1; j<HuffAmount[i]+1; j++)
{
CurIndex++;
if (temp0)
{
HuffmanAC1[CurIndex].Index = CurNum;
HuffmanAC1[CurIndex].Length = i;
}
else
{
HuffmanAC0[CurIndex].Index = CurNum;
HuffmanAC0[CurIndex].Length = i;
}
CurNum++;
}
CurNum *= 2;
}
ACTables++;
break;
}
} while (c0 < l0);
return(1);
}
u16 JPGGetImageAttr (void)
{
u32 temp4;
u16 temp0;
u16 temp1;
u16 i;
u16 id;
temp4 = JPGGetWord(); //Length of segment
temp0 = jpgdata[findex++]; // Data precision
if (temp0 != 8) return(0); // we do not support 12 or 16-bit samples
Image.Rows = JPGGetWord(); // Image Height
Image.Cols = JPGGetWord(); // Image Width
temp0 = jpgdata[findex++]; // Number of components
for (i=1; i<temp0+1; i++)
{
id = jpgdata[findex++];
switch (id)
{
case 1:
temp1 = jpgdata[findex++];
Image.SamplesY = (temp1 & 15) * (temp1 >> 4);
Image.QuantTableY =jpgdata[findex++];
break;
case 2:
case 3:
temp1 = jpgdata[findex++];
Image.SamplesCbCr = (temp1 & 15) * (temp1 >> 4);
Image.QuantTableCbCr = jpgdata[findex++];
break;
}
}
return(1);
}
u8 JPGGetQuantTables(void)
{
u32 l0 = JPGGetWord();
u16 c0 = 2;
u16 temp0;
u16 xp;
u16 yp;
u16 i;
u16 ZigIndex;
do
{
temp0 = jpgdata[findex++];
c0++;
if (temp0 & 0xf0) return(0); //we don't support 16-bit tables
temp0 &= 15;
ZigIndex = 0;
xp = 0;
yp = 0;
for (i=0; i<64; i++)
{
xp = JPGZig1[ZigIndex];
yp = JPGZig2[ZigIndex];
ZigIndex++;
QuantTable[temp0][xp][yp] = (jpgdata[findex++] * aanscales[(xp<<3) + yp]) >> 12;
c0++;
}
QTables++;
}
while (c0 < l0);
return(1);
}
u16 JPGGetSOS (void)
{
u32 temp4;
u16 temp0;
u16 temp1;
u16 temp2;
u16 i;
temp4 = JPGGetWord();
temp0 = jpgdata[findex++];
if ((temp0 != 1) && (temp0 != 3)) return(0);
Image.NumComp = temp0;
for (i=1; i<temp0+1; i++)
{
temp1 = jpgdata[findex++];
switch (temp1)
{
case 1:
temp2 = jpgdata[findex++];
Image.HuffACTableY = temp2 & 15;
Image.HuffDCTableY = temp2 >> 4;
break;
case 2:
temp2 = jpgdata[findex++];
Image.HuffACTableCbCr = temp2 & 15;
Image.HuffDCTableCbCr = temp2 >> 4;
break;
case 3:
temp2 =jpgdata[findex++];
Image.HuffACTableCbCr = temp2 & 15;
Image.HuffDCTableCbCr = temp2 >> 4;
break;
default:
return(0);
}
}
findex += 3;
return(1);
}
u16 rgb(s16 r,s16 g,s16 b)
{
u16 Temp=0;
Temp=((r>>3)<<10)|((g>>3)<<5)|(b>>3);
return Temp;
}
s16 JPGReceiveBits (u16 cat)
{
u32 temp0 = 0;
u16 i;
s32 ReceiveBits;
for (i=0; i<cat; i++) temp0 = (temp0<<1) + JPGNextBit();
if ((temp0<<1) >= (JPGpower2(cat)) ) ReceiveBits = temp0;
else ReceiveBits = -(JPGpower2(cat) - 1) + temp0;
return (ReceiveBits);
}
void jpeg_end(){}
int jpeg1(u8 xp,u8 yp,u32 loc)
{
u8 JPGZig0 [64] = {
0,0,1,2,1,0,0,1,2,3,4,3,2,1,0,0,1,2,3,4,5,6,5,4,3,2,1,0,0,1,2,3,
4,5,6,7,7,6,5,4,3,2,1,2,3,4,5,6,7,7,6,5,4,3,4,5,6,7,7,6,5,6,7,7
};
u8 JPGZig20 [64] = {
0,1,0,0,1,2,3,2,1,0,0,1,2,3,4,5,4,3,2,1,0,0,1,2,3,4,5,6,7,6,5,4,
3,2,1,0,1,2,3,4,5,6,7,7,6,5,4,3,2,3,4,5,6,7,7,6,5,4,5,6,7,7,6,7
};
u16 aanscales0[64] = {
/* precomputed values scaled up by 14 bits */
16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
};
struct JPGHuffmanEntry a1[256];
struct JPGHuffmanEntry a2[256];
struct JPGHuffmanEntry a3[256];
struct JPGHuffmanEntry a4[256];
u32 codes[((u32)jpeg_end-(u32)gfxJpeg4)/4+1];
u32 (*fun)()=(u32)codes+1;
u8 *p=NULL;
if (loc>0x8000000)
{
p=(u8 *)new_malloc(70*1024);
DmaCopy(3,loc,p,70*1024,16);
}
//u32 time1,time2;
DmaCopy(3,(u32)gfxJpeg4,codes,(u32)jpeg_end-(u32)gfxJpeg4,32);
JPGZig1=JPGZig0;
JPGZig2=JPGZig20;
aanscales=aanscales0;
if (p) jpgdata=p;
else jpgdata=(u8 *)loc;
HuffmanDC0=a1;
HuffmanDC1=a2;
HuffmanAC0=a3;
HuffmanAC1=a4;
if (fun(xp,yp, (u16 *)0x06000000))
{
new_free(p);
return 1;
}
arlt("文件错误:","您打开的文件为非Jpeg格式或者图片不能识别.");
return 0;
}
void jpeg(u8 xp,u8 yp,u32 loc)
{
if (jpeg1(xp,yp,loc)) wait_key();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -