pix_convolve.cpp 7.63 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 167 168 169 170 171 172 173 174 175 176
#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
    	    for (int c = 0; c < 3; c++)
    	    {
    		    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++)
    	    	    {
zmoelnig's avatar
zmoelnig committed
177 178
                        new_val += (int)(tempImg.data[offsetXYCMat + matX * tempImg.csize] *
                                        m_matrix[realMatY + matX]);
179 180 181 182 183 184 185 186
    	    	    }
    		    }
    		    image.data[realPos + c] = CLAMP(new_val/m_range);
    	    }
    	}
    }
    //delete [] tempImg.data;
#else
187 188
    if (m_rows == 3 && m_cols == 3) {
      calculate3x3(image,tempImg);
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
    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++)
		{
		  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]);
			}
		    }
		  image.data[realPos + c] = CLAMP(new_val/m_range);
		}
	    }
	}
    }
221
#endif
222 223
}

224 225 226
void pix_convolve :: processYUVImage(imageStruct &image)
{
    post("pix_convolve: YUV not yet implemented :-(");
zmoelnig's avatar
zmoelnig committed
227
    post("\tbut really :: why ?");
228
}
229 230 231 232 233 234 235
/////////////////////////////////////////////////////////
// rangeMess
//
/////////////////////////////////////////////////////////
void pix_convolve :: rangeMess(float range)
{
    m_range = range;
236
    m_irange = (int)(range*255.f);
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    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;
    }
    
ggeiger's avatar
ggeiger committed
252 253
    int i;
    for (i = 0; i < argc; i++) m_matrix[i] = atom_getfloat(&argv[i]);
dheck's avatar
dheck committed
254
    for (i = 0; i < argc; i++) m_imatrix[i] = (int)(atom_getfloat(&argv[i])*255.);
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

    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);
}
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);
}