nsPNGEncoder.h (3019B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #ifndef mozilla_image_encoders_png_nsPNGEncoder_h 7 #define mozilla_image_encoders_png_nsPNGEncoder_h 8 9 #include <png.h> 10 11 #include "imgIEncoder.h" 12 #include "nsCOMPtr.h" 13 14 #include "mozilla/Attributes.h" 15 #include "mozilla/ReentrantMonitor.h" 16 17 #define NS_PNGENCODER_CID \ 18 {/* 38d1592e-b81e-432b-86f8-471878bbfe07 */ \ 19 0x38d1592e, \ 20 0xb81e, \ 21 0x432b, \ 22 {0x86, 0xf8, 0x47, 0x18, 0x78, 0xbb, 0xfe, 0x07}} 23 24 // Provides PNG encoding functionality. Use InitFromData() to do the 25 // encoding. See that function definition for encoding options. 26 27 class nsPNGEncoder final : public imgIEncoder { 28 typedef mozilla::ReentrantMonitor ReentrantMonitor; 29 30 public: 31 NS_DECL_THREADSAFE_ISUPPORTS 32 NS_DECL_IMGIENCODER 33 NS_DECL_NSIINPUTSTREAM 34 NS_DECL_NSIASYNCINPUTSTREAM 35 36 nsPNGEncoder(); 37 38 protected: 39 ~nsPNGEncoder(); 40 nsresult ParseOptions(const nsAString& aOptions, bool* useTransparency, 41 bool* skipFirstFrame, uint32_t* numAnimatedFrames, 42 uint32_t* numIterations, int* zlibLevel, int* filters, 43 uint32_t* frameDispose, uint32_t* frameBlend, 44 uint32_t* frameDelay, uint32_t* offsetX, 45 uint32_t* offsetY); 46 void ConvertHostARGBRow(const uint8_t* aSrc, uint8_t* aDest, 47 uint32_t aPixelWidth, bool aUseTransparency); 48 void StripAlpha(const uint8_t* aSrc, uint8_t* aDest, uint32_t aPixelWidth); 49 static void WarningCallback(png_structp png_ptr, png_const_charp warning_msg); 50 static void ErrorCallback(png_structp png_ptr, png_const_charp error_msg); 51 static void WriteCallback(png_structp png, png_bytep data, png_size_t size); 52 void NullOutImageBuffer(); 53 void NotifyListener(); 54 nsresult MaybeAddCustomMetadata(const nsACString& aRandomizationKey); 55 56 png_struct* mPNG; 57 png_info* mPNGinfo; 58 59 bool mAddCustomMetadata; 60 bool mIsAnimation; 61 bool mFinished; 62 63 // image buffer 64 uint8_t* mImageBuffer; 65 uint32_t mImageBufferSize; 66 uint32_t mImageBufferUsed; 67 uint32_t mImageBufferHash; 68 69 uint32_t mImageBufferReadPoint; 70 71 nsCOMPtr<nsIInputStreamCallback> mCallback; 72 nsCOMPtr<nsIEventTarget> mCallbackTarget; 73 uint32_t mNotifyThreshold; 74 75 // nsPNGEncoder is designed to allow one thread to pump data into it while 76 // another reads from it. We lock to ensure that the buffer remains 77 // append-only while we read from it (that it is not realloced) and to 78 // ensure that only one thread dispatches a callback for each call to 79 // AsyncWait. 80 ReentrantMonitor mReentrantMonitor MOZ_UNANNOTATED; 81 }; 82 #endif // mozilla_image_encoders_png_nsPNGEncoder_h