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.
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>
28 using Integration::Bitmap;
29 using Dali::Integration::PixelBuffer;
30 namespace TizenPlatform
35 const unsigned int FileHeaderOffsetOfBF32V4 = 0x7A;
36 const unsigned int MaskForBFRGB565 = 0x80;
37 const unsigned int FileHeaderOffsetOfRGB24V5 = 0x8A;
41 BMP_RGB1 = 14, //BI_RGB & bpp =1
42 BMP_RGB4, //BI_RGB & bpp = 4
43 BMP_RGB8, //BI_RGB & bpp = 8
44 BMP_RGB555, //BI_RGB & bpp = 16
45 BMP_BITFIELDS555, //BI_BITFIELDS & 16bit & R:G:B = 5:5:5
46 BMP_BITFIELDS32, //BI_BITFIELDS & 32bit & R:G:B:A = 8:8:8:8
49 BMP_BITFIELDS32V4,//BI_BITFIELDS & 32bit
50 BMP_RGB24V5, //BI_RGB & bpp = 24 & bmp version5
56 unsigned short signature; // Bitmap file signature
57 unsigned int fileSize; // Bitmap file size in bytes
58 unsigned short reserved1; // Reserved bits
59 unsigned short reserved2; // Reserved bits
60 unsigned int offset; // Offset from BMP file header to BMP bits
61 } __attribute__ ( (__packed__)); // Stops the structure from being aligned to every 4 bytes
65 unsigned int infoHeaderSize; // Specifies the number of bytes required by the info header
66 unsigned int width; // The Image Width
67 int height; // The Image Height (negative value represents image data is flipped)
68 unsigned short planes; // The number of color planes, must be 1
69 unsigned short bitsPerPixel; // The bits per pixel
70 unsigned int compression; // The type of compression used by the image
71 unsigned int imageSize; // The size of the image in bytes
72 unsigned int xPixelsPerMeter; // The number of pixels per meter in x axis
73 unsigned int yPixelsPerMeter; // The number of pixels per meter in y axis
74 unsigned int numberOfColors; // The number of colors in the color table
75 unsigned int importantColors; // The important color count
76 } __attribute__ ( (__packed__)); // Stops the structure from being aligned to every 4 bytes
79 * Template function to read from the file directly into our structure.
80 * @param[in] fp The file to read from
81 * @param[out] header The structure we want to store our information in
82 * @return true, if read successful, false otherwise
85 inline bool ReadHeader(FILE* fp, T& header)
87 const unsigned int readLength = sizeof(T);
89 // Load the information directly into our structure
90 if ( fread( &header, 1, readLength, fp ) != readLength )
98 bool LoadBmpHeader(FILE *fp, unsigned int &width, unsigned int &height, BmpFileHeader &fileHeader, BmpInfoHeader &infoHeader)
100 if (!ReadHeader(fp, fileHeader))
105 if (!ReadHeader(fp, infoHeader))
110 width = infoHeader.width;
111 height = abs(infoHeader.height);
117 * function to decode format BI_RGB & bpp = 24 & bmp version5.
118 * @param[in] fp The file to read from
119 * @param[out] pixels The pointer that we want to store bmp data in
120 * @param[in] width bmp width
121 * @param[in] height bmp height
122 * @param[in] offset offset from bmp header to bmp image data
123 * @param[in] topDown indicate image data is read from bottom or from top
124 * @param[in] padding padded to a u_int32 boundary for each line
125 * @return true, if decode successful, false otherwise
127 bool DecodeRGB24V5(FILE *fp,
133 unsigned int rowStride,
134 unsigned int padding)
136 if(fp == NULL || pixels == NULL)
138 DALI_LOG_ERROR("Error decoding BMP_RGB24V5 format\n");
141 if ( fseek(fp, offset, SEEK_SET) )
143 DALI_LOG_ERROR("Error seeking BMP_RGB24V5 data\n");
147 for(unsigned int yPos = 0; yPos < height; yPos ++)
149 PixelBuffer *pixelsPtr = NULL;
152 pixelsPtr = pixels + ( yPos * rowStride);
156 pixelsPtr = pixels + (((height-1)-yPos) * rowStride);
158 if (fread(pixelsPtr, 1, rowStride, fp) != rowStride)
160 DALI_LOG_ERROR("Error reading the BMP image\n");
163 for(unsigned int i = 0; i < rowStride; i += 3)
165 unsigned char temp = pixelsPtr[i];
166 pixelsPtr[i] = pixelsPtr[i + 2];
167 pixelsPtr[i + 2] = temp;
172 // move past the padding.
173 if( fseek(fp, padding, SEEK_CUR) )
175 DALI_LOG_ERROR("Error moving past BMP_RGB24V5 padding\n");
183 * function to decode format BI_BITFIELDS & bpp = 32 & bmp version4.
184 * @param[in] fp The file to read from
185 * @param[out] pixels The pointer that we want to store bmp data in
186 * @param[in] width bmp width
187 * @param[in] height bmp height
188 * @param[in] offset offset from bmp header to bmp image data
189 * @param[in] topDown indicate image data is read from bottom or from top
190 * @param[in] rowStride bits span for each line
191 * @param[in] padding padded to a u_int32 boundary for each line
192 * @return true, if decode successful, false otherwise
194 bool DecodeBF32V4(FILE *fp,
200 unsigned int rowStride,
201 unsigned int padding)
203 if(fp == NULL || pixels == NULL)
205 DALI_LOG_ERROR("Error decoding BMP_BITFIELDS32V4 format\n");
208 if( fseek(fp, offset, SEEK_SET) )
210 DALI_LOG_ERROR("Error seeking BMP_BITFIELDS32V4 data\n");
214 for(unsigned int yPos = 0; yPos < height; yPos ++)
216 PixelBuffer *pixelsPtr = NULL;
219 pixelsPtr = pixels + ( yPos * rowStride);
223 pixelsPtr = pixels + (((height-1)-yPos) * rowStride);
225 if (fread(pixelsPtr, 1, rowStride, fp) != rowStride)
227 DALI_LOG_ERROR("Error reading the BMP image\n");
230 for(unsigned int i = 0; i < rowStride; i += 4)
232 unsigned char temp = pixelsPtr[i];
233 pixelsPtr[i] = pixelsPtr[i + 2];
234 pixelsPtr[i + 2] = temp;
238 // move past the padding.
239 if( fseek(fp, padding, SEEK_CUR) )
241 DALI_LOG_ERROR("Error moving past BMP_BITFIELDS32V4 padding\n");
250 * function to decode format BI_BITFIELDS & bpp = 32
251 * @param[in] fp The file to read from
252 * @param[out] pixels The pointer that we want to store bmp data in
253 * @param[in] width bmp width
254 * @param[in] height bmp height
255 * @param[in] offset offset from bmp header to bmp image data
256 * @param[in] topDown indicate image data is read from bottom or from top
257 * @param[in] rowStride bits span for each line
258 * @param[in] padding padded to a u_int32 boundary for each line
259 * @return true, if decode successful, false otherwise
261 bool DecodeBF32(FILE *fp,
267 unsigned int rowStride,
268 unsigned int padding)
270 if(fp == NULL || pixels == NULL)
272 DALI_LOG_ERROR("Error decoding BMP_BITFIELDS32 format\n");
275 if( fseek(fp, offset, SEEK_SET) )
277 DALI_LOG_ERROR("Error seeking BMP_BITFIELDS32 data\n");
281 for (unsigned int yPos = 0; yPos < height; yPos++)
283 PixelBuffer *pixelsPtr;
286 // the data in the file is top down, and we store the data top down
287 pixelsPtr = pixels + ( yPos * rowStride);
291 // the data in the file is bottom up, and we store the data top down
292 pixelsPtr = pixels + (((height-1)-yPos) * rowStride);
295 if (fread(pixelsPtr, 1, rowStride, fp) != rowStride)
297 DALI_LOG_ERROR("Error reading the BMP image\n");
300 for(unsigned int i = 0; i < rowStride; i += 4)
302 unsigned char temp = pixelsPtr[i];
303 pixelsPtr[i] = pixelsPtr[i + 2];
304 pixelsPtr[i + 2] = temp;
309 // move past the padding.
310 if( fseek(fp, padding, SEEK_CUR) )
312 DALI_LOG_ERROR("Error moving past BMP_BITFIELDS32 padding\n");
320 * function to decode format BI_BITFIELDS & bpp = 16 & R:G:B = 5:6:5
321 * @param[in] fp The file to read from
322 * @param[out] pixels The pointer that we want to store bmp data in
323 * @param[in] width bmp width
324 * @param[in] height bmp height
325 * @param[in] offset offset from bmp header to bmp image data
326 * @param[in] topDown indicate image data is read from bottom or from top
327 * @return true, if decode successful, false otherwise
329 bool DecodeBF565(FILE *fp,
336 if(fp == NULL || pixels == NULL)
338 DALI_LOG_ERROR("Error decoding RGB565 format\n");
341 if( fseek(fp, offset, SEEK_SET) )
343 DALI_LOG_ERROR("Error seeking RGB565 data\n");
347 width = ((width & 3) != 0) ? width + 4 - (width & 3) : width;
348 unsigned int rowStride = width * 2;
350 for(unsigned int i = 0; i < height; i++)
352 PixelBuffer *pixelsPtr = NULL;
355 // the data in the file is top down, and we store the data top down
356 pixelsPtr = pixels + ( i * rowStride);
360 // the data in the file is bottom up, and we store the data top down
361 pixelsPtr = pixels + (((height - 1) - i) * rowStride);
363 if(fread(pixelsPtr, 1, rowStride, fp) != rowStride)
373 * function to decode format BI_BITFIELDS & bpp = 16 & R:G:B = 5:5:5
374 * @param[in] fp The file to read from
375 * @param[out] pixels The pointer that we want to store bmp data in
376 * @param[in] width bmp width
377 * @param[in] height bmp height
378 * @param[in] offset offset from bmp header to bmp image data
379 * @param[in] topDown indicate image data is read from bottom or from top
380 * @return true, if decode successful, false otherwise
382 bool DecodeBF555(FILE *fp,
389 if(fp == NULL || pixels == NULL)
391 DALI_LOG_ERROR("Error decoding BMP_BITFIELDS555 format\n");
395 if( fseek(fp, offset, SEEK_SET) )
397 DALI_LOG_ERROR("Error seeking BMP_BITFIELDS555 data\n");
401 width = ((width & 3) != 0) ? width + 4 - (width & 3) : width;
403 std::vector<char> raw(width * height * 2);
404 unsigned int rawStride = width * 2;
405 unsigned int rowStride = width * 3;
408 for(unsigned int j = 0; j < height; j ++)
410 rawPtr = &raw[0] + ( j * rawStride);
411 if(fread(rawPtr, 1, rawStride, fp) != rawStride)
417 for (unsigned int yPos = 0; yPos < height; yPos++)
419 PixelBuffer *pixelsPtr = NULL;
422 // the data in the file is top down, and we store the data top down
423 pixelsPtr = pixels + ( yPos * rowStride);
427 // the data in the file is bottom up, and we store the data top down
428 pixelsPtr = pixels + (((height-1)-yPos) * rowStride);
431 for(unsigned int k = 0; k < width; k ++)
433 int index = yPos * rawStride + 2 * k;
434 pixelsPtr[3 * k] = ((raw[ index + 1] >> 2) & 0x1F) * 0xFF / 0x1F;
435 pixelsPtr[3 * k + 1] = (((raw[index + 1] & 0x03) << 3) | (raw[ index] >> 5)) * 0xFF/ 0x1F;
436 pixelsPtr[3 * k + 2] = (raw[ index] & 0x1F) * 0xFF / 0x1F;
443 * function to decode format BI_RGB & bpp = 16 & R:G:B = 5:5:5
444 * @param[in] fp The file to read from
445 * @param[out] pixels The pointer that we want to store bmp data in
446 * @param[in] width bmp width
447 * @param[in] height bmp height
448 * @param[in] offset offset from bmp header to bmp image data
449 * @param[in] topDown indicate image data is read from bottom or from top
450 * @return true, if decode successful, false otherwise
452 bool DecodeRGB555(FILE *fp,
459 if(fp == NULL || pixels == NULL)
461 DALI_LOG_ERROR("Error decoding BMP_RGB555 format\n");
464 if( fseek(fp, offset, SEEK_SET) )
466 DALI_LOG_ERROR("Error seeking BMP_RGB555 data\n");
470 width = ((width & 3) != 0) ? width + 4 - (width & 3) : width;
471 std::vector<char> raw(width * height * 2);
472 unsigned int rawStride = width * 2;
473 unsigned int rowStride = width * 3;
476 for(unsigned int j = 0; j < height; j ++)
478 rawPtr = &raw[0] + ( j * rawStride);
479 if(fread(rawPtr, 1, rawStride, fp) != rawStride)
484 for(unsigned int i = 0; i < height; i++)
486 PixelBuffer *pixelsPtr = NULL;
489 // the data in the file is top down, and we store the data top down
490 pixelsPtr = pixels + ( i * rowStride);
494 // the data in the file is bottom up, and we store the data top down
495 pixelsPtr = pixels + (((height - 1) - i) * rowStride);
497 for(unsigned int k = 0; k < width; k ++)
499 int index = i * rawStride + 2 * k;
500 pixelsPtr[3 * k] = ((raw[ index + 1] >> 2) & 0x1F) * 0xFF / 0x1F;
501 pixelsPtr[3 * k + 1] = (((raw[index + 1] & 0x03) << 3) | (raw[ index] >> 5)) * 0xFF/ 0x1F;
502 pixelsPtr[3 * k + 2] = (raw[ index] & 0x1F) * 0xFF / 0x1F;
510 * function to decode format BI_RGB & bpp = 1
511 * @param[in] fp The file to read from
512 * @param[out] pixels The pointer that we want to store bmp data in
513 * @param[in] width bmp width
514 * @param[in] height bmp height
515 * @param[in] offset offset from bmp header to bmp palette data
516 * @param[in] topDown indicate image data is read from bottom or from top
517 * @return true, if decode successful, false otherwise
519 bool DecodeRGB1(FILE *fp,
526 if(fp == NULL || pixels == NULL)
528 DALI_LOG_ERROR("Error decoding BMP_RGB1 format\n");
531 if( fseek(fp, offset, SEEK_SET) )
533 DALI_LOG_ERROR("Error seeking BMP_RGB1 data\n");
537 unsigned char colorTable[8] = {0};
539 unsigned int fillw = ((width & 63) != 0) ? width + 64 - (width & 63) : width;
540 std::vector<char> colorIndex(fillw * height);
541 unsigned int rowStride = fillw * 3; // RGB
544 if(fread(colorTable, 1, 8, fp) != 8)
549 for(unsigned int i = 0; i < fillw * height; i += 8)
551 if(fread(&cmd, 1, 1, fp) != 1)
556 colorIndex[i] = (cmd >> 7) & 0x01;
557 colorIndex[i + 1] = (cmd >> 6) & 0x01;
558 colorIndex[i + 2] = (cmd >> 5) & 0x01;
559 colorIndex[i + 3] = (cmd >> 4) & 0x01;
560 colorIndex[i + 4] = (cmd >> 3) & 0x01;
561 colorIndex[i + 5] = (cmd >> 2) & 0x01;
562 colorIndex[i + 6] = (cmd >> 1) & 0x01;
563 colorIndex[i + 7] = (cmd & 0x01);
566 for(unsigned int index = 0; index < height; index = index + 1)
568 PixelBuffer *pixelsPtr = NULL;
571 // the data in the file is top down, and we store the data top down
572 pixelsPtr = pixels + ( index * rowStride);
576 // the data in the file is bottom up, and we store the data top down
577 pixelsPtr = pixels + (((height - 1) - index) * rowStride);
579 for(unsigned int j = 0; j < fillw; j ++)
581 unsigned int ctIndex = 0;
582 if((fillw * index + j ) < (fillw * height))
584 ctIndex = colorIndex[ fillw * index + j ];
590 // temp solution for PLM bug P130411-5268, there is one mono bmp that cause DecodeRGB1 API crash.
591 if( ((3 * j + 2) < height * fillw * 3) && (ctIndex < 2))
593 pixelsPtr[ 3 * j ] = colorTable[4 * ctIndex + 2];
594 pixelsPtr[3 * j + 1] = colorTable[4 * ctIndex + 1];
595 pixelsPtr[3 * j + 2] = colorTable[4 * ctIndex ];
603 * function to decode format BI_RGB & bpp = 4
604 * @param[in] fp The file to read from
605 * @param[out] pixels The pointer that we want to store bmp data in
606 * @param[in] width bmp width
607 * @param[in] height bmp height
608 * @param[in] offset offset from bmp header to bmp palette data
609 * @param[in] topDown indicate image data is read from bottom or from top
610 * @return true, if decode successful, false otherwise
612 bool DecodeRGB4(FILE *fp,
619 if(fp == NULL || pixels == NULL)
621 DALI_LOG_ERROR("Error decoding BMP_RGB4 format\n");
624 if( fseek(fp, offset, SEEK_SET) )
626 DALI_LOG_ERROR("Error seeking BMP_RGB4 data\n");
632 unsigned int fillw = ((width & 3) != 0) ? width + 4 - (width & 3) : width;
633 std::vector<char> colorIndex(fillw * height);
634 unsigned int rowStride = fillw * 3;
636 if(fread(colorTable, 1, 64, fp) != 64)
641 for(unsigned int i = 0; i < fillw * height; i += 2)
643 if (fread(&cmd, 1, 1, fp) != 1)
648 colorIndex[i] = cmd >> 4;
649 colorIndex[i + 1] = cmd & (0x0F);
651 unsigned int ctIndex = 0;
653 for(unsigned int index = 0; index < height; index = index + 1)
655 PixelBuffer *pixelsPtr = NULL;
658 // the data in the file is top down, and we store the data top down
659 pixelsPtr = pixels + ( index * rowStride);
663 // the data in the file is bottom up, and we store the data top down
664 pixelsPtr = pixels + (((height - 1) - index) * rowStride);
666 for(unsigned int j = 0; j < fillw; j ++)
668 ctIndex = colorIndex[ fillw * index + j ];
669 pixelsPtr[ 3 * j ] = colorTable[4 * ctIndex + 2];
670 pixelsPtr[(3 * j + 1)] = colorTable[4 * ctIndex + 1];
671 pixelsPtr[(3 * j + 2)] = colorTable[4 * ctIndex ];
679 * function to decode format BI_RGB & bpp = 8
680 * @param[in] fp The file to read from
681 * @param[out] pixels The pointer that we want to store bmp data in
682 * @param[in] width bmp width
683 * @param[in] height bmp height
684 * @param[in] offset offset from bmp header to bmp palette data
685 * @param[in] topDown indicate image data is read from bottom or from top
686 * @return true, if decode successful, false otherwise
688 bool DecodeRGB8(FILE *fp,
695 if(fp == NULL || pixels == NULL)
697 DALI_LOG_ERROR("Error decoding BMP_RGB8 format\n");
700 if( fseek(fp, offset, SEEK_SET) )
702 DALI_LOG_ERROR("Error seeking BMP_RGB8 data\n");
706 std::vector<char> colorTable(1024);
707 width = ((width & 3) != 0) ? width + 4 - (width & 3) : width;
709 std::vector<char> colorIndex(width * height);
710 unsigned int rowStride = width * 3;//RGB8->RGB24
712 if(fread(&colorTable[0], 1, 1024, fp) != 1024)
716 for(unsigned int i = 0; i < width * height; i ++)
718 if (fread(&cmd, 1, 1, fp) != 1)
725 unsigned int ctIndex = 0;
726 for(unsigned int index = 0; index < height; index = index + 1)
728 PixelBuffer *pixelsPtr = NULL;
731 // the data in the file is top down, and we store the data top down
732 pixelsPtr = pixels + ( index * rowStride);
736 // the data in the file is bottom up, and we store the data top down
737 pixelsPtr = pixels + (((height - 1) - index) * rowStride);
739 for(unsigned int j = 0; j < width; j ++)
741 ctIndex = colorIndex[ width * index + j ];
742 pixelsPtr[ 3 * j ] = colorTable[4 * ctIndex + 2];
743 pixelsPtr[(3 * j + 1)] = colorTable[4 * ctIndex + 1];
744 pixelsPtr[(3 * j + 2)] = colorTable[4 * ctIndex ];
751 * function to decode format BI_RLE4 & bpp = 4
752 * @param[in] fp The file to read from
753 * @param[out] pixels The pointer that we want to store bmp data in
754 * @param[in] width bmp width
755 * @param[in] height bmp height
756 * @param[in] offset offset from bmp header to bmp palette data
757 * @param[in] topDown indicate image data is read from bottom or from top
758 * @return true, if decode successful, false otherwise
760 bool DecodeRLE4(FILE *fp,
767 if(fp == NULL || pixels == NULL)
769 DALI_LOG_ERROR("Error decoding BMP_RLE4 format\n");
772 PixelBuffer *pixelsPtr = pixels;
773 width = ((width & 3) != 0) ? width + 4 - (width & 3) : width;
775 unsigned int cmdStride = 2;
777 std::vector<char> colorIndex(width * height >> 1);
778 std::vector<char> run;
783 width += (width & 1);
788 if( fseek(fp, offset, SEEK_SET) )
790 DALI_LOG_ERROR("Error seeking BMP_RLE4 data\n");
794 if (fread(colorTable, 1, 64, fp) != 64)
799 while((x >> 1) + y * width < width * height)
805 if (fread(cmd, 1, cmdStride, fp) != cmdStride)
809 if(cmd[0] == 0) // ESCAPE
813 case 1: //end of bitmap
816 case 0: // end of line
821 if (fread(cmd, 1, cmdStride, fp) != cmdStride)
823 DALI_LOG_ERROR("Error reading the BMP image\n");
826 dx = cmd[0] & (0xFF);
827 dy = cmd[1] & (0xFF);
832 // decode a literal run
833 unsigned int length = cmd[1] & (0xFF);
834 //size of run, which is word aligned
835 unsigned int bytesize = length;
836 bytesize += (bytesize & 1);
838 bytesize += (bytesize & 1);
839 run.resize(bytesize);
840 if(fread(&run[0], 1, bytesize, fp) != bytesize)
842 DALI_LOG_ERROR("Error reading the BMP image\n");
847 length += (length & 1);
849 for(unsigned int i = 0; i < length; i += 1)
851 colorIndex[(x >> 1) + width * (height - y - 1) + i] = run[i];
856 for(unsigned int i = 0; i < length; i ++)
858 if((i & 1) == 0)//copy high to low
860 colorIndex[((x + i) >> 1) + width * (height - y - 1)] |= ((run[i >> 1] & 0xF0) >> 4);
862 else //copy low to high
864 colorIndex[((x + i) >> 1) + width * (height - y - 1)] |= ((run[i >> 1] & 0x0F) << 4);
868 x += cmd[1] & (0xFF);
874 unsigned int length = cmd[0] & (0xFF);
877 length += (length & 1);
879 for(unsigned int i = 0; i < length; i ++)
881 colorIndex[(height-y-1)*width + i + (x >> 1)] = cmd[1];
886 for(unsigned int i = 0; i < length; i ++)
890 colorIndex[((x + i) >> 1) + width * (height - y - 1)] |= ((cmd[1] & 0xF0) >> 4);
894 colorIndex[((x + i) >> 1) + width * (height - y - 1)] |= ((cmd[1] & 0x0F) << 4);
898 x += cmd[0] & (0xFF);
904 for(unsigned int index = 0; index < (width * height ); index = index + 1)
906 ctIndexHigh = colorIndex[ index] >> 4;
907 ctIndexLow = colorIndex[index] & (0x0F);
908 pixelsPtr[6 * index ] = colorTable[4 * ctIndexHigh + 2];
909 pixelsPtr[6 * index + 1] = colorTable[4 * ctIndexHigh + 1];
910 pixelsPtr[6 * index + 2] = colorTable[4 * ctIndexHigh ];
911 pixelsPtr[6 * index + 3] = colorTable[4 * ctIndexLow + 2];
912 pixelsPtr[6 * index + 4] = colorTable[4 * ctIndexLow + 1];
913 pixelsPtr[6 * index + 5] = colorTable[4 * ctIndexLow ];
919 * function to decode format BI_RLE8 & bpp = 8
920 * @param[in] fp The file to read from
921 * @param[out] pixels The pointer that we want to store bmp data in
922 * @param[in] width bmp width
923 * @param[in] height bmp height
924 * @param[in] offset offset from bmp header to bmp palette data
925 * @param[in] topDown indicate image data is read from bottom or from top
926 * @return true, if decode successful, false otherwise
928 bool DecodeRLE8(FILE *fp,
935 if(fp == NULL || pixels == NULL)
937 DALI_LOG_ERROR("Error decoding BMP_RLE8 format\n");
940 PixelBuffer *pixelsPtr = pixels;
943 unsigned int cmdStride = 2;
945 width = ((width & 3) != 0) ? width + 4 - (width & 3) : width;
946 std::vector<char> colorTable(1024);
948 std::vector<char> colorIndex(width * height);
950 if( fseek(fp, offset, SEEK_SET) )
952 DALI_LOG_ERROR("Error seeking BMP_RLE8 data\n");
956 if (fread(&colorTable[0], 1, 1024, fp) != 1024)
964 unsigned int length = 0;
965 unsigned int copylength = 0;
966 std::vector<char> run;
967 while((x + y * width) < width * height )
973 if (fread(cmd, 1, cmdStride, fp) != cmdStride)
978 if(cmd[0] == 0)//ESCAPE
982 case 1: // end of bitmap
985 case 0: // end of line
990 if (fread(cmd, 1, cmdStride, fp) != cmdStride)
992 DALI_LOG_ERROR("Error reading the BMP image\n");
995 dx = cmd[0] & (0xFF);
996 dy = cmd[1] & (0xFF);
1001 //decode a literal run
1002 length = cmd[1] & (0xFF);
1003 copylength = length;
1004 //absolute mode must be word-aligned
1005 length += (length & 1);
1007 if(fread(&run[0], 1, length, fp) != length)
1009 DALI_LOG_ERROR("Error reading the BMP image\n");
1013 for(unsigned int i = 0; i < length; i += 1)
1015 colorIndex[x + width * (height - y - 1) + i] = run[i];
1020 }// end if cmd[0] ==
1023 length = cmd[0] & (0xFF);
1024 for(unsigned int i = 0; i < length; i ++)
1026 colorIndex[(height - y - 1) * width + x] = cmd[1];
1032 for(unsigned int index = 0; index < width * height; index = index + 1)
1034 ctIndex = colorIndex[ index];
1035 pixelsPtr[3 * index ] = colorTable[4 * ctIndex + 2];
1036 pixelsPtr[3 * index + 1] = colorTable[4 * ctIndex + 1];
1037 pixelsPtr[3 * index + 2] = colorTable[4 * ctIndex ];
1042 } // unnamed namespace
1044 bool LoadBmpHeader( const ImageLoader::Input& input, unsigned int& width, unsigned int& height )
1046 BmpFileHeader fileHeader;
1047 BmpInfoHeader infoHeader;
1049 bool ret = LoadBmpHeader( input.file, width, height, fileHeader, infoHeader );
1054 bool LoadBitmapFromBmp( const ImageLoader::Input& input, Integration::Bitmap& bitmap )
1056 DALI_ASSERT_DEBUG( bitmap.GetPackedPixelsProfile() != 0 && "Need a packed pixel bitmap to load into." );
1057 FILE* const fp = input.file;
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;
1361 } // namespace TizenPlatform