public static Image Convert(ImageFile file) { return(Convert(file.GetData(), file.Format, file.Width, file.Height)); }
public static byte[] GetA8R8G8B8(ImageFile file) { return(GetA8R8G8B8(file.GetData(), file.Format, file.Width, file.Height)); }
private Image BuildImage(string size) { const string MapFileFormat = "ui/map/{0}/{1}{2}_{3}.tex"; if (string.IsNullOrEmpty(Id)) return null; var fileName = Id.ToString().Replace("/", ""); var pack = Sheet.Collection.PackCollection; var filePath = string.Format(MapFileFormat, Id, fileName, string.Empty, size); IO.File file; if (!pack.TryGetFile(filePath, out file)) return null; var imageFile = new ImageFile(file.Pack, file.CommonHeader); var maskPath = string.Format(MapFileFormat, Id, fileName, "m", size); IO.File mask; if (pack.TryGetFile(maskPath, out mask)) { // Multiply the mask against the file. var maskFile = new ImageFile(mask.Pack, mask.CommonHeader); return MultiplyBlend(imageFile, maskFile); } return imageFile.GetImage(); }
private static Image MultiplyBlend(ImageFile image, ImageFile mask) { if (image.Width != mask.Width || image.Height != mask.Height) throw new ArgumentException(); // Using 32bit color const int BytesPerPixel = 4; var aArgb = Imaging.ImageConverter.GetA8R8G8B8(image); var bArgb = Imaging.ImageConverter.GetA8R8G8B8(mask); var result = new byte[aArgb.Length]; for (var i = 0; i < aArgb.Length; i += BytesPerPixel) { // There are other algorithms that can do this with any alpha, // but I haven't the time to research them now. var bAlpha = bArgb[i + 3]; if (bAlpha == 0) { // Mask pixel is transparent, do not blend. result[i] = aArgb[i]; result[i + 1] = aArgb[i + 1]; result[i + 2] = aArgb[i + 2]; } else { for (var j = 0; j < 3; ++j) // Only blend RGB result[i + j] = (byte)((aArgb[i + j] * bArgb[i + j]) / 255); } result[i + 3] = aArgb[i + 3]; // Preserve alpha } Image output; unsafe { fixed (byte* p = result) { var ptr = (IntPtr)p; using (var tempImage = new Bitmap(image.Width, image.Height, image.Width * BytesPerPixel, PixelFormat.Format32bppArgb, ptr)) output = new Bitmap(tempImage); } } return output; }
/// <summary> /// Attempt to convert ImageFile to DDS file. Returns null if no DXT1/3/5 texture found. /// </summary> public static byte[] GetDDS(ImageFile file) { var bytes2 = file.GetData(); //var offset = bytes2[file.ImageHeader.EndOfHeader]; var width = file.ImageHeader.Width; var height = file.ImageHeader.Height; DDS_HEADER header = new DDS_HEADER(); DDS_PIXELFORMAT format = header.ddspf; format.dwFlags = (uint)(DDPF_ENUM.DDPF_ALPHAPIXELS | DDPF_ENUM.DDPF_FOURCC); header.dwFlags |= (uint)(DDSD_ENUM.DDSD_CAPS | DDSD_ENUM.DDSD_HEIGHT | DDSD_ENUM.DDSD_WIDTH | DDSD_ENUM.DDSD_PIXELFORMAT | DDSD_ENUM.DDSD_LINEARSIZE); header.dwFlags |= (uint)DDSD_ENUM.DDSD_MIPMAPCOUNT; switch (file.ImageHeader.Format) { case ImageFormat.Dxt1: format.dwFourCC = 0x31545844; header.dwPitchOrLinearSize = (uint)(Math.Max((uint)1, ((width + 3) / 4)) * Math.Max((uint)1, ((height + 3) / 4)) * 8); break; case ImageFormat.Dxt3: format.dwFourCC = 0x33545844; header.dwPitchOrLinearSize = (uint)(Math.Max((uint)1, ((width + 3) / 4)) * Math.Max((uint)1, ((height + 3) / 4)) * 16); break; case ImageFormat.Dxt5: format.dwFourCC = 0x35545844; header.dwPitchOrLinearSize = (uint)(Math.Max((uint)1, ((width + 3) / 4)) * Math.Max((uint)1, ((height + 3) / 4)) * 16); break; /* * case ImageFormat.A8R8G8B8_1: * case ImageFormat.A8R8G8B8_2: * case ImageFormat.A8R8G8B8_4: * case ImageFormat.A8R8G8B8_5: * format.dwFlags = (uint)(DDPF_ENUM.DDPF_ALPHA | DDPF_ENUM.DDPF_RGB); * format.dwFourCC = 0x42475241; * header.dwPitchOrLinearSize = (uint)(width * height); * header.dwFlags &= ~(uint)DDSD_ENUM.DDSD_LINEARSIZE; * header.dwFlags |= (uint)DDSD_ENUM.DDSD_PITCH; * break; */ default: System.Diagnostics.Debug.WriteLine("Texture format " + file.ImageHeader.Format.ToString() + " DDS export not supported!\n"); return(null); break; } format.dwSize = 32; header.dwSize = 124; // why set this if it MUST be 124? header.dwHeight = (uint)height; header.dwWidth = (uint)width; header.dwMipMapCount = file.ImageHeader._Buffer[0x0E]; header.dwCaps = 0x08 | 0x400000 | 0x1000; // DDSCAPS_COMPLEX | DDSCAPS_MIPMAP | DDSCAPS_TEXTURE header.ddspf = format; List <byte> data = new List <byte>(); int size = System.Runtime.InteropServices.Marshal.SizeOf <DDS_HEADER>(); byte[] headerBytes = new byte[size]; var ptr = System.Runtime.InteropServices.Marshal.AllocHGlobal(size); System.Runtime.InteropServices.Marshal.StructureToPtr(header, ptr, false); System.Runtime.InteropServices.Marshal.Copy(ptr, headerBytes, 0, size); System.Runtime.InteropServices.Marshal.FreeHGlobal(ptr); data.AddRange(System.Text.ASCIIEncoding.UTF8.GetBytes("DDS ")); data.AddRange(headerBytes); data.AddRange(bytes2); return(data.ToArray()); }
public static byte[] GetA8R8G8B8(ImageFile file) { return GetA8R8G8B8(file.GetData(), file.Format, file.Width, file.Height); }
public static Image Convert(ImageFile file) { return Convert(file.GetData(), file.Format, file.Width, file.Height); }