PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/WorldWind/src/main/java/gov/nasa/worldwind/data/ImageIORasterReader.java

http://geoforge.googlecode.com/
Java | 325 lines | 232 code | 38 blank | 55 comment | 56 complexity | ae15c01f39860bdd7c5f231da17a1c2e MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, GPL-3.0, LGPL-2.1, LGPL-2.0
  1. /*
  2. * Copyright (C) 2011 United States Government as represented by the Administrator of the
  3. * National Aeronautics and Space Administration.
  4. * All Rights Reserved.
  5. */
  6. package gov.nasa.worldwind.data;
  7. import gov.nasa.worldwind.avlist.AVKey;
  8. import gov.nasa.worldwind.avlist.AVList;
  9. import gov.nasa.worldwind.avlist.AVListImpl;
  10. import gov.nasa.worldwind.formats.tiff.GeotiffImageReaderSpi;
  11. import gov.nasa.worldwind.formats.worldfile.WorldFile;
  12. import gov.nasa.worldwind.geom.Sector;
  13. import gov.nasa.worldwind.util.ImageUtil;
  14. import gov.nasa.worldwind.util.Logging;
  15. import gov.nasa.worldwind.util.WWIO;
  16. /**
  17. * @author dcollins
  18. * @version $Id: ImageIORasterReader.java 1 2011-07-16 23:22:47Z dcollins $
  19. */
  20. public class ImageIORasterReader extends AbstractDataRasterReader
  21. {
  22. static
  23. {
  24. javax.imageio.spi.IIORegistry.getDefaultInstance().registerServiceProvider(GeotiffImageReaderSpi.inst());
  25. }
  26. private boolean generateMipMaps;
  27. public ImageIORasterReader(boolean generateMipMaps)
  28. {
  29. super(javax.imageio.ImageIO.getReaderMIMETypes(), getImageIOReaderSuffixes());
  30. this.generateMipMaps = generateMipMaps;
  31. }
  32. public ImageIORasterReader()
  33. {
  34. this(false);
  35. }
  36. public boolean isGenerateMipMaps()
  37. {
  38. return this.generateMipMaps;
  39. }
  40. public void setGenerateMipMaps(boolean generateMipMaps)
  41. {
  42. this.generateMipMaps = generateMipMaps;
  43. }
  44. protected boolean doCanRead(Object source, AVList params)
  45. {
  46. // Determine whether or not the data source can be read.
  47. //if (!this.canReadImage(source))
  48. // return false;
  49. // If the data source doesn't already have all the necessary metadata, then we determine whether or not
  50. // the missing metadata can be read.
  51. Object o = (params != null) ? params.getValue(AVKey.SECTOR) : null;
  52. if (o == null || !(o instanceof Sector))
  53. {
  54. if (!this.canReadWorldFiles(source))
  55. {
  56. return false;
  57. }
  58. }
  59. if (null != params && !params.hasKey(AVKey.PIXEL_FORMAT))
  60. {
  61. params.setValue(AVKey.PIXEL_FORMAT, AVKey.IMAGE);
  62. }
  63. return true;
  64. }
  65. protected DataRaster[] doRead(Object source, AVList params) throws java.io.IOException
  66. {
  67. javax.imageio.stream.ImageInputStream iis = createInputStream(source);
  68. java.awt.image.BufferedImage image = javax.imageio.ImageIO.read(iis);
  69. image = ImageUtil.toCompatibleImage(image);
  70. // If the data source doesn't already have all the necessary metadata, then we attempt to read the metadata.
  71. Object o = (params != null) ? params.getValue(AVKey.SECTOR) : null;
  72. if (o == null || !(o instanceof Sector))
  73. {
  74. AVList values = new AVListImpl();
  75. values.setValue(AVKey.IMAGE, image);
  76. this.readWorldFiles(source, values);
  77. o = values.getValue(AVKey.SECTOR);
  78. }
  79. return new DataRaster[]{this.createRaster((Sector) o, image)};
  80. }
  81. protected void doReadMetadata(Object source, AVList params) throws java.io.IOException
  82. {
  83. Object width = params.getValue(AVKey.WIDTH);
  84. Object height = params.getValue(AVKey.HEIGHT);
  85. if (width == null || height == null || !(width instanceof Integer) || !(height instanceof Integer))
  86. {
  87. this.readImageDimension(source, params);
  88. }
  89. Object sector = params.getValue(AVKey.SECTOR);
  90. if (sector == null || !(sector instanceof Sector))
  91. {
  92. this.readWorldFiles(source, params);
  93. }
  94. if (!params.hasKey(AVKey.PIXEL_FORMAT))
  95. {
  96. params.setValue(AVKey.PIXEL_FORMAT, AVKey.IMAGE);
  97. }
  98. }
  99. protected DataRaster createRaster(Sector sector, java.awt.image.BufferedImage image)
  100. {
  101. if (this.isGenerateMipMaps())
  102. {
  103. return new MipMappedBufferedImageRaster(sector, image);
  104. }
  105. else
  106. {
  107. return new BufferedImageRaster(sector, image);
  108. }
  109. }
  110. //private boolean canReadImage(DataSource source)
  111. //{
  112. // javax.imageio.stream.ImageInputStream iis = null;
  113. // javax.imageio.ImageReader reader = null;
  114. // try
  115. // {
  116. // iis = createInputStream(source);
  117. // reader = readerFor(iis);
  118. // if (reader == null)
  119. // return false;
  120. // }
  121. // catch (Exception e)
  122. // {
  123. // // Not interested in logging the exception, we only want to report the failure to read.
  124. // return false;
  125. // }
  126. // finally
  127. // {
  128. // if (reader != null)
  129. // reader.dispose();
  130. // try
  131. // {
  132. // if (iis != null)
  133. // iis.close();
  134. // }
  135. // catch (Exception e)
  136. // {
  137. // // Not interested in logging the exception.
  138. // }
  139. // }
  140. //
  141. // return true;
  142. //}
  143. private boolean canReadWorldFiles(Object source)
  144. {
  145. if (!(source instanceof java.io.File))
  146. {
  147. return false;
  148. }
  149. try
  150. {
  151. java.io.File[] worldFiles = WorldFile.getWorldFiles((java.io.File) source);
  152. if (worldFiles == null || worldFiles.length == 0)
  153. {
  154. return false;
  155. }
  156. }
  157. catch (java.io.IOException e)
  158. {
  159. // Not interested in logging the exception, we only want to report the failure to read.
  160. return false;
  161. }
  162. return true;
  163. }
  164. private void readImageDimension(Object source, AVList params) throws java.io.IOException
  165. {
  166. javax.imageio.stream.ImageInputStream iis = createInputStream(source);
  167. javax.imageio.ImageReader reader = readerFor(iis);
  168. try
  169. {
  170. if (reader == null)
  171. {
  172. String message = Logging.getMessage("generic.UnrecognizedImageSourceType", source);
  173. Logging.logger().severe(message);
  174. throw new java.io.IOException(message);
  175. }
  176. reader.setInput(iis, true, true);
  177. int width = reader.getWidth(0);
  178. int height = reader.getHeight(0);
  179. params.setValue(AVKey.WIDTH, width);
  180. params.setValue(AVKey.HEIGHT, height);
  181. }
  182. finally
  183. {
  184. if (reader != null)
  185. {
  186. reader.dispose();
  187. }
  188. iis.close();
  189. }
  190. }
  191. private void readWorldFiles(Object source, AVList params) throws java.io.IOException
  192. {
  193. if (!(source instanceof java.io.File))
  194. {
  195. String message = Logging.getMessage("DataRaster.CannotRead", source);
  196. Logging.logger().severe(message);
  197. throw new java.io.IOException(message);
  198. }
  199. // If an image is not specified in the metadata values, then attempt to construct the image size from other
  200. // parameters.
  201. Object o = params.getValue(AVKey.IMAGE);
  202. if (o == null || !(o instanceof java.awt.image.BufferedImage))
  203. {
  204. o = params.getValue(WorldFile.WORLD_FILE_IMAGE_SIZE);
  205. if (o == null || !(o instanceof int[]))
  206. {
  207. // If the image size is specified in the parameters WIDTH and HEIGHT, then translate them to the
  208. // WORLD_FILE_IMAGE_SIZE parameter.
  209. Object width = params.getValue(AVKey.WIDTH);
  210. Object height = params.getValue(AVKey.HEIGHT);
  211. if (width != null && height != null && width instanceof Integer && height instanceof Integer)
  212. {
  213. int[] size = new int[]{(Integer) width, (Integer) height};
  214. params.setValue(WorldFile.WORLD_FILE_IMAGE_SIZE, size);
  215. }
  216. }
  217. }
  218. java.io.File[] worldFiles = WorldFile.getWorldFiles((java.io.File) source);
  219. WorldFile.decodeWorldFiles(worldFiles, params);
  220. }
  221. private static javax.imageio.stream.ImageInputStream createInputStream(Object source) throws java.io.IOException
  222. {
  223. // ImageIO can create an ImageInputStream automatically from a File references or a standard I/O InputStream
  224. // reference. If the data source is a URL, or a string file path, then we must open an input stream ourselves.
  225. Object input = source;
  226. if (source instanceof java.net.URL)
  227. {
  228. input = ((java.net.URL) source).openStream();
  229. }
  230. else if (source instanceof CharSequence)
  231. {
  232. input = openInputStream(source.toString());
  233. }
  234. return javax.imageio.ImageIO.createImageInputStream(input);
  235. }
  236. private static java.io.InputStream openInputStream(String path) throws java.io.IOException
  237. {
  238. Object streamOrException = WWIO.getFileOrResourceAsStream(path, null);
  239. if (streamOrException == null)
  240. {
  241. return null;
  242. }
  243. else if (streamOrException instanceof java.io.IOException)
  244. {
  245. throw (java.io.IOException) streamOrException;
  246. }
  247. else if (streamOrException instanceof Exception)
  248. {
  249. String message = Logging.getMessage("generic.ExceptionAttemptingToReadImageFile", path);
  250. Logging.logger().log(java.util.logging.Level.SEVERE, message, streamOrException);
  251. throw new java.io.IOException(message);
  252. }
  253. return (java.io.InputStream) streamOrException;
  254. }
  255. private static javax.imageio.ImageReader readerFor(javax.imageio.stream.ImageInputStream iis)
  256. {
  257. java.util.Iterator<javax.imageio.ImageReader> readers = javax.imageio.ImageIO.getImageReaders(iis);
  258. if (!readers.hasNext())
  259. {
  260. return null;
  261. }
  262. return readers.next();
  263. }
  264. private static String[] getImageIOReaderSuffixes()
  265. {
  266. java.util.Iterator<javax.imageio.spi.ImageReaderSpi> iter;
  267. try
  268. {
  269. iter = javax.imageio.spi.IIORegistry.getDefaultInstance().getServiceProviders(
  270. javax.imageio.spi.ImageReaderSpi.class, true);
  271. }
  272. catch (Exception e)
  273. {
  274. return new String[0];
  275. }
  276. java.util.Set<String> set = new java.util.HashSet<String>();
  277. while (iter.hasNext())
  278. {
  279. javax.imageio.spi.ImageReaderSpi spi = iter.next();
  280. String[] names = spi.getFileSuffixes();
  281. set.addAll(java.util.Arrays.asList(names));
  282. }
  283. String[] array = new String[set.size()];
  284. set.toArray(array);
  285. return array;
  286. }
  287. }