use of org.gdal.gdal.ColorTable in project imageio-ext by geosolutions-it.
the class GDALCommonIIOImageMetadata method setMembers.
/**
* Set all the fields of the common metadata object.
*
* @param dataset
* the <code>Dataset</code> which will be used for the
* initialization
* @return <code>true</code> if the initialization was successfully
* completed. <code>false</code> if some field wasn't properly
* initialized
*/
private boolean setMembers(Dataset dataset) {
// Retrieving raster properties
setWidth(dataset.getRasterXSize());
setHeight(dataset.getRasterYSize());
// Retrieving block size
final int[] xBlockSize = new int[1];
final int[] yBlockSize = new int[1];
// Remember: RasterBand numeration starts from 1
Band rband = null;
try {
rband = dataset.GetRasterBand(1);
rband.GetBlockSize(xBlockSize, yBlockSize);
final int tileHeight = yBlockSize[0];
final int tileWidth = xBlockSize[0];
setTileHeight(tileHeight);
setTileWidth(tileWidth);
if (((long) tileHeight) * ((long) tileWidth) > Integer.MAX_VALUE)
performTileSizeTuning(dataset);
// /////////////////////////////////////////////////////////////////
//
// Getting dataset main properties
//
// /////////////////////////////////////////////////////////////////
final int numBands = dataset.getRasterCount();
setNumBands(numBands);
if (numBands <= 0)
return false;
// final int xsize = dataset.getRasterXSize();
// final int ysize = dataset.getRasterYSize();
// If the image is very big, its size expressed as the number of
// bytes needed to store pixels, may be a negative number
final int tileSize = tileWidth * tileHeight * numBands * (gdal.GetDataTypeSize(rband.getDataType()) / 8);
// bands variables
final int[] banks = new int[numBands];
final int[] offsetsR = new int[numBands];
final Double[] noDataValues = new Double[numBands];
final Double[] scales = new Double[numBands];
final Double[] offsets = new Double[numBands];
final Double[] minimums = new Double[numBands];
final Double[] maximums = new Double[numBands];
final int[] numOverviews = new int[numBands];
final int[] colorInterpretations = new int[numBands];
int buf_type = 0;
Band pBand = null;
// scanning bands
final Double[] tempD = new Double[1];
final int[] bandsOffset = new int[numBands];
for (int band = 0; band < numBands; band++) {
/* Bands are not 0-base indexed, so we must add 1 */
try {
pBand = dataset.GetRasterBand(band + 1);
buf_type = pBand.getDataType();
banks[band] = band;
offsetsR[band] = 0;
pBand.GetNoDataValue(tempD);
noDataValues[band] = tempD[0];
pBand.GetOffset(tempD);
offsets[band] = tempD[0];
pBand.GetScale(tempD);
scales[band] = tempD[0];
pBand.GetMinimum(tempD);
minimums[band] = tempD[0];
pBand.GetMaximum(tempD);
maximums[band] = tempD[0];
colorInterpretations[band] = pBand.GetRasterColorInterpretation();
numOverviews[band] = pBand.GetOverviewCount();
bandsOffset[band] = band;
} finally {
if (pBand != null) {
try {
// Closing the band
pBand.delete();
} catch (Throwable e) {
if (LOGGER.isLoggable(Level.FINEST))
LOGGER.log(Level.FINEST, e.getLocalizedMessage(), e);
}
}
}
}
setNoDataValues(noDataValues);
setScales(scales);
setOffsets(offsets);
setMinimums(minimums);
setMaximums(maximums);
setNumOverviews(numOverviews);
setColorInterpretations(colorInterpretations);
// /////////////////////////////////////////////////////////////////
//
// Variable used to specify the data type for the storing samples
// of the SampleModel
//
// /////////////////////////////////////////////////////////////////
int buffer_type = 0;
if (buf_type == gdalconstConstants.GDT_Byte)
buffer_type = DataBuffer.TYPE_BYTE;
else if (buf_type == gdalconstConstants.GDT_UInt16)
buffer_type = DataBuffer.TYPE_USHORT;
else if (buf_type == gdalconstConstants.GDT_Int16)
buffer_type = DataBuffer.TYPE_SHORT;
else if ((buf_type == gdalconstConstants.GDT_Int32) || (buf_type == gdalconstConstants.GDT_UInt32))
buffer_type = DataBuffer.TYPE_INT;
else if (buf_type == gdalconstConstants.GDT_Float32)
buffer_type = DataBuffer.TYPE_FLOAT;
else if (buf_type == gdalconstConstants.GDT_Float64)
buffer_type = DataBuffer.TYPE_DOUBLE;
else
return false;
// //
if (tileSize < 0)
setSampleModel(new BandedSampleModel(buffer_type, tileWidth, tileHeight, tileWidth, banks, offsetsR));
else
setSampleModel(new PixelInterleavedSampleModel(buffer_type, tileWidth, tileHeight, numBands, tileWidth * numBands, bandsOffset));
// //
if (colorInterpretations[0] == gdalconstConstants.GCI_PaletteIndex) {
ColorTable ct = null;
try {
ct = rband.GetRasterColorTable();
IndexColorModel icm = ct.getIndexColorModel(gdal.GetDataTypeSize(buf_type));
setColorModel(icm);
} finally {
if (ct != null) {
try {
// Closing the band
ct.delete();
} catch (Throwable e) {
if (LOGGER.isLoggable(Level.FINEST))
LOGGER.log(Level.FINEST, e.getLocalizedMessage(), e);
}
}
}
} else
setColorModel(GDALUtilities.buildColorModel(getSampleModel()));
if (getColorModel() == null || getSampleModel() == null)
return false;
} finally {
if (rband != null) {
try {
// Closing the band
rband.delete();
} catch (Throwable e) {
if (LOGGER.isLoggable(Level.FINEST))
LOGGER.log(Level.FINEST, e.getLocalizedMessage(), e);
}
}
}
return true;
}
use of org.gdal.gdal.ColorTable in project imageio-ext by geosolutions-it.
the class GDALImageWriter method setMetadata.
/**
* Set all the metadata available in the imageMetadata
* <code>IIOMetadata</code> instance
*
* @param dataset
* the dataset on which to set metadata and properties
* @param imageMetadata
* an instance of a {@link GDALCommonIIOImageMetadata}
* containing metadata
*/
private void setMetadata(Dataset dataset, GDALCommonIIOImageMetadata imageMetadata) {
// TODO: which metadata should be copied in the dataset?
// Should width, height and similar properties to be copied?
// //
//
// Setting GeoTransformation
//
// //
final double[] geoTransformation = imageMetadata.getGeoTransformation();
if (geoTransformation != null)
dataset.SetGeoTransform(geoTransformation);
// //
//
// Setting Projection
//
// //
final String projection = imageMetadata.getProjection();
if (projection != null && projection.trim().length() != 0)
dataset.SetProjection(projection);
// //
//
// Setting GCPs
//
// //
final int gcpNum = imageMetadata.getGcpNumber();
if (gcpNum != 0) {
final String gcpProj = imageMetadata.getGcpProjection();
List gcps = imageMetadata.getGCPs();
// TODO: Fix getGCPs access in SWIG's Java Bindings
// TODO: set GCPs. Not all dataset support GCPs settings
// dataset.SetGCPs(1, gcps, gcpProj);
}
// //
//
// Setting bands values
//
// //
final int nBands = imageMetadata.getNumBands();
for (int i = 0; i < nBands; i++) {
final Band band = dataset.GetRasterBand(i + 1);
final int colorInterpretation = imageMetadata.getColorInterpretations(i);
band.SetRasterColorInterpretation(colorInterpretation);
if (i == 0 && nBands == 1) {
// //
if (colorInterpretation == gdalconstConstants.GCI_PaletteIndex) {
ColorModel cm = imageMetadata.getColorModel();
if (cm instanceof IndexColorModel) {
IndexColorModel icm = (IndexColorModel) cm;
// //
//
// Setting color table
//
// //
final int size = icm.getMapSize();
ColorTable ct = new ColorTable(gdalconstConstants.GPI_RGB);
int j = 0;
for (; j < size; j++) ct.SetColorEntry(j, new Color(icm.getRGB(j)));
band.SetRasterColorTable(ct);
}
}
}
try {
final double noData = imageMetadata.getNoDataValue(i);
if (!Double.isNaN(noData))
band.SetNoDataValue(noData);
} catch (IllegalArgumentException iae) {
// NoDataValue not found or wrong bandIndex specified. Go on
}
}
// //
//
// Setting metadata
//
// TODO: Requires SWIG bindings extending since an HashTable as
// parameter crashes the JVM
//
// //
final List<String> domains = imageMetadata.getGdalMetadataDomainsList();
final int nDomains = domains.size();
for (int i = 0; i < nDomains; i++) {
final String domain = (String) domains.get(i);
Map metadataMap = imageMetadata.getGdalMetadataDomain(domain);
if (metadataMap != null) {
Iterator<String> keysIt = metadataMap.keySet().iterator();
while (keysIt.hasNext()) {
final String key = keysIt.next();
final String value = (String) metadataMap.get(key);
dataset.SetMetadataItem(key, value, domain);
}
}
}
}
use of org.gdal.gdal.ColorTable in project com.revolsys.open by revolsys.
the class Gdal method getBufferedImage.
/**
* <p>
* Convert the overview raster from {@link Dataset} to a
* {@link BufferedImage} . The raster will be clipped to the
* sourceOffsetX,sourceOffsetY -> sourceWidth, sourceHeight rectangle. The
* clip rectangle will be adjusted to fit inside the bounds of the source
* image. The result image will scaled to the dimensions of targetWidth,
* targetHeight.
* </p>
*
* @param dataset
* The image dataset.
* @param overviewIndex
* The index of the overview raster data. Use -1 for the whole
* image.
* @param sourceOffsetX
* The x location of the clip rectangle.
* @param sourceOffsetY
* The y location of the clip rectangle.
* @param sourceWidth
* The width of the clip rectangle. Use -1 to auto calculate.
* @param sourceHeight
* The height of the clip rectangle. Use -1 to auto calculate.
* @param targetWidth
* The width of the result image. Use -1 to auto calculate.
* @param targetHeight
* The height of the result image. Use -1 to auto calculate.
* @return The buffered image.
*/
public static BufferedImage getBufferedImage(final Dataset dataset, final int overviewIndex, int sourceOffsetX, int sourceOffsetY, int sourceWidth, int sourceHeight, int targetWidth, int targetHeight) {
synchronized (dataset) {
final int bandCount = dataset.getRasterCount();
final ByteBuffer[] bandData = new ByteBuffer[bandCount];
final int[] banks = new int[bandCount];
final int[] offsets = new int[bandCount];
int pixels = 0;
int bandDataType = 0;
int rasterColorInterpretation = -1;
ColorTable rasterColorTable = null;
for (int bandIndex = 0; bandIndex < bandCount; bandIndex++) {
final Band band = dataset.GetRasterBand(bandIndex + 1);
try {
Band overviewBand;
if (overviewIndex == -1) {
overviewBand = band;
} else {
overviewBand = band.GetOverview(overviewIndex);
}
try {
if (rasterColorTable == null) {
rasterColorTable = band.GetRasterColorTable();
rasterColorInterpretation = band.GetRasterColorInterpretation();
bandDataType = band.getDataType();
final int overviewWidth = overviewBand.getXSize();
final int overviewHeight = overviewBand.getYSize();
if (sourceOffsetX < 0) {
sourceOffsetX = 0;
}
if (sourceOffsetY < 0) {
sourceOffsetY = 0;
}
if (sourceOffsetX >= overviewWidth || sourceOffsetY >= overviewHeight) {
return new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
}
if (sourceWidth < 0) {
sourceWidth = overviewWidth;
}
if (sourceOffsetX + sourceWidth > overviewWidth) {
sourceWidth = overviewWidth - sourceOffsetX;
}
if (targetWidth < 0) {
targetWidth = sourceWidth;
}
if (sourceHeight < 0) {
sourceHeight = overviewHeight;
}
if (sourceOffsetY + sourceHeight > overviewHeight) {
sourceHeight = overviewHeight - sourceOffsetY;
}
if (targetHeight < 0) {
targetHeight = sourceHeight;
}
pixels = targetWidth * targetHeight;
}
if (pixels > 0 && sourceHeight > 0 && sourceWidth > 0) {
final int bufferSize = pixels * gdal.GetDataTypeSize(bandDataType) / 8;
final ByteBuffer data = ByteBuffer.allocateDirect(bufferSize);
data.order(ByteOrder.nativeOrder());
final int result = overviewBand.ReadRaster_Direct(sourceOffsetX, sourceOffsetY, sourceWidth, sourceHeight, targetWidth, targetHeight, bandDataType, data);
if (result == gdalconstConstants.CE_None) {
bandData[bandIndex] = data;
} else {
throw new RuntimeException("Error converting image");
}
} else {
return new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
}
banks[bandIndex] = bandIndex;
offsets[bandIndex] = 0;
} finally {
overviewBand.delete();
}
} finally {
band.delete();
}
}
DataBuffer imageBuffer = null;
SampleModel sampleModel = null;
int dataType = 0;
int dataBufferType = 0;
if (bandDataType == gdalconstConstants.GDT_Byte) {
final byte[][] bytes = new byte[bandCount][];
for (int bandIndex = 0; bandIndex < bandCount; bandIndex++) {
bytes[bandIndex] = new byte[pixels];
bandData[bandIndex].get(bytes[bandIndex]);
}
imageBuffer = new DataBufferByte(bytes, pixels);
dataBufferType = DataBuffer.TYPE_BYTE;
sampleModel = new BandedSampleModel(dataBufferType, targetWidth, targetHeight, targetWidth, banks, offsets);
dataType = rasterColorInterpretation == gdalconstConstants.GCI_PaletteIndex ? BufferedImage.TYPE_BYTE_INDEXED : BufferedImage.TYPE_BYTE_GRAY;
} else if (bandDataType == gdalconstConstants.GDT_Int16) {
final short[][] shorts = new short[bandCount][];
for (int bandIndex = 0; bandIndex < bandCount; bandIndex++) {
shorts[bandIndex] = new short[pixels];
bandData[bandIndex].asShortBuffer().get(shorts[bandIndex]);
}
imageBuffer = new DataBufferShort(shorts, pixels);
dataBufferType = DataBuffer.TYPE_USHORT;
sampleModel = new BandedSampleModel(dataBufferType, targetWidth, targetHeight, targetWidth, banks, offsets);
dataType = BufferedImage.TYPE_USHORT_GRAY;
} else if (bandDataType == gdalconstConstants.GDT_Int32) {
final int[][] ints = new int[bandCount][];
for (int bandIndex = 0; bandIndex < bandCount; bandIndex++) {
ints[bandIndex] = new int[pixels];
bandData[bandIndex].asIntBuffer().get(ints[bandIndex]);
}
imageBuffer = new DataBufferInt(ints, pixels);
dataBufferType = DataBuffer.TYPE_INT;
sampleModel = new BandedSampleModel(dataBufferType, targetWidth, targetHeight, targetWidth, banks, offsets);
dataType = BufferedImage.TYPE_CUSTOM;
}
final WritableRaster raster = Raster.createWritableRaster(sampleModel, imageBuffer, null);
BufferedImage image = null;
ColorModel colorModel = null;
if (rasterColorInterpretation == gdalconstConstants.GCI_PaletteIndex) {
dataType = BufferedImage.TYPE_BYTE_INDEXED;
colorModel = rasterColorTable.getIndexColorModel(gdal.GetDataTypeSize(bandDataType));
image = new BufferedImage(colorModel, raster, false, null);
} else {
ColorSpace colorSpace = null;
if (bandCount > 2) {
colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
colorModel = new ComponentColorModel(colorSpace, false, false, Transparency.OPAQUE, dataBufferType);
image = new BufferedImage(colorModel, raster, true, null);
} else {
image = new BufferedImage(targetWidth, targetHeight, dataType);
image.setData(raster);
}
}
return image;
}
}
Aggregations