Implement lossless crop feature from jpeg v7 and v8
git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/trunk@243 632fc199-4ca6-4c93-a231-07263d6284db
This commit is contained in:
@@ -118,6 +118,8 @@ test: testclean all
|
||||
cmp $(srcdir)/testimgp.jpg testoutp.jpg
|
||||
./jpegtran -outfile testoutt.jpg testoutp.jpg
|
||||
cmp $(srcdir)/testimgint.jpg testoutt.jpg
|
||||
./jpegtran -crop 100x100+10+10 -transpose -perfect -outfile testoutcrop.jpg $(srcdir)/testorig.jpg
|
||||
cmp $(srcdir)/testimgcrop.jpg testoutcrop.jpg
|
||||
|
||||
else
|
||||
|
||||
@@ -141,6 +143,8 @@ test: testclean all
|
||||
cmp $(srcdir)/testimgp.jpg testoutp.jpg
|
||||
./jpegtran -outfile testoutt.jpg testoutp.jpg
|
||||
cmp $(srcdir)/testimgint.jpg testoutt.jpg
|
||||
./jpegtran -crop 100x100+10+10 -transpose -perfect -outfile testoutcrop.jpg $(srcdir)/testorig.jpg
|
||||
cmp $(srcdir)/testimgcrop.jpg testoutcrop.jpg
|
||||
|
||||
endif
|
||||
|
||||
|
||||
10
jcmarker.c
10
jcmarker.c
@@ -2,6 +2,7 @@
|
||||
* jcmarker.c
|
||||
*
|
||||
* Copyright (C) 1991-1998, Thomas G. Lane.
|
||||
* Copyright (C) 2010, D. R. Commander.
|
||||
* This file is part of the Independent JPEG Group's software.
|
||||
* For conditions of distribution and use, see the accompanying README file.
|
||||
*
|
||||
@@ -11,6 +12,7 @@
|
||||
#define JPEG_INTERNALS
|
||||
#include "jinclude.h"
|
||||
#include "jpeglib.h"
|
||||
#include "jpegcomp.h"
|
||||
|
||||
|
||||
typedef enum { /* JPEG marker codes */
|
||||
@@ -285,13 +287,13 @@ emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
|
||||
emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
|
||||
|
||||
/* Make sure image isn't bigger than SOF field can handle */
|
||||
if ((long) cinfo->image_height > 65535L ||
|
||||
(long) cinfo->image_width > 65535L)
|
||||
if ((long) cinfo->_jpeg_height > 65535L ||
|
||||
(long) cinfo->_jpeg_width > 65535L)
|
||||
ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
|
||||
|
||||
emit_byte(cinfo, cinfo->data_precision);
|
||||
emit_2bytes(cinfo, (int) cinfo->image_height);
|
||||
emit_2bytes(cinfo, (int) cinfo->image_width);
|
||||
emit_2bytes(cinfo, (int) cinfo->_jpeg_height);
|
||||
emit_2bytes(cinfo, (int) cinfo->_jpeg_width);
|
||||
|
||||
emit_byte(cinfo, cinfo->num_components);
|
||||
|
||||
|
||||
28
jcmaster.c
28
jcmaster.c
@@ -16,6 +16,7 @@
|
||||
#define JPEG_INTERNALS
|
||||
#include "jinclude.h"
|
||||
#include "jpeglib.h"
|
||||
#include "jpegcomp.h"
|
||||
|
||||
|
||||
/* Private state */
|
||||
@@ -65,7 +66,7 @@ jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo)
|
||||
|
||||
|
||||
LOCAL(void)
|
||||
initial_setup (j_compress_ptr cinfo)
|
||||
initial_setup (j_compress_ptr cinfo, boolean transcode_only)
|
||||
/* Do computations that are needed before master selection phase */
|
||||
{
|
||||
int ci;
|
||||
@@ -74,17 +75,18 @@ initial_setup (j_compress_ptr cinfo)
|
||||
JDIMENSION jd_samplesperrow;
|
||||
|
||||
#if JPEG_LIB_VERSION >= 70
|
||||
jpeg_calc_jpeg_dimensions(cinfo);
|
||||
if (!transcode_only)
|
||||
jpeg_calc_jpeg_dimensions(cinfo);
|
||||
#endif
|
||||
|
||||
/* Sanity check on image dimensions */
|
||||
if (cinfo->image_height <= 0 || cinfo->image_width <= 0
|
||||
if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0
|
||||
|| cinfo->num_components <= 0 || cinfo->input_components <= 0)
|
||||
ERREXIT(cinfo, JERR_EMPTY_IMAGE);
|
||||
|
||||
/* Make sure image isn't bigger than I can handle */
|
||||
if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
|
||||
(long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
|
||||
if ((long) cinfo->_jpeg_height > (long) JPEG_MAX_DIMENSION ||
|
||||
(long) cinfo->_jpeg_width > (long) JPEG_MAX_DIMENSION)
|
||||
ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
|
||||
|
||||
/* Width of an input scanline must be representable as JDIMENSION. */
|
||||
@@ -129,17 +131,17 @@ initial_setup (j_compress_ptr cinfo)
|
||||
#endif
|
||||
/* Size in DCT blocks */
|
||||
compptr->width_in_blocks = (JDIMENSION)
|
||||
jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
|
||||
jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor,
|
||||
(long) (cinfo->max_h_samp_factor * DCTSIZE));
|
||||
compptr->height_in_blocks = (JDIMENSION)
|
||||
jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
|
||||
jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor,
|
||||
(long) (cinfo->max_v_samp_factor * DCTSIZE));
|
||||
/* Size in samples */
|
||||
compptr->downsampled_width = (JDIMENSION)
|
||||
jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
|
||||
jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor,
|
||||
(long) cinfo->max_h_samp_factor);
|
||||
compptr->downsampled_height = (JDIMENSION)
|
||||
jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
|
||||
jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor,
|
||||
(long) cinfo->max_v_samp_factor);
|
||||
/* Mark component needed (this flag isn't actually used for compression) */
|
||||
compptr->component_needed = TRUE;
|
||||
@@ -149,7 +151,7 @@ initial_setup (j_compress_ptr cinfo)
|
||||
* main controller will call coefficient controller).
|
||||
*/
|
||||
cinfo->total_iMCU_rows = (JDIMENSION)
|
||||
jdiv_round_up((long) cinfo->image_height,
|
||||
jdiv_round_up((long) cinfo->_jpeg_height,
|
||||
(long) (cinfo->max_v_samp_factor*DCTSIZE));
|
||||
}
|
||||
|
||||
@@ -377,10 +379,10 @@ per_scan_setup (j_compress_ptr cinfo)
|
||||
|
||||
/* Overall image size in MCUs */
|
||||
cinfo->MCUs_per_row = (JDIMENSION)
|
||||
jdiv_round_up((long) cinfo->image_width,
|
||||
jdiv_round_up((long) cinfo->_jpeg_width,
|
||||
(long) (cinfo->max_h_samp_factor*DCTSIZE));
|
||||
cinfo->MCU_rows_in_scan = (JDIMENSION)
|
||||
jdiv_round_up((long) cinfo->image_height,
|
||||
jdiv_round_up((long) cinfo->_jpeg_height,
|
||||
(long) (cinfo->max_v_samp_factor*DCTSIZE));
|
||||
|
||||
cinfo->blocks_in_MCU = 0;
|
||||
@@ -584,7 +586,7 @@ jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
|
||||
master->pub.is_last_pass = FALSE;
|
||||
|
||||
/* Validate parameters, determine derived values */
|
||||
initial_setup(cinfo);
|
||||
initial_setup(cinfo, transcode_only);
|
||||
|
||||
if (cinfo->scan_info != NULL) {
|
||||
#ifdef C_MULTISCAN_FILES_SUPPORTED
|
||||
|
||||
2
jerror.h
2
jerror.h
@@ -2,6 +2,7 @@
|
||||
* jerror.h
|
||||
*
|
||||
* Copyright (C) 1994-1997, Thomas G. Lane.
|
||||
* Modified 1997-2009 by Guido Vollbeding.
|
||||
* This file is part of the Independent JPEG Group's software.
|
||||
* For conditions of distribution and use, see the accompanying README file.
|
||||
*
|
||||
@@ -45,6 +46,7 @@ JMESSAGE(JERR_BAD_ALIGN_TYPE, "ALIGN_TYPE is wrong, please fix")
|
||||
JMESSAGE(JERR_BAD_ALLOC_CHUNK, "MAX_ALLOC_CHUNK is wrong, please fix")
|
||||
JMESSAGE(JERR_BAD_BUFFER_MODE, "Bogus buffer control mode")
|
||||
JMESSAGE(JERR_BAD_COMPONENT_ID, "Invalid component ID %d in SOS")
|
||||
JMESSAGE(JERR_BAD_CROP_SPEC, "Invalid crop request")
|
||||
JMESSAGE(JERR_BAD_DCT_COEF, "DCT coefficient out of range")
|
||||
JMESSAGE(JERR_BAD_DCTSIZE, "IDCT output block size %d not supported")
|
||||
JMESSAGE(JERR_BAD_HUFF_TABLE, "Bogus Huffman table definition")
|
||||
|
||||
@@ -12,7 +12,15 @@
|
||||
#if JPEG_LIB_VERSION >= 70
|
||||
#define _DCT_scaled_size DCT_h_scaled_size
|
||||
#define _min_DCT_scaled_size min_DCT_h_scaled_size
|
||||
#define _min_DCT_h_scaled_size min_DCT_h_scaled_size
|
||||
#define _min_DCT_v_scaled_size min_DCT_v_scaled_size
|
||||
#define _jpeg_width jpeg_width
|
||||
#define _jpeg_height jpeg_height
|
||||
#else
|
||||
#define _DCT_scaled_size DCT_scaled_size
|
||||
#define _min_DCT_scaled_size min_DCT_scaled_size
|
||||
#define _min_DCT_h_scaled_size min_DCT_scaled_size
|
||||
#define _min_DCT_v_scaled_size min_DCT_scaled_size
|
||||
#define _jpeg_width image_width
|
||||
#define _jpeg_height image_height
|
||||
#endif
|
||||
|
||||
57
jpegtran.1
57
jpegtran.1
@@ -1,4 +1,4 @@
|
||||
.TH JPEGTRAN 1 "3 August 1997"
|
||||
.TH JPEGTRAN 1 "28 December 2009"
|
||||
.SH NAME
|
||||
jpegtran \- lossless transformation of JPEG files
|
||||
.SH SYNOPSIS
|
||||
@@ -91,12 +91,12 @@ Transpose image (across UL-to-LR axis).
|
||||
.TP
|
||||
.B \-transverse
|
||||
Transverse transpose (across UR-to-LL axis).
|
||||
.PP
|
||||
.IP
|
||||
The transpose transformation has no restrictions regarding image dimensions.
|
||||
The other transformations operate rather oddly if the image dimensions are not
|
||||
a multiple of the iMCU size (usually 8 or 16 pixels), because they can only
|
||||
transform complete blocks of DCT coefficient data in the desired way.
|
||||
.PP
|
||||
.IP
|
||||
.BR jpegtran 's
|
||||
default behavior when transforming an odd-size image is designed
|
||||
to preserve exact reversibility and mathematical consistency of the
|
||||
@@ -108,7 +108,7 @@ able to flip all columns. The other transforms can be built up as sequences
|
||||
of transpose and flip operations; for consistency, their actions on edge
|
||||
pixels are defined to be the same as the end result of the corresponding
|
||||
transpose-and-flip sequence.
|
||||
.PP
|
||||
.IP
|
||||
For practical use, you may prefer to discard any untransformable edge pixels
|
||||
rather than having a strange-looking strip along the right and/or bottom edges
|
||||
of a transformed image. To do this, add the
|
||||
@@ -117,7 +117,7 @@ switch:
|
||||
.TP
|
||||
.B \-trim
|
||||
Drop non-transformable edge blocks.
|
||||
.PP
|
||||
.IP
|
||||
Obviously, a transformation with
|
||||
.B \-trim
|
||||
is not reversible, so strictly speaking
|
||||
@@ -130,12 +130,43 @@ trims only the bottom edge, but
|
||||
followed by
|
||||
.B \-rot 180 -trim
|
||||
trims both edges.
|
||||
.IP
|
||||
If you are only interested in perfect transformation, add the
|
||||
.B \-perfect
|
||||
switch:
|
||||
.TP
|
||||
.B \-perfect
|
||||
Fails with an error if the transformation is not perfect.
|
||||
.IP
|
||||
For example you may want to do
|
||||
.IP
|
||||
.B (jpegtran \-rot 90 -perfect
|
||||
.I foo.jpg
|
||||
.B || djpeg
|
||||
.I foo.jpg
|
||||
.B | pnmflip \-r90 | cjpeg)
|
||||
.IP
|
||||
to do a perfect rotation if available or an approximated one if not.
|
||||
.PP
|
||||
Another not-strictly-lossless transformation switch is:
|
||||
We also offer a lossless-crop option, which discards data outside a given
|
||||
image region but losslessly preserves what is inside. Like the rotate and
|
||||
flip transforms, lossless crop is restricted by the current JPEG format: the
|
||||
upper left corner of the selected region must fall on an iMCU boundary. If
|
||||
this does not hold for the given crop parameters, we silently move the upper
|
||||
left corner up and/or left to make it so, simultaneously increasing the region
|
||||
dimensions to keep the lower right crop corner unchanged. (Thus, the output
|
||||
image covers at least the requested region, but may cover more.)
|
||||
|
||||
The image can be losslessly cropped by giving the switch:
|
||||
.TP
|
||||
.B \-crop WxH+X+Y
|
||||
Crop to a rectangular subarea of width W, height H starting at point X,Y.
|
||||
.PP
|
||||
Other not-strictly-lossless transformation switches are:
|
||||
.TP
|
||||
.B \-grayscale
|
||||
Force grayscale output.
|
||||
.PP
|
||||
.IP
|
||||
This option discards the chrominance channels if the input image is YCbCr
|
||||
(ie, a standard color JPEG), resulting in a grayscale JPEG file. The
|
||||
luminance channel is preserved exactly, so this is a better method of reducing
|
||||
@@ -155,13 +186,13 @@ comments and other excess baggage present in the source file.
|
||||
.TP
|
||||
.B \-copy comments
|
||||
Copy only comment markers. This setting copies comments from the source file,
|
||||
but discards any other inessential data.
|
||||
but discards any other inessential (for image display) data.
|
||||
.TP
|
||||
.B \-copy all
|
||||
Copy all extra markers. This setting preserves miscellaneous markers
|
||||
found in the source file, such as JFIF thumbnails and Photoshop settings.
|
||||
In some files these extra markers can be sizable.
|
||||
.PP
|
||||
found in the source file, such as JFIF thumbnails, Exif data, and Photoshop
|
||||
settings. In some files these extra markers can be sizable.
|
||||
.IP
|
||||
The default behavior is
|
||||
.BR "\-copy comments" .
|
||||
(Note: in IJG releases v6 and v6a,
|
||||
@@ -231,7 +262,9 @@ Arithmetic coding is not supported for legal reasons.
|
||||
.PP
|
||||
The transform options can't transform odd-size images perfectly. Use
|
||||
.B \-trim
|
||||
if you don't like the results without it.
|
||||
or
|
||||
.B \-perfect
|
||||
if you don't like the results.
|
||||
.PP
|
||||
The entire image is read into memory and then written out again, even in
|
||||
cases where this isn't really necessary. Expect swapping on large images,
|
||||
|
||||
104
jpegtran.c
104
jpegtran.c
@@ -1,14 +1,14 @@
|
||||
/*
|
||||
* jpegtran.c
|
||||
*
|
||||
* Copyright (C) 1995-1997, Thomas G. Lane.
|
||||
* Copyright (C) 1995-2010, Thomas G. Lane, Guido Vollbeding.
|
||||
* This file is part of the Independent JPEG Group's software.
|
||||
* For conditions of distribution and use, see the accompanying README file.
|
||||
*
|
||||
* This file contains a command-line user interface for JPEG transcoding.
|
||||
* It is very similar to cjpeg.c, but provides lossless transcoding between
|
||||
* different JPEG file formats. It also provides some lossless and sort-of-
|
||||
* lossless transformations of JPEG data.
|
||||
* It is very similar to cjpeg.c, and partly to djpeg.c, but provides
|
||||
* lossless transcoding between different JPEG file formats. It also
|
||||
* provides some lossless and sort-of-lossless transformations of JPEG data.
|
||||
*/
|
||||
|
||||
#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
|
||||
@@ -62,15 +62,19 @@ usage (void)
|
||||
#ifdef C_PROGRESSIVE_SUPPORTED
|
||||
fprintf(stderr, " -progressive Create progressive JPEG file\n");
|
||||
#endif
|
||||
#if TRANSFORMS_SUPPORTED
|
||||
fprintf(stderr, "Switches for modifying the image:\n");
|
||||
#if TRANSFORMS_SUPPORTED
|
||||
fprintf(stderr, " -crop WxH+X+Y Crop to a rectangular subarea\n");
|
||||
fprintf(stderr, " -grayscale Reduce to grayscale (omit color data)\n");
|
||||
fprintf(stderr, " -flip [horizontal|vertical] Mirror image (left-right or top-bottom)\n");
|
||||
fprintf(stderr, " -perfect Fail if there is non-transformable edge blocks\n");
|
||||
fprintf(stderr, " -rotate [90|180|270] Rotate image (degrees clockwise)\n");
|
||||
#endif
|
||||
#if TRANSFORMS_SUPPORTED
|
||||
fprintf(stderr, " -transpose Transpose image\n");
|
||||
fprintf(stderr, " -transverse Transverse transpose image\n");
|
||||
fprintf(stderr, " -trim Drop non-transformable edge blocks\n");
|
||||
#endif /* TRANSFORMS_SUPPORTED */
|
||||
#endif
|
||||
fprintf(stderr, "Switches for advanced users:\n");
|
||||
fprintf(stderr, " -restart N Set restart interval in rows, or in blocks with B\n");
|
||||
fprintf(stderr, " -maxmemory N Maximum memory to use (in kbytes)\n");
|
||||
@@ -132,8 +136,10 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,
|
||||
outfilename = NULL;
|
||||
copyoption = JCOPYOPT_DEFAULT;
|
||||
transformoption.transform = JXFORM_NONE;
|
||||
transformoption.perfect = FALSE;
|
||||
transformoption.trim = FALSE;
|
||||
transformoption.force_grayscale = FALSE;
|
||||
transformoption.crop = FALSE;
|
||||
cinfo->err->trace_level = 0;
|
||||
|
||||
/* Scan command line options, adjust parameters */
|
||||
@@ -160,7 +166,7 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,
|
||||
exit(EXIT_FAILURE);
|
||||
#endif
|
||||
|
||||
} else if (keymatch(arg, "copy", 1)) {
|
||||
} else if (keymatch(arg, "copy", 2)) {
|
||||
/* Select which extra markers to copy. */
|
||||
if (++argn >= argc) /* advance to next argument */
|
||||
usage();
|
||||
@@ -173,6 +179,20 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,
|
||||
} else
|
||||
usage();
|
||||
|
||||
} else if (keymatch(arg, "crop", 2)) {
|
||||
/* Perform lossless cropping. */
|
||||
#if TRANSFORMS_SUPPORTED
|
||||
if (++argn >= argc) /* advance to next argument */
|
||||
usage();
|
||||
if (! jtransform_parse_crop_spec(&transformoption, argv[argn])) {
|
||||
fprintf(stderr, "%s: bogus -crop argument '%s'\n",
|
||||
progname, argv[argn]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
#else
|
||||
select_transform(JXFORM_NONE); /* force an error */
|
||||
#endif
|
||||
|
||||
} else if (keymatch(arg, "debug", 1) || keymatch(arg, "verbose", 1)) {
|
||||
/* Enable debug printouts. */
|
||||
/* On first -d, print version identification */
|
||||
@@ -233,7 +253,12 @@ parse_switches (j_compress_ptr cinfo, int argc, char **argv,
|
||||
usage();
|
||||
outfilename = argv[argn]; /* save it away for later use */
|
||||
|
||||
} else if (keymatch(arg, "progressive", 1)) {
|
||||
} else if (keymatch(arg, "perfect", 2)) {
|
||||
/* Fail if there is any partial edge MCUs that the transform can't
|
||||
* handle. */
|
||||
transformoption.perfect = TRUE;
|
||||
|
||||
} else if (keymatch(arg, "progressive", 2)) {
|
||||
/* Select simple progressive mode. */
|
||||
#ifdef C_PROGRESSIVE_SUPPORTED
|
||||
simple_progressive = TRUE;
|
||||
@@ -342,8 +367,10 @@ main (int argc, char **argv)
|
||||
jvirt_barray_ptr * src_coef_arrays;
|
||||
jvirt_barray_ptr * dst_coef_arrays;
|
||||
int file_index;
|
||||
FILE * input_file;
|
||||
FILE * output_file;
|
||||
/* We assume all-in-memory processing and can therefore use only a
|
||||
* single file pointer for sequential input and output operation.
|
||||
*/
|
||||
FILE * fp;
|
||||
|
||||
/* On Mac, fetch a command line. */
|
||||
#ifdef USE_CCOMMAND
|
||||
@@ -406,24 +433,13 @@ main (int argc, char **argv)
|
||||
|
||||
/* Open the input file. */
|
||||
if (file_index < argc) {
|
||||
if ((input_file = fopen(argv[file_index], READ_BINARY)) == NULL) {
|
||||
fprintf(stderr, "%s: can't open %s\n", progname, argv[file_index]);
|
||||
if ((fp = fopen(argv[file_index], READ_BINARY)) == NULL) {
|
||||
fprintf(stderr, "%s: can't open %s for reading\n", progname, argv[file_index]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
} else {
|
||||
/* default input file is stdin */
|
||||
input_file = read_stdin();
|
||||
}
|
||||
|
||||
/* Open the output file. */
|
||||
if (outfilename != NULL) {
|
||||
if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
|
||||
fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
} else {
|
||||
/* default output file is stdout */
|
||||
output_file = write_stdout();
|
||||
fp = read_stdin();
|
||||
}
|
||||
|
||||
#ifdef PROGRESS_REPORT
|
||||
@@ -431,7 +447,7 @@ main (int argc, char **argv)
|
||||
#endif
|
||||
|
||||
/* Specify data source for decompression */
|
||||
jpeg_stdio_src(&srcinfo, input_file);
|
||||
jpeg_stdio_src(&srcinfo, fp);
|
||||
|
||||
/* Enable saving of extra markers that we want to copy */
|
||||
jcopy_markers_setup(&srcinfo, copyoption);
|
||||
@@ -443,7 +459,12 @@ main (int argc, char **argv)
|
||||
* jpeg_read_coefficients so that memory allocation will be done right.
|
||||
*/
|
||||
#if TRANSFORMS_SUPPORTED
|
||||
jtransform_request_workspace(&srcinfo, &transformoption);
|
||||
/* Fail right away if -perfect is given and transformation is not perfect.
|
||||
*/
|
||||
if (!jtransform_request_workspace(&srcinfo, &transformoption)) {
|
||||
fprintf(stderr, "%s: transformation is not perfect\n", progname);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Read source file as DCT coefficients */
|
||||
@@ -463,11 +484,32 @@ main (int argc, char **argv)
|
||||
dst_coef_arrays = src_coef_arrays;
|
||||
#endif
|
||||
|
||||
/* Close input file, if we opened it.
|
||||
* Note: we assume that jpeg_read_coefficients consumed all input
|
||||
* until JPEG_REACHED_EOI, and that jpeg_finish_decompress will
|
||||
* only consume more while (! cinfo->inputctl->eoi_reached).
|
||||
* We cannot call jpeg_finish_decompress here since we still need the
|
||||
* virtual arrays allocated from the source object for processing.
|
||||
*/
|
||||
if (fp != stdin)
|
||||
fclose(fp);
|
||||
|
||||
/* Open the output file. */
|
||||
if (outfilename != NULL) {
|
||||
if ((fp = fopen(outfilename, WRITE_BINARY)) == NULL) {
|
||||
fprintf(stderr, "%s: can't open %s for writing\n", progname, outfilename);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
} else {
|
||||
/* default output file is stdout */
|
||||
fp = write_stdout();
|
||||
}
|
||||
|
||||
/* Adjust default compression parameters by re-parsing the options */
|
||||
file_index = parse_switches(&dstinfo, argc, argv, 0, TRUE);
|
||||
|
||||
/* Specify data destination for compression */
|
||||
jpeg_stdio_dest(&dstinfo, output_file);
|
||||
jpeg_stdio_dest(&dstinfo, fp);
|
||||
|
||||
/* Start compressor (note no image data is actually written here) */
|
||||
jpeg_write_coefficients(&dstinfo, dst_coef_arrays);
|
||||
@@ -488,11 +530,9 @@ main (int argc, char **argv)
|
||||
(void) jpeg_finish_decompress(&srcinfo);
|
||||
jpeg_destroy_decompress(&srcinfo);
|
||||
|
||||
/* Close files, if we opened them */
|
||||
if (input_file != stdin)
|
||||
fclose(input_file);
|
||||
if (output_file != stdout)
|
||||
fclose(output_file);
|
||||
/* Close output file, if we opened it */
|
||||
if (fp != stdout)
|
||||
fclose(fp);
|
||||
|
||||
#ifdef PROGRESS_REPORT
|
||||
end_progress_monitor((j_common_ptr) &dstinfo);
|
||||
|
||||
BIN
testimgcrop.jpg
Normal file
BIN
testimgcrop.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.3 KiB |
1139
transupp.c
1139
transupp.c
File diff suppressed because it is too large
Load Diff
133
transupp.h
133
transupp.h
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* transupp.h
|
||||
*
|
||||
* Copyright (C) 1997, Thomas G. Lane.
|
||||
* Copyright (C) 1997-2009, Thomas G. Lane, Guido Vollbeding.
|
||||
* This file is part of the Independent JPEG Group's software.
|
||||
* For conditions of distribution and use, see the accompanying README file.
|
||||
*
|
||||
@@ -22,32 +22,6 @@
|
||||
#define TRANSFORMS_SUPPORTED 1 /* 0 disables transform code */
|
||||
#endif
|
||||
|
||||
/* Short forms of external names for systems with brain-damaged linkers. */
|
||||
|
||||
#ifdef NEED_SHORT_EXTERNAL_NAMES
|
||||
#define jtransform_request_workspace jTrRequest
|
||||
#define jtransform_adjust_parameters jTrAdjust
|
||||
#define jtransform_execute_transformation jTrExec
|
||||
#define jcopy_markers_setup jCMrkSetup
|
||||
#define jcopy_markers_execute jCMrkExec
|
||||
#endif /* NEED_SHORT_EXTERNAL_NAMES */
|
||||
|
||||
|
||||
/*
|
||||
* Codes for supported types of image transformations.
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
JXFORM_NONE, /* no transformation */
|
||||
JXFORM_FLIP_H, /* horizontal flip */
|
||||
JXFORM_FLIP_V, /* vertical flip */
|
||||
JXFORM_TRANSPOSE, /* transpose across UL-to-LR axis */
|
||||
JXFORM_TRANSVERSE, /* transpose across UR-to-LL axis */
|
||||
JXFORM_ROT_90, /* 90-degree clockwise rotation */
|
||||
JXFORM_ROT_180, /* 180-degree rotation */
|
||||
JXFORM_ROT_270 /* 270-degree clockwise (or 90 ccw) */
|
||||
} JXFORM_CODE;
|
||||
|
||||
/*
|
||||
* Although rotating and flipping data expressed as DCT coefficients is not
|
||||
* hard, there is an asymmetry in the JPEG format specification for images
|
||||
@@ -75,6 +49,24 @@ typedef enum {
|
||||
* (For example, -rot 270 -trim trims only the bottom edge, but -rot 90 -trim
|
||||
* followed by -rot 180 -trim trims both edges.)
|
||||
*
|
||||
* We also offer a lossless-crop option, which discards data outside a given
|
||||
* image region but losslessly preserves what is inside. Like the rotate and
|
||||
* flip transforms, lossless crop is restricted by the JPEG format: the upper
|
||||
* left corner of the selected region must fall on an iMCU boundary. If this
|
||||
* does not hold for the given crop parameters, we silently move the upper left
|
||||
* corner up and/or left to make it so, simultaneously increasing the region
|
||||
* dimensions to keep the lower right crop corner unchanged. (Thus, the
|
||||
* output image covers at least the requested region, but may cover more.)
|
||||
*
|
||||
* We also provide a lossless-resize option, which is kind of a lossless-crop
|
||||
* operation in the DCT coefficient block domain - it discards higher-order
|
||||
* coefficients and losslessly preserves lower-order coefficients of a
|
||||
* sub-block.
|
||||
*
|
||||
* Rotate/flip transform, resize, and crop can be requested together in a
|
||||
* single invocation. The crop is applied last --- that is, the crop region
|
||||
* is specified in terms of the destination image after transform/resize.
|
||||
*
|
||||
* We also offer a "force to grayscale" option, which simply discards the
|
||||
* chrominance channels of a YCbCr image. This is lossless in the sense that
|
||||
* the luminance channel is preserved exactly. It's not the same kind of
|
||||
@@ -83,22 +75,91 @@ typedef enum {
|
||||
* be aware of the option to know how many components to work on.
|
||||
*/
|
||||
|
||||
|
||||
/* Short forms of external names for systems with brain-damaged linkers. */
|
||||
|
||||
#ifdef NEED_SHORT_EXTERNAL_NAMES
|
||||
#define jtransform_parse_crop_spec jTrParCrop
|
||||
#define jtransform_request_workspace jTrRequest
|
||||
#define jtransform_adjust_parameters jTrAdjust
|
||||
#define jtransform_execute_transform jTrExec
|
||||
#define jtransform_perfect_transform jTrPerfect
|
||||
#define jcopy_markers_setup jCMrkSetup
|
||||
#define jcopy_markers_execute jCMrkExec
|
||||
#endif /* NEED_SHORT_EXTERNAL_NAMES */
|
||||
|
||||
|
||||
/*
|
||||
* Codes for supported types of image transformations.
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
JXFORM_NONE, /* no transformation */
|
||||
JXFORM_FLIP_H, /* horizontal flip */
|
||||
JXFORM_FLIP_V, /* vertical flip */
|
||||
JXFORM_TRANSPOSE, /* transpose across UL-to-LR axis */
|
||||
JXFORM_TRANSVERSE, /* transpose across UR-to-LL axis */
|
||||
JXFORM_ROT_90, /* 90-degree clockwise rotation */
|
||||
JXFORM_ROT_180, /* 180-degree rotation */
|
||||
JXFORM_ROT_270 /* 270-degree clockwise (or 90 ccw) */
|
||||
} JXFORM_CODE;
|
||||
|
||||
/*
|
||||
* Codes for crop parameters, which can individually be unspecified,
|
||||
* positive, or negative. (Negative width or height makes no sense, though.)
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
JCROP_UNSET,
|
||||
JCROP_POS,
|
||||
JCROP_NEG
|
||||
} JCROP_CODE;
|
||||
|
||||
/*
|
||||
* Transform parameters struct.
|
||||
* NB: application must not change any elements of this struct after
|
||||
* calling jtransform_request_workspace.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
/* Options: set by caller */
|
||||
JXFORM_CODE transform; /* image transform operator */
|
||||
boolean perfect; /* if TRUE, fail if partial MCUs are requested */
|
||||
boolean trim; /* if TRUE, trim partial MCUs as needed */
|
||||
boolean force_grayscale; /* if TRUE, convert color image to grayscale */
|
||||
boolean crop; /* if TRUE, crop source image */
|
||||
|
||||
/* Crop parameters: application need not set these unless crop is TRUE.
|
||||
* These can be filled in by jtransform_parse_crop_spec().
|
||||
*/
|
||||
JDIMENSION crop_width; /* Width of selected region */
|
||||
JCROP_CODE crop_width_set;
|
||||
JDIMENSION crop_height; /* Height of selected region */
|
||||
JCROP_CODE crop_height_set;
|
||||
JDIMENSION crop_xoffset; /* X offset of selected region */
|
||||
JCROP_CODE crop_xoffset_set; /* (negative measures from right edge) */
|
||||
JDIMENSION crop_yoffset; /* Y offset of selected region */
|
||||
JCROP_CODE crop_yoffset_set; /* (negative measures from bottom edge) */
|
||||
|
||||
/* Internal workspace: caller should not touch these */
|
||||
int num_components; /* # of components in workspace */
|
||||
jvirt_barray_ptr * workspace_coef_arrays; /* workspace for transformations */
|
||||
JDIMENSION output_width; /* cropped destination dimensions */
|
||||
JDIMENSION output_height;
|
||||
JDIMENSION x_crop_offset; /* destination crop offsets measured in iMCUs */
|
||||
JDIMENSION y_crop_offset;
|
||||
int iMCU_sample_width; /* destination iMCU size */
|
||||
int iMCU_sample_height;
|
||||
} jpeg_transform_info;
|
||||
|
||||
|
||||
#if TRANSFORMS_SUPPORTED
|
||||
|
||||
/* Parse a crop specification (written in X11 geometry style) */
|
||||
EXTERN(boolean) jtransform_parse_crop_spec
|
||||
JPP((jpeg_transform_info *info, const char *spec));
|
||||
/* Request any required workspace */
|
||||
EXTERN(void) jtransform_request_workspace
|
||||
EXTERN(boolean) jtransform_request_workspace
|
||||
JPP((j_decompress_ptr srcinfo, jpeg_transform_info *info));
|
||||
/* Adjust output image parameters */
|
||||
EXTERN(jvirt_barray_ptr *) jtransform_adjust_parameters
|
||||
@@ -106,10 +167,24 @@ EXTERN(jvirt_barray_ptr *) jtransform_adjust_parameters
|
||||
jvirt_barray_ptr *src_coef_arrays,
|
||||
jpeg_transform_info *info));
|
||||
/* Execute the actual transformation, if any */
|
||||
EXTERN(void) jtransform_execute_transformation
|
||||
EXTERN(void) jtransform_execute_transform
|
||||
JPP((j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
|
||||
jvirt_barray_ptr *src_coef_arrays,
|
||||
jpeg_transform_info *info));
|
||||
/* Determine whether lossless transformation is perfectly
|
||||
* possible for a specified image and transformation.
|
||||
*/
|
||||
EXTERN(boolean) jtransform_perfect_transform
|
||||
JPP((JDIMENSION image_width, JDIMENSION image_height,
|
||||
int MCU_width, int MCU_height,
|
||||
JXFORM_CODE transform));
|
||||
|
||||
/* jtransform_execute_transform used to be called
|
||||
* jtransform_execute_transformation, but some compilers complain about
|
||||
* routine names that long. This macro is here to avoid breaking any
|
||||
* old source code that uses the original name...
|
||||
*/
|
||||
#define jtransform_execute_transformation jtransform_execute_transform
|
||||
|
||||
#endif /* TRANSFORMS_SUPPORTED */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user