Add SkBitmap::getColor(int x, int y) - return the color of a pixel in a bitmap.
authorvandebo@chromium.org <vandebo@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Thu, 24 Feb 2011 22:50:55 +0000 (22:50 +0000)
committervandebo@chromium.org <vandebo@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Thu, 24 Feb 2011 22:50:55 +0000 (22:50 +0000)
Review URL: http://codereview.appspot.com/4230041

git-svn-id: http://skia.googlecode.com/svn/trunk@854 2bbb7eff-a529-9590-31e7-b0007b416f81

include/core/SkBitmap.h
src/core/SkBitmap.cpp

index a38cafa..56d7478 100644 (file)
@@ -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;
index b302f5b..c1a9998 100644 (file)
@@ -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;
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////