From 01c02e151ef375b324466631b99cd85e9a9aa982 Mon Sep 17 00:00:00 2001 From: Arya <108480101+Dazzle-10@users.noreply.github.com> Date: Thu, 27 Apr 2023 02:52:53 +0300 Subject: [PATCH] Getting Pixel Array From Image in Java (#13911) * Added the code for "Getting Pixel Array From Image in Java". * The code and resources for "Getting Pixel Array From Image in Java". * Removed the previous files from the wrong module. --- .../algorithms/pixelarray/GetPixelArray.java | 65 +++++++++++++++++++ .../pixelarray/GetPixelArrayUnitTest.java | 30 +++++++++ 2 files changed, 95 insertions(+) create mode 100644 algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/pixelarray/GetPixelArray.java create mode 100644 algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/pixelarray/GetPixelArrayUnitTest.java diff --git a/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/pixelarray/GetPixelArray.java b/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/pixelarray/GetPixelArray.java new file mode 100644 index 0000000000..ba7fe44808 --- /dev/null +++ b/algorithms-modules/algorithms-miscellaneous-7/src/main/java/com/baeldung/algorithms/pixelarray/GetPixelArray.java @@ -0,0 +1,65 @@ +package com.baeldung.algorithms.pixelarray; + +import java.awt.image.BufferedImage; +import java.awt.image.DataBufferByte; +public class GetPixelArray { + + public static int[][] get2DPixelArraySlow(BufferedImage sampleImage) { + int width = sampleImage.getWidth(); + int height = sampleImage.getHeight(); + int[][] result = new int[height][width]; + + for (int row = 0; row < height; row++) { + for (int col = 0; col < width; col++) { + result[row][col] = sampleImage.getRGB(col, row); + } + } + + return result; + } + + public static int[][] get2DPixelArrayFast(BufferedImage image) { + final byte[] pixelData = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); + final int width = image.getWidth(); + final int height = image.getHeight(); + final boolean hasAlphaChannel = image.getAlphaRaster() != null; + + int[][] result = new int[height][width]; + if (hasAlphaChannel) { + final int numberOfValues = 4; + for (int valueIndex = 0, row = 0, col = 0; valueIndex + numberOfValues - 1 < pixelData.length; valueIndex += numberOfValues) { + // Getting the values for each pixel from the pixelData array. + int argb = 0; + argb += (((int) pixelData[valueIndex] & 0xff) << 24); // alpha value + argb += ((int) pixelData[valueIndex + 1] & 0xff); // blue value + argb += (((int) pixelData[valueIndex + 2] & 0xff) << 8); // green value + argb += (((int) pixelData[valueIndex + 3] & 0xff) << 16); // red value + result[row][col] = argb; + + col++; + if (col == width) { + col = 0; + row++; + } + } + } else { + final int numberOfValues = 3; + for (int valueIndex = 0, row = 0, col = 0; valueIndex + numberOfValues - 1 < pixelData.length; valueIndex += numberOfValues) { + int argb = 0; + argb += -16777216; // 255 alpha value (fully opaque) + argb += ((int) pixelData[valueIndex] & 0xff); // blue value + argb += (((int) pixelData[valueIndex + 1] & 0xff) << 8); // green value + argb += (((int) pixelData[valueIndex + 2] & 0xff) << 16); // red value + result[row][col] = argb; + + col++; + if (col == width) { + col = 0; + row++; + } + } + } + + return result; + } +} \ No newline at end of file diff --git a/algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/pixelarray/GetPixelArrayUnitTest.java b/algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/pixelarray/GetPixelArrayUnitTest.java new file mode 100644 index 0000000000..b7bbb462dd --- /dev/null +++ b/algorithms-modules/algorithms-miscellaneous-7/src/test/java/com/baeldung/algorithms/pixelarray/GetPixelArrayUnitTest.java @@ -0,0 +1,30 @@ +package com.baeldung.algorithms.pixelarray; + +import static com.baeldung.algorithms.pixelarray.GetPixelArray.get2DPixelArrayFast; +import static com.baeldung.algorithms.pixelarray.GetPixelArray.get2DPixelArraySlow; +import static org.junit.Assert.*; + +import org.junit.Test; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.util.Arrays; + +public class GetPixelArrayUnitTest { + @Test + public void givenImage_whenGetPixelArray_thenBothMethodsReturnEqualValues() { + BufferedImage sampleImage = null; + try { + sampleImage = ImageIO.read(new File("src/main/resources/images/sampleImage.jpg")); + } catch (IOException e) { + throw new RuntimeException(e); + } + + int[][] firstResult = get2DPixelArraySlow(sampleImage); + int[][] secondResult = get2DPixelArrayFast(sampleImage); + + assertTrue(Arrays.deepEquals(firstResult, secondResult)); + } +}