File: bmmorpho.c

package info (click to toggle)
ted 2.16-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,944 kB
  • ctags: 20,273
  • sloc: ansic: 167,980; makefile: 12,518; sh: 263
file content (467 lines) | stat: -rw-r--r-- 11,761 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#   include	"bitmapConfig.h"

#   include	"bmintern.h"
#   include	<string.h>

#   include	<appDebugon.h>

/************************************************************************/
/*									*/
/*  Binary morphology operations.					*/
/*									*/
/************************************************************************/

typedef struct DilateJob
    {
    BitmapDescription		djBdOut;
    unsigned char *		djBufOut;
    unsigned char		djInvertMaskSel;
    const BitmapDescription *	djBdSel;
    const unsigned char *	djBufSel;
    int				djRowSelOrig;
    int				djColSelOrig;
    } DilateJob;

static void bmMorphoDilatePixel(	void *			voiddj,
					int			rowIn,
					int			colIn )
    
    {
    DilateJob *	dj= (DilateJob *)voiddj;

    bmDraw1BitImage( &(dj->djBdOut), dj->djBufOut,
					dj->djBdSel, dj->djBufSel,
					dj->djInvertMaskSel,
					rowIn- dj->djRowSelOrig,
					colIn- dj->djColSelOrig );

    return;
    }

/************************************************************************/
/*									*/
/*  Check the input of a morphology operation.				*/
/*									*/
/*  1)  Check the format of the input image.				*/
/*  2)  Check the format of the structuring element.			*/
/*									*/
/************************************************************************/

static int bmMorphoCheckInputFormats(	const BitmapDescription *	bdIn,
					const BitmapDescription *	bdSel )
    {
    /*  1  */
    switch( bdIn->bdColorEncoding )
	{
	case BMcoBLACKWHITE:
	case BMcoWHITEBLACK:
	    if  ( bdIn->bdBitsPerPixel != 1 )
		{
		LLDEB(bdIn->bdColorEncoding,bdIn->bdBitsPerPixel);
		return -1;
		}
	    break;

	default:
	    LLDEB(bdIn->bdColorEncoding,bdIn->bdBitsPerPixel);
	    return -1;
	}

    /*  2  */
    if  ( bdSel->bdColorEncoding != bdIn->bdColorEncoding )
	{
	LLDEB(bdSel->bdColorEncoding,bdIn->bdColorEncoding); 
	return -1;
	}

    if  ( bdSel->bdBitsPerPixel != 1 )
	{
	LLDEB(bdSel->bdColorEncoding,bdSel->bdBitsPerPixel);
	return -1;
	}

    return 0;
    }

/************************************************************************/
/*									*/
/*  Remember what to do.						*/
/*									*/
/************************************************************************/

static void bmMorphoStartDilateJob(
				DilateJob *			dj,
				const BitmapDescription *	bdSel,
				const unsigned char *		bufSel,
				int				rowOrig,
				int				colOrig )
    {
    bmInitDescription( &(dj->djBdOut) );

    dj->djBufOut= (unsigned char *)0;
    dj->djInvertMaskSel= 0x00;
    dj->djBdSel= bdSel;
    dj->djBufSel= bufSel;
    dj->djRowSelOrig= rowOrig;
    dj->djColSelOrig= colOrig;

    return;
    }

/************************************************************************/
/*									*/
/*  Dilate an image.							*/
/*									*/
/*  As a completely arbitrary decision, the '1' value in both the	*/
/*  input and the output have been chosen as the 'black' value of the	*/
/*  morphology books. In the most common WHITEBLACK case of bitmaps,	*/
/*  this means that the colors are swapped.				*/
/*									*/
/*  1)  Allocate output resources.					*/
/*  2)  Determine the mask to ignore the trailing bits in the input	*/
/*	image.								*/
/*  3)  Dilate the individual rows.					*/
/*									*/
/************************************************************************/

static int bmMorphoDilateLow(	DilateJob *			dj,
				BitmapDescription *		bdOut,
				const BitmapDescription *	bdIn,
				unsigned char **		pBufOut,
				const unsigned char *		bufIn,
				unsigned char			invertMaskIn )
    {
    int			rval= 0;

    /*  1  */
    if  ( bmCopyDescription( &(dj->djBdOut), bdIn ) )
	{ LDEB(1); rval= -1; goto ready;	}

    dj->djBufOut= malloc( dj->djBdOut.bdBufferLength+ 1 );
    if  ( ! dj->djBufOut )
	{
	LXDEB(dj->djBdOut.bdBufferLength,dj->djBufOut);
	rval= -1; goto ready;
	}

#   if	PARANOIA
    bdSelPara= bdSel;
    bdOutPara= &dj->djBdOut;
#   endif

    memset( dj->djBufOut, 0, dj->djBdOut.bdBufferLength+ 1 );

    bmForAll1Pixels( bdIn, bufIn, invertMaskIn,
					(void *)dj, bmMorphoDilatePixel );

    if  ( bmCopyDescription( bdOut, &(dj->djBdOut) ) )
	{ LDEB(1); rval= -1; goto ready;	}

    /*  steal  */
    *pBufOut= dj->djBufOut;
    dj->djBufOut= (unsigned char *)0;

  ready:

    if  ( dj->djBufOut )
	{ free( dj->djBufOut );	}

    bmCleanDescription( &(dj->djBdOut) );

    return rval;
    }

/************************************************************************/
/*									*/
/*  Dilate an image.							*/
/*									*/
/************************************************************************/

int bmMorphoDilate(		BitmapDescription *		bdOut,
				const BitmapDescription *	bdIn,
				unsigned char **		pBufOut,
				const unsigned char *		bufIn,
				const BitmapDescription *	bdSel,
				const unsigned char *		bufSel,
				int				rowOrig,
				int				colOrig )
    {
    const unsigned char	invertMaskIn= 0x00;
    DilateJob		dj;

    if  ( bmMorphoCheckInputFormats( bdIn, bdSel ) )
	{ LDEB(1); return -1;	}

    bmMorphoStartDilateJob( &dj, bdSel, bufSel, rowOrig, colOrig );

    return bmMorphoDilateLow( &dj, bdOut, bdIn, pBufOut, bufIn, invertMaskIn );
    }

/************************************************************************/
/*									*/
/*  Erode an image.							*/
/*									*/
/*  Use the fact that the erosion is the complement of the dilation of	*/
/*  the complement of the input with the mirrored structuring element.	*/
/*									*/
/************************************************************************/

int bmMorphoErode(		BitmapDescription *		bdOut,
				const BitmapDescription *	bdIn,
				unsigned char **		pBufOut,
				const unsigned char *		bufIn,
				const BitmapDescription *	bdSel,
				const unsigned char *		bufSel,
				int				rowOrig,
				int				colOrig )
    {
    int			rval= 0;
    const unsigned char	invertMaskIn= 0xff;
    DilateJob		dj;

    unsigned char *	bufRot= (unsigned char *)0;
    BitmapDescription	bdRot;

    int			i;
    unsigned char *	to;

    bmInitDescription( &bdRot );

    if  ( bmMorphoCheckInputFormats( bdIn, bdSel ) )
	{ LDEB(1); rval= -1; goto ready;	}

    if  ( bmUpsideDown( &bdRot, bdSel, &bufRot, bufSel, 180 ) )
	{ LDEB(180); rval= -1; goto ready;	}

    bmMorphoStartDilateJob( &dj, &bdRot, bufRot,
				bdSel->bdPixelsHigh- rowOrig- 1,
				bdSel->bdPixelsWide- colOrig- 1 );

    if  ( bmMorphoDilateLow( &dj, bdOut, bdIn, pBufOut, bufIn, invertMaskIn ) )
	{ XDEB(invertMaskIn); rval= -1; goto ready;	}

    to= *pBufOut;
    for ( i= 0; i < bdOut->bdBufferLength; i++, to++ )
	{ to[0]= ~to[0]; }

  ready:

    if  ( bufRot )
	{ free( bufRot );	}

    bmCleanDescription( &bdRot );

    return rval;
    }

/************************************************************************/
/*									*/
/*  Return the 'Simple' structuring element.				*/
/*									*/
/************************************************************************/

int bmMorphoSetSimpleSe(	BitmapDescription *		bdOut,
				unsigned char **		pBufOut )
    {
    const int		size= 3;
    const int		wide= size;
    const int		high= size;

    int			bytesPerRow;
    int			bufferLength;

    unsigned char *	bufOut;

    int			row;
    unsigned char	last;

    bytesPerRow= ( wide+ 7 )/ 8;
    bufferLength= high* bytesPerRow;

    bufOut= malloc( bufferLength );
    if  ( ! bufOut )
	{ LXDEB(bufferLength,bufOut); return -1;	}

    last= 0xff;
    if  ( wide % 8 )
	{ last= ( 0xff << ( 8- wide % 8 ) ) & 0xff;	}

    for ( row= 0; row < high; row++ )
	{
	unsigned char *	to= bufOut+ row* bytesPerRow;
	int		byte;

	for ( byte= 0; byte < bytesPerRow- 1; to++, byte++ )
	    { *to= 0xff; }
	*to= last;
	}

    bmInitDescription( bdOut );

    bdOut->bdColorEncoding= BMcoWHITEBLACK;
    bdOut->bdPixelsWide= wide;
    bdOut->bdPixelsHigh= high;
    bdOut->bdBitsPerSample= 1;
    bdOut->bdSamplesPerPixel= 1;
    bdOut->bdBitsPerPixel= 1;
    bdOut->bdXResolution= 1;
    bdOut->bdYResolution= 1;
    bdOut->bdUnit= BMunPIXEL;
    bdOut->bdColorEncoding= BMcoWHITEBLACK;

    bdOut->bdBytesPerRow= bytesPerRow;
    bdOut->bdBufferLength= bufferLength;

    *pBufOut= bufOut;
    return 0;
    }

/************************************************************************/
/*									*/
/*  Dilate/Erode an image using the 'Simple' structuring element.	*/
/*									*/
/*  For Erosion, use the approach of bmMorphoErode(), but with some	*/
/*  shortcuts to avoid double work.					*/
/*									*/
/************************************************************************/

int bmMorphoDilateSimple(	BitmapDescription *		bdOut,
				const BitmapDescription *	bdIn,
				unsigned char **		pBufOut,
				const unsigned char *		bufIn,
				int				ignoredInt )
    {
    int			rval= 0;

    BitmapDescription	bdSel;
    unsigned char *	bufSel= (unsigned char *)0;

    const int		rowOrig= 1;
    const int		colOrig= 1;

    bmInitDescription( &bdSel );

    if  ( bmMorphoSetSimpleSe( &bdSel, &bufSel ) )
	{ LDEB(1); rval= -1; goto ready;	}

    if  ( bmMorphoDilate( bdOut, bdIn, pBufOut, bufIn,
					&bdSel, bufSel, rowOrig, colOrig ) )
	{ LLDEB(rowOrig,colOrig); rval= -1; goto ready;	}

  ready:

    if  ( bufSel )
	{ free( bufSel );	}

    bmCleanDescription( &bdSel );

    return rval;
    }

int bmMorphoErodeSimple(	BitmapDescription *		bdOut,
				const BitmapDescription *	bdIn,
				unsigned char **		pBufOut,
				const unsigned char *		bufIn,
				int				ignoredInt )
    {
    int			rval= 0;
    const unsigned char	invertMaskIn= 0xff;
    DilateJob		dj;

    BitmapDescription	bdSel;
    unsigned char *	bufSel= (unsigned char *)0;

    const int		rowOrig= 1;
    const int		colOrig= 1;

    int			i;
    unsigned char *	to;

    bmInitDescription( &bdSel );

    if  ( bmMorphoSetSimpleSe( &bdSel, &bufSel ) )
	{ LDEB(1); rval= -1; goto ready;	}

    if  ( bmMorphoCheckInputFormats( bdIn, &bdSel ) )
	{ LDEB(1); rval= -1; goto ready;	}

    bmMorphoStartDilateJob( &dj, &bdSel, bufSel, rowOrig, colOrig );

    if  ( bmMorphoDilateLow( &dj, bdOut, bdIn, pBufOut, bufIn, invertMaskIn ) )
	{ XDEB(invertMaskIn); rval= -1; goto ready;	}

    to= *pBufOut;
    for ( i= 0; i < bdOut->bdBufferLength; i++, to++ )
	{ to[0]= ~to[0]; }

  ready:

    if  ( bufSel )
	{ free( bufSel );	}

    bmCleanDescription( &bdSel );

    return rval;
    }

/************************************************************************/
/*									*/
/*  Make an element to recognize lines.					*/
/*									*/
/************************************************************************/

int bmMorphoLineElement(	BitmapDescription *		bdOut,
				unsigned char **		pBufOut,
				int				wide,
				int				high,
				int				x0,
				int				y0,
				int				x1,
				int				y1 )
    {
    int			bytesPerRow;
    int			bufferLength;

    unsigned char *	bufOut;

    if  ( x0 < 0 || x0 >= wide )
	{ LLDEB(x0,wide); return -1;	}
    if  ( x1 < 0 || x1 >= wide )
	{ LLDEB(x1,wide); return -1;	}

    if  ( y0 < 0 || y0 >= high )
	{ LLDEB(y0,high); return -1;	}
    if  ( y1 < 0 || y1 >= high )
	{ LLDEB(y1,high); return -1;	}

    bytesPerRow= ( wide+ 7 )/ 8;
    bufferLength= high* bytesPerRow;

    bufOut= malloc( bufferLength );
    if  ( ! bufOut )
	{ LXDEB(bufferLength,bufOut); return -1;	}

    bmInitDescription( bdOut );

    bdOut->bdColorEncoding= BMcoWHITEBLACK;
    bdOut->bdPixelsWide= wide;
    bdOut->bdPixelsHigh= high;
    bdOut->bdBitsPerSample= 1;
    bdOut->bdSamplesPerPixel= 1;
    bdOut->bdBitsPerPixel= 1;
    bdOut->bdXResolution= 1;
    bdOut->bdYResolution= 1;
    bdOut->bdUnit= BMunPIXEL;
    bdOut->bdColorEncoding= BMcoWHITEBLACK;

    bdOut->bdBytesPerRow= bytesPerRow;
    bdOut->bdBufferLength= bufferLength;

    memset( bufOut, 0x00, bufferLength );

    bmDrawLine( bufOut, bdOut, x0, y0, x1, y1, 1 );

    *pBufOut= bufOut;
    return 0;
    }