• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkJpegEncoderImpl_DEFINED
9 #define SkJpegEncoderImpl_DEFINED
10 
11 #include "include/core/SkData.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkYUVAPixmaps.h"
14 #include "include/encode/SkEncoder.h"
15 
16 #include <cstdint>
17 #include <memory>
18 #include <optional>
19 #include <utility>
20 #include <vector>
21 
22 class SkColorSpace;
23 class SkJpegEncoderMgr;
24 class SkPixmap;
25 class SkWStream;
26 
27 namespace SkJpegEncoder {
28 struct Options;
29 }  // namespace SkJpegEncoder
30 
31 // JPEG metadata is included in marker-based segments in the header of the image (the part before
32 // the first StartOfScan marker). These functions append these parameters to an SkJpegMarkerList.
33 namespace SkJpegMetadataEncoder {
34 
35 // Metadata segments that will be added to the encoded file using
36 struct Segment {
SegmentSegment37     Segment(uint8_t marker, sk_sp<SkData> parameters)
38             : fMarker(marker), fParameters(std::move(parameters)) {}
39     uint8_t fMarker = 0;
40     sk_sp<SkData> fParameters;
41 };
42 
43 using SegmentList = std::vector<Segment>;
44 
45 // Include an ICC profile in the image. If |colorSpace| is nullptr, then include no profile. If
46 // |options| specifies ICC profile data, then use that data, otherwise, generate a profile for
47 // |colorSpace|.
48 void AppendICC(SegmentList& segmentList,
49                const SkJpegEncoder::Options& options,
50                const SkColorSpace* colorSpace);
51 
52 // Include a standard (as opposed to extended) XMP metadata segment.
53 void AppendXMPStandard(SegmentList& segmentList, const SkData* xmpMetadata);
54 
55 }  // namespace SkJpegMetadataEncoder
56 
57 class SkJpegEncoderImpl : public SkEncoder {
58 public:
59     // Make an encoder from RGB or YUV data. Encoding options are specified in |options|. Metadata
60     // markers are listed in |metadata|. The ICC profile and XMP metadata are read from |metadata|
61     // and not from |options|.
62     static std::unique_ptr<SkEncoder> MakeRGB(SkWStream* dst,
63                                               const SkPixmap& src,
64                                               const SkJpegEncoder::Options& options,
65                                               const SkJpegMetadataEncoder::SegmentList& metadata);
66     static std::unique_ptr<SkEncoder> MakeYUV(SkWStream* dst,
67                                               const SkYUVAPixmaps& srcYUVA,
68                                               const SkColorSpace* srcYUVAColorSpace,
69                                               const SkJpegEncoder::Options& options,
70                                               const SkJpegMetadataEncoder::SegmentList& metadata);
71 
72     ~SkJpegEncoderImpl() override;
73 
74 protected:
75     bool onEncodeRows(int numRows) override;
76 
77 private:
78     SkJpegEncoderImpl(std::unique_ptr<SkJpegEncoderMgr>, const SkPixmap& src);
79     SkJpegEncoderImpl(std::unique_ptr<SkJpegEncoderMgr>, const SkYUVAPixmaps& srcYUVA);
80 
81     std::unique_ptr<SkJpegEncoderMgr> fEncoderMgr;
82     std::optional<SkYUVAPixmaps> fSrcYUVA;
83 };
84 
85 #endif
86