Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <jpegrecoverymap/recoverymap.h> |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 18 | #include <jpegrecoverymap/jpegencoder.h> |
| 19 | #include <jpegrecoverymap/jpegdecoder.h> |
| 20 | #include <jpegrecoverymap/recoverymapmath.h> |
| 21 | |
Dichen Zhang | a876626 | 2022-11-07 23:48:24 +0000 | [diff] [blame] | 22 | #include <image_io/jpeg/jpeg_marker.h> |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 23 | #include <image_io/xml/xml_writer.h> |
| 24 | |
| 25 | #include <memory> |
Dichen Zhang | 72fd2b1 | 2022-11-01 06:11:50 +0000 | [diff] [blame] | 26 | #include <sstream> |
| 27 | #include <string> |
| 28 | |
| 29 | using namespace std; |
Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android::recoverymap { |
| 32 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 33 | #define JPEGR_CHECK(x) \ |
| 34 | { \ |
| 35 | status_t status = (x); \ |
| 36 | if ((status) != NO_ERROR) { \ |
| 37 | return status; \ |
| 38 | } \ |
| 39 | } |
| 40 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 41 | // The current JPEGR version that we encode to |
| 42 | static const uint32_t kJpegrVersion = 1; |
| 43 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 44 | // Map is quarter res / sixteenth size |
| 45 | static const size_t kMapDimensionScaleFactor = 4; |
Dichen Zhang | 0b9f7de | 2022-11-18 06:52:46 +0000 | [diff] [blame^] | 46 | // JPEG compress quality (0 ~ 100) for recovery map |
| 47 | static const int kMapCompressQuality = 85; |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 48 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 49 | // TODO: fill in st2086 metadata |
| 50 | static const st2086_metadata kSt2086Metadata = { |
| 51 | {0.0f, 0.0f}, |
| 52 | {0.0f, 0.0f}, |
| 53 | {0.0f, 0.0f}, |
| 54 | {0.0f, 0.0f}, |
| 55 | 0, |
| 56 | 1.0f, |
| 57 | }; |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 58 | |
Dichen Zhang | 72fd2b1 | 2022-11-01 06:11:50 +0000 | [diff] [blame] | 59 | /* |
| 60 | * Helper function used for generating XMP metadata. |
| 61 | * |
| 62 | * @param prefix The prefix part of the name. |
| 63 | * @param suffix The suffix part of the name. |
| 64 | * @return A name of the form "prefix:suffix". |
| 65 | */ |
| 66 | string Name(const string &prefix, const string &suffix) { |
| 67 | std::stringstream ss; |
| 68 | ss << prefix << ":" << suffix; |
| 69 | return ss.str(); |
| 70 | } |
| 71 | |
Dichen Zhang | a876626 | 2022-11-07 23:48:24 +0000 | [diff] [blame] | 72 | /* |
| 73 | * Helper function used for writing data to destination. |
| 74 | * |
| 75 | * @param destination destination of the data to be written. |
| 76 | * @param source source of data being written. |
| 77 | * @param length length of the data to be written. |
| 78 | * @param position cursor in desitination where the data is to be written. |
| 79 | * @return status of succeed or error code. |
| 80 | */ |
| 81 | status_t Write(jr_compressed_ptr destination, const void* source, size_t length, int &position) { |
Dichen Zhang | 0b9f7de | 2022-11-18 06:52:46 +0000 | [diff] [blame^] | 82 | if (position + length > destination->maxLength) { |
Dichen Zhang | a876626 | 2022-11-07 23:48:24 +0000 | [diff] [blame] | 83 | return ERROR_JPEGR_BUFFER_TOO_SMALL; |
| 84 | } |
| 85 | |
| 86 | memcpy((uint8_t*)destination->data + sizeof(uint8_t) * position, source, length); |
| 87 | position += length; |
| 88 | return NO_ERROR; |
| 89 | } |
| 90 | |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 91 | status_t RecoveryMap::encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image, |
| 92 | jr_uncompressed_ptr uncompressed_yuv_420_image, |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 93 | jpegr_transfer_function hdr_tf, |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 94 | jr_compressed_ptr dest, |
Dichen Zhang | ffa3401 | 2022-11-03 23:21:13 +0000 | [diff] [blame] | 95 | int quality, |
Dichen Zhang | 95cbb9f | 2022-11-07 18:32:05 +0000 | [diff] [blame] | 96 | jr_exif_ptr /* exif */) { |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 97 | if (uncompressed_p010_image == nullptr |
| 98 | || uncompressed_yuv_420_image == nullptr |
| 99 | || dest == nullptr) { |
Dichen Zhang | 80b7248 | 2022-11-02 01:55:35 +0000 | [diff] [blame] | 100 | return ERROR_JPEGR_INVALID_NULL_PTR; |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Dichen Zhang | ffa3401 | 2022-11-03 23:21:13 +0000 | [diff] [blame] | 103 | if (quality < 0 || quality > 100) { |
| 104 | return ERROR_JPEGR_INVALID_INPUT_TYPE; |
| 105 | } |
| 106 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 107 | if (uncompressed_p010_image->width != uncompressed_yuv_420_image->width |
| 108 | || uncompressed_p010_image->height != uncompressed_yuv_420_image->height) { |
| 109 | return ERROR_JPEGR_RESOLUTION_MISMATCH; |
| 110 | } |
| 111 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 112 | jpegr_metadata metadata; |
| 113 | metadata.version = kJpegrVersion; |
| 114 | metadata.transferFunction = hdr_tf; |
| 115 | if (hdr_tf == JPEGR_TF_PQ) { |
| 116 | metadata.hdr10Metadata.st2086Metadata = kSt2086Metadata; |
| 117 | } |
| 118 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 119 | jpegr_uncompressed_struct map; |
Dichen Zhang | a876626 | 2022-11-07 23:48:24 +0000 | [diff] [blame] | 120 | JPEGR_CHECK(generateRecoveryMap( |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 121 | uncompressed_yuv_420_image, uncompressed_p010_image, &metadata, &map)); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 122 | std::unique_ptr<uint8_t[]> map_data; |
| 123 | map_data.reset(reinterpret_cast<uint8_t*>(map.data)); |
| 124 | |
| 125 | jpegr_compressed_struct compressed_map; |
Dichen Zhang | 0b9f7de | 2022-11-18 06:52:46 +0000 | [diff] [blame^] | 126 | compressed_map.maxLength = map.width * map.height; |
| 127 | unique_ptr<uint8_t[]> compressed_map_data = make_unique<uint8_t[]>(compressed_map.maxLength); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 128 | compressed_map.data = compressed_map_data.get(); |
| 129 | JPEGR_CHECK(compressRecoveryMap(&map, &compressed_map)); |
| 130 | |
| 131 | JpegEncoder jpeg_encoder; |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 132 | // TODO: determine ICC data based on color gamut information |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 133 | if (!jpeg_encoder.compressImage(uncompressed_yuv_420_image->data, |
| 134 | uncompressed_yuv_420_image->width, |
Dichen Zhang | 95cbb9f | 2022-11-07 18:32:05 +0000 | [diff] [blame] | 135 | uncompressed_yuv_420_image->height, quality, nullptr, 0)) { |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 136 | return ERROR_JPEGR_ENCODE_ERROR; |
| 137 | } |
| 138 | jpegr_compressed_struct jpeg; |
| 139 | jpeg.data = jpeg_encoder.getCompressedImagePtr(); |
| 140 | jpeg.length = jpeg_encoder.getCompressedImageSize(); |
| 141 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 142 | JPEGR_CHECK(appendRecoveryMap(&jpeg, &compressed_map, &metadata, dest)); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 143 | |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 144 | return NO_ERROR; |
| 145 | } |
| 146 | |
| 147 | status_t RecoveryMap::encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image, |
| 148 | jr_uncompressed_ptr uncompressed_yuv_420_image, |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 149 | jr_compressed_ptr compressed_jpeg_image, |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 150 | jpegr_transfer_function hdr_tf, |
Dichen Zhang | 95cbb9f | 2022-11-07 18:32:05 +0000 | [diff] [blame] | 151 | jr_compressed_ptr dest) { |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 152 | if (uncompressed_p010_image == nullptr |
| 153 | || uncompressed_yuv_420_image == nullptr |
| 154 | || compressed_jpeg_image == nullptr |
| 155 | || dest == nullptr) { |
Dichen Zhang | 80b7248 | 2022-11-02 01:55:35 +0000 | [diff] [blame] | 156 | return ERROR_JPEGR_INVALID_NULL_PTR; |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 159 | if (uncompressed_p010_image->width != uncompressed_yuv_420_image->width |
| 160 | || uncompressed_p010_image->height != uncompressed_yuv_420_image->height) { |
| 161 | return ERROR_JPEGR_RESOLUTION_MISMATCH; |
| 162 | } |
| 163 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 164 | jpegr_metadata metadata; |
| 165 | metadata.version = kJpegrVersion; |
| 166 | metadata.transferFunction = hdr_tf; |
| 167 | if (hdr_tf == JPEGR_TF_PQ) { |
| 168 | metadata.hdr10Metadata.st2086Metadata = kSt2086Metadata; |
| 169 | } |
| 170 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 171 | jpegr_uncompressed_struct map; |
Dichen Zhang | a876626 | 2022-11-07 23:48:24 +0000 | [diff] [blame] | 172 | JPEGR_CHECK(generateRecoveryMap( |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 173 | uncompressed_yuv_420_image, uncompressed_p010_image, &metadata, &map)); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 174 | std::unique_ptr<uint8_t[]> map_data; |
| 175 | map_data.reset(reinterpret_cast<uint8_t*>(map.data)); |
| 176 | |
| 177 | jpegr_compressed_struct compressed_map; |
Dichen Zhang | 0b9f7de | 2022-11-18 06:52:46 +0000 | [diff] [blame^] | 178 | compressed_map.maxLength = map.width * map.height; |
| 179 | unique_ptr<uint8_t[]> compressed_map_data = make_unique<uint8_t[]>(compressed_map.maxLength); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 180 | compressed_map.data = compressed_map_data.get(); |
| 181 | JPEGR_CHECK(compressRecoveryMap(&map, &compressed_map)); |
| 182 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 183 | JPEGR_CHECK(appendRecoveryMap(compressed_jpeg_image, &compressed_map, &metadata, dest)); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 184 | |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 185 | return NO_ERROR; |
| 186 | } |
| 187 | |
| 188 | status_t RecoveryMap::encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image, |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 189 | jr_compressed_ptr compressed_jpeg_image, |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 190 | jpegr_transfer_function hdr_tf, |
Dichen Zhang | 95cbb9f | 2022-11-07 18:32:05 +0000 | [diff] [blame] | 191 | jr_compressed_ptr dest) { |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 192 | if (uncompressed_p010_image == nullptr |
| 193 | || compressed_jpeg_image == nullptr |
| 194 | || dest == nullptr) { |
Dichen Zhang | 80b7248 | 2022-11-02 01:55:35 +0000 | [diff] [blame] | 195 | return ERROR_JPEGR_INVALID_NULL_PTR; |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 198 | JpegDecoder jpeg_decoder; |
| 199 | if (!jpeg_decoder.decompressImage(compressed_jpeg_image->data, compressed_jpeg_image->length)) { |
| 200 | return ERROR_JPEGR_DECODE_ERROR; |
| 201 | } |
| 202 | jpegr_uncompressed_struct uncompressed_yuv_420_image; |
| 203 | uncompressed_yuv_420_image.data = jpeg_decoder.getDecompressedImagePtr(); |
| 204 | uncompressed_yuv_420_image.width = jpeg_decoder.getDecompressedImageWidth(); |
| 205 | uncompressed_yuv_420_image.height = jpeg_decoder.getDecompressedImageHeight(); |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 206 | uncompressed_yuv_420_image.colorGamut = compressed_jpeg_image->colorGamut; |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 207 | |
| 208 | if (uncompressed_p010_image->width != uncompressed_yuv_420_image.width |
| 209 | || uncompressed_p010_image->height != uncompressed_yuv_420_image.height) { |
| 210 | return ERROR_JPEGR_RESOLUTION_MISMATCH; |
| 211 | } |
| 212 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 213 | jpegr_metadata metadata; |
| 214 | metadata.version = kJpegrVersion; |
| 215 | metadata.transferFunction = hdr_tf; |
| 216 | if (hdr_tf == JPEGR_TF_PQ) { |
| 217 | metadata.hdr10Metadata.st2086Metadata = kSt2086Metadata; |
| 218 | } |
| 219 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 220 | jpegr_uncompressed_struct map; |
Dichen Zhang | a876626 | 2022-11-07 23:48:24 +0000 | [diff] [blame] | 221 | JPEGR_CHECK(generateRecoveryMap( |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 222 | &uncompressed_yuv_420_image, uncompressed_p010_image, &metadata, &map)); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 223 | std::unique_ptr<uint8_t[]> map_data; |
| 224 | map_data.reset(reinterpret_cast<uint8_t*>(map.data)); |
| 225 | |
| 226 | jpegr_compressed_struct compressed_map; |
Dichen Zhang | 0b9f7de | 2022-11-18 06:52:46 +0000 | [diff] [blame^] | 227 | compressed_map.maxLength = map.width * map.height; |
| 228 | unique_ptr<uint8_t[]> compressed_map_data = make_unique<uint8_t[]>(compressed_map.maxLength); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 229 | compressed_map.data = compressed_map_data.get(); |
| 230 | JPEGR_CHECK(compressRecoveryMap(&map, &compressed_map)); |
| 231 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 232 | JPEGR_CHECK(appendRecoveryMap(compressed_jpeg_image, &compressed_map, &metadata, dest)); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 233 | |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 234 | return NO_ERROR; |
| 235 | } |
| 236 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 237 | status_t RecoveryMap::decodeJPEGR(jr_compressed_ptr compressed_jpegr_image, |
Dichen Zhang | ffa3401 | 2022-11-03 23:21:13 +0000 | [diff] [blame] | 238 | jr_uncompressed_ptr dest, |
| 239 | jr_exif_ptr /* exif */, |
| 240 | bool /* request_sdr */) { |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 241 | if (compressed_jpegr_image == nullptr || dest == nullptr) { |
Dichen Zhang | 80b7248 | 2022-11-02 01:55:35 +0000 | [diff] [blame] | 242 | return ERROR_JPEGR_INVALID_NULL_PTR; |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 245 | jpegr_compressed_struct compressed_map; |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 246 | jpegr_metadata metadata; |
| 247 | JPEGR_CHECK(extractRecoveryMap(compressed_jpegr_image, &compressed_map, &metadata)); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 248 | |
| 249 | jpegr_uncompressed_struct map; |
| 250 | JPEGR_CHECK(decompressRecoveryMap(&compressed_map, &map)); |
| 251 | |
| 252 | JpegDecoder jpeg_decoder; |
| 253 | if (!jpeg_decoder.decompressImage(compressed_jpegr_image->data, compressed_jpegr_image->length)) { |
| 254 | return ERROR_JPEGR_DECODE_ERROR; |
| 255 | } |
| 256 | |
| 257 | jpegr_uncompressed_struct uncompressed_yuv_420_image; |
| 258 | uncompressed_yuv_420_image.data = jpeg_decoder.getDecompressedImagePtr(); |
| 259 | uncompressed_yuv_420_image.width = jpeg_decoder.getDecompressedImageWidth(); |
| 260 | uncompressed_yuv_420_image.height = jpeg_decoder.getDecompressedImageHeight(); |
| 261 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 262 | JPEGR_CHECK(applyRecoveryMap(&uncompressed_yuv_420_image, &map, &metadata, dest)); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 263 | |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 264 | return NO_ERROR; |
| 265 | } |
| 266 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 267 | status_t RecoveryMap::decompressRecoveryMap(jr_compressed_ptr compressed_recovery_map, |
| 268 | jr_uncompressed_ptr dest) { |
Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 269 | if (compressed_recovery_map == nullptr || dest == nullptr) { |
Dichen Zhang | 80b7248 | 2022-11-02 01:55:35 +0000 | [diff] [blame] | 270 | return ERROR_JPEGR_INVALID_NULL_PTR; |
Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 273 | JpegDecoder jpeg_decoder; |
| 274 | if (!jpeg_decoder.decompressImage(compressed_recovery_map->data, |
| 275 | compressed_recovery_map->length)) { |
| 276 | return ERROR_JPEGR_DECODE_ERROR; |
| 277 | } |
| 278 | |
| 279 | dest->data = jpeg_decoder.getDecompressedImagePtr(); |
| 280 | dest->width = jpeg_decoder.getDecompressedImageWidth(); |
| 281 | dest->height = jpeg_decoder.getDecompressedImageHeight(); |
| 282 | |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 283 | return NO_ERROR; |
Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 286 | status_t RecoveryMap::compressRecoveryMap(jr_uncompressed_ptr uncompressed_recovery_map, |
| 287 | jr_compressed_ptr dest) { |
Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 288 | if (uncompressed_recovery_map == nullptr || dest == nullptr) { |
Dichen Zhang | 80b7248 | 2022-11-02 01:55:35 +0000 | [diff] [blame] | 289 | return ERROR_JPEGR_INVALID_NULL_PTR; |
Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 292 | // TODO: should we have ICC data for the map? |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 293 | JpegEncoder jpeg_encoder; |
Dichen Zhang | 0b9f7de | 2022-11-18 06:52:46 +0000 | [diff] [blame^] | 294 | if (!jpeg_encoder.compressImage(uncompressed_recovery_map->data, |
| 295 | uncompressed_recovery_map->width, |
| 296 | uncompressed_recovery_map->height, |
| 297 | kMapCompressQuality, |
| 298 | nullptr, |
| 299 | 0, |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 300 | true /* isSingleChannel */)) { |
| 301 | return ERROR_JPEGR_ENCODE_ERROR; |
| 302 | } |
| 303 | |
Dichen Zhang | 0b9f7de | 2022-11-18 06:52:46 +0000 | [diff] [blame^] | 304 | if (dest->maxLength < jpeg_encoder.getCompressedImageSize()) { |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 305 | return ERROR_JPEGR_BUFFER_TOO_SMALL; |
| 306 | } |
| 307 | |
| 308 | memcpy(dest->data, jpeg_encoder.getCompressedImagePtr(), jpeg_encoder.getCompressedImageSize()); |
| 309 | dest->length = jpeg_encoder.getCompressedImageSize(); |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 310 | dest->colorGamut = JPEGR_COLORGAMUT_UNSPECIFIED; |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 311 | |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 312 | return NO_ERROR; |
Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 315 | status_t RecoveryMap::generateRecoveryMap(jr_uncompressed_ptr uncompressed_yuv_420_image, |
| 316 | jr_uncompressed_ptr uncompressed_p010_image, |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 317 | jr_metadata_ptr metadata, |
| 318 | jr_uncompressed_ptr dest) { |
Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 319 | if (uncompressed_yuv_420_image == nullptr |
| 320 | || uncompressed_p010_image == nullptr |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 321 | || metadata == nullptr |
Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 322 | || dest == nullptr) { |
Dichen Zhang | 80b7248 | 2022-11-02 01:55:35 +0000 | [diff] [blame] | 323 | return ERROR_JPEGR_INVALID_NULL_PTR; |
Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 326 | if (uncompressed_yuv_420_image->width != uncompressed_p010_image->width |
| 327 | || uncompressed_yuv_420_image->height != uncompressed_p010_image->height) { |
| 328 | return ERROR_JPEGR_RESOLUTION_MISMATCH; |
| 329 | } |
| 330 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 331 | if (uncompressed_yuv_420_image->colorGamut == JPEGR_COLORGAMUT_UNSPECIFIED |
| 332 | || uncompressed_p010_image->colorGamut == JPEGR_COLORGAMUT_UNSPECIFIED) { |
| 333 | return ERROR_JPEGR_INVALID_COLORGAMUT; |
| 334 | } |
| 335 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 336 | size_t image_width = uncompressed_yuv_420_image->width; |
| 337 | size_t image_height = uncompressed_yuv_420_image->height; |
| 338 | size_t map_width = image_width / kMapDimensionScaleFactor; |
| 339 | size_t map_height = image_height / kMapDimensionScaleFactor; |
| 340 | |
| 341 | dest->width = map_width; |
| 342 | dest->height = map_height; |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 343 | dest->colorGamut = JPEGR_COLORGAMUT_UNSPECIFIED; |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 344 | dest->data = new uint8_t[map_width * map_height]; |
| 345 | std::unique_ptr<uint8_t[]> map_data; |
| 346 | map_data.reset(reinterpret_cast<uint8_t*>(dest->data)); |
| 347 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 348 | ColorTransformFn hdrInvOetf = nullptr; |
| 349 | switch (metadata->transferFunction) { |
| 350 | case JPEGR_TF_HLG: |
| 351 | hdrInvOetf = hlgInvOetf; |
| 352 | break; |
| 353 | case JPEGR_TF_PQ: |
| 354 | hdrInvOetf = pqInvOetf; |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | ColorTransformFn hdrGamutConversionFn = getHdrConversionFn( |
| 359 | uncompressed_yuv_420_image->colorGamut, uncompressed_p010_image->colorGamut); |
| 360 | |
| 361 | ColorCalculationFn luminanceFn = nullptr; |
| 362 | switch (uncompressed_yuv_420_image->colorGamut) { |
| 363 | case JPEGR_COLORGAMUT_BT709: |
| 364 | luminanceFn = srgbLuminance; |
| 365 | break; |
| 366 | case JPEGR_COLORGAMUT_P3: |
| 367 | luminanceFn = p3Luminance; |
| 368 | break; |
| 369 | case JPEGR_COLORGAMUT_BT2100: |
| 370 | luminanceFn = bt2100Luminance; |
| 371 | break; |
| 372 | case JPEGR_COLORGAMUT_UNSPECIFIED: |
| 373 | // Should be impossible to hit after input validation. |
| 374 | return ERROR_JPEGR_INVALID_COLORGAMUT; |
| 375 | } |
| 376 | |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 377 | float hdr_y_nits_max = 0.0f; |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 378 | double hdr_y_nits_avg = 0.0f; |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 379 | for (size_t y = 0; y < image_height; ++y) { |
| 380 | for (size_t x = 0; x < image_width; ++x) { |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 381 | Color hdr_yuv_gamma = getP010Pixel(uncompressed_p010_image, x, y); |
| 382 | Color hdr_rgb_gamma = bt2100YuvToRgb(hdr_yuv_gamma); |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 383 | Color hdr_rgb = hdrInvOetf(hdr_rgb_gamma); |
| 384 | hdr_rgb = hdrGamutConversionFn(hdr_rgb); |
| 385 | float hdr_y_nits = luminanceFn(hdr_rgb); |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 386 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 387 | hdr_y_nits_avg += hdr_y_nits; |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 388 | if (hdr_y_nits > hdr_y_nits_max) { |
| 389 | hdr_y_nits_max = hdr_y_nits; |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | } |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 393 | hdr_y_nits_avg /= image_width * image_height; |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 394 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 395 | metadata->rangeScalingFactor = hdr_y_nits_max / kSdrWhiteNits; |
| 396 | if (metadata->transferFunction == JPEGR_TF_PQ) { |
| 397 | metadata->hdr10Metadata.maxFALL = hdr_y_nits_avg; |
| 398 | metadata->hdr10Metadata.maxCLL = hdr_y_nits_max; |
| 399 | } |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 400 | |
| 401 | for (size_t y = 0; y < map_height; ++y) { |
| 402 | for (size_t x = 0; x < map_width; ++x) { |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 403 | Color sdr_yuv_gamma = sampleYuv420(uncompressed_yuv_420_image, |
| 404 | kMapDimensionScaleFactor, x, y); |
| 405 | Color sdr_rgb_gamma = srgbYuvToRgb(sdr_yuv_gamma); |
| 406 | Color sdr_rgb = srgbInvOetf(sdr_rgb_gamma); |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 407 | float sdr_y_nits = luminanceFn(sdr_rgb); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 408 | |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 409 | Color hdr_yuv_gamma = sampleP010(uncompressed_p010_image, kMapDimensionScaleFactor, x, y); |
| 410 | Color hdr_rgb_gamma = bt2100YuvToRgb(hdr_yuv_gamma); |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 411 | Color hdr_rgb = hdrInvOetf(hdr_rgb_gamma); |
| 412 | hdr_rgb = hdrGamutConversionFn(hdr_rgb); |
| 413 | float hdr_y_nits = luminanceFn(hdr_rgb); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 414 | |
| 415 | size_t pixel_idx = x + y * map_width; |
| 416 | reinterpret_cast<uint8_t*>(dest->data)[pixel_idx] = |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 417 | encodeRecovery(sdr_y_nits, hdr_y_nits, metadata->rangeScalingFactor); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
| 421 | map_data.release(); |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 422 | return NO_ERROR; |
Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 425 | status_t RecoveryMap::applyRecoveryMap(jr_uncompressed_ptr uncompressed_yuv_420_image, |
| 426 | jr_uncompressed_ptr uncompressed_recovery_map, |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 427 | jr_metadata_ptr metadata, |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 428 | jr_uncompressed_ptr dest) { |
Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 429 | if (uncompressed_yuv_420_image == nullptr |
| 430 | || uncompressed_recovery_map == nullptr |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 431 | || metadata == nullptr |
Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 432 | || dest == nullptr) { |
Dichen Zhang | 80b7248 | 2022-11-02 01:55:35 +0000 | [diff] [blame] | 433 | return ERROR_JPEGR_INVALID_NULL_PTR; |
Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 436 | size_t width = uncompressed_yuv_420_image->width; |
| 437 | size_t height = uncompressed_yuv_420_image->height; |
| 438 | |
| 439 | dest->width = width; |
| 440 | dest->height = height; |
| 441 | size_t pixel_count = width * height; |
| 442 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 443 | ColorTransformFn hdrOetf = nullptr; |
| 444 | switch (metadata->transferFunction) { |
| 445 | case JPEGR_TF_HLG: |
| 446 | hdrOetf = hlgOetf; |
| 447 | break; |
| 448 | case JPEGR_TF_PQ: |
| 449 | hdrOetf = pqOetf; |
| 450 | break; |
| 451 | } |
| 452 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 453 | for (size_t y = 0; y < height; ++y) { |
| 454 | for (size_t x = 0; x < width; ++x) { |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 455 | Color yuv_gamma_sdr = getYuv420Pixel(uncompressed_yuv_420_image, x, y); |
| 456 | Color rgb_gamma_sdr = srgbYuvToRgb(yuv_gamma_sdr); |
| 457 | Color rgb_sdr = srgbInvOetf(rgb_gamma_sdr); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 458 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 459 | // TODO: determine map scaling factor based on actual map dims |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 460 | float recovery = sampleMap(uncompressed_recovery_map, kMapDimensionScaleFactor, x, y); |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 461 | Color rgb_hdr = applyRecovery(rgb_sdr, recovery, metadata->rangeScalingFactor); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 462 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 463 | Color rgb_gamma_hdr = hdrOetf(rgb_hdr); |
| 464 | uint32_t rgba1010102 = colorToRgba1010102(rgb_gamma_hdr); |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 465 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 466 | size_t pixel_idx = x + y * width; |
| 467 | reinterpret_cast<uint32_t*>(dest->data)[pixel_idx] = rgba1010102; |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 471 | return NO_ERROR; |
Dichen Zhang | 596a756 | 2022-10-12 14:57:05 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 474 | status_t RecoveryMap::extractRecoveryMap(jr_compressed_ptr compressed_jpegr_image, |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 475 | jr_compressed_ptr dest, |
| 476 | jr_metadata_ptr metadata) { |
| 477 | if (compressed_jpegr_image == nullptr || dest == nullptr || metadata == nullptr) { |
Dichen Zhang | 80b7248 | 2022-11-02 01:55:35 +0000 | [diff] [blame] | 478 | return ERROR_JPEGR_INVALID_NULL_PTR; |
Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | // TBD |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 482 | return NO_ERROR; |
Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 485 | status_t RecoveryMap::appendRecoveryMap(jr_compressed_ptr compressed_jpeg_image, |
| 486 | jr_compressed_ptr compressed_recovery_map, |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 487 | jr_metadata_ptr metadata, |
Nick Deakin | f6bca5a | 2022-11-04 10:43:43 -0400 | [diff] [blame] | 488 | jr_compressed_ptr dest) { |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 489 | if (compressed_jpeg_image == nullptr |
| 490 | || compressed_recovery_map == nullptr |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 491 | || metadata == nullptr |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 492 | || dest == nullptr) { |
Dichen Zhang | 80b7248 | 2022-11-02 01:55:35 +0000 | [diff] [blame] | 493 | return ERROR_JPEGR_INVALID_NULL_PTR; |
Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 496 | string xmp = generateXmp(compressed_recovery_map->length, *metadata); |
Dichen Zhang | a876626 | 2022-11-07 23:48:24 +0000 | [diff] [blame] | 497 | string nameSpace = "http://ns.adobe.com/xap/1.0/\0"; |
| 498 | |
| 499 | // 2 bytes: APP1 sign (ff e1) |
| 500 | // 29 bytes: length of name space "http://ns.adobe.com/xap/1.0/\0" |
| 501 | // x bytes: length of xmp packet |
| 502 | int length = 2 + nameSpace.size() + xmp.size(); |
| 503 | uint8_t lengthH = ((length >> 8) & 0xff); |
| 504 | uint8_t lengthL = (length & 0xff); |
| 505 | |
| 506 | int pos = 0; |
| 507 | |
| 508 | // JPEG/R structure: |
| 509 | // SOI (ff d8) |
| 510 | // APP1 (ff e1) |
| 511 | // 2 bytes of length (2 + 29 + length of xmp packet) |
| 512 | // name space ("http://ns.adobe.com/xap/1.0/\0") |
| 513 | // xmp |
| 514 | // primary image (without the first two bytes, the SOI sign) |
| 515 | // secondary image (the recovery map) |
| 516 | JPEGR_CHECK(Write(dest, &photos_editing_formats::image_io::JpegMarker::kStart, 1, pos)); |
| 517 | JPEGR_CHECK(Write(dest, &photos_editing_formats::image_io::JpegMarker::kSOI, 1, pos)); |
| 518 | JPEGR_CHECK(Write(dest, &photos_editing_formats::image_io::JpegMarker::kStart, 1, pos)); |
| 519 | JPEGR_CHECK(Write(dest, &photos_editing_formats::image_io::JpegMarker::kAPP1, 1, pos)); |
| 520 | JPEGR_CHECK(Write(dest, &lengthH, 1, pos)); |
| 521 | JPEGR_CHECK(Write(dest, &lengthL, 1, pos)); |
| 522 | JPEGR_CHECK(Write(dest, (void*)nameSpace.c_str(), nameSpace.size(), pos)); |
| 523 | JPEGR_CHECK(Write(dest, (void*)xmp.c_str(), xmp.size(), pos)); |
| 524 | JPEGR_CHECK(Write(dest, |
| 525 | (uint8_t*)compressed_jpeg_image->data + 2, compressed_jpeg_image->length - 2, pos)); |
| 526 | JPEGR_CHECK(Write(dest, compressed_recovery_map->data, compressed_recovery_map->length, pos)); |
| 527 | dest->length = pos; |
| 528 | |
Dichen Zhang | 6947d53 | 2022-10-22 02:16:21 +0000 | [diff] [blame] | 529 | return NO_ERROR; |
Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 532 | string RecoveryMap::generateXmp(int secondary_image_length, jpegr_metadata& metadata) { |
Dichen Zhang | 72fd2b1 | 2022-11-01 06:11:50 +0000 | [diff] [blame] | 533 | const string kContainerPrefix = "GContainer"; |
| 534 | const string kContainerUri = "http://ns.google.com/photos/1.0/container/"; |
| 535 | const string kItemPrefix = "Item"; |
| 536 | const string kRecoveryMap = "RecoveryMap"; |
| 537 | const string kDirectory = "Directory"; |
| 538 | const string kImageJpeg = "image/jpeg"; |
| 539 | const string kItem = "Item"; |
| 540 | const string kLength = "Length"; |
| 541 | const string kMime = "Mime"; |
| 542 | const string kPrimary = "Primary"; |
| 543 | const string kSemantic = "Semantic"; |
| 544 | const string kVersion = "Version"; |
Dichen Zhang | 72fd2b1 | 2022-11-01 06:11:50 +0000 | [diff] [blame] | 545 | |
| 546 | const string kConDir = Name(kContainerPrefix, kDirectory); |
| 547 | const string kContainerItem = Name(kContainerPrefix, kItem); |
| 548 | const string kItemLength = Name(kItemPrefix, kLength); |
| 549 | const string kItemMime = Name(kItemPrefix, kMime); |
| 550 | const string kItemSemantic = Name(kItemPrefix, kSemantic); |
| 551 | |
| 552 | const vector<string> kConDirSeq({kConDir, string("rdf:Seq")}); |
| 553 | const vector<string> kLiItem({string("rdf:li"), kContainerItem}); |
| 554 | |
| 555 | std::stringstream ss; |
| 556 | photos_editing_formats::image_io::XmlWriter writer(ss); |
| 557 | writer.StartWritingElement("x:xmpmeta"); |
| 558 | writer.WriteXmlns("x", "adobe:ns:meta/"); |
| 559 | writer.WriteAttributeNameAndValue("x:xmptk", "Adobe XMP Core 5.1.2"); |
| 560 | writer.StartWritingElement("rdf:RDF"); |
| 561 | writer.WriteXmlns("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); |
| 562 | writer.StartWritingElement("rdf:Description"); |
| 563 | writer.WriteXmlns(kContainerPrefix, kContainerUri); |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 564 | writer.WriteElementAndContent(Name(kContainerPrefix, kVersion), metadata.version); |
| 565 | writer.WriteElementAndContent(Name(kContainerPrefix, "rangeScalingFactor"), |
| 566 | metadata.rangeScalingFactor); |
| 567 | // TODO: determine structure for hdr10 metadata |
| 568 | // TODO: write rest of metadata |
Dichen Zhang | 72fd2b1 | 2022-11-01 06:11:50 +0000 | [diff] [blame] | 569 | writer.StartWritingElements(kConDirSeq); |
| 570 | size_t item_depth = writer.StartWritingElements(kLiItem); |
| 571 | writer.WriteAttributeNameAndValue(kItemSemantic, kPrimary); |
| 572 | writer.WriteAttributeNameAndValue(kItemMime, kImageJpeg); |
| 573 | writer.FinishWritingElementsToDepth(item_depth); |
| 574 | writer.StartWritingElements(kLiItem); |
| 575 | writer.WriteAttributeNameAndValue(kItemSemantic, kRecoveryMap); |
| 576 | writer.WriteAttributeNameAndValue(kItemMime, kImageJpeg); |
| 577 | writer.WriteAttributeNameAndValue(kItemLength, secondary_image_length); |
| 578 | writer.FinishWriting(); |
| 579 | |
| 580 | return ss.str(); |
| 581 | } |
| 582 | |
Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 583 | } // namespace android::recoverymap |