Search in sources :

Example 6 with JEPWrapper

use of org.jwildfire.base.mathparser.JEPWrapper in project JWildfire by thargor6.

the class PerlinNoiseCreator method fillImage.

@Override
protected void fillImage(SimpleImage res) {
    if (this.seed != 0)
        Tools.srand123(this.seed);
    JEPWrapper parser = null;
    Node node = null;
    if (transform) {
        parser = new JEPWrapper();
        parser.addVariable("x", 0.0);
        node = parser.parse(transformation);
    }
    switch(colorMode) {
        case RANDOM:
            cA.randomize();
            cB.randomize();
            break;
        case CUSTOM:
            cA.setR(lowColor.getRed());
            cA.setG(lowColor.getGreen());
            cA.setB(lowColor.getBlue());
            cB.setR(highColor.getRed());
            cB.setG(highColor.getGreen());
            cB.setB(highColor.getBlue());
            break;
        case GREY:
            double dA = Tools.drand();
            cA.setR(dA);
            cA.setG(dA);
            cA.setB(dA);
            double dB = Tools.drand();
            cB.setR(dB);
            cB.setG(dB);
            cB.setB(dB);
            break;
    }
    initShape();
    int width = res.getImageWidth();
    int height = res.getImageHeight();
    for (int i = 0; i < height; i++) {
        double y = (double) i / (double) height;
        for (int j = 0; j < width; j++) {
            double x = (double) j / (double) width;
            double freq = initialFrequency;
            double alphaInt = 1.0;
            double grayValue = 0.0;
            for (int l = 0; l < octaves; l++) {
                double noiseValue = noise(x * freq, y * freq, 0.0);
                if (transform) {
                    parser.setVarValue("x", noiseValue);
                    noiseValue = (Double) parser.evaluate(node);
                }
                grayValue += alphaInt * noiseValue;
                freq *= frequencyMultiplier;
                alphaInt *= persistence;
            }
            RGBVal color = getColor(grayValue);
            fSetPixel(res, j, i, color);
        }
    }
}
Also used : JEPWrapper(org.jwildfire.base.mathparser.JEPWrapper) Node(org.nfunk.jep.Node)

Example 7 with JEPWrapper

use of org.jwildfire.base.mathparser.JEPWrapper in project JWildfire by thargor6.

the class FormulaExplorerController method calculate.

public void calculate() throws Exception {
    final int MAX_FORMULA_COUNT = 3;
    StringBuilder sb = new StringBuilder();
    double xmin = Double.parseDouble(xMinREd.getText());
    double xmax = Double.parseDouble(xMaxREd.getText());
    double xstep;
    int xCount = Integer.parseInt(xCountREd.getText());
    if (xCount < 2)
        xCount = 2;
    xstep = (xmax - xmin) / (double) (xCount - 1);
    JEPWrapper parser = new JEPWrapper();
    parser.addVariable("x", 0.0);
    Node[] fNode = new Node[MAX_FORMULA_COUNT];
    int fCount = 0;
    if (formula1REd.getText() != null && formula1REd.getText().length() > 0)
        fNode[fCount++] = parser.parse(formula1REd.getText());
    if (formula2REd.getText() != null && formula2REd.getText().length() > 0)
        fNode[fCount++] = parser.parse(formula2REd.getText());
    if (formula3REd.getText() != null && formula3REd.getText().length() > 0)
        fNode[fCount++] = parser.parse(formula3REd.getText());
    if (fCount == 0)
        fNode[fCount++] = parser.parse("0");
    double[] x = new double[xCount];
    double[][] y = new double[fCount][xCount];
    double xc = xmin;
    for (int i = 0; i < xCount; i++) {
        x[i] = xc;
        parser.setVarValue("x", xc);
        sb.append("  f(" + Tools.doubleToString(x[i]) + ")  \t");
        for (int j = 0; j < fCount; j++) {
            y[j][i] = (Double) parser.evaluate(fNode[j]);
            sb.append(Tools.doubleToString(y[j][i]) + " \t");
        }
        sb.append("\n");
        xc += xstep;
    }
    formulaPanel.setData(x, y);
    formulaPanel.repaint();
    valuesTextArea.setText(sb.toString());
    valuesTextArea.setCaretPosition(0);
}
Also used : JEPWrapper(org.jwildfire.base.mathparser.JEPWrapper) Node(org.nfunk.jep.Node)

Example 8 with JEPWrapper

use of org.jwildfire.base.mathparser.JEPWrapper in project JWildfire by thargor6.

the class FormulaComposeTransformer method performPixelTransformation.

@Override
protected void performPixelTransformation(WFImage pImg) {
    SimpleImage bgImg = (SimpleImage) pImg;
    SimpleImage fgImg = (foregroundImage != null) ? foregroundImage : foreground.getImage();
    if (fgImg == bgImg)
        fgImg = fgImg.clone();
    Pixel fgPixel = new Pixel();
    Pixel bgPixel = new Pixel();
    // calculate left and top edge
    int fgLeft, fgTop;
    int bgWidth = bgImg.getImageWidth();
    int bgHeight = bgImg.getImageHeight();
    int fgWidth = fgImg.getImageWidth();
    int fgHeight = fgImg.getImageHeight();
    if (hAlign == HAlignment.CENTRE) {
        fgLeft = (bgWidth - fgWidth) / 2;
    } else if (hAlign == HAlignment.LEFT) {
        fgLeft = 0;
    } else if (hAlign == HAlignment.RIGHT) {
        fgLeft = bgWidth - fgWidth;
    } else {
        fgLeft = this.left;
    }
    if (vAlign == VAlignment.CENTRE) {
        fgTop = (bgHeight - fgHeight) / 2;
    } else if (vAlign == VAlignment.TOP) {
        fgTop = 0;
    } else if (vAlign == VAlignment.BOTTOM) {
        fgTop = bgHeight - fgHeight;
    } else {
        fgTop = this.top;
    }
    // Initialize the parser
    JEPWrapper parser = new JEPWrapper();
    parser.addVariable("fgR", 0.0);
    parser.addVariable("fgG", 0.0);
    parser.addVariable("fgB", 0.0);
    parser.addVariable("fgWidth", (double) fgWidth);
    parser.addVariable("fgHeight", (double) fgHeight);
    parser.addVariable("bgR", 0.0);
    parser.addVariable("bgG", 0.0);
    parser.addVariable("bgB", 0.0);
    parser.addVariable("bgWidth", (double) bgWidth);
    parser.addVariable("bgHeight", (double) bgHeight);
    parser.addVariable("fgLeft", (double) fgLeft);
    parser.addVariable("fgTop", (double) fgTop);
    parser.addVariable("fgX", 0.0);
    parser.addVariable("fgY", 0.0);
    Node redNode = parser.parse(formula1Red);
    Node greenNode = parser.parse(formula2Green);
    Node blueNode = parser.parse(formula3Blue);
    // compose the images
    for (int i = 0; i < fgHeight; i++) {
        int top = fgTop + i;
        if (top >= 0 && top < bgHeight) {
            parser.setVarValue("fgY", (double) i / 255.0);
            for (int j = 0; j < fgWidth; j++) {
                int left = fgLeft + j;
                if (left >= 0 && left < bgWidth) {
                    parser.setVarValue("fgX", (double) j / 255.0);
                    bgPixel.setARGBValue(bgImg.getARGBValue(left, top));
                    fgPixel.setARGBValue(fgImg.getARGBValue(j, i));
                    parser.setVarValue("bgR", (double) bgPixel.r / 255.0);
                    parser.setVarValue("bgG", (double) bgPixel.g / 255.0);
                    parser.setVarValue("bgB", (double) bgPixel.b / 255.0);
                    parser.setVarValue("fgR", (double) fgPixel.r / 255.0);
                    parser.setVarValue("fgG", (double) fgPixel.g / 255.0);
                    parser.setVarValue("fgB", (double) fgPixel.b / 255.0);
                    // TODO Genlock: z. B. Testen, ob Intensitat 0 oder grober 0
                    // genlockFormula, genlockOperator (gleich, grober), genlockRefValue
                    bgPixel.r = Tools.roundColor((Double) parser.evaluate(redNode) * 255.0);
                    bgPixel.g = Tools.roundColor((Double) parser.evaluate(greenNode) * 255.0);
                    bgPixel.b = Tools.roundColor((Double) parser.evaluate(blueNode) * 255.0);
                    bgImg.setRGB(left, top, bgPixel);
                }
            }
        }
    }
}
Also used : JEPWrapper(org.jwildfire.base.mathparser.JEPWrapper) SimpleImage(org.jwildfire.image.SimpleImage) Node(org.nfunk.jep.Node) Pixel(org.jwildfire.image.Pixel)

Aggregations

JEPWrapper (org.jwildfire.base.mathparser.JEPWrapper)8 Node (org.nfunk.jep.Node)8 Pixel (org.jwildfire.image.Pixel)2 SimpleImage (org.jwildfire.image.SimpleImage)2