1 /*
2  * Copyright 2018 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef sk_pixel_iter_DEFINED
9 #define sk_pixel_iter_DEFINED
10 
11 #include "SkPixmap.h"
12 #include "SkSurface.h"
13 
14 namespace sk_tool_utils {
15 
16     class PixelIter {
17     public:
18         PixelIter();
PixelIter(SkSurface * surf)19         PixelIter(SkSurface* surf) {
20             SkPixmap pm;
21             if (!surf->peekPixels(&pm)) {
22                 pm.reset();
23             }
24             this->reset(pm);
25         }
26 
reset(const SkPixmap & pm)27         void reset(const SkPixmap& pm) {
28             fPM = pm;
29             fLoc = { -1, 0 };
30         }
31 
32         void* next(SkIPoint* loc = nullptr) {
33             if (!fPM.addr()) {
34                 return nullptr;
35             }
36             fLoc.fX += 1;
37             if (fLoc.fX >= fPM.width()) {
38                 fLoc.fX = 0;
39                 if (++fLoc.fY >= fPM.height()) {
40                     this->setDone();
41                     return nullptr;
42                 }
43             }
44             if (loc) {
45                 *loc = fLoc;
46             }
47             return fPM.writable_addr(fLoc.fX, fLoc.fY);
48         }
49 
setDone()50         void setDone() {
51             fPM.reset();
52         }
53 
54     private:
55         SkPixmap    fPM;
56         SkIPoint    fLoc;
57     };
58 
59 }  // namespace sk_tool_utils
60 
61 #endif  // sk_tool_utils_DEFINED
62