• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
18 
19 #include <fstream>
20 #include <iostream>
21 
22 #include "ultrahdr/ultrahdrcommon.h"
23 #include "ultrahdr/ultrahdr.h"
24 #include "ultrahdr/jpegencoderhelper.h"
25 
26 namespace ultrahdr {
27 
28 #ifdef __ANDROID__
29 #define ALIGNED_IMAGE "/data/local/tmp/minnie-320x240.yu12"
30 #define SINGLE_CHANNEL_IMAGE "/data/local/tmp/minnie-320x240.y"
31 #define UNALIGNED_IMAGE "/data/local/tmp/minnie-318x240.yu12"
32 #define RGB_IMAGE "/data/local/tmp/minnie-320x240.rgb"
33 #else
34 #define ALIGNED_IMAGE "./data/minnie-320x240.yu12"
35 #define SINGLE_CHANNEL_IMAGE "./data/minnie-320x240.y"
36 #define UNALIGNED_IMAGE "./data/minnie-318x240.yu12"
37 #define RGB_IMAGE "./data/minnie-320x240.rgb"
38 #endif
39 #define ALIGNED_IMAGE_WIDTH 320
40 #define ALIGNED_IMAGE_HEIGHT 240
41 #define SINGLE_CHANNEL_IMAGE_WIDTH ALIGNED_IMAGE_WIDTH
42 #define SINGLE_CHANNEL_IMAGE_HEIGHT ALIGNED_IMAGE_HEIGHT
43 #define UNALIGNED_IMAGE_WIDTH 318
44 #define UNALIGNED_IMAGE_HEIGHT 240
45 #define JPEG_QUALITY 90
46 
47 class JpegEncoderHelperTest : public testing::Test {
48  public:
49   struct Image {
50     std::unique_ptr<uint8_t[]> buffer;
51     size_t width;
52     size_t height;
53   };
54   JpegEncoderHelperTest();
55   ~JpegEncoderHelperTest();
56 
57  protected:
58   virtual void SetUp();
59   virtual void TearDown();
60 
61   Image mAlignedImage, mUnalignedImage, mSingleChannelImage, mRgbImage;
62 };
63 
JpegEncoderHelperTest()64 JpegEncoderHelperTest::JpegEncoderHelperTest() {}
65 
~JpegEncoderHelperTest()66 JpegEncoderHelperTest::~JpegEncoderHelperTest() {}
67 
loadFile(const char filename[],JpegEncoderHelperTest::Image * result)68 static bool loadFile(const char filename[], JpegEncoderHelperTest::Image* result) {
69   std::ifstream ifd(filename, std::ios::binary | std::ios::ate);
70   if (ifd.good()) {
71     int size = ifd.tellg();
72     ifd.seekg(0, std::ios::beg);
73     result->buffer.reset(new uint8_t[size]);
74     ifd.read(reinterpret_cast<char*>(result->buffer.get()), size);
75     ifd.close();
76     return true;
77   }
78   return false;
79 }
80 
SetUp()81 void JpegEncoderHelperTest::SetUp() {
82   if (!loadFile(ALIGNED_IMAGE, &mAlignedImage)) {
83     FAIL() << "Load file " << ALIGNED_IMAGE << " failed";
84   }
85   mAlignedImage.width = ALIGNED_IMAGE_WIDTH;
86   mAlignedImage.height = ALIGNED_IMAGE_HEIGHT;
87   if (!loadFile(UNALIGNED_IMAGE, &mUnalignedImage)) {
88     FAIL() << "Load file " << UNALIGNED_IMAGE << " failed";
89   }
90   mUnalignedImage.width = UNALIGNED_IMAGE_WIDTH;
91   mUnalignedImage.height = UNALIGNED_IMAGE_HEIGHT;
92   if (!loadFile(SINGLE_CHANNEL_IMAGE, &mSingleChannelImage)) {
93     FAIL() << "Load file " << SINGLE_CHANNEL_IMAGE << " failed";
94   }
95   mSingleChannelImage.width = SINGLE_CHANNEL_IMAGE_WIDTH;
96   mSingleChannelImage.height = SINGLE_CHANNEL_IMAGE_HEIGHT;
97   if (!loadFile(RGB_IMAGE, &mRgbImage)) {
98     FAIL() << "Load file " << RGB_IMAGE << " failed";
99   }
100   mRgbImage.width = ALIGNED_IMAGE_WIDTH;
101   mRgbImage.height = ALIGNED_IMAGE_HEIGHT;
102 }
103 
TearDown()104 void JpegEncoderHelperTest::TearDown() {}
105 
TEST_F(JpegEncoderHelperTest,encodeAlignedImage)106 TEST_F(JpegEncoderHelperTest, encodeAlignedImage) {
107   JpegEncoderHelper encoder;
108   const uint8_t* yPlane = mAlignedImage.buffer.get();
109   const uint8_t* uPlane = yPlane + mAlignedImage.width * mAlignedImage.height;
110   const uint8_t* vPlane = uPlane + mAlignedImage.width * mAlignedImage.height / 4;
111   const uint8_t* planes[3]{yPlane, uPlane, vPlane};
112   const size_t strides[3]{mAlignedImage.width, mAlignedImage.width / 2, mAlignedImage.width / 2};
113   EXPECT_TRUE(encoder.compressImage(planes, strides, mAlignedImage.width, mAlignedImage.height,
114                                     UHDR_IMG_FMT_12bppYCbCr420, JPEG_QUALITY, NULL, 0));
115   ASSERT_GT(encoder.getCompressedImageSize(), static_cast<uint32_t>(0));
116 }
117 
TEST_F(JpegEncoderHelperTest,encodeUnalignedImage)118 TEST_F(JpegEncoderHelperTest, encodeUnalignedImage) {
119   JpegEncoderHelper encoder;
120   const uint8_t* yPlane = mUnalignedImage.buffer.get();
121   const uint8_t* uPlane = yPlane + mUnalignedImage.width * mUnalignedImage.height;
122   const uint8_t* vPlane = uPlane + mUnalignedImage.width * mUnalignedImage.height / 4;
123   const uint8_t* planes[3]{yPlane, uPlane, vPlane};
124   const size_t strides[3]{mUnalignedImage.width, mUnalignedImage.width / 2,
125                           mUnalignedImage.width / 2};
126   EXPECT_TRUE(encoder.compressImage(planes, strides, mUnalignedImage.width, mUnalignedImage.height,
127                                     UHDR_IMG_FMT_12bppYCbCr420, JPEG_QUALITY, NULL, 0));
128   ASSERT_GT(encoder.getCompressedImageSize(), static_cast<uint32_t>(0));
129 }
130 
TEST_F(JpegEncoderHelperTest,encodeSingleChannelImage)131 TEST_F(JpegEncoderHelperTest, encodeSingleChannelImage) {
132   JpegEncoderHelper encoder;
133   const uint8_t* yPlane = mSingleChannelImage.buffer.get();
134   const uint8_t* planes[1]{yPlane};
135   const size_t strides[1]{mSingleChannelImage.width};
136   EXPECT_TRUE(encoder.compressImage(planes, strides, mSingleChannelImage.width,
137                                     mSingleChannelImage.height, UHDR_IMG_FMT_8bppYCbCr400,
138                                     JPEG_QUALITY, NULL, 0));
139   ASSERT_GT(encoder.getCompressedImageSize(), static_cast<uint32_t>(0));
140 }
141 
TEST_F(JpegEncoderHelperTest,encodeRGBImage)142 TEST_F(JpegEncoderHelperTest, encodeRGBImage) {
143   JpegEncoderHelper encoder;
144   const uint8_t* rgbPlane = mRgbImage.buffer.get();
145   const uint8_t* planes[1]{rgbPlane};
146   const size_t strides[1]{mRgbImage.width * 3};
147   EXPECT_TRUE(encoder.compressImage(planes, strides, mRgbImage.width, mRgbImage.height,
148                                     UHDR_IMG_FMT_24bppRGB888, JPEG_QUALITY, NULL, 0));
149   ASSERT_GT(encoder.getCompressedImageSize(), static_cast<uint32_t>(0));
150 }
151 
152 }  // namespace ultrahdr
153