#1
by c言語 » 3年前
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
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[i] = 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[i] = 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[i] = 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