From aae0e4c97d558d071538997ac23599735d04c269 Mon Sep 17 00:00:00 2001 From: sillysagiri Date: Wed, 18 Sep 2024 17:01:45 +0700 Subject: [PATCH] first commit --- .gitignore | 2 ++ CMakeLists.txt | 33 ++++++++++++++++++ readme.txt | 10 ++++++ src/main.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 readme.txt create mode 100644 src/main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d73e005 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.cache +build \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..191906e --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 3.27) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +set(PROJECT_NAME sillyimage CACHE BOOL "") +set(PROJECT_VERSION 1.0) +project(${PROJECT_NAME} VERSION ${PROJECT_VERSION}) + +message(STATUS "find Magick++ library") +add_definitions(-DMAGICKCORE_QUANTUM_DEPTH=8) +add_definitions(-DMAGICKCORE_HDRI_ENABLE=0) +find_package(ImageMagick COMPONENTS Magick++) + +# yes... im using glob... dont judge me.... +file(GLOB_RECURSE PROJECT_SOURCES CONFIGURE_DEPENDS + "src/*.cpp" "src/*.c") + +set(PROJECT_INCLUDE + "src" + ${ImageMagick_INCLUDE_DIRS}) + +set(PROJECT_LIBRARY + ${ImageMagick_LIBRARIES}) + +set(PROJECT_DEFINITION + VERSION="${PROJECT_VERSION}") + +add_executable(${PROJECT_NAME} ${PROJECT_SOURCES}) +target_include_directories(${PROJECT_NAME} PRIVATE ${PROJECT_INCLUDE}) +target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_LIBRARY}) +target_compile_definitions(${PROJECT_NAME} PRIVATE ${PROJECT_DEFINITION}) +set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/dist") \ No newline at end of file diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..75700a7 --- /dev/null +++ b/readme.txt @@ -0,0 +1,10 @@ +i make this tool for my ds framebuffer rendering... + +since ptexconv (https://github.com/Garhoogin/ptexconv) automatically convert the image dimension into power of two... +i decided to make my own... +not sure if grit did the same, i hate grit lol... + +Usage: sillyimage + +first and second 4 byte is image width and height. +the rest is raw argb15, no fancy compression or anything, just a simple raw data... diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..5c8d166 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,91 @@ +#include +#include +#include + +// Macro to convert 8-bit RGB components into a single 15 bit RGB triplet. +#define RGBtoRGB15(r,g,b,a) (((a) ? 0x8000 : 0) | ((r >> 3) & 0x1F) | (((g >> 3) & 0x1F) << 5) | (((b >> 3) & 0x1F) << 10)) + +namespace fs = std::filesystem; + +void Print_Help(); + +int main(int argc, char* argv[]) +{ + if (argc <= 1 || argc > 3) { + Print_Help(); + return 1; + } + + fs::path str_input = argv[1]; + fs::path str_output = ""; + std::ifstream file_in; + std::ofstream file_out; + Magick::Image magick_img; + uint32_t size_width; + uint32_t size_height; + uint32_t *buffer_rgba; + uint16_t *buffer_argb15; + + if (argc == 3) str_output = argv[2]; + else + { + printf("\nNo explicit output path. Borrowing from input path\n"); + str_output = str_input.parent_path() / str_input.stem(); + str_output += ".bin"; + } + + file_in.open(str_input, std::ios::binary); + if (!file_in.good()) + { + printf("\nInput file doesnt exists!\n"); + return 1; + } + file_in.close(); + + magick_img.read(str_input); + size_width = magick_img.columns(); + size_height = magick_img.rows(); + + buffer_rgba = new uint32_t[size_width * size_height]; + buffer_argb15 = new uint16_t[size_width * size_height]; + + printf("\n"); + printf("input: %s\n", str_input.c_str()); + printf("output: %s\n", str_output.c_str()); + printf("dimension: %ux%u\n", size_width, size_height); + printf("\n"); + + magick_img.write(0, 0, size_width, size_height, "RGBA", Magick::CharPixel, buffer_rgba); + + for (int i=0, n=size_width*size_height; i> 24) & 0xFF; + uint8_t b = (rgba >> 16) & 0xFF; + uint8_t g = (rgba >> 8) & 0xFF; + uint8_t r = rgba & 0xFF; + + buffer_argb15[i] = RGBtoRGB15(r, g, b, a); + } + + file_out.open(str_output, std::ios::binary); + file_out.write(reinterpret_cast(&size_width), sizeof(uint32_t)); + file_out.write(reinterpret_cast(&size_height), sizeof(uint32_t)); + file_out.write(reinterpret_cast(buffer_argb15), size_width*size_height*sizeof(uint16_t)); + file_out.close(); + + printf("\ndone...\n"); + + // clean up + delete[] buffer_rgba; + delete[] buffer_argb15; + return 0; +} + +void Print_Help() +{ + printf("\n"); + printf("Simple image converter into raw argb15 color for DS %s\n\n", VERSION); + printf("Usage: sillyimage \n"); + printf("\n"); +} \ No newline at end of file