File: bmsqueeze.c

package info (click to toggle)
ted 2.11-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 11,064 kB
  • ctags: 13,935
  • sloc: ansic: 120,446; makefile: 7,469; sh: 253
file content (372 lines) | stat: -rw-r--r-- 8,377 bytes parent folder | download | duplicates (3)
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
#   include	"bitmapConfig.h"

#   include	<stdio.h>
#   include	<stdlib.h>
#   include	<string.h>
#   include	"bmintern.h"
#   include	<sioHex.h>
#   include	<sioEndian.h>
#   include	<sioStdio.h>

#   include	<appDebugon.h>

/************************************************************************/
/*									*/
/*  Reduce the number of bits per pixel either by realizing that the	*/
/*  actual number of colors can be representing in fewer bits, or by	*/
/*  changing from a black/white palette to monochrome.			*/
/*									*/
/*  This will make it possible to save it in monochrome formats such	*/
/*  the TIFF fax formats.						*/
/*									*/
/************************************************************************/

static void bmSqueeze_8_4(	const unsigned char *	fr,
				unsigned char *		to,
				int			wide )
    {
    int			col;
    unsigned char	s[8];

    for ( col= 0; col+ 1 < wide; fr += 2, col += 2, to++ )
	{
	*to=	( ( fr[0] & 0x0f ) << 4 )	|
		( ( fr[1] & 0x0f )      )	;
	}

    if  ( col < wide )
	{
	memset( s, 0, 8 );
	memcpy( s, fr, wide- col ); fr= s;

	*to=	( ( fr[0] & 0x0f ) << 4 )	|
		( ( fr[1] & 0x0f )      )	;
	}
    }

static void bmSqueeze_8_2(	const unsigned char *	fr,
				unsigned char *		to,
				int			wide )
    {
    int			col;
    unsigned char	s[8];

    for ( col= 0; col+ 3 < wide;
				fr += 4, col += 4, to++ )
	{
	*to=	( ( fr[0] & 0x03 ) << 6 )	|
		( ( fr[1] & 0x03 ) << 4 )	|
		( ( fr[2] & 0x03 ) << 2 )	|
		( ( fr[3] & 0x03 )      )	;
	}

    if  ( col < wide )
	{
	memset( s, 0, 8 );
	memcpy( s, fr, wide- col ); fr= s;

	*to=	( ( fr[0] & 0x03 ) << 6 )	|
		( ( fr[1] & 0x03 ) << 4 )	|
		( ( fr[2] & 0x03 ) << 2 )	|
		( ( fr[3] & 0x03 )      )	;
	}
    }

static void bmSqueeze_8_1(	const unsigned char *	fr,
				unsigned char *		to,
				int			wide )
    {
    int			col;
    unsigned char	s[8];

    for ( col= 0; col+ 7 < wide; fr += 8, col += 8, to++ )
	{
	*to=	( ( fr[0] & 0x01 ) << 7 )	|
		( ( fr[1] & 0x01 ) << 6 )	|
		( ( fr[2] & 0x01 ) << 5 )	|
		( ( fr[3] & 0x01 ) << 4 )	|
		( ( fr[4] & 0x01 ) << 3 )	|
		( ( fr[5] & 0x01 ) << 2 )	|
		( ( fr[6] & 0x01 ) << 1 )	|
		( ( fr[7] & 0x01 )      )	;
	}

    if  ( col < wide )
	{
	memset( s, 0, 8 );
	memcpy( s, fr, wide- col ); fr= s;

	*to=	( ( fr[0] & 0x01 ) << 7 )	|
		( ( fr[1] & 0x01 ) << 6 )	|
		( ( fr[2] & 0x01 ) << 5 )	|
		( ( fr[3] & 0x01 ) << 4 )	|
		( ( fr[4] & 0x01 ) << 3 )	|
		( ( fr[5] & 0x01 ) << 2 )	|
		( ( fr[6] & 0x01 ) << 1 )	|
		( ( fr[7] & 0x01 )      )	;
	}
    }

static void bmSqueeze_4_2(	const unsigned char *	fr,
				unsigned char *		to,
				int			wide )
    {
    int			col;
    unsigned char	s[8];

    for ( col= 0; col+ 3 < wide; fr += 2, col += 4, to++ )
	{
	*to=	( ( fr[0] & 0x30 ) << 2 )	|
		( ( fr[0] & 0x03 ) << 6 )	|
		( ( fr[1] & 0x30 ) << 2 )	|
		( ( fr[1] & 0x03 )      )	;
	}

    if  ( col < wide )
	{
	memset( s, 0, 8 );
	memcpy( s, fr, ( wide- col+ 1 )/2 );
	fr= s;

	*to=	( ( fr[0] & 0x30 ) << 2 )	|
		( ( fr[0] & 0x03 ) << 6 )	|
		( ( fr[1] & 0x30 ) << 2 )	|
		( ( fr[1] & 0x03 )      )	;
	}
    }

static void bmSqueeze_4_1(	const unsigned char *	fr,
				unsigned char *		to,
				int			wide )
    {
    int			col;
    unsigned char	s[8];

    for ( col= 0; col+ 7 < wide; fr += 4, col += 8, to++ )
	{
	*to=	( ( fr[0] & 0x10 ) << 3 )	|
		( ( fr[0] & 0x01 ) << 6 )	|
		( ( fr[1] & 0x10 ) << 1 )	|
		( ( fr[1] & 0x01 ) << 4 )	|
		( ( fr[2] & 0x10 ) >> 1 )	|
		( ( fr[2] & 0x01 ) << 2 )	|
		( ( fr[3] & 0x10 ) >> 3 )	|
		( ( fr[3] & 0x01 )      )	;
	}

    if  ( col < wide )
	{
	memset( s, 0, 8 );
	memcpy( s, fr, ( wide- col+ 1 )/2 );
	fr= s;

	*to=	( ( fr[0] & 0x10 ) << 3 )	|
		( ( fr[0] & 0x01 ) << 6 )	|
		( ( fr[1] & 0x10 ) << 1 )	|
		( ( fr[1] & 0x01 ) << 4 )	|
		( ( fr[2] & 0x10 ) >> 1 )	|
		( ( fr[2] & 0x01 ) << 2 )	|
		( ( fr[3] & 0x10 ) >> 3 )	|
		( ( fr[3] & 0x01 )      )	;
	}
    }

static void bmSqueeze_2_1(	const unsigned char *	fr,
				unsigned char *		to,
				int			wide )
    {
    int			col;
    unsigned char	s[8];

    for ( col= 0; col+ 7 < wide; fr += 2, col += 8, to++ )
	{
	*to=	( ( fr[0] & 0x40 ) << 1 )	|
		( ( fr[0] & 0x10 ) << 2 )	|
		( ( fr[0] & 0x04 ) << 3 )	|
		( ( fr[0] & 0x01 ) << 4 )	|
		( ( fr[1] & 0x40 ) << 3 )	|
		( ( fr[1] & 0x10 ) << 2 )	|
		( ( fr[1] & 0x04 ) << 1 )	|
		( ( fr[1] & 0x01 )      )	;
	}

    if  ( col < wide )
	{
	memset( s, 0, 8 );
	memcpy( s, fr, ( wide- col+ 3 )/4 );
	fr= s;

	*to=	( ( fr[0] & 0x40 ) << 1 )	|
		( ( fr[0] & 0x10 ) << 2 )	|
		( ( fr[0] & 0x04 ) << 3 )	|
		( ( fr[0] & 0x01 ) << 4 )	|
		( ( fr[1] & 0x40 ) << 3 )	|
		( ( fr[1] & 0x10 ) << 2 )	|
		( ( fr[1] & 0x04 ) << 1 )	|
		( ( fr[1] & 0x01 )      )	;
	}
    }

static int bmSqueezeBuffer(	const BitmapDescription *	bd,
				int				bitsPerPixel,
				int				bytesPerRow,
				unsigned char *			buffer )
    {
    int				row;

    const unsigned char *	fr;
    unsigned char *		to;

    switch( bd->bdBitsPerPixel )
	{
	case 8:
	    switch( bitsPerPixel )
		{
		case 8:
		    return 0;
		case 4:
		    for ( row= 0; row < bd->bdPixelsHigh; row++ )
			{
			fr= buffer+ row* bd->bdBytesPerRow;
			to= buffer+ row* bytesPerRow;

			bmSqueeze_8_4( fr, to, bd->bdPixelsWide );
			}
		    return 0;
		case 2:
		    for ( row= 0; row < bd->bdPixelsHigh; row++ )
			{
			fr= buffer+ row* bd->bdBytesPerRow;
			to= buffer+ row* bytesPerRow;

			bmSqueeze_8_2( fr, to, bd->bdPixelsWide );
			}
		    return 0;
		case 1:
		    for ( row= 0; row < bd->bdPixelsHigh; row++ )
			{
			fr= buffer+ row* bd->bdBytesPerRow;
			to= buffer+ row* bytesPerRow;

			bmSqueeze_8_1( fr, to, bd->bdPixelsWide );
			}
		    return 0;
		default:
		    LLDEB(bd->bdBitsPerPixel,bitsPerPixel); return -1;
		}
	case 4:
	    switch( bitsPerPixel )
		{
		case 4:
		    return 0;
		case 2:
		    for ( row= 0; row < bd->bdPixelsHigh; row++ )
			{
			fr= buffer+ row* bd->bdBytesPerRow;
			to= buffer+ row* bytesPerRow;

			bmSqueeze_4_2( fr, to, bd->bdPixelsWide );
			}
		    return 0;
		case 1:
		    for ( row= 0; row < bd->bdPixelsHigh; row++ )
			{
			fr= buffer+ row* bd->bdBytesPerRow;
			to= buffer+ row* bytesPerRow;

			bmSqueeze_4_1( fr, to, bd->bdPixelsWide );
			}
		    return 0;
		default:
		    LLDEB(bd->bdBitsPerPixel,bitsPerPixel); return -1;
		}
	case 2:
	    switch( bitsPerPixel )
		{
		case 2:
		    return 0;
		case 1:
		    for ( row= 0; row < bd->bdPixelsHigh; row++ )
			{
			fr= buffer+ row* bd->bdBytesPerRow;
			to= buffer+ row* bytesPerRow;

			bmSqueeze_2_1( fr, to, bd->bdPixelsWide );
			}
		    return 0;
		default:
		    LLDEB(bd->bdBitsPerPixel,bitsPerPixel); return -1;
		}
	case 1:
	    switch( bitsPerPixel )
		{
		case 1:
		    return 0;
		default:
		    LLDEB(bd->bdBitsPerPixel,bitsPerPixel); return -1;
		}
	    break;
	default:
	    LLDEB(bd->bdBitsPerPixel,bitsPerPixel); return -1;
	}
    }

int bmMakeMonochrome(		BitmapDescription *	bd,
				RGB8Color *		palette,
				unsigned char *		buffer )
    {
    int		hasAlpha= bd->bdHasAlpha != 0;
    int		bitsPerPixel;
    int		bytesPerRow;

    if  ( bd->bdColorCount == 2 )
	{
	bitsPerPixel= 1+ hasAlpha;
	bytesPerRow= ( bd->bdPixelsWide* bitsPerPixel+ 7 )/ 8;

	if  ( palette[0].rgb8Red	== 0	&&
	      palette[0].rgb8Green	== 0	&&
	      palette[0].rgb8Blue	== 0	&&
	      palette[1].rgb8Red	== 255	&&
	      palette[1].rgb8Green	== 255	&&
	      palette[1].rgb8Blue	== 255	)
	    {
	    if  ( bmSqueezeBuffer( bd, bitsPerPixel, bytesPerRow, buffer ) )
		{ LDEB(1); return -1;	}

	    free( (char *)palette );
	    bd->bdBitsPerSample= 1;
	    bd->bdColorEncoding= BMcoWHITEBLACK;
	    bd->bdSamplesPerPixel= 1+ hasAlpha;
	    bd->bdBitsPerPixel= bitsPerPixel;
	    bd->bdBytesPerRow= bytesPerRow;
	    bd->bdBufferLength= bd->bdPixelsHigh* bytesPerRow;
	    return 0;
	    }

	if  ( palette[0].rgb8Red	== 255	&&
	      palette[0].rgb8Green	== 255	&&
	      palette[0].rgb8Blue	== 255	&&
	      palette[1].rgb8Red	== 0	&&
	      palette[1].rgb8Green	== 0	&&
	      palette[1].rgb8Blue	== 0	)
	    {
	    if  ( bmSqueezeBuffer( bd, bitsPerPixel, bytesPerRow, buffer ) )
		{ LDEB(1); return -1;	}

	    free( (char *)palette );
	    bd->bdBitsPerSample= 1;
	    bd->bdColorEncoding= BMcoBLACKWHITE;
	    bd->bdSamplesPerPixel= 1+ hasAlpha;
	    bd->bdBitsPerPixel= bitsPerPixel;
	    bd->bdBytesPerRow= bytesPerRow;
	    bd->bdBufferLength= bd->bdPixelsHigh* bytesPerRow;
	    return 0;
	    }

	return -1;
	}

    return -1;
    }