pdf output won't work

edited March 2018 in Questions about Code

Hello,

I am trying to output a sketch to a pdf file, but all I get is an empty file. If I remove all pdf code the output works very well to the sketch itself so I must be forgetting something in the first part of the code? Can anyone help me out?

Thanks a lot!

import processing.pdf.*;
Cell[][] grid; // 2 dimensionale array van cellen { {0,0}, {0,1}, {1,1},...}
PGraphics pdf ;
PImage theSource; // 
String sImage = "im1.png"; // im2.png 818*518 ; im3.jpg 1030*791 ; im4.jpg 900*586 ; im5.jpg 1841*1500 ; im6.jpg 520*477 
//; im7.jpg 1920*872
int imageWidth = 1570; // breedte foto - manueel in te vullen
int imageHeight = 1112; // hoogte foto - manueel in te vullen
int blockSize = 20; // de dimensie van de cell
int cols = imageWidth/blockSize; // het aantal kolommen in de grid
int rows = imageHeight/blockSize; // het aantal rijen in de grid

void setup() {
 size(1570,1112);  // moet exact overeenstemmen met de foto
 pdf = createGraphics(1570, 1112, PDF, "export.pdf");
 grid = new Cell[cols][rows];
 theSource = loadImage(sImage);   // laden van de foto
 for (int i=0; i< cols; i++) {   // doorlopen van de cellen per kolom in de grid
   for (int j=0; j< rows; j++) {   // doorlopen van de cellen per rij in de grid
         grid[i][j] = new Cell(i,j,blockSize,imageWidth); // maak een cell object met een kolom en rij identificator
   }
 }
 noLoop(); // dit is een sketch met statische output 
 }

void draw() {

pdf.beginDraw();
pdf.background(255);
for (int i=0; i< cols; i++) {   // doorlopen van de cellen per kolom in de grid
   for (int j=0; j< rows; j++) {   // doorlopen van de cellen per rij in de grid
      circleContainer cc = grid[i][j].getColorValuesForCell();  // haal de kleurwaarde voor elke pixel in de cell op en steek deze in een array
      sampledCell sc = grid[i][j].circleDot(i,j,blockSize, cc.isitaCircle,cc.redDelta,cc.greenDelta,cc.blueDelta);
      if (sc.isitaCircle) { 
      pdf.noStroke();
      pdf.fill(sc.RD, sc.GN, sc.BE, i);
      pdf.ellipse((i*blockSize+blockSize/2), (j*blockSize+blockSize/2), blockSize*0.7, blockSize*0.7);
  }

 }
}
pdf.endDraw();
} 


class Cell {

  int firstXPos;  // x positie van de eerste pixel in de cel
  int firstYPos; // y positie van de eerste pixel in de cel
  int canvasWidth;
  int cellDimension;

  Cell(int thisI, int thisJ, int thisCellSize, int thisImgWidth) {
    firstXPos = thisI*thisCellSize; // de x positie is gelijk aan het kolomnummer maal de celbreedte
    firstYPos = thisJ*thisCellSize; // de y positie is gelijk aan het rijnummer maal de celbreedte
    canvasWidth = thisImgWidth;
    cellDimension = thisCellSize;
    // println(thisI, thisJ, (firstXPos+cellDimension)+(firstYPos+cellDimension)*canvasWidth, cellDimension);
  }

  circleContainer getColorValuesForCell() {

    float kolom = 0;
    float rij = 0;
    float ns = 0;
    float nc = 0;
    float rs = 0;
    float bs = 0;
    float gs = 0;
    float rc = 0;
    float bc = 0;
    float gc = 0;
    float ars = 0;
    float abs = 0;
    float ags = 0;
    float arc = 0;
    float abc = 0;
    float agc = 0;
    float deltaR = 0;
    float deltaG = 0;
    float deltaB = 0;
    boolean circle;

    for (int x = firstXPos; x < firstXPos+cellDimension; x++) {
      for (int y = firstYPos; y < firstYPos+cellDimension; y++) {  // loop each pixel in this cell                       
        int loc = x + y * canvasWidth; // loc identifies the pixel where we need the color info from
        float r = red(theSource.pixels[loc]);  // red value 
        float g = green(theSource.pixels[loc]); // green value
        float b = blue(theSource.pixels[loc]); // blue value

        if ((x-firstXPos)<(cellDimension/3)) {
          kolom = 1;
        } else if ((x-firstXPos)>(cellDimension*2/3)) {
          kolom = 3;
        } else {
          kolom=2;
        }

        if ((y-firstYPos)<(cellDimension/3)) {
          rij = 1;
        } else if ((Y-firstYPos)>(cellDimension*2/3)) {
          rij = 3;
        } else {
          rij=2;
        }

        if (kolom == 1 & rij ==1 || (kolom == 3 & rij == 3) || (kolom == 1 & rij == 3) || (kolom == 3 & rij == 1)) {
          // Side
          rs = rs + r;
          bs = bs +b;
          gs = gs +g;
          ns =ns +1;
        } else {
          // Center
          nc = nc +1;
          rc = rc + r;
          bc = bc +b;
          gc = gc +g;
        }
      }
    }
    ars = rs/ns;
    abs = bs/ns;
    ags = gs/ns;
    arc = rc/nc;
    abc = bc/nc;
    agc = gc/nc;
    deltaR = abs(ars-arc);
    deltaB = abs(abs-abc);
    deltaG = abs(ags-agc);

    if (deltaR > 0.2*(ars+arc)/2 || deltaB > 0.2*(abs+abc)/2 || deltaG > 0.2*(ags+ags)/2) {
      circle = true;
    } else {
      circle = false;
    } 
    return new circleContainer(circle, deltaR, deltaG, deltaB);
  }

  sampledCell circleDot(float ii, float jj, float blockSi, boolean circle, float redd, float greenn, float bluee) {
    boolean isthisaCircle = circle;
    float blockSiz = blockSi;
    float colm = ii;
    float rw = jj;
    float RD = 30*round(redd);
    float GN = 10*round(greenn);
    float BE = 10*round(bluee);

    return new sampledCell(isthisaCircle,colm*blockSiz+blockSiz/2,(rw*blockSiz+blockSiz/2),blockSiz*0.7,RD,GN,BE);
    }
}

 class circleContainer{

         boolean isitaCircle;
         float redDelta;
         float greenDelta;
         float blueDelta;

         circleContainer(boolean isitaCircle, float redDelta, float greenDelta, float blueDelta) {

            this.isitaCircle = isitaCircle;
            this.redDelta = redDelta;
            this.greenDelta = greenDelta;
            this.blueDelta = blueDelta;


         }
        }  

class sampledCell{

         boolean isitaCircle;
         float xPos;
         float yPos;
         float dim;
         float RD;
         float GN;
         float BE;

         sampledCell(boolean isitaCircle, float xPos, float yPos, float dim, float RD, float GN, Float BE) {

            this.isitaCircle = isitaCircle;
            this.xPos = xPos;
            this.yPos = yPos;
            this.dim = dim;
            this.RD = RD;
            this.GN = GN;
            this.BE = BE;
         }
        }  

Answers

  • we don't have the image...

  • Answer ✓

    I found it. By adding:

    pdf.dispose();

    Just before

    pdf.endDraw();

    The output is sent to the pdf.

Sign In or Register to comment.