Bug 1931913 - [wpt-sync] Update web-platform-tests to fb6c27f65c829cea708e497db128f58...
[gecko.git] / image / decoders / nsJPEGDecoder.h
blobfa010f9677ed7faab9de23da1f4bea4a7fb14ef7
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 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/. */
7 #ifndef mozilla_image_decoders_nsJPEGDecoder_h
8 #define mozilla_image_decoders_nsJPEGDecoder_h
10 #include "RasterImage.h"
11 #include "SurfacePipe.h"
12 #include "EXIF.h"
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
19 #include "Decoder.h"
21 extern "C" {
22 #include "jpeglib.h"
25 #include <setjmp.h>
27 namespace mozilla::image {
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;
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;
45 class RasterImage;
46 struct Orientation;
48 class nsJPEGDecoder : public Decoder {
49 public:
50 virtual ~nsJPEGDecoder();
52 DecoderType GetType() const override { return DecoderType::JPEG; }
54 void NotifyDone();
56 protected:
57 nsresult InitInternal() override;
58 LexerResult DoDecode(SourceBufferIterator& aIterator,
59 IResumable* aOnResume) override;
60 nsresult FinishInternal() override;
62 Maybe<Telemetry::HistogramID> SpeedHistogram() const override;
64 protected:
65 EXIFData ReadExifData() const;
66 WriteState OutputScanlines();
68 private:
69 friend class DecoderFactory;
71 // Decoders should only be instantiated via DecoderFactory.
72 nsJPEGDecoder(RasterImage* aImage, Decoder::DecodeStyle aDecodeStyle);
74 enum class State { JPEG_DATA, FINISHED_JPEG_DATA };
76 void FinishRow(uint32_t aLastSourceRow);
77 LexerTransition<State> ReadJPEGData(const char* aData, size_t aLength);
78 LexerTransition<State> FinishedJPEGData();
80 StreamingLexer<State> mLexer;
82 public:
83 struct jpeg_decompress_struct mInfo;
84 struct jpeg_source_mgr mSourceMgr;
85 struct jpeg_progress_mgr mProgressMgr;
86 decoder_error_mgr mErr;
87 jstate mState;
89 uint32_t mBytesToSkip;
91 const JOCTET* mSegment; // The current segment we are decoding from
92 uint32_t mSegmentLen; // amount of data in mSegment
94 JOCTET* mBackBuffer;
95 uint32_t mBackBufferLen; // Offset of end of active backtrack data
96 uint32_t mBackBufferSize; // size in bytes what mBackBuffer was created with
97 uint32_t mBackBufferUnreadLen; // amount of data currently in mBackBuffer
99 JOCTET* mProfile;
100 uint32_t mProfileLength;
102 uint32_t* mCMSLine;
104 bool mReading;
106 const Decoder::DecodeStyle mDecodeStyle;
108 SurfacePipe mPipe;
111 } // namespace mozilla::image
113 #endif // mozilla_image_decoders_nsJPEGDecoder_h