Skip to content

Blend fails with transparency #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ajavamind opened this issue May 19, 2016 · 3 comments
Closed

Blend fails with transparency #218

ajavamind opened this issue May 19, 2016 · 3 comments
Assignees

Comments

@ajavamind
Copy link

The code at https://github.com/ajavamind/MergeTransparentPhotoLoop
runs as expected in Java Mode, but in Android Mode it fails to blend two photos.
Is there something missing in my code for the Android version to run?
Testing with 3.1.1 and Android 3.0-RC4 with different SDK versions 4.4, 5.0.2 and 6.0.1 on two phones.

@ajavamind
Copy link
Author

ajavamind commented May 26, 2016

Here is another version of the code to accomplish same desired effect that works fine in Java Mode but fails in Android Mode

// Written by Andy Modla May, 2016

// Inspiration for this code from
// https://processing.org/discourse/beta/num_1212692440.html

final int TOP_LIMIT = 255; // 255 is max tint range;
final int BOTTOM_LIMIT = 128;

String top = "rDSC_1915_cr.jpg";
String bottom = "rDSC_1836.jpg";

PImage bottomImage;
PImage topImage;
PImage displayImage;
int counter = TOP_LIMIT;
int direction = 1;
boolean stop = false;

void setup()
{
  size(700, 540);
  bottomImage = loadImage(bottom);
  topImage = loadImage(top);
  bottomImage.loadPixels();
  topImage.loadPixels();
  displayImage = loadImage(top);
  displayImage.loadPixels();
  frameRate(30);
}

void draw() {
  if (!stop) {
    background(0);
    setTransparency(topImage, counter);
    setTransparency(bottomImage, TOP_LIMIT - counter);
    // copy topImage to displayImage
    displayImage.copy(topImage,0,0,topImage.width,topImage.height,0,0,topImage.width,topImage.height);
    displayImage.blend(bottomImage, 0, 0, bottomImage.width, bottomImage.height, 0, 0, bottomImage.width, bottomImage.height, SOFT_LIGHT);
    displayImage.loadPixels();
    image(displayImage, 0, 0);

    counter += direction;
    if (counter > TOP_LIMIT) {
      direction = -direction;
      counter += direction;
    } else if (counter < BOTTOM_LIMIT) {
      direction = -direction;
      counter += direction;
    }

    //if (frameCount < 256) {  // two cycles
    //saveFrame("frames/pond####.tif");
    //}

    // debug
    //println("counter="+ counter);
    //println("frame="+ frameCount);

    //stop when enough frames are made for video
    //if (frameCount >= 255)
    // stop = true;
  }
}

void setTransparency(PImage pi, int transparency)
{
  transparency <<= 24;
  for (int i = 0; i < pi.width; i++)
  {
    for (int j = 0; j < pi.height; j++)
    {
      color c = pi.pixels[i + j * pi.width];
      c = c & 0x00FFFFFF | transparency;
      pi.pixels[i + j * pi.width] = c;
    }
  }
}

@codeanticode codeanticode added this to the 3.0.1 bugfix release milestone Jun 6, 2016
@codeanticode codeanticode self-assigned this Jun 11, 2016
@codeanticode
Copy link
Contributor

codeanticode commented Jul 18, 2016

Fixed with 24acabb and 161137e.

Please note that access to the pixels array needs to be accompanied with the use of updatePixels at the end of the pixel operations:

final int TOP_LIMIT = 255;
final int BOTTOM_LIMIT = 128;

String top = "img2.jpg";
String bottom = "img1.jpg";

PImage bottomImage;
PImage topImage;
PImage displayImage;
int counter = TOP_LIMIT;
int direction = 1;

void setup() {
  size(540, 720);
  bottomImage = loadImage(bottom);
  topImage = loadImage(top);
  bottomImage.loadPixels();
  topImage.loadPixels();
  displayImage = createImage(topImage.width, topImage.height, ARGB);  
  frameRate(30);
}

void draw() {
  background(0);

  setTransparency(topImage, counter);
  setTransparency(bottomImage, TOP_LIMIT - counter);

  displayImage.copy(topImage, 0, 0, topImage.width, topImage.height, 0, 0, topImage.width, topImage.height);
  displayImage.blend(bottomImage, 0, 0, bottomImage.width, bottomImage.height, 0, 0, bottomImage.width, bottomImage.height, SOFT_LIGHT);
  image(displayImage, 0, 0);

  counter += direction;
  if (counter > TOP_LIMIT) {
    direction = -direction;
    counter += direction;
  } else if (counter < BOTTOM_LIMIT) {
    direction = -direction;
    counter += direction;
  }
}

void setTransparency(PImage pi, int transparency) {
  pi.loadPixels();
  transparency <<= 24;
  for (int i = 0; i < pi.width; i++) {
    for (int j = 0; j < pi.height; j++) {
      color c = pi.pixels[i + j * pi.width];
      c = c & 0x00FFFFFF | transparency;
      pi.pixels[i + j * pi.width] = c;
    }
  }
  pi.updatePixels();
}

@ajavamind
Copy link
Author

thanks, works great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants