File: bmthreshold.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 (187 lines) | stat: -rw-r--r-- 4,362 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
#   include	"bitmapConfig.h"

#   include	<bmGrayHisto.h>

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

#   include	<math.h>

/************************************************************************/
/*									*/
/*  Thresholding implementations.					*/
/*									*/
/************************************************************************/

static int bmToThresholdAllocateColor(	AllocatorColor *	ac,
					ColorAllocator *	ca,
					unsigned int		r,
					unsigned int		g,
					unsigned int		b )
    {
    ThresholderHistogram *	th= (ThresholderHistogram *)ca->caSystemPrivate;
    long			i;

    if  ( r > th->thThreshold )
	{ i= 1;	}
    else{ i= 0;	}

    ac->acRed= 257* r;
    ac->acGreen= 257* g;
    ac->acBlue= 257* b;
    ac->acColorNumber= i;

    return 0;
    }

static void bmThresholdFillHistogram8(
				ThresholderHistogram *		th,
				const BitmapDescription *	bdIn,
				const unsigned char *		bufferIn )
    {
    int		row;
    int		col;

    for ( col= 0; col < 256; col++ )
	{ th->thHistogram[col]= 0;	}

    for ( row= 0; row < bdIn->bdPixelsHigh; row++ )
	{
	const unsigned char *	from= bufferIn+ row* bdIn->bdBytesPerRow;

	for ( col= 0; col < bdIn->bdPixelsWide; from++, col++ )
	    {
	    if  ( bdIn->bdColorEncoding == BMcoWHITEBLACK )
		{ th->thHistogram[     *from]++; }
	    else{ th->thHistogram[255- *from]++; }
	    }
	}

    th->thHistogramSize= 256;
    th->thPixelCount= bdIn->bdPixelsHigh* bdIn->bdPixelsWide;
    th->thThreshold= 0;

    return;
    }

/************************************************************************/
/*									*/
/*  Thresholding an image.						*/
/*									*/
/*  1)  Initialize data structures.					*/
/*  2)  Only operate on monochrome images.				*/
/*  3)  At this point histogram code only works for 8 bit/pixel images.	*/
/*  4)  Determine threshold value.					*/
/*  5)  Derive properties of output bitmap from input.			*/
/*  6)  Allocate buffer for output image.				*/
/*  7)  Actual thresholding implementaed as a bitmat format		*/
/*	modification.							*/
/*									*/
/************************************************************************/

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

    BitmapDescription		bd;
    int				bytesPerRow;

    unsigned char *		buffer= (unsigned char *)0;

    ColorAllocator		ca;
    ThresholderHistogram	th;

    int				bitmapUnit= 0;
    int				swapBitmapBytes= 0;
    int				swapBitmapBits= 0;
    const int			dither= 0;

    /*  1  */
    bmInitColorAllocator( &ca );
    ca.caSystemAllocator= bmToThresholdAllocateColor;
    ca.caSystemPrivate= &th;
    ca.caAllocationType= CA_ALLOCATOR;

    /*  2  */
    switch( bdIn->bdColorEncoding )
	{
	case BMcoBLACKWHITE:
	case BMcoWHITEBLACK:
	    break;

	case BMcoRGB8PALETTE:
	case BMcoRGB:
	default:
	    LDEB(bdIn->bdColorEncoding);
	    rval= -1; goto ready;
	}

    /*  3  */
    switch( bdIn->bdBitsPerSample )
	{
	case 8:
	    bmThresholdFillHistogram8( &th, bdIn, bufferIn );
	    break;

	default:
	    LDEB(bdIn->bdBitsPerSample);
	    rval= -1; goto ready;
	}

    /*  4  */
    if  ( 0 )
	{
	bmThresholdMean( &th );
	bmThresholdQuantile( &th, 0.5 );
	bmThreshold2Peaks( &th );
	bmThresholdRidler( &th );
	bmThresholdVariance( &th );
	bmThresholdMinimumError( &th );
	bmThresholdKapur( &th );
	bmThresholdJohannsen( &th );
	}
    else{
	bmThresholdJohannsen( &th );
	}

    /*  5  */
    bmInitDescription( &bd );
    bmCopyDescription( &bd, bdIn );

    bd.bdBitsPerSample= 1;
    bd.bdSamplesPerPixel= 1;
    bd.bdBitsPerPixel= 1;

    bd.bdColorEncoding= BMcoWHITEBLACK;

    bytesPerRow= ( bd.bdPixelsWide+ 7 )/ 8;
    bd.bdBytesPerRow= bytesPerRow;
    bd.bdBufferLength= bd.bdPixelsHigh* bytesPerRow;

    /*  6  */
    buffer= (unsigned char *)malloc( bd.bdBufferLength );
    if  ( ! buffer )
	{ LLDEB(bd.bdBufferLength,buffer); rval= -1; goto ready; }

    /*  7  */
    if  ( bmFillImage( &ca, bitmapUnit, swapBitmapBytes, swapBitmapBits,
				dither, buffer, bufferIn, &bd, bdIn ) )
	{ LDEB(1); rval= -1; goto ready;	}

    *pBufOut= buffer;
    *bdOut= bd;

  ready:

    if  ( rval && buffer )
	{ free( buffer );	}
    if  ( rval )
	{ bmCleanDescription( &bd );	}

    return rval;
    }