aboutsummaryrefslogtreecommitdiff
path: root/src/pix_opencv_clahe.cc
blob: 4de7cfcfbc3db1952f4a09ad207c333ef1dcce40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
////////////////////////////////////////////////////////
//
// GEM - Graphics Environment for Multimedia
//
// zmoelnig@iem.kug.ac.at
//
// Implementation file
//
//    Copyright (c) 1997-2000 Mark Danks.
//    Copyright (c) Günther Geiger.
//    Copyright (c) 2001-2002 IOhannes m zmoelnig. forum::für::umläute. IEM
//    Copyright (c) 2002 James Tittle & Chris Clepper
//    For information on usage and redistribution, and for a DISCLAIMER OF ALL
//    WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution.
//
/////////////////////////////////////////////////////////
// based on code written by Lluis Gomez i Bigorda ( lluisgomez _at_ hangar _dot_ org ) (pix_opencv)
// Template for pix_opencv class

#if HAVE_CLAHE
#include "pix_opencv_clahe.h"

using namespace cv;

CPPEXTERN_NEW_WITH_THREE_ARGS(pix_opencv_clahe, t_floatarg, A_DEFFLOAT, t_floatarg, A_DEFFLOAT, t_floatarg, A_DEFFLOAT);

/////////////////////////////////////////////////////////
//
// pix_opencv_clahe
//
/////////////////////////////////////////////////////////
// Constructor
//
/////////////////////////////////////////////////////////
pix_opencv_clahe :: pix_opencv_clahe(t_float clipLimit, int width, int height)
  : m_clipLimit(40), m_tileGridSize(Size(8,8)), m_rendering(false)
{ 
  if ( clipLimit > 0. ){
    m_clipLimit = clipLimit;
  }
  if ( width > 0 && height > 0 ){
    m_tileGridSize=cv::Size(width, height);
  } else if (width > 0 || height > 0) {
    int max = width > height ? width : height;
    m_tileGridSize=Size ( max, max );
  }
}

/////////////////////////////////////////////////////////
// Destructor
//
/////////////////////////////////////////////////////////
pix_opencv_clahe :: ~pix_opencv_clahe()
{ 
}

// StartRendering
void pix_opencv_clahe :: startRendering(){
#if HAVE_LIBOPENCV_CL
  ocl::DevicesInfo devicesInfo;
  ocl::getOpenCLDevices(devicesInfo);
  post("Found %d OpenCL device(s).", devicesInfo.size());
  for ( size_t i = 0; i < devicesInfo.size(); i++){
    post("%s %s", devicesInfo[i]->deviceVendor.c_str(), devicesInfo[i]->deviceName.c_str());
  }
 
  m_cpuFilter = createCLAHE();
 
  if ( devicesInfo.size() == 0 ){
    post("can't find OpenCL device, switch to CPU mode");
    m_gpuMode = false;
  } else {
    m_oclFilter = ocl::createCLAHE();
    m_gpuMode = true;
  }
  clipLimitMess(m_clipLimit);
  tileGridSizeMess(m_tileGridSize.width,m_tileGridSize.height);
#else
  verbose(2,"no OpenCL support, it could be very slow !!");
#endif /* HAVE_LIBOPENCV_CL */

  m_rendering = true;
}

void pix_opencv_clahe :: stopRendering(){
  m_rendering = false;
}

/////////////////////////////////////////////////////////
// processImage
//
///////////////////////////////////////////////////////// 	
void pix_opencv_clahe :: processImage(imageStruct &image)
{ 
  if ( image.csize == 1 ){
    m_gray = Mat( image.ysize, image.xsize, CV_8UC1, image.data, image.csize*image.xsize); // just transform imageStruct to cv::Mat without copying data
  } else if (image.csize == 4) {
    m_imgMat = Mat( image.ysize, image.xsize, CV_8UC4, image.data, image.csize*image.xsize); // just transform imageStruct to cv::Mat without copying data
    cvtColor(m_imgMat,m_gray,CV_RGBA2GRAY);
  } else {
    error("only support grayscale and RGBA image");
  }

#if HAVE_LIBOPENCV_CL
  if ( m_gpuMode ) {
    try  {
      d_outframe = m_gray;
      m_oclFilter->apply(d_outframe, d_outframe);
      d_outframe.download(m_gray);
    } catch (cv::Exception& e) {
      error("can't use OpenCL, do you have OpenCL driver installed ?");
      error("error %d : %s", e.code, e.err.c_str());
      m_gpuMode = false;
      return;
    }
#else
  if ( 0 ) {
#endif /* HAVE_LIBOPENCV_CL */
  } else {
    m_cpuFilter->apply(m_gray, m_gray);
  }
  
  if ( image.csize == 4 ){
    std::vector<Mat> split;
    cv::split(m_imgMat,split);
    split.pop_back();
    split.push_back(m_gray);
    cv::merge(split,m_imgMat);    
  }
}

void pix_opencv_clahe :: clipLimitMess(t_float limit){
  m_clipLimit=limit;
  if ( m_rendering ){
#if HAVE_LIBOPENCV_CL
    if ( m_gpuMode ){
      m_oclFilter->setClipLimit(m_clipLimit);
#else
    if ( 0 ) {
#endif /* HAVE_LIBOPENCV_CL */
    } else {
      m_cpuFilter->setClipLimit(m_clipLimit);
    }
  }
}

void pix_opencv_clahe :: tileGridSizeMess(int width, int height){
  m_tileGridSize=cv::Size(MAX(width,1),MAX(height,1));
  if ( m_rendering ){
#if HAVE_LIBOPENCV_CL
    if ( m_gpuMode ){
      m_oclFilter->setTilesGridSize(m_tileGridSize);
#else
    if ( 0 ) {
#endif /* HAVE_LIBOPENCV_CL */
    } else {
      m_cpuFilter->setTilesGridSize(m_tileGridSize);
    }
  }
}

/////////////////////////////////////////////////////////
// static member function
//
/////////////////////////////////////////////////////////
void pix_opencv_clahe :: obj_setupCallback(t_class *classPtr)
{
  CPPEXTERN_MSG1(classPtr, "clipLimit",   clipLimitMess, t_float);
  CPPEXTERN_MSG2(classPtr, "tileGridSize",   tileGridSizeMess, int, int);
}
#endif /* HAVE_CLAHE */