From 67ed433f6b2050ccf0f691cd789a1e30459a0c43 Mon Sep 17 00:00:00 2001 From: Syping Date: Thu, 4 Nov 2021 13:21:18 +0100 Subject: [PATCH] C/C++ API improvements, CMake include dir change C API: add format functions C++ API: add ABI stable wrapper RagePhotoA based on C API --- CMakeLists.txt | 4 + examples/ragephoto-gtkviewer/CMakeLists.txt | 1 - examples/ragephoto-qtviewer/CMakeLists.txt | 1 - src/RagePhoto.cpp | 18 +++ src/RagePhoto.h | 23 +++ src/RagePhotoA.h | 153 ++++++++++++++++++++ 6 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 src/RagePhotoA.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 860a0e8..6d96a3a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,6 +75,9 @@ endif() if (WITH_C_API) set(LIBRAGEPHOTO_API LIBRAGEPHOTO_C_API) + list(APPEND RAGEPHOTO_HEADERS + src/RagePhotoA.h + ) else() set(LIBRAGEPHOTO_API LIBRAGEPHOTO_C_NOAPI) endif() @@ -90,6 +93,7 @@ target_compile_definitions(ragephoto PRIVATE ) target_include_directories(ragephoto PUBLIC ${PROJECT_BINARY_DIR}/include + ${PROJECT_SOURCE_DIR}/src ) install(TARGETS ragephoto DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(FILES ${RAGEPHOTO_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/RagePhoto) diff --git a/examples/ragephoto-gtkviewer/CMakeLists.txt b/examples/ragephoto-gtkviewer/CMakeLists.txt index ab79430..8a817fc 100644 --- a/examples/ragephoto-gtkviewer/CMakeLists.txt +++ b/examples/ragephoto-gtkviewer/CMakeLists.txt @@ -32,7 +32,6 @@ set(GTKVIEWER_SOURCES ) if (TARGET ragephoto) - set(RAGEPHOTO_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src) set(RAGEPHOTO_LIBRARIES ragephoto) else() pkg_check_modules(RAGEPHOTO REQUIRED ragephoto) diff --git a/examples/ragephoto-qtviewer/CMakeLists.txt b/examples/ragephoto-qtviewer/CMakeLists.txt index 16dbb9b..ca3f350 100644 --- a/examples/ragephoto-qtviewer/CMakeLists.txt +++ b/examples/ragephoto-qtviewer/CMakeLists.txt @@ -37,7 +37,6 @@ set(QTVIEWER_SOURCES ) if (TARGET ragephoto) - set(RAGEPHOTO_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src) set(RAGEPHOTO_LIBRARIES ragephoto) else() find_package(PkgConfig REQUIRED) diff --git a/src/RagePhoto.cpp b/src/RagePhoto.cpp index 0e05d5a..9dc5e57 100644 --- a/src/RagePhoto.cpp +++ b/src/RagePhoto.cpp @@ -1001,18 +1001,36 @@ size_t ragephoto_getsavesize(ragephoto_t instance) return ragePhoto->saveSize(); } +size_t ragephoto_getsavesizef(ragephoto_t instance, uint32_t photoFormat) +{ + RagePhoto *ragePhoto = static_cast(instance); + return ragePhoto->saveSize(photoFormat); +} + int ragephoto_save(ragephoto_t instance, char *data) { RagePhoto *ragePhoto = static_cast(instance); return ragePhoto->save(data); } +int ragephoto_savef(ragephoto_t instance, char *data, uint32_t photoFormat) +{ + RagePhoto *ragePhoto = static_cast(instance); + return ragePhoto->save(data, photoFormat); +} + int ragephoto_savefile(ragephoto_t instance, const char *filename) { RagePhoto *ragePhoto = static_cast(instance); return ragePhoto->saveFile(filename); } +int ragephoto_savefilef(ragephoto_t instance, const char *filename, uint32_t photoFormat) +{ + RagePhoto *ragePhoto = static_cast(instance); + return ragePhoto->saveFile(filename, photoFormat); +} + void ragephoto_setbufferdefault(ragephoto_t instance) { RagePhoto *ragePhoto = static_cast(instance); diff --git a/src/RagePhoto.h b/src/RagePhoto.h index c56f8ba..1596d2d 100644 --- a/src/RagePhoto.h +++ b/src/RagePhoto.h @@ -30,6 +30,9 @@ typedef std::function RagePhotoLoadFunc; +/** +* \brief GTA V and RDR 2 Photo Parser. +*/ class LIBRAGEPHOTO_EXPORT RagePhoto { public: @@ -260,18 +263,38 @@ LIBRAGEPHOTO_EXPORT const char* ragephoto_getphototitle(ragephoto_t instance); */ LIBRAGEPHOTO_EXPORT size_t ragephoto_getsavesize(ragephoto_t instance); +/** Returns the Photo save file size. +* \param instance \p ragephoto_t instance +* \param photoFormat Photo Format (GTA V or RDR 2) +*/ +LIBRAGEPHOTO_EXPORT size_t ragephoto_getsavesizef(ragephoto_t instance, uint32_t photoFormat); + /** Saves a Photo to a char*. * \param instance \p ragephoto_t instance * \param data Photo data */ LIBRAGEPHOTO_EXPORT int ragephoto_save(ragephoto_t instance, char *data); +/** Saves a Photo to a char*. +* \param instance \p ragephoto_t instance +* \param data Photo data +* \param photoFormat Photo Format (GTA V or RDR 2) +*/ +LIBRAGEPHOTO_EXPORT int ragephoto_savef(ragephoto_t instance, char *data, uint32_t photoFormat); + /** Saves a Photo to a file. * \param instance \p ragephoto_t instance * \param filename File to save */ LIBRAGEPHOTO_EXPORT int ragephoto_savefile(ragephoto_t instance, const char *filename); +/** Saves a Photo to a file. +* \param instance \p ragephoto_t instance +* \param filename File to save +* \param photoFormat Photo Format (GTA V or RDR 2) +*/ +LIBRAGEPHOTO_EXPORT int ragephoto_savefilef(ragephoto_t instance, const char *filename, uint32_t photoFormat); + /** Sets all cross-format Buffer to default size. * \param instance \p ragephoto_t instance */ diff --git a/src/RagePhotoA.h b/src/RagePhotoA.h new file mode 100644 index 0000000..edd19aa --- /dev/null +++ b/src/RagePhotoA.h @@ -0,0 +1,153 @@ +/***************************************************************************** +* 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. +*****************************************************************************/ + +#ifndef RAGEPHOTOA_H +#define RAGEPHOTOA_H + +#ifdef __cplusplus +#include "RagePhoto.h" + +/** +* \brief ABI Stable Wrapper for RagePhoto. +* +* Using RagePhotoA instead of RagePhoto allows your library or application to survive more changes in the RagePhoto class, +* disadvantages include worse performance, which should be pretty minimal, and not always include the newest features. +*/ +class RagePhotoA +{ +public: + RagePhotoA() { + instance = ragephoto_open(); + } + ~RagePhotoA() { + ragephoto_close(instance); + } + bool load(const char *data, size_t size) { + return ragephoto_load(instance, data, size); + } + bool load(const std::string &data) { + return ragephoto_load(instance, data.data(), data.size()); + } + bool loadFile(const char *filename) { + return ragephoto_loadfile(instance, filename); + } + uint32_t error() const { + return ragephoto_error(instance); + } + uint32_t format() const { + return ragephoto_getphotoformat(instance); + } + const std::string photo() const { + return std::string(ragephoto_getphotojpeg(instance), ragephoto_getphotosize(instance)); + } + const char *photoData() const { + return ragephoto_getphotojpeg(instance); + } + uint32_t photoSize() const { + return ragephoto_getphotosize(instance); + } + const char* description() const { + return ragephoto_getphotodesc(instance); + } + const char* json() const { + return ragephoto_getphotojson(instance); + } + const char* header() const { + return ragephoto_getphotoheader(instance); + } + const char* title() const { + return ragephoto_getphototitle(instance); + } + static const char* version() { + return ragephoto_version(); + } + bool save(char *data, uint32_t photoFormat) { + return ragephoto_savef(instance, data, photoFormat); + } + bool save(char *data) { + return ragephoto_save(instance, data); + } + const std::string save(uint32_t photoFormat, bool *ok = nullptr) { + const size_t size = ragephoto_getsavesizef(instance, photoFormat); + if (size == 0) { + if (ok) + *ok = false; + return std::string(); + } + char *data = static_cast(malloc(size)); + if (!data) { + if (ok) + *ok = false; + return std::string(); + } + const bool saved = ragephoto_savef(instance, data, photoFormat); + if (ok) + *ok = saved; + const std::string sdata = std::string(data, size); + free(data); + return sdata; + } + const std::string save(bool *ok = nullptr) { + return save(ragephoto_getphotoformat(instance), ok); + } + bool saveFile(const char *filename, uint32_t photoFormat) { + return ragephoto_savefilef(instance, filename, photoFormat); + } + bool saveFile(const char *filename) { + return ragephoto_savefile(instance, filename); + } + size_t saveSize(uint32_t photoFormat) { + return ragephoto_getsavesizef(instance, photoFormat); + } + size_t saveSize() { + return ragephoto_getsavesize(instance); + } + void setBufferDefault() { + ragephoto_setbufferdefault(instance); + } + void setBufferOffsets() { + ragephoto_setbufferoffsets(instance); + } + void setDescription(const char *description, uint32_t bufferSize = 0) { + ragephoto_setphotodesc(instance, description, bufferSize); + } + void setFormat(uint32_t photoFormat) { + ragephoto_setphotoformat(instance, photoFormat); + } + void setJson(const char *json, uint32_t bufferSize = 0) { + ragephoto_setphotojson(instance, json, bufferSize); + } + void setHeader(const char *header, uint32_t headerSum) { + ragephoto_setphotoheader(instance, header, headerSum); + } + bool setPhoto(const char *data, uint32_t size, uint32_t bufferSize = 0) { + return ragephoto_setphotojpeg(instance, data, size, bufferSize); + } + bool setPhoto(const std::string &data, uint32_t bufferSize = 0) { + return ragephoto_setphotojpeg(instance, data.data(), static_cast(data.size()), bufferSize); + } + void setTitle(const char *title, uint32_t bufferSize = 0) { + ragephoto_setphototitle(instance, title, bufferSize); + } + +private: + ragephoto_t instance; +}; +#endif + +#endif // RAGEPHOTOA_H