2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "image-loaders.h"
19 #include <dali-test-suite-utils.h>
20 #include <dali/internal/imaging/common/pixel-buffer-impl.h>
22 AutoCloseFile::AutoCloseFile(FILE* fp)
27 AutoCloseFile::~AutoCloseFile()
35 ImageDetails::ImageDetails(const char* const _name, unsigned int _width, unsigned int _height)
39 reportedWidth(_width),
40 reportedHeight(_height),
47 ImageDetails::ImageDetails(const char* const _name, unsigned int _width, unsigned int _height, unsigned int _reportedWidth, unsigned int _reportedHeight)
51 reportedWidth(_reportedWidth),
52 reportedHeight(_reportedHeight),
59 ImageDetails::~ImageDetails()
67 void ImageDetails::LoadBuffer()
69 // Load the reference buffer from the buffer file
70 std::string refBufferFilename(name + ".buffer");
71 FILE* fp = fopen(refBufferFilename.c_str(), "rb");
72 AutoCloseFile autoCloseBufferFile(fp);
76 fseek(fp, 0, SEEK_END);
77 refBufferSize = ftell(fp);
78 fseek(fp, 0, SEEK_SET);
79 refBuffer = reinterpret_cast<Dali::Integration::PixelBuffer*>(malloc(refBufferSize));
80 fread(refBuffer, sizeof(Dali::Integration::PixelBuffer), refBufferSize, fp);
84 LoadFunctions::LoadFunctions(LoadBitmapHeaderFunction _header, LoadBitmapFunction _loader)
90 void TestImageLoading(const ImageDetails& image, const LoadFunctions& functions, Dali::Integration::Bitmap::Profile bitmapProfile)
92 FILE* fp = fopen(image.name.c_str(), "rb");
93 AutoCloseFile autoClose(fp);
94 DALI_TEST_CHECK(fp != NULL);
96 // Check the header file.
98 unsigned int width(0), height(0);
99 const Dali::ImageLoader::Input input(fp);
100 DALI_TEST_CHECK(functions.header(input, width, height));
102 DALI_TEST_EQUALS(width, image.reportedWidth, TEST_LOCATION);
103 DALI_TEST_EQUALS(height, image.reportedHeight, TEST_LOCATION);
105 // Loading the header moves the pointer within the file so reset to start of file.
108 Dali::Devel::PixelBuffer bitmap;
110 // Load Bitmap and check its return values.
111 DALI_TEST_CHECK(functions.loader(input, bitmap));
112 DALI_TEST_EQUALS(image.width, bitmap.GetWidth(), TEST_LOCATION);
113 DALI_TEST_EQUALS(image.height, bitmap.GetHeight(), TEST_LOCATION);
115 // Compare buffer generated with reference buffer.
116 Dali::Integration::PixelBuffer* bufferPtr(bitmap.GetBuffer());
117 Dali::Integration::PixelBuffer* refBufferPtr(image.refBuffer);
118 for(unsigned int i = 0; i < image.refBufferSize; ++i, ++bufferPtr, ++refBufferPtr)
120 if(*bufferPtr != *refBufferPtr)
122 tet_result(TET_FAIL);
123 tet_printf("%s Failed in %s at line %d\n", __PRETTY_FUNCTION__, __FILE__, __LINE__);
129 void CompareLoadedImageData(const ImageDetails& image, const LoadFunctions& functions, const uint32_t* master)
131 FILE* filePointer = fopen(image.name.c_str(), "rb");
132 AutoCloseFile autoClose(filePointer);
133 DALI_TEST_CHECK(filePointer != NULL);
135 // Check the header file.
136 unsigned int width = 0, height = 0;
137 const Dali::ImageLoader::Input input(filePointer);
138 DALI_TEST_CHECK(functions.header(input, width, height));
140 DALI_TEST_EQUALS(width, image.reportedWidth, TEST_LOCATION);
141 DALI_TEST_EQUALS(height, image.reportedHeight, TEST_LOCATION);
143 // Loading the header moves the pointer within the file so reset to start of file.
144 fseek(filePointer, 0, SEEK_SET);
146 Dali::Devel::PixelBuffer bitmap;
148 // Load Bitmap and check its return values.
149 DALI_TEST_CHECK(functions.loader(input, bitmap));
150 DALI_TEST_EQUALS(image.width, bitmap.GetWidth(), TEST_LOCATION);
151 DALI_TEST_EQUALS(image.height, bitmap.GetHeight(), TEST_LOCATION);
153 // Check the bytes per pixel.
154 const Pixel::Format pixelFormat = bitmap.GetPixelFormat();
155 const unsigned int bytesPerPixel = Pixel::GetBytesPerPixel(pixelFormat);
157 // Compare buffer generated with reference buffer.
158 Dali::Integration::PixelBuffer* pBitmapData(bitmap.GetBuffer());
159 const uint32_t* pMaster(master);
161 // Loop through each pixel in the bitmap.
162 for(unsigned int i = 0; i < image.refBufferSize; ++i, ++pMaster)
164 unsigned int color = 0;
165 // Loop through each byte per pixel, to build up a color value for that pixel.
166 for(unsigned int j = 0; j < bytesPerPixel; ++j, ++pBitmapData)
168 color = (color << 8) | *pBitmapData;
171 // Check the color value is what we expect.
172 DALI_TEST_EQUALS(color, *pMaster, TEST_LOCATION);
176 void DumpImageBufferToTempFile(std::string filename, std::string targetFilename, const LoadFunctions& functions)
178 FILE* fp = fopen(filename.c_str(), "rb");
179 AutoCloseFile autoClose(fp);
181 Dali::Devel::PixelBuffer bitmap;
182 const Dali::ImageLoader::Input input(fp);
184 DALI_TEST_CHECK(functions.loader(input, bitmap));
186 Dali::Integration::PixelBuffer* bufferPtr(bitmap.GetBuffer());
188 FILE* writeFp = fopen(targetFilename.c_str(), "wb");
189 AutoCloseFile autoCloseWrite(writeFp);
190 auto& impl = GetImplementation(bitmap);
191 fwrite(bufferPtr, 1, impl.GetBufferSize(), writeFp);