michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "imgIEncoder.h" michael@0: michael@0: #include "mozilla/ReentrantMonitor.h" michael@0: #include "mozilla/Attributes.h" michael@0: michael@0: #include "nsCOMPtr.h" michael@0: michael@0: // needed for JPEG library michael@0: #include michael@0: michael@0: extern "C" { michael@0: #include "jpeglib.h" michael@0: } michael@0: michael@0: #define NS_JPEGENCODER_CID \ michael@0: { /* ac2bb8fe-eeeb-4572-b40f-be03932b56e0 */ \ michael@0: 0xac2bb8fe, \ michael@0: 0xeeeb, \ michael@0: 0x4572, \ michael@0: {0xb4, 0x0f, 0xbe, 0x03, 0x93, 0x2b, 0x56, 0xe0} \ michael@0: } michael@0: michael@0: // Provides JPEG encoding functionality. Use InitFromData() to do the michael@0: // encoding. See that function definition for encoding options. michael@0: michael@0: class nsJPEGEncoder MOZ_FINAL : public imgIEncoder michael@0: { michael@0: typedef mozilla::ReentrantMonitor ReentrantMonitor; michael@0: public: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: NS_DECL_IMGIENCODER michael@0: NS_DECL_NSIINPUTSTREAM michael@0: NS_DECL_NSIASYNCINPUTSTREAM michael@0: michael@0: nsJPEGEncoder(); michael@0: michael@0: private: michael@0: ~nsJPEGEncoder(); michael@0: michael@0: protected: michael@0: michael@0: void ConvertHostARGBRow(const uint8_t* aSrc, uint8_t* aDest, michael@0: uint32_t aPixelWidth); michael@0: void ConvertRGBARow(const uint8_t* aSrc, uint8_t* aDest, uint32_t aPixelWidth); michael@0: michael@0: static void initDestination(jpeg_compress_struct* cinfo); michael@0: static boolean emptyOutputBuffer(jpeg_compress_struct* cinfo); michael@0: static void termDestination(jpeg_compress_struct* cinfo); michael@0: michael@0: static void errorExit(jpeg_common_struct* cinfo); michael@0: michael@0: void NotifyListener(); michael@0: michael@0: bool mFinished; michael@0: michael@0: // image buffer michael@0: uint8_t* mImageBuffer; michael@0: uint32_t mImageBufferSize; michael@0: uint32_t mImageBufferUsed; michael@0: michael@0: uint32_t mImageBufferReadPoint; michael@0: michael@0: nsCOMPtr mCallback; michael@0: nsCOMPtr mCallbackTarget; michael@0: uint32_t mNotifyThreshold; michael@0: michael@0: /* michael@0: nsJPEGEncoder is designed to allow one thread to pump data into it while another michael@0: reads from it. We lock to ensure that the buffer remains append-only while michael@0: we read from it (that it is not realloced) and to ensure that only one thread michael@0: dispatches a callback for each call to AsyncWait. michael@0: */ michael@0: ReentrantMonitor mReentrantMonitor; michael@0: };