aboutsummaryrefslogtreecommitdiff
path: root/pix_opencv_colorfilt.cc
blob: d977e0a607eea8e41fcc258460122401c45fec43 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
////////////////////////////////////////////////////////
//
// 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.
//
/////////////////////////////////////////////////////////

#include "pix_opencv_colorfilt.h"
#include "g_canvas.h"

CPPEXTERN_NEW(pix_opencv_colorfilt)

/////////////////////////////////////////////////////////
//
// pix_opencv_colorfilt
//
/////////////////////////////////////////////////////////
// Constructor
//
/////////////////////////////////////////////////////////
pix_opencv_colorfilt :: pix_opencv_colorfilt()
{ 
  inlet_new(this->x_obj, &this->x_obj->ob_pd, &s_float, gensym("R"));
  inlet_new(this->x_obj, &this->x_obj->ob_pd, &s_float, gensym("G"));
  inlet_new(this->x_obj, &this->x_obj->ob_pd, &s_float, gensym("B"));

  x_R  = outlet_new(this->x_obj, &s_float);
  x_G  = outlet_new(this->x_obj, &s_float);
  x_B  = outlet_new(this->x_obj, &s_float);

  x_colorR = 128;
  x_colorG = 128;
  x_colorB = 128;

  outlet_float( x_R, x_colorR );
  outlet_float( x_G, x_colorG );
  outlet_float( x_B, x_colorB );

  comp_xsize=320;
  comp_ysize=240;

  x_tolerance = 50;

  x_canvas = canvas_getcurrent();

  rgba = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 4);
  rgb = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 3);
  brgb = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 4);
}

/////////////////////////////////////////////////////////
// Destructor
//
/////////////////////////////////////////////////////////
pix_opencv_colorfilt :: ~pix_opencv_colorfilt()
{ 
   //Destroy cv_images to clean memory
   cvReleaseImage(&rgba);
   cvReleaseImage(&rgb);
   cvReleaseImage(&brgb);
}

void pix_opencv_colorfilt :: drawColor()
{
 int width, height;
 char color[32];

    sprintf( color, "#%.2X%.2X%.2X", x_colorR, x_colorG, x_colorB );
    width = rtext_width( glist_findrtext( (t_glist*)x_canvas, (t_text *)this->x_obj ) );
    height = rtext_height( glist_findrtext( (t_glist*)x_canvas, (t_text *)this->x_obj ) );
    sys_vgui((char*)".x%x.c delete rectangle %xCOLOR\n", x_canvas, this->x_obj );
    sys_vgui((char*)".x%x.c create rectangle %d %d %d %d -fill %s -tags %xCOLOR\n",
             x_canvas, this->x_obj->te_xpix+width+5, this->x_obj->te_ypix,
             this->x_obj->te_xpix+width+height+5,
             this->x_obj->te_ypix+height, color, this->x_obj );
}

/////////////////////////////////////////////////////////
// processImage
//
/////////////////////////////////////////////////////////
void pix_opencv_colorfilt :: processRGBAImage(imageStruct &image)
{
  int px,py;
  unsigned char r,g,b;
  int diff; 

  if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!rgba)) {

	comp_xsize = image.xsize;
	comp_ysize = image.ysize;

    	//Destroy cv_images to clean memory
        if ( rgba )
        {
	  cvReleaseImage(&rgba);
    	  cvReleaseImage(&rgb);
    	  cvReleaseImage(&brgb);
        }

	//create the orig image with new size
        rgba = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4);
        rgb = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3);
    	brgb = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4);
    }
    memcpy( rgba->imageData, image.data, image.xsize*image.ysize*4 );
    cvCopy(rgba, brgb);

    for( py=0; py<rgba->height; py++ ) {
      for( px=0; px<rgba->width; px++ ) {
#ifdef __APPLE__
        g = ((uchar*)(rgba->imageData + rgba->widthStep*(int)py))[(int)px*4];
        r = ((uchar*)(rgba->imageData + rgba->widthStep*(int)py))[(int)px*4+1];
        b = ((uchar*)(rgba->imageData + rgba->widthStep*(int)py))[(int)px*4+3];
#else 
        r = ((uchar*)(rgba->imageData + rgba->widthStep*(int)py))[(int)px*4];
        g = ((uchar*)(rgba->imageData + rgba->widthStep*(int)py))[(int)px*4+1];
        b = ((uchar*)(rgba->imageData + rgba->widthStep*(int)py))[(int)px*4+2];
#endif

        diff = 0;
        diff = abs(r-x_colorR );
        diff += abs(g-x_colorG );
        diff += abs(b-x_colorB );
        diff = diff/3;

        if ( diff > x_tolerance )
        {
           (rgba->imageData + rgba->widthStep*(int)py)[(int)px*4] = 0x0;
           (rgba->imageData + rgba->widthStep*(int)py)[(int)px*4+1] = 0x0;
           (rgba->imageData + rgba->widthStep*(int)py)[(int)px*4+2] = 0x0;
           (rgba->imageData + rgba->widthStep*(int)py)[(int)px*4+3] = 0x0;
        }
      }
    }
  
    //copy back the processed frame to image
    memcpy( image.data, rgba->imageData, image.xsize*image.ysize*4 );
}

void pix_opencv_colorfilt :: processRGBImage(imageStruct &image)
{
  unsigned char r,g,b;
  int diff; 
  int px, py;

  if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!rgb)) {

	this->comp_xsize = image.xsize;
	this->comp_ysize = image.ysize;

    	//Destroy cv_images to clean memory
        if ( rgb )
        {
	  cvReleaseImage(&rgba);
    	  cvReleaseImage(&rgb);
    	  cvReleaseImage(&brgb);
        }

	//create the orig image with new size
        rgba = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4);
        rgb = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3);
    	brgb = cvCreateImage(cvSize(rgb->width,rgb->height), IPL_DEPTH_8U, 4);
    
    }
    memcpy( rgb->imageData, image.data, image.xsize*image.ysize*3 );
    cvCopy(rgb, brgb);
    
    for( py=0; py<rgb->height; py++ ) {
      for( px=0; px<rgb->width; px++ ) {
        b = ((uchar*)(rgb->imageData + rgb->widthStep*(int)py))[(int)px*3];
        g = ((uchar*)(rgb->imageData + rgb->widthStep*(int)py))[(int)px*3+1];
        r = ((uchar*)(rgb->imageData + rgb->widthStep*(int)py))[(int)px*3+2];

        diff = 0;
        diff = abs(r-x_colorR );
        diff += abs(g-x_colorG );
        diff += abs(b-x_colorB );

        if ( diff > x_tolerance )
        {
           (rgb->imageData + rgb->widthStep*(int)py)[(int)px*3] = 0x0;
           (rgb->imageData + rgb->widthStep*(int)py)[(int)px*3+1] = 0x0;
           (rgb->imageData + rgb->widthStep*(int)py)[(int)px*3+2] = 0x0;
        }
      }
    }
  
    memcpy( image.data, rgb->imageData, image.xsize*image.ysize*3 );
}

void pix_opencv_colorfilt :: processYUVImage(imageStruct &image)
{
  post( "pix_opencv_colorfilt : yuv format not supported" );
}
    	
void pix_opencv_colorfilt :: processGrayImage(imageStruct &image)
{ 
  post( "pix_opencv_colorfilt : gray format not supported" );
}

void pix_opencv_colorfilt :: floatToleranceMess (float tolerance)
{
  if ( (int)tolerance>0 ) x_tolerance = (int)tolerance;
}

void pix_opencv_colorfilt :: floatRMess (float r)
{
  if ( ( (int)r>=0 ) && ( (int)r<=255 ) ) x_colorR = (int)r;
  if (glist_isvisible(x_canvas)) drawColor();
}

void pix_opencv_colorfilt :: floatGMess (float g)
{
  if ( ( (int)g>=0 ) && ( (int)g<=255 ) ) x_colorG = (int)g;
  if (glist_isvisible(x_canvas)) drawColor();
}

void pix_opencv_colorfilt :: floatBMess (float b)
{
  if ( ( (int)b>=0 ) && ( (int)b<=255 ) ) x_colorB = (int)b;
  if (glist_isvisible(x_canvas)) drawColor();
}

void pix_opencv_colorfilt :: pickMess (float xcur, float ycur)
{
   if ( ( xcur >= 0. ) && ( xcur <= comp_xsize )
        && ( ycur > 0. ) && ( ycur < comp_ysize ) )
   {
#ifdef __APPLE__
      x_colorR = ((uchar*)(brgb->imageData + brgb->widthStep*(int)ycur))[(int)xcur*4+1];
      x_colorG = ((uchar*)(brgb->imageData + brgb->widthStep*(int)ycur))[(int)xcur*4+2];
      x_colorB = ((uchar*)(brgb->imageData + brgb->widthStep*(int)ycur))[(int)xcur*4+3];
#else
      //ycur = brgb->height - ycur;
      x_colorR = ((uchar*)(brgb->imageData + brgb->widthStep*(int)ycur))[(int)xcur*4];
      x_colorG = ((uchar*)(brgb->imageData + brgb->widthStep*(int)ycur))[(int)xcur*4+1];
      x_colorB = ((uchar*)(brgb->imageData + brgb->widthStep*(int)ycur))[(int)xcur*4+2];
#endif
      outlet_float( x_R, x_colorR );
      outlet_float( x_G, x_colorG );
      outlet_float( x_B, x_colorB );

      if (glist_isvisible(x_canvas)) drawColor();
   }
}

/////////////////////////////////////////////////////////
// static member function
//
/////////////////////////////////////////////////////////
void pix_opencv_colorfilt :: obj_setupCallback(t_class *classPtr)
{
  class_addmethod(classPtr, (t_method)&pix_opencv_colorfilt::floatToleranceMessCallback,
  		  gensym("tolerance"), A_FLOAT, A_NULL);
  class_addmethod(classPtr, (t_method)&pix_opencv_colorfilt::floatRMessCallback,
  		  gensym("R"), A_FLOAT, A_NULL);
  class_addmethod(classPtr, (t_method)&pix_opencv_colorfilt::floatGMessCallback,
  		  gensym("G"), A_FLOAT, A_NULL);
  class_addmethod(classPtr, (t_method)&pix_opencv_colorfilt::floatBMessCallback,
  		  gensym("B"), A_FLOAT, A_NULL);
  class_addmethod(classPtr, (t_method)&pix_opencv_colorfilt::pickMessCallback,
  		  gensym("pick"), A_FLOAT, A_FLOAT, A_NULL);
}

void pix_opencv_colorfilt :: floatToleranceMessCallback(void *data, t_floatarg tolerance)
{
  GetMyClass(data)->floatToleranceMess((float)tolerance);
}

void pix_opencv_colorfilt :: floatRMessCallback(void *data, t_floatarg r)
{
  GetMyClass(data)->floatRMess((float)r);
}

void pix_opencv_colorfilt :: floatGMessCallback(void *data, t_floatarg g)
{
  GetMyClass(data)->floatGMess((float)g);
}

void pix_opencv_colorfilt :: floatBMessCallback(void *data, t_floatarg b)
{
  GetMyClass(data)->floatBMess((float)b);
}

void pix_opencv_colorfilt :: pickMessCallback(void *data, t_floatarg xcur, t_floatarg ycur)
{
  GetMyClass(data)->pickMess((float)xcur,(float)ycur);
}