xref: /aosp_15_r20/external/piex/src/piex.h (revision 4d671364a067eb4f124488347677d916765212d1)
1 // Copyright 2015 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ////////////////////////////////////////////////////////////////////////////////
16 //
17 // The purpose of the preview-image-extractor (piex) is to find and extract the
18 // largest JPEG compressed preview image contained in a RAW file.
19 //
20 // Even for unsupported RAW files we want to provide high quality images using a
21 // dedicated, small and portable library. That is possible by taking the preview
22 // image contained in all RAW files.
23 //
24 // Typically a preview image is stored as JPEG compressed, full size (or at
25 // least half size) image in a RAW file.
26 //
27 // A typical client code snippet:
28 //
29 //   // In C++
30 //   PreviewImageData image_data;
31 //   unique_ptr<StreamInterface> data_stream(new DataStream(file));
32 //   Error err = GetPreviewImageData(data_stream.get(), &image_data));
33 //   if (err == Error::kFail) {
34 //     // The input data seems to be broken.
35 //     return;
36 //   } else if (err == Error::kUnsupported) {
37 //     // The input data is not supported.
38 //     return;
39 //   }
40 //
41 //   // Uncompress the JPEG as usual, e.g. on Android with the BitmapFactory:
42 //   // In Java
43 //   Bitmap bitmap = BitmapFactory.decodeByteArray(
44 //       file.at(image_data.preview_offset), image_data.preview_length);
45 
46 #ifndef PIEX_PIEX_H_
47 #define PIEX_PIEX_H_
48 
49 #include <string>
50 #include <vector>
51 
52 #include "src/image_type_recognition/image_type_recognition_lite.h"
53 #include "src/piex_types.h"
54 
55 namespace piex {
56 
57 // Returns the maximum number of bytes IsRaw() will read from the stream.
58 size_t BytesRequiredForIsRaw();
59 
60 // Returns true if 'data' contains a RAW file format, even if it is not
61 // supported by Piex, false otherwise. Reads at most BytesRequiredForIsRaw()
62 // from the stream.
63 bool IsRaw(StreamInterface* data);
64 
65 // Gets the largest JPEG compressed preview image data. On success
66 // 'preview_image_data' contains image metadata, the unverified length and the
67 // offset to a JPEG compressed image from the beginning of the file.
68 //
69 // Returns 'kFail' when something with the data is wrong.
70 // Returns 'kUnsupported' if file format is not supported.
71 //
72 // One could check the "preview_image_data->preview_length != 0" for the
73 // existance of a preview image.
74 //
75 // Updates output_type based on data, if output_type is non-null.
76 Error GetPreviewImageData(
77     StreamInterface* data, PreviewImageData* preview_image_data,
78     image_type_recognition::RawImageTypes* output_type = nullptr);
79 
80 // Returns true if the full width and height and the mosaic pattern dimension of
81 // a DNG image could be obtained. False otherwise.
82 bool GetDngInformation(StreamInterface* data, std::uint32_t* width,
83                        std::uint32_t* height,
84                        std::vector<std::uint32_t>* cfa_pattern_dim);
85 
86 // Returns true if Exif orientation for the image can be obtained. False
87 // otherwise.
88 bool GetOrientation(StreamInterface* data, std::uint32_t* orientation);
89 
90 // Returns a vector of upper case file extensions, which are used as a first
91 // step to quickly guess a supported file format.
92 std::vector<std::string> SupportedExtensions();
93 
94 }  // namespace piex
95 
96 #endif  // PIEX_PIEX_H_
97