Skip to content

Commit d4db7f4

Browse files
authoredMay 5, 2023
Reorganize file structure (#10)
* Reorganize file structure * format files
1 parent 5114e9a commit d4db7f4

File tree

6 files changed

+81
-129
lines changed

6 files changed

+81
-129
lines changed
 

‎CMakeLists.txt

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ set(CMAKE_CXX_EXTENSIONS OFF)
77
project(image-exporter)
88

99
add_executable(${PROJECT_NAME})
10+
target_include_directories(${PROJECT_NAME} PUBLIC ./include)
1011
target_sources(${PROJECT_NAME} PRIVATE
11-
src/export.h
12-
src/export.cpp
13-
src/main.cpp
12+
include/image_upscaler/image_upscaler.hpp
13+
src/image_upscaler.cpp
14+
tests/main.cpp
1415
)
1516

1617
if(CMAKE_CXX_COMPILER_ID STREQUAL Clang OR CMAKE_CXX_COMPILER_ID STREQUAL GNU)
1718
target_compile_options(${PROJECT_NAME} PRIVATE
18-
-Wall -Wextra -Wpedantic -Wconversion -Werror=return-type
19+
-Wall -Wextra -Wpedantic -Wconversion -Werror=return-type
1920
)
2021
endif()
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
11
#pragma once
22

3-
#include <vector>
43
#include <cstdint>
54
#include <string>
5+
#include <vector>
66

7-
using std::uint8_t;
87
using std::uint32_t;
8+
using std::uint8_t;
99

10-
struct Rgba
11-
{
10+
struct Rgba {
1211
Rgba(uint32_t hex);
1312
Rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
1413
uint8_t r{0}, g{0}, b{0}, a{0};
15-
static constexpr uint32_t to_hex(const Rgba &rgba);
14+
static constexpr uint32_t to_hex(Rgba const& rgba);
1615
};
1716

18-
struct Index2D
19-
{
17+
struct Index2D {
2018
uint32_t x{0}, y{0};
2119
};
2220

23-
struct Extent
24-
{
21+
struct Extent {
2522
uint32_t width{0}, height{0};
2623
};
2724

28-
struct Image
29-
{
25+
struct Image {
3026
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-
27+
static void export_to_ppm(Image const& input, std::string const& path);
28+
Rgba& operator[](Index2D index);
29+
Rgba const& operator[](Index2D index) const;
30+
3531
Extent extent;
3632
std::vector<Rgba> pixels;
3733
};
3834

39-
Image upscale(const Image &input, uint32_t scale);
35+
Image upscale(Image const& input, uint32_t scale);

‎src/export.cpp

-103
This file was deleted.

‎src/image_upscaler.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <cstdlib>
2+
#include <fstream>
3+
#include <iostream>
4+
#include <string>
5+
6+
#include <image_upscaler/image_upscaler.hpp>
7+
8+
/* Implementation of Rgba */
9+
Rgba::Rgba(uint32_t hex) : r((hex & 0xFF'00'00'00) >> 24), g((hex & 0x00'FF'00'00) >> 16), b((hex & 0x00'00'FF'00) >> 8), a(hex & 0x00'00'00'FF) {}
10+
11+
Rgba::Rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a) : r(r), g(g), b(b), a(a) {}
12+
13+
constexpr uint32_t Rgba::to_hex(Rgba const& rgba) { return ((rgba.r << 24) | (rgba.g << 16) | (rgba.b << 8) | (rgba.a)); }
14+
15+
/* --- End --- */
16+
17+
/* Implementation of Image */
18+
Image::Image(uint32_t width, uint32_t height) : extent{width, height}, pixels(width * height, Rgba(0)) {}
19+
20+
void Image::export_to_ppm(Image const& input, std::string const& path) {
21+
std::ofstream output(path);
22+
output << "P3\n";
23+
output << "# " << path << '\n';
24+
output << input.extent.width << " " << input.extent.height << '\n';
25+
output << "255\n";
26+
27+
for (uint32_t y = 0; y < input.extent.height; ++y) {
28+
for (uint32_t x = 0; x < input.extent.width; ++x) {
29+
Rgba current = input.pixels.at(y * input.extent.width + x);
30+
output << static_cast<uint32_t>(current.r) << '\n' << static_cast<uint32_t>(current.g) << '\n' << static_cast<uint32_t>(current.b) << '\n';
31+
}
32+
}
33+
34+
output.close();
35+
}
36+
37+
Rgba& Image::operator[](Index2D index) { return this->pixels.at(index.y * this->extent.width + index.x); }
38+
39+
Rgba const& Image::operator[](Index2D index) const { return this->pixels.at(index.y * this->extent.width + index.x); }
40+
41+
/* --- End --- */
42+
43+
Image upscale(Image const& input, uint32_t scale) {
44+
uint32_t target_width_v = input.extent.width * scale;
45+
uint32_t target_height_v = input.extent.height * scale;
46+
uint32_t x_index{0}, y_index{0};
47+
Image target_image(target_width_v, target_height_v);
48+
49+
for (y_index = 0; y_index < input.extent.height; ++y_index) {
50+
for (x_index = 0; x_index < input.extent.width; ++x_index) {
51+
Rgba current = input[Index2D{x_index, y_index}];
52+
if (Rgba::to_hex(current)) {
53+
for (uint32_t i = y_index * scale; i < ((y_index + 1) * scale); ++i) {
54+
for (uint32_t j = x_index * scale; j < ((x_index + 1) * scale); ++j) { target_image[Index2D{j, i}] = current; }
55+
}
56+
}
57+
}
58+
}
59+
60+
return target_image;
61+
}

‎src/main.cpp

-6
This file was deleted.

‎tests/main.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <image_upscaler/image_upscaler.hpp>
2+
3+
int main() { return 0; }

0 commit comments

Comments
 (0)
Please sign in to comment.