TurboJPEG 3 API overhaul

(ChangeLog update forthcoming)

- Prefix all function names with "tj3" and remove version suffixes from
  function names.  (Future API overhauls will increment the prefix to
  "tj4", etc., thus retaining backward API/ABI compatibility without
  versioning each individual function.)

- Replace stateless boolean flags (including TJ*FLAG_ARITHMETIC and
  TJ*FLAG_LOSSLESS, which were never released) with stateful integer
  parameters, the value of which persists between function calls.
  * Use parameters for the JPEG quality and subsampling as well, in
    order to eliminate the awkwardness of specifying function arguments
    that weren't relevant for lossless compression.
  * tj3DecompressHeader() now stores all relevant information about the
    JPEG image, including the width, height, subsampling type, entropy
    coding type, etc. in parameters rather than returning that
    information in its arguments.
  * TJ*FLAG_LIMITSCANS has been reimplemented as an integer parameter
    (TJ*PARAM_SCANLIMIT) that allows the number of scans to be
    specified.

- Use the const keyword for all pointer arguments to unmodified
  buffers, as well as for both dimensions of 2D pointers.  Addresses
  #395.

- Use size_t rather than unsigned long to represent buffer sizes, since
  unsigned long is a 32-bit type on Windows.  Addresses #24.

- Return 0 from all buffer size functions if an error occurs, rather
  than awkwardly trying to return -1 in an unsigned data type.

- Implement 12-bit and 16-bit data precision using dedicated
  compression, decompression, and image I/O functions/methods.
  * Suffix the names of all data-precision-specific functions with 8,
    12, or 16.
  * Because the YUV functions are intended to be used for video, they
    are currently only implemented with 8-bit data precision, but they
    can be expanded to 12-bit data precision in the future, if
    necessary.
  * Extend TJUnitTest and TJBench to test 12-bit and 16-bit data
    precision, using a new -precision option.
  * Add appropriate regression tests for all of the above to the 'test'
    target.
  * Extend tjbenchtest to test 12-bit and 16-bit data precision, and
    add separate 'tjtest12' and 'tjtest16' targets.
  * BufferedImage I/O in the Java API is currently limited to 8-bit
    data precision, since the BufferedImage class does not
    straightforwardly support higher data precisions.
  * Extend the PPM reader to convert 12-bit and 16-bit PBMPLUS files
    to grayscale or CMYK pixels, as it already does for 8-bit files.

- Properly accommodate lossless JPEG using dedicated parameters
  (TJ*PARAM_LOSSLESS, TJ*PARAM_LOSSLESSPSV, and TJ*PARAM_LOSSLESSPT),
  rather than using a flag and awkwardly repurposing the JPEG quality.
  Update TJBench to properly reflect whether a JPEG image is lossless.

- Re-organize the TJBench usage screen.

- Update the Java docs using Java 11, to improve the formatting and
  eliminate HTML frames.

- Use the accurate integer DCT algorithm by default for both
  compression and decompression, since the "fast" algorithm is a legacy
  feature, it does not pass the ISO compliance tests, and it is not
  actually faster on modern x86 CPUs.
  * Remove the -accuratedct option from TJBench and TJExample.

- Re-implement the 'tjtest' target using a CMake script that enables
  the appropriate tests, depending on the data precision and whether or
  not the Java API is part of the build.

- Consolidate the C and Java versions of tjbenchtest into one script.

- Consolidate the C and Java versions of tjexampletest into one script.

- Combine all initialization functions into a single function
  (tj3Init()) that accepts an integer parameter specifying the
  subsystems to initialize.

- Enable decompression scaling explicitly, using a new function/method
  (tj3SetScalingFactor()/TJDecompressor.setScalingFactor()), rather
  than implicitly using awkward "desired width"/"desired height"
  parameters.

- Introduce a new macro/constant (TJUNSCALED/TJ.UNSCALED) that maps to
  a scaling factor of 1/1.

- Implement partial image decompression, using a new function/method
  (tj3SetCroppingRegion()/TJDecompressor.setCroppingRegion()) and
  TJBench option (-crop).  Extend tjbenchtest to test the new feature.
  Addresses #1.

- Allow the JPEG colorspace to be specified explicitly when
  compressing, using a new parameter (TJ*PARAM_COLORSPACE).  This
  allows JPEG images with the RGB and CMYK colorspaces to be created.

- Remove the error/difference image feature from TJBench.  Identical
  images to the ones that TJBench created can be generated using
  ImageMagick with
  'magick composite <original_image> <output_image> -compose difference <diff_image>'

- Handle JPEG images with unknown subsampling types.  TJ*PARAM_SUBSAMP
  is set to TJ*SAMP_UNKNOWN (== -1) for such images, but they can still
  be decompressed fully into packed-pixel images or losslessly
  transformed (with the exception of lossless cropping.)  They cannot
  be partially decompressed or decompressed into planar YUV images.
  Note also that TJBench, due to its lack of support for imperfect
  transforms, requires that the subsampling type be known when
  rotating, flipping, or transversely transposing an image.  Addresses
  #436

- The Java version of TJBench now has identical functionality to the C
  version.  This was accomplished by (somewhat hackishly) calling the
  TurboJPEG C image I/O functions through JNI and copying the pixels
  between the C heap and the Java heap.

- Add parameters (TJ*PARAM_RESTARTROWS and TJ*PARAM_RESTARTBLOCKS) and
  a TJBench option (-restart) to allow the restart marker interval to
  be specified when compressing.  Eliminate the undocumented TJ_RESTART
  environment variable.

- Add a parameter (TJ*PARAM_OPTIMIZE), a transform option
  (TJ*OPT_OPTIMIZE), and a TJBench option (-optimize) to allow
  optimized baseline Huffman coding to be specified when compressing.
  Eliminate the undocumented TJ_OPTIMIZE environment variable.

- Add parameters (TJ*PARAM_XDENSITY, TJ*PARAM_DENSITY, and
  TJ*DENSITYUNITS) to allow the pixel density to be specified when
  compressing or saving a Windows BMP image and to be queried when
  decompressing or loading a Windows BMP image.  Addresses #77.

- Refactor the fuzz targets to use the new API.
  * Extend decompression coverage to 12-bit and 16-bit data precision.
  * Replace the awkward cjpeg12 and cjpeg16 targets with proper
    TurboJPEG-based compress12, compress12-lossless, and
    compress16-lossless targets

- Fix innocuous UBSan warnings uncovered by the new fuzzers.

- Implement previous versions of the TurboJPEG API by wrapping the new
  functions (tested by running the 2.1.x versions of TJBench, via
  tjbenchtest, and TJUnitTest against the new implementation.)
  * Remove all JNI functions for deprecated Java methods and implement
    the deprecated methods using pure Java wrappers.  It should be
    understood that backward API compatibility in Java applies only to
    the Java classes and that one cannot mix and match a JAR file from
    one version of libjpeg-turbo with a JNI library from another
    version.

- tj3Destroy() now silently accepts a NULL handle.

- tj3Alloc() and tj3Free() now return/accept void pointers, as malloc()
  and free() do.

- The image I/O functions now accept a TurboJPEG instance handle, which
  is used to transmit/receive parameters and to receive error
  information.

Closes #517
This commit is contained in:
DRC
2023-01-05 06:36:46 -06:00
parent 1a1ea4eeba
commit fc01f4673b
132 changed files with 41404 additions and 10163 deletions

View File

@@ -49,10 +49,13 @@ final class TJUnitTest {
System.out.println("\nUSAGE: java " + CLASS_NAME + " [options]\n");
System.out.println("Options:");
System.out.println("-yuv = test YUV encoding/compression/decompression/decoding");
System.out.println(" (8-bit data precision only)");
System.out.println("-noyuvpad = do not pad each row in each Y, U, and V plane to the nearest");
System.out.println(" multiple of 4 bytes");
System.out.println("-precision N = test N-bit data precision (N is 8, 12, or 16; default is 8; if N");
System.out.println(" is 16, then -lossless is implied)");
System.out.println("-lossless = test lossless JPEG compression/decompression");
System.out.println("-bi = test BufferedImage I/O\n");
System.out.println("-bi = test BufferedImage I/O (8-bit data precision only)\n");
System.exit(1);
}
@@ -68,13 +71,13 @@ final class TJUnitTest {
"RGBA", "BGRA", "ABGR", "ARGB", "CMYK"
};
static final int[] FORMATS_3BYTE = {
static final int[] FORMATS_3SAMPLE = {
TJ.PF_RGB, TJ.PF_BGR
};
static final int[] FORMATS_3BYTEBI = {
BufferedImage.TYPE_3BYTE_BGR
};
static final int[] FORMATS_4BYTE = {
static final int[] FORMATS_4SAMPLE = {
TJ.PF_RGBX, TJ.PF_BGRX, TJ.PF_XBGR, TJ.PF_XRGB, TJ.PF_CMYK
};
static final int[] FORMATS_4BYTEBI = {
@@ -96,6 +99,8 @@ final class TJUnitTest {
private static boolean lossless = false;
private static int psv = 1;
private static int yuvAlign = 4;
private static int precision = 8;
private static int sampleSize, maxSample, tolerance, redToY, yellowToY;
private static boolean bi = false;
private static int exitStatus = 0;
@@ -145,8 +150,22 @@ final class TJUnitTest {
}
}
static void initBuf(byte[] buf, int w, int pitch, int h, int pf, int flags)
throws Exception {
static void fillArray(Object buf, int val) {
if (precision == 8)
Arrays.fill((byte[])buf, (byte)val);
else
Arrays.fill((short[])buf, (short)val);
}
static void setVal(Object buf, int index, int value) {
if (precision == 8)
((byte[])buf)[index] = (byte)value;
else
((short[])buf)[index] = (short)value;
}
static void initBuf(Object buf, int w, int pitch, int h, int pf,
boolean bottomUp) throws Exception {
int roffset = TJ.getRedOffset(pf);
int goffset = TJ.getGreenOffset(pf);
int boffset = TJ.getBlueOffset(pf);
@@ -155,67 +174,67 @@ final class TJUnitTest {
int index, row, col, halfway = 16;
if (pf == TJ.PF_GRAY) {
Arrays.fill(buf, (byte)0);
fillArray(buf, 0);
for (row = 0; row < h; row++) {
for (col = 0; col < w; col++) {
if ((flags & TJ.FLAG_BOTTOMUP) != 0)
if (bottomUp)
index = pitch * (h - row - 1) + col;
else
index = pitch * row + col;
if (((row / 8) + (col / 8)) % 2 == 0)
buf[index] = (row < halfway) ? (byte)255 : 0;
setVal(buf, index, (row < halfway) ? maxSample : 0);
else
buf[index] = (row < halfway) ? 76 : (byte)226;
setVal(buf, index, (row < halfway) ? redToY : yellowToY);
}
}
return;
}
if (pf == TJ.PF_CMYK) {
Arrays.fill(buf, (byte)255);
fillArray(buf, maxSample);
for (row = 0; row < h; row++) {
for (col = 0; col < w; col++) {
if ((flags & TJ.FLAG_BOTTOMUP) != 0)
if (bottomUp)
index = (h - row - 1) * w + col;
else
index = row * w + col;
if (((row / 8) + (col / 8)) % 2 == 0) {
if (row >= halfway) buf[index * ps + 3] = 0;
if (row >= halfway) setVal(buf, index * ps + 3, 0);
} else {
buf[index * ps + 2] = 0;
setVal(buf, index * ps + 2, 0);
if (row < halfway)
buf[index * ps + 1] = 0;
setVal(buf, index * ps + 1, 0);
}
}
}
return;
}
Arrays.fill(buf, (byte)0);
fillArray(buf, 0);
for (row = 0; row < h; row++) {
for (col = 0; col < w; col++) {
if ((flags & TJ.FLAG_BOTTOMUP) != 0)
if (bottomUp)
index = pitch * (h - row - 1) + col * ps;
else
index = pitch * row + col * ps;
if (((row / 8) + (col / 8)) % 2 == 0) {
if (row < halfway) {
buf[index + roffset] = (byte)255;
buf[index + goffset] = (byte)255;
buf[index + boffset] = (byte)255;
setVal(buf, index + roffset, maxSample);
setVal(buf, index + goffset, maxSample);
setVal(buf, index + boffset, maxSample);
}
} else {
buf[index + roffset] = (byte)255;
setVal(buf, index + roffset, maxSample);
if (row >= halfway)
buf[index + goffset] = (byte)255;
setVal(buf, index + goffset, maxSample);
}
if (aoffset >= 0)
buf[index + aoffset] = (byte)255;
setVal(buf, index + aoffset, maxSample);
}
}
}
static void initIntBuf(int[] buf, int w, int pitch, int h, int pf, int flags)
throws Exception {
static void initIntBuf(int[] buf, int w, int pitch, int h, int pf,
boolean bottomUp) throws Exception {
int rshift = TJ.getRedOffset(pf) * 8;
int gshift = TJ.getGreenOffset(pf) * 8;
int bshift = TJ.getBlueOffset(pf) * 8;
@@ -225,7 +244,7 @@ final class TJUnitTest {
Arrays.fill(buf, 0);
for (row = 0; row < h; row++) {
for (col = 0; col < w; col++) {
if ((flags & TJ.FLAG_BOTTOMUP) != 0)
if (bottomUp)
index = pitch * (h - row - 1) + col;
else
index = pitch * row + col;
@@ -246,7 +265,8 @@ final class TJUnitTest {
}
}
static void initImg(BufferedImage img, int pf, int flags) throws Exception {
static void initImg(BufferedImage img, int pf, boolean bottomUp)
throws Exception {
WritableRaster wr = img.getRaster();
int imgType = img.getType();
@@ -259,20 +279,20 @@ final class TJUnitTest {
int pitch = sm.getScanlineStride();
DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
int[] buf = db.getData();
initIntBuf(buf, img.getWidth(), pitch, img.getHeight(), pf, flags);
initIntBuf(buf, img.getWidth(), pitch, img.getHeight(), pf, bottomUp);
} else {
ComponentSampleModel sm = (ComponentSampleModel)img.getSampleModel();
int pitch = sm.getScanlineStride();
DataBufferByte db = (DataBufferByte)wr.getDataBuffer();
byte[] buf = db.getData();
initBuf(buf, img.getWidth(), pitch, img.getHeight(), pf, flags);
initBuf(buf, img.getWidth(), pitch, img.getHeight(), pf, bottomUp);
}
}
static void checkVal(int row, int col, int v, String vname, int cv)
throws Exception {
v = (v < 0) ? v + 256 : v;
if (v < cv - (lossless ? 0 : 1) || v > cv + (lossless ? 0 : 1)) {
if (v < cv - tolerance || v > cv + tolerance) {
throw new Exception("Comp. " + vname + " at " + row + "," + col +
" should be " + cv + ", not " + v);
}
@@ -281,23 +301,34 @@ final class TJUnitTest {
static void checkVal0(int row, int col, int v, String vname)
throws Exception {
v = (v < 0) ? v + 256 : v;
if (v > (lossless ? 0 : 1)) {
if (v > tolerance) {
throw new Exception("Comp. " + vname + " at " + row + "," + col +
" should be 0, not " + v);
}
}
static void checkVal255(int row, int col, int v, String vname)
static void checkValMax(int row, int col, int v, String vname)
throws Exception {
v = (v < 0) ? v + 256 : v;
if (v < 255 - (lossless ? 0 : 1)) {
if (v < maxSample - tolerance) {
throw new Exception("Comp. " + vname + " at " + row + "," + col +
" should be 255, not " + v);
" should be " + maxSample + ", not " + v);
}
}
static int checkBuf(byte[] buf, int w, int pitch, int h, int pf, int subsamp,
TJScalingFactor sf, int flags) throws Exception {
static int getVal(Object buf, int index) {
int v;
if (precision == 8)
v = (int)(((byte[])buf)[index]);
else
v = (int)(((short[])buf)[index]);
if (v < 0)
v += maxSample + 1;
return v;
}
static int checkBuf(Object buf, int w, int pitch, int h, int pf, int subsamp,
TJScalingFactor sf, boolean bottomUp) throws Exception {
int roffset = TJ.getRedOffset(pf);
int goffset = TJ.getGreenOffset(pf);
int boffset = TJ.getBlueOffset(pf);
@@ -315,29 +346,29 @@ final class TJUnitTest {
if (pf == TJ.PF_CMYK) {
for (row = 0; row < h; row++) {
for (col = 0; col < w; col++) {
if ((flags & TJ.FLAG_BOTTOMUP) != 0)
if (bottomUp)
index = (h - row - 1) * w + col;
else
index = row * w + col;
byte c = buf[index * ps];
byte m = buf[index * ps + 1];
byte y = buf[index * ps + 2];
byte k = buf[index * ps + 3];
checkVal255(row, col, c, "C");
int c = getVal(buf, index * ps);
int m = getVal(buf, index * ps + 1);
int y = getVal(buf, index * ps + 2);
int k = getVal(buf, index * ps + 3);
checkValMax(row, col, c, "C");
if (((row / blockSize) + (col / blockSize)) % 2 == 0) {
checkVal255(row, col, m, "M");
checkVal255(row, col, y, "Y");
checkValMax(row, col, m, "M");
checkValMax(row, col, y, "Y");
if (row < halfway)
checkVal255(row, col, k, "K");
checkValMax(row, col, k, "K");
else
checkVal0(row, col, k, "K");
} else {
checkVal0(row, col, y, "Y");
checkVal255(row, col, k, "K");
checkValMax(row, col, k, "K");
if (row < halfway)
checkVal0(row, col, m, "M");
else
checkVal255(row, col, m, "M");
checkValMax(row, col, m, "M");
}
}
}
@@ -346,19 +377,19 @@ final class TJUnitTest {
for (row = 0; row < halfway; row++) {
for (col = 0; col < w; col++) {
if ((flags & TJ.FLAG_BOTTOMUP) != 0)
if (bottomUp)
index = pitch * (h - row - 1) + col * ps;
else
index = pitch * row + col * ps;
byte r = buf[index + roffset];
byte g = buf[index + goffset];
byte b = buf[index + boffset];
byte a = aoffset >= 0 ? buf[index + aoffset] : (byte)255;
int r = getVal(buf, index + roffset);
int g = getVal(buf, index + goffset);
int b = getVal(buf, index + boffset);
int a = aoffset >= 0 ? getVal(buf, index + aoffset) : maxSample;
if (((row / blockSize) + (col / blockSize)) % 2 == 0) {
if (row < halfway) {
checkVal255(row, col, r, "R");
checkVal255(row, col, g, "G");
checkVal255(row, col, b, "B");
checkValMax(row, col, r, "R");
checkValMax(row, col, g, "G");
checkValMax(row, col, b, "B");
} else {
checkVal0(row, col, r, "R");
checkVal0(row, col, g, "G");
@@ -367,25 +398,25 @@ final class TJUnitTest {
} else {
if (subsamp == TJ.SAMP_GRAY) {
if (row < halfway) {
checkVal(row, col, r, "R", 76);
checkVal(row, col, g, "G", 76);
checkVal(row, col, b, "B", 76);
checkVal(row, col, r, "R", redToY);
checkVal(row, col, g, "G", redToY);
checkVal(row, col, b, "B", redToY);
} else {
checkVal(row, col, r, "R", 226);
checkVal(row, col, g, "G", 226);
checkVal(row, col, b, "B", 226);
checkVal(row, col, r, "R", yellowToY);
checkVal(row, col, g, "G", yellowToY);
checkVal(row, col, b, "B", yellowToY);
}
} else {
checkVal255(row, col, r, "R");
checkValMax(row, col, r, "R");
if (row < halfway) {
checkVal0(row, col, g, "G");
} else {
checkVal255(row, col, g, "G");
checkValMax(row, col, g, "G");
}
checkVal0(row, col, b, "B");
}
}
checkVal255(row, col, a, "A");
checkValMax(row, col, a, "A");
}
}
} catch (Exception e) {
@@ -397,22 +428,15 @@ final class TJUnitTest {
for (row = 0; row < h; row++) {
for (col = 0; col < w; col++) {
if (pf == TJ.PF_CMYK) {
int c = buf[pitch * row + col * ps];
int m = buf[pitch * row + col * ps + 1];
int y = buf[pitch * row + col * ps + 2];
int k = buf[pitch * row + col * ps + 3];
if (c < 0) c += 256;
if (m < 0) m += 256;
if (y < 0) y += 256;
if (k < 0) k += 256;
int c = getVal(buf, pitch * row + col * ps);
int m = getVal(buf, pitch * row + col * ps + 1);
int y = getVal(buf, pitch * row + col * ps + 2);
int k = getVal(buf, pitch * row + col * ps + 3);
System.out.format("%3d/%3d/%3d/%3d ", c, m, y, k);
} else {
int r = buf[pitch * row + col * ps + roffset];
int g = buf[pitch * row + col * ps + goffset];
int b = buf[pitch * row + col * ps + boffset];
if (r < 0) r += 256;
if (g < 0) g += 256;
if (b < 0) b += 256;
int r = getVal(buf, pitch * row + col * ps + roffset);
int g = getVal(buf, pitch * row + col * ps + goffset);
int b = getVal(buf, pitch * row + col * ps + boffset);
System.out.format("%3d/%3d/%3d ", r, g, b);
}
}
@@ -423,7 +447,7 @@ final class TJUnitTest {
}
static int checkIntBuf(int[] buf, int w, int pitch, int h, int pf,
int subsamp, TJScalingFactor sf, int flags)
int subsamp, TJScalingFactor sf, boolean bottomUp)
throws Exception {
int rshift = TJ.getRedOffset(pf) * 8;
int gshift = TJ.getGreenOffset(pf) * 8;
@@ -436,7 +460,7 @@ final class TJUnitTest {
try {
for (row = 0; row < halfway; row++) {
for (col = 0; col < w; col++) {
if ((flags & TJ.FLAG_BOTTOMUP) != 0)
if (bottomUp)
index = pitch * (h - row - 1) + col;
else
index = pitch * row + col;
@@ -446,9 +470,9 @@ final class TJUnitTest {
int a = ashift >= 0 ? (buf[index] >> ashift) & 0xFF : 255;
if (((row / blockSize) + (col / blockSize)) % 2 == 0) {
if (row < halfway) {
checkVal255(row, col, r, "R");
checkVal255(row, col, g, "G");
checkVal255(row, col, b, "B");
checkValMax(row, col, r, "R");
checkValMax(row, col, g, "G");
checkValMax(row, col, b, "B");
} else {
checkVal0(row, col, r, "R");
checkVal0(row, col, g, "G");
@@ -466,16 +490,16 @@ final class TJUnitTest {
checkVal(row, col, b, "B", 226);
}
} else {
checkVal255(row, col, r, "R");
checkValMax(row, col, r, "R");
if (row < halfway) {
checkVal0(row, col, g, "G");
} else {
checkVal255(row, col, g, "G");
checkValMax(row, col, g, "G");
}
checkVal0(row, col, b, "B");
}
}
checkVal255(row, col, a, "A");
checkValMax(row, col, a, "A");
}
}
} catch (Exception e) {
@@ -501,7 +525,7 @@ final class TJUnitTest {
}
static int checkImg(BufferedImage img, int pf, int subsamp,
TJScalingFactor sf, int flags) throws Exception {
TJScalingFactor sf, boolean bottomUp) throws Exception {
WritableRaster wr = img.getRaster();
int imgType = img.getType();
if (imgType == BufferedImage.TYPE_INT_RGB ||
@@ -514,14 +538,14 @@ final class TJUnitTest {
DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
int[] buf = db.getData();
return checkIntBuf(buf, img.getWidth(), pitch, img.getHeight(), pf,
subsamp, sf, flags);
subsamp, sf, bottomUp);
} else {
ComponentSampleModel sm = (ComponentSampleModel)img.getSampleModel();
int pitch = sm.getScanlineStride();
DataBufferByte db = (DataBufferByte)wr.getDataBuffer();
byte[] buf = db.getData();
return checkBuf(buf, img.getWidth(), pitch, img.getHeight(), pf, subsamp,
sf, flags);
sf, bottomUp);
}
}
@@ -552,7 +576,7 @@ final class TJUnitTest {
byte y = buf[ypitch * row + col];
if (((row / blockSize) + (col / blockSize)) % 2 == 0) {
if (row < halfway)
checkVal255(row, col, y, "Y");
checkValMax(row, col, y, "Y");
else
checkVal0(row, col, y, "Y");
} else {
@@ -575,7 +599,7 @@ final class TJUnitTest {
} else {
if (row < halfway) {
checkVal(row, col, u, "U", 85);
checkVal255(row, col, v, "V");
checkValMax(row, col, v, "V");
} else {
checkVal0(row, col, u, "U");
checkVal(row, col, v, "V", 149);
@@ -630,15 +654,17 @@ final class TJUnitTest {
}
static int compTest(TJCompressor tjc, byte[] dstBuf, int w, int h, int pf,
String baseName, int subsamp, int jpegQual, int flags)
throws Exception {
String baseName) throws Exception {
String tempStr;
byte[] srcBuf = null;
Object srcBuf = null;
BufferedImage img = null;
String pfStr, pfStrLong;
String buStr = (flags & TJ.FLAG_BOTTOMUP) != 0 ? "BU" : "TD";
String buStrLong = (flags & TJ.FLAG_BOTTOMUP) != 0 ?
"Bottom-Up" : "Top-Down ";
boolean bottomUp = (tjc.get(TJ.PARAM_BOTTOMUP) == 1);
int subsamp = tjc.get(TJ.PARAM_SUBSAMP);
int jpegQual = tjc.get(TJ.PARAM_QUALITY);
int jpegPSV = tjc.get(TJ.PARAM_LOSSLESSPSV);
String buStr = bottomUp ? "BU" : "TD";
String buStrLong = bottomUp ? "Bottom-Up" : "Top-Down ";
int size = 0, ps, imgType = pf;
if (bi) {
@@ -653,25 +679,31 @@ final class TJUnitTest {
if (bi) {
img = new BufferedImage(w, h, imgType);
initImg(img, pf, flags);
tempStr = baseName + "_enc_" + pfStr + "_" + buStr + "_" +
SUBNAME[subsamp] + "_Q" + jpegQual + ".png";
initImg(img, pf, bottomUp);
tempStr = baseName + "_enc" + precision + "_" + pfStr + "_" + buStr +
"_" + SUBNAME[subsamp] + "_Q" + jpegQual + ".png";
File file = new File(tempStr);
ImageIO.write(img, "png", file);
tjc.setSourceImage(img, 0, 0, 0, 0);
} else {
srcBuf = new byte[w * h * ps + 1];
initBuf(srcBuf, w, w * ps, h, pf, flags);
tjc.setSourceImage(srcBuf, 0, 0, w, 0, h, pf);
if (precision == 8)
srcBuf = new byte[w * h * ps + 1];
else
srcBuf = new short[w * h * ps + 1];
initBuf(srcBuf, w, w * ps, h, pf, bottomUp);
if (precision == 8)
tjc.setSourceImage((byte[])srcBuf, 0, 0, w, 0, h, pf);
else if (precision == 12)
tjc.setSourceImage12((short[])srcBuf, 0, 0, w, 0, h, pf);
else
tjc.setSourceImage16((short[])srcBuf, 0, 0, w, 0, h, pf);
}
Arrays.fill(dstBuf, (byte)0);
tjc.setSubsamp(subsamp);
tjc.setJPEGQuality(jpegQual);
if (doYUV) {
System.out.format("%s %s -> YUV %s ... ", pfStrLong, buStrLong,
SUBNAME_LONG[subsamp]);
YUVImage yuvImage = tjc.encodeYUV(yuvAlign, flags);
YUVImage yuvImage = tjc.encodeYUV(yuvAlign);
if (checkBufYUV(yuvImage.getBuf(), yuvImage.getSize(), w, h, subsamp,
new TJScalingFactor(1, 1)) == 1)
System.out.print("Passed.\n");
@@ -684,14 +716,22 @@ final class TJUnitTest {
buStrLong, jpegQual);
tjc.setSourceImage(yuvImage);
} else {
System.out.format("%s %s -> %s Q%d ... ", pfStrLong, buStrLong,
SUBNAME_LONG[subsamp], jpegQual);
if (lossless)
System.out.format("%s %s -> LOSSLESS PSV%d ... ", pfStrLong, buStrLong,
jpegPSV);
else
System.out.format("%s %s -> %s Q%d ... ", pfStrLong, buStrLong,
SUBNAME_LONG[subsamp], jpegQual);
}
tjc.compress(dstBuf, flags);
tjc.compress(dstBuf);
size = tjc.getCompressedSize();
tempStr = baseName + "_enc_" + pfStr + "_" + buStr + "_" +
SUBNAME[subsamp] + "_Q" + jpegQual + ".jpg";
if (lossless)
tempStr = baseName + "_enc" + precision + "_" + pfStr + "_" + buStr +
"_LOSSLESS_PSV" + jpegPSV + ".jpg";
else
tempStr = baseName + "_enc" + precision + "_" + pfStr + "_" + buStr +
"_" + SUBNAME[subsamp] + "_Q" + jpegQual + ".jpg";
writeJPEG(dstBuf, size, tempStr);
System.out.println("Done.\n Result in " + tempStr);
@@ -700,15 +740,15 @@ final class TJUnitTest {
static void decompTest(TJDecompressor tjd, byte[] jpegBuf, int jpegSize,
int w, int h, int pf, String baseName, int subsamp,
int flags, TJScalingFactor sf) throws Exception {
TJScalingFactor sf) throws Exception {
String pfStr, pfStrLong, tempStr;
String buStrLong = (flags & TJ.FLAG_BOTTOMUP) != 0 ?
"Bottom-Up" : "Top-Down ";
boolean bottomUp = (tjd.get(TJ.PARAM_BOTTOMUP) == 1);
String buStrLong = bottomUp ? "Bottom-Up" : "Top-Down ";
int scaledWidth = sf.getScaled(w);
int scaledHeight = sf.getScaled(h);
int temp1, temp2, imgType = pf;
BufferedImage img = null;
byte[] dstBuf = null;
Object dstBuf = null;
if (bi) {
pf = biTypePF(imgType);
@@ -720,26 +760,19 @@ final class TJUnitTest {
}
tjd.setSourceImage(jpegBuf, jpegSize);
tjd.setScalingFactor(sf);
if (lossless && subsamp != TJ.SAMP_444 && subsamp != TJ.SAMP_GRAY)
subsamp = TJ.SAMP_444;
if (tjd.getWidth() != w || tjd.getHeight() != h ||
tjd.getSubsamp() != subsamp)
tjd.get(TJ.PARAM_SUBSAMP) != subsamp)
throw new Exception("Incorrect JPEG header");
temp1 = scaledWidth;
temp2 = scaledHeight;
temp1 = tjd.getScaledWidth(temp1, temp2);
temp2 = tjd.getScaledHeight(temp1, temp2);
if (temp1 != scaledWidth || temp2 != scaledHeight)
throw new Exception("Scaled size mismatch");
if (doYUV) {
System.out.format("JPEG -> YUV %s ", SUBNAME_LONG[subsamp]);
if (!sf.isOne())
System.out.format("%d/%d ... ", sf.getNum(), sf.getDenom());
else System.out.print("... ");
YUVImage yuvImage = tjd.decompressToYUV(scaledWidth, yuvAlign,
scaledHeight, flags);
YUVImage yuvImage = tjd.decompressToYUV(yuvAlign);
if (checkBufYUV(yuvImage.getBuf(), yuvImage.getSize(), scaledWidth,
scaledHeight, subsamp, sf) == 1)
System.out.print("Passed.\n");
@@ -757,23 +790,28 @@ final class TJUnitTest {
else System.out.print("... ");
}
if (bi)
img = tjd.decompress(scaledWidth, scaledHeight, imgType, flags);
else
dstBuf = tjd.decompress(scaledWidth, 0, scaledHeight, pf, flags);
img = tjd.decompress8(imgType);
else {
if (precision == 8)
dstBuf = tjd.decompress8(0, pf);
else if (precision == 12)
dstBuf = tjd.decompress12(0, pf);
else
dstBuf = tjd.decompress16(0, pf);
}
if (bi) {
tempStr = baseName + "_dec_" + pfStr + "_" +
(((flags & TJ.FLAG_BOTTOMUP) != 0) ? "BU" : "TD") + "_" +
SUBNAME[subsamp] + "_" +
tempStr = baseName + "_dec_" + pfStr + "_" + (bottomUp ? "BU" : "TD") +
"_" + SUBNAME[subsamp] + "_" +
(double)sf.getNum() / (double)sf.getDenom() + "x" + ".png";
File file = new File(tempStr);
ImageIO.write(img, "png", file);
}
if ((bi && checkImg(img, pf, subsamp, sf, flags) == 1) ||
if ((bi && checkImg(img, pf, subsamp, sf, bottomUp) == 1) ||
(!bi && checkBuf(dstBuf, scaledWidth,
scaledWidth * TJ.getPixelSize(pf), scaledHeight, pf,
subsamp, sf, flags) == 1))
subsamp, sf, bottomUp) == 1))
System.out.print("Passed.\n");
else {
System.out.print("FAILED!\n");
@@ -782,14 +820,13 @@ final class TJUnitTest {
}
static void decompTest(TJDecompressor tjd, byte[] jpegBuf, int jpegSize,
int w, int h, int pf, String baseName, int subsamp,
int flags) throws Exception {
int w, int h, int pf, String baseName, int subsamp)
throws Exception {
int i;
TJScalingFactor sf1 = new TJScalingFactor(1, 1);
if (lossless) {
decompTest(tjd, jpegBuf, jpegSize, w, h, pf, baseName, subsamp, flags,
sf1);
decompTest(tjd, jpegBuf, jpegSize, w, h, pf, baseName, subsamp,
TJ.UNSCALED);
return;
}
@@ -802,8 +839,7 @@ final class TJUnitTest {
(denom == 2 || denom == 1)) ||
(subsamp != TJ.SAMP_411 && num == 1 &&
(denom == 4 || denom == 2 || denom == 1)))
decompTest(tjd, jpegBuf, jpegSize, w, h, pf, baseName, subsamp,
flags, sf[i]);
decompTest(tjd, jpegBuf, jpegSize, w, h, pf, baseName, subsamp, sf[i]);
}
}
@@ -811,7 +847,7 @@ final class TJUnitTest {
throws Exception {
TJCompressor tjc = null;
TJDecompressor tjd = null;
int size, quality = 100;
int size;
byte[] dstBuf;
if (lossless && subsamp != TJ.SAMP_GRAY)
@@ -823,26 +859,28 @@ final class TJUnitTest {
tjc = new TJCompressor();
tjd = new TJDecompressor();
if (lossless) {
tjc.set(TJ.PARAM_LOSSLESS, 1);
tjc.set(TJ.PARAM_LOSSLESSPSV, ((psv++ - 1) % 7) + 1);
} else {
tjc.set(TJ.PARAM_QUALITY, 100);
if (subsamp == TJ.SAMP_422 || subsamp == TJ.SAMP_420 ||
subsamp == TJ.SAMP_440 || subsamp == TJ.SAMP_411)
tjd.set(TJ.PARAM_FASTUPSAMPLE, 1);
}
tjc.set(TJ.PARAM_SUBSAMP, subsamp);
for (int pf : formats) {
if (pf < 0) continue;
for (int i = 0; i < 2; i++) {
int flags = 0;
if (lossless) {
flags |= TJ.FLAG_LOSSLESS;
quality = (((psv++ - 1) % 7) + 1) * 10;
}
if (subsamp == TJ.SAMP_422 || subsamp == TJ.SAMP_420 ||
subsamp == TJ.SAMP_440 || subsamp == TJ.SAMP_411)
flags |= TJ.FLAG_FASTUPSAMPLE;
if (i == 1)
flags |= TJ.FLAG_BOTTOMUP;
size = compTest(tjc, dstBuf, w, h, pf, baseName, subsamp, quality,
flags);
decompTest(tjd, dstBuf, size, w, h, pf, baseName, subsamp, flags);
tjc.set(TJ.PARAM_BOTTOMUP, i == 1 ? 1 : 0);
tjd.set(TJ.PARAM_BOTTOMUP, i == 1 ? 1 : 0);
size = compTest(tjc, dstBuf, w, h, pf, baseName);
decompTest(tjd, dstBuf, size, w, h, pf, baseName, subsamp);
if (pf >= TJ.PF_RGBX && pf <= TJ.PF_XRGB && !bi) {
System.out.print("\n");
decompTest(tjd, dstBuf, size, w, h, pf + (TJ.PF_RGBA - TJ.PF_RGBX),
baseName, subsamp, flags);
baseName, subsamp);
}
System.out.print("\n");
}
@@ -907,22 +945,25 @@ final class TJUnitTest {
}
static void bufSizeTest() throws Exception {
int w, h, i, subsamp, flags = 0, quality = 100, numSamp = TJ.NUMSAMP;
int w, h, i, subsamp, numSamp = TJ.NUMSAMP;
byte[] srcBuf, dstBuf = null;
YUVImage dstImage = null;
TJCompressor tjc = null;
Random r = new Random();
try {
if (lossless) {
flags |= TJ.FLAG_LOSSLESS;
quality = (((psv++ - 1) % 7) + 1) * 10;
numSamp = 1;
}
tjc = new TJCompressor();
if (lossless) {
tjc.set(TJ.PARAM_LOSSLESS, 1);
tjc.set(TJ.PARAM_LOSSLESSPSV, ((psv++ - 1) % 7) + 1);
numSamp = 1;
} else
tjc.set(TJ.PARAM_QUALITY, 100);
System.out.println("Buffer size regression test");
for (subsamp = 0; subsamp < numSamp; subsamp++) {
tjc.set(TJ.PARAM_SUBSAMP, subsamp);
for (w = 1; w < 48; w++) {
int maxh = (w == 1) ? 2048 : 48;
for (h = 1; h < maxh; h++) {
@@ -937,12 +978,10 @@ final class TJUnitTest {
srcBuf[i] = (byte)(r.nextInt(2) * 255);
}
tjc.setSourceImage(srcBuf, 0, 0, w, 0, h, TJ.PF_BGRX);
tjc.setSubsamp(subsamp);
tjc.setJPEGQuality(quality);
if (doYUV)
tjc.encodeYUV(dstImage, 0);
tjc.encodeYUV(dstImage);
else
tjc.compress(dstBuf, flags);
tjc.compress(dstBuf);
srcBuf = new byte[h * w * 4];
if (doYUV)
@@ -954,9 +993,9 @@ final class TJUnitTest {
}
tjc.setSourceImage(srcBuf, 0, 0, h, 0, w, TJ.PF_BGRX);
if (doYUV)
tjc.encodeYUV(dstImage, 0);
tjc.encodeYUV(dstImage);
else
tjc.compress(dstBuf, flags);
tjc.compress(dstBuf);
}
dstImage = null;
dstBuf = null;
@@ -984,43 +1023,67 @@ final class TJUnitTest {
else if (argv[i].equalsIgnoreCase("-bi")) {
bi = true;
testName = "javabitest";
} else if (argv[i].equalsIgnoreCase("-precision") &&
i < argv.length - 1) {
int tempi = -1;
try {
tempi = Integer.parseInt(argv[++i]);
} catch (NumberFormatException e) {}
if (tempi != 8 && tempi != 12 && tempi != 16)
usage();
precision = tempi;
if (precision == 16)
lossless = true;
} else
usage();
}
if (lossless && doYUV)
throw new Exception("Lossless JPEG and YUV encoding/decoding are incompatible.");
if (precision != 8 && doYUV)
throw new Exception("YUV encoding/decoding requires 8-bit data precision.");
if (precision != 8 && bi)
throw new Exception("BufferedImage support requires 8-bit data precision.");
System.out.format("Testing %d-bit precision\n", precision);
sampleSize = (precision == 8 ? 1 : 2);
maxSample = (1 << precision) - 1;
tolerance = (lossless ? 0 : (precision > 8 ? 2 : 1));
redToY = (19595 * maxSample) >> 16;
yellowToY = (58065 * maxSample) >> 16;
if (doYUV)
FORMATS_4BYTE[4] = -1;
FORMATS_4SAMPLE[4] = -1;
overflowTest();
doTest(35, 39, bi ? FORMATS_3BYTEBI : FORMATS_3BYTE, TJ.SAMP_444,
doTest(35, 39, bi ? FORMATS_3BYTEBI : FORMATS_3SAMPLE, TJ.SAMP_444,
testName);
doTest(39, 41, bi ? FORMATS_4BYTEBI : FORMATS_4BYTE, TJ.SAMP_444,
doTest(39, 41, bi ? FORMATS_4BYTEBI : FORMATS_4SAMPLE, TJ.SAMP_444,
testName);
doTest(41, 35, bi ? FORMATS_3BYTEBI : FORMATS_3BYTE, TJ.SAMP_422,
doTest(41, 35, bi ? FORMATS_3BYTEBI : FORMATS_3SAMPLE, TJ.SAMP_422,
testName);
if (!lossless) {
doTest(35, 39, bi ? FORMATS_4BYTEBI : FORMATS_4BYTE, TJ.SAMP_422,
doTest(35, 39, bi ? FORMATS_4BYTEBI : FORMATS_4SAMPLE, TJ.SAMP_422,
testName);
doTest(39, 41, bi ? FORMATS_3BYTEBI : FORMATS_3BYTE, TJ.SAMP_420,
doTest(39, 41, bi ? FORMATS_3BYTEBI : FORMATS_3SAMPLE, TJ.SAMP_420,
testName);
doTest(41, 35, bi ? FORMATS_4BYTEBI : FORMATS_4BYTE, TJ.SAMP_420,
doTest(41, 35, bi ? FORMATS_4BYTEBI : FORMATS_4SAMPLE, TJ.SAMP_420,
testName);
doTest(35, 39, bi ? FORMATS_3BYTEBI : FORMATS_3BYTE, TJ.SAMP_440,
doTest(35, 39, bi ? FORMATS_3BYTEBI : FORMATS_3SAMPLE, TJ.SAMP_440,
testName);
doTest(39, 41, bi ? FORMATS_4BYTEBI : FORMATS_4BYTE, TJ.SAMP_440,
doTest(39, 41, bi ? FORMATS_4BYTEBI : FORMATS_4SAMPLE, TJ.SAMP_440,
testName);
doTest(41, 35, bi ? FORMATS_3BYTEBI : FORMATS_3BYTE, TJ.SAMP_411,
doTest(41, 35, bi ? FORMATS_3BYTEBI : FORMATS_3SAMPLE, TJ.SAMP_411,
testName);
doTest(35, 39, bi ? FORMATS_4BYTEBI : FORMATS_4BYTE, TJ.SAMP_411,
doTest(35, 39, bi ? FORMATS_4BYTEBI : FORMATS_4SAMPLE, TJ.SAMP_411,
testName);
}
doTest(39, 41, bi ? FORMATS_GRAYBI : FORMATS_GRAY, TJ.SAMP_GRAY,
testName);
if (!lossless) {
doTest(41, 35, bi ? FORMATS_3BYTEBI : FORMATS_3BYTE, TJ.SAMP_GRAY,
doTest(41, 35, bi ? FORMATS_3BYTEBI : FORMATS_3SAMPLE, TJ.SAMP_GRAY,
testName);
FORMATS_4BYTE[4] = -1;
doTest(35, 39, bi ? FORMATS_4BYTEBI : FORMATS_4BYTE, TJ.SAMP_GRAY,
FORMATS_4SAMPLE[4] = -1;
doTest(35, 39, bi ? FORMATS_4BYTEBI : FORMATS_4SAMPLE, TJ.SAMP_GRAY,
testName);
}
if (!bi)