C++로 한겁니다..
참조만 하십시요...그럼 즐프..
#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main(void){
BITMAPFILEHEADER bf;
BITMAPINFOHEADER bi;
ifstream fin;
fin.open("a.bmp", ios_base::in | ios_base::binary); //a.bmp파일을 바이트로 읽어 들인다..
fin.read((char*)&bf, sizeof(BITMAPFILEHEADER)); // 사이즈..
FILE *fp = fopen("a.bmp", "rb");
fread(&bf, sizeof(unsigned char), sizeof(BITMAPFILEHEADER), fp);// 사이중 헤더에 사이즈 14바이트..
fread(&bi, sizeof(unsigned char), sizeof(BITMAPINFOHEADER), fp);// 나머지 정보 40바이트 저장..
unsigned char* data = (unsigned char*)malloc(bi.biSizeImage);
fread(data, sizeof(unsigned char), bi.biSizeImage, fp);
int r, g, b, mean;
for(int x=0; x<bi.biHeight; x++){
for(int y=0; y<bi.biWidth*3; y=y+3){
//printf("%d\t", x *(bi.biWidth*3) + y);
//printf("%d\t", x *(bi.biWidth*3) + y+1);
//printf("%d\t", x *(bi.biWidth*3) + y+2);
b = data[x *(bi.biWidth*3) + y];// *(data+x *(bi.biWidth*3) + y);
g = data[x *(bi.biWidth*3) + y+1];
r = data[x *(bi.biWidth*3) + y+2];
mean = (r+g+b)/3;
data[x *(bi.biWidth*3) + y] = mean;
data[x *(bi.biWidth*3) + y+1] = mean;
data[x *(bi.biWidth*3) + y+2] = mean;
}
}
printf("%d\n", bi.biWidth*3*bi.biHeight);
printf("%d\n", bi.biSizeImage);
//평균
int mean;
for(int i=0; i<bi.biSizeImage; i = i+3){
//data[i] = 255-data[i];
mean = (data[i] + data[i+1] + data[i+2])/3;
data[i] = mean;
data[i+1] = mean;
data[i+2] = mean;
}
fclose(fp);
// b.bmp파일에 다시 기록..
fp = fopen("b.bmp", "wb");
fwrite(&bf, sizeof(unsigned char), sizeof(BITMAPFILEHEADER), fp);
fwrite(&bi, sizeof(unsigned char), sizeof(BITMAPINFOHEADER), fp);
fwrite(data, sizeof(unsigned char), bi.biSizeImage, fp);
fclose(fp);
free(data);
return 0;*/
}