C言語 BMP画像反転、色反転

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
c言語

C言語 BMP画像反転、色反転

#1

投稿記事 by c言語 » 2年前

bmp画像の左右反転と上下反転をかなり考えたのですが、うまいこと行きません。
(画像が真っ暗か灰色にしかならない)
初めのコピーの部分は特に問題なくできました。
良ければご教授お願いしたいです。
下のコードが今書いている左右反転、上下反転のコードです。
もしよろしければ色反転も教えてくださると嬉しいです。
何卒よろしくお願い申し上げます。

code
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <fcntl.h>
#define MAX_SIZE 256
////////////////////////////データ////////////////////////////////
char r[MAX_SIZE][MAX_SIZE];
char g[MAX_SIZE][MAX_SIZE];
char b[MAX_SIZE][MAX_SIZE];
int x, y; //ループ用の変数
int width=MAX_SIZE, height=MAX_SIZE; //画像の縦横サイズ
FILE *file_in; //ファイル読み込み
FILE *file_out; //ファイル書き込み
ichar key = getchar();
/////////////////////////ヘッダ////////////////////////////////////////
FILE * fp_in = NULL;//読み込み用ポインタ
FILE * fp_out = NULL;//書き込み用ポインタ
unsigned char hdr[54]; // ヘッダ格納用の配列
int i; // ループ用変数

//Cが押されたら画像のコピーを実行
if (key == 'c'){
fopen_s(&file_in, "bmp画像.bmp", "rb"); // 画像データ開く
// ヘッダ情報の読み込み
for (i = 0; i < 54; i++) hdr = fgetc(file_in);
for (y = 0; y < height; y++) { //画像データ読み込み
for (x = 0; x < width; x++) {
fread(&b[y][x], sizeof(b[y][x]), 1, file_in);
fread(&g[y][x], sizeof(g[y][x]), 1, file_in);
fread(&r[y][x], sizeof(r[y][x]), 1, file_in);

}
}
// 画像データの書き込み
fopen_s(&file_out, "コピー.bmp", "wb"); //画像の書き込み

// ヘッダー部
fwrite(hdr, sizeof(hdr), 1, file_out);

// データ部
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
fwrite(&b[y][x], sizeof(b[y][x]), 1, file_out);
fwrite(&g[y][x], sizeof(g[y][x]), 1, file_out);
fwrite(&r[y][x], sizeof(r[y][x]), 1, file_out);
}
}
fclose(file_out);
fclose(file_in);
printf("画像の書き込みが終わりました\n");
}


//左右反転処理
if (key == 'w') {


fopen_s(&file_in, "bmp画像.bmp", "rb"); // 画像データ開く
for (i = 0; i < 54; i++) hdr = fgetc(file_in);// ヘッダ情報の読み込み
for (y = 0; y < height; y++) { //画像データ読み込み
for (x = 0; x < width; x++) {
fread(&b[y][x], sizeof(b[y][x]), 1, file_in);
fread(&g[y][x], sizeof(g[y][x]), 1, file_in);
fread(&r[y][x], sizeof(r[y][x]), 1, file_in);
}
}
fopen_s(&file_out, "yoko.bmp", "wb");
// ヘッダー部
fwrite(hdr, sizeof(hdr), 1, file_out);
// データ部
for (y = 0; y < height; y++) {
for (x = width - 1; 0 <= x; x--) {
fwrite(&b[y][x], 1, 1, file_out);
fwrite(&g[y][x], 1, 1, file_out);
fwrite(&r[y][x], 1, 1, file_out);
}
}
fclose(file_out);
fclose(file_in);
printf("画像の書き込みが終わりました\n");
}

//上下反転処理
if (key == 'u') {


fopen_s(&file_in, "bmp画像.bmp", "rb"); // 画像データ開く
for (i = 0; i < 54; i++) hdr = fgetc(file_in);// ヘッダ情報の読み込み
for (y = 0; y < height; y++) { //画像データ読み込み
for (x = 0; x < width; x++) {
fread(&b[y][x], sizeof(b[y][x]), 1, file_in);
fread(&g[y][x], sizeof(g[y][x]), 1, file_in);
fread(&r[y][x], sizeof(r[y][x]), 1, file_in);
}
}
fopen_s(&file_out, "tate.bmp", "wb");
// ヘッダー部
fwrite(hdr, sizeof(hdr), 1, file_out);
// データ部
for (y = 0 ; y < height / 2 ; y++) {
for (x = 0; x < width; x++) {
fwrite(&b[height - y - 1][x], 1, 1, file_out);
fwrite(&g[height - y - 1][x], 1, 1, file_out);
fwrite(&r[height - y - 1][x], 1, 1, file_out);
}
}
fclose(file_out);
fclose(file_in);
printf("画像の書き込みが終わりました\n");
}
_getch();
return 0;
}
/code

Bull
記事: 149
登録日時: 9年前

Re: C言語 BMP画像反転、色反転

#2

投稿記事 by Bull » 2年前

とりあえず動くようにしてみました。
左右反転だけです。上下反転も同じようにできるはずです。

コード:

#include <stdio.h>

#define MAX_SIZE 2560								//画素データの最大サイズ
////////////////////////////データ////////////////////////////////
char r[MAX_SIZE][MAX_SIZE];
char g[MAX_SIZE][MAX_SIZE];
char b[MAX_SIZE][MAX_SIZE];

int main(void)
{
	unsigned char hdr[54];							// ヘッダ格納用の配列

	//画像ファイル読込み
	FILE *file_in = fopen("bmp画像.bmp", "rb");
	if (file_in == NULL) {
		perror("Can't open image file");
		return 1;
	}
	for (int i = 0; i < 54; i++) hdr[i] = fgetc(file_in);// ヘッダ情報の読み込み
	int width  = hdr[18] + (hdr[19] + (hdr[20] + (hdr[21] * 256) * 256)) * 256;	//横幅
	int height = hdr[22] + (hdr[23] + (hdr[24] + (hdr[25] * 256) * 256)) * 256;	//高さ
	for (int y = 0; y < height; y++) {				//画像データ読み込み
		for (int x = 0; x < width; x++) {
			fread(&b[y][x], sizeof(b[y][x]), 1, file_in);
			fread(&g[y][x], sizeof(g[y][x]), 1, file_in);
			fread(&r[y][x], sizeof(r[y][x]), 1, file_in);
		}
	}

	//反転した画像保存
	FILE *file_out = fopen("反転.bmp", "wb");
	if (file_out == NULL) {
		perror("Can't open output image file");
		return 2;
	}
	// ヘッダー部
	fwrite(hdr, sizeof(hdr), 1, file_out);
	// データ部
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			fwrite(&b[y][width - x - 1], 1, 1, file_out);
			fwrite(&g[y][width - x - 1], 1, 1, file_out);
			fwrite(&r[y][width - x - 1], 1, 1, file_out);
		}
	}
	fclose(file_out);
	fclose(file_in);
	printf("画像の書き込みが終わりました\n");
}
※24bit BMP にしか対応していません。

Bull
記事: 149
登録日時: 9年前

Re: C言語 BMP画像反転、色反転

#3

投稿記事 by Bull » 2年前

すいません。パディングを考慮してなかったので、横の画素数が 4の倍数以外の画像ファイルは正常に処理できなかったです。
こうしないとダメですね。

コード:

#include <stdio.h>

#define MAX_SIZE 2560								//画素データの最大サイズ
////////////////////////////データ////////////////////////////////
unsigned char r[MAX_SIZE][MAX_SIZE];
unsigned char g[MAX_SIZE][MAX_SIZE];
unsigned char b[MAX_SIZE][MAX_SIZE];

int main(void)
{
	unsigned char hdr[54];							// ヘッダ格納用の配列

	//画像ファイル読込み
	FILE* file_in = fopen("bmp画像.bmp", "rb");
	if (file_in == NULL) {
		perror("Can't open image file");
		return 1;
	}
	for (int i = 0; i < 54; i++) hdr[i] = fgetc(file_in);// ヘッダ情報の読み込み
	int width  = hdr[18] + (hdr[19] + (hdr[20] + (hdr[21] * 256) * 256)) * 256;	//横幅
	int height = hdr[22] + (hdr[23] + (hdr[24] + (hdr[25] * 256) * 256)) * 256;	//高さ
	for (int y = 0; y < height; y++) {				//画像データ読み込み
		int x;
		for (x = 0; x < width; x++) {
			fread(&b[y][x], sizeof(b[y][x]), 1, file_in);
			fread(&g[y][x], sizeof(g[y][x]), 1, file_in);
			fread(&r[y][x], sizeof(r[y][x]), 1, file_in);
		}
		//パディングの部分を読み飛ばす
		for (int w = x * 3; w % 4; ++w) fgetc(file_in);
	}

	//反転した画像保存
	FILE *file_out = fopen("tate.bmp", "wb");
	if (file_out == NULL) {
		perror("Can't open output image file");
		return 2;
	}
	// ヘッダー部
	fwrite(hdr, sizeof(hdr), 1, file_out);
	// データ部
	for (int y = 0; y < height; y++) {
		int x;
		for (x = 0; x < width; x++) {
			fwrite(&b[height - y - 1][x], 1, 1, file_out);
			fwrite(&g[height - y - 1][x], 1, 1, file_out);
			fwrite(&r[height - y - 1][x], 1, 1, file_out);
		}
		//パディングを書き込む
		for (int w = x * 3; w % 4; ++w) fputc(0, file_out);
	}
	fclose(file_out);
	fclose(file_in);
	printf("画像の書き込みが終わりました\n");
}
※上下反転です

返信

“C言語何でも質問掲示板” へ戻る