#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <linux/types.h>
#include <linux/videodev2.h>
#include <malloc.h>
#include <math.h>
#include <string.h>
#include <sys/mman.h>
//arm-linux-gcc image_get.c -o image_get-arm
//以下程序用于将jpg格式图片转化为bmp格式,15-585行号
#define PI 3.1415927
#define widthbytes(i) ((i+31)/32*4)
short sampleYH,sampleYV,sampleUH,sampleUV,sampleVH,sampleVV;
short HYtoU,VYtoU,HYtoV,VYtoV,YinMCU,UinMCU,VinMCU;
short compressnum=0,Qt[3][64],*YQt,*UQt,*VQt,codepos[4][16],codelen[4][16];
unsigned char compressindex[3],YDCindex,YACindex,UVDCindex,UVACindex;
unsigned char HufTabindex,And[9]={0,1,3,7,0xf,0x1f,0x3f,0x7f,0xff};
unsigned short codevalue[4][256],hufmax[4][16],hufmin[4][16];
short bitpos=0,curbyte=0,run=0,value=0,MCUbuffer[10*64],blockbuffer[64];
short ycoef=0,ucoef=0,vcoef=0,intervalflag=0,interval=0,restart=0;
int Y[4*64],U[4*64],V[4*64],QtZMCUbuffer[10*64];
unsigned int imgwidth=0,imgheight=0,width=0,height=0,linebytes;
short Z[8][8]={{0,1,5,6,14,15,27,28},{2,4,7,13,16,26,29,42},
{3,8,12,17,25,30,41,43},{9,11,18,24,31,40,44,53},
{10,19,23,32,39,45,52,54},{20,22,33,38,46,51,55,60},
{21,34,37,47,50,56,59,61},{35,36,48,49,57,58,62,63}};
struct HEAD{
unsigned int size;
unsigned int reserved;
unsigned int offset;
}head;
struct BMP{
unsigned int size;
unsigned int width;
unsigned int height;
unsigned short plane;
unsigned short bitcount;
unsigned int compression;
unsigned int imagesize;
unsigned int xpels;
unsigned int ypels;
unsigned int colorused;
unsigned int colorimportant;
}bmp;
void error(char *s)
{
printf("%s\n",s);
exit(1);
}
void makebmpheader(FILE *fp)
{
unsigned short i,j;
unsigned int colorbits,imagebytes;
unsigned short type=0x4d42;
colorbits=24;
linebytes=widthbytes(colorbits*imgwidth);
imagebytes=(unsigned int)imgheight*linebytes;
fwrite(&type,sizeof(type),1,fp);
head.size=imagebytes+0x36;
head.reserved=0;
head.offset=0x36;
fwrite(&head,sizeof(head),1,fp);
bmp.size=0x28;
bmp.width=(int)imgwidth;
bmp.height=(int)imgheight;
bmp.plane=1L;
bmp.bitcount=colorbits;
bmp.compression=0;
bmp.imagesize=imagebytes;
bmp.xpels=0xece;
bmp.ypels=0xec4;
bmp.colorused=0;
bmp.colorimportant=0;
fwrite(&bmp,sizeof(bmp),1,fp);
for(j=0;j<imgheight;j++)
for(i=0;i<linebytes;i++)
fputc(0,fp);
}
void initialize(FILE *fp)
{
unsigned char *p,*q,hfindex,qtindex,number;
short i,j,k,finish=0,huftab1,huftab2,huftabindex,count;
unsigned short length,flag;
fread(&flag,sizeof(unsigned short),1,fp);
printf("%x\n",flag);
if(flag!=0xd8ff)
error("Error Jpg File format!1");
while(!finish) {
fread(&flag,sizeof(unsigned short),1,fp);
fread(&length,sizeof(short),1,fp);
length=((length<<8)|(length>>8))-2;
switch(flag) {
case 0xe0ff:
fseek(fp,length,1);break;
case 0xdbff:
p=(unsigned char*)malloc(length);
fread(p,length,1,fp);
qtindex=(*p)&0x0f;
q=p+1;
if(length+2<80)
for(i=0;i<64;i++)
Qt[qtindex][i]=(short)*(q++);
else {
for(i=0;i<64;i++)
Qt[qtindex][i]=(short)*(q++);
qtindex=*(q++)&0x0f;
for(i=0;i<64;i++)
Qt[qtindex][i]=(short)*(q++);
}
free(p);break;
case 0xc0ff:
p=(unsigned char*)malloc(length);
fread(p,length,1,fp);
imgheight=((*(p+1))<<8)+(*(p+2));
imgwidth=((*(p+3))<<8)+(*(p+4));
compressnum=*(p+5);
if((compressnum!=1)&&(compressnum!=3))
error("Error Jpg File format!2");
if(compressnum==3) {
compressindex[0]=*(p+6);
sampleYH=(*(p+7))>>4;
sampleYV=(*(p+7))&0x0f;
YQt=(short *)Qt[*(p+8)];
compressindex[1]=*(p+9);
sampleUH=(*(p+10))>>4;
sampleUV=(*(p+10))&0x0f;
UQt=(short *)Qt[*(p+11)];
compressindex[2]=*(p+12);
sampleVH=(*(p+13))>>4;
sampleVV=(*(p+13))&0x0f;
VQt=(short *)Qt[*(p+14)];
}
else {
compressindex[0]=*(p+6);
sampleYH=(*(p+7))>>4;
sampleYV=(*(p+7))&0x0f;
YQt=(short *)Qt[*(p+8)];
compressindex[1]=*(p+6);
sampleUH=1;
sampleUV=1;
UQt=(short *)Qt[*(p+8)];
compressindex[2]=*(p+6);
sampleVH=1;
sampleVV=1;
VQt=(short *)Qt[*(p+8)];
}
free(p);break;
case 0xc4ff:
p=(unsigned char*)malloc(length+1);
fread(p,length,1,fp);
p[length]=0xff;
if(length+2<0xd0) {
huftab1=(short)(*p)>>4;
huftab2=(short)(*p)&0x0f;
huftabindex=huftab1*2+huftab2;
q=p+1;
for(i=0;i<16;i++)
codelen[huftabindex][i]=(short)(*(q++));
j=0;
for(i=0;i<16;i++)
if(codelen[huftabindex][i]!=0) {
k=0;
while(k<codelen[huftabindex][i]) {
codevalue[huftabindex][k+j]=(short)(*(q++));
k++;
}
j+=k;
}
i=0;
while(codelen[huftabindex][i]==0) i++;
for(j=0;j<i;j++) {
hufmin[huftabindex][j]=0;
hufmax[huftabindex][j]=0;
}
hufmin[huftabindex][i]=0;
hufmax[huftabindex][i]=codelen[huftabindex][i]-1;
for(j=i+1;j<16;j++) {
hufmin[huftabindex][j]=(hufmax[huftabindex][j-1]+1)<<1;
hufmax[huftabindex][j]=hufmin[huftabindex][j]+codelen[huftabindex][j]-1;
}
codepos[huftabindex][0]=0;
for(j=1;j<16;j++)
codepos[huftabindex][j]=codelen[huftabindex][j-1]+codepos[huftabindex][j-1];
}
else {
hfindex=*p;
while(hfindex!=0xff) {
huftab1=(short)hfindex>>4;
huftab2=(short)hfindex&0x0f;
huftabindex=huftab1*2+huftab2;
q=p+1;
count=0;
for(i=0;i<16;i++) {
codelen[huftabindex][i]=(short)(*(q++));
count+=codelen[huftabindex][i];
}
count+=17;
j=0;
for(i=0;i<16;i++)
if(codelen[huftabindex][i]!=0) {
k=0;
while(k<codelen[huftabindex][i]) {
codevalue[huftabindex][k+j]=(short)(*(q++));
k++;
}
j+=k;
}
i=0;
while(codelen[huftabindex][i]==0) i++;
for(j=0;j<i;j++) {
hufmin[huftabindex][j]=0;
hufmax[huftabindex][j]=0;
}
hufmin[huftabindex][i]=0;
hufmax[huftabindex][i]=codelen[huftabindex][i]-1;
for(j=i+1;j<16;j++) {
hufmin[huftabindex][j]=(hufmax[huftabindex][j-1]+1)<<1;
hufmax[huftabindex][j]=hufmin[huftabindex][j]+codelen[huftabindex][j]-1;
}
codepos[huftabindex][0]=0;
for(j=1;j<16;j++)
codepos[huftabindex][j]=codelen[huftabin