michael@0: /* michael@0: * Copyright 2013 Google Inc. 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: #ifndef SkPerlinNoiseShader_DEFINED michael@0: #define SkPerlinNoiseShader_DEFINED michael@0: michael@0: #include "SkShader.h" michael@0: michael@0: /** \class SkPerlinNoiseShader michael@0: michael@0: SkPerlinNoiseShader creates an image using the Perlin turbulence function. michael@0: michael@0: It can produce tileable noise if asked to stitch tiles and provided a tile size. michael@0: In order to fill a large area with repeating noise, set the stitchTiles flag to michael@0: true, and render exactly a single tile of noise. Without this flag, the result michael@0: will contain visible seams between tiles. michael@0: michael@0: The algorithm used is described here : michael@0: http://www.w3.org/TR/SVG/filters.html#feTurbulenceElement michael@0: */ michael@0: class SK_API SkPerlinNoiseShader : public SkShader { michael@0: struct PaintingData; michael@0: public: michael@0: struct StitchData; michael@0: michael@0: /** michael@0: * About the noise types : the difference between the 2 is just minor tweaks to the algorithm, michael@0: * they're not 2 entirely different noises. The output looks different, but once the noise is michael@0: * generated in the [1, -1] range, the output is brought back in the [0, 1] range by doing : michael@0: * kFractalNoise_Type : noise * 0.5 + 0.5 michael@0: * kTurbulence_Type : abs(noise) michael@0: * Very little differences between the 2 types, although you can tell the difference visually. michael@0: */ michael@0: enum Type { michael@0: kFractalNoise_Type, michael@0: kTurbulence_Type, michael@0: kFirstType = kFractalNoise_Type, michael@0: kLastType = kTurbulence_Type michael@0: }; michael@0: /** michael@0: * This will construct Perlin noise of the given type (Fractal Noise or Turbulence). michael@0: * michael@0: * Both base frequencies (X and Y) have a usual range of (0..1). michael@0: * michael@0: * The number of octaves provided should be fairly small, although no limit is enforced. michael@0: * Each octave doubles the frequency, so 10 octaves would produce noise from michael@0: * baseFrequency * 1, * 2, * 4, ..., * 512, which quickly yields insignificantly small michael@0: * periods and resembles regular unstructured noise rather than Perlin noise. michael@0: * michael@0: * If tileSize isn't NULL or an empty size, the tileSize parameter will be used to modify michael@0: * the frequencies so that the noise will be tileable for the given tile size. If tileSize michael@0: * is NULL or an empty size, the frequencies will be used as is without modification. michael@0: */ michael@0: static SkShader* CreateFractalNoise(SkScalar baseFrequencyX, SkScalar baseFrequencyY, michael@0: int numOctaves, SkScalar seed, michael@0: const SkISize* tileSize = NULL); michael@0: static SkShader* CreateTubulence(SkScalar baseFrequencyX, SkScalar baseFrequencyY, michael@0: int numOctaves, SkScalar seed, michael@0: const SkISize* tileSize = NULL); michael@0: michael@0: virtual bool setContext(const SkBitmap& device, const SkPaint& paint, michael@0: const SkMatrix& matrix); michael@0: virtual void shadeSpan(int x, int y, SkPMColor[], int count) SK_OVERRIDE; michael@0: virtual void shadeSpan16(int x, int y, uint16_t[], int count) SK_OVERRIDE; michael@0: michael@0: virtual GrEffectRef* asNewEffect(GrContext* context, const SkPaint&) const SK_OVERRIDE; michael@0: michael@0: SK_TO_STRING_OVERRIDE() michael@0: SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkPerlinNoiseShader) michael@0: michael@0: protected: michael@0: SkPerlinNoiseShader(SkReadBuffer&); michael@0: virtual void flatten(SkWriteBuffer&) const SK_OVERRIDE; michael@0: michael@0: private: michael@0: SkPerlinNoiseShader(SkPerlinNoiseShader::Type type, SkScalar baseFrequencyX, michael@0: SkScalar baseFrequencyY, int numOctaves, SkScalar seed, michael@0: const SkISize* tileSize); michael@0: virtual ~SkPerlinNoiseShader(); michael@0: michael@0: SkScalar noise2D(int channel, const PaintingData& paintingData, michael@0: const StitchData& stitchData, const SkPoint& noiseVector) const; michael@0: michael@0: SkScalar calculateTurbulenceValueForPoint(int channel, const PaintingData& paintingData, michael@0: StitchData& stitchData, const SkPoint& point) const; michael@0: michael@0: SkPMColor shade(const SkPoint& point, StitchData& stitchData) const; michael@0: michael@0: // TODO (scroggo): Once all SkShaders are created from a factory, and we have removed the michael@0: // constructor that creates SkPerlinNoiseShader from an SkReadBuffer, several fields can michael@0: // be made constant. michael@0: /*const*/ SkPerlinNoiseShader::Type fType; michael@0: /*const*/ SkScalar fBaseFrequencyX; michael@0: /*const*/ SkScalar fBaseFrequencyY; michael@0: /*const*/ int fNumOctaves; michael@0: /*const*/ SkScalar fSeed; michael@0: /*const*/ SkISize fTileSize; michael@0: /*const*/ bool fStitchTiles; michael@0: // TODO (scroggo): Once setContext creates a new object, place this on that object. michael@0: SkMatrix fMatrix; michael@0: michael@0: PaintingData* fPaintingData; michael@0: michael@0: typedef SkShader INHERITED; michael@0: }; michael@0: michael@0: #endif