1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 18 19 20 #ifndef SkUnPreMultiply_DEFINED 21 #define SkUnPreMultiply_DEFINED 22 23 #include "SkColor.h" 24 25 class SkUnPreMultiply { 26 public: 27 typedef uint32_t Scale; 28 29 // index this table with alpha [0..255] GetScaleTable()30 static const Scale* GetScaleTable() { 31 return gTable; 32 } 33 GetScale(U8CPU alpha)34 static Scale GetScale(U8CPU alpha) { 35 SkASSERT(alpha <= 255); 36 return gTable[alpha]; 37 } 38 39 /** Usage: 40 41 const Scale* table = SkUnPreMultiply::GetScaleTable(); 42 43 for (...) { 44 unsigned a = ... 45 SkUnPreMultiply::Scale scale = table[a]; 46 47 red = SkUnPreMultiply::ApplyScale(scale, red); 48 ... 49 // now red is unpremultiplied 50 } 51 */ ApplyScale(Scale scale,U8CPU component)52 static U8CPU ApplyScale(Scale scale, U8CPU component) { 53 SkASSERT(component <= 255); 54 return (scale * component + (1 << 23)) >> 24; 55 } 56 57 static SkColor PMColorToColor(SkPMColor c); 58 59 private: 60 static const uint32_t gTable[256]; 61 }; 62 63 #endif 64