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.
45 #if defined WIN32 && defined _MSC_VER && _MSC_VER >= 1200
46 #if defined WIN64 && defined EM64T
48 #pragma comment(lib, "libjasperd_64.lib")
49 #pragma comment(lib, "libjpegd_64.lib")
50 #pragma comment(lib, "libpngd_64.lib")
51 #pragma comment(lib, "libtiffd_64.lib")
52 #pragma comment(lib, "zlibd_64.lib")
54 #pragma comment(lib, "libjasper_64.lib")
55 #pragma comment(lib, "libjpeg_64.lib")
56 #pragma comment(lib, "libpng_64.lib")
57 #pragma comment(lib, "libtiff_64.lib")
58 #pragma comment(lib, "zlib_64.lib")
62 #pragma comment(lib, "libjasperd.lib")
63 #pragma comment(lib, "libjpegd.lib")
64 #pragma comment(lib, "libpngd.lib")
65 #pragma comment(lib, "libtiffd.lib")
66 #pragma comment(lib, "zlibd.lib")
68 #pragma comment(lib, "libjasper.lib")
69 #pragma comment(lib, "libjpeg.lib")
70 #pragma comment(lib, "libpng.lib")
71 #pragma comment(lib, "libtiff.lib")
72 #pragma comment(lib, "zlib.lib")
78 #define cR (int)(0.299*(1 << SCALE) + 0.5)
79 #define cG (int)(0.587*(1 << SCALE) + 0.5)
80 #define cB ((1 << SCALE) - cR - cG)
83 void icvCvt_BGR2Gray_8u_C3C1R( const uchar* rgb, int rgb_step,
84 uchar* gray, int gray_step,
85 CvSize size, int _swap_rb )
88 int swap_rb = _swap_rb ? 2 : 0;
89 for( ; size.height--; gray += gray_step )
91 for( i = 0; i < size.width; i++, rgb += 3 )
93 int t = descale( rgb[swap_rb]*cB + rgb[1]*cG + rgb[swap_rb^2]*cR, SCALE );
97 rgb += rgb_step - size.width*3;
102 void icvCvt_BGR2Gray_16u_C3C1R( const ushort* rgb, int rgb_step,
103 ushort* gray, int gray_step,
104 CvSize size, int _swap_rb )
107 int swap_rb = _swap_rb ? 2 : 0;
108 for( ; size.height--; gray += gray_step )
110 for( i = 0; i < size.width; i++, rgb += 3 )
112 int t = descale( rgb[swap_rb]*cB + rgb[1]*cG + rgb[swap_rb^2]*cR, SCALE );
116 rgb += rgb_step - size.width*3;
121 void icvCvt_BGRA2Gray_8u_C4C1R( const uchar* rgba, int rgba_step,
122 uchar* gray, int gray_step,
123 CvSize size, int _swap_rb )
126 int swap_rb = _swap_rb ? 2 : 0;
127 for( ; size.height--; gray += gray_step )
129 for( i = 0; i < size.width; i++, rgba += 4 )
131 int t = descale( rgba[swap_rb]*cB + rgba[1]*cG + rgba[swap_rb^2]*cR, SCALE );
135 rgba += rgba_step - size.width*4;
140 void icvCvt_Gray2BGR_8u_C1C3R( const uchar* gray, int gray_step,
141 uchar* bgr, int bgr_step, CvSize size )
144 for( ; size.height--; gray += gray_step )
146 for( i = 0; i < size.width; i++, bgr += 3 )
148 bgr[0] = bgr[1] = bgr[2] = gray[i];
150 bgr += bgr_step - size.width*3;
155 void icvCvt_BGRA2BGR_8u_C4C3R( const uchar* bgra, int bgra_step,
156 uchar* bgr, int bgr_step,
157 CvSize size, int _swap_rb )
160 int swap_rb = _swap_rb ? 2 : 0;
161 for( ; size.height--; )
163 for( i = 0; i < size.width; i++, bgr += 3, bgra += 4 )
165 uchar t0 = bgra[swap_rb], t1 = bgra[1];
166 bgr[0] = t0; bgr[1] = t1;
167 t0 = bgra[swap_rb^2]; bgr[2] = t0;
169 bgr += bgr_step - size.width*3;
170 bgra += bgra_step - size.width*4;
175 void icvCvt_BGRA2RGBA_8u_C4R( const uchar* bgra, int bgra_step,
176 uchar* rgba, int rgba_step, CvSize size )
179 for( ; size.height--; )
181 for( i = 0; i < size.width; i++, bgra += 4, rgba += 4 )
183 uchar t0 = bgra[0], t1 = bgra[1];
184 uchar t2 = bgra[2], t3 = bgra[3];
185 rgba[0] = t2; rgba[1] = t1;
186 rgba[2] = t0; rgba[3] = t3;
188 bgra += bgra_step - size.width*4;
189 rgba += rgba_step - size.width*4;
194 void icvCvt_BGR2RGB_8u_C3R( const uchar* bgr, int bgr_step,
195 uchar* rgb, int rgb_step, CvSize size )
198 for( ; size.height--; )
200 for( i = 0; i < size.width; i++, bgr += 3, rgb += 3 )
202 uchar t0 = bgr[0], t1 = bgr[1], t2 = bgr[2];
203 rgb[2] = t0; rgb[1] = t1; rgb[0] = t2;
205 bgr += bgr_step - size.width*3;
206 rgb += rgb_step - size.width*3;
211 void icvCvt_BGR2RGB_16u_C3R( const ushort* bgr, int bgr_step,
212 ushort* rgb, int rgb_step, CvSize size )
215 for( ; size.height--; )
217 for( i = 0; i < size.width; i++, bgr += 3, rgb += 3 )
219 ushort t0 = bgr[0], t1 = bgr[1], t2 = bgr[2];
220 rgb[2] = t0; rgb[1] = t1; rgb[0] = t2;
222 bgr += bgr_step - size.width*3;
223 rgb += rgb_step - size.width*3;
228 typedef unsigned short ushort;
230 void icvCvt_BGR5552Gray_8u_C2C1R( const uchar* bgr555, int bgr555_step,
231 uchar* gray, int gray_step, CvSize size )
234 for( ; size.height--; gray += gray_step, bgr555 += bgr555_step )
236 for( i = 0; i < size.width; i++ )
238 int t = descale( ((((ushort*)bgr555)[i] << 3) & 0xf8)*cB +
239 ((((ushort*)bgr555)[i] >> 2) & 0xf8)*cG +
240 ((((ushort*)bgr555)[i] >> 7) & 0xf8)*cR, SCALE );
247 void icvCvt_BGR5652Gray_8u_C2C1R( const uchar* bgr565, int bgr565_step,
248 uchar* gray, int gray_step, CvSize size )
251 for( ; size.height--; gray += gray_step, bgr565 += bgr565_step )
253 for( i = 0; i < size.width; i++ )
255 int t = descale( ((((ushort*)bgr565)[i] << 3) & 0xf8)*cB +
256 ((((ushort*)bgr565)[i] >> 3) & 0xfc)*cG +
257 ((((ushort*)bgr565)[i] >> 8) & 0xf8)*cR, SCALE );
264 void icvCvt_BGR5552BGR_8u_C2C3R( const uchar* bgr555, int bgr555_step,
265 uchar* bgr, int bgr_step, CvSize size )
268 for( ; size.height--; bgr555 += bgr555_step )
270 for( i = 0; i < size.width; i++, bgr += 3 )
272 int t0 = (((ushort*)bgr555)[i] << 3) & 0xf8;
273 int t1 = (((ushort*)bgr555)[i] >> 2) & 0xf8;
274 int t2 = (((ushort*)bgr555)[i] >> 7) & 0xf8;
275 bgr[0] = (uchar)t0; bgr[1] = (uchar)t1; bgr[2] = (uchar)t2;
277 bgr += bgr_step - size.width*3;
282 void icvCvt_BGR5652BGR_8u_C2C3R( const uchar* bgr565, int bgr565_step,
283 uchar* bgr, int bgr_step, CvSize size )
286 for( ; size.height--; bgr565 += bgr565_step )
288 for( i = 0; i < size.width; i++, bgr += 3 )
290 int t0 = (((ushort*)bgr565)[i] << 3) & 0xf8;
291 int t1 = (((ushort*)bgr565)[i] >> 3) & 0xfc;
292 int t2 = (((ushort*)bgr565)[i] >> 8) & 0xf8;
293 bgr[0] = (uchar)t0; bgr[1] = (uchar)t1; bgr[2] = (uchar)t2;
295 bgr += bgr_step - size.width*3;
300 void icvCvt_CMYK2BGR_8u_C4C3R( const uchar* cmyk, int cmyk_step,
301 uchar* bgr, int bgr_step, CvSize size )
304 for( ; size.height--; )
306 for( i = 0; i < size.width; i++, bgr += 3, cmyk += 4 )
308 int c = cmyk[0], m = cmyk[1], y = cmyk[2], k = cmyk[3];
309 c = k - ((255 - c)*k>>8);
310 m = k - ((255 - m)*k>>8);
311 y = k - ((255 - y)*k>>8);
312 bgr[2] = (uchar)c; bgr[1] = (uchar)m; bgr[0] = (uchar)y;
314 bgr += bgr_step - size.width*3;
315 cmyk += cmyk_step - size.width*4;
320 void icvCvt_CMYK2Gray_8u_C4C1R( const uchar* cmyk, int cmyk_step,
321 uchar* gray, int gray_step, CvSize size )
324 for( ; size.height--; )
326 for( i = 0; i < size.width; i++, cmyk += 4 )
328 int c = cmyk[0], m = cmyk[1], y = cmyk[2], k = cmyk[3];
329 c = k - ((255 - c)*k>>8);
330 m = k - ((255 - m)*k>>8);
331 y = k - ((255 - y)*k>>8);
332 int t = descale( y*cB + m*cG + c*cR, SCALE );
336 cmyk += cmyk_step - size.width*4;
341 void CvtPaletteToGray( const PaletteEntry* palette, uchar* grayPalette, int entries )
344 for( i = 0; i < entries; i++ )
346 icvCvt_BGR2Gray_8u_C3C1R( (uchar*)(palette + i), 0, grayPalette + i, 0, cvSize(1,1) );
351 void FillGrayPalette( PaletteEntry* palette, int bpp, bool negative )
353 int i, length = 1 << bpp;
354 int xor_mask = negative ? 255 : 0;
356 for( i = 0; i < length; i++ )
358 int val = (i * 255/(length - 1)) ^ xor_mask;
359 palette[i].b = palette[i].g = palette[i].r = (uchar)val;
365 bool IsColorPalette( PaletteEntry* palette, int bpp )
367 int i, length = 1 << bpp;
369 for( i = 0; i < length; i++ )
371 if( palette[i].b != palette[i].g ||
372 palette[i].b != palette[i].r )
380 uchar* FillUniColor( uchar* data, uchar*& line_end,
381 int step, int width3,
383 int count3, PaletteEntry clr )
387 uchar* end = data + count3;
392 count3 -= (int)(end - data);
394 for( ; data < end; data += 3 )
396 WRITE_PIX( data, clr );
399 if( data >= line_end )
402 data = line_end - width3;
403 if( ++y >= height ) break;
412 uchar* FillUniGray( uchar* data, uchar*& line_end,
415 int count, uchar clr )
419 uchar* end = data + count;
424 count -= (int)(end - data);
426 for( ; data < end; data++ )
431 if( data >= line_end )
434 data = line_end - width;
435 if( ++y >= height ) break;
444 uchar* FillColorRow8( uchar* data, uchar* indices, int len, PaletteEntry* palette )
446 uchar* end = data + len*3;
447 while( (data += 3) < end )
449 *((PaletteEntry*)(data-3)) = palette[*indices++];
451 PaletteEntry clr = palette[indices[0]];
452 WRITE_PIX( data - 3, clr );
457 uchar* FillGrayRow8( uchar* data, uchar* indices, int len, uchar* palette )
460 for( i = 0; i < len; i++ )
462 data[i] = palette[indices[i]];
468 uchar* FillColorRow4( uchar* data, uchar* indices, int len, PaletteEntry* palette )
470 uchar* end = data + len*3;
472 while( (data += 6) < end )
474 int idx = *indices++;
475 *((PaletteEntry*)(data-6)) = palette[idx >> 4];
476 *((PaletteEntry*)(data-3)) = palette[idx & 15];
479 int idx = indices[0];
480 PaletteEntry clr = palette[idx >> 4];
481 WRITE_PIX( data - 6, clr );
485 clr = palette[idx & 15];
486 WRITE_PIX( data - 3, clr );
492 uchar* FillGrayRow4( uchar* data, uchar* indices, int len, uchar* palette )
494 uchar* end = data + len;
495 while( (data += 2) < end )
497 int idx = *indices++;
498 data[-2] = palette[idx >> 4];
499 data[-1] = palette[idx & 15];
502 int idx = indices[0];
503 uchar clr = palette[idx >> 4];
508 clr = palette[idx & 15];
515 uchar* FillColorRow1( uchar* data, uchar* indices, int len, PaletteEntry* palette )
517 uchar* end = data + len*3;
519 while( (data += 24) < end )
521 int idx = *indices++;
522 *((PaletteEntry*)(data - 24)) = palette[(idx & 128) != 0];
523 *((PaletteEntry*)(data - 21)) = palette[(idx & 64) != 0];
524 *((PaletteEntry*)(data - 18)) = palette[(idx & 32) != 0];
525 *((PaletteEntry*)(data - 15)) = palette[(idx & 16) != 0];
526 *((PaletteEntry*)(data - 12)) = palette[(idx & 8) != 0];
527 *((PaletteEntry*)(data - 9)) = palette[(idx & 4) != 0];
528 *((PaletteEntry*)(data - 6)) = palette[(idx & 2) != 0];
529 *((PaletteEntry*)(data - 3)) = palette[(idx & 1) != 0];
532 int idx = indices[0] << 24;
533 for( data -= 24; data < end; data += 3, idx += idx )
535 PaletteEntry clr = palette[idx < 0];
536 WRITE_PIX( data, clr );
543 uchar* FillGrayRow1( uchar* data, uchar* indices, int len, uchar* palette )
545 uchar* end = data + len;
547 while( (data += 8) < end )
549 int idx = *indices++;
550 *((uchar*)(data - 8)) = palette[(idx & 128) != 0];
551 *((uchar*)(data - 7)) = palette[(idx & 64) != 0];
552 *((uchar*)(data - 6)) = palette[(idx & 32) != 0];
553 *((uchar*)(data - 5)) = palette[(idx & 16) != 0];
554 *((uchar*)(data - 4)) = palette[(idx & 8) != 0];
555 *((uchar*)(data - 3)) = palette[(idx & 4) != 0];
556 *((uchar*)(data - 2)) = palette[(idx & 2) != 0];
557 *((uchar*)(data - 1)) = palette[(idx & 1) != 0];
560 int idx = indices[0] << 24;
561 for( data -= 8; data < end; data++, idx += idx )
563 data[0] = palette[idx < 0];
571 cvConvertImage( const CvArr* srcarr, CvArr* dstarr, int flags )
575 CV_FUNCNAME( "cvConvertImage" );
581 int src_cn, dst_cn, swap_rb = flags & CV_CVTIMG_SWAP_RB;
583 CV_CALL( src = cvGetMat( srcarr, &srcstub ));
584 CV_CALL( dst = cvGetMat( dstarr, &dststub ));
586 src_cn = CV_MAT_CN( src->type );
587 dst_cn = CV_MAT_CN( dst->type );
589 if( src_cn != 1 && src_cn != 3 && src_cn != 4 )
590 CV_ERROR( CV_BadNumChannels, "Source image must have 1, 3 or 4 channels" );
592 if( CV_MAT_DEPTH( dst->type ) != CV_8U )
593 CV_ERROR( CV_BadDepth, "Destination image must be 8u" );
595 if( CV_MAT_CN(dst->type) != 1 && CV_MAT_CN(dst->type) != 3 )
596 CV_ERROR( CV_BadNumChannels, "Destination image must have 1 or 3 channels" );
598 if( !CV_ARE_DEPTHS_EQ( src, dst ))
600 int src_depth = CV_MAT_DEPTH(src->type);
601 double scale = src_depth <= CV_8S ? 1 : src_depth <= CV_32S ? 1./256 : 255;
602 double shift = src_depth == CV_8S || src_depth == CV_16S ? 128 : 0;
604 if( !CV_ARE_CNS_EQ( src, dst ))
606 temp = cvCreateMat( src->height, src->width,
607 (src->type & CV_MAT_CN_MASK)|(dst->type & CV_MAT_DEPTH_MASK));
608 cvConvertScale( src, temp, scale, shift );
613 cvConvertScale( src, dst, scale, shift );
618 if( src_cn != dst_cn || src_cn == 3 && swap_rb )
620 uchar *s = src->data.ptr, *d = dst->data.ptr;
621 int s_step = src->step, d_step = dst->step;
622 int code = src_cn*10 + dst_cn;
623 CvSize size = { src->cols, src->rows };
625 if( CV_IS_MAT_CONT(src->type & dst->type) )
627 size.width *= size.height;
629 s_step = d_step = CV_STUB_STEP;
635 icvCvt_Gray2BGR_8u_C1C3R( s, s_step, d, d_step, size );
638 icvCvt_BGR2Gray_8u_C3C1R( s, s_step, d, d_step, size, swap_rb );
642 icvCvt_RGB2BGR_8u_C3R( s, s_step, d, d_step, size );
645 icvCvt_BGRA2Gray_8u_C4C1R( s, s_step, d, d_step, size, swap_rb );
648 icvCvt_BGRA2BGR_8u_C4C3R( s, s_step, d, d_step, size, swap_rb );
651 CV_ERROR( CV_StsUnsupportedFormat, "Unsupported combination of input/output formats" );
656 if( flags & CV_CVTIMG_FLIP )
658 CV_CALL( cvFlip( src, dst, 0 ));
660 else if( src != dst )
662 CV_CALL( cvCopy( src, dst ));
667 cvReleaseMat( &temp );