| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 #include "SkUnitMappers.h" | 8 #include "SkUnitMappers.h" |
| 9 #include "SkFlattenableBuffers.h" | 9 #include "SkReadBuffer.h" |
| 10 #include "SkWriteBuffer.h" |
| 10 | 11 |
| 11 | 12 |
| 12 SkDiscreteMapper::SkDiscreteMapper(int segments) { | 13 SkDiscreteMapper::SkDiscreteMapper(int segments) { |
| 13 if (segments < 2) { | 14 if (segments < 2) { |
| 14 fSegments = 0; | 15 fSegments = 0; |
| 15 fScale = 0; | 16 fScale = 0; |
| 16 } else { | 17 } else { |
| 17 if (segments > 0xFFFF) { | 18 if (segments > 0xFFFF) { |
| 18 segments = 0xFFFF; | 19 segments = 0xFFFF; |
| 19 } | 20 } |
| 20 fSegments = segments; | 21 fSegments = segments; |
| 21 fScale = (1 << 30) / (segments - 1); | 22 fScale = (1 << 30) / (segments - 1); |
| 22 } | 23 } |
| 23 } | 24 } |
| 24 | 25 |
| 25 uint16_t SkDiscreteMapper::mapUnit16(uint16_t input) { | 26 uint16_t SkDiscreteMapper::mapUnit16(uint16_t input) { |
| 26 SkFixed x = input * fSegments >> 16; | 27 SkFixed x = input * fSegments >> 16; |
| 27 x = x * fScale >> 14; | 28 x = x * fScale >> 14; |
| 28 x += x << 15 >> 31; // map 0x10000 to 0xFFFF | 29 x += x << 15 >> 31; // map 0x10000 to 0xFFFF |
| 29 return SkToU16(x); | 30 return SkToU16(x); |
| 30 } | 31 } |
| 31 | 32 |
| 32 SkDiscreteMapper::SkDiscreteMapper(SkFlattenableReadBuffer& rb) | 33 SkDiscreteMapper::SkDiscreteMapper(SkReadBuffer& rb) |
| 33 : SkUnitMapper(rb) { | 34 : SkUnitMapper(rb) { |
| 34 fSegments = rb.readInt(); | 35 fSegments = rb.readInt(); |
| 35 fScale = rb.read32(); | 36 fScale = rb.read32(); |
| 36 } | 37 } |
| 37 | 38 |
| 38 void SkDiscreteMapper::flatten(SkFlattenableWriteBuffer& wb) const { | 39 void SkDiscreteMapper::flatten(SkWriteBuffer& wb) const { |
| 39 this->INHERITED::flatten(wb); | 40 this->INHERITED::flatten(wb); |
| 40 | 41 |
| 41 wb.writeInt(fSegments); | 42 wb.writeInt(fSegments); |
| 42 wb.write32(fScale); | 43 wb.write32(fScale); |
| 43 } | 44 } |
| 44 | 45 |
| 45 /////////////////////////////////////////////////////////////////////////////// | 46 /////////////////////////////////////////////////////////////////////////////// |
| 46 | 47 |
| 47 uint16_t SkCosineMapper::mapUnit16(uint16_t input) | 48 uint16_t SkCosineMapper::mapUnit16(uint16_t input) |
| 48 { | 49 { |
| 49 /* we want to call cosine(input * pi/2) treating input as [0...1) | 50 /* 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 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 16bits and pi/2 is 17bits, so we shift down our pi const before we mul |
| 52 */ | 53 */ |
| 53 SkFixed rads = (unsigned)(input * (SK_FixedPI >> 2)) >> 15; | 54 SkFixed rads = (unsigned)(input * (SK_FixedPI >> 2)) >> 15; |
| 54 SkFixed x = SkFixedCos(rads); | 55 SkFixed x = SkFixedCos(rads); |
| 55 x += x << 15 >> 31; // map 0x10000 to 0xFFFF | 56 x += x << 15 >> 31; // map 0x10000 to 0xFFFF |
| 56 return SkToU16(x); | 57 return SkToU16(x); |
| 57 } | 58 } |
| 58 | 59 |
| 59 SkCosineMapper::SkCosineMapper(SkFlattenableReadBuffer& rb) | 60 SkCosineMapper::SkCosineMapper(SkReadBuffer& rb) |
| 60 : SkUnitMapper(rb) {} | 61 : SkUnitMapper(rb) {} |
| OLD | NEW |