Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | */ |
Anthony Barbier | 15d5ac8 | 2017-07-17 15:22:17 +0100 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_CL /* Needed by Utils.cpp to handle OpenCL exceptions properly */ |
| 25 | #error "This example needs to be built with -DARM_COMPUTE_CL" |
| 26 | #endif /* ARM_COMPUTE_CL */ |
| 27 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Types.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 30 | #include "arm_compute/runtime/CL/functions/CLGaussian5x5.h" |
| 31 | #include "arm_compute/runtime/CL/functions/CLScale.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | #include "arm_compute/runtime/NEON/NEFunctions.h" |
Georgios Pinitas | 7c3b924 | 2018-06-21 19:01:25 +0100 | [diff] [blame] | 33 | #include "utils/ImageLoader.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | #include "utils/Utils.h" |
| 35 | |
| 36 | using namespace arm_compute; |
| 37 | using namespace utils; |
| 38 | |
| 39 | /** Example demonstrating how to use both CL and NEON functions in the same pipeline |
| 40 | * |
| 41 | * @param[in] argc Number of arguments |
| 42 | * @param[in] argv Arguments ( [optional] Path to PPM image to process ) |
| 43 | */ |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 44 | class NEONCLScaleMedianGaussianExample : public Example |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 46 | public: |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 47 | bool do_setup(int argc, char **argv) override |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 48 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 49 | /** [NEON / OpenCL Interop] */ |
| 50 | PPMLoader ppm; |
| 51 | |
| 52 | CLScheduler::get().default_init(); |
| 53 | |
| 54 | if(argc < 2) |
| 55 | { |
| 56 | // Print help |
| 57 | std::cout << "Usage: ./build/cl_convolution [input_image.ppm]\n\n"; |
| 58 | std::cout << "No input_image provided, creating a dummy 640x480 image\n"; |
| 59 | // Create an empty grayscale 640x480 image |
| 60 | src.allocator()->init(TensorInfo(640, 480, Format::U8)); |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | ppm.open(argv[1]); |
| 65 | ppm.init_image(src, Format::U8); |
| 66 | } |
| 67 | |
| 68 | TensorInfo scale_median_info(TensorInfo(src.info()->dimension(0) / 2, src.info()->dimension(1) / 2, Format::U8)); |
| 69 | |
| 70 | // Configure the temporary and destination images |
| 71 | scale_median.allocator()->init(scale_median_info); |
| 72 | median_gauss.allocator()->init(scale_median_info); |
| 73 | dst.allocator()->init(scale_median_info); |
| 74 | |
Sang-Hoon Park | ccd9496 | 2020-06-09 12:09:24 +0100 | [diff] [blame] | 75 | scale.configure(&src, &scale_median, ScaleKernelInfo{ InterpolationPolicy::NEAREST_NEIGHBOR, BorderMode::REPLICATE }); |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 76 | median.configure(&scale_median, &median_gauss, BorderMode::REPLICATE); |
| 77 | gauss.configure(&median_gauss, &dst, BorderMode::REPLICATE); |
| 78 | |
| 79 | // Allocate all the images |
| 80 | src.allocator()->allocate(); |
| 81 | scale_median.allocator()->allocate(); |
| 82 | median_gauss.allocator()->allocate(); |
| 83 | dst.allocator()->allocate(); |
| 84 | |
| 85 | // Fill the input image with the content of the PPM image if a filename was provided: |
| 86 | if(ppm.is_open()) |
| 87 | { |
| 88 | ppm.fill_image(src); |
| 89 | const std::string output_filename = std::string(argv[1]) + "_out.ppm"; |
| 90 | } |
| 91 | /** [NEON / OpenCL Interop] */ |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 92 | |
| 93 | return true; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 94 | } |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 95 | void do_run() override |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 97 | // Enqueue and flush the OpenCL kernel: |
| 98 | scale.run(); |
| 99 | |
| 100 | // Do a blocking map of the input and output buffers of the NEON function: |
| 101 | scale_median.map(); |
| 102 | median_gauss.map(); |
| 103 | |
| 104 | // Run the NEON function: |
| 105 | median.run(); |
| 106 | |
| 107 | // Unmap the output buffer before it's used again by OpenCL: |
| 108 | scale_median.unmap(); |
| 109 | median_gauss.unmap(); |
| 110 | |
| 111 | // Run the final OpenCL function: |
| 112 | gauss.run(); |
| 113 | |
| 114 | // Make sure all the OpenCL jobs are done executing: |
| 115 | CLScheduler::get().sync(); |
| 116 | } |
| 117 | void do_teardown() override |
| 118 | { |
| 119 | // Save the result to file: |
| 120 | if(!output_filename.empty()) |
| 121 | { |
| 122 | save_to_ppm(dst, output_filename); // save_to_ppm maps and unmaps the image to store as PPM |
| 123 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 126 | private: |
| 127 | CLImage src{}, scale_median{}, median_gauss{}, dst{}; |
| 128 | CLScale scale{}; |
| 129 | NEMedian3x3 median{}; |
| 130 | CLGaussian5x5 gauss{}; |
| 131 | std::string output_filename{}; |
| 132 | }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 133 | |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 134 | /** Main program for neon/cl scale median gaussian test |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 135 | * |
| 136 | * @param[in] argc Number of arguments |
| 137 | * @param[in] argv Arguments ( [optional] Path to PPM image to process ) |
| 138 | */ |
Anthony Barbier | 6db0ff5 | 2018-01-05 10:59:12 +0000 | [diff] [blame] | 139 | int main(int argc, char **argv) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 140 | { |
Michalis Spyrou | 2b5f0f2 | 2018-01-10 14:08:50 +0000 | [diff] [blame] | 141 | return utils::run_example<NEONCLScaleMedianGaussianExample>(argc, argv); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 142 | } |