-
Notifications
You must be signed in to change notification settings - Fork 43
/
avir.h
6644 lines (5751 loc) · 167 KB
/
avir.h
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
//$ nobt
//$ nocpp
/**
* @file avir.h
*
* @brief The "main" inclusion file with all required classes and functions.
*
* This is the "main" inclusion file for the "AVIR" image resizer. This
* inclusion file contains implementation of the AVIR image resizing algorithm
* in its entirety. Also includes several classes and functions that can be
* useful elsewhere.
*
* AVIR Copyright (c) 2015-2021 Aleksey Vaneev
*
* @mainpage
*
* @section intro_sec Introduction
*
* Description is available at https://github.com/avaneev/avir
*
* AVIR is devoted to women. Your digital photos can look good at any size!
*
* Please credit the author of this library in your documentation in the
* following way: "AVIR image resizing algorithm designed by Aleksey Vaneev".
*
* @section license License
*
* MIT License
*
* Copyright (c) 2015-2021 Aleksey Vaneev
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* @version 3.0
*/
#ifndef AVIR_CIMAGERESIZER_INCLUDED
#define AVIR_CIMAGERESIZER_INCLUDED
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
namespace avir {
/**
* The macro defines AVIR version string.
*/
#define AVIR_VERSION "3.0"
/**
* The macro equals to "pi" constant, fills 53-bit floating point mantissa.
* Undefined at the end of file.
*/
#define AVIR_PI 3.1415926535897932
/**
* The macro equals to "pi divided by 2" constant, fills 53-bit floating
* point mantissa. Undefined at the end of file.
*/
#define AVIR_PId2 1.5707963267948966
/**
* A special macro that defines empty copy-constructor and copy operator with
* the "private:" prefix. This macro should be used in classes that cannot be
* copied in a standard C++ way.
*/
#define AVIR_NOCTOR( ClassName ) \
private: \
ClassName( const ClassName& ) { } \
ClassName& operator = ( const ClassName& ) { return( *this ); }
/**
* Rounding function, based on the (int) typecast. Biased result. Not suitable
* for numbers >= 2^31.
*
* @param d Value to round.
* @return Rounded value. Some bias may be introduced.
*/
template< class T >
inline T round( const T d )
{
return( d < (T) 0 ? -(T) (int) ( (T) 0.5 - d ) :
(T) (int) ( d + (T) 0.5 ));
}
/**
* Template function "clamps" (clips) the specified value so that it is not
* lesser than "minv", and not greater than "maxv".
*
* @param Value Value to clamp.
* @param minv Minimal allowed value.
* @param maxv Maximal allowed value.
* @return The clamped value.
*/
template< class T >
inline T clamp( const T& Value, const T minv, const T maxv )
{
if( Value < minv )
{
return( minv );
}
else
if( Value > maxv )
{
return( maxv );
}
else
{
return( Value );
}
}
/**
* Power 2.4 approximation function, designed for sRGB gamma correction.
*
* @param x Argument, in the range 0.09 to 1.
* @return Value raised into power 2.4, approximate.
*/
template< class T >
inline T pow24_sRGB( const T x )
{
const double x2 = (double) x * x;
const double x3 = x2 * x;
const double x4 = x2 * x2;
return( (T) ( 0.0985766365536824 + 0.839474952656502 * x2 +
0.363287814061725 * x3 - 0.0125559718896615 /
( 0.12758338921578 + 0.290283465468235 * x ) -
0.231757513261358 * x - 0.0395365717969074 * x4 ));
}
/**
* Power 1/2.4 approximation function, designed for sRGB gamma correction.
*
* @param x Argument, in the range 0.003 to 1.
* @return Value raised into power 1/2.4, approximate.
*/
template< class T >
inline T pow24i_sRGB( const T x )
{
const double sx = sqrt( (double) x );
const double ssx = sqrt( sx );
const double sssx = sqrt( ssx );
return( (T) ( 0.000213364515060263 + 0.0149409239419218 * x +
0.433973412731747 * sx + ssx * ( 0.659628181609715 * sssx -
0.0380957908841466 - 0.0706476137208521 * sx )));
}
/**
* Function approximately linearizes the sRGB gamma value.
*
* @param s sRGB gamma value, in the range 0 to 1.
* @return Linearized sRGB gamma value, approximated.
*/
template< class T >
inline T convertSRGB2Lin( const T s )
{
const T a = (T) 0.055;
if( s <= (T) 0.04045 )
{
return( s / (T) 12.92 );
}
return( pow24_sRGB(( s + a ) / ( (T) 1 + a )));
}
/**
* Function approximately de-linearizes the linear gamma value.
*
* @param s Linear gamma value, in the range 0 to 1.
* @return sRGB gamma value, approximated.
*/
template< class T >
inline T convertLin2SRGB( const T s )
{
const T a = (T) 0.055;
if( s <= (T) 0.0031308 )
{
return( (T) 12.92 * s );
}
return(( (T) 1 + a ) * pow24i_sRGB( s ) - a );
}
/**
* Function converts (via typecast) specified array of type T1 values of
* length l into array of type T2 values. If T1 is the same as T2, copy
* operation is performed. When copying data at overlapping address spaces,
* "op" should be lower than "ip".
*
* @param ip Input buffer.
* @param[out] op Output buffer.
* @param l The number of elements to copy.
* @param ip Input buffer pointer increment.
* @param op Output buffer pointer increment.
*/
template< class T1, class T2 >
inline void copyArray( const T1* ip, T2* op, int l,
const int ipinc = 1, const int opinc = 1 )
{
while( l > 0 )
{
*op = (T2) *ip;
op += opinc;
ip += ipinc;
l--;
}
}
/**
* Function adds values located in array "ip" to array "op".
*
* @param ip Input buffer.
* @param[out] op Output buffer.
* @param l The number of elements to add.
* @param ip Input buffer pointer increment.
* @param op Output buffer pointer increment.
*/
template< class T1, class T2 >
inline void addArray( const T1* ip, T2* op, int l,
const int ipinc = 1, const int opinc = 1 )
{
while( l > 0 )
{
*op += *ip;
op += opinc;
ip += ipinc;
l--;
}
}
/**
* Function that replicates a set of adjacent elements several times in a row.
* This operation is usually used to replicate pixels at the start or end of
* image's scanline.
*
* @param ip Source array.
* @param ipl Source array length (usually 1..4, but can be any number).
* @param[out] op Destination buffer.
* @param l Number of times the source array should be replicated (the
* destination buffer should be able to hold ipl * l number of elements).
* @param opinc Destination buffer position increment after replicating the
* source array. This value should be equal to at least ipl.
*/
template< class T1, class T2 >
inline void replicateArray( const T1* const ip, const int ipl, T2* op, int l,
const int opinc )
{
if( ipl == 1 )
{
while( l > 0 )
{
op[ 0 ] = (T2) ip[ 0 ];
op += opinc;
l--;
}
}
else
if( ipl == 4 )
{
while( l > 0 )
{
op[ 0 ] = (T2) ip[ 0 ];
op[ 1 ] = (T2) ip[ 1 ];
op[ 2 ] = (T2) ip[ 2 ];
op[ 3 ] = (T2) ip[ 3 ];
op += opinc;
l--;
}
}
else
if( ipl == 3 )
{
while( l > 0 )
{
op[ 0 ] = (T2) ip[ 0 ];
op[ 1 ] = (T2) ip[ 1 ];
op[ 2 ] = (T2) ip[ 2 ];
op += opinc;
l--;
}
}
else
if( ipl == 2 )
{
while( l > 0 )
{
op[ 0 ] = (T2) ip[ 0 ];
op[ 1 ] = (T2) ip[ 1 ];
op += opinc;
l--;
}
}
else
{
while( l > 0 )
{
int i;
for( i = 0; i < ipl; i++ )
{
op[ i ] = (T2) ip[ i ];
}
op += opinc;
l--;
}
}
}
/**
* Function calculates frequency response of the specified FIR filter at the
* specified circular frequency. Phase can be calculated as atan2( im, re ).
* Function uses computationally-efficient oscillators instead of "cos" and
* "sin" functions.
*
* @param flt FIR filter's coefficients.
* @param fltlen Number of coefficients (taps) in the filter.
* @param th Circular frequency [0; pi].
* @param[out] re0 Resulting real part of the complex frequency response.
* @param[out] im0 Resulting imaginary part of the complex frequency response.
* @param fltlat Filter's latency in samples (taps).
*/
template< class T >
inline void calcFIRFilterResponse( const T* flt, int fltlen,
const double th, double& re0, double& im0, const int fltlat = 0 )
{
const double sincr = 2.0 * cos( th );
double cvalue1;
double svalue1;
if( fltlat == 0 )
{
cvalue1 = 1.0;
svalue1 = 0.0;
}
else
{
cvalue1 = cos( -fltlat * th );
svalue1 = sin( -fltlat * th );
}
double cvalue2 = cos( -( fltlat + 1 ) * th );
double svalue2 = sin( -( fltlat + 1 ) * th );
double re = 0.0;
double im = 0.0;
while( fltlen > 0 )
{
re += cvalue1 * flt[ 0 ];
im += svalue1 * flt[ 0 ];
flt++;
fltlen--;
double tmp = cvalue1;
cvalue1 = sincr * cvalue1 - cvalue2;
cvalue2 = tmp;
tmp = svalue1;
svalue1 = sincr * svalue1 - svalue2;
svalue2 = tmp;
}
re0 = re;
im0 = im;
}
/**
* Function normalizes FIR filter so that its frequency response at DC is
* equal to DCGain.
*
* @param[in,out] p Filter coefficients.
* @param l Filter length.
* @param DCGain Filter's gain at DC.
* @param pstep "p" array step.
*/
template< class T >
inline void normalizeFIRFilter( T* const p, const int l, const double DCGain,
const int pstep = 1 )
{
double s = 0.0;
T* pp = p;
int i = l;
while( i > 0 )
{
s += *pp;
pp += pstep;
i--;
}
s = DCGain / s;
pp = p;
i = l;
while( i > 0 )
{
*pp = (T) ( *pp * s );
pp += pstep;
i--;
}
}
/**
* @brief Memory buffer class for element array storage, with capacity
* tracking.
*
* Allows easier handling of memory blocks allocation and automatic
* deallocation for arrays (buffers) consisting of elements of specified
* class. Tracks buffer's capacity in "int" variable; unsuitable for
* allocation of very large memory blocks (with more than 2 billion elements).
*
* This class manages memory space only - it does not perform element class
* construction (initialization) operations. Buffer's required memory address
* alignment specification is supported.
*
* Uses standard library to allocate and deallocate memory.
*
* @tparam T Buffer element's type.
* @tparam capint Buffer capacity's type to use. Use size_t for large buffers.
*/
template< class T, typename capint = int >
class CBuffer
{
public:
CBuffer()
: Data( NULL )
, DataAligned( NULL )
, Capacity( 0 )
, Alignment( 0 )
{
}
/**
* Constructor creates the buffer with the specified capacity.
*
* @param aCapacity Buffer's capacity.
* @param aAlignment Buffer's required memory address alignment. 0 - use
* stdlib's default alignment.
*/
CBuffer( const capint aCapacity, const int aAlignment = 0 )
{
allocinit( aCapacity, aAlignment );
}
CBuffer( const CBuffer& Source )
{
allocinit( Source.Capacity, Source.Alignment );
if( Capacity > 0 )
{
memcpy( DataAligned, Source.DataAligned, Capacity * sizeof( T ));
}
}
~CBuffer()
{
freeData();
}
CBuffer& operator = ( const CBuffer& Source )
{
alloc( Source.Capacity, Source.Alignment );
if( Capacity > 0 )
{
memcpy( DataAligned, Source.DataAligned, Capacity * sizeof( T ));
}
return( *this );
}
/**
* Function allocates memory so that the specified number of elements
* can be stored in *this buffer object.
*
* @param aCapacity Storage for this number of elements to allocate.
* @param aAlignment Buffer's required memory address alignment,
* power-of-2 values only. 0 - use stdlib's default alignment.
*/
void alloc( const capint aCapacity, const int aAlignment = 0 )
{
freeData();
allocinit( aCapacity, aAlignment );
}
/**
* Function deallocates any previously allocated buffer.
*/
void free()
{
freeData();
Data = NULL;
DataAligned = NULL;
Capacity = 0;
Alignment = 0;
}
/**
* @return The capacity of the element buffer.
*/
capint getCapacity() const
{
return( Capacity );
}
/**
* Function "forces" *this buffer to have an arbitary capacity. Calling
* this function invalidates all further operations except deleting *this
* object. This function should not be usually used at all. Function can
* be used to "model" certain buffer capacity without calling a costly
* memory allocation function.
*
* @param NewCapacity A new "forced" capacity.
*/
void forceCapacity( const capint NewCapacity )
{
Capacity = NewCapacity;
}
/**
* Function reallocates *this buffer to a larger size so that it will be
* able to hold the specified number of elements. Downsizing is not
* performed. Alignment is not changed.
*
* @param NewCapacity New (increased) capacity.
* @param DoDataCopy "True" if data in the buffer should be retained.
*/
void increaseCapacity( const capint NewCapacity,
const bool DoDataCopy = true )
{
if( NewCapacity < Capacity )
{
return;
}
if( DoDataCopy )
{
const capint PrevCapacity = Capacity;
T* const PrevData = Data;
T* const PrevDataAligned = DataAligned;
allocinit( NewCapacity, Alignment );
if( PrevCapacity > 0 )
{
memcpy( DataAligned, PrevDataAligned,
PrevCapacity * sizeof( T ));
}
:: free( PrevData );
}
else
{
:: free( Data );
allocinit( NewCapacity, Alignment );
}
}
/**
* Function "truncates" (reduces) capacity of the buffer without
* reallocating it. Alignment is not changed.
*
* @param NewCapacity New required capacity.
*/
void truncateCapacity( const capint NewCapacity )
{
if( NewCapacity >= Capacity )
{
return;
}
Capacity = NewCapacity;
}
/**
* Function increases capacity so that the specified number of
* elements can be stored. This function increases the previous capacity
* value by third the current capacity value until space for the required
* number of elements is available. Alignment is not changed.
*
* @param ReqCapacity Required capacity.
*/
void updateCapacity( const capint ReqCapacity )
{
if( ReqCapacity <= Capacity )
{
return;
}
capint NewCapacity = Capacity;
while( NewCapacity < ReqCapacity )
{
NewCapacity += NewCapacity / 3 + 1;
}
increaseCapacity( NewCapacity );
}
operator T* () const
{
return( DataAligned );
}
private:
T* Data; ///< Element buffer pointer.
///<
T* DataAligned; ///< Memory address-aligned element buffer pointer.
///<
capint Capacity; ///< Element buffer capacity.
///<
int Alignment; ///< Memory address alignment in use. 0 - use stdlib's
///< default alignment.
///<
/**
* Internal element buffer allocation function used during object
* construction.
*
* @param aCapacity Storage for this number of elements to allocate.
* @param aAlignment Buffer's required memory address alignment. 0 - use
* stdlib's default alignment.
*/
void allocinit( const capint aCapacity, const int aAlignment )
{
if( aAlignment == 0 )
{
Data = (T*) :: malloc( aCapacity * sizeof( T ));
DataAligned = Data;
Alignment = 0;
}
else
{
Data = (T*) :: malloc( aCapacity * sizeof( T ) + aAlignment );
DataAligned = alignptr( Data, aAlignment );
Alignment = aAlignment;
}
Capacity = aCapacity;
}
/**
* Function frees a previously allocated Data buffer.
*/
void freeData()
{
:: free( Data );
}
/**
* Function modifies the specified pointer so that it becomes memory
* address-aligned.
*
* @param ptr Pointer to align.
* @param align Alignment in bytes to apply.
* @return Pointer aligned to align bytes. Works with power-of-2
* alignments only. If no alignment is necessary, "align" bytes will be
* added to the pointer value.
*/
template< class Tp >
inline Tp alignptr( const Tp ptr, const uintptr_t align )
{
return( (Tp) ( (uintptr_t) ptr + align -
( (uintptr_t) ptr & ( align - 1 ))) );
}
};
/**
* @brief Array of structured objects.
*
* Implements allocation of a linear array of objects of class T (which are
* initialized), addressable via operator[]. Each object is created via the
* "operator new". New object insertions are quick since implementation uses
* prior space allocation (capacity), thus not requiring frequent memory block
* reallocations.
*
* @tparam T Array element's type.
*/
template< class T >
class CStructArray
{
public:
CStructArray()
: ItemCount( 0 )
{
}
CStructArray( const CStructArray& Source )
: ItemCount( 0 )
, Items( Source.getItemCount() )
{
while( ItemCount < Source.getItemCount() )
{
Items[ ItemCount ] = new T( Source[ ItemCount ]);
ItemCount++;
}
}
~CStructArray()
{
clear();
}
CStructArray& operator = ( const CStructArray& Source )
{
clear();
const int NewCount = Source.ItemCount;
Items.updateCapacity( NewCount );
while( ItemCount < NewCount )
{
Items[ ItemCount ] = new T( Source[ ItemCount ]);
ItemCount++;
}
return( *this );
}
T& operator []( const int Index )
{
return( *Items[ Index ]);
}
const T& operator []( const int Index ) const
{
return( *Items[ Index ]);
}
/**
* Function creates a new object of type T with the default constructor
* and adds this object to the array.
*
* @return Reference to a newly added object.
*/
T& add()
{
if( ItemCount == Items.getCapacity() )
{
Items.increaseCapacity( ItemCount * 3 / 2 + 1 );
}
Items[ ItemCount ] = new T();
ItemCount++;
return( (*this)[ ItemCount - 1 ]);
}
/**
* Function changes number of allocated items. New items are created with
* the default constructor. If NewCount is below the current item count,
* items that are above NewCount range will be destructed.
*
* @param NewCount New requested item count.
*/
void setItemCount( const int NewCount )
{
if( NewCount > ItemCount )
{
Items.increaseCapacity( NewCount );
while( ItemCount < NewCount )
{
Items[ ItemCount ] = new T();
ItemCount++;
}
}
else
{
while( ItemCount > NewCount )
{
ItemCount--;
delete Items[ ItemCount ];
}
}
}
/**
* Function erases all items of *this array.
*/
void clear()
{
while( ItemCount > 0 )
{
ItemCount--;
delete Items[ ItemCount ];
}
}
/**
* @return The number of allocated items.
*/
int getItemCount() const
{
return( ItemCount );
}
private:
int ItemCount; ///< The number of items available in the array.
///<
CBuffer< T* > Items; ///< Element buffer.
///<
};
/**
* @brief Sine signal generator class.
*
* Class implements sine signal generator without biasing, with
* constructor-based initalization only. This generator uses oscillator
* instead of "sin" function.
*/
class CSineGen
{
public:
/**
* Constructor initializes *this sine signal generator.
*
* @param si Sine function increment, in radians.
* @param ph Starting phase, in radians. Add 0.5 * AVIR_PI for cosine
* function.
*/
CSineGen( const double si, const double ph )
: svalue1( sin( ph ))
, svalue2( sin( ph - si ))
, sincr( 2.0 * cos( si ))
{
}
/**
* @return The next value of the sine function, without biasing.
*/
double generate()
{
const double res = svalue1;
svalue1 = sincr * res - svalue2;
svalue2 = res;
return( res );
}
private:
double svalue1; ///< Current sine value.
///<
double svalue2; ///< Previous sine value.
///<
double sincr; ///< Sine value increment.
///<
};
/**
* @brief Peaked Cosine window function generator class.
*
* Class implements Peaked Cosine window function generator. Generates the
* right-handed half of the window function. The Alpha parameter of this
* window function offers the control of the balance between the early and
* later taps of the filter. E.g. at Alpha=1 both early and later taps are
* attenuated, but at Alpha=4 mostly later taps are attenuated. This offers a
* great control over ringing artifacts produced by a low-pass filter in image
* processing, without compromising achieved image sharpness.
*/
class CDSPWindowGenPeakedCosine
{
public:
/**
* Constructor initializes *this window function generator.
*
* @param aAlpha Alpha parameter, affects the peak shape (peak
* augmentation) of the window function. Any positive value can be used.
* @param aLen2 Half filter's length (non-truncated).
*/
CDSPWindowGenPeakedCosine( const double aAlpha, const double aLen2 )
: Alpha( aAlpha )
, Len2( aLen2 )
, Len2i( 1.0 / aLen2 )
, wn( 0.0 )
, w1( AVIR_PId2 / Len2, AVIR_PI * 0.5 )
{
}
/**
* @return The next Peaked Cosine window function coefficient.
*/
double generate()
{
const double h = pow( wn * Len2i, Alpha );
wn += 1.0;
return( w1.generate() * ( 1.0 - h ));
}
private:
double Alpha; ///< Alpha parameter, affects the peak shape of window.
///<
double Len2; ///< Half length of the window function.
///<
double Len2i; ///< = 1 / Len2.
///<
double wn; ///< Window function integer position. 0 - center of the
///< window function.
///<
CSineGen w1; ///< Sine-wave generator.
///<
};
/**
* @brief FIR filter-based equalizer generator.
*
* Class implements an object used to generate symmetric-odd FIR filters with
* the specified frequency response (aka paragraphic equalizer). The
* calculated filter is windowed by the Peaked Cosine window function.
*
* In image processing, due to short length of filters being used (6-8 taps)
* the resulting frequency response of the filter is approximate and may be
* mathematically imperfect, but still adequate to the visual requirements.
*
* On a side note, this equalizer generator can be successfully used for audio
* signal equalization as well: for example, it is used in almost the same
* form in Voxengo Marvel GEQ equalizer plug-in.
*
* Filter generation is based on decomposition of frequency range into
* spectral bands, with each band represented by linear and ramp "kernels".
* When the filter is built, these kernels are combined together with
* different weights that approximate the required frequency response.
*/
class CDSPFIREQ
{
public:
/**
* Function initializes *this object with the required parameters. The
* gain of frequencies beyond the MinFreq..MaxFreq range are controlled by
* the first and the last band's gain.
*
* @param SampleRate Processing sample rate (use 2 for image processing).
* @param aFilterLength Required filter length in samples (taps). The
* actual filter length is truncated to an integer value.
* @param aBandCount Number of band crossover points required to control,