Skip to content

Commit 182650d

Browse files
authored
Merge pull request #37 from awxkee/dev
Decoding fixes
2 parents cf6554e + 827bee3 commit 182650d

File tree

13 files changed

+228
-94
lines changed

13 files changed

+228
-94
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class MainActivity : AppCompatActivity() {
122122
// allFiles = allFiles.take(5).toMutableList()
123123
// allFiles = allFiles.filter { it.contains("hato-wide-gamut-8bit.avif") || it.contains("wide_gamut.avif") || it.contains("IMG_0199_rr.avif") || it.contains("bt_2020_pq.avif") }.toMutableList()
124124
// allFiles = allFiles.filter { it.contains("bbb_alpha_inverted.avif") }.toMutableList()
125-
allFiles = allFiles.filter { it.contains("result.heic") }.toMutableList()
125+
allFiles = allFiles.filter { it.contains("head-empty-hdr.avif") }.toMutableList()
126126
for (file in allFiles) {
127127
try {
128128
Log.d("AVIF", "start processing $file")
@@ -138,6 +138,9 @@ class MainActivity : AppCompatActivity() {
138138
PreferredColorConfig.RGBA_8888,
139139
ScaleMode.RESIZE
140140
)
141+
// val bitmap0 = coder.decode(
142+
// buffer,
143+
// )
141144
var start = System.currentTimeMillis()
142145

143146
// var bitmap0 = coder.decode(
@@ -147,7 +150,7 @@ class MainActivity : AppCompatActivity() {
147150

148151
Log.d("AVIFFFF", "Decode time ${System.currentTimeMillis() - start}")
149152

150-
val encode = coder.encodeAvif(bitmap0, avifChromaSubsampling = AvifChromaSubsampling.YUV420)
153+
// val encode = coder.encodeAvif(bitmap0, avifChromaSubsampling = AvifChromaSubsampling.YUV420)
151154
// val roundTripped = coder.decode(encode)
152155
//
153156
//

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

+8
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,14 @@ ReformatColorConfig(JNIEnv *env, aligned_uint8_vector &imageData, string &imageC
247247
}
248248
break;
249249
default: {
250+
if (*useFloats) {
251+
weave_cvt_rgba16_to_rgba_f16(reinterpret_cast<const uint16_t *>(imageData.data()),
252+
*stride,
253+
depth,
254+
reinterpret_cast<uint16_t *>(imageData.data()),
255+
*stride,
256+
imageWidth, imageHeight);
257+
}
250258
}
251259
break;
252260
}
Binary file not shown.
Binary file not shown.
-288 KB
Binary file not shown.
Binary file not shown.

avifpixart/Cargo.lock

+13-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

avifpixart/Cargo.toml

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ edition = "2021"
55

66
[dependencies]
77
num-traits = "0.2"
8-
pic-scale = { version = "0.5.4", default-features = false, features = ["nightly_f16"] }
9-
yuvutils-rs = { path = "../../../RustroverProjects/yuvutils-rs", features = ["nightly_f16"], default-features = false }
8+
pic-scale = { version = "0.5.9", default-features = false, features = ["nightly_f16"] }
9+
yuv = { version = "0.8.3", features = ["nightly_f16"], default-features = false }
1010
bytemuck = { version = "1.22", features = ["nightly_float"] }
11-
moxcms = {path = "../../../RustroverProjects/moxcms"}
12-
gainforge = {path = "../../../RustroverProjects/gainforge"}
11+
moxcms = "0.6.1"
12+
gainforge = "0.3.2"
1313

1414
[features]
15-
rdm = ["yuvutils-rs/rdm", "pic-scale/rdm"]
15+
rdm = ["yuv/rdm", "pic-scale/rdm"]
1616

1717
[lib]
1818
crate-type = ["cdylib", "staticlib"]

avifpixart/src/cvt.rs

+150-32
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828
*/
2929
use core::f16;
30-
use yuvutils_rs::{
30+
use yuv::{
3131
convert_rgba16_to_f16, convert_rgba_to_f16, rgba10_to_ar30, rgba12_to_ar30, rgba8_to_ar30,
3232
BufferStoreMut, Rgb30ByteOrder,
3333
};
@@ -46,7 +46,7 @@ fn work_on_transmuted_ptr_f16<F>(
4646
let mut allocated = false;
4747
let mut dst_stride = rgba_stride as usize / 2;
4848
let mut working_slice: BufferStoreMut<'_, f16> = unsafe {
49-
if rgba as usize % 2 == 0 && rgba_stride / 2 == 0 {
49+
if rgba as usize % 2 == 0 && rgba_stride % 2 == 0 {
5050
BufferStoreMut::Borrowed(std::slice::from_raw_parts_mut(
5151
rgba as *mut f16,
5252
rgba_stride as usize / 2 * height,
@@ -78,7 +78,7 @@ fn work_on_transmuted_ptr_f16<F>(
7878

7979
if allocated {
8080
let src_slice = working_slice.borrow();
81-
let dst_slice = unsafe {
81+
let dst_slice: &mut [u8] = unsafe {
8282
std::slice::from_raw_parts_mut(rgba as *mut u8, rgba_stride as usize * height)
8383
};
8484
for (src, dst) in src_slice
@@ -110,7 +110,7 @@ pub(crate) fn work_on_transmuted_ptr_u16<F>(
110110
let mut allocated = false;
111111
let mut dst_stride = rgba_stride as usize / 2;
112112
let mut working_slice: BufferStoreMut<'_, u16> = unsafe {
113-
if rgba as usize % 2 == 0 && rgba_stride / 2 == 0 {
113+
if rgba as usize % 2 == 0 && rgba_stride % 2 == 0 {
114114
BufferStoreMut::Borrowed(std::slice::from_raw_parts_mut(
115115
rgba as *mut u16,
116116
rgba_stride as usize / 2 * height,
@@ -320,32 +320,150 @@ pub extern "C" fn weave_cvt_rgba16_to_rgba_f16(
320320
width: u32,
321321
height: u32,
322322
) {
323-
work_on_transmuted_ptr_u16(
324-
rgba16,
325-
rgba16_stride,
326-
width as usize,
327-
height as usize,
328-
true,
329-
|src: &mut [u16], src_stride: usize| {
330-
work_on_transmuted_ptr_f16(
331-
rgba_f16,
332-
rgba_f16_stride,
333-
width as usize,
334-
height as usize,
335-
false,
336-
|dst: &mut [f16], dst_stride: usize| {
337-
convert_rgba16_to_f16(
338-
src,
339-
src_stride,
340-
dst,
341-
dst_stride,
342-
bit_depth,
343-
width as usize,
344-
height as usize,
345-
)
346-
.unwrap();
347-
},
348-
);
349-
},
350-
);
323+
if rgba16 == rgba_f16 {
324+
let mut new_src = vec![0; width as usize * height as usize * 4];
325+
let src_slice = unsafe {
326+
std::slice::from_raw_parts(rgba16 as *mut u8, rgba16_stride as usize * height as usize)
327+
};
328+
for (dst, src) in new_src
329+
.chunks_exact_mut(width as usize * 4)
330+
.zip(src_slice.chunks_exact(rgba16_stride as usize))
331+
{
332+
let src = &src[0..width as usize * 4 * 2];
333+
let dst = &mut dst[0..width as usize * 4];
334+
for (dst, src) in dst.iter_mut().zip(src.chunks_exact(2)) {
335+
*dst = u16::from_ne_bytes([src[0], src[1]]);
336+
}
337+
}
338+
339+
work_on_transmuted_ptr_f16(
340+
rgba_f16,
341+
rgba_f16_stride,
342+
width as usize,
343+
height as usize,
344+
false,
345+
|dst: &mut [f16], dst_stride: usize| {
346+
convert_rgba16_to_f16(
347+
&new_src,
348+
width as usize * 4,
349+
dst,
350+
dst_stride,
351+
bit_depth,
352+
width as usize,
353+
height as usize,
354+
)
355+
.unwrap();
356+
},
357+
);
358+
} else {
359+
work_on_transmuted_ptr_u16(
360+
rgba16,
361+
rgba16_stride,
362+
width as usize,
363+
height as usize,
364+
true,
365+
|src: &mut [u16], src_stride: usize| {
366+
work_on_transmuted_ptr_f16(
367+
rgba_f16,
368+
rgba_f16_stride,
369+
width as usize,
370+
height as usize,
371+
false,
372+
|dst: &mut [f16], dst_stride: usize| {
373+
convert_rgba16_to_f16(
374+
src,
375+
src_stride,
376+
dst,
377+
dst_stride,
378+
bit_depth,
379+
width as usize,
380+
height as usize,
381+
)
382+
.unwrap();
383+
},
384+
);
385+
},
386+
);
387+
}
388+
}
389+
390+
#[cfg(test)]
391+
mod tests {
392+
use super::*;
393+
use core::f16;
394+
#[test]
395+
fn test_u16_f16() {
396+
let width = 16usize;
397+
let height = 16usize;
398+
let origin = vec![512; width * height * 4];
399+
let mut dst: Vec<f16> = vec![0.; width * height * 4];
400+
weave_cvt_rgba16_to_rgba_f16(
401+
origin.as_ptr(),
402+
width as u32 * 4 * 2,
403+
10,
404+
dst.as_mut_ptr() as *mut _,
405+
width as u32 * 4 * 2,
406+
width as u32,
407+
height as u32,
408+
);
409+
let v_src = origin
410+
.iter()
411+
.map(|&x| (x as f32 / 1023f32) as f16)
412+
.collect::<Vec<f16>>();
413+
assert_eq!(dst, v_src);
414+
415+
let mut origin = vec![512; width * height * 4];
416+
weave_cvt_rgba16_to_rgba_f16(
417+
origin.as_ptr(),
418+
width as u32 * 4 * 2,
419+
10,
420+
origin.as_mut_ptr() as *mut _,
421+
width as u32 * 4 * 2,
422+
width as u32,
423+
height as u32,
424+
);
425+
let v_src = origin
426+
.iter()
427+
.map(|&x| f16::from_bits(x))
428+
.collect::<Vec<f16>>();
429+
assert_eq!(dst, v_src);
430+
}
431+
432+
#[test]
433+
fn test_u16_f16_12() {
434+
let width = 16usize;
435+
let height = 16usize;
436+
let origin = vec![4095; width * height * 4];
437+
let mut dst: Vec<f16> = vec![0.; width * height * 4];
438+
weave_cvt_rgba16_to_rgba_f16(
439+
origin.as_ptr(),
440+
width as u32 * 4 * 2,
441+
12,
442+
dst.as_mut_ptr() as *mut _,
443+
width as u32 * 4 * 2,
444+
width as u32,
445+
height as u32,
446+
);
447+
let v_src = origin
448+
.iter()
449+
.map(|&x| (x as f32 / 4095f32) as f16)
450+
.collect::<Vec<f16>>();
451+
assert_eq!(dst, v_src);
452+
453+
let mut origin = vec![512; width * height * 4];
454+
weave_cvt_rgba16_to_rgba_f16(
455+
origin.as_ptr(),
456+
width as u32 * 4 * 2,
457+
12,
458+
origin.as_mut_ptr() as *mut _,
459+
width as u32 * 4 * 2,
460+
width as u32,
461+
height as u32,
462+
);
463+
let v_src = origin
464+
.iter()
465+
.map(|&x| f16::from_bits(x))
466+
.collect::<Vec<f16>>();
467+
assert_eq!(dst, v_src);
468+
}
351469
}

0 commit comments

Comments
 (0)