package imagej.dataset;

import imagej.data.Type;
import imagej.process.Index;

public class DatasetDuplicator
{
	/** public constructor that allows subclass/override mechanism if needed */
	public DatasetDuplicator()
	{
	}
	
	/** creates a Dataset according to given factory's style but whose shape and data values are copied from a given Dataset */ 
	public Dataset duplicateDataset(DatasetFactory factory, Dataset dataset)
	{
		Type type = dataset.getType();
		
		int[] dimensions = dataset.getDimensions();
		
		Dataset newDataset = factory.createDataset(type, dimensions);
		
		int[] position = Index.create(dimensions.length);
		int[] origin = Index.create(dimensions.length);
		int[] span = dimensions;

		// TODO - there are should be faster ways of doing this. do this most general way for now
		
		while (Index.isValid(position, origin, span))
		{
			double value = dataset.getDouble(position);
			
			newDataset.setDouble(position, value);
			
			Index.increment(position, origin, span);
		}
		
		newDataset.setMetaData(dataset.getMetaData());  // TODO - PROBABLY NEED TO CLONE THE METADATA HERE!!!!!!!!!!!
		
		return newDataset;
	}
}

@@ -580,137 +580,320 @@
 %09%0A%09%09
-int%5B%5D position = Index.create(dimensions.length);%0A%09%09int%5B%5D origin = Index.create(dimensions.length);%0A%09%09int%5B%5D span = dimensions;%0A
+// choose the best way to copy to assure no precision loss%0A%09%09if (type.isFloat())%0A%09%09%09copyRealData(dataset, newDataset);%0A%09%09else%0A%09%09%09copyIntegralData(dataset, newDataset);%0A%09%09%0A%09%09newDataset.setMetaData(dataset.getMetaData());  // TODO - PROBABLY NEED TO CLONE THE METADATA HERE!!!!!!!!!!!%0A%09%09%0A%09%09return newDataset;%0A%09%7D
 %0A%09
+%0A
 %09// 
@@ -903,90 +903,418 @@
 O - 
-there are should be faster ways of doing this. do this most general way for now%0A%09%09
+copying in the easiest and slowest way possible - do some speed up by indexing on planes to minimize Dataset subset lookup times%0A%09/** copy data as doubles */%0A%09private void copyRealData(Dataset fromDataset, Dataset toDataset)%0A%09%7B%0A%09%09int%5B%5D dimensions = fromDataset.getDimensions();%0A%09%09%0A%09%09int%5B%5D position = Index.create(dimensions.length);%0A%09%09int%5B%5D origin = Index.create(dimensions.length);%0A%09%09int%5B%5D span = dimensions;%0A
 %0A%09%09w
@@ -1376,25 +1376,29 @@
 ble value = 
-d
+fromD
 ataset.getDo
@@ -1420,19 +1420,18 @@
 %0A%09%09%09%0A%09%09%09
-new
+to
 Dataset.
@@ -1515,145 +1515,628 @@
 %09%7D%0A%09
-%09%0A%09%09newDataset.setMeta
+%7D%0A%0A%09// TODO - copying in the easiest and slowest way possible - do some speed up by indexing on planes to minimize Dataset subset lookup times%0A%09/** copy data as longs */%0A%09private void copyIntegral
 Data(
-d
+D
 ataset
-.getMetaData());  // TODO - PROBABLY NEED TO CLONE THE METADATA HERE!!!!!!!!!!!%0A%09%09%0A%09%09return newDataset;
+ fromDataset, Dataset toDataset)%0A%09%7B%0A%09%09int%5B%5D dimensions = fromDataset.getDimensions();%0A%09%09%0A%09%09int%5B%5D position = Index.create(dimensions.length);%0A%09%09int%5B%5D origin = Index.create(dimensions.length);%0A%09%09int%5B%5D span = dimensions;%0A%0A%09%09while (Index.isValid(position, origin, span))%0A%09%09%7B%0A%09%09%09long value = fromDataset.getLong(position);%0A%09%09%09%0A%09%09%09toDataset.setLong(position, value);%0A%09%09%09%0A%09%09%09Index.increment(position, origin, span);%0A%09%09%7D
 %0A%09%7D%0A

improve so that Dataset copying never loses precision