use of org.jwildfire.image.WFImage 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.image.WFImage 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.image.WFImage in project JWildfire by thargor6.
the class FlameParamsList method setRessource.
public boolean setRessource(String pName, byte[] pValue) {
for (int i = 1; i <= size(); i++) {
String flameIndexStr = getFlameIndexStr(i);
if ((RESSOURCE_FLAME + flameIndexStr).equalsIgnoreCase(pName)) {
String flameXML = pValue != null ? new String(pValue) : "";
if (flameXML.length() > 0) {
parseFlame(flameXML);
get(i - 1).setFlameXML(flameXML);
} else {
get(i - 1).setFlameXML(null);
}
return true;
} else if ((RESSOURCE_FLAME_PARAM1 + flameIndexStr).equalsIgnoreCase(pName)) {
get(i - 1).setFlameParam1(pValue != null ? new String(pValue) : "");
return true;
} else if ((RESSOURCE_FLAME_PARAM1_MAP + flameIndexStr).equalsIgnoreCase(pName)) {
String filename = pValue != null ? new String(pValue) : "";
SimpleImage map = null;
if (filename.length() > 0) {
try {
WFImage img = RessourceManager.getImage(filename);
if (img.getImageWidth() < 8 || img.getImageHeight() < 8 || !(img instanceof SimpleImage)) {
throw new Exception("Invalid param map");
}
map = (SimpleImage) img;
} catch (Exception ex) {
ex.printStackTrace();
}
}
get(i - 1).setFlameParamMap1Filename(map != null ? filename : null);
return true;
} else if ((RESSOURCE_FLAME_PARAM2 + flameIndexStr).equalsIgnoreCase(pName)) {
get(i - 1).setFlameParam2(pValue != null ? new String(pValue) : "");
return true;
} else if ((RESSOURCE_FLAME_PARAM2_MAP + flameIndexStr).equalsIgnoreCase(pName)) {
String filename = pValue != null ? new String(pValue) : "";
SimpleImage map = null;
if (filename.length() > 0) {
try {
WFImage img = RessourceManager.getImage(filename);
if (img.getImageWidth() < 8 || img.getImageHeight() < 8 || !(img instanceof SimpleImage)) {
throw new Exception("Invalid param map");
}
map = (SimpleImage) img;
} catch (Exception ex) {
ex.printStackTrace();
}
}
get(i - 1).setFlameParamMap2Filename(map != null ? filename : null);
return true;
} else if ((RESSOURCE_FLAME_PARAM3 + flameIndexStr).equalsIgnoreCase(pName)) {
get(i - 1).setFlameParam3(pValue != null ? new String(pValue) : "");
return true;
} else if ((RESSOURCE_FLAME_PARAM3_MAP + flameIndexStr).equalsIgnoreCase(pName)) {
String filename = pValue != null ? new String(pValue) : "";
SimpleImage map = null;
if (filename.length() > 0) {
try {
WFImage img = RessourceManager.getImage(filename);
if (img.getImageWidth() < 8 || img.getImageHeight() < 8 || !(img instanceof SimpleImage)) {
throw new Exception("Invalid param map");
}
map = (SimpleImage) img;
} catch (Exception ex) {
ex.printStackTrace();
}
}
get(i - 1).setFlameParamMap3Filename(map != null ? filename : null);
return true;
}
}
return false;
}
use of org.jwildfire.image.WFImage in project JWildfire by thargor6.
the class RessourceManager method getImage.
public static WFImage getImage(int pHashCode, byte[] pImageData) throws Exception {
Integer key = Integer.valueOf(pHashCode);
WFImage res = imageMapByHash.get(key);
if (res == null) {
String fileExt = guessImageExtension(pImageData);
File f = File.createTempFile("tmp", "." + fileExt);
f.deleteOnExit();
Tools.writeFile(f.getAbsolutePath(), pImageData);
if ("hdr".equalsIgnoreCase(fileExt)) {
res = new ImageReader(new JLabel()).loadHDRImage(f.getAbsolutePath());
} else {
res = new ImageReader(new JLabel()).loadImage(f.getAbsolutePath());
}
imageMapByHash.put(key, res);
}
return res;
}
use of org.jwildfire.image.WFImage in project JWildfire by thargor6.
the class RessourceManager method getImage.
public static WFImage getImage(String pFilename) throws Exception {
if (pFilename == null || pFilename.length() == 0) {
throw new IllegalStateException();
}
WFImage res = imageMapByName.get(pFilename);
if (res == null) {
if (!new File(pFilename).exists()) {
throw new FileNotFoundException(pFilename);
}
String fileExt = null;
{
int p = pFilename.lastIndexOf(".");
if (p >= 0 && p < pFilename.length() - 2) {
fileExt = pFilename.substring(p + 1, pFilename.length());
}
}
if ("hdr".equalsIgnoreCase(fileExt)) {
res = new ImageReader(new JLabel()).loadHDRImage(pFilename);
} else {
res = new ImageReader(new JLabel()).loadImage(pFilename);
}
imageMapByName.put(pFilename, res);
}
return res;
}
Aggregations