michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/Attributes.h" michael@0: #include "mozilla/ReentrantMonitor.h" michael@0: michael@0: #include "imgIEncoder.h" michael@0: #include "BMPFileHeaders.h" michael@0: michael@0: #include "nsCOMPtr.h" michael@0: michael@0: #define NS_BMPENCODER_CID \ michael@0: { /* 13a5320c-4c91-4FA4-bd16-b081a3ba8c0b */ \ michael@0: 0x13a5320c, \ michael@0: 0x4c91, \ michael@0: 0x4fa4, \ michael@0: {0xbd, 0x16, 0xb0, 0x81, 0xa3, 0Xba, 0x8c, 0x0b} \ michael@0: } michael@0: michael@0: // Provides BMP encoding functionality. Use InitFromData() to do the michael@0: // encoding. See that function definition for encoding options. michael@0: michael@0: class nsBMPEncoder MOZ_FINAL : public imgIEncoder michael@0: { michael@0: typedef mozilla::ReentrantMonitor ReentrantMonitor; michael@0: public: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: NS_DECL_IMGIENCODER michael@0: NS_DECL_NSIINPUTSTREAM michael@0: NS_DECL_NSIASYNCINPUTSTREAM michael@0: michael@0: nsBMPEncoder(); michael@0: ~nsBMPEncoder(); michael@0: michael@0: protected: michael@0: enum Version { michael@0: VERSION_3 = 3, michael@0: VERSION_5 = 5 michael@0: }; michael@0: michael@0: // See InitData in the cpp for valid parse options michael@0: nsresult ParseOptions(const nsAString& aOptions, Version* version, michael@0: uint32_t* bpp); michael@0: // Obtains data with no alpha in machine-independent byte order michael@0: void ConvertHostARGBRow(const uint8_t* aSrc, uint8_t* aDest, michael@0: uint32_t aPixelWidth); michael@0: // Thread safe notify listener michael@0: void NotifyListener(); michael@0: michael@0: // Initializes the bitmap file header member mBMPFileHeader michael@0: void InitFileHeader(Version aVersion, uint32_t aBPP, uint32_t aWidth, michael@0: uint32_t aHeight); michael@0: // Initializes the bitmap info header member mBMPInfoHeader michael@0: void InitInfoHeader(Version aVersion, uint32_t aBPP, uint32_t aWidth, michael@0: uint32_t aHeight); michael@0: michael@0: // Encodes the bitmap file header member mBMPFileHeader michael@0: void EncodeFileHeader(); michael@0: // Encodes the bitmap info header member mBMPInfoHeader michael@0: void EncodeInfoHeader(); michael@0: // Encodes a row of image data which does not have alpha data michael@0: void EncodeImageDataRow24(const uint8_t* aData); michael@0: // Encodes a row of image data which does have alpha data michael@0: void EncodeImageDataRow32(const uint8_t* aData); michael@0: // Obtains the current offset filled up to for the image buffer michael@0: inline int32_t GetCurrentImageBufferOffset() michael@0: { michael@0: return static_cast(mImageBufferCurr - mImageBufferStart); michael@0: } michael@0: michael@0: // These headers will always contain endian independent stuff michael@0: // They store the BMP headers which will be encoded michael@0: mozilla::image::BMPFILEHEADER mBMPFileHeader; michael@0: mozilla::image::BITMAPV5HEADER mBMPInfoHeader; michael@0: michael@0: // Keeps track of the start of the image buffer michael@0: uint8_t* mImageBufferStart; michael@0: // Keeps track of the current position in the image buffer michael@0: uint8_t* mImageBufferCurr; michael@0: // Keeps track of the image buffer size michael@0: uint32_t mImageBufferSize; michael@0: // Keeps track of the number of bytes in the image buffer which are read michael@0: uint32_t mImageBufferReadPoint; michael@0: // Stores true if the image is done being encoded michael@0: bool mFinished; michael@0: michael@0: nsCOMPtr mCallback; michael@0: nsCOMPtr mCallbackTarget; michael@0: uint32_t mNotifyThreshold; michael@0: };