Skip to content

Instantly share code, notes, and snippets.

@sjhalayka
Created November 23, 2023 19:30
Show Gist options
  • Save sjhalayka/71bc4a87ce612af1876a57baf6129521 to your computer and use it in GitHub Desktop.
Save sjhalayka/71bc4a87ce612af1876a57baf6129521 to your computer and use it in GitHub Desktop.
Programming Drill 1.3.2
#include <iostream>
#include <fstream>
#include <complex>
#include <vector>
using namespace std;
#include "Eigen/Dense"
using Eigen::VectorXcf;
using Eigen::VectorXf;
int main(void)
{
// Read in pixel data from disk
vector<unsigned char> pixel_data;
unsigned char idlength = 0;
unsigned char colourmaptype = 0;
unsigned char datatypecode = 0;
unsigned short int colourmaporigin = 0;
unsigned short int colourmaplength = 0;
unsigned char colourmapdepth = 0;
unsigned short int x_origin = 0;
unsigned short int y_origin = 0;
unsigned short int px = 0;
unsigned short int py = 0;
unsigned char bitsperpixel = 0;
unsigned char imagedescriptor = 0;
vector<char> idstring;
ifstream in("cat.tga", ios::binary);
if (!in.is_open())
{
cout << "Failed to open TGA file: cat.tga" << endl;
return false;
}
in.read(reinterpret_cast<char*>(&idlength), 1);
in.read(reinterpret_cast<char*>(&colourmaptype), 1);
in.read(reinterpret_cast<char*>(&datatypecode), 1);
in.read(reinterpret_cast<char*>(&colourmaporigin), 2);
in.read(reinterpret_cast<char*>(&colourmaplength), 2);
in.read(reinterpret_cast<char*>(&colourmapdepth), 1);
in.read(reinterpret_cast<char*>(&x_origin), 2);
in.read(reinterpret_cast<char*>(&y_origin), 2);
in.read(reinterpret_cast<char*>(&px), 2);
in.read(reinterpret_cast<char*>(&py), 2);
in.read(reinterpret_cast<char*>(&bitsperpixel), 1);
in.read(reinterpret_cast<char*>(&imagedescriptor), 1);
if (0 != idlength)
{
idstring.resize(idlength + 1, '\0');
in.read(&idstring[0], idlength);
}
if (2 != datatypecode)
{
cout << "TGA file must be in uncompressed/non-RLE 32-bit RGBA format." << endl;
return false;
}
else
{
if (32 != bitsperpixel)
{
cout << "TGA file must be in uncompressed/non-RLE 32-bit RGBA format." << endl;
return false;
}
size_t num_bytes = px * py * 4;
pixel_data.resize(num_bytes);
in.read(reinterpret_cast<char*>(&pixel_data[0]), num_bytes);
}
// March along image
float template_width = 2.0f;
float template_height = 2.0f;
float x_step_size = template_width / static_cast<float>(px - 1);
float y_step_size = template_height / static_cast<float>(py - 1);
float grid_x_min = -template_width / 2.0f;
float grid_y_min = -template_height / 2.0f;
float grid_x_pos = grid_x_min;
vector<unsigned char> temp_data(px*py*4, 0);
for (short unsigned int x = 0; x < px; x++, grid_x_pos += x_step_size)
{
float grid_y_pos = grid_y_min;
for (short unsigned int y = 0; y < py; y++, grid_y_pos += y_step_size)
{
size_t index = 4*(y * size_t(px) + x);
const complex<float> c(19.5f, 0.4f);
complex<float> pos(grid_x_pos, grid_y_pos);
pos = pos * c;
//temp_data[index + 0] = 255;
//temp_data[index + 1] = 255;
//temp_data[index + 2] = 255;
temp_data[index + 3] = 255;
}
}
pixel_data = temp_data;
temp_data.clear();
// Write modified image to disk
ofstream out("cat_out.tga", ios::binary);
if (!out.is_open())
return -1;
out.write(reinterpret_cast<char*>(&idlength), 1);
out.write(reinterpret_cast<char*>(&colourmaptype), 1);
out.write(reinterpret_cast<char*>(&datatypecode), 1);
out.write(reinterpret_cast<char*>(&colourmaporigin), 2);
out.write(reinterpret_cast<char*>(&colourmaplength), 2);
out.write(reinterpret_cast<char*>(&colourmapdepth), 1);
out.write(reinterpret_cast<char*>(&x_origin), 2);
out.write(reinterpret_cast<char*>(&y_origin), 2);
out.write(reinterpret_cast<char*>(&px), 2);
out.write(reinterpret_cast<char*>(&py), 2);
out.write(reinterpret_cast<char*>(&bitsperpixel), 1);
out.write(reinterpret_cast<char*>(&imagedescriptor), 1);
out.write(reinterpret_cast<char*>(&pixel_data[0]), px * py * 4 * sizeof(unsigned char));
out.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment