File: CanvasNoiseInjection.cpp

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (316 lines) | stat: -rw-r--r-- 16,389 bytes parent folder | download | duplicates (7)
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
/*
 * Copyright (c) 2023 Apple Inc. 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.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``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 APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "CanvasNoiseInjection.h"

#include "ByteArrayPixelBuffer.h"
#include "FloatRect.h"
#include "ImageBuffer.h"
#include "PixelBuffer.h"

namespace WebCore {

void CanvasNoiseInjection::updateDirtyRect(const IntRect& rect)
{
    m_postProcessDirtyRect.unite(rect);
}

void CanvasNoiseInjection::clearDirtyRect()
{
    m_postProcessDirtyRect = { };
}

static inline bool isIndexInBounds(int size, int index)
{
    ASSERT(index <= size);
    return index < size;
}

static inline bool setTightnessBounds(std::span<uint8_t> bytes, std::array<int, 4>& tightestBoundingDiff, int index1, int index2, int index3)
{
    int boundingRedDiff = std::abs(static_cast<int>(bytes[index1]) - static_cast<int>(bytes[index3]));
    int boundingGreenDiff = std::abs(static_cast<int>(bytes[index1 + 1]) - static_cast<int>(bytes[index3 + 1]));
    int boundingBlueDiff = std::abs(static_cast<int>(bytes[index1 + 2]) - static_cast<int>(bytes[index3 + 2]));
    int boundingAlphaDiff = std::abs(static_cast<int>(bytes[index1 + 3]) - static_cast<int>(bytes[index3 + 3]));

    int neighborRedDiff1 = std::abs(static_cast<int>(bytes[index1]) - static_cast<int>(bytes[index2]));
    int neighborGreenDiff1 = std::abs(static_cast<int>(bytes[index1 + 1]) - static_cast<int>(bytes[index2 + 1]));
    int neighborBlueDiff1 = std::abs(static_cast<int>(bytes[index1 + 2]) - static_cast<int>(bytes[index2 + 2]));
    int neighborAlphaDiff1 = std::abs(static_cast<int>(bytes[index1 + 3]) - static_cast<int>(bytes[index2 + 3]));

    int neighborRedDiff2 = std::abs(static_cast<int>(bytes[index2]) - static_cast<int>(bytes[index3]));
    int neighborGreenDiff2 = std::abs(static_cast<int>(bytes[index2 + 1]) - static_cast<int>(bytes[index3 + 1]));
    int neighborBlueDiff2 = std::abs(static_cast<int>(bytes[index2 + 2]) - static_cast<int>(bytes[index3 + 2]));
    int neighborAlphaDiff2 = std::abs(static_cast<int>(bytes[index2 + 3]) - static_cast<int>(bytes[index3 + 3]));

    bool updatedTightnessBounds { false };

    if (boundingRedDiff <= 1 && boundingGreenDiff <= 1 && boundingBlueDiff <= 1 && boundingAlphaDiff <= 1
        && neighborRedDiff1 <= 1 && neighborGreenDiff1 <= 1 && neighborBlueDiff1 <= 1 && neighborAlphaDiff1 <= 1
        && neighborRedDiff2 <= 1 && neighborGreenDiff2 <= 1 && neighborBlueDiff2 <= 1 && neighborAlphaDiff2 <= 1) {
        tightestBoundingDiff[0] = 0;
        tightestBoundingDiff[1] = 0;
        tightestBoundingDiff[2] = 0;
        tightestBoundingDiff[3] = 0;
        updatedTightnessBounds = true;
    } else if (boundingRedDiff < tightestBoundingDiff[0]
        && boundingGreenDiff < tightestBoundingDiff[1]
        && boundingBlueDiff < tightestBoundingDiff[2]
        && boundingAlphaDiff < tightestBoundingDiff[3]) {
        tightestBoundingDiff[0] = boundingRedDiff;
        tightestBoundingDiff[1] = boundingGreenDiff;
        tightestBoundingDiff[2] = boundingBlueDiff;
        tightestBoundingDiff[3] = boundingAlphaDiff;
        updatedTightnessBounds = true;
    }
    return updatedTightnessBounds;
}

static std::pair<std::array<int, 4>, std::array<int, 4>> boundingNeighbors(int index, std::span<uint8_t> bytes, const IntSize& size)
{
    constexpr auto bytesPerPixel = 4U;
    auto bufferSize = bytes.size_bytes();
    auto pixelIndex = index / bytesPerPixel;
    bool isInTopRow = pixelIndex < static_cast<size_t>(size.width());
    bool isInBottomRow = pixelIndex > static_cast<size_t>((size.height() - 1) * size.width());
    bool isInLeftColumn = !(pixelIndex % size.width());
    bool isInRightColumn = (pixelIndex % size.width()) == static_cast<unsigned>(size.width()) - 1;
    bool isInTopLeftCorner = isInTopRow && isInLeftColumn;
    bool isInBottomLeftCorner = isInBottomRow && isInLeftColumn;
    bool isInTopRightCorner = isInTopRow && isInRightColumn;
    bool isInBotomRightCorner = isInBottomRow && isInRightColumn;

    constexpr auto leftOffset = -bytesPerPixel;
    constexpr auto rightOffset = bytesPerPixel;
    const auto aboveOffset = -size.width() * bytesPerPixel;
    const auto belowOffset = size.width() * bytesPerPixel;
    const auto aboveLeftOffset = aboveOffset + leftOffset;
    const auto aboveRightOffset = aboveOffset + rightOffset;
    const auto belowLeftOffset = belowOffset + leftOffset;
    const auto belowRightOffset = belowOffset + rightOffset;

    const int leftIndex = index + leftOffset;
    const int rightIndex = index + rightOffset;
    const int aboveIndex = index + aboveOffset;
    const int belowIndex = index + belowOffset;
    const int aboveLeftIndex = index + aboveLeftOffset;
    const int aboveRightIndex = index + aboveRightOffset;
    const int belowLeftIndex = index + belowLeftOffset;
    const int belowRightIndex = index + belowRightOffset;

    const auto areColorsRelated = [&bytes, bufferSize](int index1, int index2, int index3) {
        constexpr auto maxDistanceThreshold = 8;
        if (!isIndexInBounds(bufferSize, index1) || !isIndexInBounds(bufferSize, index1 + 3))
            return false;
        if (!isIndexInBounds(bufferSize, index2) || !isIndexInBounds(bufferSize, index2 + 3))
            return false;
        if (!isIndexInBounds(bufferSize, index3) || !isIndexInBounds(bufferSize, index3 + 3))
            return false;
        bool isColorNearBoundingColor1 = std::abs(static_cast<int>(bytes[index1]) - static_cast<int>(bytes[index2])) <= maxDistanceThreshold
            && std::abs(static_cast<int>(bytes[index1 + 1]) - static_cast<int>(bytes[index2 + 1])) <= maxDistanceThreshold
            && std::abs(static_cast<int>(bytes[index1 + 2]) - static_cast<int>(bytes[index2 + 2])) <= maxDistanceThreshold
            && std::abs(static_cast<int>(bytes[index1 + 3]) - static_cast<int>(bytes[index2 + 3])) <= maxDistanceThreshold;

        bool isColorNearBoundingColor2 =  std::abs(static_cast<int>(bytes[index3]) - static_cast<int>(bytes[index2])) <= maxDistanceThreshold
            && std::abs(static_cast<int>(bytes[index3 + 1]) - static_cast<int>(bytes[index2 + 1])) <= maxDistanceThreshold
            && std::abs(static_cast<int>(bytes[index3 + 2]) - static_cast<int>(bytes[index2 + 2])) <= maxDistanceThreshold
            && std::abs(static_cast<int>(bytes[index3 + 3]) - static_cast<int>(bytes[index2 + 3])) <= maxDistanceThreshold;

        return isColorNearBoundingColor1 || isColorNearBoundingColor2;
    };

    const auto compareColorsAndSetBounds = [&areColorsRelated](const auto& bytes, auto& tightestBoundingColors, auto& tightestBoundingDiff, auto colorIndex, auto neighborIndex1, auto neighborIndex2) {
        if (areColorsRelated(neighborIndex1, colorIndex, neighborIndex2)) {
            if (setTightnessBounds(bytes, tightestBoundingDiff, neighborIndex1, colorIndex, neighborIndex2)) {
                if (WTF::allOf(tightestBoundingDiff, [](auto& item) { return !item; })) {
                    tightestBoundingColors = {
                        { bytes[colorIndex], bytes[colorIndex + 1], bytes[colorIndex + 2], bytes[colorIndex + 3] },
                        { bytes[colorIndex], bytes[colorIndex + 1], bytes[colorIndex + 2], bytes[colorIndex + 3] }
                    };
                } else {
                    tightestBoundingColors = {
                        { bytes[neighborIndex1], bytes[neighborIndex1 + 1], bytes[neighborIndex1 + 2], bytes[neighborIndex1 + 3] },
                        { bytes[neighborIndex2], bytes[neighborIndex2 + 1], bytes[neighborIndex2 + 2], bytes[neighborIndex2 + 3] }
                    };
                }
            }
        }

        return tightestBoundingColors;
    };

    std::pair<std::array<int, 4>, std::array<int, 4>> tightestBoundingColors {
        { 0, 0, 0, 0 },
        { 255, 255, 255, 255 }
    };
    std::array<int, 4> tightestBoundingDiff { 255, 255, 255, 255 };

    if (isInTopLeftCorner || isInBottomLeftCorner || isInTopRightCorner || isInBotomRightCorner)
        return tightestBoundingColors;

    if (isInTopRow || isInBottomRow)
        return compareColorsAndSetBounds(bytes, tightestBoundingColors, tightestBoundingDiff, index, leftIndex, rightIndex);

    if (isInLeftColumn || isInRightColumn)
        return compareColorsAndSetBounds(bytes, tightestBoundingColors, tightestBoundingDiff, index, aboveIndex, belowIndex);

    compareColorsAndSetBounds(bytes, tightestBoundingColors, tightestBoundingDiff, index, leftIndex, rightIndex);
    compareColorsAndSetBounds(bytes, tightestBoundingColors, tightestBoundingDiff, index, aboveIndex, belowIndex);
    compareColorsAndSetBounds(bytes, tightestBoundingColors, tightestBoundingDiff, index, aboveLeftIndex, belowRightIndex);
    compareColorsAndSetBounds(bytes, tightestBoundingColors, tightestBoundingDiff, index, aboveRightIndex, belowLeftIndex);

    return tightestBoundingColors;
}

void CanvasNoiseInjection::postProcessDirtyCanvasBuffer(ImageBuffer* imageBuffer, NoiseInjectionHashSalt salt, CanvasNoiseInjectionPostProcessArea postProcessArea)
{

    if (m_postProcessDirtyRect.isEmpty() && postProcessArea == CanvasNoiseInjectionPostProcessArea::DirtyRect)
        return;

    if (!imageBuffer)
        return;

    auto dirtyRect = postProcessArea == CanvasNoiseInjectionPostProcessArea::DirtyRect ? m_postProcessDirtyRect : IntRect(IntPoint::zero(), imageBuffer->truncatedLogicalSize());

    PixelBufferFormat format { AlphaPremultiplication::Unpremultiplied, PixelFormat::RGBA8, imageBuffer->colorSpace() };
    auto pixelBuffer = imageBuffer->getPixelBuffer(format, dirtyRect);
    if (!is<ByteArrayPixelBuffer>(pixelBuffer))
        return;

    if (postProcessPixelBufferResults(*pixelBuffer, salt)) {
        imageBuffer->putPixelBuffer(*pixelBuffer, { IntPoint::zero(), dirtyRect.size() }, dirtyRect.location());
        m_postProcessDirtyRect = { };
    }
}

static std::pair<int, int> lowerAndUpperBound(int component1, int component2, int component3)
{
    if (component1 <= component3) {
        if (component1 == component2 || component2 == component3)
            return { component2, component2 };
        if (component1 <= component2 && component2 <= component3)
            return { component1, component3 };
        if (component1 >= component2 && component2 <= component3)
            return { component2, component1 };
        if (component1 <= component2 && component2 >= component3)
            return { component3, component2 };
    } else if (component1 > component3) {
        if (component1 < component2 && component2 > component3)
            return { component3, component1 };
        if (component1 > component2 && component2 < component3)
            return { component2, component3 };
        if (component1 < component2 && component2 > component3)
            return { component1, component2 };
    }
    return { component2, component2 };
}

static void adjustNeighborColorBounds(std::array<int, 4>& neighborColor1, const std::array<int, 4>& color, std::array<int, 4>& neighborColor2)
{
    auto [redLowerBound, redUpperBound] = lowerAndUpperBound(neighborColor1[0], color[0], neighborColor2[0]);
    auto [greenLowerBound, greenUpperBound] = lowerAndUpperBound(neighborColor1[1], color[1], neighborColor2[1]);
    auto [blueLowerBound, blueUpperBound] = lowerAndUpperBound(neighborColor1[2], color[2], neighborColor2[2]);
    auto [alphaLowerBound, alphaUpperBound] = lowerAndUpperBound(neighborColor1[3], color[3], neighborColor2[3]);

    neighborColor1[0] = redLowerBound;
    neighborColor2[0] = redUpperBound;
    neighborColor1[1] = greenLowerBound;
    neighborColor2[1] = greenUpperBound;
    neighborColor1[2] = blueLowerBound;
    neighborColor2[2] = blueUpperBound;
    neighborColor1[3] = alphaLowerBound;
    neighborColor2[3] = alphaUpperBound;
}

bool CanvasNoiseInjection::postProcessPixelBufferResults(PixelBuffer& pixelBuffer, NoiseInjectionHashSalt salt) const
{
    ASSERT(pixelBuffer.format().pixelFormat == PixelFormat::RGBA8);

    constexpr int bytesPerPixel = 4;
    auto bytes = pixelBuffer.bytes();
    bool wasPixelBufferModified { false };

    // Salt value 0 is used for testing.
    if (!salt)
        return true;

    for (size_t i = 0; i < bytes.size_bytes(); i += bytesPerPixel) {
        auto& redChannel = bytes[i];
        auto& greenChannel = bytes[i + 1];
        auto& blueChannel = bytes[i + 2];
        auto& alphaChannel = bytes[i + 3];
        bool isBlack { !redChannel && !greenChannel && !blueChannel };

        if (!alphaChannel)
            continue;

        const auto clampedColorComponentOffset = [](int colorComponentOffset, int originalComponentValue, int minValue, int maxValue) {
            if (originalComponentValue > maxValue)
                maxValue = 255;
            if (originalComponentValue < minValue)
                minValue = 0;
            if (colorComponentOffset + originalComponentValue > maxValue)
                colorComponentOffset = maxValue - originalComponentValue;
            else if (colorComponentOffset + originalComponentValue < minValue)
                colorComponentOffset = minValue - originalComponentValue;
            return colorComponentOffset;
        };

        std::array<int, 4> lowerBoundColor { 0, 0, 0, 0 };
        std::array<int, 4> upperBoundColor { 255, 255, 255, 255 };
        auto [neighborColor1, neighborColor2] = boundingNeighbors(i, bytes, pixelBuffer.size());

        // +/- 1 is roughly ~0.5% of the 255 max value.
        // +/- 3 is roughly ~1% of the 255 max value.
        int maxNoiseValue = 3;
        if (neighborColor1 != neighborColor2 && neighborColor1 != lowerBoundColor && neighborColor2 != upperBoundColor) {
            maxNoiseValue = 1;
            adjustNeighborColorBounds(neighborColor1, { redChannel, greenChannel, blueChannel, alphaChannel }, neighborColor2);
        }

        lowerBoundColor = neighborColor1;
        upperBoundColor = neighborColor2;

        const uint64_t pixelHash = computeHash(salt, redChannel, greenChannel, blueChannel, alphaChannel);
        auto noiseValue = static_cast<int8_t>(((pixelHash * 2 * maxNoiseValue) / std::numeric_limits<uint32_t>::max()) - maxNoiseValue);

        // If alpha is non-zero and the color channels are zero, then only tweak the alpha channel's value;
        if (isBlack)
            alphaChannel += clampedColorComponentOffset(noiseValue, alphaChannel, lowerBoundColor[3], upperBoundColor[3]);
        else {
            // If alpha and any of the color channels are non-zero, then tweak all of the channels;
            redChannel += clampedColorComponentOffset(noiseValue, redChannel, lowerBoundColor[0], upperBoundColor[0]);
            greenChannel += clampedColorComponentOffset(noiseValue, greenChannel, lowerBoundColor[1], upperBoundColor[1]);
            blueChannel += clampedColorComponentOffset(noiseValue, blueChannel, lowerBoundColor[2], upperBoundColor[2]);
            alphaChannel += clampedColorComponentOffset(noiseValue, alphaChannel, lowerBoundColor[3], upperBoundColor[3]);
        }
        wasPixelBufferModified = true;
    }
    return wasPixelBufferModified;
}

} // namespace WebCore