Internal API to make a BMP file from buffer added.
authorYoungki Ahn <ykahn@samsung.com>
Thu, 16 May 2013 02:55:18 +0000 (11:55 +0900)
committerYoungki Ahn <ykahn@samsung.com>
Thu, 16 May 2013 02:55:18 +0000 (11:55 +0900)
Change-Id: I0d464530bfc6ee77eb124ce56e080bb7294ce855
Signed-off-by: Youngki Ahn <ykahn@samsung.com>
src/graphics/FGrp_BitmapImpl.cpp

index 87072d6..4676fe7 100755 (executable)
@@ -2362,4 +2362,141 @@ _OSP_EXPORT_ bool _GetImageFileSize(const Tizen::Base::String fileName, int& out
        return false;
 }
 
+_OSP_EXPORT_ bool _SaveToBmp(unsigned long* pBuffer, int width, int height, int pixelPerLine, const Tizen::Base::String& fileName)
+{\r
+       unsigned char bmpFileHeader[] =\r
+       {\r
+               0x42, 0x4D,\r
+               0x00, 0x00, 0x00, 0x00, // size\r
+               0x00, 0x00,\r
+               0x00, 0x00,\r
+               0x36, 0x00, 0x00, 0x00 // offset\r
+       };\r
+\r
+       unsigned char bmpDibHeader[] =\r
+       {\r
+               0x28, 0x00, 0x00, 0x00,\r
+               0x00, 0x00, 0x00, 0x00, // width\r
+               0x00, 0x00, 0x00, 0x00, // height\r
+               0x01, 0x00,\r
+               0x18, 0x00,\r
+               0x00, 0x00, 0x00, 0x00,\r
+               0x00, 0x00, 0x00, 0x00, // image size\r
+               0x00, 0x00, 0x00, 0x00,\r
+               0x00, 0x00, 0x00, 0x00,\r
+               0x00, 0x00, 0x00, 0x00,\r
+               0x00, 0x00, 0x00, 0x00\r
+       };\r
+\r
+       int dstPitch  = width * 3;\r
+\r
+       while (dstPitch % 4)\r
+       {\r
+               ++dstPitch;\r
+       }\r
+\r
+       int imageSize = dstPitch * height;\r
+       int offset = sizeof(bmpFileHeader) + sizeof(bmpDibHeader);\r
+\r
+       // fill in the reserved fields \r
+       {\r
+               struct _LittleEndian\r
+               {\r
+                       static inline void Write(unsigned char* pBuffer, unsigned long data)\r
+                       {\r
+                               pBuffer[0] = (data >>  0) & 0xFF;\r
+                               pBuffer[1] = (data >>  8) & 0xFF;\r
+                               pBuffer[2] = (data >> 16) & 0xFF;\r
+                               pBuffer[3] = (data >> 24) & 0xFF;\r
+                       }\r
+               };\r
+\r
+               // offset (little endian)\r
+               _LittleEndian::Write(&bmpFileHeader[2], imageSize + offset);\r
+               // offset (little endian)\r
+               _LittleEndian::Write(&bmpFileHeader[10], offset);\r
+               // width (little endian)\r
+               _LittleEndian::Write(&bmpDibHeader[4], width);\r
+               // height (little endian)\r
+               _LittleEndian::Write(&bmpDibHeader[8], height);\r
+               // image size (little endian)\r
+               _LittleEndian::Write(&bmpDibHeader[20], imageSize);\r
+       }\r
+\r
+       Tizen::Io::File file;\r
+\r
+       result r = file.Construct(fileName, "wb");\r
+\r
+       if (r != E_SUCCESS)\r
+       {\r
+               return false;\r
+       }\r
+\r
+       file.Write(bmpFileHeader, sizeof(bmpFileHeader));\r
+       file.Write(bmpDibHeader, sizeof(bmpDibHeader));\r
+\r
+       {\r
+               typedef unsigned long SourPixel;\r
+               typedef unsigned char DestPixel;\r
+\r
+               _Util::AutoPtr<DestPixel> lineBuffer(new DestPixel[dstPitch]);\r
+\r
+               for (int y = 0; y < height; y++)\r
+               {\r
+                       memset(lineBuffer.Get(), 0, dstPitch);\r
+\r
+                       SourPixel* pSour32 = pBuffer + pixelPerLine * (height - y - 1);\r
+                       DestPixel* pDest08 = lineBuffer.Get();\r
+\r
+                       for (int x = 0; x < width; x++)\r
+                       {\r
+                               *pDest08++ = (unsigned char)(*pSour32);\r
+                               *pDest08++ = (unsigned char)(*pSour32 >> 8);\r
+                               *pDest08++ = (unsigned char)(*pSour32 >> 16);\r
+\r
+                               ++pSour32;\r
+                       }\r
+\r
+                       file.Write(lineBuffer.Get(), dstPitch);\r
+               }\r
+\r
+       }\r
+\r
+       return true;\r
+}
+
+_OSP_EXPORT_ bool _SaveToBmp(const Canvas& canvas, const Tizen::Base::String& fileName)
+{
+       bool r = false;
+       BufferInfo bi;\r
+\r
+       const_cast<Canvas*>(&canvas)->Lock(bi);\r
+\r
+       if (bi.bitsPerPixel == 32)\r
+       {\r
+               r = _SaveToBmp((unsigned long*)bi.pPixels, bi.width, bi.height, bi.pitch * 8 / bi.bitsPerPixel, fileName);\r
+       }\r
+\r
+       const_cast<Canvas*>(&canvas)->Unlock();\r
+\r
+       return r;\r
+}
+
+_OSP_EXPORT_ bool _SaveToBmp(const Bitmap& bitmap, const Tizen::Base::String& fileName)
+{
+       bool r = false;
+       BufferInfo bi;\r
+\r
+       const_cast<Bitmap*>(&bitmap)->Lock(bi);\r
+\r
+       if (bi.bitsPerPixel == 32)\r
+       {\r
+               r = _SaveToBmp((unsigned long*)bi.pPixels, bi.width, bi.height, bi.pitch * 8 / bi.bitsPerPixel, fileName);\r
+       }\r
+\r
+       const_cast<Bitmap*>(&bitmap)->Unlock();\r
+\r
+       return r;\r
+}
+
 }} // Tizen::Graphics