1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is an implementation of a bitmap encoder.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Brian R. Bondy <netzen@gmail.com>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "mozilla/Attributes.h"
39 : #include "mozilla/ReentrantMonitor.h"
40 :
41 : #include "imgIEncoder.h"
42 : #include "BMPFileHeaders.h"
43 :
44 : #include "nsCOMPtr.h"
45 :
46 : #define NS_BMPENCODER_CID \
47 : { /* 13a5320c-4c91-4FA4-bd16-b081a3ba8c0b */ \
48 : 0x13a5320c, \
49 : 0x4c91, \
50 : 0x4fa4, \
51 : {0xbd, 0x16, 0xb0, 0x81, 0xa3, 0Xba, 0x8c, 0x0b} \
52 : }
53 :
54 : // Provides BMP encoding functionality. Use InitFromData() to do the
55 : // encoding. See that function definition for encoding options.
56 :
57 : class nsBMPEncoder MOZ_FINAL : public imgIEncoder
58 : {
59 : typedef mozilla::ReentrantMonitor ReentrantMonitor;
60 : public:
61 : NS_DECL_ISUPPORTS
62 : NS_DECL_IMGIENCODER
63 : NS_DECL_NSIINPUTSTREAM
64 : NS_DECL_NSIASYNCINPUTSTREAM
65 :
66 : nsBMPEncoder();
67 : ~nsBMPEncoder();
68 :
69 : protected:
70 : // See InitData in the cpp for valid parse options
71 : nsresult ParseOptions(const nsAString& aOptions, PRUint32* bpp);
72 : // Obtains data with no alpha in machine-independent byte order
73 : void ConvertHostARGBRow(const PRUint8* aSrc, PRUint8* aDest,
74 : PRUint32 aPixelWidth);
75 : // Strips the alpha from aSrc and puts it in aDest
76 : void StripAlpha(const PRUint8* aSrc, PRUint8* aDest,
77 : PRUint32 aPixelWidth);
78 : // Thread safe notify listener
79 : void NotifyListener();
80 :
81 : // Initializes the bitmap file header member mBMPFileHeader
82 : void InitFileHeader(PRUint32 aBPP, PRUint32 aWidth, PRUint32 aHeight);
83 : // Initializes the bitmap info header member mBMPInfoHeader
84 : void InitInfoHeader(PRUint32 aBPP, PRUint32 aWidth, PRUint32 aHeight);
85 : // Encodes the bitmap file header member mBMPFileHeader
86 : void EncodeFileHeader();
87 : // Encodes the bitmap info header member mBMPInfoHeader
88 : void EncodeInfoHeader();
89 : // Encodes a row of image data which does not have alpha data
90 : void EncodeImageDataRow24(const PRUint8* aData);
91 : // Encodes a row of image data which does have alpha data
92 : void EncodeImageDataRow32(const PRUint8* aData);
93 : // Obtains the current offset filled up to for the image buffer
94 0 : inline PRInt32 GetCurrentImageBufferOffset()
95 : {
96 0 : return static_cast<PRInt32>(mImageBufferCurr - mImageBufferStart);
97 : }
98 :
99 : // These headers will always contain endian independent stuff
100 : // They store the BMP headers which will be encoded
101 : mozilla::image::BMPFILEHEADER mBMPFileHeader;
102 : mozilla::image::BMPINFOHEADER mBMPInfoHeader;
103 :
104 : // Keeps track of the start of the image buffer
105 : PRUint8* mImageBufferStart;
106 : // Keeps track of the current position in the image buffer
107 : PRUint8* mImageBufferCurr;
108 : // Keeps track of the image buffer size
109 : PRUint32 mImageBufferSize;
110 : // Keeps track of the number of bytes in the image buffer which are read
111 : PRUint32 mImageBufferReadPoint;
112 : // Stores true if the image is done being encoded
113 : bool mFinished;
114 :
115 : nsCOMPtr<nsIInputStreamCallback> mCallback;
116 : nsCOMPtr<nsIEventTarget> mCallbackTarget;
117 : PRUint32 mNotifyThreshold;
118 : };
|