Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.89 KB | None | 0 0
  1. package aktortest;
  2. import java.io.IOException;
  3. import java.nio.file.Files;
  4. import java.nio.file.Path;
  5. import java.nio.file.Paths;
  6.  
  7.  
  8.  
  9. public class Aktortest {
  10.    
  11.     /* CONSTANT VALUES */
  12.     private final static int cColorCount    = 3;
  13.     private final static int cRedOffset     = 0;
  14.     private final static int cGreenOffset   = 1;
  15.     private final static int cBlueOffset    = 2;
  16.    
  17.     private final static boolean cIsVerbose = true;
  18.  
  19.  
  20.  
  21. /* ################################################################################################### */
  22. /* WORKING METHODS */
  23.     /**
  24.      * Executes the Split Pixel exercise
  25.      * @param input = byte array, with pixels to split
  26.      * @param pRedFileName  = string of filename where the red pixels have to saved to
  27.      * @param pGreenFileName    = string of filename where the green pixels have to saved to
  28.      * @param pBlueFileName = string of filename where the blue pixels have to saved to
  29.      * @throws IOException
  30.      */
  31.     public static void SplitPixel(byte[] input, String pRedFileName, String pGreenFileName, String pBlueFileName) throws IOException
  32.     {
  33.         /* DECLARING THE NEEDED VARIABLES */
  34.         int lArrayLength = input.length;       
  35.        
  36.         byte[] lRedPixel    = new byte[lArrayLength /cColorCount];
  37.         byte[] lGreenPixel  = new byte[lArrayLength /cColorCount];
  38.         byte[] lBluePixel   = new byte[lArrayLength /cColorCount];     
  39.        
  40.         if(cIsVerbose)
  41.             System.out.println("Starting splitting an array of Length: " + lArrayLength);
  42.        
  43.        
  44.         /* DO THE ACTUAL SPLITTING */
  45.         for(int i = 0; i < lArrayLength; i += cColorCount)
  46.         {          
  47.             lRedPixel[i /cColorCount]   = input[i +cRedOffset];
  48.             lGreenPixel[i /cColorCount] = input[i +cGreenOffset];
  49.             lBluePixel[i /cColorCount]  = input[i +cBlueOffset];           
  50.         }
  51.        
  52.         /* WRITE RESULTS TO THE GIVEN FILES */
  53.         writeBinaryFile(pRedFileName,   lRedPixel);
  54.         writeBinaryFile(pGreenFileName, lGreenPixel);
  55.         writeBinaryFile(pBlueFileName,  lBluePixel);
  56.        
  57.         if(cIsVerbose)
  58.             System.out.println("Splitted the Array succesfully.\n" +
  59.                                        "Wrote the results to: " + pRedFileName + " (red pixels), "
  60.                                                                 + pGreenFileName + "(green pixels), "
  61.                                                                 + pBlueFileName + "(blue pixels)");
  62.     }
  63. /* ################################################################################################### */
  64.    
  65.    
  66. /* ################################################################################################### */
  67. /* BINARY FILE METHODS */
  68.     /**
  69.      * Read in the given Binary file
  70.      * @param aFileName
  71.      * @return array of read bytes
  72.      * @throws IOException
  73.      */
  74.     static byte[] readBinaryFile(String aFileName) throws IOException {
  75.         Path path = Paths.get(aFileName);
  76.         return Files.readAllBytes(path);
  77.     }
  78.    
  79.     /**
  80.      * Writes the given array to the specified File
  81.      * @param aFileName
  82.      * @param pContent
  83.      * @throws IOException
  84.      */
  85.     static void writeBinaryFile(String aFileName, byte[] pContent) throws IOException {
  86.         Path path = Paths.get(aFileName);
  87.         Files.write(path, pContent);   
  88.     }  
  89. /* ################################################################################################### */  
  90.  
  91.     /**
  92.      * @param args
  93.      * @throws IOException
  94.      */
  95.     public static void main(String[] args)  {
  96.        
  97.         if(args[0].equals("-d"))
  98.         {      
  99.             /*
  100.              * args[0] =    "-d"
  101.              * args[1] =    input file
  102.              * args[2] =    red output file
  103.              * args[3] =    green output file
  104.              * args[4] =    blue output file
  105.              */    
  106.            
  107.             try {
  108.                
  109.                 /* READ IN ALL BYTES */
  110.                 if(cIsVerbose)
  111.                     System.out.println("Read the file: " + args[1]);
  112.                 byte[] buffer = readBinaryFile(args[1]);
  113.                
  114.                 /* WRITE TO THE ACCORDING FILES */
  115.                 if(cIsVerbose)
  116.                     System.out.println("Successfully read the file.");
  117.                 SplitPixel(buffer, args[2], args[3], args[4]);
  118.  
  119.    
  120.             } catch (Exception e)
  121.             {
  122.                 System.out.println(e);
  123.             }
  124.         }
  125.         else if(args[0].equals("-c"))
  126.         {
  127.             /*
  128.              * args[0] =    "-c"
  129.              */
  130.            
  131.             /* DO WHATEVER -c HAS TO DO */
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement