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)
51 void icvCvt_BGR2Gray_8u_C3C1R( const uchar* rgb, int rgb_step,
52 uchar* gray, int gray_step,
53 CvSize size, int _swap_rb )
56 int swap_rb = _swap_rb ? 2 : 0;
57 for( ; size.height--; gray += gray_step )
59 for( i = 0; i < size.width; i++, rgb += 3 )
61 int t = descale( rgb[swap_rb]*cB + rgb[1]*cG + rgb[swap_rb^2]*cR, SCALE );
65 rgb += rgb_step - size.width*3;
70 void icvCvt_BGR2Gray_16u_C3C1R( const ushort* rgb, int rgb_step,
71 ushort* gray, int gray_step,
72 CvSize size, int _swap_rb )
75 int swap_rb = _swap_rb ? 2 : 0;
76 for( ; size.height--; gray += gray_step )
78 for( i = 0; i < size.width; i++, rgb += 3 )
80 int t = descale( rgb[swap_rb]*cB + rgb[1]*cG + rgb[swap_rb^2]*cR, SCALE );
84 rgb += rgb_step - size.width*3;
89 void icvCvt_BGRA2Gray_8u_C4C1R( const uchar* rgba, int rgba_step,
90 uchar* gray, int gray_step,
91 CvSize size, int _swap_rb )
94 int swap_rb = _swap_rb ? 2 : 0;
95 for( ; size.height--; gray += gray_step )
97 for( i = 0; i < size.width; i++, rgba += 4 )
99 int t = descale( rgba[swap_rb]*cB + rgba[1]*cG + rgba[swap_rb^2]*cR, SCALE );
103 rgba += rgba_step - size.width*4;
108 void icvCvt_Gray2BGR_8u_C1C3R( const uchar* gray, int gray_step,
109 uchar* bgr, int bgr_step, CvSize size )
112 for( ; size.height--; gray += gray_step )
114 for( i = 0; i < size.width; i++, bgr += 3 )
116 bgr[0] = bgr[1] = bgr[2] = gray[i];
118 bgr += bgr_step - size.width*3;
123 void icvCvt_BGRA2BGR_8u_C4C3R( const uchar* bgra, int bgra_step,
124 uchar* bgr, int bgr_step,
125 CvSize size, int _swap_rb )
128 int swap_rb = _swap_rb ? 2 : 0;
129 for( ; size.height--; )
131 for( i = 0; i < size.width; i++, bgr += 3, bgra += 4 )
133 uchar t0 = bgra[swap_rb], t1 = bgra[1];
134 bgr[0] = t0; bgr[1] = t1;
135 t0 = bgra[swap_rb^2]; bgr[2] = t0;
137 bgr += bgr_step - size.width*3;
138 bgra += bgra_step - size.width*4;
143 void icvCvt_BGRA2RGBA_8u_C4R( const uchar* bgra, int bgra_step,
144 uchar* rgba, int rgba_step, CvSize size )
147 for( ; size.height--; )
149 for( i = 0; i < size.width; i++, bgra += 4, rgba += 4 )
151 uchar t0 = bgra[0], t1 = bgra[1];
152 uchar t2 = bgra[2], t3 = bgra[3];
153 rgba[0] = t2; rgba[1] = t1;
154 rgba[2] = t0; rgba[3] = t3;
156 bgra += bgra_step - size.width*4;
157 rgba += rgba_step - size.width*4;
162 void icvCvt_BGR2RGB_8u_C3R( const uchar* bgr, int bgr_step,
163 uchar* rgb, int rgb_step, CvSize size )
166 for( ; size.height--; )
168 for( i = 0; i < size.width; i++, bgr += 3, rgb += 3 )
170 uchar t0 = bgr[0], t1 = bgr[1], t2 = bgr[2];
171 rgb[2] = t0; rgb[1] = t1; rgb[0] = t2;
173 bgr += bgr_step - size.width*3;
174 rgb += rgb_step - size.width*3;
179 void icvCvt_BGR2RGB_16u_C3R( const ushort* bgr, int bgr_step,
180 ushort* rgb, int rgb_step, CvSize size )
183 for( ; size.height--; )
185 for( i = 0; i < size.width; i++, bgr += 3, rgb += 3 )
187 ushort t0 = bgr[0], t1 = bgr[1], t2 = bgr[2];
188 rgb[2] = t0; rgb[1] = t1; rgb[0] = t2;
190 bgr += bgr_step - size.width*3;
191 rgb += rgb_step - size.width*3;
196 typedef unsigned short ushort;
198 void icvCvt_BGR5552Gray_8u_C2C1R( const uchar* bgr555, int bgr555_step,
199 uchar* gray, int gray_step, CvSize size )
202 for( ; size.height--; gray += gray_step, bgr555 += bgr555_step )
204 for( i = 0; i < size.width; i++ )
206 int t = descale( ((((ushort*)bgr555)[i] << 3) & 0xf8)*cB +
207 ((((ushort*)bgr555)[i] >> 2) & 0xf8)*cG +
208 ((((ushort*)bgr555)[i] >> 7) & 0xf8)*cR, SCALE );
215 void icvCvt_BGR5652Gray_8u_C2C1R( const uchar* bgr565, int bgr565_step,
216 uchar* gray, int gray_step, CvSize size )
219 for( ; size.height--; gray += gray_step, bgr565 += bgr565_step )
221 for( i = 0; i < size.width; i++ )
223 int t = descale( ((((ushort*)bgr565)[i] << 3) & 0xf8)*cB +
224 ((((ushort*)bgr565)[i] >> 3) & 0xfc)*cG +
225 ((((ushort*)bgr565)[i] >> 8) & 0xf8)*cR, SCALE );
232 void icvCvt_BGR5552BGR_8u_C2C3R( const uchar* bgr555, int bgr555_step,
233 uchar* bgr, int bgr_step, CvSize size )
236 for( ; size.height--; bgr555 += bgr555_step )
238 for( i = 0; i < size.width; i++, bgr += 3 )
240 int t0 = (((ushort*)bgr555)[i] << 3) & 0xf8;
241 int t1 = (((ushort*)bgr555)[i] >> 2) & 0xf8;
242 int t2 = (((ushort*)bgr555)[i] >> 7) & 0xf8;
243 bgr[0] = (uchar)t0; bgr[1] = (uchar)t1; bgr[2] = (uchar)t2;
245 bgr += bgr_step - size.width*3;
250 void icvCvt_BGR5652BGR_8u_C2C3R( const uchar* bgr565, int bgr565_step,
251 uchar* bgr, int bgr_step, CvSize size )
254 for( ; size.height--; bgr565 += bgr565_step )
256 for( i = 0; i < size.width; i++, bgr += 3 )
258 int t0 = (((ushort*)bgr565)[i] << 3) & 0xf8;
259 int t1 = (((ushort*)bgr565)[i] >> 3) & 0xfc;
260 int t2 = (((ushort*)bgr565)[i] >> 8) & 0xf8;
261 bgr[0] = (uchar)t0; bgr[1] = (uchar)t1; bgr[2] = (uchar)t2;
263 bgr += bgr_step - size.width*3;
268 void icvCvt_CMYK2BGR_8u_C4C3R( const uchar* cmyk, int cmyk_step,
269 uchar* bgr, int bgr_step, CvSize size )
272 for( ; size.height--; )
274 for( i = 0; i < size.width; i++, bgr += 3, cmyk += 4 )
276 int c = cmyk[0], m = cmyk[1], y = cmyk[2], k = cmyk[3];
277 c = k - ((255 - c)*k>>8);
278 m = k - ((255 - m)*k>>8);
279 y = k - ((255 - y)*k>>8);
280 bgr[2] = (uchar)c; bgr[1] = (uchar)m; bgr[0] = (uchar)y;
282 bgr += bgr_step - size.width*3;
283 cmyk += cmyk_step - size.width*4;
288 void icvCvt_CMYK2Gray_8u_C4C1R( const uchar* cmyk, int cmyk_step,
289 uchar* gray, int gray_step, CvSize size )
292 for( ; size.height--; )
294 for( i = 0; i < size.width; i++, cmyk += 4 )
296 int c = cmyk[0], m = cmyk[1], y = cmyk[2], k = cmyk[3];
297 c = k - ((255 - c)*k>>8);
298 m = k - ((255 - m)*k>>8);
299 y = k - ((255 - y)*k>>8);
300 int t = descale( y*cB + m*cG + c*cR, SCALE );
304 cmyk += cmyk_step - size.width*4;
309 void CvtPaletteToGray( const PaletteEntry* palette, uchar* grayPalette, int entries )
312 for( i = 0; i < entries; i++ )
314 icvCvt_BGR2Gray_8u_C3C1R( (uchar*)(palette + i), 0, grayPalette + i, 0, cvSize(1,1) );
319 void FillGrayPalette( PaletteEntry* palette, int bpp, bool negative )
321 int i, length = 1 << bpp;
322 int xor_mask = negative ? 255 : 0;
324 for( i = 0; i < length; i++ )
326 int val = (i * 255/(length - 1)) ^ xor_mask;
327 palette[i].b = palette[i].g = palette[i].r = (uchar)val;
333 bool IsColorPalette( PaletteEntry* palette, int bpp )
335 int i, length = 1 << bpp;
337 for( i = 0; i < length; i++ )
339 if( palette[i].b != palette[i].g ||
340 palette[i].b != palette[i].r )
348 uchar* FillUniColor( uchar* data, uchar*& line_end,
349 int step, int width3,
351 int count3, PaletteEntry clr )
355 uchar* end = data + count3;
360 count3 -= (int)(end - data);
362 for( ; data < end; data += 3 )
364 WRITE_PIX( data, clr );
367 if( data >= line_end )
370 data = line_end - width3;
371 if( ++y >= height ) break;
380 uchar* FillUniGray( uchar* data, uchar*& line_end,
383 int count, uchar clr )
387 uchar* end = data + count;
392 count -= (int)(end - data);
394 for( ; data < end; data++ )
399 if( data >= line_end )
402 data = line_end - width;
403 if( ++y >= height ) break;
412 uchar* FillColorRow8( uchar* data, uchar* indices, int len, PaletteEntry* palette )
414 uchar* end = data + len*3;
415 while( (data += 3) < end )
417 *((PaletteEntry*)(data-3)) = palette[*indices++];
419 PaletteEntry clr = palette[indices[0]];
420 WRITE_PIX( data - 3, clr );
425 uchar* FillGrayRow8( uchar* data, uchar* indices, int len, uchar* palette )
428 for( i = 0; i < len; i++ )
430 data[i] = palette[indices[i]];
436 uchar* FillColorRow4( uchar* data, uchar* indices, int len, PaletteEntry* palette )
438 uchar* end = data + len*3;
440 while( (data += 6) < end )
442 int idx = *indices++;
443 *((PaletteEntry*)(data-6)) = palette[idx >> 4];
444 *((PaletteEntry*)(data-3)) = palette[idx & 15];
447 int idx = indices[0];
448 PaletteEntry clr = palette[idx >> 4];
449 WRITE_PIX( data - 6, clr );
453 clr = palette[idx & 15];
454 WRITE_PIX( data - 3, clr );
460 uchar* FillGrayRow4( uchar* data, uchar* indices, int len, uchar* palette )
462 uchar* end = data + len;
463 while( (data += 2) < end )
465 int idx = *indices++;
466 data[-2] = palette[idx >> 4];
467 data[-1] = palette[idx & 15];
470 int idx = indices[0];
471 uchar clr = palette[idx >> 4];
476 clr = palette[idx & 15];
483 uchar* FillColorRow1( uchar* data, uchar* indices, int len, PaletteEntry* palette )
485 uchar* end = data + len*3;
487 while( (data += 24) < end )
489 int idx = *indices++;
490 *((PaletteEntry*)(data - 24)) = palette[(idx & 128) != 0];
491 *((PaletteEntry*)(data - 21)) = palette[(idx & 64) != 0];
492 *((PaletteEntry*)(data - 18)) = palette[(idx & 32) != 0];
493 *((PaletteEntry*)(data - 15)) = palette[(idx & 16) != 0];
494 *((PaletteEntry*)(data - 12)) = palette[(idx & 8) != 0];
495 *((PaletteEntry*)(data - 9)) = palette[(idx & 4) != 0];
496 *((PaletteEntry*)(data - 6)) = palette[(idx & 2) != 0];
497 *((PaletteEntry*)(data - 3)) = palette[(idx & 1) != 0];
500 int idx = indices[0] << 24;
501 for( data -= 24; data < end; data += 3, idx += idx )
503 PaletteEntry clr = palette[idx < 0];
504 WRITE_PIX( data, clr );
511 uchar* FillGrayRow1( uchar* data, uchar* indices, int len, uchar* palette )
513 uchar* end = data + len;
515 while( (data += 8) < end )
517 int idx = *indices++;
518 *((uchar*)(data - 8)) = palette[(idx & 128) != 0];
519 *((uchar*)(data - 7)) = palette[(idx & 64) != 0];
520 *((uchar*)(data - 6)) = palette[(idx & 32) != 0];
521 *((uchar*)(data - 5)) = palette[(idx & 16) != 0];
522 *((uchar*)(data - 4)) = palette[(idx & 8) != 0];
523 *((uchar*)(data - 3)) = palette[(idx & 4) != 0];
524 *((uchar*)(data - 2)) = palette[(idx & 2) != 0];
525 *((uchar*)(data - 1)) = palette[(idx & 1) != 0];
528 int idx = indices[0] << 24;
529 for( data -= 8; data < end; data++, idx += idx )
531 data[0] = palette[idx < 0];
539 cvConvertImage( const CvArr* srcarr, CvArr* dstarr, int flags )
543 CV_FUNCNAME( "cvConvertImage" );
549 int src_cn, swap_rb = flags & CV_CVTIMG_SWAP_RB;
551 CV_CALL( src = cvGetMat( srcarr, &srcstub ));
552 CV_CALL( dst = cvGetMat( dstarr, &dststub ));
554 src_cn = CV_MAT_CN( src->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 double scale = 0, shift = 0;
568 int src_depth = CV_MAT_DEPTH(src->type);
569 temp = cvCreateMat( src->height, src->width,
570 (src->type & CV_MAT_CN_MASK)|(dst->type & CV_MAT_DEPTH_MASK));
571 scale = src_depth <= CV_8S ? 1 : src_depth <= CV_32S ? 1./256 : 255;
572 shift = src_depth == CV_8S || src_depth == CV_16S ? 128 : 0;
573 cvConvertScale( src, temp, scale, shift );
578 uchar *s = src->data.ptr, *d = dst->data.ptr;
579 int s_step = src->step, d_step = dst->step;
580 int code = src_cn*10 + CV_MAT_CN(dst->type);
581 CvSize size = { src->cols, src->rows };
583 if( CV_IS_MAT_CONT(src->type & dst->type) )
585 size.width *= size.height;
587 s_step = d_step = CV_STUB_STEP;
593 icvCvt_Gray2BGR_8u_C1C3R( s, s_step, d, d_step, size );
596 icvCvt_BGR2Gray_8u_C3C1R( s, s_step, d, d_step, size, swap_rb );
600 icvCvt_RGB2BGR_8u_C3R( s, s_step, d, d_step, size );
603 icvCvt_BGRA2Gray_8u_C4C1R( s, s_step, d, d_step, size, swap_rb );
606 icvCvt_BGRA2BGR_8u_C4C3R( s, s_step, d, d_step, size, swap_rb );
609 CV_ERROR( CV_StsUnsupportedFormat, "Unsupported combination of input/output formats" );
612 if( code != 33 || swap_rb )
616 if( flags & CV_CVTIMG_FLIP )
618 CV_CALL( cvFlip( src, dst, 0 ));
620 else if( src != dst )
622 CV_CALL( cvCopy( src, dst ));
627 cvReleaseMat( &temp );