001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package org.jvnet.substance.painter.noise;
031:
032: import java.awt.Color;
033: import java.awt.image.*;
034:
035: import org.jvnet.substance.theme.SubstanceTheme;
036: import org.jvnet.substance.utils.PerlinNoiseGenerator;
037: import org.jvnet.substance.utils.SubstanceColorUtilities;
038:
039: /**
040: * Factory for creating noise images. This class is part of officially supported
041: * API.
042: *
043: * @author Kirill Grouchnikov.
044: */
045: public class NoiseFactory {
046: /**
047: * Returns a noise image.
048: *
049: * @param theme1
050: * The first theme to use for rendering the image.
051: * @param theme2
052: * The second theme to use for rendering the image.
053: * @param width
054: * Image width.
055: * @param height
056: * Image height.
057: * @param xFactor
058: * X stretch factor.
059: * @param yFactor
060: * Y stretch factor.
061: * @param hasConstantZ
062: * Indication whether the Z is constant.
063: * @param noiseFilter
064: * Noise filter to apply.
065: * @param toBlur
066: * Indication whether the resulting image should be blurred.
067: * @param isPreview
068: * Indication whether the image is in preview mode.
069: * @return Noise image.
070: */
071: public static BufferedImage getNoiseImage(SubstanceTheme theme1,
072: SubstanceTheme theme2, int width, int height,
073: double xFactor, double yFactor, boolean hasConstantZ,
074: NoiseFilter noiseFilter, boolean toBlur, boolean isPreview) {
075: Color c1_1 = SubstanceColorUtilities
076: .getWatermarkDarkColor(theme1);
077: Color c2_1 = SubstanceColorUtilities
078: .getWatermarkStampColor(theme1);
079: Color c3_1 = SubstanceColorUtilities
080: .getWatermarkLightColor(theme1);
081: Color c1_2 = SubstanceColorUtilities
082: .getWatermarkDarkColor(theme2);
083: Color c2_2 = SubstanceColorUtilities
084: .getWatermarkStampColor(theme2);
085: Color c3_2 = SubstanceColorUtilities
086: .getWatermarkLightColor(theme2);
087:
088: Color c1 = SubstanceColorUtilities.getInterpolatedColor(c1_1,
089: c1_2, 0.1);
090: Color c2 = SubstanceColorUtilities.getInterpolatedColor(c2_1,
091: c2_2, 0.5);
092: Color c3 = SubstanceColorUtilities.getInterpolatedColor(c3_1,
093: c3_2, 0.9);
094: if (isPreview) {
095: c1 = SubstanceColorUtilities.getAlphaColor(c1, 255);
096: c2 = SubstanceColorUtilities.getAlphaColor(c2, 255);
097: c3 = SubstanceColorUtilities.getAlphaColor(c3, 255);
098: }
099:
100: BufferedImage dst = new BufferedImage(width, height,
101: BufferedImage.TYPE_INT_ARGB);
102:
103: // Borrow from Sebastien Petrucci fast blur code - direct access
104: // to the raster data
105: int[] dstBuffer = ((DataBufferInt) dst.getRaster()
106: .getDataBuffer()).getData();
107:
108: double m2 = xFactor * width * xFactor * width + yFactor
109: * height * yFactor * height;
110: int pos = 0;
111: for (int j = 0; j < height; j++) {
112: double jj = yFactor * j;
113: for (int i = 0; i < width; i++) {
114: double ii = xFactor * i;
115: double z = hasConstantZ ? 1.0 : Math.sqrt(m2 - ii * ii
116: - jj * jj);
117: double noise = 0.5 + 0.5 * PerlinNoiseGenerator.noise(
118: ii, jj, z);
119: if (noiseFilter != null)
120: noise = noiseFilter.apply(i, j, z, noise);
121:
122: // dstBuffer[pos++] = (noise < 0.5) ? SubstanceColorUtilities
123: // .getInterpolatedRGB(c2, c1, 2.0 * noise)
124: // : SubstanceColorUtilities.getInterpolatedRGB(c3, c2,
125: // 2.0 * (noise - .5));
126: dstBuffer[pos++] = SubstanceColorUtilities
127: .getInterpolatedRGB(c3, c1, 2.0 * noise);
128: }
129: }
130: if (toBlur) {
131: ConvolveOp convolve = new ConvolveOp(new Kernel(3, 3,
132: new float[] { .08f, .08f, .08f, .08f, .38f, .08f,
133: .08f, .08f, .08f }), ConvolveOp.EDGE_NO_OP,
134: null);
135: dst = convolve.filter(dst, null);
136: }
137: return dst;
138: }
139: }
|