how does loadPixels work?

I'm trying todo something advanced. I had 3 attempts so far and i will post when it's done or when i'm stuck again. Atm i wonder how loadPixels works for an PImage:

This is the code:

public void loadPixels() {  // ignore
    if (pixels == null || pixels.length != width*height) {
      pixels = new int[width*height];
    }
    setLoaded();
  }

This is all setLoaded does:

public void setLoaded() {  // ignore
    loaded = true;
  }

If i load a png for example:

// For jpeg, gif, and png, load them using createImage(),
    // because the javax.imageio code was found to be much slower.
    // http://dev.processing.org/bugs/show_bug.cgi?id=392
    try {
      if (extension.equals("jpg") || extension.equals("jpeg") ||
          extension.equals("gif") || extension.equals("png") ||
          extension.equals("unknown")) {
        byte bytes[] = loadBytes(filename);
        if (bytes == null) {
          return null;
        } else {
          Image awtImage = Toolkit.getDefaultToolkit().createImage(bytes);
          PImage image = **loadImageMT(awtImage);**
          if (image.width == -1) {
            System.err.println("The file " + filename +
                               " contains bad image data, or may not be an image.");
          }
          // if it's a .gif image, test to see if it has transparency
          if (extension.equals("gif") || extension.equals("png")) {
            image.checkAlpha();
          }

//          if (params != null) {
//            image.setParams(g, params);
//          }
          return image;
        }
      }
    } catch (Exception e) {
      // show error, but move on to the stuff below, see if it'll work
      e.printStackTrace();
    }

The loadImageMT looks like this:

protected PImage loadImageMT(Image awtImage) {
    MediaTracker tracker = new MediaTracker(this);
    tracker.addImage(awtImage, 0);
    try {
      tracker.waitForAll();
    } catch (InterruptedException e) {
      //e.printStackTrace();  // non-fatal, right?
    }

    PImage image = **new PImage(awtImage);**
    image.parent = this;
    return image;
  }

So loadPixels() does create the pixel array but i see nowhere that it set's the pixels.

What is going on?

Answers

  • do you need to use updatePixels when done?

  • is this the processing developer code?

    Sorry, I was totally wrong.

    Forget it

  • Actually, in Java mode, a PImage has the pixels[] array already loaded, and loadPixels() do nothing. It might be not true in other modes (Android, JavaScript, ...).

  • edited June 2014

    So loadPixels() does create the pixel array but I see nowhere that it sets the pixels.

    Remember that in Java, all array "slots" starts off w/ the default value for its type, just like any other field!

    So in new float[10], all slots have initial 0.0f value.
    And new PVector[30], all slots are still null, b/c that's the default value for all non-primitive types!

    Moreover, class PImage is littered w/ pixels = new int[width * height]; statements.
    Like its method init() and overloaded constructor PImage(Image img).

    https://github.com/processing/processing/blob/master/core/src/processing/core/PImage.java#L242

    In short, int pixels[] starts off as a 100% transparent black color = 0 when a PImage is instantiated! :-B

    Except when a PImage object is created by calling its empty overloaded constructor: PImage().
    B/c it neither invokes init() nor instantiates a color[] for field pixels within it! :-&

  • There is a lot changed then right? This is also strange behavior. I believe this was not possible before:

      PImage img = loadImage("img.png");
      println(img.pixels[500]);
      println(img.pixels[800]);
    
     image(img, 0, 0);
    
      for(int i = 0; i < img.pixels.length/2; i++) {
         img.pixels[i] = color(255, 0, 0); 
      }
    

    Notice i draw the image before i manipulate the pixels! Still the pixel manipulation is shown!!!

    Is there a way against that? This is really something i don't want!

    Now i understand why i had this problem: https://github.com/processing/processing/issues/2494

    And to help Ben Fry a little, does someone have a better name for the issue above?

  • edited June 2014

    I confess I haven't messed much w/ the Processing's OpenGL engines P2D & P3D yet.
    Just to add to your OpenGL bugs, if you run the program in this thread:

    http://forum.processing.org/two/discussion/5618/crop-image

    And replace JAVA2D for either P2D or P3D, the round cropped PImage got its alpha transparency void via image()! X(
    At least in my old Processing 2.0.2! Although Processing 1.5.1's OPENGL renderer works alright! @-)

Sign In or Register to comment.