blob: dd238809feee3686d21ad9dc6cb1a123524c14f6 [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 +000036void SkDiscreteMapper::flatten(SkFlattenableWriteBuffer& wb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 this->INHERITED::flatten(wb);
38
39 wb.write32(fSegments);
40 wb.write32(fScale);
41}
42
43///////////////////////////////////////////////////////////////////////////////
44
45uint16_t SkCosineMapper::mapUnit16(uint16_t input)
46{
47 /* we want to call cosine(input * pi/2) treating input as [0...1)
48 however, the straight multitply would overflow 32bits since input is
49 16bits and pi/2 is 17bits, so we shift down our pi const before we mul
50 */
51 SkFixed rads = (unsigned)(input * (SK_FixedPI >> 2)) >> 15;
52 SkFixed x = SkFixedCos(rads);
53 x += x << 15 >> 31; // map 0x10000 to 0xFFFF
54 return SkToU16(x);
55}
56
57SkCosineMapper::SkCosineMapper(SkFlattenableReadBuffer& rb)
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000058 : SkUnitMapper(rb) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000059