Skip to content

Commit 9d86b72

Browse files
committed
Reworked color profiles, decoding and embedding
1 parent 965e74f commit 9d86b72

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+5146
-5171
lines changed

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

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -93,58 +93,41 @@ class MainActivity : AppCompatActivity() {
9393

9494
// HDR EXAMPLES - https://us.zonerama.com/williamskeaguidingphotography/Photo/1000120226/1004888131
9595
lifecycleScope.launch(Dispatchers.IO) {
96-
val buffer = this@MainActivity.assets.open("test_2.jpg").source().buffer().readByteArray()
97-
var bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.size)
96+
val coder = HeifCoder()
97+
val allFiles1 = getAllFilesFromAssets().filter { it.contains(".avif") || it.contains(".heic") }
98+
val allFiles2 = getAllFilesFromAssets(path = "hdr").filter { it.contains(".avif") || it.contains(".heic") }
99+
var allFiles = mutableListOf<String>()
100+
allFiles.addAll(allFiles2)
101+
allFiles.addAll(allFiles1)
102+
for (file in allFiles) {
103+
try {
104+
Log.d("AVIF", "start processing $file")
105+
val buffer = this@MainActivity.assets.open(file).source().buffer()
106+
.readByteArray()
107+
val size = coder.getSize(buffer)
108+
if (size != null) {
109+
val bitmap = coder.decodeSampled(
110+
buffer,
111+
if (size.width > 1800 || size.height > 1800) size.width / 2 else size.width,
112+
if (size.width > 1800 || size.height > 1800) size.height / 2 else size.height,
113+
PreferredColorConfig.RGBA_8888,
114+
ScaleMode.RESIZE
115+
)
116+
coder.encodeAvif(bitmap)
117+
lifecycleScope.launch(Dispatchers.Main) {
118+
val imageView = BindingImageViewBinding.inflate(layoutInflater, binding.scrollViewContainer, false)
119+
imageView.root.setImageBitmap(bitmap)
120+
binding.scrollViewContainer.addView(imageView.root)
121+
}
122+
}
123+
} catch (e: Exception) {
124+
if (e is FileNotFoundException || e is java.io.FileNotFoundException) {
98125

99-
bitmap = bitmap.scale(bitmap.width / 2, bitmap.height / 2)
100-
.copy(Bitmap.Config.RGBA_1010102, true)
101-
lifecycleScope.launch(Dispatchers.Main) {
102-
val imageView = BindingImageViewBinding.inflate(layoutInflater, binding.scrollViewContainer, false)
103-
imageView.root.setImageBitmap(bitmap)
104-
binding.scrollViewContainer.addView(imageView.root)
105-
}
106-
val coder = HeifCoder(null, toneMapper = ToneMapper.LOGARITHMIC)
107-
val avif = coder.encodeAvif(bitmap)
108-
val decodedAvif = coder.decode(avif)
109-
lifecycleScope.launch(Dispatchers.Main) {
110-
val imageView = BindingImageViewBinding.inflate(layoutInflater, binding.scrollViewContainer, false)
111-
imageView.root.setImageBitmap(decodedAvif)
112-
binding.scrollViewContainer.addView(imageView.root)
126+
} else {
127+
throw e
128+
}
129+
}
113130
}
114-
// val allFiles1 = getAllFilesFromAssets().filter { it.contains(".avif") || it.contains(".heic") }
115-
// val allFiles2 = getAllFilesFromAssets(path = "hdr").filter { it.contains(".avif") || it.contains(".heic") }
116-
// var allFiles = mutableListOf<String>()
117-
// allFiles.addAll(allFiles2)
118-
// allFiles.addAll(allFiles1)
119-
// for (file in allFiles) {
120-
// try {
121-
// Log.d("AVIF", "start processing $file")
122-
// val buffer = this@MainActivity.assets.open(file).source().buffer()
123-
// .readByteArray()
124-
// val size = coder.getSize(buffer)
125-
// if (size != null) {
126-
// val bitmap = coder.decodeSampled(
127-
// buffer,
128-
// if (size.width > 1800 || size.height > 1800) size.width / 2 else size.width,
129-
// if (size.width > 1800 || size.height > 1800) size.height / 2 else size.height,
130-
// PreferredColorConfig.RGBA_8888,
131-
// ScaleMode.RESIZE
132-
// )
133-
// coder.encodeAvif(bitmap)
134-
// lifecycleScope.launch(Dispatchers.Main) {
135-
// val imageView = BindingImageViewBinding.inflate(layoutInflater, binding.scrollViewContainer, false)
136-
// imageView.root.setImageBitmap(bitmap)
137-
// binding.scrollViewContainer.addView(imageView.root)
138-
// }
139-
// }
140-
// } catch (e: Exception) {
141-
// if (e is FileNotFoundException || e is java.io.FileNotFoundException) {
142-
//
143-
// } else {
144-
// throw e
145-
// }
146-
// }
147-
// }
148131
}
149132

150133
// https://wh.aimuse.online/creatives/IMUSE_03617fe2db82a584166_27/TT_a9d21ff1061d785347935fef/68f06252.avif

avif-coder/src/main/cpp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ add_library(coder SHARED coder.cpp JniException.cpp SizeScaler.cpp
1616
icc/cmsps2.c icc/cmssamp.c icc/cmssm.c icc/cmstypes.c icc/cmsvirt.c
1717
icc/cmswtpnt.c icc/cmsxform.c colorspace/colorspace.cpp
1818
imagebits/RgbaF16bitToNBitU16.cpp imagebits/RgbaF16bitNBitU8.cpp imagebits/Rgb1010102.cpp
19-
colorspace/HDRTransferAdapter.cpp
19+
colorspace/GamutAdapter.cpp
2020
imagebits/CopyUnalignedRGBA.cpp JniDecoder.cpp imagebits/Rgba8ToF16.cpp
2121
imagebits/Rgb565.cpp JniBitmap.cpp ReformatBitmap.cpp Support.cpp IccRecognizer.cpp
2222
HardwareBuffersCompat.cpp imagebits/half.cpp

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

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -46,67 +46,67 @@ AHardwareBufferToHardwareBufferFunc AHardwareBuffer_toHardwareBuffer_compat = nu
4646
AHardwareBufferDescribeFunc AHardwareBuffer_describe_compat = nullptr;
4747

4848
bool loadAHardwareBuffersAPI() {
49-
// Bitmap doesn't support wrapping before 29 API
50-
if (androidOSVersion() < 29) {
51-
return false;
52-
}
53-
std::lock_guard guard(dlMutex);
54-
if (alreadyHdBuffersLoaded) {
55-
return AHardwareBuffer_allocate_compat != nullptr
56-
&& AHardwareBuffer_isSupported_compat != nullptr
57-
&& AHardwareBuffer_unlock_compat != nullptr
58-
&& AHardwareBuffer_release_compat != nullptr
59-
&& AHardwareBuffer_lock_compat != nullptr
60-
&& AHardwareBuffer_toHardwareBuffer_compat != nullptr
61-
&& AHardwareBuffer_describe_compat != nullptr;
62-
}
63-
alreadyHdBuffersLoaded = true;
64-
void *hhl = dlopen("libandroid.so", RTLD_NOW);
65-
if (!hhl) {
66-
return false;
67-
}
49+
// Bitmap doesn't support wrapping before 29 API
50+
if (androidOSVersion() < 29) {
51+
return false;
52+
}
53+
std::lock_guard guard(dlMutex);
54+
if (alreadyHdBuffersLoaded) {
55+
return AHardwareBuffer_allocate_compat != nullptr
56+
&& AHardwareBuffer_isSupported_compat != nullptr
57+
&& AHardwareBuffer_unlock_compat != nullptr
58+
&& AHardwareBuffer_release_compat != nullptr
59+
&& AHardwareBuffer_lock_compat != nullptr
60+
&& AHardwareBuffer_toHardwareBuffer_compat != nullptr
61+
&& AHardwareBuffer_describe_compat != nullptr;
62+
}
63+
alreadyHdBuffersLoaded = true;
64+
void *hhl = dlopen("libandroid.so", RTLD_NOW);
65+
if (!hhl) {
66+
return false;
67+
}
6868

69-
AHardwareBuffer_allocate_compat = (AHardwareBufferAllocateFunc) dlsym(hhl,
70-
"AHardwareBuffer_allocate");
71-
if (AHardwareBuffer_allocate_compat == nullptr) {
72-
return false;
73-
}
69+
AHardwareBuffer_allocate_compat = (AHardwareBufferAllocateFunc) dlsym(hhl,
70+
"AHardwareBuffer_allocate");
71+
if (AHardwareBuffer_allocate_compat == nullptr) {
72+
return false;
73+
}
7474

75-
AHardwareBuffer_isSupported_compat = (AHardwareBufferIsSupportedFunc) dlsym(hhl,
76-
"AHardwareBuffer_isSupported");
77-
if (AHardwareBuffer_isSupported_compat == nullptr) {
78-
return false;
79-
}
75+
AHardwareBuffer_isSupported_compat = (AHardwareBufferIsSupportedFunc) dlsym(hhl,
76+
"AHardwareBuffer_isSupported");
77+
if (AHardwareBuffer_isSupported_compat == nullptr) {
78+
return false;
79+
}
8080

81-
AHardwareBuffer_unlock_compat = (AHardwareBufferUnlockFunc) dlsym(hhl,
82-
"AHardwareBuffer_unlock");
83-
if (AHardwareBuffer_unlock_compat == nullptr) {
84-
return false;
85-
}
81+
AHardwareBuffer_unlock_compat = (AHardwareBufferUnlockFunc) dlsym(hhl,
82+
"AHardwareBuffer_unlock");
83+
if (AHardwareBuffer_unlock_compat == nullptr) {
84+
return false;
85+
}
8686

87-
AHardwareBuffer_release_compat = (AHardwareBufferReleaseFunc) dlsym(hhl,
88-
"AHardwareBuffer_release");
89-
if (AHardwareBuffer_release_compat == nullptr) {
90-
return false;
91-
}
87+
AHardwareBuffer_release_compat = (AHardwareBufferReleaseFunc) dlsym(hhl,
88+
"AHardwareBuffer_release");
89+
if (AHardwareBuffer_release_compat == nullptr) {
90+
return false;
91+
}
9292

93-
AHardwareBuffer_lock_compat = (AHardwareBufferLockFunc) dlsym(hhl,
94-
"AHardwareBuffer_lock");
95-
if (AHardwareBuffer_lock_compat == nullptr) {
96-
return false;
97-
}
93+
AHardwareBuffer_lock_compat = (AHardwareBufferLockFunc) dlsym(hhl,
94+
"AHardwareBuffer_lock");
95+
if (AHardwareBuffer_lock_compat == nullptr) {
96+
return false;
97+
}
9898

99-
AHardwareBuffer_toHardwareBuffer_compat = (AHardwareBufferToHardwareBufferFunc) dlsym(hhl,
100-
"AHardwareBuffer_toHardwareBuffer");
101-
if (AHardwareBuffer_toHardwareBuffer_compat == nullptr) {
102-
return false;
103-
}
99+
AHardwareBuffer_toHardwareBuffer_compat = (AHardwareBufferToHardwareBufferFunc) dlsym(hhl,
100+
"AHardwareBuffer_toHardwareBuffer");
101+
if (AHardwareBuffer_toHardwareBuffer_compat == nullptr) {
102+
return false;
103+
}
104104

105-
AHardwareBuffer_describe_compat = (AHardwareBufferDescribeFunc) dlsym(hhl,
106-
"AHardwareBuffer_describe");
107-
if (AHardwareBuffer_describe_compat == nullptr) {
108-
return false;
109-
}
105+
AHardwareBuffer_describe_compat = (AHardwareBufferDescribeFunc) dlsym(hhl,
106+
"AHardwareBuffer_describe");
107+
if (AHardwareBuffer_describe_compat == nullptr) {
108+
return false;
109+
}
110110

111-
return true;
111+
return true;
112112
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,32 @@
3636
bool loadAHardwareBuffersAPI();
3737

3838
typedef int (*AHardwareBufferAllocateFunc)(
39-
const AHardwareBuffer_Desc *_Nonnull desc, AHardwareBuffer *_Nullable *_Nonnull outBuffer
39+
const AHardwareBuffer_Desc *_Nonnull desc, AHardwareBuffer *_Nullable *_Nonnull outBuffer
4040
);
4141

4242
typedef int (*AHardwareBufferIsSupportedFunc)(
43-
const AHardwareBuffer_Desc *_Nonnull desc
43+
const AHardwareBuffer_Desc *_Nonnull desc
4444
);
4545

4646
typedef int (*AHardwareBufferReleaseFunc)(
47-
AHardwareBuffer *_Nonnull buffer
47+
AHardwareBuffer *_Nonnull buffer
4848
);
4949

5050
typedef int (*AHardwareBufferUnlockFunc)(
51-
AHardwareBuffer *_Nonnull buffer, int32_t *_Nullable fence
51+
AHardwareBuffer *_Nonnull buffer, int32_t *_Nullable fence
5252
);
5353

5454
typedef int (*AHardwareBufferLockFunc)(
55-
AHardwareBuffer *_Nonnull buffer, uint64_t usage, int32_t fence,
56-
const ARect *_Nullable rect, void *_Nullable *_Nonnull outVirtualAddress
55+
AHardwareBuffer *_Nonnull buffer, uint64_t usage, int32_t fence,
56+
const ARect *_Nullable rect, void *_Nullable *_Nonnull outVirtualAddress
5757
);
5858

5959
typedef void (*AHardwareBufferDescribeFunc)(
60-
const AHardwareBuffer *_Nonnull buffer, AHardwareBuffer_Desc *_Nonnull outDesc
60+
const AHardwareBuffer *_Nonnull buffer, AHardwareBuffer_Desc *_Nonnull outDesc
6161
);
6262

6363
typedef jobject (*AHardwareBufferToHardwareBufferFunc)(
64-
JNIEnv *env, AHardwareBuffer *hardwareBuffer
64+
JNIEnv *env, AHardwareBuffer *hardwareBuffer
6565
);
6666

6767
extern AHardwareBufferAllocateFunc AHardwareBuffer_allocate_compat;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void RecognizeICC(std::shared_ptr<heif_image_handle> &handle,
7070
transfer == heif_transfer_characteristic_ITU_R_BT_2100_0_PQ) {
7171
colorSpaceName = "DISPLAY_P3_PQ";
7272
} else if (colorPrimaries == heif_color_primaries_SMPTE_EG_432_1 &&
73-
transfer == heif_transfer_characteristic_IEC_61966_2_1) {
73+
transfer == heif_transfer_characteristic_ITU_R_BT_709_5) {
7474
colorSpaceName = "DISPLAY_P3";
7575
} else if (colorPrimaries == heif_color_primaries_ITU_R_BT_2020_2_and_2100_0) {
7676
colorSpaceName = "BT2020";

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
#include <string>
3333
#include "heif.h"
3434

35-
void RecognizeICC(std::shared_ptr<heif_image_handle>& handle,
36-
std::shared_ptr<heif_image>& image,
35+
void RecognizeICC(std::shared_ptr<heif_image_handle> &handle,
36+
std::shared_ptr<heif_image> &image,
3737
std::vector<uint8_t> &iccProfile,
3838
std::string &colorSpaceName,
3939
heif_color_profile_nclx **colorProfileNclx,
40-
bool* hasNclx);
40+
bool *hasNclx);
4141

4242
#endif //AVIF_ICCRECOGNIZER_H

0 commit comments

Comments
 (0)