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