summaryrefslogtreecommitdiff
path: root/src/graphics/GraphicsTest.cpp
blob: 9d517f5bc198f4135b44d6802e6c2d34dc8d7a0c (plain)
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
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#include "GraphicsTest.h"
#include "Bitmap.h"
#include "BitmapLoader.h"
#include "Filterfliprgb.h"
#include "Filtergrayscale.h"

#include "../base/Directory.h"
#include "../base/Exception.h"

#include <iostream>
#include <sstream>
#include <math.h>

namespace avg {

using namespace avg;
using namespace std;

GraphicsTest::GraphicsTest(const string& sName, int indentLevel)
        : Test(sName, indentLevel)
{
}

void GraphicsTest::createResultImgDir()
{
    Directory dir("resultimages");
    int ok = dir.open(true);
    if (ok == 0) {
        dir.empty();
    } else {
        stringstream s;
        s << "Could not create result image dir '" << dir.getName() << "'.";
        cerr << s.str() << endl;
        throw Exception(AVG_ERR_VIDEO_GENERAL, s.str());
    }
}

BitmapPtr GraphicsTest::loadTestBmp(const std::string& sFName, PixelFormat pf)
{
    try {
        string sFullName = getSrcDirName()+"../test/media/"+sFName+".png";
        return loadBitmap(sFullName, pf);
    } catch (Exception & ex) {
        cerr << ex.getStr() << endl;
        throw;
    }
}

void GraphicsTest::testEqual(Bitmap& resultBmp, const string& sFName, PixelFormat pf, 
        float maxAverage, float maxStdDev) 
{
    BitmapPtr pBaselineBmp;
    try {
        string sFullName = getSrcDirName()+"baseline/"+sFName+".png";
        pBaselineBmp = loadBitmap(sFullName, pf);
    } catch (Exception & ex) {
        cerr << ex.getStr() << endl;
        resultBmp.save("resultimages/"+sFName+".png");
        throw;
    }
    testEqual(resultBmp, *pBaselineBmp, sFName, maxAverage, maxStdDev);
}

void GraphicsTest::testEqual(Bitmap& resultBmp, Bitmap& baselineBmp, 
        const string& sFName, float maxAverage, float maxStdDev)
{
    BitmapPtr pDiffBmp;
    try {
        pDiffBmp = resultBmp.subtract(baselineBmp);
    } catch (Exception& e) {
        TEST_FAILED("Error: " << e.getStr() << ". File: '" << sFName << "'.");
        string sResultName = "resultimages/"+sFName;
        resultBmp.save(sResultName+".png");
        baselineBmp.save(sResultName+"_baseline.png");
    }
    if (pDiffBmp) {
        float average = pDiffBmp->getAvg();
        float stdDev = pDiffBmp->getStdDev();
        if (average > maxAverage || stdDev > maxStdDev) {
            TEST_FAILED("Error: Decoded image differs from baseline '" << 
                    sFName << "'. average=" << average << ", stdDev=" << stdDev);
    //        resultBmp.dump();
    //        baselineBmp.dump();
            string sResultName = "resultimages/"+sFName;
            resultBmp.save(sResultName+".png");
            baselineBmp.save(sResultName+"_baseline.png");
            BitmapPtr pDiffBmp = resultBmp.subtract(baselineBmp);
            pDiffBmp->save(sResultName+"_diff.png");
        }
    }
}

void GraphicsTest::testEqualBrightness(Bitmap& resultBmp, Bitmap& baselineBmp, 
        float epsilon)
{
    float diff = fabs(resultBmp.getAvg()-baselineBmp.getAvg());
    if (diff >= epsilon) {
        TEST_FAILED("Error: Baseline brightness: " << baselineBmp.getAvg()
                << ", Result brightness: " << resultBmp.getAvg() << ", difference: " 
                << diff);
    }
}

int GraphicsTest::sumPixels(Bitmap& bmp)
{
    AVG_ASSERT(bmp.getBytesPerPixel() == 4);
    int sum = 0;
    IntPoint size = bmp.getSize();
    for (int y = 0; y < size.y; y++) {
        unsigned char * pLine = bmp.getPixels()+y*bmp.getStride();
        for (int x = 0; x < size.x; x++) { 
            sum += pLine[x*4];
            sum += pLine[x*4+1];
            sum += pLine[x*4+2];
        }
    }
    return sum;
}

};