Skip to content

Commit

Permalink
Create HyperHDR utils-xz lib
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Jun 19, 2024
1 parent 7dbce97 commit 53e7106
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 85 deletions.
11 changes: 10 additions & 1 deletion cmake/installers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ macro(DeployApple TARGET)
install(CODE [[ file(INSTALL FILES $<TARGET_FILE:sqlite3> DESTINATION "${CMAKE_INSTALL_PREFIX}/hyperhdr.app/Contents/lib" TYPE SHARED_LIBRARY) ]] COMPONENT "HyperHDR")
endif()

# Copy utils-xz
if (USE_SHARED_LIBS AND TARGET utils-xz)
install(CODE [[ file(INSTALL FILES $<TARGET_FILE:utils-xz> DESTINATION "${CMAKE_INSTALL_PREFIX}/hyperhdr.app/Contents/lib" TYPE SHARED_LIBRARY) ]] COMPONENT "HyperHDR")
endif()

if ( Qt5Core_FOUND )
get_target_property(MYQT_QMAKE_EXECUTABLE ${Qt5Core_QMAKE_EXECUTABLE} IMPORTED_LOCATION)
else()
Expand Down Expand Up @@ -339,7 +344,6 @@ macro(DeployUnix TARGET)
"librt"
"libstdc++"
"libudev"
"libutil"
"libz"
"libxrender1"
"libxi6"
Expand Down Expand Up @@ -556,6 +560,11 @@ macro(DeployWindows TARGET)
install(CODE [[ file(INSTALL FILES $<TARGET_FILE:sqlite3> DESTINATION "${CMAKE_INSTALL_PREFIX}/bin" TYPE SHARED_LIBRARY) ]] COMPONENT "HyperHDR")
endif()

# Copy utils-xz
if (USE_SHARED_LIBS AND TARGET utils-xz)
install(CODE [[ file(INSTALL FILES $<TARGET_FILE:utils-xz> DESTINATION "${CMAKE_INSTALL_PREFIX}/bin" TYPE SHARED_LIBRARY) ]] COMPONENT "HyperHDR")
endif()

# Create a qt.conf file in 'bin' to override hard-coded search paths in Qt plugins
file(WRITE "${CMAKE_BINARY_DIR}/qt.conf" "[Paths]\nPlugins=../lib/\n")
install(
Expand Down
13 changes: 3 additions & 10 deletions external/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,19 +227,12 @@ endif()
#=============================================================================

if ( ENABLE_XZ )
option(BUILD_TESTING "" OFF)
if (NOT LIBLZMA_FOUND)
message( STATUS "System library xz could not be found. Using embedded xz library.")
if(MSVC)
set(BACKUP_OF_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
string(REGEX REPLACE "(\/W[011123456789])" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
message( STATUS "Disable warnings for xz library: ${CMAKE_CXX_FLAGS}")
endif()
option(BUILD_TESTING "" OFF)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/xz)
if(MSVC)
set(CMAKE_CXX_FLAGS ${BACKUP_OF_CMAKE_CXX_FLAGS})
message( STATUS "Restoring compiling flags after xz: ${CMAKE_CXX_FLAGS}")
endif()
set_target_properties(liblzma PROPERTIES INTERFACE_INCLUDE_DIRECTORIES $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/external/xz/src/liblzma/api>)
add_library(LibLZMA::LibLZMA ALIAS liblzma)
endif()
ENDIF()

Expand Down
7 changes: 7 additions & 0 deletions include/utils-xz/utils-xz.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#ifndef _XZ_SHARED_API
#define _XZ_SHARED_API
#endif

_XZ_SHARED_API const char* DecompressXZ(size_t downloadedDataSize, const uint8_t* downloadedData, const char* fileName);
5 changes: 5 additions & 0 deletions sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ add_subdirectory(systray)
add_subdirectory(utils)
add_subdirectory(webserver)

if(ENABLE_XZ)
add_subdirectory(utils-xz)
endif()


if (ENABLE_PROTOBUF)
add_subdirectory(proto-nano-server)
endif()
Expand Down
69 changes: 2 additions & 67 deletions sources/api/BaseAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include <hyperimage/HyperImage.h>

#ifdef ENABLE_XZ
#include <lzma.h>
#include <utils-xz/utils-xz.h>
#endif

#ifdef _WIN32
Expand Down Expand Up @@ -417,72 +417,7 @@ QString BaseAPI::installLut(QNetworkReply* reply, QString fileName, int hardware
{
QByteArray downloadedData = reply->readAll();

QFile file(fileName);
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
size_t outSize = 67174456;
MemoryBuffer<uint8_t> outBuffer(outSize);

if (outBuffer.data() == nullptr)
{
error = "Could not allocate buffer";
}
else
{
const uint32_t flags = LZMA_TELL_UNSUPPORTED_CHECK | LZMA_CONCATENATED;
lzma_stream strm = LZMA_STREAM_INIT;
strm.next_in = reinterpret_cast<uint8_t*>(downloadedData.data());
strm.avail_in = downloadedData.size();
lzma_ret lzmaRet = lzma_stream_decoder(&strm, outSize, flags);
if (lzmaRet == LZMA_OK)
{
do {
strm.next_out = outBuffer.data();
strm.avail_out = outSize;
lzmaRet = lzma_code(&strm, LZMA_FINISH);
if (lzmaRet == LZMA_MEMLIMIT_ERROR)
{
outSize = lzma_memusage(&strm);
outBuffer.resize(outSize);
if (outBuffer.data() == nullptr)
{
error = QString("Could not increase buffer size");
break;
}
lzma_memlimit_set(&strm, outSize);
strm.avail_out = 0;
}
else if (lzmaRet != LZMA_OK && lzmaRet != LZMA_STREAM_END)
{
// error
error = QString("LZMA decoder return error: %1").arg(lzmaRet);
break;
}
else
{
qint64 toWrite = static_cast<qint64>(outSize - strm.avail_out);
file.write(reinterpret_cast<char*>(outBuffer.data()), toWrite);
}
} while (strm.avail_out == 0 && lzmaRet != LZMA_STREAM_END);
file.flush();
}
else
{
error = "Could not initialize LZMA decoder";
}

if (time != 0)
file.setFileTime(QDateTime::fromMSecsSinceEpoch(time), QFileDevice::FileModificationTime);

file.close();
if (error != nullptr)
file.remove();

lzma_end(&strm);
}
}
else
error = QString("Could not open %1 for writing").arg(fileName);
error = DecompressXZ(downloadedData.size(), reinterpret_cast<uint8_t*>(downloadedData.data()), QSTRING_CSTR(fileName));
}
else
error = "Could not download LUT file";
Expand Down
8 changes: 1 addition & 7 deletions sources/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ target_link_libraries(hyperhdr-api
)

if(ENABLE_XZ)
if (NOT LIBLZMA_FOUND)
target_link_libraries(hyperhdr-api liblzma)
target_include_directories(hyperhdr-api PRIVATE "${CMAKE_SOURCE_DIR}/external/xz/src/liblzma/api")
else()
target_link_libraries(hyperhdr-api LibLZMA::LibLZMA)
target_include_directories(hyperhdr-api PRIVATE ${LIBLZMA_INCLUDE_DIR})
endif()
target_link_libraries(hyperhdr-api utils-xz)
endif()

if(USE_PRECOMPILED_HEADERS AND COMMAND target_precompile_headers)
Expand Down
34 changes: 34 additions & 0 deletions sources/utils-xz/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Define the current source locations

SET(CURRENT_HEADER_DIR ${CMAKE_SOURCE_DIR}/include/utils-xz)
SET(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/sources/utils-xz)

FILE ( GLOB_RECURSE utils_xz_SOURCES "${CURRENT_HEADER_DIR}/*.h" "${CURRENT_SOURCE_DIR}/*.cpp" )

if (USE_SHARED_LIBS)
add_library(utils-xz SHARED ${utils_xz_SOURCES})
if(WIN32)
target_compile_definitions(utils-xz
INTERFACE
"_XZ_SHARED_API=__declspec(dllimport)"
PRIVATE
"_XZ_SHARED_API=__declspec(dllexport)"
)
else()
target_compile_definitions(utils-xz
INTERFACE
"_XZ_SHARED_API=__attribute__((visibility(\"default\")))"
)
endif()
install(
TARGETS utils-xz
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
)
else()
add_library(utils-xz STATIC ${utils_xz_SOURCES})
endif()

target_link_libraries(utils-xz PRIVATE LibLZMA::LibLZMA)
target_include_directories(utils-xz PRIVATE LibLZMA::LibLZMA)

85 changes: 85 additions & 0 deletions sources/utils-xz/utils-xz.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <lzma.h>
#include <utils-xz/utils-xz.h>

_XZ_SHARED_API const char* DecompressXZ(size_t downloadedDataSize, const uint8_t* downloadedData, const char* fileName)
{
size_t outSize = 67174456;
std::vector<uint8_t> outBuffer;
const char* error = nullptr;

std::ofstream file;
file.open(fileName, std::ios::out | std::ios::trunc | std::ios::binary);

if (!file.is_open())
{
return "Could not open file for writing";
}

try
{
outBuffer.resize(outSize);
}
catch(...)
{
error = "Could not allocate buffer";
}

if (error == nullptr)
{
const uint32_t flags = LZMA_TELL_UNSUPPORTED_CHECK | LZMA_CONCATENATED;
lzma_stream strm = LZMA_STREAM_INIT;
strm.next_in = downloadedData;
strm.avail_in = downloadedDataSize;
lzma_ret lzmaRet = lzma_stream_decoder(&strm, outSize, flags);
if (lzmaRet == LZMA_OK)
{
do {
strm.next_out = outBuffer.data();
strm.avail_out = outSize;
lzmaRet = lzma_code(&strm, LZMA_FINISH);
if (lzmaRet == LZMA_MEMLIMIT_ERROR)
{
outSize = lzma_memusage(&strm);
try
{
outBuffer.resize(outSize);
}
catch (...)
{
error = "Could not increase buffer size";
break;
}
lzma_memlimit_set(&strm, outSize);
strm.avail_out = 0;
}
else if (lzmaRet != LZMA_OK && lzmaRet != LZMA_STREAM_END)
{
error = "LZMA decoder returned error";
break;
}
else
{
std::streamsize toWrite = static_cast<std::streamsize>(outSize - strm.avail_out);
file.write(reinterpret_cast<char*>(outBuffer.data()), toWrite);
}
} while (strm.avail_out == 0 && lzmaRet != LZMA_STREAM_END);
file.flush();
}
else
{
error = "Could not initialize LZMA decoder";
}

lzma_end(&strm);
}

file.close();

if (error != nullptr)
std::remove(fileName);

return error;
}

0 comments on commit 53e7106

Please sign in to comment.