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 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 10 | SkDiscreteMapper::SkDiscreteMapper(int segments) { |
| 11 | if (segments < 2) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | fSegments = 0; |
| 13 | fScale = 0; |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 14 | } else { |
| 15 | if (segments > 0xFFFF) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | segments = 0xFFFF; |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 17 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | fSegments = segments; |
| 19 | fScale = SK_Fract1 / (segments - 1); |
| 20 | } |
| 21 | } |
| 22 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 23 | uint16_t SkDiscreteMapper::mapUnit16(uint16_t input) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | 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 | |
| 30 | SkDiscreteMapper::SkDiscreteMapper(SkFlattenableReadBuffer& rb) |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 31 | : SkUnitMapper(rb) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | fSegments = rb.readU32(); |
| 33 | fScale = rb.readU32(); |
| 34 | } |
| 35 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 36 | SkFlattenable::Factory SkDiscreteMapper::getFactory() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | return Create; |
| 38 | } |
| 39 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 40 | SkFlattenable* SkDiscreteMapper::Create(SkFlattenableReadBuffer& rb) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | return SkNEW_ARGS(SkDiscreteMapper, (rb)); |
| 42 | } |
| 43 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 44 | void SkDiscreteMapper::flatten(SkFlattenableWriteBuffer& wb) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | this->INHERITED::flatten(wb); |
| 46 | |
| 47 | wb.write32(fSegments); |
| 48 | wb.write32(fScale); |
| 49 | } |
| 50 | |
| 51 | /////////////////////////////////////////////////////////////////////////////// |
| 52 | |
| 53 | uint16_t SkCosineMapper::mapUnit16(uint16_t input) |
| 54 | { |
| 55 | /* we want to call cosine(input * pi/2) treating input as [0...1) |
| 56 | however, the straight multitply would overflow 32bits since input is |
| 57 | 16bits and pi/2 is 17bits, so we shift down our pi const before we mul |
| 58 | */ |
| 59 | SkFixed rads = (unsigned)(input * (SK_FixedPI >> 2)) >> 15; |
| 60 | SkFixed x = SkFixedCos(rads); |
| 61 | x += x << 15 >> 31; // map 0x10000 to 0xFFFF |
| 62 | return SkToU16(x); |
| 63 | } |
| 64 | |
| 65 | SkCosineMapper::SkCosineMapper(SkFlattenableReadBuffer& rb) |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 66 | : SkUnitMapper(rb) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 68 | SkFlattenable::Factory SkCosineMapper::getFactory() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | return Create; |
| 70 | } |
| 71 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 72 | SkFlattenable* SkCosineMapper::Create(SkFlattenableReadBuffer& rb) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | return SkNEW_ARGS(SkCosineMapper, (rb)); |
| 74 | } |
| 75 | |