Dichen Zhang | 0d12ba8 | 2022-10-19 20:44:51 +0000 | [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 | |
Dichen Zhang | 0d12ba8 | 2022-10-19 20:44:51 +0000 | [diff] [blame] | 17 | #include <gtest/gtest.h> |
Ram Mohan | 0202c12 | 2023-08-13 17:22:35 +0530 | [diff] [blame] | 18 | #include <ultrahdr/icc.h> |
| 19 | #include <ultrahdr/jpegdecoderhelper.h> |
Dichen Zhang | 0d12ba8 | 2022-10-19 20:44:51 +0000 | [diff] [blame] | 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include <fcntl.h> |
| 23 | |
Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 24 | namespace android::ultrahdr { |
Dichen Zhang | 0d12ba8 | 2022-10-19 20:44:51 +0000 | [diff] [blame] | 25 | |
Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 26 | // No ICC or EXIF |
Ram Mohan | 0202c12 | 2023-08-13 17:22:35 +0530 | [diff] [blame] | 27 | #define YUV_IMAGE "/data/local/tmp/minnie-320x240-yuv.jpg" |
Dichen Zhang | 0d12ba8 | 2022-10-19 20:44:51 +0000 | [diff] [blame] | 28 | #define YUV_IMAGE_SIZE 20193 |
Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 29 | // Has ICC and EXIF |
Ram Mohan | 0202c12 | 2023-08-13 17:22:35 +0530 | [diff] [blame] | 30 | #define YUV_ICC_IMAGE "/data/local/tmp/minnie-320x240-yuv-icc.jpg" |
Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 31 | #define YUV_ICC_IMAGE_SIZE 34266 |
| 32 | // No ICC or EXIF |
Ram Mohan | 0202c12 | 2023-08-13 17:22:35 +0530 | [diff] [blame] | 33 | #define GREY_IMAGE "/data/local/tmp/minnie-320x240-y.jpg" |
Dichen Zhang | 0d12ba8 | 2022-10-19 20:44:51 +0000 | [diff] [blame] | 34 | #define GREY_IMAGE_SIZE 20193 |
| 35 | |
Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 36 | #define IMAGE_WIDTH 320 |
| 37 | #define IMAGE_HEIGHT 240 |
| 38 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 39 | class JpegDecoderHelperTest : public testing::Test { |
Dichen Zhang | 0d12ba8 | 2022-10-19 20:44:51 +0000 | [diff] [blame] | 40 | public: |
| 41 | struct Image { |
| 42 | std::unique_ptr<uint8_t[]> buffer; |
| 43 | size_t size; |
| 44 | }; |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 45 | JpegDecoderHelperTest(); |
| 46 | ~JpegDecoderHelperTest(); |
Ram Mohan | 0202c12 | 2023-08-13 17:22:35 +0530 | [diff] [blame] | 47 | |
Dichen Zhang | 0d12ba8 | 2022-10-19 20:44:51 +0000 | [diff] [blame] | 48 | protected: |
| 49 | virtual void SetUp(); |
| 50 | virtual void TearDown(); |
| 51 | |
Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 52 | Image mYuvImage, mYuvIccImage, mGreyImage; |
Dichen Zhang | 0d12ba8 | 2022-10-19 20:44:51 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 55 | JpegDecoderHelperTest::JpegDecoderHelperTest() {} |
Dichen Zhang | 0d12ba8 | 2022-10-19 20:44:51 +0000 | [diff] [blame] | 56 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 57 | JpegDecoderHelperTest::~JpegDecoderHelperTest() {} |
Dichen Zhang | 0d12ba8 | 2022-10-19 20:44:51 +0000 | [diff] [blame] | 58 | |
| 59 | static size_t getFileSize(int fd) { |
| 60 | struct stat st; |
| 61 | if (fstat(fd, &st) < 0) { |
| 62 | ALOGW("%s : fstat failed", __func__); |
| 63 | return 0; |
| 64 | } |
| 65 | return st.st_size; // bytes |
| 66 | } |
| 67 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 68 | static bool loadFile(const char filename[], JpegDecoderHelperTest::Image* result) { |
Dichen Zhang | 0d12ba8 | 2022-10-19 20:44:51 +0000 | [diff] [blame] | 69 | int fd = open(filename, O_CLOEXEC); |
| 70 | if (fd < 0) { |
| 71 | return false; |
| 72 | } |
| 73 | int length = getFileSize(fd); |
| 74 | if (length == 0) { |
| 75 | close(fd); |
| 76 | return false; |
| 77 | } |
| 78 | result->buffer.reset(new uint8_t[length]); |
| 79 | if (read(fd, result->buffer.get(), length) != static_cast<ssize_t>(length)) { |
| 80 | close(fd); |
| 81 | return false; |
| 82 | } |
| 83 | close(fd); |
| 84 | return true; |
| 85 | } |
| 86 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 87 | void JpegDecoderHelperTest::SetUp() { |
Dichen Zhang | 0d12ba8 | 2022-10-19 20:44:51 +0000 | [diff] [blame] | 88 | if (!loadFile(YUV_IMAGE, &mYuvImage)) { |
| 89 | FAIL() << "Load file " << YUV_IMAGE << " failed"; |
| 90 | } |
| 91 | mYuvImage.size = YUV_IMAGE_SIZE; |
Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 92 | if (!loadFile(YUV_ICC_IMAGE, &mYuvIccImage)) { |
| 93 | FAIL() << "Load file " << YUV_ICC_IMAGE << " failed"; |
| 94 | } |
| 95 | mYuvIccImage.size = YUV_ICC_IMAGE_SIZE; |
Dichen Zhang | 0d12ba8 | 2022-10-19 20:44:51 +0000 | [diff] [blame] | 96 | if (!loadFile(GREY_IMAGE, &mGreyImage)) { |
| 97 | FAIL() << "Load file " << GREY_IMAGE << " failed"; |
| 98 | } |
| 99 | mGreyImage.size = GREY_IMAGE_SIZE; |
| 100 | } |
| 101 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 102 | void JpegDecoderHelperTest::TearDown() {} |
Dichen Zhang | 0d12ba8 | 2022-10-19 20:44:51 +0000 | [diff] [blame] | 103 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 104 | TEST_F(JpegDecoderHelperTest, decodeYuvImage) { |
| 105 | JpegDecoderHelper decoder; |
Dichen Zhang | 0d12ba8 | 2022-10-19 20:44:51 +0000 | [diff] [blame] | 106 | EXPECT_TRUE(decoder.decompressImage(mYuvImage.buffer.get(), mYuvImage.size)); |
| 107 | ASSERT_GT(decoder.getDecompressedImageSize(), static_cast<uint32_t>(0)); |
Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 108 | EXPECT_EQ(IccHelper::readIccColorGamut(decoder.getICCPtr(), decoder.getICCSize()), |
| 109 | ULTRAHDR_COLORGAMUT_UNSPECIFIED); |
| 110 | } |
| 111 | |
| 112 | TEST_F(JpegDecoderHelperTest, decodeYuvIccImage) { |
| 113 | JpegDecoderHelper decoder; |
| 114 | EXPECT_TRUE(decoder.decompressImage(mYuvIccImage.buffer.get(), mYuvIccImage.size)); |
| 115 | ASSERT_GT(decoder.getDecompressedImageSize(), static_cast<uint32_t>(0)); |
| 116 | EXPECT_EQ(IccHelper::readIccColorGamut(decoder.getICCPtr(), decoder.getICCSize()), |
| 117 | ULTRAHDR_COLORGAMUT_BT709); |
Dichen Zhang | 0d12ba8 | 2022-10-19 20:44:51 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 120 | TEST_F(JpegDecoderHelperTest, decodeGreyImage) { |
| 121 | JpegDecoderHelper decoder; |
Dichen Zhang | 0d12ba8 | 2022-10-19 20:44:51 +0000 | [diff] [blame] | 122 | EXPECT_TRUE(decoder.decompressImage(mGreyImage.buffer.get(), mGreyImage.size)); |
| 123 | ASSERT_GT(decoder.getDecompressedImageSize(), static_cast<uint32_t>(0)); |
| 124 | } |
| 125 | |
Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 126 | TEST_F(JpegDecoderHelperTest, getCompressedImageParameters) { |
| 127 | size_t width = 0, height = 0; |
| 128 | std::vector<uint8_t> icc, exif; |
| 129 | |
| 130 | JpegDecoderHelper decoder; |
Ram Mohan | 0202c12 | 2023-08-13 17:22:35 +0530 | [diff] [blame] | 131 | EXPECT_TRUE(decoder.getCompressedImageParameters(mYuvImage.buffer.get(), mYuvImage.size, &width, |
| 132 | &height, &icc, &exif)); |
Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 133 | |
| 134 | EXPECT_EQ(width, IMAGE_WIDTH); |
| 135 | EXPECT_EQ(height, IMAGE_HEIGHT); |
| 136 | EXPECT_EQ(icc.size(), 0); |
| 137 | EXPECT_EQ(exif.size(), 0); |
| 138 | } |
| 139 | |
| 140 | TEST_F(JpegDecoderHelperTest, getCompressedImageParametersIcc) { |
| 141 | size_t width = 0, height = 0; |
| 142 | std::vector<uint8_t> icc, exif; |
| 143 | |
| 144 | JpegDecoderHelper decoder; |
| 145 | EXPECT_TRUE(decoder.getCompressedImageParameters(mYuvIccImage.buffer.get(), mYuvIccImage.size, |
| 146 | &width, &height, &icc, &exif)); |
| 147 | |
| 148 | EXPECT_EQ(width, IMAGE_WIDTH); |
| 149 | EXPECT_EQ(height, IMAGE_HEIGHT); |
| 150 | EXPECT_GT(icc.size(), 0); |
| 151 | EXPECT_GT(exif.size(), 0); |
| 152 | |
Ram Mohan | 0202c12 | 2023-08-13 17:22:35 +0530 | [diff] [blame] | 153 | EXPECT_EQ(IccHelper::readIccColorGamut(icc.data(), icc.size()), ULTRAHDR_COLORGAMUT_BT709); |
Nick Deakin | 0db53ee | 2023-05-19 17:14:45 -0400 | [diff] [blame] | 154 | } |
| 155 | |
Ram Mohan | 0202c12 | 2023-08-13 17:22:35 +0530 | [diff] [blame] | 156 | } // namespace android::ultrahdr |