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 ((1 << SCALE) - cR - cG)
50 void icvCvt_BGR2Gray_8u_C3C1R( const uchar* rgb, int rgb_step,
51 uchar* gray, int gray_step,
52 CvSize size, int _swap_rb )
55 int swap_rb = _swap_rb ? 2 : 0;
56 for( ; size.height--; gray += gray_step )
58 for( i = 0; i < size.width; i++, rgb += 3 )
60 int t = descale( rgb[swap_rb]*cB + rgb[1]*cG + rgb[swap_rb^2]*cR, SCALE );
64 rgb += rgb_step - size.width*3;
69 void icvCvt_BGR2Gray_16u_C3C1R( const ushort* rgb, int rgb_step,
70 ushort* gray, int gray_step,
71 CvSize size, int _swap_rb )
74 int swap_rb = _swap_rb ? 2 : 0;
75 for( ; size.height--; gray += gray_step )
77 for( i = 0; i < size.width; i++, rgb += 3 )
79 int t = descale( rgb[swap_rb]*cB + rgb[1]*cG + rgb[swap_rb^2]*cR, SCALE );
83 rgb += rgb_step - size.width*3;
88 void icvCvt_BGRA2Gray_8u_C4C1R( const uchar* rgba, int rgba_step,
89 uchar* gray, int gray_step,
90 CvSize size, int _swap_rb )
93 int swap_rb = _swap_rb ? 2 : 0;
94 for( ; size.height--; gray += gray_step )
96 for( i = 0; i < size.width; i++, rgba += 4 )
98 int t = descale( rgba[swap_rb]*cB + rgba[1]*cG + rgba[swap_rb^2]*cR, SCALE );
102 rgba += rgba_step - size.width*4;
107 void icvCvt_Gray2BGR_8u_C1C3R( const uchar* gray, int gray_step,
108 uchar* bgr, int bgr_step, CvSize size )
111 for( ; size.height--; gray += gray_step )
113 for( i = 0; i < size.width; i++, bgr += 3 )
115 bgr[0] = bgr[1] = bgr[2] = gray[i];
117 bgr += bgr_step - size.width*3;
122 void icvCvt_BGRA2BGR_8u_C4C3R( const uchar* bgra, int bgra_step,
123 uchar* bgr, int bgr_step,
124 CvSize size, int _swap_rb )
127 int swap_rb = _swap_rb ? 2 : 0;
128 for( ; size.height--; )
130 for( i = 0; i < size.width; i++, bgr += 3, bgra += 4 )
132 uchar t0 = bgra[swap_rb], t1 = bgra[1];
133 bgr[0] = t0; bgr[1] = t1;
134 t0 = bgra[swap_rb^2]; bgr[2] = t0;
136 bgr += bgr_step - size.width*3;
137 bgra += bgra_step - size.width*4;
142 void icvCvt_BGRA2RGBA_8u_C4R( const uchar* bgra, int bgra_step,
143 uchar* rgba, int rgba_step, CvSize size )
146 for( ; size.height--; )
148 for( i = 0; i < size.width; i++, bgra += 4, rgba += 4 )
150 uchar t0 = bgra[0], t1 = bgra[1];
151 uchar t2 = bgra[2], t3 = bgra[3];
152 rgba[0] = t2; rgba[1] = t1;
153 rgba[2] = t0; rgba[3] = t3;
155 bgra += bgra_step - size.width*4;
156 rgba += rgba_step - size.width*4;
161 void icvCvt_BGR2RGB_8u_C3R( const uchar* bgr, int bgr_step,
162 uchar* rgb, int rgb_step, CvSize size )
165 for( ; size.height--; )
167 for( i = 0; i < size.width; i++, bgr += 3, rgb += 3 )
169 uchar t0 = bgr[0], t1 = bgr[1], t2 = bgr[2];
170 rgb[2] = t0; rgb[1] = t1; rgb[0] = t2;
172 bgr += bgr_step - size.width*3;
173 rgb += rgb_step - size.width*3;
178 void icvCvt_BGR2RGB_16u_C3R( const ushort* bgr, int bgr_step,
179 ushort* rgb, int rgb_step, CvSize size )
182 for( ; size.height--; )
184 for( i = 0; i < size.width; i++, bgr += 3, rgb += 3 )
186 ushort t0 = bgr[0], t1 = bgr[1], t2 = bgr[2];
187 rgb[2] = t0; rgb[1] = t1; rgb[0] = t2;
189 bgr += bgr_step - size.width*3;
190 rgb += rgb_step - size.width*3;
195 typedef unsigned short ushort;
197 void icvCvt_BGR5552Gray_8u_C2C1R( const uchar* bgr555, int bgr555_step,
198 uchar* gray, int gray_step, CvSize size )
201 for( ; size.height--; gray += gray_step, bgr555 += bgr555_step )
203 for( i = 0; i < size.width; i++ )
205 int t = descale( ((((ushort*)bgr555)[i] << 3) & 0xf8)*cB +
206 ((((ushort*)bgr555)[i] >> 2) & 0xf8)*cG +
207 ((((ushort*)bgr555)[i] >> 7) & 0xf8)*cR, SCALE );
214 void icvCvt_BGR5652Gray_8u_C2C1R( const uchar* bgr565, int bgr565_step,
215 uchar* gray, int gray_step, CvSize size )
218 for( ; size.height--; gray += gray_step, bgr565 += bgr565_step )
220 for( i = 0; i < size.width; i++ )
222 int t = descale( ((((ushort*)bgr565)[i] << 3) & 0xf8)*cB +
223 ((((ushort*)bgr565)[i] >> 3) & 0xfc)*cG +
224 ((((ushort*)bgr565)[i] >> 8) & 0xf8)*cR, SCALE );
231 void icvCvt_BGR5552BGR_8u_C2C3R( const uchar* bgr555, int bgr555_step,
232 uchar* bgr, int bgr_step, CvSize size )
235 for( ; size.height--; bgr555 += bgr555_step )
237 for( i = 0; i < size.width; i++, bgr += 3 )
239 int t0 = (((ushort*)bgr555)[i] << 3) & 0xf8;
240 int t1 = (((ushort*)bgr555)[i] >> 2) & 0xf8;
241 int t2 = (((ushort*)bgr555)[i] >> 7) & 0xf8;
242 bgr[0] = (uchar)t0; bgr[1] = (uchar)t1; bgr[2] = (uchar)t2;
244 bgr += bgr_step - size.width*3;
249 void icvCvt_BGR5652BGR_8u_C2C3R( const uchar* bgr565, int bgr565_step,
250 uchar* bgr, int bgr_step, CvSize size )
253 for( ; size.height--; bgr565 += bgr565_step )
255 for( i = 0; i < size.width; i++, bgr += 3 )
257 int t0 = (((ushort*)bgr565)[i] << 3) & 0xf8;
258 int t1 = (((ushort*)bgr565)[i] >> 3) & 0xfc;
259 int t2 = (((ushort*)bgr565)[i] >> 8) & 0xf8;
260 bgr[0] = (uchar)t0; bgr[1] = (uchar)t1; bgr[2] = (uchar)t2;
262 bgr += bgr_step - size.width*3;
267 void icvCvt_CMYK2BGR_8u_C4C3R( const uchar* cmyk, int cmyk_step,
268 uchar* bgr, int bgr_step, CvSize size )
271 for( ; size.height--; )
273 for( i = 0; i < size.width; i++, bgr += 3, cmyk += 4 )
275 int c = cmyk[0], m = cmyk[1], y = cmyk[2], k = cmyk[3];
276 c = k - ((255 - c)*k>>8);
277 m = k - ((255 - m)*k>>8);
278 y = k - ((255 - y)*k>>8);
279 bgr[2] = (uchar)c; bgr[1] = (uchar)m; bgr[0] = (uchar)y;
281 bgr += bgr_step - size.width*3;
282 cmyk += cmyk_step - size.width*4;
287 void icvCvt_CMYK2Gray_8u_C4C1R( const uchar* cmyk, int cmyk_step,
288 uchar* gray, int gray_step, CvSize size )
291 for( ; size.height--; )
293 for( i = 0; i < size.width; i++, cmyk += 4 )
295 int c = cmyk[0], m = cmyk[1], y = cmyk[2], k = cmyk[3];
296 c = k - ((255 - c)*k>>8);
297 m = k - ((255 - m)*k>>8);
298 y = k - ((255 - y)*k>>8);
299 int t = descale( y*cB + m*cG + c*cR, SCALE );
303 cmyk += cmyk_step - size.width*4;
308 void CvtPaletteToGray( const PaletteEntry* palette, uchar* grayPalette, int entries )
311 for( i = 0; i < entries; i++ )
313 icvCvt_BGR2Gray_8u_C3C1R( (uchar*)(palette + i), 0, grayPalette + i, 0, cvSize(1,1) );
318 void FillGrayPalette( PaletteEntry* palette, int bpp, bool negative )
320 int i, length = 1 << bpp;
321 int xor_mask = negative ? 255 : 0;
323 for( i = 0; i < length; i++ )
325 int val = (i * 255/(length - 1)) ^ xor_mask;
326 palette[i].b = palette[i].g = palette[i].r = (uchar)val;
332 bool IsColorPalette( PaletteEntry* palette, int bpp )
334 int i, length = 1 << bpp;
336 for( i = 0; i < length; i++ )
338 if( palette[i].b != palette[i].g ||
339 palette[i].b != palette[i].r )
347 uchar* FillUniColor( uchar* data, uchar*& line_end,
348 int step, int width3,
350 int count3, PaletteEntry clr )
354 uchar* end = data + count3;
359 count3 -= (int)(end - data);
361 for( ; data < end; data += 3 )
363 WRITE_PIX( data, clr );
366 if( data >= line_end )
369 data = line_end - width3;
370 if( ++y >= height ) break;
379 uchar* FillUniGray( uchar* data, uchar*& line_end,
382 int count, uchar clr )
386 uchar* end = data + count;
391 count -= (int)(end - data);
393 for( ; data < end; data++ )
398 if( data >= line_end )
401 data = line_end - width;
402 if( ++y >= height ) break;
411 uchar* FillColorRow8( uchar* data, uchar* indices, int len, PaletteEntry* palette )
413 uchar* end = data + len*3;
414 while( (data += 3) < end )
416 *((PaletteEntry*)(data-3)) = palette[*indices++];
418 PaletteEntry clr = palette[indices[0]];
419 WRITE_PIX( data - 3, clr );
424 uchar* FillGrayRow8( uchar* data, uchar* indices, int len, uchar* palette )
427 for( i = 0; i < len; i++ )
429 data[i] = palette[indices[i]];
435 uchar* FillColorRow4( uchar* data, uchar* indices, int len, PaletteEntry* palette )
437 uchar* end = data + len*3;
439 while( (data += 6) < end )
441 int idx = *indices++;
442 *((PaletteEntry*)(data-6)) = palette[idx >> 4];
443 *((PaletteEntry*)(data-3)) = palette[idx & 15];
446 int idx = indices[0];
447 PaletteEntry clr = palette[idx >> 4];
448 WRITE_PIX( data - 6, clr );
452 clr = palette[idx & 15];
453 WRITE_PIX( data - 3, clr );
459 uchar* FillGrayRow4( uchar* data, uchar* indices, int len, uchar* palette )
461 uchar* end = data + len;
462 while( (data += 2) < end )
464 int idx = *indices++;
465 data[-2] = palette[idx >> 4];
466 data[-1] = palette[idx & 15];
469 int idx = indices[0];
470 uchar clr = palette[idx >> 4];
475 clr = palette[idx & 15];
482 uchar* FillColorRow1( uchar* data, uchar* indices, int len, PaletteEntry* palette )
484 uchar* end = data + len*3;
486 while( (data += 24) < end )
488 int idx = *indices++;
489 *((PaletteEntry*)(data - 24)) = palette[(idx & 128) != 0];
490 *((PaletteEntry*)(data - 21)) = palette[(idx & 64) != 0];
491 *((PaletteEntry*)(data - 18)) = palette[(idx & 32) != 0];
492 *((PaletteEntry*)(data - 15)) = palette[(idx & 16) != 0];
493 *((PaletteEntry*)(data - 12)) = palette[(idx & 8) != 0];
494 *((PaletteEntry*)(data - 9)) = palette[(idx & 4) != 0];
495 *((PaletteEntry*)(data - 6)) = palette[(idx & 2) != 0];
496 *((PaletteEntry*)(data - 3)) = palette[(idx & 1) != 0];
499 int idx = indices[0] << 24;
500 for( data -= 24; data < end; data += 3, idx += idx )
502 PaletteEntry clr = palette[idx < 0];
503 WRITE_PIX( data, clr );
510 uchar* FillGrayRow1( uchar* data, uchar* indices, int len, uchar* palette )
512 uchar* end = data + len;
514 while( (data += 8) < end )
516 int idx = *indices++;
517 *((uchar*)(data - 8)) = palette[(idx & 128) != 0];
518 *((uchar*)(data - 7)) = palette[(idx & 64) != 0];
519 *((uchar*)(data - 6)) = palette[(idx & 32) != 0];
520 *((uchar*)(data - 5)) = palette[(idx & 16) != 0];
521 *((uchar*)(data - 4)) = palette[(idx & 8) != 0];
522 *((uchar*)(data - 3)) = palette[(idx & 4) != 0];
523 *((uchar*)(data - 2)) = palette[(idx & 2) != 0];
524 *((uchar*)(data - 1)) = palette[(idx & 1) != 0];
527 int idx = indices[0] << 24;
528 for( data -= 8; data < end; data++, idx += idx )
530 data[0] = palette[idx < 0];
538 cvConvertImage( const CvArr* srcarr, CvArr* dstarr, int flags )
542 CV_FUNCNAME( "cvConvertImage" );
548 int src_cn, dst_cn, swap_rb = flags & CV_CVTIMG_SWAP_RB;
550 CV_CALL( src = cvGetMat( srcarr, &srcstub ));
551 CV_CALL( dst = cvGetMat( dstarr, &dststub ));
553 src_cn = CV_MAT_CN( src->type );
554 dst_cn = CV_MAT_CN( dst->type );
556 if( src_cn != 1 && src_cn != 3 && src_cn != 4 )
557 CV_ERROR( CV_BadNumChannels, "Source image must have 1, 3 or 4 channels" );
559 if( CV_MAT_DEPTH( dst->type ) != CV_8U )
560 CV_ERROR( CV_BadDepth, "Destination image must be 8u" );
562 if( CV_MAT_CN(dst->type) != 1 && CV_MAT_CN(dst->type) != 3 )
563 CV_ERROR( CV_BadNumChannels, "Destination image must have 1 or 3 channels" );
565 if( !CV_ARE_DEPTHS_EQ( src, dst ))
567 int src_depth = CV_MAT_DEPTH(src->type);
568 double scale = src_depth <= CV_8S ? 1 : src_depth <= CV_32S ? 1./256 : 255;
569 double shift = src_depth == CV_8S || src_depth == CV_16S ? 128 : 0;
571 if( !CV_ARE_CNS_EQ( src, dst ))
573 temp = cvCreateMat( src->height, src->width,
574 (src->type & CV_MAT_CN_MASK)|(dst->type & CV_MAT_DEPTH_MASK));
575 cvConvertScale( src, temp, scale, shift );
580 cvConvertScale( src, dst, scale, shift );
585 if( src_cn != dst_cn || (src_cn == 3 && swap_rb) )
587 uchar *s = src->data.ptr, *d = dst->data.ptr;
588 int s_step = src->step, d_step = dst->step;
589 int code = src_cn*10 + dst_cn;
590 CvSize size = { src->cols, src->rows };
592 if( CV_IS_MAT_CONT(src->type & dst->type) )
594 size.width *= size.height;
596 s_step = d_step = CV_STUB_STEP;
602 icvCvt_Gray2BGR_8u_C1C3R( s, s_step, d, d_step, size );
605 icvCvt_BGR2Gray_8u_C3C1R( s, s_step, d, d_step, size, swap_rb );
609 icvCvt_RGB2BGR_8u_C3R( s, s_step, d, d_step, size );
612 icvCvt_BGRA2Gray_8u_C4C1R( s, s_step, d, d_step, size, swap_rb );
615 icvCvt_BGRA2BGR_8u_C4C3R( s, s_step, d, d_step, size, swap_rb );
618 CV_ERROR( CV_StsUnsupportedFormat, "Unsupported combination of input/output formats" );
623 if( flags & CV_CVTIMG_FLIP )
625 CV_CALL( cvFlip( src, dst, 0 ));
627 else if( src != dst )
629 CV_CALL( cvCopy( src, dst ));
634 cvReleaseMat( &temp );