📄 djpeg.c
字号:
if(!(DataCode & (1<<(DataCount-1))) && DataCount !=0){
DataCode |= (~0) << DataCount;
DataCode += 1;
}
//printf(" Use Bit: %d\n",(i + DataCount +1));
BitCount += (i + DataCount +1); // 巊梡偟偨價僢僩悢傪壛嶼偡傞
if(count ==0){
// DC惉暘偺応崌丄僨乕僞偲側傞
if(DataCount ==0) DataCode =0x0; // DataCount偑0側傜僨乕僞偼0偱偁傞
PreData[table] += DataCode; // DC惉暘偼壛嶼偟側偗傟偽側傜側偄
// 媡検巕壔亄僕僌僓僌
BlockData[zigzag_table[count]] =PreData[table]*TableDQT[tabledqt][count];
count ++;
}else{
if(ZeroCount == 0x0 && DataCount == 0x0){
// AC惉暘偱EOB晞崋偑棃偨応崌偼廔椆偡傞
break;
}else if(ZeroCount ==0xF && DataCount == 0x0){
// ZRL晞崋偑棃偨応崌丄15屄偺僛儘僨乕僞偲傒側偡
count += 15;
}else{
count += ZeroCount;
// 媡検巕壔亄僕僌僓僌
BlockData[zigzag_table[count]] = DataCode * TableDQT[tabledqt][count];
count ++;
}
}
}
}
const int C1_16 = 4017; // cos( pi/16) x4096
const int C2_16 = 3784; // cos(2pi/16) x4096
const int C3_16 = 3406; // cos(3pi/16) x4096
const int C4_16 = 2896; // cos(4pi/16) x4096
const int C5_16 = 2276; // cos(5pi/16) x4096
const int C6_16 = 1567; // cos(6pi/16) x4096
const int C7_16 = 799; // cos(7pi/16) x4096
//////////////////////////////////////////////////////////////////////////////
// 媡DCT
void DctDecode(int *BlockIn, int *BlockOut){
int i;
int s0,s1,s2,s3,s4,s5,s6,s7;
int t0,t1,t2,t3,t4,t5,t6,t7;
/*
printf("-----------------------------\n");
printf(" iDCT(In)\n");
printf("-----------------------------\n");
for(i=0;i<64;i++){
printf("%2d: %8x\n",i,BlockIn[i]);
}
*/
for(i=0;i<8;i++) {
s0 = (BlockIn[0] + BlockIn[4]) * C4_16;
s1 = (BlockIn[0] - BlockIn[4]) * C4_16;
s3 = (BlockIn[2] * C2_16) + (BlockIn[6] * C6_16);
s2 = (BlockIn[2] * C6_16) - (BlockIn[6] * C2_16);
s7 = (BlockIn[1] * C1_16) + (BlockIn[7] * C7_16);
s4 = (BlockIn[1] * C7_16) - (BlockIn[7] * C1_16);
s6 = (BlockIn[5] * C5_16) + (BlockIn[3] * C3_16);
s5 = (BlockIn[5] * C3_16) - (BlockIn[3] * C5_16);
/*
printf("s0:%8x\n",s0);
printf("s1:%8x\n",s1);
printf("s2:%8x\n",s2);
printf("s3:%8x\n",s3);
printf("s4:%8x\n",s4);
printf("s5:%8x\n",s5);
printf("s6:%8x\n",s6);
printf("s7:%8x\n",s7);
*/
t0 = s0 + s3;
t3 = s0 - s3;
t1 = s1 + s2;
t2 = s1 - s2;
t4 = s4 + s5;
t5 = s4 - s5;
t7 = s7 + s6;
t6 = s7 - s6;
/*
printf("t0:%8x\n",t0);
printf("t1:%8x\n",t1);
printf("t2:%8x\n",t2);
printf("t3:%8x\n",t3);
printf("t4:%8x\n",t4);
printf("t5:%8x\n",t5);
printf("t6:%8x\n",t6);
printf("t7:%8x\n",t7);
*/
s6 = (t5 + t6) * 181 / 256; // 1/sqrt(2)
s5 = (t6 - t5) * 181 / 256; // 1/sqrt(2)
/*
printf("s5:%8x\n",s5);
printf("s6:%8x\n",s6);
*/
*BlockIn++ = (t0 + t7) >> 11;
*BlockIn++ = (t1 + s6) >> 11;
*BlockIn++ = (t2 + s5) >> 11;
*BlockIn++ = (t3 + t4) >> 11;
*BlockIn++ = (t3 - t4) >> 11;
*BlockIn++ = (t2 - s5) >> 11;
*BlockIn++ = (t1 - s6) >> 11;
*BlockIn++ = (t0 - t7) >> 11;
}
BlockIn -= 64;
/*
printf("-----------------------------\n");
printf(" iDCT(Middle)\n");
printf("-----------------------------\n");
for(i=0;i<64;i++){
printf("%2d: %8x\n",i,BlockIn[i]);
}
*/
for(i=0;i<8;i++){
s0 = (BlockIn[ 0] + BlockIn[32]) * C4_16;
s1 = (BlockIn[ 0] - BlockIn[32]) * C4_16;
s3 = BlockIn[16] * C2_16 + BlockIn[48] * C6_16;
s2 = BlockIn[16] * C6_16 - BlockIn[48] * C2_16;
s7 = BlockIn[ 8] * C1_16 + BlockIn[56] * C7_16;
s4 = BlockIn[ 8] * C7_16 - BlockIn[56] * C1_16;
s6 = BlockIn[40] * C5_16 + BlockIn[24] * C3_16;
s5 = BlockIn[40] * C3_16 - BlockIn[24] * C5_16;
/*
printf("s0:%8x\n",s0);
printf("s1:%8x\n",s1);
printf("s2:%8x\n",s2);
printf("s3:%8x\n",s3);
printf("s4:%8x\n",s4);
printf("s5:%8x\n",s5);
printf("s6:%8x\n",s6);
printf("s7:%8x\n",s7);
*/
t0 = s0 + s3;
t1 = s1 + s2;
t2 = s1 - s2;
t3 = s0 - s3;
t4 = s4 + s5;
t5 = s4 - s5;
t6 = s7 - s6;
t7 = s6 + s7;
/*
printf("t0:%8x\n",t0);
printf("t1:%8x\n",t1);
printf("t2:%8x\n",t2);
printf("t3:%8x\n",t3);
printf("t4:%8x\n",t4);
printf("t5:%8x\n",t5);
printf("t6:%8x\n",t6);
printf("t7:%8x\n",t7);
*/
s5 = (t6 - t5) * 181 / 256; // 1/sqrt(2)
s6 = (t5 + t6) * 181 / 256; // 1/sqrt(2)
/*
printf("s5:%8x\n",s5);
printf("s6:%8x\n",s6);
*/
BlockOut[ 0] = ((t0 + t7) >> 15);
BlockOut[56] = ((t0 - t7) >> 15);
BlockOut[ 8] = ((t1 + s6) >> 15);
BlockOut[48] = ((t1 - s6) >> 15);
BlockOut[16] = ((t2 + s5) >> 15);
BlockOut[40] = ((t2 - s5) >> 15);
BlockOut[24] = ((t3 + t4) >> 15);
BlockOut[32] = ((t3 - t4) >> 15);
BlockIn++;
BlockOut++;
}
BlockOut-=8;
/*
printf("-----------------------------\n");
printf(" iDCT(Out)\n");
printf("-----------------------------\n");
for(i=0;i<8;i++){
printf(" %2d: %04x;\n",i+ 0,BlockOut[i+ 0]&0xFFFF);
printf(" %2d: %04x;\n",i+56,BlockOut[i+56]&0xFFFF);
printf(" %2d: %04x;\n",i+ 8,BlockOut[i+ 8]&0xFFFF);
printf(" %2d: %04x;\n",i+48,BlockOut[i+48]&0xFFFF);
printf(" %2d: %04x;\n",i+16,BlockOut[i+16]&0xFFFF);
printf(" %2d: %04x;\n",i+40,BlockOut[i+40]&0xFFFF);
printf(" %2d: %04x;\n",i+24,BlockOut[i+24]&0xFFFF);
printf(" %2d: %04x;\n",i+32,BlockOut[i+32]&0xFFFF);
}
*/
}
//////////////////////////////////////////////////////////////////////////////
// 4:1:1偺僨僐乕僪張棟
void Decode411(unsigned char *buff, int *BlockY, int *BlockCb, int *BlockCr){
int BlockHuffman[64];
int BlockYLT[64];
int BlockYRT[64];
int BlockYLB[64];
int BlockYRB[64];
unsigned int i;
// 婸搙(嵍忋)
//printf("Block:00\n");
HuffmanDecode(buff,0x00,BlockHuffman);
DctDecode(BlockHuffman,BlockYLT);
// 婸搙(塃忋)
//printf("Block:02\n");
HuffmanDecode(buff,0x00,BlockHuffman);
DctDecode(BlockHuffman,BlockYRT);
// 婸搙(嵍壓)
//printf("Block:03\n");
HuffmanDecode(buff,0x00,BlockHuffman);
DctDecode(BlockHuffman,BlockYLB);
// 婸搙(塃壓)
//printf("Block:04\n");
HuffmanDecode(buff,0x00,BlockHuffman);
DctDecode(BlockHuffman,BlockYRB);
// 惵怓嵎
//printf("Block:10\n");
HuffmanDecode(buff,0x01,BlockHuffman);
DctDecode(BlockHuffman,BlockCb);
// 愒怓嵎
//printf("Block:11\n");
HuffmanDecode(buff,0x02,BlockHuffman);
DctDecode(BlockHuffman,BlockCr);
// 僽儘僢僋僒僀僘傪16x16偵偡傞
for(i=0;i<64;i++){
BlockY[(int)(i/8) *16 +(i % 8)] = BlockYLT[i];
BlockY[(int)(i/8) *16 +(i % 8)+8] = BlockYRT[i];
BlockY[(int)(i/8) *16 +(i % 8)+128] = BlockYLB[i];
BlockY[(int)(i/8) *16 +(i % 8)+128+8] = BlockYRB[i];
}
}
//////////////////////////////////////////////////////////////////////////////
// YUV仺RGB偵曄姺
void DecodeYUV(int *y, int *cb, int *cr, unsigned char *rgb){
int r,g,b;
int p,i;
//printf("----RGB----\n");
for(i=0;i<256;i++){
p = ((int)(i/32) * 8) + ((int)((i % 16)/2));
r = 128 + y[i] + cr[p]*1.402;
r = (r & 0xffffff00) ? (r >> 24) ^ 0xff : r;
g = 128 + y[i] - cb[p]*0.34414 - cr[p]*0.71414;
g = (g & 0xffffff00) ? (g >> 24) ^ 0xff : g;
b = 128 + y[i] + cb[p]*1.772;
b = (b & 0xffffff00) ? (b >> 24) ^ 0xff : b;
rgb[i*3+0] = b;
rgb[i*3+1] = g;
rgb[i*3+2] = r;
/*
printf("[RGB]%3d: %3x,%3x,%3x = %2x,%2x,%2x\n",i,
y[i]&0x1FF,cr[p]&0x1FF,cb[p]&0x1FF,
rgb[i*3+2],rgb[i*3+1],rgb[i*3+0]);
*/
}
}
//////////////////////////////////////////////////////////////////////////////
// 僀儊乕僕偺僨僐乕僪
void Decode(unsigned char *buff,unsigned char *rgb){
int BlockY[256];
int BlockCb[256];
int BlockCr[256];
int x,y,i,p;
for(y=0;y<BuffBlockY;y++){
for(x=0;x<BuffBlockX;x++){
Decode411(buff,BlockY,BlockCb,BlockCr); // 4:1:1偺僨僐乕僪
DecodeYUV(BlockY,BlockCb,BlockCr,rgb); // YUV仺RGB曄姺
for(i=0;i<256;i++){
if((x*16+(i%16)<BuffX) && (y*16+i/16<BuffY)){
p=y*16*BuffX*3+x*16*3+(int)(i/16)*BuffX*3+(i%16)*3;
Buff[p+0] = rgb[i*3+0];
Buff[p+1] = rgb[i*3+1];
Buff[p+2] = rgb[i*3+2];
/*
printf("RGB[%4d,%4d]: %2x,%2x,%2x\n",x*16+(i%16),y*16+i/16,
rgb[i*3+2],rgb[i*3+1],rgb[i*3+0]);
*/
}
}
}
}
}
//////////////////////////////////////////////////////////////////////////////
// 僨僐乕僪
void JpegDecode(unsigned char *buff){
unsigned short data;
unsigned int i;
unsigned int Image =0;
unsigned char RGB[256*3];
while(!(BuffIndex >= BuffSize)){
if(Image ==0){
data = get_word(buff);
switch(data){
case 0xFFD8: // SOI
break;
case 0xFFE0: // APP0
GetAPP0(buff);
break;
case 0xFFDB: // DQT
GetDQT(buff);
break;
case 0xFFC4: // DHT
GetDHT(buff);
break;
case 0xFFC0: // SOF
GetSOF(buff);
break;
case 0xFFDA: // SOS
GetSOS(buff);
Image = 1;
// 僨乕僞偺弨旛
PreData[0] = 0x00;
PreData[1] = 0x00;
PreData[2] = 0x00;
LineData = get_data(buff);
NextData = get_data(buff);
BitCount =0;
break;
case 0xFFD9: // EOI
break;
default:
// 敾暿偱偒側偄僿僢僟乕偼撉傒旘偽偡
if((data & 0xFF00) == 0xFF00 && !(data == 0xFF00)){
data = get_word(buff);
for(i=0;i<data-2;i++){
get_byte(buff);
}
}
break;
}
}else{
// 怢挿(SOS偑棃偰偄傞)
Decode(buff,RGB);
}
}
}
//////////////////////////////////////////////////////////////////////////////
// 儊僀儞娭悢
//////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
unsigned char *buff;
FILE *fp;
if((fp = fopen(argv[1],"rb")) == NULL){
perror(0);
exit(0);
}
// 僼傽僀儖僒僀僘傪庢摼偡傞
BuffSize = 0;
while(!feof(fp)){
fgetc(fp);
BuffSize ++;
}
BuffSize--;
rewind(fp); // 僼傽僀儖億僀儞僞傪嵟弶偵栠偡
buff = (unsigned char *)malloc(BuffSize); // 僶僢僼傽傪妋曐偡傞
fread(buff,1,BuffSize,fp); // 僶僢僼傽偵撉傒崬傓
BuffIndex = 0;
JpegDecode(buff); // JPEG僨僐乕僪偡傞
BmpSave(argv[2],Buff,BuffX,BuffY,3); // Bitmap偵曐懚偡傞
// 慡偰奐曻偟傑偡
fclose(fp);
free(buff);
free(Buff);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -