pix_convolve.cpp 13.2 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_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
    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;
50 51
    m_irange = 255;
    m_imatrix = new signed short[m_rows * m_cols];
52 53

    // zero out the matrix
ggeiger's avatar
ggeiger committed
54
    int i;
dheck's avatar
dheck committed
55
    for (i = 0; i < m_cols * m_rows; i++) m_imatrix[i] = 0;
56
    // insert a one for the default center value (identity matrix)
57
    m_imatrix[ ((m_cols / 2 + 1) * m_rows) + (m_rows / 2 + 1) ] = 255;
58 59 60 61 62 63 64 65 66 67 68
    
    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()
{
69 70 71
    if (m_imatrix)delete [] m_imatrix;
    post("done...");
  
72 73 74 75 76 77
}

/////////////////////////////////////////////////////////
// processImage
//
/////////////////////////////////////////////////////////
78

cclepper's avatar
cclepper committed
79

80
void pix_convolve :: calculateRGBA3x3(imageStruct &image,imageStruct &tempImg)
81 82 83 84 85 86 87
{
  int i;
  int j;
  int k;
  int xsize =  tempImg.xsize;
  int ysize =  tempImg.ysize;
  int size = xsize*ysize - xsize-1;
88
  int csize = tempImg.csize;
89 90 91 92 93

  int* src = (int*) tempImg.data;
  int* dest = (int*)image.data;
  
  //  MMXSTART;
cclepper's avatar
cclepper committed
94
//unroll this to do R G B in one pass?? (too many registers?)
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  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;
cclepper's avatar
cclepper committed
117 118 119 120 121 122
    #ifndef MACOSX
    for (j=0;j<3;j++) 
    #else
    for (j=1;j<4;j++)
    #endif
    {
123 124 125 126 127 128 129 130 131 132 133
      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;
134
      ((unsigned char*)dest)[i*csize+j] = CLAMP(res);
135 136 137 138
    }

  }

cclepper's avatar
cclepper committed
139
  
140 141
}

142 143
void pix_convolve :: processImage(imageStruct &image)
{
144
    image.copy2Image(&tempImg);
145 146 147 148 149 150
    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;
151 152


cclepper's avatar
cclepper committed
153

154 155 156 157 158
    if (m_rows == 3 && m_cols == 3 && tempImg.csize == 4) {
      calculateRGBA3x3(image,tempImg);
      return;
    }

159 160 161 162 163 164 165
    for (int y = initY; y < maxY; y++)
    {
        int realY = y * xTimesc;
        int offsetY = realY - initOffset;

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

    	    // skip the alpha value
171 172

	    for (int c = 1; c < csize; c++)
173 174
    	    {
    		    int new_val = 0;
175
		    int offsetXYC = offsetXY + c;
176 177 178 179 180 181
    		    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++)
    	    	    {
182
                        new_val += (tempImg.data[offsetXYCMat + matX * csize] *
cclepper's avatar
cclepper committed
183
                                        m_imatrix[realMatY + matX])>>8;
184 185
    	    	    }
    		    }
186 187 188
                    image.data[realPos + c] = CLAMP(new_val);  
		    //removes insult from injury ??
		    // we do not use the m_irange anymore ...  remove it ??
cclepper's avatar
cclepper committed
189

190 191 192
    	    }
    	}
    }
cclepper's avatar
cclepper committed
193

194 195
}

196

197 198
void pix_convolve :: processYUVImage(imageStruct &image)
{
cclepper's avatar
cclepper committed
199 200 201 202 203 204 205 206 207
     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;
    
cclepper's avatar
cclepper committed
208 209 210 211 212
 //   calculate3x3YUV(image,tempImg);
    if (m_rows == 3 && m_cols == 3) {
      calculate3x3YUV(image,tempImg);
      return;
    }
cclepper's avatar
cclepper committed
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
    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 += (tempImg.data[offsetXYCMat + matX * tempImg.csize] *
                                        m_imatrix[realMatY + matX])>>8;
    	    	    }
    		    }
                   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 += (tempImg.data[offsetXYCMat + matX * tempImg.csize] *
                                        m_imatrix[realMatY + matX])>>8;
    	    	    }
    		    }
                   image.data[realPos + c] = CLAMP(new_val);
                    image.data[realPos + c-1] = 128;  //remove the U+V
    	    }
    	}
    }
    }
   
278
}
cclepper's avatar
cclepper committed
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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

void pix_convolve :: calculate3x3YUV(imageStruct &image,imageStruct &tempImg)
{
  int i;
  int j;
  int k;
  int xsize =  tempImg.xsize;
  int ysize =  tempImg.ysize;
  int size = xsize*ysize - xsize-1;

  short* src = (short*) tempImg.data;
  short* dest = (short*)image.data;
  
 
if (m_chroma){
//post("pix_convolve: new YUV method");
  i = xsize;
  //make these temp register vars rather than pointers?
  /*short* val1 = 0;  
  short* val2 = src+i-xsize; //val2 = src[i-xsize];
  short* val3 = src+i-xsize+1; //val3 = src[i-xsize+1];
  short* val4 = src+i-1; //val4 = src[i-1];
  short* val5 = src+i; //val5 = src[i];
  short* val6 = src+i+1; //val6 = src[i+1];
  short* val7 = src+i+xsize-1; //val7 = src[i+xsize-1];
  short* val8 = src+i+xsize; //val8 = src[i+xsize];
  short* val9 = src+i+xsize+1; //val9 = src[i+xsize+1];*/
  register unsigned char val1 = 0;  
  register unsigned char val2 = src[i-xsize+1]; 
  register unsigned char val3 = src[i-xsize+3];
  register unsigned char val4 = src[i-1];
  register unsigned char val5 = src[i+1];
  register unsigned char val6 = src[i+3];
  register unsigned char val7 = src[i+xsize-1];
  register unsigned char val8 = src[i+xsize+1];
  register unsigned char val9 = src[i+xsize+3];
  int res;

  
  //unroll this 2x to fill the registers? (matrix*y1*y2= 9*9*9 =27)
  for (i=xsize+1;i<size;i++) {
  //load furthest value first...the rest should be in cache
    val7 = val8;
    val8 = val9;
    val9 = src[i+xsize+3]; //should be in cache from previous pass
    val1 = val2;
    val2 = val3;
    val3 = src[i-xsize+3]; //should be in cache from previous pass
    val4 = val5;
    val5 = val6;
    val6 = src[i+3];
    
    if (i%xsize == 0 || i%xsize == xsize-1) continue;
   /* #ifndef MACOSX
    for (j=0;j<3;j++) 
    #else
    for (j=1;j<3;j+=2)
    #endif
    { */
    //use separate temp vars here??
    //register the matrix values as separate vars??
      res = m_imatrix[0]*(int)((unsigned char)val1);
      res += m_imatrix[1]*(int)((unsigned char)val2);
      res += m_imatrix[2]*(int)((unsigned char)val3);
      res += m_imatrix[3]*(int)((unsigned char)val4);
      res += m_imatrix[4]*(int)((unsigned char)val5);
      res += m_imatrix[5]*(int)((unsigned char)val6);
      res += m_imatrix[6]*(int)((unsigned char)val7);
      res += m_imatrix[7]*(int)((unsigned char)val8);
      res += m_imatrix[8]*(int)((unsigned char)val9);
      res*=m_irange;
      res>>=16;
      ((unsigned char*)dest)[i*2+1] = CLAMP(res);
   // }
  } 
  }else{
  
  i = xsize;
  //make these temp register vars rather than pointers?
  short* val1 = 0;  
  short* val2 = src+i-xsize; //val2 = src[i-xsize];
  short* val3 = src+i-xsize+1; //val3 = src[i-xsize+1];
  short* val4 = src+i-1; //val4 = src[i-1];
  short* val5 = src+i; //val5 = src[i];
  short* val6 = src+i+1; //val6 = src[i+1];
  short* val7 = src+i+xsize-1; //val7 = src[i+xsize-1];
  short* val8 = src+i+xsize; //val8 = src[i+xsize];
  short* val9 = src+i+xsize+1; //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;
    #ifndef MACOSX
    for (j=0;j<3;j++) 
    #else
    for (j=1;j<3;j+=2)
    #endif
    {
      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*2] = 128;
      ((unsigned char*)dest)[i*2+2] = 128;
      ((unsigned char*)dest)[i*2+j] = CLAMP(res);
    }
  }
  }
}


405 406 407 408 409 410
/////////////////////////////////////////////////////////
// rangeMess
//
/////////////////////////////////////////////////////////
void pix_convolve :: rangeMess(float range)
{
411
    m_irange = (int)(range*255.f);
412 413 414 415 416 417 418 419 420 421 422 423 424 425
    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
426

ggeiger's avatar
ggeiger committed
427
    int i;
dheck's avatar
dheck committed
428
    for (i = 0; i < argc; i++) m_imatrix[i] = (int)(atom_getfloat(&argv[i])*255.);
429

cclepper's avatar
cclepper committed
430

431 432 433 434 435 436 437 438 439 440 441 442 443
    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
444 445
    class_addmethod(classPtr, (t_method)&pix_convolve::chromaMessCallback,
    	    gensym("chroma"), A_FLOAT, A_NULL);
446 447 448 449 450 451 452 453 454
}
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
455 456 457 458 459

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