Guest User

Untitled

a guest
Apr 26th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.18 KB | None | 0 0
  1. import javax.imageio.ImageIO;
  2. import java.awt.*;
  3. import java.awt.image.BufferedImage;
  4. import java.io.*;
  5. import java.util.TreeMap;
  6. import java.util.concurrent.Executors;
  7. import java.util.concurrent.ThreadPoolExecutor;
  8. import java.util.concurrent.TimeUnit;
  9.  
  10. public class Routines
  11. {
  12.     public static void parseAppArgs(String[] args) throws IllegalArgumentException
  13.     {
  14.         if(null == args || AppData.APP_ARGS != args.length)
  15.         {throw new IllegalArgumentException(AppData.APP_USAGE);}
  16.  
  17.         ImageData.Constants.ecgPath = args[0];
  18.  
  19.         File ecgFile = new File(ImageData.Constants.ecgPath);
  20.         if(!ecgFile.exists() || !ecgFile.isFile() || !ecgFile.canRead())
  21.         {throw new IllegalArgumentException("\nWrong file " + ImageData.Constants.ecgPath + "(check if it exists)");}
  22.  
  23.         ImageData.Constants.imgPath = args[1];
  24.  
  25.         File imgFile = new File(ImageData.Constants.imgPath);
  26.  
  27.         try
  28.         {
  29.             if(!imgFile.createNewFile() && !imgFile.isFile() && !imgFile.canWrite())
  30.             {throw new IllegalArgumentException("\nWrong file " + ImageData.Constants.imgPath + "(unable to create file)");}
  31.         }
  32.         catch(IOException ioEx)
  33.         {{throw new IllegalArgumentException("\nWrong file " + ImageData.Constants.imgPath + "(unable to create file)");}}
  34.  
  35.         ImageData.Constants.imgType = args[2];
  36.         if(!ImageData.Constants.imgType.equalsIgnoreCase("PNG") && !ImageData.Constants.imgType.equalsIgnoreCase("JPG") &&
  37.                 !ImageData.Constants.imgType.equalsIgnoreCase("GIF") && !ImageData.Constants.imgType.equalsIgnoreCase("BMP"))
  38.         {throw new IllegalArgumentException("\nWrong image type " + ImageData.Constants.imgType);}
  39.  
  40.         ImageData.Constants.vRes = Integer.parseInt(args[3]);
  41.  
  42.         if(ImageData.Constants.lowRes > ImageData.Constants.vRes)
  43.         {throw new IllegalArgumentException("Picture is too small" + AppData.APP_USAGE);}
  44.  
  45.         System.out.println("INIT\t\t\t[OK]");
  46.  
  47.         return;
  48.     }
  49.  
  50.     public static void parseECGFile() throws IOException
  51.     {
  52.         try(FileReader ecgReader = new FileReader(ImageData.Constants.ecgPath))
  53.         {
  54.             ImageData.ecgData = new TreeMap<>();
  55.             StreamTokenizer ecgTokenizer = new StreamTokenizer(ecgReader);
  56.  
  57.             double timeValue = 0;
  58.             double ecgValue = 0;
  59.  
  60.             while(StreamTokenizer.TT_EOF != ecgTokenizer.nextToken())
  61.             {
  62.                 if(ecgTokenizer.ttype != StreamTokenizer.TT_NUMBER) throw new IOException("ECG data file is corrupted");
  63.                 timeValue = ecgTokenizer.nval;
  64.                 if(ecgTokenizer.nextToken() != StreamTokenizer.TT_NUMBER) throw new IOException("ECG data file is corrupted");
  65.                 ecgValue = ecgTokenizer.nval;
  66.                
  67.                 ImageData.ecgData.put(timeValue, ecgValue);
  68.             }
  69.         }
  70.  
  71.         ImageData.Constants.hRes = ImageData.ecgData.size();
  72.        
  73.         ImageData.imgData = new double[ImageData.Constants.hRes][ImageData.Constants.vRes];
  74.         ImageData.Constants.ds = 1.0/(double) ImageData.Constants.vRes;
  75.         ImageData.Constants.dt = (ImageData.ecgData.lastKey() - ImageData.ecgData.firstKey())/ ImageData.Constants.hRes;
  76.         ImageData.Constants.s0 = ImageData.Constants.dt;
  77.         ImageData.Constants.t0 = ImageData.ecgData.firstKey();
  78.        
  79.         System.out.println("ECG DATA FILE\t\t[OK]");
  80.  
  81.         return;
  82.     }
  83.  
  84.     public static void buildImage() throws RuntimeException
  85.     {
  86.         int CPUs = Runtime.getRuntime().availableProcessors();
  87.         ThreadPoolExecutor unitPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(CPUs);
  88.  
  89.         for(int i =0; i < ImageData.Constants.vRes; ++i)
  90.         {unitPool.execute(new CompUnit(i));}
  91.  
  92.         unitPool.shutdown();
  93.  
  94.         try
  95.         {
  96.             while (!unitPool.awaitTermination(1, TimeUnit.SECONDS))
  97.             {System.out.print("\rPROCESSING DATA\t\t[" + (int)(100 * unitPool.getCompletedTaskCount() / unitPool.getTaskCount() +1) + "%]");}
  98.             System.out.print("\rPROCESSING DATA\t\t[OK]");
  99.         }
  100.         catch(InterruptedException intEx)
  101.         {throw new RuntimeException("Computation of image was aborted.\n" + intEx.getLocalizedMessage());}
  102.  
  103.         double extrMax = Double.NEGATIVE_INFINITY;
  104.         double extrMin = Double.POSITIVE_INFINITY;
  105.  
  106.         for (int xDim = 0; xDim < ImageData.Constants.hRes; ++xDim)
  107.         {
  108.             for (int yDim = 0; yDim < ImageData.Constants.vRes; ++yDim)
  109.             {
  110.                 extrMax = Math.max(extrMax, ImageData.imgData[xDim][yDim]);
  111.                 extrMin = Math.min(extrMin, ImageData.imgData[xDim][yDim]);
  112.             }
  113.         }
  114.  
  115.         ImageData.outputImage = new BufferedImage(ImageData.Constants.hRes, ImageData.Constants.vRes, BufferedImage.TYPE_BYTE_GRAY);
  116.  
  117.         for (int xDim = 0; xDim < ImageData.Constants.hRes; ++xDim)
  118.         {
  119.             for (int yDim = 0; yDim < ImageData.Constants.vRes; ++yDim)
  120.             {
  121.                 int pixelColor = (int)(255.0 * ((ImageData.imgData[xDim][yDim] - extrMin) / (extrMax - extrMin)));
  122.                 if (pixelColor > 255) pixelColor = 255;
  123.                 if (pixelColor < 0) pixelColor = 0;
  124.                 ImageData.outputImage.setRGB(xDim, yDim, (pixelColor << 16) | (pixelColor << 8) | pixelColor);
  125.             }
  126.         }
  127.  
  128.         System.out.println("\nMAKING IMAGE\t\t[OK]");
  129.  
  130.         return;
  131.     }
  132.  
  133.     public static void writeImage() throws IOException
  134.     {
  135.         try(OutputStream outputFile = new FileOutputStream(ImageData.Constants.imgPath);)
  136.         {
  137.             ImageIO.write(ImageData.outputImage, ImageData.Constants.imgType, outputFile);
  138.         }
  139.  
  140.         System.out.println("SAVING IMAGE\t\t[OK]");
  141.  
  142.         return;
  143.     }
  144.  
  145.     public static void openImage() throws IOException
  146.     {
  147.         System.out.println("\nDo you wish to view the wavelet image? [Yy(es)|Nn(o)]");
  148.         int answer = System.in.read();
  149.  
  150.         switch (answer)
  151.         {
  152.             case 'Y':
  153.             case 'y':
  154.  
  155.                 System.out.println("Opening " + ImageData.Constants.imgPath + "...");
  156.  
  157.                 File resultFile = new File(ImageData.Constants.imgPath);
  158.                 Desktop exec = Desktop.getDesktop();
  159.                 exec.open(resultFile);
  160.  
  161.                 return;
  162.  
  163.             default:
  164.                 return;
  165.         }
  166.     }
  167. }
  168.  
  169.  
  170. public class CompUnit implements Runnable
  171. {
  172.     private int yDim;
  173.  
  174.     public CompUnit(int _yDim)
  175.     {
  176.         if(_yDim < 0 || _yDim > ImageData.Constants.vRes - 1)
  177.         {throw new IllegalArgumentException("Can't compute wrong image line " + _yDim);}
  178.  
  179.         this.yDim = _yDim;
  180.     }
  181.    
  182.     public void run()
  183.     {
  184.         double s = ImageData.Constants.s0 + ImageData.Constants.ds*yDim;
  185.         double sRoot = Math.sqrt(s);
  186.         double t = 0;
  187.  
  188.         for (int xDim = 0; xDim < ImageData.Constants.hRes; ++xDim)
  189.         {
  190.             t = ImageData.Constants.t0 + xDim * ImageData.Constants.dt;
  191.  
  192.             /*здесь построение картинки*/
  193.            
  194.             ImageData.imgData[xDim][yDim] *= ImageData.Constants.dt/sRoot;
  195.         }
  196.  
  197.         return;
  198.     }
  199. }
Add Comment
Please, Sign In to add comment