nsJPEGEncoder.h 2.5 KB

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