blob: 5976e9de52ed5c3c9e1a2bd098932632efc3f583 [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
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000011SkDiscreteMapper::SkDiscreteMapper(int segments) {
12 if (segments < 2) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000013 fSegments = 0;
14 fScale = 0;
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000015 } else {
16 if (segments > 0xFFFF) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000017 segments = 0xFFFF;
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000018 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000019 fSegments = segments;
20 fScale = SK_Fract1 / (segments - 1);
21 }
22}
23
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000024uint16_t SkDiscreteMapper::mapUnit16(uint16_t input) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000025 SkFixed x = input * fSegments >> 16;
26 x = x * fScale >> 14;
27 x += x << 15 >> 31; // map 0x10000 to 0xFFFF
28 return SkToU16(x);
29}
30
31SkDiscreteMapper::SkDiscreteMapper(SkFlattenableReadBuffer& rb)
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000032 : SkUnitMapper(rb) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000033 fSegments = rb.readInt();
34 fScale = rb.read32();
reed@android.com8a1c16f2008-12-17 15:59:43 +000035}
36
djsollen@google.com54924242012-03-29 15:18:04 +000037void SkDiscreteMapper::flatten(SkFlattenableWriteBuffer& wb) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000038 this->INHERITED::flatten(wb);
39
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000040 wb.writeInt(fSegments);
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 wb.write32(fScale);
42}
43
44///////////////////////////////////////////////////////////////////////////////
45
46uint16_t SkCosineMapper::mapUnit16(uint16_t input)
47{
48 /* we want to call cosine(input * pi/2) treating input as [0...1)
49 however, the straight multitply would overflow 32bits since input is
50 16bits and pi/2 is 17bits, so we shift down our pi const before we mul
51 */
52 SkFixed rads = (unsigned)(input * (SK_FixedPI >> 2)) >> 15;
53 SkFixed x = SkFixedCos(rads);
54 x += x << 15 >> 31; // map 0x10000 to 0xFFFF
55 return SkToU16(x);
56}
57
58SkCosineMapper::SkCosineMapper(SkFlattenableReadBuffer& rb)
mike@reedtribe.org68ac0df2011-05-04 00:20:09 +000059 : SkUnitMapper(rb) {}