|
| 1 | +//===- BuddyRotate2DBenchmark.cpp -------------------------------------------===// |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | +// |
| 17 | +// This file implements the benchmark for Rotate2D operation. |
| 18 | +// |
| 19 | +//===----------------------------------------------------------------------===// |
| 20 | + |
| 21 | +#include <benchmark/benchmark.h> |
| 22 | +#include <buddy/Core/Container.h> |
| 23 | +#include <buddy/DIP/ImageContainer.h> |
| 24 | +#include <buddy/DIP/DIP.h> |
| 25 | +#include <opencv2/opencv.hpp> |
| 26 | + |
| 27 | +using namespace cv; |
| 28 | +using namespace std; |
| 29 | + |
| 30 | +// Declare input image. |
| 31 | +Mat inputImageBuddyRotate2D; |
| 32 | + |
| 33 | +// Define the angle. |
| 34 | +float BuddyRotate2DAngle; |
| 35 | + |
| 36 | +// Define sizes of input. |
| 37 | +intptr_t sizesInputBuddyRotate2D[2]; |
| 38 | + |
| 39 | +// Declare Angle option supported. |
| 40 | +enum AngleOption { ANGLE_DEGREE, ANGLE_RADIAN }; |
| 41 | + |
| 42 | +// Define Angle option selected. |
| 43 | +AngleOption AngleType; |
| 44 | + |
| 45 | +void initializeBuddyRotate2D(char **argv) { |
| 46 | + inputImageBuddyRotate2D = imread(argv[1], IMREAD_GRAYSCALE); |
| 47 | + |
| 48 | + sizesInputBuddyRotate2D[0] = inputImageBuddyRotate2D.rows; |
| 49 | + sizesInputBuddyRotate2D[1] = inputImageBuddyRotate2D.cols; |
| 50 | + |
| 51 | + if (static_cast<string>(argv[2]) == "DEGREE") { |
| 52 | + AngleType = ANGLE_DEGREE; |
| 53 | + } else { |
| 54 | + AngleType = ANGLE_RADIAN; |
| 55 | + } |
| 56 | + |
| 57 | + std::string argAngle = argv[3]; |
| 58 | + try { |
| 59 | + BuddyRotate2DAngle = std::stof(argAngle); |
| 60 | + } catch (const std::exception& e) { |
| 61 | + cout << "Exception converting rotation angle to float." << endl; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +static void Buddy_Rotate2D_ANGLE_DEGREE(benchmark::State &state) { |
| 66 | + // Define the MemRef descriptor for input. |
| 67 | + Img<float, 2> inputBuddyRotate2D(inputImageBuddyRotate2D); |
| 68 | + |
| 69 | + for (auto _ : state) { |
| 70 | + for (int i = 0; i < state.range(0); ++i) { |
| 71 | + // Call the MLIR Rotate2D function. |
| 72 | + MemRef<float, 2> output = dip::Rotate2D(&inputBuddyRotate2D, BuddyRotate2DAngle, |
| 73 | + dip::ANGLE_TYPE::DEGREE); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +static void Buddy_Rotate2D_ANGLE_RADIAN(benchmark::State &state) { |
| 79 | + // Define the MemRef descriptor for input. |
| 80 | + Img<float, 2> inputBuddyRotate2D(inputImageBuddyRotate2D); |
| 81 | + |
| 82 | + for (auto _ : state) { |
| 83 | + for (int i = 0; i < state.range(0); ++i) { |
| 84 | + // Call the MLIR Rotate2D function. |
| 85 | + MemRef<float, 2> output = dip::Rotate2D(&inputBuddyRotate2D, BuddyRotate2DAngle, |
| 86 | + dip::ANGLE_TYPE::RADIAN); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// Register benchmarking function. |
| 92 | +void registerBenchmarkBuddyRotate2D() { |
| 93 | + if (AngleType == ANGLE_DEGREE) { |
| 94 | + BENCHMARK(Buddy_Rotate2D_ANGLE_DEGREE) |
| 95 | + ->Arg(1) |
| 96 | + ->Unit(benchmark::kMillisecond); |
| 97 | + } else { |
| 98 | + BENCHMARK(Buddy_Rotate2D_ANGLE_RADIAN) |
| 99 | + ->Arg(1) |
| 100 | + ->Unit(benchmark::kMillisecond); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// Generate result image. |
| 105 | +void generateResultBuddyRotate2D() { |
| 106 | + // Define the MemRef descriptor for input. |
| 107 | + Img<float, 2> input(inputImageBuddyRotate2D); |
| 108 | + MemRef<float, 2> output(sizesInputBuddyRotate2D); |
| 109 | + // Run the resize 2D operation. |
| 110 | + if (AngleType == ANGLE_DEGREE) { |
| 111 | + // Call the MLIR Rotate2D function. |
| 112 | + output = dip::Rotate2D(&input, BuddyRotate2DAngle, |
| 113 | + dip::ANGLE_TYPE::DEGREE); |
| 114 | + } else { |
| 115 | + // Call the MLIR Rotate2D function. |
| 116 | + output = dip::Rotate2D(&input, BuddyRotate2DAngle, |
| 117 | + dip::ANGLE_TYPE::RADIAN); |
| 118 | + } |
| 119 | + |
| 120 | + // Define a cv::Mat with the output of the resize operation. |
| 121 | + Mat outputImage(output.getSizes()[0], output.getSizes()[1], CV_32FC1, |
| 122 | + output.getData()); |
| 123 | + |
| 124 | + // Choose a PNG compression level |
| 125 | + vector<int> compressionParams; |
| 126 | + compressionParams.push_back(IMWRITE_PNG_COMPRESSION); |
| 127 | + compressionParams.push_back(9); |
| 128 | + |
| 129 | + // Write output to PNG. |
| 130 | + bool result = true; |
| 131 | + try { |
| 132 | + result = imwrite("ResultBuddyRotate2D.png", outputImage, compressionParams); |
| 133 | + } catch (const cv::Exception &ex) { |
| 134 | + fprintf(stderr, "Exception converting image to PNG format: %s\n", |
| 135 | + ex.what()); |
| 136 | + } |
| 137 | + if (result) |
| 138 | + cout << "Saved PNG file." << endl; |
| 139 | + else |
| 140 | + cout << "ERROR: Can't save PNG file." << endl; |
| 141 | +} |
0 commit comments