C/C++ API improvements, CMake include dir change

C API: add format functions
C++ API: add ABI stable wrapper RagePhotoA based on C API
This commit is contained in:
Syping 2021-11-04 13:21:18 +01:00
parent d8c99b9067
commit 67ed433f6b
6 changed files with 198 additions and 2 deletions

View file

@ -75,6 +75,9 @@ endif()
if (WITH_C_API) if (WITH_C_API)
set(LIBRAGEPHOTO_API LIBRAGEPHOTO_C_API) set(LIBRAGEPHOTO_API LIBRAGEPHOTO_C_API)
list(APPEND RAGEPHOTO_HEADERS
src/RagePhotoA.h
)
else() else()
set(LIBRAGEPHOTO_API LIBRAGEPHOTO_C_NOAPI) set(LIBRAGEPHOTO_API LIBRAGEPHOTO_C_NOAPI)
endif() endif()
@ -90,6 +93,7 @@ target_compile_definitions(ragephoto PRIVATE
) )
target_include_directories(ragephoto PUBLIC target_include_directories(ragephoto PUBLIC
${PROJECT_BINARY_DIR}/include ${PROJECT_BINARY_DIR}/include
${PROJECT_SOURCE_DIR}/src
) )
install(TARGETS ragephoto DESTINATION ${CMAKE_INSTALL_LIBDIR}) install(TARGETS ragephoto DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(FILES ${RAGEPHOTO_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/RagePhoto) install(FILES ${RAGEPHOTO_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/RagePhoto)

View file

@ -32,7 +32,6 @@ set(GTKVIEWER_SOURCES
) )
if (TARGET ragephoto) if (TARGET ragephoto)
set(RAGEPHOTO_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src)
set(RAGEPHOTO_LIBRARIES ragephoto) set(RAGEPHOTO_LIBRARIES ragephoto)
else() else()
pkg_check_modules(RAGEPHOTO REQUIRED ragephoto) pkg_check_modules(RAGEPHOTO REQUIRED ragephoto)

View file

@ -37,7 +37,6 @@ set(QTVIEWER_SOURCES
) )
if (TARGET ragephoto) if (TARGET ragephoto)
set(RAGEPHOTO_INCLUDE_DIRS ${CMAKE_SOURCE_DIR}/src)
set(RAGEPHOTO_LIBRARIES ragephoto) set(RAGEPHOTO_LIBRARIES ragephoto)
else() else()
find_package(PkgConfig REQUIRED) find_package(PkgConfig REQUIRED)

View file

@ -1001,18 +1001,36 @@ size_t ragephoto_getsavesize(ragephoto_t instance)
return ragePhoto->saveSize(); return ragePhoto->saveSize();
} }
size_t ragephoto_getsavesizef(ragephoto_t instance, uint32_t photoFormat)
{
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return ragePhoto->saveSize(photoFormat);
}
int ragephoto_save(ragephoto_t instance, char *data) int ragephoto_save(ragephoto_t instance, char *data)
{ {
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance); RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return ragePhoto->save(data); return ragePhoto->save(data);
} }
int ragephoto_savef(ragephoto_t instance, char *data, uint32_t photoFormat)
{
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return ragePhoto->save(data, photoFormat);
}
int ragephoto_savefile(ragephoto_t instance, const char *filename) int ragephoto_savefile(ragephoto_t instance, const char *filename)
{ {
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance); RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return ragePhoto->saveFile(filename); return ragePhoto->saveFile(filename);
} }
int ragephoto_savefilef(ragephoto_t instance, const char *filename, uint32_t photoFormat)
{
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);
return ragePhoto->saveFile(filename, photoFormat);
}
void ragephoto_setbufferdefault(ragephoto_t instance) void ragephoto_setbufferdefault(ragephoto_t instance)
{ {
RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance); RagePhoto *ragePhoto = static_cast<RagePhoto*>(instance);

View file

@ -30,6 +30,9 @@
typedef std::function<bool(const char*, size_t, RagePhotoData*)> RagePhotoLoadFunc; typedef std::function<bool(const char*, size_t, RagePhotoData*)> RagePhotoLoadFunc;
/**
* \brief GTA V and RDR 2 Photo Parser.
*/
class LIBRAGEPHOTO_EXPORT RagePhoto class LIBRAGEPHOTO_EXPORT RagePhoto
{ {
public: public:
@ -260,18 +263,38 @@ LIBRAGEPHOTO_EXPORT const char* ragephoto_getphototitle(ragephoto_t instance);
*/ */
LIBRAGEPHOTO_EXPORT size_t ragephoto_getsavesize(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*. /** Saves a Photo to a char*.
* \param instance \p ragephoto_t instance * \param instance \p ragephoto_t instance
* \param data Photo data * \param data Photo data
*/ */
LIBRAGEPHOTO_EXPORT int ragephoto_save(ragephoto_t instance, char *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. /** Saves a Photo to a file.
* \param instance \p ragephoto_t instance * \param instance \p ragephoto_t instance
* \param filename File to save * \param filename File to save
*/ */
LIBRAGEPHOTO_EXPORT int ragephoto_savefile(ragephoto_t instance, const char *filename); 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. /** Sets all cross-format Buffer to default size.
* \param instance \p ragephoto_t instance * \param instance \p ragephoto_t instance
*/ */

153
src/RagePhotoA.h Normal file
View file

@ -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<char*>(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<uint32_t>(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