public void MakeTexture(Device device, byte[] txData, Format format, bool b黒を透過する, Pool pool) { try { var information = ImageInformation.FromMemory(txData); this.Format = format; this.sz画像サイズ = new Size(information.Width, information.Height); this.rc全画像 = new Rectangle(0, 0, this.sz画像サイズ.Width, this.sz画像サイズ.Height); this.colorKey = (b黒を透過する) ? unchecked ((int)0xFF000000) : 0; this.szテクスチャサイズ = this.t指定されたサイズを超えない最適なテクスチャサイズを返す(device, this.sz画像サイズ); this.Pl = pool; // lock ( lockobj ) // { //Trace.TraceInformation( "CTexture() start: " ); this.texture = Texture.FromMemory(device, txData, this.sz画像サイズ.Width, this.sz画像サイズ.Height, 1, Usage.None, format, pool, Filter.Point, Filter.None, this.colorKey); //Trace.TraceInformation( "CTexture() end: " ); // } this.bSharpDXTextureDispose完了済み = false; } catch { this.Dispose(); // throw new CTextureCreateFailedException( string.Format( "テクスチャの生成に失敗しました。\n{0}", strファイル名 ) ); throw new CTextureCreateFailedException(string.Format("テクスチャの生成に失敗しました。\n")); } }
public static Ema Import(Stream stream) { Ema ema = new Ema(); using (BinaryReader reader = new BinaryReader(stream)) { byte[] imgData = reader.ReadToEnd(); var imgInfo = ImageInformation.FromMemory(imgData); ema.Data = new byte[imgData.Length + 13]; BinaryWriter dataWriter = new BinaryWriter(new MemoryStream(ema.Data)); dataWriter.Write(imgInfo.Width); dataWriter.Write(imgInfo.Height); string ext = Enum.GetName(typeof(ImageFileFormat), imgInfo.ImageFileFormat).ToLowerInvariant(); dataWriter.Write((byte)'.'); dataWriter.Write(Encoding.ASCII.GetBytes(ext)); dataWriter.Write((byte)0); if (imgInfo.ImageFileFormat == ImageFileFormat.Bmp) { dataWriter.Write((short)0); dataWriter.Write(imgData, 2, imgData.Length - 2); } else { dataWriter.Write(imgData); } } return(ema); }
public void MakeTexture(Device device, byte[] txData, Format format, bool b黒を透過する, Pool pool) { try { var information = ImageInformation.FromMemory(txData); this.Format = format; this.szImageSize = new Size(information.Width, information.Height); this.rcFullImage = new Rectangle(0, 0, this.szImageSize.Width, this.szImageSize.Height); int colorKey = (b黒を透過する) ? unchecked ((int)0xFF000000) : 0; this.szTextureSize = this.tGetOptimalTextureSizeNotExceedingSpecifiedSize(device, this.szImageSize); #if TEST_Direct3D9Ex pool = poolvar; #endif // lock ( lockobj ) // { //Trace.TraceInformation( "CTexture() start: " ); this.texture = Texture.FromMemory(device, txData, this.szImageSize.Width, this.szImageSize.Height, 1, Usage.None, format, pool, Filter.Point, Filter.None, colorKey); this.bSharpDXTextureDispose完了済み = false; //Trace.TraceInformation( "CTexture() end: " ); // } } catch { this.Dispose(); // throw new CTextureCreateFailedException( string.Format( "テクスチャの生成に失敗しました。\n{0}", strファイル名 ) ); throw new CTextureCreateFailedException(string.Format("テクスチャの生成に失敗しました。\n")); } }
/// <summary> /// コンストラクタ /// </summary> /// <param name="device"></param> /// <param name="stream"></param> /// <param name="pa"></param> public ImageResource(PPDDevice device, Stream stream, bool pa) { try { using (var memoryStream = new MemoryStream()) { byte[] bytes = new byte[1024 * 1024]; while (stream.Position < stream.Length) { var bytesRead = stream.Read(bytes, 0, bytes.Length); if (bytesRead == 0) { break; } } memoryStream.Seek(0, SeekOrigin.Begin); var ii = ImageInformation.FromMemory(memoryStream.ToArray()); this.width = ii.Width; this.height = ii.Height; this.halfPixel = new Vector2(0.5f / this.width, 0.5f / this.height); this.texture = (Texture.DX11.Texture)TextureFactoryManager.Factory.FromStream(device, stream, this.width, this.height, pa); CreateVertexBuffer(device); } } catch { MessageBox.Show(PPDExceptionContentProvider.Provider.GetContent(PPDExceptionType.ImageReadError)); } }
public static xxTexture CreateTexture(ImportedTexture texture) { var imgInfo = ImageInformation.FromMemory(texture.Data); xxTexture xxTex = new xxTexture(); xxTex.Width = imgInfo.Width; xxTex.Height = imgInfo.Height; xxTex.Depth = imgInfo.Depth; xxTex.Format = (int)imgInfo.Format; xxTex.ImageFileFormat = (int)imgInfo.ImageFileFormat; xxTex.MipLevels = imgInfo.MipLevels; xxTex.Name = texture.Name; xxTex.ResourceType = (int)imgInfo.ResourceType; byte checksum = 0; for (int i = 0; i < texture.Data.Length; i += 32) { checksum += texture.Data[i]; } xxTex.Checksum = checksum; if (imgInfo.ImageFileFormat == ImageFileFormat.Bmp) { xxTex.ImageData = (byte[])texture.Data.Clone(); xxTex.ImageData[0] = 0; xxTex.ImageData[1] = 0; } else if (imgInfo.ImageFileFormat == ImageFileFormat.Tga) { byte[] tgaHeader = new byte[18]; Array.Copy(texture.Data, tgaHeader, tgaHeader.Length); int imgdataLen = tgaHeader[16] / 8 * BitConverter.ToInt16(tgaHeader, 12) * BitConverter.ToInt16(tgaHeader, 14); if (imgdataLen > texture.Data.Length - (tgaHeader.Length + tgaHeader[0])) { imgdataLen = texture.Data.Length - (tgaHeader.Length + tgaHeader[0]); } xxTex.ImageData = new byte[tgaHeader.Length + imgdataLen]; Array.Copy(texture.Data, xxTex.ImageData, tgaHeader.Length); Array.Copy(texture.Data, tgaHeader.Length + tgaHeader[0], xxTex.ImageData, tgaHeader.Length, imgdataLen); xxTex.ImageData[0] = 0; } else { xxTex.ImageData = (byte[])texture.Data.Clone(); } return(xxTex); }
public void LoadTexture() { if (texCache.ContainsKey(filePath)) { tex = texCache[filePath]; } else { try { byte[] data = MGEgui.DistantLand.BSA.GetTexture(filePath); // Work around a loading issue with TGA headers // When ColorMapType == no_palette && ImageType == truecolor, set word ColorMapLength to 0 if (data.Length > 6 && data[1] == 0 && (data[2] == 2 || data[2] == 10)) { data[5] = data[6] = 0; } // Scale down widest texture dimension to 256 to conserve memory ImageInformation imginf = ImageInformation.FromMemory(data); Filter dxf = Filter.Box | Filter.Srgb; int maximal = Math.Max(imginf.Width, imginf.Height); int skiplevels = 0, w = 0, h = 0; while (maximal > 256) { maximal /= 2; skiplevels += 1; } if (skiplevels < imginf.MipLevels) { // Skip loading mips larger than 256 pixels // Filter bits 27-31 specify the number of mip levels to be skipped dxf = (Filter)((skiplevels << 26) | (int)dxf); } else { // Rescale base level to fit inside 256 pixels w = imginf.Width >> skiplevels; h = imginf.Height >> skiplevels; } tex = Texture.FromMemory(DXMain.device, data, w, h, 0, Usage.None, Format.Unknown, Pool.Managed, Filter.Triangle | Filter.Dither, dxf, 0); texCache[filePath] = tex; } catch { tex = null; throw; } } }
private static bool TryFileImage(Stream stream, ppSubfile subfile) { BinaryReader reader = new BinaryReader(stream); try { byte[] data = reader.ReadToEnd(); var imgInfo = ImageInformation.FromMemory(data); } catch { return(false); } return(true); }
private static bool TryFileImage(ppSubfile subfile) { try { using (BinaryReader reader = new BinaryReader(subfile.CreateReadStream())) { byte[] data = reader.ReadToEnd(); var imgInfo = ImageInformation.FromMemory(data); } } catch { return(false); } return(true); }
private void MakeTexture(Device device, byte[] txData, Format format, bool b黒を透過する, Pool pool) { try { var information = ImageInformation.FromMemory(txData); this.Format = format; this.sz画像サイズ = new Size(information.Width, information.Height); this.rc全画像 = new Rectangle(0, 0, this.sz画像サイズ.Width, this.sz画像サイズ.Height); this.szテクスチャサイズ = this.t指定されたサイズを超えない最適なテクスチャサイズを返す(device, this.sz画像サイズ); int colorKey = (b黒を透過する) ? unchecked ((int)0xFF000000) : 0; this.texture = Texture.FromMemory(device.UnderlyingDevice, txData, this.sz画像サイズ.Width, this.sz画像サイズ.Height, 1, Usage.None, format, pool, Filter.Point, Filter.None, colorKey); } catch { this.Dispose(); throw new CTextureCreateFailedException(string.Format("テクスチャの生成に失敗しました。\n")); } }
public new void MakeTexture(Device device, string strファイル名, Format format, bool b黒を透過する, Pool pool) { if (!File.Exists(strファイル名)) // #27122 2012.1.13 from: ImageInformation では FileNotFound 例外は返ってこないので、ここで自分でチェックする。わかりやすいログのために。 { throw new FileNotFoundException(string.Format("ファイルが存在しません。\n[{0}]", strファイル名)); } Byte[] _txData = File.ReadAllBytes(strファイル名); bool b条件付きでサイズは2の累乗でなくてもOK = (device.Capabilities.TextureCaps & TextureCaps.NonPow2Conditional) != 0; bool bサイズは2の累乗でなければならない = (device.Capabilities.TextureCaps & TextureCaps.Pow2) != 0; bool b正方形でなければならない = (device.Capabilities.TextureCaps & TextureCaps.SquareOnly) != 0; // そもそもこんな最適化をしなくてよいのなら、さっさとbaseに処理を委ねて終了 if (!bサイズは2の累乗でなければならない && b条件付きでサイズは2の累乗でなくてもOK) { //Debug.WriteLine( Path.GetFileName( strファイル名 ) + ": 最適化は不要です。" ); base.MakeTexture(device, strファイル名, format, b黒を透過する, pool); return; } var information = ImageInformation.FromMemory(_txData); int orgWidth = information.Width, orgHeight = information.Height; int w = orgWidth, h = orgHeight, foldtimes; #region [ 折りたたみありで最適なテクスチャサイズがどうなるかを確認する(正方形にするかは考慮せず) ] if (orgWidth >= orgHeight) // 横長画像なら { this.b横長のテクスチャである = true; if (!GetFoldedTextureSize(ref w, ref h, out foldtimes)) { //Debug.WriteLine( Path.GetFileName( strファイル名 ) + ": 最適化を断念。" ); base.MakeTexture(device, strファイル名, format, b黒を透過する, pool); return; } } else // 縦長画像なら { this.b横長のテクスチャである = false; if (!GetFoldedTextureSize(ref h, ref w, out foldtimes)) // 縦横入れ替えて呼び出し { //Debug.WriteLine( Path.GetFileName( strファイル名 ) + ": 最適化を断念。" ); base.MakeTexture(device, strファイル名, format, b黒を透過する, pool); return; } } #endregion //Debug.WriteLine( Path.GetFileName( strファイル名 ) + ": texture最適化結果: width=" + w + ", height=" + h + ", 折りたたみ回数=" + foldtimes ); #region [ 折りたたみテクスチャ画像を作り、テクスチャ登録する ] // バイナリ(Byte配列)をBitmapに変換 MemoryStream mms = new MemoryStream(_txData); Bitmap bmpOrg = new Bitmap(mms); mms.Close(); Bitmap bmpNew = new Bitmap(w, h); Graphics g = Graphics.FromImage(bmpNew); for (int n = 0; n <= foldtimes; n++) { if (b横長のテクスチャである) { int x = n * w; int currentHeight = n * orgHeight; int currentWidth = (n < foldtimes) ? w : orgWidth - x; Rectangle r = new Rectangle(x, 0, currentWidth, orgHeight); Bitmap bmpTmp = bmpOrg.Clone(r, bmpOrg.PixelFormat); // ここがボトルネックになるようなら、後日unsafeコードにする。 g.DrawImage(bmpTmp, 0, currentHeight, currentWidth, orgHeight); bmpTmp.Dispose(); } else { int y = n * h; int currentWidth = n * orgWidth; int currentHeight = (n < foldtimes) ? h : orgHeight - y; Rectangle r = new Rectangle(0, y, orgWidth, currentHeight); Bitmap bmpTmp = bmpOrg.Clone(r, bmpOrg.PixelFormat); // ここがボトルネックになるようなら、後日unsafeコードにする。 g.DrawImage(bmpTmp, currentWidth, 0, orgWidth, currentHeight); bmpTmp.Dispose(); } } ; if (b黒を透過する) { bmpNew.MakeTransparent(Color.Black); } g.Dispose(); g = null; bmpOrg.Dispose(); bmpOrg = null; base.MakeTexture(device, bmpNew, format, b黒を透過する, pool); bmpNew.Dispose(); bmpNew = null; #endregion _orgWidth = orgWidth; _orgHeight = orgHeight; _foldtimes = foldtimes; this.sz画像サイズ = new Size(orgWidth, orgHeight); }
private static void InternalDDSFromStream(Stream stream, Device device, int streamOffset, bool loadMipMap, int offsetMipMaps, out SharpDX.Direct3D9.Texture texture) { var fileStream = stream as FileStream; byte[] data = fileStream != null ? null : SharpDX.Utilities.ReadStream(stream); int maxTextureDimension = MyRender.GraphicsDevice.Capabilities.MaxTextureWidth; if (IntPtr.Size == 4 || MyRender.GraphicsDevice.AvailableTextureMemory < 1024 * 1024 * 600) { maxTextureDimension = 2048; } ImageInformation imageInfo; if (fileStream != null) { imageInfo = GetImageInfoFromFileW(fileStream.Name); } else { imageInfo = ImageInformation.FromMemory(data); } var textureWidth = imageInfo.Width; var textureHeight = imageInfo.Height; textureWidth = Math.Max(1, textureWidth >> offsetMipMaps); textureHeight = Math.Max(1, textureHeight >> offsetMipMaps); int loadMipmapOffset = offsetMipMaps; while (textureWidth > maxTextureDimension || textureHeight > maxTextureDimension) { loadMipmapOffset++; textureWidth = Math.Max(1, textureWidth >> 1); textureHeight = Math.Max(1, textureHeight >> 1); } if (offsetMipMaps == 0 && loadMipmapOffset == 0) { if (fileStream != null) { texture = SharpDX.Direct3D9.Texture.FromFile(device, fileStream.Name, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0); } else { texture = SharpDX.Direct3D9.Texture.FromMemory(device, data, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0); } } else if (loadMipmapOffset > 0) { var skipFilter = (Filter)D3DXSkipDDSMipLevels((uint)loadMipmapOffset); if (fileStream != null) { texture = SharpDX.Direct3D9.Texture.FromFile(device, fileStream.Name, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, skipFilter, 0); } else { texture = SharpDX.Direct3D9.Texture.FromMemory(device, data, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, skipFilter, 0); } } else { if (fileStream != null) { texture = SharpDX.Direct3D9.Texture.FromFile(device, fileStream.Name, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0); } else { texture = SharpDX.Direct3D9.Texture.FromMemory(device, data, 0, 0, 0, Usage.None, Format.Unknown, Pool.Default, Filter.None, Filter.None, 0); } int width = MipMapSize(offsetMipMaps, texture.GetLevelDescription(0).Width); int height = MipMapSize(offsetMipMaps, texture.GetLevelDescription(0).Height); int maxLevels = Math.Min(MaxMipMapLevels(width), MaxMipMapLevels(height)); int actualLevels = Math.Min(maxLevels, texture.LevelCount - offsetMipMaps); Format format = texture.GetLevelDescription(0).Format; Texture offsetedTexture = new Texture(device, width, height, actualLevels, Usage.Dynamic, format, Pool.Default); for (int i = offsetMipMaps, j = 0; j < actualLevels; i++, j++) { int levelWidth = MipMapSize(j, width); int levelHeight = MipMapSize(j, height); SharpDX.DataStream ds; texture.LockRectangle(i, LockFlags.ReadOnly, out ds); texture.UnlockRectangle(i); SharpDX.DataStream ds2; offsetedTexture.LockRectangle(j, LockFlags.None, out ds2); ds2.Position = 0; ds2.Write(ds.DataPointer, 0, (int)MipMapSizeInBytes(levelWidth, levelHeight, format)); offsetedTexture.UnlockRectangle(j); } texture.Dispose(); texture = offsetedTexture; } }
public bool LoadTexture(string path) { if (texCache.Contains(path)) { return(true); } texCache.Add(path); byte[] data = MGEgui.DistantLand.BSA.GetTexture(path); if (data == null) { if (MessageBox.Show("Missing texture: '" + path + "'\n" + "Continuing may result in texture glitches in game.\n" + "Do you wish to continue?", "Warning", MessageBoxButtons.YesNo) == DialogResult.Yes) { return(false); } } // Work around a loading issue with TGA headers // When ColorMapType == no_palette && ImageType == truecolor, set word ColorMapLength to 0 if (data.Length > 6 && data[1] == 0 && (data[2] == 2 || data[2] == 10)) { data[5] = data[6] = 0; } path = System.IO.Path.ChangeExtension(path, ".dds"); ImageInformation imginfo; Format format; try { imginfo = ImageInformation.FromMemory(data); } catch (SlimDXException) { return(false); } int newWidth = imginfo.Width / div, newHeight = imginfo.Height / div; if (newWidth < 4 || newHeight < 4) { return(true); } if (imginfo.Format == Format.Dxt1) { format = isDXT1a(imginfo, data) ? Format.Dxt3 : Format.Dxt1; } else if (imginfo.Format == Format.Dxt3 || imginfo.Format == Format.Dxt5) { format = imginfo.Format; } else if (imginfo.Format == Format.X8R8G8B8) { format = Format.Dxt1; } else { format = Format.Dxt3; } if (div > 1 || format != imginfo.Format) { Texture t = null; System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(System.IO.Path.Combine(@"data files\distantland\statics\textures\", path))); try { if (format == imginfo.Format) { // Load reduced size texture t = Texture.FromMemory(DXMain.device, data, newWidth, newHeight, 0, Usage.None, format, Pool.Scratch, Filter.Triangle | Filter.Dither, Filter.Triangle, 0); } else { // Recalculate mipmaps Texture srctex = Texture.FromMemory(DXMain.device, data, newWidth, newHeight, 0, Usage.None, Format.A8B8G8R8, Pool.Scratch, Filter.Triangle | Filter.Dither, Filter.Triangle, 0); t = new Texture(DXMain.device, newWidth, newHeight, 0, Usage.None, format, Pool.Scratch); srctex.FilterTexture(0, Filter.Triangle | Filter.Srgb); for (int i = 0; i != t.LevelCount; ++i) { Surface dest = t.GetSurfaceLevel(i); Surface src = srctex.GetSurfaceLevel(i); Surface.FromSurface(dest, src, Filter.Point, 0); src.Dispose(); dest.Dispose(); } srctex.Dispose(); } Texture.ToFile(t, System.IO.Path.Combine(@"data files\distantland\statics\textures\", path), ImageFileFormat.Dds); t.Dispose(); } catch (SlimDXException) { if (t != null) { t.Dispose(); } return(false); } } return(true); }