1 /*====================================================================*
2 - Copyright (C) 2001 Leptonica. All rights reserved.
3 - This software is distributed in the hope that it will be
4 - useful, but with NO WARRANTY OF ANY KIND.
5 - No author or distributor accepts responsibility to anyone for the
6 - consequences of using this software, or for whether it serves any
7 - particular purpose or works at all, unless he or she says so in
8 - writing. Everyone is granted permission to copy, modify and
9 - redistribute this source code, for commercial or non-commercial
10 - purposes, with the following restrictions: (1) the origin of this
11 - source code must not be misrepresented; (2) modified versions must
12 - be plainly marked as such; and (3) this notice may not be removed
13 - or altered from any source or modified source distribution.
14 *====================================================================*/
15
16
17 /*
18 * pixacc.c
19 *
20 * Pixacc creation, destruction
21 * PIXACC *pixaccCreate()
22 * void pixaccDestroy()
23 *
24 * Pixacc finalization
25 * PIX *pixaccFinal()
26 *
27 * Pixacc accessors
28 * PIX *pixaccGetPix()
29 * l_int32 pixaccGetOffset()
30 *
31 * Pixacc accumulators
32 * l_int32 pixaccAdd()
33 * l_int32 pixaccSubtract()
34 * l_int32 pixaccMultConst()
35 *
36 * This is a simple interface to some of the pixel arithmetic operations
37 * in pixarith.c.
38 */
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include "allheaders.h"
43
44
45 /*---------------------------------------------------------------------*
46 * Pixacc creation, destruction *
47 *---------------------------------------------------------------------*/
48 /*!
49 * pixaccCreate()
50 *
51 * Input: w, h (of 32 bpp Pix)
52 * subflag (0 if only addition; 1 if subtraction ops as well
53 * Return: pixacc, or null on error
54 *
55 * Notes:
56 * (1) Includes the initialization in pixInitAccumulate().
57 */
58 PIXACC *
pixaccCreate(l_int32 w,l_int32 h,l_int32 subflag)59 pixaccCreate(l_int32 w,
60 l_int32 h,
61 l_int32 subflag)
62 {
63 PIXACC *pixacc;
64
65 PROCNAME("pixaccCreate");
66
67 if ((pixacc = (PIXACC *)CALLOC(1, sizeof(PIXACC))) == NULL)
68 return (PIXACC *)ERROR_PTR("pixacc not made", procName, NULL);
69 pixacc->w = w;
70 pixacc->h = h;
71
72 if ((pixacc->pix = pixCreate(w, h, 32)) == NULL)
73 return (PIXACC *)ERROR_PTR("pix not made", procName, NULL);
74
75 if (subflag) {
76 pixacc->offset = 0x40000000;
77 pixSetAllArbitrary(pixacc->pix, pixacc->offset);
78 }
79
80 return pixacc;
81 }
82
83
84 /*!
85 * pixaccDestroy()
86 *
87 * Input: &pixacc (<can be null>)
88 * Return: void
89 *
90 * Notes:
91 * (1) Always nulls the input ptr.
92 */
93 void
pixaccDestroy(PIXACC ** ppixacc)94 pixaccDestroy(PIXACC **ppixacc)
95 {
96 PIXACC *pixacc;
97
98 PROCNAME("pixaccDestroy");
99
100 if (ppixacc == NULL) {
101 L_WARNING("ptr address is NULL!", procName);
102 return;
103 }
104
105 if ((pixacc = *ppixacc) == NULL)
106 return;
107
108 pixDestroy(&pixacc->pix);
109 FREE(pixacc);
110 *ppixacc = NULL;
111 return;
112 }
113
114
115 /*---------------------------------------------------------------------*
116 * Pixacc finalization *
117 *---------------------------------------------------------------------*/
118 /*!
119 * pixaccFinal()
120 *
121 * Input: pixacc
122 * outdepth (8, 16 or 32 bpp)
123 * Return: pixd (8 , 16 or 32 bpp), or null on error
124 */
125 PIX *
pixaccFinal(PIXACC * pixacc,l_int32 outdepth)126 pixaccFinal(PIXACC *pixacc,
127 l_int32 outdepth)
128 {
129 PROCNAME("pixaccFinal");
130
131 if (!pixacc)
132 return (PIX *)ERROR_PTR("pixacc not defined", procName, NULL);
133
134 return pixFinalAccumulate(pixaccGetPix(pixacc), pixaccGetOffset(pixacc),
135 outdepth);
136 }
137
138
139 /*---------------------------------------------------------------------*
140 * Pixacc accessors *
141 *---------------------------------------------------------------------*/
142 /*!
143 * pixaccGetPix()
144 *
145 * Input: pixacc
146 * Return: pix, or null on error
147 */
148 PIX *
pixaccGetPix(PIXACC * pixacc)149 pixaccGetPix(PIXACC *pixacc)
150 {
151 PROCNAME("pixaccGetPix");
152
153 if (!pixacc)
154 return (PIX *)ERROR_PTR("pixacc not defined", procName, NULL);
155 return pixacc->pix;
156 }
157
158
159 /*!
160 * pixaccGetOffset()
161 *
162 * Input: pixacc
163 * Return: offset, or -1 on error
164 */
165 l_int32
pixaccGetOffset(PIXACC * pixacc)166 pixaccGetOffset(PIXACC *pixacc)
167 {
168 PROCNAME("pixaccGetOffset");
169
170 if (!pixacc)
171 return ERROR_INT("pixacc not defined", procName, -1);
172 return pixacc->offset;
173 }
174
175
176 /*---------------------------------------------------------------------*
177 * Pixacc accumulators *
178 *---------------------------------------------------------------------*/
179 /*!
180 * pixaccAdd()
181 *
182 * Input: pixacc
183 * pix (to be added)
184 * Return: 0 if OK, 1 on error
185 */
186 l_int32
pixaccAdd(PIXACC * pixacc,PIX * pix)187 pixaccAdd(PIXACC *pixacc,
188 PIX *pix)
189 {
190 PROCNAME("pixaccAdd");
191
192 if (!pixacc)
193 return ERROR_INT("pixacc not defined", procName, 1);
194 if (!pix)
195 return ERROR_INT("pix not defined", procName, 1);
196 pixAccumulate(pixaccGetPix(pixacc), pix, L_ARITH_ADD);
197 return 0;
198 }
199
200
201 /*!
202 * pixaccSubtract()
203 *
204 * Input: pixacc
205 * pix (to be subtracted)
206 * Return: 0 if OK, 1 on error
207 */
208 l_int32
pixaccSubtract(PIXACC * pixacc,PIX * pix)209 pixaccSubtract(PIXACC *pixacc,
210 PIX *pix)
211 {
212 PROCNAME("pixaccSubtract");
213
214 if (!pixacc)
215 return ERROR_INT("pixacc not defined", procName, 1);
216 if (!pix)
217 return ERROR_INT("pix not defined", procName, 1);
218 pixAccumulate(pixaccGetPix(pixacc), pix, L_ARITH_SUBTRACT);
219 return 0;
220 }
221
222
223 /*!
224 * pixaccMultConst()
225 *
226 * Input: pixacc
227 * factor
228 * Return: 0 if OK, 1 on error
229 */
230 l_int32
pixaccMultConst(PIXACC * pixacc,l_float32 factor)231 pixaccMultConst(PIXACC *pixacc,
232 l_float32 factor)
233 {
234 PROCNAME("pixaccMultConst");
235
236 if (!pixacc)
237 return ERROR_INT("pixacc not defined", procName, 1);
238 pixMultConstAccumulate(pixaccGetPix(pixacc), factor,
239 pixaccGetOffset(pixacc));
240 return 0;
241 }
242
243