blob: 764ead68e10f72a29056f0d2948e4c29007ea481 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
2 * Copyright (C) 2006 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#ifndef SkMask_DEFINED
18#define SkMask_DEFINED
19
20#include "SkRect.h"
21
22/** \class SkMask
23 SkMask is used to describe alpha bitmaps, either 1bit, 8bit, or
24 the 3-channel 3D format. These are passed to SkMaskFilter objects.
25*/
26struct SkMask {
27 enum Format {
28 kBW_Format, //!< 1bit per pixel mask (e.g. monochrome)
29 kA8_Format, //!< 8bits per pixel mask (e.g. antialiasing)
30 k3D_Format, //!< 3 8bit per pixl planes: alpha, mul, add
31 kLCD_Format //!< 3 bytes/pixel: r/g/b
32 };
33
34 enum {
35 kCountMaskFormats = kLCD_Format + 1
36 };
37
38 uint8_t* fImage;
39 SkIRect fBounds;
40 uint16_t fRowBytes;
41 uint8_t fFormat; // Format
42
43 /** Return the byte size of the mask, assuming only 1 plane.
44 Does not account for k3D_Format. For that, use computeFormatImageSize()
45 */
46 size_t computeImageSize() const;
47 /** Return the byte size of the mask, taking into account
48 any extra planes (e.g. k3D_Format).
49 */
50 size_t computeTotalImageSize() const;
51
52 /** Returns the address of the byte that holds the specified bit.
53 Asserts that the mask is kBW_Format, and that x,y are in range.
54 x,y are in the same coordiate space as fBounds.
55 */
56 uint8_t* getAddr1(int x, int y) const
57 {
58 SkASSERT(fFormat == kBW_Format);
59 SkASSERT(fBounds.contains(x, y));
60 SkASSERT(fImage != NULL);
61 return fImage + ((x - fBounds.fLeft) >> 3) + (y - fBounds.fTop) * fRowBytes;
62 }
63 /** Returns the address of the specified byte.
64 Asserts that the mask is kA8_Format, and that x,y are in range.
65 x,y are in the same coordiate space as fBounds.
66 */
67 uint8_t* getAddr(int x, int y) const
68 {
69 SkASSERT(fFormat != kBW_Format);
70 SkASSERT(fBounds.contains(x, y));
71 SkASSERT(fImage != NULL);
72 return fImage + x - fBounds.fLeft + (y - fBounds.fTop) * fRowBytes;
73 }
74
75 static uint8_t* AllocImage(size_t bytes);
76 static void FreeImage(void* image);
77
78 enum CreateMode {
79 kJustComputeBounds_CreateMode, //!< compute bounds and return
80 kJustRenderImage_CreateMode, //!< render into preallocate mask
81 kComputeBoundsAndRenderImage_CreateMode //!< compute bounds, alloc image and render into it
82 };
83};
84
85#endif
86