From: DRC Date: Fri, 17 Nov 2017 00:46:01 +0000 (-0600) Subject: Add TurboJPEG C example and clean up Java example X-Git-Tag: 1.5.90~52 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8c40ac8ae68a36a95d66b8eda17a2fbd0669d707;p=libjpeg-turbo Add TurboJPEG C example and clean up Java example Also rename example.c --> example.txt and add a disclaimer to that file so people will stop trying to compile it. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index fed6a4c..bd8f57d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -576,6 +576,9 @@ if(WITH_TURBOJPEG) if(UNIX) target_link_libraries(tjbench m) endif() + + add_executable(tjexample tjexample.c) + target_link_libraries(tjexample turbojpeg) endif() if(ENABLE_STATIC) @@ -1242,12 +1245,13 @@ add_custom_target(testclean COMMAND ${CMAKE_COMMAND} -P if(WITH_TURBOJPEG) configure_file(tjbenchtest.in tjbenchtest @ONLY) + configure_file(tjexampletest.in tjexampletest @ONLY) if(WIN32) set(BASH bash) endif() if(WITH_JAVA) configure_file(tjbenchtest.java.in tjbenchtest.java @ONLY) - configure_file(tjexampletest.in tjexampletest @ONLY) + configure_file(tjexampletest.java.in tjexampletest.java @ONLY) add_custom_target(tjtest COMMAND echo tjbenchtest COMMAND ${BASH} ${CMAKE_CURRENT_BINARY_DIR}/tjbenchtest @@ -1259,14 +1263,16 @@ if(WITH_TURBOJPEG) COMMAND ${BASH} ${CMAKE_CURRENT_BINARY_DIR}/tjbenchtest -yuv -alloc COMMAND echo tjbenchtest -progressive COMMAND ${BASH} ${CMAKE_CURRENT_BINARY_DIR}/tjbenchtest -progressive + COMMAND echo tjexampletest + COMMAND ${BASH} ${CMAKE_CURRENT_BINARY_DIR}/tjexampletest COMMAND echo tjbenchtest.java COMMAND ${BASH} ${CMAKE_CURRENT_BINARY_DIR}/tjbenchtest.java COMMAND echo tjbenchtest.java -yuv COMMAND ${BASH} ${CMAKE_CURRENT_BINARY_DIR}/tjbenchtest.java -yuv COMMAND echo tjbenchtest.java -progressive COMMAND ${BASH} ${CMAKE_CURRENT_BINARY_DIR}/tjbenchtest.java -progressive - COMMAND echo tjexampletest - COMMAND ${BASH} ${CMAKE_CURRENT_BINARY_DIR}/tjexampletest + COMMAND echo tjexampletest.java + COMMAND ${BASH} ${CMAKE_CURRENT_BINARY_DIR}/tjexampletest.java DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/tjbenchtest ${CMAKE_CURRENT_BINARY_DIR}/tjbenchtest.java ${CMAKE_CURRENT_BINARY_DIR}/tjexampletest) @@ -1280,6 +1286,8 @@ if(WITH_TURBOJPEG) COMMAND ${BASH} ${CMAKE_CURRENT_BINARY_DIR}/tjbenchtest -yuv COMMAND echo tjbenchtest -yuv -alloc COMMAND ${BASH} ${CMAKE_CURRENT_BINARY_DIR}/tjbenchtest -yuv -alloc + COMMAND echo tjexampletest + COMMAND ${BASH} ${CMAKE_CURRENT_BINARY_DIR}/tjexampletest DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/tjbenchtest) endif() endif() @@ -1327,11 +1335,16 @@ endif() install(TARGETS rdjpgcom wrjpgcom RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/README.ijg - ${CMAKE_CURRENT_SOURCE_DIR}/README.md ${CMAKE_CURRENT_SOURCE_DIR}/example.c + ${CMAKE_CURRENT_SOURCE_DIR}/README.md ${CMAKE_CURRENT_SOURCE_DIR}/example.txt + ${CMAKE_CURRENT_SOURCE_DIR}/tjexample.c ${CMAKE_CURRENT_SOURCE_DIR}/libjpeg.txt ${CMAKE_CURRENT_SOURCE_DIR}/structure.txt ${CMAKE_CURRENT_SOURCE_DIR}/usage.txt ${CMAKE_CURRENT_SOURCE_DIR}/wizard.txt ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE.md DESTINATION ${CMAKE_INSTALL_DOCDIR}) +if(WITH_JAVA) + install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/java/TJExample.java + DESTINATION ${CMAKE_INSTALL_DOCDIR}) +endif() if(UNIX) install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/cjpeg.1 diff --git a/ChangeLog.md b/ChangeLog.md index 599c839..ceea30d 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -120,6 +120,10 @@ same value. In addition, the `tjRedOffset[]`, `tjGreenOffset[]`, and `TJPF_GRAY`/`TJ.PF_GRAY` rather than 0. This allows programs to easily determine whether a pixel format has red, green, blue, and alpha components. +15. Added a new example (tjexample.c) that demonstrates the basic usage of the +TurboJPEG C API. This example mirrors the functionality of TJExample.java. +Both files are now included in the libjpeg-turbo documentation. + 1.5.2 ===== diff --git a/README.ijg b/README.ijg index d9d81b8..a503f68 100644 --- a/README.ijg +++ b/README.ijg @@ -43,7 +43,7 @@ User documentation: change.log Version-to-version change highlights. Programmer and internal documentation: libjpeg.txt How to use the JPEG library in your own programs. - example.c Sample code for calling the JPEG library. + example.txt Sample code for calling the JPEG library. structure.txt Overview of the JPEG library's internal structure. coderules.txt Coding style rules --- please read if you contribute code. diff --git a/README.md b/README.md index d54feb7..80ba6c1 100755 --- a/README.md +++ b/README.md @@ -48,7 +48,9 @@ JPEG images: straightforward to achieve using the underlying libjpeg API, such as generating planar YUV images and performing multiple simultaneous lossless transforms on an image. The Java interface for libjpeg-turbo is written on - top of the TurboJPEG API. + top of the TurboJPEG API. The TurboJPEG API is recommended for first-time + users of libjpeg-turbo. Refer to [tjexample.c](tjexample.c) and + [TJExample.java](java/TJExample.java) for examples of its usage. - **libjpeg API**
This is the de facto industry-standard API for compressing and decompressing diff --git a/example.c b/example.txt similarity index 94% rename from example.c rename to example.txt index ac27f49..d7a0189 100644 --- a/example.c +++ b/example.txt @@ -1,5 +1,5 @@ /* - * example.c + * example.txt * * This file illustrates how to use the IJG code as a subroutine library * to read or write JPEG image files. You should look at this code in @@ -13,6 +13,20 @@ * routines in a different style if you prefer. */ +/* This example was part of the original libjpeg documentation and has been + * unchanged since 1994. It is, as described in libjpeg.txt, "heavily + * commented skeleton code for calling the JPEG library." It is not meant to + * be compiled as a standalone program, since it has no main() function and + * does not compress from/decompress to a real image buffer (corollary: + * put_scanline_someplace() is not a real function.) First-time users of + * libjpeg-turbo would be better served by looking at tjexample.c, which uses + * the more straightforward TurboJPEG API, or at cjpeg.c and djpeg.c, which are + * examples of libjpeg API usage that can be (and are) compiled into standalone + * programs. Note that this example, as well as the examples in cjpeg.c and + * djpeg.c, interleave disk I/O with JPEG compression/decompression, so none of + * these examples is suitable for benchmarking purposes. + */ + #include /* diff --git a/java/TJExample.java b/java/TJExample.java index 835a5b9..c021939 100644 --- a/java/TJExample.java +++ b/java/TJExample.java @@ -28,8 +28,8 @@ */ /* - * This program demonstrates how to compress and decompress JPEG files using - * the TurboJPEG JNI wrapper + * This program demonstrates how to compress, decompress, and transform JPEG + * images using the TurboJPEG Java API */ import java.io.*; @@ -40,138 +40,175 @@ import javax.imageio.*; import javax.swing.*; import org.libjpegturbo.turbojpeg.*; + public class TJExample implements TJCustomFilter { - public static final String classname = new TJExample().getClass().getName(); + private static final String classname = new TJExample().getClass().getName(); + + private static final int DEFAULT_SUBSAMP = TJ.SAMP_444; + private static final int DEFAULT_QUALITY = 95; + + + private static final String[] subsampName = { + "4:4:4", "4:2:2", "4:2:0", "Grayscale", "4:4:0", "4:1:1" + }; + + private static final String[] colorspaceName = { + "RGB", "YCbCr", "GRAY", "CMYK", "YCCK" + }; + + + /* DCT filter example. This produces a negative of the image. */ + + public void customFilter(ShortBuffer coeffBuffer, Rectangle bufferRegion, + Rectangle planeRegion, int componentIndex, + int transformIndex, TJTransform transform) + throws TJException { + for (int i = 0; i < bufferRegion.width * bufferRegion.height; i++) { + coeffBuffer.put(i, (short)(-coeffBuffer.get(i))); + } + } + + + private static final void usage() throws Exception { + System.out.println("\nUSAGE: java [Java options] " + classname + + " [options]\n"); - private static void usage() throws Exception { - System.out.println("\nUSAGE: java " + classname + " [options]\n"); - System.out.println("Input and output files can be any image format that the Java Image I/O"); + System.out.println("Input and output images can be in any image format that the Java Image I/O"); System.out.println("extensions understand. If either filename ends in a .jpg extension, then"); - System.out.println("TurboJPEG will be used to compress or decompress the file.\n"); - System.out.println("Options:\n"); - System.out.println("-scale M/N = if the input image is a JPEG file, scale the width/height of the"); - System.out.print(" output image by a factor of M/N (M/N = "); - for (int i = 0; i < sf.length; i++) { - System.out.print(sf[i].getNum() + "/" + sf[i].getDenom()); - if (sf.length == 2 && i != sf.length - 1) + System.out.println("the TurboJPEG API will be used to compress or decompress the image.\n"); + + System.out.println("Compression Options (used if the output image is a JPEG image)"); + System.out.println("--------------------------------------------------------------\n"); + + System.out.println("-subsamp <444|422|420|gray> = Apply this level of chrominance subsampling when"); + System.out.println(" compressing the output image. The default is to use the same level of"); + System.out.println(" subsampling as in the input image, if the input image is also a JPEG"); + System.out.println(" image, or to use grayscale if the input image is a grayscale non-JPEG"); + System.out.println(" image, or to use " + subsampName[DEFAULT_SUBSAMP] + " subsampling otherwise.\n"); + + System.out.println("-q <1-100> = Compress the output image with this JPEG quality level"); + System.out.println(" (default = " + DEFAULT_QUALITY + ").\n"); + + System.out.println("Decompression Options (used if the input image is a JPEG image)"); + System.out.println("---------------------------------------------------------------\n"); + + System.out.println("-scale M/N = Scale the input image by a factor of M/N when decompressing it."); + System.out.print("(M/N = "); + for (int i = 0; i < scalingFactors.length; i++) { + System.out.print(scalingFactors[i].getNum() + "/" + + scalingFactors[i].getDenom()); + if (scalingFactors.length == 2 && i != scalingFactors.length - 1) System.out.print(" or "); - else if (sf.length > 2) { - if (i != sf.length - 1) + else if (scalingFactors.length > 2) { + if (i != scalingFactors.length - 1) System.out.print(", "); - if (i == sf.length - 2) + if (i == scalingFactors.length - 2) System.out.print("or "); } } System.out.println(")\n"); - System.out.println("-samp <444|422|420|gray> = If the output image is a JPEG file, this specifies"); - System.out.println(" the level of chrominance subsampling to use when"); - System.out.println(" recompressing it. Default is to use the same level"); - System.out.println(" of subsampling as the input, if the input is a JPEG"); - System.out.println(" file, or 4:4:4 otherwise.\n"); - System.out.println("-q <1-100> = If the output image is a JPEG file, this specifies the JPEG"); - System.out.println(" quality to use when recompressing it (default = 95).\n"); + System.out.println("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 ="); - System.out.println(" If the input image is a JPEG file, perform the corresponding lossless"); - System.out.println(" transform prior to decompression (these options are mutually exclusive)\n"); - System.out.println("-grayscale = If the input image is a JPEG file, perform lossless grayscale"); - System.out.println(" conversion prior to decompression (can be combined with the other"); - System.out.println(" transforms above)\n"); - System.out.println("-crop X,Y,WxH = If the input image is a JPEG file, perform lossless cropping"); - System.out.println(" prior to decompression. X,Y specifies the upper left corner of the"); - System.out.println(" cropping region, and WxH specifies its width and height. X,Y must be"); - System.out.println(" evenly divible by the MCU block size (8x8 if the source image was"); - System.out.println(" compressed using no subsampling or grayscale, or 16x8 for 4:2:2 or 16x16"); - System.out.println(" for 4:2:0.)\n"); - System.out.println("-display = Display output image (Output file need not be specified in this"); + System.out.println(" Perform one of these lossless transform operations on the input image"); + System.out.println(" prior to decompressing it (these options are mutually exclusive.)\n"); + + System.out.println("-grayscale = Perform lossless grayscale conversion on the input image prior"); + System.out.println(" to decompressing it (can be combined with the other transform operations"); + System.out.println(" above.)\n"); + + System.out.println("-crop WxH+X+Y = Perform lossless cropping on the input image prior to"); + System.out.println(" decompressing it. X and Y specify the upper left corner of the cropping"); + System.out.println(" region, and W and H specify the width and height of the cropping region."); + System.out.println(" X and Y must be evenly divible by the MCU block size (8x8 if the input"); + System.out.println(" image was compressed using no subsampling or grayscale, 16x8 if it was"); + System.out.println(" compressed using 4:2:2 subsampling, or 16x16 if it was compressed using"); + System.out.println(" 4:2:0 subsampling.)\n"); + + System.out.println("General Options"); + System.out.println("---------------\n"); + + System.out.println("-display = Display output image (Output filename need not be specified in this"); System.out.println(" case.)\n"); + System.out.println("-fastupsample = Use the fastest chrominance upsampling algorithm available in"); - System.out.println(" the underlying codec\n"); + System.out.println(" the underlying codec.\n"); + System.out.println("-fastdct = Use the fastest DCT/IDCT algorithms available in the underlying"); - System.out.println(" codec\n"); + System.out.println(" codec.\n"); + System.out.println("-accuratedct = Use the most accurate DCT/IDCT algorithms available in the"); - System.out.println(" underlying codec\n"); + System.out.println(" underlying codec.\n"); + System.exit(1); } - private static final String[] sampName = { - "4:4:4", "4:2:2", "4:2:0", "Grayscale", "4:4:0", "4:1:1" - }; public static void main(String[] argv) { - BufferedImage img = null; - byte[] bmpBuf = null; - TJTransform xform = new TJTransform(); - int flags = 0; - try { - sf = TJ.getScalingFactors(); + TJScalingFactor scalingFactor = new TJScalingFactor(1, 1); + int outSubsamp = -1, outQual = -1; + TJTransform xform = new TJTransform(); + boolean display = false; + int flags = 0; + int width, height; + String inFormat = "jpg", outFormat = "jpg"; + BufferedImage img = null; + byte[] imgBuf = null; - if (argv.length < 2) { + if (argv.length < 2) usage(); - } - - TJScalingFactor scaleFactor = new TJScalingFactor(1, 1); - String inFormat = "jpg", outFormat = "jpg"; - int outSubsamp = -1, outQual = 95; - boolean display = false; if (argv[1].substring(0, 2).equalsIgnoreCase("-d")) display = true; + /* Parse arguments. */ for (int i = 2; i < argv.length; i++) { if (argv[i].length() < 2) continue; else if (argv[i].length() > 2 && - argv[i].substring(0, 3).equalsIgnoreCase("-sc")) { + argv[i].substring(0, 3).equalsIgnoreCase("-sc") && + i < argv.length - 1) { int match = 0; - if (i < argv.length - 1) { - String[] scaleArg = argv[++i].split("/"); - if (scaleArg.length == 2) { - TJScalingFactor tempsf = - new TJScalingFactor(Integer.parseInt(scaleArg[0]), - Integer.parseInt(scaleArg[1])); - for (int j = 0; j < sf.length; j++) { - if (tempsf.equals(sf[j])) { - scaleFactor = sf[j]; - match = 1; - break; - } + String[] scaleArg = argv[++i].split("/"); + if (scaleArg.length == 2) { + TJScalingFactor tempsf = + new TJScalingFactor(Integer.parseInt(scaleArg[0]), + Integer.parseInt(scaleArg[1])); + for (int j = 0; j < scalingFactors.length; j++) { + if (tempsf.equals(scalingFactors[j])) { + scalingFactor = scalingFactors[j]; + match = 1; + break; } } } - if (match != 1) usage(); + if (match != 1) + usage(); } else if (argv[i].length() > 2 && - argv[i].substring(0, 3).equalsIgnoreCase("-sa")) { - if (i < argv.length - 1) { - i++; - if (argv[i].substring(0, 1).equalsIgnoreCase("g")) - outSubsamp = TJ.SAMP_GRAY; - else if (argv[i].equals("444")) - outSubsamp = TJ.SAMP_444; - else if (argv[i].equals("422")) - outSubsamp = TJ.SAMP_422; - else if (argv[i].equals("420")) - outSubsamp = TJ.SAMP_420; - else - usage(); - } else + argv[i].substring(0, 3).equalsIgnoreCase("-su") && + i < argv.length - 1) { + i++; + if (argv[i].substring(0, 1).equalsIgnoreCase("g")) + outSubsamp = TJ.SAMP_GRAY; + else if (argv[i].equals("444")) + outSubsamp = TJ.SAMP_444; + else if (argv[i].equals("422")) + outSubsamp = TJ.SAMP_422; + else if (argv[i].equals("420")) + outSubsamp = TJ.SAMP_420; + else usage(); } - else if (argv[i].substring(0, 2).equalsIgnoreCase("-q")) { - if (i < argv.length - 1) { - int qual = Integer.parseInt(argv[++i]); - if (qual >= 1 && qual <= 100) - outQual = qual; - else - usage(); - } else + else if (argv[i].substring(0, 2).equalsIgnoreCase("-q") && + i < argv.length - 1) { + outQual = Integer.parseInt(argv[++i]); + if (outQual < 1 || outQual > 100) usage(); - } - else if (argv[i].substring(0, 2).equalsIgnoreCase("-g")) + } else if (argv[i].substring(0, 2).equalsIgnoreCase("-g")) xform.options |= TJTransform.OPT_GRAY; else if (argv[i].equalsIgnoreCase("-hflip")) xform.op = TJTransform.OP_HFLIP; @@ -190,25 +227,18 @@ public class TJExample implements TJCustomFilter { else if (argv[i].equalsIgnoreCase("-custom")) xform.cf = new TJExample(); else if (argv[i].length() > 2 && - argv[i].substring(0, 2).equalsIgnoreCase("-c")) { - if (i >= argv.length - 1) - usage(); - String[] cropArg = argv[++i].split(","); - if (cropArg.length != 3) + argv[i].substring(0, 2).equalsIgnoreCase("-c") && + i < argv.length - 1) { + String[] cropArg = argv[++i].split("[x\\+]"); + if (cropArg.length != 4) usage(); - String[] dimArg = cropArg[2].split("[xX]"); - if (dimArg.length != 2) + xform.width = Integer.parseInt(cropArg[0]); + xform.height = Integer.parseInt(cropArg[1]); + xform.x = Integer.parseInt(cropArg[2]); + xform.y = Integer.parseInt(cropArg[3]); + if (xform.x < 0 || xform.y < 0 || xform.width < 1 || + xform.height < 1) usage(); - int tempx = Integer.parseInt(cropArg[0]); - int tempy = Integer.parseInt(cropArg[1]); - int tempw = Integer.parseInt(dimArg[0]); - int temph = Integer.parseInt(dimArg[1]); - if (tempx < 0 || tempy < 0 || tempw < 0 || temph < 0) - usage(); - xform.x = tempx; - xform.y = tempy; - xform.width = tempw; - xform.height = temph; xform.options |= TJTransform.OPT_CROP; } else if (argv[i].substring(0, 2).equalsIgnoreCase("-d")) @@ -227,6 +257,8 @@ public class TJExample implements TJCustomFilter { } else usage(); } + + /* Determine input and output image formats based on file extensions. */ String[] inFileTokens = argv[0].split("\\."); if (inFileTokens.length > 1) inFormat = inFileTokens[inFileTokens.length - 1]; @@ -239,61 +271,75 @@ public class TJExample implements TJCustomFilter { outFormat = outFileTokens[outFileTokens.length - 1]; } - File file = new File(argv[0]); - int width, height; - if (inFormat.equalsIgnoreCase("jpg")) { - FileInputStream fis = new FileInputStream(file); - int inputSize = fis.available(); - if (inputSize < 1) { + /* Input image is a JPEG image. Decompress and/or transform it. */ + boolean doTransform = (xform.op != TJTransform.OP_NONE || + xform.options != 0 || xform.cf != null); + + /* Read the JPEG file into memory. */ + File jpegFile = new File(argv[0]); + FileInputStream fis = new FileInputStream(jpegFile); + int jpegSize = fis.available(); + if (jpegSize < 1) { System.out.println("Input file contains no data"); System.exit(1); } - byte[] inputBuf = new byte[inputSize]; - fis.read(inputBuf); + byte[] jpegBuf = new byte[jpegSize]; + fis.read(jpegBuf); fis.close(); TJDecompressor tjd; - if (xform.op != TJTransform.OP_NONE || xform.options != 0 || - xform.cf != null) { - TJTransformer tjt = new TJTransformer(inputBuf); - TJTransform[] t = new TJTransform[1]; - t[0] = xform; - t[0].options |= TJTransform.OPT_TRIM; - TJDecompressor[] tjdx = tjt.transform(t, 0); - tjd = tjdx[0]; + if (doTransform) { + /* Transform it. */ + TJTransformer tjt = new TJTransformer(jpegBuf); + TJTransform[] xforms = new TJTransform[1]; + xforms[0] = xform; + xforms[0].options |= TJTransform.OPT_TRIM; + TJDecompressor[] tjds = tjt.transform(xforms, 0); + tjd = tjds[0]; + tjt.close(); } else - tjd = new TJDecompressor(inputBuf); + tjd = new TJDecompressor(jpegBuf); width = tjd.getWidth(); height = tjd.getHeight(); int inSubsamp = tjd.getSubsamp(); - System.out.println("Source Image: " + width + " x " + height + - " pixels, " + sampName[inSubsamp] + " subsampling"); - if (outSubsamp < 0) - outSubsamp = inSubsamp; + int inColorspace = tjd.getColorspace(); + + System.out.println((doTransform ? "Transformed" : "Input") + + " Image (jpg): " + width + " x " + height + + " pixels, " + subsampName[inSubsamp] + + " subsampling, " + colorspaceName[inColorspace]); - if (outFormat.equalsIgnoreCase("jpg") && - (xform.op != TJTransform.OP_NONE || xform.options != 0) && - scaleFactor.isOne()) { - file = new File(argv[1]); - FileOutputStream fos = new FileOutputStream(file); + if (outFormat.equalsIgnoreCase("jpg") && doTransform && + scalingFactor.isOne() && outSubsamp < 0 && outQual < 0) { + /* Input image has been transformed, and no re-compression options + have been selected. Write the transformed image to disk and + exit. */ + File outFile = new File(argv[1]); + FileOutputStream fos = new FileOutputStream(outFile); fos.write(tjd.getJPEGBuf(), 0, tjd.getJPEGSize()); fos.close(); System.exit(0); } - width = scaleFactor.getScaled(width); - height = scaleFactor.getScaled(height); + /* Scaling and/or a non-JPEG output image format and/or compression + options have been selected, so we need to decompress the + input/transformed image. */ + width = scalingFactor.getScaled(width); + height = scalingFactor.getScaled(height); + if (outSubsamp < 0) + outSubsamp = inSubsamp; if (!outFormat.equalsIgnoreCase("jpg")) img = tjd.decompress(width, height, BufferedImage.TYPE_INT_RGB, flags); else - bmpBuf = tjd.decompress(width, 0, height, TJ.PF_BGRX, flags); + imgBuf = tjd.decompress(width, 0, height, TJ.PF_BGRX, flags); tjd.close(); } else { - img = ImageIO.read(file); + /* Input image is not a JPEG image. Load it into memory. */ + img = ImageIO.read(new File(argv[0])); if (img == null) throw new Exception("Input image type not supported."); width = img.getWidth(); @@ -302,45 +348,51 @@ public class TJExample implements TJCustomFilter { if (img.getType() == BufferedImage.TYPE_BYTE_GRAY) outSubsamp = TJ.SAMP_GRAY; else - outSubsamp = TJ.SAMP_444; + outSubsamp = DEFAULT_SUBSAMP; } + System.out.println("Input Image: " + width + " x " + height + + " pixels"); } System.gc(); if (!display) - System.out.print("Dest. Image (" + outFormat + "): " + width + " x " + - height + " pixels"); + System.out.print("Output Image (" + outFormat + "): " + width + + " x " + height + " pixels"); if (display) { + /* Display the uncompressed image */ ImageIcon icon = new ImageIcon(img); JLabel label = new JLabel(icon, JLabel.CENTER); JOptionPane.showMessageDialog(null, label, "Output Image", JOptionPane.PLAIN_MESSAGE); } else if (outFormat.equalsIgnoreCase("jpg")) { - System.out.println(", " + sampName[outSubsamp] + + /* Output image format is JPEG. Compress the uncompressed image. */ + if (outQual < 0) + outQual = DEFAULT_QUALITY; + System.out.println(", " + subsampName[outSubsamp] + " subsampling, quality = " + outQual); - TJCompressor tjc = new TJCompressor(); - int jpegSize; - byte[] jpegBuf; + TJCompressor tjc = new TJCompressor(); tjc.setSubsamp(outSubsamp); tjc.setJPEGQuality(outQual); if (img != null) tjc.setSourceImage(img, 0, 0, 0, 0); - else { - tjc.setSourceImage(bmpBuf, 0, 0, width, 0, height, TJ.PF_BGRX); - } - jpegBuf = tjc.compress(flags); - jpegSize = tjc.getCompressedSize(); + else + tjc.setSourceImage(imgBuf, 0, 0, width, 0, height, TJ.PF_BGRX); + byte[] jpegBuf = tjc.compress(flags); + int jpegSize = tjc.getCompressedSize(); tjc.close(); - file = new File(argv[1]); - FileOutputStream fos = new FileOutputStream(file); + /* Write the JPEG image to disk. */ + File outFile = new File(argv[1]); + FileOutputStream fos = new FileOutputStream(outFile); fos.write(jpegBuf, 0, jpegSize); fos.close(); } else { + /* Output image format is not JPEG. Save the uncompressed image + directly to disk. */ System.out.print("\n"); - file = new File(argv[1]); - ImageIO.write(img, outFormat, file); + File outFile = new File(argv[1]); + ImageIO.write(img, outFormat, outFile); } } catch(Exception e) { @@ -349,14 +401,5 @@ public class TJExample implements TJCustomFilter { } } - public void customFilter(ShortBuffer coeffBuffer, Rectangle bufferRegion, - Rectangle planeRegion, int componentIndex, - int transformIndex, TJTransform transform) - throws TJException { - for (int i = 0; i < bufferRegion.width * bufferRegion.height; i++) { - coeffBuffer.put(i, (short)(-coeffBuffer.get(i))); - } - } - - static TJScalingFactor[] sf = null; + private static final TJScalingFactor[] scalingFactors = TJ.getScalingFactors(); }; diff --git a/libjpeg.txt b/libjpeg.txt index 13094d4..e5a01a5 100644 --- a/libjpeg.txt +++ b/libjpeg.txt @@ -11,7 +11,7 @@ For conditions of distribution and use, see the accompanying README.ijg file. This file describes how to use the IJG JPEG library within an application program. Read it if you want to write a program that uses the library. -The file example.c provides heavily commented skeleton code for calling the +The file example.txt provides heavily commented skeleton code for calling the JPEG library. Also see jpeglib.h (the include file to be used by application programs) for full details about data structures and function parameter lists. The library source code, of course, is the ultimate reference. @@ -402,7 +402,7 @@ this variable as the loop counter, so that the loop test looks like "while (cinfo.next_scanline < cinfo.image_height)". Code for this step depends heavily on the way that you store the source data. -example.c shows the following code for the case of a full-size 2-D source +example.txt shows the following code for the case of a full-size 2-D source array containing 3-byte RGB pixels: JSAMPROW row_pointer[1]; /* pointer to a single row */ @@ -1437,7 +1437,7 @@ When the default error handler is used, any error detected inside the JPEG routines will cause a message to be printed on stderr, followed by exit(). You can supply your own error handling routines to override this behavior and to control the treatment of nonfatal warnings and trace/debug messages. -The file example.c illustrates the most common case, which is to have the +The file example.txt illustrates the most common case, which is to have the application regain control after an error rather than exiting. The JPEG library never writes any message directly; it always goes through @@ -1454,7 +1454,7 @@ You may, if you wish, simply replace the entire JPEG error handling module only replacing some of the routines depending on the behavior you need. This is accomplished by calling jpeg_std_error() as usual, but then overriding some of the method pointers in the jpeg_error_mgr struct, as illustrated by -example.c. +example.txt. All of the error handling routines will receive a pointer to the JPEG object (a j_common_ptr which points to either a jpeg_compress_struct or a @@ -1465,7 +1465,7 @@ additional data which is not known to the JPEG library or the standard error handler. The most convenient way to do this is to embed either the JPEG object or the jpeg_error_mgr struct in a larger structure that contains additional fields; then casting the passed pointer provides access to the -additional fields. Again, see example.c for one way to do it. (Beginning +additional fields. Again, see example.txt for one way to do it. (Beginning with IJG version 6b, there is also a void pointer "client_data" in each JPEG object, which the application can also use to find related data. The library does not touch client_data at all.) diff --git a/release/installer.nsi.in b/release/installer.nsi.in index b413838..ec03f5e 100755 --- a/release/installer.nsi.in +++ b/release/installer.nsi.in @@ -82,11 +82,13 @@ Section "@CMAKE_PROJECT_NAME@ SDK for @INST_PLATFORM@ (required)" File "@CMAKE_CURRENT_SOURCE_DIR@\README.ijg" File "@CMAKE_CURRENT_SOURCE_DIR@\README.md" File "@CMAKE_CURRENT_SOURCE_DIR@\LICENSE.md" - File "@CMAKE_CURRENT_SOURCE_DIR@\example.c" + File "@CMAKE_CURRENT_SOURCE_DIR@\example.txt" File "@CMAKE_CURRENT_SOURCE_DIR@\libjpeg.txt" File "@CMAKE_CURRENT_SOURCE_DIR@\structure.txt" File "@CMAKE_CURRENT_SOURCE_DIR@\usage.txt" File "@CMAKE_CURRENT_SOURCE_DIR@\wizard.txt" + File "@CMAKE_CURRENT_SOURCE_DIR@\tjexample.c" + File "@CMAKE_CURRENT_SOURCE_DIR@\java\TJExample.java" WriteRegStr HKLM "SOFTWARE\@INST_REG_NAME@ @VERSION@" "Install_Dir" "$INSTDIR" @@ -144,11 +146,13 @@ Section "Uninstall" Delete $INSTDIR\doc\README.ijg Delete $INSTDIR\doc\README.md Delete $INSTDIR\doc\LICENSE.md - Delete $INSTDIR\doc\example.c + Delete $INSTDIR\doc\example.txt Delete $INSTDIR\doc\libjpeg.txt Delete $INSTDIR\doc\structure.txt Delete $INSTDIR\doc\usage.txt Delete $INSTDIR\doc\wizard.txt + Delete $INSTDIR\doc\tjexample.c + Delete $INSTDIR\doc\TJExample.java RMDir "$INSTDIR\include" RMDir "$INSTDIR\lib" diff --git a/tjexample.c b/tjexample.c new file mode 100644 index 0000000..2a13f53 --- /dev/null +++ b/tjexample.c @@ -0,0 +1,394 @@ +/* + * Copyright (C)2011-2012, 2014-2015, 2017 D. R. Commander. + * All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * - Neither the name of the libjpeg-turbo Project nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS", + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * This program demonstrates how to compress, decompress, and transform JPEG + * images using the TurboJPEG C API + */ + +#include +#include +#include +#include +#include + + +#ifdef _WIN32 +#define strcasecmp stricmp +#define strncasecmp strnicmp +#endif + +#define _throw(action, message) { \ + printf("ERROR in line %d while %s:\n%s\n", __LINE__, action, message); \ + retval = -1; goto bailout; \ +} + +#define _throwtj(action) _throw(action, tjGetErrorStr2(tjInstance)) + +#define _throwunix(action) _throw(action, strerror(errno)) + +#define DEFAULT_SUBSAMP TJSAMP_444 +#define DEFAULT_QUALITY 95 + + +const char *subsampName[TJ_NUMSAMP] = { + "4:4:4", "4:2:2", "4:2:0", "Grayscale", "4:4:0", "4:1:1" +}; + +const char *colorspaceName[TJ_NUMCS] = { + "RGB", "YCbCr", "GRAY", "CMYK", "YCCK" +}; + +tjscalingfactor *scalingFactors = NULL; +int numScalingFactors = 0; + + +/* DCT filter example. This produces a negative of the image. */ + +int customFilter(short *coeffs, tjregion arrayRegion, tjregion planeRegion, + int componentIndex, int transformIndex, + tjtransform *transform) +{ + int i; + + for(i = 0; i < arrayRegion.w * arrayRegion.h; i++) + coeffs[i] = -coeffs[i]; + + return 0; +} + + +void usage(char *programName) +{ + int i; + + printf("\nUSAGE: %s [options]\n\n", + programName); + + printf("Input and output images can be in Windows BMP or PBMPLUS (PPM/PGM) format. If\n"); + printf("either filename ends in a .jpg extension, then the TurboJPEG API will be used\n"); + printf("to compress or decompress the image.\n\n"); + + printf("Compression Options (used if the output image is a JPEG image)\n"); + printf("--------------------------------------------------------------\n\n"); + + printf("-subsamp <444|422|420|gray> = Apply this level of chrominance subsampling when\n"); + printf(" compressing the output image. The default is to use the same level of\n"); + printf(" subsampling as in the input image, if the input image is also a JPEG\n"); + printf(" image, or to use grayscale if the input image is a grayscale non-JPEG\n"); + printf(" image, or to use %s subsampling otherwise.\n\n", + subsampName[DEFAULT_SUBSAMP]); + + printf("-q <1-100> = Compress the output image with this JPEG quality level\n"); + printf(" (default = %d).\n\n", DEFAULT_QUALITY); + + printf("Decompression Options (used if the input image is a JPEG image)\n"); + printf("---------------------------------------------------------------\n\n"); + + printf("-scale M/N = Scale the input image by a factor of M/N when decompressing it.\n"); + printf("(M/N = "); + for (i = 0; i < numScalingFactors; i++) { + printf("%d/%d", scalingFactors[i].num, scalingFactors[i].denom); + if (numScalingFactors == 2 && i != numScalingFactors - 1) + printf(" or "); + else if (numScalingFactors > 2) { + if (i != numScalingFactors - 1) + printf(", "); + if (i == numScalingFactors - 2) + printf("or "); + } + } + printf(")\n\n"); + + printf("-hflip, -vflip, -transpose, -transverse, -rot90, -rot180, -rot270 =\n"); + printf(" Perform one of these lossless transform operations on the input image\n"); + printf(" prior to decompressing it (these options are mutually exclusive.)\n\n"); + + printf("-grayscale = Perform lossless grayscale conversion on the input image prior\n"); + printf(" to decompressing it (can be combined with the other transform operations\n"); + printf(" above.)\n\n"); + + printf("-crop WxH+X+Y = Perform lossless cropping on the input image prior to\n"); + printf(" decompressing it. X and Y specify the upper left corner of the cropping\n"); + printf(" region, and W and H specify the width and height of the cropping region.\n"); + printf(" X and Y must be evenly divible by the MCU block size (8x8 if the input\n"); + printf(" image was compressed using no subsampling or grayscale, 16x8 if it was\n"); + printf(" compressed using 4:2:2 subsampling, or 16x16 if it was compressed using\n"); + printf(" 4:2:0 subsampling.)\n\n"); + + printf("General Options\n"); + printf("---------------\n\n"); + + printf("-fastupsample = Use the fastest chrominance upsampling algorithm available in\n"); + printf(" the underlying codec.\n\n"); + + printf("-fastdct = Use the fastest DCT/IDCT algorithms available in the underlying\n"); + printf(" codec.\n\n"); + + printf("-accuratedct = Use the most accurate DCT/IDCT algorithms available in the\n"); + printf(" underlying codec.\n\n"); + + exit(1); +} + + +int main(int argc, char **argv) +{ + tjscalingfactor scalingFactor = { 1, 1 }; + int outSubsamp = -1, outQual = -1; + tjtransform xform; + int flags = 0; + int width, height; + char *inFormat, *outFormat; + FILE *jpegFile = NULL; + unsigned char *imgBuf = NULL, *jpegBuf = NULL; + int retval = 0, i, pixelFormat = TJPF_UNKNOWN; + tjhandle tjInstance = NULL; + + if ((scalingFactors = tjGetScalingFactors(&numScalingFactors)) == NULL) + _throwtj("getting scaling factors"); + memset(&xform, 0, sizeof(tjtransform)); + + if (argc < 3) + usage(argv[0]); + + /* Parse arguments. */ + for (i = 3; i < argc; i++) { + if (!strncasecmp(argv[i], "-sc", 3) && i < argc - 1) { + int match = 0, temp1 = 0, temp2 = 0, j; + if (sscanf(argv[++i], "%d/%d", &temp1, &temp2) < 2) + usage(argv[0]); + for (j = 0; j < numScalingFactors; j++) { + if ((double)temp1 / (double)temp2 == (double)scalingFactors[j].num / + (double)scalingFactors[j].denom) { + scalingFactor = scalingFactors[j]; + match = 1; + break; + } + } + if (match != 1) + usage(argv[0]); + } else if (!strncasecmp(argv[i], "-su", 3) && i < argc - 1) { + i++; + if (!strncasecmp(argv[i], "g", 1)) + outSubsamp = TJSAMP_GRAY; + else if (!strcasecmp(argv[i], "444")) + outSubsamp = TJSAMP_444; + else if (!strcasecmp(argv[i], "422")) + outSubsamp = TJSAMP_422; + else if (!strcasecmp(argv[i], "420")) + outSubsamp = TJSAMP_420; + else + usage(argv[0]); + } else if (!strncasecmp(argv[i], "-q", 2) && i < argc - 1) { + outQual = atoi(argv[++i]); + if (outQual < 1 || outQual > 100) + usage(argv[0]); + } else if (!strncasecmp(argv[i], "-g", 2)) + xform.options |= TJXOPT_GRAY; + else if (!strcasecmp(argv[i], "-hflip")) + xform.op = TJXOP_HFLIP; + else if (!strcasecmp(argv[i], "-vflip")) + xform.op = TJXOP_VFLIP; + else if (!strcasecmp(argv[i], "-transpose")) + xform.op = TJXOP_TRANSPOSE; + else if (!strcasecmp(argv[i], "-transverse")) + xform.op = TJXOP_TRANSVERSE; + else if (!strcasecmp(argv[i], "-rot90")) + xform.op = TJXOP_ROT90; + else if (!strcasecmp(argv[i], "-rot180")) + xform.op = TJXOP_ROT180; + else if (!strcasecmp(argv[i], "-rot270")) + xform.op = TJXOP_ROT270; + else if (!strcasecmp(argv[i], "-custom")) + xform.customFilter = customFilter; + else if (!strncasecmp(argv[i], "-c", 2) && i < argc - 1) { + if (sscanf(argv[++i], "%dx%d+%d+%d", &xform.r.w, &xform.r.h, &xform.r.x, + &xform.r.y) < 4 || + xform.r.x < 0 || xform.r.y < 0 || xform.r.w < 1 || xform.r.h < 1) + usage(argv[0]); + xform.options |= TJXOPT_CROP; + } else if (!strcasecmp(argv[i], "-fastupsample")) { + printf("Using fast upsampling code\n"); + flags |= TJFLAG_FASTUPSAMPLE; + } else if (!strcasecmp(argv[i], "-fastdct")) { + printf("Using fastest DCT/IDCT algorithm\n"); + flags |= TJFLAG_FASTDCT; + } else if (!strcasecmp(argv[i], "-accuratedct")) { + printf("Using most accurate DCT/IDCT algorithm\n"); + flags |= TJFLAG_ACCURATEDCT; + } else usage(argv[0]); + } + + /* Determine input and output image formats based on file extensions. */ + inFormat = strrchr(argv[1], '.'); + outFormat = strrchr(argv[2], '.'); + if (inFormat == NULL || outFormat == NULL || strlen(inFormat) < 2 || + strlen(outFormat) < 2) + usage(argv[0]); + inFormat = &inFormat[1]; + outFormat = &outFormat[1]; + + if (!strcasecmp(inFormat, "jpg")) { + /* Input image is a JPEG image. Decompress and/or transform it. */ + long size; + int inSubsamp, inColorspace; + int doTransform = (xform.op != TJXOP_NONE || xform.options != 0 || + xform.customFilter != NULL); + unsigned long jpegSize; + + /* Read the JPEG file into memory. */ + if ((jpegFile = fopen(argv[1], "rb")) == NULL) + _throwunix("opening input file"); + if (fseek(jpegFile, 0, SEEK_END) < 0 || ((size = ftell(jpegFile)) < 0) || + fseek(jpegFile, 0, SEEK_SET) < 0) + _throwunix("determining input file size"); + if (size == 0) + _throw("determining input file size", "Input file contains no data"); + jpegSize = (unsigned long)size; + if ((jpegBuf = (unsigned char *)tjAlloc(jpegSize)) == NULL) + _throwunix("allocating JPEG buffer"); + if (fread(jpegBuf, jpegSize, 1, jpegFile) < 1) + _throwunix("reading input file"); + fclose(jpegFile); jpegFile = NULL; + + if (doTransform) { + /* Transform it. */ + unsigned char *dstBuf = NULL; /* Dynamically allocate the JPEG buffer */ + unsigned long dstSize = 0; + + if ((tjInstance = tjInitTransform()) == NULL) + _throwtj("initializing transformer"); + xform.options |= TJXOPT_TRIM; + if (tjTransform(tjInstance, jpegBuf, jpegSize, 1, &dstBuf, &dstSize, + &xform, flags) < 0) + _throwtj("transforming input image"); + tjFree(jpegBuf); + jpegBuf = dstBuf; + jpegSize = dstSize; + } else { + if ((tjInstance = tjInitDecompress()) == NULL) + _throwtj("initializing decompressor"); + } + + if (tjDecompressHeader3(tjInstance, jpegBuf, jpegSize, &width, &height, + &inSubsamp, &inColorspace) < 0) + _throwtj("reading JPEG header"); + + printf("%s Image: %d x %d pixels, %s subsampling, %s colorspace\n", + (doTransform ? "Transformed" : "Input"), width, height, + subsampName[inSubsamp], colorspaceName[inColorspace]); + + if (!strcasecmp(outFormat, "jpg") && doTransform && + scalingFactor.num == 1 && scalingFactor.denom == 1 && outSubsamp < 0 && + outQual < 0) { + /* Input image has been transformed, and no re-compression options + have been selected. Write the transformed image to disk and exit. */ + if ((jpegFile = fopen(argv[2], "wb")) == NULL) + _throwunix("opening output file"); + if (fwrite(jpegBuf, jpegSize, 1, jpegFile) < 1) + _throwunix("writing output file"); + fclose(jpegFile); jpegFile = NULL; + goto bailout; + } + + /* Scaling and/or a non-JPEG output image format and/or compression options + have been selected, so we need to decompress the input/transformed + image. */ + width = TJSCALED(width, scalingFactor); + height = TJSCALED(height, scalingFactor); + if (outSubsamp < 0) + outSubsamp = inSubsamp; + + pixelFormat = TJPF_BGRX; + if ((imgBuf = (unsigned char *)tjAlloc(width * height * + tjPixelSize[pixelFormat])) == NULL) + _throwunix("allocating uncompressed image buffer"); + + if (tjDecompress2(tjInstance, jpegBuf, jpegSize, imgBuf, width, 0, height, + pixelFormat, flags) < 0) + _throwtj("decompressing JPEG image"); + tjFree(jpegBuf); jpegBuf = NULL; + tjDestroy(tjInstance); tjInstance = NULL; + } else { + /* Input image is not a JPEG image. Load it into memory. */ + if ((imgBuf = tjLoadImage(argv[1], &width, 1, &height, &pixelFormat, + 0)) == NULL) + _throwtj("loading input image"); + if (outSubsamp < 0) { + if (pixelFormat == TJPF_GRAY) + outSubsamp = TJSAMP_GRAY; + else + outSubsamp = TJSAMP_444; + } + printf("Input Image: %d x %d pixels\n", width, height); + } + + printf("Output Image (%s): %d x %d pixels", outFormat, width, height); + + if (!strcasecmp(outFormat, "jpg")) { + /* Output image format is JPEG. Compress the uncompressed image. */ + unsigned char *jpegBuf = NULL; /* Dynamically allocate the JPEG buffer */ + unsigned long jpegSize = 0; + + if (outQual < 0) + outQual = DEFAULT_QUALITY; + printf(", %s subsampling, quality = %d\n", subsampName[outSubsamp], + outQual); + + if ((tjInstance = tjInitCompress()) == NULL) + _throwtj("initializing compressor"); + if (tjCompress2(tjInstance, imgBuf, width, 0, height, pixelFormat, + &jpegBuf, &jpegSize, outSubsamp, outQual, flags) < 0) + _throwtj("compressing image"); + tjDestroy(tjInstance); tjInstance = NULL; + + /* Write the JPEG image to disk. */ + if ((jpegFile = fopen(argv[2], "wb")) == NULL) + _throwunix("opening output file"); + if (fwrite(jpegBuf, jpegSize, 1, jpegFile) < 1) + _throwunix("writing output file"); + tjDestroy(tjInstance); tjInstance = NULL; + fclose(jpegFile); jpegFile = NULL; + tjFree(jpegBuf); jpegBuf = NULL; + } else { + /* Output image format is not JPEG. Save the uncompressed image + directly to disk. */ + printf("\n"); + if (tjSaveImage(argv[2], imgBuf, width, 0, height, pixelFormat, 0) < 0) + _throwtj("saving output image"); + } + + bailout: + if (imgBuf) tjFree(imgBuf); + if (tjInstance) tjDestroy(tjInstance); + if (jpegBuf) tjFree(jpegBuf); + if (jpegFile) fclose(jpegFile); + return retval; +}; diff --git a/tjexampletest.in b/tjexampletest.in index 1693342..0d3047e 100755 --- a/tjexampletest.in +++ b/tjexampletest.in @@ -16,15 +16,13 @@ onexit() runme() { echo \*\*\* $* - "$@" + $* } IMAGES="vgl_5674_0098.bmp vgl_6434_0018a.bmp vgl_6548_0026a.bmp nightshot_iso_100.bmp" IMGDIR=@CMAKE_CURRENT_SOURCE_DIR@/testimages OUTDIR=`mktemp -d /tmp/__tjexampletest_output.XXXXXX` EXEDIR=@CMAKE_CURRENT_BINARY_DIR@ -JAVA="@Java_JAVA_EXECUTABLE@" -JAVAARGS="-cp $EXEDIR/java/turbojpeg.jar -Djava.library.path=$EXEDIR" if [ -d $OUTDIR ]; then rm -rf $OUTDIR @@ -59,7 +57,7 @@ for image in $IMAGES; do # Compression for dct in fast accurate; do for samp in GRAY 420 422 444; do - runme "$JAVA" $JAVAARGS TJExample $OUTDIR/$image $OUTDIR/${basename}_${samp}_${dct}.jpg -q 95 -samp ${samp} -${dct}dct + runme $EXEDIR/tjexample $OUTDIR/$image $OUTDIR/${basename}_${samp}_${dct}.jpg -q 95 -subsamp ${samp} -${dct}dct runme cmp $OUTDIR/${basename}_${samp}_${dct}.jpg $OUTDIR/${basename}_${samp}_${dct}_cjpeg.jpg done done @@ -73,12 +71,12 @@ for image in $IMAGES; do dctarg= fi for samp in GRAY 420 422 444; do - runme "$JAVA" $JAVAARGS TJExample $OUTDIR/${basename}_${samp}_${srcdct}.jpg $OUTDIR/${basename}_${samp}_${dct}.bmp ${dctarg} + runme $EXEDIR/tjexample $OUTDIR/${basename}_${samp}_${srcdct}.jpg $OUTDIR/${basename}_${samp}_${dct}.bmp ${dctarg} runme cmp -i 54:54 $OUTDIR/${basename}_${samp}_${dct}.bmp $OUTDIR/${basename}_${samp}_${dct}_djpeg.bmp rm $OUTDIR/${basename}_${samp}_${dct}.bmp done for samp in 420 422; do - runme "$JAVA" $JAVAARGS TJExample $OUTDIR/${basename}_${samp}_${srcdct}.jpg $OUTDIR/${basename}_${samp}_${dct}_nosmooth.bmp -fastupsample ${dctarg} + runme $EXEDIR/tjexample $OUTDIR/${basename}_${samp}_${srcdct}.jpg $OUTDIR/${basename}_${samp}_${dct}_nosmooth.bmp -fastupsample ${dctarg} runme cmp -i 54:54 $OUTDIR/${basename}_${samp}_${dct}_nosmooth.bmp $OUTDIR/${basename}_${samp}_${dct}_nosmooth_djpeg.bmp rm $OUTDIR/${basename}_${samp}_${dct}_nosmooth.bmp done @@ -89,7 +87,7 @@ for image in $IMAGES; do scalearg=`echo $scale | sed 's/\_/\//g'` for samp in GRAY 420 422 444; do runme $EXEDIR/djpeg -rgb -bmp -scale ${scalearg} -outfile $OUTDIR/${basename}_${samp}_${scale}_djpeg.bmp $OUTDIR/${basename}_${samp}_fast_cjpeg.jpg - runme "$JAVA" $JAVAARGS TJExample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${scale}.bmp -scale ${scalearg} + runme $EXEDIR/tjexample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${scale}.bmp -scale ${scalearg} runme cmp -i 54:54 $OUTDIR/${basename}_${samp}_${scale}.bmp $OUTDIR/${basename}_${samp}_${scale}_djpeg.bmp rm $OUTDIR/${basename}_${samp}_${scale}.bmp done @@ -107,16 +105,16 @@ for image in $IMAGES; do done for xform in hflip vflip transpose transverse rot90 rot180 rot270; do for samp in GRAY 420 422 444; do - runme "$JAVA" $JAVAARGS TJExample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${xform}.jpg -$xform -crop 16,16,70x60 + runme $EXEDIR/tjexample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${xform}.jpg -$xform -crop 70x60+16+16 runme cmp $OUTDIR/${basename}_${samp}_${xform}.jpg $OUTDIR/${basename}_${samp}_${xform}_jpegtran.jpg runme $EXEDIR/djpeg -rgb -bmp -outfile $OUTDIR/${basename}_${samp}_${xform}_jpegtran.bmp $OUTDIR/${basename}_${samp}_${xform}_jpegtran.jpg - runme "$JAVA" $JAVAARGS TJExample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${xform}.bmp -$xform -crop 16,16,70x60 + runme $EXEDIR/tjexample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${xform}.bmp -$xform -crop 70x60+16+16 runme cmp -i 54:54 $OUTDIR/${basename}_${samp}_${xform}.bmp $OUTDIR/${basename}_${samp}_${xform}_jpegtran.bmp rm $OUTDIR/${basename}_${samp}_${xform}.bmp done for samp in 420 422; do runme $EXEDIR/djpeg -nosmooth -rgb -bmp -outfile $OUTDIR/${basename}_${samp}_${xform}_jpegtran.bmp $OUTDIR/${basename}_${samp}_${xform}_jpegtran.jpg - runme "$JAVA" $JAVAARGS TJExample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${xform}.bmp -$xform -crop 16,16,70x60 -fastupsample + runme $EXEDIR/tjexample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${xform}.bmp -$xform -crop 70x60+16+16 -fastupsample runme cmp -i 54:54 $OUTDIR/${basename}_${samp}_${xform}.bmp $OUTDIR/${basename}_${samp}_${xform}_jpegtran.bmp rm $OUTDIR/${basename}_${samp}_${xform}.bmp done @@ -125,9 +123,9 @@ for image in $IMAGES; do # Grayscale transform for xform in hflip vflip transpose transverse rot90 rot180 rot270; do for samp in GRAY 444 422 420; do - runme "$JAVA" $JAVAARGS TJExample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${xform}.jpg -$xform -grayscale -crop 16,16,70x60 + runme $EXEDIR/tjexample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${xform}.jpg -$xform -grayscale -crop 70x60+16+16 runme cmp $OUTDIR/${basename}_${samp}_${xform}.jpg $OUTDIR/${basename}_GRAY_${xform}_jpegtran.jpg - runme "$JAVA" $JAVAARGS TJExample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${xform}.bmp -$xform -grayscale -crop 16,16,70x60 + runme $EXEDIR/tjexample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${xform}.bmp -$xform -grayscale -crop 70x60+16+16 runme cmp -i 54:54 $OUTDIR/${basename}_${samp}_${xform}.bmp $OUTDIR/${basename}_GRAY_${xform}_jpegtran.bmp rm $OUTDIR/${basename}_${samp}_${xform}.bmp done @@ -139,7 +137,7 @@ for image in $IMAGES; do for scale in 2_1 15_8 7_4 13_8 3_2 11_8 5_4 9_8 7_8 3_4 5_8 1_2 3_8 1_4 1_8; do scalearg=`echo $scale | sed 's/\_/\//g'` runme $EXEDIR/djpeg -rgb -bmp -scale ${scalearg} -outfile $OUTDIR/${basename}_${samp}_${xform}_${scale}_jpegtran.bmp $OUTDIR/${basename}_${samp}_${xform}_jpegtran.jpg - runme "$JAVA" $JAVAARGS TJExample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${xform}_${scale}.bmp -$xform -scale ${scalearg} -crop 16,16,70x60 + runme $EXEDIR/tjexample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${xform}_${scale}.bmp -$xform -scale ${scalearg} -crop 70x60+16+16 runme cmp -i 54:54 $OUTDIR/${basename}_${samp}_${xform}_${scale}.bmp $OUTDIR/${basename}_${samp}_${xform}_${scale}_jpegtran.bmp rm $OUTDIR/${basename}_${samp}_${xform}_${scale}.bmp done diff --git a/tjexampletest.java.in b/tjexampletest.java.in new file mode 100755 index 0000000..d4b63bc --- /dev/null +++ b/tjexampletest.java.in @@ -0,0 +1,151 @@ +#!/bin/bash + +set -u +set -e +trap onexit INT +trap onexit TERM +trap onexit EXIT + +onexit() +{ + if [ -d $OUTDIR ]; then + rm -rf $OUTDIR + fi +} + +runme() +{ + echo \*\*\* $* + "$@" +} + +IMAGES="vgl_5674_0098.bmp vgl_6434_0018a.bmp vgl_6548_0026a.bmp nightshot_iso_100.bmp" +IMGDIR=@CMAKE_CURRENT_SOURCE_DIR@/testimages +OUTDIR=`mktemp -d /tmp/__tjexampletest_java_output.XXXXXX` +EXEDIR=@CMAKE_CURRENT_BINARY_DIR@ +JAVA="@Java_JAVA_EXECUTABLE@" +JAVAARGS="-cp $EXEDIR/java/turbojpeg.jar -Djava.library.path=$EXEDIR" + +if [ -d $OUTDIR ]; then + rm -rf $OUTDIR +fi +mkdir -p $OUTDIR + +exec >$EXEDIR/tjexampletest-java.log + +for image in $IMAGES; do + + cp $IMGDIR/$image $OUTDIR + basename=`basename $image .bmp` + runme $EXEDIR/cjpeg -quality 95 -dct fast -grayscale -outfile $OUTDIR/${basename}_GRAY_fast_cjpeg.jpg $IMGDIR/${basename}.bmp + runme $EXEDIR/cjpeg -quality 95 -dct fast -sample 2x2 -outfile $OUTDIR/${basename}_420_fast_cjpeg.jpg $IMGDIR/${basename}.bmp + runme $EXEDIR/cjpeg -quality 95 -dct fast -sample 2x1 -outfile $OUTDIR/${basename}_422_fast_cjpeg.jpg $IMGDIR/${basename}.bmp + runme $EXEDIR/cjpeg -quality 95 -dct fast -sample 1x1 -outfile $OUTDIR/${basename}_444_fast_cjpeg.jpg $IMGDIR/${basename}.bmp + runme $EXEDIR/cjpeg -quality 95 -dct int -grayscale -outfile $OUTDIR/${basename}_GRAY_accurate_cjpeg.jpg $IMGDIR/${basename}.bmp + runme $EXEDIR/cjpeg -quality 95 -dct int -sample 2x2 -outfile $OUTDIR/${basename}_420_accurate_cjpeg.jpg $IMGDIR/${basename}.bmp + runme $EXEDIR/cjpeg -quality 95 -dct int -sample 2x1 -outfile $OUTDIR/${basename}_422_accurate_cjpeg.jpg $IMGDIR/${basename}.bmp + runme $EXEDIR/cjpeg -quality 95 -dct int -sample 1x1 -outfile $OUTDIR/${basename}_444_accurate_cjpeg.jpg $IMGDIR/${basename}.bmp + for samp in GRAY 420 422 444; do + runme $EXEDIR/djpeg -rgb -bmp -outfile $OUTDIR/${basename}_${samp}_default_djpeg.bmp $OUTDIR/${basename}_${samp}_fast_cjpeg.jpg + runme $EXEDIR/djpeg -dct fast -rgb -bmp -outfile $OUTDIR/${basename}_${samp}_fast_djpeg.bmp $OUTDIR/${basename}_${samp}_fast_cjpeg.jpg + runme $EXEDIR/djpeg -dct int -rgb -bmp -outfile $OUTDIR/${basename}_${samp}_accurate_djpeg.bmp $OUTDIR/${basename}_${samp}_accurate_cjpeg.jpg + done + for samp in 420 422; do + runme $EXEDIR/djpeg -nosmooth -bmp -outfile $OUTDIR/${basename}_${samp}_default_nosmooth_djpeg.bmp $OUTDIR/${basename}_${samp}_fast_cjpeg.jpg + runme $EXEDIR/djpeg -dct fast -nosmooth -bmp -outfile $OUTDIR/${basename}_${samp}_fast_nosmooth_djpeg.bmp $OUTDIR/${basename}_${samp}_fast_cjpeg.jpg + runme $EXEDIR/djpeg -dct int -nosmooth -bmp -outfile $OUTDIR/${basename}_${samp}_accurate_nosmooth_djpeg.bmp $OUTDIR/${basename}_${samp}_accurate_cjpeg.jpg + done + + # Compression + for dct in fast accurate; do + for samp in GRAY 420 422 444; do + runme "$JAVA" $JAVAARGS TJExample $OUTDIR/$image $OUTDIR/${basename}_${samp}_${dct}.jpg -q 95 -subsamp ${samp} -${dct}dct + runme cmp $OUTDIR/${basename}_${samp}_${dct}.jpg $OUTDIR/${basename}_${samp}_${dct}_cjpeg.jpg + done + done + + # Decompression + for dct in fast accurate default; do + srcdct=${dct} + dctarg=-${dct}dct + if [ "${dct}" = "default" ]; then + srcdct=fast + dctarg= + fi + for samp in GRAY 420 422 444; do + runme "$JAVA" $JAVAARGS TJExample $OUTDIR/${basename}_${samp}_${srcdct}.jpg $OUTDIR/${basename}_${samp}_${dct}.bmp ${dctarg} + runme cmp -i 54:54 $OUTDIR/${basename}_${samp}_${dct}.bmp $OUTDIR/${basename}_${samp}_${dct}_djpeg.bmp + rm $OUTDIR/${basename}_${samp}_${dct}.bmp + done + for samp in 420 422; do + runme "$JAVA" $JAVAARGS TJExample $OUTDIR/${basename}_${samp}_${srcdct}.jpg $OUTDIR/${basename}_${samp}_${dct}_nosmooth.bmp -fastupsample ${dctarg} + runme cmp -i 54:54 $OUTDIR/${basename}_${samp}_${dct}_nosmooth.bmp $OUTDIR/${basename}_${samp}_${dct}_nosmooth_djpeg.bmp + rm $OUTDIR/${basename}_${samp}_${dct}_nosmooth.bmp + done + done + + # Scaled decompression + for scale in 2_1 15_8 7_4 13_8 3_2 11_8 5_4 9_8 7_8 3_4 5_8 1_2 3_8 1_4 1_8; do + scalearg=`echo $scale | sed 's/\_/\//g'` + for samp in GRAY 420 422 444; do + runme $EXEDIR/djpeg -rgb -bmp -scale ${scalearg} -outfile $OUTDIR/${basename}_${samp}_${scale}_djpeg.bmp $OUTDIR/${basename}_${samp}_fast_cjpeg.jpg + runme "$JAVA" $JAVAARGS TJExample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${scale}.bmp -scale ${scalearg} + runme cmp -i 54:54 $OUTDIR/${basename}_${samp}_${scale}.bmp $OUTDIR/${basename}_${samp}_${scale}_djpeg.bmp + rm $OUTDIR/${basename}_${samp}_${scale}.bmp + done + done + + # Transforms + for samp in GRAY 420 422 444; do + runme $EXEDIR/jpegtran -crop 70x60+16+16 -flip horizontal -trim -outfile $OUTDIR/${basename}_${samp}_hflip_jpegtran.jpg $OUTDIR/${basename}_${samp}_fast.jpg + runme $EXEDIR/jpegtran -crop 70x60+16+16 -flip vertical -trim -outfile $OUTDIR/${basename}_${samp}_vflip_jpegtran.jpg $OUTDIR/${basename}_${samp}_fast.jpg + runme $EXEDIR/jpegtran -crop 70x60+16+16 -transpose -trim -outfile $OUTDIR/${basename}_${samp}_transpose_jpegtran.jpg $OUTDIR/${basename}_${samp}_fast.jpg + runme $EXEDIR/jpegtran -crop 70x60+16+16 -transverse -trim -outfile $OUTDIR/${basename}_${samp}_transverse_jpegtran.jpg $OUTDIR/${basename}_${samp}_fast.jpg + runme $EXEDIR/jpegtran -crop 70x60+16+16 -rotate 90 -trim -outfile $OUTDIR/${basename}_${samp}_rot90_jpegtran.jpg $OUTDIR/${basename}_${samp}_fast.jpg + runme $EXEDIR/jpegtran -crop 70x60+16+16 -rotate 180 -trim -outfile $OUTDIR/${basename}_${samp}_rot180_jpegtran.jpg $OUTDIR/${basename}_${samp}_fast.jpg + runme $EXEDIR/jpegtran -crop 70x60+16+16 -rotate 270 -trim -outfile $OUTDIR/${basename}_${samp}_rot270_jpegtran.jpg $OUTDIR/${basename}_${samp}_fast.jpg + done + for xform in hflip vflip transpose transverse rot90 rot180 rot270; do + for samp in GRAY 420 422 444; do + runme "$JAVA" $JAVAARGS TJExample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${xform}.jpg -$xform -crop 70x60+16+16 + runme cmp $OUTDIR/${basename}_${samp}_${xform}.jpg $OUTDIR/${basename}_${samp}_${xform}_jpegtran.jpg + runme $EXEDIR/djpeg -rgb -bmp -outfile $OUTDIR/${basename}_${samp}_${xform}_jpegtran.bmp $OUTDIR/${basename}_${samp}_${xform}_jpegtran.jpg + runme "$JAVA" $JAVAARGS TJExample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${xform}.bmp -$xform -crop 70x60+16+16 + runme cmp -i 54:54 $OUTDIR/${basename}_${samp}_${xform}.bmp $OUTDIR/${basename}_${samp}_${xform}_jpegtran.bmp + rm $OUTDIR/${basename}_${samp}_${xform}.bmp + done + for samp in 420 422; do + runme $EXEDIR/djpeg -nosmooth -rgb -bmp -outfile $OUTDIR/${basename}_${samp}_${xform}_jpegtran.bmp $OUTDIR/${basename}_${samp}_${xform}_jpegtran.jpg + runme "$JAVA" $JAVAARGS TJExample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${xform}.bmp -$xform -crop 70x60+16+16 -fastupsample + runme cmp -i 54:54 $OUTDIR/${basename}_${samp}_${xform}.bmp $OUTDIR/${basename}_${samp}_${xform}_jpegtran.bmp + rm $OUTDIR/${basename}_${samp}_${xform}.bmp + done + done + + # Grayscale transform + for xform in hflip vflip transpose transverse rot90 rot180 rot270; do + for samp in GRAY 444 422 420; do + runme "$JAVA" $JAVAARGS TJExample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${xform}.jpg -$xform -grayscale -crop 70x60+16+16 + runme cmp $OUTDIR/${basename}_${samp}_${xform}.jpg $OUTDIR/${basename}_GRAY_${xform}_jpegtran.jpg + runme "$JAVA" $JAVAARGS TJExample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${xform}.bmp -$xform -grayscale -crop 70x60+16+16 + runme cmp -i 54:54 $OUTDIR/${basename}_${samp}_${xform}.bmp $OUTDIR/${basename}_GRAY_${xform}_jpegtran.bmp + rm $OUTDIR/${basename}_${samp}_${xform}.bmp + done + done + + # Transforms with scaling + for xform in hflip vflip transpose transverse rot90 rot180 rot270; do + for samp in GRAY 444 422 420; do + for scale in 2_1 15_8 7_4 13_8 3_2 11_8 5_4 9_8 7_8 3_4 5_8 1_2 3_8 1_4 1_8; do + scalearg=`echo $scale | sed 's/\_/\//g'` + runme $EXEDIR/djpeg -rgb -bmp -scale ${scalearg} -outfile $OUTDIR/${basename}_${samp}_${xform}_${scale}_jpegtran.bmp $OUTDIR/${basename}_${samp}_${xform}_jpegtran.jpg + runme "$JAVA" $JAVAARGS TJExample $OUTDIR/${basename}_${samp}_fast.jpg $OUTDIR/${basename}_${samp}_${xform}_${scale}.bmp -$xform -scale ${scalearg} -crop 70x60+16+16 + runme cmp -i 54:54 $OUTDIR/${basename}_${samp}_${xform}_${scale}.bmp $OUTDIR/${basename}_${samp}_${xform}_${scale}_jpegtran.bmp + rm $OUTDIR/${basename}_${samp}_${xform}_${scale}.bmp + done + done + done + +done + +echo SUCCESS! diff --git a/turbojpeg.c b/turbojpeg.c index 46ea36b..771b904 100644 --- a/turbojpeg.c +++ b/turbojpeg.c @@ -53,7 +53,7 @@ extern void jpeg_mem_src_tj(j_decompress_ptr, const unsigned char *, #define isPow2(x) (((x)&(x-1))==0) -/* Error handling (based on example in example.c) */ +/* Error handling (based on example in example.txt) */ static char errStr[JMSG_LENGTH_MAX]="No error"; @@ -412,7 +412,7 @@ static tjhandle _tjInitCompress(tjinstance *this) static unsigned char buffer[1]; unsigned char *buf=buffer; unsigned long size=1; - /* This is also straight out of example.c */ + /* This is also straight out of example.txt */ this->cinfo.err=jpeg_std_error(&this->jerr.pub); this->jerr.pub.error_exit=my_error_exit; this->jerr.pub.output_message=my_output_message; @@ -1067,7 +1067,7 @@ static tjhandle _tjInitDecompress(tjinstance *this) { static unsigned char buffer[1]; - /* This is also straight out of example.c */ + /* This is also straight out of example.txt */ this->dinfo.err=jpeg_std_error(&this->jerr.pub); this->jerr.pub.error_exit=my_error_exit; this->jerr.pub.output_message=my_output_message;