|
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 "imgIEncoder.h" |
|
7 |
|
8 #include "mozilla/ReentrantMonitor.h" |
|
9 #include "mozilla/Attributes.h" |
|
10 |
|
11 #include "nsCOMPtr.h" |
|
12 |
|
13 // needed for JPEG library |
|
14 #include <stdio.h> |
|
15 |
|
16 extern "C" { |
|
17 #include "jpeglib.h" |
|
18 } |
|
19 |
|
20 #define NS_JPEGENCODER_CID \ |
|
21 { /* ac2bb8fe-eeeb-4572-b40f-be03932b56e0 */ \ |
|
22 0xac2bb8fe, \ |
|
23 0xeeeb, \ |
|
24 0x4572, \ |
|
25 {0xb4, 0x0f, 0xbe, 0x03, 0x93, 0x2b, 0x56, 0xe0} \ |
|
26 } |
|
27 |
|
28 // Provides JPEG encoding functionality. Use InitFromData() to do the |
|
29 // encoding. See that function definition for encoding options. |
|
30 |
|
31 class nsJPEGEncoder MOZ_FINAL : public imgIEncoder |
|
32 { |
|
33 typedef mozilla::ReentrantMonitor ReentrantMonitor; |
|
34 public: |
|
35 NS_DECL_THREADSAFE_ISUPPORTS |
|
36 NS_DECL_IMGIENCODER |
|
37 NS_DECL_NSIINPUTSTREAM |
|
38 NS_DECL_NSIASYNCINPUTSTREAM |
|
39 |
|
40 nsJPEGEncoder(); |
|
41 |
|
42 private: |
|
43 ~nsJPEGEncoder(); |
|
44 |
|
45 protected: |
|
46 |
|
47 void ConvertHostARGBRow(const uint8_t* aSrc, uint8_t* aDest, |
|
48 uint32_t aPixelWidth); |
|
49 void ConvertRGBARow(const uint8_t* aSrc, uint8_t* aDest, uint32_t aPixelWidth); |
|
50 |
|
51 static void initDestination(jpeg_compress_struct* cinfo); |
|
52 static boolean emptyOutputBuffer(jpeg_compress_struct* cinfo); |
|
53 static void termDestination(jpeg_compress_struct* cinfo); |
|
54 |
|
55 static void errorExit(jpeg_common_struct* cinfo); |
|
56 |
|
57 void NotifyListener(); |
|
58 |
|
59 bool mFinished; |
|
60 |
|
61 // image buffer |
|
62 uint8_t* mImageBuffer; |
|
63 uint32_t mImageBufferSize; |
|
64 uint32_t mImageBufferUsed; |
|
65 |
|
66 uint32_t mImageBufferReadPoint; |
|
67 |
|
68 nsCOMPtr<nsIInputStreamCallback> mCallback; |
|
69 nsCOMPtr<nsIEventTarget> mCallbackTarget; |
|
70 uint32_t mNotifyThreshold; |
|
71 |
|
72 /* |
|
73 nsJPEGEncoder is designed to allow one thread to pump data into it while another |
|
74 reads from it. We lock to ensure that the buffer remains append-only while |
|
75 we read from it (that it is not realloced) and to ensure that only one thread |
|
76 dispatches a callback for each call to AsyncWait. |
|
77 */ |
|
78 ReentrantMonitor mReentrantMonitor; |
|
79 }; |