/** * Compress the uncompressed source image stored in <code>srcImage</code> and return a buffer * containing a JPEG image. * * @param srcImage a <code>BufferedImage</code> instance containing RGB or grayscale pixels to be * compressed * @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*} * @return a buffer containing a JPEG image. The length of this buffer will not be equal to the * size of the JPEG image. Use {@link #getCompressedSize} to obtain the size of the JPEG * image. */ public byte[] compress(BufferedImage srcImage, int flags) throws Exception { int width = srcImage.getWidth(); int height = srcImage.getHeight(); byte[] buf = new byte[TJ.bufSize(width, height, subsamp)]; compress(srcImage, buf, flags); return buf; }
private static void bufSizeTest() throws Exception { int w, h, i, subsamp; byte[] srcBuf, jpegBuf; TJCompressor tjc = null; Random r = new Random(); try { tjc = new TJCompressor(); System.out.println("Buffer size regression test"); for (subsamp = 0; subsamp < TJ.NUMSAMP; subsamp++) { for (w = 1; w < 48; w++) { int maxh = (w == 1) ? 2048 : 48; for (h = 1; h < maxh; h++) { if (h % 100 == 0) System.out.format("%04d x %04d\b\b\b\b\b\b\b\b\b\b\b", w, h); srcBuf = new byte[w * h * 4]; jpegBuf = new byte[TJ.bufSize(w, h, subsamp)]; for (i = 0; i < w * h * 4; i++) { srcBuf[i] = (byte) (r.nextInt(2) * 255); } tjc.setSourceImage(srcBuf, w, 0, h, TJ.PF_BGRX); tjc.setSubsamp(subsamp); tjc.setJPEGQuality(100); tjc.compress(jpegBuf, 0); srcBuf = new byte[h * w * 4]; jpegBuf = new byte[TJ.bufSize(h, w, subsamp)]; for (i = 0; i < h * w * 4; i++) { srcBuf[i] = (byte) (r.nextInt(2) * 255); } tjc.setSourceImage(srcBuf, h, 0, w, TJ.PF_BGRX); tjc.compress(jpegBuf, 0); } } } System.out.println("Done. "); } catch (Exception e) { if (tjc != null) tjc.close(); throw e; } if (tjc != null) tjc.close(); }
private static void doTest(int w, int h, int[] formats, int subsamp, String baseName) throws Exception { TJCompressor tjc = null; TJDecompressor tjd = null; int size; byte[] dstBuf; if (yuv == YUVENCODE) dstBuf = new byte[TJ.bufSizeYUV(w, h, subsamp)]; else dstBuf = new byte[TJ.bufSize(w, h, subsamp)]; try { tjc = new TJCompressor(); tjd = new TJDecompressor(); for (int pf : formats) { for (int i = 0; i < 2; i++) { int flags = 0; if (subsamp == TJ.SAMP_422 || subsamp == TJ.SAMP_420 || subsamp == TJ.SAMP_440) flags |= TJ.FLAG_FASTUPSAMPLE; if (i == 1) { if (yuv == YUVDECODE) { tjc.close(); tjd.close(); return; } else flags |= TJ.FLAG_BOTTOMUP; } size = compTest(tjc, dstBuf, w, h, pf, baseName, subsamp, 100, flags); decompTest(tjd, dstBuf, size, w, h, pf, baseName, subsamp, flags); if (pf >= TJ.PF_RGBX && pf <= TJ.PF_XRGB && !bi) decompTest( tjd, dstBuf, size, w, h, pf + (TJ.PF_RGBA - TJ.PF_RGBX), baseName, subsamp, flags); } } } catch (Exception e) { if (tjc != null) tjc.close(); if (tjd != null) tjd.close(); throw e; } if (tjc != null) tjc.close(); if (tjd != null) tjd.close(); }
/** * Compress the uncompressed source image associated with this compressor instance and return a * buffer containing a JPEG image. * * @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*} * @return a buffer containing a JPEG image. The length of this buffer will not be equal to the * size of the JPEG image. Use {@link #getCompressedSize} to obtain the size of the JPEG * image. */ public byte[] compress(int flags) throws Exception { if (srcWidth < 1 || srcHeight < 1) throw new Exception(NO_ASSOC_ERROR); byte[] buf = new byte[TJ.bufSize(srcWidth, srcHeight, subsamp)]; compress(buf, flags); return buf; }