Skip to content

Commit

Permalink
Initial commit; Cmd arguments parsing; Palette colors parsing;
Browse files Browse the repository at this point in the history
Test pictures added;
  • Loading branch information
Danleb committed Feb 2, 2022
0 parents commit 8cfc36c
Show file tree
Hide file tree
Showing 28 changed files with 465 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
BasedOnStyle: Mozilla
IndentWidth: 4

Language: Cpp
ColumnLimit: 100
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AccessModifierOffset: -4
BreakBeforeBraces: Allman
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
###############################
# Core EditorConfig Options #
###############################
# You can learn more about editorconfig here: https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference
root = true
# All files
[*]

# Code files
[*.{cpp,h}]
indent_size = 4
insert_final_newline = true
charset = utf-8-bom
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
out/
build/
.vscode/
.vs/
TestResults/
obj/
bin/
.gradle/
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.15)
project(Dithering VERSION 1.0.0)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)

include (utils.cmake)
InstallAndSetupConanPackages()

add_subdirectory(src)

enable_testing()
27 changes: 27 additions & 0 deletions CMakeSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"configurations": [
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": ""
},
{
"name": "x64-Release",
"generator": "Ninja",
"configurationType": "RelWithDebInfo",
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": "",
"inheritEnvironments": [ "msvc_x64_x64" ],
"variables": []
}
]
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Danylo Lebediev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Dithering

Dithering algorithm implementation.

## How to use

This program works as a console utility:

```cmd
./dithering_app MyColors.json MyPicture.png
```

## License

This program and sources are distributed under the MIT licence.

Media resources used in this project are distributed under their respective license.

Unsplash license:
<https://unsplash.com/license>
10 changes: 10 additions & 0 deletions conanfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[requires]
glm/0.9.9.8
spdlog/1.9.2
benchmark/1.6.0
stb/cci.20210713
gtest/cci.20210126
rapidjson/cci.20211112

[generators]
cmake
6 changes: 6 additions & 0 deletions configs/BlackWhite.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"palette": [
"000000",
"ffffff"
]
}
13 changes: 13 additions & 0 deletions launch.vs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "0.2.1",
"defaults": {},
"configurations": [
{
"type": "default",
"project": "CMakeLists.txt",
"projectTarget": "",
"name": "Make BlackWhite Cat",
"args": [ "../../../../configs/BlackWhite.json", "../../../../pics/1.jpg" ]
}
]
}
Binary file added pics/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pics/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pics/3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pics/4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pics/5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pics/6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pics/7.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions pics/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Unsplash license:
https://unsplash.com/license
16 changes: 16 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
add_executable(dithering_app "")

file (GLOB COLLECTED_SOURCES "*.cpp")
target_sources(dithering_app PRIVATE
${COLLECTED_SOURCES}
)

target_include_directories(dithering_app PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)

#target_link_libraries(dithering_app
# dithering
#)

CONAN_TARGET_LINK_LIBRARIES(dithering_app)
55 changes: 55 additions & 0 deletions src/DitheringConfiguration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "DitheringConfiguration.h"

#include <iomanip>
#include <sstream>

namespace
{

const auto PaletteObjName = "palette";

inline float toFloat(uint32_t integerColorChannel)
{
return static_cast<float>(integerColorChannel) / 255.0f;
}

// Expects 3-component color without alpha-channel
effects::Color toColor(uint32_t v)
{
const uint32_t rv = (v >> 16) & 0xFF;
const uint32_t gv = (v >> 8) & 0xFF;
const uint32_t bv = v & 0xFF;

const float r = toFloat(rv);
const float g = toFloat(gv);
const float b = toFloat(bv);
return { r, g, b, 1.0f };
}

} // namespace

namespace effects
{
DitheringConfiguration::DitheringConfiguration(const rapidjson::Document& configuration)
{
if (configuration.HasMember(PaletteObjName))
{
const auto colors = configuration[PaletteObjName].GetArray();
m_palette.reserve(colors.Size());
for (const auto& v : colors)
{
const auto colorCodeStr = v.GetString();
const auto length = v.GetStringLength();
const auto index =
(length > 0 && colorCodeStr[0] == '#') ? colorCodeStr + 1 : colorCodeStr;

uint32_t colorCode;
std::stringstream ss;
ss << std::hex << colorCodeStr;
ss >> colorCode;
const auto color = toColor(colorCode);
m_palette.push_back(color);
}
}
}
}
20 changes: 20 additions & 0 deletions src/DitheringConfiguration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <glm/glm.hpp>
#include <rapidjson/document.h>

#include <vector>

namespace effects
{
using Color = glm::vec4;

class DitheringConfiguration
{
public:
DitheringConfiguration(const rapidjson::Document& configuration);

private:
std::vector<Color> m_palette;
};
}
17 changes: 17 additions & 0 deletions src/FloydSteinbergDithering.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "FloydSteinbergDithering.h"

#include "Image.h"

namespace effects
{
FloydSteinbergDithering::FloydSteinbergDithering(DitheringConfiguration config)
: m_config{ config }
{
}

void FloydSteinbergDithering::dither(Image& image)
{
//
}

}
18 changes: 18 additions & 0 deletions src/FloydSteinbergDithering.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "DitheringConfiguration.h"
#include "Image.h"

namespace effects
{
class FloydSteinbergDithering
{
public:
FloydSteinbergDithering(DitheringConfiguration config);

void dither(Image& image);

private:
DitheringConfiguration m_config;
};
}
78 changes: 78 additions & 0 deletions src/Image.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "Image.h"

#if _WIN32
#define STBIW_WINDOWS_UTF8
#endif

#include <stb_image.h>
#include <stb_image_write.h>

#include <assert.h>

namespace effects
{
constexpr auto TARGET_CHANNELS_COUNT = 4;

Image::Image(const std::filesystem::path& path)
: m_width{ 0 }
, m_height{ 0 }
, m_channels{ 0 }
, m_data{ nullptr }
{
const auto buffer = convertPath(path);
if (!std::filesystem::exists(path))
{
return;
}
m_data = stbi_load(buffer.get(), &m_width, &m_height, &m_channels, TARGET_CHANNELS_COUNT);
}

Image::~Image()
{
if (m_data)
{
stbi_image_free(m_data);
m_data = nullptr;
}
}

int Image::width() const
{
return m_width;
}

int Image::height() const
{
return m_height;
}

ImageData Image::data()
{
return m_data;
}

ImageData Image::data() const
{
return m_data;
}

void Image::save(const std::filesystem::path& path) const
{
const auto buffer = convertPath(path);
stbi_write_png(buffer.get(),
m_width,
m_height,
TARGET_CHANNELS_COUNT,
m_data,
m_width * TARGET_CHANNELS_COUNT);
}

std::shared_ptr<char[]> Image::convertPath(const std::filesystem::path& path) const
{
constexpr auto BUFFER_SIZE = 4096;
std::shared_ptr<char[]> buffer{ new char[BUFFER_SIZE] };
stbiw_convert_wchar_to_utf8(buffer.get(), BUFFER_SIZE, path.c_str());
return buffer;
}

}
31 changes: 31 additions & 0 deletions src/Image.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <filesystem>
#include <memory>

namespace effects
{
using ImageData = unsigned char*;

class Image
{
public:
Image(const std::filesystem::path& path);
~Image();

int width() const;
int height() const;
ImageData data();
ImageData data() const;

void save(const std::filesystem::path& path) const;

private:
std::shared_ptr<char[]> convertPath(const std::filesystem::path& path) const;

int m_width;
int m_height;
int m_channels;
ImageData m_data;
};
}
Loading

0 comments on commit 8cfc36c

Please sign in to comment.