xref: /aosp_15_r20/external/skia/src/codec/SkJpegMultiPicture.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2023 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 SkJpegMultiPicture_codec_DEFINED
9 #define SkJpegMultiPicture_codec_DEFINED
10 
11 #include "include/core/SkRefCnt.h"
12 
13 #include <cstdint>
14 #include <memory>
15 #include <vector>
16 
17 class SkData;
18 
19 /*
20  * Parsed Jpeg Multi-Picture Format structure as specified in CIPA DC-x007-2009. An introduction to
21  * the format can be found in Figure 1 (Basic MP File format data structure) and Figure 6 (Internal
22  * Structure of the MP Index IFD) in that document. This parsing will extract only the size and
23  * offset parameters from the images in the Index Image File Directory.
24  */
25 struct SkJpegMultiPictureParameters {
SkJpegMultiPictureParametersSkJpegMultiPictureParameters26     explicit SkJpegMultiPictureParameters(size_t numberOfImages) : images(numberOfImages) {}
27 
28     // An individual image.
29     struct Image {
30         // The size of the image in bytes.
31         uint32_t size = 0;
32         // The offset of the image in bytes. This offset is specified relative to the address of
33         // the MP Endian field in the MP Header, unless the image is a First Individual Image, in
34         // which case the value of the offest [sic] shall be NULL (from section 5.2.3.3).
35         uint32_t dataOffset = 0;
36     };
37 
38     // The images listed in the Index Image File Directory.
39     std::vector<Image> images;
40 
41     /*
42      * Parse Jpeg Multi-Picture Format parameters. The specified data should be APP2 segment
43      * parameters, which, if they are MPF parameter, should start with the {'M', 'P', 'F', 0}
44      * signature. Returns nullptr the parameters do not start with the MPF signature, or if there
45      * is an error in parsing the parameters.
46      */
47     static std::unique_ptr<SkJpegMultiPictureParameters> Make(
48             const sk_sp<const SkData>& segmentParameters);
49 
50     /*
51      * Serialize Jpeg Multi-Picture Format segment parameters for the indicated individual image.
52      * This segment will start with the {'M', 'P', 'F', 0} signature (it will not include the
53      * segment marker or parameter length).
54      */
55     sk_sp<SkData> serialize(uint32_t individualImageNumber) const;
56 
57     /*
58      * Compute the absolute offset (from the start of the image) for the offset in the multi-picture
59      * parameters, given the absolute offset of the MPF segment (the offset of the {0xFF, 0xE2}
60      * marker from the start of the image.
61      */
62     static size_t GetImageAbsoluteOffset(uint32_t dataOffset, size_t mpSegmentOffset);
63 
64     /*
65      * Compute the data offset (as stored in the multi-picture params) for an image given its
66      * absolute offset (from the start of the first individual image), and the absolute offset
67      * of the MPF segment in the first individual image. This will return 0 for an image at
68      * absolute offset 0.
69      */
70     static uint32_t GetImageDataOffset(size_t imageAbsoluteOffset, size_t mpSegmentOffset);
71 };
72 
73 #endif
74