有這樣一個二進制文件,大小在100M左右,里面存放了多張JPG圖片,文件格式如下:

為龍鳳等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及龍鳳網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為做網(wǎng)站、成都做網(wǎng)站、龍鳳網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
無關(guān)數(shù)據(jù)區(qū)
JPG圖片數(shù)據(jù)區(qū)
無關(guān)數(shù)據(jù)區(qū)
JPG圖片數(shù)據(jù)區(qū)
無關(guān)數(shù)據(jù)區(qū)
JPG圖片數(shù)據(jù)區(qū)
......
已知JPG圖片起始標(biāo)志為:“FF D8 FF E0 00 10 4A”,結(jié)束標(biāo)志為:“FF D9”。
現(xiàn)想把這些JPG圖片數(shù)據(jù)從該文件中讀出來,生成一個個單獨的圖片文件,該如何做呢?
幫你寫了個程序, 測試了只含有一個圖片信息的文件
int len=0;
int filesn=0;
void ReadJpg(CFile *sfp,const char *dstfile,unsigned char a,unsigned char b)
{
CFile nf;
nf.Open(dstfile,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
unsigned t=0,t1=0;
nf.Write(a,sizeof(char));
nf.Write(b,sizeof(char));
while(1)
{
sfp-Read(t,sizeof(char));
len++;
nf.Write(t,sizeof(char));
if(t==0xFF)
{
sfp-Read(t1,sizeof(char));
len++;
if(t1==0xD9)
break;
else
nf.Write(t1,sizeof(char));
}
}
nf.Close();
}
調(diào)用:
void test()
{
CFile fp;
fp.Open("86b05621.jpg",CFile::modeRead|CFile::typeBinary);//文件名改一下
unsigned char a=0,b=0;
len=0;
while(1)
{
fp.Read(a,sizeof(char));
len++;
if(len=fp.GetLength())
break;
if(a==0xFF)
{
fp.Read(b,sizeof(char));
len++;
if(b==0xD8)
{
filesn++;
char filename[256];
sprintf(filename,"Jpg %d.jpg",filesn);
ReadJpg(fp,filename,a,b);
}
}
}
fp.Close();
}
思路是,先得到FF的值,然后再判斷下一位是否是0xD8, 但這里要說明一下,如果你的其它數(shù)據(jù)信息里也含有FF D8值的話就會出錯了, 所以為保險起見,應(yīng)該再多比較幾位, JPG的頭是固定的,再往下就是EF E0 00 1E, 建議樓主再往下比較四位比較保險(在程序中稍作修改即可)
你是不是在 分析某個 游戲的或程序的資源文件呀,如果是的話,這個資源文件應(yīng)該配有一個 索引文件!或在其文件自身。索引可以列表索引,鏈表形示……
如果是你自已生成的文件包,最好加上索引,因為這樣可以提高效率!
#include
using namespace std;
#define Twoto1(i,j,w) i*w+j
void createimage(unsigned char *img, int w, int h)
{img = new unsigned char[w*h];}
void delateimage(unsigned char*img)
{delete []img;}
void readimage(unsigned char*img, int w, int h, char *fname)
{
FILE *fp;
fopen_s(fp,fname, "rb");
if (fp == NULL){ cout "error" endl; return; }
size_t result;
result=fread(img , sizeof(unsigned char), w*h, fp);
if (result != w*h)
{
cout "Reading error" endl;
return;
}
else
cout "Reading Ok!" endl;
fclose(fp);
}
void mobanjuanji(unsigned char image, unsigned char*image1, int w, int h, float moban[5][5])
{
for (int i = 0; i for (int j = 0; j if (iw - 3 || jh - 3)
image1[Twoto1(i,j,w)] = 0;
else
{
float temp = 0;
for (int m = 0; m5; m++)
for (int n = 0; n5; n++)
{
temp += (image[Twoto1(i-2+m,j-2+n,w)] moban[m][n]);
}
if (temp255) image1[Twoto1(i, j, w)] = 255;
else if (temp0) image1[Twoto1(i, j, w)] = 0;
else image1[Twoto1(i, j, w)] = temp;
}
}
void saveimage(unsigned char *img, int w, int h, char *fname)
{
FILE *fp;
fopen_s(fp, fname, "wb");
if (fp == NULL) { cout "error" endl; return; }
size_t result;
result = fwrite(img, sizeof(unsigned char), w*h, fp);
if (result != w*h)
{
cout "Write error" endl;
return;
}
else
cout "Write Ok!" endl;
fclose(fp);
}
void main()
{
unsigned char *img;
unsigned char *img1;
float moban[5][5] = { {0,0,0,0,0},{0, -1, 0, 1, 0 }, { 0, -2, 0, 2, 0 }, { 0, -1, 0, 1, 0 }, { 0,0,0,0,0 } };
//float moban[5][5] = { 0 };
int w = 512, h = 512;
createimage(img, w, h);
createimage(img1, w, h);
readimage(img, w, h, "E:\ss.raw");
mobanjuanji(img, img1,w, h, moban);
saveimage(img, w, h, "E:\ss_1.raw");
saveimage(img1, w, h, "E:\ss_2.raw");
delateimage(img);
delateimage(img1);
}
擴展資料
C語言實現(xiàn)一個圖片的讀出和寫入
#include stdlib.h
#include windows.h
int file_size(char* filename)//獲取文件名為filename的文件大小。
{
FILE *fp = fopen(filename, "rb");//打開文件。
int size;
if(fp == NULL) // 打開文件失敗
return -1;
fseek(fp, 0, SEEK_END);//定位文件指針到文件尾。
size=ftell(fp);//獲取文件指針偏移量,即文件大小。
fclose(fp);//關(guān)閉文件。
return size;
}
int main ()
{
int size=0;
size=file_size("qw");
printf("%d\n",size);
FILE * pFile,*qw;
char *buffer=(char*)malloc(sizeof(char)*size);
qw? ?=fopen("qw","r");
pFile = fopen ( "qwe" , "wb" );
printf("%d==\n",pFile);
printf("%d\n",size);
fread(buffer,1,size,qw);
fwrite (buffer , sizeof(byte), size , pFile );
fclose (pFile);
rename("qwe","Groot.jpg");
return 0;
}
#ifndef IMAGE_H
#define IMAGE_H
void image_info(FILE* file);
void image_save(FILE *file);
void image_gray();
void image_binarization();
void image_opposite();
void image_channel(); //抽取RGB通道
void image_bright();//改變圖像亮度
typedef struct BMP
{
//14字節(jié)
unsigned short bfType; //文件標(biāo)識 2字節(jié) 必須為BM
unsigned int bfSize; //文件大小 4字節(jié)
unsigned short bfReserved1; //保留,每字節(jié)以"00"填寫 2字節(jié)
unsigned short bfReserved2; //同上 2字節(jié)
unsigned int bfOffBits; //記錄圖像數(shù)據(jù)區(qū)的起始位置(圖象數(shù)據(jù)相對于文件頭字節(jié)的偏移量)。 4字節(jié)
//40字節(jié)
unsigned int biSize; //表示本結(jié)構(gòu)的大小 4字節(jié)
int biWidth; //位圖的寬度 4字節(jié)
int biHeight; //位圖的高度 4字節(jié)
unsigned short biPlanes; //永遠(yuǎn)為1 , 2字節(jié)
unsigned short biBitCount; //位圖的位數(shù) 分為1 4 8 16 24 32 2字節(jié)
unsigned int biCompression; //壓縮說明 4字節(jié)
unsigned int biSizeImage; //表示位圖數(shù)據(jù)區(qū)域的大小以字節(jié)為單位 4字節(jié)
int biXPelsPerMeter; //用象素/米表示的水平分辨率 4字節(jié)
int biYPelsPerMeter; //用象素/米表示的垂直分辨率 4字節(jié)
unsigned int biClrUsed; //位圖使用的顏色索引數(shù) 4字節(jié)
unsigned int biClrImportant; //對圖象顯示有重要影響的顏色索引的數(shù)目 4字節(jié)
} BMP;
int line_byte;
unsigned char *imagedata;
extern BMP bmp;
extern int line_byte;
extern unsigned char *imagedata;
#endif
//image_rw.c文件
#includestdio.h
#includestdlib.h
#include"image.h"
void image_info(FILE *file)
{
int times=3; //輸入文件名次數(shù)。
char bmp_name[10]; //文件名
printf("\nplease enter a file name for reading:");
do
{
if (times3)
{
printf("\nplease enter a file name for reading again:");
}
fflush(stdin);
gets(bmp_name);
//printf("\n%s",bmp_name);
file=fopen(bmp_name,"rb+"); //打開一個文件進行讀寫操作。
--times;
if (file==NULL)
{
printf("\nerror opening %s for reading! ",bmp_name);
}
else
{
break;
}
}
while(times!=0);
if (times==0)
{
printf("\nsorry, shutdown!");
exit(1);
}
//讀取圖像信息
fseek(file,0L,0); //讀取圖像文件類型
fread(bmp,sizeof(BMP),1,file);
printf("\n bmp tpye: %u",bmp.bfType);
printf("\n bmp size: %u",bmp.bfSize);
printf("\n bmp reserved1: %u",bmp.bfReserved1);
printf("\n bmp reserved2: %u",bmp.bfReserved2);
printf("\n bmp offBits: %u",bmp.bfOffBits);
printf("\n bmp bisize: %u",bmp.biSize);
printf("\n bmp biWidth: %d",bmp.biWidth);
printf("\n bmp biHeight: %d",bmp.biHeight);
printf("\n bmp biplans: %u",bmp.biPlanes);
printf("\n bmp biBitCount: %u",bmp.biBitCount);
printf("\n bmp biCompression: %u",bmp.biCompression);
printf("\n bmp biSizeImage: %u",bmp.biSizeImage);
printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);
printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);
printf("\n bmp biClrUsed: %u",bmp.biClrUsed);
printf("\n bmp biClrImportant: %u\n",bmp.biClrImportant);
line_byte=(bmp.biWidth*bmp.biBitCount/8+3)/4*4; //獲得圖像數(shù)據(jù)每行的數(shù)據(jù)個數(shù)
//printf("dfsa%u",bmp.line_byte);
//bmp.imagedata=NULL;
imagedata=(unsigned char*)malloc(bmp.biSizeImage);
fseek(file,(long)bmp.bfOffBits,0);
fread(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);
fclose(file);
}
//保存圖像
void image_save(FILE *file)
{
int times=3; //輸入文件名次數(shù)。
char bmp_name[10]; //文件名
//int i; //記錄數(shù)據(jù)區(qū)個數(shù)
printf("\nplease enter a file name for writeing:");
do
{
if (times3)
{
printf("\nplease enter a file name for writeing again:");
}
fflush(stdin);
gets(bmp_name);
printf("\n%s",bmp_name);
file=fopen(bmp_name,"wb+"); //打開一個文件進行讀寫操作。
--times;
if (file==NULL)
{
printf("\nerror opening %s for writing",bmp_name);
}
else
{
break;
}
}
while(times!=0);
if (times==0)
{
printf("\nsorry, shutdown!");
exit(1);
}
//寫文件頭
printf("\n%s",bmp_name);
fseek(file,0L,0); //圖像文件類型
fwrite((bmp.bfType),sizeof(short),1,file);
printf("\n bmp tpye: %d",bmp.bfType);
fseek(file,2L,0); //圖像文件大小
fwrite((bmp.bfSize),sizeof(int),1,file);
printf("\n bmp size: %d",bmp.bfSize);
fseek(file,6L,0); //圖像文件保留字1
fwrite((bmp.bfReserved1),sizeof(short),1,file);
printf("\n bmp reserved1: %d",bmp.bfReserved1);
fseek(file,8L,0); //圖像文件保留字2
fwrite((bmp.bfReserved2),sizeof(short),1,file);
printf("\n bmp reserved2: %d",bmp.bfReserved2);
fseek(file,10L,0);//數(shù)據(jù)區(qū)的偏移量
fwrite((bmp.bfOffBits),sizeof(short),1,file);
printf("\n bmp offBits: %d",bmp.bfOffBits);
fseek(file,14L,0);//文件頭結(jié)構(gòu)大小
fwrite((bmp.biSize),sizeof(int),1,file);
printf("\n bmp bisize: %d",bmp.biSize);
fseek(file,18L,0);//圖像的寬度
fwrite((bmp.biWidth),sizeof(int),1,file);
printf("\n bmp biWidth: %d",bmp.biWidth);
fseek(file,22L,0);//圖像的高度
fwrite((bmp.biHeight),sizeof(int),1,file);
printf("\n bmp biHeight: %d",bmp.biHeight);
fseek(file,24L,0);//圖像的面數(shù)
fwrite((bmp.biPlanes),sizeof(short),1,file);
printf("\n bmp biplans: %d",bmp.biPlanes);
fseek(file,28L,0);//圖像一個像素的字節(jié)數(shù)
fwrite((bmp.biBitCount),sizeof(short),1,file);
printf("\n bmp biBitCount: %d",bmp.biBitCount);
fseek(file,30L,0);//圖像壓縮信息
fwrite((bmp.biCompression),sizeof(short),1,file);
printf("\n bmp biCompression: %d",bmp.biCompression);
fseek(file,34L,0);//圖像數(shù)據(jù)區(qū)的大小
fwrite((bmp.biSizeImage),sizeof(int),1,file);
printf("\n bmp biSizeImage: %d",bmp.biSizeImage);
fseek(file,38L,0);//水平分辨率
fwrite((bmp.biXPelsPerMeter),sizeof(int),1,file);
printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);
fseek(file,42L,0);//垂直分辨率
fwrite((bmp.biYPelsPerMeter),sizeof(int),1,file);
printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);
fseek(file,46L,0);//顏色索引數(shù)
fwrite((bmp.biClrUsed),sizeof(int),1,file);
printf("\n bmp biClrUsed: %d",bmp.biClrUsed);
fseek(file,50L,0);//重要顏色索引數(shù)
fwrite((bmp.biClrImportant),sizeof(int),1,file);
printf("\n bmp biClrImportant: %d\n",bmp.biClrImportant);
fseek(file,(long)(bmp.bfOffBits),0);
fwrite(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);
fclose(file);
}
//pixProcess.c文件
#includestdio.h
#includestdlib.h
#includemath.h
#include"image.h"
//灰度化
void image_gray()
{
int i,j;
unsigned char tmp;
for (i=0;ibmp.biHeight;i++)
{
for (j=0;jline_byte/3;j++)
{
tmp=0.11*(*(imagedata+i*line_byte+j*3+0))+0.59*(*(imagedata+i*line_byte+j*3+1))+0.3*(*(imagedata+i*line_byte+j*3+2));
imagedata[i*line_byte+j*3+0]=tmp;
imagedata[i*line_byte+j*3+1]=tmp;
imagedata[i*line_byte+j*3+2]=tmp;
//printf("\nnidsfh%d %d",i,j);
}
}
}
//二值化
void image_binarization()
{
int i,j;
for (i=0;ibmp.biHeight;i++)
{
for (j=0;jline_byte;j++)
{
if ((*(imagedata+i*line_byte+j))128)
{
imagedata[i*line_byte+j]=0;
}
else
{
imagedata[i*line_byte+j]=255;
}
}
}
}
void image_opposite() //反相
{
int i,j;
for (i=0;ibmp.biHeight;i++)
{
for (j=0;jline_byte;j++)
{
imagedata[i*line_byte+j]=abs(255-imagedata[i*line_byte+j]);
}
}
}
void image_channel() //抽取RGB通道
{
int i,j;
char rgb;
printf("\nplease enter a char(r/g/b): ");
fflush(stdin);
scanf("%c",rgb);
if (rgb=='b')
{
for (i=0;ibmp.biHeight;i++)
{
for (j=0;jline_byte/3;j++)
{
imagedata[i*line_byte+3*j+1]=0;
imagedata[i*line_byte+3*j+2]=0;
}
}
}
else if(rgb=='g')
{
for (i=0;ibmp.biHeight;i++)
{
for (j=0;jline_byte/3;j++)
{
imagedata[i*line_byte+3*j]=0;
imagedata[i*line_byte+3*j+2]=0;
}
}
}
else
{
for (i=0;ibmp.biHeight;i++)
{
for (j=0;jline_byte/3;j++)
{
imagedata[i*line_byte+3*j]=0;
imagedata[i*line_byte+3*j+1]=0;
}
}
}
}
void image_bright()//改變圖像亮度
{
int level;
int i,j;
printf("\n please enter the level of brightness[-255 to 255] :");
fflush(stdin);
scanf("%d",level);
for (i=0;ibmp.biHeight;i++)
{
for (j=0;jline_byte;j++)
{
if (level=0)
{
if ((imagedata[i*line_byte+j]+level)255)
imagedata[i*line_byte+j]=255;
else
imagedata[i*line_byte+j]+=level;
}
else
{
if ((imagedata[i*line_byte+j]-abs(level))0)
imagedata[i*line_byte+j]=0;
else
imagedata[i*line_byte+j]+=level;
}
}
}
}
//void image_create() //創(chuàng)建一幅24位BMP圖像文件。
//{
//main.c文件
#includestdio.h
#includestdlib.h
#includestring.h
#includeconio.h
#include"image.h"
BMP bmp;
int main()
{
FILE *file=NULL;
int choose;
char gono;
do
{
image_info(file); //imagedata已經(jīng)分配了動態(tài)內(nèi)存,但是沒有釋放
printf("\n 1.image_opposite");
printf("\n 2.image_gray");
printf("\n 3.image_binarization");
printf("\n 4.image_channel");
printf("\n 5.image_brightness");
//printf("6.image_opposite");
//printf("7.image_opposite");
printf("\nchoose your options:");
fflush(stdin);
scanf("%d",choose);
switch(choose)
{
case 1:
image_opposite();
image_save(file);
free(imagedata);
break;
case 2:
image_gray();
image_save(file);
free(imagedata);
break;
case 3:
image_binarization();
image_save(file);
free(imagedata);
break;
case 4:
image_channel();
image_save(file);
free(imagedata);
break;
case 5:
image_bright();
image_save(file);
free(imagedata);
break;
default:
printf("\n wrong choose!");
}
printf("\nlet's go on?(y/n):");
fflush(stdin);
scanf("%c",gono);
if (gono=='n')
{
printf("\nbye bye!");
break;
}
}
while(1);
return 0;
}
#include graphics.h
int main()
{
int gdriver, gmode;
gdriver=VGA;
gmode=VGAHI;
initgraph(gdriver, gmode, "c:\\tc");
bar3d(100, 100, 300, 250, 50, 1); /*畫一長方體*/
getch();
closegraph();
return 0;
}
有時編程者并不知道所用的圖形顯示器適配器種類, 或者需要將編寫的程序 用于不同圖形驅(qū)動器, Turbo C提供了一個自動檢測顯示器硬件的函數(shù), 其調(diào)用
格式為:
void far detectgraph(int *gdriver, *gmode);
其中g(shù)driver和gmode的意義與上面相同。
例5. 自動進行硬件測試后進行圖形初始化
#include graphics.h
int main()
{
int gdriver, gmode;
detectgraph(gdriver, gmode); /*自動測試硬件*/
printf("the graphics driver is %d, mode is %d\n", gdriver, gmode); /*輸出測試結(jié)果*/
getch();
initgraph(gdriver, gmode, "c:\\tc");
/* 根據(jù)測試結(jié)果初始化圖形*/
bar3d(10, 10, 130, 250, 20, 1);
getch();
closegraph();
return 0;
}
上例程序中先對圖形顯示器自動檢測, 然后再用圖形初始化函數(shù)進行初始化設(shè)置, 但Turbo C提供了一種更簡單的方法, 即用gdriver= DETECT 語句后再跟 initgraph()函數(shù)就行了。采用這種方法后, 上例可改為:
例6.
#include graphics.h
int main()
{
int gdriver=DETECT, gmode;
initgraph(gdriver, gmode, "c:\\tc");
bar3d(50, 50, 150, 30, 1);
getch();
closegraph();
return 0;
}
另外, Turbo C提供了退出圖形狀態(tài)的函數(shù)closegraph(), 其調(diào)用格式為:void far closegraph(void);調(diào)用該函數(shù)后可退出圖形狀態(tài)而進入文本方式(Turbo C 默認(rèn)方式), 并釋放用于保存圖形驅(qū)動程序和字體的系統(tǒng)內(nèi)存。
本文標(biāo)題:c語言圖片讀取函數(shù),C語言調(diào)用圖片
鏈接URL:http://chinadenli.net/article14/dsgsede.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、品牌網(wǎng)站建設(shè)、網(wǎng)站營銷、網(wǎng)頁設(shè)計公司、品牌網(wǎng)站設(shè)計、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)