46
56

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C++で超簡単に画像出力+サンプル

Posted at

準備

"stb_image_write.h"をダウンロードして、これから作る main.cpp と同じフォルダに入れる。
後は以下のサンプルを貼り付けて実行。

以下のサンプルはC++11以降の環境で動作する。

何もない画像

main.cpp
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

#include <cstddef>
#include <memory>
#include <new>

struct RGBA {
	unsigned char r, g, b, a; //赤, 緑, 青, 透過
	RGBA() = default;
	constexpr RGBA(const unsigned char r_, const unsigned char g_, const unsigned char b_, const unsigned char a_) :r(r_), g(g_), b(b_), a(a_) {}
};

int main() {

	constexpr std::size_t width{ 512 }, height{ 512 }; //幅と高さ

	std::unique_ptr<RGBA[][width]> rgba(new(std::nothrow) RGBA[height][width]);
	if (!rgba) return -1;

	stbi_write_png("picture_0.png", static_cast<int>(width), static_cast<int>(height), static_cast<int>(sizeof(RGBA)), rgba.get(), 0);
}

picture_0.png

赤いHD画像

main.cpp
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

#include <cstddef>
#include <memory>
#include <new>

struct RGBA {
	unsigned char r, g, b, a; //赤, 緑, 青, 透過
	RGBA() = default;
	constexpr RGBA(const unsigned char r_, const unsigned char g_, const unsigned char b_, const unsigned char a_) :r(r_), g(g_), b(b_), a(a_) {}
};

int main() {

	constexpr std::size_t width{ 1280 }, height{ 720 }; //幅と高さ

	std::unique_ptr<RGBA[][width]> rgba(new(std::nothrow) RGBA[height][width]);
	if (!rgba) return -1;

	for (std::size_t row{}; row < height; ++row)
		for (std::size_t col{}; col < width; ++col) {
			rgba[row][col].r = 255; //赤
			rgba[row][col].g = 0;
			rgba[row][col].b = 0;
			rgba[row][col].a = 255; //不透過
		}

	stbi_write_png("picture_1.png", static_cast<int>(width), static_cast<int>(height), static_cast<int>(sizeof(RGBA)), rgba.get(), 0);
}

picture_2.png

ホワイトノイズ

main.cpp
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

#include <cstddef>
#include <memory>
#include <new>
#include <random>

struct RGBA {
	unsigned char r, g, b, a; //赤, 緑, 青, 透過
	RGBA() = default;
	constexpr RGBA(const unsigned char r_, const unsigned char g_, const unsigned char b_, const unsigned char a_) :r(r_), g(g_), b(b_), a(a_) {}
};

int main() {

	constexpr std::size_t width{ 1280 }, height{ 720 }; //幅と高さ

	std::unique_ptr<RGBA[][width]> rgba(new(std::nothrow) RGBA[height][width]);
	if (!rgba) return -1;

	std::random_device rd;
	std::mt19937 mt;
	mt.seed(rd());

	std::uniform_int_distribution<> uid(0, 255);

	for (std::size_t row{}; row < height; ++row)
		for (std::size_t col{}; col < width; ++col) {
			rgba[row][col].r = uid(mt); //赤
			rgba[row][col].g = uid(mt);
			rgba[row][col].b = uid(mt);
			rgba[row][col].a = 255; //不透過
		}

	stbi_write_png("picture_2.png", static_cast<int>(width), static_cast<int>(height), static_cast<int>(sizeof(RGBA)), rgba.get(), 0);
}

picture_2.png

RPGマップ

main.cpp
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

#include <cstddef>
#include <memory>
#include <new>
#include <random>

struct RGBA {
	unsigned char r, g, b, a; //赤, 緑, 青, 透過
	RGBA() = default;
	constexpr RGBA(const unsigned char r_, const unsigned char g_, const unsigned char b_, const unsigned char a_) :r(r_), g(g_), b(b_), a(a_) {}
	constexpr bool operator&&(const RGBA& rgba_) noexcept {
		return this->r == rgba_.r && this->g == rgba_.g && this->b == rgba_.b && this->a == rgba_.a;
	}
};

int main() {

	constexpr std::size_t width{ 512 }, height{ 512 }; //幅と高さ

	std::unique_ptr<RGBA[][width]> rgba(new(std::nothrow) RGBA[height][width]);
	if (!rgba) return -1;

	std::random_device rd;
	std::mt19937 mt;
	mt.seed(rd());
	std::uniform_int_distribution<> uid(0, 255);

	std::uniform_int_distribution<> uid_probability(0, 1);
	std::uniform_int_distribution<> uid_b(0, 3);

	for (std::size_t row{}; row < height; ++row)
		for (std::size_t col{}; col < width; ++col) {
			rgba[row][col].r = 41;
			rgba[row][col].g = 40;
			rgba[row][col].b = 159;
			rgba[row][col].a = 255;
		}
	for (std::size_t row{ 1 }; row < height - 1; ++row)
		for (std::size_t col{ 1 }; col < width - 1; ++col) {
			if (uid_probability(mt)) rgba[row][col] = RGBA(41, 40, 159, 255);
			else switch (uid_b(mt))
			{
			case 0:rgba[row][col] = RGBA(101, 163, 56, 255); break;
			case 1:rgba[row][col] = RGBA(223, 203, 140, 255); break;
			case 2:rgba[row][col] = RGBA(9, 100, 5, 255); break;
			case 3:rgba[row][col] = RGBA(164, 143, 50, 255); break;
			}
		}
	for (std::size_t i{}; i < 700; ++i)
		for (std::size_t row{ 1 }; row < height - 1; ++row)
			for (std::size_t col{ 1 }; col < width - 1; ++col) {
				if ((rgba[row][col - 1] && rgba[row][col + 1]) && ((rgba[row][col + 1]) && (rgba[row - 1][col])) && (rgba[row - 1][col] && rgba[row + 1][col])) {
					rgba[row][col] = rgba[row][col + 1];
				}
				else switch (uid_b(mt))
				{
				case 0:rgba[row][col] = rgba[row][col - 1]; break;
				case 1:rgba[row][col] = rgba[row][col + 1]; break;
				case 2:rgba[row][col] = rgba[row - 1][col]; break;
				case 3:rgba[row][col] = rgba[row + 1][col]; break;
				}
			}

	stbi_write_png("picture_3.png", static_cast<int>(width), static_cast<int>(height), static_cast<int>(sizeof(RGBA)), rgba.get(), 0);
}

picture_3.png

##ソースコードのライセンス

These codes are licensed under CC0.
CC0

ソースコードは自由に使用してください。

46
56
3

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
46
56

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?