aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/sk_pixel_iter.h
blob: 8bf5a55647fd48cb193bfeefab0d1e3cd61c6007 (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
/*
 * Copyright 2018 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef sk_pixel_iter_DEFINED
#define sk_pixel_iter_DEFINED

#include "SkPixmap.h"
#include "SkSurface.h"

namespace sk_tool_utils {

    class PixelIter {
    public:
        PixelIter();
        PixelIter(SkSurface* surf) {
            SkPixmap pm;
            if (!surf->peekPixels(&pm)) {
                pm.reset();
            }
            this->reset(pm);
        }

        void reset(const SkPixmap& pm) {
            fPM = pm;
            fLoc = { -1, 0 };
        }

        void* next(SkIPoint* loc = nullptr) {
            if (!fPM.addr()) {
                return nullptr;
            }
            fLoc.fX += 1;
            if (fLoc.fX >= fPM.width()) {
                fLoc.fX = 0;
                if (++fLoc.fY >= fPM.height()) {
                    this->setDone();
                    return nullptr;
                }
            }
            if (loc) {
                *loc = fLoc;
            }
            return fPM.writable_addr(fLoc.fX, fLoc.fY);
        }

        void setDone() {
            fPM.reset();
        }

    private:
        SkPixmap    fPM;
        SkIPoint    fLoc;
    };

}  // namespace sk_tool_utils

#endif  // sk_tool_utils_DEFINED