📄 jpeg.c
字号:
for(i=0;i<SampRate_Y_V*8;i++)
{
if((sizei+i)<ImgHeight)// sizei表示行 sizej 表示列
{
for(j=0;j<SampRate_Y_H*8;j++)
{
if((sizej+j)<ImgWidth)
{
y=Y[i*8*SampRate_Y_H+j];
u=U[(i/V_YtoU)*8*SampRate_Y_H+j/H_YtoU];
v=V[(i/V_YtoV)*8*SampRate_Y_H+j/H_YtoV];
rr=((y<<8)+18*u+367*v)>>8;
gg=((y<<8)-159*u-220*v)>>8;
bb=((y<<8)+411*u-29*v)>>8;
R=(unsigned char)rr;
G=(unsigned char)gg;
B=(unsigned char)bb;
if (rr&0xffffff00) if (rr>255) R=255; else if (rr<0) R=0;
if (gg&0xffffff00) if (gg>255) G=255; else if (gg<0) G=0;
if (bb&0xffffff00) if (bb>255) B=255; else if (bb<0) B=0;
color=R>>3;
color=color<<6;
color |=(G>>2);
color=color<<5;
color |=(B>>3);
//UART_Put_Inf("X:",/*sizej+j*/CurX);
//UART_Put_Inf("Y:",/*sizei+i*/CurY);
//UART_Send_Enter();
TFT_Draw_Point(sizej+j,sizei+i,color);
//在这里送给LCD显示
//POlong_COLOR=color;
//TFT_DrawPolong(sizej+j+CurX,sizej+j+CurX);//显示图片
//pDC->SetPixel(sizej+j+CurX,sizei+i+CurY,B*65536+G*256+R);
//if((sizej+j+CurX)==0 && (sizei+i+CurY)==0)
//UART_Put_Inf("counter:",counter++);
//UART_Put_Inf("color:",color);
//UART_Put_Inf("R:",R);
//UART_Put_Inf("G:",G);
//UART_Put_Inf("B:",B);
//UART_Send_Enter();
//colorH|=(R>>3);colorH<<=3;
//colorH|=(G>>5);
//colorL|=(G<<3);
//colorL|=B>>3;
//TFT_Write_Dat(colorH);
//TFT_Write_Dat(colorL);
{
//P1=~R;
//CString temp;
//temp.Format("%d,%d,%d",R,G,B);
//AfxMessageBox(temp);
}
}
else break;
}
}
else break;
}
}
//Huffman Decode MCU 出口 MCUBuffer 入口Blockbuffer[ ]
long DecodeMCUBlock()
{
long *lpMCUBuffer;
long xdata i,j;
long xdata funcret;
if (longervalFlag)//差值复位
{
lp+=2;
ycoef=ucoef=vcoef=0;
BitPos=0;
CurByte=0;
}
switch(comp_num)
{
case 3: //comp_num 指图的类型(彩色图、灰度图)
lpMCUBuffer=MCUBuffer;
for (i=0;i<SampRate_Y_H*SampRate_Y_V;i++) //Y
{
funcret=HufBlock(YDcIndex,YAcIndex);//解码4 * (8*8)
if (funcret!=FUNC_OK)
return funcret;
BlockBuffer[0]=BlockBuffer[0]+ycoef;//直流分量是差值,所以要累加。
ycoef=BlockBuffer[0];
for (j=0;j<64;j++)
*lpMCUBuffer++=BlockBuffer[j];
}
for (i=0;i<SampRate_U_H*SampRate_U_V;i++) //U
{
funcret=HufBlock(UVDcIndex,UVAcIndex);
if (funcret!=FUNC_OK)
return funcret;
BlockBuffer[0]=BlockBuffer[0]+ucoef;
ucoef=BlockBuffer[0];
for (j=0;j<64;j++)
*lpMCUBuffer++=BlockBuffer[j];
}
for (i=0;i<SampRate_V_H*SampRate_V_V;i++) //V
{
funcret=HufBlock(UVDcIndex,UVAcIndex);
if (funcret!=FUNC_OK)
return funcret;
BlockBuffer[0]=BlockBuffer[0]+vcoef;
vcoef=BlockBuffer[0];
for (j=0;j<64;j++)
*lpMCUBuffer++=BlockBuffer[j];
}
break;
case 1: //Gray Picture
lpMCUBuffer=MCUBuffer;
funcret=HufBlock(YDcIndex,YAcIndex);
if (funcret!=FUNC_OK)
return funcret;
BlockBuffer[0]=BlockBuffer[0]+ycoef;
ycoef=BlockBuffer[0];
for (j=0;j<64;j++)
*lpMCUBuffer++=BlockBuffer[j];
for (i=0;i<128;i++)
*lpMCUBuffer++=0;
break;
default:
return FUNC_FORMAT_ERROR;
}
return FUNC_OK;
}
long HufBlock(unsigned char dchufindex,unsigned char achufindex)
{
long xdata count=0;
long xdata i;
long xdata funcret;
//dc
HufTabIndex=dchufindex;
funcret=DecodeElement();
if(funcret!=FUNC_OK)
return funcret;
BlockBuffer[count++]=vvalue;//解出的直流系数
//ac
HufTabIndex=achufindex;
while (count<64)
{
funcret=DecodeElement();
if(funcret!=FUNC_OK)
return funcret;
if ((rrun==0)&&(vvalue==0))
{
for (i=count;i<64;i++)
BlockBuffer[i]=0;
count=64;
}
else
{
for (i=0;i<rrun;i++) //前面的零
BlockBuffer[count++]=0;
BlockBuffer[count++]=vvalue;//解出的值
}
}
return FUNC_OK;
}
//Huffman 解码 每个元素 出口 vvalue 入口 读文件ReadByte
long DecodeElement()
{
long xdata thiscode,tempcode;
unsigned long xdata temp,valueex;
long xdata codelen;
unsigned char xdata hufexbyte,runsize,tempsize,sign;
unsigned char xdata newbyte,lastbyte;
if(BitPos>=1) //BitPos指示当前比特位置
{
BitPos--;
thiscode=(unsigned char)CurByte>>BitPos;//取一个比特
CurByte=CurByte&And[BitPos]; //清除取走的比特位
}
else //取出的一个字节已用完
{ //新取
lastbyte=ReadByte(); //读出一个字节
BitPos--; //and[]:=0x0,0x1,0x3,0x7,0xf,0x1f,0x2f,0x3f,0x4f
newbyte=CurByte&And[BitPos];
thiscode=lastbyte>>7;
CurByte=newbyte;
}
codelen=1;
//与Huffman表中的码字匹配,直自找到为止
while ((thiscode<huf_min_value[HufTabIndex][codelen-1])||
(code_len_table[HufTabIndex][codelen-1]==0)||
(thiscode>huf_max_value[HufTabIndex][codelen-1]))
{
if(BitPos>=1)//取出的一个字节还有
{
BitPos--;
tempcode=(unsigned char)CurByte>>BitPos;
CurByte=CurByte&And[BitPos];
}
else
{
lastbyte=ReadByte();
BitPos--;
newbyte=CurByte&And[BitPos];
tempcode=(unsigned char)lastbyte>>7;
CurByte=newbyte;
}
thiscode=(thiscode<<1)+tempcode;
codelen++;
if(codelen>16)return FUNC_FORMAT_ERROR;
} //while
temp=thiscode-huf_min_value[HufTabIndex][codelen-1]+code_pos_table[HufTabIndex][codelen-1];
hufexbyte=(unsigned char)code_value_table[HufTabIndex][temp];
rrun=(long)(hufexbyte>>4); //一个字节中,高四位是其前面的零的个数。
runsize=hufexbyte&0x0f; //后四位为后面字的尺寸
if(runsize==0)
{
vvalue=0;
return FUNC_OK;
}
tempsize=runsize;
if(BitPos>=runsize)
{
BitPos-=runsize;
valueex=(unsigned char)CurByte>>BitPos;
CurByte=CurByte&And[BitPos];
}
else
{
valueex=CurByte;
tempsize-=BitPos;
while(tempsize>8)
{
lastbyte=ReadByte();
valueex=(valueex<<8)+(unsigned char)lastbyte;
tempsize-=8;
} //while
lastbyte=ReadByte();
BitPos-=tempsize;
valueex=(valueex<<tempsize)+(lastbyte>>BitPos);
CurByte=lastbyte&And[BitPos];
} //else
sign=valueex>>(runsize-1);
if(sign)vvalue=valueex;//解出的码值
else
{
valueex=valueex^0xffff;
temp=0xffff<<runsize;
vvalue=-(long)(valueex^temp);
}
return FUNC_OK;
}
//反量化MCU中的每个组件 入口 MCUBuffer 出口 QtZzMCUBuffer
void IQtIZzMCUComponent(long flag)
{
long xdata H,VV;
long xdata i,j;
long *pQtZzMCUBuffer;
long *pMCUBuffer;
switch(flag){
case 0:
H=SampRate_Y_H;
VV=SampRate_Y_V;
pMCUBuffer=MCUBuffer;
pQtZzMCUBuffer=QtZzMCUBuffer;
break;
case 1:
H=SampRate_U_H;
VV=SampRate_U_V;
pMCUBuffer=MCUBuffer+Y_in_MCU*64;
pQtZzMCUBuffer=QtZzMCUBuffer+Y_in_MCU*64;
break;
case 2:
H=SampRate_V_H;
VV=SampRate_V_V;
pMCUBuffer=MCUBuffer+(Y_in_MCU+U_in_MCU)*64;
pQtZzMCUBuffer=QtZzMCUBuffer+(Y_in_MCU+U_in_MCU)*64;
break;
}
for(i=0;i<VV;i++)
for (j=0;j<H;j++)
IQtIZzBlock(pMCUBuffer+(i*H+j)*64,pQtZzMCUBuffer+(i*H+j)*64,flag);
}
//要量化的字
//反量化 8*8 DU
void IQtIZzBlock(long *s ,long * d,long flag)
{
long xdata i,j;
long xdata tag;
long *pQt;
long xdata buffer2[8][8];
long *buffer1;
long xdata offset;
switch(flag)
{
case 0: //亮度
pQt=YQtTable;
offset=128;
break;
case 1: //红
pQt=UQtTable;
offset=0;
break;
case 2: //蓝
pQt=VQtTable;
offset=0;
break;
}
for(i=0;i<8;i++)
for(j=0;j<8;j++)
{
tag=Zig_Zag[i][j];
buffer2[i][j]=(long)s[tag]*(long)pQt[tag];
}
buffer1=(long *)buffer2;
Fast_IDCT(buffer1);//反DCT
for(i=0;i<8;i++)
for(j=0;j<8;j++)
d[i*8+j]=buffer2[i][j]+offset;
}
//快速反DCT
void Fast_IDCT(long * block)
{
long data i;
for (i=0; i<8; i++)idctrow(block+8*i);
for (i=0; i<8; i++)idctcol(block+i);
}
//从源文件读取一个字节
unsigned char ReadByte()
{
unsigned char xdata i;
i=*(lp++);// lp 为解码的起始位置
if(i==0xff)lp++;
BitPos=8;
CurByte=i;
return i;
}
//初始化快速反DCT
void Initialize_Fast_IDCT()
{
long xdata i;
iclp = iclip+512;
for (i= -512; i<512; i++)
iclp[i] = (i<-256) ? -256 : ((i>255) ? 255 : i);
}
////////////////////////////////////////////////////////////////////////
void idctrow(long * blk)
{
long data x0, x1, x2, x3, x4, x5, x6, x7, x8;
//longcut
if (!((x1 = blk[4]<<11) | (x2 = blk[6]) | (x3 = blk[2]) |
(x4 = blk[1]) | (x5 = blk[7]) | (x6 = blk[5]) | (x7 = blk[3])))
{
blk[0]=blk[1]=blk[2]=blk[3]=blk[4]=blk[5]=blk[6]=blk[7]=blk[0]<<3;
return;
}
x0 = (blk[0]<<11) + 128; // for proper rounding in the fourth stage
//first stage
x8 = W7*(x4+x5);
x4 = x8 + (W1-W7)*x4;
x5 = x8 - (W1+W7)*x5;
x8 = W3*(x6+x7);
x6 = x8 - (W3-W5)*x6;
x7 = x8 - (W3+W5)*x7;
//second stage
x8 = x0 + x1;
x0 -= x1;
x1 = W6*(x3+x2);
x2 = x1 - (W2+W6)*x2;
x3 = x1 + (W2-W6)*x3;
x1 = x4 + x6;
x4 -= x6;
x6 = x5 + x7;
x5 -= x7;
//third stage
x7 = x8 + x3;
x8 -= x3;
x3 = x0 + x2;
x0 -= x2;
x2 = (181*(x4+x5)+128)>>8;
x4 = (181*(x4-x5)+128)>>8;
//fourth stage
blk[0] = (x7+x1)>>8;
blk[1] = (x3+x2)>>8;
blk[2] = (x0+x4)>>8;
blk[3] = (x8+x6)>>8;
blk[4] = (x8-x6)>>8;
blk[5] = (x0-x4)>>8;
blk[6] = (x3-x2)>>8;
blk[7] = (x7-x1)>>8;
}
void idctcol(long * blk)
{
long xdata x0, x1, x2, x3, x4, x5, x6, x7, x8;
//longcut
if (!((x1 = (blk[8*4]<<8)) | (x2 = blk[8*6]) | (x3 = blk[8*2]) |
(x4 = blk[8*1]) | (x5 = blk[8*7]) | (x6 = blk[8*5]) | (x7 = blk[8*3])))
{
blk[8*0]=blk[8*1]=blk[8*2]=blk[8*3]=blk[8*4]=blk[8*5]
=blk[8*6]=blk[8*7]=iclp[(blk[8*0]+32)>>6];
return;
}
x0 = (blk[8*0]<<8) + 8192;
//first stage
x8 = W7*(x4+x5) + 4;
x4 = (x8+(W1-W7)*x4)>>3;
x5 = (x8-(W1+W7)*x5)>>3;
x8 = W3*(x6+x7) + 4;
x6 = (x8-(W3-W5)*x6)>>3;
x7 = (x8-(W3+W5)*x7)>>3;
//second stage
x8 = x0 + x1;
x0 -= x1;
x1 = W6*(x3+x2) + 4;
x2 = (x1-(W2+W6)*x2)>>3;
x3 = (x1+(W2-W6)*x3)>>3;
x1 = x4 + x6;
x4 -= x6;
x6 = x5 + x7;
x5 -= x7;
//third stage
x7 = x8 + x3;
x8 -= x3;
x3 = x0 + x2;
x0 -= x2;
x2 = (181*(x4+x5)+128)>>8;
x4 = (181*(x4-x5)+128)>>8;
//fourth stage
blk[8*0] = iclp[(x7+x1)>>14];
blk[8*1] = iclp[(x3+x2)>>14];
blk[8*2] = iclp[(x0+x4)>>14];
blk[8*3] = iclp[(x8+x6)>>14];
blk[8*4] = iclp[(x8-x6)>>14];
blk[8*5] = iclp[(x0-x4)>>14];
blk[8*6] = iclp[(x3-x2)>>14];
blk[8*7] = iclp[(x7-x1)>>14];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -