Leptonica  1.77.0
Image processing and image analysis suite
rop.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 
46 #include <string.h>
47 #include "allheaders.h"
48 
49 /*--------------------------------------------------------------------*
50  * General rasterop (basic pix interface) *
51  *--------------------------------------------------------------------*/
192 l_ok
194  l_int32 dx,
195  l_int32 dy,
196  l_int32 dw,
197  l_int32 dh,
198  l_int32 op,
199  PIX *pixs,
200  l_int32 sx,
201  l_int32 sy)
202 {
203 l_int32 dd;
204 
205  PROCNAME("pixRasterop");
206 
207  if (!pixd)
208  return ERROR_INT("pixd not defined", procName, 1);
209 
210  if (op == PIX_DST) /* no-op */
211  return 0;
212 
213  /* Check if operation is only on dest */
214  dd = pixGetDepth(pixd);
215  if (op == PIX_CLR || op == PIX_SET || op == PIX_NOT(PIX_DST)) {
217  pixGetWidth(pixd), pixGetHeight(pixd), dd,
218  pixGetWpl(pixd),
219  dx, dy, dw, dh,
220  op);
221  return 0;
222  }
223 
224  if (!pixs)
225  return ERROR_INT("pixs not defined", procName, 1);
226 
227  /* Check depth of src and dest; these must agree */
228  if (dd != pixGetDepth(pixs))
229  return ERROR_INT("depths of pixs and pixd differ", procName, 1);
230 
231  rasteropLow(pixGetData(pixd),
232  pixGetWidth(pixd), pixGetHeight(pixd), dd,
233  pixGetWpl(pixd),
234  dx, dy, dw, dh,
235  op,
236  pixGetData(pixs),
237  pixGetWidth(pixs), pixGetHeight(pixs),
238  pixGetWpl(pixs),
239  sx, sy);
240 
241  return 0;
242 }
243 
244 
245 /*--------------------------------------------------------------------*
246  * In-place full band translation *
247  *--------------------------------------------------------------------*/
268 l_ok
270  l_int32 bx,
271  l_int32 bw,
272  l_int32 vshift,
273  l_int32 incolor)
274 {
275 l_int32 w, h, d, index, op;
276 PIX *pixt;
277 PIXCMAP *cmap;
278 
279  PROCNAME("pixRasteropVip");
280 
281  if (!pixd)
282  return ERROR_INT("pixd not defined", procName, 1);
283  if (incolor != L_BRING_IN_WHITE && incolor != L_BRING_IN_BLACK)
284  return ERROR_INT("invalid value for incolor", procName, 1);
285  if (bw <= 0)
286  return ERROR_INT("bw must be > 0", procName, 1);
287 
288  if (vshift == 0)
289  return 0;
290 
291  pixGetDimensions(pixd, &w, &h, &d);
292  rasteropVipLow(pixGetData(pixd), w, h, d, pixGetWpl(pixd), bx, bw, vshift);
293 
294  cmap = pixGetColormap(pixd);
295  if (!cmap) {
296  if ((d == 1 && incolor == L_BRING_IN_BLACK) ||
297  (d > 1 && incolor == L_BRING_IN_WHITE))
298  op = PIX_SET;
299  else
300  op = PIX_CLR;
301 
302  /* Set the pixels brought in at top or bottom */
303  if (vshift > 0)
304  pixRasterop(pixd, bx, 0, bw, vshift, op, NULL, 0, 0);
305  else /* vshift < 0 */
306  pixRasterop(pixd, bx, h + vshift, bw, -vshift, op, NULL, 0, 0);
307  return 0;
308  }
309 
310  /* Get the nearest index and fill with that */
311  if (incolor == L_BRING_IN_BLACK)
312  pixcmapGetRankIntensity(cmap, 0.0, &index);
313  else /* white */
314  pixcmapGetRankIntensity(cmap, 1.0, &index);
315  pixt = pixCreate(bw, L_ABS(vshift), d);
316  pixSetAllArbitrary(pixt, index);
317  if (vshift > 0)
318  pixRasterop(pixd, bx, 0, bw, vshift, PIX_SRC, pixt, 0, 0);
319  else /* vshift < 0 */
320  pixRasterop(pixd, bx, h + vshift, bw, -vshift, PIX_SRC, pixt, 0, 0);
321  pixDestroy(&pixt);
322  return 0;
323 }
324 
325 
346 l_ok
348  l_int32 by,
349  l_int32 bh,
350  l_int32 hshift,
351  l_int32 incolor)
352 {
353 l_int32 w, h, d, index, op;
354 PIX *pixt;
355 PIXCMAP *cmap;
356 
357  PROCNAME("pixRasteropHip");
358 
359  if (!pixd)
360  return ERROR_INT("pixd not defined", procName, 1);
361  if (incolor != L_BRING_IN_WHITE && incolor != L_BRING_IN_BLACK)
362  return ERROR_INT("invalid value for incolor", procName, 1);
363  if (bh <= 0)
364  return ERROR_INT("bh must be > 0", procName, 1);
365 
366  if (hshift == 0)
367  return 0;
368 
369  pixGetDimensions(pixd, &w, &h, &d);
370  rasteropHipLow(pixGetData(pixd), h, d, pixGetWpl(pixd), by, bh, hshift);
371 
372  cmap = pixGetColormap(pixd);
373  if (!cmap) {
374  if ((d == 1 && incolor == L_BRING_IN_BLACK) ||
375  (d > 1 && incolor == L_BRING_IN_WHITE))
376  op = PIX_SET;
377  else
378  op = PIX_CLR;
379 
380  /* Set the pixels brought in at left or right */
381  if (hshift > 0)
382  pixRasterop(pixd, 0, by, hshift, bh, op, NULL, 0, 0);
383  else /* hshift < 0 */
384  pixRasterop(pixd, w + hshift, by, -hshift, bh, op, NULL, 0, 0);
385  return 0;
386  }
387 
388  /* Get the nearest index and fill with that */
389  if (incolor == L_BRING_IN_BLACK)
390  pixcmapGetRankIntensity(cmap, 0.0, &index);
391  else /* white */
392  pixcmapGetRankIntensity(cmap, 1.0, &index);
393  pixt = pixCreate(L_ABS(hshift), bh, d);
394  pixSetAllArbitrary(pixt, index);
395  if (hshift > 0)
396  pixRasterop(pixd, 0, by, hshift, bh, PIX_SRC, pixt, 0, 0);
397  else /* hshift < 0 */
398  pixRasterop(pixd, w + hshift, by, -hshift, bh, PIX_SRC, pixt, 0, 0);
399  pixDestroy(&pixt);
400  return 0;
401 }
402 
403 
404 /*--------------------------------------------------------------------*
405  * Full image translation (general and in-place) *
406  *--------------------------------------------------------------------*/
430 PIX *
432  PIX *pixs,
433  l_int32 hshift,
434  l_int32 vshift,
435  l_int32 incolor)
436 {
437  PROCNAME("pixTranslate");
438 
439  if (!pixs)
440  return (PIX *)ERROR_PTR("pixs not defined", procName, NULL);
441 
442  /* Prepare pixd for in-place operation */
443  if ((pixd = pixCopy(pixd, pixs)) == NULL)
444  return (PIX *)ERROR_PTR("pixd not made", procName, NULL);
445 
446  pixRasteropIP(pixd, hshift, vshift, incolor);
447  return pixd;
448 }
449 
450 
460 l_ok
462  l_int32 hshift,
463  l_int32 vshift,
464  l_int32 incolor)
465 {
466 l_int32 w, h;
467 
468  PROCNAME("pixRasteropIP");
469 
470  if (!pixd)
471  return ERROR_INT("pixd not defined", procName, 1);
472 
473  pixGetDimensions(pixd, &w, &h, NULL);
474  pixRasteropHip(pixd, 0, h, hshift, incolor);
475  pixRasteropVip(pixd, 0, w, vshift, incolor);
476 
477  return 0;
478 }
479 
480 
481 /*--------------------------------------------------------------------*
482  * Full image rasterop with no shifts *
483  *--------------------------------------------------------------------*/
501 l_ok
503  PIX *pixs,
504  l_int32 op)
505 {
506  PROCNAME("pixRasteropFullImage");
507 
508  if (!pixd)
509  return ERROR_INT("pixd not defined", procName, 1);
510  if (!pixs)
511  return ERROR_INT("pixs not defined", procName, 1);
512 
513  pixRasterop(pixd, 0, 0, pixGetWidth(pixd), pixGetHeight(pixd), op,
514  pixs, 0, 0);
515  return 0;
516 }
#define PIX_CLR
Definition: pix.h:330
l_ok pixRasteropIP(PIX *pixd, l_int32 hshift, l_int32 vshift, l_int32 incolor)
pixRasteropIP()
Definition: rop.c:461
l_ok pixRasteropVip(PIX *pixd, l_int32 bx, l_int32 bw, l_int32 vshift, l_int32 incolor)
pixRasteropVip()
Definition: rop.c:269
l_ok pixRasterop(PIX *pixd, l_int32 dx, l_int32 dy, l_int32 dw, l_int32 dh, l_int32 op, PIX *pixs, l_int32 sx, l_int32 sy)
pixRasterop()
Definition: rop.c:193
l_ok pixRasteropFullImage(PIX *pixd, PIX *pixs, l_int32 op)
pixRasteropFullImage()
Definition: rop.c:502
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
Definition: pix1.c:302
void rasteropLow(l_uint32 *datad, l_int32 dpixw, l_int32 dpixh, l_int32 depth, l_int32 dwpl, l_int32 dx, l_int32 dy, l_int32 dw, l_int32 dh, l_int32 op, l_uint32 *datas, l_int32 spixw, l_int32 spixh, l_int32 swpl, l_int32 sx, l_int32 sy)
rasteropLow()
Definition: roplow.c:481
l_uint32 * pixGetData(PIX *pix)
pixGetData()
Definition: pix1.c:1624
l_ok pixSetAllArbitrary(PIX *pix, l_uint32 val)
pixSetAllArbitrary()
Definition: pix2.c:876
#define PIX_SET
Definition: pix.h:331
void rasteropVipLow(l_uint32 *data, l_int32 pixw, l_int32 pixh, l_int32 depth, l_int32 wpl, l_int32 x, l_int32 w, l_int32 shift)
rasteropVipLow()
Definition: roplow.c:2146
void rasteropUniLow(l_uint32 *datad, l_int32 dpixw, l_int32 dpixh, l_int32 depth, l_int32 dwpl, l_int32 dx, l_int32 dy, l_int32 dw, l_int32 dh, l_int32 op)
rasteropUniLow()
Definition: roplow.c:124
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:543
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
Definition: pix1.c:1065
void rasteropHipLow(l_uint32 *data, l_int32 pixh, l_int32 depth, l_int32 wpl, l_int32 y, l_int32 h, l_int32 shift)
rasteropHipLow()
Definition: roplow.c:2359
#define PIX_NOT(op)
Definition: pix.h:329
Definition: pix.h:134
#define PIX_SRC
Definition: pix.h:327
PIX * pixCopy(PIX *pixd, PIX *pixs)
pixCopy()
Definition: pix1.c:628
PIX * pixTranslate(PIX *pixd, PIX *pixs, l_int32 hshift, l_int32 vshift, l_int32 incolor)
pixTranslate()
Definition: rop.c:431
l_int32 index
Definition: regutils.h:123
l_ok pixRasteropHip(PIX *pixd, l_int32 by, l_int32 bh, l_int32 hshift, l_int32 incolor)
pixRasteropHip()
Definition: rop.c:347
l_ok pixcmapGetRankIntensity(PIXCMAP *cmap, l_float32 rankval, l_int32 *pindex)
pixcmapGetRankIntensity()
Definition: colormap.c:1158
#define PIX_DST
Definition: pix.h:328