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.
18 #include <dali/internal/imaging/common/loader-webp.h>
21 #ifdef DALI_WEBP_AVAILABLE
22 #include <webp/decode.h>
23 #include <webp/demux.h>
25 #if WEBP_DEMUX_ABI_VERSION > 0x0101
26 #define DALI_ANIMATED_WEBP_ENABLED 1
29 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
30 #include <dali/integration-api/debug.h>
34 typedef unsigned char WebPByteType;
38 namespace TizenPlatform
40 #ifdef DALI_ANIMATED_WEBP_ENABLED
41 bool ReadWebPInformation(FILE* const fp, WebPData& webPData)
48 if(fseek(fp, 0, SEEK_END) <= -1)
52 WebPDataInit(&webPData);
53 webPData.size = ftell(fp);
55 if((!fseek(fp, 0, SEEK_SET)))
57 unsigned char* WebPDataBuffer;
58 WebPDataBuffer = reinterpret_cast<WebPByteType*>(malloc(sizeof(WebPByteType) * webPData.size));
59 webPData.size = fread(WebPDataBuffer, sizeof(WebPByteType), webPData.size, fp);
60 webPData.bytes = WebPDataBuffer;
69 void ReleaseResource(WebPData& webPData, WebPAnimDecoder* webPAnimDecoder)
71 free((void*)webPData.bytes);
72 webPData.bytes = nullptr;
73 WebPDataInit(&webPData);
76 WebPAnimDecoderDelete(webPAnimDecoder);
82 bool LoadWebpHeader(const Dali::ImageLoader::Input& input, unsigned int& width, unsigned int& height)
84 FILE* const fp = input.file;
90 if(fseek(fp, 0, SEEK_END) <= -1)
95 // If the image is non-animated webp
96 #ifdef DALI_WEBP_AVAILABLE
97 size_t webPSize = ftell(fp);
98 if((!fseek(fp, 0, SEEK_SET)))
100 std::vector<uint8_t> encodedImage;
101 encodedImage.resize(webPSize, 0);
102 size_t readCount = fread(&encodedImage[0], sizeof(uint8_t), encodedImage.size(), fp);
103 if(readCount != encodedImage.size())
107 int32_t imageWidth, imageHeight;
108 if(WebPGetInfo(&encodedImage[0], encodedImage.size(), &imageWidth, &imageHeight))
110 width = static_cast<uint32_t>(imageWidth);
111 height = static_cast<uint32_t>(imageHeight);
117 // If the image is animated webp
118 #ifdef DALI_ANIMATED_WEBP_ENABLED
120 WebPAnimDecoder* webPAnimDecoder = nullptr;
121 WebPAnimInfo webPAnimInfo;
122 if(ReadWebPInformation(fp, webPData))
124 WebPAnimDecoderOptions webPAnimDecoderOptions;
125 WebPAnimDecoderOptionsInit(&webPAnimDecoderOptions);
126 webPAnimDecoderOptions.color_mode = MODE_RGBA;
127 webPAnimDecoder = WebPAnimDecoderNew(&webPData, &webPAnimDecoderOptions);
128 if(webPAnimDecoder != nullptr)
130 WebPAnimDecoderGetInfo(webPAnimDecoder, &webPAnimInfo);
131 width = webPAnimInfo.canvas_width;
132 height = webPAnimInfo.canvas_height;
133 ReleaseResource(webPData, webPAnimDecoder);
137 ReleaseResource(webPData, webPAnimDecoder);
139 DALI_LOG_ERROR("WebP file open failed.\n");
143 bool LoadBitmapFromWebp(const Dali::ImageLoader::Input& input, Dali::Devel::PixelBuffer& bitmap)
145 FILE* const fp = input.file;
151 if(fseek(fp, 0, SEEK_END) <= -1)
156 // If the image is non-animated webp
157 #ifdef DALI_WEBP_AVAILABLE
158 size_t webPSize = ftell(fp);
159 if((!fseek(fp, 0, SEEK_SET)))
161 std::vector<uint8_t> encodedImage;
162 encodedImage.resize(webPSize, 0);
163 size_t readCount = fread(&encodedImage[0], sizeof(uint8_t), encodedImage.size(), fp);
164 if(readCount != encodedImage.size())
166 DALI_LOG_ERROR("WebP image loading failed.\n");
170 int32_t width, height;
171 if(!WebPGetInfo(&encodedImage[0], encodedImage.size(), &width, &height))
173 DALI_LOG_ERROR("Cannot retrieve WebP image size information.\n");
177 WebPBitstreamFeatures features;
178 if(VP8_STATUS_NOT_ENOUGH_DATA == WebPGetFeatures(&encodedImage[0], encodedImage.size(), &features))
180 DALI_LOG_ERROR("Cannot retrieve WebP image features.\n");
184 uint32_t channelNumber = (features.has_alpha) ? 4 : 3;
185 Pixel::Format pixelFormat = (channelNumber == 4) ? Pixel::RGBA8888 : Pixel::RGB888;
186 bitmap = Dali::Devel::PixelBuffer::New(width, height, pixelFormat);
187 uint8_t* frameBuffer = nullptr;
188 if(channelNumber == 4)
190 frameBuffer = WebPDecodeRGBA(&encodedImage[0], encodedImage.size(), &width, &height);
194 frameBuffer = WebPDecodeRGB(&encodedImage[0], encodedImage.size(), &width, &height);
197 if(frameBuffer != nullptr)
199 const int32_t bufferSize = width * height * sizeof(uint8_t) * channelNumber;
200 memcpy(bitmap.GetBuffer(), frameBuffer, bufferSize);
201 free((void*)frameBuffer);
207 // If the image is animated webp
208 #ifdef DALI_ANIMATED_WEBP_ENABLED
210 WebPAnimDecoder* webPAnimDecoder = nullptr;
211 WebPAnimInfo webPAnimInfo;
212 if(ReadWebPInformation(fp, webPData))
214 WebPAnimDecoderOptions webPAnimDecoderOptions;
215 WebPAnimDecoderOptionsInit(&webPAnimDecoderOptions);
216 webPAnimDecoderOptions.color_mode = MODE_RGBA;
217 webPAnimDecoder = WebPAnimDecoderNew(&webPData, &webPAnimDecoderOptions);
218 if(webPAnimDecoder != nullptr)
220 uint8_t* frameBuffer;
222 WebPAnimDecoderGetInfo(webPAnimDecoder, &webPAnimInfo);
223 WebPAnimDecoderReset(webPAnimDecoder);
224 WebPAnimDecoderGetNext(webPAnimDecoder, &frameBuffer, ×tamp);
226 bitmap = Dali::Devel::PixelBuffer::New(webPAnimInfo.canvas_width, webPAnimInfo.canvas_height, Dali::Pixel::RGBA8888);
227 const int32_t bufferSize = webPAnimInfo.canvas_width * webPAnimInfo.canvas_height * sizeof(uint32_t);
228 memcpy(bitmap.GetBuffer(), frameBuffer, bufferSize);
229 ReleaseResource(webPData, webPAnimDecoder);
233 ReleaseResource(webPData, webPAnimDecoder);
236 DALI_LOG_ERROR("WebP image loading failed.\n");
240 } // namespace TizenPlatform