arm_compute v17.03.1
[platform/upstream/armcl.git] / examples / neoncl_scale_median_gaussian.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 #define ARM_COMPUTE_CL /* So that OpenCL exceptions get caught too */
25 #include "arm_compute/core/Types.h"
26 #include "arm_compute/runtime/CL/CLFunctions.h"
27 #include "arm_compute/runtime/CL/CLScheduler.h"
28 #include "arm_compute/runtime/NEON/NEFunctions.h"
29 #include "test_helpers/Utils.h"
30
31 using namespace arm_compute;
32 using namespace test_helpers;
33
34 /** Example demonstrating how to use both CL and NEON functions in the same pipeline
35  *
36  * @param[in] argc Number of arguments
37  * @param[in] argv Arguments ( [optional] Path to PPM image to process )
38  */
39 void main_neoncl_scale_median_gaussian(int argc, const char **argv)
40 {
41     /** [NEON / OpenCL Interop] */
42     PPMLoader ppm;
43     CLImage   src, scale_median, median_gauss, dst;
44
45     CLScheduler::get().default_init();
46
47     if(argc < 2)
48     {
49         // Print help
50         std::cout << "Usage: ./build/cl_convolution [input_image.ppm]\n\n";
51         std::cout << "No input_image provided, creating a dummy 640x480 image\n";
52         // Create an empty grayscale 640x480 image
53         src.allocator()->init(TensorInfo(640, 480, Format::U8));
54     }
55     else
56     {
57         ppm.open(argv[1]);
58         ppm.init_image(src, Format::U8);
59     }
60
61     TensorInfo scale_median_info(TensorInfo(src.info()->dimension(0) / 2, src.info()->dimension(1) / 2, Format::U8));
62
63     // Configure the temporary and destination images
64     scale_median.allocator()->init(scale_median_info);
65     median_gauss.allocator()->init(scale_median_info);
66     dst.allocator()->init(scale_median_info);
67
68     // Declare and configure the functions to create the following pipeline: scale -> median -> gauss
69     CLScale       scale;
70     NEMedian3x3   median;
71     CLGaussian5x5 gauss;
72
73     scale.configure(&src, &scale_median, InterpolationPolicy::NEAREST_NEIGHBOR, BorderMode::REPLICATE);
74     median.configure(&scale_median, &median_gauss, BorderMode::REPLICATE);
75     gauss.configure(&median_gauss, &dst, BorderMode::REPLICATE);
76
77     // Allocate all the images
78     src.allocator()->allocate();
79     scale_median.allocator()->allocate();
80     median_gauss.allocator()->allocate();
81     dst.allocator()->allocate();
82
83     // Fill the input image with the content of the PPM image if a filename was provided:
84     if(ppm.is_open())
85     {
86         ppm.fill_image(src);
87     }
88
89     // Enqueue and flush the OpenCL kernel:
90     scale.run();
91
92     // Do a blocking map of the input and output buffers of the NEON function:
93     scale_median.map();
94     median_gauss.map();
95
96     // Run the NEON function:
97     median.run();
98
99     // Unmap the output buffer before it's used again by OpenCL:
100     scale_median.unmap();
101     median_gauss.unmap();
102
103     // Run the final OpenCL function:
104     gauss.run();
105
106     // Make sure all the OpenCL jobs are done executing:
107     CLScheduler::get().sync();
108
109     // Save the result to file:
110     if(ppm.is_open())
111     {
112         const std::string output_filename = std::string(argv[1]) + "_out.ppm";
113         save_to_ppm(dst, output_filename); // save_to_ppm maps and unmaps the image to store as PPM
114     }
115     /** [NEON / OpenCL Interop] */
116 }
117
118 /** Main program for convolution test
119  * @param[in] argc Number of arguments
120  * @param[in] argv Arguments ( [optional] Path to PPM image to process )
121  */
122 int main(int argc, const char **argv)
123 {
124     return test_helpers::run_example(argc, argv, main_neoncl_scale_median_gaussian);
125 }