How to export frames as layers to an ico file

I want to be able to export the processing window across several frames, using saveFrames() or similar, to a single ico file with one layer per frame. It might work similar to the code shown.

PImage img;

void setup() {
  size(400, 200);
  surface.setResizable(true);
  imageMode(CENTER);
  img = loadImage("icon.png");
  int size = 192;
  saveImage(size); //Layer 1
  size = 144;
  saveImage(size); //Layer 2
  size = 96;
  saveImage(size); //Layer 3
  size = 72;
  saveImage(size); //Layer 4
  size = 48;
  saveImage(size); //Layer 5
  size = 36;
  saveImage(size); //Layer 6
  exit();
}

void draw() {
}


void saveImage(int size) {
  img.resize(size, size);
  surface.setSize(size, size);
  background(150);
  image(img, width/2, height/2);
  save("icon-" +size +".png"); //This function would change
}

Is there any function or library for performing such a task?