Mipmaps in OpenGL

I have the following image:

And the following sketch:

PImage img;
PGraphics big;
PGraphics small;

boolean from_big_pg = true;

void setup() {
  size(320, 180, P2D);
  img = loadImage("big.png");
  big = createGraphics(1280, 720, P2D);
  small = createGraphics(32, 18, P2D);
}


void draw() {
  
  big.beginDraw();
  big.image(img, 0, 0, big.width, big.height);
  big.endDraw();
  
  small.beginDraw();
  small.image(from_big_pg ? big : img, 0, 0, small.width, small.height);
  small.endDraw();
  
  
  gl_nearest_for_texture(small);
  image(small, 0, 0, 320, 180);
  gl_linear_for_texture(small);
}


public void gl_nearest_for_texture(PImage image) {
  PGL pgl = beginPGL();
  Texture image_tex = ((PGraphicsOpenGL)g).getTexture(image);
  pgl.bindTexture(PGL.TEXTURE_2D, image_tex.glName);
  pgl.texParameteri(PGL.TEXTURE_2D, PGL.TEXTURE_MIN_FILTER, PGL.NEAREST);
  pgl.texParameteri(PGL.TEXTURE_2D, PGL.TEXTURE_MAG_FILTER, PGL.NEAREST);
  pgl.bindTexture(PGL.TEXTURE_2D, 0);
  endPGL();
}


public void gl_linear_for_texture(PImage image) {
  PGL pgl = beginPGL();
  Texture image_tex = ((PGraphicsOpenGL)g).getTexture(image);
  pgl.bindTexture(PGL.TEXTURE_2D, image_tex.glName);
  pgl.texParameteri(PGL.TEXTURE_2D, PGL.TEXTURE_MIN_FILTER, PGL.LINEAR);
  pgl.texParameteri(PGL.TEXTURE_2D, PGL.TEXTURE_MAG_FILTER, PGL.LINEAR);
  pgl.bindTexture(PGL.TEXTURE_2D, 0);
  endPGL();
}

This is the result with from_big_pg set to false:

and this when it is set to true:

I would like the second result to look like the first. I suspect I have to set something with the mipmaps but I’m stuck here. Does someone know why the downscale from the PGraphics looks so bad?

This is one of the attempts:

public void gl_texParameter_min_mag(PImage image, int min, int mag) {
  PGL pgl = beginPGL();
  Texture image_tex = ((PGraphicsOpenGL)g).getTexture(image);
  pgl.bindTexture(PGL.TEXTURE_2D, image_tex.glName);
  pgl.texParameteri(PGL.TEXTURE_2D, PGL.TEXTURE_MIN_FILTER, min);
  pgl.texParameteri(PGL.TEXTURE_2D, PGL.TEXTURE_MAG_FILTER, mag);
  pgl.bindTexture(PGL.TEXTURE_2D, 0);
  endPGL();
}

Then before small.beginDraw() I added gl_texParameter_min_mag(big, PGL.LINEAR_MIPMAP_LINEAR, PGL.LINEAR);

Now everything is black, so maybe this would work, but the mipmaps are not updated?

I don’t know, hope someone can help since this seems a bit advanced.

Ok I’m a bit further, I can use gl_texture(in).usingMipmaps(true, 3); and this fixes it.
But it does not work when the input comes from a video file…

public Texture gl_texture(PImage img) {
  return ((PGraphicsOpenGL)g).getTexture(img);
}