Code

raw to bmp

BIGFROG 2022. 3. 29. 01:11

make raw file

* Mach Bands

#include <iostream>
#include <fstream>
#define X_MAX 512
#define Y_MAX 512
#include <Windows.h>
#pragma warning(disable:4996)

using namespace std;

int main(void) {
	int x, y;
	unsigned char arr[X_MAX*Y_MAX];


	ofstream raw;	
	raw.open("my_image.raw");
	if (raw.is_open()) {
		cout << raw.is_open() << endl;
		cout << "raw file is created and opened" << endl;
		
		for (y = 0; y < 512; y++) {
			for (x = 0; x < 512; x++) {
				if (x < 100) {
					arr[y * X_MAX + x] = 120;
				}
				else if (x < 200) {
					arr[y * X_MAX + x] = (15 * x / 100) + 120;
				}
				else if (x < 280) {
					arr[y * X_MAX + x] = (90 * x / 80) + 135;
				}
				else if (x < 300) {
					arr[y * X_MAX + x] = (15 * x / 20) + 225;
				}
				else {
					arr[y * X_MAX + x] = 240;
				}

			}
		}

		raw.write((char*) arr,X_MAX * Y_MAX);
		raw.close();
	
	}
	return 0;
}

 

make bmp file using another bmp file.

#include <iostream>
#include <fstream>
#define X_MAX 512
#define Y_MAX 512
#include <Windows.h>
#pragma warning(disable:4996)

using namespace std;


int main(void) {

	
	ifstream raw;
	string path = "C:\\Users\\bigfr\\source\\repos\\image_processing_hw1\\image_processing_hw1\\";
	//string filename = "my_image.raw";
	string filename = "lena_raw_512x512.raw";
	raw.open(path+filename,ios::binary);
	if (raw.fail()) {
		cerr << "Error: " << strerror(errno);
		return 0;
	
	}
	unsigned char arr_bmp[X_MAX * Y_MAX];
	raw.read((char*)arr_bmp, X_MAX * Y_MAX); // ! Make it reversed !
	unsigned char reversed_raw[X_MAX * Y_MAX];
	unsigned char test_raw[X_MAX * Y_MAX];

	/*
	Make reversed array after file read
	*/
	
	for (int y = 0; y < Y_MAX; y++) {
		for (int x = 0; x < X_MAX; x++) {
			reversed_raw[x + 512 * (511 - y)] = arr_bmp[x + 512 * y];
		}
	}
	
	
	FILE* infile;
	FILE* outfile;
	
	fopen_s(&infile, "lena_bmp_512x512_new.bmp", "rb");
	fopen_s(&outfile, "result_lena.bmp", "wb");

	BITMAPFILEHEADER hfile; // 파일 정보 헤더
	BITMAPINFOHEADER hinfo; // 영상 정보 헤더

	fread(&hfile, sizeof(BITMAPFILEHEADER), 1, infile);
	fread(&hinfo, sizeof(BITMAPINFOHEADER), 1, infile);

	RGBQUAD hRGB[256];
	fread(hRGB, sizeof(RGBQUAD), 256, infile);
	

	fwrite(&hfile, sizeof(unsigned char), sizeof(BITMAPFILEHEADER), outfile);
	fwrite(&hinfo, sizeof(unsigned char), sizeof(BITMAPINFOHEADER), outfile);

	fwrite(hRGB, sizeof(RGBQUAD), 256, outfile);
	fwrite(reversed_raw, sizeof(unsigned char), 512 * 512, outfile);

	raw.close();
	fclose(infile);
	fclose(outfile);

	return 0;
}

 

Rotate counter-clockwise 90 degree

#include <stdio.h>
#include <iostream>
#define X_MAX 512
#define Y_MAX 512
#include <Windows.h>
#pragma warning(disable:4996)

using namespace std;
void main() {
	int x, y;
	
	//unsigned char arr_bmp[X_MAX * Y_MAX];
	unsigned char * pArr = (unsigned char*)malloc(sizeof(unsigned char) * 512 * 512);
	//unsigned char rot_arr[X_MAX * Y_MAX];
	unsigned char* pRot = (unsigned char*)malloc(sizeof(unsigned char) * 512 * 512);
	FILE* infile;
	fopen_s(&infile, "C:\\Users\\bigfr\\source\\repos\\image_processing_hw1\\hw1_2\\result.bmp", "rb");
	FILE* outfile;
	fopen_s(&outfile, "rotated_result.bmp", "wb");

	BITMAPFILEHEADER hfile; // 파일 정보 헤더
	BITMAPINFOHEADER hinfo; // 영상 정보 헤더

	fread(&hfile, sizeof(BITMAPFILEHEADER), 1, infile);
	fread(&hinfo, sizeof(BITMAPINFOHEADER), 1, infile);

	RGBQUAD hRGB[256];
	fread(hRGB, sizeof(RGBQUAD), 256, infile);
	fread(pArr, sizeof(unsigned char), 512 * 512, infile);
	
	unsigned char reversed_raw[X_MAX * Y_MAX];
	unsigned char reversed_rot[X_MAX * Y_MAX];
	/*
	Make reversed array after file read
	*/

	for (int y = 0; y < Y_MAX; y++) {
		for (int x = 0; x < X_MAX; x++) {
			reversed_raw[x + 512 * (511 - y)] = pArr[x + 512 * y];
		}
	}

	/*
	Rotate "counter-clockwise" 90 degree.
	*/
	for (y = 0; y < 512; y++) {
		for (x = 0; x < 512; x++) {
			pRot[(511-x) * 512 + y] = reversed_raw[x + y * 512];
		}
	}
	std::cout << "rotated counter-clockwise." << std::endl;
	

	/*
	Make reversed array again
	*/

	for (int y = 0; y < Y_MAX; y++) {
		for (int x = 0; x < X_MAX; x++) {
			reversed_rot[x + 512 * (511 - y)] = pRot[x + 512 * y];
		}
	}

	fwrite(&hfile, sizeof(unsigned char), sizeof(BITMAPFILEHEADER), outfile);
	fwrite(&hinfo, sizeof(unsigned char), sizeof(BITMAPINFOHEADER), outfile);
	fwrite(hRGB, sizeof(RGBQUAD), 256, outfile);
	fwrite(reversed_rot, sizeof(unsigned char), 512 * 512, outfile);

	fclose(infile);
	fclose(outfile);
	free(pArr);
	free(pRot);
	std::cout << "Done." << std::endl;
}

 

'Code' 카테고리의 다른 글

python TCP calculator  (0) 2022.05.10
python UDP Calculator  (0) 2022.05.09
백준 4354  (0) 2022.02.07
백준 1786  (0) 2022.02.07
[백준1978] 소수찾기  (0) 2020.01.09