Home | History | Annotate | Download | only in utils
      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 #include "SkUnitMappers.h"
      9 
     10 SkDiscreteMapper::SkDiscreteMapper(int segments) {
     11     if (segments < 2) {
     12         fSegments = 0;
     13         fScale = 0;
     14     } else {
     15         if (segments > 0xFFFF) {
     16             segments = 0xFFFF;
     17         }
     18         fSegments = segments;
     19         fScale = SK_Fract1 / (segments - 1);
     20     }
     21 }
     22 
     23 uint16_t SkDiscreteMapper::mapUnit16(uint16_t input) {
     24     SkFixed x = input * fSegments >> 16;
     25     x = x * fScale >> 14;
     26     x += x << 15 >> 31; // map 0x10000 to 0xFFFF
     27     return SkToU16(x);
     28 }
     29 
     30 SkDiscreteMapper::SkDiscreteMapper(SkFlattenableReadBuffer& rb)
     31         : SkUnitMapper(rb) {
     32     fSegments = rb.readU32();
     33     fScale = rb.readU32();
     34 }
     35 
     36 SkFlattenable::Factory SkDiscreteMapper::getFactory() {
     37     return Create;
     38 }
     39 
     40 SkFlattenable* SkDiscreteMapper::Create(SkFlattenableReadBuffer& rb) {
     41     return SkNEW_ARGS(SkDiscreteMapper, (rb));
     42 }
     43 
     44 void SkDiscreteMapper::flatten(SkFlattenableWriteBuffer& wb) {
     45     this->INHERITED::flatten(wb);
     46 
     47     wb.write32(fSegments);
     48     wb.write32(fScale);
     49 }
     50 
     51 ///////////////////////////////////////////////////////////////////////////////
     52 
     53 uint16_t SkCosineMapper::mapUnit16(uint16_t input)
     54 {
     55     /*  we want to call cosine(input * pi/2) treating input as [0...1)
     56         however, the straight multitply would overflow 32bits since input is
     57         16bits and pi/2 is 17bits, so we shift down our pi const before we mul
     58     */
     59     SkFixed rads = (unsigned)(input * (SK_FixedPI >> 2)) >> 15;
     60     SkFixed x = SkFixedCos(rads);
     61     x += x << 15 >> 31; // map 0x10000 to 0xFFFF
     62     return SkToU16(x);
     63 }
     64 
     65 SkCosineMapper::SkCosineMapper(SkFlattenableReadBuffer& rb)
     66     : SkUnitMapper(rb) {}
     67 
     68 SkFlattenable::Factory SkCosineMapper::getFactory() {
     69     return Create;
     70 }
     71 
     72 SkFlattenable* SkCosineMapper::Create(SkFlattenableReadBuffer& rb) {
     73     return SkNEW_ARGS(SkCosineMapper, (rb));
     74 }
     75 
     76