From 82ae29cb8a59a1e093190d14f8c5097658545a12 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Fri, 13 Feb 2026 22:03:01 +0100 Subject: =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0?= =?UTF-8?q?=20=D0=B2=D1=82=D0=BE=D1=80=D0=B0=D1=8F=20=D0=B7=D0=B0=D0=B4?= =?UTF-8?q?=D0=B0=D1=87=D0=B0=20=D0=BF=D1=8F=D1=82=D0=BE=D0=B9=20=D0=B3?= =?UTF-8?q?=D0=BB=D0=B0=D0=B2=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../5/2_grey/grey.cpp" | 91 ++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 "\320\227\320\260\320\275\320\270\320\274\320\260\321\202\320\265\320\273\321\214\320\275\320\276\320\265 \320\277\321\200\320\276\320\263\321\200\320\260\320\274\320\274\320\270\321\200\320\276\320\262\320\260\320\275\320\270\320\265/5/2_grey/grey.cpp" (limited to 'Занимательное программирование/5/2_grey/grey.cpp') diff --git "a/\320\227\320\260\320\275\320\270\320\274\320\260\321\202\320\265\320\273\321\214\320\275\320\276\320\265 \320\277\321\200\320\276\320\263\321\200\320\260\320\274\320\274\320\270\321\200\320\276\320\262\320\260\320\275\320\270\320\265/5/2_grey/grey.cpp" "b/\320\227\320\260\320\275\320\270\320\274\320\260\321\202\320\265\320\273\321\214\320\275\320\276\320\265 \320\277\321\200\320\276\320\263\321\200\320\260\320\274\320\274\320\270\321\200\320\276\320\262\320\260\320\275\320\270\320\265/5/2_grey/grey.cpp" new file mode 100644 index 0000000..8e4510a --- /dev/null +++ "b/\320\227\320\260\320\275\320\270\320\274\320\260\321\202\320\265\320\273\321\214\320\275\320\276\320\265 \320\277\321\200\320\276\320\263\321\200\320\260\320\274\320\274\320\270\321\200\320\276\320\262\320\260\320\275\320\270\320\265/5/2_grey/grey.cpp" @@ -0,0 +1,91 @@ +#include +#include "grey.hpp" +#include + +#define STB_IMAGE_IMPLEMENTATION +#include "stb_image.h" +#define STB_IMAGE_WRITE_IMPLEMENTATION +#include "stb_image_write.h" + +static void push_in_network_order(std::uint32_t input, std::vector& output) +{ + input = htonl(input); + auto pointer_range = reinterpret_cast(&input); + std::copy(pointer_range, pointer_range + 4, std::back_inserter(output)); +} + +static std::uint32_t pop_in_native_order(std::string::const_iterator& input) +{ + std::uint32_t in_native = ntohl(*reinterpret_cast(&*input)); + input += 4; + + return in_native; +} + +std::vector to_rle(const std::filesystem::path& input) +{ + int x, y, n; + unsigned char *data = stbi_load(input.native().data(), &x, &y, &n, 1); + + if (data == nullptr) + { + throw std::runtime_error(stbi_failure_reason()); + } + unsigned char *original_data = data; + std::uint8_t current_color = 255; + std::uint32_t current_count = 0; + std::vector rle_stream; + + push_in_network_order(x, rle_stream); + push_in_network_order(y, rle_stream); + push_in_network_order(1, rle_stream); + + for (std::size_t i = 0; i < x; ++i) + { + for (std::size_t j = 0; j < y; ++j) + { + if (current_color == *data) + { + ++current_count; + } + else + { + current_color = current_color == 255 ? 0 : 255; + push_in_network_order(current_count, rle_stream); + current_count = 1; + } + ++data; + } + } + stbi_image_free(original_data); + if (current_count != 0) + { + push_in_network_order(current_count, rle_stream); + } + return rle_stream; +} + +void from_rle(const std::string& input, const std::filesystem::path& output) +{ + std::vector buffer; + auto input_position = std::cbegin(input); + + int x = pop_in_native_order(input_position); + int y = pop_in_native_order(input_position); + int comp = pop_in_native_order(input_position); + + std::uint8_t current_color = 255; + std::uint32_t current_count = 0; + + while (input_position != std::cend(input)) + { + auto in_native = pop_in_native_order(input_position); + + for (; in_native > 0; --in_native) + { + buffer.push_back(current_color); + } + current_color = current_color == 255 ? 0 : 255; + } + stbi_write_jpg(output.native().data(), x, y, comp, buffer.data(), 100); +} -- cgit v1.2.3