element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Experts, Learning and Guidance
  • Technologies
  • More
Experts, Learning and Guidance
Ask an Expert Forum Help.....getting an out of bound exeception
  • Blog
  • Forum
  • Documents
  • Leaderboard
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Experts, Learning and Guidance to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 2 replies
  • Subscribers 302 subscribers
  • Views 267 views
  • Users 0 members are here
  • help
  • experts
  • programming
  • ask_the_expert
Related
See a helpful answer?

Be sure to click 'more' and select 'suggest as answer'!

If you're the thread creator, be sure to click 'more' then 'Verify as Answer'!

Help.....getting an out of bound exeception

Former Member
Former Member over 10 years ago

I am trying to get the code below to distort an image. The distortion creates square bumps on the image surface.

The code below keeps giving me an out of bounds exeception. Hope someone could find the problem.

 

***********************************************************

 

PImage img;

PImage img_new;

int cellsize=2;

int cols, rows;

 

 

void setup(){

size(600,600);

img=loadImage("bcircle.jpg");

img_new=createImage(img.width,img.height,RGB);

cols = width/cellsize;

rows = height/cellsize;

}

 

 

void draw(){

  img.resize(img.width,img.height);

  img_new.loadPixels();

  int shif=0;

  for(int j=0; j<rows; j++){

    for(int i=0; i<cols; i++){

     int x=i*cellsize+cellsize/2;

     int y=j*cellsize+cellsize/2;

     int loc=x+width*y;

     int left_loc=(x-cellsize)+width*y;

     if(x>cellsize && x<width-cellsize && shif%2==0){

       img_new.pixels[loc] = color (img.pixels[left_loc]);     

     }else{

       img_new.pixels[loc] = color (img.pixels[loc]);

     }

    }

   }

   shif++;

   img_new.updatePixels();

   image(img_new,0,0);

}

  • Sign in to reply
  • Cancel
Parents
  • gadget.iom
    gadget.iom over 10 years ago

    Hi Eddy.

     

    Ignore my last post. It's looked like you were using processing, so I downloaded a copy and attempted to replicate the issue.

    I have no experience with processing and the code works with an image of 600pxx600px and above,  but it seems to fail when the image falls below that size.

     

    Is the bcirgle.jpg image you're using 600x600?

     

    I have now modified the code so that it loads the image first and defines the canvas based on the dimensions determined from the image itself. This means you can have an image of any dimensions, even non square ones. (Hopefully)

     

    Code:

    PImage img;
    PImage img_new;
    int cellsize=2;
    int cols, rows;
    
    
    void setup(){
    //size(600,600);
    img=loadImage("bcircle.jpg");
    size(img.width, img.height);
    img_new=createImage(img.width,img.height,RGB);
    cols = width/cellsize;
    rows = height/cellsize;
    }
    
    
    void draw(){
      img.resize(img.width,img.height);
      img_new.loadPixels();
      int shif=0;
      for(int j=0; j<rows; j++){
        for(int i=0; i<cols; i++){
         int x=i*cellsize+cellsize/2;
         int y=j*cellsize+cellsize/2;
         int loc=x+width*y;
         int left_loc=(x-cellsize)+width*y;
         if(x>cellsize && x<width-cellsize && shif%2==0){
           img_new.pixels[loc] = color (img.pixels[left_loc]);     
         }else{
           img_new.pixels[loc] = color (img.pixels[loc]);
         }
        }
       }
       shif++;
       img_new.updatePixels();
       image(img_new,0,0);
    }

     

    Line 8 has been commented out and line 10 added. Let me know how you get on.

     

    Paul

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • gadget.iom
    gadget.iom over 10 years ago

    Hi Eddy.

     

    Ignore my last post. It's looked like you were using processing, so I downloaded a copy and attempted to replicate the issue.

    I have no experience with processing and the code works with an image of 600pxx600px and above,  but it seems to fail when the image falls below that size.

     

    Is the bcirgle.jpg image you're using 600x600?

     

    I have now modified the code so that it loads the image first and defines the canvas based on the dimensions determined from the image itself. This means you can have an image of any dimensions, even non square ones. (Hopefully)

     

    Code:

    PImage img;
    PImage img_new;
    int cellsize=2;
    int cols, rows;
    
    
    void setup(){
    //size(600,600);
    img=loadImage("bcircle.jpg");
    size(img.width, img.height);
    img_new=createImage(img.width,img.height,RGB);
    cols = width/cellsize;
    rows = height/cellsize;
    }
    
    
    void draw(){
      img.resize(img.width,img.height);
      img_new.loadPixels();
      int shif=0;
      for(int j=0; j<rows; j++){
        for(int i=0; i<cols; i++){
         int x=i*cellsize+cellsize/2;
         int y=j*cellsize+cellsize/2;
         int loc=x+width*y;
         int left_loc=(x-cellsize)+width*y;
         if(x>cellsize && x<width-cellsize && shif%2==0){
           img_new.pixels[loc] = color (img.pixels[left_loc]);     
         }else{
           img_new.pixels[loc] = color (img.pixels[loc]);
         }
        }
       }
       shif++;
       img_new.updatePixels();
       image(img_new,0,0);
    }

     

    Line 8 has been commented out and line 10 added. Let me know how you get on.

     

    Paul

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube