Coverage Report

Created: 2026-05-16 07:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebp/src/enc/near_lossless_enc.c
Line
Count
Source
1
// Copyright 2014 Google Inc. All Rights Reserved.
2
//
3
// Use of this source code is governed by a BSD-style license
4
// that can be found in the COPYING file in the root of the source
5
// tree. An additional intellectual property rights grant can be found
6
// in the file PATENTS. All contributing project authors may
7
// be found in the AUTHORS file in the root of the source tree.
8
// -----------------------------------------------------------------------------
9
//
10
// Near-lossless image preprocessing adjusts pixel values to help
11
// compressibility with a guarantee of maximum deviation between original and
12
// resulting pixel values.
13
//
14
// Author: Jyrki Alakuijala (jyrki@google.com)
15
// Converted to C by Aleksander Kramarz (akramarz@google.com)
16
17
#include <assert.h>
18
#include <stdlib.h>
19
#include <string.h>
20
21
#include "src/dsp/lossless_common.h"
22
#include "src/enc/vp8li_enc.h"
23
#include "src/utils/utils.h"
24
#include "src/webp/encode.h"
25
#include "src/webp/types.h"
26
27
#if (WEBP_NEAR_LOSSLESS == 1)
28
29
0
#define MIN_DIM_FOR_NEAR_LOSSLESS 64
30
#define MAX_LIMIT_BITS 5
31
32
// Quantizes the value up or down to a multiple of 1<<bits (or to 255),
33
// choosing the closer one, resolving ties using bankers' rounding.
34
0
static uint32_t FindClosestDiscretized(uint32_t a, int bits) {
35
0
  const uint32_t mask = (1u << bits) - 1;
36
0
  const uint32_t biased = a + (mask >> 1) + ((a >> bits) & 1);
37
0
  assert(bits > 0);
38
0
  if (biased > 0xff) return 0xff;
39
0
  return biased & ~mask;
40
0
}
41
42
// Applies FindClosestDiscretized to all channels of pixel.
43
0
static uint32_t ClosestDiscretizedArgb(uint32_t a, int bits) {
44
0
  return (FindClosestDiscretized(a >> 24, bits) << 24) |
45
0
         (FindClosestDiscretized((a >> 16) & 0xff, bits) << 16) |
46
0
         (FindClosestDiscretized((a >> 8) & 0xff, bits) << 8) |
47
0
         (FindClosestDiscretized(a & 0xff, bits));
48
0
}
49
50
// Checks if distance between corresponding channel values of pixels a and b
51
// is within the given limit.
52
0
static int IsNear(uint32_t a, uint32_t b, int limit) {
53
0
  int k;
54
0
  for (k = 0; k < 4; ++k) {
55
0
    const int delta =
56
0
        (int)((a >> (k * 8)) & 0xff) - (int)((b >> (k * 8)) & 0xff);
57
0
    if (delta >= limit || delta <= -limit) {
58
0
      return 0;
59
0
    }
60
0
  }
61
0
  return 1;
62
0
}
63
64
static int IsSmooth(const uint32_t* const prev_row,
65
                    const uint32_t* const curr_row,
66
0
                    const uint32_t* const next_row, int ix, int limit) {
67
  // Check that all pixels in 4-connected neighborhood are smooth.
68
0
  return (IsNear(curr_row[ix], curr_row[ix - 1], limit) &&
69
0
          IsNear(curr_row[ix], curr_row[ix + 1], limit) &&
70
0
          IsNear(curr_row[ix], prev_row[ix], limit) &&
71
0
          IsNear(curr_row[ix], next_row[ix], limit));
72
0
}
73
74
// Adjusts pixel values of image with given maximum error.
75
static void NearLossless(int xsize, int ysize, const uint32_t* argb_src,
76
                         int stride, int limit_bits, uint32_t* copy_buffer,
77
0
                         uint32_t* argb_dst) {
78
0
  int x, y;
79
0
  const int limit = 1 << limit_bits;
80
0
  uint32_t* prev_row = copy_buffer;
81
0
  uint32_t* curr_row = prev_row + xsize;
82
0
  uint32_t* next_row = curr_row + xsize;
83
0
  memcpy(curr_row, argb_src, xsize * sizeof(argb_src[0]));
84
0
  memcpy(next_row, argb_src + stride, xsize * sizeof(argb_src[0]));
85
86
0
  for (y = 0; y < ysize; ++y, argb_src += stride, argb_dst += xsize) {
87
0
    if (y == 0 || y == ysize - 1) {
88
0
      memcpy(argb_dst, argb_src, xsize * sizeof(argb_src[0]));
89
0
    } else {
90
0
      memcpy(next_row, argb_src + stride, xsize * sizeof(argb_src[0]));
91
0
      argb_dst[0] = argb_src[0];
92
0
      argb_dst[xsize - 1] = argb_src[xsize - 1];
93
0
      for (x = 1; x < xsize - 1; ++x) {
94
0
        if (IsSmooth(prev_row, curr_row, next_row, x, limit)) {
95
0
          argb_dst[x] = curr_row[x];
96
0
        } else {
97
0
          argb_dst[x] = ClosestDiscretizedArgb(curr_row[x], limit_bits);
98
0
        }
99
0
      }
100
0
    }
101
0
    {
102
      // Three-way swap.
103
0
      uint32_t* const temp = prev_row;
104
0
      prev_row = curr_row;
105
0
      curr_row = next_row;
106
0
      next_row = temp;
107
0
    }
108
0
  }
109
0
}
110
111
int VP8ApplyNearLossless(const WebPPicture* const picture, int quality,
112
0
                         uint32_t* const argb_dst) {
113
0
  int i;
114
0
  uint32_t* copy_buffer;
115
0
  const int xsize = picture->width;
116
0
  const int ysize = picture->height;
117
0
  const int stride = picture->argb_stride;
118
0
  const int limit_bits = VP8LNearLosslessBits(quality);
119
0
  assert(argb_dst != NULL);
120
0
  assert(limit_bits > 0);
121
0
  assert(limit_bits <= MAX_LIMIT_BITS);
122
123
  // For small icon images, don't attempt to apply near-lossless compression.
124
0
  if ((xsize < MIN_DIM_FOR_NEAR_LOSSLESS &&
125
0
       ysize < MIN_DIM_FOR_NEAR_LOSSLESS) ||
126
0
      ysize < 3) {
127
0
    for (i = 0; i < ysize; ++i) {
128
0
      memcpy(argb_dst + i * xsize, picture->argb + i * picture->argb_stride,
129
0
             xsize * sizeof(*argb_dst));
130
0
    }
131
0
    return 1;
132
0
  }
133
134
0
  copy_buffer = (uint32_t*)WebPSafeMalloc(xsize * 3, sizeof(*copy_buffer));
135
0
  if (copy_buffer == NULL) {
136
0
    return 0;
137
0
  }
138
139
0
  NearLossless(xsize, ysize, picture->argb, stride, limit_bits, copy_buffer,
140
0
               argb_dst);
141
0
  for (i = limit_bits - 1; i != 0; --i) {
142
0
    NearLossless(xsize, ysize, argb_dst, xsize, i, copy_buffer, argb_dst);
143
0
  }
144
0
  WebPSafeFree(copy_buffer);
145
0
  return 1;
146
0
}
147
#else  // (WEBP_NEAR_LOSSLESS == 1)
148
149
// Define a stub to suppress compiler warnings.
150
extern void VP8LNearLosslessStub(void);
151
void VP8LNearLosslessStub(void) {}
152
153
#endif  // (WEBP_NEAR_LOSSLESS == 1)