/*************************************************************************************

This file is part of FragMend.

Written by Florian Buchholz, Glenn Henderson, David Horvath, and Jeff Jones.

Copyright (c) 2006, Florian Buchholz, Glenn Henderson, David Horvath, and Jeff Jones.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.     
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.       
    * Neither the name of Florian Buchholz, Glenn Henderson, David Horvath, Jeff
      Jones, nor the names of its contributors may be used to endorse or promote
      products derived from this software without specific prior written
      permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*************************************************************************************/

public class JPGUtilities {
	
	public static boolean containsHeader(byte[] data) {
	
		return ((data[0] == -1) &&   // 0xff
			(data[1] == -40) && //0xd8
			(data[2] == -1) &&
			(data[3] == -32) && // 0xe0
			(data[6] == 0x4a) &&
			(data[7] == 0x46) &&
			(data[8] == 0x49) &&
			(data[9] == 0x46) &&
			(data[10] == 0x00));
		
	}

	public static boolean containsFooter(byte[] data) {
		
		for (int i = 0; i < data.length-1; i++) {
			
			if ((data[i] == -1) && (data[i+1] == -39))
				return true;
			
		}
		
		return false;
	}
	
	public static int getMarkerCount(byte[] data) {
		
		int count = 0;
		
		for (int i = 0; i < data.length-1; i++) {
			if (data[i] == -1) { // 0xff
			
				switch (data[i+1]) {
					case -37: // 0xdb
					case -60: // 0xc4
					case -64: // 0xc0
					case -38: // 0xda
					case -2: // 0xfe
					case -39: // 0xd9
						count++;
				}
				
			}
		}
		
		return count;
		
	}
	
	public static int getDataMarkerCount(byte[] data) {
		
		int count = 0;
		
		for (int i = 0; i < data.length-1; i++) {
			if (data[i] == -1) { // 0xff
			
				if ((data[i+1] == 0) || ((data[i+1] <= -41) && (data[i+1] >= -48)))
					count++;
			}
		}
		
		return count;
		
	}
	
	public static int getSize(byte[] data) {
		
		int current_pos;
		byte active_marker;
		int marker_pos;
		int marker_size;
		
		if (!((data[0] == -1) && (data[1] == -40))) {
			System.err.println("No JPG header");
			return -1;
		}
		
		current_pos = 2;
		active_marker = data[1];
		marker_pos = 0;
		marker_size = 2;
		
		while (current_pos < data.length-1) {
			
			if (data[current_pos] != -1) {
				System.err.println("JPG Marker expected but not found at byte " + current_pos);
				System.err.println(String.format("Last valid marker: %x of size %d at byte %d",
									active_marker, marker_size, marker_pos));
				return -1;
			}
			
			marker_pos = current_pos;
			active_marker = data[current_pos+1];
			if (active_marker == -39)
				return current_pos+1;
								
			if (isStandAlone(active_marker))
				marker_size = 0;
			else {
				if (current_pos > data.length-3) {
					System.err.println("End of file before marker end");
					System.err.println(String.format("Last valid marker: %x of size %d at byte %d",
									active_marker, marker_size, marker_pos));
					return -1;
				}
				
				marker_size = (((int)data[current_pos+2]&0xff) << 8) + 
						((int)data[current_pos+3]&0xff);
			}
			
			current_pos += marker_size + 2;
			
			if (active_marker == -38) // SOS marker
				break;

		}
			
		byte allowed_restart_marker = -48;  // 0xd0
		
		while (current_pos < data.length-1) {
			
			if ((data[current_pos] == -1) && (current_pos < data.length-2)) {
				if (data[current_pos+1] == -39)
					return current_pos+1;
				else if (data[current_pos+1] == allowed_restart_marker) {
					allowed_restart_marker++;
					if (allowed_restart_marker > -41)
						allowed_restart_marker = -48;
				}
				else if (data[current_pos+1] != 0) {
					System.err.println("Unexpected marker found in scan data at byte " + current_pos);
					System.err.println("Marker found: " + data[current_pos+1] + " allowed markers: 0, -39, " + allowed_restart_marker);
					return -1;
				}
			}
			current_pos++;
		}
		
		System.err.println("No EOI marker found");
		return -1;
		
	}
	
	private static boolean isStandAlone(byte marker) {
		return ((marker == 1) || ((marker >= -48) && (marker <= -39)));
	}
}

