Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.io.*;
- import java.util.TreeMap;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
- public class Routines
- {
- public static void parseAppArgs(String[] args) throws IllegalArgumentException
- {
- if(null == args || AppData.APP_ARGS != args.length)
- {throw new IllegalArgumentException(AppData.APP_USAGE);}
- ImageData.Constants.ecgPath = args[0];
- File ecgFile = new File(ImageData.Constants.ecgPath);
- if(!ecgFile.exists() || !ecgFile.isFile() || !ecgFile.canRead())
- {throw new IllegalArgumentException("\nWrong file " + ImageData.Constants.ecgPath + "(check if it exists)");}
- ImageData.Constants.imgPath = args[1];
- File imgFile = new File(ImageData.Constants.imgPath);
- try
- {
- if(!imgFile.createNewFile() && !imgFile.isFile() && !imgFile.canWrite())
- {throw new IllegalArgumentException("\nWrong file " + ImageData.Constants.imgPath + "(unable to create file)");}
- }
- catch(IOException ioEx)
- {{throw new IllegalArgumentException("\nWrong file " + ImageData.Constants.imgPath + "(unable to create file)");}}
- ImageData.Constants.imgType = args[2];
- if(!ImageData.Constants.imgType.equalsIgnoreCase("PNG") && !ImageData.Constants.imgType.equalsIgnoreCase("JPG") &&
- !ImageData.Constants.imgType.equalsIgnoreCase("GIF") && !ImageData.Constants.imgType.equalsIgnoreCase("BMP"))
- {throw new IllegalArgumentException("\nWrong image type " + ImageData.Constants.imgType);}
- ImageData.Constants.vRes = Integer.parseInt(args[3]);
- if(ImageData.Constants.lowRes > ImageData.Constants.vRes)
- {throw new IllegalArgumentException("Picture is too small" + AppData.APP_USAGE);}
- System.out.println("INIT\t\t\t[OK]");
- return;
- }
- public static void parseECGFile() throws IOException
- {
- try(FileReader ecgReader = new FileReader(ImageData.Constants.ecgPath))
- {
- ImageData.ecgData = new TreeMap<>();
- StreamTokenizer ecgTokenizer = new StreamTokenizer(ecgReader);
- double timeValue = 0;
- double ecgValue = 0;
- while(StreamTokenizer.TT_EOF != ecgTokenizer.nextToken())
- {
- if(ecgTokenizer.ttype != StreamTokenizer.TT_NUMBER) throw new IOException("ECG data file is corrupted");
- timeValue = ecgTokenizer.nval;
- if(ecgTokenizer.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("ECG data file is corrupted");
- ecgValue = ecgTokenizer.nval;
- ImageData.ecgData.put(timeValue, ecgValue);
- }
- }
- ImageData.Constants.hRes = ImageData.ecgData.size();
- ImageData.imgData = new double[ImageData.Constants.hRes][ImageData.Constants.vRes];
- ImageData.Constants.ds = 1.0/(double) ImageData.Constants.vRes;
- ImageData.Constants.dt = (ImageData.ecgData.lastKey() - ImageData.ecgData.firstKey())/ ImageData.Constants.hRes;
- ImageData.Constants.s0 = ImageData.Constants.dt;
- ImageData.Constants.t0 = ImageData.ecgData.firstKey();
- System.out.println("ECG DATA FILE\t\t[OK]");
- return;
- }
- public static void buildImage() throws RuntimeException
- {
- int CPUs = Runtime.getRuntime().availableProcessors();
- ThreadPoolExecutor unitPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(CPUs);
- for(int i =0; i < ImageData.Constants.vRes; ++i)
- {unitPool.execute(new CompUnit(i));}
- unitPool.shutdown();
- try
- {
- while (!unitPool.awaitTermination(1, TimeUnit.SECONDS))
- {System.out.print("\rPROCESSING DATA\t\t[" + (int)(100 * unitPool.getCompletedTaskCount() / unitPool.getTaskCount() +1) + "%]");}
- System.out.print("\rPROCESSING DATA\t\t[OK]");
- }
- catch(InterruptedException intEx)
- {throw new RuntimeException("Computation of image was aborted.\n" + intEx.getLocalizedMessage());}
- double extrMax = Double.NEGATIVE_INFINITY;
- double extrMin = Double.POSITIVE_INFINITY;
- for (int xDim = 0; xDim < ImageData.Constants.hRes; ++xDim)
- {
- for (int yDim = 0; yDim < ImageData.Constants.vRes; ++yDim)
- {
- extrMax = Math.max(extrMax, ImageData.imgData[xDim][yDim]);
- extrMin = Math.min(extrMin, ImageData.imgData[xDim][yDim]);
- }
- }
- ImageData.outputImage = new BufferedImage(ImageData.Constants.hRes, ImageData.Constants.vRes, BufferedImage.TYPE_BYTE_GRAY);
- for (int xDim = 0; xDim < ImageData.Constants.hRes; ++xDim)
- {
- for (int yDim = 0; yDim < ImageData.Constants.vRes; ++yDim)
- {
- int pixelColor = (int)(255.0 * ((ImageData.imgData[xDim][yDim] - extrMin) / (extrMax - extrMin)));
- if (pixelColor > 255) pixelColor = 255;
- if (pixelColor < 0) pixelColor = 0;
- ImageData.outputImage.setRGB(xDim, yDim, (pixelColor << 16) | (pixelColor << 8) | pixelColor);
- }
- }
- System.out.println("\nMAKING IMAGE\t\t[OK]");
- return;
- }
- public static void writeImage() throws IOException
- {
- try(OutputStream outputFile = new FileOutputStream(ImageData.Constants.imgPath);)
- {
- ImageIO.write(ImageData.outputImage, ImageData.Constants.imgType, outputFile);
- }
- System.out.println("SAVING IMAGE\t\t[OK]");
- return;
- }
- public static void openImage() throws IOException
- {
- System.out.println("\nDo you wish to view the wavelet image? [Yy(es)|Nn(o)]");
- int answer = System.in.read();
- switch (answer)
- {
- case 'Y':
- case 'y':
- System.out.println("Opening " + ImageData.Constants.imgPath + "...");
- File resultFile = new File(ImageData.Constants.imgPath);
- Desktop exec = Desktop.getDesktop();
- exec.open(resultFile);
- return;
- default:
- return;
- }
- }
- }
- public class CompUnit implements Runnable
- {
- private int yDim;
- public CompUnit(int _yDim)
- {
- if(_yDim < 0 || _yDim > ImageData.Constants.vRes - 1)
- {throw new IllegalArgumentException("Can't compute wrong image line " + _yDim);}
- this.yDim = _yDim;
- }
- public void run()
- {
- double s = ImageData.Constants.s0 + ImageData.Constants.ds*yDim;
- double sRoot = Math.sqrt(s);
- double t = 0;
- for (int xDim = 0; xDim < ImageData.Constants.hRes; ++xDim)
- {
- t = ImageData.Constants.t0 + xDim * ImageData.Constants.dt;
- /*здесь построение картинки*/
- ImageData.imgData[xDim][yDim] *= ImageData.Constants.dt/sRoot;
- }
- return;
- }
- }
Add Comment
Please, Sign In to add comment