⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdi.c

📁 一个自己编的将bmp压缩成jpeg格式图片并由串口与MD卡存取的程序。
💻 C
📖 第 1 页 / 共 4 页
字号:
/*
头8字节,文件名称,3字节文件后缀,1字节属性
EF  高6位小时5位分 低5位秒
10-13 文件时间
14 15FAT高占用 1A 1B FAT占用
1C-1F 文件长度
*/

#include <stdio.h>
#include <string.h>
#include "def.h"
#include "option.h"
#include "2440addr.h"
#include "2440lib.h"
#include "sdi.h"

//YH 0811#define INICLK	300000
#define INICLK	300000
//#define NORCLK	25000000
#define SDCLK	24000000	//PCLK=49.392MHz
#define MMCCLK	15000000	//PCLK=49.392MHz

#define POL	0
#define INT	1
#define DMA	2

int CMD13(void);    // Send card status
int CMD9(void);
// Global variables
//extern int CAM_FRAMEBUFFER_P;

unsigned int *Tx_buffer;	//128[word]*16[blk]=8192[byte]
unsigned int *Rx_buffer;	//128[word]*16[blk]=8192[byte]
volatile unsigned int rd_cnt;
volatile unsigned int wt_cnt;
volatile unsigned int block;
volatile unsigned int TR_end=0;
U32 timercount;
U32 timerc;
U16 year ;
U8 month, day ;		// week
U8 hour, minute, second ;

union datafile
 {unsigned int l1data[128];
  char b1data[512];
 } RWFDATA;
union fatdata
 {unsigned int l1data[512];
  char b1data[2048];
 }FATDATA;
 union jpegdata
 {unsigned int l1data[230800];
  char b1data[921600];
 }JPEG1;
 union firdata
 {U8 b1data[512];
 U32 l1data[128];
 }firstdata;
unsigned int DDATA[512];
unsigned int leng1;
/*
定义文件名称结构
*/
struct filenamedata
{char name[11];
char attrib;
char reserved;
char mtec;
unsigned long date1;
unsigned short date2;
unsigned short highcluster;
unsigned long date3;
unsigned short lowcluster;
unsigned long length1;
};
/*
存储读出文件名称
*/
union filedata
{unsigned int ldata[216];
unsigned char cdata[1024];
struct filenamedata dataa[32];
}FILE_DATA;
int Wide=0; // 0:1bit, 1:4bit
int MMC=0;  // 0:SD  , 1:MMC

int  Maker_ID;
char Product_Name[7]; 
int  Serial_Num;

volatile int RCA;
static int number; 
 U8 *tupian1;
 U8 *tupiany;
 U8 *tupianu;
 U8 *tupianv;
#define writebyte(b) JPEG1.b1data[number]=b;number=number+1;
//#define writeword(w) writebyte((w)/256);writebyte((w)%256);
#define writeword(w) writebyte((w)>>8);writebyte((w));
// A BMP truecolor to JPEG encoder
// Copyright 1999 Cristi Cuturicu

#include "jtypes.h"
#include "jtables.h"
#include "jglobals.h"


void write_APP0info()
//Nothing to overwrite for APP0info
{
 writeword(APP0info.marker);
 writeword(APP0info.length);
 writebyte('J');writebyte('F');writebyte('I');writebyte('F');writebyte(0);
 writebyte(APP0info.versionhi);writebyte(APP0info.versionlo);
 writebyte(APP0info.xyunits);
 writeword(APP0info.xdensity);writeword(APP0info.ydensity);
 writebyte(APP0info.thumbnwidth);writebyte(APP0info.thumbnheight);
}

void write_SOF0info()
// We should overwrite width and height
{
 writeword(SOF0info.marker);
 writeword(SOF0info.length);
 writebyte(SOF0info.precision);
 writeword(SOF0info.height);writeword(SOF0info.width);
 writebyte(SOF0info.nrofcomponents);
 writebyte(SOF0info.IdY);writebyte(SOF0info.HVY);writebyte(SOF0info.QTY);
 writebyte(SOF0info.IdCb);writebyte(SOF0info.HVCb);writebyte(SOF0info.QTCb);
 writebyte(SOF0info.IdCr);writebyte(SOF0info.HVCr);writebyte(SOF0info.QTCr);
}

void write_DQTinfo()
{
 U8 i;
 writeword(DQTinfo.marker);
 writeword(DQTinfo.length);
 writebyte(DQTinfo.QTYinfo);
 for (i=0;i<64;i++) 
 {writebyte(DQTinfo.Ytable[i]);

 }
 writebyte(DQTinfo.QTCbinfo);
 for (i=0;i<64;i++) 
 {writebyte(DQTinfo.Cbtable[i]);
 }
}

void set_quant_table(U8 *basic_table,U8 scale_factor,U8 *newtable)
// Set quantization table and zigzag reorder it
{
 U8 i;
 long temp;
 
 for (i = 0; i < 64; i++) 
{ newtable[zigzag[i]]=(U8)basic_table[i];}
/*
 {
      temp = ((long) basic_table[i] * scale_factor + 50L) / 100L;
	// limit the values to the valid range 
    if (temp <= 0L) temp = 1L;
    if (temp > 255L) temp = 255L; // limit to baseline range if requested 
 
    newtable[zigzag[i]] = (U8) temp;
			  }

*/
}

void set_DQTinfo()
{
 U8 scalefactor=50;// scalefactor controls the visual quality of the image
			 // the smaller is, the better image we'll get, and the smaller
			 // compression we'll achieve
 DQTinfo.marker=0xFFDB;
 DQTinfo.length=132;
 DQTinfo.QTYinfo=0;
 DQTinfo.QTCbinfo=1;
 set_quant_table(std_luminance_qt,scalefactor,DQTinfo.Ytable);
 set_quant_table(std_chrominance_qt,scalefactor,DQTinfo.Cbtable);
}

void write_DHTinfo()
{
 U8 i;
 writeword(DHTinfo.marker);
 writeword(DHTinfo.length);
 writebyte(DHTinfo.HTYDCinfo);
 for (i=0;i<16;i++) 
 { writebyte(DHTinfo.YDC_nrcodes[i]);}
 for (i=0;i<=11;i++)
 { writebyte(DHTinfo.YDC_values[i]);}
 writebyte(DHTinfo.HTYACinfo);
 for (i=0;i<16;i++)  
 {writebyte(DHTinfo.YAC_nrcodes[i]);}
 for (i=0;i<=161;i++) 
 {writebyte(DHTinfo.YAC_values[i]);}
 writebyte(DHTinfo.HTCbDCinfo);
 for (i=0;i<16;i++)
   {writebyte(DHTinfo.CbDC_nrcodes[i]);}
 for (i=0;i<=11;i++) 
  {writebyte(DHTinfo.CbDC_values[i]);}
 writebyte(DHTinfo.HTCbACinfo);
 for (i=0;i<16;i++)  
 {writebyte(DHTinfo.CbAC_nrcodes[i]);}
 for (i=0;i<=161;i++)
  {writebyte(DHTinfo.CbAC_values[i]);}
}

void set_DHTinfo()
{
 U8 i;
 DHTinfo.marker=0xFFC4;
 DHTinfo.length=0x01A2;
 DHTinfo.HTYDCinfo=0;
 for (i=0;i<16;i++) 
 { DHTinfo.YDC_nrcodes[i]=std_dc_luminance_nrcodes[i+1];}
 for (i=0;i<=11;i++) 
 { DHTinfo.YDC_values[i]=std_dc_luminance_values[i];}
 DHTinfo.HTYACinfo=0x10;
 for (i=0;i<16;i++) 
 { DHTinfo.YAC_nrcodes[i]=std_ac_luminance_nrcodes[i+1];}
 for (i=0;i<=161;i++) 
 {DHTinfo.YAC_values[i]=std_ac_luminance_values[i];}
 DHTinfo.HTCbDCinfo=1;
 for (i=0;i<16;i++)
 {  DHTinfo.CbDC_nrcodes[i]=std_dc_chrominance_nrcodes[i+1];}
 for (i=0;i<=11;i++)  
 {DHTinfo.CbDC_values[i]=std_dc_chrominance_values[i];}
 DHTinfo.HTCbACinfo=0x11;
 for (i=0;i<16;i++) 
 { DHTinfo.CbAC_nrcodes[i]=std_ac_chrominance_nrcodes[i+1];}
 for (i=0;i<=161;i++)
 { DHTinfo.CbAC_values[i]=std_ac_chrominance_values[i];}
}

void write_SOSinfo()
//Nothing to overwrite for SOSinfo
{
 writeword(SOSinfo.marker);
 writeword(SOSinfo.length);
 writebyte(SOSinfo.nrofcomponents);
 writebyte(SOSinfo.IdY);writebyte(SOSinfo.HTY);
 writebyte(SOSinfo.IdCb);writebyte(SOSinfo.HTCb);
 writebyte(SOSinfo.IdCr);writebyte(SOSinfo.HTCr);
 writebyte(SOSinfo.Ss);writebyte(SOSinfo.Se);writebyte(SOSinfo.Bf);
}

void write_comment(U8 *comment)
{
 U16 i,length;
 writeword(0xFFFE); //The COM marker
 length=strlen((const char *)comment);
 writeword(length+2);
 for (i=0;i<length;i++) 
 {writebyte(comment[i]);}
}

void writebits(bitstring bs)
// A portable version; it should be done in assembler
{
 U16 value;
 S8 posval;//bit position in the bitstring we read, should be<=15 and >=0
 value=bs.value;
 posval=bs.length-1;
 while (posval>=0)
  
  {
   if (value & mask[posval]) bytenew|=mask[bytepos];
   posval--;bytepos--;
   if (bytepos<0) { if (bytenew==0xFF) {writebyte(0xFF);writebyte(0);}
		     else {writebyte(bytenew);}
		    bytepos=7;bytenew=0;
		  }
  }

}

void compute_Huffman_table(U8 *nrcodes,U8 *std_table,bitstring *HT)
{
 U8 k,j;
 U8 pos_in_table;
 U16 codevalue;
 codevalue=0; pos_in_table=0;
 for (k=1;k<=16;k++)
   {
     for (j=1;j<=nrcodes[k];j++) {HT[std_table[pos_in_table]].value=codevalue;
				  HT[std_table[pos_in_table]].length=k;
				  pos_in_table++;
				  codevalue++;
				 }
     codevalue*=2;
   }
}
void init_Huffman_tables()
{
 compute_Huffman_table(std_dc_luminance_nrcodes,std_dc_luminance_values,YDC_HT);
 compute_Huffman_table(std_dc_chrominance_nrcodes,std_dc_chrominance_values,CbDC_HT);
 compute_Huffman_table(std_ac_luminance_nrcodes,std_ac_luminance_values,YAC_HT);
 compute_Huffman_table(std_ac_chrominance_nrcodes,std_ac_chrominance_values,CbAC_HT);
}

//void exitmessage(char *error_message)
//{
 //printf("%s\n",error_message);exit(EXIT_FAILURE);
//}

void set_numbers_category_and_bitcode()
{
 S32 nr;
 S32 nrlower,nrupper;
 U8 cat,value;

 category_alloc=(U8 *)malloc(65535*sizeof(U8));
 //if (category_alloc==NULL) exitmessage("Not enough memory.");
 category=category_alloc+32767; //allow negative subscripts
 bitcode_alloc=(bitstring *)malloc(65535*sizeof(bitstring));
// if (bitcode_alloc==NULL) exitmessage("Not enough memory.");
 bitcode=bitcode_alloc+32767;
 nrlower=1;nrupper=2;
 for (cat=1;cat<=15;cat++) {
				 //Positive numbers
				 for (nr=nrlower;nr<nrupper;nr++)
				  { category[nr]=cat;
				bitcode[nr].length=cat;
				bitcode[nr].value=(U16)nr;
				  }
				 //Negative numbers
				 for (nr=-(nrupper-1);nr<=-nrlower;nr++)
				  { category[nr]=cat;
				bitcode[nr].length=cat;
				bitcode[nr].value=(U16)(nrupper-1+nr);
				  }
				 nrlower<<=1;
				 nrupper<<=1;
			   }
}

/*
void precalculate_YCbCr_tables()
{
 U16 R,G,B;
 for (R=0;R<=255;R++) {YRtab[R]=(S32)(65536*0.299+0.5)*R;
			   CbRtab[R]=(S32)(65536*-0.16874+0.5)*R;
			   CrRtab[R]=(S32)(32768)*R;
			  }
 for (G=0;G<=255;G++) {YGtab[G]=(S32)(65536*0.587+0.5)*G;
			   CbGtab[G]=(S32)(65536*-0.33126+0.5)*G;
			   CrGtab[G]=(S32)(65536*-0.41869+0.5)*G;
			  }
 for (B=0;B<=255;B++) {YBtab[B]=(S32)(65536*0.114+0.5)*B;
			   CbBtab[B]=(S32)(32768)*B;
			   CrBtab[B]=(S32)(65536*-0.08131+0.5)*B;
			  }
}
*/
// Using a bit modified form of the FDCT routine from IJG's C source:
// Forward DCT routine idea taken from Independent JPEG Group's C source for
// JPEG encoders/decoders

/* For float AA&N IDCT method, divisors are equal to quantization
   coefficients scaled by scalefactor[row]*scalefactor[col], where
   scalefactor[0] = 1
   scalefactor[k] = cos(k*PI/16) * sqrt(2)    for k=1..7
   We apply a further scale factor of 8.
   What's actually stored is 1/divisor so that the inner loop can
   use a multiplication rather than a division. */
void prepare_quant_tables()
{
 double aanscalefactor[8] = {1.0, 1.387039845, 1.306562965, 1.175875602,
			   1.0, 0.785694958, 0.541196100, 0.275899379};
 U8 row, col;
 U8 i = 0;
 for (row = 0; row < 8; row++)
 {
   for (col = 0; col < 8; col++)
     {
       fdtbl_Y[i] = (float) (1.0 / ((double) DQTinfo.Ytable[zigzag[i]] * aanscalefactor[row] * aanscalefactor[col] * 8.0));
       fdtbl_Cb[i] = (float) (1.0 / ((double) DQTinfo.Cbtable[zigzag[i]] * aanscalefactor[row] * aanscalefactor[col] * 8.0));
	   i++;
     }
 }
}

void fdct_and_quantization(S8 *data,float *fdtbl,S16 *outdata)
{
  S32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  S32 tmp10, tmp11, tmp12, tmp13;
  float z1, z2, z3, z4, z5, z11, z13;
  float *dataptr;
  float datafloat[64];
  float temp;
  S8 ctr;
  U8 i;
  for (i=0;i<64;i++)
  { datafloat[i]=data[i];}

  /* Pass 1: process rows. */
  dataptr=datafloat;
  for (ctr = 7; ctr >= 0; ctr--) {
	tmp0 = dataptr[0] + dataptr[7];
    tmp7 = dataptr[0] - dataptr[7];
    tmp1 = dataptr[1] + dataptr[6];
    tmp6 = dataptr[1] - dataptr[6];
    tmp2 = dataptr[2] + dataptr[5];
    tmp5 = dataptr[2] - dataptr[5];
    tmp3 = dataptr[3] + dataptr[4];
    tmp4 = dataptr[3] - dataptr[4];

	/* Even part */

    tmp10 = tmp0 + tmp3;	/* phase 2 */
    tmp13 = tmp0 - tmp3;
    tmp11 = tmp1 + tmp2;
    tmp12 = tmp1 - tmp2;

    dataptr[0] = tmp10 + tmp11; /* phase 3 */
    dataptr[4] = tmp10 - tmp11;

    //z1 = (tmp12 + tmp13) * ((float) 0.707106781); /* c4 */
    z1 = (((tmp12 + tmp13) * 46341)>>16);//((float) 0.707106781); /* c4 */
	dataptr[2] = tmp13 + z1;	/* phase 5 */
    dataptr[6] = tmp13 - z1;

    /* Odd part */

    tmp10 = tmp4 + tmp5;	/* phase 2 */
    tmp11 = tmp5 + tmp6;
    tmp12 = tmp6 + tmp7;

	/* The rotator is modified from fig 4-8 to avoid extra negations. */
    //z5 = (tmp10 - tmp12) * ((float) 0.382683433); /* c6 */
    //z2 = ((float) 0.541196100) * tmp10 + z5; /* c2-c6 */
    //z4 = ((float) 1.306562965) * tmp12 + z5; /* c2+c6 */
    //z3 = tmp11 * ((float) 0.707106781); /* c4 */
    z5 = ((tmp10 - tmp12) * 25080)>>16;//((float) 0.382683433); /* c6 */
    z2 = ((35468 * tmp10)>>16) + z5; /* c2-c6 */
    z4 = ((21407 * tmp12)>>14) + z5; /* c2+c6 */
    z3 = (tmp11 * 46341)>>16; /* c4 */
    z11 = tmp7 + z3;		/* phase 5 */
    z13 = tmp7 - z3;

    dataptr[5] = z13 + z2;	/* phase 6 */
    dataptr[3] = z13 - z2;
	dataptr[1] = z11 + z4;
    dataptr[7] = z11 - z4;

    dataptr += 8;		/* advance pointer to next row */
  }

  /* Pass 2: process columns. */

  dataptr = datafloat;
  for (ctr = 7; ctr >= 0; ctr--) {
    tmp0 = dataptr[0] + dataptr[56];
    tmp7 = dataptr[0] - dataptr[56];
    tmp1 = dataptr[8] + dataptr[48];
    tmp6 = dataptr[8] - dataptr[48];
    tmp2 = dataptr[16] + dataptr[40];
    tmp5 = dataptr[16] - dataptr[40];
    tmp3 = dataptr[24] + dataptr[32];
    tmp4 = dataptr[24] - dataptr[32];

    /* Even part */

    tmp10 = tmp0 + tmp3;	/* phase 2 */
    tmp13 = tmp0 - tmp3;
    tmp11 = tmp1 + tmp2;
    tmp12 = tmp1 - tmp2;

    dataptr[0] = tmp10 + tmp11; /* phase 3 */
    dataptr[32] = tmp10 - tmp11;

	z1 = ((tmp12 + tmp13) * 46341)>>16; /* c4 */
    dataptr[16] = tmp13 + z1; /* phase 5 */
    dataptr[48] = tmp13 - z1;

    /* Odd part */

    tmp10 = tmp4 + tmp5;	/* phase 2 */
    tmp11 = tmp5 + tmp6;
    tmp12 = tmp6 + tmp7;

    /* The rotator is modified from fig 4-8 to avoid extra negations. */
	//z5 = (tmp10 - tmp12) * ((float) 0.382683433); /* c6 */
    //z2 = ((float) 0.541196100) * tmp10 + z5; /* c2-c6 */
    //z4 = ((float) 1.306562965) * tmp12 + z5; /* c2+c6 */
    //z3 = tmp11 * ((float) 0.707106781); /* c4 */
	
	z5 = ((tmp10 - tmp12) *25080)>>16;// ((float) 0.382683433); /* c6 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -