jpeg_crop_scanlines: Handle gray images w/ samp!=1
Since the sampling factor has no meaning for single-component images,
the decompressor ignores it, and jpeg_crop_scanlines() should as well.
Fixes #195
diff --git a/ChangeLog.md b/ChangeLog.md
index d7d31ca..f5fe44b 100644
--- a/ChangeLog.md
+++ b/ChangeLog.md
@@ -37,6 +37,10 @@
`jpeg_consume_input()` would return `JPEG_SUSPENDED` rather than
`JPEG_REACHED_EOI`.
+9. `jpeg_crop_scanlines()` now works correctly when decompressing grayscale
+JPEG images that were compressed with a sampling factor other than 1 (for
+instance, with `cjpeg -grayscale -sample 2x2`).
+
1.5.2
=====
diff --git a/jdapistd.c b/jdapistd.c
index 14fc6e3..105121d 100644
--- a/jdapistd.c
+++ b/jdapistd.c
@@ -190,7 +190,10 @@
* single-pass decompression case, allowing us to use the same MCU column
* width for all of the components.
*/
- align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor;
+ if (cinfo->comps_in_scan == 1 && cinfo->num_components == 1)
+ align = cinfo->_min_DCT_scaled_size;
+ else
+ align = cinfo->_min_DCT_scaled_size * cinfo->max_h_samp_factor;
/* Adjust xoffset to the nearest iMCU boundary <= the requested value */
input_xoffset = *xoffset;
@@ -215,6 +218,9 @@
for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
ci++, compptr++) {
+ int hsf = (cinfo->comps_in_scan == 1 && cinfo->num_components == 1) ?
+ 1 : compptr->h_samp_factor;
+
/* Set downsampled_width to the new output width. */
orig_downsampled_width = compptr->downsampled_width;
compptr->downsampled_width =
@@ -228,11 +234,10 @@
* values will be used in multi-scan decompressions.
*/
cinfo->master->first_MCU_col[ci] =
- (JDIMENSION) (long) (*xoffset * compptr->h_samp_factor) /
- (long) align;
+ (JDIMENSION) (long) (*xoffset * hsf) / (long) align;
cinfo->master->last_MCU_col[ci] =
(JDIMENSION) jdiv_round_up((long) ((*xoffset + cinfo->output_width) *
- compptr->h_samp_factor),
+ hsf),
(long) align) - 1;
}