Home | History | Annotate | Download | only in effects
      1 
      2 /*
      3  * Copyright 2011 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 #include "SkTableMaskFilter.h"
     11 
     12 SkTableMaskFilter::SkTableMaskFilter() {
     13     for (int i = 0; i < 256; i++) {
     14         fTable[i] = i;
     15     }
     16 }
     17 
     18 SkTableMaskFilter::SkTableMaskFilter(const uint8_t table[256]) {
     19     this->setTable(table);
     20 }
     21 
     22 SkTableMaskFilter::~SkTableMaskFilter() {}
     23 
     24 void SkTableMaskFilter::setTable(const uint8_t table[256]) {
     25     memcpy(fTable, table, 256);
     26 }
     27 
     28 bool SkTableMaskFilter::filterMask(SkMask* dst, const SkMask& src,
     29                                  const SkMatrix&, SkIPoint* margin) {
     30     if (src.fFormat != SkMask::kA8_Format) {
     31         return false;
     32     }
     33 
     34     dst->fBounds = src.fBounds;
     35     dst->fRowBytes = SkAlign4(dst->fBounds.width());
     36     dst->fFormat = SkMask::kA8_Format;
     37     dst->fImage = NULL;
     38 
     39     if (src.fImage) {
     40         dst->fImage = SkMask::AllocImage(dst->computeImageSize());
     41 
     42         const uint8_t* srcP = src.fImage;
     43         uint8_t* dstP = dst->fImage;
     44         const uint8_t* table = fTable;
     45         int dstWidth = dst->fBounds.width();
     46         int extraZeros = dst->fRowBytes - dstWidth;
     47 
     48         for (int y = dst->fBounds.height() - 1; y >= 0; --y) {
     49             for (int x = dstWidth - 1; x >= 0; --x) {
     50                 dstP[x] = table[srcP[x]];
     51             }
     52             srcP += src.fRowBytes;
     53             // we can't just inc dstP by rowbytes, because if it has any
     54             // padding between its width and its rowbytes, we need to zero those
     55             // so that the bitters can read those safely if that is faster for
     56             // them
     57             dstP += dstWidth;
     58             for (int i = extraZeros - 1; i >= 0; --i) {
     59                 *dstP++ = 0;
     60             }
     61         }
     62     }
     63 
     64     if (margin) {
     65         margin->set(0, 0);
     66     }
     67     return true;
     68 }
     69 
     70 SkMask::Format SkTableMaskFilter::getFormat() {
     71     return SkMask::kA8_Format;
     72 }
     73 
     74 void SkTableMaskFilter::flatten(SkFlattenableWriteBuffer& wb) {
     75     this->INHERITED::flatten(wb);
     76     wb.writePad(fTable, 256);
     77 }
     78 
     79 SkTableMaskFilter::SkTableMaskFilter(SkFlattenableReadBuffer& rb)
     80         : INHERITED(rb) {
     81     rb.read(fTable, 256);
     82 }
     83 
     84 SkFlattenable* SkTableMaskFilter::Factory(SkFlattenableReadBuffer& rb) {
     85     return SkNEW_ARGS(SkTableMaskFilter, (rb));
     86 }
     87 
     88 SkFlattenable::Factory SkTableMaskFilter::getFactory() {
     89     return SkTableMaskFilter::Factory;
     90 }
     91 
     92 ///////////////////////////////////////////////////////////////////////////////
     93 
     94 void SkTableMaskFilter::MakeGammaTable(uint8_t table[256], SkScalar gamma) {
     95     const float dx = 1 / 255.0f;
     96     const float g = SkScalarToFloat(gamma);
     97 
     98     float x = 0;
     99     for (int i = 0; i < 256; i++) {
    100         float ee = powf(x, g) * 255;
    101         table[i] = SkPin32(sk_float_round2int(powf(x, g) * 255), 0, 255);
    102         x += dx;
    103     }
    104 }
    105 
    106 void SkTableMaskFilter::MakeClipTable(uint8_t table[256], uint8_t min,
    107                                       uint8_t max) {
    108     if (0 == max) {
    109         max = 1;
    110     }
    111     if (min >= max) {
    112         min = max - 1;
    113     }
    114     SkASSERT(min < max);
    115 
    116     SkFixed scale = (1 << 16) * 255 / (max - min);
    117     memset(table, 0, min + 1);
    118     for (int i = min + 1; i < max; i++) {
    119         int value = SkFixedRound(scale * (i - min));
    120         SkASSERT(value <= 255);
    121         table[i] = value;
    122     }
    123     memset(table + max, 255, 256 - max);
    124 
    125 #if 0
    126     int j;
    127     for (j = 0; j < 256; j++) {
    128         if (table[j]) {
    129             break;
    130         }
    131     }
    132     SkDebugf("%d %d start [%d]", min, max, j);
    133     for (; j < 256; j++) {
    134         SkDebugf(" %d", table[j]);
    135     }
    136     SkDebugf("\n\n");
    137 #endif
    138 }
    139 
    140