Coverage Report

Created: 2025-07-18 06:57

/src/libavif/ext/aom/third_party/fastfeat/nonmax.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2006, 2008 Edward Rosten
2
// All rights reserved.
3
//
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions
6
// are met:
7
//
8
//  *Redistributions of source code must retain the above copyright
9
//   notice, this list of conditions and the following disclaimer.
10
//
11
//  *Redistributions in binary form must reproduce the above copyright
12
//   notice, this list of conditions and the following disclaimer in the
13
//   documentation and/or other materials provided with the distribution.
14
//
15
//  *Neither the name of the University of Cambridge nor the names of
16
//   its contributors may be used to endorse or promote products derived
17
//   from this software without specific prior written permission.
18
//
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
// A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER
23
// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31
// clang-format off
32
#include <assert.h>
33
#include <stdlib.h>
34
#include "fast.h"
35
36
37
1.29M
#define Compare(X, Y) ((X)>=(Y))
38
39
xy* aom_nonmax_suppression(const xy* corners, const int* scores, int num_corners,
40
                           int** ret_scores, int* ret_num_nonmax)
41
18.4k
{
42
18.4k
  int num_nonmax=0;
43
18.4k
  int last_row;
44
18.4k
  int* row_start;
45
18.4k
  int i, j;
46
18.4k
  xy* ret_nonmax;
47
18.4k
  int* nonmax_scores;
48
18.4k
  const int sz = (int)num_corners;
49
50
  /*Point above points (roughly) to the pixel above the one of interest, if there
51
    is a feature there.*/
52
18.4k
  int point_above = 0;
53
18.4k
  int point_below = 0;
54
55
18.4k
  *ret_scores = 0;
56
18.4k
  *ret_num_nonmax = -1;
57
18.4k
  if(!(corners && scores) || num_corners < 1)
58
11.0k
  {
59
11.0k
    *ret_num_nonmax = 0;
60
11.0k
    return 0;
61
11.0k
  }
62
63
7.34k
  ret_nonmax = (xy*)malloc(num_corners * sizeof(xy));
64
7.34k
  if(!ret_nonmax)
65
0
  {
66
0
    return 0;
67
0
  }
68
69
7.34k
  nonmax_scores = (int*)malloc(num_corners * sizeof(*nonmax_scores));
70
7.34k
  if (!nonmax_scores)
71
0
  {
72
0
    free(ret_nonmax);
73
0
    return 0;
74
0
  }
75
76
  /* Find where each row begins
77
     (the corners are output in raster scan order). A beginning of -1 signifies
78
     that there are no corners on that row. */
79
7.34k
  last_row = corners[num_corners-1].y;
80
7.34k
  row_start = (int*)malloc((last_row+1)*sizeof(int));
81
7.34k
  if(!row_start)
82
0
  {
83
0
    free(ret_nonmax);
84
0
    free(nonmax_scores);
85
0
    return 0;
86
0
  }
87
88
303k
  for(i=0; i < last_row+1; i++)
89
296k
    row_start[i] = -1;
90
91
7.34k
  {
92
7.34k
    int prev_row = -1;
93
1.10M
    for(i=0; i< num_corners; i++)
94
1.09M
      if(corners[i].y != prev_row)
95
229k
      {
96
229k
        row_start[corners[i].y] = i;
97
229k
        prev_row = corners[i].y;
98
229k
      }
99
7.34k
  }
100
101
102
103
1.10M
  for(i=0; i < sz; i++)
104
1.09M
  {
105
1.09M
    int score = scores[i];
106
1.09M
    xy pos = corners[i];
107
1.09M
    assert(pos.y <= last_row);
108
109
    /*Check left */
110
1.09M
    if(i > 0)
111
1.08M
      if(corners[i-1].x == pos.x-1 && corners[i-1].y == pos.y && Compare(scores[i-1], score))
112
124k
        continue;
113
114
    /*Check right*/
115
971k
    if(i < (sz - 1))
116
965k
      if(corners[i+1].x == pos.x+1 && corners[i+1].y == pos.y && Compare(scores[i+1], score))
117
104k
        continue;
118
119
    /*Check above (if there is a valid row above)*/
120
867k
    if(pos.y > 0 && row_start[pos.y - 1] != -1)
121
783k
    {
122
      /*Make sure that current point_above is one
123
        row above.*/
124
783k
      if(corners[point_above].y < pos.y - 1)
125
135k
        point_above = row_start[pos.y-1];
126
127
      /*Make point_above point to the first of the pixels above the current point,
128
        if it exists.*/
129
1.54M
      for(; corners[point_above].y < pos.y && corners[point_above].x < pos.x - 1; point_above++)
130
764k
      {}
131
132
133
1.04M
      for(j=point_above; corners[j].y < pos.y && corners[j].x <= pos.x + 1; j++)
134
475k
      {
135
475k
        int x = corners[j].x;
136
475k
        if( (x == pos.x - 1 || x ==pos.x || x == pos.x+1) && Compare(scores[j], score))
137
209k
          goto cont;
138
475k
      }
139
140
783k
    }
141
142
    /*Check below (if there is anything below)*/
143
658k
    if (pos.y + 1 < last_row+1 && row_start[pos.y + 1] != -1 && point_below < sz) /*Nothing below*/
144
591k
    {
145
591k
      if(corners[point_below].y < pos.y + 1)
146
139k
        point_below = row_start[pos.y+1];
147
148
      /* Make point below point to one of the pixels belowthe current point, if it
149
         exists.*/
150
1.30M
      for(; point_below < sz && corners[point_below].y == pos.y+1 && corners[point_below].x < pos.x - 1; point_below++)
151
709k
      {}
152
153
810k
      for(j=point_below; j < sz && corners[j].y == pos.y+1 && corners[j].x <= pos.x + 1; j++)
154
357k
      {
155
357k
        int x = corners[j].x;
156
357k
        if( (x == pos.x - 1 || x ==pos.x || x == pos.x+1) && Compare(scores[j],score))
157
139k
          goto cont;
158
357k
      }
159
591k
    }
160
161
519k
    ret_nonmax[num_nonmax] = corners[i];
162
519k
    nonmax_scores[num_nonmax] = scores[i];
163
519k
    num_nonmax++;
164
867k
cont:
165
867k
    ;
166
867k
  }
167
168
7.34k
  free(row_start);
169
7.34k
  *ret_scores = nonmax_scores;
170
7.34k
  *ret_num_nonmax = num_nonmax;
171
7.34k
  return ret_nonmax;
172
7.34k
}
173
174
// clang-format on