Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +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 | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 17 | #include <ultrahdr/jpegencoderhelper.h> |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 18 | #include <gtest/gtest.h> |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <fcntl.h> |
| 22 | |
Dichen Zhang | dbceb0e | 2023-04-14 19:03:18 +0000 | [diff] [blame] | 23 | namespace android::ultrahdr { |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 24 | |
Dichen Zhang | 56a7d59 | 2023-04-14 16:57:34 +0000 | [diff] [blame] | 25 | #define ALIGNED_IMAGE "/sdcard/Documents/minnie-320x240.yu12" |
| 26 | #define ALIGNED_IMAGE_WIDTH 320 |
| 27 | #define ALIGNED_IMAGE_HEIGHT 240 |
Dichen Zhang | a481914 | 2022-10-21 04:12:30 +0000 | [diff] [blame] | 28 | #define SINGLE_CHANNEL_IMAGE "/sdcard/Documents/minnie-320x240.y" |
Dichen Zhang | 56a7d59 | 2023-04-14 16:57:34 +0000 | [diff] [blame] | 29 | #define SINGLE_CHANNEL_IMAGE_WIDTH ALIGNED_IMAGE_WIDTH |
| 30 | #define SINGLE_CHANNEL_IMAGE_HEIGHT ALIGNED_IMAGE_HEIGHT |
| 31 | #define UNALIGNED_IMAGE "/sdcard/Documents/minnie-318x240.yu12" |
| 32 | #define UNALIGNED_IMAGE_WIDTH 318 |
| 33 | #define UNALIGNED_IMAGE_HEIGHT 240 |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 34 | #define JPEG_QUALITY 90 |
| 35 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 36 | class JpegEncoderHelperTest : public testing::Test { |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 37 | public: |
| 38 | struct Image { |
| 39 | std::unique_ptr<uint8_t[]> buffer; |
| 40 | size_t width; |
| 41 | size_t height; |
| 42 | }; |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 43 | JpegEncoderHelperTest(); |
| 44 | ~JpegEncoderHelperTest(); |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 45 | |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 46 | protected: |
| 47 | virtual void SetUp(); |
| 48 | virtual void TearDown(); |
| 49 | |
Dichen Zhang | 56a7d59 | 2023-04-14 16:57:34 +0000 | [diff] [blame] | 50 | Image mAlignedImage, mUnalignedImage, mSingleChannelImage; |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 53 | JpegEncoderHelperTest::JpegEncoderHelperTest() {} |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 54 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 55 | JpegEncoderHelperTest::~JpegEncoderHelperTest() {} |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 56 | |
| 57 | static size_t getFileSize(int fd) { |
| 58 | struct stat st; |
| 59 | if (fstat(fd, &st) < 0) { |
| 60 | ALOGW("%s : fstat failed", __func__); |
| 61 | return 0; |
| 62 | } |
| 63 | return st.st_size; // bytes |
| 64 | } |
| 65 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 66 | static bool loadFile(const char filename[], JpegEncoderHelperTest::Image* result) { |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 67 | int fd = open(filename, O_CLOEXEC); |
| 68 | if (fd < 0) { |
| 69 | return false; |
| 70 | } |
| 71 | int length = getFileSize(fd); |
| 72 | if (length == 0) { |
| 73 | close(fd); |
| 74 | return false; |
| 75 | } |
| 76 | result->buffer.reset(new uint8_t[length]); |
| 77 | if (read(fd, result->buffer.get(), length) != static_cast<ssize_t>(length)) { |
| 78 | close(fd); |
| 79 | return false; |
| 80 | } |
| 81 | close(fd); |
| 82 | return true; |
| 83 | } |
| 84 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 85 | void JpegEncoderHelperTest::SetUp() { |
Dichen Zhang | 56a7d59 | 2023-04-14 16:57:34 +0000 | [diff] [blame] | 86 | if (!loadFile(ALIGNED_IMAGE, &mAlignedImage)) { |
| 87 | FAIL() << "Load file " << ALIGNED_IMAGE << " failed"; |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 88 | } |
Dichen Zhang | 56a7d59 | 2023-04-14 16:57:34 +0000 | [diff] [blame] | 89 | mAlignedImage.width = ALIGNED_IMAGE_WIDTH; |
| 90 | mAlignedImage.height = ALIGNED_IMAGE_HEIGHT; |
| 91 | if (!loadFile(UNALIGNED_IMAGE, &mUnalignedImage)) { |
| 92 | FAIL() << "Load file " << UNALIGNED_IMAGE << " failed"; |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 93 | } |
Dichen Zhang | 56a7d59 | 2023-04-14 16:57:34 +0000 | [diff] [blame] | 94 | mUnalignedImage.width = UNALIGNED_IMAGE_WIDTH; |
| 95 | mUnalignedImage.height = UNALIGNED_IMAGE_HEIGHT; |
Dichen Zhang | a481914 | 2022-10-21 04:12:30 +0000 | [diff] [blame] | 96 | if (!loadFile(SINGLE_CHANNEL_IMAGE, &mSingleChannelImage)) { |
| 97 | FAIL() << "Load file " << SINGLE_CHANNEL_IMAGE << " failed"; |
| 98 | } |
| 99 | mSingleChannelImage.width = SINGLE_CHANNEL_IMAGE_WIDTH; |
| 100 | mSingleChannelImage.height = SINGLE_CHANNEL_IMAGE_HEIGHT; |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 103 | void JpegEncoderHelperTest::TearDown() {} |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 104 | |
Dichen Zhang | 56a7d59 | 2023-04-14 16:57:34 +0000 | [diff] [blame] | 105 | TEST_F(JpegEncoderHelperTest, encodeAlignedImage) { |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 106 | JpegEncoderHelper encoder; |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 107 | EXPECT_TRUE(encoder.compressImage(mAlignedImage.buffer.get(), |
| 108 | mAlignedImage.buffer.get() + |
| 109 | mAlignedImage.width * mAlignedImage.height, |
| 110 | mAlignedImage.width, mAlignedImage.height, |
| 111 | mAlignedImage.width, mAlignedImage.width / 2, JPEG_QUALITY, |
| 112 | NULL, 0)); |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 113 | ASSERT_GT(encoder.getCompressedImageSize(), static_cast<uint32_t>(0)); |
| 114 | } |
| 115 | |
Dichen Zhang | 56a7d59 | 2023-04-14 16:57:34 +0000 | [diff] [blame] | 116 | TEST_F(JpegEncoderHelperTest, encodeUnalignedImage) { |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 117 | JpegEncoderHelper encoder; |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 118 | EXPECT_TRUE(encoder.compressImage(mUnalignedImage.buffer.get(), |
| 119 | mUnalignedImage.buffer.get() + |
| 120 | mUnalignedImage.width * mUnalignedImage.height, |
| 121 | mUnalignedImage.width, mUnalignedImage.height, |
| 122 | mUnalignedImage.width, mUnalignedImage.width / 2, |
| 123 | JPEG_QUALITY, NULL, 0)); |
Dichen Zhang | 56a7d59 | 2023-04-14 16:57:34 +0000 | [diff] [blame] | 124 | ASSERT_GT(encoder.getCompressedImageSize(), static_cast<uint32_t>(0)); |
Dichen Zhang | 4a66b3c | 2022-10-19 18:04:47 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Dichen Zhang | 56a7d59 | 2023-04-14 16:57:34 +0000 | [diff] [blame] | 127 | TEST_F(JpegEncoderHelperTest, encodeSingleChannelImage) { |
Dichen Zhang | 02dd059 | 2023-02-10 20:16:57 +0000 | [diff] [blame] | 128 | JpegEncoderHelper encoder; |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 129 | EXPECT_TRUE(encoder.compressImage(mSingleChannelImage.buffer.get(), nullptr, |
| 130 | mSingleChannelImage.width, mSingleChannelImage.height, |
| 131 | mSingleChannelImage.width, 0, JPEG_QUALITY, NULL, 0)); |
Dichen Zhang | a481914 | 2022-10-21 04:12:30 +0000 | [diff] [blame] | 132 | ASSERT_GT(encoder.getCompressedImageSize(), static_cast<uint32_t>(0)); |
| 133 | } |
| 134 | |
Ram Mohan | eca8194 | 2023-07-29 14:41:48 +0530 | [diff] [blame] | 135 | } // namespace android::ultrahdr |