Hash :
89b14ca9
Author :
Thomas de Grivel
Date :
2025-11-25T11:33:25
change encoding to utf8-unix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
/**
* @file avir_dil.h
*
* @version 3.1
*
* @brief Inclusion file for de-interleaved image resizing functions.
*
* This file includes the CImageResizerFilterStepDIL class which implements
* image resizing functions in de-interleaved mode.
*
* AVIR Copyright (c) 2015-2025 Aleksey Vaneev
*
* See the "LICENSE" file for license.
*/
namespace avir {
/**
* @brief De-interleaved filtering steps implementation class.
*
* This class implements scanline filtering functions in de-interleaved mode.
* This means that pixels are processed in groups.
*
* @tparam fptype Floating point type to use for storing pixel elements.
* SIMD types cannot be used.
* @tparam fptypesimd The SIMD type used to store a pack of `fptype` values.
*/
template< class fptype, class fptypesimd >
class CImageResizerFilterStepDIL :
public CImageResizerFilterStep< fptype, fptype >
{
public:
using CImageResizerFilterStep< fptype, fptype > :: IsUpsample;
using CImageResizerFilterStep< fptype, fptype > :: ResampleFactor;
using CImageResizerFilterStep< fptype, fptype > :: Flt;
using CImageResizerFilterStep< fptype, fptype > :: FltOrig;
using CImageResizerFilterStep< fptype, fptype > :: FltLatency;
using CImageResizerFilterStep< fptype, fptype > :: Vars;
using CImageResizerFilterStep< fptype, fptype > :: InLen;
using CImageResizerFilterStep< fptype, fptype > :: InPrefix;
using CImageResizerFilterStep< fptype, fptype > :: InSuffix;
using CImageResizerFilterStep< fptype, fptype > :: InElIncr;
using CImageResizerFilterStep< fptype, fptype > :: OutLen;
using CImageResizerFilterStep< fptype, fptype > :: OutPrefix;
using CImageResizerFilterStep< fptype, fptype > :: OutSuffix;
using CImageResizerFilterStep< fptype, fptype > :: OutElIncr;
using CImageResizerFilterStep< fptype, fptype > :: PrefixDC;
using CImageResizerFilterStep< fptype, fptype > :: SuffixDC;
using CImageResizerFilterStep< fptype, fptype > :: RPosBuf;
using CImageResizerFilterStep< fptype, fptype > :: FltBank;
using CImageResizerFilterStep< fptype, fptype > :: EdgePixelCount;
/**
* @brief Performs "packing" (de-interleaving) of a scanline, and type
* conversion. If required, the sRGB gamma correction is applied.
*
* @param ip0 Input scanline, pixel elements interleaved.
* @param op0 Output scanline, pixel elements are grouped, `l` elements
* apart.
* @param l The number of pixels to "pack".
*/
template< class Tin >
void packScanline( const Tin* const ip0, fptype* const op0,
const int l ) const
{
const int ElCount = Vars -> ElCount;
int j;
if( !Vars -> UseSRGBGamma )
{
for( j = 0; j < ElCount; j++ )
{
const Tin* ip = ip0 + j;
fptype* const op = op0 + j * InElIncr;
int i;
for( i = 0; i < l; i++ )
{
op[ i ] = (fptype) *ip;
ip += ElCount;
}
}
}
else
{
const int AlphaIndex = Vars -> AlphaIndex;
const fptype gm = (fptype) Vars -> InGammaMult;
for( j = 0; j < ElCount; j++ )
{
const Tin* ip = ip0 + j;
fptype* const op = op0 + j * InElIncr;
int i;
if( ElCount == 4 && j == AlphaIndex )
{
for( i = 0; i < l; i++ )
{
op[ i ] = (fptype) *ip * gm;
ip += ElCount;
}
}
else
{
for( i = 0; i < l; i++ )
{
op[ i ] = convertSRGB2Lin( *ip, gm );
ip += ElCount;
}
}
}
}
}
/**
* @brief Applies Linear to sRGB gamma correction to the specified
* scanline, in-place.
*
* @param[in,out] p0 Scanline.
* @param l The number of pixels to de-linearize.
* @param Vars0 Image resizing-related variables.
*/
static void applySRGBGamma( fptype* const p0, const int l,
const CImageResizerVars& Vars0 )
{
const int ElCount = Vars0.ElCount;
const int AlphaIndex = Vars0.AlphaIndex;
const fptype gm = (fptype) Vars0.OutGammaMult;
int j;
for( j = 0; j < ElCount; j++ )
{
fptype* const p = p0 + j * l;
int i;
if( ElCount == 4 && j == AlphaIndex )
{
for( i = 0; i < l; i++ )
{
p[ i ] *= gm;
}
}
else
{
for( i = 0; i < l; i++ )
{
p[ i ] = convertLin2SRGB( p[ i ]) * gm;
}
}
}
}
/**
* @brief Converts vertical scanline to horizontal scanline.
*
* This function is called by the image resizer when image is resized
* vertically. This means that the vertical scanline is stored in the
* same format produced by the packScanline() and maintained by other
* filtering functions.
*
* @param ip Input vertical scanline, pixel elements are grouped, SrcLen
* elements apart.
* @param op Output buffer (temporary buffer used during resizing), pixel
* elements are grouped, `l` elements apart.
* @param SrcLen The number of pixels in the input scanline, also used to
* calculate input buffer increment.
* @param SrcIncr Input buffer increment to the next vertical pixel.
*/
void convertVtoH( const fptype* ip, fptype* op, const int SrcLen,
const int SrcIncr ) const
{
const int ElCount = Vars -> ElCount;
const int SrcElIncr = SrcIncr / ElCount;
const int ips1 = SrcElIncr;
const int ips2 = SrcElIncr * 2;
const int ips3 = SrcElIncr * 3;
const int ops1 = InElIncr;
const int ops2 = InElIncr * 2;
const int ops3 = InElIncr * 3;
int j;
if( ElCount == 1 )
{
for( j = 0; j < SrcLen; j++ )
{
op[ 0 ] = ip[ 0 ];
ip += SrcIncr;
op++;
}
}
else
if( ElCount == 4 )
{
for( j = 0; j < SrcLen; j++ )
{
op[ 0 ] = ip[ 0 ];
op[ ops1 ] = ip[ ips1 ];
op[ ops2 ] = ip[ ips2 ];
op[ ops3 ] = ip[ ips3 ];
ip += SrcIncr;
op++;
}
}
else
if( ElCount == 3 )
{
for( j = 0; j < SrcLen; j++ )
{
op[ 0 ] = ip[ 0 ];
op[ ops1 ] = ip[ ips1 ];
op[ ops2 ] = ip[ ips2 ];
ip += SrcIncr;
op++;
}
}
else
if( ElCount == 2 )
{
for( j = 0; j < SrcLen; j++ )
{
op[ 0 ] = ip[ 0 ];
op[ ops1 ] = ip[ ips1 ];
ip += SrcIncr;
op++;
}
}
}
/**
* @brief Performs "unpacking" of a scanline, and type conversion.
*
* Truncation is used when floating point is converted to integer.
* The unpacking function assumes that scanline is stored in the style
* produced by the packScanline() function.
*
* @param ip0 Input scanline, pixel elements are grouped, `l` elements
* apart.
* @param op0 Output scanline, pixel elements are interleaved.
* @param l The number of pixels to "unpack".
* @param Vars0 Image resizing-related variables. `ElCount` is assumed to
* be equal to `ElCountIO`.
*/
template< class Tout >
static void unpackScanline( const fptype* const ip0, Tout* const op0,
const int l, const CImageResizerVars& Vars0 )
{
const int ElCount = Vars0.ElCount;
int j;
for( j = 0; j < ElCount; j++ )
{
const fptype* const ip = ip0 + j * l;
Tout* op = op0 + j;
int i;
for( i = 0; i < l; i++ )
{
*op = (Tout) ip[ i ];
op += ElCount;
}
}
}
/**
* @brief Prepares input scanline buffer for *this* filtering step.
*
* Left- and right-most pixels are replicated to make sure no buffer
* overrun happens. Such approach also allows to bypass any pointer
* range checks.
*
* @param Src Source buffer.
*/
void prepareInBuf( fptype* Src ) const
{
if( IsUpsample || InPrefix + InSuffix == 0 )
{
return;
}
int j;
for( j = 0; j < Vars -> ElCount; j++ )
{
replicateArray( Src, 1, Src - InPrefix, InPrefix, 1 );
fptype* const Src2 = Src + InLen - 1;
replicateArray( Src2, 1, Src2 + 1, InSuffix, 1 );
Src += InElIncr;
}
}
/**
* @brief Peforms scanline upsampling with filtering.
*
* @param Src Source scanline buffer (length = this -> InLen). Source
* scanline increment will be equal to ElCount.
* @param Dst Destination scanline buffer.
*/
void doUpsample( const fptype* Src, fptype* Dst ) const
{
const int elalign = Vars -> elalign;
const int opstep = ResampleFactor;
const fptype* const f = Flt;
const int flen = Flt.getCapacity();
int l;
int i;
int j;
for( j = 0; j < Vars -> ElCount; j++ )
{
const fptype* ip = Src;
fptype* op0 = &Dst[ -OutPrefix ];
memset( op0, 0, (size_t) ( OutPrefix + OutLen + OutSuffix ) *
sizeof( fptype ));
if( FltOrig.getCapacity() > 0 )
{
// Do not perform filtering, only upsample.
op0 += OutPrefix % ResampleFactor;
l = OutPrefix / ResampleFactor;
while( l > 0 )
{
op0[ 0 ] = ip[ 0 ];
op0 += opstep;
l--;
}
l = InLen - 1;
while( l > 0 )
{
op0[ 0 ] = ip[ 0 ];
op0 += opstep;
ip++;
l--;
}
l = OutSuffix / ResampleFactor;
while( l >= 0 )
{
op0[ 0 ] = ip[ 0 ];
op0 += opstep;
l--;
}
Src += InElIncr;
Dst += OutElIncr;
continue;
}
l = InPrefix;
fptypesimd ipv = (fptypesimd) ip[ 0 ];
while( l > 0 )
{
for( i = 0; i < flen; i += elalign )
{
fptypesimd :: addu( op0 + i,
fptypesimd :: load( f + i ) * ipv );
}
op0 += opstep;
l--;
}
l = InLen - 1;
while( l > 0 )
{
ipv = (fptypesimd) ip[ 0 ];
for( i = 0; i < flen; i += elalign )
{
fptypesimd :: addu( op0 + i,
fptypesimd :: load( f + i ) * ipv );
}
ip++;
op0 += opstep;
l--;
}
l = InSuffix;
ipv = (fptypesimd) ip[ 0 ];
while( l >= 0 )
{
for( i = 0; i < flen; i += elalign )
{
fptypesimd :: addu( op0 + i,
fptypesimd :: load( f + i ) * ipv );
}
op0 += opstep;
l--;
}
const fptype* dc = SuffixDC;
l = SuffixDC.getCapacity();
for( i = 0; i < l; i += elalign )
{
fptypesimd :: addu( op0 + i,
fptypesimd :: load( dc + i ) * ipv );
}
ipv = (fptypesimd) Src[ 0 ];
op0 = Dst - InPrefix * opstep;
dc = PrefixDC;
l = PrefixDC.getCapacity();
for( i = 0; i < l; i += elalign )
{
fptypesimd :: addu( op0 + i,
fptypesimd :: load( dc + i ) * ipv );
}
Src += InElIncr;
Dst += OutElIncr;
}
}
/**
* @brief Performs scanline filtering with optional downsampling.
*
* Function makes use of the symmetry of the filter.
*
* @param Src Source scanline buffer (length = this -> InLen). Source
* scanline increment will be equal to 1.
* @param Dst Destination scanline buffer.
* @param DstIncr Destination scanline buffer increment, used for
* horizontal or vertical scanline stepping.
*/
void doFilter( const fptype* const Src, fptype* Dst,
const int DstIncr ) const
{
const int ElCount = Vars -> ElCount;
const int elalign = Vars -> elalign;
const fptype* const f = &Flt[ 0 ];
const int flen = Flt.getCapacity();
const int ipstep = ResampleFactor;
int i;
int j;
if( ElCount == 1 )
{
const fptype* ip = Src - EdgePixelCount * ipstep - FltLatency;
fptype* op = Dst;
int l = OutLen;
while( l > 0 )
{
fptypesimd s = fptypesimd :: load( f ) *
fptypesimd :: loadu( ip );
for( i = elalign; i < flen; i += elalign )
{
s += fptypesimd :: load( f + i ) *
fptypesimd :: loadu( ip + i );
}
op[ 0 ] = s.hadd();
op += DstIncr;
ip += ipstep;
l--;
}
}
else
if( DstIncr == 1 )
{
for( j = 0; j < ElCount; j++ )
{
const fptype* ip = Src - EdgePixelCount * ipstep -
FltLatency + j * InElIncr;
fptype* op = Dst + j * OutElIncr;
int l = OutLen;
while( l > 0 )
{
fptypesimd s = fptypesimd :: load( f ) *
fptypesimd :: loadu( ip );
for( i = elalign; i < flen; i += elalign )
{
s += fptypesimd :: load( f + i ) *
fptypesimd :: loadu( ip + i );
}
op[ 0 ] = s.hadd();
op += DstIncr;
ip += ipstep;
l--;
}
}
}
else
{
const fptype* ip0 = Src - EdgePixelCount * ipstep - FltLatency;
fptype* op0 = Dst;
int l = OutLen;
while( l > 0 )
{
const fptype* ip = ip0;
fptype* op = op0;
for( j = 0; j < ElCount; j++ )
{
fptypesimd s = fptypesimd :: load( f ) *
fptypesimd :: loadu( ip );
for( i = elalign; i < flen; i += elalign )
{
s += fptypesimd :: load( f + i ) *
fptypesimd :: loadu( ip + i );
}
op[ 0 ] = s.hadd();
ip += InElIncr;
op += OutElIncr;
}
ip0 += ipstep;
op0 += DstIncr;
l--;
}
}
}
/**
* @brief Performs resizing of a single scanline.
*
* This function does not "know" about the length of the source scanline
* buffer. This buffer should be padded with enough pixels so that
* `SrcPos - FilterLenD2` is always positive, and
* `SrcPos + ( DstLineLen - 1 ) * k + FilterLenD2 + 1` does not exceed
* source scanline's buffer length. SrcLine's increment is assumed to be
* equal to 1.
*
* @param SrcLine Source scanline buffer.
* @param DstLine Destination (resized) scanline buffer.
* @param DstLineIncr Destination scanline position increment, used for
* horizontal or vertical scanline stepping.
* @param xx Temporary buffer, of size FltBank.getFilterLen(), must be
* aligned by fpclass :: fpalign.
*/
void doResize( const fptype* SrcLine, fptype* DstLine,
int DstLineIncr, fptype* const xx ) const
{
const int IntFltLen = FltBank -> getFilterLen();
const int ElCount = Vars -> ElCount;
const int elalign = Vars -> elalign;
const typename CImageResizerFilterStep< fptype, fptype > ::
CResizePos* rpos = &(*RPosBuf)[ 0 ];
int DstLineLen = OutLen;
int i;
int j;
#define AVIR_RESIZE_PART1 \
while( DstLineLen > 0 ) \
{ \
const fptypesimd x = (fptypesimd) rpos -> x; \
const fptype* ftp = rpos -> ftp; \
const fptype* ftp2 = rpos -> ftp + IntFltLen; \
const fptype* Src = SrcLine + rpos -> SrcOffs;
#define AVIR_RESIZE_PART1nx \
while( DstLineLen > 0 ) \
{ \
const fptype* ftp = rpos -> ftp; \
const fptype* Src = SrcLine + rpos -> SrcOffs;
#define AVIR_RESIZE_PART2 \
DstLine += DstLineIncr; \
rpos++; \
DstLineLen--; \
}
if( ElCount == 1 )
{
if( FltBank -> getOrder() == 1 )
{
AVIR_RESIZE_PART1
fptypesimd sum = ( fptypesimd :: load( ftp ) +
fptypesimd :: load( ftp2 ) * x ) *
fptypesimd :: loadu( Src );
for( i = elalign; i < IntFltLen; i += elalign )
{
sum += ( fptypesimd :: load( ftp + i ) +
fptypesimd :: load( ftp2 + i ) * x ) *
fptypesimd :: loadu( Src + i );
}
DstLine[ 0 ] = sum.hadd();
AVIR_RESIZE_PART2
}
else
{
AVIR_RESIZE_PART1nx
fptypesimd sum = fptypesimd :: load( ftp ) *
fptypesimd :: loadu( Src );
for( i = elalign; i < IntFltLen; i += elalign )
{
sum += fptypesimd :: load( ftp + i ) *
fptypesimd :: loadu( Src + i );
}
DstLine[ 0 ] = sum.hadd();
AVIR_RESIZE_PART2
}
}
else
if( DstLineIncr == 1 )
{
// Horizontal-oriented processing, element loop is outer.
const int SrcIncr = InElIncr;
const int DstLineElIncr = OutElIncr - DstLineIncr * DstLineLen;
if( FltBank -> getOrder() == 1 )
{
for( j = 0; j < ElCount; j++ )
{
AVIR_RESIZE_PART1
fptypesimd sum = 0.0;
for( i = 0; i < IntFltLen; i += elalign )
{
sum += ( fptypesimd :: load( ftp + i ) +
fptypesimd :: load( ftp2 + i ) * x ) *
fptypesimd :: loadu( Src + i );
}
DstLine[ 0 ] = sum.hadd();
AVIR_RESIZE_PART2
DstLine += DstLineElIncr;
SrcLine += SrcIncr;
DstLineLen = OutLen;
rpos = &(*RPosBuf)[ 0 ];
}
}
else
{
for( j = 0; j < ElCount; j++ )
{
AVIR_RESIZE_PART1nx
fptypesimd sum = fptypesimd :: load( ftp ) *
fptypesimd :: loadu( Src );
for( i = elalign; i < IntFltLen; i += elalign )
{
sum += fptypesimd :: load( ftp + i ) *
fptypesimd :: loadu( Src + i );
}
DstLine[ 0 ] = sum.hadd();
AVIR_RESIZE_PART2
DstLine += DstLineElIncr;
SrcLine += SrcIncr;
DstLineLen = OutLen;
rpos = &(*RPosBuf)[ 0 ];
}
}
}
else
{
const int SrcIncr = InElIncr;
const int DstLineElIncr = OutElIncr;
DstLineIncr -= DstLineElIncr * ElCount;
if( FltBank -> getOrder() == 1 )
{
AVIR_RESIZE_PART1
for( i = 0; i < IntFltLen; i += elalign )
{
( fptypesimd :: load( ftp + i ) +
fptypesimd :: load( ftp2 + i ) * x ).store( xx + i );
}
for( j = 0; j < ElCount; j++ )
{
fptypesimd sum = fptypesimd :: load( xx ) *
fptypesimd :: loadu( Src );
for( i = elalign; i < IntFltLen; i += elalign )
{
sum += fptypesimd :: load( xx + i ) *
fptypesimd :: loadu( Src + i );
}
DstLine[ 0 ] = sum.hadd();
DstLine += DstLineElIncr;
Src += SrcIncr;
}
AVIR_RESIZE_PART2
}
else
{
AVIR_RESIZE_PART1nx
for( j = 0; j < ElCount; j++ )
{
fptypesimd sum = fptypesimd :: load( ftp ) *
fptypesimd :: loadu( Src );
for( i = elalign; i < IntFltLen; i += elalign )
{
sum += fptypesimd :: load( ftp + i ) *
fptypesimd :: loadu( Src + i );
}
DstLine[ 0 ] = sum.hadd();
DstLine += DstLineElIncr;
Src += SrcIncr;
}
AVIR_RESIZE_PART2
}
}
#undef AVIR_RESIZE_PART2
#undef AVIR_RESIZE_PART1nx
#undef AVIR_RESIZE_PART1
}
/**
* @copydoc doResize()
*/
void doResize2( const fptype* SrcLine, fptype* DstLine,
int DstLineIncr, fptype* const xx ) const
{
doResize( SrcLine, DstLine, DstLineIncr, xx );
}
};
/**
* @brief Image resizer's default de-interleaved dithering class.
*
* This class defines an object that performs rounding, clipping and dithering
* operations over horizontal scanline pixels before scanline is stored in the
* output buffer.
*
* This ditherer implementation uses de-interleaved SIMD algorithm.
*
* @tparam fptype Floating point type to use for storing pixel data. SIMD
* types cannot be used.
* @tparam fptypesimd The SIMD type used to store a pack of `fptype` values.
*/
template< class fptype, class fptypesimd >
class CImageResizerDithererDefDIL
{
public:
/**
* @brief Initializes the ditherer object.
*
* @param aLen Scanline length in pixels to process.
* @param aVars Image resizing-related variables.
* @param aTrMul Bit-depth truncation multiplier. 1 - no additional
* truncation.
* @param aPkOut Peak output value allowed.
*/
void init( const int aLen, const CImageResizerVars& aVars,
const double aTrMul, const double aPkOut )
{
Len = aLen;
Vars = &aVars;
LenE = aLen * Vars -> ElCount;
TrMul0 = aTrMul;
PkOut0 = aPkOut;
}
/**
* @copydoc CImageResizerDithererDefINL::isRecursive()
*/
static bool isRecursive()
{
return( false );
}
/**
* @copydoc CImageResizerDithererDefINL::dither()
*/
void dither( fptype* const ResScanline ) const
{
const int elalign = Vars -> elalign;
const fptypesimd c0 = 0.0;
const fptypesimd PkOut = (fptypesimd) PkOut0;
int j;
if( TrMul0 == 1.0 )
{
// Optimization - do not perform bit truncation.
for( j = 0; j < LenE - elalign; j += elalign )
{
const fptypesimd z0 = round(
fptypesimd :: loadu( ResScanline + j ));
clamp( z0, c0, PkOut ).storeu( ResScanline + j );
}
const int lim = LenE - j;
const fptypesimd z0 = round(
fptypesimd :: loadu( ResScanline + j, lim ));
clamp( z0, c0, PkOut ).storeu( ResScanline + j, lim );
}
else
{
const fptypesimd TrMul = (fptypesimd) TrMul0;
const fptypesimd TrMulI = (fptypesimd) ( 1.0 / TrMul0 );
for( j = 0; j < LenE - elalign; j += elalign )
{
const fptypesimd z0 = round(
fptypesimd :: loadu( ResScanline + j ) * TrMulI ) * TrMul;
clamp( z0, c0, PkOut ).storeu( ResScanline + j );
}
const int lim = LenE - j;
const fptypesimd z0 = round( fptypesimd :: loadu(
ResScanline + j, lim ) * TrMulI ) * TrMul;
clamp( z0, c0, PkOut ).storeu( ResScanline + j, lim );
}
}
protected:
int Len; ///< Scanline's length in pixels.
const CImageResizerVars* Vars; ///< Image resizing-related variables.
int LenE; ///< = LenE * ElCount.
double TrMul0; ///< Bit-depth truncation multiplier.
double PkOut0; ///< Peak output value allowed.
};
/**
* @brief Image resizer's error-diffusion dithering class, de-interleaved
* mode.
*
* This ditherer implements error-diffusion dithering which looks good, and
* whose results are compressed by PNG well.
*
* @tparam fptype Floating point type to use for storing pixel data. SIMD
* types cannot be used.
* @tparam fptypesimd Processing type, SIMD can be used.
*/
template< class fptype, class fptypesimd >
class CImageResizerDithererErrdDIL
{
public:
/**
* Function initializes the ditherer object.
*
* @param aLen Scanline length in pixels to process.
* @param aVars Image resizing-related variables.
* @param aTrMul Bit-depth truncation multiplier. 1 - no additional
* truncation.
* @param aPkOut Peak output value allowed.
*/
void init( const int aLen, const CImageResizerVars& aVars,
const double aTrMul, const double aPkOut )
{
Len = aLen;
Vars = &aVars;
LenE = aLen * Vars -> ElCount;
TrMul0 = aTrMul;
PkOut0 = aPkOut;
ResScanlineDith0.alloc( LenE + Vars -> ElCount, sizeof( fptype ));
ResScanlineDith = ResScanlineDith0 + Vars -> ElCount;
int i;
for( i = 0; i < LenE + Vars -> ElCount; i++ )
{
ResScanlineDith0[ i ] = 0.0;
}
}
/**
* @copydoc CImageResizerDithererDefINL::isRecursive()
*/
static bool isRecursive()
{
return( true );
}
/**
* @copydoc CImageResizerDithererDefINL::dither()
*/
void dither( fptype* const ResScanline )
{
const int ea = Vars -> elalign;
const fptypesimd c0 = 0;
const fptypesimd TrMul = (fptypesimd) TrMul0;
const fptypesimd TrMulI = (fptypesimd) ( 1.0 / TrMul0 );
const fptypesimd PkOut = (fptypesimd) PkOut0;
int j;
for( j = 0; j < LenE - ea; j += ea )
{
fptypesimd :: addu( ResScanline + j,
fptypesimd :: loadu( ResScanlineDith + j ));
c0.storeu( ResScanlineDith + j );
}
int lim = LenE - j;
fptypesimd :: addu( ResScanline + j,
fptypesimd :: loadu( ResScanlineDith + j, lim ), lim );
c0.storeu( ResScanlineDith + j, lim );
const int Len1 = Len - 1;
fptype* rs = ResScanline;
fptype* rsd = ResScanlineDith;
int i;
for( i = 0; i < Vars -> ElCount; i++ )
{
for( j = 0; j < Len1; j++ )
{
// Perform rounding, noise estimation and saturation.
fptype* const rsj = rs + j;
const fptype z0 = round( rsj[ 0 ] * TrMulI ) * TrMul;
const fptype Noise = rsj[ 0 ] - z0;
rsj[ 0 ] = clamp( z0, (fptype) 0, PkOut );
fptype* const rsdj = rsd + j;
const fptype NoiseM1 = Noise * (fptype) 0.364842;
rsj[ 1 ] += NoiseM1;
rsdj[ -1 ] += Noise * (fptype) 0.207305;
rsdj[ 0 ] += NoiseM1;
rsdj[ 1 ] += Noise * (fptype) 0.063011;
}
// Process the last pixel element in scanline.
const fptype z1 = round( rs[ Len1 ] * TrMulI ) * TrMul;
const fptype Noise2 = rs[ Len1 ] - z1;
rs[ Len1 ] = clamp( z1, c0, PkOut );
rsd[ Len1 - 1 ] += Noise2 * (fptype) 0.207305;
rsd[ Len1 ] += Noise2 * (fptype) 0.364842;
rs += Len;
rsd += Len;
}
}
protected:
int Len; ///< Scanline's length in pixels.
const CImageResizerVars* Vars; ///< Image resizing-related variables.
int LenE; ///< = LenE * ElCount.
double TrMul0; ///< Bit-depth truncation multiplier.
double PkOut0; ///< Peak output value allowed.
CBuffer< fptype > ResScanlineDith0; ///< Error propagation buffer for
///< dithering, first pixel unused.
fptype* ResScanlineDith; ///< Error propagation buffer pointer which skips
///< the first ElCount elements.
};
/**
* @brief Floating-point processing definition and abstraction class for
* de-interleaved processing.
*
* This class defines several constants and typedefs that point to classes
* that should be used by the image resizing algorithm. This implementation
* points to de-interleaved processing classes.
*
* @tparam afptype Floating point type to use for storing intermediate data
* and variables. SIMD types should not be used.
* @tparam afptypesimd SIMD type used to perform processing.
* @tparam adith Ditherer class to use during processing.
*/
template< class afptype, class afptypesimd,
class adith = CImageResizerDithererDefDIL< afptype, afptypesimd > >
class fpclass_def_dil
{
public:
typedef afptype fptype; ///< Floating-point type to use during processing.
typedef afptype fptypeatom; ///< Atomic type `fptype` consists of.
static const int fppack = 1; ///< The number of atomic types stored in a
///< single `fptype` element.
static const int fpalign = sizeof( afptypesimd ); ///< Suggested alignment
///< size in bytes. This is not a required alignment, because image
///< resizing algorithm cannot be made to have a strictly aligned data
///< access in all cases (e.g. de-interleaved interpolation cannot
///< perform aligned accesses).
static const int elalign = sizeof( afptypesimd ) / sizeof( afptype ); ///<
///< Length alignment of arrays of elements. This applies to filters
///< and intermediate buffers: this constant forces filters and
///< scanlines to have a length which is a multiple of this value, for
///< more efficient SIMD implementation.
static const int packmode = 1; ///< 0 if interleaved packing, 1 if
///< de-interleaved.
typedef CImageResizerFilterStepDIL< fptype, afptypesimd > CFilterStep; ///<
///< Filtering step class to use during processing.
typedef adith CDitherer; ///< Ditherer class to use during processing.
};
} // namespace avir