nsJPEGDecoder.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. *
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #ifndef mozilla_image_decoders_nsJPEGDecoder_h
  7. #define mozilla_image_decoders_nsJPEGDecoder_h
  8. #include "RasterImage.h"
  9. // On Windows systems, RasterImage.h brings in 'windows.h', which defines INT32.
  10. // But the jpeg decoder has its own definition of INT32. To avoid build issues,
  11. // we need to undefine the version from 'windows.h'.
  12. #undef INT32
  13. #include "Decoder.h"
  14. #include "nsIInputStream.h"
  15. #include "nsIPipe.h"
  16. #include "qcms.h"
  17. extern "C" {
  18. #include "jpeglib.h"
  19. }
  20. #include <setjmp.h>
  21. namespace mozilla {
  22. namespace image {
  23. typedef struct {
  24. struct jpeg_error_mgr pub; // "public" fields for IJG library
  25. jmp_buf setjmp_buffer; // For handling catastropic errors
  26. } decoder_error_mgr;
  27. typedef enum {
  28. JPEG_HEADER, // Reading JFIF headers
  29. JPEG_START_DECOMPRESS,
  30. JPEG_DECOMPRESS_PROGRESSIVE, // Output progressive pixels
  31. JPEG_DECOMPRESS_SEQUENTIAL, // Output sequential pixels
  32. JPEG_DONE,
  33. JPEG_SINK_NON_JPEG_TRAILER, // Some image files have a
  34. // non-JPEG trailer
  35. JPEG_ERROR
  36. } jstate;
  37. class RasterImage;
  38. struct Orientation;
  39. class nsJPEGDecoder : public Decoder
  40. {
  41. public:
  42. virtual ~nsJPEGDecoder();
  43. virtual void SetSampleSize(int aSampleSize) override
  44. {
  45. mSampleSize = aSampleSize;
  46. }
  47. void NotifyDone();
  48. protected:
  49. nsresult InitInternal() override;
  50. LexerResult DoDecode(SourceBufferIterator& aIterator,
  51. IResumable* aOnResume) override;
  52. nsresult FinishInternal() override;
  53. Maybe<Telemetry::ID> SpeedHistogram() const override;
  54. protected:
  55. Orientation ReadOrientationFromEXIF();
  56. void OutputScanlines(bool* suspend);
  57. private:
  58. friend class DecoderFactory;
  59. // Decoders should only be instantiated via DecoderFactory.
  60. nsJPEGDecoder(RasterImage* aImage, Decoder::DecodeStyle aDecodeStyle);
  61. enum class State
  62. {
  63. JPEG_DATA,
  64. FINISHED_JPEG_DATA
  65. };
  66. LexerTransition<State> ReadJPEGData(const char* aData, size_t aLength);
  67. LexerTransition<State> FinishedJPEGData();
  68. StreamingLexer<State> mLexer;
  69. public:
  70. struct jpeg_decompress_struct mInfo;
  71. struct jpeg_source_mgr mSourceMgr;
  72. decoder_error_mgr mErr;
  73. jstate mState;
  74. uint32_t mBytesToSkip;
  75. const JOCTET* mSegment; // The current segment we are decoding from
  76. uint32_t mSegmentLen; // amount of data in mSegment
  77. JOCTET* mBackBuffer;
  78. uint32_t mBackBufferLen; // Offset of end of active backtrack data
  79. uint32_t mBackBufferSize; // size in bytes what mBackBuffer was created with
  80. uint32_t mBackBufferUnreadLen; // amount of data currently in mBackBuffer
  81. JOCTET * mProfile;
  82. uint32_t mProfileLength;
  83. qcms_profile* mInProfile;
  84. qcms_transform* mTransform;
  85. bool mReading;
  86. const Decoder::DecodeStyle mDecodeStyle;
  87. uint32_t mCMSMode;
  88. int mSampleSize;
  89. };
  90. } // namespace image
  91. } // namespace mozilla
  92. #endif // mozilla_image_decoders_nsJPEGDecoder_h