From 96e96d07e3f93f44eb9829e6e3b7de4cce0574a4 Mon Sep 17 00:00:00 2001 From: Justin Kunimune Date: Wed, 15 Mar 2023 12:14:57 -0400 Subject: [PATCH] handle transparency correctly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit yeah, a little surprising I hadn’t done this before now. --- Map-Projections.iml | 67 +++++++++++++++++++++++--------------- src/apps/MapConverter.java | 14 +++++--- src/image/PixelMap.java | 13 +++++++- 3 files changed, 61 insertions(+), 33 deletions(-) diff --git a/Map-Projections.iml b/Map-Projections.iml index b92b1e0..5019563 100644 --- a/Map-Projections.iml +++ b/Map-Projections.iml @@ -17,27 +17,9 @@ - + - - - - - - - - - - - - - - - - - - - + @@ -46,7 +28,7 @@ - + @@ -55,7 +37,7 @@ - + @@ -64,7 +46,7 @@ - + @@ -73,7 +55,7 @@ - + @@ -82,7 +64,7 @@ - + @@ -91,7 +73,7 @@ - + @@ -100,7 +82,38 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/apps/MapConverter.java b/src/apps/MapConverter.java index 56f8eda..c748258 100644 --- a/src/apps/MapConverter.java +++ b/src/apps/MapConverter.java @@ -43,7 +43,7 @@ public class MapConverter { public static void main(String[] args) throws IOException { // the directory with the equirectangular maps to convert - String directory = "C:/Users/Justin Kunimune/Downloads/AR Dymaxion Selections-20220212T141615Z-001/AR Dymaxion Selections"; + String directory = "C:\\Users\\justi\\Downloads\\new_dymaxion_maps"; // the desired map projection Projection projection = Polyhedral.DYMAXION; @@ -53,8 +53,10 @@ public class MapConverter { // iterate thru the directory for (Path inputPath: pathIterable) { // look for images that are not dymaxion projections - if (inputPath.toString().endsWith(".png") && - !inputPath.toString().endsWith(".dymaxion.png")) { + if (inputPath.toString().endsWith(".jpg") || + inputPath.toString().endsWith(".tif") || + inputPath.toString().endsWith(".png") && + !inputPath.toString().endsWith(".dymaxion.png")) { System.out.println(inputPath); PixelMap inputImage = new PixelMap(inputPath.toFile()); @@ -71,8 +73,10 @@ public class MapConverter { null, null, null); // update the filename and save to disk - String outputPath = inputPath.toString().replace( - ".png", ".dymaxion.png"); + String outputPath = inputPath.toString(); + outputPath = outputPath.replace(".jpg", ".png"); + outputPath = outputPath.replace(".tif", ".png"); + outputPath = outputPath.replace(".png", ".dymaxion.png"); SavableImage.savable(outputImage).save(new File(outputPath)); } } diff --git a/src/image/PixelMap.java b/src/image/PixelMap.java index 9edd39f..f5af3c6 100644 --- a/src/image/PixelMap.java +++ b/src/image/PixelMap.java @@ -23,7 +23,9 @@ */ package image; +import java.awt.Transparency; import java.awt.image.BufferedImage; +import java.awt.image.WritableRaster; import java.io.File; import java.io.IOException; @@ -37,10 +39,12 @@ import javax.imageio.ImageIO; public class PixelMap { private final BufferedImage pixels; + private final WritableRaster alphaPixels; public PixelMap(File f) throws IOException { pixels = ImageIO.read(f); + alphaPixels = pixels.getAlphaRaster(); } @@ -63,7 +67,14 @@ public class PixelMap { y = 0; else if (y >= pixels.getHeight()) y = pixels.getHeight() - 1; + + int alpha; + if (pixels.getTransparency() != Transparency.OPAQUE) + alpha = alphaPixels.getPixel((int) x, (int) y, new int[1])[0]; + else + alpha = 0xFF; + int rgb = pixels.getRGB((int) x, (int) y); - return (0xFF000000) | pixels.getRGB((int) x, (int) y); + return (alpha << 24) | rgb; } }