From: David ‘Bombe’ Roden Date: Fri, 28 Feb 2014 21:17:12 +0000 (+0100) Subject: Determine image dimensions correctly. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=7e3fd504e2425deb4b6efb792398b4ec30bbd2b3;p=Sone.git Determine image dimensions correctly. --- diff --git a/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java b/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java index 3031970..5dd4cbb 100644 --- a/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java +++ b/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java @@ -17,12 +17,16 @@ package net.pterodactylus.sone.web; +import java.awt.Dimension; import java.awt.Image; +import java.awt.image.ImageObserver; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; import java.util.logging.Logger; @@ -121,9 +125,8 @@ public class UploadImagePage extends SoneTemplatePage { return; } String mimeType = getMimeType(imageData); - int width = uploadedImage.getWidth(null); - int height = uploadedImage.getHeight(null); - TemporaryImage temporaryImage = webInterface.getCore().createTemporaryImage(mimeType, imageData, width, height); + Dimension imageSize = getImageDimensions(uploadedImage); + TemporaryImage temporaryImage = webInterface.getCore().createTemporaryImage(mimeType, imageData, imageSize.width, imageSize.height); image = webInterface.getCore().createImage(currentSone, parent, temporaryImage); image.modify().setTitle(name).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update(); } catch (IOException ioe1) { @@ -137,6 +140,36 @@ public class UploadImagePage extends SoneTemplatePage { } } + private static Dimension getImageDimensions(Image uploadedImage) { + final CountDownLatch widthHeightLatch = new CountDownLatch(2); + final AtomicInteger finalWidth = new AtomicInteger(); + final AtomicInteger finalHeight = new AtomicInteger(); + ImageObserver imageObserver = new ImageObserver() { + @Override + public boolean imageUpdate(Image image, int infoFlags, int x, int y, int width, int height) { + if ((infoFlags & WIDTH) != 0) { + finalWidth.set(width); + widthHeightLatch.countDown(); + } + if ((infoFlags & HEIGHT) != 0) { + finalHeight.set(width); + widthHeightLatch.countDown(); + } + return (infoFlags & ALLBITS) != 0; + } + }; + finalWidth.set(uploadedImage.getWidth(imageObserver)); + finalHeight.set(uploadedImage.getHeight(imageObserver)); + while ((finalWidth.get() == -1) || (finalHeight.get() == -1)) { + try { + widthHeightLatch.await(); + } catch (InterruptedException ie1) { + logger.log(Level.WARNING, "Interrupted while waiting for latch..."); + } + } + return new Dimension(finalWidth.get(), finalHeight.get()); + } + // // PRIVATE METHODS //