Leptonica  1.83.1
Image processing and image analysis suite
pixacc.c
Go to the documentation of this file.
1 /*====================================================================*
2  - Copyright (C) 2001 Leptonica. 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  - 1. Redistributions of source code must retain the above copyright
8  - notice, this list of conditions and the following disclaimer.
9  - 2. Redistributions in binary form must reproduce the above
10  - copyright notice, this list of conditions and the following
11  - disclaimer in the documentation and/or other materials
12  - provided with the distribution.
13  -
14  - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18  - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *====================================================================*/
26 
64 #ifdef HAVE_CONFIG_H
65 #include <config_auto.h>
66 #endif /* HAVE_CONFIG_H */
67 
68 #include "allheaders.h"
69 #include "pix_internal.h"
70 
71 /*---------------------------------------------------------------------*
72  * Pixacc creation, destruction *
73  *---------------------------------------------------------------------*/
92 PIXACC *
93 pixaccCreate(l_int32 w,
94  l_int32 h,
95  l_int32 negflag)
96 {
97 PIXACC *pixacc;
98 
99  pixacc = (PIXACC *)LEPT_CALLOC(1, sizeof(PIXACC));
100  pixacc->w = w;
101  pixacc->h = h;
102 
103  if ((pixacc->pix = pixCreate(w, h, 32)) == NULL) {
104  pixaccDestroy(&pixacc);
105  return (PIXACC *)ERROR_PTR("pix not made", __func__, NULL);
106  }
107 
108  if (negflag) {
109  pixacc->offset = 0x40000000;
110  pixSetAllArbitrary(pixacc->pix, pixacc->offset);
111  }
112 
113  return pixacc;
114 }
115 
116 
130 PIXACC *
132  l_int32 negflag)
133 {
134 l_int32 w, h;
135 PIXACC *pixacc;
136 
137  if (!pix)
138  return (PIXACC *)ERROR_PTR("pix not defined", __func__, NULL);
139 
140  pixGetDimensions(pix, &w, &h, NULL);
141  pixacc = pixaccCreate(w, h, negflag);
142  pixaccAdd(pixacc, pix);
143  return pixacc;
144 }
145 
146 
158 void
160 {
161 PIXACC *pixacc;
162 
163  if (ppixacc == NULL) {
164  L_WARNING("ptr address is NULL!", __func__);
165  return;
166  }
167 
168  if ((pixacc = *ppixacc) == NULL)
169  return;
170 
171  pixDestroy(&pixacc->pix);
172  LEPT_FREE(pixacc);
173  *ppixacc = NULL;
174 }
175 
176 
177 /*---------------------------------------------------------------------*
178  * Pixacc finalization *
179  *---------------------------------------------------------------------*/
187 PIX *
189  l_int32 outdepth)
190 {
191  if (!pixacc)
192  return (PIX *)ERROR_PTR("pixacc not defined", __func__, NULL);
193 
194  return pixFinalAccumulate(pixaccGetPix(pixacc), pixaccGetOffset(pixacc),
195  outdepth);
196 }
197 
198 
199 /*---------------------------------------------------------------------*
200  * Pixacc accessors *
201  *---------------------------------------------------------------------*/
208 PIX *
210 {
211  if (!pixacc)
212  return (PIX *)ERROR_PTR("pixacc not defined", __func__, NULL);
213  return pixacc->pix;
214 }
215 
216 
223 l_int32
225 {
226  if (!pixacc)
227  return ERROR_INT("pixacc not defined", __func__, -1);
228  return pixacc->offset;
229 }
230 
231 
232 /*---------------------------------------------------------------------*
233  * Pixacc accumulators *
234  *---------------------------------------------------------------------*/
242 l_ok
244  PIX *pix)
245 {
246  if (!pixacc)
247  return ERROR_INT("pixacc not defined", __func__, 1);
248  if (!pix)
249  return ERROR_INT("pix not defined", __func__, 1);
250  pixAccumulate(pixaccGetPix(pixacc), pix, L_ARITH_ADD);
251  return 0;
252 }
253 
254 
262 l_ok
264  PIX *pix)
265 {
266  if (!pixacc)
267  return ERROR_INT("pixacc not defined", __func__, 1);
268  if (!pix)
269  return ERROR_INT("pix not defined", __func__, 1);
270  pixAccumulate(pixaccGetPix(pixacc), pix, L_ARITH_SUBTRACT);
271  return 0;
272 }
273 
274 
282 l_ok
284  l_float32 factor)
285 {
286  if (!pixacc)
287  return ERROR_INT("pixacc not defined", __func__, 1);
288  pixMultConstAccumulate(pixaccGetPix(pixacc), factor,
289  pixaccGetOffset(pixacc));
290  return 0;
291 }
292 
293 
308 l_ok
310  PIX *pix,
311  l_float32 factor)
312 {
313 l_int32 w, h, d, negflag;
314 PIX *pixt;
315 PIXACC *pacct;
316 
317  if (!pixacc)
318  return ERROR_INT("pixacc not defined", __func__, 1);
319  if (!pix)
320  return ERROR_INT("pix not defined", __func__, 1);
321 
322  if (factor == 0.0) return 0;
323 
324  pixGetDimensions(pix, &w, &h, &d);
325  negflag = (factor > 0.0) ? 0 : 1;
326  pacct = pixaccCreate(w, h, negflag);
327  pixaccAdd(pacct, pix);
328  pixaccMultConst(pacct, factor);
329  pixt = pixaccFinal(pacct, d);
330  pixaccAdd(pixacc, pixt);
331 
332  pixaccDestroy(&pacct);
333  pixDestroy(&pixt);
334  return 0;
335 }
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:608
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
Definition: pix1.c:1074
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
Definition: pix1.c:315
l_ok pixSetAllArbitrary(PIX *pix, l_uint32 val)
pixSetAllArbitrary()
Definition: pix2.c:929
PIXACC * pixaccCreateFromPix(PIX *pix, l_int32 negflag)
pixaccCreateFromPix()
Definition: pixacc.c:131
PIX * pixaccFinal(PIXACC *pixacc, l_int32 outdepth)
pixaccFinal()
Definition: pixacc.c:188
PIX * pixaccGetPix(PIXACC *pixacc)
pixaccGetPix()
Definition: pixacc.c:209
l_ok pixaccSubtract(PIXACC *pixacc, PIX *pix)
pixaccSubtract()
Definition: pixacc.c:263
l_ok pixaccAdd(PIXACC *pixacc, PIX *pix)
pixaccAdd()
Definition: pixacc.c:243
l_ok pixaccMultConstAccumulate(PIXACC *pixacc, PIX *pix, l_float32 factor)
pixaccMultConstAccumulate()
Definition: pixacc.c:309
l_int32 pixaccGetOffset(PIXACC *pixacc)
pixaccGetOffset()
Definition: pixacc.c:224
void pixaccDestroy(PIXACC **ppixacc)
pixaccDestroy()
Definition: pixacc.c:159
PIXACC * pixaccCreate(l_int32 w, l_int32 h, l_int32 negflag)
pixaccCreate()
Definition: pixacc.c:93
l_ok pixaccMultConst(PIXACC *pixacc, l_float32 factor)
pixaccMultConst()
Definition: pixacc.c:283
l_ok pixMultConstAccumulate(PIX *pixs, l_float32 factor, l_uint32 offset)
pixMultConstAccumulate()
Definition: pixarith.c:896
PIX * pixFinalAccumulate(PIX *pixs, l_uint32 offset, l_int32 depth)
pixFinalAccumulate()
Definition: pixarith.c:669
l_ok pixAccumulate(PIX *pixd, PIX *pixs, l_int32 op)
pixAccumulate()
Definition: pixarith.c:799
l_int32 offset
Definition: pix_internal.h:317
l_int32 h
Definition: pix_internal.h:316
l_int32 w
Definition: pix_internal.h:315
struct Pix * pix
Definition: pix_internal.h:319