File: bmflip.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 (799 lines) | stat: -rw-r--r-- 19,362 bytes parent folder | download
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
#   include	"bitmapConfig.h"

#   include	"bmintern.h"

#   include	<string.h>

#   define	y0	math_y0
#   define	y1	math_y1
#   include	<math.h>
#   undef	y0
#   undef	y1

#   include	<appDebugon.h>

#   ifndef	M_PI
#	define	M_PI	3.14159265358979323846
#   endif

/************************************************************************/
/*  Flip a scan line consisting of 1 bit pixels				*/
/************************************************************************/
static unsigned char	bmFlipLeft[256];
static unsigned char	bmFlipRight[256];

static void bmFlip1Init( int n )
    {
    int		i;
    int		rshift= n % 8;
    int		lshift= 8- rshift;

    for ( i= 0; i < 256; i++ )
	{
	unsigned char value=	( i & 0x01 ) << 7	|
				( i & 0x02 ) << 5	|
				( i & 0x04 ) << 3	|
				( i & 0x08 ) << 1	|
				( i & 0x10 ) >> 1	|
				( i & 0x20 ) >> 3	|
				( i & 0x40 ) >> 5	|
				( i & 0x80 ) >> 7	;

	bmFlipLeft[i]=  value << lshift;
	bmFlipRight[i]= value >> rshift;
	}
    }

static void bmFlip2Init( int n )
    {
    int		i;
    int		rshift= 2* ( n % 4 );
    int		lshift= 8- rshift;

    for ( i= 0; i < 256; i++ )
	{
	unsigned char value=	( i & 0x03 ) << 6	|
				( i & 0x0c ) << 2	|
				( i & 0x30 ) >> 2	|
				( i & 0xc0 ) >> 6	;

	bmFlipLeft[i]=  value << lshift;
	bmFlipRight[i]= value >> rshift;
	}
    }

static void bmFlip4Init( int n )
    {
    int		i;
    int		rshift= 4* ( n % 2 );
    int		lshift= 8- rshift;

    for ( i= 0; i < 256; i++ )
	{
	unsigned char value=	( i & 0x0f ) << 4	|
				( i & 0xf0 ) >> 4	;

	bmFlipLeft[i]=  value << lshift;
	bmFlipRight[i]= value >> rshift;
	}
    }

/************************************************************************/
/*									*/
/*  Flip the bytes in a scan line.					*/
/*									*/
/************************************************************************/

int bmFlipBytes(		unsigned char *	buffer,
				int		byteCount,
				int		bitsPerPixel )
    {
    switch( bitsPerPixel )
	{
	case  1:
	    bmFlip1Init( 8/bitsPerPixel ); break;
	case  2:
	    bmFlip2Init( 8/bitsPerPixel ); break;
	case  4:
	    bmFlip4Init( 8/bitsPerPixel ); break;
	case  8:
	    return 0;
	case 24:
	default:
	    LDEB(bitsPerPixel); return -1;
	}

    while( byteCount-- > 0 )
	{ *buffer= bmFlipRight[*buffer]; buffer++;	}

    return 0;
    }


/************************************************************************/
/*									*/
/*  Flip a scan line consisting of 1 bit pixels				*/
/*									*/
/************************************************************************/

static void bmFlipBits(	unsigned char *		to,
			const unsigned char *	from,
			int			pixelsWide,
			int			pixelsPerByte	)
    {
    int			i;
    int			count;

    count= pixelsWide/ pixelsPerByte;
    to += count;

    if  ( pixelsWide % pixelsPerByte )
	{
	*(to)= bmFlipLeft[*from];
	to--;

	for ( i= count; i; i-- )
	    {
	    *to=       bmFlipRight[*(from++)];
	    *(to--) |= bmFlipLeft [* from   ];
	    }
	}
    else{
	for ( i= count; i; i-- )
	    { *(--to)= bmFlipRight[*(from++)];	}
	}
    }

/************************************************************************/
/*  Flip a scan line consisting of 8 bit pixels				*/
/************************************************************************/
static void bmFlip8Bits(	unsigned char *		to,
				const unsigned char *	from,
				int			pixelsWide,
				int			ignored	)
    {
    int			i;

    to += pixelsWide- 1;

    for ( i= pixelsWide; i; i-- )
	{ *(to--)= *(from++); }
    }

/************************************************************************/
/*  Flip a scan line consisting of 24 bit pixels			*/
/************************************************************************/
static void bmFlip24Bits(	unsigned char *		to,
				const unsigned char *	from,
				int			pixelsWide,
				int			ignored	)
    {
    int			i;

    to += 3*pixelsWide- 3;

    for ( i= pixelsWide; i; i-- )
	{
	to[0]= from[0]; to[1]= from[1]; to[2]= from[2];
	to -= 3; from += 3;
	}
    }

/************************************************************************/
/*  Mirror bitmap images: Flip over horizontal axis.			*/
/************************************************************************/
int bmVerticalFlip(	BitmapDescription *		bdOut,
			const BitmapDescription *	bdIn,
			unsigned char **		pBufOut,
			const unsigned char *		bufIn,
			int				ignoredInt	)
    {
    const unsigned char *	from;
    unsigned char *		to;
    unsigned int		row;
    unsigned char * 		bufOut;

    bufOut= (unsigned char *)malloc( bdIn->bdBufferLength );
    if  ( ! bufOut )
	{ LLDEB(bdIn->bdBufferLength,bufOut); return -1;	}

    for ( row= 0; row < bdIn->bdPixelsHigh; row++ )
	{
	from= bufIn+ row* bdIn->bdBytesPerRow;
	to= bufOut+ ( bdIn->bdPixelsHigh- row -1 )* bdIn->bdBytesPerRow;

	memcpy( to, from, bdIn->bdBytesPerRow );
	}

    bmCopyDescription( bdOut, bdIn ); *pBufOut= bufOut;

    return 0;
    }

/************************************************************************/
/*  Mirror bitmap images: Rotate 180 degrees.				*/
/************************************************************************/

int bmUpsideDown(	BitmapDescription *		bdOut,
			const BitmapDescription *	bdIn,
			unsigned char **		pBufOut,
			const unsigned char *		bufIn,
			int				ignoredInt )
    {
    const unsigned char *	from;
    unsigned char *		to;
    unsigned int		row;
    int				pixelsPerByte= 8/ bdIn->bdBitsPerPixel;
    unsigned char * 		bufOut;
    void			(*flip)( unsigned char *,
					const unsigned char *, int, int );

    bufOut= (unsigned char *)malloc( bdIn->bdBufferLength );
    if  ( ! bufOut )
	{ LLDEB(bdIn->bdBufferLength,bufOut); return -1;	}

    switch( bdIn->bdBitsPerPixel )
	{
	case  1:
	    flip= bmFlipBits;	bmFlip1Init( bdIn->bdPixelsWide ); break;
	case  2:
	    flip= bmFlipBits;	bmFlip2Init( bdIn->bdPixelsWide ); break;
	case  4:
	    flip= bmFlipBits;	bmFlip4Init( bdIn->bdPixelsWide ); break;
	case  8:
	    flip= bmFlip8Bits;	break;
	case 24:
	    flip= bmFlip24Bits;	break;
	default:
	    LDEB(bdIn->bdBitsPerPixel); return -1;
	}

    for ( row= 0; row < bdIn->bdPixelsHigh; row++ )
	{
	from= bufIn+ row* bdIn->bdBytesPerRow;
	to= bufOut+ ( bdIn->bdPixelsHigh- row -1 )* bdIn->bdBytesPerRow;

	(*flip)( to, from, bdIn->bdPixelsWide, pixelsPerByte );
	}

    bmCopyDescription( bdOut, bdIn ); *pBufOut= bufOut;

    return 0;
    }

/************************************************************************/
/*  Mirror bitmap images: Flip over vertical axis.			*/
/************************************************************************/
int bmHorizontalFlip(	BitmapDescription *		bdOut,
			const BitmapDescription *	bdIn,
			unsigned char **		pBufOut,
			const unsigned char *		bufIn,
			int				ignoredInt	)
    {
    const unsigned char *	from;
    unsigned char *		to;
    unsigned int		row;
    int				pixelsPerByte= 8/ bdIn->bdBitsPerPixel;
    unsigned char * 		bufOut;
    void			(*flip)( unsigned char *,
					const unsigned char *, int, int );

    bufOut= (unsigned char *)malloc( bdIn->bdBufferLength );
    if  ( ! bufOut )
	{ LLDEB(bdIn->bdBufferLength,bufOut); return -1;	}

    switch( bdIn->bdBitsPerPixel )
	{
	case  1:
	    flip= bmFlipBits;	bmFlip1Init( bdIn->bdPixelsWide ); break;
	case  2:
	    flip= bmFlipBits;	bmFlip2Init( bdIn->bdPixelsWide ); break;
	case  4:
	    flip= bmFlipBits;	bmFlip4Init( bdIn->bdPixelsWide ); break;
	case  8:
	    flip= bmFlip8Bits;	break;
	case 24:
	    flip= bmFlip24Bits;	break;
	default:
	    LDEB(bdIn->bdBitsPerPixel); return -1;
	}

    for ( row= 0; row < bdIn->bdPixelsHigh; row++ )
	{
	from= bufIn+ row* bdIn->bdBytesPerRow;
	to= bufOut+ row* bdIn->bdBytesPerRow;
	(*flip)( to, from, bdIn->bdPixelsWide, pixelsPerByte );
	}

    bmCopyDescription( bdOut, bdIn ); *pBufOut= bufOut;

    return 0;
    }

/************************************************************************/
/*  Rotate a bitmap by 0,90,180,270 degrees.				*/
/************************************************************************/

int bmRotate90(	BitmapDescription *		bdOut,
		const BitmapDescription *	bdIn,
		unsigned char **		pBufOut,
		const unsigned char *		bufIn,
		int				angle )
    {
    const unsigned char *	from;
    unsigned char *		to;
    unsigned int		row, col;
    unsigned char * 		bufOut;
    BitmapDescription		bd;

    int				s;
    int				shift;
    unsigned char		m= 0;
    unsigned char		mr;
    int				step;

    switch( angle )
	{
	case 0:
	    if  ( bmCopyDescription( &bd, bdIn ) )
		{ LDEB(1); return -1;	}

	    bufOut= (unsigned char *)malloc( bd.bdBufferLength );
	    if  ( ! bufOut )
		{ LLDEB(bd.bdBufferLength,bufOut); return -1;	}

	    memcpy( bufOut, bufIn, bd.bdBufferLength );

	    *bdOut= bd; *pBufOut= bufOut;
	    return 0;
	case 180:
	    return bmUpsideDown( bdOut, bdIn, pBufOut, bufIn, angle );
	case 270:
	case 90:
	    break;
	default:
	    LDEB(angle); return -1;
	}


    bmCopyDescription( &bd, bdIn );
    bd.bdPixelsWide= bdIn->bdPixelsHigh;
    bd.bdPixelsHigh= bdIn->bdPixelsWide;
    bd.bdBytesPerRow= ( bd.bdPixelsWide* bdIn->bdBitsPerPixel+ 7 )/ 8;
    bd.bdBufferLength= bd.bdPixelsHigh* bd.bdBytesPerRow;

    bufOut= (unsigned char *)malloc( bd.bdBufferLength );
    if  ( ! bufOut )
	{ LLDEB(bd.bdBufferLength,bufOut); return -1;	}

    switch( bdIn->bdBitsPerPixel )
	{
	case  4:
	    m |= 0x30;
	    /*FALLTHROUGH*/
	case  2:
	    m |= 0x40;
	    /*FALLTHROUGH*/
	case  1:
	    m |= 0x80;

	    memset( bufOut, 0, bd.bdBufferLength );
	    step= 8/ bdIn->bdBitsPerPixel;

	    if  ( angle == 90 )
		{
		for ( row= 0; row < bd.bdPixelsHigh; row++ )
		    {
		    int		toRow= bd.bdPixelsHigh- row- 1;

		    mr= m >> bdIn->bdBitsPerPixel* ( row % step );

		    for ( s= 0; s < row % step; s++ )
			{
			to= bufOut+ toRow* bd.bdBytesPerRow;
			from= bufIn+ row/step+ s* bdIn->bdBytesPerRow;

			shift= row % step- s;
			shift *= bdIn->bdBitsPerPixel;

			for ( col= s; col < bd.bdPixelsWide; col += step )
			    {
			    *(to++) |= ( *from & mr ) << shift;
			    from += step* bdIn->bdBytesPerRow;
			    }
			}

		    for ( s= row % step; s < step; s++ )
			{
			to= bufOut+ toRow* bd.bdBytesPerRow;
			from= bufIn+ row/step+ s* bdIn->bdBytesPerRow;

			shift= s- row % step;
			shift *= bdIn->bdBitsPerPixel;

			for ( col= s; col < bd.bdPixelsWide; col += step )
			    {
			    *(to++) |= ( *from & mr ) >> shift;
			    from += step* bdIn->bdBytesPerRow;
			    }
			}
		    }
		}
	    else{
		/*270*/
		for ( row= 0; row < bd.bdPixelsHigh; row++ )
		    {
		    mr= m >> bdIn->bdBitsPerPixel* ( row % step );

		    for ( s= 0; s < row % step; s++ )
			{
			to= bufOut+ row* bd.bdBytesPerRow;
			from= bufIn+ ( bdIn->bdPixelsHigh- 1 )*
							bdIn->bdBytesPerRow;
			from -= s* bdIn->bdBytesPerRow;
			from += row/ step;

			shift= row % step- s;
			shift *= bdIn->bdBitsPerPixel;

			for ( col= 0; col < bd.bdPixelsWide; col += step )
			    {
			    *(to++) |= ( *from & mr ) << shift;
			    from -= step* bdIn->bdBytesPerRow;
			    }
			}

		    for ( s= row % step; s < step; s++ )
			{
			to= bufOut+ row* bd.bdBytesPerRow;
			from= bufIn+ ( bdIn->bdPixelsHigh- 1 )*
							bdIn->bdBytesPerRow;
			from -= s* bdIn->bdBytesPerRow;
			from += row/ step;

			shift= s- row % step;
			shift *= bdIn->bdBitsPerPixel;

			for ( col= 0; col < bd.bdPixelsWide; col += step )
			    {
			    *(to++) |= ( *from & mr ) >> shift;
			    from -= step* bdIn->bdBytesPerRow;
			    }
			}
		    }
		}

	    break;
	case  8:
	    if  ( angle == 90 )
		{
		for ( row= 0; row < bd.bdPixelsHigh; row++ )
		    {
		    from= bufIn+ row;
		    to= bufOut+ ( bd.bdPixelsHigh- row- 1 )* bd.bdBytesPerRow;

		    for ( col= 0; col < bd.bdPixelsWide; col++ )
			{ *(to++)= *from; from += bdIn->bdBytesPerRow; }
		    }
		}
	    else{
		/*270*/
		for ( row= 0; row < bd.bdPixelsHigh; row++ )
		    {
		    from= bufIn+ row;
		    to= bufOut+ ( row+ 1 )* bd.bdBytesPerRow- 1;

		    for ( col= 0; col < bd.bdPixelsWide; col++ )
			{ *(to--)= *from; from += bdIn->bdBytesPerRow; }
		    }
		}
	    break;
	case 24:
	    if  ( angle == 90 )
		{
		for ( row= 0; row < bd.bdPixelsHigh; row++ )
		    {
		    from= bufIn+ 3* row;
		    to= bufOut+ ( bd.bdPixelsHigh- row- 1 )* bd.bdBytesPerRow;

		    for ( col= 0; col < bd.bdPixelsWide; col++ )
			{
			*(to++)= from[0]; *(to++)= from[1]; *(to++)= from[2];
			from += bdIn->bdBytesPerRow;
			}
		    }
		}
	    else{
		for ( row= 0; row < bd.bdPixelsHigh; row++ )
		    {
		    from= bufIn+ 3* row;
		    to= bufOut+ ( row+ 1 )* bd.bdBytesPerRow- 3;

		    for ( col= 0; col < bd.bdPixelsWide; col++ )
			{
			to[0]= from[0]; to[1]= from[1]; to[2]= from[2];
			to -= 3; from += bdIn->bdBytesPerRow;
			}
		    }
		}
	    break;
	default:
	    LDEB(bdIn->bdBitsPerPixel); return -1;
	}

    *bdOut= bd; *pBufOut= bufOut;

    return 0;
    }

/************************************************************************/
/*  Rotate a 1, 2, 4 bit scan line over a sharp angle.			*/
/************************************************************************/

static void bmRotateScanline124( const unsigned char *	from,
				int			pixelsWide,
				unsigned char *		bufOut,
				int			bytesPerRow,
				int			trow,
				int			tcol,
				int			eRow,
				int			eCol,
				int			xRow,
				int			xCol,
				int			sRow,
				int			d,
				int			bitsPerPixel )
    {
    int			fcol;
    int			step= 8/ bitsPerPixel;
    unsigned char	tmask;
    int			rshift= ( tcol % step ) * bitsPerPixel;

    unsigned char *	to= bufOut+ trow* bytesPerRow+ tcol/step;

    tmask= 0xff << ( 8- bitsPerPixel );
    tmask= tmask >> ( ( tcol % step )* bitsPerPixel );

    for ( fcol= 0; fcol < pixelsWide; fcol += step )
	{
	int		s;
	unsigned char	byteval= *(from++);

	for ( s= 0; s < step; s++ )
	    {
	    while( eCol >= 0 )
		{
		*to &= ~tmask; *to |=  tmask & ( byteval >> rshift );

		tcol++; eCol -= d;
		tmask= ( 257 * tmask ) >> bitsPerPixel;
		rshift= ( rshift+ bitsPerPixel )% 8;
		if  ( tcol % step == 0 )
		    { to++;	}
		}
	    eCol += xCol;
	    while( eRow >= 0 )
		{
		*to &= ~tmask; *to |=  tmask & ( byteval >> rshift );

		trow += sRow; eRow -= d;
		to += sRow* bytesPerRow;
		}
	    eRow += xRow;

	    byteval= byteval << bitsPerPixel;
	    }
	}
    }

/************************************************************************/
/*  Rotate an 8 bit scan line over a sharp angle.			*/
/************************************************************************/

static void bmRotateScanline8(	const unsigned char *	from,
				int			pixelsWide,
				unsigned char *		bufOut,
				int			bytesPerRow,
				int			trow,
				int			tcol,
				int			eRow,
				int			eCol,
				int			xRow,
				int			xCol,
				int			sRow,
				int			d		)
    {
    int		fcol;

    for ( fcol= 0; fcol < pixelsWide; fcol++ )
	{
	int		pixval= *(from++);

	while( eCol >= 0 )
	    {
	    bufOut[ trow* bytesPerRow+ tcol ]= pixval;
	    tcol++; eCol -= d;
	    }
	eCol += xCol;
	while( eRow >= 0 )
	    {
	    bufOut[ trow* bytesPerRow+ tcol ]= pixval;
	    trow += sRow; eRow -= d;
	    }
	eRow += xRow;
	}
    }

/************************************************************************/
/*  Rotate a 24 bit scan line over a sharp angle.			*/
/************************************************************************/

static void bmRotateScanline24(	const unsigned char *	from,
				int			pixelsWide,
				unsigned char *		bufOut,
				int			bytesPerRow,
				int			trow,
				int			tcol,
				int			eRow,
				int			eCol,
				int			xRow,
				int			xCol,
				int			sRow,
				int			d		)
    {
    int		fcol;
    int		tbyte= tcol* 3;

    for ( fcol= 0; fcol < pixelsWide; fcol++ )
	{
	int		r= *(from++);
	int		g= *(from++);
	int		b= *(from++);

	while( eCol >= 0 )
	    {
	    bufOut[ trow* bytesPerRow+ tbyte    ]= r;
	    bufOut[ trow* bytesPerRow+ tbyte+ 1 ]= g;
	    bufOut[ trow* bytesPerRow+ tbyte+ 2 ]= b;
	    tbyte += 3; eCol -= d;
	    }
	eCol += xCol;
	while( eRow >= 0 )
	    {
	    bufOut[ trow* bytesPerRow+ tbyte    ]= r;
	    bufOut[ trow* bytesPerRow+ tbyte+ 1 ]= g;
	    bufOut[ trow* bytesPerRow+ tbyte+ 2 ]= b;
	    trow += sRow; eRow -= d;
	    }
	eRow += xRow;
	}
    }

/************************************************************************/
/*  Rotate a bitmap over an arbitrary angle.				*/
/*  1)  Sanity of the input.						*/
/************************************************************************/

int bmRotate(	BitmapDescription *		bdOut,
		const BitmapDescription *	bdIn,
		unsigned char **		pBufOut,
		const unsigned char *		bufIn,
		double				angle )
    {
    int				rotations;

    double			sinAngle;
    double			cosAngle;

    int				frow, trow0;
    int				tcol0;

    int				h1;
    int				h2;
    int				w1;
    int				w2;

    /****************************/
    /*  Within the rows.	*/
    /****************************/
    int				d;
    int				xRow, xCol;
    int				sRow;

    /****************************/
    /*  Beginning of rows.	*/
    /****************************/
    int				eRow0, eCol0;
    int				d0;
    int				xRow0, xCol0;

    BitmapDescription		bd;
    unsigned char *		bufOut;

    /*  1  */
    while( angle < -M_PI/4 )
	{ angle += 2* M_PI;	}
    for ( rotations= 0; angle > M_PI/4; rotations++ )
	{ angle -= M_PI/2;	}
    rotations= rotations % 4;

    sinAngle= sin( angle );
    cosAngle= cos( angle );

    h1= sinAngle* bdIn->bdPixelsWide;	/* neg for neg angle.	*/
    h2= cosAngle* bdIn->bdPixelsHigh;
    w1= sinAngle* bdIn->bdPixelsHigh;	/* neg for neg angle.	*/
    w2= cosAngle* bdIn->bdPixelsWide;

    bmCopyDescription( &bd, bdIn );

    if  ( angle > 0 )
	{ bd.bdPixelsWide=  w1+ w2; bd.bdPixelsHigh=  h1+ h2; }
    else{ bd.bdPixelsWide= -w1+ w2; bd.bdPixelsHigh= -h1+ h2; }

    bd.bdBytesPerRow= ( bd.bdPixelsWide* bdIn->bdBitsPerPixel+ 7 )/ 8;
    bd.bdBufferLength= bd.bdPixelsHigh* bd.bdBytesPerRow;
    bufOut= malloc( bd.bdBufferLength );
    if  ( ! bufOut )
	{ LLDEB(bd.bdBufferLength,bufOut);	}
    memset( bufOut, 0, bd.bdBufferLength );

    if  ( angle > 0 )
	{ sRow= -1; xRow=  2* h1; trow0= h1; tcol0=   0; xCol0=  2* w1; }
    else{ sRow=  1; xRow= -2* h1; trow0=  0; tcol0= -w1; xCol0= -2* w1; }

    xRow0= 2* h2;
    eRow0= xRow0- bdIn->bdPixelsHigh;
    eCol0= xCol0- bdIn->bdPixelsHigh;
    d0= 2* bdIn->bdPixelsHigh;

    d= 2* bdIn->bdPixelsWide;
    xCol= 2* w2;

    for ( frow= 0; frow < bdIn->bdPixelsHigh; frow++ )
	{
	while( eRow0 >= 0 )
	    {
	    switch( bdIn->bdBitsPerPixel )
		{
		case 1:
		case 2:
		case 4:
		    bmRotateScanline124( bufIn+ frow* bdIn->bdBytesPerRow,
				bdIn->bdPixelsWide, bufOut,
				bd.bdBytesPerRow, trow0, tcol0,
				xRow- bdIn->bdPixelsWide,
				xCol- bdIn->bdPixelsWide,
				xRow, xCol, sRow, d, bdIn->bdBitsPerPixel );
		    break;
		case 8:
		    bmRotateScanline8( bufIn+ frow* bdIn->bdBytesPerRow,
				bdIn->bdPixelsWide, bufOut,
				bd.bdBytesPerRow, trow0, tcol0,
				xRow- bdIn->bdPixelsWide,
				xCol- bdIn->bdPixelsWide,
				xRow, xCol, sRow, d );
		    break;
		case 24:
		    bmRotateScanline24( bufIn+ frow* bdIn->bdBytesPerRow,
				bdIn->bdPixelsWide, bufOut,
				bd.bdBytesPerRow, trow0, tcol0,
				xRow- bdIn->bdPixelsWide,
				xCol- bdIn->bdPixelsWide,
				xRow, xCol, sRow, d );
		    break;
		default:
		    LDEB(bdIn->bdBitsPerPixel); return -1;
		}

	    trow0++; eRow0 -= d0;
	    }
	eRow0 += xRow0;
	while( eCol0 >= 0 )
	    { tcol0 -= sRow; eCol0 -= d0; }
	eCol0 += xCol0;
	}

    *bdOut= bd; *pBufOut= bufOut;
    return 0;
    }