/*
#include "jpeg.h"
#include "memory.h"
#include "math.h"
#include "stdio.h"
#include "windows.h"
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "jpeg.h"
#include "header.h"
//macro definition
#define WIDTHBYTES(i) ((i+31)/32*4)
#define PI 3.1415926535
//define return value of function
#define FUNC_OK 0
#define FUNC_MEMORY_ERROR 1
#define FUNC_FILE_ERROR 2
#define FUNC_FORMAT_ERROR 3
//////////////////////////////////////////////////
//Jpeg functions
char* LoadJpegFile(char *jpegbuf,char *bmpbuf);
void showerror(int funcret);
int InitTag();
void InitTable();
int Decode();
int DecodeMCUBlock();
int HufBlock(unsigned char dchufindex,unsigned char achufindex);
int DecodeElement();
void IQtIZzMCUComponent(short flag);
void IQtIZzBlock(short *s ,int * d,short flag);
void GetYUV(short flag);
void StoreBuffer();
BYTE ReadByte();
void Initialize_Fast_IDCT();
void Fast_IDCT(int * block);
void idctrow(int * blk);
void idctcol(int * blk);
//////////////////////////////////////////////////
//global variable declaration
BITMAPFILEHEADER bf;
BITMAPINFOHEADER bi;
void* hImgData=NULL;
DWORD NumColors;
DWORD LineBytes;
DWORD ImgWidth=0 , ImgHeight=0;
unsigned int PcxBytesPerLine;
LPSTR lpPtr;
//////////////////////////////////////////////////
//variables used in jpeg function
short SampRate_Y_H,SampRate_Y_V;
short SampRate_U_H,SampRate_U_V;
short SampRate_V_H,SampRate_V_V;
short H_YtoU,V_YtoU,H_YtoV,V_YtoV;
short Y_in_MCU,U_in_MCU,V_in_MCU;
unsigned char *lpJpegBuf;
unsigned char *lp;
short qt_table[3][64];
short comp_num;
BYTE comp_index[3];
BYTE YDcIndex,YAcIndex,UVDcIndex,UVAcIndex;
BYTE HufTabIndex;
short *YQtTable,*UQtTable,*VQtTable;
BYTE And[9]={0,1,3,7,0xf,0x1f,0x3f,0x7f,0xff};
short code_pos_table[4][16],code_len_table[4][16];
unsigned short code_value_table[4][256];
unsigned short huf_max_value[4][16],huf_min_value[4][16];
short BitPos,CurByte;
short rrun,vvalue;
short MCUBuffer[10*64];
int QtZzMCUBuffer[10*64];
short BlockBuffer[64];
short ycoef,ucoef,vcoef;
BOOL IntervalFlag;
short interval=0;
int Y[4*64],U[4*64],V[4*64];
DWORD sizei,sizej;
short restart;
static long iclip[1024];
static long *iclp;
////////////////////////////////////////////////////////////////
char* LoadJpegFile (char *jpegbuf, char *bmpbuf)
{
printf("load 0 \n");
DWORD ImgSize;
DWORD JpegBufSize;
int funcret;
LPBITMAPINFOHEADER lpImgData;
//JpegBufSize=size;
lpJpegBuf=jpegbuf;
InitTable();
printf("load 0.1 \n");
if((funcret=InitTag())!=FUNC_OK)
{
showerror(funcret);
return NULL;
}
printf("load 1 \n");
//create new bitmapfileheader and bitmapinfoheader
memset((char *)&bf,0,sizeof(BITMAPFILEHEADER));
memset((char *)&bi,0,sizeof(BITMAPINFOHEADER));
printf("load 2 \n");
bi.biSize=(DWORD)sizeof(BITMAPINFOHEADER);
bi.biWidth=(LONG)(ImgWidth);
bi.biHeight=(LONG)(ImgHeight);
bi.biPlanes=1;
bi.biBitCount=24;
bi.biClrUsed=0;
bi.biClrImportant=0;
bi.biCompression=BI_RGB;
NumColors=0;
LineBytes=(DWORD)WIDTHBYTES(bi.biWidth*bi.biBitCount);
ImgSize=(DWORD)LineBytes*bi.biHeight;
bf.bfType=0x4d42;
bf.bfSize=/*sizeof(BITMAPFILEHEADER)*/ 14+sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+ImgSize;
bf.bfOffBits=(DWORD)(NumColors*sizeof(RGBQUAD)+14/*sizeof(BITMAPFILEHEADER)*/+sizeof(BITMAPINFOHEADER));
lpPtr = (char*)bmpbuf + 54;
printf("load 3 \n");
if((SampRate_Y_H==0)||(SampRate_Y_V==0))
{
showerror(FUNC_FORMAT_ERROR);
return NULL ;
}
funcret=Decode();
memcpy(bmpbuf, &bf.bfType,sizeof(WORD));
memcpy(bmpbuf+2, ((char*)&bf)+4, 12);
memcpy(bmpbuf+14, (LPSTR)&bi,sizeof(BITMAPINFOHEADER));
if(funcret!=FUNC_OK)
{
showerror(funcret);
return NULL;
}
return lpPtr;
}
/////////////////////////////////////////////////
void showerror(int funcret)
{
switch(funcret)
{
case FUNC_MEMORY_ERROR:
printf("Error alloc memory!\n");
exit(1);
break;
case FUNC_FILE_ERROR:
printf("File not found!!\n");
exit(1);
break;
case FUNC_FORMAT_ERROR:
printf("File format error!\n");
exit(1);
break;
}
}
////////////////////////////////////////////////////////////////////////////////
int InitTag()
{
BOOL finish=FALSE;
BYTE id;
short llength;
short i,j,k;
short huftab1,huftab2;
short huftabindex;
BYTE hf_table_index;
BYTE qt_table_index;
BYTE comnum;
unsigned char *lptemp;
short ccount;
printf("init tag 1 \n");
lp=lpJpegBuf+2;
while (!finish)
{
//printf("init tag 2 \n");
id=*(lp+1);
// printf("id = 0x%x\n",id);
//printf("init tag 3 \n");
lp+=2;
//printf("init tag 4 \n");
switch (id)
{
case M_APP0:
printf("M_APP0\n");
llength=MAKEWORD(*(lp+1),*lp);
lp+=llength;
break;
case M_DQT:
printf("M_DQT\n");
llength=MAKEWORD(*(lp+1),*lp);
qt_table_index=(*(lp+2))&0x0f;
lptemp=lp+3;
if(llength<80)
{
for(i=0;i<64;i++)
qt_table[qt_table_index][i]=(short)*(lptemp++);
}
else
{
for(i=0;i<64;i++)
qt_table[qt_table_index][i]=(short)*(lptemp++);
qt_table_index=(*(lptemp++))&0x0f;
for(i=0;i<64;i++)
qt_table[qt_table_index][i]=(short)*(lptemp++);
}
lp+=llength;
break;
case M_SOF0:
printf("M_SOF0\n");
llength=MAKEWORD(*(lp+1),*lp);
ImgHeight=MAKEWORD(*(lp+4),*(lp+3));
ImgWidth=MAKEWORD(*(lp+6),*(lp+5));
comp_num=*(lp+7);
if((comp_num!=1)&&(comp_num!=3))
return FUNC_FORMAT_ERROR;
if(comp_num==3)
{
comp_index[0]=*(lp+8);
SampRate_Y_H=(*(lp+9))>>4;
SampRate_Y_V=(*(lp+9))&0x0f;
YQtTable=(short *)qt_table[*(lp+10)];
comp_index[1]=*(lp+11);
SampRate_U_H=(*(lp+12))>>4;
SampRate_U_V=(*(lp+12))&0x0f;
UQtTable=(short *)qt_table[*(lp+13)];
comp_index[2]=*(lp+14);
SampRate_V_H=(*(lp+15))>>4;
SampRate_V_V=(*(lp+15))&0x0f;
VQtTable=(short *)qt_table[*(lp+16)];
}
else
{
comp_index[0]=*(lp+8);
SampRate_Y_H=(*(lp+9))>>4;
SampRate_Y_V=(*(lp+9))&0x0f;
YQtTable=(short *)qt_table[*(lp+10)];
comp_index[1]=*(lp+8);
SampRate_U_H=1;
SampRate_U_V=1;
UQtTable=(short *)qt_table[*(lp+10)];
comp_index[2]=*(lp+8);
SampRate_V_H=1;
SampRate_V_V=1;
VQtTable=(short *)qt_table[*(lp+10)];
}
lp+=llength;
break;
case M_DHT:
printf("M_DHT\n");
llength=MAKEWORD(*(lp+1),*lp);
if (llength<0xd0)
{
huftab1=(short)(*(lp+2))>>4; //huftab1=0,1
huftab2=(short)(*(lp+2))&0x0f; //huftab2=0,1
huftabindex=huftab1*2+huftab2;
lptemp=lp+3;
for (i=0; i<16; i++)
code_len_table[huftabindex][i]=(short)(*(lptemp++));
j=0;
for (i=0; i<16; i++)
if(code_len_table[huftabindex][i]!=0)
{
k=0;
while(k<code_len_table[huftabindex][i])
{
code_value_table[huftabindex][k+j]=(short)(*(lptemp++));
k++;
}
j+=k;
}
i=0;
while (code_len_table[huftabindex][i]==0)
i++;
for (j=0;j<i;j++)
{
huf_min_value[huftabindex][j]=0;
huf_max_value[huftabindex][j]=0;
}
huf_min_value[huftabindex][i]=0;
huf_max_value[huftabindex][i]=code_len_table[huftabindex][i]-1;
for (j=i+1;j<16;j++)
{
huf_min_value[huftabindex][j]=(huf_max_value[huftabindex][j-1]+1)<<1;
huf_max_value[huftabindex][j]=huf_min_value[huftabindex][j]+code_len_table[huftabindex][j]-1;
}
code_pos_table[huftabindex][0]=0;
for (j=1;j<16;j++)
code_pos_table[huftabindex][j]=code_len_table[huftabindex][j-1]+code_pos_table[huftabindex][j-1];
lp+=llength;
} //if
else
{
hf_table_index=*(lp+2);
lp+=2;
while (hf_table_index!=0xff)
{
huftab1=(short)hf_table_index>>4; //huftab1=0,1
huftab2=(short)hf_table_index&0x0f; //huf