summaryrefslogtreecommitdiff
path: root/src/graphics/HistoryPreProcessor.cpp
blob: 673df9053130641c40afb46c3e5b4239e3d7bf8d (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
//  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
//
//  Original author of this file is igor@c-base.org.
//

#include "HistoryPreProcessor.h"

#include "Bitmap.h"
#include "Filterfill.h"

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

#include <iostream>
#include <sstream>

using namespace std;

#define FAST_HISTORY_SPEED 16

namespace avg {
    
HistoryPreProcessor::HistoryPreProcessor(IntPoint dimensions, 
        unsigned int updateInterval, bool bBrighter)
    : m_FrameCounter(0),
      m_UpdateInterval(updateInterval),
      m_bBrighter(bBrighter)
{
    m_pHistoryBmp = BitmapPtr(new Bitmap(dimensions, I16));
    reset();
}

HistoryPreProcessor::~HistoryPreProcessor()
{
}

void HistoryPreProcessor::setInterval(unsigned int updateInterval)
{
    m_FrameCounter = 0;
    m_UpdateInterval = updateInterval;
}

unsigned int HistoryPreProcessor::getInterval()
{
    return m_UpdateInterval;
}

void HistoryPreProcessor::reset()
{
    m_State = NO_IMAGE;
}

void HistoryPreProcessor::updateHistory(BitmapPtr pNewBmp)
{
    AVG_ASSERT(pNewBmp->getSize() == m_pHistoryBmp->getSize());
    switch (m_State) {
        case NO_IMAGE:
            m_pHistoryBmp->copyPixels(*pNewBmp);
            m_State = INITIALIZING;
            m_NumInitImages = 0;
            break;
        case INITIALIZING:
            calcAvg<FAST_HISTORY_SPEED>(pNewBmp);
            m_NumInitImages++;
            if (m_NumInitImages == FAST_HISTORY_SPEED*2) {
                m_State = NORMAL;
            }
            break;
        case NORMAL:
            if (m_FrameCounter < m_UpdateInterval-1) {
                m_FrameCounter++;
            } else {
                m_FrameCounter = 0;
                calcAvg<256>(pNewBmp);
            }
            break;
    }
}

void HistoryPreProcessor::applyInPlace(BitmapPtr pBmp)
{
    updateHistory(pBmp);
    unsigned short * pSrc = (unsigned short*)m_pHistoryBmp->getPixels();
    int srcStride = m_pHistoryBmp->getStride()/m_pHistoryBmp->getBytesPerPixel();
    int destStride = pBmp->getStride();
    unsigned char * pDest = pBmp->getPixels();
    IntPoint size = pBmp->getSize();
    for (int y = 0; y < size.y; y++) {
        const unsigned short * pSrcPixel = pSrc;
        unsigned char * pDestPixel = pDest;
        if (m_bBrighter) {
            for (int x = 0; x < size.x; x++) {
                unsigned char Src = *pSrcPixel/256;
                if ((*pDestPixel) > Src) {
                    *pDestPixel = *pDestPixel-Src;
                } else {
                    *pDestPixel = 0;
                }
                pDestPixel++;
                pSrcPixel++;
            }
        } else {
            for (int x = 0; x < size.x; x++) {
                unsigned char Src = *pSrcPixel/256;
                if ((*pDestPixel) < Src) {
                    *pDestPixel = Src-*pDestPixel;
                } else {
                    *pDestPixel = 0;
                }
                pDestPixel++;
                pSrcPixel++;
            }
        }
        pDest += destStride;
        pSrc += srcStride;
    }
}

// Fast pseudo-normalization with an integer factor.
void HistoryPreProcessor::normalizeHistogram(BitmapPtr pBmp, unsigned char max)
{
    if (max < 128) {
        max = 128;
    }
    int factor = int(256.0/max);
    unsigned char * pLine = pBmp->getPixels();
    IntPoint size = pBmp->getSize();
    int stride = pBmp->getStride();
    for (int y = 0; y < size.y; y++) {
        unsigned char * pPixel = pLine;
        for (int x = 0; x < size.x; x++) {
            *pPixel *= factor;
            pPixel++;
        }
        pLine += stride;
    }
}

}