TIL) 직선 그리기

PPM이미지를 기반으로 여러 직선을 그려보는 실습을 진행하였다.

Conditions

  1. 128x128 PPM 이미지에 가장자리 4개, 대각선 형태 2개, 총 6개의 직선을 그렸다.
  2. 안티 얼라이싱은 하지 않았다.

Codes

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

void Make_imagefile(const string& fname, const int& width, const int& height, const vector<uint8_t>& pixel);

int main()
{
	const string filename = "Example_straight.PPM";

	const int image_width = 128;
	const int image_height = 128;

	vector<uint8_t> buffer(image_width * image_height * 3);

	for (int j = 0; j < image_height;j++)
	{
		for (int i = 0; i < image_width;i++)
		{
			const int offset = (i + j* image_width) * 3;
			if (j == 0 || j+1 == image_height || i == 0 || i+1 == image_width)
			{
				buffer[offset] = 0;
				buffer[offset + 1] = 0;
				buffer[offset + 2] = 0;
			}
			else if (i == j || ((image_width - (i+1)) == j))
			{
				buffer[offset] = 0;
				buffer[offset + 1] = 0;
				buffer[offset + 2] = 0;
			}
			else
			{
				buffer[offset] = 255;
				buffer[offset + 1] = 255;
				buffer[offset + 2] = 255;
			}
		}
	}

	Make_imagefile(filename, image_width, image_height, buffer);
}

void Make_imagefile(const string& fname, const int& width, const int& height, const vector<uint8_t>& pixel)
{
	ofstream ofile(fname);

	ofile << "P3\n" << width << " " << height << "\n255\n";

	for (int j = 0; j < height; ++j)
	{
		for (int i = 0; i < width; i++)
		{
			const int offset = (j + i * width) * 3;
			for (int c = 0; c < 3; c++)
			{
				ofile << (int)pixel[offset + c];
				ofile << " ";
			}
		}
	}

	ofile << flush;
}

Result

Review

  1. 코드의 일반화를 생각하는 설계가 이제 더더욱 필요하다. 현재는 이미지 파일을 만드는 부분만 함수로 처리했지만, 앞으로는 추가적으로 pixel을 그리는 부분을 클래스를 이용해서 정리할 필요가 있다.

좋은 웹페이지 즐겨찾기