From 98e0f45c0b64f753e1f66c9395b54c79e609471a Mon Sep 17 00:00:00 2001 From: sr1g19 <sr1g19@soton.ac.uk> Date: Tue, 11 May 2021 00:18:22 +0000 Subject: [PATCH] Upload New File --- ImageToUINT_Tool/Main.java | 75 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 ImageToUINT_Tool/Main.java diff --git a/ImageToUINT_Tool/Main.java b/ImageToUINT_Tool/Main.java new file mode 100644 index 0000000..0962a92 --- /dev/null +++ b/ImageToUINT_Tool/Main.java @@ -0,0 +1,75 @@ +import java.awt.image.*; +import java.awt.*; +import java.nio.file.*; +import javax.imageio.ImageIO; +import java.io.*; +import java.util.*; + +public class Main{ + public static void main(String[] args) { + if (args.length == 0) + System.exit(1); + try{ + for(String s : args){ + errorCentral(s); + } + } catch (Exception e){ + + } + } + + public static void errorCentral(String filePath) throws Exception{ + BufferedImage img = null; + File imgFile = new File(filePath); + img = ImageIO.read(imgFile); + + if(img == null) + System.exit(1); + + int width = img.getWidth(); + int height = img.getHeight(); + ColorModel colorModel = img.getColorModel(); + Raster imgRaster = img.getRaster(); + + /* + int[] colorData = new int[height*width*3]; + + for(int yDisp = 0; yDisp < height; yDisp++) + for(int xDisp = 0; xDisp < width; xDisp++){ + Object pix = imgRaster.getDataElements(xDisp, yDisp, null); + colorData[yDisp*width*3 + xDisp*3] = colorModel.getRed(pix) >>> 3; + colorData[yDisp*width*3 + xDisp*3 + 1] = colorModel.getGreen(pix) >>> 2; + colorData[yDisp*width*3 + xDisp*3 + 2] = colorModel.getBlue(pix) >>> 3; + } + */ + + + + int[] colorData = new int[height*width]; + + for(int yDisp = 0; yDisp < height; yDisp++) + for(int xDisp = 0; xDisp < width; xDisp++){ + Object pix = imgRaster.getDataElements(xDisp, yDisp, null); + int redData = colorModel.getRed(pix) >>> 3; + int greenData = colorModel.getGreen(pix) >>> 2; + int blueData = colorModel.getBlue(pix) >>> 3; + colorData[yDisp * width + xDisp] = (redData << 11) + (greenData << 5) + blueData; + } + + + String finalOutput = ""; + finalOutput += "File Name: " + imgFile.getName(); + finalOutput += "\nImgWidth: " + width + "\nImgHeight: " + height; + String finalFormatArray = Arrays.toString(colorData).replace("[", "").replace("]", ""); + finalOutput += "\nData: \nuint16_t[] = {" + finalFormatArray + "};"; + + //Create stuff + File newFile = new File(imgFile.getName() + ".txt"); + newFile.createNewFile(); + FileWriter outputStrm = new FileWriter(imgFile.getName() + ".txt"); + outputStrm.write(finalOutput); + outputStrm.close(); + + System.out.println(finalOutput); + } +} \ No newline at end of file -- GitLab