當前位置:首頁 » 編程語言 » 點陣圖c語言

點陣圖c語言

發布時間: 2022-07-01 19:25:35

① 用c語言讀取24位點陣圖bmp文件

可以使用C語言標准函數庫中的fopen、fseek、fclose等系列函數來打開bmp點陣圖文件,以及進行相應的處理,下面是一個demo,僅供參考。以下代碼在vc6.0中編譯通過。


#include<stdio.h>
#include<stdlib.h>
#//ThebmpFileHeaderlengthis14
#defineBM19778//TheASCIIcodeforBM
/*Testthefileisbmpfileornot*/
voidbmpFileTest(FILE*fpbmp);
/**/
voidbmpHeaderPartLength(FILE*fpbmp);
/**/
voidBmpWidthHeight(FILE*fpbmp);
//getr,g,bdata
voidbmpDataPart(FILE*fpbmp);
//
voidbmpoutput(FILE*fpout);
unsignedintOffSet=0;//
longwidth;//TheWidthoftheDataPart
longheight;//TheHeightoftheDataPart
unsignedcharr[2000][2000],output_r[2000][2000];
unsignedcharg[2000][2000],output_g[2000][2000];
unsignedcharb[2000][2000],output_b[2000][2000];
intmain(intargc,char*argv[])
{
/*Openbmpfile*/
unsignedchar*fp_temp;
FILE*fpbmp;
FILE*fpout;
fpbmp=fopen("1.bmp","rb");
if(fpbmp==NULL)
{
printf("Openbmpfailed!!! ");
return1;
}
fpout=fopen("out.bmp","wb+");
if(fpout==NULL)
{
printf("Openout.bmpfailed!!! ");
return1;
}

bmpFileTest(fpbmp);//Testthefileisbmpfileornot
bmpHeaderPartLength(fpbmp);//GetthelengthofHeaderPart
BmpWidthHeight(fpbmp);//


//
fseek(fpbmp,0L,SEEK_SET);
fseek(fpout,0L,SEEK_SET);

fp_temp=(unsignedchar*)malloc(OffSet);
fread(fp_temp,1,OffSet,fpbmp);
fwrite(fp_temp,1,OffSet,fpout);

bmpDataPart(fpbmp);//Reservethedatatofile

/*


如果您想對圖片進行處理,請您再這里插入處理函數!!!!!!!!!!!!!!!!!!

*/
bmpoutput(fpout);
fclose(fpbmp);
fclose(fpout);
return0;
}
voidbmpoutput(FILE*fpout)
{
inti,j=0;
intstride;
unsignedchar*pixout=NULL;

stride=(24*width+31)/8;
stride=stride/4*4;
pixout=(unsignedchar*)malloc(stride);

fseek(fpout,OffSet,SEEK_SET);
for(j=0;j<height;j++)
{
for(i=0;i<width;i++)
{
pixout[i*3+2]=output_r[height-1-j][i];
pixout[i*3+1]=output_g[height-1-j][i];
pixout[i*3]=output_b[height-1-j][i];
}
fwrite(pixout,1,stride,fpout);
}
}
voidbmpDataPart(FILE*fpbmp)
{
inti,j=0;
intstride;
unsignedchar*pix=NULL;
FILE*fpr;
FILE*fpg;
FILE*fpb;

if((fpr=fopen("bmpr.txt","w+"))==NULL)
{
printf("Failedtoconstructfilebmpr.txt.!!!");
exit(1);
}
if((fpg=fopen("bmpg.txt","w+"))==NULL)
{
printf("Failedtoconstructfilebmpg.txt.!!!");
exit(1);
}
if((fpb=fopen("bmpb.txt","w+"))==NULL)
{
printf("Failedtoconstructfilebmpb.txt.!!!");
exit(1);
}

fseek(fpbmp,OffSet,SEEK_SET);
stride=(24*width+31)/8;
stride=stride/4*4;
pix=(unsignedchar*)malloc(stride);

for(j=0;j<height;j++)
{
fread(pix,1,stride,fpbmp);
for(i=0;i<width;i++)
{
r[height-1-j][i]=pix[i*3+2];
g[height-1-j][i]=pix[i*3+1];
b[height-1-j][i]=pix[i*3];
output_r[height-1-j][i]=pix[i*3+2];
output_g[height-1-j][i]=pix[i*3+1];
output_b[height-1-j][i]=pix[i*3];
}
}
for(i=0;i<height;i++)
{
for(j=0;j<width-1;j++)
{
fprintf(fpb,"%4d",b[i][j]);
fprintf(fpg,"%4d",g[i][j]);
fprintf(fpr,"%4d",r[i][j]);
}
fprintf(fpb,"%4d ",b[i][j]);
fprintf(fpg,"%4d ",g[i][j]);
fprintf(fpr,"%4d ",r[i][j]);
}

fclose(fpr);
fclose(fpg);
fclose(fpb);

}
voidbmpFileTest(FILE*fpbmp)
{
unsignedshortbfType=0;

fseek(fpbmp,0L,SEEK_SET);//seek_set起始位置
fread(&bfType,sizeof(char),2,fpbmp);
if(BM!=bfType)
{
printf("Thisfileisnotbmpfile.!!! ");
exit(1);
}
}
/**/
voidbmpHeaderPartLength(FILE*fpbmp)
{
fseek(fpbmp,10L,SEEK_SET);
fread(&OffSet,sizeof(char),4,fpbmp);
printf("TheHeaderPartisoflength%d. ",OffSet);
}
/**/
voidBmpWidthHeight(FILE*fpbmp)
{
fseek(fpbmp,18L,SEEK_SET);
fread(&width,sizeof(char),4,fpbmp);
fseek(fpbmp,22L,SEEK_SET);
fread(&height,sizeof(char),4,fpbmp);
printf("TheWidthofthebmpfileis%ld. ",width);
printf("TheHeightofthebmpfileis%ld. ",height);
}

② 如何用C語言(C++)讀取點陣圖的像素點RGB信息

pData裡面保存的就是一個一個的COLORREF結構,你只需要通過BITMAPINFOHEADER中的寬高等信息,計算位移,就可以讀取某個點的RGB值了。
還有一個簡單的辦法,你之前已經有memBitmap這個CBitmap了,通過這個做更方便。通過SelectObject將memBitmap放到一個CDC中,直接使用函數GetPixel函數就可以獲取指定某個點的RGB值了,這個不需要計算和位移。

③ c語言 點陣圖操作

對於一個24位色的圖,你直接就拿int(32位機器,長度32位)型二維數組存就行了。肯定能存這個24位數,沒必要把顏色分開存。如果分開存你就用下面的結構體數組的方法吧。建議參考,十分方便實用。

RGB24使用24位來表示一個像素,RGB分量都用8位表示,取值范圍為0-255。注意在內存中RGB各分量的排列順序為:BGR BGR BGR…。通常可以使用RGBTRIPLE數據結構來操作一個像素,它的定義為:
typedef struct tagRGBTRIPLE
{BYTE rgbtBlue; // 藍色分量
BYTE rgbtGreen; // 綠色分量
BYTE rgbtRed; // 紅色分量
} RGBTRIPLE;

④ VC++里,c語言讀取點陣圖之前怎麼找到圖片的

File.OpenRead("image.bmp");
這裡面放的就是文件的詳細路徑,如果省略路徑只有文件名字的話,那麼就表示該文件在當前程序的工作目錄下,就是在你的工程文件夾裡面的。

⑤ c語言如何打開點陣圖

不像打開文本那樣一個函數就可以 需要調用好多模塊 自己查資料去吧

⑥ 如何用C語言讀取點陣圖的像素點RGB信息

可以自己去查一下BMP文件的文件結構,這種文件的格式最簡單。當然,其他常用的圖片格式也可以去查一下。查到了之後,就能根據文件,用fopen打開圖片再fread讀取了

⑦ BMP格式點陣圖TC語言怎麼顯示

下面的是<<C & C++編程實例>>隨書光碟上的代碼,我在TC2. 0下編譯通過. 它是利用了抖動技術顯示了8bit和24bit的點陣圖( 也就是256色和16M色點陣圖),應該能滿足你的需要. 不過,我想問下,你老師教過抖動顯示嗎? #include <stdio.h> #include <dos.h> #include <stdio.h> #include <conio.h> #define NoError 0 #define ErrorFileOpen 1 #define ErrorFileType 2 #define ErrorImageColor 3 typedef struct tagBITMAPFILEHEADER { unsigned int bfType; unsigned long bfSize; unsigned int bfReserved1; unsigned int bfReserved2; unsigned long bfoffBits; }BITMAPFILEHEADER; typedef struct tagBITMAPINFOHEADER { unsigned long biSize; unsigned long biWidth; unsigned long biHeight; unsigned int biPlanes; unsigned int biBitCount; unsigned long biCompression; unsigned long biSizeImage; unsigned long biXPelsPerMeter; unsigned long biYPelsPerMeter; unsigned long biClrUsed; unsigned long biClrImportant; } BITMAPINFOHEADER; typedef struct tagRGBQUAD { unsigned char rgbBlue; unsigned char rgbGreen; unsigned char rgbRed; unsigned char rgbReserved; } RGBQUAD; unsigned char PalReg[17]= { 0,1,2,3,4,5,6,7,8,9,10,11,12, 13,14,15,0}; unsigned char StandardPal[48]= { 0, 0, 0, 32, 0, 0, 0,32, 0, 32,32, 0, 0, 0,32, 32, 0,32, 0,32,32, 32,32, 32, 48, 48,48, 63, 0, 0, 0,63, 0, 63,63, 0, 0, 0,63, 63, 0,63, 0,63,63, 63,63,63, }; unsigned char LightnessMatrix [16][16]= { { 0,235,59,219,15,231,55,215,2, 232,56,217,12,229,52,213}, {128,64,187,123,143,79,183, 119,130,66,184,120,140,76,180, 116}, {33,192,16,251,47,207,31,247, 34,194,18,248,44,204,28,244}, {161,97,144,80,175,111,159,95, 162,98,146,82,172,108,156,92}, {8,225,48,208,5,239,63,223,10, 226,50,210,6,236,60,220}, {136,72,176,112,133,69,191, 127,138,74,178,114,134,70,188, 124}, {41,200,24,240,36,197,20,255, 42,202,26,242,38,198,22,252}, {169,105,152,88,164,100,148, 84,170,106,154,90,166,102,150, 86}, {3,233,57,216,13,228,53,212,1, 234,58,218,14,230,54,214}, {131,67,185,121,141,77,181, 117,129,65,186,122,142,78,182, 118}, {35,195,19,249,45,205,29,245, 32,193,17,250,46,206,30,246}, {163,99,147,83,173,109,157,93, 160,96,145,81,174,110,158,94}, {11,227,51,211,7,237,61,221,9, 224,49,209,4,238,62,222}, {139,75,179,115,135,71,189, 125,137,73,177,113,132,68,190, 126}, {43,203,27,243,39,199,23,253, 40,201,25,241,37,196,21,254}, {171,107,155,91,167,103,151, 87,168,104,153,89,165,101,149, 85}, }; unsigned char ColorTable[2][2][2]= {{{0,12},{10,14}},{{9,13},{11, 15}}}; unsigned char ColorMap[256][3]; int ShowBmp(char *FileName); int GetColor(unsigned char R,unsigned char G, unsigned char B,int X,int Y); void SetVideoMode(unsigned char Mode); void SetPalReg(unsigned char *palReg); void SetDacReg(unsigned char *DacReg, int Color, int Count); void PutPixel(int X, int Y, unsigned char Color); /* 主函數 */ void main (int argc, char *argv[]) { if(argc!=2) { printf("Usage:\tSHOW Filename.BMP\n"); exit(1); } ShowBmp(argv[1]); } /* 根據圖像文件名,讀取圖像內容並利用抖動技術進行顯示 */ int ShowBmp(char *FileName) { FILE *Fp; BITMAPFILEHEADER FileHead; BITMAPINFOHEADER InfoHead; RGBQUAD RGB; int N, W,Y,X,C,Color; unsigned char Buffer[4096]; Fp=fopen(FileName,"rb"); if (Fp==NULL) return(ErrorFileOpen); fread(&FileHead,sizeof( BITMAPFILEHEADER),1,Fp); if(FileHead.bfType!='BM') { fclose(Fp); return(ErrorFileType); } fread(&InfoHead,sizeof( BITMAPINFOHEADER),1,Fp); if(InfoHead.biBitCount!=8 && InfoHead.biBitCount!=24) { fclose(Fp); return(ErrorImageColor); } /* 設置顯示模式和顯示區域 */ SetVideoMode(0x12); SetPalReg(PalReg); SetDacReg(StandardPal,0,16); /* 對兩種不同色彩數的圖像分別進行處理 */ if(InfoHead.biBitCount==8) /* 256色 */ { for (N=0;N<256;N++) { fread(&RGB, sizeof(RGBQUAD),1,Fp); ColorMap[N][0]=RGB.rgbRed; ColorMap[N][1]=RGB.rgbGreen; ColorMap[N][2]=RGB.rgbBlue; } W=(InfoHead.biWidth+3)/4*4; for(Y=InfoHead.biHeight-1;Y>= 480;Y--) fread(Buffer,sizeof(unsigned char),W,Fp); for(;Y>0;Y--) { fread(Buffer,sizeof(unsigned char),W,Fp); for (X=0;X<InfoHead.biWidth && X<640;X++) { C=Buffer[X]; Color=GetColor(ColorMap[C][0], ColorMap[C][1],ColorMap[C][2], X,Y); PutPixel (X,Y,Color); } } } else /* 24bits真彩色 */ { W=(InfoHead.biWidth*3+3)/4*4; for(Y=InfoHead.biHeight-1;Y> 639;Y--) fread(Buffer,sizeof(unsigned char),W,Fp); for(;Y>=0;Y--) { fread(Buffer,sizeof(unsigned char),W,Fp); for(X=0;X<InfoHead.biWidth && X<640;X++) { C=X*3; Color=GetColor(Buffer[C+2], Buffer[C+1],Buffer[C],X,Y); PutPixel(X,Y,Color); } } } getch(); fclose(Fp); SetVideoMode(0x03); return(NoError); } int GetColor(unsigned char R, unsigned char G, unsigned char B, int X, int Y) { unsigned int L=LightnessMatrix[Y & 0x0F][X & 0x0F]; return(ColorTable[(unsigned int)R*256/255>L][(unsigned int)G*256/255>L][(unsigned int)B*256/255>L]); } void SetVideoMode(unsigned char Mode) { _AH=0x00; _AL=Mode; geninterrupt(0x10); } void SetPalReg(unsigned char *PalReg) { _ES=FP_SEG((unsigned char far*)PalReg); _DX=FP_OFF((unsigned char far*)PalReg); _AX=0x1002; geninterrupt(0x10); } void SetDacReg(unsigned char *DacReg,int Color,int Count) { _ES=FP_SEG((unsigned char far*)DacReg); _DX=FP_OFF((unsigned char far*)DacReg); _AX=0x1012; _BX=Color; _CX=Count; geninterrupt(0x10); } /* 在對應位置顯示像素色彩 */ void PutPixel(int X, int Y, unsigned char Color) { _AH=0x0C; _AL=Color; _CX=X; _DX=Y; geninterrupt(0x10); } === 再給個實例鏈接,剛找到的: http://www.turinger.com/ article_view.asp?id=3

⑧ C語言如何生成點陣圖文件

去了解點陣圖的文件頭結構和點陣圖的數據結構,然後按照這些數據格式寫成數據文件流保存成點陣圖文件就可了。網上有比較多的C語言範例可以參考。

⑨ C語言操作點陣圖

去找本《Visual C++ 數字圖像處理》 (人民郵電出版社)

另外這本書還有其他基本配套的書,涵蓋了所有的圖像處理內容

⑩ c語言讀取點陣圖編程

我也遇到過類似的問題,點陣圖數據讀取到內存,然後在保存的文件中。
新點陣圖文件,雖然能夠顯示但是圖片內容中存在大量錯誤。
後來發現問題的關鍵在於
fopen(bmpname,"rb");
注意rb標志,不管是只讀標志還是只寫標志,或者讀寫標志後面要加+。
也就是rb+
不加+的標志是針對的文本讀寫的。
加+的標志是針對二進制數據讀寫的。
而點陣圖文件是一種二進制數據。

熱點內容
javaip埠 發布:2025-02-04 09:27:09 瀏覽:853
國產存儲科技進步二等獎 發布:2025-02-04 09:13:00 瀏覽:693
編程課v 發布:2025-02-04 08:45:00 瀏覽:106
模擬器能有手機腳本么 發布:2025-02-04 08:39:50 瀏覽:757
android顯示html圖片 發布:2025-02-04 08:35:31 瀏覽:791
如何查學信網賬號及密碼 發布:2025-02-04 08:33:55 瀏覽:502
linux32位jdk 發布:2025-02-04 08:33:55 瀏覽:247
康佳伺服器連接失敗是怎麼回事 發布:2025-02-04 08:18:51 瀏覽:916
編譯編譯有什麼 發布:2025-02-04 08:05:52 瀏覽:735
讓外網訪問內網伺服器 發布:2025-02-04 08:02:20 瀏覽:783