Leptonica  1.77.0
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 
27 
65 #include "allheaders.h"
66 
67 
68 /*---------------------------------------------------------------------*
69  * Pixacc creation, destruction *
70  *---------------------------------------------------------------------*/
89 PIXACC *
90 pixaccCreate(l_int32 w,
91  l_int32 h,
92  l_int32 negflag)
93 {
94 PIXACC *pixacc;
95 
96  PROCNAME("pixaccCreate");
97 
98  if ((pixacc = (PIXACC *)LEPT_CALLOC(1, sizeof(PIXACC))) == NULL)
99  return (PIXACC *)ERROR_PTR("pixacc not made", procName, NULL);
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", procName, 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  PROCNAME("pixaccCreateFromPix");
138 
139  if (!pix)
140  return (PIXACC *)ERROR_PTR("pix not defined", procName, NULL);
141 
142  pixGetDimensions(pix, &w, &h, NULL);
143  pixacc = pixaccCreate(w, h, negflag);
144  pixaccAdd(pixacc, pix);
145  return pixacc;
146 }
147 
148 
159 void
161 {
162 PIXACC *pixacc;
163 
164  PROCNAME("pixaccDestroy");
165 
166  if (ppixacc == NULL) {
167  L_WARNING("ptr address is NULL!", procName);
168  return;
169  }
170 
171  if ((pixacc = *ppixacc) == NULL)
172  return;
173 
174  pixDestroy(&pixacc->pix);
175  LEPT_FREE(pixacc);
176  *ppixacc = NULL;
177  return;
178 }
179 
180 
181 /*---------------------------------------------------------------------*
182  * Pixacc finalization *
183  *---------------------------------------------------------------------*/
191 PIX *
193  l_int32 outdepth)
194 {
195  PROCNAME("pixaccFinal");
196 
197  if (!pixacc)
198  return (PIX *)ERROR_PTR("pixacc not defined", procName, NULL);
199 
200  return pixFinalAccumulate(pixaccGetPix(pixacc), pixaccGetOffset(pixacc),
201  outdepth);
202 }
203 
204 
205 /*---------------------------------------------------------------------*
206  * Pixacc accessors *
207  *---------------------------------------------------------------------*/
214 PIX *
216 {
217  PROCNAME("pixaccGetPix");
218 
219  if (!pixacc)
220  return (PIX *)ERROR_PTR("pixacc not defined", procName, NULL);
221  return pixacc->pix;
222 }
223 
224 
231 l_int32
233 {
234  PROCNAME("pixaccGetOffset");
235 
236  if (!pixacc)
237  return ERROR_INT("pixacc not defined", procName, -1);
238  return pixacc->offset;
239 }
240 
241 
242 /*---------------------------------------------------------------------*
243  * Pixacc accumulators *
244  *---------------------------------------------------------------------*/
252 l_ok
254  PIX *pix)
255 {
256  PROCNAME("pixaccAdd");
257 
258  if (!pixacc)
259  return ERROR_INT("pixacc not defined", procName, 1);
260  if (!pix)
261  return ERROR_INT("pix not defined", procName, 1);
262  pixAccumulate(pixaccGetPix(pixacc), pix, L_ARITH_ADD);
263  return 0;
264 }
265 
266 
274 l_ok
276  PIX *pix)
277 {
278  PROCNAME("pixaccSubtract");
279 
280  if (!pixacc)
281  return ERROR_INT("pixacc not defined", procName, 1);
282  if (!pix)
283  return ERROR_INT("pix not defined", procName, 1);
284  pixAccumulate(pixaccGetPix(pixacc), pix, L_ARITH_SUBTRACT);
285  return 0;
286 }
287 
288 
296 l_ok
298  l_float32 factor)
299 {
300  PROCNAME("pixaccMultConst");
301 
302  if (!pixacc)
303  return ERROR_INT("pixacc not defined", procName, 1);
304  pixMultConstAccumulate(pixaccGetPix(pixacc), factor,
305  pixaccGetOffset(pixacc));
306  return 0;
307 }
308 
309 
324 l_ok
326  PIX *pix,
327  l_float32 factor)
328 {
329 l_int32 w, h, d, negflag;
330 PIX *pixt;
331 PIXACC *pacct;
332 
333  PROCNAME("pixaccMultConstAccumulate");
334 
335  if (!pixacc)
336  return ERROR_INT("pixacc not defined", procName, 1);
337  if (!pix)
338  return ERROR_INT("pix not defined", procName, 1);
339 
340  if (factor == 0.0) return 0;
341 
342  pixGetDimensions(pix, &w, &h, &d);
343  negflag = (factor > 0.0) ? 0 : 1;
344  pacct = pixaccCreate(w, h, negflag);
345  pixaccAdd(pacct, pix);
346  pixaccMultConst(pacct, factor);
347  pixt = pixaccFinal(pacct, d);
348  pixaccAdd(pixacc, pixt);
349 
350  pixaccDestroy(&pacct);
351  pixDestroy(&pixt);
352  return 0;
353 }
PIXACC * pixaccCreateFromPix(PIX *pix, l_int32 negflag)
pixaccCreateFromPix()
Definition: pixacc.c:131
l_int32 w
Definition: pix.h:548
l_ok pixMultConstAccumulate(PIX *pixs, l_float32 factor, l_uint32 offset)
pixMultConstAccumulate()
Definition: pixarith.c:818
l_int32 h
Definition: pix.h:549
PIX * pixaccGetPix(PIXACC *pixacc)
pixaccGetPix()
Definition: pixacc.c:215
l_ok pixaccMultConstAccumulate(PIXACC *pixacc, PIX *pix, l_float32 factor)
pixaccMultConstAccumulate()
Definition: pixacc.c:325
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
Definition: pix1.c:302
l_ok pixaccMultConst(PIXACC *pixacc, l_float32 factor)
pixaccMultConst()
Definition: pixacc.c:297
l_ok pixSetAllArbitrary(PIX *pix, l_uint32 val)
pixSetAllArbitrary()
Definition: pix2.c:876
void pixaccDestroy(PIXACC **ppixacc)
pixaccDestroy()
Definition: pixacc.c:160
l_int32 offset
Definition: pix.h:550
l_ok pixAccumulate(PIX *pixd, PIX *pixs, l_int32 op)
pixAccumulate()
Definition: pixarith.c:719
Definition: pix.h:546
PIX * pixFinalAccumulate(PIX *pixs, l_uint32 offset, l_int32 depth)
pixFinalAccumulate()
Definition: pixarith.c:585
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:543
struct Pix * pix
Definition: pix.h:552
PIX * pixaccFinal(PIXACC *pixacc, l_int32 outdepth)
pixaccFinal()
Definition: pixacc.c:192
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
Definition: pix1.c:1065
PIXACC * pixaccCreate(l_int32 w, l_int32 h, l_int32 negflag)
pixaccCreate()
Definition: pixacc.c:90
l_int32 pixaccGetOffset(PIXACC *pixacc)
pixaccGetOffset()
Definition: pixacc.c:232
Definition: pix.h:134
l_ok pixaccSubtract(PIXACC *pixacc, PIX *pix)
pixaccSubtract()
Definition: pixacc.c:275
l_ok pixaccAdd(PIXACC *pixacc, PIX *pix)
pixaccAdd()
Definition: pixacc.c:253