2 * Copyright (c) 2014 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 "loader-bmp.h"
20 #include <dali/public-api/common/vector-wrapper.h>
21 #include <dali/integration-api/debug.h>
22 #include <dali/integration-api/bitmap.h>
23 #include <dali/public-api/images/image-attributes.h>
29 using Integration::Bitmap;
30 using Dali::Integration::PixelBuffer;
31 namespace TizenPlatform
36 const unsigned int FileHeaderOffsetOfBF32V4 = 0x7A;
37 const unsigned int MaskForBFRGB565 = 0x80;
38 const unsigned int FileHeaderOffsetOfRGB24V5 = 0x8A;
42 BMP_RGB1 = 14, //BI_RGB & bpp =1
43 BMP_RGB4, //BI_RGB & bpp = 4
44 BMP_RGB8, //BI_RGB & bpp = 8
45 BMP_RGB555, //BI_RGB & bpp = 16
46 BMP_BITFIELDS555, //BI_BITFIELDS & 16bit & R:G:B = 5:5:5
47 BMP_BITFIELDS32, //BI_BITFIELDS & 32bit & R:G:B:A = 8:8:8:8
50 BMP_BITFIELDS32V4,//BI_BITFIELDS & 32bit
51 BMP_RGB24V5, //BI_RGB & bpp = 24 & bmp version5
57 unsigned short signature; // Bitmap file signature
58 unsigned int fileSize; // Bitmap file size in bytes
59 unsigned short reserved1; // Reserved bits
60 unsigned short reserved2; // Reserved bits
61 unsigned int offset; // Offset from BMP file header to BMP bits
62 } __attribute__ ( (__packed__)); // Stops the structure from being aligned to every 4 bytes
66 unsigned int infoHeaderSize; // Specifies the number of bytes required by the info header
67 unsigned int width; // The Image Width
68 int height; // The Image Height (negative value represents image data is flipped)
69 unsigned short planes; // The number of color planes, must be 1
70 unsigned short bitsPerPixel; // The bits per pixel
71 unsigned int compression; // The type of compression used by the image
72 unsigned int imageSize; // The size of the image in bytes
73 unsigned int xPixelsPerMeter; // The number of pixels per meter in x axis
74 unsigned int yPixelsPerMeter; // The number of pixels per meter in y axis
75 unsigned int numberOfColors; // The number of colors in the color table
76 unsigned int importantColors; // The important color count
77 } __attribute__ ( (__packed__)); // Stops the structure from being aligned to every 4 bytes
80 * Template function to read from the file directly into our structure.
81 * @param[in] fp The file to read from
82 * @param[out] header The structure we want to store our information in
83 * @return true, if read successful, false otherwise
86 inline bool ReadHeader(FILE* fp, T& header)
88 unsigned int readLength = sizeof(T);
90 // Load the information directly into our structure
91 if (fread((void*)&header, 1, readLength, fp) != readLength)
99 bool LoadBmpHeader(FILE *fp, unsigned int &width, unsigned int &height, BmpFileHeader &fileHeader, BmpInfoHeader &infoHeader)
101 if (!ReadHeader(fp, fileHeader))
106 if (!ReadHeader(fp, infoHeader))
111 width = infoHeader.width;
112 height = infoHeader.height;
118 * function to decode format BI_RGB & bpp = 24 & bmp version5.
119 * @param[in] fp The file to read from
120 * @param[out] pixels The pointer that we want to store bmp data in
121 * @param[in] width bmp width
122 * @param[in] height bmp height
123 * @param[in] offset offset from bmp header to bmp image data
124 * @param[in] topDown indicate image data is read from bottom or from top
125 * @param[in] padding padded to a u_int32 boundary for each line
126 * @return true, if decode successful, false otherwise
128 bool DecodeRGB24V5(FILE *fp,
134 unsigned int rowStride,
135 unsigned int padding)
137 if(fp == NULL || pixels == NULL)
139 DALI_LOG_ERROR("Error decoding BMP_RGB24V5 format\n");
142 if ( fseek(fp, offset, SEEK_SET) )
144 DALI_LOG_ERROR("Error seeking BMP_RGB24V5 data\n");
148 for(unsigned int yPos = 0; yPos < height; yPos ++)
150 PixelBuffer *pixelsPtr = NULL;
153 pixelsPtr = pixels + ( yPos * rowStride);
157 pixelsPtr = pixels + (((height-1)-yPos) * rowStride);
159 if (fread(pixelsPtr, 1, rowStride, fp) != rowStride)
161 DALI_LOG_ERROR("Error reading the BMP image\n");
164 for(unsigned int i = 0; i < rowStride; i += 3)
166 unsigned char temp = pixelsPtr[i];
167 pixelsPtr[i] = pixelsPtr[i + 2];
168 pixelsPtr[i + 2] = temp;
173 // move past the padding.
174 if( fseek(fp, padding, SEEK_CUR) )
176 DALI_LOG_ERROR("Error moving past BMP_RGB24V5 padding\n");
184 * function to decode format BI_BITFIELDS & bpp = 32 & bmp version4.
185 * @param[in] fp The file to read from
186 * @param[out] pixels The pointer that we want to store bmp data in
187 * @param[in] width bmp width
188 * @param[in] height bmp height
189 * @param[in] offset offset from bmp header to bmp image data
190 * @param[in] topDown indicate image data is read from bottom or from top
191 * @param[in] rowStride bits span for each line
192 * @param[in] padding padded to a u_int32 boundary for each line
193 * @return true, if decode successful, false otherwise
195 bool DecodeBF32V4(FILE *fp,
201 unsigned int rowStride,
202 unsigned int padding)
204 if(fp == NULL || pixels == NULL)
206 DALI_LOG_ERROR("Error decoding BMP_BITFIELDS32V4 format\n");
209 if( fseek(fp, offset, SEEK_SET) )
211 DALI_LOG_ERROR("Error seeking BMP_BITFIELDS32V4 data\n");
215 for(unsigned int yPos = 0; yPos < height; yPos ++)
217 PixelBuffer *pixelsPtr = NULL;
220 pixelsPtr = pixels + ( yPos * rowStride);
224 pixelsPtr = pixels + (((height-1)-yPos) * rowStride);
226 if (fread(pixelsPtr, 1, rowStride, fp) != rowStride)
228 DALI_LOG_ERROR("Error reading the BMP image\n");
231 for(unsigned int i = 0; i < rowStride; i += 4)
233 unsigned char temp = pixelsPtr[i];
234 pixelsPtr[i] = pixelsPtr[i + 2];
235 pixelsPtr[i + 2] = temp;
239 // move past the padding.
240 if( fseek(fp, padding, SEEK_CUR) )
242 DALI_LOG_ERROR("Error moving past BMP_BITFIELDS32V4 padding\n");
251 * function to decode format BI_BITFIELDS & bpp = 32
252 * @param[in] fp The file to read from
253 * @param[out] pixels The pointer that we want to store bmp data in
254 * @param[in] width bmp width
255 * @param[in] height bmp height
256 * @param[in] offset offset from bmp header to bmp image data
257 * @param[in] topDown indicate image data is read from bottom or from top
258 * @param[in] rowStride bits span for each line
259 * @param[in] padding padded to a u_int32 boundary for each line
260 * @return true, if decode successful, false otherwise
262 bool DecodeBF32(FILE *fp,
268 unsigned int rowStride,
269 unsigned int padding)
271 if(fp == NULL || pixels == NULL)
273 DALI_LOG_ERROR("Error decoding BMP_BITFIELDS32 format\n");
276 if( fseek(fp, offset, SEEK_SET) )
278 DALI_LOG_ERROR("Error seeking BMP_BITFIELDS32 data\n");
282 for (unsigned int yPos = 0; yPos < height; yPos++)
284 PixelBuffer *pixelsPtr;
287 // the data in the file is top down, and we store the data top down
288 pixelsPtr = pixels + ( yPos * rowStride);
292 // the data in the file is bottom up, and we store the data top down
293 pixelsPtr = pixels + (((height-1)-yPos) * rowStride);
296 if (fread(pixelsPtr, 1, rowStride, fp) != rowStride)
298 DALI_LOG_ERROR("Error reading the BMP image\n");
301 for(unsigned int i = 0; i < rowStride; i += 4)
303 unsigned char temp = pixelsPtr[i];
304 pixelsPtr[i] = pixelsPtr[i + 2];
305 pixelsPtr[i + 2] = temp;
310 // move past the padding.
311 if( fseek(fp, padding, SEEK_CUR) )
313 DALI_LOG_ERROR("Error moving past BMP_BITFIELDS32 padding\n");
321 * function to decode format BI_BITFIELDS & bpp = 16 & R:G:B = 5:6:5
322 * @param[in] fp The file to read from
323 * @param[out] pixels The pointer that we want to store bmp data in
324 * @param[in] width bmp width
325 * @param[in] height bmp height
326 * @param[in] offset offset from bmp header to bmp image data
327 * @param[in] topDown indicate image data is read from bottom or from top
328 * @return true, if decode successful, false otherwise
330 bool DecodeBF565(FILE *fp,
337 if(fp == NULL || pixels == NULL)
339 DALI_LOG_ERROR("Error decoding RGB565 format\n");
342 if( fseek(fp, offset, SEEK_SET) )
344 DALI_LOG_ERROR("Error seeking RGB565 data\n");
348 width = ((width & 3) != 0) ? width + 4 - (width & 3) : width;
349 unsigned int rowStride = width * 2;
351 for(unsigned int i = 0; i < height; i++)
353 PixelBuffer *pixelsPtr = NULL;
356 // the data in the file is top down, and we store the data top down
357 pixelsPtr = pixels + ( i * rowStride);
361 // the data in the file is bottom up, and we store the data top down
362 pixelsPtr = pixels + (((height - 1) - i) * rowStride);
364 if(fread(pixelsPtr, 1, rowStride, fp) != rowStride)
374 * function to decode format BI_BITFIELDS & bpp = 16 & R:G:B = 5:5:5
375 * @param[in] fp The file to read from
376 * @param[out] pixels The pointer that we want to store bmp data in
377 * @param[in] width bmp width
378 * @param[in] height bmp height
379 * @param[in] offset offset from bmp header to bmp image data
380 * @param[in] topDown indicate image data is read from bottom or from top
381 * @return true, if decode successful, false otherwise
383 bool DecodeBF555(FILE *fp,
390 if(fp == NULL || pixels == NULL)
392 DALI_LOG_ERROR("Error decoding BMP_BITFIELDS555 format\n");
396 if( fseek(fp, offset, SEEK_SET) )
398 DALI_LOG_ERROR("Error seeking BMP_BITFIELDS555 data\n");
402 width = ((width & 3) != 0) ? width + 4 - (width & 3) : width;
404 std::vector<char> raw(width * height * 2);
405 unsigned int rawStride = width * 2;
406 unsigned int rowStride = width * 3;
409 for(unsigned int j = 0; j < height; j ++)
411 rawPtr = &raw[0] + ( j * rawStride);
412 if(fread(rawPtr, 1, rawStride, fp) != rawStride)
418 for (unsigned int yPos = 0; yPos < height; yPos++)
420 PixelBuffer *pixelsPtr = NULL;
423 // the data in the file is top down, and we store the data top down
424 pixelsPtr = pixels + ( yPos * rowStride);
428 // the data in the file is bottom up, and we store the data top down
429 pixelsPtr = pixels + (((height-1)-yPos) * rowStride);
432 for(unsigned int k = 0; k < width; k ++)
434 int index = yPos * rawStride + 2 * k;
435 pixelsPtr[3 * k] = ((raw[ index + 1] >> 2) & 0x1F) * 0xFF / 0x1F;
436 pixelsPtr[3 * k + 1] = (((raw[index + 1] & 0x03) << 3) | (raw[ index] >> 5)) * 0xFF/ 0x1F;
437 pixelsPtr[3 * k + 2] = (raw[ index] & 0x1F) * 0xFF / 0x1F;
444 * function to decode format BI_RGB & bpp = 16 & R:G:B = 5:5:5
445 * @param[in] fp The file to read from
446 * @param[out] pixels The pointer that we want to store bmp data in
447 * @param[in] width bmp width
448 * @param[in] height bmp height
449 * @param[in] offset offset from bmp header to bmp image data
450 * @param[in] topDown indicate image data is read from bottom or from top
451 * @return true, if decode successful, false otherwise
453 bool DecodeRGB555(FILE *fp,
460 if(fp == NULL || pixels == NULL)
462 DALI_LOG_ERROR("Error decoding BMP_RGB555 format\n");
465 if( fseek(fp, offset, SEEK_SET) )
467 DALI_LOG_ERROR("Error seeking BMP_RGB555 data\n");
471 width = ((width & 3) != 0) ? width + 4 - (width & 3) : width;
472 std::vector<char> raw(width * height * 2);
473 unsigned int rawStride = width * 2;
474 unsigned int rowStride = width * 3;
477 for(unsigned int j = 0; j < height; j ++)
479 rawPtr = &raw[0] + ( j * rawStride);
480 if(fread(rawPtr, 1, rawStride, fp) != rawStride)
485 for(unsigned int i = 0; i < height; i++)
487 PixelBuffer *pixelsPtr = NULL;
490 // the data in the file is top down, and we store the data top down
491 pixelsPtr = pixels + ( i * rowStride);
495 // the data in the file is bottom up, and we store the data top down
496 pixelsPtr = pixels + (((height - 1) - i) * rowStride);
498 for(unsigned int k = 0; k < width; k ++)
500 int index = i * rawStride + 2 * k;
501 pixelsPtr[3 * k] = ((raw[ index + 1] >> 2) & 0x1F) * 0xFF / 0x1F;
502 pixelsPtr[3 * k + 1] = (((raw[index + 1] & 0x03) << 3) | (raw[ index] >> 5)) * 0xFF/ 0x1F;
503 pixelsPtr[3 * k + 2] = (raw[ index] & 0x1F) * 0xFF / 0x1F;
511 * function to decode format BI_RGB & bpp = 1
512 * @param[in] fp The file to read from
513 * @param[out] pixels The pointer that we want to store bmp data in
514 * @param[in] width bmp width
515 * @param[in] height bmp height
516 * @param[in] offset offset from bmp header to bmp palette data
517 * @param[in] topDown indicate image data is read from bottom or from top
518 * @return true, if decode successful, false otherwise
520 bool DecodeRGB1(FILE *fp,
527 if(fp == NULL || pixels == NULL)
529 DALI_LOG_ERROR("Error decoding BMP_RGB1 format\n");
532 if( fseek(fp, offset, SEEK_SET) )
534 DALI_LOG_ERROR("Error seeking BMP_RGB1 data\n");
538 unsigned char colorTable[8] = {0};
540 unsigned int fillw = ((width & 63) != 0) ? width + 64 - (width & 63) : width;
541 std::vector<char> colorIndex(fillw * height);
542 unsigned int rowStride = fillw * 3; // RGB
545 if(fread(colorTable, 1, 8, fp) != 8)
550 for(unsigned int i = 0; i < fillw * height; i += 8)
552 if(fread(&cmd, 1, 1, fp) != 1)
557 colorIndex[i] = (cmd >> 7) & 0x01;
558 colorIndex[i + 1] = (cmd >> 6) & 0x01;
559 colorIndex[i + 2] = (cmd >> 5) & 0x01;
560 colorIndex[i + 3] = (cmd >> 4) & 0x01;
561 colorIndex[i + 4] = (cmd >> 3) & 0x01;
562 colorIndex[i + 5] = (cmd >> 2) & 0x01;
563 colorIndex[i + 6] = (cmd >> 1) & 0x01;
564 colorIndex[i + 7] = (cmd & 0x01);
567 for(unsigned int index = 0; index < height; index = index + 1)
569 PixelBuffer *pixelsPtr = NULL;
572 // the data in the file is top down, and we store the data top down
573 pixelsPtr = pixels + ( index * rowStride);
577 // the data in the file is bottom up, and we store the data top down
578 pixelsPtr = pixels + (((height - 1) - index) * rowStride);
580 for(unsigned int j = 0; j < fillw; j ++)
583 if((fillw * index + j ) < (fillw * height))
585 ctIndex = colorIndex[ fillw * index + j ];
591 // temp solution for PLM bug P130411-5268, there is one mono bmp that cause DecodeRGB1 API crash.
592 if( ((3 * j + 2) < height * fillw * 3) && (ctIndex < 2))
594 pixelsPtr[ 3 * j ] = colorTable[4 * ctIndex + 2];
595 pixelsPtr[3 * j + 1] = colorTable[4 * ctIndex + 1];
596 pixelsPtr[3 * j + 2] = colorTable[4 * ctIndex ];
604 * function to decode format BI_RGB & bpp = 4
605 * @param[in] fp The file to read from
606 * @param[out] pixels The pointer that we want to store bmp data in
607 * @param[in] width bmp width
608 * @param[in] height bmp height
609 * @param[in] offset offset from bmp header to bmp palette data
610 * @param[in] topDown indicate image data is read from bottom or from top
611 * @return true, if decode successful, false otherwise
613 bool DecodeRGB4(FILE *fp,
620 if(fp == NULL || pixels == NULL)
622 DALI_LOG_ERROR("Error decoding BMP_RGB4 format\n");
625 if( fseek(fp, offset, SEEK_SET) )
627 DALI_LOG_ERROR("Error seeking BMP_RGB4 data\n");
633 unsigned int fillw = ((width & 3) != 0) ? width + 4 - (width & 3) : width;
634 std::vector<char> colorIndex(fillw * height);
635 unsigned int rowStride = fillw * 3;
637 if(fread(colorTable, 1, 64, fp) != 64)
642 for(unsigned int i = 0; i < fillw * height; i += 2)
644 if (fread(&cmd, 1, 1, fp) != 1)
649 colorIndex[i] = cmd >> 4;
650 colorIndex[i + 1] = cmd & (0x0F);
654 for(unsigned int index = 0; index < height; index = index + 1)
656 PixelBuffer *pixelsPtr = NULL;
659 // the data in the file is top down, and we store the data top down
660 pixelsPtr = pixels + ( index * rowStride);
664 // the data in the file is bottom up, and we store the data top down
665 pixelsPtr = pixels + (((height - 1) - index) * rowStride);
667 for(unsigned int j = 0; j < fillw; j ++)
669 ctIndex = colorIndex[ fillw * index + j ];
670 pixelsPtr[ 3 * j ] = colorTable[4 * ctIndex + 2];
671 pixelsPtr[(3 * j + 1)] = colorTable[4 * ctIndex + 1];
672 pixelsPtr[(3 * j + 2)] = colorTable[4 * ctIndex ];
680 * function to decode format BI_RGB & bpp = 8
681 * @param[in] fp The file to read from
682 * @param[out] pixels The pointer that we want to store bmp data in
683 * @param[in] width bmp width
684 * @param[in] height bmp height
685 * @param[in] offset offset from bmp header to bmp palette data
686 * @param[in] topDown indicate image data is read from bottom or from top
687 * @return true, if decode successful, false otherwise
689 bool DecodeRGB8(FILE *fp,
696 if(fp == NULL || pixels == NULL)
698 DALI_LOG_ERROR("Error decoding BMP_RGB8 format\n");
701 if( fseek(fp, offset, SEEK_SET) )
703 DALI_LOG_ERROR("Error seeking BMP_RGB8 data\n");
707 std::vector<char> colorTable(1024);
708 width = ((width & 3) != 0) ? width + 4 - (width & 3) : width;
710 std::vector<char> colorIndex(width * height);
711 unsigned int rowStride = width * 3;//RGB8->RGB24
713 if(fread(&colorTable[0], 1, 1024, fp) != 1024)
717 for(unsigned int i = 0; i < width * height; i ++)
719 if (fread(&cmd, 1, 1, fp) != 1)
727 for(unsigned int index = 0; index < height; index = index + 1)
729 PixelBuffer *pixelsPtr = NULL;
732 // the data in the file is top down, and we store the data top down
733 pixelsPtr = pixels + ( index * rowStride);
737 // the data in the file is bottom up, and we store the data top down
738 pixelsPtr = pixels + (((height - 1) - index) * rowStride);
740 for(unsigned int j = 0; j < width; j ++)
742 ctIndex = colorIndex[ width * index + j ];
743 pixelsPtr[ 3 * j ] = colorTable[4 * ctIndex + 2];
744 pixelsPtr[(3 * j + 1)] = colorTable[4 * ctIndex + 1];
745 pixelsPtr[(3 * j + 2)] = colorTable[4 * ctIndex ];
752 * function to decode format BI_RLE4 & bpp = 4
753 * @param[in] fp The file to read from
754 * @param[out] pixels The pointer that we want to store bmp data in
755 * @param[in] width bmp width
756 * @param[in] height bmp height
757 * @param[in] offset offset from bmp header to bmp palette data
758 * @param[in] topDown indicate image data is read from bottom or from top
759 * @return true, if decode successful, false otherwise
761 bool DecodeRLE4(FILE *fp,
768 if(fp == NULL || pixels == NULL)
770 DALI_LOG_ERROR("Error decoding BMP_RLE4 format\n");
773 PixelBuffer *pixelsPtr = pixels;
774 width = ((width & 3) != 0) ? width + 4 - (width & 3) : width;
776 unsigned int cmdStride = 2;
778 std::vector<char> colorIndex(width * height >> 1);
779 std::vector<char> run;
784 width += (width & 1);
789 if( fseek(fp, offset, SEEK_SET) )
791 DALI_LOG_ERROR("Error seeking BMP_RLE4 data\n");
795 if (fread(colorTable, 1, 64, fp) != 64)
800 while((x >> 1) + y * width < width * height)
806 if (fread(cmd, 1, cmdStride, fp) != cmdStride)
810 if(cmd[0] == 0) // ESCAPE
814 case 1: //end of bitmap
817 case 0: // end of line
822 if (fread(cmd, 1, cmdStride, fp) != cmdStride)
824 DALI_LOG_ERROR("Error reading the BMP image\n");
827 dx = cmd[0] & (0xFF);
828 dy = cmd[1] & (0xFF);
833 // decode a literal run
834 unsigned int length = cmd[1] & (0xFF);
835 //size of run, which is word aligned
836 unsigned int bytesize = length;
837 bytesize += (bytesize & 1);
839 bytesize += (bytesize & 1);
840 run.resize(bytesize);
841 if(fread(&run[0], 1, bytesize, fp) != bytesize)
843 DALI_LOG_ERROR("Error reading the BMP image\n");
848 length += (length & 1);
850 for(unsigned int i = 0; i < length; i += 1)
852 colorIndex[(x >> 1) + width * (height - y - 1) + i] = run[i];
857 for(unsigned int i = 0; i < length; i ++)
859 if((i & 1) == 0)//copy high to low
861 colorIndex[((x + i) >> 1) + width * (height - y - 1)] |= ((run[i >> 1] & 0xF0) >> 4);
863 else //copy low to high
865 colorIndex[((x + i) >> 1) + width * (height - y - 1)] |= ((run[i >> 1] & 0x0F) << 4);
869 x += cmd[1] & (0xFF);
875 unsigned int length = cmd[0] & (0xFF);
878 length += (length & 1);
880 for(unsigned int i = 0; i < length; i ++)
882 colorIndex[(height-y-1)*width + i + (x >> 1)] = cmd[1];
887 for(unsigned int i = 0; i < length; i ++)
891 colorIndex[((x + i) >> 1) + width * (height - y - 1)] |= ((cmd[1] & 0xF0) >> 4);
895 colorIndex[((x + i) >> 1) + width * (height - y - 1)] |= ((cmd[1] & 0x0F) << 4);
899 x += cmd[0] & (0xFF);
905 for(unsigned int index = 0; index < (width * height ); index = index + 1)
907 ctIndexHigh = colorIndex[ index] >> 4;
908 ctIndexLow = colorIndex[index] & (0x0F);
909 pixelsPtr[6 * index ] = colorTable[4 * ctIndexHigh + 2];
910 pixelsPtr[6 * index + 1] = colorTable[4 * ctIndexHigh + 1];
911 pixelsPtr[6 * index + 2] = colorTable[4 * ctIndexHigh ];
912 pixelsPtr[6 * index + 3] = colorTable[4 * ctIndexLow + 2];
913 pixelsPtr[6 * index + 4] = colorTable[4 * ctIndexLow + 1];
914 pixelsPtr[6 * index + 5] = colorTable[4 * ctIndexLow ];
920 * function to decode format BI_RLE8 & bpp = 8
921 * @param[in] fp The file to read from
922 * @param[out] pixels The pointer that we want to store bmp data in
923 * @param[in] width bmp width
924 * @param[in] height bmp height
925 * @param[in] offset offset from bmp header to bmp palette data
926 * @param[in] topDown indicate image data is read from bottom or from top
927 * @return true, if decode successful, false otherwise
929 bool DecodeRLE8(FILE *fp,
936 if(fp == NULL || pixels == NULL)
938 DALI_LOG_ERROR("Error decoding BMP_RLE8 format\n");
941 PixelBuffer *pixelsPtr = pixels;
944 unsigned int cmdStride = 2;
946 width = ((width & 3) != 0) ? width + 4 - (width & 3) : width;
947 std::vector<char> colorTable(1024);
949 std::vector<char> colorIndex(width * height);
951 if( fseek(fp, offset, SEEK_SET) )
953 DALI_LOG_ERROR("Error seeking BMP_RLE8 data\n");
957 if (fread(&colorTable[0], 1, 1024, fp) != 1024)
965 unsigned int length = 0;
966 unsigned int copylength = 0;
967 std::vector<char> run;
968 while((x + y * width) < width * height )
974 if (fread(cmd, 1, cmdStride, fp) != cmdStride)
979 if(cmd[0] == 0)//ESCAPE
983 case 1: // end of bitmap
986 case 0: // end of line
991 if (fread(cmd, 1, cmdStride, fp) != cmdStride)
993 DALI_LOG_ERROR("Error reading the BMP image\n");
996 dx = cmd[0] & (0xFF);
997 dy = cmd[1] & (0xFF);
1002 //decode a literal run
1003 length = cmd[1] & (0xFF);
1004 copylength = length;
1005 //absolute mode must be word-aligned
1006 length += (length & 1);
1008 if(fread(&run[0], 1, length, fp) != length)
1010 DALI_LOG_ERROR("Error reading the BMP image\n");
1014 for(unsigned int i = 0; i < length; i += 1)
1016 colorIndex[x + width * (height - y - 1) + i] = run[i];
1021 }// end if cmd[0] ==
1024 length = cmd[0] & (0xFF);
1025 for(unsigned int i = 0; i < length; i ++)
1027 colorIndex[(height - y - 1) * width + x] = cmd[1];
1033 for(unsigned int index = 0; index < width * height; index = index + 1)
1035 ctIndex = colorIndex[ index];
1036 pixelsPtr[3 * index ] = colorTable[4 * ctIndex + 2];
1037 pixelsPtr[3 * index + 1] = colorTable[4 * ctIndex + 1];
1038 pixelsPtr[3 * index + 2] = colorTable[4 * ctIndex ];
1043 } // unnamed namespace
1045 bool LoadBmpHeader(FILE *fp, const ImageAttributes& attributes, unsigned int &width, unsigned int &height)
1047 BmpFileHeader fileHeader;
1048 BmpInfoHeader infoHeader;
1050 bool ret = LoadBmpHeader(fp, width, height, fileHeader, infoHeader);
1055 bool LoadBitmapFromBmp( FILE *fp, Bitmap& bitmap, ImageAttributes& attributes, const ResourceLoadingClient& client )
1057 DALI_ASSERT_DEBUG(bitmap.GetPackedPixelsProfile() != 0 && "Need a packed pixel bitmap to load into.");
1060 DALI_LOG_ERROR("Error loading bitmap\n");
1063 BmpFormat customizedFormat = BMP_NOTEXIST;
1064 BmpFileHeader fileHeader;
1065 BmpInfoHeader infoHeader;
1067 // Load the header info
1068 unsigned int width, height;
1070 if (!LoadBmpHeader(fp, width, height, fileHeader, infoHeader))
1075 Pixel::Format pixelFormat = Pixel::RGB888;
1076 switch(infoHeader.compression)
1079 switch (infoHeader.bitsPerPixel)
1082 pixelFormat = Pixel::BGR8888;
1086 if(fileHeader.offset == FileHeaderOffsetOfRGB24V5)//0x8A
1088 customizedFormat = BMP_RGB24V5;
1092 pixelFormat = Pixel::RGB888;
1097 customizedFormat = BMP_RGB555;
1101 customizedFormat = BMP_RGB8;
1105 customizedFormat = BMP_RGB4;
1109 customizedFormat = BMP_RGB1;
1112 DALI_LOG_WARNING("%d bits per pixel not supported for BMP files\n", infoHeader.bitsPerPixel);
1118 if(infoHeader.bitsPerPixel == 8)
1120 customizedFormat = BMP_RLE8;
1126 if(infoHeader.bitsPerPixel == 4)
1128 customizedFormat = BMP_RLE4;
1132 case 3: // // BI_BITFIELDS
1134 if(infoHeader.bitsPerPixel == 16)
1136 if( fseek(fp, 14 + infoHeader.infoHeaderSize + 1, SEEK_SET) )
1142 if(fread(&mask, 1, 1, fp) != 1)
1147 if((mask & 0x80) == MaskForBFRGB565) // mask is 0xF8
1149 pixelFormat = Pixel::RGB565;
1151 else if((mask & 0x80) == 0)// mask is 0x 7C
1153 customizedFormat = BMP_BITFIELDS555;
1160 else if(infoHeader.bitsPerPixel == 32)
1162 if(fileHeader.offset == FileHeaderOffsetOfBF32V4)// 0x7A
1164 customizedFormat = BMP_BITFIELDS32V4;
1168 customizedFormat = BMP_BITFIELDS32;
1174 DALI_LOG_WARNING("Compression not supported for BMP files\n");
1178 bool topDown = false;
1180 // if height is negative, bitmap data is top down
1181 if (infoHeader.height<0)
1183 infoHeader.height = abs(infoHeader.height);
1184 height = infoHeader.height;
1188 unsigned int rowStride = infoHeader.width * (infoHeader.bitsPerPixel >>3);
1190 // bitmaps row stride is padded to 4 bytes
1191 unsigned int padding = (rowStride % 4);
1194 padding = 4 - padding;
1197 PixelBuffer *pixels = NULL;
1198 int imageW = infoHeader.width;
1199 int pixelBufferW = 0;
1200 switch(customizedFormat)
1207 case BMP_BITFIELDS555:
1209 pixelBufferW = ((imageW & 3) != 0) ? imageW + 4 - (imageW & 3) : imageW;
1210 pixels = bitmap.GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGB888, pixelBufferW, abs(infoHeader.height));
1215 pixelBufferW = ((imageW & 63) != 0) ? imageW + 64 - (imageW & 63) : imageW;
1216 pixels = bitmap.GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGB888, pixelBufferW, abs(infoHeader.height));
1219 case BMP_BITFIELDS32:
1220 case BMP_BITFIELDS32V4:
1222 pixels = bitmap.GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGB8888, infoHeader.width, abs(infoHeader.height));
1227 pixels = bitmap.GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGB888, infoHeader.width, infoHeader.height);
1231 if(pixelFormat == Pixel::RGB565 )
1233 pixelBufferW = ((imageW & 3) != 0) ? imageW + 4 - (imageW & 3) : imageW;
1234 pixels = bitmap.GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGB565, pixelBufferW, abs(infoHeader.height));
1238 pixels = bitmap.GetPackedPixelsProfile()->ReserveBuffer(pixelFormat, infoHeader.width, infoHeader.height);
1243 // TODO: Add scaling support
1245 // Read the raw bitmap data
1246 PixelBuffer *pixelsPtr;
1247 bool decodeResult(false);
1248 switch(customizedFormat)
1252 decodeResult = DecodeRGB1( fp, pixels, infoHeader.width, abs(infoHeader.height), 14 + infoHeader.infoHeaderSize, topDown);
1257 decodeResult = DecodeRGB4(fp, pixels, infoHeader.width, infoHeader.height, 14 + infoHeader.infoHeaderSize, topDown);
1262 decodeResult = DecodeRLE4( fp, pixels, infoHeader.width, abs(infoHeader.height), 14 + infoHeader.infoHeaderSize, topDown);
1265 case BMP_BITFIELDS32:
1267 decodeResult = DecodeBF32(fp, pixels, infoHeader.width, abs(infoHeader.height), fileHeader.offset, topDown, rowStride, padding);
1270 case BMP_BITFIELDS555:
1272 decodeResult = DecodeBF555(fp, pixels,infoHeader.width, abs(infoHeader.height), fileHeader.offset, topDown);
1277 decodeResult = DecodeRGB555(fp, pixels, infoHeader.width, abs(infoHeader.height), fileHeader.offset, topDown);
1282 decodeResult = DecodeRGB8(fp, pixels, infoHeader.width, abs(infoHeader.height), 14 + infoHeader.infoHeaderSize, topDown);
1287 decodeResult = DecodeRLE8(fp, pixels, infoHeader.width, abs(infoHeader.height), 14 + infoHeader.infoHeaderSize, topDown);
1292 decodeResult = DecodeRGB24V5(fp, pixels, infoHeader.width, abs(infoHeader.height), fileHeader.offset, topDown, rowStride, padding);
1295 case BMP_BITFIELDS32V4:
1297 decodeResult = DecodeBF32V4(fp, pixels, infoHeader.width, abs(infoHeader.height), fileHeader.offset, topDown, rowStride, padding);
1302 if(pixelFormat == Pixel::RGB565)
1304 decodeResult = DecodeBF565(fp, pixels, infoHeader.width, abs(infoHeader.height), fileHeader.offset, topDown);
1308 for (unsigned int yPos = 0; yPos < height; yPos++)
1312 // the data in the file is top down, and we store the data top down
1313 pixelsPtr = pixels + ( yPos * rowStride);
1317 // the data in the file is bottom up, and we store the data top down
1318 pixelsPtr = pixels + (((height-1)-yPos) * rowStride);
1321 if (fread(pixelsPtr, 1, rowStride, fp) != rowStride)
1323 DALI_LOG_ERROR("Error reading the BMP image\n");
1327 // If 24 bit mode then swap Blue and Red pixels
1328 // BGR888 doesn't seem to be supported by dali-core
1329 if (infoHeader.bitsPerPixel == 24 )
1331 for(unsigned int i = 0; i < rowStride; i += 3)
1333 unsigned char temp = pixelsPtr[i];
1334 pixelsPtr[i] = pixelsPtr[i+2];
1335 pixelsPtr[i+2] = temp;
1341 if( fseek(fp, padding, SEEK_CUR) ) // move past the padding.
1343 DALI_LOG_ERROR("Error moving past BMP padding\n");
1347 decodeResult = true;
1358 attributes.SetSize(infoHeader.width, infoHeader.height);
1362 } // namespace TizenPlatform