📄 jpeg.c
字号:
BitPos=0;
CurByte=0;
}
switch(comp_num)
{
case 3:
lpMCUBuffer=MCUBuffer;
for (i=0;i<SampRate_Y_H*SampRate_Y_V;i++) //Y
{
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<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:
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;
}
///////////////////////////////////////////////////////////////////////////////
FUNCRET HufBlock(BYTE dchufindex,BYTE achufindex)
{
short count=0;
short i;
FUNCRET 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;
}
//////////////////////////////////////////////////////////////////////////////
FUNCRET DecodeElement()
{
int thiscode,tempcode;
unsigned short temp,valueex;
short codelen;
BYTE hufexbyte,runsize,tempsize,sign;
BYTE newbyte,lastbyte;
if(BitPos>=1)
{
BitPos--;
thiscode=(BYTE)CurByte>>BitPos;
CurByte=CurByte&And[BitPos];
}
else
{
lastbyte=ReadByte();
BitPos--;
newbyte=CurByte&And[BitPos];
thiscode=lastbyte>>7;
CurByte=newbyte;
}
codelen=1;
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=(BYTE)CurByte>>BitPos;
CurByte=CurByte&And[BitPos];
}
else
{
lastbyte=ReadByte();
BitPos--;
newbyte=CurByte&And[BitPos];
tempcode=(BYTE)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=(BYTE)code_value_table[HufTabIndex][temp];
rrun=(short)(hufexbyte>>4);
runsize=hufexbyte&0x0f;
if(runsize==0)
{
vvalue=0;
return FUNC_OK;
}
tempsize=runsize;
if(BitPos>=runsize)
{
BitPos-=runsize;
valueex=(BYTE)CurByte>>BitPos;
CurByte=CurByte&And[BitPos];
}
else
{
valueex=CurByte;
tempsize-=BitPos;
while(tempsize>8)
{
lastbyte=ReadByte();
valueex=(valueex<<8)+(BYTE)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=-(short)(valueex^temp);
}
return FUNC_OK;
}
/////////////////////////////////////////////////////////////////////////////////////
void IQtIZzMCUComponent(short flag)
{
short H,VV;
short i,j;
int *pQtZzMCUBuffer;
short *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);
}
//////////////////////////////////////////////////////////////////////////////////////////
void IQtIZzBlock(short *s ,int * d,short flag)
{
short i,j;
short tag;
short *pQt;
int buffer2[8][8];
int *buffer1;
short 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[j][i];
tag=Zig_Zag[i][j];
buffer2[i][j]=(int)s[tag]*(int)pQt[tag];
}
buffer1=(int *)buffer2;
Fast_IDCT(buffer1);
for(i=0;i<8;i++)
for(j=0;j<8;j++)
d[i*8+j]=buffer2[i][j]+offset;
}
///////////////////////////////////////////////////////////////////////////////
void Fast_IDCT(int * block)
{
short i;
for (i=0; i<8; i++)
idctrow(block+8*i);
for (i=0; i<8; i++)
idctcol(block+i);
}
///////////////////////////////////////////////////////////////////////////////
BYTE ReadByte()
{
BYTE i;
i=*(lp++);
if(i==0xff)
lp++;
BitPos=8;
CurByte=i;
return i;
}
/////////////////////////////////////////////////
void showerror(FUNCRET funcret)
{
switch(funcret)
{
case FUNC_MEMORY_ERROR:
MessageBox(NULL,"Not enough memory!","Error Message",MB_OK);
break;
case FUNC_FILE_ERROR:
MessageBox(NULL,"File read error!","Error Message",MB_OK);
break;
case FUNC_FORMAT_ERROR:
MessageBox(NULL,"File format error!","Error Message",MB_OK);
break;
}
}
/////////////////////////////////////////////////////////////////
void InitTable()
{
short i,j;
sizei=sizej=0;
imgwidth=imgheight=0;
rrun=vvalue=0;
BitPos=0;
CurByte=0;
IntervalFlag=FALSE;
restart=0;
for(i=0;i<3;i++)
for(j=0;j<64;j++)
qt_table[i][j]=0;
comp_num=0;
HufTabIndex=0;
for(i=0;i<3;i++)
comp_index[i]=0;
for(i=0;i<4;i++)
for(j=0;j<16;j++)
{
code_len_table[i][j]=0;
code_pos_table[i][j]=0;
huf_max_value[i][j]=0;
huf_min_value[i][j]=0;
}
for(i=0;i<4;i++)
for(j=0;j<256;j++)
code_value_table[i][j]=0;
for(i=0;i<10*64;i++)
{
MCUBuffer[i]=0;
QtZzMCUBuffer[i]=0;
}
for(i=0;i<64;i++)
{
Y[i]=0;
U[i]=0;
V[i]=0;
BlockBuffer[i]=0;
}
ycoef=ucoef=vcoef=0;
}
////////////////////////////////////////////////////////////////////
void DispABand()
{
HDC hDC;
//RECT destrc,sourc;
hDC = GetDC (hwnd);
if (!hbiband)
return;
/*
sourc.left = 0;
sourc.top = 0;
sourc.right = imgwidth;
sourc.bottom = 8*SampRate_Y_V;
destrc.left=0;
destrc.top=imgheight-sizei-8*SampRate_Y_V;
destrc.right=destrc.left+imgwidth;
destrc.bottom=imgheight-sizei;
*/
SetStretchBltMode (hDC, COLORONCOLOR);
SetDIBitsToDevice (hDC,
0,
sizei,
imgwidth,
8*SampRate_Y_V,
0,
0,
0,
8*SampRate_Y_V,
lpbibandbits,
(LPBITMAPINFO)lpbibandheader,
DIB_RGB_COLORS);
ReleaseDC(hwnd,hDC);
}
///////////////////////////////////////////////////////////////
void GetJPGSize(char *fn)
{
HFILE hf;
HLOCAL hinfo;
unsigned char *lpinfo;
unsigned short llength;
BYTE id;
short count=0;
if((hf=_lopen(fn,OF_READ))==HFILE_ERROR)
{
showerror(FUNC_FILE_ERROR);
imgwidth=0;
imgheight=0;
return ;
}
if((hinfo=LocalAlloc(LHND,2000))==NULL)
{
_lclose(hf);
showerror(FUNC_MEMORY_ERROR);
imgwidth=0;
imgheight=0;
return ;
}
lpinfo=(unsigned char *)LocalLock(hinfo);
_lread(hf,(unsigned char *)lpinfo,2000);
_lclose(hf);
if((llength=MAKEWORD(*(lpinfo+1),*lpinfo))!=0xffd8)
{
showerror(FUNC_FORMAT_ERROR);
LocalUnlock(hinfo);
LocalFree(hinfo);
imgwidth=0;
imgheight=0;
return;
}
lpinfo+=2;
count+=2;
while (count<2000)
{
id=*(lpinfo+1);
lpinfo+=2;
count+=2;
switch (id)
{
case M_APP0:
case M_DQT:
case M_DHT:
case M_DRI:
case M_SOS:
llength=MAKEWORD(*(lpinfo+1),*lpinfo);
lpinfo+=llength;
count+=llength;
break;
case M_SOF0:
llength=MAKEWORD(*(lpinfo+1),*lpinfo);
imgheight=MAKEWORD(*(lpinfo+4),*(lpinfo+3));
imgwidth=MAKEWORD(*(lpinfo+6),*(lpinfo+5));
LocalUnlock(hinfo);
LocalFree(hinfo);
return;
case M_EOI:
showerror(FUNC_FORMAT_ERROR);
LocalUnlock(hinfo);
LocalFree(hinfo);
imgwidth=0;
imgheight=0;
return;
default:
if ((id&0xf0)!=0xd0)
{
llength=MAKEWORD(*(lpinfo+1),*lpinfo);
lpinfo+=llength;
count+=llength;
}
else
{
lpinfo+=2;
count+=2;
}
break;
} //switch
} //while
showerror(FUNC_FORMAT_ERROR);
LocalUnlock(hinfo);
LocalFree(hinfo);
imgwidth=0;
imgheight=0;
return;
}
///////////////////////////////////////////////////////////////////////
void Initialize_Fast_IDCT()
{
short i;
iclp = iclip+512;
for (i= -512; i<512; i++)
iclp[i] = (i<-256) ? -256 : ((i>255) ? 255 : i);
}
////////////////////////////////////////////////////////////////////////
static void idctrow(int * blk)
{
int x0, x1, x2, x3, x4, x5, x6, x7, x8;
/* intcut */
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;
}
//////////////////////////////////////////////////////////////////////////////
// column (vertical) IDCT
//
// 7 pi 1
// dst[8*k] = sum c[l] * src[8*l] * cos( -- * ( k + - ) * l )
// l=0 8 2
//
// where: c[0] = 1/1024
// c[1..7] = (1/1024)*sqrt(2)
//
static void idctcol(int * blk)
{
int x0, x1, x2, x3, x4, x5, x6, x7, x8;
/* intcut */
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 + -