summaryrefslogtreecommitdiff
path: root/xfa/src/fxbarcode/src/BC_GlobalHistogramBinarizer.cpp
blob: e20ad68e706557ceb59064cb836d2bd5e48fffae (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
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "barcode.h"
#include "include/BC_Binarizer.h"
#include "include/BC_LuminanceSource.h"
#include "include/BC_CommonBitMatrix.h"
#include "include/BC_CommonBitArray.h"
#include "include/BC_GlobalHistogramBinarizer.h"
const FX_INT32 LUMINANCE_BITS = 5;
const FX_INT32 LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
const FX_INT32 LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
CBC_GlobalHistogramBinarizer::CBC_GlobalHistogramBinarizer(CBC_LuminanceSource *source): CBC_Binarizer(source)
{
}
CBC_GlobalHistogramBinarizer::~CBC_GlobalHistogramBinarizer()
{
}
CBC_CommonBitArray *CBC_GlobalHistogramBinarizer::GetBlackRow(FX_INT32 y, CBC_CommonBitArray *row, FX_INT32 &e)
{
    CBC_LuminanceSource *source = GetLuminanceSource();
    FX_INT32 width = source->GetWidth();
    CBC_AutoPtr<CBC_CommonBitArray> result(FX_NEW CBC_CommonBitArray(width));
    InitArrays(width);
    CFX_ByteArray *localLuminances = source->GetRow(y, m_luminance, e);
    if (e != BCExceptionNO) {
        return result.release();
    }
    CFX_Int32Array localBuckets;
    localBuckets.Copy(m_buckets);
    FX_INT32 x;
    for (x = 0; x < width; x++) {
        FX_INT32 pixel = (*localLuminances)[x] & 0xff;
        localBuckets[pixel >> LUMINANCE_SHIFT]++;
    }
    FX_INT32 blackPoint = EstimateBlackPoint(localBuckets, e);
    if (e != BCExceptionNO) {
        return result.release();
    }
    FX_INT32 left = (*localLuminances)[0] & 0xff;
    FX_INT32 center = (*localLuminances)[1] & 0xff;
    for (x = 1; x < width - 1; x++) {
        FX_INT32 right = (*localLuminances)[x + 1] & 0xff;
        FX_INT32 luminance = ((center << 2) - left - right) >> 1;
        if (luminance < blackPoint) {
            result->Set(x);
        }
        left = center;
        center = right;
    }
    return  result.release();
}
CBC_CommonBitMatrix *CBC_GlobalHistogramBinarizer::GetBlackMatrix(FX_INT32 &e)
{
    CBC_LuminanceSource *source = GetLuminanceSource();
    FX_INT32 width = source->GetWidth();
    FX_INT32 height = source->GetHeight();
    CBC_CommonBitMatrix *BitMatrixTemp = FX_NEW CBC_CommonBitMatrix();
    BitMatrixTemp->Init(width, height);
    CBC_AutoPtr<CBC_CommonBitMatrix> matrix(BitMatrixTemp);
    InitArrays(width);
    CFX_Int32Array localBuckets;
    localBuckets.Copy(m_buckets);
    FX_INT32 y;
    for (y = 1; y < 5; y++) {
        FX_INT32 row = height * y / 5;
        CFX_ByteArray *localLuminances = source->GetRow(row, m_luminance, e);
        BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
        FX_INT32 right = (width << 2) / 5;
        FX_INT32 x;
        for (x = width / 5; x < right; x++) {
            FX_INT32 pixel = (*localLuminances)[x] & 0xff;
            localBuckets[pixel >> LUMINANCE_SHIFT]++;
        }
    }
    FX_INT32 blackPoint = EstimateBlackPoint(localBuckets, e);
    BC_EXCEPTION_CHECK_ReturnValue(e, NULL);
    CBC_AutoPtr<CFX_ByteArray> localLuminances(source->GetMatrix());
    for (y = 0; y < height; y++) {
        FX_INT32 offset = y * width;
        for (FX_INT32 x = 0; x < width; x++) {
            FX_INT32 pixel = (*localLuminances)[offset + x] & 0xff;
            if (pixel < blackPoint) {
                matrix->Set(x, y);
            }
        }
    }
    return matrix.release();
}
void CBC_GlobalHistogramBinarizer::InitArrays(FX_INT32 luminanceSize)
{
    if(m_luminance.GetSize() < luminanceSize) {
        m_luminance.SetSize(luminanceSize);
    }
    if(m_buckets.GetSize() <= 0) {
        m_buckets.SetSize(LUMINANCE_BUCKETS);
    } else {
        FX_INT32 x;
        for(x = 0; x < LUMINANCE_BUCKETS; x++) {
            m_buckets[x] = 0;
        }
    }
}
FX_INT32 CBC_GlobalHistogramBinarizer::EstimateBlackPoint(CFX_Int32Array &buckets, FX_INT32 &e)
{
    FX_INT32 numBuckets = buckets.GetSize();
    FX_INT32 maxBucketCount = 0;
    FX_INT32 firstPeak = 0;
    FX_INT32 firstPeakSize = 0;
    FX_INT32 x;
    for (x = 0; x < numBuckets; x++) {
        if (buckets[x] > firstPeakSize) {
            firstPeak = x;
            firstPeakSize = buckets[x];
        }
        if (buckets[x] > maxBucketCount) {
            maxBucketCount = buckets[x];
        }
    }
    FX_INT32 secondPeak = 0;
    FX_INT32 secondPeakScore = 0;
    for (x = 0; x < numBuckets; x++) {
        FX_INT32 distanceToBiggest = x - firstPeak;
        FX_INT32 score = buckets[x] * distanceToBiggest * distanceToBiggest;
        if (score > secondPeakScore) {
            secondPeak = x;
            secondPeakScore = score;
        }
    }
    if (firstPeak > secondPeak) {
        FX_INT32 temp = firstPeak;
        firstPeak = secondPeak;
        secondPeak = temp;
    }
    if (secondPeak - firstPeak <= numBuckets >> 4) {
        e = BCExceptionRead;
        return 0;
    }
    FX_INT32 bestValley = secondPeak - 1;
    FX_INT32 bestValleyScore = -1;
    for (x = secondPeak - 1; x > firstPeak; x--) {
        FX_INT32 fromFirst = x - firstPeak;
        FX_INT32 score = fromFirst * fromFirst * (secondPeak - x) * (maxBucketCount - buckets[x]);
        if (score > bestValleyScore) {
            bestValley = x;
            bestValleyScore = score;
        }
    }
    return bestValley << LUMINANCE_SHIFT;
}