We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Loading image and make it's modified copy
Page Index Toggle Pages: 1
Loading image and make it's modified copy (Read 173 times)
Loading image and make it's modified copy
Mar 20th, 2009, 12:58pm
 
Halo :]

Check this code (compile in Processing):
(You'll need the controlP5 library)

import javax.swing.*;
import controlP5.*;
ControlP5 controlP5;

public PImage img;
PImage a;

JFrame new_window;
MApplet imageviewer;

void setup()
{
 size(400,800);
 background(50);
 //noLoop();
 controlP5 = new ControlP5(this);
   
 Controller imgLoad = controlP5.addButton("load",1,10,10,80,20);
 imgLoad.setId(1);
 
 Controller imgInvert = controlP5.addButton("invert",3,10,100,80,20);
 imgInvert.setId(2);
}

void draw() {
 controlP5.draw();
}

void controlEvent(ControlEvent theEvent) {
 println("ID =  "+theEvent.controller().id());
 switch(theEvent.controller().id()) {
   case(1):
     loadNewImage();
   break;
   
   case(2):
     imageviewer.negatyw();
     println("OK");
   break;
   
   case(3):
     //negatyw();
   break;
 
   case(4):
     //r_g_b();
   break;  
 }
}



void loadNewImage() {
 // pick image to display in a new window
 FileDialog fd = new FileDialog(this.frame);
 fd.setMode(FileDialog.LOAD);
 fd.setLocation(50, 50);
 fd.show();
 String dir = fd.getDirectory();
 String file = fd.getFile();
 if( dir != null && file != null &&  
(
  file.indexOf(".jpg") != -1 ||  
  file.indexOf(".png") != -1 ||
  file.indexOf(".PNG") != -1 ||  
  file.indexOf(".gif") != -1 ||  
  file.indexOf(".jpeg") != -1 ||
  file.indexOf(".JPEG") != -1 ||
  file.indexOf(".JPG") != -1 ||
  file.indexOf(".tif") != -1 ||
  file.indexOf(".bmp") != -1
)
   ){
   // load image
   img = loadImage(dir+file);
   PImage ob = createImage(0, 0, RGB);
   ob=img;
   ob.resize(200,200);
   //img.resize(10,10);
   image(ob,200,400);
   
   // open the window / change the content
   if(new_window == null) {
    new_window = new JFrame(file);
    // create the image viewer applet
    imageviewer = new MApplet();
    // add the image viewer applet
    new_window.getContentPane().add(imageviewer, BorderLayout.CENTER);
    // set it to not be resizable
    //new_window.setResizable(false);
    // show it
    new_window.setVisible(true);
    // init
    imageviewer.init();
    }
   // size it
   new_window.setSize(img.width,img.height);
 }
 imageviewer.redraw();
}

public class MApplet extends PApplet {
 public void setup() {
   size(img.width,img.height);
   background(img);
   noLoop();
 }
     
 public void draw() {
   //image(img,0,0);
   //imageviewer.redraw();
 }
 
 public void negatyw() {
 a=img;
 a.loadPixels();
 for (int i = 0; i < a.pixels.length; i++ ) {
   float r = red (a.pixels[i]);
   float g = green (a.pixels[i]);
   float b = blue (a.pixels[i]);
   r = 255 - r;
   g = 255 - g;
   b = 255 - b;
   color c = color(r,g,b);
   a.pixels[i] = c;
 }
 a.updatePixels();
 //a=img;
 image(a,0,0);
 redraw();
}
}


NOW Please WHY image "a" is resized on the second window too ?
I need to display resized source image on the first window
than modiffy it and dosplay on the second window with origin size.
Please HELP.
Page Index Toggle Pages: 1