LoginSignup
1
1

More than 1 year has passed since last update.

C++でRaw画像を読み込んで、そのまま出力する

Last updated at Posted at 2022-08-18

取り組んだこと

C++でRaw画像を読み込んで、そのまま出力することに取り組んだ。
勉強のためにスマートポインタも使ってみた。

コード

main.cpp
#include <iostream>
#include <string>
#include <fstream>


#define X_SIZE 768
#define Y_SIZE 576
#define CH 3

std::string input_name = "dog.raw";
std::string output_name = "dog_out.raw";


int main(void) {
	
	std::unique_ptr<unsigned char[]> input_image=std::make_unique<unsigned char[]>(X_SIZE * Y_SIZE * CH);
	std::unique_ptr<unsigned char[]> output_image=std::make_unique<unsigned char[]>(X_SIZE * Y_SIZE * CH);

	// 画像読み込み
	std::ifstream fin(input_name.c_str(), std::ios::in | std::ios::binary );

	if (!fin){
		std::cout << "ファイル が開けません"<<std::endl;
		return 1;
    }

	fin.read(reinterpret_cast<char*>(input_image.get()), X_SIZE * Y_SIZE * CH);

	// 画像書き込み
	for (int y = 0; y < Y_SIZE; y++) {
		for (int x = 0; x < X_SIZE; x++) {
			for (int ch = 0; ch < CH; ch++) {
				output_image[(y*X_SIZE + x)*CH + ch] = input_image[(y*X_SIZE + x)*CH + ch];
			}
		}
	}

    std::ofstream fout;
    fout.open(output_name.c_str(), std::ios::out|std::ios::binary);

	fout.write(reinterpret_cast<char*>(input_image.get()), X_SIZE * Y_SIZE * CH);


	fin.close();
	fout.close();

	return 0;
}

使用画像

以下の画像をimage JでRawに直したもの。
dog.jpg

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1