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