⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pngloader.java

📁 一款基于java 的赛车类游戏 一款基于java 的赛车类游戏
💻 JAVA
字号:
/*
 * Created on 2005-02-10
 *  
 */

//package com.nano.KangooJumper;

import java.io.*;
import javax.microedition.lcdui.*;

/**
 * @author diament PNGLoader
 * @version 1.1 modified by plumkawka to support palette offset and palette from
 *          array
 *  
 */
public class PNGLoader
{

	private static final int	IDAT_CHUNK			= ((int) 'I' << 24) | ((int) 'D' << 16) | ((int) 'A' << 8) | ((int) 'T');
	private static final int	PALETTE_CHUNK		= ((int) 'P' << 24) | ((int) 'L' << 16) | ((int) 'T' << 8) | ((int) 'E');
	private static final int	ENDMARK				= ((int) 'I' << 24) | ((int) 'E' << 16) | ((int) 'N' << 8) | ((int) 'D');

	private static boolean		IsCRCTableComputed	= false;
	private static int[]		CRCTable			= new int[256]; // Table of CRCs of all messages.	

	protected byte				buffer[]			= null;
	protected byte				plteRemaps[]		= null;
	protected int				size				= 0;
	protected int				plteDataOffset		= 0;
	protected int				plteSize			= 0;
	protected int				imageDataOffset		= 0;
	protected int				imageSize			= 0;



	PNGLoader(int name)
	{

		try
		{

			
			//DataInputStream ind = new DataInputStream(name.getClass().getResourceAsStream(name));
			DataInputStream ind = new DataInputStream(Utils.openStream(name));
			
			// Determine the filesize
			//
			size = 8;
			ind.skip(8);

			int ChnkType, ChnkLen;

			do
			{
				ChnkLen = ind.readInt();
				ChnkType = ind.readInt();

				if (ChnkType == PALETTE_CHUNK)
				{
					plteDataOffset = size + 8;
					plteSize = ChnkLen;
				}

				if (ChnkType == IDAT_CHUNK)
				{
					imageDataOffset = size + 8;
					imageSize = ChnkLen;
				}

				size += ChnkLen + 12;

				ind.skip(ChnkLen + 4); // Skip past data and the CRC

			} while (ENDMARK != ChnkType);

			ind.close();

			buffer = new byte[size];

			//ind = new DataInputStream(name.getClass().getResourceAsStream(name));
			ind = new DataInputStream(Utils.openStream(name));
			size = ind.read(buffer, 0, size);
			ind.close();
		}
		catch (Exception e)
		{
		}
	}



	PNGLoader(int name, int paletteName, int numRemaps)
	//PNGLoader(String name, String paletteName, int numRemaps)
	{
		//plteRemaps = new byte[numRemaps * 4];

		try
		{
			// read palette
			plteRemaps = Utils.loadByteArray(paletteName);

			//DataInputStream ind = new DataInputStream(name.getClass().getResourceAsStream(name));
			DataInputStream ind = new DataInputStream(Utils.openStream(name));

			// Determine the filesize
			//
			size = 8;
			ind.skip(8);

			int ChnkType, ChnkLen;

			do
			{
				ChnkLen = ind.readInt();
				ChnkType = ind.readInt();

				if (ChnkType == PALETTE_CHUNK)
				{
					plteDataOffset = size + 8;
					plteSize = ChnkLen;
				}

				if (ChnkType == IDAT_CHUNK)
				{
					imageDataOffset = size + 8;
					imageSize = ChnkLen;
				}

				size += ChnkLen + 12;

				ind.skip(ChnkLen + 4); // Skip past data and the CRC

			} while (ENDMARK != ChnkType);

			ind.close();

			buffer = new byte[size];

			ind = new DataInputStream(Utils.openStream(name));
			//ind = new DataInputStream(name.getClass().getResourceAsStream(name));
			size = ind.read(buffer, 0, size);
			ind.close();
		}
		catch (Exception e)
		{
		}

		if (0 != plteDataOffset)
		{
			if (null != plteRemaps)
			{
				for (int k = 0; k < numRemaps; k++)
				{
					int pos = 4 * k;
					int index = plteRemaps[pos] * 3;

					if (index + 3 > plteSize)
					{
						break;
					}

					index += plteDataOffset;

					buffer[index] = plteRemaps[pos + 1];
					buffer[index + 1] = plteRemaps[pos + 2];
					buffer[index + 2] = plteRemaps[pos + 3];
				}
			}

			int CRC = getCRC(buffer, plteDataOffset - 4, plteSize + 4); // PLTE
			// marker
			// included

			buffer[plteDataOffset + plteSize] = (byte) ((CRC >> 24) & 0xFF);
			buffer[plteDataOffset + plteSize + 1] = (byte) ((CRC >> 16) & 0xFF);
			buffer[plteDataOffset + plteSize + 2] = (byte) ((CRC >> 8) & 0xFF);
			buffer[plteDataOffset + plteSize + 3] = (byte) ((CRC) & 0xFF);
		}
	}



	public void remapPalette(short[] pal, int ofs, int num)
	{
		if (0 != plteDataOffset)
		{

			int index = plteDataOffset + (ofs * 3);
			int pos = 0;
			for (int k = 0; k < num; k++)
			{

				buffer[index++] = (byte) (pal[pos++] & 255);
				buffer[index++] = (byte) (pal[pos++] & 255);
				buffer[index++] = (byte) (pal[pos++] & 255);
			}

			int CRC = getCRC(buffer, plteDataOffset - 4, plteSize + 4); // PLTE
			// marker
			// included

			buffer[plteDataOffset + plteSize] = (byte) ((CRC >> 24) & 0xFF);
			buffer[plteDataOffset + plteSize + 1] = (byte) ((CRC >> 16) & 0xFF);
			buffer[plteDataOffset + plteSize + 2] = (byte) ((CRC >> 8) & 0xFF);
			buffer[plteDataOffset + plteSize + 3] = (byte) ((CRC) & 0xFF);
		}

	}



	public Image getImage()
	{
		if (null == buffer)
			return null;
		else
			return Image.createImage(buffer, 0, size);
	}



	private static void makeCRCTable()
	{
		int c, n, k;

		for (n = 0; n < 256; n++)
		{
			c = n;
			for (k = 0; k < 8; k++)
			{
				if ((c & 1) == 1)
					c = 0xedb88320 ^ (c >>> 1);
				else
					c = c >>> 1;
			}

			CRCTable[n] = c;
		}

		IsCRCTableComputed = true;
	}



	private static int getCRC(byte[] buf, int offset, int len)
	{
		if (!IsCRCTableComputed)
			makeCRCTable();

		int crc = 0xffffffff; //init to all 1's
		int end = offset + len;
		for (int n = offset; n < end; n++) // run thru specified bytes in buf
		{
			crc = CRCTable[(crc ^ buf[n]) & 0xff] ^ (crc >>> 8);
		}
		return crc ^ 0xffffffff; //send the 1's complement of the final value
	}

}

⌨️ 快捷键说明

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