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