1+ //
2+ // Created by Radzivon Bartoshyk on 16/09/2023.
3+ //
4+
5+ #include < android/log.h>
6+ #include " IccRecognizer.h"
7+ #include " LinearExtendedRec2020.h"
8+
9+ void RecognizeICC (heif_image_handle* handle,
10+ heif_image *image,
11+ std::vector<uint8_t > &iccProfile,
12+ std::string &colorSpaceName) {
13+
14+ heif_color_profile_nclx *colorProfileNclx = nullptr ;
15+ auto type = heif_image_get_color_profile_type (image);
16+
17+ auto nclxColorProfile = heif_image_handle_get_nclx_color_profile (handle, &colorProfileNclx);
18+
19+ if (nclxColorProfile.code == heif_error_Ok) {
20+ if (colorProfileNclx && colorProfileNclx->color_primaries != 0 &&
21+ colorProfileNclx->transfer_characteristics != 0 ) {
22+ auto transfer = colorProfileNclx->transfer_characteristics ;
23+ auto colorPrimaries = colorProfileNclx->color_primaries ;
24+ if (colorPrimaries == heif_color_primaries_ITU_R_BT_2020_2_and_2100_0 &&
25+ transfer == heif_transfer_characteristic_ITU_R_BT_2100_0_PQ) {
26+ colorSpaceName = " BT2020_PQ" ;
27+ } else if (colorPrimaries == heif_color_primaries_ITU_R_BT_709_5 &&
28+ transfer == heif_transfer_characteristic_linear) {
29+ colorSpaceName = " LINEAR_SRGB" ;
30+ } else if (colorPrimaries == heif_color_primaries_ITU_R_BT_2020_2_and_2100_0 &&
31+ transfer == heif_transfer_characteristic_ITU_R_BT_2100_0_HLG) {
32+ colorSpaceName = " BT2020_HLG" ;
33+ } else if (colorPrimaries == heif_color_primaries_ITU_R_BT_709_5 &&
34+ transfer == heif_transfer_characteristic_ITU_R_BT_709_5) {
35+ colorSpaceName = " BT709" ;
36+ } else if (colorPrimaries == heif_color_primaries_ITU_R_BT_2020_2_and_2100_0 &&
37+ transfer == heif_transfer_characteristic_linear) {
38+ iccProfile.resize (sizeof (linearExtendedBT2020));
39+ std::copy (&linearExtendedBT2020[0 ],
40+ &linearExtendedBT2020[0 ] + sizeof (linearExtendedBT2020),
41+ iccProfile.begin ());
42+ } else if (colorPrimaries == heif_color_primaries_ITU_R_BT_2020_2_and_2100_0 &&
43+ (transfer == heif_transfer_characteristic_ITU_R_BT_2020_2_10bit ||
44+ transfer == heif_transfer_characteristic_ITU_R_BT_2020_2_12bit)) {
45+ colorSpaceName = " BT2020" ;
46+ } else if (colorPrimaries == heif_color_primaries_SMPTE_EG_432_1 &&
47+ transfer == heif_transfer_characteristic_ITU_R_BT_2100_0_HLG) {
48+ colorSpaceName = " DISPLAY_P3_HLG" ;
49+ } else if (colorPrimaries == heif_color_primaries_SMPTE_EG_432_1 &&
50+ transfer == heif_transfer_characteristic_ITU_R_BT_2100_0_PQ) {
51+ colorSpaceName = " DISPLAY_P3_PQ" ;
52+ } else if (colorPrimaries == heif_color_primaries_SMPTE_EG_432_1 &&
53+ transfer == heif_transfer_characteristic_IEC_61966_2_1) {
54+ colorSpaceName = " DISPLAY_P3" ;
55+ } else if (colorPrimaries == heif_color_primaries_ITU_R_BT_2020_2_and_2100_0) {
56+ colorSpaceName = " BT2020" ;
57+ }
58+ }
59+ } else if (type == heif_color_profile_type_prof || type == heif_color_profile_type_rICC) {
60+ auto profileSize = heif_image_get_raw_color_profile_size (image);
61+ if (profileSize > 0 ) {
62+ iccProfile.resize (profileSize);
63+ auto iccStatus = heif_image_get_raw_color_profile (image, iccProfile.data ());
64+ if (iccStatus.code != heif_error_Ok) {
65+ if (iccStatus.message ) {
66+ __android_log_print (ANDROID_LOG_ERROR, " AVIF" ,
67+ " ICC profile retrieving failed with: %s" ,
68+ iccStatus.message );
69+ } else {
70+ __android_log_print (ANDROID_LOG_ERROR, " AVIF" ,
71+ " ICC profile retrieving failed with unknown error" );
72+ }
73+ }
74+ }
75+ }
76+ }
0 commit comments