Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re-creation of pixelator image #8

Merged
merged 7 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ _deps
# Build files and folders
**/build/
**/.cache/

.vscode
11 changes: 11 additions & 0 deletions homeworks/homework_5/pixelator/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

Language: Cpp
BasedOnStyle: Google
AllowShortBlocksOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
BinPackArguments: false
BinPackParameters: false
IndentWidth: 4
27 changes: 27 additions & 0 deletions homeworks/homework_5/pixelator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
cmake_minimum_required(VERSION 3.16..3.24)
project(pixelator VERSION 1
DESCRIPTION "Pixelate images in terminal"
LANGUAGES CXX)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "" FORCE)
endif()

message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")

# Create a pseudo-library to propagate the needed flags.
add_library(cxx_setup INTERFACE)
target_compile_options(cxx_setup INTERFACE -Wall -Wpedantic -Wextra)
target_compile_features(cxx_setup INTERFACE cxx_std_17)
target_include_directories(cxx_setup INTERFACE ${PROJECT_SOURCE_DIR})

# Update the submodules here
include(external/UpdateSubmodules.cmake)

# Enable testing for this project
include(CTest)

# Add code in subdirectories
add_subdirectory(external)
add_subdirectory(${PROJECT_NAME})
add_subdirectory(examples)
11 changes: 11 additions & 0 deletions homeworks/homework_5/pixelator/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
add_executable(use_ftxui use_ftxui.cpp)
target_link_libraries(use_ftxui PRIVATE cxx_setup ftxui::screen)

add_executable(use_stb_image use_stb_image.cpp)
target_link_libraries(use_stb_image PRIVATE cxx_setup stb::stb)

add_executable(pixelate pixelate.cpp)
target_link_libraries(pixelate PRIVATE pixelate_image stb::stb ftxui::screen)

# TODO: Add your binaries here
# There must be a binary `pixelate` here
35 changes: 35 additions & 0 deletions homeworks/homework_5/pixelator/examples/pixelate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "pixelator/drawer.hpp"
#include "pixelator/pixelate_image.hpp"
#include "pixelator/stb_image_data_view.hpp"

#include <cstddef>
#include <filesystem>
#include <iostream>
#include <utility>

namespace {
using pixelator::Drawer;
using pixelator::PixelateImage;
using pixelator::StbImageDataView;
} // namespace

int main(int argc, char **argv) {
if (argc < 2)
{
std::cerr << "No image provided." << std::endl;
return 0;
}

const StbImageDataView image{argv[1]};
if (image.empty()) {
std::cerr << "Image could not be loaded" << std::endl;
exit(1);
}

Drawer drawer{ftxui::Dimension::Fixed(35)};
drawer.Set(PixelateImage(image, drawer.size()));
drawer.Draw();


return 0;
}
21 changes: 21 additions & 0 deletions homeworks/homework_5/pixelator/examples/use_ftxui.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <ftxui/screen/color.hpp>
#include "ftxui/screen/screen.hpp"
#include <iostream>
namespace {
const ftxui::Color kYellowishColor = ftxui::Color::RGB(255, 200, 100);
}

int main() {
ftxui::Dimensions dimensions{ftxui::Dimension::Full()};
dimensions.dimy = dimensions.dimx *2;
std::cout << dimensions.dimx << " " << dimensions.dimy << std::endl;
ftxui::Screen screen{ftxui::Screen::Create(dimensions)};
auto &pixel_left = screen.PixelAt(10, 10);
pixel_left.background_color = kYellowishColor;
pixel_left.character = ' ';
auto &pixel_right = screen.PixelAt(10, 1);
pixel_right.background_color = kYellowishColor;
pixel_right.character = ' ';
screen.Print();
return 0;
}
64 changes: 64 additions & 0 deletions homeworks/homework_5/pixelator/examples/use_stb_image.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Make sure to have this in EXACTLY one cpp file
// The best place for this is the cpp file of your library
// that holds a class that wraps around the stb_image data
// For more see here: https://github.com/nothings/stb#faq
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"

#include <filesystem>
#include <iostream>

namespace {
static constexpr auto kLoadAllChannels{0};

// A dummy color structure. Use ftxui::Color in actual code.
struct Color {
int red;
int green;
int blue;
};

} // namespace

int main(int argc, char **argv) {
if (argc < 2) { std::cerr << "No image provided.\n"; }
const std::filesystem::path image_path{argv[1]};
if (!std::filesystem::exists(image_path)) {
std::cerr << "No image file: " << image_path << std::endl;
std::exit(1);
}

// Load the data
int rows{};
int cols{};
int channels{};
// This call also populates rows, cols, channels.
auto image_data{
stbi_load(image_path.c_str(), &cols, &rows, &channels, kLoadAllChannels)};
std::cout << "Loaded image of size: [" << rows << ", " << cols << "] with "
<< channels << " channels\n";
if (!image_data) {
std::cerr << "Failed to load image data from file: " << image_path
<< std::endl;
std::exit(1);
}

// The data is stored sequentially, in this order per pixel: red, green, blue,
// alpha This patterns repeats for every pixel of the image, so the resulting
// data layout is: [rgbargbargba...]
int query_row = 3;
int query_col = 2;
const auto index{channels * (query_row * cols + query_col)};
const Color color{
image_data[index], image_data[index + 1], image_data[index + 2]};
std::cout << "Color at pixel: [" << query_row << ", " << query_col
<< "] = RGB: (" << color.red << ", " << color.green << ", "
<< color.blue << ")\n";

// We must explicitly free the memory allocated for this image.
// The reason for this is that stb_image is a C library,
// which has no classes and no RAII in the form about which we talked before.
// Now you see why people want to write C++ and not C? ;)
stbi_image_free(image_data);
return 0;
}
3 changes: 3 additions & 0 deletions homeworks/homework_5/pixelator/external/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include(gtest.cmake)
include(ftxui.cmake)
include(stb.cmake)
27 changes: 27 additions & 0 deletions homeworks/homework_5/pixelator/external/UpdateSubmodules.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
find_package(Git QUIET)

if(GIT_FOUND)
option(UPDATE_SUBMODULES "Check submodules during build" ON)

if(NOT UPDATE_SUBMODULES)
return()
endif()

execute_process(COMMAND ${GIT_EXECUTABLE} submodule
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE EXISTING_SUBMODULES
RESULT_VARIABLE RETURN_CODE)

message(STATUS "Updating git submodules:\n${EXISTING_SUBMODULES}")

execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE RETURN_CODE)

if(NOT RETURN_CODE EQUAL "0")
message(WARNING "Cannot update submodules. Git command failed with ${RETURN_CODE}.")
return()
endif()

message(STATUS "Git submodules updated successfully.")
endif()
4 changes: 4 additions & 0 deletions homeworks/homework_5/pixelator/external/ftxui.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_subdirectory(ftxui)
4 changes: 4 additions & 0 deletions homeworks/homework_5/pixelator/external/ftxui/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Defines the Chromium style for automatic reformatting.
# http://clang.llvm.org/docs/ClangFormatStyleOptions.html
BasedOnStyle: Chromium
Standard: Cpp11
34 changes: 34 additions & 0 deletions homeworks/homework_5/pixelator/external/ftxui/.clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
Checks: "*,
-*-macro-usage,
-*-magic-numbers,
-*-narrowing-conversions
-*-unnecessary-value-param,
-*-uppercase-literal-suffix,
-abseil-*,
-altera-*,
-android-*,
-bugprone-easily-swappable-parameters,
-cppcoreguidelines-non-private-member-variables-in-classes,
-cppcoreguidelines-pro-type-union-access,
-fuchsia-*,
-google-*,
-hicpp-signed-bitwise,
-llvm*,
-misc-no-recursion,
-misc-non-private-member-variables-in-classes,
-modernize-use-nodiscard,
-modernize-use-trailing-return-type,
-readability-avoid-const-params-in-decls,
-readability-else-after-return,
-readability-identifier-length,
-readability-implicit-bool-conversion,
-readability-non-const-parameter,
-readability-simplify-boolean-expr,
-readability-static-accessed-through-instance,
-readability-use-anyofallof,
-zircon-*,
"
WarningsAsErrors: ''
HeaderFilterRegex: ''
FormatStyle: none
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: [ArthurSonzogni]
Loading
Loading