You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
5.2 KiB

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;
public class TextureData
{
public static int lastId = 0;
public string type { get { return _type; } }
public int id { get { return _id; } }
public Texture2D texture { get { return _tex; } }
public string filePath { get { return _filePath; } }
//private bool _is_decoded = false;
public TextureData(string filePath)
{
_id = lastId;
_filePath = filePath;
//string tmp = "YYYY-MM-DD-hh-mm-ss-fff-";
//int b = _filePath.LastIndexOf("\\")+tmp.Length;
int e = _filePath.LastIndexOf(".");
int b = _filePath.LastIndexOf("+",e);
if (b != -1 && e != -1 && b < e)
_type = _filePath.Substring(b + 1, e - b - 1);
}
public override bool Equals(object obj)
{
if (obj == null || !(obj is TextureData))
return false;
else
return this.id == ((TextureData)obj).id;
}
public override int GetHashCode()
{
return this.id;
}
public override string ToString()
{
return _type+": "+_filePath;
}
public bool loadToMemory(int maxSize = 0)
{
try
{
#if false
FileStream fs = File.Open(_filePath, FileMode.Open);
System.Drawing.Image img = System.Drawing.Image.FromStream(fs);
fs.Close();
#else
using (System.Drawing.Image img = System.Drawing.Image.FromFile(_filePath))
#endif
{
if (maxSize == 0)
maxSize = Math.Max(img.Width, img.Height);
//Bitmap resizedBitmap = new Bitmap(img);// ImageUtil.ResizeImage(img, maxSize);
Bitmap resizedBitmap = ImageUtil.ResizeImage(img, maxSize);
_width = resizedBitmap.Width;
_height = resizedBitmap.Height;
_rawdata = createRawbytes(resizedBitmap);
//_is_decoded = true;
}
return true;
}
catch (System.Exception err)
{
Debug.Log(err.StackTrace);
return false;
}
}
#if false
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
public bool encode()
{
if(_rawdata == null)
{
return false;
}
Bitmap bitmap = new Bitmap(_width,_height, PixelFormat.Format32bppArgb);
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, _width, _height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
int length = bitmapData.Stride * bitmapData.Height;
Marshal.Copy(_rawdata,0, bitmapData.Scan0,length);
bitmap.UnlockBits(bitmapData);
MemoryStream ms = new MemoryStream();
ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/png");
EncoderParameters myEncoderParameters = new EncoderParameters(1);
// Save the bitmap as a JPEG file with quality level 25.
EncoderParameter myEncoderParameter = new EncoderParameter(Encoder.Quality, 90);
myEncoderParameters.Param[0] = myEncoderParameter;
bitmap.Save(ms, myImageCodecInfo, myEncoderParameters);
_compacted_data = ms.ToArray();
_rawdata = null;
_is_decoded = false;
return true;
}
public bool decode()
{
if (_compacted_data == null)
{
return false;
}
MemoryStream ms = new MemoryStream();
ms.Write(_compacted_data, 0, _compacted_data.Length);
Bitmap bitmap = new Bitmap(ms);
_rawdata = createRawbytes(bitmap);
_compacted_data = null;
_is_decoded = true;
return true;
}
#endif
private byte[] createRawbytes(Bitmap bitmap)
{
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, _width, _height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
int length = bitmapData.Stride * bitmapData.Height;
byte[] result = new byte[length];
Marshal.Copy(bitmapData.Scan0, result, 0, length);
bitmap.UnlockBits(bitmapData);
return result;
}
public bool createTextureFromMemory()
{
destroyTexture();
_tex = new Texture2D(_width, _height, TextureFormat.BGRA32, false);
_tex.LoadRawTextureData(_rawdata);
_tex.Compress(false);
_rawdata = null;
_tex.Apply();
return true;
}
public void destroyTexture()
{
//if(_tex!=null)
// UnityEngine.Object.Destroy(_tex);
_tex = null;
}
/*
public bool isDecoded()
{
return _is_decoded;
}
*/
////////////////////////////////////////////////////////////////////////////////
private int _id=0;
private string _filePath = "";
private string _type="";
private byte[] _rawdata;
//private byte[] _compacted_data;
private Texture2D _tex;
private int _width;
private int _height;
}