1 #   include	"bitmapConfig.h"
2 
3 #   include	<stdlib.h>
4 #   include	<string.h>
5 
6 #   include	"bmintern.h"
7 #   include	<appDebugon.h>
8 
9 /************************************************************************/
10 /*									*/
11 /*  Make images with standard content.					*/
12 /*									*/
13 /************************************************************************/
14 
15 /************************************************************************/
16 /*									*/
17 /*  Make a completely transparent image					*/
18 /*									*/
19 /************************************************************************/
20 
bmTransparentImage(BitmapDescription * bdOut,unsigned char ** pBufOut,int colorEncoding,int wide,int high)21 int bmTransparentImage(		BitmapDescription *		bdOut,
22 				unsigned char **		pBufOut,
23 				int				colorEncoding,
24 				int				wide,
25 				int				high )
26     {
27     int				rval= 0;
28     BitmapDescription		bd;
29     unsigned char *		buffer= (unsigned char *)0;
30 
31     bmInitDescription( &bd );
32 
33     switch( colorEncoding )
34 	{
35 	case BMcoRGB8PALETTE:
36 	    bd.bdPixelsWide= wide;
37 	    bd.bdPixelsHigh= high;
38 	    bd.bdHasAlpha= 1;
39 	    bd.bdUnit= BMunINCH;
40 	    bd.bdXResolution= 72;
41 	    bd.bdYResolution= 72;
42 
43 	    bd.bdBitsPerSample= 8;
44 	    bd.bdSamplesPerPixel= 3;
45 
46 	    if  ( bd.bdHasAlpha )
47 		{ bd.bdBitsPerPixel= 8;	}
48 	    else{ bd.bdBitsPerPixel= 4;	}
49 
50 	    bd.bdColorEncoding= BMcoRGB8PALETTE;
51 	    if  ( utilPaletteSetCount( &(bd.bdPalette), 3 ) )
52 		{ LDEB(3); rval= -1; goto ready;	}
53 
54 	    if  ( bmCalculateSizes( &bd ) )
55 		{ LDEB(1); rval= -1; goto ready;	}
56 
57 	    buffer= (unsigned char *)malloc( bd.bdBufferLength );
58 	    if  ( ! buffer )
59 		{ LXDEB(bd.bdBufferLength,buffer); rval= -1; goto ready; }
60 
61 	    /*  transparent */
62 	    bd.bdPalette.cpColors[0].rgb8Red= 255;
63 	    bd.bdPalette.cpColors[0].rgb8Green= 255;
64 	    bd.bdPalette.cpColors[0].rgb8Blue= 255;
65 	    bd.bdPalette.cpColors[0].rgb8Alpha= 0;
66 
67 	    /*  white */
68 	    bd.bdPalette.cpColors[1].rgb8Red= 255;
69 	    bd.bdPalette.cpColors[1].rgb8Green= 255;
70 	    bd.bdPalette.cpColors[1].rgb8Blue= 255;
71 	    bd.bdPalette.cpColors[1].rgb8Alpha= 255;
72 
73 	    /*  black */
74 	    bd.bdPalette.cpColors[2].rgb8Red= 0;
75 	    bd.bdPalette.cpColors[2].rgb8Green= 0;
76 	    bd.bdPalette.cpColors[2].rgb8Blue= 0;
77 	    bd.bdPalette.cpColors[2].rgb8Alpha= 255;
78 
79 	    memset( buffer, 0, bd.bdBufferLength );
80 
81 	    break;
82 
83 	default:
84 	    LDEB(colorEncoding); rval= -1; goto ready;
85 	}
86 
87     /* steal */
88     *bdOut= bd;
89     *pBufOut= buffer;
90 
91     buffer= (unsigned char *)0;
92     bmInitDescription( &bd );
93 
94   ready:
95 
96     bmCleanDescription( &bd );
97     if  ( buffer )
98 	{ free( buffer );	}
99 
100     return rval;
101     }
102 
103 /************************************************************************/
104 /*									*/
105 /*  Make an image containing just one RGB color.			*/
106 /*									*/
107 /************************************************************************/
108 
bmRGBImage(BitmapDescription * bdOut,unsigned char ** pBufOut,int colorEncoding,int r,int g,int b,int wide,int high)109 int bmRGBImage(			BitmapDescription *		bdOut,
110 				unsigned char **		pBufOut,
111 				int				colorEncoding,
112 				int				r,
113 				int				g,
114 				int				b,
115 				int				wide,
116 				int				high )
117     {
118     int				rval= 0;
119     BitmapDescription		bd;
120     unsigned char *		buffer= (unsigned char *)0;
121 
122     bmInitDescription( &bd );
123 
124     switch( colorEncoding )
125 	{
126 	case BMcoRGB8PALETTE:
127 
128 	    if  ( r < 0 || r > 255 )
129 		{ LDEB(r); rval= -1; goto ready;	}
130 	    if  ( g < 0 || g > 255 )
131 		{ LDEB(g); rval= -1; goto ready;	}
132 	    if  ( b < 0 || b > 255 )
133 		{ LDEB(b); rval= -1; goto ready;	}
134 
135 	    bd.bdPixelsWide= wide;
136 	    bd.bdPixelsHigh= high;
137 	    bd.bdHasAlpha= 0;
138 	    bd.bdUnit= BMunINCH;
139 	    bd.bdXResolution= 72;
140 	    bd.bdYResolution= 72;
141 
142 	    bd.bdBitsPerSample= 8;
143 	    bd.bdSamplesPerPixel= 3;
144 
145 	    if  ( bd.bdHasAlpha )
146 		{ bd.bdBitsPerPixel= 8;	}
147 	    else{ bd.bdBitsPerPixel= 4;	}
148 
149 	    bd.bdColorEncoding= BMcoRGB8PALETTE;
150 	    if  ( utilPaletteSetCount( &(bd.bdPalette), 3 ) )
151 		{ LDEB(3); rval= -1; goto ready;	}
152 
153 	    if  ( bmCalculateSizes( &bd ) )
154 		{ LDEB(1); rval= -1; goto ready;	}
155 
156 	    buffer= (unsigned char *)malloc( bd.bdBufferLength );
157 	    if  ( ! buffer )
158 		{ LXDEB(bd.bdBufferLength,buffer); rval= -1; goto ready; }
159 
160 	    /*  color */
161 	    bd.bdPalette.cpColors[0].rgb8Red= r;
162 	    bd.bdPalette.cpColors[0].rgb8Green= g;
163 	    bd.bdPalette.cpColors[0].rgb8Blue= b;
164 	    bd.bdPalette.cpColors[0].rgb8Alpha= 255;
165 
166 	    /*  white */
167 	    bd.bdPalette.cpColors[1].rgb8Red= 255;
168 	    bd.bdPalette.cpColors[1].rgb8Green= 255;
169 	    bd.bdPalette.cpColors[1].rgb8Blue= 255;
170 	    bd.bdPalette.cpColors[1].rgb8Alpha= 255;
171 
172 	    /*  black */
173 	    bd.bdPalette.cpColors[2].rgb8Red= 0;
174 	    bd.bdPalette.cpColors[2].rgb8Green= 0;
175 	    bd.bdPalette.cpColors[2].rgb8Blue= 0;
176 	    bd.bdPalette.cpColors[2].rgb8Alpha= 255;
177 
178 	    memset( buffer, 0, bd.bdBufferLength );
179 
180 	    break;
181 
182 	default:
183 	    LDEB(colorEncoding); rval= -1; goto ready;
184 	}
185 
186     /* steal */
187     *bdOut= bd;
188     *pBufOut= buffer;
189 
190     buffer= (unsigned char *)0;
191     bmInitDescription( &bd );
192 
193   ready:
194 
195     bmCleanDescription( &bd );
196     if  ( buffer )
197 	{ free( buffer );	}
198 
199     return rval;
200     }
201 
202 /************************************************************************/
203 /*									*/
204 /*  Make a solid white/black image.					*/
205 /*									*/
206 /************************************************************************/
207 
bmSetSolidWhite(RasterImage * ri)208 int bmSetSolidWhite(		RasterImage *	ri )
209     {
210     int			col;
211     BitmapDescription *	bd= &(ri->riDescription);
212 
213     switch( bd->bdColorEncoding )
214 	{
215 	case BMcoBLACKWHITE:
216 	    if  ( bd->bdHasAlpha )
217 		{
218 		switch( bd->bdBitsPerPixel )
219 		    {
220 		    case 2:
221 			memset( ri->riBytes, 0x55, bd->bdBufferLength );
222 			break;
223 		    case 4:
224 			memset( ri->riBytes, 0x33, bd->bdBufferLength );
225 			break;
226 		    case 8:
227 			memset( ri->riBytes, 0x0f, bd->bdBufferLength );
228 			break;
229 		    default:
230 			LLDEB(bd->bdBitsPerPixel,bd->bdHasAlpha);
231 			return -1;
232 		    }
233 		}
234 	    else{
235 		memset( ri->riBytes, 0x00, bd->bdBufferLength );
236 		}
237 	    break;
238 
239 	case BMcoWHITEBLACK:
240 	case BMcoRGB:
241 	    memset( ri->riBytes, 0xff, bd->bdBufferLength );
242 	    break;
243 
244 	case BMcoRGB8PALETTE:
245 	    if  ( bd->bdHasAlpha )
246 		{ LDEB(bd->bdHasAlpha); return -1;	}
247 
248 	    col= bmPaletteColor( bd, 255, 255, 255, 255 );
249 	    if  ( col < 0 )
250 		{ LLDEB(bd->bdPalette.cpColorCount,col); return -1;	}
251 
252 	    if  ( ! bd->bdHasAlpha && bd->bdBitsPerPixel == 8 )
253 		{
254 		memset( ri->riBytes, col, bd->bdBufferLength );
255 		return 0;
256 		}
257 	    if  ( ! bd->bdHasAlpha && bd->bdBitsPerPixel == 4 )
258 		{
259 		memset( ri->riBytes, col << 4 | col, bd->bdBufferLength );
260 		return 0;
261 		}
262 
263 	    LDEB(bd->bdColorEncoding); return -1;
264 	    break;
265 
266 	default:
267 	    LDEB(bd->bdColorEncoding); return -1;
268 	}
269 
270     return 0;
271     }
272 
bmSetSolidBlack(RasterImage * ri)273 int bmSetSolidBlack(		RasterImage *	ri )
274     {
275     int			col;
276     BitmapDescription *	bd= &(ri->riDescription);
277 
278     if  ( bd->bdHasAlpha )
279 	{ LDEB(bd->bdHasAlpha); return -1;	}
280 
281     switch( bd->bdColorEncoding )
282 	{
283 	case BMcoBLACKWHITE:
284 	    memset( ri->riBytes, 0xff, bd->bdBufferLength );
285 	    break;
286 
287 	case BMcoWHITEBLACK:
288 	case BMcoRGB:
289 	    memset( ri->riBytes, 0x00, bd->bdBufferLength );
290 	    break;
291 
292 	case BMcoRGB8PALETTE:
293 	    col= bmPaletteColor( bd, 0, 0, 0, 255 );
294 	    if  ( col < 0 )
295 		{ LLDEB(bd->bdPalette.cpColorCount,col); return -1;	}
296 
297 	    if  ( ! bd->bdHasAlpha && bd->bdBitsPerPixel == 8 )
298 		{
299 		memset( ri->riBytes, col, bd->bdBufferLength );
300 		return 0;
301 		}
302 	    if  ( ! bd->bdHasAlpha && bd->bdBitsPerPixel == 4 )
303 		{
304 		memset( ri->riBytes, col << 4 | col, bd->bdBufferLength );
305 		return 0;
306 		}
307 
308 	    LDEB(bd->bdColorEncoding); return -1;
309 	    break;
310 
311 	default:
312 	    LDEB(bd->bdColorEncoding); return -1;
313 	}
314 
315     return 0;
316     }
317