From 7daf4bd7a4b15b3ffe055ba7b11a41eb1350f312 Mon Sep 17 00:00:00 2001 From: Rhythmic System Date: Fri, 19 Jul 2024 16:53:13 -0700 Subject: [PATCH] Yay rescaling --- .../entityicons/ImageController.java | 54 ++++++++++++++----- .../java/simplexity/entityicons/Rescaler.java | 29 ++++++++++ 2 files changed, 70 insertions(+), 13 deletions(-) create mode 100644 src/main/java/simplexity/entityicons/Rescaler.java diff --git a/src/main/java/simplexity/entityicons/ImageController.java b/src/main/java/simplexity/entityicons/ImageController.java index 5e03d71..a20a2d9 100644 --- a/src/main/java/simplexity/entityicons/ImageController.java +++ b/src/main/java/simplexity/entityicons/ImageController.java @@ -12,11 +12,16 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; + @RestController @RequestMapping("/api/v1/textures") public class ImageController { @@ -31,25 +36,48 @@ public ImageController(ResourceLoader resourceLoader) { } @GetMapping(value = "/{category}/{filename}/{size}", produces = MediaType.IMAGE_PNG_VALUE) - public ResponseEntity getImage(@PathVariable String category, @PathVariable String filename, @PathVariable int size) { + public ResponseEntity getImage( + @PathVariable String category, + @PathVariable String filename, + @PathVariable int size, + @RequestParam(required = false, defaultValue = "1") int scale) { String filePath = "file:" + assetsBasePath + "/png_files/" + category + "/" + size + "x" + size + "/" + filename + ".png"; Resource resource = resourceLoader.getResource(filePath); - if (resource.exists()) { - try (InputStream inputStream = resource.getInputStream()) { - byte[] imageBytes = StreamUtils.copyToByteArray(inputStream); - String downloadName = filename + "_" + size + "x" + size; - HttpHeaders headers = new HttpHeaders(); - headers.add(HttpHeaders.CONTENT_TYPE, "image/png"); - headers.add(HttpHeaders.CONTENT_DISPOSITION, String.format("inline; filename=%s", downloadName)); - - return new ResponseEntity<>(imageBytes, headers, HttpStatus.OK); + if (!resource.exists()) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); + + } + byte[] imageBytes; + try (InputStream inputStream = resource.getInputStream()) { + imageBytes = StreamUtils.copyToByteArray(inputStream); + } catch (IOException e) { + e.printStackTrace(); + return ResponseEntity.status(HttpStatus.ALREADY_REPORTED).build(); + } + String downloadName = filename + "_" + size + "x" + size; + if (scale > 1) { + BufferedImage rescaledImage = Rescaler.rescaleImage(filePath, scale, size); + if (rescaledImage == null) { + return ResponseEntity.status(HttpStatus.CONFLICT).build(); + } + ByteArrayOutputStream imageByteArrayOutputStream = new ByteArrayOutputStream(); + try { + ImageIO.write(rescaledImage, "png", imageByteArrayOutputStream); } catch (IOException e) { e.printStackTrace(); - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + return ResponseEntity.status(HttpStatus.FAILED_DEPENDENCY).build(); } - } else { - return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); + imageBytes = imageByteArrayOutputStream.toByteArray(); + downloadName = filename + "_" + (size * scale) + "x" + (size * scale); + } + + HttpHeaders headers = new HttpHeaders(); + headers.add(HttpHeaders.CONTENT_TYPE, "image/png"); + headers.add(HttpHeaders.CONTENT_DISPOSITION, String.format("inline; filename=%s", downloadName)); + return new ResponseEntity<>(imageBytes, headers, HttpStatus.OK); + + } } diff --git a/src/main/java/simplexity/entityicons/Rescaler.java b/src/main/java/simplexity/entityicons/Rescaler.java new file mode 100644 index 0000000..cfba97b --- /dev/null +++ b/src/main/java/simplexity/entityicons/Rescaler.java @@ -0,0 +1,29 @@ +package simplexity.entityicons; + +import javax.imageio.ImageIO; +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; + +public class Rescaler { + + public static BufferedImage rescaleImage(String path, int multiplier, int originalSize) { + BufferedImage originalImage; + try { + path = path.substring(5); + File file = new File(path); + originalImage = ImageIO.read(file); + } catch (IOException e) { + return null; + } + int size = multiplier * originalSize; + BufferedImage resizedImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB); + Graphics2D graphicsImage = resizedImage.createGraphics(); + graphicsImage.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); + graphicsImage.drawImage(originalImage, 0,0, size, size,null); + graphicsImage.dispose(); + return resizedImage; + } +}