Extract images enhancements (#1757)

* fix

* extarct images

* langs

* logging

* cuke fix

---------

Co-authored-by: a <a>
This commit is contained in:
Anthony Stirling
2024-08-27 11:46:18 +02:00
committed by GitHub
parent 63dfcfe688
commit 47314a0f38
43 changed files with 131 additions and 22 deletions

View File

@@ -262,4 +262,5 @@ public class GeneralUtils {
}
return true;
}
}

View File

@@ -1,6 +1,10 @@
package stirling.software.SPDF.utils;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.DataBufferInt;
import java.nio.ByteBuffer;
public class ImageProcessingUtils {
@@ -29,4 +33,30 @@ public class ImageProcessingUtils {
}
return convertedImage;
}
public static byte[] getImageData(BufferedImage image) {
DataBuffer dataBuffer = image.getRaster().getDataBuffer();
if (dataBuffer instanceof DataBufferByte) {
return ((DataBufferByte) dataBuffer).getData();
} else if (dataBuffer instanceof DataBufferInt) {
int[] intData = ((DataBufferInt) dataBuffer).getData();
ByteBuffer byteBuffer = ByteBuffer.allocate(intData.length * 4);
byteBuffer.asIntBuffer().put(intData);
return byteBuffer.array();
} else {
int width = image.getWidth();
int height = image.getHeight();
byte[] data = new byte[width * height * 3];
int index = 0;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int rgb = image.getRGB(x, y);
data[index++] = (byte) ((rgb >> 16) & 0xFF); // Red
data[index++] = (byte) ((rgb >> 8) & 0xFF); // Green
data[index++] = (byte) (rgb & 0xFF); // Blue
}
}
return data;
}
}
}