[ create a new paste ] login | about

Link: http://codepad.org/yd0C9GTB    [ raw code | output | fork ]

C, pasted on Dec 23:
#define WIDTH 320
#define HEIGHT 240

#include<stdio.h>
#include<stdlib.h>

typedef struct color{
	unsigned char r;
	unsigned char g;
	unsigned char b;

}COLOR;

COLOR data[WIDTH][HEIGHT];

int main(){
	int i, j;
	FILE *fp;
	FILE *out;

	char header[54];

	if((fp = fopen("image2.bmp", "rb")) == NULL){
		fprintf(stderr, "file open error");
		exit(1);
	}

	fread(header, 1, 54, fp);

	for(j=HEIGHT-1; j>=0; j--){
		for(i=0; i<WIDTH; i++){
			data[i][j].b = getc(fp);
			data[i][j].g = getc(fp);
			data[i][j].r = getc(fp);
		}
	}

	fclose(fp);



    // 輝度平均算出
    int sum = 0;
    for(j=0; j<HEIGHT; j++){
        for (i=0; i<WIDTH; i++){
  			sum += data[i][j].g;
        }
    }

    // 二値変換処理
	int binarization = sum/(WIDTH*HEIGHT); // この計算によって以下のif文で白黒かを判断させる
    for(j=0; j<HEIGHT; j++){
        for(i=0; i<WIDTH; i++) {
			if (data[i][j].g < binarization){
				data[i][j].r = data[i][j].g = data[i][j].b = 0;  // 黒
			}else{
				data[i][j].r = data[i][j].g = data[i][j].b = 255; // 白
            }
        }
    }

	if((out = fopen("out.bmp", "wb")) == NULL){
		fprintf(stderr, "file open error");
		exit(1);
	}
	fwrite(header, 1, 54, out);
    for(j=0; j<HEIGHT; j++){
        for(i=0; i<WIDTH; i++){
			fputc(data[i][j].b, out);
			fputc(data[i][j].g, out);
			fputc(data[i][j].r, out);
		}
	}
	fclose(out);
	return 0;
}


Output:
1
2
3
4
5
6
7
In function 'main':
Line 51: error: stray '\343' in program
Line 51: error: stray '\200' in program
Line 51: error: stray '\200' in program
Line 57: error: stray '\343' in program
Line 57: error: stray '\200' in program
Line 57: error: stray '\200' in program


Create a new paste based on this one


Comments: