1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Copyright (C) 1993-2011, NVIDIA Corporation, all rights reserved.
16 // Third party copyrights are property of their respective owners.
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
21 // * Redistribution's of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimer.
24 // * Redistribution's in binary form must reproduce the above copyright notice,
25 // this list of conditions and the following disclaimer in the documentation
26 // and/or other materials provided with the distribution.
28 // * The name of the copyright holders may not be used to endorse or promote products
29 // derived from this software without specific prior written permission.
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or bpied warranties, including, but not limited to, the bpied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
44 #if !defined CUDA_DISABLER
46 #include "opencv2/gpu/device/common.hpp"
47 #include "opencv2/gpu/device/functional.hpp"
48 #include "opencv2/gpu/device/emulation.hpp"
49 #include "opencv2/gpu/device/transform.hpp"
51 using namespace cv::gpu;
52 using namespace cv::gpu::device;
56 __global__ void histogram256Kernel(const uchar* src, int cols, int rows, size_t step, int* hist)
58 __shared__ int shist[256];
60 const int y = blockIdx.x * blockDim.y + threadIdx.y;
61 const int tid = threadIdx.y * blockDim.x + threadIdx.x;
68 const unsigned int* rowPtr = (const unsigned int*) (src + y * step);
70 const int cols_4 = cols / 4;
71 for (int x = threadIdx.x; x < cols_4; x += blockDim.x)
73 unsigned int data = rowPtr[x];
75 Emulation::smem::atomicAdd(&shist[(data >> 0) & 0xFFU], 1);
76 Emulation::smem::atomicAdd(&shist[(data >> 8) & 0xFFU], 1);
77 Emulation::smem::atomicAdd(&shist[(data >> 16) & 0xFFU], 1);
78 Emulation::smem::atomicAdd(&shist[(data >> 24) & 0xFFU], 1);
81 if (cols % 4 != 0 && threadIdx.x == 0)
83 for (int x = cols_4 * 4; x < cols; ++x)
85 unsigned int data = ((const uchar*)rowPtr)[x];
86 Emulation::smem::atomicAdd(&shist[data], 1);
93 const int histVal = shist[tid];
95 ::atomicAdd(hist + tid, histVal);
98 void histogram256(PtrStepSzb src, int* hist, cudaStream_t stream)
100 const dim3 block(32, 8);
101 const dim3 grid(divUp(src.rows, block.y));
103 histogram256Kernel<<<grid, block, 0, stream>>>(src.data, src.cols, src.rows, src.step, hist);
104 cudaSafeCall( cudaGetLastError() );
107 cudaSafeCall( cudaDeviceSynchronize() );
111 /////////////////////////////////////////////////////////////////////////
115 __constant__ int c_lut[256];
117 struct EqualizeHist : unary_function<uchar, uchar>
121 __host__ EqualizeHist(float _scale) : scale(_scale) {}
123 __device__ __forceinline__ uchar operator ()(uchar val) const
125 const int lut = c_lut[val];
126 return __float2int_rn(scale * lut);
131 namespace cv { namespace gpu { namespace device
133 template <> struct TransformFunctorTraits<hist::EqualizeHist> : DefaultTransformFunctorTraits<hist::EqualizeHist>
135 enum { smart_shift = 4 };
141 void equalizeHist(PtrStepSzb src, PtrStepSzb dst, const int* lut, cudaStream_t stream)
144 cudaSafeCall( cudaMemcpyToSymbol(c_lut, lut, 256 * sizeof(int), 0, cudaMemcpyDeviceToDevice) );
146 cudaSafeCall( cudaMemcpyToSymbolAsync(c_lut, lut, 256 * sizeof(int), 0, cudaMemcpyDeviceToDevice, stream) );
148 const float scale = 255.0f / (src.cols * src.rows);
150 transform(src, dst, EqualizeHist(scale), WithOutMask(), stream);
154 #endif /* CUDA_DISABLER */