diff options
author | 2011-02-24 22:50:55 +0000 | |
---|---|---|
committer | 2011-02-24 22:50:55 +0000 | |
commit | 112706d4c566e283890322bb246b4b86d59837e1 (patch) | |
tree | 6a1015664d9f4ed7c3bf95060f9e053174aff935 | |
parent | 8ab3e0f07ff5a0a5b9357700fa7961f9a15b7200 (diff) |
Add SkBitmap::getColor(int x, int y) - return the color of a pixel in a bitmap.
Review URL: http://codereview.appspot.com/4230041
git-svn-id: http://skia.googlecode.com/svn/trunk@854 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r-- | include/core/SkBitmap.h | 7 | ||||
-rw-r--r-- | src/core/SkBitmap.cpp | 52 |
2 files changed, 59 insertions, 0 deletions
diff --git a/include/core/SkBitmap.h b/include/core/SkBitmap.h index a38cafa351..56d7478e48 100644 --- a/include/core/SkBitmap.h +++ b/include/core/SkBitmap.h @@ -401,6 +401,13 @@ public: */ void* getAddr(int x, int y) const; + /** Return the SkColor of the specified pixel. In most cases this will + require un-premultiplying the color. Alpha only configs (A1 and A8) + return black with the appropriate alpha set. The value is undefined + for kNone_Config or if x or y are out of bounds. + */ + SkColor getColor(int x, int y) const; + /** Returns the address of the pixel specified by x,y for 32bit pixels. */ inline uint32_t* getAddr32(int x, int y) const; diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp index b302f5b8d9..c1a9998bf1 100644 --- a/src/core/SkBitmap.cpp +++ b/src/core/SkBitmap.cpp @@ -22,6 +22,7 @@ #include "SkMask.h" #include "SkPixelRef.h" #include "SkThread.h" +#include "SkUnPreMultiply.h" #include "SkUtils.h" #include "SkPackBits.h" #include <new> @@ -598,6 +599,57 @@ void* SkBitmap::getAddr(int x, int y) const { return base; } +SkColor SkBitmap::getColor(int x, int y) const { + SkASSERT((unsigned)x < (unsigned)this->width()); + SkASSERT((unsigned)y < (unsigned)this->height()); + + switch (this->config()) { + case SkBitmap::kA1_Config: { + uint8_t* addr = getAddr1(x, y); + uint8_t mask = 1 << (7 - (x % 8)); + if (addr[0] & mask) { + return SK_ColorBLACK; + } else { + return 0; + } + } + case SkBitmap::kA8_Config: { + uint8_t* addr = getAddr8(x, y); + return SkColorSetA(0, addr[0]); + } + case SkBitmap::kIndex8_Config: { + SkPMColor c = getIndex8Color(x, y); + return SkUnPreMultiply::PMColorToColor(c); + } + case SkBitmap::kRGB_565_Config: { + uint16_t* addr = getAddr16(x, y); + return SkPixel16ToColor(addr[0]); + } + case SkBitmap::kARGB_4444_Config: { + uint16_t* addr = getAddr16(x, y); + SkPMColor c = SkPixel4444ToPixel32(addr[0]); + return SkUnPreMultiply::PMColorToColor(c); + } + case SkBitmap::kARGB_8888_Config: { + uint32_t* addr = getAddr32(x, y); + return SkUnPreMultiply::PMColorToColor(addr[0]); + } + case kRLE_Index8_Config: { + uint8_t dst; + const SkBitmap::RLEPixels* rle = + (const SkBitmap::RLEPixels*) getPixels(); + SkPackBits::Unpack8(&dst, x, 1, rle->packedAtY(y)); + return SkUnPreMultiply::PMColorToColor((*fColorTable)[dst]); + } + case kNo_Config: + case kConfigCount: + SkASSERT(false); + return 0; + } + SkASSERT(false); // Not reached. + return 0; +} + /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// |