// loop through the files to get the minimum bounding rectangle.
if (mySourceImageDirectory == null) throw new Exception("Source Image Directory is null, no conversion can occur.");
// look for the index file.
Envelope tempEnvelope = null;
tempEnvelope = readIndex();
if (tempEnvelope == null){
mySourceList.clear();
// Find the envelope of all the files.
EnvelopeBuffer tempEnvelopeBuffer = new EnvelopeBuffer();
notify("Reading World Files", -1, 0,0);
// get the directory listing.
File[] tempFiles = mySourceImageDirectory.listFiles();
for (int i=0; i<tempFiles.length; i++){
if (myStopProcessing) return;
String tempName = tempFiles[i].getName();
String tempUCName = tempName.toUpperCase();
if ((tempUCName.endsWith(".TFW")) || (tempUCName.endsWith(".JGW")) || (tempUCName.endsWith(".TIFW"))){
int tempExtensionLength = 0;
if (tempUCName.endsWith(".TFW")) tempExtensionLength = 4;
if (tempUCName.endsWith(".JGW")) tempExtensionLength = 4;
if (tempUCName.endsWith(".TIFW")) tempExtensionLength = 5;
// look for the corrisponding Image File
String tempImageName = tempName.substring(0, tempName.length()-tempExtensionLength);
File tempImageFile = null;
for (int j=0; j<myExtensions.length; j++){
String tempFileName = mySourceImageDirectory.getAbsolutePath()+File.separatorChar+tempImageName+myExtensions[j];
File tempFile = new File(tempFileName);
if (tempFile.exists()){
tempImageFile = tempFile;
break;
}
}
// if the image file was found then calculate the Envelope for this image.
notify("Reading World File "+tempImageFile.getAbsolutePath(), -1, 0,0);
if (tempImageFile != null){
// read the image file
ImageInformation tempInformation = ImageUtilities.readImageInformation(tempImageFile);
if (tempInformation != null){
// calculate the envelope for this file.
Envelope tempImgEnvelope = ImageUtilities.calculateEnvelope(tempFiles[i], tempInformation);
tempEnvelopeBuffer.expandToInclude(tempImgEnvelope);
// save in the source list
ImageTile tempImageTile = new ImageTile(tempImageFile.getAbsolutePath(), tempImgEnvelope, tempInformation.getImageWidth(), tempInformation.getImageHeight());
mySourceList.add(tempImageTile);
}
}
}
}
// loop through the envelope.
tempEnvelope = tempEnvelopeBuffer.getEnvelope();
// write the XML file for these nodes.
writeIndex(tempEnvelope);
}
if (tempEnvelope != null){
// create the tiles for the first resolution.
int tempCurrentResolution = myResolutions[0];
// create the directory
File tempResolutionDirectory = new File(myDestinationImageDirectory.getAbsolutePath()+File.separatorChar+"r"+tempCurrentResolution);
tempResolutionDirectory.mkdirs();
// how big are the tiles in world coordinates.
double tempWorldWidth = tempCurrentResolution * myMaxWidth;
double tempWorldHeight = tempCurrentResolution * myMaxWidth;
// how many tiles is it going to take at this resolution
int tempXItter = (int) tempEnvelope.getWidth()/(tempCurrentResolution*myMaxWidth) + 1;
int tempYItter = (int) tempEnvelope.getHeight()/(tempCurrentResolution*myMaxWidth) + 1;
// loop through the tiles generating each one.
for (int j=0; j<tempXItter; j++){
for (int k=0; k<tempYItter; k++){
if (myStopProcessing) return;
notify(null, tempCurrentResolution, j, k);
// the file name
String tempFileName = "X"+j+"Y"+k+"."+myOutputType.toLowerCase();;
File tempFile = new File(myDestinationImageDirectory.getAbsolutePath()+File.separatorChar+"r"+tempCurrentResolution+File.separatorChar+tempFileName);
if (!tempFile.exists()){
// determine the world coordinates of the tile.
double tempWorldStartX = tempEnvelope.getMinX() + j*tempWorldWidth;
double tempWorldStartY = tempEnvelope.getMinY() + k*tempWorldHeight;
double tempWorldEndX = tempWorldStartX+tempWorldWidth;
double tempWorldEndY = tempWorldStartY+tempWorldHeight;
Envelope tempTileEnvelope = new Envelope(tempWorldStartX, tempWorldStartY, tempWorldEndX, tempWorldEndY);
System.out.println("TileEnvelope = "+tempTileEnvelope);
// create the buffer.
BufferedImage tempImage = null;
Graphics2D g2d = null;
boolean tempFound = false;
// loop through the images attempting to find the right one.
for (int l=0; l<mySourceList.size(); l++){
if (myStopProcessing) return;
ImageTile tempTile = (ImageTile) mySourceList.get(l);
if (tempTileEnvelope.intersects(tempTile.getEnvelope())){
System.out.println("ImageEnvelope = "+tempTile.getEnvelope());
// draw this tile on the buffer.
Envelope tempNewEnvelope = tempTileEnvelope.getOverlap(tempTile.getEnvelope());
// determine the image coordinates of the source image.
int tempSourceStartX = (int) (((tempNewEnvelope.getMinX()-tempTile.getEnvelope().getMinX())/(tempTile.getEnvelope().getWidth())) * tempTile.getWidth());
int tempSourceStartY = tempTile.getHeight() - (int) (((tempNewEnvelope.getMinY()-tempTile.getEnvelope().getMinY())/(tempTile.getEnvelope().getHeight())) * tempTile.getHeight());
int tempSourceEndX = (int) (((tempNewEnvelope.getMaxX()-tempTile.getEnvelope().getMinX())/(tempTile.getEnvelope().getWidth())) * tempTile.getWidth());
int tempSourceEndY = tempTile.getHeight() - (int) (((tempNewEnvelope.getMaxY()-tempTile.getEnvelope().getMinY())/(tempTile.getEnvelope().getHeight())) * tempTile.getHeight());
// determine the image coordinates of the destination image.
int tempDestinationStartX = (int) (((tempNewEnvelope.getMinX()-tempWorldStartX)/(tempWorldWidth)) * myMaxWidth);
int tempDestinationStartY = myMaxWidth - (int) (((tempNewEnvelope.getMinY()-tempWorldStartY)/(tempWorldHeight)) * myMaxWidth);
int tempDestinationEndX = (int) (((tempNewEnvelope.getMaxX()-tempWorldStartX)/(tempWorldWidth)) * myMaxWidth);
int tempDestinationEndY = myMaxWidth - (int) (((tempNewEnvelope.getMaxY()-tempWorldStartY)/(tempWorldHeight)) * myMaxWidth);
// read the image
ImageInformation tempInformation = null;
for (int m=0; m<myCacheFileNames.size(); m++){
String tempTestFileName = (String) myCacheFileNames.get(m);
if (tempTestFileName.equals(tempTile.getfileName())){
tempInformation = (ImageInformation) myCacheImages.get(m);
}
}
if (tempInformation == null){
System.out.println("Reading "+tempTile.getfileName());
notify(tempTile.getfileName(), tempCurrentResolution, j, k);
tempInformation = ImageUtilities.readImageInformation(new File(tempTile.getfileName()));
if (tempInformation != null){
myCacheFileNames.add(0, tempTile.getfileName());
myCacheImages.add(0, tempInformation);
if (myCacheFileNames.size() > 2){
myCacheFileNames.remove(2);
myCacheImages.remove(2);
}
}
}
if (tempInformation != null){
if (!tempFound){
tempImage = new BufferedImage(myMaxWidth, myMaxWidth, BufferedImage.TYPE_INT_BGR);
g2d = (Graphics2D) tempImage.getGraphics();
g2d.setColor(new Color(255,255,255,255));
g2d.fillRect(0, 0, myMaxWidth, myMaxWidth);
tempFound = true;
}
// draw the image
g2d.drawImage(tempInformation.getImage(),
tempDestinationStartX,
tempDestinationStartY,
tempDestinationEndX,
tempDestinationEndY,
tempSourceStartX,
tempSourceStartY,
tempSourceEndX,
tempSourceEndY,
null);
}
}
}
// write the immage.
if (tempFound){
ImageIO.write(tempImage, myOutputType.toLowerCase(), tempFile);
}
}
}
}
// After doing the first resolution, do all the next ones based on the first one.
for (int i=1; i<myResolutions.length; i++){
// create the tiles for the first resolution.
tempCurrentResolution = myResolutions[i];
// create the directory
tempResolutionDirectory = new File(myDestinationImageDirectory.getAbsolutePath()+File.separatorChar+"r"+tempCurrentResolution);
tempResolutionDirectory.mkdirs();
// how big are the tiles in world coordinates.
tempWorldWidth = tempCurrentResolution * myMaxWidth;
tempWorldHeight = tempCurrentResolution * myMaxWidth;
// how many tiles is it going to take at this resolution
tempXItter = (int) tempEnvelope.getWidth()/(tempCurrentResolution*myMaxWidth) + 1;
tempYItter = (int) tempEnvelope.getHeight()/(tempCurrentResolution*myMaxWidth) + 1;
// loop through the tiles generating each one.
for (int j=0; j<tempXItter; j++){
for (int k=0; k<tempYItter; k++){
if (myStopProcessing) return;
notify(null, tempCurrentResolution, j, k);
// the file name
String tempFileName = "X"+j+"Y"+k+"."+myOutputType.toLowerCase();;
File tempFile = new File(myDestinationImageDirectory.getAbsolutePath()+File.separatorChar+"r"+tempCurrentResolution+File.separatorChar+tempFileName);
if (!tempFile.exists()){
// determine the world coordinates of the tile.
double tempWorldStartX = tempEnvelope.getMinX() + j*tempWorldWidth;
double tempWorldStartY = tempEnvelope.getMinY() + k*tempWorldHeight;
double tempWorldEndX = tempWorldStartX+tempWorldWidth;
double tempWorldEndY = tempWorldStartY+tempWorldHeight;
Envelope tempTileEnvelope = new Envelope(tempWorldStartX, tempWorldStartY, tempWorldEndX, tempWorldEndY);
System.out.println("TileEnvelope = "+tempTileEnvelope);
// create the buffer.
BufferedImage tempImage = null;
Graphics2D g2d = null;
boolean tempFound = false;
// find the tiles from the previous resolution for this resoltuion.
int tempPreviousResolution = myResolutions[i-1];
double tempPreviousWorldWidth = myMaxWidth*tempPreviousResolution;
double tempPreviousWorldHeight = myMaxWidth*tempPreviousResolution;
int tempPreviousResolutionStartX = (int) ((tempWorldStartX - tempEnvelope.getMinX())/(tempPreviousWorldWidth));
int tempPreviousResolutionEndX = (int) ((tempWorldEndX - tempEnvelope.getMinX())/(tempPreviousWorldWidth));
int tempPreviousResolutionStartY = (int) ((tempWorldStartY - tempEnvelope.getMinY())/(tempPreviousWorldHeight));
int tempPreviousResolutionEndY = (int) ((tempWorldEndY - tempEnvelope.getMinY())/(tempPreviousWorldHeight));
// loop through the x and y tiles drawing them on the current set
for (int tx = tempPreviousResolutionStartX; tx<tempPreviousResolutionEndX+1; tx++){
for (int ty = tempPreviousResolutionStartY; ty<tempPreviousResolutionEndY+1; ty++){
if (myStopProcessing) return;
// Does this tile exist.
String tempPreviousTileFileName = "X"+tx+"Y"+ty+"."+myOutputType.toLowerCase();;
File tempPreviousFile = new File(myDestinationImageDirectory.getAbsolutePath()+File.separatorChar+"r"+tempPreviousResolution+File.separatorChar+tempPreviousTileFileName);
if (tempPreviousFile.exists()){
// determine the world coordinates of the source tile.
double tempTileMinX = tempEnvelope.getMinX() + (tx * tempPreviousWorldWidth);
double tempTileMaxX = tempEnvelope.getMinX() + ((tx+1) * tempPreviousWorldWidth);
double tempTileMinY = tempEnvelope.getMinY() + (ty * tempPreviousWorldHeight);
double tempTileMaxY = tempEnvelope.getMinY() + ((ty+1) * tempPreviousWorldHeight);
Envelope tempPreviousTileEnvelope = new Envelope(tempTileMinX, tempTileMinY, tempTileMaxX, tempTileMaxY);
Envelope tempNewEnvelope = tempTileEnvelope.getOverlap(tempPreviousTileEnvelope);
// determine the image coordinates of the source image.
if (tempNewEnvelope != null){
int tempSourceStartX = (int) (((tempNewEnvelope.getMinX()-tempPreviousTileEnvelope.getMinX())/(tempPreviousTileEnvelope.getWidth())) * myMaxWidth);
int tempSourceStartY = myMaxWidth - (int) (((tempNewEnvelope.getMinY()-tempPreviousTileEnvelope.getMinY())/(tempPreviousTileEnvelope.getHeight())) * myMaxWidth);
int tempSourceEndX = (int) (((tempNewEnvelope.getMaxX()-tempPreviousTileEnvelope.getMinX())/(tempPreviousTileEnvelope.getWidth())) * myMaxWidth);
int tempSourceEndY = myMaxWidth - (int) (((tempNewEnvelope.getMaxY()-tempPreviousTileEnvelope.getMinY())/(tempPreviousTileEnvelope.getHeight())) * myMaxWidth);
// determine the image coordinates of the destination image.
int tempDestinationStartX = (int) (((tempNewEnvelope.getMinX()-tempWorldStartX)/(tempWorldWidth)) * myMaxWidth);
int tempDestinationStartY = myMaxWidth - (int) (((tempNewEnvelope.getMinY()-tempWorldStartY)/(tempWorldHeight)) * myMaxWidth);
int tempDestinationEndX = (int) (((tempNewEnvelope.getMaxX()-tempWorldStartX)/(tempWorldWidth)) * myMaxWidth);
int tempDestinationEndY = myMaxWidth - (int) (((tempNewEnvelope.getMaxY()-tempWorldStartY)/(tempWorldHeight)) * myMaxWidth);
// read the image
ImageInformation tempInformation = null;
System.out.println("Reading "+tempPreviousFile.getAbsolutePath());
notify(tempPreviousFile.getAbsolutePath(), tempCurrentResolution, j, k);