diff --git a/thermion_dart/lib/src/viewer/src/ffi/src/ffi_texture.dart b/thermion_dart/lib/src/viewer/src/ffi/src/ffi_texture.dart index 81e33331..31aa127c 100644 --- a/thermion_dart/lib/src/viewer/src/ffi/src/ffi_texture.dart +++ b/thermion_dart/lib/src/viewer/src/ffi/src/ffi_texture.dart @@ -76,10 +76,22 @@ class FFITexture extends Texture { } @override - Future setImage( - int level, Uint8List buffer, PixelDataFormat format, PixelDataType type) { - // TODO: implement setImage - throw UnimplementedError(); + Future setImage(int level, Uint8List buffer, int width, int height, + int channels, PixelDataFormat format, PixelDataType type) async { + final success = Texture_setImage( + _engine, + pointer, + level, + buffer.address, + buffer.lengthInBytes, + width, + height, + channels, + format.index, + type.index); + if (!success) { + throw Exception("Failed to set image"); + } } @override diff --git a/thermion_dart/native/include/c_api/TTexture.h b/thermion_dart/native/include/c_api/TTexture.h index 9cb5df22..9dbf3e0d 100644 --- a/thermion_dart/native/include/c_api/TTexture.h +++ b/thermion_dart/native/include/c_api/TTexture.h @@ -192,6 +192,18 @@ enum TPixelDataType { }; EMSCRIPTEN_KEEPALIVE bool Texture_loadImage(TEngine *tEngine, TTexture *tTexture, TLinearImage *tImage, TPixelDataFormat bufferFormat, TPixelDataType pixelDataType); +EMSCRIPTEN_KEEPALIVE bool Texture_setImage( + TEngine *tEngine, + TTexture *tTexture, + uint32_t level, + uint8_t *data, + size_t size, + uint32_t width, + uint32_t height, + uint32_t channels, + uint32_t bufferFormat, + uint32_t pixelDataType +); EMSCRIPTEN_KEEPALIVE TLinearImage *Image_createEmpty(uint32_t width,uint32_t height,uint32_t channel); EMSCRIPTEN_KEEPALIVE TLinearImage *Image_decode(uint8_t* data, size_t length, const char* name = "image"); EMSCRIPTEN_KEEPALIVE float *Image_getBytes(TLinearImage *tLinearImage); diff --git a/thermion_dart/native/src/c_api/TTexture.cpp b/thermion_dart/native/src/c_api/TTexture.cpp index d883541e..1c605db4 100644 --- a/thermion_dart/native/src/c_api/TTexture.cpp +++ b/thermion_dart/native/src/c_api/TTexture.cpp @@ -105,6 +105,55 @@ namespace thermion return true; } + EMSCRIPTEN_KEEPALIVE bool Texture_setImage( + TEngine *tEngine, + TTexture *tTexture, + uint32_t level, + uint8_t *data, + size_t size, + uint32_t width, + uint32_t height, + uint32_t channels, + uint32_t tBufferFormat, + uint32_t tPixelDataType + ) { + auto engine = reinterpret_cast(tEngine); + + auto texture = reinterpret_cast(tTexture); + auto bufferFormat = static_cast(tBufferFormat); + auto pixelDataType = static_cast(tPixelDataType); + + switch (bufferFormat) + { + case PixelBufferDescriptor::PixelDataFormat::RGB: + case PixelBufferDescriptor::PixelDataFormat::RGBA: + if(size != width * height * channels * sizeof(float)) { + Log("Size mismatch"); + return false; + } + break; + case PixelBufferDescriptor::PixelDataFormat::RGB_INTEGER: + case PixelBufferDescriptor::PixelDataFormat::RGBA_INTEGER: + if(size != width * height * channels * sizeof(uint8_t)) { + Log("Size mismatch"); + // return false; + } + break; + default: + Log("Unsupported buffer format type : %d", bufferFormat); + return false; + } + + filament::Texture::PixelBufferDescriptor buffer( + data, + size, + bufferFormat, + pixelDataType); + + texture->setImage(*engine, level, std::move(buffer)); + return true; + } + EMSCRIPTEN_KEEPALIVE TLinearImage *Image_createEmpty(uint32_t width,uint32_t height,uint32_t channel) { auto *image = new ::image::LinearImage(width, height, channel);