From b5a36cc9adda0c1876d7ad686a442068774dd8b9 Mon Sep 17 00:00:00 2001 From: skal Date: Tue, 5 Aug 2014 19:14:57 +0200 Subject: [PATCH] add -near_lossless [0..100] experimental option This compresses the uimage using lossless compression and controlable decimating pre-process. Code is under WEBP_EXPERIMENTAL_FEATURE while it's being experimented with. Change-Id: I8b7f4cfcc3c6afc52a556102842bdbb045ed5ee8 --- Android.mk | 1 + Makefile.vc | 1 + examples/cwebp.c | 7 ++ makefile.unix | 1 + man/cwebp.1 | 6 ++ src/enc/Makefile.am | 1 + src/enc/config.c | 3 + src/enc/near_lossless.c | 138 ++++++++++++++++++++++++++++++++++++++++ src/enc/vp8enci.h | 4 ++ src/enc/webpenc.c | 5 ++ src/webp/encode.h | 5 +- 11 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 src/enc/near_lossless.c diff --git a/Android.mk b/Android.mk index e02caef2..6d45a49f 100644 --- a/Android.mk +++ b/Android.mk @@ -63,6 +63,7 @@ LOCAL_SRC_FILES := \ src/enc/frame.c \ src/enc/histogram.c \ src/enc/iterator.c \ + src/enc/near_lossless.c \ src/enc/picture.c \ src/enc/picture_csp.c \ src/enc/picture_psnr.c \ diff --git a/Makefile.vc b/Makefile.vc index 5a387846..4d2956af 100644 --- a/Makefile.vc +++ b/Makefile.vc @@ -217,6 +217,7 @@ ENC_OBJS = \ $(DIROBJ)\enc\frame.obj \ $(DIROBJ)\enc\histogram.obj \ $(DIROBJ)\enc\iterator.obj \ + $(DIROBJ)\enc\near_lossless.obj \ $(DIROBJ)\enc\picture.obj \ $(DIROBJ)\enc\picture_csp.obj \ $(DIROBJ)\enc\picture_psnr.obj \ diff --git a/examples/cwebp.c b/examples/cwebp.c index 8e91c735..757c94fe 100644 --- a/examples/cwebp.c +++ b/examples/cwebp.c @@ -612,6 +612,10 @@ static void HelpLong(void) { " green=0xe0 and blue=0xd0\n"); printf(" -noalpha ............... discard any transparency information\n"); printf(" -lossless .............. encode image losslessly\n"); +#ifdef WEBP_EXPERIMENTAL_FEATURES + printf(" -near_lossless ......... use near-lossless image\n" + " preprocessing (0=off..100)\n"); +#endif printf(" -hint ......... specify image characteristics hint,\n"); printf(" one of: photo, picture or graph\n"); @@ -765,6 +769,9 @@ int main(int argc, const char *argv[]) { keep_alpha = 0; } else if (!strcmp(argv[c], "-lossless")) { config.lossless = 1; + } else if (!strcmp(argv[c], "-near_lossless") && c < argc - 1) { + config.near_lossless = strtol(argv[++c], NULL, 0); + config.lossless = 1; // use near-lossless only with lossless } else if (!strcmp(argv[c], "-hint") && c < argc - 1) { ++c; if (!strcmp(argv[c], "photo")) { diff --git a/makefile.unix b/makefile.unix index 023b5217..6a84fa43 100644 --- a/makefile.unix +++ b/makefile.unix @@ -142,6 +142,7 @@ ENC_OBJS = \ src/enc/frame.o \ src/enc/histogram.o \ src/enc/iterator.o \ + src/enc/near_lossless.o \ src/enc/picture.o \ src/enc/picture_csp.o \ src/enc/picture_psnr.o \ diff --git a/man/cwebp.1 b/man/cwebp.1 index 75b667cd..d5a3beb2 100644 --- a/man/cwebp.1 +++ b/man/cwebp.1 @@ -218,6 +218,12 @@ Using this option will discard the alpha channel. .B \-lossless Encode the image without any loss. .TP +.\" .B \-near_lossless " int +.\" Use near-lossless image preprocessing. This option adjusts pixel values +.\" to help compressibility, but has minimal impact on the visual quality. +.\" It triggers lossless compression mode automatically. +.\" Range is 0 (no preprocessing, the default) to 100. +.\" .TP .BI \-hint " string Specify the hint about input image type. Possible values are: \fBphoto\fP, \fBpicture\fP or \fBgraph\fP. diff --git a/src/enc/Makefile.am b/src/enc/Makefile.am index 0dc375b8..91714fc4 100644 --- a/src/enc/Makefile.am +++ b/src/enc/Makefile.am @@ -11,6 +11,7 @@ libwebpencode_la_SOURCES += filter.c libwebpencode_la_SOURCES += frame.c libwebpencode_la_SOURCES += histogram.c libwebpencode_la_SOURCES += iterator.c +libwebpencode_la_SOURCES += near_lossless.c libwebpencode_la_SOURCES += picture.c libwebpencode_la_SOURCES += picture_csp.c libwebpencode_la_SOURCES += picture_psnr.c diff --git a/src/enc/config.c b/src/enc/config.c index 6531438f..34dcbfe4 100644 --- a/src/enc/config.c +++ b/src/enc/config.c @@ -47,6 +47,7 @@ int WebPConfigInitInternal(WebPConfig* config, config->emulate_jpeg_size = 0; config->thread_level = 0; config->low_memory = 0; + config->near_lossless = 0; // TODO(skal): tune. switch (preset) { @@ -125,6 +126,8 @@ int WebPValidateConfig(const WebPConfig* config) { return 0; if (config->lossless < 0 || config->lossless > 1) return 0; + if (config->near_lossless < 0 || config->near_lossless > 100) + return 0; if (config->image_hint >= WEBP_HINT_LAST) return 0; if (config->emulate_jpeg_size < 0 || config->emulate_jpeg_size > 1) diff --git a/src/enc/near_lossless.c b/src/enc/near_lossless.c new file mode 100644 index 00000000..93aeaa0e --- /dev/null +++ b/src/enc/near_lossless.c @@ -0,0 +1,138 @@ +// Copyright 2014 Google Inc. All Rights Reserved. +// +// Use of this source code is governed by a BSD-style license +// that can be found in the COPYING file in the root of the source +// tree. An additional intellectual property rights grant can be found +// in the file PATENTS. All contributing project authors may +// be found in the AUTHORS file in the root of the source tree. +// ----------------------------------------------------------------------------- +// +// Near-lossless image preprocessing adjusts pixel values to help +// compressibility with a guarantee of maximum deviation between original and +// resulting pixel values. +// +// Author: Jyrki Alakuijala (jyrki@google.com) +// Converted to C by Aleksander Kramarz (akramarz@google.com) + +#include + +#include "./vp8enci.h" +#include "../utils/utils.h" + +#ifdef WEBP_EXPERIMENTAL_FEATURES +// Computes quantized pixel value and distance from original value. +static void GetValAndDistance(int a, int initial, int bits, + int* const val, int* const distance) { + const int mask = ~((1 << bits) - 1); + *val = (initial & mask) | (initial >> (8 - bits)); + *distance = 2 * abs(a - *val); +} + +// Quantizes values {a, a+(1< 255) { + val = 255; + } + GetValAndDistance(a, val, bits, &candidate, &distance); + ++distance; + // Smallest distance but favor i == 0 over i == -1 and i == 1 + // since that keeps the overall intensity more constant in the + // images. + if (distance < min_distance) { + min_distance = distance; + best_val = candidate; + } + } + return best_val; +} + +// Applies FindClosestDiscretized to all channels of pixel. +static uint32_t ClosestDiscretizedArgb(uint32_t a, int bits) { + return (FindClosestDiscretized(a >> 24, bits) << 24) | + (FindClosestDiscretized((a >> 16) & 0xff, bits) << 16) | + (FindClosestDiscretized((a >> 8) & 0xff, bits) << 8) | + (FindClosestDiscretized(a & 0xff, bits)); +} + +// Checks if distance between corresponding channel values of pixels a and b +// exceeds given limit. +static int IsFar(uint32_t a, uint32_t b, int limit) { + int k; + for (k = 0; k < 4; ++k) { + const int delta = (int)((a >> (k * 8)) & 0xff) - + (int)((b >> (k * 8)) & 0xff); + if (delta >= limit || delta <= -limit) { + return 1; + } + } + return 0; +} + +// Adjusts pixel values of image with given maximum error. +static void NearLossless(int xsize, int ysize, uint32_t* argb, + int limit_bits, uint32_t* copy_buffer) { + int x, y; + const int limit = 1 << limit_bits; + memcpy(copy_buffer, argb, xsize * ysize * sizeof(argb[0])); + + for (y = 0; y < ysize; ++y) { + const int offset = y * xsize; + for (x = 0; x < xsize; ++x) { + const int ix = offset + x; + // Check that all pixels in 4-connected neighborhood are smooth. + int smooth_area = 1; + if (x != 0 && IsFar(copy_buffer[ix], copy_buffer[ix - 1], limit)) { + smooth_area = 0; + } else if (y != 0 && + IsFar(copy_buffer[ix], copy_buffer[ix - xsize], limit)) { + smooth_area = 0; + } else if (x != xsize - 1 && + IsFar(copy_buffer[ix], copy_buffer[ix + 1], limit)) { + smooth_area = 0; + } else if (y != ysize - 1 && + IsFar(copy_buffer[ix], copy_buffer[ix + xsize], limit)) { + smooth_area = 0; + } + if (!smooth_area) { + argb[ix] = ClosestDiscretizedArgb(argb[ix], limit_bits); + } + } + } +} +#endif + +// TODO(akramarz): optimize memory to O(xsize) +int VP8ApplyNearLossless(int xsize, int ysize, uint32_t* argb, int quality) { +#ifndef WEBP_EXPERIMENTAL_FEATURES + (void)xsize; + (void)ysize; + (void)argb; + (void)quality; +#else + int i; + uint32_t* const copy_buffer = + (uint32_t *)WebPSafeMalloc(xsize * ysize, sizeof(*copy_buffer)); + // quality mapping 0..12 -> 5 + // 13..100 -> 4..1 + const int limit_bits = 5 - (quality + 12) / 25; + assert(argb != NULL); + assert(limit_bits >= 0); + assert(limit_bits < 31); + if (copy_buffer == NULL) { + return 0; + } + for (i = limit_bits; i != 0; --i) { + NearLossless(xsize, ysize, argb, i, copy_buffer); + } + WebPSafeFree(copy_buffer); +#endif + return 1; +} diff --git a/src/enc/vp8enci.h b/src/enc/vp8enci.h index 4f06ab8e..54b7953d 100644 --- a/src/enc/vp8enci.h +++ b/src/enc/vp8enci.h @@ -569,6 +569,10 @@ int WebPPictureAllocARGB(WebPPicture* const picture, int width, int height); // Returns false in case of error (invalid param, out-of-memory). int WebPPictureAllocYUVA(WebPPicture* const picture, int width, int height); + // in near_lossless.c +// Near lossless preprocessing in RGB color-space. +int VP8ApplyNearLossless(int xsize, int ysize, uint32_t* argb, int quality); + //------------------------------------------------------------------------------ #ifdef __cplusplus diff --git a/src/enc/webpenc.c b/src/enc/webpenc.c index fe8a358f..20a11d7c 100644 --- a/src/enc/webpenc.c +++ b/src/enc/webpenc.c @@ -362,6 +362,11 @@ int WebPEncode(const WebPConfig* config, WebPPicture* pic) { } ok &= DeleteVP8Encoder(enc); // must always be called, even if !ok } else { + if (config->near_lossless > 0 && + !VP8ApplyNearLossless(pic->width, pic->height, + pic->argb, config->near_lossless)) { + return 0; + } // Make sure we have ARGB samples. if (pic->argb == NULL && !WebPPictureYUVAToARGB(pic)) { return 0; diff --git a/src/webp/encode.h b/src/webp/encode.h index ccd2bd66..c23db206 100644 --- a/src/webp/encode.h +++ b/src/webp/encode.h @@ -131,7 +131,10 @@ struct WebPConfig { int thread_level; // If non-zero, try and use multi-threaded encoding. int low_memory; // If set, reduce memory usage (but increase CPU use). - uint32_t pad[5]; // padding for later use + int near_lossless; // Near lossless encoding [0 = off(default) .. 100]. + // This feature is experimental. + + uint32_t pad[4]; // padding for later use }; // Enumerate some predefined settings for WebPConfig, depending on the type