Leptonica  1.83.1
Image processing and image analysis suite
colormorph.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 
41 #ifdef HAVE_CONFIG_H
42 #include <config_auto.h>
43 #endif /* HAVE_CONFIG_H */
44 
45 #include "allheaders.h"
46 
47 /*-----------------------------------------------------------------*
48  * Top-level color morphological operations *
49  *-----------------------------------------------------------------*/
68 PIX *
70  l_int32 type,
71  l_int32 hsize,
72  l_int32 vsize)
73 {
74 PIX *pixr, *pixg, *pixb, *pixrm, *pixgm, *pixbm, *pixd;
75 
76  if (!pixs)
77  return (PIX *)ERROR_PTR("pixs not defined", __func__, NULL);
78  if (pixGetDepth(pixs) != 32)
79  return (PIX *)ERROR_PTR("pixs not 32 bpp", __func__, NULL);
80  if (type != L_MORPH_DILATE && type != L_MORPH_ERODE &&
81  type != L_MORPH_OPEN && type != L_MORPH_CLOSE)
82  return (PIX *)ERROR_PTR("invalid morph type", __func__, NULL);
83  if (hsize < 1 || vsize < 1)
84  return (PIX *)ERROR_PTR("hsize or vsize < 1", __func__, NULL);
85  if ((hsize & 1) == 0 ) {
86  L_WARNING("horiz sel size must be odd; increasing by 1\n", __func__);
87  hsize++;
88  }
89  if ((vsize & 1) == 0 ) {
90  L_WARNING("vert sel size must be odd; increasing by 1\n", __func__);
91  vsize++;
92  }
93 
94  if (hsize == 1 && vsize == 1)
95  return pixCopy(NULL, pixs);
96 
97  pixr = pixGetRGBComponent(pixs, COLOR_RED);
98  pixg = pixGetRGBComponent(pixs, COLOR_GREEN);
99  pixb = pixGetRGBComponent(pixs, COLOR_BLUE);
100  if (type == L_MORPH_DILATE) {
101  pixrm = pixDilateGray(pixr, hsize, vsize);
102  pixgm = pixDilateGray(pixg, hsize, vsize);
103  pixbm = pixDilateGray(pixb, hsize, vsize);
104  } else if (type == L_MORPH_ERODE) {
105  pixrm = pixErodeGray(pixr, hsize, vsize);
106  pixgm = pixErodeGray(pixg, hsize, vsize);
107  pixbm = pixErodeGray(pixb, hsize, vsize);
108  } else if (type == L_MORPH_OPEN) {
109  pixrm = pixOpenGray(pixr, hsize, vsize);
110  pixgm = pixOpenGray(pixg, hsize, vsize);
111  pixbm = pixOpenGray(pixb, hsize, vsize);
112  } else { /* type == L_MORPH_CLOSE */
113  pixrm = pixCloseGray(pixr, hsize, vsize);
114  pixgm = pixCloseGray(pixg, hsize, vsize);
115  pixbm = pixCloseGray(pixb, hsize, vsize);
116  }
117  pixd = pixCreateRGBImage(pixrm, pixgm, pixbm);
118  pixDestroy(&pixr);
119  pixDestroy(&pixrm);
120  pixDestroy(&pixg);
121  pixDestroy(&pixgm);
122  pixDestroy(&pixb);
123  pixDestroy(&pixbm);
124 
125  return pixd;
126 }
PIX * pixColorMorph(PIX *pixs, l_int32 type, l_int32 hsize, l_int32 vsize)
pixColorMorph()
Definition: colormorph.c:69
PIX * pixCloseGray(PIX *pixs, l_int32 hsize, l_int32 vsize)
pixCloseGray()
Definition: graymorph.c:520
PIX * pixDilateGray(PIX *pixs, l_int32 hsize, l_int32 vsize)
pixDilateGray()
Definition: graymorph.c:276
PIX * pixOpenGray(PIX *pixs, l_int32 hsize, l_int32 vsize)
pixOpenGray()
Definition: graymorph.c:390
PIX * pixErodeGray(PIX *pixs, l_int32 hsize, l_int32 vsize)
pixErodeGray()
Definition: graymorph.c:162
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:608
PIX * pixCopy(PIX *pixd, const PIX *pixs)
pixCopy()
Definition: pix1.c:689
PIX * pixGetRGBComponent(PIX *pixs, l_int32 comp)
pixGetRGBComponent()
Definition: pix2.c:2464
PIX * pixCreateRGBImage(PIX *pixr, PIX *pixg, PIX *pixb)
pixCreateRGBImage()
Definition: pix2.c:2410
@ COLOR_BLUE
Definition: pix.h:330
@ COLOR_RED
Definition: pix.h:328
@ COLOR_GREEN
Definition: pix.h:329