Don't optimize non-8-bit images

Also add test cases for additional coverage on image optimization.
This commit is contained in:
Jay Berkenbilt 2019-01-31 16:07:33 -05:00
parent eb49e07c0a
commit 0a470d2daf
9 changed files with 80 additions and 5 deletions

View File

@ -1,5 +1,9 @@
2019-01-31 Jay Berkenbilt <ejb@ql.org>
* Bug fix: do better pre-checks on images before optimizing;
refuse to optimize images that can't be converted to JPEG because
of colorspace or depth.
* Add new options --externalize-inline-images, which converts
inline images larger than a specified size to regular images, and
--ii-min-bytes, which tweaks that size.

5
TODO
View File

@ -1,8 +1,3 @@
Now
===
* Deal with compiler warnings
Soon
====

View File

@ -4454,6 +4454,13 @@ print "\n";
suite and properly handled.
</para>
</listitem>
<listitem>
<para>
When optimizing images, detect and refuse to optimize
images that can't be converted to JPEG because of bit depth
or color space.
</para>
</listitem>
<listitem>
<para>
Linearization and page manipulation APIs now detect and

View File

@ -3816,6 +3816,18 @@ ImageOptimizer::makePipeline(std::string const& description, Pipeline* next)
}
return result;
}
QPDFObjectHandle components_obj = dict.getKey("/BitsPerComponent");
if (! (components_obj.isInteger() && (components_obj.getIntValue() == 8)))
{
QTC::TC("qpdf", "qpdf image optimize bits per component");
if (o.verbose && (! description.empty()))
{
std::cout << whoami << ": " << description
<< ": not optimizing because image has other than"
<< " 8 bits per component" << std::endl;
}
return result;
}
// Files have been seen in the wild whose width and height are
// floating point, which is goofy, but we can deal with it.
JDIMENSION w = static_cast<JDIMENSION>(
@ -3844,6 +3856,7 @@ ImageOptimizer::makePipeline(std::string const& description, Pipeline* next)
}
else
{
QTC::TC("qpdf", "qpdf image optimize colorspace");
if (o.verbose && (! description.empty()))
{
std::cout << whoami << ": " << description

View File

@ -437,3 +437,5 @@ QPDFTokenizer inline image at EOF the old way 0
QPDFTokenizer found EI after more than one try 0
QPDFPageObjectHelper externalize inline image 0
QPDFPageObjectHelper keep inline image 0
qpdf image optimize colorspace 0
qpdf image optimize bits per component 0

View File

@ -2088,6 +2088,8 @@ my @image_opt = (
'--oi-min-width=0 --oi-min-height=0 --oi-min-area=0 --ii-min-bytes=0'],
['large-inline-image', 'inline-images-keep-some', ''],
['large-inline-image', 'inline-images-keep-all', '--keep-inline-images'],
['unsupported-optimization', 'unsupported',
'--oi-min-width=0 --oi-min-height=0 --oi-min-area=0'],
);
$n_tests += 2 * scalar(@image_opt);

View File

@ -0,0 +1,49 @@
{
"pages": [
{
"contents": [
"4 0 R"
],
"images": [
{
"bitspercomponent": 1,
"colorspace": "/DeviceGray",
"decodeparms": [
null
],
"filter": [
"/FlateDecode"
],
"filterable": true,
"height": 200,
"name": "/Im1",
"object": "6 0 R",
"width": 100
},
{
"bitspercomponent": 8,
"colorspace": "/XeviceGray",
"decodeparms": [
null
],
"filter": [
"/FlateDecode"
],
"filterable": true,
"height": 200,
"name": "/Im2",
"object": "7 0 R",
"width": 200
}
],
"label": null,
"object": "3 0 R",
"outlines": [],
"pageposfrom1": 1
}
],
"parameters": {
"decodelevel": "generalized"
},
"version": 1
}

View File

@ -0,0 +1,3 @@
qpdf: image /Im1 on page 1: not optimizing because image has other than 8 bits per component
qpdf: image /Im2 on page 1: not optimizing because qpdf can't optimize images with this colorspace
qpdf: wrote file a.pdf

Binary file not shown.