c语言 写出raw文件,求指导,如何用c语言实现读取*.raw格式图像
該樓層疑似違規已被系統折疊?隱藏此樓查看此樓
/*
** 這個程序是讀取jpg圖像的
** 后續加上jpg圖像打開和存放
*/ #include
#include
#include
#include
#include #define SOI 0xD8 //文件頭
#define EOI 0xD9 //文件尾
#define APP0 0xE0 //定義交換格式和圖像識別信息
#define SOF0 0xC0 //幀開始(標準 JPEG)
#define DQT 0xDB //定義量化表
#define DHT 0xC4 //定義 Huffman 表(霍夫曼表)
#define SOS 0xDA // 掃描行開始
#define DRI 0xDD //定義重新開始間隔
#define COM 0xFE //注釋 unsigned short jpgWidth; //圖像的寬
unsigned short jpgHeight; //圖像的高
unsigned char bitDepth; //圖像類型,每像素位數
unsigned short jpgXpelsPerMeter; //水平分辨率
unsigned short jpgYpelsPerMeter; //垂直分辨率
unsigned char SOF_find=0; char readPath[100]; //圖像路徑
unsigned char CurrentByte; //當前字節
unsigned short CurrentWord; //當前word
unsigned int BytePos; //當前字節的位置 bool readjpg(char *jpgName); //讀取路徑
void showjpg(); //顯示
void GetJpgSize(FILE *fp); //獲取大小 /****************************分割線**************************************/
bool readjpg(char *jpgName)
{
unsigned char Flag_1,Flag_2; //文件頭 FFD8
unsigned short Xdensity,Ydensity;
/*unsigned char SOS_find=0;
unsigned int FileLength;
unsigned char Length;*/
//二進制讀方式打開指定的圖像文件
FILE *fp;
fp=fopen(jpgName,"rb");
if(fp==0)
{
perror("jpgName"); //提示錯誤信息
getch();
exit(EXIT_FAILURE); //出現錯誤,終止程序。
} /*
判斷文件類型是否為JPG
檢查第1, 2字節
*/
fread(&Flag_1,1,1,fp);
fseek(fp,1L,0);
fread(&Flag_2,1,1,fp);
if(( Flag_1 != 0xff) || (Flag_2 != 0xd8) )
{
printf("Not a jpg file?? (SOI)\n");
exit(EXIT_FAILURE);
}
//開始尋找開始位置
GetJpgSize(fp); fclose(fp);
return 1;
} void showjpg()
{
//窗口大小為圖片大小
initgraph(jpgWidth,jpgHeight+40);
setbkcolor(WHITE);
setcolor(BLACK);
cleardevice(); setfont(16, 8, "宋體");
char width[5],height[5];
sprintf(width, "%d",jpgWidth);
sprintf(height, "%d",jpgHeight); outtextxy(0,0,"圖像的寬:");
outtextxy(10+textwidth("圖像的寬:"),0,width); outtextxy(jpgWidth/2,0,"圖像的高:");
outtextxy(jpgWidth/2 + 10 + textwidth("圖像的高"),0,height); IMAGE img; // 定義 IMAGE 對象
loadimage(&img, readPath); //讀取圖片到 img 對象中
putimage(0, 40, &img); // 在坐標 (0, 0) 位置顯示 IMAGE 對象 getch();
closegraph();
} //獲取照片尺寸大小
void GetJpgSize(FILE *fp)
{ unsigned char HeightH;
unsigned char HeightL;
unsigned char WidthH;
unsigned char WidthL;
unsigned char BitDepth;
while(!SOF_find)
{
CurrentByte=fgetc(fp);
if(CurrentByte!=0xFF)
continue;
//發現標記符
CurrentByte=fgetc(fp);
switch(CurrentByte)
{
case APP0: break;
case SOF0:
fseek(fp,2L,1); BitDepth=fgetc(fp); //圖像高的高位和低位
HeightH=fgetc(fp); HeightL=fgetc(fp); //圖像寬的高位和低位
WidthH=fgetc(fp); WidthL=fgetc(fp); SOF_find=1;
break;
default:
break;
}
}
jpgHeight=HeightH * 256 + HeightL;
jpgWidth=WidthH * 256 + WidthL; } void main()
{
printf("輸入圖像的完整路徑及文件名:\n");
gets(readPath);
readjpg(readPath);
printf(" bitDepth=%x\n height=%x\n width=%x\n ",bitDepth,jpgHeight,jpgWidth);
printf(" jpgXpelsPerMeter=%x\n jpgYpelsPerMeter=%x\n",jpgXpelsPerMeter,jpgYpelsPerMeter); printf("按任意鍵顯示圖像\n");
getch();
showjpg();
}
總結
以上是生活随笔為你收集整理的c语言 写出raw文件,求指导,如何用c语言实现读取*.raw格式图像的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 单片机c语言实验,单片机实验C语言编程.
- 下一篇: linux cant open file