From d8c99b906737584a7ece4445f388c07b3ce5ce94 Mon Sep 17 00:00:00 2001 From: Syping Date: Wed, 3 Nov 2021 11:21:37 +0100 Subject: [PATCH] RagePhoto-Extract: use C API when enabled --- CMakeLists.txt | 9 ++++-- src/RagePhoto-Extract.c | 68 +++++++++++++++++++++++++++++++++++++++ src/RagePhoto-Extract.cpp | 1 - 3 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 src/RagePhoto-Extract.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 86c10da..860a0e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -116,9 +116,12 @@ else() option(WITH_EXTRACT "Build libragephoto with ragephoto-extract" OFF) endif() if (WITH_EXTRACT) - set(EXTRACT_SOURCES - src/RagePhoto-Extract.cpp - ) + if (WITH_C_API) + enable_language(C) + set(EXTRACT_SOURCES src/RagePhoto-Extract.c) + else() + set(EXTRACT_SOURCES src/RagePhoto-Extract.cpp) + endif() add_executable(ragephoto-extract ${RAGEPHOTO_HEADERS} ${EXTRACT_SOURCES}) target_link_libraries(ragephoto-extract PRIVATE ragephoto) install(TARGETS ragephoto-extract DESTINATION ${CMAKE_INSTALL_BINDIR}) diff --git a/src/RagePhoto-Extract.c b/src/RagePhoto-Extract.c new file mode 100644 index 0000000..84cc2c1 --- /dev/null +++ b/src/RagePhoto-Extract.c @@ -0,0 +1,68 @@ +/***************************************************************************** +* libragephoto RAGE Photo Parser +* Copyright (C) 2021 Syping +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* +* 1. Redistributions of source code must retain the above copyright notice, +* this list of conditions and the following disclaimer. +* +* 2. Redistributions in binary form must reproduce the above copyright notice, +* this list of conditions and the following disclaimer in the documentation +* and/or other materials provided with the distribution. +* +* This software is provided as-is, no warranties are given to you, we are not +* responsible for anything with use of the software, you are self responsible. +*****************************************************************************/ + +#include "RagePhoto.h" +#include +#include + +int main(int argc, char *argv[]) +{ + if (argc != 3) { + printf("Usage: %s [photo] [jpegout]\n", argv[0]); + return 0; + } + + ragephoto_t ragephoto_in = ragephoto_open(); + + // Load Photo + const int loaded = ragephoto_loadfile(ragephoto_in, argv[1]); + + if (loaded != 1) { + if (ragephoto_error(ragephoto_in) == 0) { + printf("Failed to open file: %s\n", argv[1]); + return 1; + } + else if (ragephoto_getphotosize(ragephoto_in) <= 0) { + printf("Failed to load photo\n"); + return 1; + } + } + + // Write jpeg + FILE *file = fopen(argv[2], "wb"); + if (!file) { + printf("Failed to write file: %s\n", argv[2]); + return 1; + } + const size_t size = fwrite(ragephoto_getphotojpeg(ragephoto_in), sizeof(char), ragephoto_getphotosize(ragephoto_in), file); + fclose(file); + + if (size != ragephoto_getphotosize(ragephoto_in)) { + printf("Failed to write file: %s\n", argv[2]); + return 1; + } + + if (ragephoto_getphotoformat(ragephoto_in) == ragephoto_format_gta5()) + printf("GTA V Photo successfully exported\n"); + else + printf("RDR 2 Photo successfully exported\n"); + + ragephoto_close(ragephoto_in); + + return 0; +} diff --git a/src/RagePhoto-Extract.cpp b/src/RagePhoto-Extract.cpp index 922080b..43c0286 100644 --- a/src/RagePhoto-Extract.cpp +++ b/src/RagePhoto-Extract.cpp @@ -19,7 +19,6 @@ #include "RagePhoto.h" #include #include -#include int main(int argc, char *argv[]) {