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" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 9 | #include "SkFlattenableBuffers.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 11 | SkDiscreteMapper::SkDiscreteMapper(int segments) { |
| 12 | if (segments < 2) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | fSegments = 0; |
| 14 | fScale = 0; |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 15 | } else { |
| 16 | if (segments > 0xFFFF) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | segments = 0xFFFF; |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 18 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | fSegments = segments; |
| 20 | fScale = SK_Fract1 / (segments - 1); |
| 21 | } |
| 22 | } |
| 23 | |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 24 | uint16_t SkDiscreteMapper::mapUnit16(uint16_t input) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | 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 | |
| 31 | SkDiscreteMapper::SkDiscreteMapper(SkFlattenableReadBuffer& rb) |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 32 | : SkUnitMapper(rb) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 33 | fSegments = rb.readInt(); |
| 34 | fScale = rb.read32(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | } |
| 36 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 37 | void SkDiscreteMapper::flatten(SkFlattenableWriteBuffer& wb) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | this->INHERITED::flatten(wb); |
| 39 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 40 | wb.writeInt(fSegments); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | wb.write32(fScale); |
| 42 | } |
| 43 | |
| 44 | /////////////////////////////////////////////////////////////////////////////// |
| 45 | |
| 46 | uint16_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 | |
| 58 | SkCosineMapper::SkCosineMapper(SkFlattenableReadBuffer& rb) |
mike@reedtribe.org | 68ac0df | 2011-05-04 00:20:09 +0000 | [diff] [blame] | 59 | : SkUnitMapper(rb) {} |