2 * Copyright (c) 2017 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-wbmp.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/devel-api/adaptor-framework/pixel-buffer.h>
33 namespace TizenPlatform
39 #if defined(DEBUG_ENABLED)
40 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_LOADER_WBMP");
43 #define IMG_MAX_SIZE 65536
45 #define IMG_TOO_BIG(w, h) \
46 ((((unsigned long long)w) * ((unsigned long long)h)) >= \
47 ((1ULL << (29)) - 2048))
50 //extract multiple bytes integer , and saved in *data
51 int extractMultiByteInteger(unsigned int *data, void *map, size_t length, size_t *position)
53 // the header field contains an image type indentifier of multi-byte length(TypeField), an octet of general header info(FixHeaderField)
54 //, a multi-byte width field(Width) and a multi-byte height field(Height) and so on.
55 // The actual organisation of the image data depends on the image type
56 // for Ext Headers flag (7th bit), 1 = More will follow, 0 = Last octet
57 // so in the for loop, if(buf & 0x80 == 0), loop will be exited
58 int targetMultiByteInteger = 0, readBufCount;
61 for (readBufCount = 0;;)
63 // readBufCount means the count that fetched data from map
64 // extractMultiByteInteger() is to fetch wbmp type , width, and height
65 // for wbmp type, when readBufCount == 1, buf = 0x00, it will exit the loop
66 // for width, it have 4 bytes, so when readBufCount == 4, it must exit the loop
67 // for general width and height, if(buf & 0x80) == 0, then the next byte does not need to fetch again
68 // first step, readBufCount = 1 , read int(4 bytes) to buf, if buf & 0x80 !=0, the buf need to continue to fetch
69 // second step, readBufCount = 2, read next( 4 bytes) to buf, if buf & 0x80 == 0, then assigned the buf to target
70 if ((readBufCount ++) == 4)
74 if (*position > length)
78 buf = reinterpret_cast< unsigned char * >( map )[(*position)++];
79 targetMultiByteInteger = (targetMultiByteInteger << 7) | (buf & 0x7f);
81 if ((buf & 0x80) == 0)
83 DALI_LOG_INFO(gLogFilter, Debug::Verbose, "position: %d, readBufCount: %d\n", *position, readBufCount);
87 *data = targetMultiByteInteger;
91 }// end unnamed namespace
93 bool LoadBitmapFromWbmp( const Dali::ImageLoader::Input& input, Dali::Devel::PixelBuffer& bitmap )
95 FILE* const fp = input.file;
98 DALI_LOG_ERROR("Error loading bitmap\n");
101 Dali::Vector<unsigned char> map;
102 Dali::Vector<unsigned char> surface;//unsigned int
107 unsigned int line_length;
108 unsigned char *line = NULL;
109 unsigned int cur = 0, x, y;
111 if( fseek(fp,0,SEEK_END) )
113 DALI_LOG_ERROR("Error seeking WBMP data\n");
116 long positionIndicator = ftell(fp);
118 unsigned int fsize( 0u );
119 if( positionIndicator > -1L )
121 fsize = static_cast<unsigned int>(positionIndicator);
126 DALI_LOG_ERROR("Error: filesize is 0!\n");
130 if( fseek(fp, 0, SEEK_SET) )
132 DALI_LOG_ERROR("Error seeking WBMP data\n");
137 DALI_LOG_ERROR("Error: WBMP Raw Data Not Found!\n");
140 if(fsize > 4096 * 4096 * 4)
142 DALI_LOG_ERROR("Error: WBMP size is too large!\n");
147 if(fread(&map[0], 1, fsize, fp) != fsize)
149 DALI_LOG_WARNING("image file read opeation error!\n");
153 if (extractMultiByteInteger(&type, &map[0], fsize, &position) < 0)
158 position++; /* skipping one byte */
160 if (extractMultiByteInteger(&w, &map[0], fsize, &position) < 0)
164 if (extractMultiByteInteger(&h, &map[0], fsize, &position) < 0)
170 DALI_LOG_ERROR("Unknown Format!\n");
174 if ((w < 1) || (h < 1) || (w > IMG_MAX_SIZE) || (h > IMG_MAX_SIZE))
179 surface.Resize(w* h );//(w * h * 4);
180 memset(&surface[0], 0, w * h ); // w * h * 4
182 line_length = (w + 7) >> 3;
183 for (y = 0; y < h; y ++)
185 if (position + line_length > fsize)
189 line = &map[0] + position;
190 position += line_length;
191 for (x = 0; x < w; x++)
194 int offset = 1 << (0x07 - (x & 0x07));
195 if (line[idx] & offset)
197 surface[cur] = 0xff;//0xffffffff;
201 surface[cur] = 0x00;//0xff000000;
206 auto pixels = (bitmap = Dali::Devel::PixelBuffer::New(w, h, Pixel::L8)).GetBuffer();
208 memcpy( pixels, &surface[0], w * h ); //w * h * 4
214 bool LoadWbmpHeader( const Dali::ImageLoader::Input& input, unsigned int& width, unsigned int& height )
216 FILE* const fp = input.file;
219 DALI_LOG_ERROR("Error loading bitmap\n");
222 Dali::Vector<unsigned char> map;
227 if( fseek(fp,0,SEEK_END) )
229 DALI_LOG_ERROR("Error seeking WBMP data\n");
232 long positionIndicator = ftell(fp);
234 unsigned int fsize( 0u );
235 if( positionIndicator > -1L )
237 fsize = static_cast<unsigned int>(positionIndicator);
245 if( fseek(fp, 0, SEEK_SET) )
247 DALI_LOG_ERROR("Error seeking WBMP data\n");
252 DALI_LOG_ERROR("Error: WBMP Raw Data Not Found!\n");
256 // type(1 byte) + fixedheader(1 byte) + width(uint) + height(uint)
257 unsigned int headerSize = 1 + 1 + 4 + 4;// 8 + 8 + 32 + 32;
258 headerSize = std::min(headerSize, fsize);
260 map.Resize(headerSize);
261 if(fread(&map[0], 1, headerSize, fp) != headerSize)
263 DALI_LOG_WARNING("image file read opeation error!\n");
267 if (extractMultiByteInteger(&type, &map[0], headerSize, &position) < 0)
269 DALI_LOG_ERROR("Error: unable to read type!\n");
272 position++; /* skipping one byte */
275 DALI_LOG_ERROR("Error: unknown format!\n");
278 if (extractMultiByteInteger(&w, &map[0], headerSize, &position) < 0)
280 DALI_LOG_ERROR("Error: can not read width!\n");
283 if (extractMultiByteInteger(&h, &map[0], headerSize, &position) < 0)
285 DALI_LOG_ERROR("Error: can not read height!\n");
289 if ((w < 1) || (h < 1) || (w > IMG_MAX_SIZE) || (h > IMG_MAX_SIZE) )
291 DALI_LOG_ERROR("Error: file size is not supported!\n");