1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/image/encoders/jpeg/nsJPEGEncoder.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,79 @@ 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 "imgIEncoder.h" 1.10 + 1.11 +#include "mozilla/ReentrantMonitor.h" 1.12 +#include "mozilla/Attributes.h" 1.13 + 1.14 +#include "nsCOMPtr.h" 1.15 + 1.16 +// needed for JPEG library 1.17 +#include <stdio.h> 1.18 + 1.19 +extern "C" { 1.20 +#include "jpeglib.h" 1.21 +} 1.22 + 1.23 +#define NS_JPEGENCODER_CID \ 1.24 +{ /* ac2bb8fe-eeeb-4572-b40f-be03932b56e0 */ \ 1.25 + 0xac2bb8fe, \ 1.26 + 0xeeeb, \ 1.27 + 0x4572, \ 1.28 + {0xb4, 0x0f, 0xbe, 0x03, 0x93, 0x2b, 0x56, 0xe0} \ 1.29 +} 1.30 + 1.31 +// Provides JPEG encoding functionality. Use InitFromData() to do the 1.32 +// encoding. See that function definition for encoding options. 1.33 + 1.34 +class nsJPEGEncoder MOZ_FINAL : public imgIEncoder 1.35 +{ 1.36 + typedef mozilla::ReentrantMonitor ReentrantMonitor; 1.37 +public: 1.38 + NS_DECL_THREADSAFE_ISUPPORTS 1.39 + NS_DECL_IMGIENCODER 1.40 + NS_DECL_NSIINPUTSTREAM 1.41 + NS_DECL_NSIASYNCINPUTSTREAM 1.42 + 1.43 + nsJPEGEncoder(); 1.44 + 1.45 +private: 1.46 + ~nsJPEGEncoder(); 1.47 + 1.48 +protected: 1.49 + 1.50 + void ConvertHostARGBRow(const uint8_t* aSrc, uint8_t* aDest, 1.51 + uint32_t aPixelWidth); 1.52 + void ConvertRGBARow(const uint8_t* aSrc, uint8_t* aDest, uint32_t aPixelWidth); 1.53 + 1.54 + static void initDestination(jpeg_compress_struct* cinfo); 1.55 + static boolean emptyOutputBuffer(jpeg_compress_struct* cinfo); 1.56 + static void termDestination(jpeg_compress_struct* cinfo); 1.57 + 1.58 + static void errorExit(jpeg_common_struct* cinfo); 1.59 + 1.60 + void NotifyListener(); 1.61 + 1.62 + bool mFinished; 1.63 + 1.64 + // image buffer 1.65 + uint8_t* mImageBuffer; 1.66 + uint32_t mImageBufferSize; 1.67 + uint32_t mImageBufferUsed; 1.68 + 1.69 + uint32_t mImageBufferReadPoint; 1.70 + 1.71 + nsCOMPtr<nsIInputStreamCallback> mCallback; 1.72 + nsCOMPtr<nsIEventTarget> mCallbackTarget; 1.73 + uint32_t mNotifyThreshold; 1.74 + 1.75 + /* 1.76 + nsJPEGEncoder is designed to allow one thread to pump data into it while another 1.77 + reads from it. We lock to ensure that the buffer remains append-only while 1.78 + we read from it (that it is not realloced) and to ensure that only one thread 1.79 + dispatches a callback for each call to AsyncWait. 1.80 + */ 1.81 + ReentrantMonitor mReentrantMonitor; 1.82 +};