Issue with image() and beginRecord(SVG)

Hi there :wave:

I am having an issue with image() loading a PGraphics object and SVG beginRecord().
I am running windows Processing 3.5.4

If beginRecord() is commented out in setup() my code runs without issue.
If beginRecord() is active, image() throws a Null Pointer Exception when called.
Interestingly if beginRecord() is called in PDF mode the NPE does not occur…

My code (as below) creates two PGraphics objects, several lines are drawn to background and a circle is drawn for a mask before an alpha masking function is applied to background. image() is called to display the resulting PGraphic.

Ideally I’d like to export an SVG of my sketch, I may be doing something wrong here but I’m struggling to work out what, any help would be greatly appreciated!

import processing.svg.*;
PGraphics background;
PGraphics mask;

float spacing = 5;
float exponent = 0.038;
float base = 3;

void setup() {
  size(594, 840);
  background(255);
  noLoop();
  //beginRecord(SVG, "ExponentialLines.svg");
}

int[] minAlphas(PImage img, PImage img2) {
  img.loadPixels();
  img2.loadPixels();
  int[] a = new int[img.pixels.length];
  for (int i =0; i<img.pixels.length; i++) {
    a[i] = min(img.pixels[i] >> 24 & 0xFF, img2.pixels[i] >> 24 & 0xFF);
  }
  return a;
}

void draw() {

  //create background and draw lots of lines!
  background = createGraphics(width, height);
  background.beginDraw();
  background.noFill();
  background.translate((width/2), 0);    
  for (int i=1; i<(width/spacing)/2; i++) {
    background.beginShape();
    for (int j=0; j<(height/spacing); j++) {
      background.curveVertex((i*spacing-pow(base, j*exponent)), j*spacing);
    }
    background.endShape();
  }
  background.endDraw();

  // create mask
  mask = createGraphics(width, height);
  mask.beginDraw();
  mask.noStroke();
  mask.fill(255);
  mask.circle(width/2, height/2, width*.8);
  mask.endDraw();

  // apply mask
  background.mask(minAlphas(background, mask));

  image(background, 0, 0);  
  endRecord();
}

Hi,

svg is vector based and doesn t save images pixels,
when you use Pgraphics, draw some vectors on it and draw it with image(background, 0, 0);, those drawing was converted to an image, then fail to be saved as svg.

good point is : all you drawings in PGraphics background; can be done in draw(){}, as they re vectors
so just move all in draw and they will be saved
(note : masking function manipulate pixels and they re not saved so you need to replace by a test when doing your curvevertex)

1 Like

Thanks for your answer!

I had begun to suspect that it may not have liked the pixelwise manipulation in the mask or minAlphas functions…

I was hoping to avoid having to write some sort of ‘mask limits detection’ for the vector graphics and get away with something that could easily slice the vectors. I guess there’s some function writing in my future!

Thanks Again!