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
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
|
// This file includes code for scaling images, in two versions.
// One ported from ImageMagick (slower, but can achieve better quality),
// and from Imlib2 ported by Mosfet (very fast).
// ImageMagick code begin
// ----------------------
// This code is ImageMagick's resize code, adapted for TQImage, with
// fastfloat class added as an optimization.
// The original license text follows.
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% RRRR EEEEE SSSSS IIIII ZZZZZ EEEEE %
% R R E SS I ZZ E %
% RRRR EEE SSS I ZZZ EEE %
% R R E SS I ZZ E %
% R R EEEEE SSSSS IIIII ZZZZZ EEEEE %
% %
% ImageMagick Image Resize Methods %
% %
% %
% Software Design %
% John Cristy %
% July 1992 %
% %
% %
% Copyright (C) 2003 ImageMagick Studio, a non-profit organization dedicated %
% to making software imaging solutions freely available. %
% %
% Permission is hereby granted, free of charge, to any person obtaining a %
% copy of this software and associated documentation files ("ImageMagick"), %
% to deal in ImageMagick without restriction, including without limitation %
% the rights to use, copy, modify, merge, publish, distribute, sublicense, %
% and/or sell copies of ImageMagick, and to permit persons to whom the %
% ImageMagick 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 ImageMagick. %
% %
% 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 %
% ImageMagick Studio 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 ImageMagick or the use or other dealings in %
% ImageMagick. %
% %
% Except as contained in this notice, the name of the ImageMagick Studio %
% shall not be used in advertising or otherwise to promote the sale, use or %
% other dealings in ImageMagick without prior written authorization from the %
% ImageMagick Studio. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
*/
#include "config.h"
// System
#ifdef HAVE_ENDIAN_H
#include <endian.h>
#else
#ifdef HAVE_SYS_ENDIAN_H
#include <sys/endian.h>
#endif
#endif
// TQt
#include <tqimage.h>
#include <tqcolor.h>
#include <tdeversion.h>
#include <kcpuinfo.h>
#include <string.h>
#include <stdlib.h>
// Local
#include "imageutils.h"
// everything in namespace
namespace ImageUtils {
#define Max TQMAX
#define Min TQMIN
// mustn't be less than used precision (i.e. 1/fastfloat::RATIO)
#define MagickEpsilon 0.0002
// fastfloat begin
// this class stores floating point numbers as integers, with BITS shift,
// i.e. value XYZ is stored as XYZ * RATIO
struct fastfloat
{
private:
enum { BITS = 12, RATIO = 4096 };
public:
fastfloat() {}
fastfloat( long v ) : value( v << BITS ) {}
fastfloat( int v ) : value( v << BITS ) {}
fastfloat( double v ) : value( static_cast< long >( v * RATIO + 0.5 )) {}
double toDouble() const { return static_cast< double >( value ) / RATIO; }
long toLong() const { return value >> BITS; }
fastfloat& operator += ( fastfloat r ) { value += r.value; return *this; }
fastfloat& operator -= ( fastfloat r ) { value -= r.value; return *this; }
fastfloat& operator *= ( fastfloat r ) { value = static_cast< long long >( value ) * r.value >> BITS; return *this; }
fastfloat& operator /= ( fastfloat r ) { value = ( static_cast< long long >( value ) << BITS ) / r.value; return *this; }
bool operator< ( fastfloat r ) const { return value < r.value; }
bool operator<= ( fastfloat r ) const { return value <= r.value; }
bool operator> ( fastfloat r ) const { return value > r.value; }
bool operator>= ( fastfloat r ) const { return value >= r.value; }
bool operator== ( fastfloat r ) const { return value == r.value; }
bool operator!= ( fastfloat r ) const { return value != r.value; }
fastfloat operator-() const { return fastfloat( -value, false ); }
private:
fastfloat( long v, bool ) : value( v ) {} // for operator-()
long value;
};
inline fastfloat operator+ ( fastfloat l, fastfloat r ) { return fastfloat( l ) += r; }
inline fastfloat operator- ( fastfloat l, fastfloat r ) { return fastfloat( l ) -= r; }
inline fastfloat operator* ( fastfloat l, fastfloat r ) { return fastfloat( l ) *= r; }
inline fastfloat operator/ ( fastfloat l, fastfloat r ) { return fastfloat( l ) /= r; }
inline bool operator< ( fastfloat l, double r ) { return l < fastfloat( r ); }
inline bool operator<= ( fastfloat l, double r ) { return l <= fastfloat( r ); }
inline bool operator> ( fastfloat l, double r ) { return l > fastfloat( r ); }
inline bool operator>= ( fastfloat l, double r ) { return l >= fastfloat( r ); }
inline bool operator== ( fastfloat l, double r ) { return l == fastfloat( r ); }
inline bool operator!= ( fastfloat l, double r ) { return l != fastfloat( r ); }
inline bool operator< ( double l, fastfloat r ) { return fastfloat( l ) < r ; }
inline bool operator<= ( double l, fastfloat r ) { return fastfloat( l ) <= r ; }
inline bool operator> ( double l, fastfloat r ) { return fastfloat( l ) > r ; }
inline bool operator>= ( double l, fastfloat r ) { return fastfloat( l ) >= r ; }
inline bool operator== ( double l, fastfloat r ) { return fastfloat( l ) == r ; }
inline bool operator!= ( double l, fastfloat r ) { return fastfloat( l ) != r ; }
inline double fasttodouble( fastfloat v ) { return v.toDouble(); }
inline long fasttolong( fastfloat v ) { return v.toLong(); }
#if 1 // change to 0 to turn fastfloat usage off
#else
#define fastfloat double
#define fasttodouble( v ) double( v )
#define fasttolong( v ) long( v )
#endif
//fastfloat end
typedef fastfloat (*Filter)(const fastfloat, const fastfloat);
typedef struct _ContributionInfo
{
fastfloat
weight;
long
pixel;
} ContributionInfo;
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% R e s i z e I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% ResizeImage() scales an image to the desired dimensions with one of these
% filters:
%
% Bessel Blackman Box
% Catrom Cubic Gaussian
% Hanning Hermite Lanczos
% Mitchell Point Quandratic
% Sinc Triangle
%
% Most of the filters are FIR (finite impulse response), however, Bessel,
% Gaussian, and Sinc are IIR (infinite impulse response). Bessel and Sinc
% are windowed (brought down to zero) with the Blackman filter.
%
% ResizeImage() was inspired by Paul Heckbert's zoom program.
%
% The format of the ResizeImage method is:
%
% Image *ResizeImage(Image *image,const unsigned long columns,
% const unsigned long rows,const FilterTypes filter,const double blur,
% ExceptionInfo *exception)
%
% A description of each parameter follows:
%
% o image: The image.
%
% o columns: The number of columns in the scaled image.
%
% o rows: The number of rows in the scaled image.
%
% o filter: Image filter to use.
%
% o blur: The blur factor where > 1 is blurry, < 1 is sharp.
%
% o exception: Return any errors or warnings in this structure.
%
%
*/
#if 0
static fastfloat Bessel(const fastfloat x,const fastfloat)
{
if (x == 0.0)
return(MagickPI/4.0);
return(BesselOrderOne(MagickPI*x)/(2.0*x));
}
static fastfloat Sinc(const fastfloat x,const fastfloat)
{
if (x == 0.0)
return(1.0);
return(sin(MagickPI*x)/(MagickPI*x));
}
static fastfloat Blackman(const fastfloat x,const fastfloat)
{
return(0.42+0.5*cos(MagickPI*x)+0.08*cos(2*MagickPI*x));
}
static fastfloat BlackmanBessel(const fastfloat x,const fastfloat)
{
return(Blackman(x/support,support)*Bessel(x,support));
}
static fastfloat BlackmanSinc(const fastfloat x,const fastfloat)
{
return(Blackman(x/support,support)*Sinc(x,support));
}
#endif
static fastfloat Box(const fastfloat x,const fastfloat)
{
if (x < -0.5)
return(0.0);
if (x < 0.5)
return(1.0);
return(0.0);
}
#if 0
static fastfloat Catrom(const fastfloat x,const fastfloat)
{
if (x < -2.0)
return(0.0);
if (x < -1.0)
return(0.5*(4.0+x*(8.0+x*(5.0+x))));
if (x < 0.0)
return(0.5*(2.0+x*x*(-5.0-3.0*x)));
if (x < 1.0)
return(0.5*(2.0+x*x*(-5.0+3.0*x)));
if (x < 2.0)
return(0.5*(4.0+x*(-8.0+x*(5.0-x))));
return(0.0);
}
static fastfloat Cubic(const fastfloat x,const fastfloat)
{
if (x < -2.0)
return(0.0);
if (x < -1.0)
return((2.0+x)*(2.0+x)*(2.0+x)/6.0);
if (x < 0.0)
return((4.0+x*x*(-6.0-3.0*x))/6.0);
if (x < 1.0)
return((4.0+x*x*(-6.0+3.0*x))/6.0);
if (x < 2.0)
return((2.0-x)*(2.0-x)*(2.0-x)/6.0);
return(0.0);
}
static fastfloat Gaussian(const fastfloat x,const fastfloat)
{
return(exp(-2.0*x*x)*sqrt(2.0/MagickPI));
}
static fastfloat Hanning(const fastfloat x,const fastfloat)
{
return(0.5+0.5*cos(MagickPI*x));
}
static fastfloat Hamming(const fastfloat x,const fastfloat)
{
return(0.54+0.46*cos(MagickPI*x));
}
static fastfloat Hermite(const fastfloat x,const fastfloat)
{
if (x < -1.0)
return(0.0);
if (x < 0.0)
return((2.0*(-x)-3.0)*(-x)*(-x)+1.0);
if (x < 1.0)
return((2.0*x-3.0)*x*x+1.0);
return(0.0);
}
static fastfloat Lanczos(const fastfloat x,const fastfloat support)
{
if (x < -3.0)
return(0.0);
if (x < 0.0)
return(Sinc(-x,support)*Sinc(-x/3.0,support));
if (x < 3.0)
return(Sinc(x,support)*Sinc(x/3.0,support));
return(0.0);
}
static fastfloat Mitchell(const fastfloat x,const fastfloat)
{
#define B (1.0/3.0)
#define C (1.0/3.0)
#define P0 (( 6.0- 2.0*B )/6.0)
#define P2 ((-18.0+12.0*B+ 6.0*C)/6.0)
#define P3 (( 12.0- 9.0*B- 6.0*C)/6.0)
#define Q0 (( 8.0*B+24.0*C)/6.0)
#define Q1 (( -12.0*B-48.0*C)/6.0)
#define Q2 (( 6.0*B+30.0*C)/6.0)
#define Q3 (( - 1.0*B- 6.0*C)/6.0)
if (x < -2.0)
return(0.0);
if (x < -1.0)
return(Q0-x*(Q1-x*(Q2-x*Q3)));
if (x < 0.0)
return(P0+x*x*(P2-x*P3));
if (x < 1.0)
return(P0+x*x*(P2+x*P3));
if (x < 2.0)
return(Q0+x*(Q1+x*(Q2+x*Q3)));
return(0.0);
#undef B
#undef C
#undef P0
#undef P2
#undef P3
#undef Q0
#undef Q1
#undef Q2
#undef Q3
}
#endif
// this is the same like Mitchell, but it has different values
// for B and C, resulting in sharper images
// http://sourceforge.net/mailarchive/forum.php?thread_id=7445822&forum_id=1210
static fastfloat Bicubic(const fastfloat x,const fastfloat)
{
#define B (0.0/3.0)
#define C (2.0/3.0)
#define P0 (( 6.0- 2.0*B )/6.0)
#define P2 ((-18.0+12.0*B+ 6.0*C)/6.0)
#define P3 (( 12.0- 9.0*B- 6.0*C)/6.0)
#define Q0 (( 8.0*B+24.0*C)/6.0)
#define Q1 (( -12.0*B-48.0*C)/6.0)
#define Q2 (( 6.0*B+30.0*C)/6.0)
#define Q3 (( - 1.0*B- 6.0*C)/6.0)
if (x < -2.0)
return(0.0);
if (x < -1.0)
return(Q0-x*(Q1-x*(Q2-x*Q3)));
if (x < 0.0)
return(P0+x*x*(P2-x*P3));
if (x < 1.0)
return(P0+x*x*(P2+x*P3));
if (x < 2.0)
return(Q0+x*(Q1+x*(Q2+x*Q3)));
return(0.0);
#undef B
#undef C
#undef P0
#undef P2
#undef P3
#undef Q0
#undef Q1
#undef Q2
#undef Q3
}
#if 0
static fastfloat Quadratic(const fastfloat x,const fastfloat)
{
if (x < -1.5)
return(0.0);
if (x < -0.5)
return(0.5*(x+1.5)*(x+1.5));
if (x < 0.5)
return(0.75-x*x);
if (x < 1.5)
return(0.5*(x-1.5)*(x-1.5));
return(0.0);
}
#endif
static fastfloat Triangle(const fastfloat x,const fastfloat)
{
if (x < -1.0)
return(0.0);
if (x < 0.0)
return(1.0+x);
if (x < 1.0)
return(1.0-x);
return(0.0);
}
static void HorizontalFilter(const TQImage& source,TQImage& destination,
const fastfloat x_factor,const fastfloat blur,
ContributionInfo *contribution, Filter filter, fastfloat filtersupport)
{
fastfloat
center,
density,
scale,
support;
long
n,
start,
stop,
y;
long
i,
x;
/*
Apply filter to resize horizontally from source to destination.
*/
scale=blur*Max(1.0/x_factor,1.0);
support=scale* filtersupport;
if (support <= 0.5)
{
/*
Reduce to point sampling.
*/
support=0.5+MagickEpsilon;
scale=1.0;
}
scale=1.0/scale;
for (x=0; x < (long) destination.width(); x++)
{
center=(fastfloat) (x+0.5)/x_factor;
start= fasttolong(Max(center-support+0.5,0));
stop= fasttolong(Min(center+support+0.5,source.width()));
density=0.0;
for (n=0; n < (stop-start); n++)
{
contribution[n].pixel=start+n;
contribution[n].weight=
filter (scale*(start+n-center+0.5), filtersupport );
density+=contribution[n].weight;
}
if ((density != 0.0) && (density != 1.0))
{
/*
Normalize.
*/
density=1.0/density;
for (i=0; i < n; i++)
contribution[i].weight*=density;
}
// p=AcquireImagePixels(source,contribution[0].pixel,0,contribution[n-1].pixel-
// contribution[0].pixel+1,source->rows,exception);
// q=SetImagePixels(destination,x,0,1,destination->rows);
for (y=0; y < (long) destination.height(); y++)
{
fastfloat red = 0;
fastfloat green = 0;
fastfloat blue = 0;
fastfloat alpha = 0;
for (i=0; i < n; i++)
{
int px = contribution[i].pixel;
int py = y;
TQRgb p = reinterpret_cast< TQRgb* >( const_cast<TQImage&>(source).jumpTable()[ py ])[ px ];
red+=contribution[i].weight*tqRed(p);
green+=contribution[i].weight*tqGreen(p);
blue+=contribution[i].weight*tqBlue(p);
alpha+=contribution[i].weight*tqAlpha(p);
}
TQRgb pix = tqRgba(
fasttolong( red < 0 ? 0 : red > 255 ? 255 : red + 0.5 ),
fasttolong( green < 0 ? 0 : green > 255 ? 255 : green + 0.5 ),
fasttolong( blue < 0 ? 0 : blue > 255 ? 255 : blue + 0.5 ),
fasttolong( alpha < 0 ? 0 : alpha > 255 ? 255 : alpha + 0.5 ));
reinterpret_cast< TQRgb* >( const_cast<TQImage&>(destination).jumpTable()[ y ])[ x ] = pix;
}
}
}
static void VerticalFilter(const TQImage& source,TQImage& destination,
const fastfloat y_factor,const fastfloat blur,
ContributionInfo *contribution, Filter filter, fastfloat filtersupport )
{
fastfloat
center,
density,
scale,
support;
long
n,
start,
stop,
x;
long
i,
y;
/*
Apply filter to resize vertically from source to destination.
*/
scale=blur*Max(1.0/y_factor,1.0);
support=scale* filtersupport;
if (support <= 0.5)
{
/*
Reduce to point sampling.
*/
support=0.5+MagickEpsilon;
scale=1.0;
}
scale=1.0/scale;
for (y=0; y < (long) destination.height(); y++)
{
center=(fastfloat) (y+0.5)/y_factor;
start= fasttolong(Max(center-support+0.5,0));
stop= fasttolong(Min(center+support+0.5,source.height()));
density=0.0;
for (n=0; n < (stop-start); n++)
{
contribution[n].pixel=start+n;
contribution[n].weight=
filter (scale*(start+n-center+0.5), filtersupport);
density+=contribution[n].weight;
}
if ((density != 0.0) && (density != 1.0))
{
/*
Normalize.
*/
density=1.0/density;
for (i=0; i < n; i++)
contribution[i].weight*=density;
}
// p=AcquireImagePixels(source,0,contribution[0].pixel,source->columns,
// contribution[n-1].pixel-contribution[0].pixel+1,exception);
// q=SetImagePixels(destination,0,y,destination->columns,1);
for (x=0; x < (long) destination.width(); x++)
{
fastfloat red = 0;
fastfloat green = 0;
fastfloat blue = 0;
fastfloat alpha = 0;
for (i=0; i < n; i++)
{
int px = x;
int py = contribution[i].pixel;
TQRgb p = reinterpret_cast< TQRgb* >( const_cast<TQImage&>(source).jumpTable()[ py ])[ px ];
red+=contribution[i].weight*tqRed(p);
green+=contribution[i].weight*tqGreen(p);
blue+=contribution[i].weight*tqBlue(p);
alpha+=contribution[i].weight*tqAlpha(p);
}
TQRgb pix = tqRgba(
fasttolong( red < 0 ? 0 : red > 255 ? 255 : red + 0.5 ),
fasttolong( green < 0 ? 0 : green > 255 ? 255 : green + 0.5 ),
fasttolong( blue < 0 ? 0 : blue > 255 ? 255 : blue + 0.5 ),
fasttolong( alpha < 0 ? 0 : alpha > 255 ? 255 : alpha + 0.5 ));
reinterpret_cast< TQRgb* >( const_cast<TQImage&>(destination).jumpTable()[ y ])[ x ] = pix;
}
}
}
static TQImage ResizeImage(const TQImage& image,const int columns,
const int rows, Filter filter, fastfloat filtersupport, double blur)
{
ContributionInfo
*contribution;
fastfloat
support,
x_factor,
x_support,
y_factor,
y_support;
/*
Initialize resize image attributes.
*/
if ((columns == image.width()) && (rows == image.height()) && (blur == 1.0))
return image.copy();
TQImage resize_image( columns, rows, 32 );
resize_image.setAlphaBuffer( image.hasAlphaBuffer());
/*
Allocate filter contribution info.
*/
x_factor=(fastfloat) resize_image.width()/image.width();
y_factor=(fastfloat) resize_image.height()/image.height();
// i=(long) LanczosFilter;
// if (image->filter != UndefinedFilter)
// i=(long) image->filter;
// else
// if ((image->storage_class == PseudoClass) || image->matte ||
// ((x_factor*y_factor) > 1.0))
// i=(long) MitchellFilter;
x_support=blur*Max(1.0/x_factor,1.0)*filtersupport;
y_support=blur*Max(1.0/y_factor,1.0)*filtersupport;
support=Max(x_support,y_support);
if (support < filtersupport)
support=filtersupport;
contribution=new ContributionInfo[ fasttolong( 2.0*Max(support,0.5)+3 ) ];
TQ_CHECK_PTR( contribution );
/*
Resize image.
*/
if (((fastfloat) columns*(image.height()+rows)) >
((fastfloat) rows*(image.width()+columns)))
{
TQImage source_image( columns, image.height(), 32 );
source_image.setAlphaBuffer( image.hasAlphaBuffer());
HorizontalFilter(image,source_image,x_factor,blur,
contribution,filter,filtersupport);
VerticalFilter(source_image,resize_image,y_factor,
blur,contribution,filter,filtersupport);
}
else
{
TQImage source_image( image.width(), rows, 32 );
source_image.setAlphaBuffer( image.hasAlphaBuffer());
VerticalFilter(image,source_image,y_factor,blur,
contribution,filter,filtersupport);
HorizontalFilter(source_image,resize_image,x_factor,
blur,contribution,filter,filtersupport);
}
/*
Free allocated memory.
*/
delete[] contribution;
return(resize_image);
}
#undef Max
#undef Min
#undef MagickEpsilon
// filters and their matching support values
#if 0
static const FilterInfo
filters[SincFilter+1] =
{
{ Box, 0.0 },
{ Box, 0.0 },
{ Box, 0.5 },
{ Triangle, 1.0 },
{ Hermite, 1.0 },
{ Hanning, 1.0 },
{ Hamming, 1.0 },
{ Blackman, 1.0 },
{ Gaussian, 1.25 },
{ Quadratic, 1.5 },
{ Cubic, 2.0 },
{ Catrom, 2.0 },
{ Mitchell, 2.0 },
{ Lanczos, 3.0 },
{ BlackmanBessel, 3.2383 },
{ BlackmanSinc, 4.0 }
};
#endif
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% S a m p l e I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% SampleImage() scales an image to the desired dimensions with pixel
% sampling. Unlike other scaling methods, this method does not introduce
% any additional color into the scaled image.
%
% The format of the SampleImage method is:
%
% Image *SampleImage(const Image *image,const unsigned long columns,
% const unsigned long rows,ExceptionInfo *exception)
%
% A description of each parameter follows:
%
% o image: The image.
%
% o columns: The number of columns in the sampled image.
%
% o rows: The number of rows in the sampled image.
%
% o exception: Return any errors or warnings in this structure.
%
%
*/
TQImage SampleImage(const TQImage& image,const int columns,
const int rows)
{
int
*x_offset,
*y_offset;
long
j,
y;
uchar
*pixels;
const uchar
*p;
long
x;
uchar
*q;
/*
Initialize sampled image attributes.
*/
if ((columns == image.width()) && (rows == image.height()))
return image;
// This function is modified to handle any image depth, not only
// 32bit like the ImageMagick original. This avoids the relatively
// expensive conversion.
const int d = image.depth() / 8;
TQImage sample_image( columns, rows, image.depth());
sample_image.setAlphaBuffer( image.hasAlphaBuffer());
/*
Allocate scan line buffer and column offset buffers.
*/
pixels= new uchar[ image.width() * d ];
x_offset= new int[ sample_image.width() ];
y_offset= new int[ sample_image.height() ];
/*
Initialize pixel offsets.
*/
// In the following several code 0.5 needs to be added, otherwise the image
// would be moved by half a pixel to bottom-right, just like
// with TQt's TQImage::scale()
for (x=0; x < (long) sample_image.width(); x++)
{
x_offset[x]=int((x+0.5)*image.width()/sample_image.width());
}
for (y=0; y < (long) sample_image.height(); y++)
{
y_offset[y]=int((y+0.5)*image.height()/sample_image.height());
}
/*
Sample each row.
*/
j=(-1);
for (y=0; y < (long) sample_image.height(); y++)
{
q= sample_image.scanLine( y );
if (j != y_offset[y] )
{
/*
Read a scan line.
*/
j= y_offset[y];
p= image.scanLine( j );
(void) memcpy(pixels,p,image.width()*d);
}
/*
Sample each column.
*/
switch( d )
{
case 1: // 8bit
for (x=0; x < (long) sample_image.width(); x++)
{
*q++=pixels[ x_offset[x] ];
}
break;
case 4: // 32bit
for (x=0; x < (long) sample_image.width(); x++)
{
*(TQRgb*)q=((TQRgb*)pixels)[ x_offset[x] ];
q += d;
}
break;
default:
for (x=0; x < (long) sample_image.width(); x++)
{
memcpy( q, pixels + x_offset[x] * d, d );
q += d;
}
break;
}
}
if( d != 4 ) // != 32bit
{
sample_image.setNumColors( image.numColors());
for( int i = 0; i < image.numColors(); ++i )
sample_image.setColor( i, image.color( i ));
}
delete[] y_offset;
delete[] x_offset;
delete[] pixels;
return sample_image;
}
// ImageMagick code end
// Imlib2/Mosfet code begin
// ------------------------
// This code is Imlib2 code, additionally modified by Mosfet, and with few small
// modifications for Gwenview. The MMX scaling code also belongs to it.
// The original license texts follow.
/**
* This is the normal smoothscale method, based on Imlib2's smoothscale.
*
* Originally I took the algorithm used in NetPBM and TQt and added MMX/3dnow
* optimizations. It ran in about 1/2 the time as TQt. Then I ported Imlib's
* C algorithm and it ran at about the same speed as my MMX optimized one...
* Finally I ported Imlib's MMX version and it ran in less than half the
* time as my MMX algorithm, (taking only a quarter of the time TQt does).
*
* Changes include formatting, namespaces and other C++'ings, removal of old
* #ifdef'ed code, and removal of unneeded border calculation code.
*
* Imlib2 is (C) Carsten Haitzler and various contributors. The MMX code
* is by Willem Monsuwe <willem@stack.nl>. All other modifications are
* (C) Daniel M. Duley.
*/
/*
Copyright (C) 2004 Daniel M. Duley <dan.duley@verizon.net>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
Copyright (C) 2000 Carsten Haitzler and various contributors (see AUTHORS)
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 of the Software and its Copyright notices. In addition publicly
documented acknowledgment must be given that this software has been used if no
source code of this software is made available publicly. This includes
acknowledgments in either Copyright notices, Manuals, Publicity and Marketing
documents or any documentation provided with any product containing this
software. This License does not apply to any software that links to the
libraries provided by this software (statically or dynamically), but only to
the software provided.
Please see the COPYING.PLAIN for a plain-english explanation of this notice
and it's intent.
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 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.
*/
namespace MImageScale{
typedef struct __mimage_scale_info
{
int *xpoints;
unsigned int **ypoints;
int *xapoints, *yapoints;
int xup_yup;
} MImageScaleInfo;
unsigned int** mimageCalcYPoints(unsigned int *src, int sow, int sh,
int dh);
int* mimageCalcXPoints(int sw, int dw);
int* mimageCalcApoints(int s, int d, int up);
MImageScaleInfo* mimageFreeScaleInfo(MImageScaleInfo *isi);
MImageScaleInfo *mimageCalcScaleInfo(TQImage &img, int sw, int sh,
int dw, int dh, char aa, int sow);
void mimageSampleRGBA(MImageScaleInfo *isi, unsigned int *dest, int dxx,
int dyy, int dx, int dy, int dw, int dh, int dow);
void mimageScaleAARGBA(MImageScaleInfo *isi, unsigned int *dest, int dxx,
int dyy, int dx, int dy, int dw, int dh, int dow,
int sow);
void mimageScaleAARGB(MImageScaleInfo *isi, unsigned int *dest, int dxx,
int dyy, int dx, int dy, int dw, int dh, int dow, int
sow);
TQImage smoothScale(const TQImage& img, int dw, int dh);
typedef long long llong;
}
#ifdef HAVE_X86_MMX
extern "C" {
void __mimageScale_mmx_AARGBA(MImageScale::MImageScaleInfo *isi,
unsigned int *dest, int dxx, int dyy,
int dx, int dy, int dw, int dh,
int dow, int sow);
}
#endif
using namespace MImageScale;
TQImage MImageScale::smoothScale(const TQImage& image, int dw, int dh)
{
TQImage img = image.depth() < 32 ? image.convertDepth( 32 ) : image;
int w = img.width();
int h = img.height();
int sow = img.bytesPerLine();
// handle CroppedTQImage
if( img.height() > 1 && sow != img.scanLine( 1 ) - img.scanLine( 0 ))
sow = img.scanLine( 1 ) - img.scanLine( 0 );
sow = sow / ( img.depth() / 8 );
MImageScaleInfo *scaleinfo =
mimageCalcScaleInfo(img, w, h, dw, dh, true, sow);
if(!scaleinfo)
return TQImage();
TQImage buffer(dw, dh, 32);
buffer.setAlphaBuffer(img.hasAlphaBuffer());
#ifdef HAVE_X86_MMX
//#warning Using MMX Smoothscale
bool haveMMX = KCPUInfo::haveExtension( KCPUInfo::IntelMMX );
if(haveMMX){
__mimageScale_mmx_AARGBA(scaleinfo, (unsigned int *)buffer.scanLine(0),
0, 0, 0, 0, dw, dh, dw, sow);
}
else
#endif
{
if(img.hasAlphaBuffer())
mimageScaleAARGBA(scaleinfo, (unsigned int *)buffer.scanLine(0), 0, 0,
0, 0, dw, dh, dw, sow);
else
mimageScaleAARGB(scaleinfo, (unsigned int *)buffer.scanLine(0), 0, 0,
0, 0, dw, dh, dw, sow);
}
mimageFreeScaleInfo(scaleinfo);
return(buffer);
}
//
// Code ported from Imlib...
//
// FIXME: replace with mRed, etc... These work on pointers to pixels, not
// pixel values
#if BYTE_ORDER == BIG_ENDIAN
#define A_VAL(p) ((unsigned char *)(p))[0]
#define R_VAL(p) ((unsigned char *)(p))[1]
#define G_VAL(p) ((unsigned char *)(p))[2]
#define B_VAL(p) ((unsigned char *)(p))[3]
#elif BYTE_ORDER == LITTLE_ENDIAN
#define A_VAL(p) ((unsigned char *)(p))[3]
#define R_VAL(p) ((unsigned char *)(p))[2]
#define G_VAL(p) ((unsigned char *)(p))[1]
#define B_VAL(p) ((unsigned char *)(p))[0]
#else
#error "BYTE_ORDER is not defined"
#endif
#define INV_XAP (256 - xapoints[x])
#define XAP (xapoints[x])
#define INV_YAP (256 - yapoints[dyy + y])
#define YAP (yapoints[dyy + y])
unsigned int** MImageScale::mimageCalcYPoints(unsigned int *src,
int sow, int sh, int dh)
{
unsigned int **p;
int i, j = 0;
int rv = 0;
llong val, inc;
if(dh < 0){
dh = -dh;
rv = 1;
}
p = new unsigned int* [dh+1];
val = 0;
inc = (llong(sh) << 16) / dh;
for(i = 0; i < dh; i++){
p[j++] = src + ((val >> 16) * sow);
val += inc;
}
if(rv){
for(i = dh / 2; --i >= 0; ){
unsigned int *tmp = p[i];
p[i] = p[dh - i - 1];
p[dh - i - 1] = tmp;
}
}
return(p);
}
int* MImageScale::mimageCalcXPoints(int sw, int dw)
{
int *p, i, j = 0;
int rv = 0;
llong val, inc;
if(dw < 0){
dw = -dw;
rv = 1;
}
p = new int[dw+1];
val = 0;
inc = (llong(sw) << 16) / dw;
for(i = 0; i < dw; i++){
p[j++] = (val >> 16);
val += inc;
}
if(rv){
for(i = dw / 2; --i >= 0; ){
int tmp = p[i];
p[i] = p[dw - i - 1];
p[dw - i - 1] = tmp;
}
}
return(p);
}
int* MImageScale::mimageCalcApoints(int s, int d, int up)
{
int *p, i, j = 0, rv = 0;
if(d < 0){
rv = 1;
d = -d;
}
p = new int[d];
/* scaling up */
if(up){
llong val, inc;
val = 0;
inc = (llong(s) << 16) / d;
for(i = 0; i < d; i++){
p[j++] = (val >> 8) - ((val >> 8) & 0xffffff00);
if((val >> 16) >= (s - 1))
p[j - 1] = 0;
val += inc;
}
}
/* scaling down */
else{
llong val, inc;
int ap, Cp;
val = 0;
inc = (llong(s) << 16) / d;
Cp = ((llong(d) << 14) / s) + 1;
for(i = 0; i < d; i++){
ap = ((0x100 - ((val >> 8) & 0xff)) * Cp) >> 8;
p[j] = ap | (Cp << 16);
j++;
val += inc;
}
}
if(rv){
int tmp;
for(i = d / 2; --i >= 0; ){
tmp = p[i];
p[i] = p[d - i - 1];
p[d - i - 1] = tmp;
}
}
return(p);
}
MImageScaleInfo* MImageScale::mimageFreeScaleInfo(MImageScaleInfo *isi)
{
if(isi){
delete[] isi->xpoints;
delete[] isi->ypoints;
delete[] isi->xapoints;
delete[] isi->yapoints;
delete isi;
}
return(NULL);
}
MImageScaleInfo* MImageScale::mimageCalcScaleInfo(TQImage &img, int sw, int sh,
int dw, int dh, char aa, int sow)
{
MImageScaleInfo *isi;
int scw, sch;
scw = dw * img.width() / sw;
sch = dh * img.height() / sh;
isi = new MImageScaleInfo;
if(!isi)
return(NULL);
memset(isi, 0, sizeof(MImageScaleInfo));
isi->xup_yup = (abs(dw) >= sw) + ((abs(dh) >= sh) << 1);
isi->xpoints = mimageCalcXPoints(img.width(), scw);
if(!isi->xpoints)
return(mimageFreeScaleInfo(isi));
isi->ypoints = mimageCalcYPoints((unsigned int *)img.scanLine(0),
sow, img.height(), sch );
if (!isi->ypoints)
return(mimageFreeScaleInfo(isi));
if(aa){
isi->xapoints = mimageCalcApoints(img.width(), scw, isi->xup_yup & 1);
if(!isi->xapoints)
return(mimageFreeScaleInfo(isi));
isi->yapoints = mimageCalcApoints(img.height(), sch, isi->xup_yup & 2);
if(!isi->yapoints)
return(mimageFreeScaleInfo(isi));
}
return(isi);
}
/* scale by pixel sampling only */
void MImageScale::mimageSampleRGBA(MImageScaleInfo *isi, unsigned int *dest,
int dxx, int dyy, int dx, int dy, int dw,
int dh, int dow)
{
unsigned int *sptr, *dptr;
int x, y, end;
unsigned int **ypoints = isi->ypoints;
int *xpoints = isi->xpoints;
/* whats the last pixel ont he line so we stop there */
end = dxx + dw;
/* go through every scanline in the output buffer */
for(y = 0; y < dh; y++){
/* get the pointer to the start of the destination scanline */
dptr = dest + dx + ((y + dy) * dow);
/* calculate the source line we'll scan from */
sptr = ypoints[dyy + y];
/* go thru the scanline and copy across */
for(x = dxx; x < end; x++)
*dptr++ = sptr[xpoints[x]];
}
}
/* FIXME: NEED to optimise ScaleAARGBA - currently its "ok" but needs work*/
/* scale by area sampling */
void MImageScale::mimageScaleAARGBA(MImageScaleInfo *isi, unsigned int *dest,
int dxx, int dyy, int dx, int dy, int dw,
int dh, int dow, int sow)
{
unsigned int *sptr, *dptr;
int x, y, end;
unsigned int **ypoints = isi->ypoints;
int *xpoints = isi->xpoints;
int *xapoints = isi->xapoints;
int *yapoints = isi->yapoints;
end = dxx + dw;
/* scaling up both ways */
if(isi->xup_yup == 3){
/* go through every scanline in the output buffer */
for(y = 0; y < dh; y++){
/* calculate the source line we'll scan from */
dptr = dest + dx + ((y + dy) * dow);
sptr = ypoints[dyy + y];
if(YAP > 0){
for(x = dxx; x < end; x++){
int r, g, b, a;
int rr, gg, bb, aa;
unsigned int *pix;
if(XAP > 0){
pix = ypoints[dyy + y] + xpoints[x];
r = R_VAL(pix) * INV_XAP;
g = G_VAL(pix) * INV_XAP;
b = B_VAL(pix) * INV_XAP;
a = A_VAL(pix) * INV_XAP;
pix++;
r += R_VAL(pix) * XAP;
g += G_VAL(pix) * XAP;
b += B_VAL(pix) * XAP;
a += A_VAL(pix) * XAP;
pix += sow;
rr = R_VAL(pix) * XAP;
gg = G_VAL(pix) * XAP;
bb = B_VAL(pix) * XAP;
aa = A_VAL(pix) * XAP;
pix--;
rr += R_VAL(pix) * INV_XAP;
gg += G_VAL(pix) * INV_XAP;
bb += B_VAL(pix) * INV_XAP;
aa += A_VAL(pix) * INV_XAP;
r = ((rr * YAP) + (r * INV_YAP)) >> 16;
g = ((gg * YAP) + (g * INV_YAP)) >> 16;
b = ((bb * YAP) + (b * INV_YAP)) >> 16;
a = ((aa * YAP) + (a * INV_YAP)) >> 16;
*dptr++ = tqRgba(r, g, b, a);
}
else{
pix = ypoints[dyy + y] + xpoints[x];
r = R_VAL(pix) * INV_YAP;
g = G_VAL(pix) * INV_YAP;
b = B_VAL(pix) * INV_YAP;
a = A_VAL(pix) * INV_YAP;
pix += sow;
r += R_VAL(pix) * YAP;
g += G_VAL(pix) * YAP;
b += B_VAL(pix) * YAP;
a += A_VAL(pix) * YAP;
r >>= 8;
g >>= 8;
b >>= 8;
a >>= 8;
*dptr++ = tqRgba(r, g, b, a);
}
}
}
else{
for(x = dxx; x < end; x++){
int r, g, b, a;
unsigned int *pix;
if(XAP > 0){
pix = ypoints[dyy + y] + xpoints[x];
r = R_VAL(pix) * INV_XAP;
g = G_VAL(pix) * INV_XAP;
b = B_VAL(pix) * INV_XAP;
a = A_VAL(pix) * INV_XAP;
pix++;
r += R_VAL(pix) * XAP;
g += G_VAL(pix) * XAP;
b += B_VAL(pix) * XAP;
a += A_VAL(pix) * XAP;
r >>= 8;
g >>= 8;
b >>= 8;
a >>= 8;
*dptr++ = tqRgba(r, g, b, a);
}
else
*dptr++ = sptr[xpoints[x] ];
}
}
}
}
/* if we're scaling down vertically */
else if(isi->xup_yup == 1){
/*\ 'Correct' version, with math units prepared for MMXification \*/
int Cy, j;
unsigned int *pix;
int r, g, b, a, rr, gg, bb, aa;
int yap;
/* go through every scanline in the output buffer */
for(y = 0; y < dh; y++){
Cy = YAP >> 16;
yap = YAP & 0xffff;
dptr = dest + dx + ((y + dy) * dow);
for(x = dxx; x < end; x++){
pix = ypoints[dyy + y] + xpoints[x];
r = (R_VAL(pix) * yap) >> 10;
g = (G_VAL(pix) * yap) >> 10;
b = (B_VAL(pix) * yap) >> 10;
a = (A_VAL(pix) * yap) >> 10;
for(j = (1 << 14) - yap; j > Cy; j -= Cy){
pix += sow;
r += (R_VAL(pix) * Cy) >> 10;
g += (G_VAL(pix) * Cy) >> 10;
b += (B_VAL(pix) * Cy) >> 10;
a += (A_VAL(pix) * Cy) >> 10;
}
if(j > 0){
pix += sow;
r += (R_VAL(pix) * j) >> 10;
g += (G_VAL(pix) * j) >> 10;
b += (B_VAL(pix) * j) >> 10;
a += (A_VAL(pix) * j) >> 10;
}
if(XAP > 0){
pix = ypoints[dyy + y] + xpoints[x] + 1;
rr = (R_VAL(pix) * yap) >> 10;
gg = (G_VAL(pix) * yap) >> 10;
bb = (B_VAL(pix) * yap) >> 10;
aa = (A_VAL(pix) * yap) >> 10;
for(j = (1 << 14) - yap; j > Cy; j -= Cy){
pix += sow;
rr += (R_VAL(pix) * Cy) >> 10;
gg += (G_VAL(pix) * Cy) >> 10;
bb += (B_VAL(pix) * Cy) >> 10;
aa += (A_VAL(pix) * Cy) >> 10;
}
if(j > 0){
pix += sow;
rr += (R_VAL(pix) * j) >> 10;
gg += (G_VAL(pix) * j) >> 10;
bb += (B_VAL(pix) * j) >> 10;
aa += (A_VAL(pix) * j) >> 10;
}
r = r * INV_XAP;
g = g * INV_XAP;
b = b * INV_XAP;
a = a * INV_XAP;
r = (r + ((rr * XAP))) >> 12;
g = (g + ((gg * XAP))) >> 12;
b = (b + ((bb * XAP))) >> 12;
a = (a + ((aa * XAP))) >> 12;
}
else{
r >>= 4;
g >>= 4;
b >>= 4;
a >>= 4;
}
*dptr = tqRgba(r, g, b, a);
dptr++;
}
}
}
/* if we're scaling down horizontally */
else if(isi->xup_yup == 2){
/*\ 'Correct' version, with math units prepared for MMXification \*/
int Cx, j;
unsigned int *pix;
int r, g, b, a, rr, gg, bb, aa;
int xap;
/* go through every scanline in the output buffer */
for(y = 0; y < dh; y++){
dptr = dest + dx + ((y + dy) * dow);
for(x = dxx; x < end; x++){
Cx = XAP >> 16;
xap = XAP & 0xffff;
pix = ypoints[dyy + y] + xpoints[x];
r = (R_VAL(pix) * xap) >> 10;
g = (G_VAL(pix) * xap) >> 10;
b = (B_VAL(pix) * xap) >> 10;
a = (A_VAL(pix) * xap) >> 10;
for(j = (1 << 14) - xap; j > Cx; j -= Cx){
pix++;
r += (R_VAL(pix) * Cx) >> 10;
g += (G_VAL(pix) * Cx) >> 10;
b += (B_VAL(pix) * Cx) >> 10;
a += (A_VAL(pix) * Cx) >> 10;
}
if(j > 0){
pix++;
r += (R_VAL(pix) * j) >> 10;
g += (G_VAL(pix) * j) >> 10;
b += (B_VAL(pix) * j) >> 10;
a += (A_VAL(pix) * j) >> 10;
}
if(YAP > 0){
pix = ypoints[dyy + y] + xpoints[x] + sow;
rr = (R_VAL(pix) * xap) >> 10;
gg = (G_VAL(pix) * xap) >> 10;
bb = (B_VAL(pix) * xap) >> 10;
aa = (A_VAL(pix) * xap) >> 10;
for(j = (1 << 14) - xap; j > Cx; j -= Cx){
pix++;
rr += (R_VAL(pix) * Cx) >> 10;
gg += (G_VAL(pix) * Cx) >> 10;
bb += (B_VAL(pix) * Cx) >> 10;
aa += (A_VAL(pix) * Cx) >> 10;
}
if(j > 0){
pix++;
rr += (R_VAL(pix) * j) >> 10;
gg += (G_VAL(pix) * j) >> 10;
bb += (B_VAL(pix) * j) >> 10;
aa += (A_VAL(pix) * j) >> 10;
}
r = r * INV_YAP;
g = g * INV_YAP;
b = b * INV_YAP;
a = a * INV_YAP;
r = (r + ((rr * YAP))) >> 12;
g = (g + ((gg * YAP))) >> 12;
b = (b + ((bb * YAP))) >> 12;
a = (a + ((aa * YAP))) >> 12;
}
else{
r >>= 4;
g >>= 4;
b >>= 4;
a >>= 4;
}
*dptr = tqRgba(r, g, b, a);
dptr++;
}
}
}
/* if we're scaling down horizontally & vertically */
else{
/*\ 'Correct' version, with math units prepared for MMXification:
|*| The operation 'b = (b * c) >> 16' translates to pmulhw,
|*| so the operation 'b = (b * c) >> d' would translate to
|*| psllw (16 - d), %mmb; pmulh %mmc, %mmb
\*/
int Cx, Cy, i, j;
unsigned int *pix;
int a, r, g, b, ax, rx, gx, bx;
int xap, yap;
for(y = 0; y < dh; y++){
Cy = YAP >> 16;
yap = YAP & 0xffff;
dptr = dest + dx + ((y + dy) * dow);
for(x = dxx; x < end; x++){
Cx = XAP >> 16;
xap = XAP & 0xffff;
sptr = ypoints[dyy + y] + xpoints[x];
pix = sptr;
sptr += sow;
rx = (R_VAL(pix) * xap) >> 9;
gx = (G_VAL(pix) * xap) >> 9;
bx = (B_VAL(pix) * xap) >> 9;
ax = (A_VAL(pix) * xap) >> 9;
pix++;
for(i = (1 << 14) - xap; i > Cx; i -= Cx){
rx += (R_VAL(pix) * Cx) >> 9;
gx += (G_VAL(pix) * Cx) >> 9;
bx += (B_VAL(pix) * Cx) >> 9;
ax += (A_VAL(pix) * Cx) >> 9;
pix++;
}
if(i > 0){
rx += (R_VAL(pix) * i) >> 9;
gx += (G_VAL(pix) * i) >> 9;
bx += (B_VAL(pix) * i) >> 9;
ax += (A_VAL(pix) * i) >> 9;
}
r = (rx * yap) >> 14;
g = (gx * yap) >> 14;
b = (bx * yap) >> 14;
a = (ax * yap) >> 14;
for(j = (1 << 14) - yap; j > Cy; j -= Cy){
pix = sptr;
sptr += sow;
rx = (R_VAL(pix) * xap) >> 9;
gx = (G_VAL(pix) * xap) >> 9;
bx = (B_VAL(pix) * xap) >> 9;
ax = (A_VAL(pix) * xap) >> 9;
pix++;
for(i = (1 << 14) - xap; i > Cx; i -= Cx){
rx += (R_VAL(pix) * Cx) >> 9;
gx += (G_VAL(pix) * Cx) >> 9;
bx += (B_VAL(pix) * Cx) >> 9;
ax += (A_VAL(pix) * Cx) >> 9;
pix++;
}
if(i > 0){
rx += (R_VAL(pix) * i) >> 9;
gx += (G_VAL(pix) * i) >> 9;
bx += (B_VAL(pix) * i) >> 9;
ax += (A_VAL(pix) * i) >> 9;
}
r += (rx * Cy) >> 14;
g += (gx * Cy) >> 14;
b += (bx * Cy) >> 14;
a += (ax * Cy) >> 14;
}
if(j > 0){
pix = sptr;
sptr += sow;
rx = (R_VAL(pix) * xap) >> 9;
gx = (G_VAL(pix) * xap) >> 9;
bx = (B_VAL(pix) * xap) >> 9;
ax = (A_VAL(pix) * xap) >> 9;
pix++;
for(i = (1 << 14) - xap; i > Cx; i -= Cx){
rx += (R_VAL(pix) * Cx) >> 9;
gx += (G_VAL(pix) * Cx) >> 9;
bx += (B_VAL(pix) * Cx) >> 9;
ax += (A_VAL(pix) * Cx) >> 9;
pix++;
}
if(i > 0){
rx += (R_VAL(pix) * i) >> 9;
gx += (G_VAL(pix) * i) >> 9;
bx += (B_VAL(pix) * i) >> 9;
ax += (A_VAL(pix) * i) >> 9;
}
r += (rx * j) >> 14;
g += (gx * j) >> 14;
b += (bx * j) >> 14;
a += (ax * j) >> 14;
}
R_VAL(dptr) = r >> 5;
G_VAL(dptr) = g >> 5;
B_VAL(dptr) = b >> 5;
A_VAL(dptr) = a >> 5;
dptr++;
}
}
}
}
/* scale by area sampling - IGNORE the ALPHA byte*/
void MImageScale::mimageScaleAARGB(MImageScaleInfo *isi, unsigned int *dest,
int dxx, int dyy, int dx, int dy, int dw,
int dh, int dow, int sow)
{
unsigned int *sptr, *dptr;
int x, y, end;
unsigned int **ypoints = isi->ypoints;
int *xpoints = isi->xpoints;
int *xapoints = isi->xapoints;
int *yapoints = isi->yapoints;
end = dxx + dw;
/* scaling up both ways */
if(isi->xup_yup == 3){
/* go through every scanline in the output buffer */
for(y = 0; y < dh; y++){
/* calculate the source line we'll scan from */
dptr = dest + dx + ((y + dy) * dow);
sptr = ypoints[dyy + y];
if(YAP > 0){
for(x = dxx; x < end; x++){
int r = 0, g = 0, b = 0;
int rr = 0, gg = 0, bb = 0;
unsigned int *pix;
if(XAP > 0){
pix = ypoints[dyy + y] + xpoints[x];
r = R_VAL(pix) * INV_XAP;
g = G_VAL(pix) * INV_XAP;
b = B_VAL(pix) * INV_XAP;
pix++;
r += R_VAL(pix) * XAP;
g += G_VAL(pix) * XAP;
b += B_VAL(pix) * XAP;
pix += sow;
rr = R_VAL(pix) * XAP;
gg = G_VAL(pix) * XAP;
bb = B_VAL(pix) * XAP;
pix --;
rr += R_VAL(pix) * INV_XAP;
gg += G_VAL(pix) * INV_XAP;
bb += B_VAL(pix) * INV_XAP;
r = ((rr * YAP) + (r * INV_YAP)) >> 16;
g = ((gg * YAP) + (g * INV_YAP)) >> 16;
b = ((bb * YAP) + (b * INV_YAP)) >> 16;
*dptr++ = tqRgba(r, g, b, 0xff);
}
else{
pix = ypoints[dyy + y] + xpoints[x];
r = R_VAL(pix) * INV_YAP;
g = G_VAL(pix) * INV_YAP;
b = B_VAL(pix) * INV_YAP;
pix += sow;
r += R_VAL(pix) * YAP;
g += G_VAL(pix) * YAP;
b += B_VAL(pix) * YAP;
r >>= 8;
g >>= 8;
b >>= 8;
*dptr++ = tqRgba(r, g, b, 0xff);
}
}
}
else{
for(x = dxx; x < end; x++){
int r = 0, g = 0, b = 0;
unsigned int *pix;
if(XAP > 0){
pix = ypoints[dyy + y] + xpoints[x];
r = R_VAL(pix) * INV_XAP;
g = G_VAL(pix) * INV_XAP;
b = B_VAL(pix) * INV_XAP;
pix++;
r += R_VAL(pix) * XAP;
g += G_VAL(pix) * XAP;
b += B_VAL(pix) * XAP;
r >>= 8;
g >>= 8;
b >>= 8;
*dptr++ = tqRgba(r, g, b, 0xff);
}
else
*dptr++ = sptr[xpoints[x] ];
}
}
}
}
/* if we're scaling down vertically */
else if(isi->xup_yup == 1){
/*\ 'Correct' version, with math units prepared for MMXification \*/
int Cy, j;
unsigned int *pix;
int r, g, b, rr, gg, bb;
int yap;
/* go through every scanline in the output buffer */
for(y = 0; y < dh; y++){
Cy = YAP >> 16;
yap = YAP & 0xffff;
dptr = dest + dx + ((y + dy) * dow);
for(x = dxx; x < end; x++){
pix = ypoints[dyy + y] + xpoints[x];
r = (R_VAL(pix) * yap) >> 10;
g = (G_VAL(pix) * yap) >> 10;
b = (B_VAL(pix) * yap) >> 10;
pix += sow;
for(j = (1 << 14) - yap; j > Cy; j -= Cy){
r += (R_VAL(pix) * Cy) >> 10;
g += (G_VAL(pix) * Cy) >> 10;
b += (B_VAL(pix) * Cy) >> 10;
pix += sow;
}
if(j > 0){
r += (R_VAL(pix) * j) >> 10;
g += (G_VAL(pix) * j) >> 10;
b += (B_VAL(pix) * j) >> 10;
}
if(XAP > 0){
pix = ypoints[dyy + y] + xpoints[x] + 1;
rr = (R_VAL(pix) * yap) >> 10;
gg = (G_VAL(pix) * yap) >> 10;
bb = (B_VAL(pix) * yap) >> 10;
pix += sow;
for(j = (1 << 14) - yap; j > Cy; j -= Cy){
rr += (R_VAL(pix) * Cy) >> 10;
gg += (G_VAL(pix) * Cy) >> 10;
bb += (B_VAL(pix) * Cy) >> 10;
pix += sow;
}
if(j > 0){
rr += (R_VAL(pix) * j) >> 10;
gg += (G_VAL(pix) * j) >> 10;
bb += (B_VAL(pix) * j) >> 10;
}
r = r * INV_XAP;
g = g * INV_XAP;
b = b * INV_XAP;
r = (r + ((rr * XAP))) >> 12;
g = (g + ((gg * XAP))) >> 12;
b = (b + ((bb * XAP))) >> 12;
}
else{
r >>= 4;
g >>= 4;
b >>= 4;
}
*dptr = tqRgba(r, g, b, 0xff);
dptr++;
}
}
}
/* if we're scaling down horizontally */
else if(isi->xup_yup == 2){
/*\ 'Correct' version, with math units prepared for MMXification \*/
int Cx, j;
unsigned int *pix;
int r, g, b, rr, gg, bb;
int xap;
/* go through every scanline in the output buffer */
for(y = 0; y < dh; y++){
dptr = dest + dx + ((y + dy) * dow);
for(x = dxx; x < end; x++){
Cx = XAP >> 16;
xap = XAP & 0xffff;
pix = ypoints[dyy + y] + xpoints[x];
r = (R_VAL(pix) * xap) >> 10;
g = (G_VAL(pix) * xap) >> 10;
b = (B_VAL(pix) * xap) >> 10;
pix++;
for(j = (1 << 14) - xap; j > Cx; j -= Cx){
r += (R_VAL(pix) * Cx) >> 10;
g += (G_VAL(pix) * Cx) >> 10;
b += (B_VAL(pix) * Cx) >> 10;
pix++;
}
if(j > 0){
r += (R_VAL(pix) * j) >> 10;
g += (G_VAL(pix) * j) >> 10;
b += (B_VAL(pix) * j) >> 10;
}
if(YAP > 0){
pix = ypoints[dyy + y] + xpoints[x] + sow;
rr = (R_VAL(pix) * xap) >> 10;
gg = (G_VAL(pix) * xap) >> 10;
bb = (B_VAL(pix) * xap) >> 10;
pix++;
for(j = (1 << 14) - xap; j > Cx; j -= Cx){
rr += (R_VAL(pix) * Cx) >> 10;
gg += (G_VAL(pix) * Cx) >> 10;
bb += (B_VAL(pix) * Cx) >> 10;
pix++;
}
if(j > 0){
rr += (R_VAL(pix) * j) >> 10;
gg += (G_VAL(pix) * j) >> 10;
bb += (B_VAL(pix) * j) >> 10;
}
r = r * INV_YAP;
g = g * INV_YAP;
b = b * INV_YAP;
r = (r + ((rr * YAP))) >> 12;
g = (g + ((gg * YAP))) >> 12;
b = (b + ((bb * YAP))) >> 12;
}
else{
r >>= 4;
g >>= 4;
b >>= 4;
}
*dptr = tqRgba(r, g, b, 0xff);
dptr++;
}
}
}
/* fully optimized (i think) - onyl change of algorithm can help */
/* if we're scaling down horizontally & vertically */
else{
/*\ 'Correct' version, with math units prepared for MMXification \*/
int Cx, Cy, i, j;
unsigned int *pix;
int r, g, b, rx, gx, bx;
int xap, yap;
for(y = 0; y < dh; y++){
Cy = YAP >> 16;
yap = YAP & 0xffff;
dptr = dest + dx + ((y + dy) * dow);
for(x = dxx; x < end; x++){
Cx = XAP >> 16;
xap = XAP & 0xffff;
sptr = ypoints[dyy + y] + xpoints[x];
pix = sptr;
sptr += sow;
rx = (R_VAL(pix) * xap) >> 9;
gx = (G_VAL(pix) * xap) >> 9;
bx = (B_VAL(pix) * xap) >> 9;
pix++;
for(i = (1 << 14) - xap; i > Cx; i -= Cx){
rx += (R_VAL(pix) * Cx) >> 9;
gx += (G_VAL(pix) * Cx) >> 9;
bx += (B_VAL(pix) * Cx) >> 9;
pix++;
}
if(i > 0){
rx += (R_VAL(pix) * i) >> 9;
gx += (G_VAL(pix) * i) >> 9;
bx += (B_VAL(pix) * i) >> 9;
}
r = (rx * yap) >> 14;
g = (gx * yap) >> 14;
b = (bx * yap) >> 14;
for(j = (1 << 14) - yap; j > Cy; j -= Cy){
pix = sptr;
sptr += sow;
rx = (R_VAL(pix) * xap) >> 9;
gx = (G_VAL(pix) * xap) >> 9;
bx = (B_VAL(pix) * xap) >> 9;
pix++;
for(i = (1 << 14) - xap; i > Cx; i -= Cx){
rx += (R_VAL(pix) * Cx) >> 9;
gx += (G_VAL(pix) * Cx) >> 9;
bx += (B_VAL(pix) * Cx) >> 9;
pix++;
}
if(i > 0){
rx += (R_VAL(pix) * i) >> 9;
gx += (G_VAL(pix) * i) >> 9;
bx += (B_VAL(pix) * i) >> 9;
}
r += (rx * Cy) >> 14;
g += (gx * Cy) >> 14;
b += (bx * Cy) >> 14;
}
if(j > 0){
pix = sptr;
sptr += sow;
rx = (R_VAL(pix) * xap) >> 9;
gx = (G_VAL(pix) * xap) >> 9;
bx = (B_VAL(pix) * xap) >> 9;
pix++;
for(i = (1 << 14) - xap; i > Cx; i -= Cx){
rx += (R_VAL(pix) * Cx) >> 9;
gx += (G_VAL(pix) * Cx) >> 9;
bx += (B_VAL(pix) * Cx) >> 9;
pix++;
}
if(i > 0){
rx += (R_VAL(pix) * i) >> 9;
gx += (G_VAL(pix) * i) >> 9;
bx += (B_VAL(pix) * i) >> 9;
}
r += (rx * j) >> 14;
g += (gx * j) >> 14;
b += (bx * j) >> 14;
}
R_VAL(dptr) = r >> 5;
G_VAL(dptr) = g >> 5;
B_VAL(dptr) = b >> 5;
dptr++;
}
}
}
}
// Imlib2/Mosfet code end
// public functions :
// ------------------
// This function returns how many pixels around the zoomed area should be
// included in the image. This is used when doing incremental painting, because
// some smoothing algorithms use surrounding pixels and not including them
// could sometimes make the edges between incremental steps visible.
int extraScalePixels( SmoothAlgorithm alg, double zoom, double blur )
{
double filtersupport = 0;
Filter filter = NULL;
switch( alg ) {
case SMOOTH_NONE:
filter = NULL;
filtersupport = 0.0;
break;
case SMOOTH_FAST:
filter = Box;
filtersupport = 0.5;
break;
case SMOOTH_NORMAL:
filter = Triangle;
filtersupport = 1.0;
break;
case SMOOTH_BEST:
// filter = Mitchell;
filter = Bicubic;
filtersupport = 2.0;
break;
}
if( zoom == 1.0 || filtersupport == 0.0 ) return 0;
// Imlib2/Mosfet scale - I have really no idea how many pixels it needs
if( filter == Box && blur == 1.0 ) return int( 3 / zoom + 1 );
// This is support size for ImageMagick's scaling.
double scale=blur*TQMAX(1.0/zoom,1.0);
double support=scale* filtersupport;
if (support <= 0.5) support=0.5+0.000001;
return int( support + 1 );
}
TQImage scale(const TQImage& image, int width, int height,
SmoothAlgorithm alg, TQImage::ScaleMode mode, double blur )
{
if( image.isNull()) return image.copy();
TQSize newSize( image.size() );
newSize.scale( TQSize( width, height ), (TQSize::ScaleMode)mode ); // ### remove cast in TQt 4.0
newSize = newSize.expandedTo( TQSize( 1, 1 )); // make sure it doesn't become null
if ( newSize == image.size() ) return image.copy();
width = newSize.width();
height = newSize.height();
Filter filter = NULL;
fastfloat filtersupport;
switch( alg ) {
case SMOOTH_NONE:
filter = NULL;
filtersupport = 0.0;
break;
case SMOOTH_FAST:
filter = Box;
filtersupport = 0.5;
break;
case SMOOTH_NORMAL:
default:
filter = Triangle;
filtersupport = 1.0;
break;
case SMOOTH_BEST:
// filter = Mitchell;
filter = Bicubic;
filtersupport = 2.0;
break;
}
if( filter == Box && blur == 1.0 )
return MImageScale::smoothScale( image, width, height );
if( filter == Box && width > image.width() && height > image.height() && blur == 1.0 ) {
filter = NULL; // Box doesn't really smooth when enlarging
}
if( filter == NULL ) {
return SampleImage( image, width, height ); // doesn't need 32bit
}
return ResizeImage( image.convertDepth( 32 ), width, height, filter, filtersupport, blur );
// unlike TQt's smoothScale() this function introduces new colors to grayscale images ... oh well
}
} // namespace
|