1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/image/encoders/png/nsPNGEncoder.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,82 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "mozilla/Attributes.h" 1.10 +#include "mozilla/ReentrantMonitor.h" 1.11 + 1.12 +#include "imgIEncoder.h" 1.13 + 1.14 +#include "nsCOMPtr.h" 1.15 + 1.16 +#include <png.h> 1.17 + 1.18 +#define NS_PNGENCODER_CID \ 1.19 +{ /* 38d1592e-b81e-432b-86f8-471878bbfe07 */ \ 1.20 + 0x38d1592e, \ 1.21 + 0xb81e, \ 1.22 + 0x432b, \ 1.23 + {0x86, 0xf8, 0x47, 0x18, 0x78, 0xbb, 0xfe, 0x07} \ 1.24 +} 1.25 + 1.26 +// Provides PNG encoding functionality. Use InitFromData() to do the 1.27 +// encoding. See that function definition for encoding options. 1.28 + 1.29 +class nsPNGEncoder MOZ_FINAL : public imgIEncoder 1.30 +{ 1.31 + typedef mozilla::ReentrantMonitor ReentrantMonitor; 1.32 +public: 1.33 + NS_DECL_THREADSAFE_ISUPPORTS 1.34 + NS_DECL_IMGIENCODER 1.35 + NS_DECL_NSIINPUTSTREAM 1.36 + NS_DECL_NSIASYNCINPUTSTREAM 1.37 + 1.38 + nsPNGEncoder(); 1.39 + ~nsPNGEncoder(); 1.40 + 1.41 +protected: 1.42 + nsresult ParseOptions(const nsAString& aOptions, 1.43 + bool* useTransparency, 1.44 + bool* skipFirstFrame, 1.45 + uint32_t* numAnimatedFrames, 1.46 + uint32_t* numIterations, 1.47 + uint32_t* frameDispose, 1.48 + uint32_t* frameBlend, 1.49 + uint32_t* frameDelay, 1.50 + uint32_t* offsetX, 1.51 + uint32_t* offsetY); 1.52 + void ConvertHostARGBRow(const uint8_t* aSrc, uint8_t* aDest, 1.53 + uint32_t aPixelWidth, bool aUseTransparency); 1.54 + void StripAlpha(const uint8_t* aSrc, uint8_t* aDest, 1.55 + uint32_t aPixelWidth); 1.56 + static void WarningCallback(png_structp png_ptr, png_const_charp warning_msg); 1.57 + static void ErrorCallback(png_structp png_ptr, png_const_charp error_msg); 1.58 + static void WriteCallback(png_structp png, png_bytep data, png_size_t size); 1.59 + void NotifyListener(); 1.60 + 1.61 + png_struct* mPNG; 1.62 + png_info* mPNGinfo; 1.63 + 1.64 + bool mIsAnimation; 1.65 + bool mFinished; 1.66 + 1.67 + // image buffer 1.68 + uint8_t* mImageBuffer; 1.69 + uint32_t mImageBufferSize; 1.70 + uint32_t mImageBufferUsed; 1.71 + 1.72 + uint32_t mImageBufferReadPoint; 1.73 + 1.74 + nsCOMPtr<nsIInputStreamCallback> mCallback; 1.75 + nsCOMPtr<nsIEventTarget> mCallbackTarget; 1.76 + uint32_t mNotifyThreshold; 1.77 + 1.78 + /* 1.79 + nsPNGEncoder is designed to allow one thread to pump data into it while another 1.80 + reads from it. We lock to ensure that the buffer remains append-only while 1.81 + we read from it (that it is not realloced) and to ensure that only one thread 1.82 + dispatches a callback for each call to AsyncWait. 1.83 + */ 1.84 + ReentrantMonitor mReentrantMonitor; 1.85 +};