Home | History | Annotate | Download | only in core
      1 
      2 /*
      3  * Copyright 2012 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 
     10 #ifndef SkColorTable_DEFINED
     11 #define SkColorTable_DEFINED
     12 
     13 #include "../private/SkOncePtr.h"
     14 #include "SkColor.h"
     15 #include "SkFlattenable.h"
     16 #include "SkImageInfo.h"
     17 
     18 /** \class SkColorTable
     19 
     20     SkColorTable holds an array SkPMColors (premultiplied 32-bit colors) used by
     21     8-bit bitmaps, where the bitmap bytes are interpreted as indices into the colortable.
     22 
     23     SkColorTable is thread-safe.
     24 */
     25 class SK_API SkColorTable : public SkRefCnt {
     26 public:
     27     /** Copy up to 256 colors into a new SkColorTable.
     28      */
     29     SkColorTable(const SkPMColor colors[], int count);
     30     virtual ~SkColorTable();
     31 
     32     /** Returns the number of colors in the table.
     33      */
     34     int count() const { return fCount; }
     35 
     36     /** Returns the specified color from the table. In the debug build, this asserts that
     37      *  the index is in range (0 <= index < count).
     38      */
     39     SkPMColor operator[](int index) const {
     40         SkASSERT(fColors != NULL && (unsigned)index < (unsigned)fCount);
     41         return fColors[index];
     42     }
     43 
     44     /** Return the array of colors for reading.
     45      */
     46     const SkPMColor* readColors() const { return fColors; }
     47 
     48     /** read16BitCache() returns the array of RGB16 colors that mirror the 32bit colors.
     49      */
     50     const uint16_t* read16BitCache() const;
     51 
     52     void writeToBuffer(SkWriteBuffer&) const;
     53 
     54     // may return null
     55     static SkColorTable* Create(SkReadBuffer&);
     56 
     57 private:
     58     enum AllocatedWithMalloc {
     59         kAllocatedWithMalloc
     60     };
     61     // assumes ownership of colors (assumes it was allocated w/ malloc)
     62     SkColorTable(SkPMColor* colors, int count, AllocatedWithMalloc);
     63 
     64     SkPMColor*            fColors;
     65     SkOncePtr<uint16_t[]> f16BitCache;
     66     int                   fCount;
     67 
     68     void init(const SkPMColor* colors, int count);
     69 
     70     friend class SkImageGenerator;
     71     // Only call if no other thread or cache has seen this table.
     72     void dangerous_overwriteColors(const SkPMColor newColors[], int count) {
     73         if (count < 0 || count > fCount) {
     74             sk_throw();
     75         }
     76         // assumes that f16BitCache nas NOT been initialized yet, so we don't try to update it
     77         memcpy(fColors, newColors, count * sizeof(SkPMColor));
     78         fCount = count; // update fCount, in case count is smaller
     79     }
     80 
     81     typedef SkRefCnt INHERITED;
     82 };
     83 
     84 #endif
     85