/*******************************************************************************
* Copyright (c)                                                                *
*           2005   Tristan Allwood,                                            *
*           2005   Daniel Burke,                                               *
*           2005   Marc Hull,                                                  *
*           2005   Ekaterina Itskova,                                          *
*           2005   Steve Zymler                                                *
*                                                                              *
* Permission is hereby granted, free of charge, to any person obtaining a      *
* copy of this software and associated documentation files (the                *
* "Software"), to deal in the Software without restriction, including          *
* without limitation the rights to use, copy, modify, merge, publish,          *
* distribute, sublicense, and/or sell copies of the Software, and to           *
* permit persons to whom the Software is furnished to do so, subject to        *
* the following conditions:                                                    *
*                                                                              *
* The above copyright notice and this permission notice shall be included      *
* in all copies or substantial portions of the Software.                       *
*                                                                              *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS      *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF                   *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.       *
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY         *
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,         *
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE            *
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                       *
*******************************************************************************/
/*
 * Created on 24-Dec-2004
 *
 */

package uk.ac.ic.doc.kevlar.gui.internal.caches;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Display;

/**
 * @author Kate
 *
 */

public class Converter {
	
	public static ImageData convertToImage(String hexData)
	{
		/*
		 * Takes a string of hex digits and converts them into an image
		 * by first converting it into a byte array
		 * then converting a byte array into an InputStream
		 */
		byte[] data = new byte[hexData.length()/2];
		
		for (int i=0; i<hexData.length(); i+=2)
		{
			byte temp = 0;
			for (int j = 0; j<2; j++)
			{
				char c = hexData.charAt(i+j);
				
				if (c>='0' && c<='9')
				{
					temp = (byte) ((temp << 4) | (c -'0'));
				}
				else
				{
					temp = (byte)((temp<<4) | ((c -'A')+10));
				}
				
			}
			
			data[i/2]=temp;
		}
		
		ImageData imageData = new ImageData(new ByteArrayInputStream(data));
//		Image image = new Image(Display.getCurrent(), imageData);
		
		//return null;
		return imageData;
	} 
	
	public static String convertToString(byte[] imageData)
	{
		/* 
		 * Takes a byte array of image data
		 * and converts it into a string of hex digits
		 */

		char converted[] = new char[imageData.length << 1];
		String hexString;
		
		for (int i = 0; i<imageData.length; i++)
		{
			byte nextByte = imageData[i];
			
			char c;
			
			for (int stringPart = 1; stringPart>=0; stringPart--)
			{
				int part = (nextByte >> (stringPart*4)) & 0xF;
				
				if (part >= 10)
				{
					c = (char)('A'+(part-10));
				}
				else
				{
					c = (char)('0'+part);
				}
				
				converted[(i << 1) | ((~stringPart) & 1)] = c;
			}
			
			
		}
		
		hexString = new String(converted);
		return hexString;
	}

}
