mxj: max crashes

David Bangerter's icon

David Bangerter

Jun 27, 2019, 10:08 AM

Hi, I'm using mxj jitter java for the first time and there seems to be a problem in my code. The goal is, to get the brightness factor of all surrounding pixels of each pixel and calculating a new redness value for the pixel. So it should resemble a blurring effect only in red tones. I'm using the object.copyMatrixToArray(array) function. But I can't see where the problem is. The compiling of the code works, but as soon as I feed a jitter-matrix into my mxj-object, max crashes completely. Can anyone help me?

import com.cycling74.max.*;
import com.cycling74.jitter.*;

public class slope extends MaxObject
{
    JitterMatrix output = new JitterMatrix(4,"char",512,512);

    float brightness; //brightness of individual pixel
    float[][] pixSlope; //Array of slopes
    int[] slpCol; //color associated with slope
    float[][] freqFilt; //Array of values for each column
    public int[] pixels; //Input-Matrix as Array

    public int width;
    public int height;
    public int planecount = 4;

    float freq;
    float qFact;
    float sampRate = 44100;
    
    public slope(float gain)
    {
        declareIO(1,2);
        setInletAssist(0, "input (matrix)");
        setOutletAssist(0, "output (matrix)");
        setOutletAssist(1, "output (filter-list)");
    }
    
/**    
    public void bang() {
        jit_matrix(output.getName());
    }
**/

    public void jit_matrix(String mname)
    {
        output.frommatrix(mname);

        int dim[] = output.getDim();
        int width = dim[0];
        int height = dim[1];

//          planecount = output.getPlanecount();
          pixels = new int[width*height*planecount];
        
        output.copyMatrixToArray(pixels);

          freqFilt = new float[width][height*2]; //setting up array of filter values

          pixSlope = new float[width][height]; //setting up slope array
        for(int i = 0; i < width; i++) {
             for(int j = 0; j < height; j++) {
                pixSlope[i][j] = 0;
                   for(int xoffset = 0; xoffset < 3; xoffset++) {
                     for(int yoffset = 0; yoffset < 3; yoffset++) {
                        if(xoffset == 1 && yoffset == 1) {
                            continue;
                        }
                           pixSlope[i][j] += bri(i+xoffset-1, j+yoffset-1);
                     }
                   }
                   pixSlope[i][j] = pixSlope[i][j]/8;
             }
        }
        
        for(int x = 0; x < width; x++) { //iterating through image to color each pixel
             for(int y = 0; y < height; y++) {
                  slpCol = new int[]{255,(int)(pixSlope[x][y]*255),0,0};
                for(int offset = 0; offset < planecount; offset++) {
                    pixels[(x+y*width)*planecount+offset] = slpCol[offset];
                }
      
                  freq = (float)(y/(height-1)*sampRate/2);
                  qFact = pixSlope[x][y];

                  freqFilt[x][y*2] = freq;
                  freqFilt[x][y*2+1] = qFact;
             }
        }
        
          output.copyArrayToMatrix(pixels);
        outlet(0, "jit_matrix", output.getName());
    }

    public void getFilt(int index) 
    {
        outlet(1, "freqFilt", freqFilt[index]);
    }

    public float bri(int x, int y) //function to return brightness of pixel
    { 
        int[] pixCol = new int[4];

          if(x < 0) {
            x = width-1;
          } 
          if(x > width-1) {
            x = 0;
         } 
         if(y < 0) {
          y = height-1;
         } 
         if(y > height-1) {
          y = 0;
          }

        for (int k = 0; k < planecount; k++) {
            pixels[(x+y*width)*planecount+k] = pixCol[k];
        }
          
          brightness = Math.max(Math.max(pixCol[1], pixCol[2]), pixCol[3]);
          brightness = brightness/255;
          return brightness;
    }

}