バイナリデータを画像に変換
Posted: 2014年8月03日(日) 14:02
現在ppmという拡張子のファイル(画像)をbmpまたはjpegに変換するプログラムを作成しています。
開発環境は、Borland C++ compilerを用いて、Cpadでコードを書いています。
ppmファイルから画像のサイズ等は取得でき、
画像部分のデータも文字の3次元配列として取得できたのですが、
それから画像を生成する方法がわかりません。
c#で書いたときはsetPxcelでできましたが、
c++での方法がわかりません。
使用するAPI等を教えていただけるとありがたいです。
以下現在のコードです。
開発環境は、Borland C++ compilerを用いて、Cpadでコードを書いています。
ppmファイルから画像のサイズ等は取得でき、
画像部分のデータも文字の3次元配列として取得できたのですが、
それから画像を生成する方法がわかりません。
c#で書いたときはsetPxcelでできましたが、
c++での方法がわかりません。
使用するAPI等を教えていただけるとありがたいです。
以下現在のコードです。
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <cstdio>
#define sscanf_s sscanf
#define buffer line
using namespace std;
int main(void)
{
int x, y, color;
std::ifstream file;
// 画像格納配列
static unsigned char image[1050][1050][3];
// 読み込んだ画像の解像度
int width = 0;
int height = 0;
// 読み込んだ画像の諧調数
int max_gray = 0;
// 画像のpath
const char* name = "test.ppm";
file.open(name,file.in | file.binary);
// 画像の形式の取得
std::string line;
getline(file, line);
// 画像の解像度の取得
while ( width == 0 || height == 0 )
{
std::string line;
std::getline(file, line);
//コメント行を飛ばす
if ( line.at(0) != '#' )
{
sscanf_s( line.c_str(), "%d %d", &width, &height );
}
}
// 画像の諧調数の取得
while (max_gray == 0)
{
std::string line;
std::getline(file, line);
if ( buffer[0] != '#' )
{
sscanf_s( line.c_str(), "%d", &max_gray );
}
}
// 画像データの取得
for(y=0; y<height; y++)
{
for(x=0; x<width; x++)
{
for(color=0; color<3; color++)
{
file.read(reinterpret_cast<char*>(&image[x][y][color]), 1);
}
}
}
printf("line = %s\n",line.c_str());
printf("width = %d, height = %d\n",width, height);
printf("max_gray = %d\n", max_gray);
puts("image data:");
for(y=0;y<height;y++)
{
for(x=0;x<width;x++)
{
for(color=0;color<3;color++)
{
if(x+1==width && color+1==3)
{
printf("%3d\n", image[x][y][color]);
}
else
{
printf("%3d ", image[x][y][color]);
}
}
}
}
return 0;
}