Skip to content

Instantly share code, notes, and snippets.

@peterlee0127
Created May 25, 2012 07:34
Show Gist options
  • Save peterlee0127/2786427 to your computer and use it in GitHub Desktop.
Save peterlee0127/2786427 to your computer and use it in GitHub Desktop.
HW LC
#include <stdio.h>
#include <windows.h> //這裡面有定義BITMAPFILEHEADER BITMAPINFOHEADER RGBQUAD
#define WORD short int
#define DWORD long int
#define LONG long int
char *File1_Name = "0001.bmp";
BITMAPFILEHEADER File1_F; //BMP檔頭結構
BITMAPINFOHEADER File1_I; //BMP資料結構
RGBQUAD File1_rgb[256]; //調色盤
byte *File1_pixel; //Image data 存的矩陣(大小未知)
char *File2_Name = "0002.bmp";
BITMAPFILEHEADER File2_F; //BMP檔頭結構
BITMAPINFOHEADER File2_I; //BMP資料結構
RGBQUAD File2_rgb[256]; //調色盤
byte *File2_pixel; //Image data 存的矩陣(大小未知)
char *File3_Name = "0003.bmp";
byte *File3_pixel; //Image data 存的矩陣(大小未知)
//-------主程式-----------------------------------------------------------------------------
int main()
{
long int i;
////////////////////////////////////////////////////
//1. 影像matrix讀進來: *pixel 的大小為 (I.biWidth)X(I.biHeight)
// 請你動態的new空間出來
FILE *F1;
F1=fopen(File1_Name,"rb");
FILE *file2 = fopen( File2_Name, "rb"); //開啟File
fread( (char*)&File2_F, sizeof(BITMAPFILEHEADER), 1, file2); //讀取BITMAPFILEHEADER
fread( (char*)&File2_I, sizeof(BITMAPINFOHEADER), 1, file2); //讀取BITMAPINFOHEADER
fread( (char*)&File2_rgb, sizeof(File1_rgb), 1, file2); //讀取調色盤
byte *File2_pixel=(byte*)malloc(sizeof(byte)*(File1_I.biHeight*File1_I.biWidth));
while(!feof(file2))
{
fread(&File2_pixel,sizeof(char),1,file2);
}
fclose(F1);
fclose(file2);
////////////////////////////////////////////////////
//read file1
FILE *file1 = fopen( File1_Name, "rb"); //開啟File
fread( (char*)&File1_F, sizeof(BITMAPFILEHEADER), 1, file1); //讀取BITMAPFILEHEADER
fread( (char*)&File1_I, sizeof(BITMAPINFOHEADER), 1, file1); //讀取BITMAPINFOHEADER
fread( (char*)&File1_rgb, sizeof(File1_rgb), 1, file1); //讀取調色盤
File1_pixel=(byte*)malloc(sizeof(byte)*(File1_I.biHeight*File1_I.biWidth)); //malloc
while( !feof(file1) )
{
fread(File1_pixel,sizeof(byte)*(File1_I.biHeight)*(File1_I.biWidth),1,file1); //讀取Image data
}
fclose(file1); //關閉File
////////////////////////////////////////////////////
///2. Show 檔頭資訊:
//
printf("Size=%d\n",File1_F.bfSize);
printf("Height=%d\n",File1_I.biHeight);
printf("Width=%d\n",File1_I.biWidth);
////////////////////////////////////////////////////
////////////////////////////////////////////////////
///3. 影像處理: 改變矩陣內容
//
byte *File3_pixel=(byte*)malloc(sizeof(byte)*(File1_I.biHeight*File1_I.biWidth));
for(i=0;i<sizeof(byte)*(File1_I.biHeight)*(File1_I.biWidth);i++)
{
if((*(File1_pixel+i)+*(File2_pixel+i))>255)
{ *(File3_pixel+i)=255;}
else
{*(File3_pixel+i)=0;}
}
////////////////////////////////////////////////////
////////////////////////////////////////////////////
///4. 影像matrix檔案寫出
//
////////////////////////////////////////////////////
FILE *file3 = fopen( File3_Name, "wb");
fwrite( (char*)&File1_F, sizeof(BITMAPFILEHEADER), 1, file3); //寫入BITMAPFILEHEADER
fwrite( (char*)&File1_I, sizeof(BITMAPINFOHEADER), 1, file3); //寫入BITMAPINFOHEADER
fwrite( (char*)File1_rgb, sizeof(File1_rgb) , 1, file3); //寫入調色盤
//寫入Image data
fwrite((byte*)*&(File3_pixel),sizeof(byte)*(File1_I.biHeight)*(File1_I.biWidth),1
,file3);
fclose(file3);
system("pause");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment