[help with RGB channel shifting on processing]

Home Forums Processing [help with RGB channel shifting on processing]

This topic contains 5 replies, has 2 voices, and was last updated by  Phil 1 year, 4 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #401

    Dennis Pacheco
    Participant

    I’m having trouble saving some images and even running the process on some others. It’s an error related to the pixel array.
    As a total beginner (or n00b) I don’t know how to program with processing yet, so I pretty much followed the tutorial and would start experimenting with the options later on. But I’m having trouble saving the glitched images and even showing previews and running the process on some others.
    I’m posting the script and the error.
    Thanks for the help, for the tutorials and the space!

    /*
    
     Channel Shift Image Glitching Processing
     datamoshing.com | 2016
     
     */
    
    PImage sourceImg;
    PImage targetImg;
    
    boolean glitchComplete = false;
    boolean glitchSaved = false;
    
    // script settings
    
    // image path is relative to sketch directory
    String imgFileName = "72a9dd40-8c74-4a80-9072-1576f2e4c289";
    String fileType = "jpg";
    
    // repeat the process this many times
    int iterations = 3;
    
    // use result image as new source for iterations
    boolean recursiveIterations = false;
    
    // shift the image vertically true/false
    boolean shiftVertically = true;
    
    // shift the image horizontally true/false
    boolean shiftHorizontally = true;
    
    void setup() {
      // load images into PImage variables
      targetImg = loadImage(imgFileName+"."+fileType);
      sourceImg = loadImage(imgFileName+"."+fileType);
      
      // use only numbers (not variables) for the size() command, Processing 3
      size(1, 1);
    
      // allow resize and update surface to image dimensions
      surface.setResizable(true);
      surface.setSize(targetImg.width, targetImg.height);
    
      // load image onto surface
      image(sourceImg, 0, 0);
    }
    
    void draw() { 
      if (!glitchComplete)
      {
        // load pixels
        sourceImg.loadPixels();
        targetImg.loadPixels();
      
        // repeat the process according to the iterations variable
        for(int i = 0;i < iterations;i++)
        {
          // generate random numbers for which channels to swap
          int sourceChannel = int(random(3));
          int targetChannel = int(random(3));
          
          // start with no horizontalShift
          int horizontalShift = 0; 
          
          // if shiftHorizontally is true, generate a random number to shift horizontally by
          if(shiftHorizontally)
            horizontalShift = int(random(width));
          
          // start with no verticalShift
          int verticalShift = 0;
          
          // if shiftVertically is true, generate a random number to shift vertically by
          if(shiftVertically)
            verticalShift = int(random(height));
          
          // shift the channel
          copyChannel(sourceImg.pixels, targetImg.pixels, verticalShift, horizontalShift, sourceChannel, targetChannel);
          
          // use the target as the new source for the next iteration
          if(recursiveIterations)
            sourceImg.pixels = targetImg.pixels;
        }
        
        // update the target pixels
        targetImg.updatePixels();
          
        // glitch is done, proceed to update surface and save
        glitchComplete = true;
    
        // load updated image onto surface
        image(targetImg, 0, 0);
      }
    
      if (glitchComplete && !glitchSaved) {
    
        // save surface
        saveFrame(imgFileName+"_glitched.png");
        glitchSaved = true;
        println("Glitched image saved");
        println("Click or press any key to exit...");
      }
    }
    
    void copyChannel(color[] sourcePixels, color[] targetPixels, int sourceY, int sourceX, int sourceChannel, int targetChannel)
    {
        // starting at the sourceY and pointerY loop through the rows
        for(int y = 0; y < height; y++)
        {   
            // add y counter to sourceY
            int sourceYOffset = sourceY + y;
            
            // wrap around the top of the image if we've hit the bottom
            if(sourceYOffset > height)
              sourceYOffset -= height;
                  
            // starting at the sourceX and pointerX loop through the pixels in this row
            for(int x = 0; x < width; x++)
            {
                // add x counter to sourceX
                int sourceXOffset = sourceX + x;
                
                // wrap around the left side of the image if we've hit the right side
                if(sourceXOffset > width)
                  sourceXOffset -= width;
    
                // get the color of the source pixel
                color sourcePixel = sourcePixels[sourceYOffset * width + sourceXOffset];
                
                // get the RGB values of the source pixel
                float sourceRed = red(sourcePixel);
                float sourceGreen = green(sourcePixel);
                float sourceBlue = blue(sourcePixel);
       
                // get the color of the target pixel
                color targetPixel = targetPixels[y * width + x]; 
    
                // get the RGB of the target pixel
                // two of the RGB channel values are required to create the new target color
                // the new target color is two of the target RGB channel values and one RGB channel value from the source
                float targetRed = red(targetPixel);
                float targetGreen = green(targetPixel);
                float targetBlue = blue(targetPixel);
                
                // create a variable to hold the new source RGB channel value
                float sourceChannelValue = 0;
                
                // assigned the source channel value based on sourceChannel random number passed in
                switch(sourceChannel)
                {
                  case 0:
                    // use red channel from source
                    sourceChannelValue = sourceRed;
                    break;
                  case 1:
                  // use green channel from source
                    sourceChannelValue = sourceGreen;
                    break;
                  case 2:
                  // use blue channel from source
                    sourceChannelValue = sourceBlue;
                    break;
                }
                
                // assigned the source channel value to a target channel based on targetChannel random number passed in
                switch(targetChannel)
                {
                  case 0:
                    // assign source channel value to target red channel
                    targetPixels[y * width + x] =  color(sourceChannelValue, targetGreen, targetBlue);
                    break;
                  case 1:
                  // assign source channel value to target green channel
                    targetPixels[y * width + x] =  color(targetRed, sourceChannelValue, targetBlue);
                    break;
                  case 2:
                    // assign source channel value to target blue channel
                    targetPixels[y * width + x] =  color(targetRed, targetGreen, sourceChannelValue);
                    break;
                }
    
            }
        }
    }
    
    void keyPressed() {
      if (glitchSaved)
      {
        System.exit(0);
      }
    }
    
    void mouseClicked() {
      if (glitchSaved)
      {
        System.exit(0);
      }
    }

    The error displayed is:
    ArrayIndexOutOfBondsException: 824320

    And it highlits line 128: color sourcePixel = sourcePixels [sourceYOfsset * width + sourceXOffset];

    Thank you for the patience guys.

    • This topic was modified 1 year, 4 months ago by  Phil.
    #402

    Phil
    Keymaster

    Okay, I think I found an issue with the script that was obfuscated due to the window sizing improperly. Try changing the following:

    Line 114
    if(sourceYOffset > height)

    To

    if(sourceYOffset >= height)

    And

    Line 124
    if(sourceXOffset > width)

    To

    if(sourceXOffset >= width)

    If that works for you I’ll update the script.

    • This reply was modified 1 year, 4 months ago by  Phil.
    • This reply was modified 1 year, 4 months ago by  Phil.
    #406

    Dennis Pacheco
    Participant

    Still the ArrayIndexOutOfBorder 824320 error and the highlight still on line 128 color sourcePixel = sourcePixels[sourceYOffset * width + sourceXOffset];

    The code is now as follows:

    /*
    
     Channel Shift Image Glitching Processing
     datamoshing.com | 2016
     
     */
    
    PImage sourceImg;
    PImage targetImg;
    
    boolean glitchComplete = false;
    boolean glitchSaved = false;
    
    // script settings
    
    // image path is relative to sketch directory
    String imgFileName = "72a9dd40-8c74-4a80-9072-1576f2e4c289";
    String fileType = "jpg";
    
    // repeat the process this many times
    int iterations = 3;
    
    // use result image as new source for iterations
    boolean recursiveIterations = false;
    
    // shift the image vertically true/false
    boolean shiftVertically = true;
    
    // shift the image horizontally true/false
    boolean shiftHorizontally = false;
    
    void setup() {
      // load images into PImage variables
      targetImg = loadImage(imgFileName+"."+fileType);
      sourceImg = loadImage(imgFileName+"."+fileType);
      
      // use only numbers (not variables) for the size() command, Processing 3
      size(1, 1);
    
      // allow resize and update surface to image dimensions
      surface.setResizable(true);
      surface.setSize(targetImg.width, targetImg.height);
    
      // load image onto surface
      image(sourceImg, 0, 0);
    }
    
    void draw() { 
      if (!glitchComplete)
      {
        // load pixels
        sourceImg.loadPixels();
        targetImg.loadPixels();
      
        // repeat the process according to the iterations variable
        for(int i = 0;i < iterations;i++)
        {
          // generate random numbers for which channels to swap
          int sourceChannel = int(random(3));
          int targetChannel = int(random(3));
          
          // start with no horizontalShift
          int horizontalShift = 0; 
          
          // if shiftHorizontally is true, generate a random number to shift horizontally by
          if(shiftHorizontally)
            horizontalShift = int(random(width));
          
          // start with no verticalShift
          int verticalShift = 0;
          
          // if shiftVertically is true, generate a random number to shift vertically by
          if(shiftVertically)
            verticalShift = int(random(height));
          
          // shift the channel
          copyChannel(sourceImg.pixels, targetImg.pixels, verticalShift, horizontalShift, sourceChannel, targetChannel);
          
          // use the target as the new source for the next iteration
          if(recursiveIterations)
            sourceImg.pixels = targetImg.pixels;
        }
        
        // update the target pixels
        targetImg.updatePixels();
          
        // glitch is done, proceed to update surface and save
        glitchComplete = true;
    
        // load updated image onto surface
        image(targetImg, 0, 0);
      }
    
      if (glitchComplete && !glitchSaved) {
    
        // save surface
        saveFrame(imgFileName+"_glitched.png");
        glitchSaved = true;
        println("Glitched image saved");
        println("Click or press any key to exit...");
      }
    }
    
    void copyChannel(color[] sourcePixels, color[] targetPixels, int sourceY, int sourceX, int sourceChannel, int targetChannel)
    {
        // starting at the sourceY and pointerY loop through the rows
        for(int y = 0; y < height; y++)
        {   
            // add y counter to sourceY
            int sourceYOffset = sourceY + y;
            
            // wrap around the top of the image if we've hit the bottom
            if(sourceYOffset > height)
              sourceYOffset -= height;
                  
            // starting at the sourceX and pointerX loop through the pixels in this row
            for(int x = 0; x <= width; x++)
            {
                // add x counter to sourceX
                int sourceXOffset = sourceX + x;
                
                // wrap around the left side of the image if we've hit the right side
                if(sourceXOffset >= width)
                  sourceXOffset -= width;
    
                // get the color of the source pixel
                color sourcePixel = sourcePixels[sourceYOffset * width + sourceXOffset];
                
                // get the RGB values of the source pixel
                float sourceRed = red(sourcePixel);
                float sourceGreen = green(sourcePixel);
                float sourceBlue = blue(sourcePixel);
       
                // get the color of the target pixel
                color targetPixel = targetPixels[y * width + x]; 
    
                // get the RGB of the target pixel
                // two of the RGB channel values are required to create the new target color
                // the new target color is two of the target RGB channel values and one RGB channel value from the source
                float targetRed = red(targetPixel);
                float targetGreen = green(targetPixel);
                float targetBlue = blue(targetPixel);
                
                // create a variable to hold the new source RGB channel value
                float sourceChannelValue = 0;
                
                // assigned the source channel value based on sourceChannel random number passed in
                switch(sourceChannel)
                {
                  case 0:
                    // use red channel from source
                    sourceChannelValue = sourceRed;
                    break;
                  case 1:
                  // use green channel from source
                    sourceChannelValue = sourceGreen;
                    break;
                  case 2:
                  // use blue channel from source
                    sourceChannelValue = sourceBlue;
                    break;
                }
                
                // assigned the source channel value to a target channel based on targetChannel random number passed in
                switch(targetChannel)
                {
                  case 0:
                    // assign source channel value to target red channel
                    targetPixels[y * width + x] =  color(sourceChannelValue, targetGreen, targetBlue);
                    break;
                  case 1:
                  // assign source channel value to target green channel
                    targetPixels[y * width + x] =  color(targetRed, sourceChannelValue, targetBlue);
                    break;
                  case 2:
                    // assign source channel value to target blue channel
                    targetPixels[y * width + x] =  color(targetRed, targetGreen, sourceChannelValue);
                    break;
                }
    
            }
        }
    }
    
    void keyPressed() {
      if (glitchSaved)
      {
        System.exit(0);
      }
    }
    
    void mouseClicked() {
      if (glitchSaved)
      {
        System.exit(0);
      }
    }
    • This reply was modified 1 year, 4 months ago by  Phil.
    #408

    Phil
    Keymaster

    I think you applied one of the fixes to the wrong line. Try the script below.

    /*
    
     Channel Shift Image Glitching Processing
     datamoshing.com | 2016
     
     */
    
    PImage sourceImg;
    PImage targetImg;
    
    boolean glitchComplete = false;
    boolean glitchSaved = false;
    
    // script settings
    
    // image path is relative to sketch directory
    String imgFileName = "72a9dd40-8c74-4a80-9072-1576f2e4c289";
    String fileType = "jpg";
    
    // repeat the process this many times
    int iterations = 3;
    
    // use result image as new source for iterations
    boolean recursiveIterations = false;
    
    // shift the image vertically true/false
    boolean shiftVertically = true;
    
    // shift the image horizontally true/false
    boolean shiftHorizontally = true;
    
    void setup() {
      // load images into PImage variables
      targetImg = loadImage(imgFileName+"."+fileType);
      sourceImg = loadImage(imgFileName+"."+fileType);
      
      // use only numbers (not variables) for the size() command, Processing 3
      size(1, 1);
    
      // allow resize and update surface to image dimensions
      surface.setResizable(true);
      surface.setSize(targetImg.width, targetImg.height);
    
      // load image onto surface
      image(sourceImg, 0, 0);
    }
    
    void draw() { 
      if (!glitchComplete)
      {
        // load pixels
        sourceImg.loadPixels();
        targetImg.loadPixels();
      
        // repeat the process according to the iterations variable
        for(int i = 0;i < iterations;i++)
        {
          // generate random numbers for which channels to swap
          int sourceChannel = int(random(3));
          int targetChannel = int(random(3));
          
          // start with no horizontalShift
          int horizontalShift = 0; 
          
          // if shiftHorizontally is true, generate a random number to shift horizontally by
          if(shiftHorizontally)
            horizontalShift = int(random(width));
          
          // start with no verticalShift
          int verticalShift = 0;
          
          // if shiftVertically is true, generate a random number to shift vertically by
          if(shiftVertically)
            verticalShift = int(random(height));
          
          // shift the channel
          copyChannel(sourceImg.pixels, targetImg.pixels, verticalShift, horizontalShift, sourceChannel, targetChannel);
          
          // use the target as the new source for the next iteration
          if(recursiveIterations)
            sourceImg.pixels = targetImg.pixels;
        }
        
        // update the target pixels
        targetImg.updatePixels();
          
        // glitch is done, proceed to update surface and save
        glitchComplete = true;
    
        // load updated image onto surface
        image(targetImg, 0, 0);
      }
    
      if (glitchComplete && !glitchSaved) {
    
        // save surface
        saveFrame(imgFileName+"_glitched.png");
        glitchSaved = true;
        println("Glitched image saved");
        println("Click or press any key to exit...");
      }
    }
    
    void copyChannel(color[] sourcePixels, color[] targetPixels, int sourceY, int sourceX, int sourceChannel, int targetChannel)
    {
        // starting at the sourceY and pointerY loop through the rows
        for(int y = 0; y < height; y++)
        {   
            // add y counter to sourceY
            int sourceYOffset = sourceY + y;
            
            // wrap around the top of the image if we've hit the bottom
            if(sourceYOffset >= height)
              sourceYOffset -= height;
                  
            // starting at the sourceX and pointerX loop through the pixels in this row
            for(int x = 0; x < width; x++)
            {
                // add x counter to sourceX
                int sourceXOffset = sourceX + x;
                
                // wrap around the left side of the image if we've hit the right side
                if(sourceXOffset >= width)
                  sourceXOffset -= width;
    
                // get the color of the source pixel
                color sourcePixel = sourcePixels[sourceYOffset * width + sourceXOffset];
                
                // get the RGB values of the source pixel
                float sourceRed = red(sourcePixel);
                float sourceGreen = green(sourcePixel);
                float sourceBlue = blue(sourcePixel);
       
                // get the color of the target pixel
                color targetPixel = targetPixels[y * width + x]; 
    
                // get the RGB of the target pixel
                // two of the RGB channel values are required to create the new target color
                // the new target color is two of the target RGB channel values and one RGB channel value from the source
                float targetRed = red(targetPixel);
                float targetGreen = green(targetPixel);
                float targetBlue = blue(targetPixel);
                
                // create a variable to hold the new source RGB channel value
                float sourceChannelValue = 0;
                
                // assigned the source channel value based on sourceChannel random number passed in
                switch(sourceChannel)
                {
                  case 0:
                    // use red channel from source
                    sourceChannelValue = sourceRed;
                    break;
                  case 1:
                  // use green channel from source
                    sourceChannelValue = sourceGreen;
                    break;
                  case 2:
                  // use blue channel from source
                    sourceChannelValue = sourceBlue;
                    break;
                }
                
                // assigned the source channel value to a target channel based on targetChannel random number passed in
                switch(targetChannel)
                {
                  case 0:
                    // assign source channel value to target red channel
                    targetPixels[y * width + x] =  color(sourceChannelValue, targetGreen, targetBlue);
                    break;
                  case 1:
                  // assign source channel value to target green channel
                    targetPixels[y * width + x] =  color(targetRed, sourceChannelValue, targetBlue);
                    break;
                  case 2:
                    // assign source channel value to target blue channel
                    targetPixels[y * width + x] =  color(targetRed, targetGreen, sourceChannelValue);
                    break;
                }
    
            }
        }
    }
    
    void keyPressed() {
      if (glitchSaved)
      {
        System.exit(0);
      }
    }
    
    void mouseClicked() {
      if (glitchSaved)
      {
        System.exit(0);
      }
    }
    #410

    Dennis Pacheco
    Participant

    It worked perfectly.

    Thank you so much!

    #412

    Phil
    Keymaster

    Great, I’ve updated the script download with this change. Cheers.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.