From 9d82d7e4264307d0573b646ca16e4f985e426b75 Mon Sep 17 00:00:00 2001 From: Youngki Ahn Date: Thu, 16 May 2013 11:55:18 +0900 Subject: [PATCH] Internal API to make a BMP file from buffer added. Change-Id: I0d464530bfc6ee77eb124ce56e080bb7294ce855 Signed-off-by: Youngki Ahn --- src/graphics/FGrp_BitmapImpl.cpp | 137 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) diff --git a/src/graphics/FGrp_BitmapImpl.cpp b/src/graphics/FGrp_BitmapImpl.cpp index 87072d6..4676fe7 100755 --- a/src/graphics/FGrp_BitmapImpl.cpp +++ b/src/graphics/FGrp_BitmapImpl.cpp @@ -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) +{ + unsigned char bmpFileHeader[] = + { + 0x42, 0x4D, + 0x00, 0x00, 0x00, 0x00, // size + 0x00, 0x00, + 0x00, 0x00, + 0x36, 0x00, 0x00, 0x00 // offset + }; + + unsigned char bmpDibHeader[] = + { + 0x28, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, // width + 0x00, 0x00, 0x00, 0x00, // height + 0x01, 0x00, + 0x18, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, // image size + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00 + }; + + int dstPitch = width * 3; + + while (dstPitch % 4) + { + ++dstPitch; + } + + int imageSize = dstPitch * height; + int offset = sizeof(bmpFileHeader) + sizeof(bmpDibHeader); + + // fill in the reserved fields + { + struct _LittleEndian + { + static inline void Write(unsigned char* pBuffer, unsigned long data) + { + pBuffer[0] = (data >> 0) & 0xFF; + pBuffer[1] = (data >> 8) & 0xFF; + pBuffer[2] = (data >> 16) & 0xFF; + pBuffer[3] = (data >> 24) & 0xFF; + } + }; + + // offset (little endian) + _LittleEndian::Write(&bmpFileHeader[2], imageSize + offset); + // offset (little endian) + _LittleEndian::Write(&bmpFileHeader[10], offset); + // width (little endian) + _LittleEndian::Write(&bmpDibHeader[4], width); + // height (little endian) + _LittleEndian::Write(&bmpDibHeader[8], height); + // image size (little endian) + _LittleEndian::Write(&bmpDibHeader[20], imageSize); + } + + Tizen::Io::File file; + + result r = file.Construct(fileName, "wb"); + + if (r != E_SUCCESS) + { + return false; + } + + file.Write(bmpFileHeader, sizeof(bmpFileHeader)); + file.Write(bmpDibHeader, sizeof(bmpDibHeader)); + + { + typedef unsigned long SourPixel; + typedef unsigned char DestPixel; + + _Util::AutoPtr lineBuffer(new DestPixel[dstPitch]); + + for (int y = 0; y < height; y++) + { + memset(lineBuffer.Get(), 0, dstPitch); + + SourPixel* pSour32 = pBuffer + pixelPerLine * (height - y - 1); + DestPixel* pDest08 = lineBuffer.Get(); + + for (int x = 0; x < width; x++) + { + *pDest08++ = (unsigned char)(*pSour32); + *pDest08++ = (unsigned char)(*pSour32 >> 8); + *pDest08++ = (unsigned char)(*pSour32 >> 16); + + ++pSour32; + } + + file.Write(lineBuffer.Get(), dstPitch); + } + + } + + return true; +} + +_OSP_EXPORT_ bool _SaveToBmp(const Canvas& canvas, const Tizen::Base::String& fileName) +{ + bool r = false; + BufferInfo bi; + + const_cast(&canvas)->Lock(bi); + + if (bi.bitsPerPixel == 32) + { + r = _SaveToBmp((unsigned long*)bi.pPixels, bi.width, bi.height, bi.pitch * 8 / bi.bitsPerPixel, fileName); + } + + const_cast(&canvas)->Unlock(); + + return r; +} + +_OSP_EXPORT_ bool _SaveToBmp(const Bitmap& bitmap, const Tizen::Base::String& fileName) +{ + bool r = false; + BufferInfo bi; + + const_cast(&bitmap)->Lock(bi); + + if (bi.bitsPerPixel == 32) + { + r = _SaveToBmp((unsigned long*)bi.pPixels, bi.width, bi.height, bi.pitch * 8 / bi.bitsPerPixel, fileName); + } + + const_cast(&bitmap)->Unlock(); + + return r; +} + }} // Tizen::Graphics -- 2.7.4