Bug 1809679 [wpt PR 37879] - Update wpt metadata, a=testonly
[gecko.git] / image / decoders / nsJPEGDecoder.h
blob9e21afc6a272921dd9bd9b93b9c8ba9a12b1a229
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 decoder_error_mgr mErr;
86 jstate mState;
88 uint32_t mBytesToSkip;
90 const JOCTET* mSegment; // The current segment we are decoding from
91 uint32_t mSegmentLen; // amount of data in mSegment
93 JOCTET* mBackBuffer;
94 uint32_t mBackBufferLen; // Offset of end of active backtrack data
95 uint32_t mBackBufferSize; // size in bytes what mBackBuffer was created with
96 uint32_t mBackBufferUnreadLen; // amount of data currently in mBackBuffer
98 JOCTET* mProfile;
99 uint32_t mProfileLength;
101 uint32_t* mCMSLine;
103 bool mReading;
105 const Decoder::DecodeStyle mDecodeStyle;
107 SurfacePipe mPipe;
110 } // namespace mozilla::image
112 #endif // mozilla_image_decoders_nsJPEGDecoder_h