Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/encode #2

Merged
merged 8 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 174 additions & 20 deletions cpp/react-native-basis-universal.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#include "react-native-basis-universal.h"

#define DEFINE_BASIS_ENCODER_PARAMS_SETTER(func_name, param_name, param_type) \
void ReactNativeBasisUniversal::func_name(jsi::Runtime &rt, jsi::Object handle, param_type flag) { \
auto encoder = tryGetBasisEncoder(rt, handle); \
encoder->m_params.param_name = flag; \
}


using namespace basist;
using namespace basisu;

Expand Down Expand Up @@ -43,49 +50,196 @@ jsi::Object ReactNativeBasisUniversal::createBasisHandle(jsi::Runtime &rt) {
return basisObject;
}

void ReactNativeBasisUniversal::setDebug(jsi::Runtime &rt, jsi::Object handle, bool flag) {
int ReactNativeBasisUniversal::encode(jsi::Runtime &rt, jsi::Object handle, jsi::Object basisFileData) {
auto encoder = tryGetBasisEncoder(rt, handle);
encoder->m_params.m_debug = flag;
}

void ReactNativeBasisUniversal::setCreateKTX2File(jsi::Runtime &rt, jsi::Object handle, bool flag) {
auto encoder = tryGetBasisEncoder(rt, handle);
encoder->m_params.m_create_ktx2_file = flag;

if (!basisFileData.isArrayBuffer(rt)) {
throw jsi::JSError(rt, "Image Array needs to be ArrayBuffer");
}

auto arrayBuffer = basisFileData.getArrayBuffer(rt);
auto data = arrayBuffer.data(rt);

if (!basis_initialized_flag)
{
assert(0);
return 0;
}

// We don't use threading for now, but the compressor needs a job pool.
job_pool jpool(1);

// Initialize the compression parameters structure. This is the same structure that the command line tool fills in.
basis_compressor_params &params = encoder->m_params;

params.m_pJob_pool = &jpool;

// Disabling multithreading for now, which sucks.
params.m_multithreading = false;

params.m_status_output = params.m_debug;

params.m_read_source_images = false;
params.m_write_output_basis_or_ktx2_files = false;

basis_compressor comp;

if (!comp.init(params))
{
return 0;
}

basis_compressor::error_code ec = comp.process();

if (ec != basis_compressor::cECSuccess)
{
// Something failed during compression.
return 0;
}

if (params.m_create_ktx2_file)
{
// Compression succeeded, so copy the .ktx2 file bytes to the caller's buffer.
auto output = comp.get_output_basis_file();

if (!data) {
return 0;
}

auto outputBuffer = jsi::ArrayBuffer(std::move(arrayBuffer));
memcpy(outputBuffer.data(rt), output.data(), output.size());
basisFileData.setProperty(rt, jsi::PropNameID::forAscii(rt, "buffer"), outputBuffer);


// Return the file size of the .basis file in bytes.
return (uint32_t)comp.get_output_ktx2_file().size();
}
else
{
auto output = comp.get_output_basis_file();

if (!data) {
return 0;
}

// Compression succeeded, so copy the .basis file bytes to the caller's buffer.
auto outputBuffer = jsi::ArrayBuffer(std::move(arrayBuffer));
memcpy(outputBuffer.data(rt), output.data(), output.size());
basisFileData.setProperty(rt, jsi::PropNameID::forAscii(rt, "buffer"), outputBuffer);

// Return the file size of the .basis file in bytes.
return (uint32_t)comp.get_output_basis_file().size();
}

return 0;
}

void ReactNativeBasisUniversal::setComputeStats(jsi::Runtime &rt, jsi::Object handle, bool flag) {
void ReactNativeBasisUniversal::setKTX2UASTCSupercompression(jsi::Runtime &rt, jsi::Object handle, bool flag) {
auto encoder = tryGetBasisEncoder(rt, handle);
encoder->m_params.m_compute_stats = flag;
encoder->m_params.m_ktx2_uastc_supercompression = flag ? basist::KTX2_SS_ZSTANDARD : basist::KTX2_SS_NONE;
}

void ReactNativeBasisUniversal::setUASTC(jsi::Runtime &rt, jsi::Object handle, bool flag) {
void ReactNativeBasisUniversal::setQualityLevel(jsi::Runtime &rt, jsi::Object handle, int qualityLevel) {
auto encoder = tryGetBasisEncoder(rt, handle);
encoder->m_params.m_uastc = flag;
assert(qualityLevel >= -1 && qualityLevel <= BASISU_QUALITY_MAX);
encoder->m_params.m_quality_level = qualityLevel;
}

void ReactNativeBasisUniversal::setKTX2UASTCSupercompression(jsi::Runtime &rt, jsi::Object handle, bool flag) {
bool ReactNativeBasisUniversal::setSliceSourceImage(jsi::Runtime &rt, jsi::Object handle, int sliceIndex, jsi::Object imageArray, int width, int height, bool isPng) {
if (!imageArray.isArrayBuffer(rt)) {
throw jsi::JSError(rt, "Image Array needs to be ArrayBuffer");
}
auto encoder = tryGetBasisEncoder(rt, handle);
encoder->m_params.m_ktx2_uastc_supercompression = flag ? basist::KTX2_SS_ZSTANDARD : basist::KTX2_SS_NONE;;
}

void ReactNativeBasisUniversal::setQualityLevel(jsi::Runtime &rt, jsi::Object handle, int qualityLevel) {

if (sliceIndex >= encoder->m_params.m_source_images.size())
encoder->m_params.m_source_images.resize(sliceIndex + 1);

auto arrayBuffer = imageArray.getArrayBuffer(rt);
auto data = arrayBuffer.data(rt);

image& src_img = encoder->m_params.m_source_images[sliceIndex];

if (isPng)
{
// It's a PNG file, so try and parse it.
if (!load_png(data, arrayBuffer.size(rt), src_img, nullptr))
{
return false;
}

width = src_img.get_width();
height = src_img.get_height();
}
else
{
// It's a raw image, so check the buffer's size.
if (arrayBuffer.size(rt) != width * height * sizeof(uint32_t))
{
return false;
}

// Copy the raw image's data into our source image.
src_img.resize(width, height);
memcpy(src_img.get_ptr(), arrayBuffer.data(rt), width * height * sizeof(uint32_t));
}

return true;
}

void ReactNativeBasisUniversal::setSliceSourceImage(jsi::Runtime &rt, jsi::Object handle, int sliceIndex, jsi::Object imageArray, int width, int height, bool isPng) {
if (imageArray.isArrayBuffer(rt)) {
bool ReactNativeBasisUniversal::setSliceSourceImageHDR(jsi::Runtime &rt, jsi::Object handle, int sliceIndex, jsi::Object imageArray, int width, int height, int imgType, bool ldrSrgbToLinear) {
if (!imageArray.isArrayBuffer(rt)) {
throw jsi::JSError(rt, "Image Array needs to be ArrayBuffer");
}
auto encoder = tryGetBasisEncoder(rt, handle);

auto arrayBuffer = imageArray.getArrayBuffer(rt);
auto data = arrayBuffer.data(rt);

// Resize the source_images_hdr array if necessary
if (sliceIndex >= encoder->m_params.m_source_images_hdr.size())
encoder->m_params.m_source_images_hdr.resize(sliceIndex + 1);

// Now load the source image.
imagef& src_img = encoder->m_params.m_source_images_hdr[sliceIndex];

return load_image_hdr(data,
arrayBuffer.size(rt),
src_img,
width,
height,
(hdr_image_type)imgType,
ldrSrgbToLinear);
}

void ReactNativeBasisUniversal::setPackUASTCFlags(jsi::Runtime &rt, jsi::Object handle, int flags) {

auto encoder = tryGetBasisEncoder(rt, handle);
assert((flags & cPackUASTCLevelMask) >= cPackUASTCLevelFastest);
assert((flags & cPackUASTCLevelMask) <= cPackUASTCLevelVerySlow);
encoder->m_params.m_pack_uastc_flags = flags;
}

void ReactNativeBasisUniversal::setCompressionLevel(jsi::Runtime &rt, jsi::Object handle, int level) {

auto encoder = tryGetBasisEncoder(rt, handle);
assert(level >= 0 && level <= BASISU_MAX_COMPRESSION_LEVEL);
encoder->m_params.m_compression_level = level;
}

void ReactNativeBasisUniversal::setNormalMap(jsi::Runtime &rt, jsi::Object handle) {
auto encoder = tryGetBasisEncoder(rt, handle);
encoder->m_params.m_perceptual = false;
encoder->m_params.m_mip_srgb = false;
encoder->m_params.m_no_selector_rdo = true;
encoder->m_params.m_no_endpoint_rdo = true;
}

DEFINE_BASIS_ENCODER_PARAMS_SETTER(setMipSRGB, m_mip_srgb, bool);
DEFINE_BASIS_ENCODER_PARAMS_SETTER(setMipRenormalize, m_mip_renormalize, bool);
DEFINE_BASIS_ENCODER_PARAMS_SETTER(setKTX2SRGBTransferFunc, m_ktx2_srgb_transfer_func, bool);
DEFINE_BASIS_ENCODER_PARAMS_SETTER(setYFlip, m_y_flip, bool);
DEFINE_BASIS_ENCODER_PARAMS_SETTER(setMipGen, m_mip_gen, bool);
DEFINE_BASIS_ENCODER_PARAMS_SETTER(setPerceptual, m_perceptual, bool);
DEFINE_BASIS_ENCODER_PARAMS_SETTER(setUASTC, m_uastc, bool);
DEFINE_BASIS_ENCODER_PARAMS_SETTER(setComputeStats, m_compute_stats, bool);
DEFINE_BASIS_ENCODER_PARAMS_SETTER(setCreateKTX2File, m_create_ktx2_file, bool);
DEFINE_BASIS_ENCODER_PARAMS_SETTER(setDebug, m_debug, bool);
}
13 changes: 11 additions & 2 deletions cpp/react-native-basis-universal.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,19 @@ class ReactNativeBasisUniversal : public NativeBasisUniversalCxxSpecJSI {
void setUASTC(jsi::Runtime &rt, jsi::Object handle, bool flag) override;
void setCompressionLevel(jsi::Runtime &rt, jsi::Object handle, int level) override;
void setKTX2UASTCSupercompression(jsi::Runtime &rt, jsi::Object handle, bool flag) override;
void setSliceSourceImage(jsi::Runtime &rt, jsi::Object handle, int sliceIndex, jsi::Object imageArray, int width, int height, bool isPng) override;
bool setSliceSourceImage(jsi::Runtime &rt, jsi::Object handle, int sliceIndex, jsi::Object imageArray, int width, int height, bool isPng) override;
void setPackUASTCFlags(jsi::Runtime &rt, jsi::Object handle, int flags) override;
void setQualityLevel(jsi::Runtime &rt, jsi::Object handle, int qualityLevel) override;

int encode(jsi::Runtime &rt, jsi::Object handle, jsi::Object basisFileData) override;
void setPerceptual(jsi::Runtime &rt, jsi::Object handle, bool flag) override;
void setYFlip(jsi::Runtime &rt, jsi::Object handle, bool flag) override;
void setMipGen(jsi::Runtime &rt, jsi::Object handle, bool flag) override;
void setNormalMap(jsi::Runtime &rt, jsi::Object handle) override;
void setKTX2SRGBTransferFunc(jsi::Runtime &rt, jsi::Object handle, bool flag) override;
void setMipSRGB(jsi::Runtime &rt, jsi::Object handle, bool flag) override;
void setMipRenormalize(jsi::Runtime &rt, jsi::Object handle, bool flag) override;
bool setSliceSourceImageHDR(jsi::Runtime &rt, jsi::Object handle, int sliceIndex, jsi::Object imageArray, int width, int height, int imgType, bool ldrSrgbToLinear) override;


private:
std::shared_ptr<CallInvoker> _callInvoker;
okwasniewski marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
1 change: 1 addition & 0 deletions example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ workspace 'BasisUniversalExample.xcworkspace'
options = {
:fabric_enabled => true,
:bridgeless_enabled => true,
:hermes_enabled => true,
}

use_test_app! options
Loading
Loading