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;
}
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;
}
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();
}
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;
}
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;
}
Aggregations