reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 26 | struct 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; |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 40 | uint32_t fRowBytes; |
| 41 | Format fFormat; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame^] | 43 | /** Returns true if the mask is empty: i.e. it has an empty bounds. |
| 44 | */ |
| 45 | bool isEmpty() const { return fBounds.isEmpty(); } |
| 46 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | /** Return the byte size of the mask, assuming only 1 plane. |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame^] | 48 | Does not account for k3D_Format. For that, use computeTotalImageSize(). |
| 49 | If there is an overflow of 32bits, then returns 0. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | */ |
| 51 | size_t computeImageSize() const; |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 52 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | /** Return the byte size of the mask, taking into account |
| 54 | any extra planes (e.g. k3D_Format). |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame^] | 55 | If there is an overflow of 32bits, then returns 0. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | */ |
| 57 | size_t computeTotalImageSize() const; |
| 58 | |
| 59 | /** Returns the address of the byte that holds the specified bit. |
| 60 | Asserts that the mask is kBW_Format, and that x,y are in range. |
| 61 | x,y are in the same coordiate space as fBounds. |
| 62 | */ |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 63 | uint8_t* getAddr1(int x, int y) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | SkASSERT(fFormat == kBW_Format); |
| 65 | SkASSERT(fBounds.contains(x, y)); |
| 66 | SkASSERT(fImage != NULL); |
| 67 | return fImage + ((x - fBounds.fLeft) >> 3) + (y - fBounds.fTop) * fRowBytes; |
| 68 | } |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 69 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | /** Returns the address of the specified byte. |
| 71 | Asserts that the mask is kA8_Format, and that x,y are in range. |
| 72 | x,y are in the same coordiate space as fBounds. |
| 73 | */ |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 74 | uint8_t* getAddr(int x, int y) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | SkASSERT(fFormat != kBW_Format); |
| 76 | SkASSERT(fBounds.contains(x, y)); |
| 77 | SkASSERT(fImage != NULL); |
| 78 | return fImage + x - fBounds.fLeft + (y - fBounds.fTop) * fRowBytes; |
| 79 | } |
| 80 | |
| 81 | static uint8_t* AllocImage(size_t bytes); |
| 82 | static void FreeImage(void* image); |
| 83 | |
| 84 | enum CreateMode { |
| 85 | kJustComputeBounds_CreateMode, //!< compute bounds and return |
| 86 | kJustRenderImage_CreateMode, //!< render into preallocate mask |
| 87 | kComputeBoundsAndRenderImage_CreateMode //!< compute bounds, alloc image and render into it |
| 88 | }; |
| 89 | }; |
| 90 | |
| 91 | #endif |
| 92 | |