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;
136 ReleaseResource(webPData, webPAnimDecoder);
138 DALI_LOG_ERROR("WebP file open failed.\n");
142 bool LoadBitmapFromWebp(const Dali::ImageLoader::Input& input, Dali::Devel::PixelBuffer& bitmap)
144 FILE* const fp = input.file;
150 if(fseek(fp, 0, SEEK_END) <= -1)
155 // If the image is non-animated webp
156 #ifdef DALI_WEBP_AVAILABLE
157 size_t webPSize = ftell(fp);
158 if((!fseek(fp, 0, SEEK_SET)))
160 std::vector<uint8_t> encodedImage;
161 encodedImage.resize(webPSize, 0);
162 size_t readCount = fread(&encodedImage[0], sizeof(uint8_t), encodedImage.size(), fp);
163 if(readCount != encodedImage.size())
165 DALI_LOG_ERROR("WebP image loading failed.\n");
169 int32_t width, height;
170 if(!WebPGetInfo(&encodedImage[0], encodedImage.size(), &width, &height))
172 DALI_LOG_ERROR("Cannot retrieve WebP image size information.\n");
176 WebPBitstreamFeatures features;
177 if(VP8_STATUS_NOT_ENOUGH_DATA == WebPGetFeatures(&encodedImage[0], encodedImage.size(), &features))
179 DALI_LOG_ERROR("Cannot retrieve WebP image features.\n");
183 uint32_t channelNumber = (features.has_alpha) ? 4 : 3;
184 Pixel::Format pixelFormat = (channelNumber == 4) ? Pixel::RGBA8888 : Pixel::RGB888;
185 bitmap = Dali::Devel::PixelBuffer::New(width, height, pixelFormat);
186 uint8_t* frameBuffer = nullptr;
187 if(channelNumber == 4)
189 frameBuffer = WebPDecodeRGBA(&encodedImage[0], encodedImage.size(), &width, &height);
193 frameBuffer = WebPDecodeRGB(&encodedImage[0], encodedImage.size(), &width, &height);
196 if(frameBuffer != nullptr)
198 const int32_t bufferSize = width * height * sizeof(uint8_t) * channelNumber;
199 memcpy(bitmap.GetBuffer(), frameBuffer, bufferSize);
200 free((void*)frameBuffer);
206 // If the image is animated webp
207 #ifdef DALI_ANIMATED_WEBP_ENABLED
209 WebPAnimDecoder* webPAnimDecoder = nullptr;
210 WebPAnimInfo webPAnimInfo;
211 if(ReadWebPInformation(fp, webPData))
213 WebPAnimDecoderOptions webPAnimDecoderOptions;
214 WebPAnimDecoderOptionsInit(&webPAnimDecoderOptions);
215 webPAnimDecoderOptions.color_mode = MODE_RGBA;
216 webPAnimDecoder = WebPAnimDecoderNew(&webPData, &webPAnimDecoderOptions);
217 if(webPAnimDecoder != nullptr)
219 uint8_t* frameBuffer;
221 WebPAnimDecoderGetInfo(webPAnimDecoder, &webPAnimInfo);
222 WebPAnimDecoderReset(webPAnimDecoder);
223 WebPAnimDecoderGetNext(webPAnimDecoder, &frameBuffer, ×tamp);
225 bitmap = Dali::Devel::PixelBuffer::New(webPAnimInfo.canvas_width, webPAnimInfo.canvas_height, Dali::Pixel::RGBA8888);
226 const int32_t bufferSize = webPAnimInfo.canvas_width * webPAnimInfo.canvas_height * sizeof(uint32_t);
227 memcpy(bitmap.GetBuffer(), frameBuffer, bufferSize);
231 ReleaseResource(webPData, webPAnimDecoder);
234 DALI_LOG_ERROR("WebP image loading failed.\n");
238 } // namespace TizenPlatform