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 an icon 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 :
43 : #include "nsAutoPtr.h"
44 : #include "nsCOMPtr.h"
45 : #include "ICOFileHeaders.h"
46 :
47 : class nsBMPEncoder;
48 : class nsPNGEncoder;
49 :
50 : #define NS_ICOENCODER_CID \
51 : { /*92AE3AB2-8968-41B1-8709-B6123BCEAF21 */ \
52 : 0x92ae3ab2, \
53 : 0x8968, \
54 : 0x41b1, \
55 : {0x87, 0x09, 0xb6, 0x12, 0x3b, 0Xce, 0xaf, 0x21} \
56 : }
57 :
58 : // Provides ICO encoding functionality. Use InitFromData() to do the
59 : // encoding. See that function definition for encoding options.
60 :
61 : class nsICOEncoder MOZ_FINAL : public imgIEncoder
62 : {
63 : typedef mozilla::ReentrantMonitor ReentrantMonitor;
64 : public:
65 : NS_DECL_ISUPPORTS
66 : NS_DECL_IMGIENCODER
67 : NS_DECL_NSIINPUTSTREAM
68 : NS_DECL_NSIASYNCINPUTSTREAM
69 :
70 : nsICOEncoder();
71 : ~nsICOEncoder();
72 :
73 : // Obtains the width of the icon directory entry
74 8 : PRUint32 GetRealWidth() const
75 : {
76 8 : return mICODirEntry.mWidth == 0 ? 256 : mICODirEntry.mWidth;
77 : }
78 :
79 : // Obtains the height of the icon directory entry
80 12 : PRUint32 GetRealHeight() const
81 : {
82 12 : return mICODirEntry.mHeight == 0 ? 256 : mICODirEntry.mHeight;
83 : }
84 :
85 : protected:
86 : nsresult ParseOptions(const nsAString& aOptions, PRUint32* bpp,
87 : bool *usePNG);
88 : void NotifyListener();
89 :
90 : // Initializes the icon file header mICOFileHeader
91 : void InitFileHeader();
92 : // Initializes the icon directory info header mICODirEntry
93 : void InitInfoHeader(PRUint32 aBPP, PRUint8 aWidth, PRUint8 aHeight);
94 : // Encodes the icon file header mICOFileHeader
95 : void EncodeFileHeader();
96 : // Encodes the icon directory info header mICODirEntry
97 : void EncodeInfoHeader();
98 : // Obtains the current offset filled up to for the image buffer
99 8 : inline PRInt32 GetCurrentImageBufferOffset()
100 : {
101 8 : return static_cast<PRInt32>(mImageBufferCurr - mImageBufferStart);
102 : }
103 :
104 : // Holds either a PNG or a BMP depending on the encoding options specified
105 : // or if no encoding options specified will use the default (PNG)
106 : nsCOMPtr<imgIEncoder> mContainedEncoder;
107 :
108 : // These headers will always contain endian independent stuff.
109 : // Don't trust the width and height of mICODirEntry directly,
110 : // instead use the accessors GetRealWidth() and GetRealHeight().
111 : mozilla::image::IconFileHeader mICOFileHeader;
112 : mozilla::image::IconDirEntry mICODirEntry;
113 :
114 : // Keeps track of the start of the image buffer
115 : PRUint8* mImageBufferStart;
116 : // Keeps track of the current position in the image buffer
117 : PRUint8* mImageBufferCurr;
118 : // Keeps track of the image buffer size
119 : PRUint32 mImageBufferSize;
120 : // Keeps track of the number of bytes in the image buffer which are read
121 : PRUint32 mImageBufferReadPoint;
122 : // Stores true if the image is done being encoded
123 : bool mFinished;
124 : // Stores true if the contained image is a PNG
125 : bool mUsePNG;
126 :
127 : nsCOMPtr<nsIInputStreamCallback> mCallback;
128 : nsCOMPtr<nsIEventTarget> mCallbackTarget;
129 : PRUint32 mNotifyThreshold;
130 : };
|