Search in sources :

Example 1 with RGBPalette

use of org.jwildfire.create.tina.palette.RGBPalette in project JWildfire by thargor6.

the class WikimediaCommonsRandomFlameGenerator method obtainImage.

protected ImageData obtainImage() {
    try {
        String url = "http://commons.wikimedia.org/wiki/Special:Random/File";
        int minSize = 16;
        int maxSize = 16000;
        byte[] htmlData = downloadRessource(url);
        String html = new String(htmlData);
        String imgUrl = getImgUrl(html);
        String pageUrl = getPageUrl(html);
        if (imgUrl != null && pageUrl != null && isValidImgUrl(imgUrl)) {
            byte[] imgData = downloadRessource(imgUrl);
            String fileExt = RessourceManager.guessImageExtension(imgData);
            File f = File.createTempFile("tmp", "." + fileExt);
            f.deleteOnExit();
            Tools.writeFile(f.getAbsolutePath(), imgData);
            WFImage img = new ImageReader(new JLabel()).loadImage(f.getAbsolutePath());
            if (img.getImageWidth() >= minSize && img.getImageWidth() <= maxSize && img.getImageHeight() >= minSize && img.getImageHeight() <= maxSize) {
                int hashcode = RessourceManager.calcHashCode(imgData);
                SimpleImage wfImg = (SimpleImage) RessourceManager.getImage(hashcode, imgData);
                RGBPalette gradient = new MedianCutQuantizer().createPalette(wfImg);
                return new ImageData(pageUrl, imgUrl, imgData, gradient);
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}
Also used : WFImage(org.jwildfire.image.WFImage) RGBPalette(org.jwildfire.create.tina.palette.RGBPalette) SimpleImage(org.jwildfire.image.SimpleImage) JLabel(javax.swing.JLabel) ImageReader(org.jwildfire.io.ImageReader) File(java.io.File) MedianCutQuantizer(org.jwildfire.create.tina.palette.MedianCutQuantizer)

Example 2 with RGBPalette

use of org.jwildfire.create.tina.palette.RGBPalette in project JWildfire by thargor6.

the class RandomGradientGenerator method generatePalette.

public static final RGBPalette generatePalette(List<RGBColor> pKeyFrames, boolean pFadeColors, boolean pUniformWidth) {
    RGBPalette res = new RGBPalette();
    if (pKeyFrames.size() == 1) {
        RGBColor c = pKeyFrames.get(0);
        for (int i = 0; i < RGBPalette.PALETTE_SIZE; i++) {
            res.addColor(c.getRed(), c.getGreen(), c.getBlue());
        }
    } else {
        // Compute uniform sizes for each color
        int[] kfCount = new int[pKeyFrames.size()];
        int sum1 = 0;
        double sum2 = 0.0;
        int n = pKeyFrames.size() - (pFadeColors ? 1 : 0);
        double idxScl = (double) (RGBPalette.PALETTE_SIZE) / (double) (n);
        for (int i = 0; i < n; i++) {
            kfCount[i] = (int) idxScl;
            sum1 += kfCount[i];
            sum2 += idxScl;
            if (sum2 - sum1 > 1) {
                kfCount[i]++;
                sum1++;
            }
        }
        kfCount[n - 1] += RGBPalette.PALETTE_SIZE - sum1;
        // Randomize sizes
        if (!pUniformWidth && idxScl > 3) {
            for (int i = 0; i < n; i++) {
                int j = (int) (Math.random() * n);
                int min = (kfCount[i] < kfCount[j] ? kfCount[i] : kfCount[j]);
                int r = (int) (Math.random() * min - min / 2);
                kfCount[i] += r;
                kfCount[j] -= r;
            }
        }
        // Allocate colors
        int j = 0;
        int lIdx = 0;
        for (int i = 0; i < RGBPalette.PALETTE_SIZE; i++) {
            RGBColor lColor = pKeyFrames.get(lIdx);
            int r, g, b;
            if (pFadeColors) {
                double relX = (double) (j) / (double) kfCount[lIdx];
                RGBColor rColor = pKeyFrames.get(lIdx + 1);
                r = Tools.roundColor((double) lColor.getRed() + ((double) (rColor.getRed() - lColor.getRed())) * relX);
                g = Tools.roundColor((double) lColor.getGreen() + ((double) (rColor.getGreen() - lColor.getGreen())) * relX);
                b = Tools.roundColor((double) lColor.getBlue() + ((double) (rColor.getBlue() - lColor.getBlue())) * relX);
            } else {
                r = lColor.getRed();
                g = lColor.getGreen();
                b = lColor.getBlue();
            }
            res.addColor(r, g, b);
            // System.out.println(i + ": " + r + " " + g + " " + b);
            j++;
            if (j >= kfCount[lIdx]) {
                j = 0;
                if (lIdx < n - 1)
                    lIdx++;
            }
        }
    }
    return res;
}
Also used : RGBPalette(org.jwildfire.create.tina.palette.RGBPalette) RGBColor(org.jwildfire.create.tina.palette.RGBColor)

Example 3 with RGBPalette

use of org.jwildfire.create.tina.palette.RGBPalette in project JWildfire by thargor6.

the class RandomFlameGeneratorSampler method createSample.

public RandomFlameGeneratorSample createSample() {
    RenderInfo info = new RenderInfo(imageWidth, imageHeight, RenderMode.PREVIEW);
    Flame bestFlame = null;
    int bgRed = prefs.getTinaRandomBatchBGColorRed();
    int bgGreen = prefs.getTinaRandomBatchBGColorGreen();
    int bgBlue = prefs.getTinaRandomBatchBGColorBlue();
    RandomFlameGeneratorState randGenState = randGen.initState(prefs, randGradientGen);
    double bestCoverage = 0.0;
    for (int j = 0; j < quality.getMaxSamples(); j++) {
        // create flame
        Flame flame;
        try {
            flame = randGen.createFlame(prefs, randGenState);
            if (randGen.supportsSymmetry()) {
                randSymmGen.addSymmetry(flame);
            }
        } catch (Exception ex) {
            flame = new Flame();
            ex.printStackTrace();
        }
        flame.setWidth(imageWidth);
        flame.setHeight(imageHeight);
        flame.setPixelsPerUnit(10);
        for (Layer layer : flame.getLayers()) {
            RGBPalette palette = randGradientGen.generatePalette(paletteSize, fadePaletteColors, uniformWidth);
            layer.setPalette(palette);
        }
        flame = randGen.postProcessFlameBeforeRendering(randGenState, flame);
        // render it
        flame.setSampleDensity(25);
        RenderedFlame renderedFlame;
        flame.applyFastOversamplingSettings();
        try {
            FlameRenderer renderer = new FlameRenderer(flame, prefs, false, true);
            renderedFlame = renderer.renderFlame(info);
            flame = randGen.postProcessFlameAfterRendering(randGenState, flame);
            if (j == quality.getMaxSamples() - 1) {
                renderedFlame = new FlameRenderer(bestFlame, prefs, false, true).renderFlame(info);
                return new RandomFlameGeneratorSample(bestFlame, renderedFlame.getImage());
            } else {
                double fCoverage = calculateCoverage(renderedFlame.getImage(), bgRed, bgGreen, bgBlue, randGen.isUseFilter(randGenState));
                if (fCoverage >= quality.getCoverage() && fCoverage < randGen.getMaxCoverage()) {
                    return new RandomFlameGeneratorSample(flame, renderedFlame.getImage());
                } else {
                    if (bestFlame == null || fCoverage > bestCoverage) {
                        bestFlame = flame;
                        bestCoverage = fCoverage;
                    }
                }
            }
        } finally {
            flame.applyDefaultOversamplingSettings();
        }
    }
    throw new IllegalStateException();
}
Also used : RGBPalette(org.jwildfire.create.tina.palette.RGBPalette) FlameRenderer(org.jwildfire.create.tina.render.FlameRenderer) RenderInfo(org.jwildfire.create.tina.render.RenderInfo) Layer(org.jwildfire.create.tina.base.Layer) RenderedFlame(org.jwildfire.create.tina.render.RenderedFlame) Flame(org.jwildfire.create.tina.base.Flame) RenderedFlame(org.jwildfire.create.tina.render.RenderedFlame)

Example 4 with RGBPalette

use of org.jwildfire.create.tina.palette.RGBPalette in project JWildfire by thargor6.

the class ColorMapRandomFlameGenerator method obtainImage.

@Override
protected ImageData obtainImage() {
    try {
        int minSize = 16;
        int maxSize = 16000;
        File file = getRandomFile();
        if (file != null) {
            byte[] imgData = Tools.readFile(file.getAbsolutePath());
            WFImage img = new ImageReader(new JLabel()).loadImage(file.getAbsolutePath());
            if (img.getImageWidth() >= minSize && img.getImageWidth() <= maxSize && img.getImageHeight() >= minSize && img.getImageHeight() <= maxSize) {
                int hashcode = RessourceManager.calcHashCode(imgData);
                SimpleImage wfImg = (SimpleImage) RessourceManager.getImage(hashcode, imgData);
                RGBPalette gradient = new MedianCutQuantizer().createPalette(wfImg);
                return new ImageData(null, file.getName(), imgData, gradient);
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}
Also used : WFImage(org.jwildfire.image.WFImage) RGBPalette(org.jwildfire.create.tina.palette.RGBPalette) SimpleImage(org.jwildfire.image.SimpleImage) JLabel(javax.swing.JLabel) ImageReader(org.jwildfire.io.ImageReader) File(java.io.File) MedianCutQuantizer(org.jwildfire.create.tina.palette.MedianCutQuantizer)

Example 5 with RGBPalette

use of org.jwildfire.create.tina.palette.RGBPalette in project JWildfire by thargor6.

the class BlackAndWhiteRandomFlameGenerator method postProcessFlameBeforeRendering.

@Override
protected Flame postProcessFlameBeforeRendering(RandomFlameGeneratorState pState, Flame pFlame) {
    if (Math.random() < 0.42) {
        if (Math.random() < 0.5) {
            pFlame.setBgColorType(BGColorType.SINGLE_COLOR);
            pFlame.setBgColorRed(0);
            pFlame.setBgColorGreen(0);
            pFlame.setBgColorBlue(0);
        } else {
            pFlame.setBgColorType(BGColorType.GRADIENT_2X2);
            pFlame.setBgColorRed((int) (Math.random() * 64));
            pFlame.setBgColorGreen((int) (Math.random() * 64));
            pFlame.setBgColorBlue((int) (Math.random() * 64));
        }
        List<RGBColor> colors = new ArrayList<RGBColor>();
        colors.add(new RGBColor(255, 255, 255));
        RGBPalette gradient = RandomGradientGenerator.generatePalette(colors, true, true);
        for (Layer layer : pFlame.getLayers()) {
            layer.setPalette(gradient);
        }
    } else {
        if (Math.random() < 0.5) {
            pFlame.setBgColorType(BGColorType.SINGLE_COLOR);
            pFlame.setBgColorRed(255);
            pFlame.setBgColorGreen(255);
            pFlame.setBgColorBlue(255);
        } else {
            pFlame.setBgColorType(BGColorType.GRADIENT_2X2);
            pFlame.setBgColorRed((int) (255 - Math.random() * 64));
            pFlame.setBgColorGreen((int) (255 - Math.random() * 64));
            pFlame.setBgColorBlue((int) (255 - Math.random() * 64));
        }
        List<RGBColor> colors = new ArrayList<RGBColor>();
        colors.add(new RGBColor(0, 0, 0));
        RGBPalette gradient = RandomGradientGenerator.generatePalette(colors, true, true);
        for (Layer layer : pFlame.getLayers()) {
            layer.setPalette(gradient);
        }
    }
    pFlame.setGamma(0.45 + Math.random() * 1.5);
    pFlame.setGammaThreshold(0.2 * Math.random());
    pFlame.setSaturation(1.0);
    pFlame.setBrightness(3.0 + Math.random() * 2.0);
    pFlame.setContrast(2.5);
    pFlame.setBGTransparency(false);
    return pFlame;
}
Also used : RGBPalette(org.jwildfire.create.tina.palette.RGBPalette) RGBColor(org.jwildfire.create.tina.palette.RGBColor) ArrayList(java.util.ArrayList) Layer(org.jwildfire.create.tina.base.Layer)

Aggregations

RGBPalette (org.jwildfire.create.tina.palette.RGBPalette)29 ArrayList (java.util.ArrayList)12 Layer (org.jwildfire.create.tina.base.Layer)9 File (java.io.File)8 Flame (org.jwildfire.create.tina.base.Flame)8 RGBColor (org.jwildfire.create.tina.palette.RGBColor)5 RenderedFlame (org.jwildfire.create.tina.render.RenderedFlame)5 SimpleImage (org.jwildfire.image.SimpleImage)5 Field (java.lang.reflect.Field)4 JFileChooser (javax.swing.JFileChooser)4 XForm (org.jwildfire.create.tina.base.XForm)4 ImageReader (org.jwildfire.io.ImageReader)4 List (java.util.List)3 MedianCutQuantizer (org.jwildfire.create.tina.palette.MedianCutQuantizer)3 HashMap (java.util.HashMap)2 JLabel (javax.swing.JLabel)2 Test (org.junit.Test)2 QualityProfile (org.jwildfire.base.QualityProfile)2 ResolutionProfile (org.jwildfire.base.ResolutionProfile)2 MotionCurve (org.jwildfire.create.tina.base.motion.MotionCurve)2