epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkUnitMappers.h" |
| 9 | |
robertphillips@google.com | 0456e0b | 2012-06-27 14:03:26 +0000 | [diff] [blame] | 10 | SK_DEFINE_INST_COUNT(SkUnitMapper) |
| 11 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 12 | SkDiscreteMapper::SkDiscreteMapper(int segments) { |
| 13 | if (segments < 2) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | fSegments = 0; |
| 15 | fScale = 0; |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 16 | } else { |
| 17 | if (segments > 0xFFFF) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | segments = 0xFFFF; |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 19 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | fSegments = segments; |
| 21 | fScale = SK_Fract1 / (segments - 1); |
| 22 | } |
| 23 | } |
| 24 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 25 | uint16_t SkDiscreteMapper::mapUnit16(uint16_t input) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | 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 | |
| 32 | SkDiscreteMapper::SkDiscreteMapper(SkFlattenableReadBuffer& rb) |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 33 | : SkUnitMapper(rb) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | fSegments = rb.readU32(); |
| 35 | fScale = rb.readU32(); |
| 36 | } |
| 37 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 38 | void SkDiscreteMapper::flatten(SkFlattenableWriteBuffer& wb) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | this->INHERITED::flatten(wb); |
| 40 | |
| 41 | wb.write32(fSegments); |
| 42 | wb.write32(fScale); |
| 43 | } |
| 44 | |
| 45 | /////////////////////////////////////////////////////////////////////////////// |
| 46 | |
| 47 | uint16_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 | |
| 59 | SkCosineMapper::SkCosineMapper(SkFlattenableReadBuffer& rb) |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 60 | : SkUnitMapper(rb) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | |
scroggo@google.com | 6eb0d62 | 2012-06-25 20:32:12 +0000 | [diff] [blame] | 62 | SK_DEFINE_FLATTENABLE_REGISTRAR(SkDiscreteMapper) |
| 63 | SK_DEFINE_FLATTENABLE_REGISTRAR(SkCosineMapper) |