|
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 #include "mozilla/Attributes.h" |
|
7 #include "mozilla/ReentrantMonitor.h" |
|
8 |
|
9 #include "imgIEncoder.h" |
|
10 |
|
11 #include "nsCOMPtr.h" |
|
12 |
|
13 #include <png.h> |
|
14 |
|
15 #define NS_PNGENCODER_CID \ |
|
16 { /* 38d1592e-b81e-432b-86f8-471878bbfe07 */ \ |
|
17 0x38d1592e, \ |
|
18 0xb81e, \ |
|
19 0x432b, \ |
|
20 {0x86, 0xf8, 0x47, 0x18, 0x78, 0xbb, 0xfe, 0x07} \ |
|
21 } |
|
22 |
|
23 // Provides PNG encoding functionality. Use InitFromData() to do the |
|
24 // encoding. See that function definition for encoding options. |
|
25 |
|
26 class nsPNGEncoder MOZ_FINAL : public imgIEncoder |
|
27 { |
|
28 typedef mozilla::ReentrantMonitor ReentrantMonitor; |
|
29 public: |
|
30 NS_DECL_THREADSAFE_ISUPPORTS |
|
31 NS_DECL_IMGIENCODER |
|
32 NS_DECL_NSIINPUTSTREAM |
|
33 NS_DECL_NSIASYNCINPUTSTREAM |
|
34 |
|
35 nsPNGEncoder(); |
|
36 ~nsPNGEncoder(); |
|
37 |
|
38 protected: |
|
39 nsresult ParseOptions(const nsAString& aOptions, |
|
40 bool* useTransparency, |
|
41 bool* skipFirstFrame, |
|
42 uint32_t* numAnimatedFrames, |
|
43 uint32_t* numIterations, |
|
44 uint32_t* frameDispose, |
|
45 uint32_t* frameBlend, |
|
46 uint32_t* frameDelay, |
|
47 uint32_t* offsetX, |
|
48 uint32_t* offsetY); |
|
49 void ConvertHostARGBRow(const uint8_t* aSrc, uint8_t* aDest, |
|
50 uint32_t aPixelWidth, bool aUseTransparency); |
|
51 void StripAlpha(const uint8_t* aSrc, uint8_t* aDest, |
|
52 uint32_t aPixelWidth); |
|
53 static void WarningCallback(png_structp png_ptr, png_const_charp warning_msg); |
|
54 static void ErrorCallback(png_structp png_ptr, png_const_charp error_msg); |
|
55 static void WriteCallback(png_structp png, png_bytep data, png_size_t size); |
|
56 void NotifyListener(); |
|
57 |
|
58 png_struct* mPNG; |
|
59 png_info* mPNGinfo; |
|
60 |
|
61 bool mIsAnimation; |
|
62 bool mFinished; |
|
63 |
|
64 // image buffer |
|
65 uint8_t* mImageBuffer; |
|
66 uint32_t mImageBufferSize; |
|
67 uint32_t mImageBufferUsed; |
|
68 |
|
69 uint32_t mImageBufferReadPoint; |
|
70 |
|
71 nsCOMPtr<nsIInputStreamCallback> mCallback; |
|
72 nsCOMPtr<nsIEventTarget> mCallbackTarget; |
|
73 uint32_t mNotifyThreshold; |
|
74 |
|
75 /* |
|
76 nsPNGEncoder is designed to allow one thread to pump data into it while another |
|
77 reads from it. We lock to ensure that the buffer remains append-only while |
|
78 we read from it (that it is not realloced) and to ensure that only one thread |
|
79 dispatches a callback for each call to AsyncWait. |
|
80 */ |
|
81 ReentrantMonitor mReentrantMonitor; |
|
82 }; |