nsJPEGDecoder.h (3164B)
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 7 #ifndef mozilla_image_decoders_nsJPEGDecoder_h 8 #define mozilla_image_decoders_nsJPEGDecoder_h 9 10 #include "RasterImage.h" 11 #include "SurfacePipe.h" 12 #include "EXIF.h" 13 14 // On Windows systems, RasterImage.h brings in 'windows.h', which defines INT32. 15 // But the jpeg decoder has its own definition of INT32. To avoid build issues, 16 // we need to undefine the version from 'windows.h'. 17 #undef INT32 18 19 #include "Decoder.h" 20 21 extern "C" { 22 #include "jpeglib.h" 23 } 24 25 #include <setjmp.h> 26 27 namespace mozilla::image { 28 29 typedef struct { 30 struct jpeg_error_mgr pub; // "public" fields for IJG library 31 jmp_buf setjmp_buffer; // For handling catastropic errors 32 } decoder_error_mgr; 33 34 typedef enum { 35 JPEG_HEADER, // Reading JFIF headers 36 JPEG_START_DECOMPRESS, 37 JPEG_DECOMPRESS_PROGRESSIVE, // Output progressive pixels 38 JPEG_DECOMPRESS_SEQUENTIAL, // Output sequential pixels 39 JPEG_DONE, 40 JPEG_SINK_NON_JPEG_TRAILER, // Some image files have a 41 // non-JPEG trailer 42 JPEG_ERROR 43 } jstate; 44 45 class RasterImage; 46 struct Orientation; 47 48 class nsJPEGDecoder : public Decoder { 49 public: 50 virtual ~nsJPEGDecoder(); 51 52 DecoderType GetType() const override { return DecoderType::JPEG; } 53 54 void NotifyDone(); 55 56 protected: 57 nsresult InitInternal() override; 58 LexerResult DoDecode(SourceBufferIterator& aIterator, 59 IResumable* aOnResume) override; 60 nsresult FinishInternal() override; 61 62 Maybe<glean::impl::MemoryDistributionMetric> SpeedMetric() const override; 63 64 protected: 65 EXIFData ReadExifData() const; 66 WriteState OutputScanlines(); 67 68 private: 69 friend class DecoderFactory; 70 71 // Decoders should only be instantiated via DecoderFactory. 72 nsJPEGDecoder(RasterImage* aImage, Decoder::DecodeStyle aDecodeStyle, 73 bool aIsPDF = false); 74 75 enum class State { JPEG_DATA, FINISHED_JPEG_DATA }; 76 77 void FinishRow(uint32_t aLastSourceRow); 78 LexerTransition<State> ReadJPEGData(const char* aData, size_t aLength); 79 LexerTransition<State> FinishedJPEGData(); 80 81 StreamingLexer<State> mLexer; 82 83 public: 84 struct jpeg_decompress_struct mInfo; 85 struct jpeg_source_mgr mSourceMgr; 86 struct jpeg_progress_mgr mProgressMgr; 87 decoder_error_mgr mErr; 88 jstate mState; 89 90 uint32_t mBytesToSkip; 91 92 const JOCTET* mSegment; // The current segment we are decoding from 93 uint32_t mSegmentLen; // amount of data in mSegment 94 95 JOCTET* mBackBuffer; 96 uint32_t mBackBufferLen; // Offset of end of active backtrack data 97 uint32_t mBackBufferSize; // size in bytes what mBackBuffer was created with 98 uint32_t mBackBufferUnreadLen; // amount of data currently in mBackBuffer 99 100 JOCTET* mProfile; 101 uint32_t mProfileLength; 102 103 uint32_t* mCMSLine; 104 105 bool mReading; 106 107 const Decoder::DecodeStyle mDecodeStyle; 108 bool mIsPDF; 109 110 SurfacePipe mPipe; 111 }; 112 113 } // namespace mozilla::image 114 115 #endif // mozilla_image_decoders_nsJPEGDecoder_h