2 * Copyright (c) 2023 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.
19 #include <dali/internal/imaging/common/loader-jpeg.h>
23 #include <libexif/exif-data.h>
24 #include <libexif/exif-loader.h>
25 #include <libexif/exif-tag.h>
27 #include <turbojpeg.h>
34 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
35 #include <dali/public-api/object/property-array.h>
36 #include <dali/public-api/object/property-map.h>
39 #include <dali/devel-api/adaptor-framework/environment-variable.h>
40 #include <dali/devel-api/adaptor-framework/image-loading.h>
41 #include <dali/internal/imaging/common/image-operations.h>
42 #include <dali/internal/imaging/common/pixel-buffer-impl.h>
43 #include <dali/internal/legacy/tizen/platform-capabilities.h>
48 namespace Pixel = Dali::Pixel;
49 using PixelArray = uint8_t*;
50 const unsigned int DECODED_L8 = 1;
51 const unsigned int DECODED_RGB888 = 3;
52 const unsigned int DECODED_RGBA8888 = 4;
54 const char* CHROMINANCE_SUBSAMPLING_OPTIONS_ENV[] = {"DALI_ENABLE_DECODE_JPEG_TO_YUV_444",
55 "DALI_ENABLE_DECODE_JPEG_TO_YUV_422",
56 "DALI_ENABLE_DECODE_JPEG_TO_YUV_420",
58 "DALI_ENABLE_DECODE_JPEG_TO_YUV_440",
59 "DALI_ENABLE_DECODE_JPEG_TO_YUV_411"};
61 static bool gSubsamplingFormatTable[TJ_NUMSAMP] = {
64 static bool gIsSubsamplingFormatTableInitialized = false;
66 /** Transformations that can be applied to decoded pixels to respect exif orientation
67 * codes in image headers */
68 enum class JpegTransform
70 NONE, //< no transformation 0th-Row = top & 0th-Column = left
71 FLIP_HORIZONTAL, //< horizontal flip 0th-Row = top & 0th-Column = right
72 ROTATE_180, //< 180-degree rotation 0th-Row = bottom & 0th-Column = right
73 FLIP_VERTICAL, //< vertical flip 0th-Row = bottom & 0th-Column = left
74 TRANSPOSE, //< transpose across UL-to-LR axis 0th-Row = left & 0th-Column = top
75 ROTATE_90, //< 90-degree clockwise rotation 0th-Row = right & 0th-Column = top
76 TRANSVERSE, //< transpose across UR-to-LL axis 0th-Row = right & 0th-Column = bottom
77 ROTATE_270, //< 270-degree clockwise (or 90 ccw) 0th-Row = left & 0th-Column = bottom
81 * @brief Error handling bookeeping for the JPEG Turbo library's
82 * setjmp/longjmp simulated exceptions.
86 struct jpeg_error_mgr errorManager;
90 static bool IsSubsamplingFormatEnabled(int chrominanceSubsampling)
92 if(!gIsSubsamplingFormatTableInitialized)
94 for(int i = 0; i < TJ_NUMSAMP; i++)
96 auto valueString = Dali::EnvironmentVariable::GetEnvironmentVariable(CHROMINANCE_SUBSAMPLING_OPTIONS_ENV[i]);
97 gSubsamplingFormatTable[i] = valueString ? std::atoi(valueString) : false;
100 gIsSubsamplingFormatTableInitialized = true;
103 return gSubsamplingFormatTable[chrominanceSubsampling];
107 * @brief Called by the JPEG library when it hits an error.
108 * We jump out of the library so our loader code can return an error.
110 void JpegErrorHandler(j_common_ptr cinfo)
112 DALI_LOG_ERROR("JpegErrorHandler(): libjpeg-turbo fatal error in JPEG decoding.\n");
113 /* cinfo->err really points to a JpegErrorState struct, so coerce pointer */
114 JpegErrorState* myerr = reinterpret_cast<JpegErrorState*>(cinfo->err);
116 /* Return control to the setjmp point */
117 longjmp(myerr->jumpBuffer, 1);
120 void JpegOutputMessageHandler(j_common_ptr cinfo)
122 /* Stop libjpeg from printing to stderr - Do Nothing */
126 * LibJPEG Turbo tjDecompress2 API doesn't distinguish between errors that still allow
127 * the JPEG to be displayed and fatal errors.
129 bool IsJpegErrorFatal(const std::string& errorMessage)
131 if((errorMessage.find("Corrupt JPEG data") != std::string::npos) ||
132 (errorMessage.find("Invalid SOS parameters") != std::string::npos) ||
133 (errorMessage.find("Invalid JPEG file structure") != std::string::npos) ||
134 (errorMessage.find("Unsupported JPEG process") != std::string::npos) ||
135 (errorMessage.find("Unsupported marker type") != std::string::npos) ||
136 (errorMessage.find("Bogus marker length") != std::string::npos) ||
137 (errorMessage.find("Bogus DQT index") != std::string::npos) ||
138 (errorMessage.find("Bogus Huffman table definition") != std::string::npos))
145 bool IsJpegDecodingFailed()
147 std::string errorString = tjGetErrorStr();
149 if(DALI_UNLIKELY(IsJpegErrorFatal(errorString)))
151 DALI_LOG_ERROR("%s\n", errorString.c_str());
156 DALI_LOG_WARNING("%s\n", errorString.c_str());
161 // helpers for safe exif memory handling
162 using ExifHandle = std::unique_ptr<ExifData, decltype(exif_data_free)*>;
164 ExifHandle MakeNullExifData()
166 return ExifHandle{nullptr, exif_data_free};
169 ExifHandle MakeExifDataFromData(uint8_t* data, unsigned int size)
171 return ExifHandle{exif_data_new_from_data(data, size), exif_data_free};
174 // Helpers for safe Jpeg memory handling
175 using JpegHandle = std::unique_ptr<void /*tjhandle*/, decltype(tjDestroy)*>;
177 JpegHandle MakeJpegCompressor()
179 return JpegHandle{tjInitCompress(), tjDestroy};
182 JpegHandle MakeJpegDecompressor()
184 return JpegHandle{tjInitDecompress(), tjDestroy};
187 using JpegMemoryHandle = std::unique_ptr<uint8_t, decltype(tjFree)*>;
189 JpegMemoryHandle MakeJpegMemory()
191 return JpegMemoryHandle{nullptr, tjFree};
194 template<class T, class Deleter>
195 class UniquePointerSetter final
198 UniquePointerSetter(std::unique_ptr<T, Deleter>& uniquePointer)
199 : mUniquePointer(uniquePointer),
204 /// @brief Pointer to Pointer cast operator
210 /// @brief Destructor, reset the unique_ptr
211 ~UniquePointerSetter()
213 mUniquePointer.reset(mRawPointer);
217 std::unique_ptr<T, Deleter>& mUniquePointer;
221 template<typename T, typename Deleter>
222 UniquePointerSetter<T, Deleter> SetPointer(std::unique_ptr<T, Deleter>& uniquePointer)
224 return UniquePointerSetter<T, Deleter>{uniquePointer};
227 using TransformFunction = std::function<void(PixelArray, unsigned, unsigned)>;
228 using TransformFunctionArray = std::array<TransformFunction, 3>; // 1, 3 and 4 bytes per pixel
230 /// @brief Select the transform function depending on the pixel format
231 TransformFunction GetTransformFunction(const TransformFunctionArray& functions,
232 Pixel::Format pixelFormat)
234 auto function = TransformFunction{};
236 int decodedPixelSize = Pixel::GetBytesPerPixel(pixelFormat);
237 switch(decodedPixelSize)
241 function = functions[0];
246 function = functions[1];
249 case DECODED_RGBA8888:
251 function = functions[2];
256 DALI_LOG_ERROR("Transform operation not supported on this Pixel::Format!");
257 function = functions[1];
264 // Storing Exif fields as properties
265 template<class R, class V>
266 R ConvertExifNumeric(const ExifEntry& entry)
268 return static_cast<R>((*reinterpret_cast<V*>(entry.data)));
271 void AddExifFieldPropertyMap(Dali::Property::Map& out, const ExifEntry& entry, ExifIfd ifd)
273 auto shortName = std::string(exif_tag_get_name_in_ifd(entry.tag, ifd));
276 case EXIF_FORMAT_ASCII:
278 out.Insert(shortName, std::string(reinterpret_cast<char*>(entry.data), entry.size));
281 case EXIF_FORMAT_SHORT:
283 out.Insert(shortName, ConvertExifNumeric<int, uint16_t>(entry));
286 case EXIF_FORMAT_LONG:
288 out.Insert(shortName, ConvertExifNumeric<int, uint32_t>(entry));
291 case EXIF_FORMAT_SSHORT:
293 out.Insert(shortName, ConvertExifNumeric<int, int16_t>(entry));
296 case EXIF_FORMAT_SLONG:
298 out.Insert(shortName, ConvertExifNumeric<int, int32_t>(entry));
301 case EXIF_FORMAT_FLOAT:
303 out.Insert(shortName, ConvertExifNumeric<float, float>(entry));
306 case EXIF_FORMAT_DOUBLE:
308 out.Insert(shortName, ConvertExifNumeric<float, double>(entry));
311 case EXIF_FORMAT_RATIONAL:
313 auto values = reinterpret_cast<unsigned int*>(entry.data);
314 Dali::Property::Array array;
315 array.Add(static_cast<int>(values[0]));
316 array.Add(static_cast<int>(values[1]));
317 out.Insert(shortName, array);
320 case EXIF_FORMAT_SBYTE:
322 out.Insert(shortName, "EXIF_FORMAT_SBYTE Unsupported");
325 case EXIF_FORMAT_BYTE:
327 out.Insert(shortName, "EXIF_FORMAT_BYTE Unsupported");
330 case EXIF_FORMAT_SRATIONAL:
332 auto values = reinterpret_cast<int*>(entry.data);
333 Dali::Property::Array array;
334 array.Add(values[0]);
335 array.Add(values[1]);
336 out.Insert(shortName, array);
339 case EXIF_FORMAT_UNDEFINED:
342 std::stringstream ss;
343 ss << "EXIF_FORMAT_UNDEFINED, size: " << entry.size << ", components: " << entry.components;
344 out.Insert(shortName, ss.str());
349 /// @brief Apply a transform to a buffer
350 bool Transform(const TransformFunctionArray& transformFunctions,
354 Pixel::Format pixelFormat)
356 auto transformFunction = GetTransformFunction(transformFunctions, pixelFormat);
357 if(transformFunction)
359 transformFunction(buffer, width, height);
361 return bool(transformFunction);
364 /// @brief Auxiliar type to represent pixel data with different number of bytes
372 void Rotate180(PixelArray buffer, int width, int height)
374 // Destination pixel, set as the first pixel of screen
375 auto to = reinterpret_cast<PixelType<N>*>(buffer);
377 // Source pixel, as the image is flipped horizontally and vertically,
378 // the source pixel is the end of the buffer of size width * height
379 auto from = reinterpret_cast<PixelType<N>*>(buffer) + width * height - 1;
381 for(auto ix = 0, endLoop = (width * height) / 2; ix < endLoop; ++ix, ++to, --from)
383 std::swap(*from, *to);
388 void FlipHorizontal(PixelArray buffer, int width, int height)
390 for(auto iy = 0; iy < height; ++iy)
392 //Set the destination pixel as the beginning of the row
393 auto to = reinterpret_cast<PixelType<N>*>(buffer) + width * iy;
394 //Set the source pixel as the end of the row to flip in X axis
395 auto from = reinterpret_cast<PixelType<N>*>(buffer) + width * (iy + 1) - 1;
396 for(auto ix = 0; ix < width / 2; ++ix, ++to, --from)
398 std::swap(*from, *to);
404 void FlipVertical(PixelArray buffer, int width, int height)
406 //Transform vertically only
407 for(auto iy = 0; iy < height / 2; ++iy)
409 for(auto ix = 0; ix < width; ++ix)
411 auto to = reinterpret_cast<PixelType<N>*>(buffer) + iy * width + ix;
412 auto from = reinterpret_cast<PixelType<N>*>(buffer) + (height - 1 - iy) * width + ix;
413 std::swap(*from, *to);
419 void Transpose(PixelArray buffer, int width, int height)
421 using PixelT = PixelType<N>;
423 data.ResizeUninitialized(width * height);
424 auto dataPtr = data.Begin();
426 auto original = reinterpret_cast<PixelT*>(buffer);
427 std::copy(original, original + width * height, dataPtr);
430 for(auto iy = 0; iy < width; ++iy)
432 for(auto ix = 0; ix < height; ++ix, ++to)
434 auto from = dataPtr + ix * width + iy;
441 void Rotate90(PixelArray buffer, int width, int height)
443 using PixelT = PixelType<N>;
445 data.ResizeUninitialized(width * height);
446 auto dataPtr = data.Begin();
448 auto original = reinterpret_cast<PixelT*>(buffer);
449 std::copy(original, original + width * height, dataPtr);
451 std::swap(width, height);
452 auto hw = width * height;
455 auto to = original + width - 1;
458 for(auto ix = width; --ix >= 0;)
460 for(auto iy = height; --iy >= 0; ++from)
470 void Transverse(PixelArray buffer, int width, int height)
472 using PixelT = PixelType<N>;
474 data.ResizeUninitialized(width * height);
475 auto dataPtr = data.Begin();
477 auto original = reinterpret_cast<PixelT*>(buffer);
478 std::copy(original, original + width * height, dataPtr);
481 for(auto iy = 0; iy < width; iy++)
483 for(auto ix = 0; ix < height; ix++)
485 auto from = dataPtr + (height - ix) * width - 1 - iy;
493 void Rotate270(PixelArray buffer, int width, int height)
495 using PixelT = PixelType<N>;
497 data.ResizeUninitialized(width * height);
498 auto dataPtr = data.Begin();
500 auto original = reinterpret_cast<PixelT*>(buffer);
501 std::copy(original, original + width * height, dataPtr);
504 std::swap(width, height);
505 auto hw = width * height;
507 auto* to = original + hw - width;
508 auto* from = dataPtr;
512 for(auto ix = width; --ix >= 0;)
514 for(auto iy = height; --iy >= 0;)
524 void GetJpegPixelFormat(int jpegColorspace, TJPF& pixelLibJpegType, Pixel::Format& pixelFormat)
526 pixelLibJpegType = TJPF_RGB;
527 pixelFormat = Pixel::RGB888;
529 switch(jpegColorspace)
532 // YCbCr is not an absolute colorspace but rather a mathematical transformation of RGB designed solely for storage and transmission.
533 // YCbCr images must be converted to RGB before they can actually be displayed.
536 pixelLibJpegType = TJPF_RGB;
537 pixelFormat = Pixel::RGB888;
542 pixelLibJpegType = TJPF_GRAY;
543 pixelFormat = Pixel::L8;
549 pixelLibJpegType = TJPF_CMYK;
550 pixelFormat = Pixel::RGB888;
555 pixelLibJpegType = TJPF_RGB;
556 pixelFormat = Pixel::RGB888;
562 bool TransformBitmap(int scaledPreXformWidth, int scaledPreXformHeight, JpegTransform transform, uint8_t* bitmapPixelBuffer, Pixel::Format pixelFormat)
564 const unsigned int bufferWidth = Dali::TizenPlatform::GetTextureDimension(scaledPreXformWidth);
565 const unsigned int bufferHeight = Dali::TizenPlatform::GetTextureDimension(scaledPreXformHeight);
571 case JpegTransform::NONE:
576 // 3 orientation changes for a camera held perpendicular to the ground or upside-down:
577 case JpegTransform::ROTATE_180:
579 static auto rotate180Functions = TransformFunctionArray{
584 result = Transform(rotate180Functions, bitmapPixelBuffer, bufferWidth, bufferHeight, pixelFormat);
587 case JpegTransform::ROTATE_270:
589 static auto rotate270Functions = TransformFunctionArray{
594 result = Transform(rotate270Functions, bitmapPixelBuffer, bufferWidth, bufferHeight, pixelFormat);
597 case JpegTransform::ROTATE_90:
599 static auto rotate90Functions = TransformFunctionArray{
604 result = Transform(rotate90Functions, bitmapPixelBuffer, bufferWidth, bufferHeight, pixelFormat);
607 case JpegTransform::FLIP_VERTICAL:
609 static auto flipVerticalFunctions = TransformFunctionArray{
614 result = Transform(flipVerticalFunctions, bitmapPixelBuffer, bufferWidth, bufferHeight, pixelFormat);
617 // Less-common orientation changes, since they don't correspond to a camera's physical orientation:
618 case JpegTransform::FLIP_HORIZONTAL:
620 static auto flipHorizontalFunctions = TransformFunctionArray{
625 result = Transform(flipHorizontalFunctions, bitmapPixelBuffer, bufferWidth, bufferHeight, pixelFormat);
628 case JpegTransform::TRANSPOSE:
630 static auto transposeFunctions = TransformFunctionArray{
635 result = Transform(transposeFunctions, bitmapPixelBuffer, bufferWidth, bufferHeight, pixelFormat);
638 case JpegTransform::TRANSVERSE:
640 static auto transverseFunctions = TransformFunctionArray{
645 result = Transform(transverseFunctions, bitmapPixelBuffer, bufferWidth, bufferHeight, pixelFormat);
650 DALI_LOG_ERROR("Unsupported JPEG Orientation transformation: %x.\n", transform);
657 bool LoadJpegFile(const Dali::ImageLoader::Input& input, Vector<uint8_t>& jpegBuffer, unsigned int& jpegBufferSize)
659 FILE* const fp = input.file;
661 if(DALI_UNLIKELY(fseek(fp, 0, SEEK_END)))
663 DALI_LOG_ERROR("Error seeking to end of file\n");
667 long positionIndicator = ftell(fp);
669 if(positionIndicator > -1L)
671 jpegBufferSize = static_cast<unsigned int>(positionIndicator);
674 if(DALI_UNLIKELY(0u == jpegBufferSize))
676 DALI_LOG_ERROR("Jpeg buffer size error\n");
680 if(DALI_UNLIKELY(fseek(fp, 0, SEEK_SET)))
682 DALI_LOG_ERROR("Error seeking to start of file\n");
688 jpegBuffer.ResizeUninitialized(jpegBufferSize);
692 DALI_LOG_ERROR("Could not allocate temporary memory to hold JPEG file of size %uMB.\n", jpegBufferSize / 1048576U);
695 uint8_t* const jpegBufferPtr = jpegBuffer.Begin();
697 // Pull the compressed JPEG image bytes out of a file and into memory:
698 if(DALI_UNLIKELY(fread(jpegBufferPtr, 1, jpegBufferSize, fp) != jpegBufferSize))
700 DALI_LOG_ERROR("Error on image file read.\n");
704 if(DALI_UNLIKELY(fseek(fp, 0, SEEK_SET)))
706 DALI_LOG_ERROR("Error seeking to start of file\n");
714 * @brief Helper function to convert from Turbo Jpeg Pixel Format as TJPF_CMYK to RGB888 by naive method.
716 * @param[in] cmykBuffer buffer of cmyk.
717 * @param[in] rgbBuffer buffer of Pixel::RGB888
718 * @param[in] width width of image.
719 * @param[in] height height of image.
721 void ConvertTjpfCMYKToRGB888(PixelArray __restrict__ cmykBuffer, PixelArray __restrict__ rgbBuffer, int32_t width, int32_t height)
723 const int32_t pixelCount = width * height;
724 const uint8_t cmykBpp = 4u;
725 const uint8_t bpp = 3u;
727 const PixelArray cmykBufferEnd = cmykBuffer + pixelCount * cmykBpp;
728 // Convert every pixel
729 while(cmykBuffer != cmykBufferEnd)
731 const uint8_t channelK = *(cmykBuffer + 3u);
732 *(rgbBuffer + 0u) = Dali::Internal::Platform::MultiplyAndNormalizeColor(*(cmykBuffer + 0u), channelK);
733 *(rgbBuffer + 1u) = Dali::Internal::Platform::MultiplyAndNormalizeColor(*(cmykBuffer + 1u), channelK);
734 *(rgbBuffer + 2u) = Dali::Internal::Platform::MultiplyAndNormalizeColor(*(cmykBuffer + 2u), channelK);
735 cmykBuffer += cmykBpp;
743 namespace TizenPlatform
745 bool DecodeJpeg(const Dali::ImageLoader::Input& input, std::vector<Dali::Devel::PixelBuffer>& pixelBuffers, bool decodeToYuv);
746 JpegTransform ConvertExifOrientation(ExifData* exifData);
747 bool TransformSize(int requiredWidth, int requiredHeight, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, JpegTransform transform, int& preXformImageWidth, int& preXformImageHeight, int& postXformImageWidth, int& postXformImageHeight);
749 bool LoadJpegHeader(FILE* fp, unsigned int& width, unsigned int& height)
751 // using libjpeg API to avoid having to read the whole file in a buffer
752 struct jpeg_decompress_struct cinfo;
753 struct JpegErrorState jerr;
754 cinfo.err = jpeg_std_error(&jerr.errorManager);
756 jerr.errorManager.output_message = JpegOutputMessageHandler;
757 jerr.errorManager.error_exit = JpegErrorHandler;
759 // On error exit from the JPEG lib, control will pass via JpegErrorHandler
760 // into this branch body for cleanup and error return:
761 if(DALI_UNLIKELY(setjmp(jerr.jumpBuffer)))
763 DALI_LOG_ERROR("setjmp failed\n");
764 jpeg_destroy_decompress(&cinfo);
768 // jpeg_create_decompress internally uses C casts
769 #pragma GCC diagnostic push
770 #pragma GCC diagnostic ignored "-Wold-style-cast"
771 jpeg_create_decompress(&cinfo);
772 #pragma GCC diagnostic pop
774 jpeg_stdio_src(&cinfo, fp);
776 // Check header to see if it is JPEG file
777 if(DALI_UNLIKELY(jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK))
779 DALI_LOG_ERROR("jpeg_read_header failed\n");
781 jpeg_destroy_decompress(&cinfo);
785 width = cinfo.image_width;
786 height = cinfo.image_height;
788 jpeg_destroy_decompress(&cinfo);
792 bool LoadBitmapFromJpeg(const Dali::ImageLoader::Input& input, Dali::Devel::PixelBuffer& bitmap)
794 std::vector<Dali::Devel::PixelBuffer> pixelBuffers;
796 bool result = DecodeJpeg(input, pixelBuffers, false);
797 if(!result && pixelBuffers.empty())
803 bitmap = pixelBuffers[0];
808 bool LoadPlanesFromJpeg(const Dali::ImageLoader::Input& input, std::vector<Dali::Devel::PixelBuffer>& pixelBuffers)
810 return DecodeJpeg(input, pixelBuffers, true);
813 bool DecodeJpeg(const Dali::ImageLoader::Input& input, std::vector<Dali::Devel::PixelBuffer>& pixelBuffers, bool decodeToYuv)
815 Vector<uint8_t> jpegBuffer;
816 unsigned int jpegBufferSize = 0u;
818 if(!LoadJpegFile(input, jpegBuffer, jpegBufferSize))
820 DALI_LOG_ERROR("LoadJpegFile failed\n");
824 auto jpeg = MakeJpegDecompressor();
825 if(DALI_UNLIKELY(!jpeg))
827 DALI_LOG_ERROR("%s\n", tjGetErrorStr());
831 uint8_t* const jpegBufferPtr = jpegBuffer.Begin();
832 auto transform = JpegTransform::NONE;
835 auto exifData = MakeExifDataFromData(jpegBufferPtr, jpegBufferSize);
837 if(exifData && input.reorientationRequested)
839 transform = ConvertExifOrientation(exifData.get());
842 // Push jpeg data in memory buffer through TurboJPEG decoder to make a raw pixel array:
843 int chrominanceSubsampling = -1;
844 int preXformImageWidth = 0, preXformImageHeight = 0;
845 int jpegColorspace = -1;
847 if(tjDecompressHeader3(jpeg.get(), jpegBufferPtr, jpegBufferSize, &preXformImageWidth, &preXformImageHeight, &chrominanceSubsampling, &jpegColorspace) == -1)
849 DALI_LOG_ERROR("%s\n", tjGetErrorStr());
850 // Do not set width and height to 0 or return early as this sometimes fails only on determining subsampling type.
853 if(DALI_UNLIKELY(preXformImageWidth == 0 || preXformImageHeight == 0))
855 DALI_LOG_ERROR("Invalid Image!\n");
859 int requiredWidth = input.scalingParameters.dimensions.GetWidth();
860 int requiredHeight = input.scalingParameters.dimensions.GetHeight();
862 // If transform is a 90 or 270 degree rotation, the logical width and height
863 // request from the client needs to be adjusted to account by effectively
864 // rotating that too, and the final width and height need to be swapped:
865 int postXformImageWidth = preXformImageWidth;
866 int postXformImageHeight = preXformImageHeight;
868 int scaledPreXformWidth = preXformImageWidth;
869 int scaledPreXformHeight = preXformImageHeight;
870 int scaledPostXformWidth = postXformImageWidth;
871 int scaledPostXformHeight = postXformImageHeight;
873 TransformSize(requiredWidth, requiredHeight, input.scalingParameters.scalingMode, input.scalingParameters.samplingMode, transform, scaledPreXformWidth, scaledPreXformHeight, scaledPostXformWidth, scaledPostXformHeight);
877 // Check decoding format
878 if(decodeToYuv && IsSubsamplingFormatEnabled(chrominanceSubsampling) && transform == JpegTransform::NONE)
882 // Allocate buffers for each plane and decompress the jpeg buffer into the buffers
883 for(int i = 0; i < 3; i++)
885 auto planeSize = tjPlaneSizeYUV(i, scaledPostXformWidth, 0, scaledPostXformHeight, chrominanceSubsampling);
887 uint8_t* buffer = static_cast<uint8_t*>(malloc(planeSize));
890 DALI_LOG_ERROR("Buffer allocation is failed [%d]\n", planeSize);
891 pixelBuffers.clear();
895 int width, height, planeWidth;
896 Pixel::Format pixelFormat = Pixel::RGB888;
901 width = scaledPostXformWidth;
902 height = scaledPostXformHeight;
903 planeWidth = tjPlaneWidth(i, scaledPostXformWidth, chrominanceSubsampling);
904 pixelFormat = Pixel::L8;
909 width = tjPlaneWidth(i, scaledPostXformWidth, chrominanceSubsampling);
910 height = tjPlaneHeight(i, scaledPostXformHeight, chrominanceSubsampling);
912 pixelFormat = (i == 1 ? Pixel::CHROMINANCE_U : Pixel::CHROMINANCE_V);
915 Internal::Adaptor::PixelBufferPtr internal = Internal::Adaptor::PixelBuffer::New(buffer, planeSize, width, height, planeWidth, pixelFormat);
916 Dali::Devel::PixelBuffer bitmap = Devel::PixelBuffer(internal.Get());
918 pixelBuffers.push_back(bitmap);
923 int decodeResult = tjDecompressToYUVPlanes(jpeg.get(), jpegBufferPtr, jpegBufferSize, reinterpret_cast<uint8_t**>(&planes), scaledPostXformWidth, nullptr, scaledPostXformHeight, flags);
924 if(DALI_UNLIKELY(decodeResult == -1 && IsJpegDecodingFailed()))
926 pixelBuffers.clear();
934 // Colorspace conversion options
935 TJPF pixelLibJpegType = TJPF_RGB;
936 Pixel::Format pixelFormat = Pixel::RGB888;
938 GetJpegPixelFormat(jpegColorspace, pixelLibJpegType, pixelFormat);
940 // Allocate a bitmap and decompress the jpeg buffer into its pixel buffer:
941 Dali::Devel::PixelBuffer bitmap = Dali::Devel::PixelBuffer::New(scaledPostXformWidth, scaledPostXformHeight, pixelFormat);
944 if(DALI_LIKELY(exifData))
946 std::unique_ptr<Property::Map> exifMap = std::make_unique<Property::Map>();
948 for(auto k = 0u; k < EXIF_IFD_COUNT; ++k)
950 auto content = exifData->ifd[k];
951 for(auto i = 0u; i < content->count; ++i)
953 auto&& tag = content->entries[i];
954 const char* shortName = exif_tag_get_name_in_ifd(tag->tag, static_cast<ExifIfd>(k));
957 AddExifFieldPropertyMap(*exifMap, *tag, static_cast<ExifIfd>(k));
962 GetImplementation(bitmap).SetMetadata(std::move(exifMap));
965 auto bitmapPixelBuffer = bitmap.GetBuffer();
968 int decodeResult = -1;
969 if(pixelLibJpegType == TJPF_CMYK)
971 // Currently we support only for 4 bytes per each CMYK pixel.
972 const uint8_t cmykBytesPerPixel = 4u;
974 uint8_t* cmykBuffer = static_cast<uint8_t*>(malloc(sizeof(uint8_t) * scaledPostXformWidth * scaledPostXformHeight * cmykBytesPerPixel));
976 decodeResult = tjDecompress2(jpeg.get(), jpegBufferPtr, jpegBufferSize, reinterpret_cast<uint8_t*>(cmykBuffer), scaledPreXformWidth, 0, scaledPreXformHeight, pixelLibJpegType, flags);
977 if(DALI_UNLIKELY(decodeResult == -1 && IsJpegDecodingFailed()))
982 ConvertTjpfCMYKToRGB888(cmykBuffer, bitmapPixelBuffer, scaledPostXformWidth, scaledPostXformHeight);
988 decodeResult = tjDecompress2(jpeg.get(), jpegBufferPtr, jpegBufferSize, reinterpret_cast<uint8_t*>(bitmapPixelBuffer), scaledPreXformWidth, 0, scaledPreXformHeight, pixelLibJpegType, flags);
989 if(DALI_UNLIKELY(decodeResult == -1 && IsJpegDecodingFailed()))
994 pixelBuffers.push_back(bitmap);
997 result = TransformBitmap(scaledPreXformWidth, scaledPreXformHeight, transform, bitmapPixelBuffer, pixelFormat);
1003 bool EncodeToJpeg(const uint8_t* const pixelBuffer, Vector<uint8_t>& encodedPixels, const std::size_t width, const std::size_t height, const Pixel::Format pixelFormat, unsigned quality)
1005 if(DALI_UNLIKELY(!pixelBuffer))
1007 DALI_LOG_ERROR("Null input buffer\n");
1011 // Translate pixel format enum:
1012 int jpegPixelFormat = -1;
1018 jpegPixelFormat = TJPF_GRAY;
1023 jpegPixelFormat = TJPF_RGB;
1026 case Pixel::RGBA8888:
1028 // Ignore the alpha:
1029 jpegPixelFormat = TJPF_RGBX;
1032 case Pixel::BGRA8888:
1034 // Ignore the alpha:
1035 jpegPixelFormat = TJPF_BGRX;
1040 DALI_LOG_ERROR("Unsupported pixel format for encoding to JPEG. Format enum : [%d]\n", pixelFormat);
1045 // Assert quality is in the documented allowable range of the jpeg-turbo lib:
1046 DALI_ASSERT_DEBUG(quality >= 1);
1047 DALI_ASSERT_DEBUG(quality <= 100);
1057 // Initialise a JPEG codec:
1059 auto jpeg = MakeJpegCompressor();
1060 if(DALI_UNLIKELY(!jpeg))
1062 DALI_LOG_ERROR("JPEG Compressor init failed: %s\n", tjGetErrorStr());
1066 // Safely wrap the jpeg codec's buffer in case we are about to throw, then
1067 // save the pixels to a persistent buffer that we own and let our cleaner
1068 // class clean up the buffer as it goes out of scope:
1069 auto dstBuffer = MakeJpegMemory();
1071 // Run the compressor:
1072 unsigned long dstBufferSize = 0;
1073 const int flags = 0;
1075 if(DALI_UNLIKELY(tjCompress2(jpeg.get(),
1076 const_cast<uint8_t*>(pixelBuffer),
1081 SetPointer(dstBuffer),
1087 DALI_LOG_ERROR("JPEG Compression failed: %s\n", tjGetErrorStr());
1091 encodedPixels.ResizeUninitialized(dstBufferSize);
1092 memcpy(encodedPixels.Begin(), dstBuffer.get(), dstBufferSize);
1097 JpegTransform ConvertExifOrientation(ExifData* exifData)
1099 auto transform = JpegTransform::NONE;
1100 ExifEntry* const entry = exif_data_get_entry(exifData, EXIF_TAG_ORIENTATION);
1101 int orientation = 0;
1104 orientation = exif_get_short(entry->data, exif_data_get_byte_order(entry->parent->parent));
1109 transform = JpegTransform::NONE;
1114 transform = JpegTransform::FLIP_HORIZONTAL;
1119 transform = JpegTransform::ROTATE_180;
1124 transform = JpegTransform::FLIP_VERTICAL;
1129 transform = JpegTransform::TRANSPOSE;
1134 transform = JpegTransform::ROTATE_90;
1139 transform = JpegTransform::TRANSVERSE;
1144 transform = JpegTransform::ROTATE_270;
1149 // Try to keep loading the file, but let app developer know there was something fishy:
1150 DALI_LOG_WARNING("Incorrect/Unknown Orientation setting found in EXIF header of JPEG image (%x). Orientation setting will be ignored.\n", entry);
1158 bool TransformSize(int requiredWidth, int requiredHeight, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, JpegTransform transform, int& preXformImageWidth, int& preXformImageHeight, int& postXformImageWidth, int& postXformImageHeight)
1160 bool success = true;
1161 if(transform == JpegTransform::TRANSPOSE || transform == JpegTransform::ROTATE_90 || transform == JpegTransform::TRANSVERSE || transform == JpegTransform::ROTATE_270)
1163 std::swap(requiredWidth, requiredHeight);
1164 std::swap(postXformImageWidth, postXformImageHeight);
1167 // Apply the special rules for when there are one or two zeros in requested dimensions:
1168 const ImageDimensions correctedDesired = Internal::Platform::CalculateDesiredDimensions(ImageDimensions(postXformImageWidth, postXformImageHeight), ImageDimensions(requiredWidth, requiredHeight));
1169 requiredWidth = correctedDesired.GetWidth();
1170 requiredHeight = correctedDesired.GetHeight();
1172 // Rescale image during decode using one of the decoder's built-in rescaling
1173 // ratios (expected to be powers of 2), keeping the final image at least as
1174 // wide and high as was requested:
1177 tjscalingfactor* factors = tjGetScalingFactors(&numFactors);
1178 if(DALI_UNLIKELY(factors == NULL))
1180 DALI_LOG_WARNING("TurboJpeg tjGetScalingFactors error!\n");
1185 // Internal jpeg downscaling is the same as our BOX_X sampling modes so only
1186 // apply it if the application requested one of those:
1187 // (use a switch case here so this code will fail to compile if other modes are added)
1188 bool downscale = true;
1189 switch(samplingMode)
1191 case SamplingMode::BOX:
1192 case SamplingMode::BOX_THEN_NEAREST:
1193 case SamplingMode::BOX_THEN_LINEAR:
1194 case SamplingMode::DONT_CARE:
1199 case SamplingMode::NO_FILTER:
1200 case SamplingMode::NEAREST:
1201 case SamplingMode::LINEAR:
1208 int scaleFactorIndex(0);
1211 // Find nearest supported scaling factor (factors are in sequential order, getting smaller)
1212 for(int i = 1; i < numFactors; ++i)
1214 bool widthLessRequired = TJSCALED(postXformImageWidth, factors[i]) < requiredWidth;
1215 bool heightLessRequired = TJSCALED(postXformImageHeight, factors[i]) < requiredHeight;
1216 // If either scaled dimension is smaller than the desired one, we were done at the last iteration
1217 if((fittingMode == FittingMode::SCALE_TO_FILL) && (widthLessRequired || heightLessRequired))
1221 // If both dimensions are smaller than the desired one, we were done at the last iteration:
1222 if((fittingMode == FittingMode::SHRINK_TO_FIT) && (widthLessRequired && heightLessRequired))
1226 // If the width is smaller than the desired one, we were done at the last iteration:
1227 if(fittingMode == FittingMode::FIT_WIDTH && widthLessRequired)
1231 // If the width is smaller than the desired one, we were done at the last iteration:
1232 if(fittingMode == FittingMode::FIT_HEIGHT && heightLessRequired)
1236 // This factor stays is within our fitting mode constraint so remember it:
1237 scaleFactorIndex = i;
1241 // Regardless of requested size, downscale to avoid exceeding the maximum texture size:
1242 for(int i = scaleFactorIndex; i < numFactors; ++i)
1244 // Continue downscaling to below maximum texture size (if possible)
1245 scaleFactorIndex = i;
1247 if(TJSCALED(postXformImageWidth, (factors[i])) < static_cast<int>(Dali::GetMaxTextureSize()) &&
1248 TJSCALED(postXformImageHeight, (factors[i])) < static_cast<int>(Dali::GetMaxTextureSize()))
1250 // Current scale-factor downscales to below maximum texture size
1255 // We have finally chosen the scale-factor, return width/height values
1256 if(scaleFactorIndex > 0)
1258 preXformImageWidth = TJSCALED(preXformImageWidth, (factors[scaleFactorIndex]));
1259 preXformImageHeight = TJSCALED(preXformImageHeight, (factors[scaleFactorIndex]));
1260 postXformImageWidth = TJSCALED(postXformImageWidth, (factors[scaleFactorIndex]));
1261 postXformImageHeight = TJSCALED(postXformImageHeight, (factors[scaleFactorIndex]));
1268 ExifHandle LoadExifData(FILE* fp)
1270 auto exifData = MakeNullExifData();
1271 uint8_t dataBuffer[1024];
1273 if(DALI_UNLIKELY(fseek(fp, 0, SEEK_SET)))
1275 DALI_LOG_ERROR("Error seeking to start of file\n");
1279 auto exifLoader = std::unique_ptr<ExifLoader, decltype(exif_loader_unref)*>{
1280 exif_loader_new(), exif_loader_unref};
1284 int size = fread(dataBuffer, 1, sizeof(dataBuffer), fp);
1289 if(!exif_loader_write(exifLoader.get(), dataBuffer, size))
1295 exifData.reset(exif_loader_get_data(exifLoader.get()));
1301 bool LoadJpegHeader(const Dali::ImageLoader::Input& input, unsigned int& width, unsigned int& height)
1303 unsigned int requiredWidth = input.scalingParameters.dimensions.GetWidth();
1304 unsigned int requiredHeight = input.scalingParameters.dimensions.GetHeight();
1305 FILE* const fp = input.file;
1307 bool success = false;
1309 unsigned int headerWidth;
1310 unsigned int headerHeight;
1312 success = LoadJpegHeader(fp, headerWidth, headerHeight);
1313 if(DALI_LIKELY(success))
1315 auto transform = JpegTransform::NONE;
1317 if(input.reorientationRequested)
1319 auto exifData = LoadExifData(fp);
1322 transform = ConvertExifOrientation(exifData.get());
1326 if(requiredWidth == 0 && requiredHeight == 0)
1328 if(transform == JpegTransform::TRANSPOSE || transform == JpegTransform::ROTATE_90 || transform == JpegTransform::TRANSVERSE || transform == JpegTransform::ROTATE_270)
1330 std::swap(headerWidth, headerHeight);
1335 int preXformImageWidth = headerWidth;
1336 int preXformImageHeight = headerHeight;
1337 int postXformImageWidth = headerWidth;
1338 int postXformImageHeight = headerHeight;
1340 success = TransformSize(requiredWidth, requiredHeight, input.scalingParameters.scalingMode, input.scalingParameters.samplingMode, transform, preXformImageWidth, preXformImageHeight, postXformImageWidth, postXformImageHeight);
1341 if(DALI_LIKELY(success))
1343 headerWidth = postXformImageWidth;
1344 headerHeight = postXformImageHeight;
1347 width = headerWidth;
1348 height = headerHeight;
1354 } // namespace TizenPlatform