1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
46 #define cR (int)(0.299*(1 << SCALE) + 0.5)
47 #define cG (int)(0.587*(1 << SCALE) + 0.5)
48 #define cB (int)(0.114*(1 << SCALE) + 0.5)
50 void CvtBGRToGray( const uchar* bgr, uchar* gray, int len )
53 for( i = 0; i < len; i++, bgr += 3 )
55 int t = descale( bgr[0]*cB + bgr[1]*cG + bgr[2]*cR, SCALE );
56 gray[i] = saturate( t );
61 void CvtRGBToGray( const uchar* rgb, uchar* gray, int len )
64 for( i = 0; i < len; i++, rgb += 3 )
66 int t = descale( rgb[2]*cB + rgb[1]*cG + rgb[0]*cR, SCALE );
67 gray[i] = saturate( t );
72 void CvtRGBToBGR( const uchar* rgb, uchar* bgr, int len )
77 for( i = 0; i < len; i++, rgb += 3, bgr += 3 )
90 void CvtPaletteToGray( const PaletteEntry* palette, uchar* grayPalette, int entries )
93 for( i = 0; i < entries; i++ )
95 CvtBGRToGray( (uchar*)(palette + i), grayPalette + i, 1 );
100 void FillGrayPalette( PaletteEntry* palette, int bpp )
102 int i, length = 1 << bpp, shift = 8 - bpp;
104 assert( shift >= 0 );
106 for( i = 0; i < length; i++ )
108 palette[i].b = palette[i].g = palette[i].r = (uchar)(i << shift);
113 void CalcShifts( uchar* data, uchar* line_end, int width3,
114 int y, int height, int& x_shift3, int& y_shift )
116 int x3 = data - (line_end - width3);
117 int new_x3 = x3 + x_shift3;
119 y_shift = new_x3 / width3;
120 new_x3 -= y_shift * width3;
122 x_shift3 = new_x3 - x3;
124 if( y + y_shift >= height )
126 if( y + y_shift > height )
127 y_shift = height - y;
128 if( width3 - (line_end - data) + x_shift3 > 0 )
129 x_shift3 = (line_end - data) - width3;
134 uchar* FillUni( uchar* data, uchar* line_end, int width3, int delta,
135 int x_shift3, int y_shift, PaletteEntry clr, int color )
137 int new_x3 = (data - (line_end - width3)) + x_shift3;
140 if( y_shift < 0 ) // calculate y_shift
142 y_shift = new_x3 / width3;
143 new_x3 -= y_shift * width3;
148 CvtBGRToGray( (uchar*)&clr, &gr_val, 1 );
155 for( uchar* end = y_shift == 0 ? (line_end - width3) + new_x3 : line_end;
156 data < end; data += 3 )
158 WRITE_PIX( data, clr );
163 for( uchar* end = y_shift == 0 ? (line_end - width3) + new_x3 : line_end;
169 if( y_shift == 0 ) break;
170 data = line_end + delta;
171 line_end = data + width3;
177 uchar* FillRow32( uchar* data, uchar* src, int len )
179 for( uchar* end = data + len*3; (data += 3) < end; src += 4 )
181 *((PaletteEntry*)(data-3)) = *((PaletteEntry*)src);
183 PaletteEntry clr = *((PaletteEntry*)src);
184 WRITE_PIX( data - 3, clr );
189 uchar* FillColorRow8( uchar* data, uchar* indices, int len, PaletteEntry* palette )
191 uchar* end = data + len*3;
192 while( (data += 3) < end )
194 *((PaletteEntry*)(data-3)) = palette[*indices++];
196 PaletteEntry clr = palette[indices[0]];
197 WRITE_PIX( data - 3, clr );
202 uchar* FillGrayRow8( uchar* data, uchar* indices, int len, uchar* palette )
205 for( i = 0; i < len; i++ )
207 data[i] = palette[indices[i]];
213 uchar* FillColorRow4( uchar* data, uchar* indices, int len, PaletteEntry* palette )
215 uchar* end = data + len*3;
217 while( (data += 6) < end )
219 int idx = *indices++;
220 *((PaletteEntry*)(data-6)) = palette[idx >> 4];
221 *((PaletteEntry*)(data-3)) = palette[idx & 15];
224 int idx = indices[0];
225 PaletteEntry clr = palette[idx >> 4];
226 WRITE_PIX( data - 6, clr );
230 clr = palette[idx & 15];
231 WRITE_PIX( data - 3, clr );
237 uchar* FillGrayRow4( uchar* data, uchar* indices, int len, uchar* palette )
239 uchar* end = data + len;
240 while( (data += 2) < end )
242 int idx = *indices++;
243 data[-2] = palette[idx >> 4];
244 data[-1] = palette[idx & 15];
247 int idx = indices[0];
248 uchar clr = palette[idx >> 4];
253 clr = palette[idx & 15];
260 uchar* FillColorRow1( uchar* data, uchar* indices, int len, PaletteEntry* palette )
262 uchar* end = data + len*3;
264 while( (data += 24) < end )
266 int idx = *indices++;
267 *((PaletteEntry*)(data - 24)) = palette[(idx & 128) != 0];
268 *((PaletteEntry*)(data - 21)) = palette[(idx & 64) != 0];
269 *((PaletteEntry*)(data - 18)) = palette[(idx & 32) != 0];
270 *((PaletteEntry*)(data - 15)) = palette[(idx & 16) != 0];
271 *((PaletteEntry*)(data - 12)) = palette[(idx & 8) != 0];
272 *((PaletteEntry*)(data - 9)) = palette[(idx & 4) != 0];
273 *((PaletteEntry*)(data - 6)) = palette[(idx & 2) != 0];
274 *((PaletteEntry*)(data - 3)) = palette[(idx & 1) != 0];
277 int idx = indices[0] << 24;
278 for( data -= 24; data < end; data += 3, idx += idx )
280 PaletteEntry clr = palette[idx < 0];
281 WRITE_PIX( data, clr );
288 uchar* FillGrayRow1( uchar* data, uchar* indices, int len, uchar* palette )
290 uchar* end = data + len;
292 while( (data += 8) < end )
294 int idx = *indices++;
295 *((uchar*)(data - 8)) = palette[(idx & 128) != 0];
296 *((uchar*)(data - 7)) = palette[(idx & 64) != 0];
297 *((uchar*)(data - 6)) = palette[(idx & 32) != 0];
298 *((uchar*)(data - 5)) = palette[(idx & 16) != 0];
299 *((uchar*)(data - 4)) = palette[(idx & 8) != 0];
300 *((uchar*)(data - 3)) = palette[(idx & 4) != 0];
301 *((uchar*)(data - 2)) = palette[(idx & 2) != 0];
302 *((uchar*)(data - 1)) = palette[(idx & 1) != 0];
305 int idx = indices[0] << 24;
306 for( data -= 8; data < end; data++, idx += idx )
308 data[0] = palette[idx < 0];