hppicture.java

来自「一个简单的visio程序。」· Java 代码 · 共 527 行

JAVA
527
字号
package HPCore.stdfunc;

import HPCore.Exception.*;
import HECore.stddata.*;
import java.io.*;
import java.awt.*;
import java.net.*;
import java.awt.image.*;
import HAB.object.*;

public class hppicture {

   //public static Image image =null;
   public static final int BI_RGB = 0;
   public static final int BI_RLE8 = 1;
   public static final int BI_RLE4 = 2;

   public static Picture LOADPICTURE(String picname) throws HpException
   {
      Picture pic = new Picture();
      Image image =null;

	  if (picname == null)
	  {
		  pic.image = null;
		  return pic;
	  }
	  //if (picname== null)
	  //    throw new HpException(13,"Type mismatch");

	  //String picname=picnm.strValue();

	  image = loadpicture(picname);

	  if (image == null && !picname.equals(""))
	      throw new HpException(481,"Invalid picture");

      pic.image = image;

      return pic;

   }

   public static  Image  loadpicture(String pname ) throws HpException
   {
      char fseparator = System.getProperty("file.separator").charAt(0);

		pname = pname.trim().replace('\\',fseparator);

      Image image=null;

		if (pname.equals(""))
			return image;

      boolean ishttp = false;
      boolean isfile = false;
      String picname=pname.toLowerCase();

      if (picname.charAt(4) == ':')
      {
         isfile = true;
         if ((picname.substring(0,4)).equals("http"))
            ishttp = true;
      }

		URL url =null;
      File inf=null;
      if (!isfile)
      {

	      if (picname.indexOf("/") == -1 && picname.indexOf(File.separator) == -1)
		     pname = System.getProperty("user.dir")+File.separator+pname;
		   inf = new File(pname);

		   if (!inf.exists())
			   throw new HpException(53,"File not found");
		   if (inf.isDirectory() || !inf.isFile())
			   throw new HpException(75,"Path or File access error");
      }else
      {
         try{
            url = new URL(pname);
         }catch(Exception ce){
   	      throw new HpException(481,"Invalid picture");
         }
      }

      try
      {
         if(!isfile || ishttp)
         {
            InputStream inStream;
            if (ishttp)
               inStream = url.openConnection().getInputStream();
            else
			      inStream = (InputStream)(new FileInputStream(inf));
			   DataInputStream in = new DataInputStream(inStream);
			   if (in.read() == 'B' && in.read() == 'M')
			   {
				   InputStream inS;
               if (ishttp)
                  inS = url.openStream();
               else
			         inS = (InputStream)(new FileInputStream(inf));
				   //if(f!=null)
					//   image = f.getToolkit().createImage(getBMPImage(inS));
				   //else
					image = Toolkit.getDefaultToolkit().createImage(getBMPImage(inS));
			   }else
			   {
				   InputStream inS;
               if (ishttp)
                  inS = url.openStream();
               else
			         inS = (InputStream)(new FileInputStream(inf));
				   DataInputStream iin = new DataInputStream(inS);
				   int Reserved = intelShort(iin.readUnsignedShort());      //should be zero
				   if(Reserved == 0)
				   {
					   int ResourceType = intelShort(iin.readUnsignedShort());  //icon resource has to be 1.
					   if(ResourceType == 1 || ResourceType == 2)
					   {
						   //InputStream inStm = (InputStream)(new FileInputStream(inf));
					      InputStream inStm;
                     if (ishttp)
                        inStm = url.openStream();
                     else
			               inStm = (InputStream)(new FileInputStream(inf));
						   //if(f!=null)
							//   image = f.getToolkit().createImage(getICOImage(inStm));
						   //else
							image = Toolkit.getDefaultToolkit().createImage(getICOImage(inStm));
					   }
					   else
						   throw new HpException(481,"Invalid picture");
				   }
				   else
				   {
					   if (!ishttp && picname.charAt(4) != ':')
					   {
						   if (picname.indexOf(File.separator) != -1)
						   {
							   pname = "file:///"+pname;
						   }
						   else
							   pname = "file:///"+File.separator+pname;
					   }
					   //InputStream inStm = (InputStream)(new FileInputStream(inf));
					   InputStream inStm;
                  if (ishttp)
                     inStm = url.openStream();
                  else
			            inStm = (InputStream)(new FileInputStream(inf));
					   DataInputStream gin = new DataInputStream(inStm);
					   if (gin.read() == 'G' && gin.read() == 'I' && gin.read() == 'F')
					   {
                     if (!ishttp)
							   url = new URL(pname);

						   //if (f != null)
							//   image = f.getToolkit().getImage(url);
						   //else
							image = Toolkit.getDefaultToolkit().getImage(url);
					   }
					   else
					   {
						   //InputStream inStrm = (InputStream)(new FileInputStream(inf));
					      InputStream inStrm;
                     if (ishttp)
                        inStrm = url.openStream();
                     else
			               inStrm = (InputStream)(new FileInputStream(inf));
						   DataInputStream jin = new DataInputStream(inStrm);
						   jin.readInt();
						   if (jin.readUnsignedShort() == 16 && jin.read() == 'J')
						   {
                           if (!ishttp)
								     url = new URL(pname);

								   //if (f != null)
									//   image = f.getToolkit().getImage(url);
								   //else
									image = Toolkit.getDefaultToolkit().getImage(url);
						   }else
							   throw new HpException(481,"Invalid picture");
					   }
				   }
			   }
         }
         else
         {
				//if (f != null)
				//	image = f.getToolkit().getImage(url);
				//else
				image = Toolkit.getDefaultToolkit().getImage(url);
         }
		}catch (IOException io){
			image = null;
			throw new HpException (7,"Out of memory");
         //System.out.println(io.getMessage());

      }catch (Exception e) {
			image = null;
	      throw new HpException(481,"Invalid picture");
		}

      return image;
   }


   public static ImageProducer getBMPImage(InputStream stream) throws IOException,Exception
   {
	DataInputStream in = new DataInputStream(stream);
	in.read();
	in.read();

	int fileSize = intelInt(in.readInt());

	in.readUnsignedShort();
	in.readUnsignedShort();

	int bitmapOffset = intelInt(in.readInt());

	int bitmapInfoSize = intelInt(in.readInt());

	int width = intelInt(in.readInt());
	int height = intelInt(in.readInt());

	in.readUnsignedShort();

	int bitCount = intelShort(in.readUnsignedShort());

	int compressionType = intelInt(in.readInt());

	int imageSize = intelInt(in.readInt());

	in.readInt();
	in.readInt();

	int colorsUsed = intelInt(in.readInt());
	int colorsImportant = intelInt(in.readInt());

	int pixels[] = null;
	try{
		pixels = new int[width * height];
	}catch(java.lang.OutOfMemoryError ou)
	{
		throw new IOException("Out of memory");
	}

	if (colorsUsed == 0)
		colorsUsed = 1 << bitCount;

	if (compressionType == BI_RGB && bitCount == 24)
	{
			readRGB24(width, height, pixels, in);
   } else
	{
		int colorTable[] = null;
		try{
			colorTable = new int[colorsUsed];
		}catch(java.lang.OutOfMemoryError ou)
		{
			throw new IOException("Out of memory");
		}

      try{
         for (int i=0; i < colorsUsed; i++) {
			    colorTable[i] = (intelInt(in.readInt()) & 0xffffff) + 0xff000000;
		   }
      }catch(Exception e){
         throw new Exception("Invalid picture");
      }

		if (compressionType == BI_RGB && bitCount != 24)
		{
			readRGB(width, height, colorTable, bitCount,
				pixels, in);
		} else if (compressionType == BI_RLE8) {
			readRLE(width, height, colorTable, bitCount,
					pixels, in, imageSize, 8);
		} else if (compressionType == BI_RLE4) {
			readRLE(width, height, colorTable, bitCount,
			pixels, in, imageSize, 4);
		}
	}

	return new MemoryImageSource(width, height, pixels, 0,
	    width);
   }

   protected static void readRGB24(int width, int height, int pixels[],
		DataInputStream in)throws IOException
   {

		for (int h = height-1; h >= 0; h--) {
			 int pos = h * width;
			 for (int w = 0; w < width; w++) {

				int red = in.read();
				int green = in.read();
				int blue = in.read();

				pixels[pos++] = 0xff000000 + (blue << 16) +
					(green << 8) + red;
			 }
		}
   }

    protected static void readRGB(int width, int height, int colorTable[],
	int bitCount, int pixels[], DataInputStream in)
    throws IOException
    {

	int pixelsPerByte = 8 / bitCount;

	int bitMask = (1 << bitCount) - 1;

	int bitShifts[] = new int[pixelsPerByte];

	for (int i=0; i < pixelsPerByte; i++) {
	    bitShifts[i] = 8 - ((i+1) * bitCount);
	}

	int whichBit = 0;

	int currByte = in.read();

	for (int h=height-1; h >= 0; h--) {
	    int pos = h * width;
	    for (int w=0; w < width; w++) {

		pixels[pos] = colorTable[
		    (currByte >> bitShifts[whichBit]) &
		    bitMask];
		pos++;
		whichBit++;

		if (whichBit >= pixelsPerByte) {
		    whichBit = 0;
		    currByte = in.read();
		}
	    }
	}
    }

    protected static void readRLE(int width, int height, int colorTable[],
	int bitCount, int pixels[], DataInputStream in,
	int imageSize, int pixelSize)
    throws IOException
    {
	int x = 0;
	int y = height-1;


	for (int i=0; i < imageSize; i++) {

	    int byte1 = in.read();
	    int byte2 = in.read();
	    i += 2;

	    if (byte1 == 0) {

		if (byte2 == 0) {
		    x = 0;
		    y--;

		} else if (byte2 == 1) {
		    return;

		} else if (byte2 == 2) {
		    int xoff = (char) intelShort(
			in.readUnsignedShort());
		    i+= 2;
		    int yoff = (char) intelShort(
			in.readUnsignedShort());
		    i+= 2;
		    x += xoff;
		    y -= yoff;

		} else {
		    int whichBit = 0;

		    int currByte = in.read();

		    i++;
		    for (int j=0; j < byte2; j++) {

			if (pixelSize == 4) {
			    if (whichBit == 0) {
				pixels[y*width+x] = colorTable[(currByte >> 4)
				    & 0xf];
			    } else {


				pixels[y*width+x] = colorTable[currByte & 0xf];
				currByte = in.read();
				i++;
			    }
			} else {
			    pixels[y*width+x] = colorTable[currByte];
			    currByte = in.read();
			    i++;
			}
			x++;
			if (x >= width) {
			    x = 0;
			    y--;
			}
		    }
		    if ((byte2 & 1) == 1) {
			in.read();
			i++;
		    }
		}


	    } else {
		for (int j=0; j < byte1; j++) {

		   if (pixelSize == 4) {
		       if ((j & 1) == 0) {
			   pixels[y*width+x] = colorTable[(byte2 >> 4) & 0xf];
		       } else {
			   pixels[y*width+x+1] = colorTable[byte2 & 0xf];
		       }
		   } else {
		       pixels[y*width+x+1] = colorTable[byte2];
		   }
		   x++;
		   if (x >= width) {
		       x = 0;
		       y--;
		   }
		}
	    }
	}
    }

    protected static int intelShort(int i)
    {
	return ((i >> 8) & 0xff) + ((i << 8) & 0xff00);
    }

   protected static int intelInt(int i)
   {
	return ((i & 0xff) << 24) + ((i & 0xff00) << 8) +
	    ((i & 0xff0000) >> 8) + ((i >> 24) & 0xff);
   }


   public static ImageProducer getICOImage(InputStream stream) throws IOException
   { // Tang tusheng add the method for ICON file and CURSOR file[12/02/97]
	DataInputStream in = new DataInputStream(stream);


	int Reserved = intelShort(in.readUnsignedShort());      //should be zero
	if(Reserved != 0)
		throw new IOException("Not a .ico or .cur file");
	int ResourceType = intelShort(in.readUnsignedShort());  //icon resource has to be 1.
	if(ResourceType != 1 && ResourceType != 2)
		throw new IOException("Not a .ico or .cur file");
	int ResourceCount = intelShort(in.readUnsignedShort()); //number of images contained in the file

	int width = in.read();
	int height = in.read();
	int ColorCount = in.read();
	in.read();
	in.readInt();
	int IcoSize = intelInt(in.readInt());
	int imageOffset = intelInt(in.readInt());
	in.skipBytes(imageOffset - 22);
	int imagewidth = in.readInt();
	int imageheight = in.readInt();

	in.readInt();
	in.readUnsignedShort();

	int bitCount = intelShort(in.readUnsignedShort());

	int compressionType = intelInt(in.readInt());

	int imageSize = intelInt(in.readInt());

	in.readInt();
	in.readInt();

	int colorsUsed = intelInt(in.readInt());
	int colorsImportant = intelInt(in.readInt());
	if (colorsUsed == 0) colorsUsed = 1 << bitCount;


	int colorTable[] = new int[colorsUsed];

	for (int i=0; i < colorsUsed; i++) {
	    colorTable[i] = (intelInt(in.readInt()) & 0xffffff) + 0xff000000;
	}

	int pixels[] = new int[width * height];

	if (compressionType == BI_RGB)
	{
	 if (bitCount == 24)
	 {
		readRGB24(width, height, pixels, in);
	 }
	 else
	 {
		readRGB(width, height, colorTable, bitCount,pixels, in);
	 }
	}
	else
	if (compressionType == BI_RLE8)
	{
	    readRLE(width, height, colorTable, bitCount,pixels, in, imageSize, 8);
	}
	else
	 if (compressionType == BI_RLE4)
	{
	    readRLE(width, height, colorTable, bitCount,pixels, in, imageSize, 4);
	}

	return new MemoryImageSource(width, height, pixels, 0,width);
   }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?