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 "_highgui.h" 43 #include "utils.h" 44 45 #if defined WIN32 && defined _MSC_VER && _MSC_VER >= 1200 46 #if defined WIN64 && defined EM64T 47 #ifdef _DEBUG 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") 53 #else 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") 59 #endif 60 #elif !defined WIN64 61 #ifdef _DEBUG 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") 67 #else 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") 73 #endif 74 #endif 75 #endif 76 77 #define SCALE 14 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) 81 82 83 void icvCvt_BGR2Gray_8u_C3C1R( const uchar* rgb, int rgb_step, 84 uchar* gray, int gray_step, 85 CvSize size, int _swap_rb ) 86 { 87 int i; 88 int swap_rb = _swap_rb ? 2 : 0; 89 for( ; size.height--; gray += gray_step ) 90 { 91 for( i = 0; i < size.width; i++, rgb += 3 ) 92 { 93 int t = descale( rgb[swap_rb]*cB + rgb[1]*cG + rgb[swap_rb^2]*cR, SCALE ); 94 gray[i] = (uchar)t; 95 } 96 97 rgb += rgb_step - size.width*3; 98 } 99 } 100 101 102 void icvCvt_BGR2Gray_16u_C3C1R( const ushort* rgb, int rgb_step, 103 ushort* gray, int gray_step, 104 CvSize size, int _swap_rb ) 105 { 106 int i; 107 int swap_rb = _swap_rb ? 2 : 0; 108 for( ; size.height--; gray += gray_step ) 109 { 110 for( i = 0; i < size.width; i++, rgb += 3 ) 111 { 112 int t = descale( rgb[swap_rb]*cB + rgb[1]*cG + rgb[swap_rb^2]*cR, SCALE ); 113 gray[i] = (ushort)t; 114 } 115 116 rgb += rgb_step - size.width*3; 117 } 118 } 119 120 121 void icvCvt_BGRA2Gray_8u_C4C1R( const uchar* rgba, int rgba_step, 122 uchar* gray, int gray_step, 123 CvSize size, int _swap_rb ) 124 { 125 int i; 126 int swap_rb = _swap_rb ? 2 : 0; 127 for( ; size.height--; gray += gray_step ) 128 { 129 for( i = 0; i < size.width; i++, rgba += 4 ) 130 { 131 int t = descale( rgba[swap_rb]*cB + rgba[1]*cG + rgba[swap_rb^2]*cR, SCALE ); 132 gray[i] = (uchar)t; 133 } 134 135 rgba += rgba_step - size.width*4; 136 } 137 } 138 139 140 void icvCvt_Gray2BGR_8u_C1C3R( const uchar* gray, int gray_step, 141 uchar* bgr, int bgr_step, CvSize size ) 142 { 143 int i; 144 for( ; size.height--; gray += gray_step ) 145 { 146 for( i = 0; i < size.width; i++, bgr += 3 ) 147 { 148 bgr[0] = bgr[1] = bgr[2] = gray[i]; 149 } 150 bgr += bgr_step - size.width*3; 151 } 152 } 153 154 155 void icvCvt_BGRA2BGR_8u_C4C3R( const uchar* bgra, int bgra_step, 156 uchar* bgr, int bgr_step, 157 CvSize size, int _swap_rb ) 158 { 159 int i; 160 int swap_rb = _swap_rb ? 2 : 0; 161 for( ; size.height--; ) 162 { 163 for( i = 0; i < size.width; i++, bgr += 3, bgra += 4 ) 164 { 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; 168 } 169 bgr += bgr_step - size.width*3; 170 bgra += bgra_step - size.width*4; 171 } 172 } 173 174 175 void icvCvt_BGRA2RGBA_8u_C4R( const uchar* bgra, int bgra_step, 176 uchar* rgba, int rgba_step, CvSize size ) 177 { 178 int i; 179 for( ; size.height--; ) 180 { 181 for( i = 0; i < size.width; i++, bgra += 4, rgba += 4 ) 182 { 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; 187 } 188 bgra += bgra_step - size.width*4; 189 rgba += rgba_step - size.width*4; 190 } 191 } 192 193 194 void icvCvt_BGR2RGB_8u_C3R( const uchar* bgr, int bgr_step, 195 uchar* rgb, int rgb_step, CvSize size ) 196 { 197 int i; 198 for( ; size.height--; ) 199 { 200 for( i = 0; i < size.width; i++, bgr += 3, rgb += 3 ) 201 { 202 uchar t0 = bgr[0], t1 = bgr[1], t2 = bgr[2]; 203 rgb[2] = t0; rgb[1] = t1; rgb[0] = t2; 204 } 205 bgr += bgr_step - size.width*3; 206 rgb += rgb_step - size.width*3; 207 } 208 } 209 210 211 void icvCvt_BGR2RGB_16u_C3R( const ushort* bgr, int bgr_step, 212 ushort* rgb, int rgb_step, CvSize size ) 213 { 214 int i; 215 for( ; size.height--; ) 216 { 217 for( i = 0; i < size.width; i++, bgr += 3, rgb += 3 ) 218 { 219 ushort t0 = bgr[0], t1 = bgr[1], t2 = bgr[2]; 220 rgb[2] = t0; rgb[1] = t1; rgb[0] = t2; 221 } 222 bgr += bgr_step - size.width*3; 223 rgb += rgb_step - size.width*3; 224 } 225 } 226 227 228 typedef unsigned short ushort; 229 230 void icvCvt_BGR5552Gray_8u_C2C1R( const uchar* bgr555, int bgr555_step, 231 uchar* gray, int gray_step, CvSize size ) 232 { 233 int i; 234 for( ; size.height--; gray += gray_step, bgr555 += bgr555_step ) 235 { 236 for( i = 0; i < size.width; i++ ) 237 { 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 ); 241 gray[i] = (uchar)t; 242 } 243 } 244 } 245 246 247 void icvCvt_BGR5652Gray_8u_C2C1R( const uchar* bgr565, int bgr565_step, 248 uchar* gray, int gray_step, CvSize size ) 249 { 250 int i; 251 for( ; size.height--; gray += gray_step, bgr565 += bgr565_step ) 252 { 253 for( i = 0; i < size.width; i++ ) 254 { 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 ); 258 gray[i] = (uchar)t; 259 } 260 } 261 } 262 263 264 void icvCvt_BGR5552BGR_8u_C2C3R( const uchar* bgr555, int bgr555_step, 265 uchar* bgr, int bgr_step, CvSize size ) 266 { 267 int i; 268 for( ; size.height--; bgr555 += bgr555_step ) 269 { 270 for( i = 0; i < size.width; i++, bgr += 3 ) 271 { 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; 276 } 277 bgr += bgr_step - size.width*3; 278 } 279 } 280 281 282 void icvCvt_BGR5652BGR_8u_C2C3R( const uchar* bgr565, int bgr565_step, 283 uchar* bgr, int bgr_step, CvSize size ) 284 { 285 int i; 286 for( ; size.height--; bgr565 += bgr565_step ) 287 { 288 for( i = 0; i < size.width; i++, bgr += 3 ) 289 { 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; 294 } 295 bgr += bgr_step - size.width*3; 296 } 297 } 298 299 300 void icvCvt_CMYK2BGR_8u_C4C3R( const uchar* cmyk, int cmyk_step, 301 uchar* bgr, int bgr_step, CvSize size ) 302 { 303 int i; 304 for( ; size.height--; ) 305 { 306 for( i = 0; i < size.width; i++, bgr += 3, cmyk += 4 ) 307 { 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; 313 } 314 bgr += bgr_step - size.width*3; 315 cmyk += cmyk_step - size.width*4; 316 } 317 } 318 319 320 void icvCvt_CMYK2Gray_8u_C4C1R( const uchar* cmyk, int cmyk_step, 321 uchar* gray, int gray_step, CvSize size ) 322 { 323 int i; 324 for( ; size.height--; ) 325 { 326 for( i = 0; i < size.width; i++, cmyk += 4 ) 327 { 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 ); 333 gray[i] = (uchar)t; 334 } 335 gray += gray_step; 336 cmyk += cmyk_step - size.width*4; 337 } 338 } 339 340 341 void CvtPaletteToGray( const PaletteEntry* palette, uchar* grayPalette, int entries ) 342 { 343 int i; 344 for( i = 0; i < entries; i++ ) 345 { 346 icvCvt_BGR2Gray_8u_C3C1R( (uchar*)(palette + i), 0, grayPalette + i, 0, cvSize(1,1) ); 347 } 348 } 349 350 351 void FillGrayPalette( PaletteEntry* palette, int bpp, bool negative ) 352 { 353 int i, length = 1 << bpp; 354 int xor_mask = negative ? 255 : 0; 355 356 for( i = 0; i < length; i++ ) 357 { 358 int val = (i * 255/(length - 1)) ^ xor_mask; 359 palette[i].b = palette[i].g = palette[i].r = (uchar)val; 360 palette[i].a = 0; 361 } 362 } 363 364 365 bool IsColorPalette( PaletteEntry* palette, int bpp ) 366 { 367 int i, length = 1 << bpp; 368 369 for( i = 0; i < length; i++ ) 370 { 371 if( palette[i].b != palette[i].g || 372 palette[i].b != palette[i].r ) 373 return true; 374 } 375 376 return false; 377 } 378 379 380 uchar* FillUniColor( uchar* data, uchar*& line_end, 381 int step, int width3, 382 int& y, int height, 383 int count3, PaletteEntry clr ) 384 { 385 do 386 { 387 uchar* end = data + count3; 388 389 if( end > line_end ) 390 end = line_end; 391 392 count3 -= (int)(end - data); 393 394 for( ; data < end; data += 3 ) 395 { 396 WRITE_PIX( data, clr ); 397 } 398 399 if( data >= line_end ) 400 { 401 line_end += step; 402 data = line_end - width3; 403 if( ++y >= height ) break; 404 } 405 } 406 while( count3 > 0 ); 407 408 return data; 409 } 410 411 412 uchar* FillUniGray( uchar* data, uchar*& line_end, 413 int step, int width, 414 int& y, int height, 415 int count, uchar clr ) 416 { 417 do 418 { 419 uchar* end = data + count; 420 421 if( end > line_end ) 422 end = line_end; 423 424 count -= (int)(end - data); 425 426 for( ; data < end; data++ ) 427 { 428 *data = clr; 429 } 430 431 if( data >= line_end ) 432 { 433 line_end += step; 434 data = line_end - width; 435 if( ++y >= height ) break; 436 } 437 } 438 while( count > 0 ); 439 440 return data; 441 } 442 443 444 uchar* FillColorRow8( uchar* data, uchar* indices, int len, PaletteEntry* palette ) 445 { 446 uchar* end = data + len*3; 447 while( (data += 3) < end ) 448 { 449 *((PaletteEntry*)(data-3)) = palette[*indices++]; 450 } 451 PaletteEntry clr = palette[indices[0]]; 452 WRITE_PIX( data - 3, clr ); 453 return data; 454 } 455 456 457 uchar* FillGrayRow8( uchar* data, uchar* indices, int len, uchar* palette ) 458 { 459 int i; 460 for( i = 0; i < len; i++ ) 461 { 462 data[i] = palette[indices[i]]; 463 } 464 return data + len; 465 } 466 467 468 uchar* FillColorRow4( uchar* data, uchar* indices, int len, PaletteEntry* palette ) 469 { 470 uchar* end = data + len*3; 471 472 while( (data += 6) < end ) 473 { 474 int idx = *indices++; 475 *((PaletteEntry*)(data-6)) = palette[idx >> 4]; 476 *((PaletteEntry*)(data-3)) = palette[idx & 15]; 477 } 478 479 int idx = indices[0]; 480 PaletteEntry clr = palette[idx >> 4]; 481 WRITE_PIX( data - 6, clr ); 482 483 if( data == end ) 484 { 485 clr = palette[idx & 15]; 486 WRITE_PIX( data - 3, clr ); 487 } 488 return end; 489 } 490 491 492 uchar* FillGrayRow4( uchar* data, uchar* indices, int len, uchar* palette ) 493 { 494 uchar* end = data + len; 495 while( (data += 2) < end ) 496 { 497 int idx = *indices++; 498 data[-2] = palette[idx >> 4]; 499 data[-1] = palette[idx & 15]; 500 } 501 502 int idx = indices[0]; 503 uchar clr = palette[idx >> 4]; 504 data[-2] = clr; 505 506 if( data == end ) 507 { 508 clr = palette[idx & 15]; 509 data[-1] = clr; 510 } 511 return end; 512 } 513 514 515 uchar* FillColorRow1( uchar* data, uchar* indices, int len, PaletteEntry* palette ) 516 { 517 uchar* end = data + len*3; 518 519 while( (data += 24) < end ) 520 { 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]; 530 } 531 532 int idx = indices[0] << 24; 533 for( data -= 24; data < end; data += 3, idx += idx ) 534 { 535 PaletteEntry clr = palette[idx < 0]; 536 WRITE_PIX( data, clr ); 537 } 538 539 return data; 540 } 541 542 543 uchar* FillGrayRow1( uchar* data, uchar* indices, int len, uchar* palette ) 544 { 545 uchar* end = data + len; 546 547 while( (data += 8) < end ) 548 { 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]; 558 } 559 560 int idx = indices[0] << 24; 561 for( data -= 8; data < end; data++, idx += idx ) 562 { 563 data[0] = palette[idx < 0]; 564 } 565 566 return data; 567 } 568 569 570 CV_IMPL void 571 cvConvertImage( const CvArr* srcarr, CvArr* dstarr, int flags ) 572 { 573 CvMat* temp = 0; 574 575 CV_FUNCNAME( "cvConvertImage" ); 576 577 __BEGIN__; 578 579 CvMat srcstub, *src; 580 CvMat dststub, *dst; 581 int src_cn, dst_cn, swap_rb = flags & CV_CVTIMG_SWAP_RB; 582 583 CV_CALL( src = cvGetMat( srcarr, &srcstub )); 584 CV_CALL( dst = cvGetMat( dstarr, &dststub )); 585 586 src_cn = CV_MAT_CN( src->type ); 587 dst_cn = CV_MAT_CN( dst->type ); 588 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" ); 591 592 if( CV_MAT_DEPTH( dst->type ) != CV_8U ) 593 CV_ERROR( CV_BadDepth, "Destination image must be 8u" ); 594 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" ); 597 598 if( !CV_ARE_DEPTHS_EQ( src, dst )) 599 { 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; 603 604 if( !CV_ARE_CNS_EQ( src, dst )) 605 { 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 ); 609 src = temp; 610 } 611 else 612 { 613 cvConvertScale( src, dst, scale, shift ); 614 src = dst; 615 } 616 } 617 618 if( src_cn != dst_cn || src_cn == 3 && swap_rb ) 619 { 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 }; 624 625 if( CV_IS_MAT_CONT(src->type & dst->type) ) 626 { 627 size.width *= size.height; 628 size.height = 1; 629 s_step = d_step = CV_STUB_STEP; 630 } 631 632 switch( code ) 633 { 634 case 13: 635 icvCvt_Gray2BGR_8u_C1C3R( s, s_step, d, d_step, size ); 636 break; 637 case 31: 638 icvCvt_BGR2Gray_8u_C3C1R( s, s_step, d, d_step, size, swap_rb ); 639 break; 640 case 33: 641 assert( swap_rb ); 642 icvCvt_RGB2BGR_8u_C3R( s, s_step, d, d_step, size ); 643 break; 644 case 41: 645 icvCvt_BGRA2Gray_8u_C4C1R( s, s_step, d, d_step, size, swap_rb ); 646 break; 647 case 43: 648 icvCvt_BGRA2BGR_8u_C4C3R( s, s_step, d, d_step, size, swap_rb ); 649 break; 650 default: 651 CV_ERROR( CV_StsUnsupportedFormat, "Unsupported combination of input/output formats" ); 652 } 653 src = dst; 654 } 655 656 if( flags & CV_CVTIMG_FLIP ) 657 { 658 CV_CALL( cvFlip( src, dst, 0 )); 659 } 660 else if( src != dst ) 661 { 662 CV_CALL( cvCopy( src, dst )); 663 } 664 665 __END__; 666 667 cvReleaseMat( &temp ); 668 } 669