aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/images
diff options
context:
space:
mode:
authorGravatar Matt Sarett <msarett@google.com>2017-05-09 14:02:10 -0400
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2017-05-09 18:37:10 +0000
commitfe3190846c9af2cf19a76d7ab9799d7fa69c2369 (patch)
treecab2adff1d287769e501f25f726585134a5228c2 /src/images
parent443531126ac7d281c49fca48674ec7e14436c2b6 (diff)
Add downsampling option to SkJpegEncoder
Bug: 713862 Change-Id: Ibad6ecf836ccfd355499e1bace7bcd4ba772a97a Reviewed-on: https://skia-review.googlesource.com/15891 Commit-Queue: Matt Sarett <msarett@google.com> Reviewed-by: Leon Scroggins <scroggo@google.com>
Diffstat (limited to 'src/images')
-rw-r--r--src/images/SkJpegEncoder.cpp29
-rw-r--r--src/images/SkJpegEncoder.h25
2 files changed, 54 insertions, 0 deletions
diff --git a/src/images/SkJpegEncoder.cpp b/src/images/SkJpegEncoder.cpp
index d87fed81bc..4b37cd3be3 100644
--- a/src/images/SkJpegEncoder.cpp
+++ b/src/images/SkJpegEncoder.cpp
@@ -152,6 +152,35 @@ bool SkJpegEncoderMgr::setParams(const SkImageInfo& srcInfo, const SkJpegEncoder
fCInfo.input_components = numComponents;
jpeg_set_defaults(&fCInfo);
+ if (kGray_8_SkColorType != srcInfo.colorType()) {
+ switch (options.fDownsample) {
+ case SkJpegEncoder::Downsample::k420:
+ SkASSERT(2 == fCInfo.comp_info[0].h_samp_factor);
+ SkASSERT(2 == fCInfo.comp_info[0].v_samp_factor);
+ SkASSERT(1 == fCInfo.comp_info[1].h_samp_factor);
+ SkASSERT(1 == fCInfo.comp_info[1].v_samp_factor);
+ SkASSERT(1 == fCInfo.comp_info[2].h_samp_factor);
+ SkASSERT(1 == fCInfo.comp_info[2].v_samp_factor);
+ break;
+ case SkJpegEncoder::Downsample::k422:
+ fCInfo.comp_info[0].h_samp_factor = 2;
+ fCInfo.comp_info[0].v_samp_factor = 1;
+ fCInfo.comp_info[1].h_samp_factor = 1;
+ fCInfo.comp_info[1].v_samp_factor = 1;
+ fCInfo.comp_info[2].h_samp_factor = 1;
+ fCInfo.comp_info[2].v_samp_factor = 1;
+ break;
+ case SkJpegEncoder::Downsample::k444:
+ fCInfo.comp_info[0].h_samp_factor = 1;
+ fCInfo.comp_info[0].v_samp_factor = 1;
+ fCInfo.comp_info[1].h_samp_factor = 1;
+ fCInfo.comp_info[1].v_samp_factor = 1;
+ fCInfo.comp_info[2].h_samp_factor = 1;
+ fCInfo.comp_info[2].v_samp_factor = 1;
+ break;
+ }
+ }
+
// Tells libjpeg-turbo to compute optimal Huffman coding tables
// for the image. This improves compression at the cost of
// slower encode performance.
diff --git a/src/images/SkJpegEncoder.h b/src/images/SkJpegEncoder.h
index 07290aeb9f..a8b9dfb540 100644
--- a/src/images/SkJpegEncoder.h
+++ b/src/images/SkJpegEncoder.h
@@ -21,6 +21,23 @@ public:
kBlendOnBlack,
};
+ enum class Downsample {
+ /**
+ * Reduction by a factor of two in both the horizontal and vertical directions.
+ */
+ k420,
+
+ /**
+ * Reduction by a factor of two in the horizontal direction.
+ */
+ k422,
+
+ /**
+ * No downsampling.
+ */
+ k444,
+ };
+
struct Options {
/**
* |fQuality| must be in [0, 100] where 0 corresponds to the lowest quality.
@@ -28,6 +45,14 @@ public:
int fQuality = 100;
/**
+ * Choose the downsampling factor for the U and V components. This is only
+ * meaningful if the |src| is not kGray, since kGray will not be encoded as YUV.
+ *
+ * Our default value matches the libjpeg-turbo default.
+ */
+ Downsample fDownsample = Downsample::k420;
+
+ /**
* Jpegs must be opaque. This instructs the encoder on how to handle input
* images with alpha.
*