1 #   include	"bitmapConfig.h"
2 
3 #   include	"bmintern.h"
4 #   include	<string.h>
5 #   include	<stdlib.h>
6 #   include	<appDebugon.h>
7 #   include	<geoUnits.h>
8 
9 /************************************************************************/
10 /*  Translation back and forth between color encoding and string.	*/
11 /************************************************************************/
12 
13 const char *	bmcoStrings[]=
14     {
15     "black on white",
16     "white on black",
17     "RGB",
18     "RGB palette",
19 
20     (const char *)0
21     };
22 
bmcoIntToString(int colorEncodingInt)23 const char *	bmcoIntToString( int colorEncodingInt )
24     {
25     if  ( colorEncodingInt >= 0						&&
26 	  colorEncodingInt < sizeof( bmcoStrings )/sizeof(char *) -1	)
27 	{ return bmcoStrings[colorEncodingInt]; }
28 
29     LDEB(colorEncodingInt); return (char *)0;
30     }
31 
bmcoStringToInt(const char * colorEncodingString)32 int		bmcoStringToInt( const char * colorEncodingString )
33     {
34     int	i;
35 
36     for ( i= 0; i < sizeof( bmcoStrings )/sizeof(char *) -1; i++ )
37 	{
38 	if  ( ! strcmp( colorEncodingString, bmcoStrings[i] ) )
39 	    { return i;	}
40 	}
41 
42     SDEB(colorEncodingString); return -1;
43     }
44 
45 /************************************************************************/
46 /*  Translation back and forth between resolution unit and string.	*/
47 /************************************************************************/
48 
49 const char *	bmunStrings[]=
50     {
51     "m",
52     "inch",
53     "point",
54     "pixel",
55 
56     (const char *)0
57     };
58 
bmunIntToString(int unitInt)59 const char * bmunIntToString( int unitInt )
60     {
61     if  ( unitInt >= 0						&&
62 	  unitInt < sizeof( bmcoStrings )/sizeof(char *) -1	)
63 	{ return bmunStrings[unitInt]; }
64 
65     LDEB(unitInt); return (char *)0;
66     }
67 
bmunStringToInt(const char * unitString)68 int bmunStringToInt( const char * unitString )
69     {
70     int	i;
71 
72     for ( i= 0; i < sizeof( bmunStrings )/sizeof(char *) -1; i++ )
73 	{
74 	if  ( ! strcmp( unitString, bmunStrings[i] ) )
75 	    { return i;	}
76 	}
77 
78     SDEB(unitString); return -1;
79     }
80 
81 /************************************************************************/
82 /*									*/
83 /*  Initialise a BitmapDescription.					*/
84 /*									*/
85 /************************************************************************/
86 
bmInitDescription(BitmapDescription * bd)87 void bmInitDescription	( BitmapDescription *	bd )
88     {
89     bd->bdBufferLength= 0;
90     bd->bdBytesPerRow= 0;
91     bd->bdPixelsWide= 0;
92     bd->bdPixelsHigh= 0;
93     bd->bdBitsPerSample= 0;
94     bd->bdSamplesPerPixel= 0;
95     bd->bdBitsPerPixel= 0;
96     bd->bdXResolution= 0;
97     bd->bdYResolution= 0;
98     bd->bdUnit= BMunILLEGALVALUE;
99     bd->bdColorEncoding= BMcoILLEGALVALUE;
100 
101     bd->bdHasAlpha= 0;
102 
103     utilInitColorPalette( &(bd->bdPalette) );
104     }
105 
106 /************************************************************************/
107 /*									*/
108 /*  Clean a BitmapDescription.						*/
109 /*									*/
110 /************************************************************************/
111 
bmCleanDescription(BitmapDescription * bd)112 void bmCleanDescription	( BitmapDescription *	bd )
113     {
114     utilCleanColorPalette( &(bd->bdPalette) );
115     }
116 
117 /************************************************************************/
118 /*									*/
119 /*  Copy a BitmapDescription.						*/
120 /*									*/
121 /************************************************************************/
122 
bmCopyDescription(BitmapDescription * to,const BitmapDescription * from)123 int bmCopyDescription	(	BitmapDescription *		to,
124 				const BitmapDescription *	from )
125     {
126     utilCleanColorPalette( &(to->bdPalette) );
127 
128     *to= *from;
129 
130     utilInitColorPalette( &(to->bdPalette) );
131 
132     if  ( utilCopyColorPalette( &(to->bdPalette), &(from->bdPalette) ) )
133 	{ LDEB(1); return -1;	}
134 
135     return 0;
136     }
137 
138 /************************************************************************/
139 /*									*/
140 /*  Byte masks								*/
141 /*									*/
142 /************************************************************************/
143 
144 unsigned char	Bmc1Masks[8]=
145     { 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1 };
146 
147 unsigned char	Bmc7Masks[8]=
148     { 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe };
149 
150 /************************************************************************/
151 /*									*/
152 /*  Geometry.								*/
153 /*									*/
154 /************************************************************************/
155 
bmRectangleSizeTwips(int * pRectangleWideTwips,int * pRectangleHighTwips,const BitmapDescription * bd,int pixelsWide,int pixelsHigh)156 void bmRectangleSizeTwips(
157 			int *				pRectangleWideTwips,
158 			int *				pRectangleHighTwips,
159 			const BitmapDescription *	bd,
160 			int				pixelsWide,
161 			int				pixelsHigh )
162     {
163     int		rectangleWideTwips;
164     int		rectangleHighTwips;
165 
166     switch( bd->bdUnit )
167 	{
168 	case BMunM:
169 	    rectangleWideTwips= (int) ( ( TWIPS_PER_M* pixelsWide )/
170 							bd->bdXResolution );
171 	    rectangleHighTwips= (int) ( ( TWIPS_PER_M* pixelsHigh )/
172 							bd->bdYResolution );
173 	    break;
174 
175 	case BMunINCH:
176 	    rectangleWideTwips= (int)( ( 72* 20* pixelsWide )/
177 							bd->bdXResolution );
178 	    rectangleHighTwips= (int)( ( 72* 20* pixelsHigh )/
179 							bd->bdYResolution );
180 	    break;
181 
182 	case BMunPOINT:
183 	    rectangleWideTwips= (int)( ( 20* pixelsWide )/
184 							bd->bdXResolution );
185 	    rectangleHighTwips= (int)( ( 20* pixelsHigh )/
186 							bd->bdYResolution );
187 	    break;
188 
189 	case BMunPIXEL:
190 	    rectangleWideTwips= 20* pixelsWide;
191 	    rectangleHighTwips= 20* pixelsHigh;
192 	    break;
193 
194 	default:
195 	    LDEB(bd->bdUnit);
196 	    rectangleWideTwips= 20* pixelsWide;
197 	    rectangleHighTwips= 20* pixelsHigh;
198 	    break;
199 	}
200 
201     *pRectangleWideTwips= rectangleWideTwips;
202     *pRectangleHighTwips= rectangleHighTwips;
203 
204     return;
205     }
206 
bmImageSizeTwips(int * pImageWideTwips,int * pImageHighTwips,const BitmapDescription * bd)207 void bmImageSizeTwips(	int *				pImageWideTwips,
208 			int *				pImageHighTwips,
209 			const BitmapDescription *	bd )
210     {
211     bmRectangleSizeTwips( pImageWideTwips, pImageHighTwips, bd,
212 				bd->bdPixelsWide, bd->bdPixelsHigh );
213     }
214 
215 /************************************************************************/
216 /*									*/
217 /*  Derive the sizes of a description from the other fields.		*/
218 /*									*/
219 /************************************************************************/
220 
bmCalculateSizes(BitmapDescription * bd)221 int bmCalculateSizes(	BitmapDescription *	bd )
222     {
223     switch( bd->bdColorEncoding )
224 	{
225 	case BMcoBLACKWHITE:
226 	case BMcoWHITEBLACK:
227 	    if  ( bd->bdHasAlpha )
228 		{ bd->bdSamplesPerPixel= 2;	}
229 	    else{ bd->bdSamplesPerPixel= 1;	}
230 
231 	    bd->bdBitsPerPixel= bd->bdSamplesPerPixel* bd->bdBitsPerSample;
232 	    break;
233 
234 	case BMcoRGB:
235 	    if  ( bd->bdHasAlpha )
236 		{ bd->bdSamplesPerPixel= 4;	}
237 	    else{ bd->bdSamplesPerPixel= 3;	}
238 
239 	    bd->bdBitsPerPixel= bd->bdSamplesPerPixel* bd->bdBitsPerSample;
240 	    break;
241 
242 	case BMcoRGB8PALETTE:
243 	    if  ( bd->bdHasAlpha )
244 		{ bd->bdSamplesPerPixel= 4;	}
245 	    else{ bd->bdSamplesPerPixel= 3;	}
246 
247 	    break;
248 
249 	default:
250 	    LDEB(bd->bdColorEncoding);
251 	    return -1;
252 	}
253 
254     bd->bdBytesPerRow= ( bd->bdBitsPerPixel* bd->bdPixelsWide+ 7 )/ 8;
255     bd->bdBufferLength= bd->bdPixelsHigh* bd->bdBytesPerRow;
256 
257     return 0;
258     }
259 
260 /************************************************************************/
261 /*									*/
262 /*  Allocate buffer for an image.					*/
263 /*									*/
264 /************************************************************************/
265 
bmAllocateBuffer(RasterImage * ri)266 int bmAllocateBuffer(		RasterImage *		ri )
267     {
268     unsigned char *	fresh;
269 
270     fresh= (unsigned char *)realloc( ri->riBytes,
271 					    ri->riDescription.bdBufferLength );
272     if  ( ! fresh )
273 	{ LXDEB(ri->riDescription.bdBufferLength,fresh); return -1; }
274 
275     ri->riBytes= fresh;
276     return 0;
277     }
278 
279