|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "mozilla/Attributes.h" |
|
6 #include "mozilla/ReentrantMonitor.h" |
|
7 |
|
8 #include "imgIEncoder.h" |
|
9 |
|
10 #include "nsAutoPtr.h" |
|
11 #include "nsCOMPtr.h" |
|
12 #include "ICOFileHeaders.h" |
|
13 |
|
14 class nsBMPEncoder; |
|
15 class nsPNGEncoder; |
|
16 |
|
17 #define NS_ICOENCODER_CID \ |
|
18 { /*92AE3AB2-8968-41B1-8709-B6123BCEAF21 */ \ |
|
19 0x92ae3ab2, \ |
|
20 0x8968, \ |
|
21 0x41b1, \ |
|
22 {0x87, 0x09, 0xb6, 0x12, 0x3b, 0Xce, 0xaf, 0x21} \ |
|
23 } |
|
24 |
|
25 // Provides ICO encoding functionality. Use InitFromData() to do the |
|
26 // encoding. See that function definition for encoding options. |
|
27 |
|
28 class nsICOEncoder MOZ_FINAL : public imgIEncoder |
|
29 { |
|
30 typedef mozilla::ReentrantMonitor ReentrantMonitor; |
|
31 public: |
|
32 NS_DECL_THREADSAFE_ISUPPORTS |
|
33 NS_DECL_IMGIENCODER |
|
34 NS_DECL_NSIINPUTSTREAM |
|
35 NS_DECL_NSIASYNCINPUTSTREAM |
|
36 |
|
37 nsICOEncoder(); |
|
38 ~nsICOEncoder(); |
|
39 |
|
40 // Obtains the width of the icon directory entry |
|
41 uint32_t GetRealWidth() const |
|
42 { |
|
43 return mICODirEntry.mWidth == 0 ? 256 : mICODirEntry.mWidth; |
|
44 } |
|
45 |
|
46 // Obtains the height of the icon directory entry |
|
47 uint32_t GetRealHeight() const |
|
48 { |
|
49 return mICODirEntry.mHeight == 0 ? 256 : mICODirEntry.mHeight; |
|
50 } |
|
51 |
|
52 protected: |
|
53 nsresult ParseOptions(const nsAString& aOptions, uint32_t* bpp, |
|
54 bool *usePNG); |
|
55 void NotifyListener(); |
|
56 |
|
57 // Initializes the icon file header mICOFileHeader |
|
58 void InitFileHeader(); |
|
59 // Initializes the icon directory info header mICODirEntry |
|
60 void InitInfoHeader(uint32_t aBPP, uint8_t aWidth, uint8_t aHeight); |
|
61 // Encodes the icon file header mICOFileHeader |
|
62 void EncodeFileHeader(); |
|
63 // Encodes the icon directory info header mICODirEntry |
|
64 void EncodeInfoHeader(); |
|
65 // Obtains the current offset filled up to for the image buffer |
|
66 inline int32_t GetCurrentImageBufferOffset() |
|
67 { |
|
68 return static_cast<int32_t>(mImageBufferCurr - mImageBufferStart); |
|
69 } |
|
70 |
|
71 // Holds either a PNG or a BMP depending on the encoding options specified |
|
72 // or if no encoding options specified will use the default (PNG) |
|
73 nsCOMPtr<imgIEncoder> mContainedEncoder; |
|
74 |
|
75 // These headers will always contain endian independent stuff. |
|
76 // Don't trust the width and height of mICODirEntry directly, |
|
77 // instead use the accessors GetRealWidth() and GetRealHeight(). |
|
78 mozilla::image::IconFileHeader mICOFileHeader; |
|
79 mozilla::image::IconDirEntry mICODirEntry; |
|
80 |
|
81 // Keeps track of the start of the image buffer |
|
82 uint8_t* mImageBufferStart; |
|
83 // Keeps track of the current position in the image buffer |
|
84 uint8_t* mImageBufferCurr; |
|
85 // Keeps track of the image buffer size |
|
86 uint32_t mImageBufferSize; |
|
87 // Keeps track of the number of bytes in the image buffer which are read |
|
88 uint32_t mImageBufferReadPoint; |
|
89 // Stores true if the image is done being encoded |
|
90 bool mFinished; |
|
91 // Stores true if the contained image is a PNG |
|
92 bool mUsePNG; |
|
93 |
|
94 nsCOMPtr<nsIInputStreamCallback> mCallback; |
|
95 nsCOMPtr<nsIEventTarget> mCallbackTarget; |
|
96 uint32_t mNotifyThreshold; |
|
97 }; |