import org.apache.commons.io.FilenameUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
        if (args.length != 1) {
            System.out.println("Wrong number of arguments");
            return;
        }

        BufferedImage sourceImage;

        try {
            sourceImage = ImageIO.read(new File(args[0]));
        } catch (IOException e) {
            System.out.println("Cannot read '" + args[0] + "'");
            return;
        }

        String baseFilename = FilenameUtils.removeExtension(args[0]);
        //System.out.println(args[0]);
        //System.out.println(baseFilename);

        ArrayList<Byte> outputData = new ArrayList<Byte>();
        ArrayList<Byte> uncompressedData = new ArrayList<Byte>();
        ArrayList<Byte> outputPalette = new ArrayList<Byte>();
        ArrayList<Color> uncompressedPalette = new ArrayList<Color>();
        Color pixel;

        for (int y = 0; y < sourceImage.getHeight(); y++)
            for (int x = 0; x < sourceImage.getWidth(); x++) {
                // Add color to the uncompressed palette
                // If the color is a duplicate to one that already exists, do not add it
                pixel = new Color(sourceImage.getRGB(x, y));
                if (!uncompressedPalette.contains(pixel)) {
                    uncompressedPalette.add(pixel);

                    // Add color to the output palette
                    outputPalette.add((byte) pixel.getRed());
                    outputPalette.add((byte) pixel.getGreen());
                    outputPalette.add((byte) pixel.getBlue());
                }

                // Add the index number of the palette entry to the compressed data array
                uncompressedData.add((byte) uncompressedPalette.indexOf(pixel));
            }

        /*for (Color color : uncompressedPalette) {
            System.out.println(color.toString());
        }*/

        /*System.out.println();

        int rgbCounter = 1;
        for (Byte rgbValue : outputPalette) {
            if (rgbCounter == 1) {
                System.out.print(" R=" + String.format("%x", rgbValue));
                rgbCounter++;
            } else if (rgbCounter == 2) {
                System.out.print(" G=" + String.format("%x", rgbValue));
                rgbCounter++;
            } else {
                System.out.println(" B=" + String.format("%x", rgbValue));
                rgbCounter = 1;
            }
        }

        for (int y = 0; y < sourceImage.getHeight(); y++) {
            for (int x = 0; x < sourceImage.getWidth(); x++) {
                System.out.print(String.format("%x", uncompressedData.get(y * sourceImage.getHeight() + x)));
            }
            System.out.println();
        }*/

        byte compressedByte;

        for (int i = 0; i < uncompressedData.size(); i += 2) {
            //noinspection RedundantCast
            compressedByte = (byte) (uncompressedData.get(i) << 4);
            compressedByte += uncompressedData.get(i + 1);
            outputData.add(compressedByte);
        }

        /*System.out.println();

        for (int y = 0; y < sourceImage.getHeight() / 2; y++) {
            for (int x = 0; x < sourceImage.getWidth() / 2; x++) {
                System.out.print(String.format("%x", outputData.get(y * (sourceImage.getHeight() / 2) + x)));
            }
            System.out.println();
        }*/

        String paletteFilename = baseFilename + ".pal";
        DataOutputStream paletteFileStream;
        try {
            paletteFileStream = new DataOutputStream(new FileOutputStream(paletteFilename));
        } catch (FileNotFoundException fileNotFound) {
            System.out.println("Can't write to " + paletteFilename + "; can't open or create file");
            return;
        } catch (SecurityException security) {
            System.out.println("Can't write to " + paletteFilename + "; operating system security manager denies write permission");
            return;
        }

        String imageFilename = baseFilename + ".pxl";
        DataOutputStream imageFileStream;
        try {
            imageFileStream = new DataOutputStream(new FileOutputStream(imageFilename));
        } catch (FileNotFoundException fileNotFound) {
            System.out.println("Can't write to " + imageFilename + "; can't open or create file");
            return;
        } catch (SecurityException security) {
            System.out.println("Can't write to " + imageFilename + "; operating system security manager denies write permission");
            return;
        }

        System.out.println("Writing palette data to " + paletteFilename + "...");
        try {
            for (Byte data : outputPalette) {
                paletteFileStream.writeByte(data);
            }
        } catch (IOException io) {
            System.out.println("An error occurred when writing to " + paletteFilename + " - " + io.getMessage());
            return;
        }

        System.out.println("Writing image data to " + imageFilename + "...");
        try {
            for (Byte data : outputData) {
                imageFileStream.writeByte(data);
            }
        } catch (IOException io) {
            System.out.println("An error occurred when writing to " + imageFilename + " - " + io.getMessage());
            return;
        }

        System.out.println("Operation completed successfully");
    }
}

@@ -684,91 +684,8 @@
 0%5D);
-%0A        //System.out.println(args%5B0%5D);%0A        //System.out.println(baseFilename);
 %0A%0A  
@@ -1866,1602 +1866,493 @@
     
-/*for (Color color : uncompressedPalette) %7B%0A            System.out.println(color.toString());%0A        %7D*/%0A%0A        /*System.out.println();%0A%0A        int rgbCounter = 1;%0A        for (Byte rgbValue : outputPalette) %7B%0A            if (rgbCounter == 1) %7B%0A                System.out.print(%22 R=%22 + String.format(%22%25x%22, rgbValue));%0A                rgbCounter++;%0A            %7D 
+byte compressedByte;%0A%0A        // Squeeze two pix
 els
-e
  i
-f (rgbCounter == 2) %7B%0A                System.out.print(%22 G=%22 + String.format(%22%25x%22, rgbValue));%0A                rgbCounter++;%0A            %7D else %7B%0A                System.out.println(%22 B=%22 + String.format(%22%25x%22, rgbValue));%0A                rgbCounter = 1;%0A            %7D%0A        %7D%0A%0A        for (int y = 0; y %3C sourceImage.getHeight(); y++) %7B%0A            for (int x = 0; x %3C sourceImage.getWidth(); x++) %7B%0A                System.out.print(String.format(%22%25x%22, uncompressedData.get(y * sourceImage.getHeight() + x)));%0A            %7D%0A            System.out.println();%0A        %7D*/%0A%0A        byte compressedByte;%0A%0A        for (int i = 0; i %3C uncompressedData.size(); i += 2) %7B%0A            //noinspection RedundantCast%0A            compressedByte = (byte) (uncompressedData.get(i) %3C%3C 4);%0A            compressedByte += uncompressedData.get(i + 1);%0A            outputData.add(compressedByte);%0A        %7D%0A%0A        /*System.out.println();%0A%0A        for (int y = 0; y %3C sourceImage.getHeight() / 2; y++) %7B%0A            for (int x = 0; x %3C sourceImage.getWidth() / 2; x++) %7B%0A                System.out.print(String.format(%22%25x%22, outputData.get(y * (sourceImage.getHeight() / 2) + x)));%0A            %7D%0A            System.out.println();%0A        %7D*/%0A
+nto one byte: leftmost pixel in the high nibble, rightmost in the low nibble%0A        for (int i = 0; i %3C uncompressedData.size(); i += 2) %7B%0A            //noinspection RedundantCast%0A            compressedByte = (byte) (uncompressedData.get(i) %3C%3C 4);%0A            compressedByte += uncompressedData.get(i + 1);%0A            outputData.add(compressedByte);%0A        %7D%0A%0A        // Create or open the file to which to write the palette data
 %0A   
@@ -2934,32 +2934,111 @@
 urn;%0A        %7D%0A%0A
+        // Create or open the file to which to write the compressed image data%0A
         String i
@@ -3595,32 +3595,70 @@
 urn;%0A        %7D%0A%0A
+        // Write palette data to file%0A
         System.o
@@ -4020,32 +4020,79 @@
 urn;%0A        %7D%0A%0A
+        // Write compressed image data to file%0A
         System.o

Remove commented code blocks, add comments