blob: b2ab02bb0e5cf6b93e5f400e361cb360f07f05d7 [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
robertphillips@google.com0456e0b2012-06-27 14:03:26 +000010SK_DEFINE_INST_COUNT(SkUnitMapper)
11
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000012SkDiscreteMapper::SkDiscreteMapper(int segments) {
13 if (segments < 2) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000014 fSegments = 0;
15 fScale = 0;
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000016 } else {
17 if (segments > 0xFFFF) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000018 segments = 0xFFFF;
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000019 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000020 fSegments = segments;
21 fScale = SK_Fract1 / (segments - 1);
22 }
23}
24
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000025uint16_t SkDiscreteMapper::mapUnit16(uint16_t input) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000026 SkFixed x = input * fSegments >> 16;
27 x = x * fScale >> 14;
28 x += x << 15 >> 31; // map 0x10000 to 0xFFFF
29 return SkToU16(x);
30}
31
32SkDiscreteMapper::SkDiscreteMapper(SkFlattenableReadBuffer& rb)
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000033 : SkUnitMapper(rb) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 fSegments = rb.readU32();
35 fScale = rb.readU32();
36}
37
djsollen@google.com54924242012-03-29 15:18:04 +000038void SkDiscreteMapper::flatten(SkFlattenableWriteBuffer& wb) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 this->INHERITED::flatten(wb);
40
41 wb.write32(fSegments);
42 wb.write32(fScale);
43}
44
45///////////////////////////////////////////////////////////////////////////////
46
47uint16_t SkCosineMapper::mapUnit16(uint16_t input)
48{
49 /* we want to call cosine(input * pi/2) treating input as [0...1)
50 however, the straight multitply would overflow 32bits since input is
51 16bits and pi/2 is 17bits, so we shift down our pi const before we mul
52 */
53 SkFixed rads = (unsigned)(input * (SK_FixedPI >> 2)) >> 15;
54 SkFixed x = SkFixedCos(rads);
55 x += x << 15 >> 31; // map 0x10000 to 0xFFFF
56 return SkToU16(x);
57}
58
59SkCosineMapper::SkCosineMapper(SkFlattenableReadBuffer& rb)
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000060 : SkUnitMapper(rb) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000061
scroggo@google.com6eb0d622012-06-25 20:32:12 +000062SK_DEFINE_FLATTENABLE_REGISTRAR(SkDiscreteMapper)
63SK_DEFINE_FLATTENABLE_REGISTRAR(SkCosineMapper)