michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 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: michael@0: #include "nsCOMPtr.h" michael@0: michael@0: #include michael@0: michael@0: #define NS_PNGENCODER_CID \ michael@0: { /* 38d1592e-b81e-432b-86f8-471878bbfe07 */ \ michael@0: 0x38d1592e, \ michael@0: 0xb81e, \ michael@0: 0x432b, \ michael@0: {0x86, 0xf8, 0x47, 0x18, 0x78, 0xbb, 0xfe, 0x07} \ michael@0: } michael@0: michael@0: // Provides PNG encoding functionality. Use InitFromData() to do the michael@0: // encoding. See that function definition for encoding options. michael@0: michael@0: class nsPNGEncoder 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: nsPNGEncoder(); michael@0: ~nsPNGEncoder(); michael@0: michael@0: protected: michael@0: nsresult ParseOptions(const nsAString& aOptions, michael@0: bool* useTransparency, michael@0: bool* skipFirstFrame, michael@0: uint32_t* numAnimatedFrames, michael@0: uint32_t* numIterations, michael@0: uint32_t* frameDispose, michael@0: uint32_t* frameBlend, michael@0: uint32_t* frameDelay, michael@0: uint32_t* offsetX, michael@0: uint32_t* offsetY); michael@0: void ConvertHostARGBRow(const uint8_t* aSrc, uint8_t* aDest, michael@0: uint32_t aPixelWidth, bool aUseTransparency); michael@0: void StripAlpha(const uint8_t* aSrc, uint8_t* aDest, michael@0: uint32_t aPixelWidth); michael@0: static void WarningCallback(png_structp png_ptr, png_const_charp warning_msg); michael@0: static void ErrorCallback(png_structp png_ptr, png_const_charp error_msg); michael@0: static void WriteCallback(png_structp png, png_bytep data, png_size_t size); michael@0: void NotifyListener(); michael@0: michael@0: png_struct* mPNG; michael@0: png_info* mPNGinfo; michael@0: michael@0: bool mIsAnimation; michael@0: bool mFinished; michael@0: michael@0: // image buffer michael@0: uint8_t* mImageBuffer; michael@0: uint32_t mImageBufferSize; michael@0: uint32_t mImageBufferUsed; michael@0: michael@0: uint32_t mImageBufferReadPoint; michael@0: michael@0: nsCOMPtr mCallback; michael@0: nsCOMPtr mCallbackTarget; michael@0: uint32_t mNotifyThreshold; michael@0: michael@0: /* michael@0: nsPNGEncoder is designed to allow one thread to pump data into it while another michael@0: reads from it. We lock to ensure that the buffer remains append-only while michael@0: we read from it (that it is not realloced) and to ensure that only one thread michael@0: dispatches a callback for each call to AsyncWait. michael@0: */ michael@0: ReentrantMonitor mReentrantMonitor; michael@0: };