1 #   include	"bitmapConfig.h"
2 
3 #   include	"bmintern.h"
4 #   include	"bmgetrow.h"
5 #   include	"bmputrow.h"
6 #   include	<appDebugon.h>
7 
8 /************************************************************************/
9 /*									*/
10 /*  Scaling, Dithering, Antialiassing.					*/
11 /*									*/
12 /************************************************************************/
13 
bmBuildImage(FillJob * fj,unsigned char * bufferOut,const BitmapDescription * bdOut,const RasterImage * riIn,const DocumentRectangle * drSel,GetSourceRow getSource,PutScreenRow putRow)14 static int bmBuildImage(	FillJob *			fj,
15 				unsigned char *			bufferOut,
16 				const BitmapDescription *	bdOut,
17 				const RasterImage *		riIn,
18 				const DocumentRectangle *	drSel,
19 				GetSourceRow			getSource,
20 				PutScreenRow			putRow )
21     {
22     int				rval= 0;
23 
24     int				rowOut;
25     int				rowIn;
26 
27     const unsigned char *	from;
28     unsigned char *		to;
29 
30     int				toWide= bdOut->bdPixelsWide;
31     int				toHigh= bdOut->bdPixelsHigh;
32     int				frWide;
33     int				frHigh;
34 
35     int				e;
36     int				d2;
37     int				e2;
38 
39     DocumentRectangle		drAll;
40     RasterImage			riSel;
41 
42     bmInitRasterImage( &riSel );
43 
44     drAll.drX0= 0;
45     drAll.drY0= 0;
46     drAll.drX1= riIn->riDescription.bdPixelsWide- 1;
47     drAll.drY1= riIn->riDescription.bdPixelsHigh- 1;
48 
49     if  ( ! drSel )
50 	{ drSel= &drAll;	}
51     else{
52 	if  ( drSel->drX0 != drAll.drX0	||
53 	      drSel->drY0 != drAll.drY0	||
54 	      drSel->drX1 != drAll.drX1	||
55 	      drSel->drY1 != drAll.drY1	)
56 	    {
57 	    /*  Could be done MUCH more efficiently as part of the	*/
58 	    /*  regular approach.					*/
59 
60 	    if  ( bmSelect( &riSel, riIn, drSel ) )
61 		{ RECTDEB(drSel); rval= -1; goto ready;	}
62 
63 	    riIn= &riSel;
64 	    }
65 	}
66 
67     frWide= riIn->riDescription.bdPixelsWide;
68     frHigh= riIn->riDescription.bdPixelsHigh;
69 
70     if  ( toHigh <= frHigh )
71 	{
72 	e2= 2* frHigh;
73 	d2= 2* toHigh;
74 	e= e2- toHigh;
75 
76 	bmInitColorRow( fj->fjNextCor+ 1, toWide );
77 
78 	rowIn= 0;
79 	for ( rowOut= 0; rowOut < toHigh; rowOut++ )
80 	    {
81 	    bmInitColorRow( fj->fjThisRow+ 1, frWide );
82 	    to= bufferOut+ rowOut* bdOut->bdBytesPerRow;
83 
84 	    while( e >= 0 )
85 		{
86 		from= riIn->riBytes+ rowIn* riIn->riDescription.bdBytesPerRow;
87 
88 		(*getSource)( fj->fjThisRow+ 1, 0, from, 0,
89 					    riIn->riDescription.bdPixelsWide,
90 					    &(riIn->riDescription) );
91 
92 		rowIn++; e -= d2;
93 		}
94 
95 	    e += e2;
96 
97 	    if  ( (*putRow)( to, fj, fj->fjThisRow+ 1 ) )
98 		{ LDEB(rowOut); rval= -1; goto ready;	}
99 	    }
100 	}
101     else{
102 	e2= 2* toHigh;
103 	d2= 2* frHigh;
104 	e= e2- frHigh;
105 
106 	rowOut= 0;
107 	for ( rowIn= 0; rowIn < frHigh; rowIn++ )
108 	    {
109 	    from= riIn->riBytes+ rowIn* riIn->riDescription.bdBytesPerRow;
110 
111 	    bmInitColorRow( fj->fjThisRow+ 1, frWide );
112 	    (*getSource)( fj->fjThisRow+ 1, 0, from, 0,
113 					    riIn->riDescription.bdPixelsWide,
114 					    &(riIn->riDescription) );
115 
116 	    while( e >= 0 )
117 		{
118 		to= bufferOut+ rowOut* bdOut->bdBytesPerRow;
119 
120 		if  ( (*putRow)( to, fj, fj->fjThisRow+ 1 ) )
121 		    { LDEB(rowOut); rval= -1; goto ready; }
122 
123 		e -= d2; rowOut++;
124 		}
125 
126 	    e += e2;
127 	    }
128 	}
129 
130   ready:
131 
132     bmCleanRasterImage( &riSel );
133 
134     return rval;
135     }
136 
bmFillImage(ColorAllocator * ca,int swapBitmapUnit,int swapBitmapBytes,int swapBitmapBits,int dither,unsigned char * bufferOut,const BitmapDescription * bdOut,const RasterImage * riIn,const DocumentRectangle * drSel)137 int bmFillImage(	ColorAllocator *		ca,
138 			int				swapBitmapUnit,
139 			int				swapBitmapBytes,
140 			int				swapBitmapBits,
141 			int				dither,
142 			unsigned char *			bufferOut,
143 			const BitmapDescription *	bdOut,
144 			const RasterImage *		riIn,
145 			const DocumentRectangle *	drSel )
146     {
147     const BitmapDescription *	bdIn= &(riIn->riDescription);
148     int				rval= 0;
149 
150     PutScreenRow		putRow= (PutScreenRow)0;
151     GetSourceRow		getRow= (GetSourceRow)0;
152     int				scratchSize= 0;
153     int				pixelsWideIn= bdIn->bdPixelsWide;
154 
155     FillJob			fj;
156 
157     bmInitFillJob( &fj );
158 
159     if  ( drSel )
160 	{ pixelsWideIn= drSel->drX1- drSel->drX0+ 1;	}
161 
162     /*  1  */
163     if  ( bmGetPutRow( &putRow, &scratchSize, ca,
164 					    swapBitmapUnit, swapBitmapBytes,
165 					    swapBitmapBits, bdOut ) )
166 	{ LDEB(1); rval= -1; goto ready; }
167 
168     if  ( bmGetGetRow( &getRow, bdIn ) )
169 	{ LDEB(1); return -1; }
170 
171     if  ( bmSetFillJob( &fj, ca,
172 			    pixelsWideIn, bdOut->bdPixelsWide,
173 			    scratchSize, dither ) )
174 	{ LDEB(scratchSize); rval= -1; goto ready;	}
175 
176     if  ( bmBuildImage( &fj, bufferOut, bdOut, riIn, drSel, getRow, putRow ) )
177 	{ LDEB(1); rval= -1; goto ready;	}
178 
179   ready:
180 
181     bmCleanFillJob( &fj );
182 
183     return rval;
184     }
185 
186 /************************************************************************/
187 /*									*/
188 /*  Make a color allocator for a certain kind of image.			*/
189 /*									*/
190 /************************************************************************/
191 
bmSetColorAllocatorForImage(ColorAllocator * ca,const BitmapDescription * bd)192 int bmSetColorAllocatorForImage(	ColorAllocator *		ca,
193 					const BitmapDescription *	bd )
194     {
195     switch( bd->bdColorEncoding )
196 	{
197 	case BMcoRGB8PALETTE:
198 	    if  ( bmSetColorAllocatorForPaletteImage( ca, bd ) )
199 		{ LDEB(1); return -1;	}
200 	    return 0;
201 
202 	case BMcoBLACKWHITE:
203 	    if  ( bmSetColorAllocatorForBWImage( ca, bd ) )
204 		{ LDEB(1); return -1;	}
205 	    return 0;
206 
207 	case BMcoWHITEBLACK:
208 	    if  ( bmSetColorAllocatorForWBImage( ca, bd ) )
209 		{ LDEB(1); return -1;	}
210 	    return 0;
211 
212 	case BMcoRGB:
213 	    switch( bd->bdBitsPerSample )
214 		{
215 		case 8:
216 		    bmSetCalculatedShifts( ca, 0xff0000, 0x00ff00, 0x0000ff );
217 		    ca->caAllocationType= CA_CALCULATED;
218 		    return 0;
219 
220 		default:
221 		    LLDEB(bd->bdColorEncoding,bd->bdBitsPerSample);
222 		    return -1;
223 		}
224 
225 	default:
226 	    LDEB(bd->bdColorEncoding); return -1;
227 	}
228     }
229 
230