arm_compute v17.09
[platform/upstream/armcl.git] / examples / neon_scale.cpp
1 /*
2  * Copyright (c) 2016, 2017 ARM Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "arm_compute/runtime/NEON/NEFunctions.h"
25
26 #include "arm_compute/core/Types.h"
27 #include "utils/Utils.h"
28
29 using namespace arm_compute;
30 using namespace utils;
31
32 void main_neon_scale(int argc, const char **argv)
33 {
34     PPMLoader ppm;
35     Image     src, dst;
36
37     if(argc < 2)
38     {
39         // Print help
40         std::cout << "Usage: ./build/neon_scale[input_image.ppm]\n\n";
41         std::cout << "No input_image provided, creating a dummy 640x480 image\n";
42         // Create an empty grayscale 640x480 image
43         src.allocator()->init(TensorInfo(640, 480, Format::U8));
44     }
45     else
46     {
47         ppm.open(argv[1]);
48         ppm.init_image(src, Format::U8);
49     }
50
51     constexpr int scale_factor = 2;
52
53     TensorInfo dst_tensor_info(src.info()->dimension(0) / scale_factor, src.info()->dimension(1) / scale_factor, Format::U8);
54
55     // Configure the destination image
56     dst.allocator()->init(dst_tensor_info);
57
58     // Create and initialize a Scale function object:
59     NEScale scale;
60     scale.configure(&src, &dst, InterpolationPolicy::NEAREST_NEIGHBOR, BorderMode::UNDEFINED);
61
62     // Allocate all the images
63     src.allocator()->allocate();
64     dst.allocator()->allocate();
65     // Fill the input image with the content of the PPM image if a filename was provided:
66     if(ppm.is_open())
67     {
68         ppm.fill_image(src);
69     }
70
71     // Run the scale operation:
72     scale.run();
73
74     // Save the result to file:
75     if(ppm.is_open())
76     {
77         const std::string output_filename = std::string(argv[1]) + "_out.ppm";
78         save_to_ppm(dst, output_filename);
79     }
80 }
81
82 /** Main program for convolution test
83  *
84  * @param[in] argc Number of arguments
85  * @param[in] argv Arguments ( [optional] Path to PPM image to process )
86  */
87 int main(int argc, const char **argv)
88 {
89     return utils::run_example(argc, argv, main_neon_scale);
90 }