blob: da8ce3f84f5bb411ea18c22cc716fd198905f0cc [file] [log] [blame]
Gian Marco Iodicec4124b52017-10-11 13:12:34 +01001/*
Anthony Barbier6db0ff52018-01-05 10:59:12 +00002 * Copyright (c) 2017, 2018 ARM Limited.
Gian Marco Iodicec4124b52017-10-11 13:12:34 +01003 *
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
25#include "arm_compute/runtime/NEON/NEFunctions.h"
26
27#include "arm_compute/core/Types.h"
28#include "utils/Utils.h"
29
30using namespace arm_compute;
31using namespace utils;
32
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000033class NEONCartoonEffectExample : public Example
Gian Marco Iodicec4124b52017-10-11 13:12:34 +010034{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000035public:
36 void do_setup(int argc, char **argv) override
37 {
38 // Open PPM file
39 PPMLoader ppm;
Gian Marco Iodicec4124b52017-10-11 13:12:34 +010040
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000041 if(argc < 2)
42 {
43 // Print help
44 std::cout << "Usage: ./build/neon_cartoon_effect [input_image.ppm]\n\n";
45 std::cout << "No input_image provided, creating a dummy 640x480 image\n";
46 // Create an empty grayscale 640x480 image
47 src_img.allocator()->init(TensorInfo(640, 480, Format::U8));
48 }
49 else
50 {
51 ppm.open(argv[1]);
52 ppm.init_image(src_img, Format::U8);
53 }
54
55 // Initialize just the dimensions and format of the images:
56 gaus5x5_img.allocator()->init(*src_img.info());
57 canny_edge_img.allocator()->init(*src_img.info());
58 dst_img.allocator()->init(*src_img.info());
59
60 // Configure the functions to call
61 gaus5x5.configure(&src_img, &gaus5x5_img, BorderMode::REPLICATE);
62 canny_edge.configure(&src_img, &canny_edge_img, 100, 80, 3, 1, BorderMode::REPLICATE);
63 sub.configure(&gaus5x5_img, &canny_edge_img, &dst_img, ConvertPolicy::SATURATE);
64
65 // Now that the padding requirements are known we can allocate the images:
66 src_img.allocator()->allocate();
67 dst_img.allocator()->allocate();
68 gaus5x5_img.allocator()->allocate();
69 canny_edge_img.allocator()->allocate();
70
71 // Fill the input image with the content of the PPM image if a filename was provided:
72 if(ppm.is_open())
73 {
74 ppm.fill_image(src_img);
75 output_filename = std::string(argv[1]) + "_out.ppm";
76 }
Gian Marco Iodicec4124b52017-10-11 13:12:34 +010077 }
78
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000079 void do_run() override
Gian Marco Iodicec4124b52017-10-11 13:12:34 +010080 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000081 // Execute the functions:
82 gaus5x5.run();
83 canny_edge.run();
84 sub.run();
Gian Marco Iodicec4124b52017-10-11 13:12:34 +010085 }
86
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000087 void do_teardown() override
Gian Marco Iodicec4124b52017-10-11 13:12:34 +010088 {
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000089 // Save the result to file:
90 if(!output_filename.empty())
91 {
92 save_to_ppm(dst_img, output_filename); // save_to_ppm maps and unmaps the image to store as PPM
93 }
Gian Marco Iodicec4124b52017-10-11 13:12:34 +010094 }
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +000095
96private:
97 Image src_img{}, dst_img{}, gaus5x5_img{}, canny_edge_img{};
98 NEGaussian5x5 gaus5x5{};
99 NECannyEdge canny_edge{};
100 NEArithmeticSubtraction sub{};
101 std::string output_filename{};
102};
Gian Marco Iodicec4124b52017-10-11 13:12:34 +0100103
104/** Main program for cartoon effect test
105 *
106 * @param[in] argc Number of arguments
107 * @param[in] argv Arguments ( [optional] Path to PPM image to process )
108 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000109int main(int argc, char **argv)
Gian Marco Iodicec4124b52017-10-11 13:12:34 +0100110{
Michalis Spyrou2b5f0f22018-01-10 14:08:50 +0000111 return utils::run_example<NEONCartoonEffectExample>(argc, argv);
Gian Marco Iodicec4124b52017-10-11 13:12:34 +0100112}