/** * The CodecOptions parameter should have the following fields set: {@link CodecOptions#width * width} {@link CodecOptions#height height} {@link CodecOptions#channels channels} {@link * CodecOptions#bitsPerSample bitsPerSample} {@link CodecOptions#interleaved interleaved} {@link * CodecOptions#littleEndian littleEndian} {@link CodecOptions#signed signed} * * @see Codec#compress(byte[], CodecOptions) */ @Override public byte[] compress(final byte[] data, CodecOptions options) throws FormatException { if (data == null || data.length == 0) return data; if (options == null) options = CodecOptions.getDefaultOptions(); if (options.bitsPerSample > 8) { throw new FormatException("> 8 bit data cannot be compressed with JPEG."); } final ByteArrayOutputStream out = new ByteArrayOutputStream(); final BufferedImage img = AWTImageTools.makeImage( data, options.width, options.height, options.channels, options.interleaved, options.bitsPerSample / 8, false, options.littleEndian, options.signed); try { ImageIO.write(img, "jpeg", out); } catch (final IOException e) { throw new FormatException("Could not write JPEG data", e); } return out.toByteArray(); }