mirror of
https://github.com/webmproject/libwebp.git
synced 2024-12-27 06:08:21 +01:00
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
This commit is contained in:
parent
0524d9e5e8
commit
b5a36cc9ad
@ -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 \
|
||||
|
@ -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 \
|
||||
|
@ -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 <string> ......... 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")) {
|
||||
|
@ -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 \
|
||||
|
@ -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.
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
138
src/enc/near_lossless.c
Normal file
138
src/enc/near_lossless.c
Normal file
@ -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 <stdlib.h>
|
||||
|
||||
#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<<bits), a-(1<<bits)} and returns the nearest one.
|
||||
static int FindClosestDiscretized(int a, int bits) {
|
||||
int best_val, min_distance, i;
|
||||
GetValAndDistance(a, a, bits, &best_val, &min_distance);
|
||||
|
||||
for (i = -1; i <= 1; i += 2) {
|
||||
int val = a + i * (1 << bits);
|
||||
int candidate, distance;
|
||||
if (val < 0) {
|
||||
val = 0;
|
||||
} else if (val > 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;
|
||||
}
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user