8
8
* - small celanups
9
9
* 17/8/13
10
10
* - redo as a class
11
+ * 28/1/22 travisbell
12
+ * - better arg checking
11
13
*/
12
14
13
15
/*
@@ -57,8 +59,6 @@ struct _VipsHistFindNDim;
57
59
typedef struct {
58
60
struct _VipsHistFindNDim * ndim ;
59
61
60
- int bins ;
61
- int max_val ;
62
62
unsigned int * * * data ;
63
63
} Histogram ;
64
64
@@ -69,6 +69,10 @@ typedef struct _VipsHistFindNDim {
69
69
*/
70
70
int bins ;
71
71
72
+ /* Max pixel value for this format.
73
+ */
74
+ int max_val ;
75
+
72
76
/* Main image histogram. Subhists accumulate to this.
73
77
*/
74
78
Histogram * hist ;
@@ -89,7 +93,6 @@ static Histogram *
89
93
histogram_new ( VipsHistFindNDim * ndim )
90
94
{
91
95
VipsImage * in = VIPS_STATISTIC ( ndim )-> ready ;
92
- VipsObjectClass * class = VIPS_OBJECT_GET_CLASS ( ndim );
93
96
int bins = ndim -> bins ;
94
97
95
98
/* How many dimensions do we need to allocate?
@@ -104,14 +107,6 @@ histogram_new( VipsHistFindNDim *ndim )
104
107
return ( NULL );
105
108
106
109
hist -> ndim = ndim ;
107
- hist -> bins = bins ;
108
- hist -> max_val = in -> BandFmt == VIPS_FORMAT_UCHAR ? 256 : 65536 ;
109
- if ( bins < 1 ||
110
- bins > hist -> max_val ) {
111
- vips_error ( class -> nickname ,
112
- _ ( "bins out of range [1,%d]" ), hist -> max_val );
113
- return ( NULL );
114
- }
115
110
116
111
if ( !(hist -> data = VIPS_ARRAY ( ndim , bins , unsigned int * * )) )
117
112
return ( NULL );
@@ -147,6 +142,25 @@ vips_hist_find_ndim_build( VipsObject *object )
147
142
"out" , vips_image_new (),
148
143
NULL );
149
144
145
+ if ( statistic -> in ) {
146
+ VipsObjectClass * class = VIPS_OBJECT_GET_CLASS ( ndim );
147
+
148
+ if ( statistic -> in -> Bands > 3 ) {
149
+ vips_error ( class -> nickname ,
150
+ "%s" , _ ( "image is not 1 - 3 bands" ) );
151
+ return ( -1 );
152
+ }
153
+
154
+ ndim -> max_val =
155
+ statistic -> in -> BandFmt == VIPS_FORMAT_UCHAR ? 256 : 65536 ;
156
+ if ( ndim -> bins < 1 ||
157
+ ndim -> bins > ndim -> max_val ) {
158
+ vips_error ( class -> nickname ,
159
+ _ ( "bins out of range [1,%d]" ), ndim -> max_val );
160
+ return ( -1 );
161
+ }
162
+ }
163
+
150
164
/* main hist made on first thread start.
151
165
*/
152
166
@@ -204,9 +218,9 @@ vips_hist_find_ndim_stop( VipsStatistic *statistic, void *seq )
204
218
205
219
int i , j , k ;
206
220
207
- for ( i = 0 ; i < hist -> bins ; i ++ )
208
- for ( j = 0 ; j < hist -> bins ; j ++ )
209
- for ( k = 0 ; k < hist -> bins ; k ++ )
221
+ for ( i = 0 ; i < ndim -> bins ; i ++ )
222
+ for ( j = 0 ; j < ndim -> bins ; j ++ )
223
+ for ( k = 0 ; k < ndim -> bins ; k ++ )
210
224
if ( hist -> data [i ] && hist -> data [i ][j ] ) {
211
225
hist -> data [i ][j ][k ] +=
212
226
sub_hist -> data [i ][j ][k ];
@@ -236,9 +250,10 @@ vips_hist_find_ndim_scan( VipsStatistic *statistic, void *seq,
236
250
int x , int y , void * in , int n )
237
251
{
238
252
Histogram * hist = (Histogram * ) seq ;
253
+ VipsHistFindNDim * ndim = (VipsHistFindNDim * ) statistic ;
239
254
VipsImage * im = statistic -> ready ;
240
255
int nb = im -> Bands ;
241
- double scale = (double ) (hist -> max_val + 1 ) / hist -> bins ;
256
+ double scale = (double ) (ndim -> max_val + 1 ) / ndim -> bins ;
242
257
int i , j , k ;
243
258
int index [3 ];
244
259
0 commit comments