Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef SkBitmap_DEFINED
     18 #define SkBitmap_DEFINED
     19 
     20 #include "Sk64.h"
     21 #include "SkColor.h"
     22 #include "SkPoint.h"
     23 #include "SkRefCnt.h"
     24 
     25 struct SkIRect;
     26 class SkColorTable;
     27 class SkPaint;
     28 class SkPixelRef;
     29 class SkRegion;
     30 class SkFlattenableReadBuffer;
     31 class SkFlattenableWriteBuffer;
     32 
     33 /** \class SkBitmap
     34 
     35     The SkBitmap class specifies a raster bitmap. A bitmap has an integer width
     36     and height, and a format (config), and a pointer to the actual pixels.
     37     Bitmaps can be drawn into a SkCanvas, but they are also used to specify the target
     38     of a SkCanvas' drawing operations.
     39 */
     40 class SkBitmap {
     41 public:
     42     class Allocator;
     43 
     44     enum Config {
     45         kNo_Config,         //!< bitmap has not been configured
     46         kA1_Config,         //!< 1-bit per pixel, (0 is transparent, 1 is opaque)
     47         kA8_Config,         //!< 8-bits per pixel, with only alpha specified (0 is transparent, 0xFF is opaque)
     48         kIndex8_Config,     //!< 8-bits per pixel, using SkColorTable to specify the colors
     49         kRGB_565_Config,    //!< 16-bits per pixel, (see SkColorPriv.h for packing)
     50         kARGB_4444_Config,  //!< 16-bits per pixel, (see SkColorPriv.h for packing)
     51         kARGB_8888_Config,  //!< 32-bits per pixel, (see SkColorPriv.h for packing)
     52         kRLE_Index8_Config,
     53 
     54         kConfigCount
     55     };
     56 
     57     /** Default construct creates a bitmap with zero width and height, and no pixels.
     58         Its config is set to kNo_Config.
     59     */
     60     SkBitmap();
     61     /** Constructor initializes the new bitmap by copying the src bitmap. All fields are copied,
     62         but ownership of the pixels remains with the src bitmap.
     63     */
     64     SkBitmap(const SkBitmap& src);
     65     /** Decrements our (shared) pixel ownership if needed.
     66     */
     67     ~SkBitmap();
     68 
     69     /** Copies the src bitmap into this bitmap. Ownership of the src bitmap's pixels remains
     70         with the src bitmap.
     71     */
     72     SkBitmap& operator=(const SkBitmap& src);
     73     /** Swap the fields of the two bitmaps. This routine is guaranteed to never fail or throw.
     74     */
     75     //  This method is not exported to java.
     76     void swap(SkBitmap& other);
     77 
     78     /** Return true iff the bitmap has empty dimensions.
     79     */
     80     bool empty() const { return 0 == fWidth || 0 == fHeight; }
     81 
     82     /** Return true iff the bitmap has no pixels nor a pixelref. Note: this can
     83         return true even if the dimensions of the bitmap are > 0 (see empty()).
     84     */
     85     bool isNull() const { return NULL == fPixels && NULL == fPixelRef; }
     86 
     87     /** Return the config for the bitmap.
     88     */
     89     Config  config() const { return (Config)fConfig; }
     90     /** DEPRECATED, use config()
     91     */
     92     Config  getConfig() const { return this->config(); }
     93     /** Return the bitmap's width, in pixels.
     94     */
     95     int width() const { return fWidth; }
     96     /** Return the bitmap's height, in pixels.
     97     */
     98     int height() const { return fHeight; }
     99     /** Return the number of bytes between subsequent rows of the bitmap.
    100     */
    101     int rowBytes() const { return fRowBytes; }
    102 
    103     /** Return the shift amount per pixel (i.e. 0 for 1-byte per pixel, 1 for
    104         2-bytes per pixel configs, 2 for 4-bytes per pixel configs). Return 0
    105         for configs that are not at least 1-byte per pixel (e.g. kA1_Config
    106         or kNo_Config)
    107     */
    108     int shiftPerPixel() const { return fBytesPerPixel >> 1; }
    109 
    110     /** Return the number of bytes per pixel based on the config. If the config
    111         does not have at least 1 byte per (e.g. kA1_Config) then 0 is returned.
    112     */
    113     int bytesPerPixel() const { return fBytesPerPixel; }
    114 
    115     /** Return the rowbytes expressed as a number of pixels (like width and
    116         height). Note, for 1-byte per pixel configs like kA8_Config, this will
    117         return the same as rowBytes(). Is undefined for configs that are less
    118         than 1-byte per pixel (e.g. kA1_Config)
    119     */
    120     int rowBytesAsPixels() const { return fRowBytes >> (fBytesPerPixel >> 1); }
    121 
    122     /** Return the address of the pixels for this SkBitmap.
    123     */
    124     void* getPixels() const { return fPixels; }
    125 
    126     /** Return the byte size of the pixels, based on the height and rowBytes.
    127         Note this truncates the result to 32bits. Call getSize64() to detect
    128         if the real size exceeds 32bits.
    129     */
    130     size_t getSize() const { return fHeight * fRowBytes; }
    131 
    132     /** Return the byte size of the pixels, based on the height and rowBytes.
    133         This routine is slightly slower than getSize(), but does not truncate
    134         the answer to 32bits.
    135     */
    136     Sk64 getSize64() const {
    137         Sk64 size;
    138         size.setMul(fHeight, fRowBytes);
    139         return size;
    140     }
    141 
    142     /** Returns true if the bitmap is opaque (has no translucent/transparent pixels).
    143     */
    144     bool isOpaque() const;
    145     /** Specify if this bitmap's pixels are all opaque or not. Is only meaningful for configs
    146         that support per-pixel alpha (RGB32, A1, A8).
    147     */
    148     void setIsOpaque(bool);
    149 
    150     /** Reset the bitmap to its initial state (see default constructor). If we are a (shared)
    151         owner of the pixels, that ownership is decremented.
    152     */
    153     void reset();
    154 
    155     /** Given a config and a width, this computes the optimal rowBytes value. This is called automatically
    156         if you pass 0 for rowBytes to setConfig().
    157     */
    158     static int ComputeRowBytes(Config c, int width);
    159 
    160     /** Return the bytes-per-pixel for the specified config. If the config is
    161         not at least 1-byte per pixel, return 0, including for kNo_Config.
    162     */
    163     static int ComputeBytesPerPixel(Config c);
    164 
    165     /** Return the shift-per-pixel for the specified config. If the config is
    166      not at least 1-byte per pixel, return 0, including for kNo_Config.
    167      */
    168     static int ComputeShiftPerPixel(Config c) {
    169         return ComputeBytesPerPixel(c) >> 1;
    170     }
    171 
    172     static Sk64 ComputeSize64(Config, int width, int height);
    173     static size_t ComputeSize(Config, int width, int height);
    174 
    175     /** Set the bitmap's config and dimensions. If rowBytes is 0, then
    176         ComputeRowBytes() is called to compute the optimal value. This resets
    177         any pixel/colortable ownership, just like reset().
    178     */
    179     void setConfig(Config, int width, int height, int rowBytes = 0);
    180     /** Use this to assign a new pixel address for an existing bitmap. This
    181         will automatically release any pixelref previously installed. Only call
    182         this if you are handling ownership/lifetime of the pixel memory.
    183 
    184         If the bitmap retains a reference to the colortable (assuming it is
    185         not null) it will take care of incrementing the reference count.
    186 
    187         @param pixels   Address for the pixels, managed by the caller.
    188         @param ctable   ColorTable (or null) that matches the specified pixels
    189     */
    190     void setPixels(void* p, SkColorTable* ctable = NULL);
    191 
    192     /** Use the standard HeapAllocator to create the pixelref that manages the
    193         pixel memory. It will be sized based on the current width/height/config.
    194         If this is called multiple times, a new pixelref object will be created
    195         each time.
    196 
    197         If the bitmap retains a reference to the colortable (assuming it is
    198         not null) it will take care of incrementing the reference count.
    199 
    200         @param ctable   ColorTable (or null) to use with the pixels that will
    201                         be allocated. Only used if config == Index8_Config
    202         @return true if the allocation succeeds. If not the pixelref field of
    203                      the bitmap will be unchanged.
    204     */
    205     bool allocPixels(SkColorTable* ctable = NULL) {
    206         return this->allocPixels(NULL, ctable);
    207     }
    208 
    209     /** Use the specified Allocator to create the pixelref that manages the
    210         pixel memory. It will be sized based on the current width/height/config.
    211         If this is called multiple times, a new pixelref object will be created
    212         each time.
    213 
    214         If the bitmap retains a reference to the colortable (assuming it is
    215         not null) it will take care of incrementing the reference count.
    216 
    217         @param allocator The Allocator to use to create a pixelref that can
    218                          manage the pixel memory for the current
    219                          width/height/config. If allocator is NULL, the standard
    220                          HeapAllocator will be used.
    221         @param ctable   ColorTable (or null) to use with the pixels that will
    222                         be allocated. Only used if config == Index8_Config.
    223                         If it is non-null and the config is not Index8, it will
    224                         be ignored.
    225         @return true if the allocation succeeds. If not the pixelref field of
    226                      the bitmap will be unchanged.
    227     */
    228     bool allocPixels(Allocator* allocator, SkColorTable* ctable);
    229 
    230     /** Return the current pixelref object, of any
    231     */
    232     SkPixelRef* pixelRef() const { return fPixelRef; }
    233     /** Return the offset into the pixelref, if any. Will return 0 if there is
    234         no pixelref installed.
    235     */
    236     size_t pixelRefOffset() const { return fPixelRefOffset; }
    237     /** Assign a pixelref and optional offset. Pixelrefs are reference counted,
    238         so the existing one (if any) will be unref'd and the new one will be
    239         ref'd.
    240     */
    241     SkPixelRef* setPixelRef(SkPixelRef* pr, size_t offset = 0);
    242 
    243     /** Call this to ensure that the bitmap points to the current pixel address
    244         in the pixelref. Balance it with a call to unlockPixels(). These calls
    245         are harmless if there is no pixelref.
    246     */
    247     void lockPixels() const;
    248     /** When you are finished access the pixel memory, call this to balance a
    249         previous call to lockPixels(). This allows pixelrefs that implement
    250         cached/deferred image decoding to know when there are active clients of
    251         a given image.
    252     */
    253     void unlockPixels() const;
    254 
    255     /** Call this to be sure that the bitmap is valid enough to be drawn (i.e.
    256         it has non-null pixels, and if required by its config, it has a
    257         non-null colortable. Returns true if all of the above are met.
    258     */
    259     bool readyToDraw() const {
    260         return this->getPixels() != NULL &&
    261                ((this->config() != kIndex8_Config && this->config() != kRLE_Index8_Config) ||
    262                        fColorTable != NULL);
    263     }
    264 
    265     /** Return the bitmap's colortable (if any). Does not affect the colortable's
    266         reference count.
    267     */
    268     SkColorTable* getColorTable() const { return fColorTable; }
    269 
    270     /** Returns a non-zero, unique value corresponding to the pixels in our
    271         pixelref, or 0 if we do not have a pixelref. Each time the pixels are
    272         changed (and notifyPixelsChanged is called), a different generation ID
    273         will be returned.
    274     */
    275     uint32_t getGenerationID() const;
    276 
    277     /** Call this if you have changed the contents of the pixels. This will in-
    278         turn cause a different generation ID value to be returned from
    279         getGenerationID().
    280     */
    281     void notifyPixelsChanged() const;
    282 
    283     /** Initialize the bitmap's pixels with the specified color+alpha, automatically converting into the correct format
    284         for the bitmap's config. If the config is kRGB_565_Config, then the alpha value is ignored.
    285         If the config is kA8_Config, then the r,g,b parameters are ignored.
    286     */
    287     void eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b) const;
    288     /** Initialize the bitmap's pixels with the specified color+alpha, automatically converting into the correct format
    289         for the bitmap's config. If the config is kRGB_565_Config, then the alpha value is presumed
    290         to be 0xFF. If the config is kA8_Config, then the r,g,b parameters are ignored and the
    291         pixels are all set to 0xFF.
    292     */
    293     void eraseRGB(U8CPU r, U8CPU g, U8CPU b) const {
    294         this->eraseARGB(0xFF, r, g, b);
    295     }
    296     /** Initialize the bitmap's pixels with the specified color, automatically converting into the correct format
    297         for the bitmap's config. If the config is kRGB_565_Config, then the color's alpha value is presumed
    298         to be 0xFF. If the config is kA8_Config, then only the color's alpha value is used.
    299     */
    300     void eraseColor(SkColor c) const {
    301         this->eraseARGB(SkColorGetA(c), SkColorGetR(c), SkColorGetG(c),
    302                         SkColorGetB(c));
    303     }
    304 
    305     /** Scroll (a subset of) the contents of this bitmap by dx/dy. If there are
    306         no pixels allocated (i.e. getPixels() returns null) the method will
    307         still update the inval region (if present).
    308 
    309         @param subset The subset of the bitmap to scroll/move. To scroll the
    310                       entire contents, specify [0, 0, width, height] or just
    311                       pass null.
    312         @param dx The amount to scroll in X
    313         @param dy The amount to scroll in Y
    314         @param inval Optional (may be null). Returns the area of the bitmap that
    315                      was scrolled away. E.g. if dx = dy = 0, then inval would
    316                      be set to empty. If dx >= width or dy >= height, then
    317                      inval would be set to the entire bounds of the bitmap.
    318         @return true if the scroll was doable. Will return false if the bitmap
    319                      uses an unsupported config for scrolling (only kA8,
    320                      kIndex8, kRGB_565, kARGB_4444, kARGB_8888 are supported).
    321                      If no pixels are present (i.e. getPixels() returns false)
    322                      inval will still be updated, and true will be returned.
    323     */
    324     bool scrollRect(const SkIRect* subset, int dx, int dy,
    325                     SkRegion* inval = NULL) const;
    326 
    327     /** Returns the address of the specified pixel. This performs a runtime
    328         check to know the size of the pixels, and will return the same answer
    329         as the corresponding size-specific method (e.g. getAddr16). Since the
    330         check happens at runtime, it is much slower than using a size-specific
    331         version. Unlike the size-specific methods, this routine also checks if
    332         getPixels() returns null, and returns that. The size-specific routines
    333         perform a debugging assert that getPixels() is not null, but they do
    334         not do any runtime checks.
    335     */
    336     void* getAddr(int x, int y) const;
    337 
    338     /** Returns the address of the pixel specified by x,y for 32bit pixels.
    339     */
    340     inline uint32_t* getAddr32(int x, int y) const;
    341     /** Returns the address of the pixel specified by x,y for 16bit pixels.
    342     */
    343     inline uint16_t* getAddr16(int x, int y) const;
    344     /** Returns the address of the pixel specified by x,y for 8bit pixels.
    345     */
    346     inline uint8_t* getAddr8(int x, int y) const;
    347     /** Returns the address of the byte containing the pixel specified by x,y
    348         for 1bit pixels.
    349         */
    350     inline uint8_t* getAddr1(int x, int y) const;
    351 
    352     /** Returns the color corresponding to the pixel specified by x,y for
    353         colortable based bitmaps.
    354     */
    355     inline SkPMColor getIndex8Color(int x, int y) const;
    356 
    357     /** Set dst to be a setset of this bitmap. If possible, it will share the
    358         pixel memory, and just point into a subset of it. However, if the config
    359         does not support this, a local copy will be made and associated with
    360         the dst bitmap. If the subset rectangle, intersected with the bitmap's
    361         dimensions is empty, or if there is an unsupported config, false will be
    362         returned and dst will be untouched.
    363         @param dst  The bitmap that will be set to a subset of this bitmap
    364         @param subset The rectangle of pixels in this bitmap that dst will
    365                       reference.
    366         @return true if the subset copy was successfully made.
    367     */
    368     bool extractSubset(SkBitmap* dst, const SkIRect& subset) const;
    369 
    370     /** Makes a deep copy of this bitmap, respecting the requested config.
    371         Returns false if either there is an error (i.e. the src does not have
    372         pixels) or the request cannot be satisfied (e.g. the src has per-pixel
    373         alpha, and the requested config does not support alpha).
    374         @param dst The bitmap to be sized and allocated
    375         @param c The desired config for dst
    376         @param allocator Allocator used to allocate the pixelref for the dst
    377                          bitmap. If this is null, the standard HeapAllocator
    378                          will be used.
    379         @return true if the copy could be made.
    380     */
    381     bool copyTo(SkBitmap* dst, Config c, Allocator* allocator = NULL) const;
    382 
    383     /** Returns true if this bitmap can be deep copied into the requested config
    384         by calling copyTo().
    385      */
    386     bool canCopyTo(Config newConfig) const;
    387 
    388     bool hasMipMap() const;
    389     void buildMipMap(bool forceRebuild = false);
    390     void freeMipMap();
    391 
    392     /** Given scale factors sx, sy, determine the miplevel available in the
    393         bitmap, and return it (this is the amount to shift matrix iterators
    394         by). If dst is not null, it is set to the correct level.
    395     */
    396     int extractMipLevel(SkBitmap* dst, SkFixed sx, SkFixed sy);
    397 
    398     void extractAlpha(SkBitmap* dst) const {
    399         this->extractAlpha(dst, NULL, NULL);
    400     }
    401 
    402     void extractAlpha(SkBitmap* dst, const SkPaint* paint,
    403                       SkIPoint* offset) const;
    404 
    405     void flatten(SkFlattenableWriteBuffer&) const;
    406     void unflatten(SkFlattenableReadBuffer&);
    407 
    408     SkDEBUGCODE(void validate() const;)
    409 
    410     class Allocator : public SkRefCnt {
    411     public:
    412         /** Allocate the pixel memory for the bitmap, given its dimensions and
    413             config. Return true on success, where success means either setPixels
    414             or setPixelRef was called. The pixels need not be locked when this
    415             returns. If the config requires a colortable, it also must be
    416             installed via setColorTable. If false is returned, the bitmap and
    417             colortable should be left unchanged.
    418         */
    419         virtual bool allocPixelRef(SkBitmap*, SkColorTable*) = 0;
    420     };
    421 
    422     /** Subclass of Allocator that returns a pixelref that allocates its pixel
    423         memory from the heap. This is the default Allocator invoked by
    424         allocPixels().
    425     */
    426     class HeapAllocator : public Allocator {
    427     public:
    428         virtual bool allocPixelRef(SkBitmap*, SkColorTable*);
    429     };
    430 
    431     class RLEPixels {
    432     public:
    433         RLEPixels(int width, int height);
    434         virtual ~RLEPixels();
    435 
    436         uint8_t* packedAtY(int y) const {
    437             SkASSERT((unsigned)y < (unsigned)fHeight);
    438             return fYPtrs[y];
    439         }
    440 
    441         // called by subclasses during creation
    442         void setPackedAtY(int y, uint8_t* addr) {
    443             SkASSERT((unsigned)y < (unsigned)fHeight);
    444             fYPtrs[y] = addr;
    445         }
    446 
    447     private:
    448         uint8_t** fYPtrs;
    449         int       fHeight;
    450     };
    451 
    452 private:
    453     struct MipMap;
    454     mutable MipMap* fMipMap;
    455 
    456     mutable SkPixelRef* fPixelRef;
    457     mutable size_t      fPixelRefOffset;
    458     mutable int         fPixelLockCount;
    459     // either user-specified (in which case it is not treated as mutable)
    460     // or a cache of the returned value from fPixelRef->lockPixels()
    461     mutable void*       fPixels;
    462     mutable SkColorTable* fColorTable;    // only meaningful for kIndex8
    463 
    464     enum Flags {
    465         kImageIsOpaque_Flag  = 0x01
    466     };
    467 
    468     uint32_t    fRowBytes;
    469     uint32_t    fWidth;
    470     uint32_t    fHeight;
    471     uint8_t     fConfig;
    472     uint8_t     fFlags;
    473     uint8_t     fBytesPerPixel; // based on config
    474 
    475     /*  Unreference any pixelrefs or colortables
    476     */
    477     void freePixels();
    478     void updatePixelsFromRef() const;
    479 
    480     static SkFixed ComputeMipLevel(SkFixed sx, SkFixed dy);
    481 };
    482 
    483 /** \class SkColorTable
    484 
    485     SkColorTable holds an array SkPMColors (premultiplied 32-bit colors) used by
    486     8-bit bitmaps, where the bitmap bytes are interpreted as indices into the colortable.
    487 */
    488 class SkColorTable : public SkRefCnt {
    489 public:
    490     /** Makes a deep copy of colors.
    491      */
    492     SkColorTable(const SkColorTable& src);
    493     /** Preallocates the colortable to have 'count' colors, which
    494      *  are initially set to 0.
    495     */
    496     explicit SkColorTable(int count);
    497     explicit SkColorTable(SkFlattenableReadBuffer&);
    498     SkColorTable(const SkPMColor colors[], int count);
    499     virtual ~SkColorTable();
    500 
    501     enum Flags {
    502         kColorsAreOpaque_Flag   = 0x01  //!< if set, all of the colors in the table are opaque (alpha==0xFF)
    503     };
    504     /** Returns the flag bits for the color table. These can be changed with setFlags().
    505     */
    506     unsigned getFlags() const { return fFlags; }
    507     /** Set the flags for the color table. See the Flags enum for possible values.
    508     */
    509     void    setFlags(unsigned flags);
    510 
    511     bool isOpaque() const { return (fFlags & kColorsAreOpaque_Flag) != 0; }
    512     void setIsOpaque(bool isOpaque);
    513 
    514     /** Returns the number of colors in the table.
    515     */
    516     int count() const { return fCount; }
    517 
    518     /** Returns the specified color from the table. In the debug build, this asserts that
    519         the index is in range (0 <= index < count).
    520     */
    521     SkPMColor operator[](int index) const {
    522         SkASSERT(fColors != NULL && (unsigned)index < fCount);
    523         return fColors[index];
    524     }
    525 
    526     /** Specify the number of colors in the color table. This does not initialize the colors
    527         to any value, just allocates memory for them. To initialize the values, either call
    528         setColors(array, count), or follow setCount(count) with a call to
    529         lockColors()/{set the values}/unlockColors(true).
    530     */
    531 //    void    setColors(int count) { this->setColors(NULL, count); }
    532 //    void    setColors(const SkPMColor[], int count);
    533 
    534     /** Return the array of colors for reading and/or writing. This must be
    535         balanced by a call to unlockColors(changed?), telling the colortable if
    536         the colors were changed during the lock.
    537     */
    538     SkPMColor* lockColors() {
    539         SkDEBUGCODE(fColorLockCount += 1;)
    540         return fColors;
    541     }
    542     /** Balancing call to lockColors(). If the colors have been changed, pass true.
    543     */
    544     void unlockColors(bool changed);
    545 
    546     /** Similar to lockColors(), lock16BitCache() returns the array of
    547         RGB16 colors that mirror the 32bit colors. However, this function
    548         will return null if kColorsAreOpaque_Flag is not set.
    549         Also, unlike lockColors(), the returned array here cannot be modified.
    550     */
    551     const uint16_t* lock16BitCache();
    552     /** Balancing call to lock16BitCache().
    553     */
    554     void unlock16BitCache() {
    555         SkASSERT(f16BitCacheLockCount > 0);
    556         SkDEBUGCODE(f16BitCacheLockCount -= 1);
    557     }
    558 
    559     void flatten(SkFlattenableWriteBuffer&) const;
    560 
    561 private:
    562     SkPMColor*  fColors;
    563     uint16_t*   f16BitCache;
    564     uint16_t    fCount;
    565     uint8_t     fFlags;
    566     SkDEBUGCODE(int fColorLockCount;)
    567     SkDEBUGCODE(int f16BitCacheLockCount;)
    568 
    569     void inval16BitCache();
    570 };
    571 
    572 class SkAutoLockPixels {
    573 public:
    574     SkAutoLockPixels(const SkBitmap& bitmap) : fBitmap(bitmap) {
    575         bitmap.lockPixels();
    576     }
    577     ~SkAutoLockPixels() {
    578         fBitmap.unlockPixels();
    579     }
    580 
    581 private:
    582     const SkBitmap& fBitmap;
    583 };
    584 
    585 /** Helper class that performs the lock/unlockColors calls on a colortable.
    586     The destructor will call unlockColors(false) if it has a bitmap's colortable
    587 */
    588 class SkAutoLockColors : public SkNoncopyable {
    589 public:
    590     /** Initialize with no bitmap. Call lockColors(bitmap) to lock bitmap's
    591         colortable
    592      */
    593     SkAutoLockColors() : fCTable(NULL), fColors(NULL) {}
    594     /** Initialize with bitmap, locking its colortable if present
    595      */
    596     explicit SkAutoLockColors(const SkBitmap& bm) {
    597         fCTable = bm.getColorTable();
    598         fColors = fCTable ? fCTable->lockColors() : NULL;
    599     }
    600     /** Initialize with a colortable (may be null)
    601      */
    602     explicit SkAutoLockColors(SkColorTable* ctable) {
    603         fCTable = ctable;
    604         fColors = ctable ? ctable->lockColors() : NULL;
    605     }
    606     ~SkAutoLockColors() {
    607         if (fCTable) {
    608             fCTable->unlockColors(false);
    609         }
    610     }
    611 
    612     /** Return the currently locked colors, or NULL if no bitmap's colortable
    613         is currently locked.
    614     */
    615     const SkPMColor* colors() const { return fColors; }
    616 
    617     /** Locks the table and returns is colors (assuming ctable is not null) and
    618         unlocks the previous table if one was present
    619      */
    620     const SkPMColor* lockColors(SkColorTable* ctable) {
    621         if (fCTable) {
    622             fCTable->unlockColors(false);
    623         }
    624         fCTable = ctable;
    625         fColors = ctable ? ctable->lockColors() : NULL;
    626         return fColors;
    627     }
    628 
    629     const SkPMColor* lockColors(const SkBitmap& bm) {
    630         return this->lockColors(bm.getColorTable());
    631     }
    632 
    633 private:
    634     SkColorTable*    fCTable;
    635     const SkPMColor* fColors;
    636 };
    637 
    638 ///////////////////////////////////////////////////////////////////////////////
    639 
    640 inline uint32_t* SkBitmap::getAddr32(int x, int y) const {
    641     SkASSERT(fPixels);
    642     SkASSERT(fConfig == kARGB_8888_Config);
    643     SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight);
    644     return (uint32_t*)((char*)fPixels + y * fRowBytes + (x << 2));
    645 }
    646 
    647 inline uint16_t* SkBitmap::getAddr16(int x, int y) const {
    648     SkASSERT(fPixels);
    649     SkASSERT(fConfig == kRGB_565_Config || fConfig == kARGB_4444_Config);
    650     SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight);
    651     return (uint16_t*)((char*)fPixels + y * fRowBytes + (x << 1));
    652 }
    653 
    654 inline uint8_t* SkBitmap::getAddr8(int x, int y) const {
    655     SkASSERT(fPixels);
    656     SkASSERT(fConfig == kA8_Config || fConfig == kIndex8_Config);
    657     SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight);
    658     return (uint8_t*)fPixels + y * fRowBytes + x;
    659 }
    660 
    661 inline SkPMColor SkBitmap::getIndex8Color(int x, int y) const {
    662     SkASSERT(fPixels);
    663     SkASSERT(fConfig == kIndex8_Config);
    664     SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight);
    665     SkASSERT(fColorTable);
    666     return (*fColorTable)[*((const uint8_t*)fPixels + y * fRowBytes + x)];
    667 }
    668 
    669 // returns the address of the byte that contains the x coordinate
    670 inline uint8_t* SkBitmap::getAddr1(int x, int y) const {
    671     SkASSERT(fPixels);
    672     SkASSERT(fConfig == kA1_Config);
    673     SkASSERT((unsigned)x < fWidth && (unsigned)y < fHeight);
    674     return (uint8_t*)fPixels + y * fRowBytes + (x >> 3);
    675 }
    676 
    677 #endif
    678 
    679