2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include "bin_image.h"
22 BinImage::BinImage(unsigned int width, unsigned int height, unsigned int channels)
23 : _width(width), _height(height), _channels(channels)
27 BinImage::~BinImage() {}
29 void BinImage::loadImage(const std::string &filename)
31 std::ifstream fin(filename);
35 std::cerr << "image filename is not specified. "
36 << "Input image will not be set." << std::endl;
40 _image.reserve(_width * _height * _channels);
42 // Assuption: binary image is stored in the order of [H,W,C]
43 for (unsigned int i = 0; i < _width * _height * _channels; ++i)
44 _image.push_back(fin.get());
47 void BinImage::AssignTensor(TfLiteTensor *t)
50 const int IMAGE_MEAN = 128;
51 const float IMAGE_STD = 128.0f;
53 // to prevent runtime exception
54 if (_image.size() < _width * _height * _channels)
56 std::cerr << "Input image size is smaller than the size required by the model."
57 << " Input will not be set." << std::endl;
61 for (int x = 0; x < _width; ++x)
63 for (int y = 0; y < _height; ++y)
65 for (int c = 0; c < _channels; ++c)
67 *p++ = (_image[y * _width * _channels + x * _channels + c] - IMAGE_MEAN) / IMAGE_STD;