2 * Copyright (c) 2021 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/image-loading.h>
40 #include <dali/internal/imaging/common/image-operations.h>
41 #include <dali/internal/imaging/common/pixel-buffer-impl.h>
42 #include <dali/internal/legacy/tizen/platform-capabilities.h>
47 namespace Pixel = Dali::Pixel;
48 using PixelArray = unsigned char*;
49 const unsigned int DECODED_L8 = 1;
50 const unsigned int DECODED_RGB888 = 3;
51 const unsigned int DECODED_RGBA8888 = 4;
53 /** Transformations that can be applied to decoded pixels to respect exif orientation
54 * codes in image headers */
55 enum class JpegTransform
57 NONE, //< no transformation 0th-Row = top & 0th-Column = left
58 FLIP_HORIZONTAL, //< horizontal flip 0th-Row = top & 0th-Column = right
59 ROTATE_180, //< 180-degree rotation 0th-Row = bottom & 0th-Column = right
60 FLIP_VERTICAL, //< vertical flip 0th-Row = bottom & 0th-Column = left
61 TRANSPOSE, //< transpose across UL-to-LR axis 0th-Row = left & 0th-Column = top
62 ROTATE_90, //< 90-degree clockwise rotation 0th-Row = right & 0th-Column = top
63 TRANSVERSE, //< transpose across UR-to-LL axis 0th-Row = right & 0th-Column = bottom
64 ROTATE_270, //< 270-degree clockwise (or 90 ccw) 0th-Row = left & 0th-Column = bottom
68 * @brief Error handling bookeeping for the JPEG Turbo library's
69 * setjmp/longjmp simulated exceptions.
73 struct jpeg_error_mgr errorManager;
78 * @brief Called by the JPEG library when it hits an error.
79 * We jump out of the library so our loader code can return an error.
81 void JpegErrorHandler(j_common_ptr cinfo)
83 DALI_LOG_ERROR("JpegErrorHandler(): libjpeg-turbo fatal error in JPEG decoding.\n");
84 /* cinfo->err really points to a JpegErrorState struct, so coerce pointer */
85 JpegErrorState* myerr = reinterpret_cast<JpegErrorState*>(cinfo->err);
87 /* Return control to the setjmp point */
88 longjmp(myerr->jumpBuffer, 1);
91 void JpegOutputMessageHandler(j_common_ptr cinfo)
93 /* Stop libjpeg from printing to stderr - Do Nothing */
97 * LibJPEG Turbo tjDecompress2 API doesn't distinguish between errors that still allow
98 * the JPEG to be displayed and fatal errors.
100 bool IsJpegErrorFatal(const std::string& errorMessage)
102 if((errorMessage.find("Corrupt JPEG data") != std::string::npos) ||
103 (errorMessage.find("Invalid SOS parameters") != std::string::npos) ||
104 (errorMessage.find("Invalid JPEG file structure") != std::string::npos) ||
105 (errorMessage.find("Unsupported JPEG process") != std::string::npos) ||
106 (errorMessage.find("Unsupported marker type") != std::string::npos) ||
107 (errorMessage.find("Bogus marker length") != std::string::npos) ||
108 (errorMessage.find("Bogus DQT index") != std::string::npos) ||
109 (errorMessage.find("Bogus Huffman table definition") != std::string::npos))
116 // helpers for safe exif memory handling
117 using ExifHandle = std::unique_ptr<ExifData, decltype(exif_data_free)*>;
119 ExifHandle MakeNullExifData()
121 return ExifHandle{nullptr, exif_data_free};
124 ExifHandle MakeExifDataFromData(unsigned char* data, unsigned int size)
126 return ExifHandle{exif_data_new_from_data(data, size), exif_data_free};
129 // Helpers for safe Jpeg memory handling
130 using JpegHandle = std::unique_ptr<void /*tjhandle*/, decltype(tjDestroy)*>;
132 JpegHandle MakeJpegCompressor()
134 return JpegHandle{tjInitCompress(), tjDestroy};
137 JpegHandle MakeJpegDecompressor()
139 return JpegHandle{tjInitDecompress(), tjDestroy};
142 using JpegMemoryHandle = std::unique_ptr<unsigned char, decltype(tjFree)*>;
144 JpegMemoryHandle MakeJpegMemory()
146 return JpegMemoryHandle{nullptr, tjFree};
149 template<class T, class Deleter>
150 class UniquePointerSetter final
153 UniquePointerSetter(std::unique_ptr<T, Deleter>& uniquePointer)
154 : mUniquePointer(uniquePointer),
159 /// @brief Pointer to Pointer cast operator
165 /// @brief Destructor, reset the unique_ptr
166 ~UniquePointerSetter()
168 mUniquePointer.reset(mRawPointer);
172 std::unique_ptr<T, Deleter>& mUniquePointer;
176 template<typename T, typename Deleter>
177 UniquePointerSetter<T, Deleter> SetPointer(std::unique_ptr<T, Deleter>& uniquePointer)
179 return UniquePointerSetter<T, Deleter>{uniquePointer};
182 using TransformFunction = std::function<void(PixelArray, unsigned, unsigned)>;
183 using TransformFunctionArray = std::array<TransformFunction, 3>; // 1, 3 and 4 bytes per pixel
185 /// @brief Select the transform function depending on the pixel format
186 TransformFunction GetTransformFunction(const TransformFunctionArray& functions,
187 Pixel::Format pixelFormat)
189 auto function = TransformFunction{};
191 int decodedPixelSize = Pixel::GetBytesPerPixel(pixelFormat);
192 switch(decodedPixelSize)
196 function = functions[0];
201 function = functions[1];
204 case DECODED_RGBA8888:
206 function = functions[2];
211 DALI_LOG_ERROR("Transform operation not supported on this Pixel::Format!");
212 function = functions[1];
219 // Storing Exif fields as properties
220 template<class R, class V>
221 R ConvertExifNumeric(const ExifEntry& entry)
223 return static_cast<R>((*reinterpret_cast<V*>(entry.data)));
226 void AddExifFieldPropertyMap(Dali::Property::Map& out, const ExifEntry& entry, ExifIfd ifd)
228 auto shortName = std::string(exif_tag_get_name_in_ifd(entry.tag, ifd));
231 case EXIF_FORMAT_ASCII:
233 out.Insert(shortName, std::string(reinterpret_cast<char*>(entry.data), entry.size));
236 case EXIF_FORMAT_SHORT:
238 out.Insert(shortName, ConvertExifNumeric<int, uint16_t>(entry));
241 case EXIF_FORMAT_LONG:
243 out.Insert(shortName, ConvertExifNumeric<int, uint32_t>(entry));
246 case EXIF_FORMAT_SSHORT:
248 out.Insert(shortName, ConvertExifNumeric<int, int16_t>(entry));
251 case EXIF_FORMAT_SLONG:
253 out.Insert(shortName, ConvertExifNumeric<int, int32_t>(entry));
256 case EXIF_FORMAT_FLOAT:
258 out.Insert(shortName, ConvertExifNumeric<float, float>(entry));
261 case EXIF_FORMAT_DOUBLE:
263 out.Insert(shortName, ConvertExifNumeric<float, double>(entry));
266 case EXIF_FORMAT_RATIONAL:
268 auto values = reinterpret_cast<unsigned int*>(entry.data);
269 Dali::Property::Array array;
270 array.Add(static_cast<int>(values[0]));
271 array.Add(static_cast<int>(values[1]));
272 out.Insert(shortName, array);
275 case EXIF_FORMAT_SBYTE:
277 out.Insert(shortName, "EXIF_FORMAT_SBYTE Unsupported");
280 case EXIF_FORMAT_BYTE:
282 out.Insert(shortName, "EXIF_FORMAT_BYTE Unsupported");
285 case EXIF_FORMAT_SRATIONAL:
287 auto values = reinterpret_cast<int*>(entry.data);
288 Dali::Property::Array array;
289 array.Add(values[0]);
290 array.Add(values[1]);
291 out.Insert(shortName, array);
294 case EXIF_FORMAT_UNDEFINED:
297 std::stringstream ss;
298 ss << "EXIF_FORMAT_UNDEFINED, size: " << entry.size << ", components: " << entry.components;
299 out.Insert(shortName, ss.str());
304 /// @brief Apply a transform to a buffer
305 bool Transform(const TransformFunctionArray& transformFunctions,
309 Pixel::Format pixelFormat)
311 auto transformFunction = GetTransformFunction(transformFunctions, pixelFormat);
312 if(transformFunction)
314 transformFunction(buffer, width, height);
316 return bool(transformFunction);
319 /// @brief Auxiliar type to represent pixel data with different number of bytes
327 void Rotate180(PixelArray buffer, int width, int height)
329 // Destination pixel, set as the first pixel of screen
330 auto to = reinterpret_cast<PixelType<N>*>(buffer);
332 // Source pixel, as the image is flipped horizontally and vertically,
333 // the source pixel is the end of the buffer of size width * height
334 auto from = reinterpret_cast<PixelType<N>*>(buffer) + width * height - 1;
336 for(auto ix = 0, endLoop = (width * height) / 2; ix < endLoop; ++ix, ++to, --from)
338 std::swap(*from, *to);
343 void FlipHorizontal(PixelArray buffer, int width, int height)
345 for(auto iy = 0; iy < height; ++iy)
347 //Set the destination pixel as the beginning of the row
348 auto to = reinterpret_cast<PixelType<N>*>(buffer) + width * iy;
349 //Set the source pixel as the end of the row to flip in X axis
350 auto from = reinterpret_cast<PixelType<N>*>(buffer) + width * (iy + 1) - 1;
351 for(auto ix = 0; ix < width / 2; ++ix, ++to, --from)
353 std::swap(*from, *to);
359 void FlipVertical(PixelArray buffer, int width, int height)
361 //Transform vertically only
362 for(auto iy = 0; iy < height / 2; ++iy)
364 for(auto ix = 0; ix < width; ++ix)
366 auto to = reinterpret_cast<PixelType<N>*>(buffer) + iy * width + ix;
367 auto from = reinterpret_cast<PixelType<N>*>(buffer) + (height - 1 - iy) * width + ix;
368 std::swap(*from, *to);
374 void Transpose(PixelArray buffer, int width, int height)
376 using PixelT = PixelType<N>;
378 data.Resize(width * height);
379 auto dataPtr = data.Begin();
381 auto original = reinterpret_cast<PixelT*>(buffer);
382 std::copy(original, original + width * height, dataPtr);
385 for(auto iy = 0; iy < width; ++iy)
387 for(auto ix = 0; ix < height; ++ix, ++to)
389 auto from = dataPtr + ix * width + iy;
396 void Rotate90(PixelArray buffer, int width, int height)
398 using PixelT = PixelType<N>;
400 data.Resize(width * height);
401 auto dataPtr = data.Begin();
403 auto original = reinterpret_cast<PixelT*>(buffer);
404 std::copy(original, original + width * height, dataPtr);
406 std::swap(width, height);
407 auto hw = width * height;
410 auto to = original + width - 1;
413 for(auto ix = width; --ix >= 0;)
415 for(auto iy = height; --iy >= 0; ++from)
425 void Transverse(PixelArray buffer, int width, int height)
427 using PixelT = PixelType<N>;
429 data.Resize(width * height);
430 auto dataPtr = data.Begin();
432 auto original = reinterpret_cast<PixelT*>(buffer);
433 std::copy(original, original + width * height, dataPtr);
436 for(auto iy = 0; iy < width; iy++)
438 for(auto ix = 0; ix < height; ix++)
440 auto from = dataPtr + (height - ix) * width - 1 - iy;
448 void Rotate270(PixelArray buffer, int width, int height)
450 using PixelT = PixelType<N>;
452 data.Resize(width * height);
453 auto dataPtr = data.Begin();
455 auto original = reinterpret_cast<PixelT*>(buffer);
456 std::copy(original, original + width * height, dataPtr);
459 std::swap(width, height);
460 auto hw = width * height;
462 auto* to = original + hw - width;
463 auto* from = dataPtr;
467 for(auto ix = width; --ix >= 0;)
469 for(auto iy = height; --iy >= 0;)
483 namespace TizenPlatform
485 JpegTransform ConvertExifOrientation(ExifData* exifData);
486 bool TransformSize(int requiredWidth, int requiredHeight, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, JpegTransform transform, int& preXformImageWidth, int& preXformImageHeight, int& postXformImageWidth, int& postXformImageHeight);
488 bool LoadJpegHeader(FILE* fp, unsigned int& width, unsigned int& height)
490 // using libjpeg API to avoid having to read the whole file in a buffer
491 struct jpeg_decompress_struct cinfo;
492 struct JpegErrorState jerr;
493 cinfo.err = jpeg_std_error(&jerr.errorManager);
495 jerr.errorManager.output_message = JpegOutputMessageHandler;
496 jerr.errorManager.error_exit = JpegErrorHandler;
498 // On error exit from the JPEG lib, control will pass via JpegErrorHandler
499 // into this branch body for cleanup and error return:
500 if(setjmp(jerr.jumpBuffer))
502 jpeg_destroy_decompress(&cinfo);
506 // jpeg_create_decompress internally uses C casts
507 #pragma GCC diagnostic push
508 #pragma GCC diagnostic ignored "-Wold-style-cast"
509 jpeg_create_decompress(&cinfo);
510 #pragma GCC diagnostic pop
512 jpeg_stdio_src(&cinfo, fp);
514 // Check header to see if it is JPEG file
515 if(jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK)
518 jpeg_destroy_decompress(&cinfo);
522 width = cinfo.image_width;
523 height = cinfo.image_height;
525 jpeg_destroy_decompress(&cinfo);
529 bool LoadBitmapFromJpeg(const Dali::ImageLoader::Input& input, Dali::Devel::PixelBuffer& bitmap)
532 FILE* const fp = input.file;
534 if(fseek(fp, 0, SEEK_END))
536 DALI_LOG_ERROR("Error seeking to end of file\n");
540 long positionIndicator = ftell(fp);
541 unsigned int jpegBufferSize = 0u;
542 if(positionIndicator > -1L)
544 jpegBufferSize = static_cast<unsigned int>(positionIndicator);
547 if(0u == jpegBufferSize)
552 if(fseek(fp, 0, SEEK_SET))
554 DALI_LOG_ERROR("Error seeking to start of file\n");
558 Vector<unsigned char> jpegBuffer;
561 jpegBuffer.Resize(jpegBufferSize);
565 DALI_LOG_ERROR("Could not allocate temporary memory to hold JPEG file of size %uMB.\n", jpegBufferSize / 1048576U);
568 unsigned char* const jpegBufferPtr = jpegBuffer.Begin();
570 // Pull the compressed JPEG image bytes out of a file and into memory:
571 if(fread(jpegBufferPtr, 1, jpegBufferSize, fp) != jpegBufferSize)
573 DALI_LOG_WARNING("Error on image file read.\n");
577 if(fseek(fp, 0, SEEK_SET))
579 DALI_LOG_ERROR("Error seeking to start of file\n");
582 auto jpeg = MakeJpegDecompressor();
586 DALI_LOG_ERROR("%s\n", tjGetErrorStr());
590 auto transform = JpegTransform::NONE;
593 auto exifData = MakeExifDataFromData(jpegBufferPtr, jpegBufferSize);
595 if(exifData && input.reorientationRequested)
597 transform = ConvertExifOrientation(exifData.get());
600 std::unique_ptr<Property::Map> exifMap;
601 exifMap.reset(new Property::Map());
603 for(auto k = 0u; k < EXIF_IFD_COUNT; ++k)
605 auto content = exifData->ifd[k];
606 for(auto i = 0u; i < content->count; ++i)
608 auto&& tag = content->entries[i];
609 const char* shortName = exif_tag_get_name_in_ifd(tag->tag, static_cast<ExifIfd>(k));
612 AddExifFieldPropertyMap(*exifMap, *tag, static_cast<ExifIfd>(k));
617 // Push jpeg data in memory buffer through TurboJPEG decoder to make a raw pixel array:
618 int chrominanceSubsampling = -1;
619 int preXformImageWidth = 0, preXformImageHeight = 0;
621 // In Ubuntu, the turbojpeg version is not correct. so build error occurs.
622 // Temporarily separate Ubuntu and other profiles.
623 #ifndef DALI_PROFILE_UBUNTU
624 int jpegColorspace = -1;
625 if(tjDecompressHeader3(jpeg.get(), jpegBufferPtr, jpegBufferSize, &preXformImageWidth, &preXformImageHeight, &chrominanceSubsampling, &jpegColorspace) == -1)
627 DALI_LOG_ERROR("%s\n", tjGetErrorStr());
628 // Do not set width and height to 0 or return early as this sometimes fails only on determining subsampling type.
631 if(tjDecompressHeader2(jpeg.get(), jpegBufferPtr, jpegBufferSize, &preXformImageWidth, &preXformImageHeight, &chrominanceSubsampling) == -1)
633 DALI_LOG_ERROR("%s\n", tjGetErrorStr());
634 // Do not set width and height to 0 or return early as this sometimes fails only on determining subsampling type.
638 if(preXformImageWidth == 0 || preXformImageHeight == 0)
640 DALI_LOG_WARNING("Invalid Image!\n");
644 int requiredWidth = input.scalingParameters.dimensions.GetWidth();
645 int requiredHeight = input.scalingParameters.dimensions.GetHeight();
647 // If transform is a 90 or 270 degree rotation, the logical width and height
648 // request from the client needs to be adjusted to account by effectively
649 // rotating that too, and the final width and height need to be swapped:
650 int postXformImageWidth = preXformImageWidth;
651 int postXformImageHeight = preXformImageHeight;
653 int scaledPreXformWidth = preXformImageWidth;
654 int scaledPreXformHeight = preXformImageHeight;
655 int scaledPostXformWidth = postXformImageWidth;
656 int scaledPostXformHeight = postXformImageHeight;
658 TransformSize(requiredWidth, requiredHeight, input.scalingParameters.scalingMode, input.scalingParameters.samplingMode, transform, scaledPreXformWidth, scaledPreXformHeight, scaledPostXformWidth, scaledPostXformHeight);
660 // Colorspace conversion options
661 TJPF pixelLibJpegType = TJPF_RGB;
662 Pixel::Format pixelFormat = Pixel::RGB888;
663 #ifndef DALI_PROFILE_UBUNTU
664 switch(jpegColorspace)
667 // YCbCr is not an absolute colorspace but rather a mathematical transformation of RGB designed solely for storage and transmission.
668 // YCbCr images must be converted to RGB before they can actually be displayed.
671 pixelLibJpegType = TJPF_RGB;
672 pixelFormat = Pixel::RGB888;
677 pixelLibJpegType = TJPF_GRAY;
678 pixelFormat = Pixel::L8;
684 pixelLibJpegType = TJPF_CMYK;
685 pixelFormat = Pixel::RGBA8888;
690 pixelLibJpegType = TJPF_RGB;
691 pixelFormat = Pixel::RGB888;
696 // Allocate a bitmap and decompress the jpeg buffer into its pixel buffer:
697 bitmap = Dali::Devel::PixelBuffer::New(scaledPostXformWidth, scaledPostXformHeight, pixelFormat);
700 GetImplementation(bitmap).SetMetadata(std::move(exifMap));
702 auto bitmapPixelBuffer = bitmap.GetBuffer();
704 if(tjDecompress2(jpeg.get(), jpegBufferPtr, jpegBufferSize, reinterpret_cast<unsigned char*>(bitmapPixelBuffer), scaledPreXformWidth, 0, scaledPreXformHeight, pixelLibJpegType, flags) == -1)
706 std::string errorString = tjGetErrorStr();
708 if(IsJpegErrorFatal(errorString))
710 DALI_LOG_ERROR("%s\n", errorString.c_str());
715 DALI_LOG_WARNING("%s\n", errorString.c_str());
719 const unsigned int bufferWidth = GetTextureDimension(scaledPreXformWidth);
720 const unsigned int bufferHeight = GetTextureDimension(scaledPreXformHeight);
725 case JpegTransform::NONE:
730 // 3 orientation changes for a camera held perpendicular to the ground or upside-down:
731 case JpegTransform::ROTATE_180:
733 static auto rotate180Functions = TransformFunctionArray{
738 result = Transform(rotate180Functions, bitmapPixelBuffer, bufferWidth, bufferHeight, pixelFormat);
741 case JpegTransform::ROTATE_270:
743 static auto rotate270Functions = TransformFunctionArray{
748 result = Transform(rotate270Functions, bitmapPixelBuffer, bufferWidth, bufferHeight, pixelFormat);
751 case JpegTransform::ROTATE_90:
753 static auto rotate90Functions = TransformFunctionArray{
758 result = Transform(rotate90Functions, bitmapPixelBuffer, bufferWidth, bufferHeight, pixelFormat);
761 case JpegTransform::FLIP_VERTICAL:
763 static auto flipVerticalFunctions = TransformFunctionArray{
768 result = Transform(flipVerticalFunctions, bitmapPixelBuffer, bufferWidth, bufferHeight, pixelFormat);
771 // Less-common orientation changes, since they don't correspond to a camera's physical orientation:
772 case JpegTransform::FLIP_HORIZONTAL:
774 static auto flipHorizontalFunctions = TransformFunctionArray{
779 result = Transform(flipHorizontalFunctions, bitmapPixelBuffer, bufferWidth, bufferHeight, pixelFormat);
782 case JpegTransform::TRANSPOSE:
784 static auto transposeFunctions = TransformFunctionArray{
789 result = Transform(transposeFunctions, bitmapPixelBuffer, bufferWidth, bufferHeight, pixelFormat);
792 case JpegTransform::TRANSVERSE:
794 static auto transverseFunctions = TransformFunctionArray{
799 result = Transform(transverseFunctions, bitmapPixelBuffer, bufferWidth, bufferHeight, pixelFormat);
804 DALI_LOG_ERROR("Unsupported JPEG Orientation transformation: %x.\n", transform);
812 bool EncodeToJpeg(const unsigned char* const pixelBuffer, Vector<unsigned char>& encodedPixels, const std::size_t width, const std::size_t height, const Pixel::Format pixelFormat, unsigned quality)
816 DALI_LOG_ERROR("Null input buffer\n");
820 // Translate pixel format enum:
821 int jpegPixelFormat = -1;
827 jpegPixelFormat = TJPF_RGB;
830 case Pixel::RGBA8888:
833 jpegPixelFormat = TJPF_RGBX;
836 case Pixel::BGRA8888:
839 jpegPixelFormat = TJPF_BGRX;
844 DALI_LOG_ERROR("Unsupported pixel format for encoding to JPEG.\n");
849 // Assert quality is in the documented allowable range of the jpeg-turbo lib:
850 DALI_ASSERT_DEBUG(quality >= 1);
851 DALI_ASSERT_DEBUG(quality <= 100);
861 // Initialise a JPEG codec:
863 auto jpeg = MakeJpegCompressor();
866 DALI_LOG_ERROR("JPEG Compressor init failed: %s\n", tjGetErrorStr());
870 // Safely wrap the jpeg codec's buffer in case we are about to throw, then
871 // save the pixels to a persistent buffer that we own and let our cleaner
872 // class clean up the buffer as it goes out of scope:
873 auto dstBuffer = MakeJpegMemory();
875 // Run the compressor:
876 unsigned long dstBufferSize = 0;
879 if(tjCompress2(jpeg.get(),
880 const_cast<unsigned char*>(pixelBuffer),
885 SetPointer(dstBuffer),
891 DALI_LOG_ERROR("JPEG Compression failed: %s\n", tjGetErrorStr());
895 encodedPixels.Resize(dstBufferSize);
896 memcpy(encodedPixels.Begin(), dstBuffer.get(), dstBufferSize);
901 JpegTransform ConvertExifOrientation(ExifData* exifData)
903 auto transform = JpegTransform::NONE;
904 ExifEntry* const entry = exif_data_get_entry(exifData, EXIF_TAG_ORIENTATION);
908 orientation = exif_get_short(entry->data, exif_data_get_byte_order(entry->parent->parent));
913 transform = JpegTransform::NONE;
918 transform = JpegTransform::FLIP_HORIZONTAL;
923 transform = JpegTransform::ROTATE_180;
928 transform = JpegTransform::FLIP_VERTICAL;
933 transform = JpegTransform::TRANSPOSE;
938 transform = JpegTransform::ROTATE_90;
943 transform = JpegTransform::TRANSVERSE;
948 transform = JpegTransform::ROTATE_270;
953 // Try to keep loading the file, but let app developer know there was something fishy:
954 DALI_LOG_WARNING("Incorrect/Unknown Orientation setting found in EXIF header of JPEG image (%x). Orientation setting will be ignored.\n", entry);
962 bool TransformSize(int requiredWidth, int requiredHeight, FittingMode::Type fittingMode, SamplingMode::Type samplingMode, JpegTransform transform, int& preXformImageWidth, int& preXformImageHeight, int& postXformImageWidth, int& postXformImageHeight)
965 if(transform == JpegTransform::TRANSPOSE || transform == JpegTransform::ROTATE_90 || transform == JpegTransform::TRANSVERSE || transform == JpegTransform::ROTATE_270)
967 std::swap(requiredWidth, requiredHeight);
968 std::swap(postXformImageWidth, postXformImageHeight);
971 // Apply the special rules for when there are one or two zeros in requested dimensions:
972 const ImageDimensions correctedDesired = Internal::Platform::CalculateDesiredDimensions(ImageDimensions(postXformImageWidth, postXformImageHeight), ImageDimensions(requiredWidth, requiredHeight));
973 requiredWidth = correctedDesired.GetWidth();
974 requiredHeight = correctedDesired.GetHeight();
976 // Rescale image during decode using one of the decoder's built-in rescaling
977 // ratios (expected to be powers of 2), keeping the final image at least as
978 // wide and high as was requested:
981 tjscalingfactor* factors = tjGetScalingFactors(&numFactors);
984 DALI_LOG_WARNING("TurboJpeg tjGetScalingFactors error!\n");
989 // Internal jpeg downscaling is the same as our BOX_X sampling modes so only
990 // apply it if the application requested one of those:
991 // (use a switch case here so this code will fail to compile if other modes are added)
992 bool downscale = true;
995 case SamplingMode::BOX:
996 case SamplingMode::BOX_THEN_NEAREST:
997 case SamplingMode::BOX_THEN_LINEAR:
998 case SamplingMode::DONT_CARE:
1003 case SamplingMode::NO_FILTER:
1004 case SamplingMode::NEAREST:
1005 case SamplingMode::LINEAR:
1012 int scaleFactorIndex(0);
1015 // Find nearest supported scaling factor (factors are in sequential order, getting smaller)
1016 for(int i = 1; i < numFactors; ++i)
1018 bool widthLessRequired = TJSCALED(postXformImageWidth, factors[i]) < requiredWidth;
1019 bool heightLessRequired = TJSCALED(postXformImageHeight, factors[i]) < requiredHeight;
1020 // If either scaled dimension is smaller than the desired one, we were done at the last iteration
1021 if((fittingMode == FittingMode::SCALE_TO_FILL) && (widthLessRequired || heightLessRequired))
1025 // If both dimensions are smaller than the desired one, we were done at the last iteration:
1026 if((fittingMode == FittingMode::SHRINK_TO_FIT) && (widthLessRequired && heightLessRequired))
1030 // If the width is smaller than the desired one, we were done at the last iteration:
1031 if(fittingMode == FittingMode::FIT_WIDTH && widthLessRequired)
1035 // If the width is smaller than the desired one, we were done at the last iteration:
1036 if(fittingMode == FittingMode::FIT_HEIGHT && heightLessRequired)
1040 // This factor stays is within our fitting mode constraint so remember it:
1041 scaleFactorIndex = i;
1045 // Regardless of requested size, downscale to avoid exceeding the maximum texture size:
1046 for(int i = scaleFactorIndex; i < numFactors; ++i)
1048 // Continue downscaling to below maximum texture size (if possible)
1049 scaleFactorIndex = i;
1051 if(TJSCALED(postXformImageWidth, (factors[i])) < static_cast<int>(Dali::GetMaxTextureSize()) &&
1052 TJSCALED(postXformImageHeight, (factors[i])) < static_cast<int>(Dali::GetMaxTextureSize()))
1054 // Current scale-factor downscales to below maximum texture size
1059 // We have finally chosen the scale-factor, return width/height values
1060 if(scaleFactorIndex > 0)
1062 preXformImageWidth = TJSCALED(preXformImageWidth, (factors[scaleFactorIndex]));
1063 preXformImageHeight = TJSCALED(preXformImageHeight, (factors[scaleFactorIndex]));
1064 postXformImageWidth = TJSCALED(postXformImageWidth, (factors[scaleFactorIndex]));
1065 postXformImageHeight = TJSCALED(postXformImageHeight, (factors[scaleFactorIndex]));
1072 ExifHandle LoadExifData(FILE* fp)
1074 auto exifData = MakeNullExifData();
1075 unsigned char dataBuffer[1024];
1077 if(fseek(fp, 0, SEEK_SET))
1079 DALI_LOG_ERROR("Error seeking to start of file\n");
1083 auto exifLoader = std::unique_ptr<ExifLoader, decltype(exif_loader_unref)*>{
1084 exif_loader_new(), exif_loader_unref};
1088 int size = fread(dataBuffer, 1, sizeof(dataBuffer), fp);
1093 if(!exif_loader_write(exifLoader.get(), dataBuffer, size))
1099 exifData.reset(exif_loader_get_data(exifLoader.get()));
1105 bool LoadJpegHeader(const Dali::ImageLoader::Input& input, unsigned int& width, unsigned int& height)
1107 unsigned int requiredWidth = input.scalingParameters.dimensions.GetWidth();
1108 unsigned int requiredHeight = input.scalingParameters.dimensions.GetHeight();
1109 FILE* const fp = input.file;
1111 bool success = false;
1113 unsigned int headerWidth;
1114 unsigned int headerHeight;
1116 success = LoadJpegHeader(fp, headerWidth, headerHeight);
1119 auto transform = JpegTransform::NONE;
1121 if(input.reorientationRequested)
1123 auto exifData = LoadExifData(fp);
1126 transform = ConvertExifOrientation(exifData.get());
1130 if(requiredWidth == 0 && requiredHeight == 0)
1132 if(transform == JpegTransform::TRANSPOSE || transform == JpegTransform::ROTATE_90 || transform == JpegTransform::TRANSVERSE || transform == JpegTransform::ROTATE_270)
1134 std::swap(headerWidth, headerHeight);
1139 int preXformImageWidth = headerWidth;
1140 int preXformImageHeight = headerHeight;
1141 int postXformImageWidth = headerWidth;
1142 int postXformImageHeight = headerHeight;
1144 success = TransformSize(requiredWidth, requiredHeight, input.scalingParameters.scalingMode, input.scalingParameters.samplingMode, transform, preXformImageWidth, preXformImageHeight, postXformImageWidth, postXformImageHeight);
1147 headerWidth = postXformImageWidth;
1148 headerHeight = postXformImageHeight;
1151 width = headerWidth;
1152 height = headerHeight;
1158 } // namespace TizenPlatform