1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
28a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2008 The Android Open Source Project
48a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
78a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com */
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
12ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#ifndef SkUnPreMultiply_DEFINED
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#define SkUnPreMultiply_DEFINED
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColor.h"
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
187ffb1b21abcc7bbed5a0fc711f6dd7b9dbb4f577ctguil@chromium.orgclass SK_API SkUnPreMultiply {
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic:
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    typedef uint32_t Scale;
21fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // index this table with alpha [0..255]
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static const Scale* GetScaleTable() {
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return gTable;
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static Scale GetScale(U8CPU alpha) {
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkASSERT(alpha <= 255);
298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return gTable[alpha];
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
31fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    /** Usage:
33fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        const Scale* table = SkUnPreMultiply::GetScaleTable();
35fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        for (...) {
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            unsigned a = ...
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            SkUnPreMultiply::Scale scale = table[a];
39fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            red = SkUnPreMultiply::ApplyScale(scale, red);
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            ...
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com            // now red is unpremultiplied
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        }
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    */
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static U8CPU ApplyScale(Scale scale, U8CPU component) {
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        SkASSERT(component <= 255);
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return (scale * component + (1 << 23)) >> 24;
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
49fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static SkColor PMColorToColor(SkPMColor c);
51fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com
52b06faacaf13b9b5c47b8526492ad155bdce3863bcommit-bot@chromium.org    static uint32_t UnPreMultiplyPreservingByteOrder(SkPMColor c);
53b06faacaf13b9b5c47b8526492ad155bdce3863bcommit-bot@chromium.org
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate:
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    static const uint32_t gTable[256];
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com};
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#endif
59