Skip to content

Commit 5114e9a

Browse files
authored
Clean up code (#9)
* Clean up code * Add suggested changes * fix minor typo * remove unused file: <fstream.h> from export.h
1 parent 271da53 commit 5114e9a

File tree

3 files changed

+119
-9
lines changed

3 files changed

+119
-9
lines changed

Diff for: src/export.cpp

+102
Original file line numberDiff line numberDiff line change
@@ -1 +1,103 @@
1+
#include <cstdlib>
2+
#include <iostream>
3+
#include <string>
4+
#include <fstream>
5+
16
#include "export.h"
7+
8+
/* Implementation of Rgba */
9+
Rgba::Rgba(uint32_t hex)
10+
:r((hex & 0xFF'00'00'00) >> 24),
11+
g((hex & 0x00'FF'00'00) >> 16),
12+
b((hex & 0x00'00'FF'00) >> 8),
13+
a(hex & 0x00'00'00'FF)
14+
{
15+
}
16+
17+
Rgba::Rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
18+
:r(r),
19+
g(g),
20+
b(b),
21+
a(a)
22+
{
23+
}
24+
25+
constexpr uint32_t Rgba::to_hex(const Rgba &rgba)
26+
{
27+
return ((rgba.r << 24) |
28+
(rgba.g << 16) |
29+
(rgba.b << 8) |
30+
(rgba.a));
31+
}
32+
33+
/* --- End --- */
34+
35+
/* Implementation of Image */
36+
Image::Image(uint32_t width, uint32_t height)
37+
:extent{width, height},
38+
pixels(width * height, Rgba(0))
39+
{
40+
}
41+
42+
void Image::export_to_ppm(const Image &input, const std::string &path)
43+
{
44+
std::ofstream output(path);
45+
output << "P3\n";
46+
output << "# " << path << '\n';
47+
output << input.extent.width << " " << input.extent.height << '\n';
48+
output << "255\n";
49+
50+
for(uint32_t y = 0; y < input.extent.height; ++y)
51+
{
52+
for(uint32_t x = 0; x < input.extent.width; ++x)
53+
{
54+
Rgba current = input.pixels.at(y * input.extent.width + x);
55+
output << static_cast<uint32_t>(current.r) << '\n'
56+
<< static_cast<uint32_t>(current.g) << '\n'
57+
<< static_cast<uint32_t>(current.b) << '\n';
58+
}
59+
}
60+
61+
output.close();
62+
}
63+
64+
65+
Rgba &Image::operator[](Index2D index)
66+
{
67+
return this->pixels.at(index.y * this->extent.width + index.x);
68+
}
69+
70+
const Rgba &Image::operator[](Index2D index) const
71+
{
72+
return this->pixels.at(index.y * this->extent.width + index.x);
73+
}
74+
75+
/* --- End --- */
76+
77+
Image upscale(const Image &input, uint32_t scale)
78+
{
79+
uint32_t target_width_v = input.extent.width * scale;
80+
uint32_t target_height_v = input.extent.height * scale;
81+
uint32_t x_index{0}, y_index{0};
82+
Image target_image(target_width_v, target_height_v);
83+
84+
for(y_index = 0; y_index < input.extent.height; ++y_index)
85+
{
86+
for(x_index = 0; x_index < input.extent.width; ++x_index)
87+
{
88+
Rgba current = input[Index2D{x_index, y_index}];
89+
if(Rgba::to_hex(current))
90+
{
91+
for(uint32_t i = y_index * scale; i < ((y_index + 1) * scale); ++i)
92+
{
93+
for(uint32_t j = x_index * scale; j < ((x_index + 1) * scale); ++j)
94+
{
95+
target_image[Index2D{j, i}] = current;
96+
}
97+
}
98+
}
99+
}
100+
}
101+
102+
return target_image;
103+
}

Diff for: src/export.h

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
#pragma once
22

33
#include <vector>
4-
#include <span>
54
#include <cstdint>
5+
#include <string>
66

77
using std::uint8_t;
88
using std::uint32_t;
99

1010
struct Rgba
1111
{
12+
Rgba(uint32_t hex);
13+
Rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
1214
uint8_t r{0}, g{0}, b{0}, a{0};
15+
static constexpr uint32_t to_hex(const Rgba &rgba);
16+
};
17+
18+
struct Index2D
19+
{
20+
uint32_t x{0}, y{0};
1321
};
1422

1523
struct Extent
@@ -19,8 +27,13 @@ struct Extent
1927

2028
struct Image
2129
{
22-
std::vector<Rgba> pixels;
30+
Image(uint32_t width, uint32_t height);
31+
static void export_to_ppm(const Image &input, const std::string &path);
32+
Rgba &operator[](Index2D index);
33+
const Rgba &operator[](Index2D index) const;
34+
2335
Extent extent;
36+
std::vector<Rgba> pixels;
2437
};
2538

26-
Image upscale(std::span<Rgba const> input, uint32_t scale);
39+
Image upscale(const Image &input, uint32_t scale);

Diff for: src/main.cpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
#include "export.h"
22

3-
constexpr uint32_t image_width_v = 512;
4-
constexpr uint32_t image_height_v = 512;
5-
constexpr uint32_t input_extent_v = 1;
6-
constexpr uint32_t output_extent_v = 32;
7-
83
int main()
94
{
105
return 0;
11-
}
6+
}

0 commit comments

Comments
 (0)