This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <string.h> | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <cstdlib> | |
/* 定数宣言 */ | |
#define MAX_IMAGESIZE 4000 /* 想定する縦・横の最大画素数 */ | |
#define MAX_BRIGHTNESS 255 /* 想定する最大階調値 */ | |
#define GRAYLEVEL 256 /* 想定する階調数(=最大階調値+1) */ | |
#define MAX_FILENAME 256 /* 想定するファイル名の最大長 */ | |
#define MAX_BUFFERSIZE 256 /* 利用するバッファ最大長 */ | |
#define MAX_NUM_OF_IMAGES 3 /* 利用する画像の枚数 */ | |
/* 大域変数の宣言 */ | |
/* 画像データ image[n][x][y][col] (col=0:R, =1:G, =2:B) */ | |
unsigned char image[MAX_NUM_OF_IMAGES] | |
[MAX_IMAGESIZE][MAX_IMAGESIZE][3]; | |
/* 横幅と縦幅 */ | |
int width[MAX_NUM_OF_IMAGES],height[MAX_NUM_OF_IMAGES]; | |
void load_color_image( int n, char name[] ); /* 画像の入力 */ | |
void save_color_image( int n, char name[] ); /* 画像の出力 */ | |
void load_color_image( int n, char name[] ) | |
// カラー画像No.n | |
{ | |
char fname[MAX_FILENAME]; /* ファイル名用の文字配列 */ | |
char buffer[MAX_BUFFERSIZE]; /* データ読込み用変数 */ | |
FILE *fp; /* ファイルポインタ */ | |
int max_gray=0,x,y,col; /* 最大階調値と制御変数 */ | |
// ファイルのオープン | |
std::ifstream file; | |
file.open(name, file.in | file.binary); | |
if(!file.is_open()){ | |
std::cout << "ファイルが開けません" << std::endl; | |
exit(1); | |
} | |
// 読み込み | |
std::string line; | |
getline(file, line); | |
// P6モードのみ対応 | |
if ( line != "P6" ){ | |
printf("ファイルタイプがP6ではありません\n"); | |
exit(1); | |
} | |
// 全行読み込み | |
width[n] = 0; | |
height[n] = 0; | |
while ( width[n] == 0 || height[n] == 0 ){ | |
std::string line; | |
std::getline(file, line); | |
if ( line.at(0) != '#' ) { | |
sscanf_s( line.c_str(), "%d %d", &width[n], &height[n] ); | |
} | |
} | |
/* max_gray の代入(#からのコメントは読み飛ばす) */ | |
while ( max_gray == 0 ){ | |
std::string line; | |
std::getline(file, line); | |
if ( buffer[0] != '#' ){ | |
sscanf_s( line.c_str(), "%d", &max_gray ); | |
} | |
} | |
/* パラメータの画面への表示 */ | |
printf("横:%d, 縦:%d\n", width[n], height[n] ); | |
printf("最大階調値 = %d\n",max_gray); | |
if ( width[n] > MAX_IMAGESIZE || height[n] > | |
MAX_IMAGESIZE || | |
max_gray != MAX_BRIGHTNESS ){ | |
printf("サイズか最大階調値が不適切です\n"); | |
exit(1); /* 強制終了 */ | |
} | |
// 画像データロード | |
int whilecnt = 0; | |
for(y=0;y<height[n];++y){ | |
for(x=0;x<width[n];++x){ | |
for(col=0;col<3;++col){ | |
file.read(reinterpret_cast<char*>(&image[n][x][y][col]), 1); | |
} | |
} | |
} | |
std::cout << "while cnt: " << whilecnt << std::endl; | |
std::cout << "カラー画像を画像 No." << n << " に読み込みました" << std::endl; | |
file.close(); | |
} | |
void save_color_image( int n, char name[] ) | |
// n:画像番号 | |
// 画像の横幅,縦幅はそれぞれ width[n], height[n] に予め入れておく | |
{ | |
char fname[MAX_FILENAME]; /* ファイル名用の文字配列 */ | |
FILE *fp; /* ファイルポインタ */ | |
int x,y,col; /* ループ変数 */ | |
std::ofstream file; | |
file.open(name, file.out | file.binary | file.trunc); | |
if(!file.is_open()){ | |
std::cout << "ファイルが開けませんでした!" << std::endl; | |
exit(1); | |
} | |
// タイプ | |
file << "P6" << std::endl; | |
// コメント | |
file << "# pmm image created by masaz" << std::endl; | |
// 解像度 | |
file << width[n] << " " << height[n] << std::endl; | |
// 階調 | |
file << MAX_BRIGHTNESS << std::endl; | |
// データ出力 | |
for(y=0;y<height[n];y++){ | |
for(x=0;x<width[n];x++){ | |
for(col=0;col<3;col++){ | |
file.write(reinterpret_cast<char*>(&image[n][x][y][col]), 1); | |
} | |
} | |
} | |
file.close(); | |
std::cout << "画像出力完了" << std::endl; | |
} |
0 件のコメント:
コメントを投稿