Skip to content

Commit

Permalink
Added fastest scaling mode
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Oct 14, 2024
1 parent c2ab7ff commit 7253af4
Show file tree
Hide file tree
Showing 18 changed files with 50 additions and 88 deletions.
47 changes: 14 additions & 33 deletions app/src/main/java/com/radzivon/bartoshyk/avif/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class MainActivity : AppCompatActivity() {
var allFiles = mutableListOf<String>()
allFiles.addAll(allFiles2)
allFiles.addAll(allFiles1)
allFiles = allFiles.filter { it.contains("test_4.avif") }.toMutableList()
allFiles = allFiles.filter { it.contains("output_shot.avif") }.toMutableList()
// allFiles = allFiles.filter { it.contains("bbb_alpha_inverted.avif") }.toMutableList()
for (file in allFiles) {
try {
Expand All @@ -144,34 +144,15 @@ class MainActivity : AppCompatActivity() {

var bitmap0 = coder.decodeSampled(
byteArray = buffer,
scaledWidth = size.width / 3,
scaledHeight = size.height / 3,
scaledWidth = 325,
scaledHeight = 325,
preferredColorConfig = PreferredColorConfig.RGBA_8888,
scaleMode = ScaleMode.FIT,
scaleQuality = ScalingQuality.HIGH,
scaleMode = ScaleMode.FILL,
scaleQuality = ScalingQuality.FASTEST,
)

// bitmap0.setColorSpace(ColorSpace.getFromDataSpace(DataSpace.DATASPACE_BT2020_PQ)!!)

Log.i("AVIF", "Decoding time ${System.currentTimeMillis() - start}")

start = System.currentTimeMillis()

Log.i("AVIFFFF", "Starts encoding")

val encode = coder.encodeHeic(
bitmap = bitmap0,
quality = 25,
preciseMode = PreciseMode.LOSSY,
preset = HeifPreset.VERYSLOW
)
Log.i(
"AVIFFFF",
"Encoding time ${System.currentTimeMillis() - start}, encoded size ${encode.size}"
)

val bitmap = coder.decode(encode)

lifecycleScope.launch(Dispatchers.Main) {
val imageView = BindingImageViewBinding.inflate(
layoutInflater,
Expand All @@ -181,15 +162,15 @@ class MainActivity : AppCompatActivity() {
imageView.root.setImageBitmap(bitmap0)
binding.scrollViewContainer.addView(imageView.root)
}
lifecycleScope.launch(Dispatchers.Main) {
val imageView = BindingImageViewBinding.inflate(
layoutInflater,
binding.scrollViewContainer,
false
)
imageView.root.setImageBitmap(bitmap)
binding.scrollViewContainer.addView(imageView.root)
}
// lifecycleScope.launch(Dispatchers.Main) {
// val imageView = BindingImageViewBinding.inflate(
// layoutInflater,
// binding.scrollViewContainer,
// false
// )
// imageView.root.setImageBitmap(bitmap)
// binding.scrollViewContainer.addView(imageView.root)
// }
}
} catch (e: Exception) {
Log.d("AVIF", e.toString())
Expand Down
4 changes: 2 additions & 2 deletions avif-coder/src/main/cpp/AvifDecoderController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ AvifImageFrame AvifDecoderController::getFrame(uint32_t frame,
PreferredColorConfig javaColorSpace,
ScaleMode javaScaleMode,
CurveToneMapper toneMapper,
bool highQualityResizer) {
int scalingQuality) {
std::lock_guard guard(this->mutex);
if (!this->isBufferAttached) {
throw std::runtime_error("AVIF controller methods can't be called without attached buffer");
Expand Down Expand Up @@ -147,7 +147,7 @@ AvifImageFrame AvifDecoderController::getFrame(uint32_t frame,
imageStore = RescaleSourceImage(avifUniqueImage.rgbImage.pixels, &stride,
bitDepth, isImageRequires64Bit, &imageWidth,
&imageHeight, scaledWidth, scaledHeight, javaScaleMode,
highQualityResizer);
scalingQuality);

avifUniqueImage.clear();

Expand Down
2 changes: 1 addition & 1 deletion avif-coder/src/main/cpp/AvifDecoderController.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class AvifDecoderController {
PreferredColorConfig javaColorSpace,
ScaleMode javaScaleMode,
CurveToneMapper javaToneMapper,
bool highQualityResizer);
int scalingQuality);
void attachBuffer(uint8_t *data, uint32_t bufferSize);
uint32_t getFramesCount();
uint32_t getLoopsCount();
Expand Down
4 changes: 2 additions & 2 deletions avif-coder/src/main/cpp/HeifImageDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ AvifImageFrame HeifImageDecoder::getFrame(std::vector<uint8_t> &srcBuffer,
PreferredColorConfig javaColorSpace,
ScaleMode javaScaleMode,
CurveToneMapper toneMapper,
bool highQualityResizer) {
int scalingQuality) {
heif_context_set_max_decoding_threads(ctx.get(), (int) std::thread::hardware_concurrency());

auto result = heif_context_read_from_memory_without_copy(ctx.get(), srcBuffer.data(),
Expand Down Expand Up @@ -116,7 +116,7 @@ AvifImageFrame HeifImageDecoder::getFrame(std::vector<uint8_t> &srcBuffer,
static_cast<int>(scaledWidth),
static_cast<int>(scaledHeight),
javaScaleMode,
highQualityResizer);
scalingQuality);
if (!scaleResult) {
throw std::runtime_error("Rescaling an image has failed");
}
Expand Down
2 changes: 1 addition & 1 deletion avif-coder/src/main/cpp/HeifImageDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class HeifImageDecoder {
PreferredColorConfig javaColorSpace,
ScaleMode javaScaleMode,
CurveToneMapper javaToneMapper,
bool highQualityResizer);
int scalingQuality);

static std::string getImageType(std::vector<uint8_t> &srcBuffer);

Expand Down
2 changes: 1 addition & 1 deletion avif-coder/src/main/cpp/JniAnimatedController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ Java_com_radzivon_bartoshyk_avif_coder_AvifAnimatedDecoder_getFrameImpl(JNIEnv *
preferredColorConfig,
scaleMode,
toneMapper,
scaleQuality == 2);
scaleQuality);

int osVersion = androidOSVersion();

Expand Down
4 changes: 2 additions & 2 deletions avif-coder/src/main/cpp/JniDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobject decodeImplementationNative(JNIEnv *env, jobject thiz,
preferredColorConfig,
scaleMode,
toneMapper,
scalingQuality == 2);
scalingQuality);
} else {
HeifImageDecoder heifDecoder;
frame = heifDecoder.getFrame(srcBuffer,
Expand All @@ -58,7 +58,7 @@ jobject decodeImplementationNative(JNIEnv *env, jobject thiz,
preferredColorConfig,
scaleMode,
toneMapper,
scalingQuality == 2);
scalingQuality);
}

int osVersion = androidOSVersion();
Expand Down
12 changes: 6 additions & 6 deletions avif-coder/src/main/cpp/SizeScaler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bool RescaleImage(aligned_uint8_vector &initialData,
int *stride,
bool useFloats,
int *imageWidthPtr, int *imageHeightPtr,
int scaledWidth, int scaledHeight, ScaleMode scaleMode, bool highQualityScaling) {
int scaledWidth, int scaledHeight, ScaleMode scaleMode, int scalingQuality) {
int imageWidth = *imageWidthPtr;
int imageHeight = *imageHeightPtr;
if ((scaledHeight != 0 || scaledWidth != 0) && (scaledWidth != 0 && scaledHeight != 0)) {
Expand Down Expand Up @@ -105,7 +105,7 @@ bool RescaleImage(aligned_uint8_vector &initialData,
scaledWidth * 4,
scaledWidth,
scaledHeight,
highQualityScaling);
scalingQuality);
} else {
outData.resize(scaledHeight * scaledWidth * 4 * sizeof(uint16_t));
weave_scale_u16(reinterpret_cast<const uint16_t *>(imagePlane),
Expand All @@ -116,7 +116,7 @@ bool RescaleImage(aligned_uint8_vector &initialData,
scaledWidth,
scaledHeight,
bitDepth,
highQualityScaling);
scalingQuality);
}

auto data = outData.data();
Expand Down Expand Up @@ -230,7 +230,7 @@ aligned_uint8_vector RescaleSourceImage(uint8_t *sourceData,
uint32_t scaledWidth,
uint32_t scaledHeight,
ScaleMode scaleMode,
bool highQualityScaling) {
int scalingQuality) {
uint32_t imageWidth = *imageWidthPtr;
uint32_t imageHeight = *imageHeightPtr;
if ((scaledHeight != 0 || scaledWidth != 0) && (scaledWidth != 0 && scaledHeight != 0)) {
Expand Down Expand Up @@ -284,7 +284,7 @@ aligned_uint8_vector RescaleSourceImage(uint8_t *sourceData,
scaledWidth * 4,
scaledWidth,
scaledHeight,
highQualityScaling);
scalingQuality);
} else {
outData.resize(scaledHeight * scaledWidth * 4 * sizeof(uint16_t));
weave_scale_u16(reinterpret_cast<const uint16_t *>(sourceData),
Expand All @@ -295,7 +295,7 @@ aligned_uint8_vector RescaleSourceImage(uint8_t *sourceData,
scaledWidth,
scaledHeight,
bitDepth,
highQualityScaling);
scalingQuality);
}

auto data = outData.data();
Expand Down
4 changes: 2 additions & 2 deletions avif-coder/src/main/cpp/SizeScaler.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ bool RescaleImage(aligned_uint8_vector &initialData,
int *stride,
bool useFloats,
int *imageWidthPtr, int *imageHeightPtr,
int scaledWidth, int scaledHeight, ScaleMode scaleMode, bool scalingQuality);
int scaledWidth, int scaledHeight, ScaleMode scaleMode, int scalingQuality);

aligned_uint8_vector RescaleSourceImage(uint8_t *data,
uint32_t *stride,
Expand All @@ -57,7 +57,7 @@ aligned_uint8_vector RescaleSourceImage(uint8_t *data,
uint32_t scaledWidth,
uint32_t scaledHeight,
ScaleMode scaleMode,
bool highQualityScaling);
int scalingQuality);

std::pair<uint32_t, uint32_t>
ResizeAspectFit(std::pair<uint32_t, uint32_t> sourceSize,
Expand Down
32 changes: 2 additions & 30 deletions avif-coder/src/main/cpp/avifweaver.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,3 @@
/*
* MIT License
*
* Copyright (c) 2024 Radzivon Bartoshyk
* avif-coder [/~https://github.com/awxkee/avif-coder]
*
* Created by Radzivon Bartoshyk on 13/10/2024
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

#include <cstdarg>
#include <cstdint>
#include <cstdlib>
Expand All @@ -42,7 +14,7 @@ void weave_scale_u8(const uint8_t *src,
uint32_t dst_stride,
uint32_t new_width,
uint32_t new_height,
bool high_precision);
uint32_t method);

void weave_scale_u16(const uint16_t *src,
uintptr_t src_stride,
Expand All @@ -52,6 +24,6 @@ void weave_scale_u16(const uint16_t *src,
uint32_t new_width,
uint32_t new_height,
uintptr_t bit_depth,
bool high_precision);
uint32_t method);

} // extern "C"
Binary file modified avif-coder/src/main/cpp/lib/arm64-v8a/libavifweaver.so
Binary file not shown.
Binary file modified avif-coder/src/main/cpp/lib/armeabi-v7a/libavifweaver.so
Binary file not shown.
Binary file modified avif-coder/src/main/cpp/lib/x86/libavifweaver.so
Binary file not shown.
Binary file modified avif-coder/src/main/cpp/lib/x86_64/libavifweaver.so
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ enum class ScalingQuality(internal val level: Int) {
*/
DEFAULT(0),

/**
* Nearest neighbors
*/
FASTEST(1),

/**
* Lanczos 3
*/
Expand Down
4 changes: 2 additions & 2 deletions avifpixart/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
#

set -e
rustup default nightly
rustup default stable

rustup +nightly target add x86_64-linux-android aarch64-linux-android armv7-linux-androideabi i686-linux-android
rustup +stable target add x86_64-linux-android aarch64-linux-android armv7-linux-androideabi i686-linux-android

RUSTFLAGS="-C target-feature=+neon -C opt-level=2 -C strip=symbols" cargo +stable build --target aarch64-linux-android --release --manifest-path Cargo.toml

Expand Down
4 changes: 2 additions & 2 deletions avifpixart/include/avifweaver.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void weave_scale_u8(const uint8_t *src,
uint32_t dst_stride,
uint32_t new_width,
uint32_t new_height,
bool high_precision);
uint32_t method);

void weave_scale_u16(const uint16_t *src,
uintptr_t src_stride,
Expand All @@ -24,6 +24,6 @@ void weave_scale_u16(const uint16_t *src,
uint32_t new_width,
uint32_t new_height,
uintptr_t bit_depth,
bool high_precision);
uint32_t method);

} // extern "C"
12 changes: 8 additions & 4 deletions avifpixart/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub unsafe extern "C-unwind" fn weave_scale_u8(
dst_stride: u32,
new_width: u32,
new_height: u32,
high_precision: bool,
method: u32,
) {
let mut src_slice = vec![0u8; width as usize * height as usize * 4];

Expand All @@ -62,8 +62,10 @@ pub unsafe extern "C-unwind" fn weave_scale_u8(
let source_store =
ImageStore::<u8, 4>::new(src_slice, width as usize, height as usize).unwrap();

let mut scaler = Scaler::new(if high_precision {
let mut scaler = Scaler::new(if method == 3 {
ResamplingFunction::Lanczos3
} else if method == 1 {
ResamplingFunction::Nearest
} else {
ResamplingFunction::Bilinear
});
Expand Down Expand Up @@ -96,7 +98,7 @@ pub unsafe extern "C-unwind" fn weave_scale_u16(
new_width: u32,
new_height: u32,
bit_depth: usize,
high_precision: bool,
method: u32,
) {
let mut _src_slice = vec![0u16; width as usize * height as usize * 4];

Expand All @@ -122,8 +124,10 @@ pub unsafe extern "C-unwind" fn weave_scale_u16(
)
.unwrap();

let mut scaler = Scaler::new(if high_precision {
let mut scaler = Scaler::new(if method == 3 {
ResamplingFunction::Lanczos3
} else if method == 1 {
ResamplingFunction::Nearest
} else {
ResamplingFunction::Bilinear
});
Expand Down

0 comments on commit 7253af4

Please sign in to comment.