diff --git a/src/ascii.cpp b/src/ascii.cpp index 36becbc..eb55c11 100644 --- a/src/ascii.cpp +++ b/src/ascii.cpp @@ -6,7 +6,43 @@ #include "convert.h" -void lily_png::convert_to_ascii(image &src, image &dest) +void lily_png::convert_to_ascii(image &src, file_reader::buffer &dest) { + image intermediate_img(src.meta); + intermediate_img.meta.width = 40; + intermediate_img.meta.height = 40; + auto ret = src.resize_image(intermediate_img); + if (!ret) + { + std::println("Error! {}", ret.error()); + return ; + } + size_t resized_size = ret.value(); + size_t reference_size = strlen("\033[38;2;255;255;255ma\033[0m "); + dest.allocate(resized_size + (resized_size * intermediate_img.meta.width * intermediate_img.meta.height)); + size_t dest_index = 0; + size_t byte_size = (intermediate_img.meta.bit_depth + 7)/8; + size_t lines = 0; + for (int y = 0; y < intermediate_img.meta.height; y++) + { + for (int x = 0; x < intermediate_img.meta.width; x++) + { + unsigned char *pxl = intermediate_img[y, x]; + color_rgb tmp{}; + tmp.r = pxl[0]; + tmp.g = pxl[byte_size]; + tmp.b = pxl[byte_size * 2]; + std::string format = std::format("\033[38;2;{};{};{}ma\033[0m ", tmp.r, tmp.g, tmp.b); + memcpy(&dest.data[dest_index], format.c_str(), format.size()); + dest_index += format.size(); + lines++; + if (lines == intermediate_img.meta.width) + { + dest.data[dest_index] = '\n'; + dest_index++; + lines = 0; + } + } + } } diff --git a/src/ascii.h b/src/ascii.h index 23a089c..e288a12 100644 --- a/src/ascii.h +++ b/src/ascii.h @@ -7,5 +7,5 @@ namespace lily_png { - void convert_to_ascii(image &src, image &dest); + void convert_to_ascii(image &src, file_reader::buffer &dest); } diff --git a/src/lily_png.cpp b/src/lily_png.cpp index 10e364d..a932c68 100644 --- a/src/lily_png.cpp +++ b/src/lily_png.cpp @@ -181,6 +181,7 @@ std::expected lily_png::read_png(const std::string &f { return std::unexpected(ret.error()); } + data.add_metadata(data.meta); if (palette_found == true) { file_reader::buffer dest_palette{}; diff --git a/src/lily_png.h b/src/lily_png.h index b40bc30..fea2418 100644 --- a/src/lily_png.h +++ b/src/lily_png.h @@ -15,24 +15,4 @@ namespace lily_png { std::expected apply_to_pixel(file_reader::buffer &src, metadata &meta, std::function func); std::expected read_png(const std::string &file_path, image &data); -} - -template <> -struct std::formatter -{ - - constexpr auto parse(std::format_parse_context& ctx) { - return ctx.begin(); - } - - auto format(const lily_png::png_error& id, std::format_context& ctx) const - { - if (id == lily_png::png_error::file_doesnt_exist) - return std::format_to(ctx.out(), "{}", "File doesn't exist"); - if (id == lily_png::png_error::read_failed) - return std::format_to(ctx.out(), "{}", "Read failed"); - if (id == lily_png::png_error::file_is_not_a_png) - return std::format_to(ctx.out(), "{}", "File is not a png"); - return std::format_to(ctx.out(), "{}", "Unkown error"); - } -}; \ No newline at end of file +} \ No newline at end of file diff --git a/src/utils.cpp b/src/utils.cpp index 9ecc236..bb09586 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -12,7 +12,7 @@ std::expected lily_png::get_pixel_bit_size(const me ret = meta.bit_depth; } else - throw std::runtime_error("Invalid bit depht"); + return std::unexpected(png_error::invalid_bit_depth); break; case static_cast(color::rgb): if (meta.bit_depth == 8 || meta.bit_depth == 16) @@ -90,7 +90,7 @@ float lily_png::get_aspect_ratio(const metadata &meta) std::expected lily_png::image::resize_image(image &dest) { - size_t new_size = (dest.meta.width * pixel_size_bytes) * dest.meta.height; + size_t new_size = (dest.meta.width * dest.pixel_size_bytes) * dest.meta.height; dest.buffer.allocate(new_size); //this implementation is bad but its only a test @@ -101,7 +101,7 @@ std::expected lily_png::image::resize_image(image & { for (int x = 0; x < dest.meta.width; x++) { - *(dest[y, x]) = *(operator[](y * compressed_pixels_height, x * compressed_pixels_width)); + memcpy(dest[y, x], operator[](y * compressed_pixels_height, x * compressed_pixels_width), pixel_size_bytes); } } return new_size; diff --git a/src/utils.h b/src/utils.h index e03331c..659ec53 100644 --- a/src/utils.h +++ b/src/utils.h @@ -31,9 +31,30 @@ namespace lily_png pixel_size = pixel_size_ret.value(); pixel_size_bytes = (pixel_size + 7)/8; } + + explicit image(metadata m) + :meta(m) + { + auto pixel_size_ret = get_pixel_bit_size(m).or_else([] (png_error error) + { + return std::expected(0); + }); + pixel_size = pixel_size_ret.value(); + pixel_size_bytes = (pixel_size + 7)/8; + } file_reader::buffer buffer{}; metadata meta{}; + void add_metadata(metadata m) + { + auto pixel_size_ret = get_pixel_bit_size(m).or_else([] (png_error error) + { + return std::expected(0); + }); + pixel_size = pixel_size_ret.value(); + pixel_size_bytes = (pixel_size + 7)/8; + this->meta = m; + } constexpr unsigned char *operator[](std::size_t y, std::size_t x) const { @@ -49,3 +70,29 @@ namespace lily_png size_t pixel_size_bytes; }; } + +template <> +struct std::formatter +{ + + constexpr auto parse(std::format_parse_context& ctx) { + return ctx.begin(); + } + + auto format(const lily_png::png_error& id, std::format_context& ctx) const + { + if (id == lily_png::png_error::file_doesnt_exist) + return std::format_to(ctx.out(), "{}", "File doesn't exist"); + if (id == lily_png::png_error::read_failed) + return std::format_to(ctx.out(), "{}", "Read failed"); + if (id == lily_png::png_error::file_is_not_a_png) + return std::format_to(ctx.out(), "{}", "File is not a png"); + if (id == lily_png::png_error::invalid_bit_depth) + return std::format_to(ctx.out(), "{}", "Invalid bit depth"); + if (id == lily_png::png_error::invalid_color_type) + return std::format_to(ctx.out(), "{}", "Invalid color type"); + if (id == lily_png::png_error::non_standard_filter) + return std::format_to(ctx.out(), "{}", "Non standard filter"); + return std::format_to(ctx.out(), "{}", "Unknown error"); + } +};