Skip to content

Commit

Permalink
Helps understanding #3016 by reporting the faulty file names
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Sep 3, 2021
1 parent de7cde6 commit 3129e49
Showing 1 changed file with 138 additions and 101 deletions.
@@ -1,14 +1,13 @@
/*********************************************************************************************
/*******************************************************************************************************
*
* 'ImageDataLoader.java, in plugin ummisco.gama.ui.navigator, is part of the source code of the GAMA modeling and
* simulation platform. (v. 1.8.1)
* ImageDataLoader.java, in ummisco.gama.ui.navigator, is part of the source code of the GAMA modeling and simulation
* platform (v.2.0.0).
*
* (c) 2007-2020 UMI 209 UMMISCO IRD/UPMC & Partners
* (c) 2007-2021 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and developers contact.
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
*
**********************************************************************************************/
********************************************************************************************************/
package ummisco.gama.ui.metadata;

import static javax.imageio.ImageIO.createImageInputStream;
Expand Down Expand Up @@ -62,16 +61,26 @@
*/
public class ImageDataLoader {

/** The Constant READER_SPI. */
private final static TIFFImageReaderSpi READER_SPI = new TIFFImageReaderSpi();

/** The Constant IMAGE_PGM. */
public static final int IMAGE_ASC = 8, IMAGE_PGM = 9;

/**
* Gets the image data.
*
* @param file
* the file
* @return the image data
*/
public static ImageData getImageData(final IFile file) {
ImageData imageData = null;
final String ext = file.getFileExtension();
try {
file.refreshLocal(IResource.DEPTH_ONE, null);
} catch (final CoreException e1) {
System.err.println("Impossible to load the metadata of " + file.getFullPath());
e1.printStackTrace();
}
if (ext != null) {
Expand All @@ -81,105 +90,116 @@ public static ImageData getImageData(final IFile file) {
} else if ("pgm".equals(ext)) {
imageData = readPGM(in);
} else if (ext.contains("tif")) {
try {
imageData = new ImageData(in);
final PaletteData palette = imageData.palette;
if (!((imageData.depth == 1 || imageData.depth == 2 || imageData.depth == 4
|| imageData.depth == 8) && !palette.isDirect || imageData.depth == 8
|| (imageData.depth == 16 || imageData.depth == 24 || imageData.depth == 32)
&& palette.isDirect)) {
imageData = null;
}
if (imageData == null) {
final BufferedImage tif = ImageIO.read(in);
imageData = convertToSWT(tif);
}
if (imageData == null) {
try (ImageInputStream is =
createImageInputStream(new File(file.getLocation().toFile().getAbsolutePath()))) {
final ImageReader reader = READER_SPI.createReaderInstance();
reader.setInput(is);
final BufferedImage image = toCompatibleImage(reader.read(0));
imageData = convertToSWT(image);
image.flush();
imageData.type = SWT.IMAGE_TIFF;
} catch (final IOException e1) {
e1.printStackTrace();
}
}
} catch (Exception ex) {
try {
AbstractGridCoverage2DReader reader =
new GeoTiffReader(file.getLocation().toFile().getAbsolutePath());// format.getReader(file,
// hints);
GridCoverage2D grid = reader.read(null);
reader.dispose();

BufferedImage image = new BufferedImage(grid.getGridGeometry().getGridRange2D().width,
grid.getGridGeometry().getGridRange2D().height, BufferedImage.TYPE_4BYTE_ABGR);

MapContent mapContent = new MapContent();
mapContent.getViewport().setCoordinateReferenceSystem(grid.getCoordinateReferenceSystem());
Layer rasterLayer = new GridCoverageLayer(grid, createStyle(1, -0.4, 0.2));
mapContent.addLayer(rasterLayer);

//TODO: Patrick: I removed these lines to avoid an error with some geotiffs, but no idea of there roles
/* GTRenderer draw = new StreamingRenderer();
draw.setMapContent(mapContent);
Graphics2D graphics = image.createGraphics();
draw.paint(graphics, grid.getGridGeometry().getGridRange2D(), mapContent.getMaxBounds());
*/
imageData = convertToSWT(image);
image.flush();
mapContent.dispose();
imageData.type = SWT.IMAGE_TIFF;
} catch (Exception e) {
try {
final BufferedImage image = toCompatibleImage(
read(new File(file.getLocation().toFile().getAbsolutePath())));
imageData = convertToSWT(image);
image.flush();
imageData.type = SWT.IMAGE_TIFF;
} catch (final IOException e1) {
e1.printStackTrace();
}
}

}

// AbstractGridFormat format = GridFormatFinder.findFormat(file);
// Hints hints = null;
// if (format instanceof GeoTiffFormat) {
// hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
// }

imageData = readTiff(file, in);
} else {
try {
imageData = new ImageData(in);
} catch (final SWTException e) {
// Bad format. Can happen for PNG. See #2825
if ("png".equals(ext) || "jpg".equals(ext)) {
try {
final BufferedImage image = toCompatibleImage(
read(new File(file.getLocation().toFile().getAbsolutePath())));
imageData = convertToSWT(image);
image.flush();
imageData.type = "png".equals(ext) ? SWT.IMAGE_PNG : SWT.IMAGE_JPEG;
} catch (final IOException e1) {
e1.printStackTrace();
}
final BufferedImage image =
toCompatibleImage(read(new File(file.getLocation().toFile().getAbsolutePath())));
imageData = convertToSWT(image);
image.flush();
imageData.type = "png".equals(ext) ? SWT.IMAGE_PNG : SWT.IMAGE_JPEG;
}
}
}
} catch (final Exception ex) {
System.err.println("Impossible to load the metadata of " + file.getFullPath());
ex.printStackTrace();
}
}
if (imageData == null) { DEBUG.ERR("null image data"); }
return imageData;

}

/**
* Extracted.
*
* @param file
* the file
* @param in
* the in
* @return the image data
* @throws IOException
*/
private static ImageData readTiff(final IFile file, final InputStream in) throws IOException {
ImageData imageData = new ImageData(in);
try {
final PaletteData palette = imageData.palette;
if ((imageData.depth != 1 && imageData.depth != 2 && imageData.depth != 4 && imageData.depth != 8
|| palette.isDirect) && imageData.depth != 8
&& (imageData.depth != 16 && imageData.depth != 24 && imageData.depth != 32 || !palette.isDirect)) {
imageData = null;
}
if (imageData == null) {
final BufferedImage tif = ImageIO.read(in);
imageData = convertToSWT(tif);
}
if (imageData == null) {
try (ImageInputStream is =
createImageInputStream(new File(file.getLocation().toFile().getAbsolutePath()))) {
final ImageReader reader = READER_SPI.createReaderInstance();
reader.setInput(is);
final BufferedImage image = toCompatibleImage(reader.read(0));
imageData = convertToSWT(image);
image.flush();
imageData.type = SWT.IMAGE_TIFF;
}
}
} catch (Exception ex) {
try {
AbstractGridCoverage2DReader reader = new GeoTiffReader(file.getLocation().toFile().getAbsolutePath());// format.getReader(file,
GridCoverage2D grid = reader.read(null);
reader.dispose();
BufferedImage image = new BufferedImage(grid.getGridGeometry().getGridRange2D().width,
grid.getGridGeometry().getGridRange2D().height, BufferedImage.TYPE_4BYTE_ABGR);

MapContent mapContent = new MapContent();
mapContent.getViewport().setCoordinateReferenceSystem(grid.getCoordinateReferenceSystem());
Layer rasterLayer = new GridCoverageLayer(grid, createStyle(1, -0.4, 0.2));
mapContent.addLayer(rasterLayer);

// TODO: Patrick: I removed these lines to avoid an error with some geotiffs, but no idea of there roles
/*
* GTRenderer draw = new StreamingRenderer(); draw.setMapContent(mapContent); Graphics2D graphics =
* image.createGraphics(); draw.paint(graphics, grid.getGridGeometry().getGridRange2D(),
* mapContent.getMaxBounds());
*/
imageData = convertToSWT(image);
image.flush();
mapContent.dispose();
imageData.type = SWT.IMAGE_TIFF;
} catch (Exception e) {
final BufferedImage image =
toCompatibleImage(read(new File(file.getLocation().toFile().getAbsolutePath())));
imageData = convertToSWT(image);
image.flush();
imageData.type = SWT.IMAGE_TIFF;
}

}

// AbstractGridFormat format = GridFormatFinder.findFormat(file);
// Hints hints = null;
// if (format instanceof GeoTiffFormat) {
// hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
// }
return imageData;
}

/**
* Creates the style.
*
* @param band
* the band
* @param min
* the min
* @param max
* the max
* @return the style
*/
private static Style createStyle(final int band, final double min, final double max) {

FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
Expand All @@ -197,18 +217,23 @@ private static Style createStyle(final int band, final double min, final double
cMap.addColorMapEntry(start);
cMap.addColorMapEntry(end);
sym.setColorMap(cMap);
Style style = SLD.wrapSymbolizers(sym);

return style;
return SLD.wrapSymbolizers(sym);
}

/**
* Read PGM.
*
* @param filename
* the filename
* @return the image data
*/
private static ImageData readPGM(final InputStream filename) {
int[][] pixels;
try (Scanner infile = new Scanner(filename);) {

// process the top 4 header lines
final String filetype = infile.nextLine();
if (!filetype.equalsIgnoreCase("p2")) {
if (!"p2".equalsIgnoreCase(filetype)) {
DEBUG.ERR("Not a PGM");
infile.close();
return null;
Expand All @@ -229,9 +254,7 @@ private static ImageData readPGM(final InputStream filename) {
}
}
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
pixels[r][c] = (int) (pixels[r][c] / (double) maxValue);
}
for (int c = 0; c < cols; c++) { pixels[r][c] = (int) (pixels[r][c] / (double) maxValue); }
}
} catch (final Exception e) {
DEBUG.ERR(e.toString());
Expand All @@ -251,6 +274,13 @@ private static ImageData readPGM(final InputStream filename) {
return result;
}

/**
* Read ASC.
*
* @param filename
* the filename
* @return the image data
*/
private static ImageData readASC(final InputStream filename) {
int[][] pixels;
try (Scanner infile = new Scanner(filename);) {
Expand Down Expand Up @@ -309,9 +339,7 @@ private static ImageData readASC(final InputStream filename) {
ratio = range / 255d;
}
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
pixels[r][c] = (int) ((pixels[r][c] - minValue) * ratio);
}
for (int c = 0; c < cols; c++) { pixels[r][c] = (int) ((pixels[r][c] - minValue) * ratio); }
}
} catch (final Exception e) {
DEBUG.ERR(e.toString());
Expand All @@ -332,6 +360,13 @@ private static ImageData readASC(final InputStream filename) {
return result;
}

/**
* Convert to SWT.
*
* @param image
* the image
* @return the image data
*/
public static ImageData convertToSWT(final java.awt.image.BufferedImage image) {
if (image == null) return null;
if (image.getColorModel() instanceof java.awt.image.DirectColorModel) {
Expand All @@ -349,7 +384,8 @@ public static ImageData convertToSWT(final java.awt.image.BufferedImage image) {
}
}
return data;
} else if (image.getColorModel() instanceof java.awt.image.IndexColorModel) {
}
if (image.getColorModel() instanceof java.awt.image.IndexColorModel) {
final java.awt.image.IndexColorModel colorModel = (java.awt.image.IndexColorModel) image.getColorModel();
final int size = colorModel.getMapSize();
final byte[] reds = new byte[size];
Expand All @@ -375,7 +411,8 @@ public static ImageData convertToSWT(final java.awt.image.BufferedImage image) {
}
}
return data;
} else if (image.getColorModel() instanceof java.awt.image.ComponentColorModel) {
}
if (image.getColorModel() instanceof java.awt.image.ComponentColorModel) {

final java.awt.image.ComponentColorModel colorModel =
(java.awt.image.ComponentColorModel) image.getColorModel();
Expand Down

0 comments on commit 3129e49

Please sign in to comment.