File: bmtogray.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 (266 lines) | stat: -rw-r--r-- 6,346 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
#   include	"bitmapConfig.h"

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

/************************************************************************/
/*									*/
/*  Map a color image to grayscale.					*/
/*									*/
/************************************************************************/

#   define	F_R	77
#   define	F_G	150
#   define	F_B	29

/************************************************************************/
/*									*/
/*  Free memory in a grayscale allocator.				*/
/*									*/
/************************************************************************/

static void bmGrayCleanupAllocator(	ColorAllocator *	ca )
    {
    if  ( ca->caSystemPrivate )
	{ free( ca->caSystemPrivate );	}

    return;
    }

/************************************************************************/
/*									*/
/*  Set resources in a grayscale allocator, depending on the number of	*/
/*  bits.								*/
/*									*/
/************************************************************************/

static int bmGraySetAllocator(		ColorAllocator *	ca,
					int			bitsPerPixel,
					SystemAllocator		sysAllocator )
    {
    unsigned int *	fresh;

    unsigned int	mask;
    unsigned int	divi;

    mask= ( 1 << bitsPerPixel )- 1;
    divi= ( 255* 256 )/mask;

    fresh= (unsigned int *)realloc( ca->caSystemPrivate, 2* sizeof(int) );
    if  ( ! fresh )
	{ XDEB(fresh); return -1;	}

    ca->caSystemPrivate= fresh;

    fresh[0]= divi;
    fresh[1]= mask;

    ca->caSystemAllocator= sysAllocator;
    ca->caSystemCleanup= bmGrayCleanupAllocator;
    ca->caAllocationType= CA_ALLOCATOR;

    return 0;
    }

/************************************************************************/
/*									*/
/*  Allocate a grayscale color:						*/
/*									*/
/*  WB:	White on Black i.e. White is maximum.				*/
/*  BW:	Black on White i.e. Black is maximum.				*/
/*									*/
/************************************************************************/

static int bmToGrayAllocateWBColor(	AllocatorColor *	ac,
					ColorAllocator *	ca,
					unsigned int		r,
					unsigned int		g,
					unsigned int		b )
    {
    const unsigned int *	pars= (unsigned int *)ca->caSystemPrivate;
    unsigned int		divi= pars[0];
    long			val;

    val= ( F_R* r+ F_G* g+ F_B* b )/ divi;

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

    return 0;
    }

static int bmToGrayAllocateBWColor(	AllocatorColor *	ac,
					ColorAllocator *	ca,
					unsigned int		r,
					unsigned int		g,
					unsigned int		b )
    {
    const unsigned int *	pars= (unsigned int *)ca->caSystemPrivate;
    unsigned int		divi= pars[0];
    unsigned int		mask= pars[1];
    long			val;

    val= ( F_R* r+ F_G* g+ F_B* b )/ divi;

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

    return 0;
    }

/************************************************************************/
/*									*/
/*  Choose an allocator for colors in a grayscale image.		*/
/*									*/
/*  WB:	White on Black i.e. White is maximum.				*/
/*  BW:	Black on White i.e. Black is maximum.				*/
/*									*/
/************************************************************************/

int bmSetColorAllocatorForBWImage(	ColorAllocator *		ca,
					const BitmapDescription *	bd )
    {
    switch( bd->bdBitsPerPixel )
	{
	case 8:
	case 4:
	case 2:
	case 1:
	    if  ( bmGraySetAllocator( ca, bd->bdBitsPerPixel,
						    bmToGrayAllocateBWColor ) )
		{ LDEB(bd->bdBitsPerPixel); return -1;	}

	    return 0;

	default:
	    LDEB(bd->bdBitsPerPixel); return -1;
	}
    }

int bmSetColorAllocatorForWBImage(	ColorAllocator *		ca,
					const BitmapDescription *	bd )
    {
    switch( bd->bdBitsPerPixel )
	{
	case 8:
	case 4:
	case 2:
	case 1:
	    if  ( bmGraySetAllocator( ca, bd->bdBitsPerPixel,
						bmToGrayAllocateWBColor ) )
		{ LDEB(bd->bdBitsPerPixel); return -1;	}

	    return 0;

	default:
	    LDEB(bd->bdBitsPerPixel); return -1;
	}
    }

/************************************************************************/
/*									*/
/*  Convert an image to grayscale.					*/
/*									*/
/*  1)  Check input format.						*/
/*  2)	Derive properties of output bitmap from input.			*/
/*  3)	Choose grayscale color allocator.				*/
/*  4)	Allocate output buffer.						*/
/*  5)	Fill it.							*/
/*  6)	Return outputs.							*/
/*  7)	Cleanup.							*/
/*									*/
/************************************************************************/

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

    BitmapDescription	bd;

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

    ColorAllocator	ca;

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

    bmInitDescription( &bd );
    bmInitColorAllocator( &ca );

    /*  1  */
    switch( bdIn->bdColorEncoding )
	{
	case BMcoRGB:
	    break;

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

    switch( bdIn->bdBitsPerSample )
	{
	case 8:
	    break;
	default:
	    LDEB(bdIn->bdBitsPerSample);
	    rval= -1; goto ready;
	}

    /*  2  */
    bmCopyDescription( &bd, bdIn );

    bd.bdColorEncoding= BMcoWHITEBLACK;
    bd.bdBitsPerSample= 8;

    if  ( bmCalculateSizes( &bd ) )
	{ LDEB(1); rval= -1; goto ready;	}

    /*  3  */
    if  ( bmGraySetAllocator( &ca, bd.bdBitsPerPixel,
						    bmToGrayAllocateWBColor ) )
	{ LDEB(bd.bdBitsPerPixel); rval= -1; goto ready;	}

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

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

    /*  6  */
    if  ( bmCopyDescription( bdOut, &bd ) )
	{ LDEB(1); rval= -1; goto ready;	}

    /* steal */
    *pBufOut= buffer; buffer= (unsigned char *)0;

  ready:

    /*  7  */
    if  ( buffer )
	{ free( buffer );	}

    bmCleanDescription( &bd );

    bmCleanColorAllocator( &ca );

    return rval;
    }