blob: 1f9bd0f930f1e1059fd49d6edcd7bc6be93efc3a [file] [log] [blame]
Dichen Zhang85b37562022-10-11 11:08:28 -07001/*
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
Dichen Zhangdbceb0e2023-04-14 19:03:18 +000017#ifndef ANDROID_ULTRAHDR_JPEGR_H
18#define ANDROID_ULTRAHDR_JPEGR_H
Nick Deakinf6bca5a2022-11-04 10:43:43 -040019
Ram Mohanf9540402023-05-18 20:08:55 +053020#include "jpegencoderhelper.h"
Dichen Zhang80b72482022-11-02 01:55:35 +000021#include "jpegrerrorcode.h"
Dichen Zhangdbceb0e2023-04-14 19:03:18 +000022#include "ultrahdr.h"
Dichen Zhang6947d532022-10-22 02:16:21 +000023
Dichen Zhangb96ba8f2023-03-21 23:33:41 +000024#ifndef FLT_MAX
25#define FLT_MAX 0x1.fffffep127f
26#endif
27
Dichen Zhangdbceb0e2023-04-14 19:03:18 +000028namespace android::ultrahdr {
Dichen Zhang3e5798c2023-03-01 22:30:43 +000029
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +000030struct jpegr_info_struct {
31 size_t width;
32 size_t height;
33 std::vector<uint8_t>* iccData;
34 std::vector<uint8_t>* exifData;
35};
36
Dichen Zhang596a7562022-10-12 14:57:05 -070037/*
Dichen Zhang10959a42023-04-10 16:28:16 -070038 * Holds information for uncompressed image or gain map.
Dichen Zhang596a7562022-10-12 14:57:05 -070039 */
Dichen Zhang6947d532022-10-22 02:16:21 +000040struct jpegr_uncompressed_struct {
Dichen Zhang596a7562022-10-12 14:57:05 -070041 // Pointer to the data location.
42 void* data;
Dichen Zhang10959a42023-04-10 16:28:16 -070043 // Width of the gain map or the luma plane of the image in pixels.
Dichen Zhang596a7562022-10-12 14:57:05 -070044 int width;
Dichen Zhang10959a42023-04-10 16:28:16 -070045 // Height of the gain map or the luma plane of the image in pixels.
Dichen Zhang596a7562022-10-12 14:57:05 -070046 int height;
Nick Deakin6bd90432022-11-20 16:26:37 -050047 // Color gamut.
Dichen Zhangdbceb0e2023-04-14 19:03:18 +000048 ultrahdr_color_gamut colorGamut;
Dichen Zhang66ca6e32023-04-05 12:22:54 -070049
50 // Values below are optional
51 // Pointer to chroma data, if it's NULL, chroma plane is considered to be immediately
52 // following after the luma plane.
53 // Note: currently this feature is only supported for P010 image (HDR input).
54 void* chroma_data = nullptr;
55 // Strides of Y plane in number of pixels, using 0 to present uninitialized, must be
56 // larger than or equal to luma width.
57 // Note: currently this feature is only supported for P010 image (HDR input).
58 int luma_stride = 0;
59 // Strides of UV plane in number of pixels, using 0 to present uninitialized, must be
60 // larger than or equal to chroma width.
61 // Note: currently this feature is only supported for P010 image (HDR input).
62 int chroma_stride = 0;
Dichen Zhang596a7562022-10-12 14:57:05 -070063};
64
65/*
Dichen Zhang10959a42023-04-10 16:28:16 -070066 * Holds information for compressed image or gain map.
Dichen Zhang596a7562022-10-12 14:57:05 -070067 */
Dichen Zhang6947d532022-10-22 02:16:21 +000068struct jpegr_compressed_struct {
Dichen Zhang596a7562022-10-12 14:57:05 -070069 // Pointer to the data location.
70 void* data;
Dichen Zhang0b9f7de2022-11-18 06:52:46 +000071 // Used data length in bytes.
Dichen Zhangffa34012022-11-03 23:21:13 +000072 int length;
Dichen Zhang0b9f7de2022-11-18 06:52:46 +000073 // Maximum available data length in bytes.
74 int maxLength;
Nick Deakin6bd90432022-11-20 16:26:37 -050075 // Color gamut.
Dichen Zhangdbceb0e2023-04-14 19:03:18 +000076 ultrahdr_color_gamut colorGamut;
Dichen Zhangffa34012022-11-03 23:21:13 +000077};
78
79/*
80 * Holds information for EXIF metadata.
81 */
82struct jpegr_exif_struct {
83 // Pointer to the data location.
84 void* data;
Dichen Zhang596a7562022-10-12 14:57:05 -070085 // Data length;
86 int length;
87};
88
Dichen Zhang6947d532022-10-22 02:16:21 +000089typedef struct jpegr_uncompressed_struct* jr_uncompressed_ptr;
90typedef struct jpegr_compressed_struct* jr_compressed_ptr;
Dichen Zhangffa34012022-11-03 23:21:13 +000091typedef struct jpegr_exif_struct* jr_exif_ptr;
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +000092typedef struct jpegr_info_struct* jr_info_ptr;
Dichen Zhang596a7562022-10-12 14:57:05 -070093
Dichen Zhang761b52d2023-02-10 22:38:38 +000094class JpegR {
Dichen Zhang85b37562022-10-11 11:08:28 -070095public:
96 /*
Dichen Zhange286f1c2023-03-14 00:22:00 +000097 * Experimental only
98 *
Dichen Zhang636f5242022-12-07 20:25:44 +000099 * Encode API-0
100 * Compress JPEGR image from 10-bit HDR YUV.
101 *
Dichen Zhang10959a42023-04-10 16:28:16 -0700102 * Tonemap the HDR input to a SDR image, generate gain map from the HDR and SDR images,
103 * compress SDR YUV to 8-bit JPEG and append the gain map to the end of the compressed
Dichen Zhang636f5242022-12-07 20:25:44 +0000104 * JPEG.
105 * @param uncompressed_p010_image uncompressed HDR image in P010 color format
106 * @param hdr_tf transfer function of the HDR image
Dichen Zhang92e6c6b2023-04-14 20:20:14 +0000107 * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength}
108 * represents the maximum available size of the desitination buffer, and it must be
109 * set before calling this method. If the encoded JPEGR size exceeds
110 * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}.
Dichen Zhang636f5242022-12-07 20:25:44 +0000111 * @param quality target quality of the JPEG encoding, must be in range of 0-100 where 100 is
112 * the highest quality
113 * @param exif pointer to the exif metadata.
114 * @return NO_ERROR if encoding succeeds, error code if error occurs.
115 */
116 status_t encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000117 ultrahdr_transfer_function hdr_tf,
Dichen Zhang636f5242022-12-07 20:25:44 +0000118 jr_compressed_ptr dest,
119 int quality,
120 jr_exif_ptr exif);
121
122 /*
123 * Encode API-1
Dichen Zhang6947d532022-10-22 02:16:21 +0000124 * Compress JPEGR image from 10-bit HDR YUV and 8-bit SDR YUV.
125 *
Dichen Zhang10959a42023-04-10 16:28:16 -0700126 * Generate gain map from the HDR and SDR inputs, compress SDR YUV to 8-bit JPEG and append
127 * the gain map to the end of the compressed JPEG. HDR and SDR inputs must be the same
Nick Deakin6bd90432022-11-20 16:26:37 -0500128 * resolution.
Dichen Zhang6947d532022-10-22 02:16:21 +0000129 * @param uncompressed_p010_image uncompressed HDR image in P010 color format
130 * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
Nick Deakin6bd90432022-11-20 16:26:37 -0500131 * @param hdr_tf transfer function of the HDR image
Dichen Zhang92e6c6b2023-04-14 20:20:14 +0000132 * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength}
133 * represents the maximum available size of the desitination buffer, and it must be
134 * set before calling this method. If the encoded JPEGR size exceeds
135 * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}.
Dichen Zhangffa34012022-11-03 23:21:13 +0000136 * @param quality target quality of the JPEG encoding, must be in range of 0-100 where 100 is
137 * the highest quality
138 * @param exif pointer to the exif metadata.
Dichen Zhang6947d532022-10-22 02:16:21 +0000139 * @return NO_ERROR if encoding succeeds, error code if error occurs.
140 */
141 status_t encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
142 jr_uncompressed_ptr uncompressed_yuv_420_image,
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000143 ultrahdr_transfer_function hdr_tf,
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400144 jr_compressed_ptr dest,
Dichen Zhangffa34012022-11-03 23:21:13 +0000145 int quality,
Dichen Zhang95cbb9f2022-11-07 18:32:05 +0000146 jr_exif_ptr exif);
Dichen Zhang6947d532022-10-22 02:16:21 +0000147
148 /*
Dichen Zhang636f5242022-12-07 20:25:44 +0000149 * Encode API-2
Dichen Zhangffa34012022-11-03 23:21:13 +0000150 * Compress JPEGR image from 10-bit HDR YUV, 8-bit SDR YUV and compressed 8-bit JPEG.
151 *
152 * This method requires HAL Hardware JPEG encoder.
Dichen Zhang6947d532022-10-22 02:16:21 +0000153 *
Dichen Zhang10959a42023-04-10 16:28:16 -0700154 * Generate gain map from the HDR and SDR inputs, append the gain map to the end of the
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400155 * compressed JPEG. HDR and SDR inputs must be the same resolution and color space.
Dichen Zhang6947d532022-10-22 02:16:21 +0000156 * @param uncompressed_p010_image uncompressed HDR image in P010 color format
157 * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
Dichen Zhang636f5242022-12-07 20:25:44 +0000158 * Note: the SDR image must be the decoded version of the JPEG
159 * input
Dichen Zhang6947d532022-10-22 02:16:21 +0000160 * @param compressed_jpeg_image compressed 8-bit JPEG image
Nick Deakin6bd90432022-11-20 16:26:37 -0500161 * @param hdr_tf transfer function of the HDR image
Dichen Zhang92e6c6b2023-04-14 20:20:14 +0000162 * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength}
163 * represents the maximum available size of the desitination buffer, and it must be
164 * set before calling this method. If the encoded JPEGR size exceeds
165 * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}.
Dichen Zhang6947d532022-10-22 02:16:21 +0000166 * @return NO_ERROR if encoding succeeds, error code if error occurs.
167 */
168 status_t encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
169 jr_uncompressed_ptr uncompressed_yuv_420_image,
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400170 jr_compressed_ptr compressed_jpeg_image,
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000171 ultrahdr_transfer_function hdr_tf,
Dichen Zhang95cbb9f2022-11-07 18:32:05 +0000172 jr_compressed_ptr dest);
Dichen Zhang6947d532022-10-22 02:16:21 +0000173
174 /*
Dichen Zhang636f5242022-12-07 20:25:44 +0000175 * Encode API-3
Dichen Zhang6947d532022-10-22 02:16:21 +0000176 * Compress JPEGR image from 10-bit HDR YUV and 8-bit SDR YUV.
177 *
Dichen Zhangffa34012022-11-03 23:21:13 +0000178 * This method requires HAL Hardware JPEG encoder.
179 *
Dichen Zhang10959a42023-04-10 16:28:16 -0700180 * Decode the compressed 8-bit JPEG image to YUV SDR, generate gain map from the HDR input
181 * and the decoded SDR result, append the gain map to the end of the compressed JPEG. HDR
Nick Deakin6bd90432022-11-20 16:26:37 -0500182 * and SDR inputs must be the same resolution.
Dichen Zhang6947d532022-10-22 02:16:21 +0000183 * @param uncompressed_p010_image uncompressed HDR image in P010 color format
184 * @param compressed_jpeg_image compressed 8-bit JPEG image
Nick Deakin6bd90432022-11-20 16:26:37 -0500185 * @param hdr_tf transfer function of the HDR image
Dichen Zhang92e6c6b2023-04-14 20:20:14 +0000186 * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength}
187 * represents the maximum available size of the desitination buffer, and it must be
188 * set before calling this method. If the encoded JPEGR size exceeds
189 * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}.
Dichen Zhang6947d532022-10-22 02:16:21 +0000190 * @return NO_ERROR if encoding succeeds, error code if error occurs.
191 */
192 status_t encodeJPEGR(jr_uncompressed_ptr uncompressed_p010_image,
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400193 jr_compressed_ptr compressed_jpeg_image,
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000194 ultrahdr_transfer_function hdr_tf,
Dichen Zhang95cbb9f2022-11-07 18:32:05 +0000195 jr_compressed_ptr dest);
Dichen Zhang6947d532022-10-22 02:16:21 +0000196
197 /*
Dichen Zhang92e6c6b2023-04-14 20:20:14 +0000198 * Encode API-4
199 * Assemble JPEGR image from SDR JPEG and gainmap JPEG.
200 *
201 * Assemble the primary JPEG image, the gain map and the metadata to JPEG/R format.
202 * @param compressed_jpeg_image compressed 8-bit JPEG image
203 * @param compressed_gainmap compressed 8-bit JPEG single channel image
204 * @param metadata metadata to be written in XMP of the primary jpeg
205 * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength}
206 * represents the maximum available size of the desitination buffer, and it must be
207 * set before calling this method. If the encoded JPEGR size exceeds
208 * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}.
209 * @return NO_ERROR if encoding succeeds, error code if error occurs.
210 */
211 status_t encodeJPEGR(jr_compressed_ptr compressed_jpeg_image,
212 jr_compressed_ptr compressed_gainmap,
213 ultrahdr_metadata_ptr metadata,
214 jr_compressed_ptr dest);
215
216 /*
Dichen Zhang636f5242022-12-07 20:25:44 +0000217 * Decode API
Dichen Zhang6947d532022-10-22 02:16:21 +0000218 * Decompress JPEGR image.
219 *
Dichen Zhange286f1c2023-03-14 00:22:00 +0000220 * @param compressed_jpegr_image compressed JPEGR image.
221 * @param dest destination of the uncompressed JPEGR image.
Dichen Zhangb96ba8f2023-03-21 23:33:41 +0000222 * @param max_display_boost (optional) the maximum available boost supported by a display,
223 * the value must be greater than or equal to 1.0.
Dichen Zhange286f1c2023-03-14 00:22:00 +0000224 * @param exif destination of the decoded EXIF metadata. The default value is NULL where the
225 decoder will do nothing about it. If configured not NULL the decoder will write
226 EXIF data into this structure. The format is defined in {@code jpegr_exif_struct}
227 * @param output_format flag for setting output color format. Its value configures the output
228 color format. The default value is {@code JPEGR_OUTPUT_HDR_LINEAR}.
229 ----------------------------------------------------------------------
Dichen Zhangc6605702023-03-15 18:40:55 -0700230 | output_format | decoded color format to be written |
Dichen Zhange286f1c2023-03-14 00:22:00 +0000231 ----------------------------------------------------------------------
232 | JPEGR_OUTPUT_SDR | RGBA_8888 |
233 ----------------------------------------------------------------------
234 | JPEGR_OUTPUT_HDR_LINEAR | (default)RGBA_F16 linear |
235 ----------------------------------------------------------------------
236 | JPEGR_OUTPUT_HDR_PQ | RGBA_1010102 PQ |
237 ----------------------------------------------------------------------
238 | JPEGR_OUTPUT_HDR_HLG | RGBA_1010102 HLG |
239 ----------------------------------------------------------------------
Dichen Zhang10959a42023-04-10 16:28:16 -0700240 * @param gain_map destination of the decoded gain map. The default value is NULL where
Dichen Zhange286f1c2023-03-14 00:22:00 +0000241 the decoder will do nothing about it. If configured not NULL the decoder
Dichen Zhang10959a42023-04-10 16:28:16 -0700242 will write the decoded gain_map data into this structure. The format
Dichen Zhange286f1c2023-03-14 00:22:00 +0000243 is defined in {@code jpegr_uncompressed_struct}.
244 * @param metadata destination of the decoded metadata. The default value is NULL where the
245 decoder will do nothing about it. If configured not NULL the decoder will
246 write metadata into this structure. the format of metadata is defined in
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000247 {@code ultrahdr_metadata_struct}.
Dichen Zhang6947d532022-10-22 02:16:21 +0000248 * @return NO_ERROR if decoding succeeds, error code if error occurs.
249 */
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400250 status_t decodeJPEGR(jr_compressed_ptr compressed_jpegr_image,
Dichen Zhangffa34012022-11-03 23:21:13 +0000251 jr_uncompressed_ptr dest,
Dichen Zhangb96ba8f2023-03-21 23:33:41 +0000252 float max_display_boost = FLT_MAX,
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000253 jr_exif_ptr exif = nullptr,
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000254 ultrahdr_output_format output_format = ULTRAHDR_OUTPUT_HDR_LINEAR,
Dichen Zhang10959a42023-04-10 16:28:16 -0700255 jr_uncompressed_ptr gain_map = nullptr,
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000256 ultrahdr_metadata_ptr metadata = nullptr);
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000257
258 /*
259 * Gets Info from JPEGR file without decoding it.
260 *
261 * The output is filled jpegr_info structure
262 * @param compressed_jpegr_image compressed JPEGR image
Fyodor Kyslova90ee002023-01-10 23:41:41 +0000263 * @param jpegr_info pointer to output JPEGR info. Members of jpegr_info
264 * are owned by the caller
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000265 * @return NO_ERROR if JPEGR parsing succeeds, error code otherwise
266 */
267 status_t getJPEGRInfo(jr_compressed_ptr compressed_jpegr_image,
268 jr_info_ptr jpegr_info);
Nick Deakind19e5762023-02-10 15:39:08 -0500269protected:
Dichen Zhang85b37562022-10-11 11:08:28 -0700270 /*
271 * This method is called in the encoding pipeline. It will take the uncompressed 8-bit and
Dichen Zhang10959a42023-04-10 16:28:16 -0700272 * 10-bit yuv images as input, and calculate the uncompressed gain map. The input images
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400273 * must be the same resolution.
Dichen Zhang85b37562022-10-11 11:08:28 -0700274 *
Dichen Zhang596a7562022-10-12 14:57:05 -0700275 * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
276 * @param uncompressed_p010_image uncompressed HDR image in P010 color format
Nick Deakin01759062023-02-02 18:21:43 -0500277 * @param hdr_tf transfer function of the HDR image
Dichen Zhang10959a42023-04-10 16:28:16 -0700278 * @param dest gain map; caller responsible for memory of data
Dichen Zhang761b52d2023-02-10 22:38:38 +0000279 * @param metadata max_content_boost is filled in
Dichen Zhang6947d532022-10-22 02:16:21 +0000280 * @return NO_ERROR if calculation succeeds, error code if error occurs.
Dichen Zhang85b37562022-10-11 11:08:28 -0700281 */
Dichen Zhang10959a42023-04-10 16:28:16 -0700282 status_t generateGainMap(jr_uncompressed_ptr uncompressed_yuv_420_image,
283 jr_uncompressed_ptr uncompressed_p010_image,
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000284 ultrahdr_transfer_function hdr_tf,
285 ultrahdr_metadata_ptr metadata,
Dichen Zhang10959a42023-04-10 16:28:16 -0700286 jr_uncompressed_ptr dest);
Dichen Zhang85b37562022-10-11 11:08:28 -0700287
288 /*
289 * This method is called in the decoding pipeline. It will take the uncompressed (decoded)
Dichen Zhang10959a42023-04-10 16:28:16 -0700290 * 8-bit yuv image, the uncompressed (decoded) gain map, and extracted JPEG/R metadata as
Nick Deakin6bd90432022-11-20 16:26:37 -0500291 * input, and calculate the 10-bit recovered image. The recovered output image is the same
Nick Deakin01759062023-02-02 18:21:43 -0500292 * color gamut as the SDR image, with HLG transfer function, and is in RGBA1010102 data format.
Dichen Zhang85b37562022-10-11 11:08:28 -0700293 *
Dichen Zhang596a7562022-10-12 14:57:05 -0700294 * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
Dichen Zhang10959a42023-04-10 16:28:16 -0700295 * @param uncompressed_gain_map uncompressed gain map
Nick Deakin6bd90432022-11-20 16:26:37 -0500296 * @param metadata JPEG/R metadata extracted from XMP.
Dichen Zhang3e5798c2023-03-01 22:30:43 +0000297 * @param output_format flag for setting output color format. if set to
298 * {@code JPEGR_OUTPUT_SDR}, decoder will only decode the primary image
299 * which is SDR. Default value is JPEGR_OUTPUT_HDR_LINEAR.
Dichen Zhangc6605702023-03-15 18:40:55 -0700300 * @param max_display_boost the maximum available boost supported by a display
Dichen Zhang596a7562022-10-12 14:57:05 -0700301 * @param dest reconstructed HDR image
Dichen Zhang6947d532022-10-22 02:16:21 +0000302 * @return NO_ERROR if calculation succeeds, error code if error occurs.
Dichen Zhang85b37562022-10-11 11:08:28 -0700303 */
Dichen Zhang10959a42023-04-10 16:28:16 -0700304 status_t applyGainMap(jr_uncompressed_ptr uncompressed_yuv_420_image,
305 jr_uncompressed_ptr uncompressed_gain_map,
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000306 ultrahdr_metadata_ptr metadata,
307 ultrahdr_output_format output_format,
Dichen Zhang10959a42023-04-10 16:28:16 -0700308 float max_display_boost,
309 jr_uncompressed_ptr dest);
Dichen Zhang85b37562022-10-11 11:08:28 -0700310
Nick Deakind19e5762023-02-10 15:39:08 -0500311private:
312 /*
Dichen Zhang10959a42023-04-10 16:28:16 -0700313 * This method is called in the encoding pipeline. It will encode the gain map.
Nick Deakind19e5762023-02-10 15:39:08 -0500314 *
Dichen Zhang10959a42023-04-10 16:28:16 -0700315 * @param uncompressed_gain_map uncompressed gain map
Ram Mohanf9540402023-05-18 20:08:55 +0530316 * @param resource to compress gain map
Nick Deakind19e5762023-02-10 15:39:08 -0500317 * @return NO_ERROR if encoding succeeds, error code if error occurs.
318 */
Dichen Zhang10959a42023-04-10 16:28:16 -0700319 status_t compressGainMap(jr_uncompressed_ptr uncompressed_gain_map,
Ram Mohanf9540402023-05-18 20:08:55 +0530320 JpegEncoderHelper* jpeg_encoder);
Nick Deakind19e5762023-02-10 15:39:08 -0500321
Dichen Zhang85b37562022-10-11 11:08:28 -0700322 /*
Dichen Zhang10959a42023-04-10 16:28:16 -0700323 * This methoud is called to separate primary image and gain map image from JPEGR
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000324 *
325 * @param compressed_jpegr_image compressed JPEGR image
326 * @param primary_image destination of primary image
Dichen Zhang10959a42023-04-10 16:28:16 -0700327 * @param gain_map destination of compressed gain map
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000328 * @return NO_ERROR if calculation succeeds, error code if error occurs.
329 */
Dichen Zhang10959a42023-04-10 16:28:16 -0700330 status_t extractPrimaryImageAndGainMap(jr_compressed_ptr compressed_jpegr_image,
331 jr_compressed_ptr primary_image,
332 jr_compressed_ptr gain_map);
Fyodor Kyslov1dcc4422022-11-16 01:40:53 +0000333 /*
Dichen Zhang85b37562022-10-11 11:08:28 -0700334 * This method is called in the decoding pipeline. It will read XMP metadata to find the start
Dichen Zhang10959a42023-04-10 16:28:16 -0700335 * position of the compressed gain map, and will extract the compressed gain map.
Dichen Zhang85b37562022-10-11 11:08:28 -0700336 *
Dichen Zhang6947d532022-10-22 02:16:21 +0000337 * @param compressed_jpegr_image compressed JPEGR image
Dichen Zhang10959a42023-04-10 16:28:16 -0700338 * @param dest destination of compressed gain map
Dichen Zhang6947d532022-10-22 02:16:21 +0000339 * @return NO_ERROR if calculation succeeds, error code if error occurs.
Dichen Zhang85b37562022-10-11 11:08:28 -0700340 */
Dichen Zhang10959a42023-04-10 16:28:16 -0700341 status_t extractGainMap(jr_compressed_ptr compressed_jpegr_image,
342 jr_compressed_ptr dest);
Dichen Zhang85b37562022-10-11 11:08:28 -0700343
344 /*
Dichen Zhang50ff1292023-01-27 18:03:43 +0000345 * This method is called in the encoding pipeline. It will take the standard 8-bit JPEG image,
Dichen Zhang10959a42023-04-10 16:28:16 -0700346 * the compressed gain map and optionally the exif package as inputs, and generate the XMP
Dichen Zhang50ff1292023-01-27 18:03:43 +0000347 * metadata, and finally append everything in the order of:
Dichen Zhang10959a42023-04-10 16:28:16 -0700348 * SOI, APP2(EXIF) (if EXIF is from outside), APP2(XMP), primary image, gain map
Dichen Zhang50ff1292023-01-27 18:03:43 +0000349 * Note that EXIF package is only available for encoding API-0 and API-1. For encoding API-2 and
350 * API-3 this parameter is null, but the primary image in JPEG/R may still have EXIF as long as
351 * the input JPEG has EXIF.
Dichen Zhang85b37562022-10-11 11:08:28 -0700352 *
Dichen Zhang596a7562022-10-12 14:57:05 -0700353 * @param compressed_jpeg_image compressed 8-bit JPEG image
Dichen Zhang10959a42023-04-10 16:28:16 -0700354 * @param compress_gain_map compressed recover map
Dichen Zhang50ff1292023-01-27 18:03:43 +0000355 * @param (nullable) exif EXIF package
Nick Deakin6bd90432022-11-20 16:26:37 -0500356 * @param metadata JPEG/R metadata to encode in XMP of the jpeg
Dichen Zhang6947d532022-10-22 02:16:21 +0000357 * @param dest compressed JPEGR image
358 * @return NO_ERROR if calculation succeeds, error code if error occurs.
Dichen Zhang85b37562022-10-11 11:08:28 -0700359 */
Dichen Zhang10959a42023-04-10 16:28:16 -0700360 status_t appendGainMap(jr_compressed_ptr compressed_jpeg_image,
361 jr_compressed_ptr compressed_gain_map,
362 jr_exif_ptr exif,
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000363 ultrahdr_metadata_ptr metadata,
Dichen Zhang10959a42023-04-10 16:28:16 -0700364 jr_compressed_ptr dest);
Dichen Zhang72fd2b12022-11-01 06:11:50 +0000365
366 /*
Dichen Zhang636f5242022-12-07 20:25:44 +0000367 * This method will tone map a HDR image to an SDR image.
368 *
Dichen Zhangc3437ca2023-01-04 14:00:08 -0800369 * @param src (input) uncompressed P010 image
Dichen Zhang636f5242022-12-07 20:25:44 +0000370 * @param dest (output) tone mapping result as a YUV_420 image
371 * @return NO_ERROR if calculation succeeds, error code if error occurs.
372 */
Dichen Zhangc3437ca2023-01-04 14:00:08 -0800373 status_t toneMap(jr_uncompressed_ptr src,
Dichen Zhang636f5242022-12-07 20:25:44 +0000374 jr_uncompressed_ptr dest);
Dichen Zhang66ca6e32023-04-05 12:22:54 -0700375
376 /*
Ram Mohanac1cfec2023-05-18 14:41:15 +0530377 * This method will check the validity of the input arguments.
Dichen Zhang66ca6e32023-04-05 12:22:54 -0700378 *
379 * @param uncompressed_p010_image uncompressed HDR image in P010 color format
380 * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
Ram Mohanac1cfec2023-05-18 14:41:15 +0530381 * @param hdr_tf transfer function of the HDR image
382 * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength}
383 * represents the maximum available size of the desitination buffer, and it must be
384 * set before calling this method. If the encoded JPEGR size exceeds
385 * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}.
386 * @return NO_ERROR if the input args are valid, error code is not valid.
Dichen Zhang66ca6e32023-04-05 12:22:54 -0700387 */
Ram Mohanac1cfec2023-05-18 14:41:15 +0530388 status_t areInputArgumentsValid(jr_uncompressed_ptr uncompressed_p010_image,
389 jr_uncompressed_ptr uncompressed_yuv_420_image,
390 ultrahdr_transfer_function hdr_tf,
391 jr_compressed_ptr dest);
392
393 /*
394 * This method will check the validity of the input arguments.
395 *
396 * @param uncompressed_p010_image uncompressed HDR image in P010 color format
397 * @param uncompressed_yuv_420_image uncompressed SDR image in YUV_420 color format
398 * @param hdr_tf transfer function of the HDR image
399 * @param dest destination of the compressed JPEGR image. Please note that {@code maxLength}
400 * represents the maximum available size of the desitination buffer, and it must be
401 * set before calling this method. If the encoded JPEGR size exceeds
402 * {@code maxLength}, this method will return {@code ERROR_JPEGR_BUFFER_TOO_SMALL}.
403 * @param quality target quality of the JPEG encoding, must be in range of 0-100 where 100 is
404 * the highest quality
405 * @return NO_ERROR if the input args are valid, error code is not valid.
406 */
407 status_t areInputArgumentsValid(jr_uncompressed_ptr uncompressed_p010_image,
408 jr_uncompressed_ptr uncompressed_yuv_420_image,
409 ultrahdr_transfer_function hdr_tf,
410 jr_compressed_ptr dest,
411 int quality);
Dichen Zhang85b37562022-10-11 11:08:28 -0700412};
413
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000414} // namespace android::ultrahdr
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400415
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000416#endif // ANDROID_ULTRAHDR_JPEGR_H