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> |
Dichen Zhang | 36c1e73 | 2022-11-23 01:25:34 +0000 | [diff] [blame] | 18 | #include <fcntl.h> |
| 19 | #include <fstream> |
| 20 | #include <gtest/gtest.h> |
| 21 | #include <utils/Log.h> |
| 22 | |
| 23 | #define RAW_P010_IMAGE "/sdcard/Documents/raw_p010_image.p010" |
| 24 | #define RAW_P010_IMAGE_WIDTH 1280 |
| 25 | #define RAW_P010_IMAGE_HEIGHT 720 |
| 26 | #define JPEG_IMAGE "/sdcard/Documents/jpeg_image.jpg" |
| 27 | |
| 28 | #define SAVE_ENCODING_RESULT true |
| 29 | #define SAVE_DECODING_RESULT true |
Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 30 | |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 31 | namespace android::recoverymap { |
Dichen Zhang | 85b3756 | 2022-10-11 11:08:28 -0700 | [diff] [blame] | 32 | |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 33 | class RecoveryMapTest : public testing::Test { |
| 34 | public: |
| 35 | RecoveryMapTest(); |
| 36 | ~RecoveryMapTest(); |
| 37 | protected: |
| 38 | virtual void SetUp(); |
| 39 | virtual void TearDown(); |
Dichen Zhang | 36c1e73 | 2022-11-23 01:25:34 +0000 | [diff] [blame] | 40 | |
| 41 | struct jpegr_uncompressed_struct mRawP010Image; |
| 42 | struct jpegr_compressed_struct mJpegImage; |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | RecoveryMapTest::RecoveryMapTest() {} |
| 46 | RecoveryMapTest::~RecoveryMapTest() {} |
| 47 | |
| 48 | void RecoveryMapTest::SetUp() {} |
Dichen Zhang | 36c1e73 | 2022-11-23 01:25:34 +0000 | [diff] [blame] | 49 | void RecoveryMapTest::TearDown() { |
| 50 | free(mRawP010Image.data); |
| 51 | free(mJpegImage.data); |
| 52 | } |
| 53 | |
| 54 | static size_t getFileSize(int fd) { |
| 55 | struct stat st; |
| 56 | if (fstat(fd, &st) < 0) { |
| 57 | ALOGW("%s : fstat failed", __func__); |
| 58 | return 0; |
| 59 | } |
| 60 | return st.st_size; // bytes |
| 61 | } |
| 62 | |
| 63 | static bool loadFile(const char filename[], void*& result, int* fileLength) { |
| 64 | int fd = open(filename, O_CLOEXEC); |
| 65 | if (fd < 0) { |
| 66 | return false; |
| 67 | } |
| 68 | int length = getFileSize(fd); |
| 69 | if (length == 0) { |
| 70 | close(fd); |
| 71 | return false; |
| 72 | } |
| 73 | if (fileLength != nullptr) { |
| 74 | *fileLength = length; |
| 75 | } |
| 76 | result = malloc(length); |
| 77 | if (read(fd, result, length) != static_cast<ssize_t>(length)) { |
| 78 | close(fd); |
| 79 | return false; |
| 80 | } |
| 81 | close(fd); |
| 82 | return true; |
| 83 | } |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 84 | |
| 85 | TEST_F(RecoveryMapTest, build) { |
| 86 | // Force all of the recovery map lib to be linked by calling all public functions. |
| 87 | RecoveryMap recovery_map; |
Nick Deakin | 6bd9043 | 2022-11-20 16:26:37 -0500 | [diff] [blame] | 88 | recovery_map.encodeJPEGR(nullptr, nullptr, static_cast<jpegr_transfer_function>(0), |
| 89 | nullptr, 0, nullptr); |
| 90 | recovery_map.encodeJPEGR(nullptr, nullptr, nullptr, static_cast<jpegr_transfer_function>(0), |
| 91 | nullptr); |
| 92 | recovery_map.encodeJPEGR(nullptr, nullptr, static_cast<jpegr_transfer_function>(0), nullptr); |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 93 | recovery_map.decodeJPEGR(nullptr, nullptr, nullptr, false); |
| 94 | } |
| 95 | |
Dichen Zhang | 36c1e73 | 2022-11-23 01:25:34 +0000 | [diff] [blame] | 96 | TEST_F(RecoveryMapTest, encodeFromJpegThenDecode) { |
| 97 | int ret; |
| 98 | |
| 99 | // Load input files. |
| 100 | if (!loadFile(RAW_P010_IMAGE, mRawP010Image.data, nullptr)) { |
| 101 | FAIL() << "Load file " << RAW_P010_IMAGE << " failed"; |
| 102 | } |
| 103 | mRawP010Image.width = RAW_P010_IMAGE_WIDTH; |
| 104 | mRawP010Image.height = RAW_P010_IMAGE_HEIGHT; |
| 105 | mRawP010Image.colorGamut = jpegr_color_gamut::JPEGR_COLORGAMUT_BT2100; |
| 106 | |
| 107 | if (!loadFile(JPEG_IMAGE, mJpegImage.data, &mJpegImage.length)) { |
| 108 | FAIL() << "Load file " << JPEG_IMAGE << " failed"; |
| 109 | } |
| 110 | mJpegImage.colorGamut = jpegr_color_gamut::JPEGR_COLORGAMUT_BT709; |
| 111 | |
| 112 | RecoveryMap recoveryMap; |
| 113 | |
| 114 | jpegr_compressed_struct jpegR; |
| 115 | jpegR.maxLength = RAW_P010_IMAGE_WIDTH * RAW_P010_IMAGE_HEIGHT * sizeof(uint8_t); |
| 116 | jpegR.data = malloc(jpegR.maxLength); |
| 117 | ret = recoveryMap.encodeJPEGR( |
| 118 | &mRawP010Image, &mJpegImage, jpegr_transfer_function::JPEGR_TF_HLG, &jpegR); |
| 119 | if (ret != OK) { |
| 120 | FAIL() << "Error code is " << ret; |
| 121 | } |
| 122 | if (SAVE_ENCODING_RESULT) { |
| 123 | // Output image data to file |
| 124 | std::string filePath = "/sdcard/Documents/encoded_from_jpeg_input.jpgr"; |
| 125 | std::ofstream imageFile(filePath.c_str(), std::ofstream::binary); |
| 126 | if (!imageFile.is_open()) { |
| 127 | ALOGE("%s: Unable to create file %s", __FUNCTION__, filePath.c_str()); |
| 128 | } |
| 129 | imageFile.write((const char*)jpegR.data, jpegR.length); |
| 130 | } |
| 131 | |
| 132 | jpegr_uncompressed_struct decodedJpegR; |
| 133 | int decodedJpegRSize = RAW_P010_IMAGE_WIDTH * RAW_P010_IMAGE_HEIGHT * 4; |
| 134 | decodedJpegR.data = malloc(decodedJpegRSize); |
| 135 | ret = recoveryMap.decodeJPEGR(&jpegR, &decodedJpegR); |
| 136 | if (ret != OK) { |
| 137 | FAIL() << "Error code is " << ret; |
| 138 | } |
| 139 | if (SAVE_DECODING_RESULT) { |
| 140 | // Output image data to file |
| 141 | std::string filePath = "/sdcard/Documents/decoded_from_jpeg_input.rgb10"; |
| 142 | std::ofstream imageFile(filePath.c_str(), std::ofstream::binary); |
| 143 | if (!imageFile.is_open()) { |
| 144 | ALOGE("%s: Unable to create file %s", __FUNCTION__, filePath.c_str()); |
| 145 | } |
| 146 | imageFile.write((const char*)decodedJpegR.data, decodedJpegRSize); |
| 147 | } |
| 148 | |
| 149 | free(jpegR.data); |
| 150 | free(decodedJpegR.data); |
| 151 | } |
| 152 | |
Nick Deakin | 594a4ca | 2022-11-16 20:57:42 -0500 | [diff] [blame] | 153 | } // namespace android::recoverymap |