1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <ultrahdr/jpegencoderhelper.h>
#include <gtest/gtest.h>
#include <utils/Log.h>
#include <fcntl.h>
namespace android::ultrahdr {
#define ALIGNED_IMAGE "/data/local/tmp/minnie-320x240.yu12"
#define ALIGNED_IMAGE_WIDTH 320
#define ALIGNED_IMAGE_HEIGHT 240
#define SINGLE_CHANNEL_IMAGE "/data/local/tmp/minnie-320x240.y"
#define SINGLE_CHANNEL_IMAGE_WIDTH ALIGNED_IMAGE_WIDTH
#define SINGLE_CHANNEL_IMAGE_HEIGHT ALIGNED_IMAGE_HEIGHT
#define UNALIGNED_IMAGE "/data/local/tmp/minnie-318x240.yu12"
#define UNALIGNED_IMAGE_WIDTH 318
#define UNALIGNED_IMAGE_HEIGHT 240
#define JPEG_QUALITY 90
class JpegEncoderHelperTest : public testing::Test {
public:
struct Image {
std::unique_ptr<uint8_t[]> buffer;
size_t width;
size_t height;
};
JpegEncoderHelperTest();
~JpegEncoderHelperTest();
protected:
virtual void SetUp();
virtual void TearDown();
Image mAlignedImage, mUnalignedImage, mSingleChannelImage;
};
JpegEncoderHelperTest::JpegEncoderHelperTest() {}
JpegEncoderHelperTest::~JpegEncoderHelperTest() {}
static size_t getFileSize(int fd) {
struct stat st;
if (fstat(fd, &st) < 0) {
ALOGW("%s : fstat failed", __func__);
return 0;
}
return st.st_size; // bytes
}
static bool loadFile(const char filename[], JpegEncoderHelperTest::Image* result) {
int fd = open(filename, O_CLOEXEC);
if (fd < 0) {
return false;
}
int length = getFileSize(fd);
if (length == 0) {
close(fd);
return false;
}
result->buffer.reset(new uint8_t[length]);
if (read(fd, result->buffer.get(), length) != static_cast<ssize_t>(length)) {
close(fd);
return false;
}
close(fd);
return true;
}
void JpegEncoderHelperTest::SetUp() {
if (!loadFile(ALIGNED_IMAGE, &mAlignedImage)) {
FAIL() << "Load file " << ALIGNED_IMAGE << " failed";
}
mAlignedImage.width = ALIGNED_IMAGE_WIDTH;
mAlignedImage.height = ALIGNED_IMAGE_HEIGHT;
if (!loadFile(UNALIGNED_IMAGE, &mUnalignedImage)) {
FAIL() << "Load file " << UNALIGNED_IMAGE << " failed";
}
mUnalignedImage.width = UNALIGNED_IMAGE_WIDTH;
mUnalignedImage.height = UNALIGNED_IMAGE_HEIGHT;
if (!loadFile(SINGLE_CHANNEL_IMAGE, &mSingleChannelImage)) {
FAIL() << "Load file " << SINGLE_CHANNEL_IMAGE << " failed";
}
mSingleChannelImage.width = SINGLE_CHANNEL_IMAGE_WIDTH;
mSingleChannelImage.height = SINGLE_CHANNEL_IMAGE_HEIGHT;
}
void JpegEncoderHelperTest::TearDown() {}
TEST_F(JpegEncoderHelperTest, encodeAlignedImage) {
JpegEncoderHelper encoder;
EXPECT_TRUE(encoder.compressImage(mAlignedImage.buffer.get(),
mAlignedImage.buffer.get() +
mAlignedImage.width * mAlignedImage.height,
mAlignedImage.width, mAlignedImage.height,
mAlignedImage.width, mAlignedImage.width / 2, JPEG_QUALITY,
NULL, 0));
ASSERT_GT(encoder.getCompressedImageSize(), static_cast<uint32_t>(0));
}
TEST_F(JpegEncoderHelperTest, encodeUnalignedImage) {
JpegEncoderHelper encoder;
EXPECT_TRUE(encoder.compressImage(mUnalignedImage.buffer.get(),
mUnalignedImage.buffer.get() +
mUnalignedImage.width * mUnalignedImage.height,
mUnalignedImage.width, mUnalignedImage.height,
mUnalignedImage.width, mUnalignedImage.width / 2,
JPEG_QUALITY, NULL, 0));
ASSERT_GT(encoder.getCompressedImageSize(), static_cast<uint32_t>(0));
}
TEST_F(JpegEncoderHelperTest, encodeSingleChannelImage) {
JpegEncoderHelper encoder;
EXPECT_TRUE(encoder.compressImage(mSingleChannelImage.buffer.get(), nullptr,
mSingleChannelImage.width, mSingleChannelImage.height,
mSingleChannelImage.width, 0, JPEG_QUALITY, NULL, 0));
ASSERT_GT(encoder.getCompressedImageSize(), static_cast<uint32_t>(0));
}
} // namespace android::ultrahdr
|