Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / utils / SkUnitMappers.cpp
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 "SkReadBuffer.h"
10 #include "SkWriteBuffer.h"
11
12
13 SkDiscreteMapper::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 = (1 << 30) / (segments - 1);
23     }
24 }
25
26 uint16_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
33 SkDiscreteMapper::SkDiscreteMapper(SkReadBuffer& rb)
34         : SkUnitMapper(rb) {
35     fSegments = rb.readInt();
36     fScale = rb.read32();
37 }
38
39 void SkDiscreteMapper::flatten(SkWriteBuffer& wb) const {
40     this->INHERITED::flatten(wb);
41
42     wb.writeInt(fSegments);
43     wb.write32(fScale);
44 }
45
46 ///////////////////////////////////////////////////////////////////////////////
47
48 uint16_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
60 SkCosineMapper::SkCosineMapper(SkReadBuffer& rb)
61     : SkUnitMapper(rb) {}