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