1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
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 #ifndef TENSORFLOW_LITE_SUPPORT_EXAMPLES_TASK_VISION_DESKTOP_UTILS_IMAGE_UTILS_H_
16 #define TENSORFLOW_LITE_SUPPORT_EXAMPLES_TASK_VISION_DESKTOP_UTILS_IMAGE_UTILS_H_
17 
18 #include "absl/status/status.h"
19 #include "absl/strings/string_view.h"
20 #include "tensorflow_lite_support/cc/port/integral_types.h"
21 #include "tensorflow_lite_support/cc/port/statusor.h"
22 
23 namespace tflite {
24 namespace task {
25 namespace vision {
26 
27 // Image data with pixels stored as a row-major flattened array.
28 // Channels can be:
29 // 1 : grayscale
30 // 3 : RGB, interleaved
31 // 4 : RGBA, interleaved
32 struct ImageData {
33   uint8* pixel_data;
34   int width;
35   int height;
36   int channels;
37 };
38 
39 // Decodes image file and returns the corresponding image if no error
40 // occurred. If decoding succeeded, the caller must manage deletion of the
41 // underlying pixel data using `ImageDataFree`.
42 // Supports a wide range of image formats, listed in `stb_image/stb_image.h`.
43 tflite::support::StatusOr<ImageData> DecodeImageFromFile(
44     const std::string& file_name);
45 
46 // Encodes the image provided as an ImageData as lossless PNG to the provided
47 // path.
48 absl::Status EncodeImageToPngFile(const ImageData& image_data,
49                                   const std::string& image_path);
50 
51 // Releases image pixel data memory.
52 void ImageDataFree(ImageData* image);
53 
54 }  // namespace vision
55 }  // namespace task
56 }  // namespace tflite
57 
58 #endif  // TENSORFLOW_LITE_SUPPORT_EXAMPLES_TASK_VISION_DESKTOP_UTILS_IMAGE_UTILS_H_
59