michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #ifndef SkMask_DEFINED michael@0: #define SkMask_DEFINED michael@0: michael@0: #include "SkRect.h" michael@0: michael@0: /** \class SkMask michael@0: SkMask is used to describe alpha bitmaps, either 1bit, 8bit, or michael@0: the 3-channel 3D format. These are passed to SkMaskFilter objects. michael@0: */ michael@0: struct SkMask { michael@0: enum Format { michael@0: kBW_Format, //!< 1bit per pixel mask (e.g. monochrome) michael@0: kA8_Format, //!< 8bits per pixel mask (e.g. antialiasing) michael@0: k3D_Format, //!< 3 8bit per pixl planes: alpha, mul, add michael@0: kARGB32_Format, //!< SkPMColor michael@0: kLCD16_Format, //!< 565 alpha for r/g/b michael@0: kLCD32_Format //!< 888 alpha for r/g/b michael@0: }; michael@0: michael@0: enum { michael@0: kCountMaskFormats = kLCD32_Format + 1 michael@0: }; michael@0: michael@0: uint8_t* fImage; michael@0: SkIRect fBounds; michael@0: uint32_t fRowBytes; michael@0: Format fFormat; michael@0: michael@0: /** Returns true if the mask is empty: i.e. it has an empty bounds. michael@0: */ michael@0: bool isEmpty() const { return fBounds.isEmpty(); } michael@0: michael@0: /** Return the byte size of the mask, assuming only 1 plane. michael@0: Does not account for k3D_Format. For that, use computeTotalImageSize(). michael@0: If there is an overflow of 32bits, then returns 0. michael@0: */ michael@0: size_t computeImageSize() const; michael@0: michael@0: /** Return the byte size of the mask, taking into account michael@0: any extra planes (e.g. k3D_Format). michael@0: If there is an overflow of 32bits, then returns 0. michael@0: */ michael@0: size_t computeTotalImageSize() const; michael@0: michael@0: /** Returns the address of the byte that holds the specified bit. michael@0: Asserts that the mask is kBW_Format, and that x,y are in range. michael@0: x,y are in the same coordiate space as fBounds. michael@0: */ michael@0: uint8_t* getAddr1(int x, int y) const { michael@0: SkASSERT(kBW_Format == fFormat); michael@0: SkASSERT(fBounds.contains(x, y)); michael@0: SkASSERT(fImage != NULL); michael@0: return fImage + ((x - fBounds.fLeft) >> 3) + (y - fBounds.fTop) * fRowBytes; michael@0: } michael@0: michael@0: /** Returns the address of the specified byte. michael@0: Asserts that the mask is kA8_Format, and that x,y are in range. michael@0: x,y are in the same coordiate space as fBounds. michael@0: */ michael@0: uint8_t* getAddr8(int x, int y) const { michael@0: SkASSERT(kA8_Format == fFormat); michael@0: SkASSERT(fBounds.contains(x, y)); michael@0: SkASSERT(fImage != NULL); michael@0: return fImage + x - fBounds.fLeft + (y - fBounds.fTop) * fRowBytes; michael@0: } michael@0: michael@0: /** michael@0: * Return the address of the specified 16bit mask. In the debug build, michael@0: * this asserts that the mask's format is kLCD16_Format, and that (x,y) michael@0: * are contained in the mask's fBounds. michael@0: */ michael@0: uint16_t* getAddrLCD16(int x, int y) const { michael@0: SkASSERT(kLCD16_Format == fFormat); michael@0: SkASSERT(fBounds.contains(x, y)); michael@0: SkASSERT(fImage != NULL); michael@0: uint16_t* row = (uint16_t*)(fImage + (y - fBounds.fTop) * fRowBytes); michael@0: return row + (x - fBounds.fLeft); michael@0: } michael@0: michael@0: /** michael@0: * Return the address of the specified 32bit mask. In the debug build, michael@0: * this asserts that the mask's format is kLCD32_Format, and that (x,y) michael@0: * are contained in the mask's fBounds. michael@0: */ michael@0: uint32_t* getAddrLCD32(int x, int y) const { michael@0: SkASSERT(kLCD32_Format == fFormat); michael@0: SkASSERT(fBounds.contains(x, y)); michael@0: SkASSERT(fImage != NULL); michael@0: uint32_t* row = (uint32_t*)(fImage + (y - fBounds.fTop) * fRowBytes); michael@0: return row + (x - fBounds.fLeft); michael@0: } michael@0: michael@0: /** michael@0: * Return the address of the specified 32bit mask. In the debug build, michael@0: * this asserts that the mask's format is 32bits, and that (x,y) michael@0: * are contained in the mask's fBounds. michael@0: */ michael@0: uint32_t* getAddr32(int x, int y) const { michael@0: SkASSERT(kLCD32_Format == fFormat || kARGB32_Format == fFormat); michael@0: SkASSERT(fBounds.contains(x, y)); michael@0: SkASSERT(fImage != NULL); michael@0: uint32_t* row = (uint32_t*)(fImage + (y - fBounds.fTop) * fRowBytes); michael@0: return row + (x - fBounds.fLeft); michael@0: } michael@0: michael@0: /** michael@0: * Returns the address of the specified pixel, computing the pixel-size michael@0: * at runtime based on the mask format. This will be slightly slower than michael@0: * using one of the routines where the format is implied by the name michael@0: * e.g. getAddr8 or getAddrLCD32. michael@0: * michael@0: * x,y must be contained by the mask's bounds (this is asserted in the michael@0: * debug build, but not checked in the release build.) michael@0: * michael@0: * This should not be called with kBW_Format, as it will give unspecified michael@0: * results (and assert in the debug build). michael@0: */ michael@0: void* getAddr(int x, int y) const; michael@0: michael@0: static uint8_t* AllocImage(size_t bytes); michael@0: static void FreeImage(void* image); michael@0: michael@0: enum CreateMode { michael@0: kJustComputeBounds_CreateMode, //!< compute bounds and return michael@0: kJustRenderImage_CreateMode, //!< render into preallocate mask michael@0: kComputeBoundsAndRenderImage_CreateMode //!< compute bounds, alloc image and render into it michael@0: }; michael@0: }; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: /** michael@0: * \class SkAutoMaskImage michael@0: * michael@0: * Stack class used to manage the fImage buffer in a SkMask. michael@0: * When this object loses scope, the buffer is freed with SkMask::FreeImage(). michael@0: */ michael@0: class SkAutoMaskFreeImage { michael@0: public: michael@0: SkAutoMaskFreeImage(uint8_t* maskImage) { michael@0: fImage = maskImage; michael@0: } michael@0: michael@0: ~SkAutoMaskFreeImage() { michael@0: SkMask::FreeImage(fImage); michael@0: } michael@0: michael@0: private: michael@0: uint8_t* fImage; michael@0: }; michael@0: #define SkAutoMaskFreeImage(...) SK_REQUIRE_LOCAL_VAR(SkAutoMaskFreeImage) michael@0: michael@0: #endif