1 /* Copyright 2017 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
16 #include <cstdint>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <fstream>
20 #include <iostream>
21
22 #include <unistd.h> // NOLINT(build/include_order)
23
24 #include "tensorflow/lite/examples/label_image/bitmap_helpers.h"
25
26 #define LOG(x) std::cerr
27
28 namespace tflite {
29 namespace label_image {
30
decode_bmp(const uint8_t * input,int row_size,int width,int height,int channels,bool top_down)31 std::vector<uint8_t> decode_bmp(const uint8_t* input, int row_size, int width,
32 int height, int channels, bool top_down) {
33 std::vector<uint8_t> output(height * width * channels);
34 for (int i = 0; i < height; i++) {
35 int src_pos;
36 int dst_pos;
37
38 for (int j = 0; j < width; j++) {
39 if (!top_down) {
40 src_pos = ((height - 1 - i) * row_size) + j * channels;
41 } else {
42 src_pos = i * row_size + j * channels;
43 }
44
45 dst_pos = (i * width + j) * channels;
46
47 switch (channels) {
48 case 1:
49 output[dst_pos] = input[src_pos];
50 break;
51 case 3:
52 // BGR -> RGB
53 output[dst_pos] = input[src_pos + 2];
54 output[dst_pos + 1] = input[src_pos + 1];
55 output[dst_pos + 2] = input[src_pos];
56 break;
57 case 4:
58 // BGRA -> RGBA
59 output[dst_pos] = input[src_pos + 2];
60 output[dst_pos + 1] = input[src_pos + 1];
61 output[dst_pos + 2] = input[src_pos];
62 output[dst_pos + 3] = input[src_pos + 3];
63 break;
64 default:
65 LOG(FATAL) << "Unexpected number of channels: " << channels;
66 break;
67 }
68 }
69 }
70 return output;
71 }
72
read_bmp(const std::string & input_bmp_name,int * width,int * height,int * channels,Settings * s)73 std::vector<uint8_t> read_bmp(const std::string& input_bmp_name, int* width,
74 int* height, int* channels, Settings* s) {
75 int begin, end;
76
77 std::ifstream file(input_bmp_name, std::ios::in | std::ios::binary);
78 if (!file) {
79 LOG(FATAL) << "input file " << input_bmp_name << " not found\n";
80 exit(-1);
81 }
82
83 begin = file.tellg();
84 file.seekg(0, std::ios::end);
85 end = file.tellg();
86 size_t len = end - begin;
87
88 if (s->verbose) LOG(INFO) << "len: " << len << "\n";
89
90 std::vector<uint8_t> img_bytes(len);
91 file.seekg(0, std::ios::beg);
92 file.read(reinterpret_cast<char*>(img_bytes.data()), len);
93 const int32_t header_size =
94 *(reinterpret_cast<const int32_t*>(img_bytes.data() + 10));
95 *width = *(reinterpret_cast<const int32_t*>(img_bytes.data() + 18));
96 *height = *(reinterpret_cast<const int32_t*>(img_bytes.data() + 22));
97 const int32_t bpp =
98 *(reinterpret_cast<const int32_t*>(img_bytes.data() + 28));
99 *channels = bpp / 8;
100
101 if (s->verbose)
102 LOG(INFO) << "width, height, channels: " << *width << ", " << *height
103 << ", " << *channels << "\n";
104
105 // there may be padding bytes when the width is not a multiple of 4 bytes
106 // 8 * channels == bits per pixel
107 const int row_size = (8 * *channels * *width + 31) / 32 * 4;
108
109 // if height is negative, data layout is top down
110 // otherwise, it's bottom up
111 bool top_down = (*height < 0);
112
113 // Decode image, allocating tensor once the image size is known
114 const uint8_t* bmp_pixels = &img_bytes[header_size];
115 return decode_bmp(bmp_pixels, row_size, *width, abs(*height), *channels,
116 top_down);
117 }
118
119 } // namespace label_image
120 } // namespace tflite
121