blob: ceff9ca00c5005b082b98e6d4dee78964b90e2c3 [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"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00009#include "SkFlattenableBuffers.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010
robertphillips@google.com0456e0b2012-06-27 14:03:26 +000011SK_DEFINE_INST_COUNT(SkUnitMapper)
12
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000013SkDiscreteMapper::SkDiscreteMapper(int segments) {
14 if (segments < 2) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000015 fSegments = 0;
16 fScale = 0;
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000017 } else {
18 if (segments > 0xFFFF) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000019 segments = 0xFFFF;
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000020 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000021 fSegments = segments;
22 fScale = SK_Fract1 / (segments - 1);
23 }
24}
25
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000026uint16_t SkDiscreteMapper::mapUnit16(uint16_t input) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 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)
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000034 : SkUnitMapper(rb) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000035 fSegments = rb.readInt();
36 fScale = rb.read32();
reed@android.com8a1c16f2008-12-17 15:59:43 +000037}
38
djsollen@google.com54924242012-03-29 15:18:04 +000039void SkDiscreteMapper::flatten(SkFlattenableWriteBuffer& wb) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 this->INHERITED::flatten(wb);
41
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000042 wb.writeInt(fSegments);
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 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)
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000061 : SkUnitMapper(rb) {}