1
+ #include < cstdlib>
2
+ #include < iostream>
3
+ #include < string>
4
+ #include < fstream>
5
+
1
6
#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
+ }
0 commit comments