2 Copyright (C) 2012 fmaj7b5.info
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "binarize.cuh"
23 using namespace FM7b5;
26 FM7b5::binarize_gpu(ImageGray& out, const ImageGray& in, const uint8_t thres)
29 if (in.width() != out.width() || in.height() != out.height()) {
30 throw std::runtime_error("sizes of input and output images are diferent.");
33 const size_t width(in.width()), height(in.height()), bpp(in.bpp());
35 uint8_t* d_in(nullptr);
36 uint8_t* d_out(nullptr);
37 size_t in_pitch(0), out_pitch(0);
39 const size_t threads_per_dim(32);
40 dim3 threads_per_block(threads_per_dim, threads_per_dim);
41 dim3 blocks_per_grid((width + threads_per_block.x - 1)/ threads_per_block.x,
42 (height + threads_per_block.y - 1)/ threads_per_block.y);
46 // allocate input/output memories
47 status = cudaMallocPitch(&d_in, &in_pitch, width * bpp, height);
48 if (status != cudaSuccess) {
52 status = cudaMallocPitch(&d_out, &out_pitch, width * bpp, height);
53 if (status != cudaSuccess) {
57 // copy an input image to device memory
58 status = cudaMemcpy2D(d_in, in_pitch, in.data(), in.stride(), bpp * width, height, cudaMemcpyHostToDevice);
59 if (status != cudaSuccess) {
64 binarize<<<blocks_per_grid, threads_per_block>>>(d_out, out_pitch, d_in, in_pitch, width, height, thres);
66 // copy the result back to host memory
67 status = cudaMemcpy2D(out.data(), out.stride(), d_out, out_pitch, bpp * width, height, cudaMemcpyDeviceToHost);
68 if (status != cudaSuccess) {
72 // free device memories
84 throw std::runtime_error(cudaGetErrorString(status));
89 FM7b5::binarize(uint8_t* out, const size_t out_pitch, const uint8_t* in, const size_t in_pitch, const size_t width, const size_t height, const uint8_t thres)
91 const size_t w(blockDim.x * blockIdx.x + threadIdx.x);
92 const size_t h(blockDim.y * blockIdx.y + threadIdx.y);
94 if (w >= width || h >= height) {
98 out[out_pitch * h + w] = (in[in_pitch * h + w] < thres) ? 0 : 255;