2 * Copyright (c) 2016 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 * Derived from Enlightenment file evas_image_load_ico.c[1] which is licensed
19 * under the BSD 2-clause license[2] reproduced below.
21 * [1][http://web.archive.org/web/20141201151111/http://git.enlightenment.org/core/efl.git/tree/src/modules/evas/loaders/ico/evas_image_load_ico.c]
22 * [2][http://web.archive.org/web/20140717012400/https://git.enlightenment.org/core/efl.git/about/]
24 * Copyright (C) 2002-2012 Carsten Haitzler, Dan Sinclair, Mike Blumenkrantz,
25 * Samsung Electronics and various contributors (see AUTHORS)
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
38 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
39 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
40 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
41 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
42 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
44 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
45 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
46 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
47 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 #include "loader-ico.h"
55 #include <dali/public-api/common/dali-vector.h>
58 #include <dali/integration-api/debug.h>
59 #include <dali/integration-api/bitmap.h>
63 using Integration::Bitmap;
64 using Dali::Integration::PixelBuffer;
66 namespace TizenPlatform
71 // Reserved 2 bytes + type 2 bytes + count 2 bytes + count * 16 bytes
72 const unsigned char ICO_FILE_HEADER = 22;
73 // Info header 40 bytes = size 4 bytes + width 4 bytes + height 4 bytes + planes 2 bytes + bitcount 2 bytes
74 // + compression 4 bytes + imagesize 4 bytes + xpixelsPerM 4 bytes + ypixelsPerM 4 bytes + colorsUsed 4 bytes + colorImportant 4 bytes
75 // besides, there are rgba color data = numberOfColors * 4 bytes
76 const unsigned char ICO_IMAGE_INFO_HEADER = 40;
78 typedef unsigned char DATA8;
79 #define A_VAL(p) (((DATA8 *)(p))[3])
81 #define RGB_JOIN(r,g,b) \
82 (((r) << 16) + ((g) << 8) + (b))
84 #define ARGB_JOIN(a,r,g,b) \
85 (((a) << 24) + ((r) << 16) + ((g) << 8) + (b))
86 #define IMG_TOO_BIG(w, h) \
87 ((((unsigned long long)w) * ((unsigned long long)h)) >= \
88 ((1ULL << (29 * (sizeof(void *) / 4))) - 2048))
90 bool read_ushort(unsigned char *map, size_t length, size_t *position, unsigned short *ret)
94 if (*position + 2 > length)
98 b[0] = map[(*position)++];
99 b[1] = map[(*position)++];
100 *ret = (b[1] << 8) | b[0];
104 bool read_uint(unsigned char *map, size_t length, size_t *position, unsigned int *ret)
109 if (*position + 4 > length)
113 for (i = 0; i < 4; i++)
115 b[i] = map[(*position)++];
117 *ret = ARGB_JOIN(b[3], b[2], b[1], b[0]);
121 bool read_uchar(unsigned char *map, size_t length, size_t *position, unsigned char *ret)
123 if (*position + 1 > length)
127 *ret = map[(*position)++];
131 bool read_mem(unsigned char *map, size_t length, size_t *position, void *buffer, int size)
133 if (*position + size > length)
137 memcpy(buffer, map + *position, size);
163 unsigned int bmoffset, bmsize;
166 bool LoadIcoHeaderHelper( FILE* fp,
168 Dali::Vector<unsigned char>& map,
169 unsigned int& fsize )
171 memset( &chosen, 0, sizeof(chosen) );
175 DALI_LOG_ERROR("Error loading bitmap\n");
182 if( fseek(fp,0,SEEK_END) )
184 DALI_LOG_ERROR("Error seeking ICO data\n");
188 long positionIndicator = ftell(fp);
191 if( positionIndicator > -1L )
193 fsize = static_cast<unsigned int>(positionIndicator);
201 if( fseek(fp, 0, SEEK_SET) )
203 DALI_LOG_ERROR("Error seeking ICO data\n");
207 if (fsize < (ICO_FILE_HEADER + ICO_IMAGE_INFO_HEADER)) //6 + 16 + 40
213 if(fread(&map[0], 1, fsize, fp) != fsize)
215 DALI_LOG_WARNING("image file read opeation error!\n");
219 int search = BIGGEST;
220 unsigned short reserved, type, count;
221 if (!read_ushort(&map[0], fsize, &position, &reserved))
225 if (!read_ushort(&map[0], fsize, &position, &type))
229 if (!read_ushort(&map[0], fsize, &position, &count))
233 if (!((reserved == 0) &&
234 ((type == ICON) || (type == CURSOR)) && (count != 0)))
240 bool have_choice = false;
242 for (unsigned short i = 0; i < count; i++)
244 unsigned char tw = 0, th = 0, tcols = 0;
245 if (!read_uchar(&map[0], fsize, &position, &tw))
254 if (!read_uchar(&map[0], fsize, &position, &th))
264 if (!read_uchar(&map[0], fsize, &position, &tcols))
269 if (!read_uchar(&map[0], fsize, &position, &byte))
273 if (!read_ushort(&map[0], fsize, &position, &word))
283 if (!read_ushort(&map[0], fsize, &position, &word))
293 // 0 colors means 256 for paletized modes.
294 // Note: We must not do this conversion for bpp greater than 8, as there is no palette.
295 if( bpp <= 8 && cols == 0 )
301 unsigned int bmoffset, bmsize;
302 if (!read_uint(&map[0], fsize, &position, &bmsize))
306 if (!read_uint(&map[0], fsize, &position, &bmoffset))
310 if ((bmsize <= 0) || (bmoffset <= 0) || (bmoffset >= fsize))
314 if (search == BIGGEST)
317 if ((!have_choice) ||
318 ((pdelta >= chosen.pdelta) &&
319 (((bpp >= 3) && (bpp >= chosen.bpp)) ||
320 ((bpp < 3) && (cols >= chosen.cols)))))
323 chosen.pdelta = pdelta;
328 chosen.planes = planes;
329 chosen.bmsize = bmsize;
330 chosen.bmoffset = bmoffset;
335 if (chosen.bmoffset == 0)
345 bool LoadIcoHeader( const ImageLoader::Input& input, unsigned int& width, unsigned int& height )
348 Dali::Vector<unsigned char> map;
350 FILE* const fp = input.file;
352 if ( false == LoadIcoHeaderHelper(fp, chosen, map, fsize) )
363 bool LoadBitmapFromIco( const ResourceLoadingClient& client, const ImageLoader::Input& input, Integration::Bitmap& bitmap )
366 Dali::Vector<unsigned char> map;
368 FILE* const fp = input.file;
370 if ( false == LoadIcoHeaderHelper(fp, chosen, map, fsize) )
375 Dali::Vector<unsigned int> pal;
376 Dali::Vector<unsigned int> surface;
377 Dali::Vector<unsigned char> maskbuf;
378 Dali::Vector<unsigned char> pixbuf;
386 PixelBuffer* pixels = NULL;
388 size_t position = chosen.bmoffset;//22 == position
392 int cols = chosen.cols;
394 // read bmp header time... let's do some checking
395 if (!read_uint(&map[0], fsize, &position, &dword))
397 return false; // headersize - dont care
399 if (!read_uint(&map[0], fsize, &position, &dword))
401 return false; // width
411 if (!read_uint(&map[0], fsize, &position, &dword))
413 return false; // height
417 if ((int)dword != (h * 2))
425 DALI_LOG_WARNING("Broken ICO file!\n");
428 // Set up the surface as soon as we have the width and height, so we have a black image if there are any further errors.
429 surface.Resize( w * h * 4 );
430 memset( &surface[0], 0, w * h * 4 );
432 if (!read_ushort(&map[0], fsize, &position, &word))
434 return false; // planes
437 if (!read_ushort(&map[0], fsize, &position, &word))
439 return false; // bitcount
441 unsigned int bitcount = word;
442 if (!read_uint(&map[0], fsize, &position, &dword))
444 return false; // compression
446 //compression = dword;
447 if (!read_uint(&map[0], fsize, &position, &dword))
449 return false; // imagesize
452 if (!read_uint(&map[0], fsize, &position, &dword))
454 return false; // z pixels per m
456 if (!read_uint(&map[0], fsize, &position, &dword))
458 return false; // y pizels per m
460 if (!read_uint(&map[0], fsize, &position, &dword))
462 return false; // colors used
464 //colorsused = dword;
465 if (!read_uint(&map[0], fsize, &position, &dword))
467 return false; // colors important
470 for(int i = 0; i < cols ; i ++)
472 unsigned char a, r, g, b;
474 if (!read_uchar(&map[0], fsize, &position, &b))
478 if (!read_uchar(&map[0], fsize, &position, &g))
482 if (!read_uchar(&map[0], fsize, &position, &r))
486 if (!read_uchar(&map[0], fsize, &position, &a))
490 pal[i] = ARGB_JOIN( 0xff, b, g, r );
493 // This is the reference way of calculating the total number of bytes necessary to store one row of pixels.
494 int stride = ( ( ( bitcount * w ) + 31 ) / 32 ) * 4;
495 int bitStride = ( ( w + 31 ) / 32 ) * 4;
497 // Pixbuf only ever contains one scanline worth of data.
498 pixbuf.Resize( stride );
499 maskbuf.Resize( bitStride * h );
501 // Handle different bits-per-pixel.
502 // Note: Switch is in order of most common format first.
507 unsigned char* p = &map[position];
508 pix = &surface[0] + ( ( h - 1 ) * w );
510 for( int i = 0; i < h; i++ )
512 for( int j = 0; j < w; j++ )
514 *pix++ = ARGB_JOIN( p[3], p[0], p[1], p[2] );
517 // Move the output up 1 line (we subtract 2 lines because we moved forward one line while copying).
525 for( int i = 0; i < h; i++ )
527 pix = &surface[0] + ( ( h - 1 - i ) * w );
528 if( !read_mem( &map[0], fsize, &position, &pixbuf[0], stride ) )
532 unsigned char* p = &pixbuf[0];
533 for( int j = 0; j < w; j++ )
535 *pix++ = ARGB_JOIN( 0xff, p[0], p[1], p[2] );
544 for( int i = 0; i < h; i++ )
546 pix = &surface[0] + ( ( h - 1 - i ) * w );
547 if( !read_mem( &map[0], fsize, &position, &pixbuf[0], stride ) )
551 unsigned char* p = &pixbuf[0];
552 for( int j = 0; j < w; j++ )
562 for( int i = 0; i < h; i++ )
564 pix = &surface[0] + ( ( h - 1 - i ) * w );
565 if( !read_mem( &map[0], fsize, &position, &pixbuf[0], stride ) )
569 unsigned char* p = &pixbuf[0];
570 for( int j = 0; j < w; j++ )
574 *pix = pal[*p & 0x0f];
589 for( int i = 0; i < h; i++ )
591 pix = &surface[0] + ( ( h - 1 - i ) * w );
592 if( !read_mem( &map[0], fsize, &position, &pixbuf[0], stride ) )
596 unsigned char* p = &pixbuf[0];
598 for( int j = 0; j < w; j += 8 )
600 *pix++ = pal[ *p >> 7 ];
601 *pix++ = pal[ *p >> 6 & 0x01 ];
602 *pix++ = pal[ *p >> 5 & 0x01 ];
603 *pix++ = pal[ *p >> 4 & 0x01 ];
604 *pix++ = pal[ *p >> 3 & 0x01 ];
605 *pix++ = pal[ *p >> 2 & 0x01 ];
606 *pix++ = pal[ *p >> 1 & 0x01 ];
607 *pix++ = pal[ *p >> 0 & 0x01 ];
617 DALI_LOG_WARNING( "Image file contains unsupported bits-per-pixel %d\n", bitcount );
622 // From the spec: If bpp is less than 32, there will be a 1bpp mask bitmap also.
625 if( !read_mem( &map[0], fsize, &position, &maskbuf[0], bitStride * h ) )
631 // Precalc to save time in the loops.
632 int bytesPerWidth = w / 8;
633 int bytesRemainingPerWidth = w - ( bytesPerWidth << 3 );
635 // Loop for each line of the image.
636 for( int i = 0; i < h; ++i )
638 unsigned char *m = &maskbuf[0] + ( bitStride * i );
639 pix = &surface[0] + ( ( h - 1 - i ) * w );
641 // Do chunks of 8 pixels first so mask operations can be unrolled.
642 for( int j = 0; j < bytesPerWidth; ++j )
644 // Unrolled 8 bits of the mask to avoid many conditions and branches.
645 A_VAL( pix++ ) = ( *m & ( 1 << 7 ) ) ? 0x00 : 0xff;
646 A_VAL( pix++ ) = ( *m & ( 1 << 6 ) ) ? 0x00 : 0xff;
647 A_VAL( pix++ ) = ( *m & ( 1 << 5 ) ) ? 0x00 : 0xff;
648 A_VAL( pix++ ) = ( *m & ( 1 << 4 ) ) ? 0x00 : 0xff;
649 A_VAL( pix++ ) = ( *m & ( 1 << 3 ) ) ? 0x00 : 0xff;
650 A_VAL( pix++ ) = ( *m & ( 1 << 2 ) ) ? 0x00 : 0xff;
651 A_VAL( pix++ ) = ( *m & ( 1 << 1 ) ) ? 0x00 : 0xff;
652 A_VAL( pix++ ) = ( *m & ( 1 << 0 ) ) ? 0x00 : 0xff;
656 // Handle any remaining width ( < 8 ) or images that are < 8 wide.
657 if( bytesRemainingPerWidth > 0 )
659 for( int j = 0; j < bytesRemainingPerWidth; ++j )
661 // Note: Although we are doing less that a bytes worth of mask, we still always start on the first bit.
662 // If the image is smaller than 8 pixels wide, each mask will still start on a new byte.
663 A_VAL( pix++ ) = ( *m & ( 1 << ( 7 - j ) ) ) ? 0x00 : 0xff;
670 pixels = bitmap.GetPackedPixelsProfile()->ReserveBuffer( Pixel::RGBA8888, w, h );
671 memcpy( pixels, (unsigned char*)&surface[0], w * h * 4 );