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 #include "tensorflow_lite_support/examples/task/vision/desktop/utils/image_utils.h"
16
17 #include <cstdlib>
18 #include <cstring>
19 #include <vector>
20
21 // These need to be defined for stb_image.h and stb_image_write.h to include
22 // the actual implementations of image decoding/encoding functions.
23 #define STB_IMAGE_IMPLEMENTATION
24 #define STB_IMAGE_WRITE_IMPLEMENTATION
25
26 #include "absl/status/status.h"
27 #include "absl/strings/match.h"
28 #include "absl/strings/str_format.h"
29 #include "stb_image.h"
30 #include "stb_image_write.h"
31 #include "tensorflow_lite_support/cc/port/status_macros.h"
32 #include "tensorflow_lite_support/cc/port/statusor.h"
33
34 namespace tflite {
35 namespace task {
36 namespace vision {
37
38 using ::tflite::support::StatusOr;
39
DecodeImageFromFile(const std::string & file_name)40 StatusOr<ImageData> DecodeImageFromFile(const std::string& file_name) {
41 ImageData image_data;
42 image_data.pixel_data = stbi_load(file_name.c_str(), &image_data.width,
43 &image_data.height, &image_data.channels,
44 /*desired_channels=*/0);
45 if (image_data.pixel_data == nullptr) {
46 return absl::InternalError(absl::StrFormat(
47 "An error occurred while decoding image: %s", stbi_failure_reason()));
48 }
49 if (image_data.channels != 1 && image_data.channels != 3 &&
50 image_data.channels != 4) {
51 stbi_image_free(image_data.pixel_data);
52 return absl::UnimplementedError(
53 absl::StrFormat("Expected image with 1 (grayscale), 3 (RGB) or 4 "
54 "(RGBA) channels, found %d",
55 image_data.channels));
56 }
57 return image_data;
58 }
59
EncodeImageToPngFile(const ImageData & image_data,const std::string & image_path)60 absl::Status EncodeImageToPngFile(const ImageData& image_data,
61 const std::string& image_path) {
62 // Sanity check inputs.
63 if (image_data.width <= 0 || image_data.height <= 0) {
64 return absl::InvalidArgumentError(
65 absl::StrFormat("Expected positive image dimensions, found %d x %d.",
66 image_data.width, image_data.height));
67 }
68 if (image_data.channels != 1 && image_data.channels != 3 &&
69 image_data.channels != 4) {
70 return absl::UnimplementedError(
71 absl::StrFormat("Expected image data with 1 (grayscale), 3 (RGB) or 4 "
72 "(RGBA) channels, found %d",
73 image_data.channels));
74 }
75 if (image_data.pixel_data == nullptr) {
76 return absl::InvalidArgumentError(
77 "Expected pixel data to be set, found nullptr.");
78 }
79
80 if (stbi_write_png(
81 image_path.c_str(), image_data.width, image_data.height,
82 image_data.channels, image_data.pixel_data,
83 /*stride_in_bytes=*/image_data.width * image_data.channels) == 0) {
84 return absl::InternalError("An error occurred while encoding image.");
85 }
86
87 return absl::OkStatus();
88 }
89
ImageDataFree(ImageData * image)90 void ImageDataFree(ImageData* image) { stbi_image_free(image->pixel_data); }
91
92 } // namespace vision
93 } // namespace task
94 } // namespace tflite
95