Coverage Report

Created: 2025-10-13 06:21

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
23.9k
#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
30.6M
static uint32_t FindClosestDiscretized(uint32_t a, int bits) {
35
30.6M
  const uint32_t mask = (1u << bits) - 1;
36
30.6M
  const uint32_t biased = a + (mask >> 1) + ((a >> bits) & 1);
37
30.6M
  assert(bits > 0);
38
30.6M
  if (biased > 0xff) return 0xff;
39
24.2M
  return biased & ~mask;
40
30.6M
}
41
42
// Applies FindClosestDiscretized to all channels of pixel.
43
7.67M
static uint32_t ClosestDiscretizedArgb(uint32_t a, int bits) {
44
7.67M
  return (FindClosestDiscretized(a >> 24, bits) << 24) |
45
7.67M
         (FindClosestDiscretized((a >> 16) & 0xff, bits) << 16) |
46
7.67M
         (FindClosestDiscretized((a >> 8) & 0xff, bits) << 8) |
47
7.67M
         (FindClosestDiscretized(a & 0xff, bits));
48
7.67M
}
49
50
// Checks if distance between corresponding channel values of pixels a and b
51
// is within the given limit.
52
24.5M
static int IsNear(uint32_t a, uint32_t b, int limit) {
53
24.5M
  int k;
54
95.6M
  for (k = 0; k < 4; ++k) {
55
78.6M
    const int delta =
56
78.6M
        (int)((a >> (k * 8)) & 0xff) - (int)((b >> (k * 8)) & 0xff);
57
78.6M
    if (delta >= limit || delta <= -limit) {
58
7.67M
      return 0;
59
7.67M
    }
60
78.6M
  }
61
16.9M
  return 1;
62
24.5M
}
63
64
static int IsSmooth(const uint32_t* const prev_row,
65
                    const uint32_t* const curr_row,
66
11.4M
                    const uint32_t* const next_row, int ix, int limit) {
67
  // Check that all pixels in 4-connected neighborhood are smooth.
68
11.4M
  return (IsNear(curr_row[ix], curr_row[ix - 1], limit) &&
69
5.03M
          IsNear(curr_row[ix], curr_row[ix + 1], limit) &&
70
4.41M
          IsNear(curr_row[ix], prev_row[ix], limit) &&
71
3.95M
          IsNear(curr_row[ix], next_row[ix], limit));
72
11.4M
}
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
2.96k
                         uint32_t* argb_dst) {
78
2.96k
  int x, y;
79
2.96k
  const int limit = 1 << limit_bits;
80
2.96k
  uint32_t* prev_row = copy_buffer;
81
2.96k
  uint32_t* curr_row = prev_row + xsize;
82
2.96k
  uint32_t* next_row = curr_row + xsize;
83
2.96k
  memcpy(curr_row, argb_src, xsize * sizeof(argb_src[0]));
84
2.96k
  memcpy(next_row, argb_src + stride, xsize * sizeof(argb_src[0]));
85
86
309k
  for (y = 0; y < ysize; ++y, argb_src += stride, argb_dst += xsize) {
87
306k
    if (y == 0 || y == ysize - 1) {
88
5.91k
      memcpy(argb_dst, argb_src, xsize * sizeof(argb_src[0]));
89
300k
    } else {
90
300k
      memcpy(next_row, argb_src + stride, xsize * sizeof(argb_src[0]));
91
300k
      argb_dst[0] = argb_src[0];
92
300k
      argb_dst[xsize - 1] = argb_src[xsize - 1];
93
11.7M
      for (x = 1; x < xsize - 1; ++x) {
94
11.4M
        if (IsSmooth(prev_row, curr_row, next_row, x, limit)) {
95
3.86M
          argb_dst[x] = curr_row[x];
96
7.60M
        } else {
97
7.60M
          argb_dst[x] = ClosestDiscretizedArgb(curr_row[x], limit_bits);
98
7.60M
        }
99
11.4M
      }
100
300k
    }
101
306k
    {
102
      // Three-way swap.
103
306k
      uint32_t* const temp = prev_row;
104
306k
      prev_row = curr_row;
105
306k
      curr_row = next_row;
106
306k
      next_row = temp;
107
306k
    }
108
306k
  }
109
2.96k
}
110
111
int VP8ApplyNearLossless(const WebPPicture* const picture, int quality,
112
8.18k
                         uint32_t* const argb_dst) {
113
8.18k
  int i;
114
8.18k
  const int xsize = picture->width;
115
8.18k
  const int ysize = picture->height;
116
8.18k
  const int stride = picture->argb_stride;
117
8.18k
  uint32_t* const copy_buffer =
118
8.18k
      (uint32_t*)WebPSafeMalloc(xsize * 3, sizeof(*copy_buffer));
119
8.18k
  const int limit_bits = VP8LNearLosslessBits(quality);
120
8.18k
  assert(argb_dst != NULL);
121
8.18k
  assert(limit_bits > 0);
122
8.18k
  assert(limit_bits <= MAX_LIMIT_BITS);
123
8.18k
  if (copy_buffer == NULL) {
124
0
    return 0;
125
0
  }
126
  // For small icon images, don't attempt to apply near-lossless compression.
127
8.18k
  if ((xsize < MIN_DIM_FOR_NEAR_LOSSLESS &&
128
7.57k
       ysize < MIN_DIM_FOR_NEAR_LOSSLESS) ||
129
7.29k
      ysize < 3) {
130
106k
    for (i = 0; i < ysize; ++i) {
131
98.9k
      memcpy(argb_dst + i * xsize, picture->argb + i * picture->argb_stride,
132
98.9k
             xsize * sizeof(*argb_dst));
133
98.9k
    }
134
7.29k
    WebPSafeFree(copy_buffer);
135
7.29k
    return 1;
136
7.29k
  }
137
138
886
  NearLossless(xsize, ysize, picture->argb, stride, limit_bits, copy_buffer,
139
886
               argb_dst);
140
2.96k
  for (i = limit_bits - 1; i != 0; --i) {
141
2.07k
    NearLossless(xsize, ysize, argb_dst, xsize, i, copy_buffer, argb_dst);
142
2.07k
  }
143
886
  WebPSafeFree(copy_buffer);
144
886
  return 1;
145
8.18k
}
146
#else  // (WEBP_NEAR_LOSSLESS == 1)
147
148
// Define a stub to suppress compiler warnings.
149
extern void VP8LNearLosslessStub(void);
150
void VP8LNearLosslessStub(void) {}
151
152
#endif  // (WEBP_NEAR_LOSSLESS == 1)