xref: /aosp_15_r20/frameworks/av/services/camera/virtualcamera/util/JpegUtil.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
2  * Copyright (C) 2023 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 #ifndef ANDROID_COMPANION_VIRTUALCAMERA_JPEGUTIL_H
18 #define ANDROID_COMPANION_VIRTUALCAMERA_JPEGUTIL_H
19 
20 #include <optional>
21 #include <vector>
22 
23 #include "android/hardware_buffer.h"
24 #include "util/Util.h"
25 
26 namespace android {
27 namespace companion {
28 namespace virtualcamera {
29 
30 // Jpeg-compress image into the output buffer.
31 // * width - width of the image, can be less than width of inBuffer.
32 // * heigh - height of the image, can be less than height of inBuffer.
33 // * quality - 0-100, higher number corresponds to higher quality.
34 // * inBuffer - Input buffer, the dimensions of the buffer must be aligned to
35 //   2*DCT_SIZE (16) to include necessary padding in case width and height of
36 //   image is not aligned with 2*DCT_SIZE.
37 // * app1ExifData - vector containing data to be included in APP1
38 //   segment. Can be empty.
39 // * outBufferSize - capacity of the output buffer.
40 // * outBuffer - output buffer to write compressed data into.
41 // Returns size of compressed data if the compression was successful,
42 // empty optional otherwise.
43 std::optional<size_t> compressJpeg(int width, int height, int quality,
44                                    std::shared_ptr<AHardwareBuffer> inBuffer,
45                                    const std::vector<uint8_t>& app1ExifData,
46                                    size_t outBufferSize, void* outBuffer);
47 
48 // Round the resolution to the closest higher resolution where width and height
49 // are divisible by 2*DCT_SIZE ().
50 Resolution roundTo2DctSize(Resolution resolution);
51 
52 }  // namespace virtualcamera
53 }  // namespace companion
54 }  // namespace android
55 
56 #endif  // ANDROID_COMPANION_VIRTUALCAMERA_JPEGUTIL_H
57