private final int gluScaleImageJava(
     int format,
     int widthin,
     int heightin,
     int typein,
     Buffer datain,
     int widthout,
     int heightout,
     int typeout,
     Buffer dataout) {
   ByteBuffer in = null;
   ByteBuffer out = null;
   in = copyToByteBuffer(datain);
   if (dataout instanceof ByteBuffer) {
     out = (ByteBuffer) dataout;
   } else if (dataout instanceof ShortBuffer) {
     out = Buffers.newDirectByteBuffer(dataout.remaining() * Buffers.SIZEOF_SHORT);
   } else if (dataout instanceof IntBuffer) {
     out = Buffers.newDirectByteBuffer(dataout.remaining() * Buffers.SIZEOF_INT);
   } else if (dataout instanceof FloatBuffer) {
     out = Buffers.newDirectByteBuffer(dataout.remaining() * Buffers.SIZEOF_FLOAT);
   } else {
     throw new IllegalArgumentException(
         "Unsupported destination buffer type (must be byte, short, int, or float)");
   }
   int errno =
       Mipmap.gluScaleImage(
           getCurrentGL2(),
           format,
           widthin,
           heightin,
           typein,
           in,
           widthout,
           heightout,
           typeout,
           out);
   if (errno == 0) {
     out.rewind();
     if (out != dataout) {
       if (dataout instanceof ShortBuffer) {
         ((ShortBuffer) dataout).put(out.asShortBuffer());
       } else if (dataout instanceof IntBuffer) {
         ((IntBuffer) dataout).put(out.asIntBuffer());
       } else if (dataout instanceof FloatBuffer) {
         ((FloatBuffer) dataout).put(out.asFloatBuffer());
       } else {
         throw new RuntimeException("Should not reach here");
       }
     }
   }
   return (errno);
 }
 private final ByteBuffer copyToByteBuffer(Buffer buf) {
   if (buf instanceof ByteBuffer) {
     if (buf.position() == 0) {
       return (ByteBuffer) buf;
     }
     return Buffers.copyByteBuffer((ByteBuffer) buf);
   } else if (buf instanceof ShortBuffer) {
     return Buffers.copyShortBufferAsByteBuffer((ShortBuffer) buf);
   } else if (buf instanceof IntBuffer) {
     return Buffers.copyIntBufferAsByteBuffer((IntBuffer) buf);
   } else if (buf instanceof FloatBuffer) {
     return Buffers.copyFloatBufferAsByteBuffer((FloatBuffer) buf);
   } else {
     throw new IllegalArgumentException(
         "Unsupported buffer type (must be one of byte, short, int, or float)");
   }
 }