blob: 583d091791624fbb9d45413d3ae90dfa145b10d0 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkUnitMappers.h"
9
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000010SkDiscreteMapper::SkDiscreteMapper(int segments) {
11 if (segments < 2) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000012 fSegments = 0;
13 fScale = 0;
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000014 } else {
15 if (segments > 0xFFFF) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000016 segments = 0xFFFF;
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000017 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000018 fSegments = segments;
19 fScale = SK_Fract1 / (segments - 1);
20 }
21}
22
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000023uint16_t SkDiscreteMapper::mapUnit16(uint16_t input) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000024 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
30SkDiscreteMapper::SkDiscreteMapper(SkFlattenableReadBuffer& rb)
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000031 : SkUnitMapper(rb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000032 fSegments = rb.readU32();
33 fScale = rb.readU32();
34}
35
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000036SkFlattenable::Factory SkDiscreteMapper::getFactory() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 return Create;
38}
39
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000040SkFlattenable* SkDiscreteMapper::Create(SkFlattenableReadBuffer& rb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 return SkNEW_ARGS(SkDiscreteMapper, (rb));
42}
43
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000044void SkDiscreteMapper::flatten(SkFlattenableWriteBuffer& wb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 this->INHERITED::flatten(wb);
46
47 wb.write32(fSegments);
48 wb.write32(fScale);
49}
50
51///////////////////////////////////////////////////////////////////////////////
52
53uint16_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
65SkCosineMapper::SkCosineMapper(SkFlattenableReadBuffer& rb)
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000066 : SkUnitMapper(rb) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000067
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000068SkFlattenable::Factory SkCosineMapper::getFactory() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 return Create;
70}
71
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000072SkFlattenable* SkCosineMapper::Create(SkFlattenableReadBuffer& rb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 return SkNEW_ARGS(SkCosineMapper, (rb));
74}
75