Skip to content

Commit 7253af4

Browse files
committed
Added fastest scaling mode
1 parent c2ab7ff commit 7253af4

File tree

18 files changed

+50
-88
lines changed

18 files changed

+50
-88
lines changed

app/src/main/java/com/radzivon/bartoshyk/avif/MainActivity.kt

+14-33
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class MainActivity : AppCompatActivity() {
122122
var allFiles = mutableListOf<String>()
123123
allFiles.addAll(allFiles2)
124124
allFiles.addAll(allFiles1)
125-
allFiles = allFiles.filter { it.contains("test_4.avif") }.toMutableList()
125+
allFiles = allFiles.filter { it.contains("output_shot.avif") }.toMutableList()
126126
// allFiles = allFiles.filter { it.contains("bbb_alpha_inverted.avif") }.toMutableList()
127127
for (file in allFiles) {
128128
try {
@@ -144,34 +144,15 @@ class MainActivity : AppCompatActivity() {
144144

145145
var bitmap0 = coder.decodeSampled(
146146
byteArray = buffer,
147-
scaledWidth = size.width / 3,
148-
scaledHeight = size.height / 3,
147+
scaledWidth = 325,
148+
scaledHeight = 325,
149149
preferredColorConfig = PreferredColorConfig.RGBA_8888,
150-
scaleMode = ScaleMode.FIT,
151-
scaleQuality = ScalingQuality.HIGH,
150+
scaleMode = ScaleMode.FILL,
151+
scaleQuality = ScalingQuality.FASTEST,
152152
)
153153

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

156-
Log.i("AVIF", "Decoding time ${System.currentTimeMillis() - start}")
157-
158-
start = System.currentTimeMillis()
159-
160-
Log.i("AVIFFFF", "Starts encoding")
161-
162-
val encode = coder.encodeHeic(
163-
bitmap = bitmap0,
164-
quality = 25,
165-
preciseMode = PreciseMode.LOSSY,
166-
preset = HeifPreset.VERYSLOW
167-
)
168-
Log.i(
169-
"AVIFFFF",
170-
"Encoding time ${System.currentTimeMillis() - start}, encoded size ${encode.size}"
171-
)
172-
173-
val bitmap = coder.decode(encode)
174-
175156
lifecycleScope.launch(Dispatchers.Main) {
176157
val imageView = BindingImageViewBinding.inflate(
177158
layoutInflater,
@@ -181,15 +162,15 @@ class MainActivity : AppCompatActivity() {
181162
imageView.root.setImageBitmap(bitmap0)
182163
binding.scrollViewContainer.addView(imageView.root)
183164
}
184-
lifecycleScope.launch(Dispatchers.Main) {
185-
val imageView = BindingImageViewBinding.inflate(
186-
layoutInflater,
187-
binding.scrollViewContainer,
188-
false
189-
)
190-
imageView.root.setImageBitmap(bitmap)
191-
binding.scrollViewContainer.addView(imageView.root)
192-
}
165+
// lifecycleScope.launch(Dispatchers.Main) {
166+
// val imageView = BindingImageViewBinding.inflate(
167+
// layoutInflater,
168+
// binding.scrollViewContainer,
169+
// false
170+
// )
171+
// imageView.root.setImageBitmap(bitmap)
172+
// binding.scrollViewContainer.addView(imageView.root)
173+
// }
193174
}
194175
} catch (e: Exception) {
195176
Log.d("AVIF", e.toString())

avif-coder/src/main/cpp/AvifDecoderController.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ AvifImageFrame AvifDecoderController::getFrame(uint32_t frame,
8080
PreferredColorConfig javaColorSpace,
8181
ScaleMode javaScaleMode,
8282
CurveToneMapper toneMapper,
83-
bool highQualityResizer) {
83+
int scalingQuality) {
8484
std::lock_guard guard(this->mutex);
8585
if (!this->isBufferAttached) {
8686
throw std::runtime_error("AVIF controller methods can't be called without attached buffer");
@@ -147,7 +147,7 @@ AvifImageFrame AvifDecoderController::getFrame(uint32_t frame,
147147
imageStore = RescaleSourceImage(avifUniqueImage.rgbImage.pixels, &stride,
148148
bitDepth, isImageRequires64Bit, &imageWidth,
149149
&imageHeight, scaledWidth, scaledHeight, javaScaleMode,
150-
highQualityResizer);
150+
scalingQuality);
151151

152152
avifUniqueImage.clear();
153153

avif-coder/src/main/cpp/AvifDecoderController.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class AvifDecoderController {
5959
PreferredColorConfig javaColorSpace,
6060
ScaleMode javaScaleMode,
6161
CurveToneMapper javaToneMapper,
62-
bool highQualityResizer);
62+
int scalingQuality);
6363
void attachBuffer(uint8_t *data, uint32_t bufferSize);
6464
uint32_t getFramesCount();
6565
uint32_t getLoopsCount();

avif-coder/src/main/cpp/HeifImageDecoder.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ AvifImageFrame HeifImageDecoder::getFrame(std::vector<uint8_t> &srcBuffer,
4242
PreferredColorConfig javaColorSpace,
4343
ScaleMode javaScaleMode,
4444
CurveToneMapper toneMapper,
45-
bool highQualityResizer) {
45+
int scalingQuality) {
4646
heif_context_set_max_decoding_threads(ctx.get(), (int) std::thread::hardware_concurrency());
4747

4848
auto result = heif_context_read_from_memory_without_copy(ctx.get(), srcBuffer.data(),
@@ -116,7 +116,7 @@ AvifImageFrame HeifImageDecoder::getFrame(std::vector<uint8_t> &srcBuffer,
116116
static_cast<int>(scaledWidth),
117117
static_cast<int>(scaledHeight),
118118
javaScaleMode,
119-
highQualityResizer);
119+
scalingQuality);
120120
if (!scaleResult) {
121121
throw std::runtime_error("Rescaling an image has failed");
122122
}

avif-coder/src/main/cpp/HeifImageDecoder.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class HeifImageDecoder {
5858
PreferredColorConfig javaColorSpace,
5959
ScaleMode javaScaleMode,
6060
CurveToneMapper javaToneMapper,
61-
bool highQualityResizer);
61+
int scalingQuality);
6262

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

avif-coder/src/main/cpp/JniAnimatedController.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ Java_com_radzivon_bartoshyk_avif_coder_AvifAnimatedDecoder_getFrameImpl(JNIEnv *
196196
preferredColorConfig,
197197
scaleMode,
198198
toneMapper,
199-
scaleQuality == 2);
199+
scaleQuality);
200200

201201
int osVersion = androidOSVersion();
202202

avif-coder/src/main/cpp/JniDecoder.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobject decodeImplementationNative(JNIEnv *env, jobject thiz,
4949
preferredColorConfig,
5050
scaleMode,
5151
toneMapper,
52-
scalingQuality == 2);
52+
scalingQuality);
5353
} else {
5454
HeifImageDecoder heifDecoder;
5555
frame = heifDecoder.getFrame(srcBuffer,
@@ -58,7 +58,7 @@ jobject decodeImplementationNative(JNIEnv *env, jobject thiz,
5858
preferredColorConfig,
5959
scaleMode,
6060
toneMapper,
61-
scalingQuality == 2);
61+
scalingQuality);
6262
}
6363

6464
int osVersion = androidOSVersion();

avif-coder/src/main/cpp/SizeScaler.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ bool RescaleImage(aligned_uint8_vector &initialData,
4242
int *stride,
4343
bool useFloats,
4444
int *imageWidthPtr, int *imageHeightPtr,
45-
int scaledWidth, int scaledHeight, ScaleMode scaleMode, bool highQualityScaling) {
45+
int scaledWidth, int scaledHeight, ScaleMode scaleMode, int scalingQuality) {
4646
int imageWidth = *imageWidthPtr;
4747
int imageHeight = *imageHeightPtr;
4848
if ((scaledHeight != 0 || scaledWidth != 0) && (scaledWidth != 0 && scaledHeight != 0)) {
@@ -105,7 +105,7 @@ bool RescaleImage(aligned_uint8_vector &initialData,
105105
scaledWidth * 4,
106106
scaledWidth,
107107
scaledHeight,
108-
highQualityScaling);
108+
scalingQuality);
109109
} else {
110110
outData.resize(scaledHeight * scaledWidth * 4 * sizeof(uint16_t));
111111
weave_scale_u16(reinterpret_cast<const uint16_t *>(imagePlane),
@@ -116,7 +116,7 @@ bool RescaleImage(aligned_uint8_vector &initialData,
116116
scaledWidth,
117117
scaledHeight,
118118
bitDepth,
119-
highQualityScaling);
119+
scalingQuality);
120120
}
121121

122122
auto data = outData.data();
@@ -230,7 +230,7 @@ aligned_uint8_vector RescaleSourceImage(uint8_t *sourceData,
230230
uint32_t scaledWidth,
231231
uint32_t scaledHeight,
232232
ScaleMode scaleMode,
233-
bool highQualityScaling) {
233+
int scalingQuality) {
234234
uint32_t imageWidth = *imageWidthPtr;
235235
uint32_t imageHeight = *imageHeightPtr;
236236
if ((scaledHeight != 0 || scaledWidth != 0) && (scaledWidth != 0 && scaledHeight != 0)) {
@@ -284,7 +284,7 @@ aligned_uint8_vector RescaleSourceImage(uint8_t *sourceData,
284284
scaledWidth * 4,
285285
scaledWidth,
286286
scaledHeight,
287-
highQualityScaling);
287+
scalingQuality);
288288
} else {
289289
outData.resize(scaledHeight * scaledWidth * 4 * sizeof(uint16_t));
290290
weave_scale_u16(reinterpret_cast<const uint16_t *>(sourceData),
@@ -295,7 +295,7 @@ aligned_uint8_vector RescaleSourceImage(uint8_t *sourceData,
295295
scaledWidth,
296296
scaledHeight,
297297
bitDepth,
298-
highQualityScaling);
298+
scalingQuality);
299299
}
300300

301301
auto data = outData.data();

avif-coder/src/main/cpp/SizeScaler.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ bool RescaleImage(aligned_uint8_vector &initialData,
4646
int *stride,
4747
bool useFloats,
4848
int *imageWidthPtr, int *imageHeightPtr,
49-
int scaledWidth, int scaledHeight, ScaleMode scaleMode, bool scalingQuality);
49+
int scaledWidth, int scaledHeight, ScaleMode scaleMode, int scalingQuality);
5050

5151
aligned_uint8_vector RescaleSourceImage(uint8_t *data,
5252
uint32_t *stride,
@@ -57,7 +57,7 @@ aligned_uint8_vector RescaleSourceImage(uint8_t *data,
5757
uint32_t scaledWidth,
5858
uint32_t scaledHeight,
5959
ScaleMode scaleMode,
60-
bool highQualityScaling);
60+
int scalingQuality);
6161

6262
std::pair<uint32_t, uint32_t>
6363
ResizeAspectFit(std::pair<uint32_t, uint32_t> sourceSize,

avif-coder/src/main/cpp/avifweaver.h

+2-30
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,3 @@
1-
/*
2-
* MIT License
3-
*
4-
* Copyright (c) 2024 Radzivon Bartoshyk
5-
* avif-coder [https://github.com/awxkee/avif-coder]
6-
*
7-
* Created by Radzivon Bartoshyk on 13/10/2024
8-
*
9-
* Permission is hereby granted, free of charge, to any person obtaining a copy
10-
* of this software and associated documentation files (the "Software"), to deal
11-
* in the Software without restriction, including without limitation the rights
12-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13-
* copies of the Software, and to permit persons to whom the Software is
14-
* furnished to do so, subject to the following conditions:
15-
*
16-
* The above copyright notice and this permission notice shall be included in all
17-
* copies or substantial portions of the Software.
18-
*
19-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25-
* SOFTWARE.
26-
*
27-
*/
28-
291
#include <cstdarg>
302
#include <cstdint>
313
#include <cstdlib>
@@ -42,7 +14,7 @@ void weave_scale_u8(const uint8_t *src,
4214
uint32_t dst_stride,
4315
uint32_t new_width,
4416
uint32_t new_height,
45-
bool high_precision);
17+
uint32_t method);
4618

4719
void weave_scale_u16(const uint16_t *src,
4820
uintptr_t src_stride,
@@ -52,6 +24,6 @@ void weave_scale_u16(const uint16_t *src,
5224
uint32_t new_width,
5325
uint32_t new_height,
5426
uintptr_t bit_depth,
55-
bool high_precision);
27+
uint32_t method);
5628

5729
} // extern "C"
Binary file not shown.
Binary file not shown.
48 Bytes
Binary file not shown.
Binary file not shown.

avif-coder/src/main/java/com/radzivon/bartoshyk/avif/coder/ScalingQuality.kt

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ enum class ScalingQuality(internal val level: Int) {
3434
*/
3535
DEFAULT(0),
3636

37+
/**
38+
* Nearest neighbors
39+
*/
40+
FASTEST(1),
41+
3742
/**
3843
* Lanczos 3
3944
*/

avifpixart/build.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
#
2929

3030
set -e
31-
rustup default nightly
31+
rustup default stable
3232

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

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

avifpixart/include/avifweaver.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void weave_scale_u8(const uint8_t *src,
1414
uint32_t dst_stride,
1515
uint32_t new_width,
1616
uint32_t new_height,
17-
bool high_precision);
17+
uint32_t method);
1818

1919
void weave_scale_u16(const uint16_t *src,
2020
uintptr_t src_stride,
@@ -24,6 +24,6 @@ void weave_scale_u16(const uint16_t *src,
2424
uint32_t new_width,
2525
uint32_t new_height,
2626
uintptr_t bit_depth,
27-
bool high_precision);
27+
uint32_t method);
2828

2929
} // extern "C"

avifpixart/src/lib.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub unsafe extern "C-unwind" fn weave_scale_u8(
4040
dst_stride: u32,
4141
new_width: u32,
4242
new_height: u32,
43-
high_precision: bool,
43+
method: u32,
4444
) {
4545
let mut src_slice = vec![0u8; width as usize * height as usize * 4];
4646

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

65-
let mut scaler = Scaler::new(if high_precision {
65+
let mut scaler = Scaler::new(if method == 3 {
6666
ResamplingFunction::Lanczos3
67+
} else if method == 1 {
68+
ResamplingFunction::Nearest
6769
} else {
6870
ResamplingFunction::Bilinear
6971
});
@@ -96,7 +98,7 @@ pub unsafe extern "C-unwind" fn weave_scale_u16(
9698
new_width: u32,
9799
new_height: u32,
98100
bit_depth: usize,
99-
high_precision: bool,
101+
method: u32,
100102
) {
101103
let mut _src_slice = vec![0u16; width as usize * height as usize * 4];
102104

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

125-
let mut scaler = Scaler::new(if high_precision {
127+
let mut scaler = Scaler::new(if method == 3 {
126128
ResamplingFunction::Lanczos3
129+
} else if method == 1 {
130+
ResamplingFunction::Nearest
127131
} else {
128132
ResamplingFunction::Bilinear
129133
});

0 commit comments

Comments
 (0)