1 #   include	"bitmapConfig.h"
2 
3 #   include	"bmRender.h"
4 #   include	<stdlib.h>
5 #   include	<appDebugon.h>
6 
7 # define N(r,g,b) ( 6* 6* ( (r)/ 43 )+ 6* ( (g)/ 43 )+ ( (b) / 43 ) )
8 
9 /************************************************************************/
10 /*									*/
11 /*  Map an image to the so called web safe palette.			*/
12 /*									*/
13 /************************************************************************/
14 
bmWebSafeCleanupAllocator(ColorAllocator * ca)15 static void bmWebSafeCleanupAllocator(	ColorAllocator *	ca )
16     {
17     return;
18     }
19 
20 /************************************************************************/
21 /*									*/
22 /*  Set resources in a grayscale allocator, depending on the number of	*/
23 /*  bits.								*/
24 /*									*/
25 /************************************************************************/
26 
bmWebSafeSetAllocator(ColorAllocator * ca,int bitsPerPixel,SystemAllocator sysAllocator)27 static int bmWebSafeSetAllocator(	ColorAllocator *	ca,
28 					int			bitsPerPixel,
29 					SystemAllocator		sysAllocator )
30     {
31     if  ( bitsPerPixel != 8 )
32 	{ LDEB(bitsPerPixel); return -1;	}
33 
34     ca->caSystemAllocator= sysAllocator;
35     ca->caSystemCleanup= bmWebSafeCleanupAllocator;
36     ca->caAllocationType= CA_ALLOCATOR;
37 
38     return 0;
39     }
40 
41 /************************************************************************/
42 /*									*/
43 /*  Allocate a 'Web Safe' color.					*/
44 /*									*/
45 /************************************************************************/
46 
bmToWebSafeAllocateColor(AllocatorColor * ac,ColorAllocator * ca,unsigned int r,unsigned int g,unsigned int b)47 static int bmToWebSafeAllocateColor(	AllocatorColor *	ac,
48 					ColorAllocator *	ca,
49 					unsigned int		r,
50 					unsigned int		g,
51 					unsigned int		b )
52     {
53     ac->acRed= 257* r;
54     ac->acGreen= 257* g;
55     ac->acBlue= 257* b;
56     ac->acColorNumber= N( r, g, b );
57 
58     return 0;
59     }
60 
61 /************************************************************************/
62 /*									*/
63 /*  Convert an image to the web safe palette.				*/
64 /*									*/
65 /*  1)  Check input format.						*/
66 /*  2)	Derive properties of output bitmap from input.			*/
67 /*  4)	Allocate output buffer.						*/
68 /*  5)	Fill it.							*/
69 /*  6)	Return outputs.							*/
70 /*  7)	Cleanup.							*/
71 /*									*/
72 /************************************************************************/
73 
bmToWebSafe(RasterImage * riOut,const RasterImage * riIn,int ignoredInt)74 int bmToWebSafe(	RasterImage *			riOut,
75 			const RasterImage *		riIn,
76 			int				ignoredInt )
77     {
78     const BitmapDescription *	bdIn= &(riIn->riDescription);
79     int				rval= 0;
80 
81     RasterImage			ri;
82 
83     ColorAllocator		ca;
84 
85     int				bitmapUnit= 0;
86     int				swapBitmapBytes= 0;
87     int				swapBitmapBits= 0;
88     const int			dither= 0;
89 
90     bmInitRasterImage( &ri );
91     bmInitColorAllocator( &ca );
92 
93     /*  1  */
94     switch( bdIn->bdColorEncoding )
95 	{
96 	case BMcoRGB:
97 	case BMcoRGB8PALETTE:
98 	    break;
99 
100 	case BMcoBLACKWHITE:
101 	case BMcoWHITEBLACK:
102 	default:
103 	    LDEB(bdIn->bdColorEncoding);
104 	    rval= -1; goto ready;
105 	}
106 
107     switch( bdIn->bdBitsPerSample )
108 	{
109 	case 8:
110 	    break;
111 	default:
112 	    LDEB(bdIn->bdBitsPerSample);
113 	    rval= -1; goto ready;
114 	}
115 
116     /*  2  */
117     if  ( bmCopyDescription( &(ri.riDescription), bdIn ) )
118 	{ LDEB(1); rval= -1; goto ready;	}
119 
120     ri.riDescription.bdColorEncoding= BMcoRGB8PALETTE;
121     ri.riDescription.bdBitsPerSample= 8;
122     ri.riDescription.bdBitsPerPixel= 8;
123 
124     if  ( utilPaletteSetCount( &(ri.riDescription.bdPalette), 216 ) )
125 	{ LDEB(216); rval= -1; goto ready;	}
126 
127     {
128     int		r, g, b;
129 
130     for ( r= 0; r < 256; r += 51 )
131     for ( g= 0; g < 256; g += 51 )
132     for ( b= 0; b < 256; b += 51 )
133     	{
134 	int		n= N( r, g, b );
135 	RGB8Color *	rgb8= &(ri.riDescription.bdPalette.cpColors[n]);
136 
137 	rgb8->rgb8Red= r;
138 	rgb8->rgb8Green= g;
139 	rgb8->rgb8Blue= b;
140 	rgb8->rgb8Alpha= 255;
141 	}
142     }
143 
144     if  ( bmCalculateSizes( &(ri.riDescription) ) )
145 	{ LDEB(1); rval= -1; goto ready;	}
146 
147     /*  3  */
148     if  ( bmWebSafeSetAllocator( &ca, ri.riDescription.bdBitsPerPixel,
149 						    bmToWebSafeAllocateColor ) )
150 	{ LDEB(ri.riDescription.bdBitsPerPixel); rval= -1; goto ready; }
151 
152     /*  4  */
153     if  ( bmAllocateBuffer( &ri ) )
154 	{
155 	LLDEB(ri.riDescription.bdBufferLength,ri.riBytes);
156 	rval= -1; goto ready;
157 	}
158 
159     /*  5  */
160     if  ( bmFillImage( &ca, bitmapUnit, swapBitmapBytes, swapBitmapBits,
161 			dither, ri.riBytes, &(ri.riDescription),
162 			riIn, (const DocumentRectangle *)0 ) )
163 	{ LDEB(1); rval= -1; goto ready;	}
164 
165     /* steal */
166     *riOut= ri; bmInitRasterImage( &ri );
167 
168   ready:
169 
170     /*  7  */
171     bmCleanRasterImage( &ri );
172     bmCleanColorAllocator( &ca );
173 
174     return rval;
175     }
176