pix_convolve.cpp 10.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
////////////////////////////////////////////////////////
//
// GEM - Graphics Environment for Multimedia
//
// zmoelnig@iem.kug.ac.at
//
// Implementation file
//
//    Copyright (c) 1997-1998 Mark Danks.
//    Copyright (c) Gnther Geiger.
//    Copyright (c) 2001-2002 IOhannes m zmoelnig. forum::fr::umlute. IEM
12
//    Copyright (c) 2002 James Tittle & Chris Clepper
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
//    For information on usage and redistribution, and for a DISCLAIMER OF ALL
//    WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution.
//
/////////////////////////////////////////////////////////

#include "pix_convolve.h"

CPPEXTERN_NEW_WITH_TWO_ARGS(pix_convolve, t_floatarg, A_DEFFLOAT, t_floatarg, A_DEFFLOAT)

/////////////////////////////////////////////////////////
//
// pix_convolve
//
/////////////////////////////////////////////////////////
// Constructor
//
/////////////////////////////////////////////////////////
pix_convolve :: pix_convolve(t_floatarg fRow, t_floatarg fCol)
31
  : m_matrix(NULL), m_imatrix(NULL)
32
{
33 34 35
  int row = (int)fRow;
  int col = (int)fCol;

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    if (!row || !col )
    {
    	error("GEM: pix_convolve: matrix must have some dimension");
    	return;
    }
    
    if (!(row % 2) || !(col % 2) )
    {
    	error("GEM: pix_convolve: matrix must have odd dimensions");
    	return;
    }
    
    m_rows = row;
    m_cols = col;
    m_range = 1.0;
51
    m_irange = 255;
52
    m_matrix = new float[m_rows * m_cols];
53
    m_imatrix = new signed short[m_rows * m_cols];
54 55

    // zero out the matrix
ggeiger's avatar
ggeiger committed
56 57
    int i;
    for (i = 0; i < m_cols * m_rows; i++) m_matrix[i] = 0.0;
dheck's avatar
dheck committed
58
    for (i = 0; i < m_cols * m_rows; i++) m_imatrix[i] = 0;
59 60
    // insert a one for the default center value (identity matrix)
    m_matrix[ ((m_cols / 2 + 1) * m_rows) + (m_rows / 2 + 1) ] = 1.0;
61
    m_imatrix[ ((m_cols / 2 + 1) * m_rows) + (m_rows / 2 + 1) ] = 255;
62 63 64 65 66 67 68 69 70 71 72
    
    inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("float"), gensym("ft1"));
    inlet_new(this->x_obj, &this->x_obj->ob_pd, gensym("list"), gensym("matrix"));
}

/////////////////////////////////////////////////////////
// Destructor
//
/////////////////////////////////////////////////////////
pix_convolve :: ~pix_convolve()
{
73 74 75 76 77
  post("convolution...%x %x", m_matrix, m_imatrix);
    if (m_matrix)delete [] m_matrix;
    if (m_imatrix)delete [] m_imatrix;
    post("done...");
  
78 79 80 81 82 83
}

/////////////////////////////////////////////////////////
// processImage
//
/////////////////////////////////////////////////////////
84

85 86
#ifndef MMX
#else
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
void pix_convolve :: calculate3x3(imageStruct &image,imageStruct &tempImg)
{
  int i;
  int j;
  int k;
  int xsize =  tempImg.xsize;
  int ysize =  tempImg.ysize;
  int size = xsize*ysize - xsize-1;

  int* src = (int*) tempImg.data;
  int* dest = (int*)image.data;
  
  //  MMXSTART;

  i = xsize;
  int* val1 = 0;
  int* val2 = src+i-xsize;
  int* val3 = src+i-xsize+1;
  int* val4 = src+i-1;
  int* val5 = src+i;
  int* val6 = src+i+1;
  int* val7 = src+i+xsize-1;
  int* val8 = src+i+xsize;
  int* val9 = src+i+xsize+1;
  int res;
  for (i=xsize+1;i<size;i++) {
    val1 = val2;
    val2 = val3;
    val3 = src+i-xsize+1;
    val4 = val5;
    val5 = val6;
    val6 = src+i+1;
    val7 = val8;
    val8 = val9;
    val9 = src+i+xsize+1;
    if (i%xsize == 0 || i%xsize == xsize-1) continue;
    for (j=0;j<3;j++) {
      res = m_imatrix[0]*(int)((unsigned char*)val1)[j];
      res += m_imatrix[1]*(int)((unsigned char*)val2)[j];
      res += m_imatrix[2]*(int)((unsigned char*)val3)[j];
      res += m_imatrix[3]*(int)((unsigned char*)val4)[j];
      res += m_imatrix[4]*(int)((unsigned char*)val5)[j];
      res += m_imatrix[5]*(int)((unsigned char*)val6)[j];
      res += m_imatrix[6]*(int)((unsigned char*)val7)[j];
      res += m_imatrix[7]*(int)((unsigned char*)val8)[j];
      res += m_imatrix[8]*(int)((unsigned char*)val9)[j];
      res*=m_irange;
      res>>=16;
      ((unsigned char*)dest)[i*4+j] = CLAMP(res);
    }

  }

  //  MMXDONE;
}
142
#endif
143 144 145


#define MMULT(a,b) (a*b>>8)
146 147
void pix_convolve :: processImage(imageStruct &image)
{
148
    image.copy2Image(&tempImg);
149 150 151 152 153 154
    int initX = m_rows / 2;
    int initY = m_cols / 2;
    int maxX = tempImg.xsize - initX;
    int maxY = tempImg.ysize - initY;
    int xTimesc = tempImg.xsize * tempImg.csize;
    int initOffset = initY * xTimesc + initX * tempImg.csize;
155 156 157 158 159 160 161 162 163 164 165 166
#ifndef MMX
    for (int y = initY; y < maxY; y++)
    {
        int realY = y * xTimesc;
        int offsetY = realY - initOffset;

    	for (int x = initX; x < maxX; x++)
    	{
    	    int realPos = x * tempImg.csize + realY;
            int offsetXY = x * tempImg.csize + offsetY;

    	    // skip the alpha value
cclepper's avatar
cclepper committed
167
            #ifndef MACOSX
168
    	    for (int c = 0; c < 3; c++)
cclepper's avatar
cclepper committed
169 170 171
            #else
              for (int c = 1; c < 4; c++)
            #endif
172 173 174 175 176 177 178 179 180
    	    {
    		    int new_val = 0;
                int offsetXYC = offsetXY + c;
    		    for (int matY = 0; matY < m_cols; matY++)
    		    {
    		        int offsetXYCMat = matY * xTimesc + offsetXYC;
    		        int realMatY = matY * m_rows;
    	    	    for (int matX = 0; matX < m_rows; matX++)
    	    	    {
cclepper's avatar
cclepper committed
181 182
                        new_val += (tempImg.data[offsetXYCMat + matX * tempImg.csize] *
                                        m_imatrix[realMatY + matX])>>8;
183 184
    	    	    }
    		    }
cclepper's avatar
cclepper committed
185 186 187
    		    //image.data[realPos + c] = CLAMP(new_val/m_range);
                    image.data[realPos + c] = CLAMP(new_val);  //removes insult from injury

188 189 190 191 192
    	    }
    	}
    }
    //delete [] tempImg.data;
#else
193 194
    if (m_rows == 3 && m_cols == 3) {
      calculate3x3(image,tempImg);
195
    }
196 197 198 199 200 201 202 203 204 205 206 207 208
    else {
      for (int y = initY; y < maxY; y++)
	{
	  int realY = y * xTimesc;
	  int offsetY = realY - initOffset;
	  
	  for (int x = initX; x < maxX; x++)
	    {
	      int realPos = x * tempImg.csize + realY;
	      int offsetXY = x * tempImg.csize + offsetY;
	      
	      // skip the alpha value
	      for (int c = 0; c < 3; c++)
cclepper's avatar
cclepper committed
209
              
210 211 212 213 214 215 216 217 218 219 220
		{
		  int new_val = 0;
		  int offsetXYC = offsetXY + c;
		  for (int matY = 0; matY < m_cols; matY++)
		    {
		      int offsetXYCMat = matY * xTimesc + offsetXYC;
		      int realMatY = matY * m_rows;
		      for (int matX = 0; matX < m_rows; matX++)
			{
			  new_val += MMULT(tempImg.data[offsetXYCMat + matX * tempImg.csize],
					   m_imatrix[realMatY + matX]);
cclepper's avatar
cclepper committed
221
                                
222 223 224
			}
		    }
		  image.data[realPos + c] = CLAMP(new_val/m_range);
cclepper's avatar
cclepper committed
225
                 
226 227 228 229
		}
	    }
	}
    }
230
#endif
231 232
}

233 234
void pix_convolve :: processYUVImage(imageStruct &image)
{
cclepper's avatar
cclepper committed
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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
     image.copy2Image(&tempImg);
     //float range = 1;
    int initX = m_rows / 2;
    int initY = m_cols / 2;
    int maxX = tempImg.xsize - initX;
    int maxY = tempImg.ysize - initY;
    int xTimesc = tempImg.xsize * tempImg.csize;
    int initOffset = initY * xTimesc + initX * tempImg.csize;
    
    if (m_chroma) {
    
    for (int y = initY; y < maxY; y++)
    {
        int realY = y * xTimesc;
        int offsetY = realY - initOffset;

    	for (int x = initX; x < maxX; x++)
    	{
    	    int realPos = x * tempImg.csize + realY;
            int offsetXY = x * tempImg.csize + offsetY;

    	    // skip the UV
    	    for (int c = 1; c < 3; c+=2)
    	    {
    		    int new_val = 0;
                int offsetXYC = offsetXY + c;
    		    for (int matY = 0; matY < m_cols; matY++)
    		    {
    		        int offsetXYCMat = matY * xTimesc + offsetXYC;
    		        int realMatY = matY * m_rows;
    	    	    for (int matX = 0; matX < m_rows; matX++)
    	    	    {
                      //new_val += (int)(tempImg.data[offsetXYCMat + matX * tempImg.csize] *
                      //                  m_matrix[realMatY + matX]);
                      new_val += (tempImg.data[offsetXYCMat + matX * tempImg.csize] *
                                        m_imatrix[realMatY + matX])>>8;
    	    	    }
    		    }
    		   // image.data[realPos + c] = CLAMP(new_val/m_range);
                   image.data[realPos + c] = CLAMP(new_val);
                   // image.data[realPos + c-1] = 128;  //remove the U+V
    	    }
    	}
    }
    }else{
    for (int y = initY; y < maxY; y++)
    {
        int realY = y * xTimesc;
        int offsetY = realY - initOffset;

    	for (int x = initX; x < maxX; x++)
    	{
    	    int realPos = x * tempImg.csize + realY;
            int offsetXY = x * tempImg.csize + offsetY;

    	    // skip the UV
    	    for (int c = 1; c < 3; c+=2)
    	    {
    		    int new_val = 0;
                int offsetXYC = offsetXY + c;
    		    for (int matY = 0; matY < m_cols; matY++)
    		    {
    		        int offsetXYCMat = matY * xTimesc + offsetXYC;
    		        int realMatY = matY * m_rows;
    	    	    for (int matX = 0; matX < m_rows; matX++)
    	    	    {
                      //new_val += (int)(tempImg.data[offsetXYCMat + matX * tempImg.csize] *
                      //                  m_matrix[realMatY + matX]);
                      new_val += (tempImg.data[offsetXYCMat + matX * tempImg.csize] *
                                        m_imatrix[realMatY + matX])>>8;
    	    	    }
    		    }
    		   // image.data[realPos + c] = CLAMP(new_val/m_range);
                   image.data[realPos + c] = CLAMP(new_val);
                    image.data[realPos + c-1] = 128;  //remove the U+V
    	    }
    	}
    }
    }
   
315
}
316 317 318 319 320 321 322
/////////////////////////////////////////////////////////
// rangeMess
//
/////////////////////////////////////////////////////////
void pix_convolve :: rangeMess(float range)
{
    m_range = range;
323
    m_irange = (int)(range*255.f);
324 325 326 327 328 329 330 331 332 333 334 335 336 337
    setPixModified();
}

/////////////////////////////////////////////////////////
// matrixMess
//
/////////////////////////////////////////////////////////
void pix_convolve :: matrixMess(int argc, t_atom *argv)
{
    if (argc != m_cols * m_rows)
    {
    	error("GEM: pix_convolve: matrix size not correct");
    	return;
    }
cclepper's avatar
cclepper committed
338

ggeiger's avatar
ggeiger committed
339 340
    int i;
    for (i = 0; i < argc; i++) m_matrix[i] = atom_getfloat(&argv[i]);
dheck's avatar
dheck committed
341
    for (i = 0; i < argc; i++) m_imatrix[i] = (int)(atom_getfloat(&argv[i])*255.);
342

cclepper's avatar
cclepper committed
343

344 345 346 347 348 349 350 351 352 353 354 355 356
    setPixModified();
}

/////////////////////////////////////////////////////////
// static member function
//
/////////////////////////////////////////////////////////
void pix_convolve :: obj_setupCallback(t_class *classPtr)
{
    class_addmethod(classPtr, (t_method)&pix_convolve::matrixMessCallback,
    	    gensym("matrix"), A_GIMME, A_NULL);
    class_addmethod(classPtr, (t_method)&pix_convolve::rangeMessCallback,
    	    gensym("ft1"), A_FLOAT, A_NULL);
cclepper's avatar
cclepper committed
357 358
    class_addmethod(classPtr, (t_method)&pix_convolve::chromaMessCallback,
    	    gensym("chroma"), A_FLOAT, A_NULL);
359 360 361 362 363 364 365 366 367
}
void pix_convolve :: matrixMessCallback(void *data, t_symbol *, int argc, t_atom *argv)
{
    GetMyClass(data)->matrixMess(argc, argv);
}
void pix_convolve :: rangeMessCallback(void *data, t_floatarg range)
{
    GetMyClass(data)->rangeMess((float)range);
}
cclepper's avatar
cclepper committed
368 369 370 371 372

void pix_convolve :: chromaMessCallback(void *data, t_floatarg value)
{
    GetMyClass(data)->m_chroma=(int)value;
}