Move the sources to trunk
[opencv] / otherlibs / VlGrFmts / utils.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
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.
25 //
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.
28 //
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.
39 //
40 //M*/
41
42 #include <assert.h>
43 #include "utils.h"
44
45 #define  SCALE  8
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)
49
50 void CvtBGRToGray( const uchar* bgr, uchar* gray, int len )
51 {
52     int i;
53     for( i = 0; i < len; i++, bgr += 3 )
54     {
55         int t = descale( bgr[0]*cB + bgr[1]*cG + bgr[2]*cR, SCALE );
56         gray[i] = saturate( t );
57     }
58 }
59
60
61 void CvtRGBToGray( const uchar* rgb, uchar* gray, int len )
62 {
63     int i;
64     for( i = 0; i < len; i++, rgb += 3 )
65     {
66         int t = descale( rgb[2]*cB + rgb[1]*cG + rgb[0]*cR, SCALE );
67         gray[i] = saturate( t );
68     }
69 }
70
71
72 void CvtRGBToBGR( const uchar* rgb, uchar* bgr, int len )
73 {
74     int i;
75
76     len *= 3;
77     for( i = 0; i < len; i++, rgb += 3, bgr += 3 )
78     {
79         uchar b, g, r;
80         r = rgb[0];
81         g = rgb[1];
82         b = rgb[2];
83         bgr[0] = b;
84         bgr[1] = g;
85         bgr[2] = r;
86     }
87 }
88
89
90 void CvtPaletteToGray( const PaletteEntry* palette, uchar* grayPalette, int entries )
91 {
92     int i;
93     for( i = 0; i < entries; i++ )
94     {
95         CvtBGRToGray( (uchar*)(palette + i), grayPalette + i, 1 );
96     }
97 }
98
99
100 void  FillGrayPalette( PaletteEntry* palette, int bpp )
101 {
102     int i, length = 1 << bpp, shift = 8 - bpp;
103
104     assert( shift >= 0 );
105
106     for( i = 0; i < length; i++ )
107     {
108         palette[i].b = palette[i].g = palette[i].r = (uchar)(i << shift);
109         palette[i].a = 0;
110     }
111 }
112
113 void CalcShifts( uchar* data, uchar* line_end, int width3,
114                  int y, int height, int& x_shift3, int& y_shift )
115 {
116     int x3 = data - (line_end - width3);
117     int new_x3 = x3 + x_shift3;
118     
119     y_shift = new_x3 / width3;
120     new_x3 -= y_shift * width3;
121
122     x_shift3 = new_x3 - x3;
123
124     if( y + y_shift >= height )
125     {
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;
130     }
131 }
132
133
134 uchar* FillUni( uchar* data, uchar* line_end, int width3, int delta, 
135                        int x_shift3, int y_shift, PaletteEntry clr, int color )
136 {
137     int new_x3 = (data - (line_end - width3)) + x_shift3;
138     uchar gr_val = 0;
139
140     if( y_shift < 0 ) // calculate y_shift
141     {
142         y_shift = new_x3 / width3;
143         new_x3 -= y_shift * width3;
144     }
145
146     if( !color )
147     {
148         CvtBGRToGray( (uchar*)&clr, &gr_val, 1 );
149     }
150
151     for(;;)
152     {
153         if( color )
154         {
155             for( uchar* end = y_shift == 0 ? (line_end - width3) + new_x3 : line_end;
156                  data < end; data += 3 )
157             {
158                 WRITE_PIX( data, clr );
159             }
160         }
161         else
162         {
163             for( uchar* end = y_shift == 0 ? (line_end - width3) + new_x3 : line_end;
164                  data < end; data++ )
165             {
166                 *data = gr_val;
167             }
168         }
169         if( y_shift == 0 ) break;
170         data = line_end + delta;
171         line_end = data + width3;
172     }
173     return data;
174 }
175
176
177 uchar* FillRow32( uchar* data, uchar* src, int len )
178 {
179     for( uchar* end = data + len*3; (data += 3) < end; src += 4 )
180     {
181         *((PaletteEntry*)(data-3)) = *((PaletteEntry*)src);
182     }
183     PaletteEntry clr = *((PaletteEntry*)src);
184     WRITE_PIX( data - 3, clr );
185     return data;
186 }
187
188
189 uchar* FillColorRow8( uchar* data, uchar* indices, int len, PaletteEntry* palette )
190 {
191     uchar* end = data + len*3;
192     while( (data += 3) < end )
193     {
194         *((PaletteEntry*)(data-3)) = palette[*indices++];
195     }
196     PaletteEntry clr = palette[indices[0]];
197     WRITE_PIX( data - 3, clr );
198     return data;
199 }
200                        
201
202 uchar* FillGrayRow8( uchar* data, uchar* indices, int len, uchar* palette )
203 {
204     int i;
205     for( i = 0; i < len; i++ )
206     {
207         data[i] = palette[indices[i]];
208     }
209     return data + len;
210 }
211
212
213 uchar* FillColorRow4( uchar* data, uchar* indices, int len, PaletteEntry* palette )
214 {
215     uchar* end = data + len*3;
216
217     while( (data += 6) < end )
218     {
219         int idx = *indices++;
220         *((PaletteEntry*)(data-6)) = palette[idx >> 4];
221         *((PaletteEntry*)(data-3)) = palette[idx & 15];
222     }
223
224     int idx = indices[0];
225     PaletteEntry clr = palette[idx >> 4];
226     WRITE_PIX( data - 6, clr );
227
228     if( data == end )
229     {
230         clr = palette[idx & 15];
231         WRITE_PIX( data - 3, clr );
232     }
233     return end;
234 }
235
236
237 uchar* FillGrayRow4( uchar* data, uchar* indices, int len, uchar* palette )
238 {
239     uchar* end = data + len;
240     while( (data += 2) < end )
241     {
242         int idx = *indices++;
243         data[-2] = palette[idx >> 4];
244         data[-1] = palette[idx & 15];
245     }
246
247     int idx = indices[0];
248     uchar clr = palette[idx >> 4];
249     data[-2] = clr;
250
251     if( data == end )
252     {
253         clr = palette[idx & 15];
254         data[-1] = clr;
255     }
256     return end;
257 }
258
259
260 uchar* FillColorRow1( uchar* data, uchar* indices, int len, PaletteEntry* palette )
261 {
262     uchar* end = data + len*3;
263
264     while( (data += 24) < end )
265     {
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];
275     }
276     
277     int idx = indices[0] << 24;
278     for( data -= 24; data < end; data += 3, idx += idx )
279     {
280         PaletteEntry clr = palette[idx < 0];
281         WRITE_PIX( data, clr );
282     }
283
284     return data;
285 }
286
287
288 uchar* FillGrayRow1( uchar* data, uchar* indices, int len, uchar* palette )
289 {
290     uchar* end = data + len;
291
292     while( (data += 8) < end )
293     {
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];
303     }
304     
305     int idx = indices[0] << 24;
306     for( data -= 8; data < end; data++, idx += idx )
307     {
308         data[0] = palette[idx < 0];
309     }
310
311     return data;
312 }
313