Last active
May 26, 2017 14:19
-
-
Save HalCanary/821795fe01062c9927af999bd26e58ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2017 Google Inc. | |
* | |
* Use of this source code is governed by a BSD-style license that can be | |
* found in the LICENSE file. | |
*/ | |
#ifndef SkPixelIterator_DEFINED | |
#define SkPixelIterator_DEFINED | |
#include "SkBitmap.h" | |
template <typename T> | |
class SkPixelIterator { | |
public: | |
struct Pixel { | |
SkIPoint pt; | |
const T* addr; | |
}; | |
struct Iter { | |
Pixel operator*() const { return {{fX, fY}, fPtr}; } | |
bool operator!=(const Iter& o) const { return fY != o.fY || fX != o.fX; } | |
void operator++() { | |
++fX; | |
++fPtr; | |
if (fX == fWidth) { | |
++fY; | |
fX = 0; | |
fPtr += fPad; | |
} | |
} | |
int fX, fY, fWidth, fPad; | |
const T* fPtr; | |
}; | |
SkPixelIterator(const SkPixmap& pm) | |
: fAddr((const T*)pm.addr()) | |
, fWidth(pm.width()) | |
, fHeight(pm.height()) | |
, fPad(pm.rowBytesAsPixels() - pm.width()) { | |
SkASSERT(sizeof(T) == SkColorTypeBytesPerPixel(pm.colorType())); | |
} | |
SkPixelIterator(const SkBitmap& bm) : SkPixelIterator(MakePixmap(bm)) {} | |
Iter begin() const { return Iter{0, 0, fWidth, fPad, fAddr}; } | |
Iter end() const { return Iter{0, fHeight, fWidth, fPad, fAddr}; } | |
private: | |
const T* fAddr; | |
int fWidth, fHeight, fPad; | |
static SkPixmap MakePixmap(const SkBitmap& bm) { | |
SkPixmap pm; | |
bm.peekPixels(&pm); | |
return pm; | |
} | |
}; | |
using SkPixelIterator32 = SkPixelIterator<uint32_t>; | |
using SkPixelIterator16 = SkPixelIterator<uint16_t>; | |
using SkPixelIterator8 = SkPixelIterator<uint8_t>; | |
#endif // SkPixelIterator_DEFINED | |
///////////////////////////////////////////////////////////////////////////////////////////// | |
#include "SkPixelIterator.h" | |
#include "Test.h" | |
DEF_TEST(Bitmap_iterator, r) { | |
SkBitmap bigBitmap; | |
bigBitmap.allocN32Pixels(400, 400); | |
SkBitmap bitmap; | |
bigBitmap.extractSubset(&bitmap, {15, 25, 19, 35}); | |
for (int y = 0; y < bitmap.height(); ++y) { | |
for (int x = 0; x < bitmap.width(); ++x) { | |
*bitmap.getAddr32(x,y) = (uint32_t) (x * 0x100 + y); | |
} | |
} | |
for (SkPixelIterator32::Pixel pixel: SkPixelIterator32(bitmap)) { | |
if (0 == pixel.pt.x()) { SkDebugf("\n"); } | |
SkDebugf("%d %d 0x%08X ", pixel.pt.x(), pixel.pt.y(), *pixel.addr); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment