SkMask.cpp revision ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976e
1
2/*
3 * Copyright 2007 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#include "Sk64.h"
11#include "SkMask.h"
12
13/** returns the product if it is positive and fits in 31 bits. Otherwise this
14    returns 0.
15 */
16static int32_t safeMul32(int32_t a, int32_t b) {
17    Sk64 size;
18    size.setMul(a, b);
19    if (size.is32() && size.isPos()) {
20        return size.get32();
21    }
22    return 0;
23}
24
25size_t SkMask::computeImageSize() const {
26    return safeMul32(fBounds.height(), fRowBytes);
27}
28
29size_t SkMask::computeTotalImageSize() const {
30    size_t size = this->computeImageSize();
31    if (fFormat == SkMask::k3D_Format) {
32        size = safeMul32(size, 3);
33    }
34    return size;
35}
36
37/** We explicitly use this allocator for SkBimap pixels, so that we can
38    freely assign memory allocated by one class to the other.
39*/
40uint8_t* SkMask::AllocImage(size_t size) {
41    return (uint8_t*)sk_malloc_throw(SkAlign4(size));
42}
43
44/** We explicitly use this allocator for SkBimap pixels, so that we can
45    freely assign memory allocated by one class to the other.
46*/
47void SkMask::FreeImage(void* image) {
48    sk_free(image);
49}
50
51