This example computes the histogram of the image. This program illustrates the use of the glHistogram() function. The example is ported from C examples in the OpenGL Programming Guide (known as the "red book").

-> Copyright and Permission Notice

 package glredbook12x;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.io.*;
import java.nio.*;

import javax.media.opengl.*;
import javax.media.opengl.glu.*;

import com.sun.opengl.util.*;

/**
 * Compute the histogram of the image. This program illustrates the use of the
 * glHistogram() function.
 * 
 * @author Kiet Le (Java port)
 */
public class histogram
  extends JFrame
    implements GLEventListener//
    , KeyListener //
// , MouseListener //
// , MouseMotionListener //
// , MouseWheelListener //
{
  private GLCapabilities caps;
  private GLCanvas canvas;
  private KeyEvent key;
  //
  private ByteBuffer pixels;
  // private int width; not reference as params...
  // private int height;...as are all Java primitives
  private Dimension dim = new Dimension(0, 0);
  private static final int HISTOGRAM_SIZE = 256;

  public histogram()
  {
    super("histogram");
  
    caps = new GLCapabilities();
    canvas = new GLCanvas(caps);
    canvas.addGLEventListener(this);
    canvas.addKeyListener(this);

    add(canvas);
  }

  public void run()
  {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(640, 480);
    setLocationRelativeTo(null);
    setVisible(true);
    canvas.requestFocusInWindow();
  }

  public static void main(String[] args)
  {
    new histogram().run();
  }

  public void init(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
    //
    gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    if (gl.isExtensionAvailable("GL_ARB_imaging") //
        && gl.isFunctionAvailable("glHistogram"))
    {
      gl.glHistogram(GL.GL_HISTOGRAM, HISTOGRAM_SIZE, GL.GL_RGB, false);
      gl.glEnable(GL.GL_HISTOGRAM);
    }
    else
    {
      String msg = "GL_ARB_imaging (optional) subset is is not available.";
      setTitle(getTitle() + " GL_ARB_imaging not available :(");
      System.err.println(msg);
      add(new JLabel(msg), BorderLayout.NORTH);
      SwingUtilities.updateComponentTreeUI(this);
    }

    pixels = readImage("Data/leeds.bin", dim);
    System.out.println(pixels.toString());

  }

  public void display(GLAutoDrawable drawable)
  {
    GL gl = drawable.getGL();
 
    gl.glClear(GL.GL_COLOR_BUFFER_BIT);

    boolean sink = false;
    if (key != null) switch (key.getKeyChar()) {
      case 's':
        if (gl.isExtensionAvailable("GL_ARB_imaging") && //
            gl.isFunctionAvailable("glHistogram"))
        {
          sink = !sink;
          gl.glHistogram(GL.GL_HISTOGRAM, HISTOGRAM_SIZE, GL.GL_RGB, sink);
        }
        break;

    }// key sw

    // short values[][] = new short[HISTOGRAM_SIZE][3];
    ShortBuffer values = BufferUtil.newShortBuffer(HISTOGRAM_SIZE * 3);

    gl.glClear(GL.GL_COLOR_BUFFER_BIT);
    gl.glRasterPos2i(1, 1);
    gl.glDrawPixels(dim.width, dim.height, //
        GL.GL_RGB, GL.GL_UNSIGNED_BYTE, pixels);

    if (gl.isExtensionAvailable("GL_ARB_imaging")//
        && gl.isFunctionAvailable("glGetHistogram"))
    {
      gl.glGetHistogram(GL.GL_HISTOGRAM, true, GL.GL_RGB,//
          GL.GL_UNSIGNED_SHORT, values);
    }

    /* Plot histogram */
    gl.glBegin(GL.GL_LINE_STRIP);
    gl.glColor3f(1.0f, 0.0f, 0.0f);
    for (int i = 0; i < HISTOGRAM_SIZE; i++)
      gl.glVertex2s((short) i, values.get(i * 2));
    gl.glEnd();

    gl.glBegin(GL.GL_LINE_STRIP);
    gl.glColor3f(0.0f, 1.0f, 0.0f);
    for (int i = 0; i < HISTOGRAM_SIZE; i++)
      gl.glVertex2s((short) i, values.get(i * 2 + 1));// [i][1]);
    gl.glEnd();

    gl.glBegin(GL.GL_LINE_STRIP);
    gl.glColor3f(0.0f, 0.0f, 1.0f);
    for (int i = 0; i < HISTOGRAM_SIZE; i++)
      gl.glVertex2s((short) i, values.get(i * 2 + 2));// [i][2]);
    gl.glEnd();

    gl.glFlush();

  }

  public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h)
  {
    GL gl = drawable.getGL();
  
    gl.glViewport(0, 0, w, h);
    gl.glMatrixMode(GL.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glOrtho(0, 256, 0, 10000, -1.0, 1.0);
    gl.glMatrixMode(GL.GL_MODELVIEW);
  }

  public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
      boolean deviceChanged)
  {
  }

  /**
   * Reads an image from an archived file and return it as ByteBuffer object.
   * 
   * @author Mike Butler, Kiet Le
   */
  private ByteBuffer readImage(String filename, Dimension dim)
  {
    if (dim == null) dim = new Dimension(0, 0);
    ByteBuffer bytes = null;
    try
    {
      DataInputStream dis = new DataInputStream(getClass().getClassLoader()
          .getResourceAsStream(filename));
      dim.width = dis.readInt();
      dim.height = dis.readInt();
      System.out.println("Creating buffer, width: " + dim.width + " height: "
                         + dim.height);
      // byte[] buf = new byte[3 * dim.height * dim.width];
      bytes = BufferUtil.newByteBuffer(3 * dim.width * dim.height);
      for (int i = 0; i < bytes.capacity(); i++)
      {
        bytes.put(dis.readByte());
      }
      dis.close();

    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    bytes.rewind();
    return bytes;
  }

  public void keyTyped(KeyEvent key)
  {
  }

  public void keyPressed(KeyEvent key)
  {
    this.key = key;
    switch (key.getKeyChar()) {
      case KeyEvent.VK_ESCAPE:
        System.exit(0);
        break;
    }
    canvas.display();
  }

  public void keyReleased(KeyEvent key)
  {
  }

}

Source: Kiet Le's The Red Book Examples using JOGL