Import.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package holeg.ui.view.image;
  2. import holeg.preferences.ImagePreference;
  3. import java.awt.Image;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.util.HashMap;
  11. import javax.imageio.ImageIO;
  12. /**
  13. * Centralized resource loading methods for improved
  14. * performance and easier troubleshooting.
  15. */
  16. public class Import {
  17. /*
  18. * Save nothing: Reload every image whenever requested. Save external: Only save
  19. * images that were found as external file. Save raw: Save all non-scaled
  20. * images. Save everything: self-explanatory.
  21. */
  22. private static final HashMap<String, Image> imgStorage;
  23. static {
  24. imgStorage = new HashMap<>();
  25. }
  26. private Import() {
  27. }
  28. /**
  29. * The rawest function replacing the old method without any default parameters. Currently not used
  30. * in HOLEG at all(aside form calls from the convenience methods).
  31. *
  32. * @param url Path to the image to be loaded.
  33. * @param w Width the loaded image should be scaled to.
  34. * @param h Height the loaded image should be scaled to.
  35. * @param hints Hints for the scaling algorithm to be used. Same as the parameter in the
  36. * getScaledInstance function of Image.
  37. * @return A loaded and scaled image from the requested path.
  38. */
  39. static Image loadImage(String url, int w, int h, int hints) {
  40. String key = url + "?" + w + "?" + h + "?" + hints;
  41. if (imgStorage.containsKey(key)) {
  42. return (imgStorage.get(key));
  43. } else {
  44. Image img = loadImage(url).getScaledInstance(w, h, hints);
  45. imgStorage.put(key, img);
  46. return img;
  47. }
  48. }
  49. /**
  50. * Loads an image from the given path and scales it using the smooth algorithm.
  51. *
  52. * @param url Path to the image to be loaded.
  53. * @param w Width the loaded image should be scaled to.
  54. * @param h Height the loaded image should be scaled to.
  55. * @return A loaded and (smoothly) scaled image from the requested path.
  56. */
  57. public static Image loadImage(String url, int w, int h) {
  58. return loadImage(url, w, h, Image.SCALE_SMOOTH);
  59. }
  60. /**
  61. * Loads an image from the given path and scales it using the smooth algorithm.
  62. *
  63. * @param url Path to the image to be loaded.
  64. * @return An image loaded from the requested path.
  65. */
  66. public static Image loadImage(String url) {
  67. if (imgStorage.containsKey(url)) {
  68. return imgStorage.get(url);
  69. } else {
  70. Image img;
  71. try {
  72. img = ImageIO.read(loadStream(url));
  73. } catch (IOException e) {
  74. return new BufferedImage(0, 0,
  75. BufferedImage.TYPE_INT_RGB);
  76. }
  77. imgStorage.put(url, img);
  78. return img;
  79. }
  80. }
  81. /**
  82. * Loads any resource with a given path, regardless of whether it is inside the jar or an external
  83. * resource. Every loadImage() function uses this as a basis.
  84. *
  85. * @param url The path (and file name) of the requested resource.
  86. * @return An InputStream from the requested resource.
  87. * @throws FileNotFoundException if file cannot be found
  88. */
  89. public static InputStream loadStream(String url) throws FileNotFoundException {
  90. InputStream o = InputStream.class.getResourceAsStream(url);
  91. if (o != null) {
  92. return o;
  93. } else {
  94. boolean rootSymbol = switch (url.charAt(
  95. 0)) { // So we can make sure to construct res/path correctly.
  96. case '/', '\\' -> true;
  97. default -> false; // Whether url starts with a / or \
  98. };
  99. File f = new File(url);
  100. if (!f.exists()) {
  101. url = "res" + (rootSymbol ? "" : "/") + url;
  102. }
  103. f = new File(url);// Possible bug with duplicate names.
  104. if (!f.exists()) {
  105. url = "res" + ImagePreference.Canvas.ImageNotFound;
  106. }
  107. return new FileInputStream(url);
  108. }
  109. }
  110. }