File: filterhpa.cpp

package info (click to toggle)
hplip 3.22.10%2Bdfsg0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 171,296 kB
  • sloc: python: 83,373; ansic: 71,016; cpp: 65,202; sh: 4,408; perl: 4,397; makefile: 937
file content (1967 lines) | stat: -rw-r--r-- 79,289 bytes parent folder | download | duplicates (6)
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
/*****************************************************************************\
  filterhpa.cpp : Implimentation for the TErnieFilter class

  Copyright (c) 1996 - 2015, HP Co.
  All rights reserved.

  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.
  3. Neither the name of HP nor the names of its
     contributors may be used to endorse or promote products derived
     from this software without specific prior written permission.

  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, PATENT INFRINGEMENT; 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.
\*****************************************************************************/


#if defined(APDK_DJ9xxVIP) && defined(APDK_VIP_COLORFILTERING)

#include "header.h"

// copied from vob \di_research on 10/31/00
// MODIFICATIONS BY GE:
// 0. remove Windows header references
// 1. define assert
// 2. set iRastersReady, iRastersDelivered in submitrowtofilter
// 3. (constructor) allocate (and delete in destructor) buffers for fRowPtr
//      (instead of setting it to input buffers, since we reuse input buffers)
// 4. copy data into fRowPtr in submitrowtofilter

//#define assert ASSERT

#include "ernieplatform.h"
#include "filterhpa.h"

#if kGatherStats == 1
extern int blockStats[];
#endif

#if ((kMemWritesOptimize != 1) && (kMemWritesOptimize != 0))
#error "kMemWritesOptimize must be 0 or 1"
#endif

APDK_BEGIN_NAMESPACE

inline void AverageNRound(bool roundGreenDown, int &rFinal, int &r0, int &r1, int &gFinal, int &g0, int &g1, int &bFinal, int &b0, int &b1);
inline void AverageNRound(bool roundGreenDown, int &rFinal, int &r0, int &r1, int &gFinal, int &g0, int &g1, int &bFinal, int &b0, int &b1)
{
    // By rounding G in the other direction than R and B L* variations are minimized while mathematically alternate rounding is accomplished. EGW 2 Dec. 1999.
    if (roundGreenDown)
    {
        rFinal = (r0 + r1 + 1) / 2;
        gFinal = (g0 + g1) / 2;
        bFinal = (b0 + b1 + 1) / 2;
    }
    else
    {
        rFinal = (r0 + r1) / 2;
        gFinal = (g0 + g1 + 1) / 2;
        bFinal = (b0 + b1) / 2;
    }
}


// Filter1RawRow.  To be used to filter an odd row for which we don't have a pair,
// found at the bottom of bands that aren't divisable by 2.  This routine
// filters its row horizontally forming 4x1 and 2x1 blocks.
void TErnieFilter::Filter1RawRow(unsigned char *currPtr, int rowWidthInPixels, unsigned int *flagsPtr)
{
    ASSERT(currPtr);
    ASSERT(rowWidthInPixels > 0);

    int R0, G0, B0, R1, G1, B1, lastR, lastG, lastB;
    const unsigned int maxErrorForFourPixels = fMaxErrorForTwoPixels / 2;
//    const unsigned int maxErrorForEightPixels = maxErrorForFourPixels / 2;

//    int currPixel, lastPixel;
    uint32_t currPixel, lastPixel;
    bool lastPairAveraged = false;
    bool last2by2Averaged = false;


    for (int pixelNum = 0; pixelNum < rowWidthInPixels; pixelNum++)
    {
        if ((pixelNum & 0x03) == 0x00) // 0,4,8...
        {
            last2by2Averaged = false; // Reinitialize every four columns;
        }

        currPixel = get4Pixel(currPtr);

        flagsPtr[0] = (e11n|e11s);  // Initialize in case nothing is found for this column

        if (isWhite(currPixel))
        {
            flagsPtr[0] = eDone;
#if kGatherStats == 1
            blockStats[esWhiteFound]++;
#endif
        }

        // Currently we bail entirely if there is white. Later we may still do RLE on the non white pixel if one is present.
        if (flagsPtr[0] == (e11n|e11s))
        {
            R1 = GetRed(currPixel);
            G1 = GetGreen(currPixel);
            B1 = GetBlue(currPixel);

            // Can only horizontally average every other pixel, much like the 2x2 blocks.
            if (isOdd(pixelNum))
            {
                // do horizontal on current raster
                lastPixel = get4Pixel(currPtr,-1);
                lastR = GetRed(lastPixel);
                lastG = GetGreen(lastPixel);
                lastB = GetBlue(lastPixel);
                if ((fMaxErrorForTwoPixels >= 3) && (NewDeltaE(lastR, R1, lastG, G1, lastB, B1, fMaxErrorForTwoPixels)))
                {
                    /*   - -
                        |   | build 2x1
                         - -
                    */
                    int didNotBuild4by1 = true;
#if kGatherStats == 1
                    blockStats[es21nw]++;
#endif
                    AverageNRound(isOdd(pixelNum), lastR, lastR, R1, lastG, lastG, G1, lastB, lastB, B1);
                    if ((pixelNum >= 3) && (flagsPtr[-3] & e21nw)) // 4,5,6,7,12,13,14,15,20...
                    {
                        // Look for a 4x1
                        ASSERT(!((flagsPtr[-3] | flagsPtr[-2] | flagsPtr[-1] | flagsPtr[0]) & eTheRest)); // no vertical blocks

                        lastPixel = get4Pixel(currPtr,-3);
                        R0 = GetRed(lastPixel);
                        G0 = GetGreen(lastPixel);
                        B0 = GetBlue(lastPixel);
                        if ((maxErrorForFourPixels >= 3) && (NewDeltaE(lastR, R0, lastG, G0, lastB, B0, maxErrorForFourPixels)))
                        {
                            /*   - - - -
                                |       | build 4x1
                                 - - - -
                            */
#if kGatherStats == 1
                            blockStats[es41ni]++;
#endif
                            didNotBuild4by1 = false;
                            AverageNRound((pixelNum & 0x04)== 0x04, lastR, lastR, R0, lastG, lastG, G0, lastB, lastB, B0); // 4,5,6,7,12,13,14,15,20...

                            if(littleEndian)
                                currPixel = (lastR<<16) + (lastG<<8) + lastB;
                            else if(bigEndian)
                                currPixel = (lastR<<24) + (lastG<<16) + (lastB<<8);

#if kMemWritesOptimize == 0
                            put4Pixel(currPtr, -3, currPixel);
                            put4Pixel(currPtr, -2, currPixel);
                            put4Pixel(currPtr, -1, currPixel);
                            put4Pixel(currPtr, 0, currPixel);
#else
                            put4Pixel(currPtr, -3, currPixel);
#endif
                            flagsPtr[-3] = (flagsPtr[-3] & ~eNorths) | e41ni;
                            flagsPtr[-2] = (flagsPtr[-2] & ~eNorths) | e41n;
                            flagsPtr[-1] = (flagsPtr[-1] & ~eNorths) | e41n;
                            flagsPtr[0] = (flagsPtr[0] & ~eNorths) | e41n;
                        }
                    }

                    if (didNotBuild4by1) // Not a 4x1 so output 2x1.
                    {
                        ASSERT(!((flagsPtr[-1] | flagsPtr[0]) & eTheRest)); // no vertical blocks

                        if(littleEndian)
                            currPixel = (lastR<<16) + (lastG<<8) + lastB;
                        else if(bigEndian)
                            currPixel = (lastR<<24) + (lastG<<16) + (lastB<<8);

#if kMemWritesOptimize == 0
                        put4Pixel(currPtr, -1, currPixel);
                        put4Pixel(currPtr, 0, currPixel);
#else
                        put4Pixel(currPtr, -1, currPixel);
#endif
                        flagsPtr[-1] = (flagsPtr[-1] & ~eNorths) | e21nw;
                        flagsPtr[0] = (flagsPtr[0] & ~eNorths) | e21ne;
                    }
                }  // If DeltaE... Looking for two by one
            }  // IsOdd(pixelNum)
        }
        else // no flag bits set.
        {
            lastPairAveraged = false; // EGW Fixes bug on business graphics. 11/24/97
        }

        currPtr += eBufferedPixelWidthInBytes;
        flagsPtr++;
    }      // for each pixel...
}

// Filter2RawRows:  Looks filter two raw rows together to form blocks.  Vertical
// blocks are prefered over horizontal ones.  The routine will create 1x2 blocks
// before it will create 4x1's.  In total this routine will create 1x2, 2x2, 4x2,
// 4x1, and 2x1 blocks sizes, with the potential for two seperate 4x1's or 2x1's
// in the upper and lower rasters.
void TErnieFilter::Filter2RawRows(unsigned char *currPtr, unsigned char *upPtr, int rowWidthInPixels, unsigned int *flagsPtr)
{
    ASSERT(currPtr);
    ASSERT(upPtr);
    ASSERT(rowWidthInPixels > 0);

    int R0, G0, B0, R1, G1, B1, lastR, lastG, lastB;
    const unsigned int maxErrorForFourPixels = fMaxErrorForTwoPixels / 2;
    const unsigned int maxErrorForEightPixels = maxErrorForFourPixels / 2;

//    int currPixel, upPixel, lastPixel;
    uint32_t currPixel, upPixel, lastPixel;
    bool lastPairAveraged = false;
    bool last2by2Averaged = false;

    for (int pixelNum = 0; pixelNum < rowWidthInPixels; pixelNum++)
    {
        if ((pixelNum & 0x03) == 0x00) // 0,4,8...
        {
            last2by2Averaged = false; // Reinitialize every four columns;
        }

        upPixel = get4Pixel(upPtr);
        currPixel = get4Pixel(currPtr);

        flagsPtr[0] = (e11n|e11s);  // Initialize in case nothing is found for this column

        if (isWhite(upPixel) && isWhite(currPixel)) // both white?
        {
            flagsPtr[0] = eDone;
#if kGatherStats == 1
            blockStats[esWhiteFound]++;
#endif
        }

        // Do vertical average on the current 2 pixel high column

        // Currently we bail entirely if there is white. Later we may still do RLE on the non white pixel if one is present.
        if (flagsPtr[0] == (e11n|e11s))
        {
            R1 = GetRed(currPixel);
            G1 = GetGreen(currPixel);
            B1 = GetBlue(currPixel);

            R0 = GetRed(upPixel);
            G0 = GetGreen(upPixel);
            B0 = GetBlue(upPixel);

            if ((fMaxErrorForTwoPixels >= 3) && (NewDeltaE(R0, R1, G0, G1, B0, B1, fMaxErrorForTwoPixels)))
            {
                /*   _
                    | | build 1x2
                    | |
                     -
                */
                ASSERT(flagsPtr[0] == (e11n|e11s));
                flagsPtr[0] = e12;
#if kGatherStats == 1
                blockStats[es12]++;
#endif
                R1 = GetRed(currPixel);
                G1 = GetGreen(currPixel);
                B1 = GetBlue(currPixel);

                R0 = GetRed(upPixel);
                G0 = GetGreen(upPixel);
                B0 = GetBlue(upPixel);

                AverageNRound(isOdd(pixelNum), R1, R1, R0, G1, G1, G0, B1, B1, B0);

                // look for a 2x2 block average on every other column
                if (isOdd(pixelNum))
                {   // It looks like we are at the end of a 2x2 block
                    if (lastPairAveraged)
                    {
                        // Last pair was averaged so it's ok to try to make a 2x2 block
                        if ((maxErrorForFourPixels >= 3) && (NewDeltaE(lastR, R1, lastG, G1,lastB, B1, maxErrorForFourPixels)))
                        {
                            /* - -
                              |   | build 2x2
                              |   |
                               - -
                            */
                            ASSERT(flagsPtr[-1] == e12);
                            int didNotBuild4by2 = true;
#if kGatherStats == 1
                            blockStats[es22w]++;
#endif
                            flagsPtr[-1] = e22w;
                            flagsPtr[0] = e22e;

                            AverageNRound((pixelNum & 0x02) == 0x02, R1, R1, lastR, G1, G1, lastG, B1, B1, lastB); // 2,3,6,7... Alternate between rounding up and down for these 2x2 blocks

                            if ((pixelNum & 0x03) == 0x03)  // 3,7,11,15... Looking for a 4x2 block to average
                            {
                                if (last2by2Averaged)
                                {
                                    /*   - -   - -
                                        |   | |   | We have two 2x2s.
                                        |   | |   |
                                         - -   - -
                                    */

                                    lastPixel = get4Pixel(upPtr, -3); // Go back to previous 2x2 block and get the pixel
                                    lastR = GetRed(lastPixel);
                                    lastG = GetGreen(lastPixel);
                                    lastB = GetBlue(lastPixel);
                                    if ((maxErrorForEightPixels >= 3) && (NewDeltaE(lastR, R1, lastG, G1,lastB, B1, maxErrorForEightPixels)))
                                    {


                                        /* - - - -
                                          |       | build 4x2.
                                          |       |
                                           - - - -
                                        */
#if kGatherStats == 1
                                        blockStats[es42i]++;
#endif
                                        didNotBuild4by2 = false;

                                        flagsPtr[-3] = e42i;
                                        flagsPtr[-2] = flagsPtr[-1] = flagsPtr[0] = e42;

                                        AverageNRound((pixelNum & 0x04) == 0x04, R1, R1, lastR, G1, G1, lastG, B1, B1, lastB); // 4,5,6,7,12,13,14,15,20... Alternate between rounding up down for these 4x2 blocks

                                        if(littleEndian)
                                            currPixel = (R1<<16) + (G1<<8) + B1;
                                        else if(bigEndian)
                                            currPixel = (R1<<24) + (G1<<16) + (B1<<8);

#if kMemWritesOptimize == 0
                                        put4Pixel(upPtr, -3, currPixel);
                                        put4Pixel(upPtr, -2, currPixel);
                                        put4Pixel(upPtr, -1, currPixel);
                                        put4Pixel(upPtr,  0, currPixel);
                                        put4Pixel(currPtr, -3, currPixel);
                                        put4Pixel(currPtr, -2, currPixel);
                                        put4Pixel(currPtr, -1, currPixel);
                                        put4Pixel(currPtr, 0, currPixel);
#else
                                        put4Pixel(upPtr, -3, currPixel);
#endif
                                    }
                                }

                                if (didNotBuild4by2)
                                {   // The first 2x2 block of this pair of 2x2 blocks wasn't averaged.
                                    /*    - -    - -
                                         |X X|  |   | not averaged block and averaged 2x2.
                                         |X X|  |   |
                                          - -    - -
                                    */

                                    last2by2Averaged = true;

                                    if(littleEndian)
                                        currPixel = (R1<<16) + (G1<<8) + B1;
                                    else if(bigEndian)
                                        currPixel = (R1<<24) + (G1<<16) + (B1<<8);

#if kMemWritesOptimize == 0
                                    put4Pixel(upPtr, -1, currPixel);
                                    put4Pixel(upPtr, 0, currPixel);
                                    put4Pixel(currPtr, -1, currPixel);
                                    put4Pixel(currPtr, 0, currPixel);
#else
                                    put4Pixel(upPtr, -1, currPixel);
#endif
                                }
                            }
                            else  // Not looking for a 4x2 block yet so just output this 2x2 block for now.
                            {
                                /*    - -    - -
                                     |   |  |? ?| 1st 2x2 and maybe another later.
                                     |   |  |? ?|
                                      - -    - -
                                */

                                last2by2Averaged = true;

                                if(littleEndian)
                                    currPixel = (R1<<16) + (G1<<8) + B1;
                                else if(bigEndian)
                                    currPixel = (R1<<24) + (G1<<16) + (B1<<8);

#if kMemWritesOptimize == 0
                                put4Pixel(upPtr, -1, currPixel);
                                put4Pixel(upPtr, 0, currPixel);
                                put4Pixel(currPtr, -1, currPixel);
                                put4Pixel(currPtr, 0, currPixel);
#else
                                put4Pixel(upPtr, -1, currPixel);
#endif
                            }
                        }
                        else  // The two averaged columns are not close enough in Delta E
                        {
                            /*  -    _
                               | |  | | 2 1x2 blocks
                               | |  | |
                                -    -
                            */

                            last2by2Averaged = false;

                            if(littleEndian)
                                currPixel = (R1<<16) + (G1<<8) + B1;
                            else if(bigEndian)
                                currPixel = (R1<<24) + (G1<<16) + (B1<<8);

#if kMemWritesOptimize == 0
                            put4Pixel(upPtr, 0, currPixel);
                            put4Pixel(currPtr, 0, currPixel);
#else
                            put4Pixel(upPtr,0, currPixel);
#endif
                        }
                        lastR = R1;
                        lastG = G1;
                        lastB = B1;
                        lastPairAveraged = true;
                    }
                    else  // This is the right place for 2x2 averaging but the previous column wasn't averaged
                    {
                        /*     -
                            X | | Two non averaged pixels and a 1x2.
                            X | |
                               -
                        */
                        last2by2Averaged = false;
                        lastPairAveraged = true;
                        lastR = R1;
                        lastG = G1;
                        lastB = B1;

                        if(littleEndian)
                            currPixel = (R1<<16) + (G1<<8) + B1;
                        else if(bigEndian)
                            currPixel = (R1<<24) + (G1<<16) + (B1<<8);

#if kMemWritesOptimize == 0
                        put4Pixel(upPtr, 0, currPixel);
                        put4Pixel(currPtr, 0, currPixel);
#else
                        put4Pixel(upPtr, 0, currPixel);
#endif
                    }
                }
                else  // Not on the boundary for a 2x2 block, so just output current averaged 1x2 column
                {
                    /*    -
                         | | ?  1x2
                         | | ?
                          -
                    */

                    lastPairAveraged = true;
                    lastR = R1;
                    lastG = G1;
                    lastB = B1;

                    if(littleEndian)
                        currPixel = (R1<<16) + (G1<<8) + B1;
                    else if(bigEndian)
                        currPixel = (R1<<24) + (G1<<16) + (B1<<8);

#if kMemWritesOptimize == 0
                    put4Pixel(upPtr, 0, currPixel);
                    put4Pixel(currPtr, 0, currPixel);
#else
                    put4Pixel(upPtr, 0, currPixel);
#endif
                }
            }
            else if (lastPairAveraged)
            {   // This is the case where we can't average current column and the last column was averaged.
                // Don't do anything if last pair was averaged and this one can't be

                /*    -
                     | | X 1x2 averaged block and two non averaged pixels.
                     | | X
                      -
                */

                lastPairAveraged = false;
            }
            else
             // can't vertically average current column so look for some horizontal averaging as a fallback
             // Only do it if the last pair wasn't averaged either because we don't want to mess up a vertical averaging
             // just to create a possible horizontal averaging.
            {
                // Can only horizontally average every other pixel, much like the 2x2 blocks.
                if (isOdd(pixelNum))
                {
                    // do horizontal averaging on previous raster
                    lastPixel = get4Pixel(upPtr,-1);
                    lastR = GetRed(lastPixel);
                    lastG = GetGreen(lastPixel);
                    lastB = GetBlue(lastPixel);
                    if (((fMaxErrorForTwoPixels >= 3)) && (NewDeltaE(lastR, R0, lastG, G0,lastB, B0, fMaxErrorForTwoPixels)))
                    {
                        /*   - -
                            |   | build upper 2x1
                             - -
                        */
#if kGatherStats == 1
                        blockStats[es21nw]++;
#endif
                        int didNotBuild4by1 = true;

                        AverageNRound(isOdd(pixelNum), lastR, lastR, R0, lastG, lastG, G0, lastB, lastB, B0);

                        if ((pixelNum >= 3) && (flagsPtr[-3] & e21nw)) // 4,5,6,7,12,13,14,15,20...
                        {
                            ASSERT(!((flagsPtr[-3] | flagsPtr[-2] | flagsPtr[-1] | flagsPtr[0]) & eTheRest)); // no vertical blocks

                            // Attempt an upper 4x1
                            lastPixel = get4Pixel(upPtr,-3);
                            R0 = GetRed(lastPixel);
                            G0 = GetGreen(lastPixel);
                            B0 = GetBlue(lastPixel);
                            if ( (maxErrorForFourPixels >= 3) && (NewDeltaE(lastR, R0, lastG, G0,lastB, B0, maxErrorForFourPixels)))
                            {
                                /*   - - - -
                                    |       | build upper 4x1
                                     - - - -
                                */
#if kGatherStats == 1
                                blockStats[es41ni]++;
#endif
                                didNotBuild4by1 = false;
                                AverageNRound((pixelNum & 0x04)== 0x04, lastR, lastR, R0, lastG, lastG, G0, lastB, lastB, B0); // 4,5,6,7,12,13,14,15,20...

                                if(littleEndian)
                                    currPixel = (lastR<<16) + (lastG<<8) + lastB;
                                else if(bigEndian)
                                    currPixel = (lastR<<24) + (lastG<<16) + (lastB<<8);

#if kMemWritesOptimize == 0
                                put4Pixel(upPtr, -3, currPixel);
                                put4Pixel(upPtr, -2, currPixel);
                                put4Pixel(upPtr, -1, currPixel);
                                put4Pixel(upPtr, 0, currPixel);
#else
                                put4Pixel(upPtr, -3, currPixel);
#endif

                                ASSERT(!((flagsPtr[-3] | flagsPtr[-2] | flagsPtr[-1] | flagsPtr[0]) & eTheRest)); // no vertical blocks

                                flagsPtr[-3] = (flagsPtr[-3] & ~eNorths) | e41ni;
                                flagsPtr[-2] = (flagsPtr[-2] & ~eNorths) | e41n;
                                flagsPtr[-1] = (flagsPtr[-1] & ~eNorths) | e41n;
                                flagsPtr[0] = (flagsPtr[0] & ~eNorths) | e41n;
                            }
                        }

                        if (didNotBuild4by1) // Not an upper 4x1 so output upper 2x1.
                        {
                            if(littleEndian)
                                currPixel = (lastR<<16) + (lastG<<8) + lastB;
                            else if(bigEndian)
                                currPixel = (lastR<<24) + (lastG<<16) + (lastB<<8);

#if kMemWritesOptimize == 0
                            put4Pixel(upPtr, -1, currPixel);
                            put4Pixel(upPtr, 0, currPixel);
#else
                            put4Pixel(upPtr, -1, currPixel);
#endif
                            ASSERT(!((flagsPtr[-1] | flagsPtr[0]) & eTheRest)); // no vertical blocks
                            flagsPtr[-1] = (flagsPtr[-1] & ~eNorths) | e21nw;
                            flagsPtr[0] = (flagsPtr[0] & ~eNorths) | e21ne;
                        }
                    }

                    // do horizontal on current raster
                    lastPixel = get4Pixel(currPtr,-1);
                    lastR = GetRed(lastPixel);
                    lastG = GetGreen(lastPixel);
                    lastB = GetBlue(lastPixel);
                    if ((fMaxErrorForTwoPixels >= 3) && (NewDeltaE(lastR, R1, lastG, G1, lastB, B1, fMaxErrorForTwoPixels)))
                    {
                        /*   - -
                            |   | build lower 2x1
                             - -
                        */
                        int didNotBuild4by1 = true;
#if kGatherStats == 1
                        blockStats[es21sw]++;
#endif
                        AverageNRound(isOdd(pixelNum), lastR, lastR, R1, lastG, lastG, G1, lastB, lastB, B1);
                        if ((pixelNum >= 3) && (flagsPtr[-3] & e21sw)) // 4,5,6,7,12,13,14,15,20...
                        {
                            // Look for a lower 4x1
                            ASSERT(!((flagsPtr[-3] | flagsPtr[-2] | flagsPtr[-1] | flagsPtr[0]) & eTheRest)); // no vertical blocks

                            lastPixel = get4Pixel(currPtr,-3);
                            R0 = GetRed(lastPixel);
                            G0 = GetGreen(lastPixel);
                            B0 = GetBlue(lastPixel);
                            if ((maxErrorForFourPixels >= 3) && (NewDeltaE(lastR, R0, lastG, G0, lastB, B0, maxErrorForFourPixels)))
                            {
                                /*   - - - -
                                    |       | build lower 4x1
                                     - - - -
                                */
#if kGatherStats == 1
                                blockStats[es41si]++;
#endif
                                didNotBuild4by1 = false;
                                AverageNRound((pixelNum & 0x04)== 0x04, lastR, lastR, R0, lastG, lastG, G0, lastB, lastB, B0); // 4,5,6,7,12,13,14,15,20...

                                if(littleEndian)
                                    currPixel = (lastR<<16) + (lastG<<8) + lastB;
                                else if(bigEndian)
                                    currPixel = (lastR<<24) + (lastG<<16) + (lastB<<8);

#if kMemWritesOptimize == 0
                                put4Pixel(currPtr, -3, currPixel);
                                put4Pixel(currPtr, -2, currPixel);
                                put4Pixel(currPtr, -1, currPixel);
                                put4Pixel(currPtr, 0, currPixel);
#else
                                put4Pixel(currPtr, -3, currPixel);
#endif
                                flagsPtr[-3] = (flagsPtr[-3] & ~eSouths) | e41si;
                                flagsPtr[-2] = (flagsPtr[-2] & ~eSouths) | e41s;
                                flagsPtr[-1] = (flagsPtr[-1] & ~eSouths) | e41s;
                                flagsPtr[0] = (flagsPtr[0] & ~eSouths) | e41s;
                            }
                        }

                        if (didNotBuild4by1) // Not a lower 4x1 so output lower 2x1.
                        {
                            ASSERT(!((flagsPtr[-1] | flagsPtr[0]) & eTheRest)); // no vertical blocks

                            if(littleEndian)
                                currPixel = (lastR<<16) + (lastG<<8) + lastB;
                            else if(bigEndian)
                                currPixel = (lastR<<24) + (lastG<<16) + (lastB<<8);

#if kMemWritesOptimize == 0
                            put4Pixel(currPtr, -1, currPixel);
                            put4Pixel(currPtr, 0, currPixel);
#else
                            put4Pixel(currPtr, -1, currPixel);
#endif

                            flagsPtr[-1] = (flagsPtr[-1] & ~eSouths) | e21sw;
                            flagsPtr[0] = (flagsPtr[0] & ~eSouths) | e21se;
                        }
                    }  // If DeltaE... Looking for two by one
                }  // IsOdd(pixelNum)
            }
        }
        else // no flag bits set.
        {
            lastPairAveraged = false; // EGW Fixes bug on business graphics. 11/24/97
        }

        upPtr += eBufferedPixelWidthInBytes;
        currPtr += eBufferedPixelWidthInBytes;
        flagsPtr++;
    }  // for each pixel...
}

// Filter2PairsOfFilteredRows.  This routine takes 2 pairs of rows that
// have been through the Filter2RawRows routine and puts blocks together
// to make bigger blocks.  It prefers taking 2 high blocks and putting
// them together to make four high blocks, but as a last resort it will
// take try to take a 1 high blocks from the second and third rasters and
// create 2 high blocks.  The possible block sizes this routine could
// create are 8x4, 4x4, 2x4, and 1x4, and then with the second and third rasters
// 4x2, 2x2, and 1x2.
void TErnieFilter::Filter2PairsOfFilteredRows(unsigned char *row1Ptr, unsigned char *row2Ptr, unsigned char *row3Ptr, unsigned char *row4Ptr)
{
    const unsigned int maxErrorForFourPixels = fMaxErrorForTwoPixels / 2;
    const unsigned int maxErrorForEightPixels = maxErrorForFourPixels / 2;
    const unsigned int maxErrorForSixteenPixels = maxErrorForEightPixels / 2;
    const unsigned int maxErrorForThirtyTwoPixels = maxErrorForSixteenPixels / 2;

    for (int pixelNum = 0; pixelNum < (fRowWidthInPixels-3);)  // Make sure we have four pixels to work with
    {
        int currPixel, upPixel;
        int R0, G0, B0, R1, G1, B1;

        if ((fPixelFilteredFlags[0][pixelNum] & e42i) && (fPixelFilteredFlags[1][pixelNum] & e42i))
        {
            /*  - - - -
               |       |
               |       |
                - - - -     We have two 4x2s.
                - - - -
               |       |
               |       |
                - - - -
            */
            ASSERT(fPixelFilteredFlags[0][pixelNum] == e42i && fPixelFilteredFlags[0][pixelNum+1] == e42 && fPixelFilteredFlags[0][pixelNum+2] == e42 && fPixelFilteredFlags[0][pixelNum+3] == e42);
            ASSERT(fPixelFilteredFlags[1][pixelNum] == e42i && fPixelFilteredFlags[1][pixelNum+1] == e42 && fPixelFilteredFlags[1][pixelNum+2] == e42 && fPixelFilteredFlags[1][pixelNum+3] == e42);

            upPixel = get4Pixel(row1Ptr);
            currPixel = get4Pixel(row3Ptr);

            R1 = GetRed(currPixel);
            G1 = GetGreen(currPixel);
            B1 = GetBlue(currPixel);

            R0 = GetRed(upPixel);
            G0 = GetGreen(upPixel);
            B0 = GetBlue(upPixel);

            if((maxErrorForSixteenPixels >= 3) &&(NewDeltaE(R1, R0, G1, G0, B1, B0, maxErrorForSixteenPixels)))
            {
                /*   - - - -
                    |       |
                    |       | build 4x4
                    |       |
                    |       |
                     - - - -
                */
#if kGatherStats == 1
                blockStats[es44ni]++;
#endif
                AverageNRound((pixelNum & 0x04) == 0x04, R1, R1, R0, G1, G1, G0, B1, B1, B0); // 4,5,6,7,12,13,14,15,20... Alternate between rounding up down

                if(littleEndian)
                    currPixel = (R1<<16) + (G1<<8) + B1;
                else if(bigEndian)
                    currPixel = (R1<<24) + (G1<<16) + (B1<<8);

#if kMemWritesOptimize == 0
                put4Pixel(row1Ptr, 0, currPixel);
                put4Pixel(row1Ptr, 1, currPixel);
                put4Pixel(row1Ptr, 2, currPixel);
                put4Pixel(row1Ptr, 3, currPixel);
                put4Pixel(row2Ptr, 0, currPixel);
                put4Pixel(row2Ptr, 1, currPixel);
                put4Pixel(row2Ptr, 2, currPixel);
                put4Pixel(row2Ptr, 3, currPixel);
                put4Pixel(row3Ptr, 0, currPixel);
                put4Pixel(row3Ptr, 1, currPixel);
                put4Pixel(row3Ptr, 2, currPixel);
                put4Pixel(row3Ptr, 3, currPixel);
                put4Pixel(row4Ptr, 0, currPixel);
                put4Pixel(row4Ptr, 1, currPixel);
                put4Pixel(row4Ptr, 2, currPixel);
                put4Pixel(row4Ptr, 3, currPixel);
#else
                put4Pixel(row1Ptr, 0, currPixel);
#endif
                row1Ptr += 4*eBufferedPixelWidthInBytes;
                row2Ptr += 4*eBufferedPixelWidthInBytes;
                row3Ptr += 4*eBufferedPixelWidthInBytes;
                row4Ptr += 4*eBufferedPixelWidthInBytes;

                fPixelFilteredFlags[0][pixelNum] = e44ni;
                fPixelFilteredFlags[0][pixelNum+1] = fPixelFilteredFlags[0][pixelNum+2] = fPixelFilteredFlags[0][pixelNum+3] = e44n;
                fPixelFilteredFlags[1][pixelNum] = e44si;
                fPixelFilteredFlags[1][pixelNum+1] = fPixelFilteredFlags[1][pixelNum+2] = fPixelFilteredFlags[1][pixelNum+3] = e44s;

                if ((pixelNum >= 4) && (fPixelFilteredFlags[1][pixelNum-4] & e44si)) // 4,5,6,7,12,13,14,15,20...
                {
                    /*   - - - -     - - - -
                        |       |   |       |
                        |       |   |       | We have two 4x4s.
                        |       |   |       |
                        |       |   |       |
                         - - - -     - - - -
                    */
                    ASSERT(fPixelFilteredFlags[0][pixelNum-4] == e44ni && fPixelFilteredFlags[0][pixelNum-3] == e44n && fPixelFilteredFlags[0][pixelNum-2] == e44n && fPixelFilteredFlags[0][pixelNum-1] == e44n);
                    ASSERT(fPixelFilteredFlags[1][pixelNum-4] == e44si && fPixelFilteredFlags[1][pixelNum-3] == e44s && fPixelFilteredFlags[1][pixelNum-2] == e44s && fPixelFilteredFlags[1][pixelNum-1] == e44s);

                    upPixel = get4Pixel(row1Ptr, -8);

                    R0 = GetRed(upPixel);
                    G0 = GetGreen(upPixel);
                    B0 = GetBlue(upPixel);

                    if( (maxErrorForThirtyTwoPixels >= 3) && (NewDeltaE(R1, R0, G1, G0, B1, B0, maxErrorForThirtyTwoPixels)))
                    {
                        /*   - - - - - - - -
                            |               |
                            |               | build 8x4
                            |               |
                            |               |
                             - - - - - - - -
                        */
#if kGatherStats == 1
                        blockStats[es84ni]++;
#endif
                        AverageNRound((pixelNum & 0x08) == 0x08, R1, R1, R0, G1, G1, G0, B1, B1, B0);
                        if(littleEndian)
                            currPixel = (R1<<16) + (G1<<8) + B1;
                        else if(bigEndian)
                            currPixel = (R1<<24) + (G1<<16) + (B1<<8);

#if kMemWritesOptimize == 0
                        put4Pixel(row1Ptr, -8, currPixel);
                        put4Pixel(row1Ptr, -7, currPixel);
                        put4Pixel(row1Ptr, -6, currPixel);
                        put4Pixel(row1Ptr, -5, currPixel);
                        put4Pixel(row1Ptr, -4, currPixel);
                        put4Pixel(row1Ptr, -3, currPixel);
                        put4Pixel(row1Ptr, -2, currPixel);
                        put4Pixel(row1Ptr, -1, currPixel);
                        put4Pixel(row2Ptr, -8, currPixel);
                        put4Pixel(row2Ptr, -7, currPixel);
                        put4Pixel(row2Ptr, -6, currPixel);
                        put4Pixel(row2Ptr, -5, currPixel);
                        put4Pixel(row2Ptr, -4, currPixel);
                        put4Pixel(row2Ptr, -3, currPixel);
                        put4Pixel(row2Ptr, -2, currPixel);
                        put4Pixel(row2Ptr, -1, currPixel);
                        put4Pixel(row3Ptr, -8, currPixel);
                        put4Pixel(row3Ptr, -7, currPixel);
                        put4Pixel(row3Ptr, -6, currPixel);
                        put4Pixel(row3Ptr, -5, currPixel);
                        put4Pixel(row3Ptr, -4, currPixel);
                        put4Pixel(row3Ptr, -3, currPixel);
                        put4Pixel(row3Ptr, -2, currPixel);
                        put4Pixel(row3Ptr, -1, currPixel);
                        put4Pixel(row4Ptr, -8, currPixel);
                        put4Pixel(row4Ptr, -7, currPixel);
                        put4Pixel(row4Ptr, -6, currPixel);
                        put4Pixel(row4Ptr, -5, currPixel);
                        put4Pixel(row4Ptr, -4, currPixel);
                        put4Pixel(row4Ptr, -3, currPixel);
                        put4Pixel(row4Ptr, -2, currPixel);
                        put4Pixel(row4Ptr, -1, currPixel);
#else
                        put4Pixel(row1Ptr, -8, currPixel);
#endif
                        fPixelFilteredFlags[0][pixelNum-4] = e84ni;
                        fPixelFilteredFlags[0][pixelNum-3] = fPixelFilteredFlags[0][pixelNum-2] = fPixelFilteredFlags[0][pixelNum-1] = fPixelFilteredFlags[0][pixelNum] = fPixelFilteredFlags[0][pixelNum+1] = fPixelFilteredFlags[0][pixelNum+2] = fPixelFilteredFlags[0][pixelNum+3] = e84n;
                        fPixelFilteredFlags[1][pixelNum-4] = e84si;
                        fPixelFilteredFlags[1][pixelNum-3] = fPixelFilteredFlags[1][pixelNum-2] = fPixelFilteredFlags[1][pixelNum-1] = fPixelFilteredFlags[1][pixelNum] = fPixelFilteredFlags[1][pixelNum+1] = fPixelFilteredFlags[1][pixelNum+2] = fPixelFilteredFlags[1][pixelNum+3] = e84s;
                    }
                }
            }
            else // could not build 4x4 so move forward past the stacked 4x2s.
            {
                row1Ptr += 4*eBufferedPixelWidthInBytes;
                row2Ptr += 4*eBufferedPixelWidthInBytes;
                row3Ptr += 4*eBufferedPixelWidthInBytes;
                row4Ptr += 4*eBufferedPixelWidthInBytes;
            }
            pixelNum += 4;
        }
        else if ((fPixelFilteredFlags[0][pixelNum] & e22w) && (fPixelFilteredFlags[1][pixelNum] & e22w))
        {
            /*   - -
                |   |
                |   |
                 - -   we have 2 2x2s.
                 - -
                |   |
                |   |
                 - -
            */
            ASSERT(fPixelFilteredFlags[0][pixelNum] == e22w && fPixelFilteredFlags[0][pixelNum+1] == e22e);
            ASSERT(fPixelFilteredFlags[1][pixelNum] == e22w && fPixelFilteredFlags[1][pixelNum+1] == e22e);

            upPixel = get4Pixel(row1Ptr);
            currPixel = get4Pixel(row3Ptr);

            R1 = GetRed(currPixel);
            G1 = GetGreen(currPixel);
            B1 = GetBlue(currPixel);

            R0 = GetRed(upPixel);
            G0 = GetGreen(upPixel);
            B0 = GetBlue(upPixel);

            if ((maxErrorForEightPixels >= 3) && (NewDeltaE(R1, R0, G1, G0, B1, B0, maxErrorForEightPixels)))
            {
                /*   - -
                    |   |
                    |   | build 2x4
                    |   |
                    |   |
                     - -
                */
#if kGatherStats == 1
                blockStats[es24nw]++;
#endif
                AverageNRound((pixelNum & 0x02) == 0x02, R1, R1, R0, G1, G1, G0, B1, B1, B0);

                if(littleEndian)
                    currPixel = (R1<<16) + (G1<<8) + B1;
                else if(bigEndian)
                    currPixel = (R1<<24) + (G1<<16) + (B1<<8);

#if kMemWritesOptimize == 0
                put4Pixel(row1Ptr, 0, currPixel);
                put4Pixel(row1Ptr, 1, currPixel);
                put4Pixel(row2Ptr, 0, currPixel);
                put4Pixel(row2Ptr, 1, currPixel);
                put4Pixel(row3Ptr, 0, currPixel);
                put4Pixel(row3Ptr, 1, currPixel);
                put4Pixel(row4Ptr, 0, currPixel);
                put4Pixel(row4Ptr, 1, currPixel);
#else
                put4Pixel(row1Ptr, 0, currPixel);
#endif
                row1Ptr += 2*eBufferedPixelWidthInBytes;
                row2Ptr += 2*eBufferedPixelWidthInBytes;
                row3Ptr += 2*eBufferedPixelWidthInBytes;
                row4Ptr += 2*eBufferedPixelWidthInBytes;

                fPixelFilteredFlags[0][pixelNum] = e24nw;
                fPixelFilteredFlags[0][pixelNum+1] = e24ne;
                fPixelFilteredFlags[1][pixelNum] = e24sw;
                fPixelFilteredFlags[1][pixelNum+1] = e24se;
            }
            else
            {
                row1Ptr += 2*eBufferedPixelWidthInBytes;
                row2Ptr += 2*eBufferedPixelWidthInBytes;
                row3Ptr += 2*eBufferedPixelWidthInBytes;
                row4Ptr += 2*eBufferedPixelWidthInBytes;
            }
            pixelNum += 2;
        }
        else if ((fPixelFilteredFlags[0][pixelNum] & e12) && (fPixelFilteredFlags[1][pixelNum] & e12))
        {
            /*   -
                | |
                | |
                 -  we have two 1x2s.
                 -
                | |
                | |
                 -
            */
            ASSERT(fPixelFilteredFlags[0][pixelNum] == e12);
            ASSERT(fPixelFilteredFlags[1][pixelNum] == e12);

            upPixel = get4Pixel(row1Ptr);
            currPixel = get4Pixel(row3Ptr);

            R1 = GetRed(currPixel);
            G1 = GetGreen(currPixel);
            B1 = GetBlue(currPixel);

            R0 = GetRed(upPixel);
            G0 = GetGreen(upPixel);
            B0 = GetBlue(upPixel);

            if ((maxErrorForFourPixels >= 3) && (NewDeltaE(R1, R0, G1, G0, B1, B0, maxErrorForFourPixels)))
            {
                /*   -
                    | |
                    | | build 1x4
                    | |
                    | |
                     -
                */
#if kGatherStats == 1
                blockStats[es14n]++;
#endif
                AverageNRound((pixelNum & 0x01) == 0x01, R1, R1, R0, G1, G1, G0, B1, B1, B0);

                if(littleEndian)
                    currPixel = (R1<<16) + (G1<<8) + B1;
                else if(bigEndian)
                    currPixel = (R1<<24) + (G1<<16) + (B1<<8);

#if kMemWritesOptimize == 0
                put4Pixel(row1Ptr, 0, currPixel);
                put4Pixel(row2Ptr, 0, currPixel);
                put4Pixel(row3Ptr, 0, currPixel);
                put4Pixel(row4Ptr, 0, currPixel);
#else
                put4Pixel(row1Ptr, 0, currPixel);
#endif
                fPixelFilteredFlags[0][pixelNum] = e14n;
                fPixelFilteredFlags[1][pixelNum] = e14s;
            }

            row1Ptr += eBufferedPixelWidthInBytes;
            row2Ptr += eBufferedPixelWidthInBytes;
            row3Ptr += eBufferedPixelWidthInBytes;
            row4Ptr += eBufferedPixelWidthInBytes;

            pixelNum++;
        }
        else if ((fPixelFilteredFlags[0][pixelNum] & e41si)
            && (fPixelFilteredFlags[1][pixelNum] & e41ni))
        {
            /*    - - - -
                 |       |
                  - - - -   We have two 4x1s.
                  - - - -
                 |       |
                  - - - -
            */

            upPixel = get4Pixel(row2Ptr);
            currPixel = get4Pixel(row3Ptr);

            R1 = GetRed(currPixel);
            G1 = GetGreen(currPixel);
            B1 = GetBlue(currPixel);

            R0 = GetRed(upPixel);
            G0 = GetGreen(upPixel);
            B0 = GetBlue(upPixel);


            if ((maxErrorForEightPixels >= 3) && (NewDeltaE(R1, R0, G1, G0, B1, B0, maxErrorForEightPixels)))
            {

                /*    - - - -
                     |       |  build 4x2.
                     |       |
                      - - - -
                */
#if kGatherStats == 1
                blockStats[es42w]++;
#endif
                AverageNRound((pixelNum & 0x04) == 0x04, R1, R1, R0, G1, G1, G0, B1, B1, B0);

                if(littleEndian)
                    currPixel = (R1<<16) + (G1<<8) + B1;
                else if(bigEndian)
                    currPixel = (R1<<24) + (G1<<16) + (B1<<8);

                // Note we write this block out now and do not delay the writes for the postprocessing step since we do not track this block.
                put4Pixel(row2Ptr, 0, currPixel);
                put4Pixel(row2Ptr, 1, currPixel);
                put4Pixel(row2Ptr, 2, currPixel);
                put4Pixel(row2Ptr, 3, currPixel);
                put4Pixel(row3Ptr, 0, currPixel);
                put4Pixel(row3Ptr, 1, currPixel);
                put4Pixel(row3Ptr, 2, currPixel);
                put4Pixel(row3Ptr, 3, currPixel);

                fPixelFilteredFlags[0][pixelNum] = fPixelFilteredFlags[0][pixelNum] & ~e41si;
                fPixelFilteredFlags[0][pixelNum+1] = fPixelFilteredFlags[0][pixelNum+1] & ~e41s;
                fPixelFilteredFlags[0][pixelNum+2] = fPixelFilteredFlags[0][pixelNum+1] & ~e41s;
                fPixelFilteredFlags[0][pixelNum+3] = fPixelFilteredFlags[0][pixelNum+1] & ~e41s;

                fPixelFilteredFlags[1][pixelNum] = fPixelFilteredFlags[1][pixelNum] & ~e41ni;  // Note that we just formed a 2x2 in the middle of the filtered sets of rows (and do not remember it). We then remove the 2x1s that the 2x2 eliminated.
                fPixelFilteredFlags[1][pixelNum+1] = fPixelFilteredFlags[1][pixelNum+1] & ~e41n;
                fPixelFilteredFlags[1][pixelNum+2] = fPixelFilteredFlags[1][pixelNum+1] & ~e41n;
                fPixelFilteredFlags[1][pixelNum+3] = fPixelFilteredFlags[1][pixelNum+1] & ~e41n;
            }
            pixelNum += 4;

            row1Ptr += 4*eBufferedPixelWidthInBytes;
            row2Ptr += 4*eBufferedPixelWidthInBytes;
            row3Ptr += 4*eBufferedPixelWidthInBytes;
            row4Ptr += 4*eBufferedPixelWidthInBytes;
        }
        else if ((fPixelFilteredFlags[0][pixelNum] & e21sw)
                && (fPixelFilteredFlags[1][pixelNum] & e21nw))
        {
            /*    - -
                 |   |
                  - -  We have two 2x1s.
                  - -
                 |   |
                  - -
            */
            ASSERT(!((fPixelFilteredFlags[0][pixelNum] & e11s) | (fPixelFilteredFlags[0][pixelNum+1] & e11s)));
            ASSERT(!((fPixelFilteredFlags[1][pixelNum] & e11n) | (fPixelFilteredFlags[1][pixelNum+1] & e11n)));

            upPixel = get4Pixel(row2Ptr);
            currPixel = get4Pixel(row3Ptr);

            R1 = GetRed(currPixel);
            G1 = GetGreen(currPixel);
            B1 = GetBlue(currPixel);

            R0 = GetRed(upPixel);
            G0 = GetGreen(upPixel);
            B0 = GetBlue(upPixel);

            if ((maxErrorForFourPixels >= 3) && (NewDeltaE(R1, R0, G1, G0, B1, B0, maxErrorForFourPixels)))
            {
                /*    - -
                     |   |  build 2x2.
                     |   |
                      - -
                */
#if kGatherStats == 1
                blockStats[es22w]++;
#endif
                AverageNRound((pixelNum & 0x02) == 0x02, R1, R1, R0, G1, G1, G0, B1, B1, B0);

                if(littleEndian)
                    currPixel = (R1<<16) + (G1<<8) + B1;
                else if(bigEndian)
                    currPixel = (R1<<24) + (G1<<16) + (B1<<8);

                // Note we write this block out now and do not delay the writes for the postprocessing step since we do not track this block.
                put4Pixel(row2Ptr, 0, currPixel);
                put4Pixel(row2Ptr, 1, currPixel);
                put4Pixel(row3Ptr, 0, currPixel);
                put4Pixel(row3Ptr, 1, currPixel);

                fPixelFilteredFlags[0][pixelNum] = fPixelFilteredFlags[0][pixelNum] & ~e21sw;
                fPixelFilteredFlags[0][pixelNum+1] = fPixelFilteredFlags[0][pixelNum+1] & ~e21se;

                fPixelFilteredFlags[1][pixelNum] = fPixelFilteredFlags[1][pixelNum] & ~e21nw;  // Note that we just formed a 2x2 in the middle of the filtered sets of rows (and do not remember it). We then remove the 2x1s that the 2x2 eliminated.
                fPixelFilteredFlags[1][pixelNum+1] = fPixelFilteredFlags[1][pixelNum+1] & ~e21ne;
            }

            pixelNum += 2;

            row1Ptr += 2*eBufferedPixelWidthInBytes;
            row2Ptr += 2*eBufferedPixelWidthInBytes;
            row3Ptr += 2*eBufferedPixelWidthInBytes;
            row4Ptr += 2*eBufferedPixelWidthInBytes;
        }
        else if ((fPixelFilteredFlags[0][pixelNum] & e11s)
                && (fPixelFilteredFlags[1][pixelNum] & e11n))
        {
            /*    -
                 | |
                  -   We have two 1x1s.
                  -
                 | |
                  -
            */

            upPixel = get4Pixel(row2Ptr);
            currPixel = get4Pixel(row3Ptr);

            R1 = GetRed(currPixel);
            G1 = GetGreen(currPixel);
            B1 = GetBlue(currPixel);

            R0 = GetRed(upPixel);
            G0 = GetGreen(upPixel);
            B0 = GetBlue(upPixel);

            if ((fMaxErrorForTwoPixels >= 3) && (NewDeltaE(R1, R0, G1, G0, B1, B0, fMaxErrorForTwoPixels)))
            {
                /*    -
                     | |  build 1x2.
                     | |
                      -
                */
#if kGatherStats == 1
                blockStats[es12w]++;
#endif
                AverageNRound(isOdd(pixelNum), R1, R1, R0, G1, G1, G0, B1, B1, B0);

                if(littleEndian)
                    currPixel = (R1<<16) + (G1<<8) + B1;
                else if(bigEndian)
                    currPixel = (R1<<24) + (G1<<16) + (B1<<8);

                // Note we write this block out now and do not delay the writes for the postprocessing step since we do not track this block.
                put4Pixel(row2Ptr, 0, currPixel);
                put4Pixel(row3Ptr, 0, currPixel);

                fPixelFilteredFlags[0][pixelNum] = fPixelFilteredFlags[0][pixelNum] & ~e11s;

                fPixelFilteredFlags[1][pixelNum] = fPixelFilteredFlags[1][pixelNum] & ~e11n;  // Note that we just formed a 2x2 in the middle of the filtered sets of rows (and do not remember it). We then remove the 2x1s that the 2x2 eliminated.
            }

            pixelNum += 1;

            row1Ptr += eBufferedPixelWidthInBytes;
            row2Ptr += eBufferedPixelWidthInBytes;
            row3Ptr += eBufferedPixelWidthInBytes;
            row4Ptr += eBufferedPixelWidthInBytes;
        }
        else // Do no vertical filtering here.
        {
            pixelNum += 1;

            row1Ptr += eBufferedPixelWidthInBytes;
            row2Ptr += eBufferedPixelWidthInBytes;
            row3Ptr += eBufferedPixelWidthInBytes;
            row4Ptr += eBufferedPixelWidthInBytes;
        }
    }
}

// Filter3FilteredRows.  This routine only exists for the case of the odd size band
// with three rasters left over.  I'm not sure how much extra benifit we really
// get from running this, but for now I'm leaving it in.  Since Ernie deals with
// block sizes that are powers of two its rather difficult to filter 3 rows together,
// about all I've been able to do is look for 1 high blocks in the second and third
// rasters to put together into 2 high blocks.  This routine will create 4x2, 2x2, and
// 1x2 blocks from those second and third rasters.
void TErnieFilter::Filter3FilteredRows(unsigned char *row1Ptr, unsigned char *row2Ptr, unsigned char *row3Ptr)
{
    const unsigned int maxErrorForFourPixels = fMaxErrorForTwoPixels / 2;
    const unsigned int maxErrorForEightPixels = maxErrorForFourPixels / 2;
//    const unsigned int maxErrorForSixteenPixels = maxErrorForEightPixels / 2;
//    const unsigned int maxErrorForThirtyTwoPixels = maxErrorForSixteenPixels / 2;

    for (int pixelNum = 0; pixelNum < (fRowWidthInPixels-3);)  // Make sure we have four pixels to work with
    {
//        int currPixel, upPixel;
        uint32_t currPixel, upPixel;
        int R0, G0, B0, R1, G1, B1;

        if ((fPixelFilteredFlags[0][pixelNum] & e41si)
                && (fPixelFilteredFlags[1][pixelNum] & e41ni))
        {
            /*    - - - -
                 |       |
                  - - - -   We have two 4x1s.
                  - - - -
                 |       |
                  - - - -
            */
            ASSERT(!((fPixelFilteredFlags[0][pixelNum] & e11s) | (fPixelFilteredFlags[0][pixelNum+1] & e11s)));
            ASSERT(!((fPixelFilteredFlags[1][pixelNum] & e11n) | (fPixelFilteredFlags[1][pixelNum+1] & e11n)));

            upPixel = get4Pixel(row2Ptr);
            currPixel = get4Pixel(row3Ptr);

            R1 = GetRed(currPixel);
            G1 = GetGreen(currPixel);
            B1 = GetBlue(currPixel);

            R0 = GetRed(upPixel);
            G0 = GetGreen(upPixel);
            B0 = GetBlue(upPixel);


            if ((maxErrorForEightPixels >= 3) && (NewDeltaE(R1, R0, G1, G0, B1, B0, maxErrorForEightPixels)))
            {

                /*    - - - -
                     |       |  build 4x2.
                     |       |
                      - - - -
                */
#if kGatherStats == 1
                blockStats[es42w]++;
#endif
                AverageNRound((pixelNum & 0x04) == 0x04, R1, R1, R0, G1, G1, G0, B1, B1, B0);

                if(littleEndian)
                    currPixel = (R1<<16) + (G1<<8) + B1;
                else if(bigEndian)
                    currPixel = (R1<<24) + (G1<<16) + (B1<<8);

                // Note we write this block out now and do not delay the writes for the postprocessing step since we do not track this block.
                put4Pixel(row2Ptr, 0, currPixel);
                put4Pixel(row2Ptr, 1, currPixel);
                put4Pixel(row2Ptr, 2, currPixel);
                put4Pixel(row2Ptr, 3, currPixel);
                put4Pixel(row3Ptr, 0, currPixel);
                put4Pixel(row3Ptr, 1, currPixel);
                put4Pixel(row3Ptr, 2, currPixel);
                put4Pixel(row3Ptr, 3, currPixel);

                fPixelFilteredFlags[0][pixelNum] = fPixelFilteredFlags[0][pixelNum] & ~e41si;
                fPixelFilteredFlags[0][pixelNum+1] = fPixelFilteredFlags[0][pixelNum+1] & ~e41s;
                fPixelFilteredFlags[0][pixelNum+2] = fPixelFilteredFlags[0][pixelNum+1] & ~e41s;
                fPixelFilteredFlags[0][pixelNum+3] = fPixelFilteredFlags[0][pixelNum+1] & ~e41s;

                fPixelFilteredFlags[1][pixelNum] = fPixelFilteredFlags[1][pixelNum] & ~e41ni;  // Note that we just formed a 2x2 in the middle of the filtered sets of rows (and do not remember it). We then remove the 2x1s that the 2x2 eliminated.
                fPixelFilteredFlags[1][pixelNum+1] = fPixelFilteredFlags[1][pixelNum+1] & ~e41n;
                fPixelFilteredFlags[1][pixelNum+2] = fPixelFilteredFlags[1][pixelNum+1] & ~e41n;
                fPixelFilteredFlags[1][pixelNum+3] = fPixelFilteredFlags[1][pixelNum+1] & ~e41n;
            }
            pixelNum += 4;

            row1Ptr += 4*eBufferedPixelWidthInBytes;
            row2Ptr += 4*eBufferedPixelWidthInBytes;
            row3Ptr += 4*eBufferedPixelWidthInBytes;
        }
        else if ((fPixelFilteredFlags[0][pixelNum] & e21sw)
                && (fPixelFilteredFlags[1][pixelNum] & e21nw))
        {
            /*    - -
                 |   |
                  - -  We have two 2x1s.
                  - -
                 |   |
                  - -
            */
            ASSERT(!((fPixelFilteredFlags[0][pixelNum] & e11s) | (fPixelFilteredFlags[0][pixelNum+1] & e11s)));
            ASSERT(!((fPixelFilteredFlags[1][pixelNum] & e11n) | (fPixelFilteredFlags[1][pixelNum+1] & e11n)));

            upPixel = get4Pixel(row2Ptr);
            currPixel = get4Pixel(row3Ptr);

            R1 = GetRed(currPixel);
            G1 = GetGreen(currPixel);
            B1 = GetBlue(currPixel);

            R0 = GetRed(upPixel);
            G0 = GetGreen(upPixel);
            B0 = GetBlue(upPixel);

            if ((maxErrorForFourPixels >= 3) && (NewDeltaE(R1, R0, G1, G0, B1, B0, maxErrorForFourPixels)))
            {
                /*    - -
                     |   |  build 2x2.
                     |   |
                      - -
                */
#if kGatherStats == 1
                blockStats[es22w]++;
#endif
                AverageNRound((pixelNum & 0x02) == 0x02, R1, R1, R0, G1, G1, G0, B1, B1, B0);

                if(littleEndian)
                    currPixel = (R1<<16) + (G1<<8) + B1;
                else if(bigEndian)
                    currPixel = (R1<<24) + (G1<<16) + (B1<<8);

                // Note we write this block out now and do not delay the writes for the postprocessing step since we do not track this block.
                put4Pixel(row2Ptr, 0, currPixel);
                put4Pixel(row2Ptr, 1, currPixel);
                put4Pixel(row3Ptr, 0, currPixel);
                put4Pixel(row3Ptr, 1, currPixel);

                fPixelFilteredFlags[0][pixelNum] = fPixelFilteredFlags[0][pixelNum] & ~e21sw;
                fPixelFilteredFlags[0][pixelNum+1] = fPixelFilteredFlags[0][pixelNum+1] & ~e21se;

                fPixelFilteredFlags[1][pixelNum] = fPixelFilteredFlags[1][pixelNum] & ~e21nw;  // Note that we just formed a 2x2 in the middle of the filtered sets of rows (and do not remember it). We then remove the 2x1s that the 2x2 eliminated.
                fPixelFilteredFlags[1][pixelNum+1] = fPixelFilteredFlags[1][pixelNum+1] & ~e21ne;
            }

            pixelNum += 2;

            row1Ptr += 2*eBufferedPixelWidthInBytes;
            row2Ptr += 2*eBufferedPixelWidthInBytes;
            row3Ptr += 2*eBufferedPixelWidthInBytes;
        }
        else if ((fPixelFilteredFlags[0][pixelNum] & e11s)
                && (fPixelFilteredFlags[1][pixelNum] & e11n))
        {
            /*    -
                 | |
                  -   We have two 1x1s.
                  -
                 | |
                  -
            */

            upPixel = get4Pixel(row2Ptr);
            currPixel = get4Pixel(row3Ptr);

            R1 = GetRed(currPixel);
            G1 = GetGreen(currPixel);
            B1 = GetBlue(currPixel);

            R0 = GetRed(upPixel);
            G0 = GetGreen(upPixel);
            B0 = GetBlue(upPixel);

            if ((fMaxErrorForTwoPixels >= 3) && (NewDeltaE(R1, R0, G1, G0, B1, B0, fMaxErrorForTwoPixels)))
            {
                /*    -
                     | |  build 1x2.
                     | |
                      -
                */
#if kGatherStats == 1
                blockStats[es12w]++;
#endif
                AverageNRound(isOdd(pixelNum), R1, R1, R0, G1, G1, G0, B1, B1, B0);

                if(littleEndian)
                    currPixel = (R1<<16) + (G1<<8) + B1;
                else if(bigEndian)
                    currPixel = (R1<<24) + (G1<<16) + (B1<<8);

                // Note we write this block out now and do not delay the writes for the postprocessing step since we do not track this block.
                put4Pixel(row2Ptr, 0, currPixel);
                put4Pixel(row3Ptr, 0, currPixel);

                fPixelFilteredFlags[0][pixelNum] = fPixelFilteredFlags[0][pixelNum] & ~e11s;

                fPixelFilteredFlags[1][pixelNum] = fPixelFilteredFlags[1][pixelNum] & ~e11n;  // Note that we just formed a 2x2 in the middle of the filtered sets of rows (and do not remember it). We then remove the 2x1s that the 2x2 eliminated.
            }

            pixelNum += 1;

            row1Ptr += eBufferedPixelWidthInBytes;
            row2Ptr += eBufferedPixelWidthInBytes;
            row3Ptr += eBufferedPixelWidthInBytes;
        }
        else // Do no vertical filtering here.
        {
            pixelNum += 1;

            row1Ptr += eBufferedPixelWidthInBytes;
            row2Ptr += eBufferedPixelWidthInBytes;
            row3Ptr += eBufferedPixelWidthInBytes;
        }
    }
}

#define NEWTEST true

inline bool TErnieFilter::NewDeltaE(int dr0, int dr1, int dg0, int dg1, int db0, int db1, int tolerance)
{
    int Y0, Y1, dY, Cr0, Cr1, Cb0, Cb1, dCr, dCb;

    // new Delta E stuff from Jay

    Y0 = 5*dr0 + 9*dg0 + 2*db0;
    Y1 = 5*dr1 + 9*dg1 + 2*db1;

    dY = ABS(Y0 - Y1) >> 4;

    if(dY > tolerance) {
        return false;
    }
    else
    {
        Cr0 = (dr0 << 4) - Y0;
        Cr1 = (dr1 << 4) - Y1;
        dCr = ABS(Cr0 - Cr1) >> 5;
        if(dCr > tolerance)
        {
            return false;
        }
        else
        {
            Cb0 = (db0 << 4) - Y0;
            Cb1 = (db1 << 4) - Y1;
            dCb = ABS(Cb0 - Cb1) >> 6;
            if(dCb > tolerance)
            {
                return false;
            }
        }
    }
    return true;
}

TErnieFilter::TErnieFilter(int rowWidthInPixels, pixelTypes pixelType, unsigned int maxErrorForTwoPixels, int bytesPerPixel)
: fOriginalPixelSize(bytesPerPixel)
{
    int index;
    ASSERT((fOriginalPixelSize == 3) || (fOriginalPixelSize == 4));
    ASSERT(rowWidthInPixels > 0);
    ASSERT(pixelType == eBGRPixelData);

    fInternalBufferPixelSize = 4;

    fPixelOffsetIndex = 0;
    fRowWidthInPixels = rowWidthInPixels;
    fRowWidthInBytes = fRowWidthInPixels*fInternalBufferPixelSize;
    fMaxErrorForTwoPixels = maxErrorForTwoPixels;

    for (index = 0; index < 4; index++)
    {
        fRowBuf[index] = new uint32_t[rowWidthInPixels];
        ASSERT(fRowBuf[index]);

        fRowPtr[index] = new unsigned char[rowWidthInPixels*fOriginalPixelSize];
		ASSERT(fRowPtr[index]);

		fBlackRowPtr[index] = new BYTE[rowWidthInPixels*fOriginalPixelSize];
		ASSERT(fBlackRowPtr[index]);

		BlackRasterSize[index] = 0;
    }

    for (index = 0; index < 2; index++)
    {
        fPixelFilteredFlags[index] = new unsigned int[rowWidthInPixels];
        ASSERT(fPixelFilteredFlags[index]);
    }

    // The least compressible image will be all raw pixels. Maximum compressed size is:
    // full size + a bloat of Cmd byte + 1 VLI byte per 255 pixels rounded up to nearest integer.

    int maxCompressionBufSize = fRowWidthInBytes + 1 + ((int)ceil((float)MAX((rowWidthInPixels-2)/255, 0)));

    fCompressionOutBuf = new unsigned char[maxCompressionBufSize];
    ASSERT(fCompressionOutBuf);

    fNumberOfBufferedRows = 0;

    fPixelOffset[0] = 0;
    fPixelOffset[1] = 5;
    fPixelOffset[2] = 2;
    fPixelOffset[3] = 7;
    fPixelOffset[4] = 1;
    fPixelOffset[5] = 4;
    fPixelOffset[6] = 6;
    fPixelOffset[7] = 3;

	RowIndex = 0;
}


TErnieFilter::~TErnieFilter()
{
    // Deallocate memory next.
    int index;

    for (index = 0; index < 4; index++)
    {
        delete [] fRowBuf[index];
        delete [] fRowPtr[index];
		delete [] fBlackRowPtr[index];
    }

    for (index = 0; index < 2; index++)
    {
        delete [] fPixelFilteredFlags[index];
    }

    delete [] fCompressionOutBuf;
}

void TErnieFilter::writeBufferedRows()
{
    int pixelIndex = 0;

    // We just have one lonely raster left.  Nothing
    // we can do but filter it horizontally.
    if( 1 == fNumberOfBufferedRows)
    {

        int offset2 = fPixelOffset[fPixelOffsetIndex];

        Filter1RawRow( (unsigned char*)(fRowBuf[0] + offset2),
                       fRowWidthInPixels - fPixelOffset[fPixelOffsetIndex],
                       fPixelFilteredFlags[0] + fPixelOffset[fPixelOffsetIndex]);


        unsigned char *rowPtr = fRowPtr[0];
        ASSERT(rowPtr);
        pixelIndex = 0;
        do
        {
            memcpy(rowPtr, &fRowBuf[0][pixelIndex], 3);
            rowPtr += 3;
        } while (++pixelIndex < fRowWidthInPixels);

    }
    // If we've got a pair of rasters in the buffer, that pair
    // has already been filtered somewhat.  So lets just write them
    // out, some filtering is better than none.
    else if (2 == fNumberOfBufferedRows)
    {
        // Write the two rows back out.
        int k;
        for (k = 0; k < 2; k++)
        {
            unsigned char *rowPtr = fRowPtr[k];
            ASSERT(rowPtr);
            pixelIndex = 0;
            do
            {
                memcpy(rowPtr, &fRowBuf[k][pixelIndex], 3);
                rowPtr += 3;
            } while (++pixelIndex < fRowWidthInPixels);
        }
    }
    // Okay, if we had three rasters in the buffer, the pair
    // should have already been written out above, so lets
    // just run the odd raster through Ernie with to
    // get the horizontal filtering.  [Need to look to see
    // if there's something more we can do with filtering
    // all three together.]
    else if (3 == fNumberOfBufferedRows)
    {

        int offset2 = fPixelOffset[fPixelOffsetIndex];

        Filter1RawRow( (unsigned char*)(fRowBuf[2] + offset2),
                       fRowWidthInPixels - fPixelOffset[fPixelOffsetIndex],
                       fPixelFilteredFlags[1] + fPixelOffset[fPixelOffsetIndex]);


        Filter3FilteredRows( (unsigned char*)fRowBuf[0],
                             (unsigned char*)fRowBuf[1],
                             (unsigned char*)fRowBuf[2]);

        int k;
        for (k = 0; k < 3; k++)
        {
            unsigned char *rowPtr = fRowPtr[k];
            ASSERT(rowPtr);
            pixelIndex = 0;
            do
            {
                memcpy(rowPtr, &fRowBuf[k][pixelIndex], 3);
                rowPtr += 3;
            } while (++pixelIndex < fRowWidthInPixels);
        }
    }
}

void TErnieFilter::submitRowToFilter(unsigned char *rowPtr)
{
    memcpy(fRowPtr[fNumberOfBufferedRows], rowPtr, fRowWidthInPixels*3);

    // Now reformat the pixel data from 24 bit to 32 bit pixels
    int pixelIndex = 0;
    uint32_t *RowPtrDest = fRowBuf[fNumberOfBufferedRows];
    BYTE byte1 = 0;
    BYTE byte2 = 0;
    BYTE byte3 = 0;
    do
    {
        byte1 = *rowPtr++;
        byte2 = *rowPtr++;
        byte3 = *rowPtr++;
        if(littleEndian)
            RowPtrDest[pixelIndex] = ((byte3 << 16) | (byte2 << 8) | (byte1)) & 0x00FFFFFF;
        else if(bigEndian)
            RowPtrDest[pixelIndex] = ((byte1 << 24) | (byte2 << 16) | (byte3 << 8)) & 0xFFFFFF00;
    } while (++pixelIndex < fRowWidthInPixels);

    fNumberOfBufferedRows++;

    iRastersReady=0;
    iRastersDelivered=0;

    // Next see about filtering & compression.
    // NOTE 1: as an optimization only do subsections of the raster at a time to stay in cache.
    // NOTE 2: Could filter the pixels left of the offset.
    if (2 == fNumberOfBufferedRows)
    {
        int offset2 = fPixelOffset[fPixelOffsetIndex];

        Filter2RawRows( (unsigned char*)(fRowBuf[1] + offset2),
                        (unsigned char*)(fRowBuf[0] + offset2),
                        fRowWidthInPixels - fPixelOffset[fPixelOffsetIndex],
                        fPixelFilteredFlags[0] + fPixelOffset[fPixelOffsetIndex]);
    }

    if (4 == fNumberOfBufferedRows)
    {
        int offset4 = fPixelOffset[fPixelOffsetIndex];
        Filter2RawRows( (unsigned char*)(fRowBuf[3] + offset4),
                        (unsigned char*)(fRowBuf[2] + offset4),
                        fRowWidthInPixels - fPixelOffset[fPixelOffsetIndex],
                        fPixelFilteredFlags[1] + fPixelOffset[fPixelOffsetIndex]);

        Filter2PairsOfFilteredRows( (unsigned char*)fRowBuf[0],
                                    (unsigned char*)fRowBuf[1],
                                    (unsigned char*)fRowBuf[2],
                                    (unsigned char*)fRowBuf[3]);

#if kMemWritesOptimize == 1
        // Writing the blocks out on a post processing step in this manner could leave the last 3 rows
        // unfiltered. This is a trade off we make for simplicity. The resulting loss in compression is small.
        WriteBlockPixels();
#endif

        fPixelOffsetIndex = (fPixelOffsetIndex + 1) % 8; // cycle the offset index.

        int k;
        for (k = 0; k < fPixelOffset[fPixelOffsetIndex]; k++) // Clear out the flags that we're offsetting past for this next iteration.
        {
            fPixelFilteredFlags[0][k] = eDone;
            fPixelFilteredFlags[1][k] = eDone;
        }

        // Write the four rows back out.
        for (k = 0; k < 4; k++)
        {
            unsigned char *rowPtr = fRowPtr[k];
            ASSERT(rowPtr);
            pixelIndex = 0;
            do
            {
                memcpy(rowPtr, &fRowBuf[k][pixelIndex], fOriginalPixelSize);
                rowPtr += fOriginalPixelSize;
            } while (++pixelIndex < fRowWidthInPixels);
        }

        fNumberOfBufferedRows = 0;
        iRastersReady = 4;
    }
}


#if kMemWritesOptimize == 1


/*
At this point the color for the entire block is stored in the top left
corner of the block. This routine takes that pixel and smears it into the
rest of the block.
*/
void TErnieFilter::WriteBlockPixels(void)
{
    unsigned char *row1Ptr = (unsigned char*)fRowBuf[0];
    unsigned char *row2Ptr = (unsigned char*)fRowBuf[1];
    unsigned char *row3Ptr = (unsigned char*)fRowBuf[2];
    unsigned char *row4Ptr = (unsigned char*)fRowBuf[3];

    for (int flagSet = 0; flagSet <= 1; flagSet++)
    {
        unsigned int *flagsPtr = fPixelFilteredFlags[0];
        unsigned char *rowA = (unsigned char*)fRowBuf[0];
        unsigned char *rowB = (unsigned char*)fRowBuf[1];

        if (flagSet == 1)
        {
            flagsPtr = fPixelFilteredFlags[1];
            rowA = (unsigned char*)fRowBuf[2];
            rowB = (unsigned char*)fRowBuf[3];
        }

        for (int rowIndex = 0; rowIndex < fRowWidthInPixels;)
        {
            unsigned int currentFlags = flagsPtr[rowIndex];

#ifndef NDEBUG /* only done for debug builds */
            int numberOfBitsSet = 0;
            unsigned int currentFlagsCopy = currentFlags & eTopLeftOfBlocks;
            while (currentFlagsCopy)
            {
                if (currentFlagsCopy & 1) numberOfBitsSet++;
                currentFlagsCopy >>= 1;
            }
            ASSERT( (numberOfBitsSet <= 1) ||
                    ((numberOfBitsSet == 2) &&
                    (((currentFlags & eTopLeftOfBlocks) & ~(e21nw|e21sw|e41ni|e41si))==0)));
#endif

            if (currentFlags & eTopLeftOfBlocks) // Avoids doing a lot of checks if nothing is set.
            {
//                unsigned int pixel;
                uint32_t pixel;
                //  The three possible scenerios are:
                //  1: No top left of block bits are set.
                //  2: 1 top left block bit is set.
                //  3: 2 top left block bits are set. They are 21nw and 21sw.

                // Note: Due to possibly having two groups tracked by this flag we require the north checks to occur before the south checks.
                if (currentFlags & e22w)
                {
                    pixel = get4Pixel(rowA, rowIndex);

                    put4Pixel(rowB, rowIndex, pixel);
                    rowIndex += 1;
                    put4Pixel(rowA, rowIndex, pixel);
                    put4Pixel(rowB, rowIndex, pixel);

                    rowIndex += 1;
                    continue;
                }

                if (currentFlags & e12)
                {
                    put4Pixel(rowB, rowIndex, get4Pixel(rowA, rowIndex));

                    rowIndex += 1;
                    continue;
                }

                if (currentFlags & e42i)
                {
                    pixel = get4Pixel(rowA, rowIndex);

                    put4Pixel(rowB, rowIndex, pixel);

                    rowIndex += 1;
                    put4Pixel(rowA, rowIndex, pixel);
                    put4Pixel(rowB, rowIndex, pixel);

                    rowIndex += 1;
                    put4Pixel(rowB, rowIndex, pixel);
                    put4Pixel(rowA, rowIndex, pixel);

                    rowIndex += 1;
                    put4Pixel(rowA, rowIndex, pixel);
                    put4Pixel(rowB, rowIndex, pixel);

                    rowIndex += 1;
                    continue;
                }

                if (currentFlags & e84ni)
                {
                    pixel = get4Pixel(rowA, rowIndex);

                    put4Pixel(row2Ptr, rowIndex, pixel);
                    put4Pixel(row3Ptr, rowIndex, pixel);
                    put4Pixel(row4Ptr, rowIndex, pixel);

                    rowIndex += 1;
                    put4Pixel(row1Ptr, rowIndex, pixel);
                    put4Pixel(row2Ptr, rowIndex, pixel);
                    put4Pixel(row3Ptr, rowIndex, pixel);
                    put4Pixel(row4Ptr, rowIndex, pixel);

                    rowIndex += 1;
                    put4Pixel(row1Ptr, rowIndex, pixel);
                    put4Pixel(row2Ptr, rowIndex, pixel);
                    put4Pixel(row3Ptr, rowIndex, pixel);
                    put4Pixel(row4Ptr, rowIndex, pixel);

                    rowIndex += 1;
                    put4Pixel(row1Ptr, rowIndex, pixel);
                    put4Pixel(row2Ptr, rowIndex, pixel);
                    put4Pixel(row3Ptr, rowIndex, pixel);
                    put4Pixel(row4Ptr, rowIndex, pixel);

                    rowIndex += 1;
                    put4Pixel(row1Ptr, rowIndex, pixel);
                    put4Pixel(row2Ptr, rowIndex, pixel);
                    put4Pixel(row3Ptr, rowIndex, pixel);
                    put4Pixel(row4Ptr, rowIndex, pixel);

                    rowIndex += 1;
                    put4Pixel(row1Ptr, rowIndex, pixel);
                    put4Pixel(row2Ptr, rowIndex, pixel);
                    put4Pixel(row3Ptr, rowIndex, pixel);
                    put4Pixel(row4Ptr, rowIndex, pixel);

                    rowIndex += 1;
                    put4Pixel(row1Ptr, rowIndex, pixel);
                    put4Pixel(row2Ptr, rowIndex, pixel);
                    put4Pixel(row3Ptr, rowIndex, pixel);
                    put4Pixel(row4Ptr, rowIndex, pixel);

                    rowIndex += 1;
                    put4Pixel(row1Ptr, rowIndex, pixel);
                    put4Pixel(row2Ptr, rowIndex, pixel);
                    put4Pixel(row3Ptr, rowIndex, pixel);
                    put4Pixel(row4Ptr, rowIndex, pixel);

                    rowIndex += 1;

                    continue;
                }

                if (currentFlags & e24nw)
                {
                    pixel = get4Pixel(row1Ptr, rowIndex);

                    put4Pixel(row2Ptr, rowIndex, pixel);
                    put4Pixel(row3Ptr, rowIndex, pixel);
                    put4Pixel(row4Ptr, rowIndex, pixel);

                    rowIndex += 1;
                    put4Pixel(row1Ptr, rowIndex, pixel);
                    put4Pixel(row2Ptr, rowIndex, pixel);
                    put4Pixel(row3Ptr, rowIndex, pixel);
                    put4Pixel(row4Ptr, rowIndex, pixel);

                    rowIndex += 1;
                    continue;
                }

                if (currentFlags & e44ni)
                {
                    pixel = get4Pixel(row1Ptr, rowIndex);

                    put4Pixel(row2Ptr, rowIndex, pixel);
                    put4Pixel(row3Ptr, rowIndex, pixel);
                    put4Pixel(row4Ptr, rowIndex, pixel);

                    rowIndex += 1;
                    put4Pixel(row1Ptr, rowIndex, pixel);
                    put4Pixel(row2Ptr, rowIndex, pixel);
                    put4Pixel(row3Ptr, rowIndex, pixel);
                    put4Pixel(row4Ptr, rowIndex, pixel);

                    rowIndex += 1;
                    put4Pixel(row1Ptr, rowIndex, pixel);
                    put4Pixel(row2Ptr, rowIndex, pixel);
                    put4Pixel(row3Ptr, rowIndex, pixel);
                    put4Pixel(row4Ptr, rowIndex, pixel);

                    rowIndex += 1;
                    put4Pixel(row1Ptr, rowIndex, pixel);
                    put4Pixel(row2Ptr, rowIndex, pixel);
                    put4Pixel(row3Ptr, rowIndex, pixel);
                    put4Pixel(row4Ptr, rowIndex, pixel);

                    rowIndex += 1;
                    continue;
                }

                if (currentFlags & e14n)
                {
                    pixel = get4Pixel(row1Ptr, rowIndex);

                    put4Pixel(row2Ptr, rowIndex, pixel);
                    put4Pixel(row3Ptr, rowIndex, pixel);
                    put4Pixel(row4Ptr, rowIndex, pixel);

                    rowIndex += 1;
                    continue;
                }

                if (currentFlags & e21nw)
                {
                    put4Pixel(rowA, rowIndex+1, get4Pixel(rowA, rowIndex));

                    if (!(currentFlags & (e21sw|e41si))) // if no south groups
                    {
                        rowIndex += 2;
                        continue;
                    }
                }

                if (currentFlags & e41ni)
                {
                    pixel = get4Pixel(rowA, rowIndex);

                    put4Pixel(rowA, rowIndex+1, pixel);
                    put4Pixel(rowA, rowIndex+2, pixel);
                    put4Pixel(rowA, rowIndex+3, pixel);

                    if (!(currentFlags & (e21sw|e41si))) // if no south groups.
                    {
                        rowIndex += 2;
                        continue;
                    }
                }

                if (currentFlags & e21sw)
                {
                    put4Pixel(rowB, rowIndex+1, get4Pixel(rowB, rowIndex));

                    rowIndex += 2;
                    continue;
                }

                if (currentFlags & e41si)
                {
                    pixel = get4Pixel(rowB, rowIndex);

                    put4Pixel(rowB, rowIndex+1, pixel);
                    put4Pixel(rowB, rowIndex+2, pixel);
                    put4Pixel(rowB, rowIndex+3, pixel);

                    rowIndex += 2;
                    continue;
                }
            }
            rowIndex += 1;
        }
    }
}

#endif // kMemWritesOptimize

APDK_END_NAMESPACE

#endif  // APDK_DJ9xxVIP && APDK_VIP_COLORFILTERING