1 #   include	"bitmapConfig.h"
2 
3 #   include	<string.h>
4 
5 #   include	"bmintern.h"
6 
7 #   include	<appDebugon.h>
8 
9 /************************************************************************/
10 /*									*/
11 /*  Convert a buffer in planar format to chunky format.			*/
12 /*									*/
13 /*  No attempt was made to write optimal code.				*/
14 /*  Only certain 'bits per sample' are supported.			*/
15 /*									*/
16 /************************************************************************/
17 
bmPlanarToChunky(unsigned char * to,const unsigned char * from,const BitmapDescription * bd)18 int bmPlanarToChunky(	unsigned char *			to,
19 			const unsigned char *		from,
20 			const BitmapDescription *	bd )
21     {
22     const unsigned char *	fr[6];
23     unsigned int		sh[6];
24     int				spl;
25     int				pixelCount;
26     int				pos;
27     int				by;
28 
29     int				bytesPerComponent;
30     unsigned char		mask;
31     unsigned int		v;
32     int				bits;
33 
34     if  ( bd->bdSamplesPerPixel == 1 )
35 	{
36 	LDEB(bd->bdSamplesPerPixel);
37 	memcpy( to, from, bd->bdBufferLength );
38 	return 0;
39 	}
40 
41     if  ( bd->bdSamplesPerPixel > sizeof(fr)/sizeof(const unsigned char *) )
42 	{ LDEB(bd->bdSamplesPerPixel); return -1;	}
43 
44     bytesPerComponent= bd->bdBufferLength/ bd->bdSamplesPerPixel;
45     pixelCount= bd->bdPixelsWide* bd->bdPixelsHigh;
46 
47     for ( spl= 0; spl < bd->bdSamplesPerPixel; spl++ )
48 	{ fr[spl]= from+ spl* bytesPerComponent;	}
49 
50     if  ( bd->bdBitsPerSample >= 8 )
51 	{
52 	int	bytesPerSample= bd->bdBitsPerSample/ 8;
53 
54 	if  ( bd->bdBitsPerSample % 8 != 0 )
55 	    { LDEB(bd->bdBitsPerSample); return -1;	}
56 
57 	for ( pos= 0; pos < bytesPerComponent; pos++ )
58 	    {
59 	    for ( spl= 0; spl < bd->bdSamplesPerPixel; spl++ )
60 		{
61 		for ( by= 0; by < bytesPerSample; by++ )
62 		    { *(to++)= *(fr[spl]++);	}
63 		}
64 	    }
65 
66 	return 0;
67 	}
68 
69     if  ( 8 % bd->bdBitsPerSample != 0 )
70 	{ LDEB(bd->bdBitsPerSample); return -1; }
71 
72     mask= 1;
73     for ( spl= 0; spl < bd->bdBitsPerSample; spl++ )
74 	{ mask= 2* mask;	}
75     mask--;
76 
77     for ( spl= 0; spl < bd->bdSamplesPerPixel; spl++ )
78 	{
79 	fr[spl]= from+ spl* bytesPerComponent;
80 	sh[spl]= 8- bd->bdBitsPerSample;
81 	}
82 
83     v= 0; bits= 0;
84     for ( pos= 0; pos < pixelCount; pos++ )
85 	{
86 	for ( spl= 0; spl < bd->bdSamplesPerPixel; spl++ )
87 	    {
88 	    v |= ( *(fr[spl]) >> sh[spl] ) & mask;
89 
90 	    bits += bd->bdBitsPerSample;
91 	    if  ( bits >= 8 )
92 		{ bits -= 8; *(to++)= v; v= 0;	}
93 	    else{ v <<= bd->bdBitsPerSample;	}
94 
95 	    if  ( sh[spl] < bd->bdBitsPerSample )
96 		{ fr[spl]++; sh[spl]= 8;	}
97 	    sh[spl] -= bd->bdBitsPerSample;
98 	    }
99 	}
100 
101     if  ( bits > 0 )
102 	{ *(to++)= v;	}
103 
104     return 0;
105     }
106