CoreTexture readFromBinaryReader(Func <UInt32> uintReader, Func <UInt32, byte[]> byteReader) { CoreTexture resultTexture = new CoreTexture(); resultTexture.glType = uintReader(); resultTexture.glTypeSize = uintReader(); resultTexture.glFormat = uintReader(); resultTexture.glInternalFormat = uintReader(); resultTexture.glBaseInternalFormat = uintReader(); resultTexture.pixelWidth = uintReader(); resultTexture.pixelHeight = uintReader(); resultTexture.pixelDepth = uintReader(); resultTexture.numberOfArrayElements = uintReader(); resultTexture.numberOfFaces = uintReader(); UInt32 numberOfMipmapLevels = uintReader(); UInt32 bytesOfKeyValueData = uintReader(); resultTexture.mipmapLevels = new CoreTextureMipmapLevel[numberOfMipmapLevels == 0 ? 1 : numberOfMipmapLevels]; readKeyValuePairsFromBinaryReader(uintReader, byteReader, bytesOfKeyValueData, ref resultTexture); readMipmapLevelsFromBinaryReader(uintReader, byteReader, ref resultTexture); return(resultTexture); }
public CoreTexture ToCoreTexture(GenericImage image, IGLDataFormat dataFormat) { CoreTexture texture = new CoreTexture(); texture.glType = dataFormat.Value; texture.glFormat = Value; texture.glTypeSize = 1; texture.pixelWidth = image.width; texture.pixelHeight = image.height; texture.pixelDepth = image.depth == 1 ? 0 : image.depth; texture.numberOfFaces = image.faces; texture.numberOfArrayElements = image.arrays == 1 ? 0 : image.arrays; texture.mipmapLevels = new CoreTextureMipmapLevel[image.mipmapLevels.Length]; for (int mip = 0; mip < texture.mipmapLevels.Length; ++mip) { CoreTextureMipmapLevel mipTexture = new CoreTextureMipmapLevel(); mipTexture.pixels = ToCoreMipTexture(image, dataFormat, mip).ToArray(); texture.mipmapLevels[mip] = mipTexture; } texture.keyValuePairs = new CoreTextureKeyValuePair[1] { new CoreTextureKeyValuePair("FileGenerator", "KTXToolkit by Tiemo Jung") }; return(texture); }
public void CopyTo(ref CoreTexture texture) { texture.glType = 0x1401; texture.glTypeSize = 1; if (BitsPerPixel > 24) { texture.glBaseInternalFormat = texture.glInternalFormat = 0x1908; texture.glFormat = 0x80E1; } else { texture.glBaseInternalFormat = texture.glInternalFormat = 0x1907; texture.glFormat = 0x80E0; } texture.pixelWidth = (UInt32)Width; texture.pixelHeight = (UInt32)Height; texture.pixelDepth = 0; texture.numberOfArrayElements = 0; texture.numberOfFaces = 1; texture.keyValuePairs = new CoreTextureKeyValuePair[0]; texture.mipmapLevels = new CoreTextureMipmapLevel[1]; texture.mipmapLevels[0] = new CoreTextureMipmapLevel(); if (BitsPerPixel >= 24) { texture.mipmapLevels[0].pixels = UncompressedImage; } else { // todo: use extended stuff... } }
public CoreTexture Load(string path) { TGAImage image = new TGAImage(path); CoreTexture texture = new CoreTexture(); image.CopyTo(ref texture); return(texture); }
private void DisplayTexture(string name, CoreTexture texture) { ImageDisplay imageDisplay = new ImageDisplay(PluginList, texture); imageDisplay.Width = (int)texture.pixelWidth; imageDisplay.Height = (int)texture.pixelHeight; imageDisplay.Text = name; imageDisplay.Show(this); }
public ImageDisplay(IPlugin[] plugins, CoreTexture texture_) { InitializeComponent(); PluginList = plugins; texture = texture_; AutoSize = true; MinimumSize = new Size(158, 98); UpdateImageDisplay(); }
public void Store(string path, CoreTexture texture, GenericImage image) { if (image.depth > 1) { MessageBox.Show("The image is a volumetric image, the container format can not store volumetric images" , "Inconpatible image type" , MessageBoxButtons.OK , MessageBoxIcon.Error); return; } if (image.arrays > 1) { MessageBox.Show("The image is a array of images, the container format can not store an array of images" , "Inconpatible image type" , MessageBoxButtons.OK , MessageBoxIcon.Error); return; } if (image.faces > 1) { MessageBox.Show("The image is a cubemap image, the container format can not store a cubemap image" , "Inconpatible image type" , MessageBoxButtons.OK , MessageBoxIcon.Error); return; } if (image.mipmapLevels.Length > 1) { MessageBox.Show("The image has multiple mipmap levels, the container format does not support mipmap images, ony the first mipmap image is store" , "Container can't store mipmaps" , MessageBoxButtons.OK , MessageBoxIcon.Information); } int[] buf = new int[] { 0, 0, 0, 255 }; System.Drawing.Imaging.PixelFormat pfm = System.Drawing.Imaging.PixelFormat.Canonical; if (image.channels <= 3) { pfm = System.Drawing.Imaging.PixelFormat.Format24bppRgb; } Bitmap bitmap = new Bitmap((int)texture.pixelWidth, (int)texture.pixelHeight, pfm); for (int y = 0; y < bitmap.Height; ++y) { for (int x = 0; x < bitmap.Width; ++x) { for (int c = 0; c < image.channels; ++c) { buf[c] = (byte)((image.mipmapLevels[0].pixels[(x + y * image.width) * image.channels + c]) * 255); } bitmap.SetPixel(x, y, Color.FromArgb(buf[3], buf[0], buf[1], buf[2])); } } bitmap.Save(path); }
protected List <double> ToGenericImagePixel(CoreTexture texture, IGLDataFormat dataFormat, int mipIndex, int pixelIndex) { List <double> byteBuffer = new List <double>(); for (int channel = 0; channel < Channels; ++channel) { byteBuffer.Add(ToGenericImagePixelAtPixel(texture, dataFormat, mipIndex, pixelIndex, channel)); } return(byteBuffer); }
void WriteKeyValuePairs(BinaryWriter writer, CoreTexture texture) { if (texture.keyValuePairs.Length == 0) { writer.Write((UInt32)0); } else { byte[] buffer = WriteKeyValuePairsToBuffer(texture); writer.Write((UInt32)buffer.Length); writer.Write(buffer); } }
void WriteImages(BinaryWriter writer, CoreTexture texture) { foreach (CoreTextureMipmapLevel mip in texture.mipmapLevels) { writer.Write((UInt32)mip.pixels.Length); writer.Write(mip.pixels); int padding = 3 - ((mip.pixels.Length + 3) % 4); for (int i = 0; i < padding; ++i) { writer.Write((byte)0); } } }
public CoreTexture Load(string path) { FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read); if (file == null) { return(null); } BinaryReader fileReader = new BinaryReader(file); CoreTexture tex = readFromBinaryReader(fileReader); file.Dispose(); return(tex); }
public void Store(string path, CoreTexture texture, GenericImage image) { FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write); if (file == null) { return; } BinaryWriter fileWriter = new BinaryWriter(file); WriteToBinaryWriter(fileWriter, texture); file.Flush(); file.Dispose(); }
private void TransformTextureBack() { IGLDataFormat dataFormat = GetDataFormat(texture.glType); IGLPixelFormat pixelFormat = GetPixelFormat(texture.glFormat); IGLInteralPixelFormat internalPixelFormat = GetInternalPixelFormat(texture.glInternalFormat); CoreTexture tex = internalPixelFormat.ToCoreTexture(genericTexture, pixelFormat, dataFormat); if (tex != null) { texture = tex; } else { MessageBox.Show("Unable to convert image data", "Error while converting image data", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
override protected double ToGenericImagePixelAtPixel(CoreTexture texture, IGLDataFormat dataFormat, int mipIndex, int pixelIndex, int channelIndex) { long offset = pixelIndex * dataFormat.PixelSize(Channels); if (channelIndex == 0) { return(dataFormat.ToGenericFormat(texture.mipmapLevels[mipIndex].pixels, (int)offset, 2, Channels)); } else if (channelIndex == 2) { return(dataFormat.ToGenericFormat(texture.mipmapLevels[mipIndex].pixels, (int)offset, 0, Channels)); } else { return(dataFormat.ToGenericFormat(texture.mipmapLevels[mipIndex].pixels, (int)offset, channelIndex, Channels)); } }
protected List <double> ToGenericMipImage(CoreTexture texture, IGLDataFormat dataFormat, int mipIndex) { int x = (int)texture.pixelWidth >> (int)mipIndex; int y = (int)texture.pixelHeight >> (int)mipIndex; int z = (int)texture.pixelDepth >> (int)mipIndex; x = x < 1 ? 1 : x; y = y < 1 ? 1 : y; z = z < 1 ? 1 : z; int pixels = (int)(texture.numberOfFaces * Math.Max(1, texture.numberOfArrayElements) * x * y * z); List <double> byteBuffer = new List <double>(); for (int pixel = 0; pixel < pixels; ++pixel) { byteBuffer.AddRange(ToGenericImagePixel(texture, dataFormat, mipIndex, pixel)); } return(byteBuffer); }
void WriteHeader(BinaryWriter writer, CoreTexture texture) { writer.Write(new byte[12] { 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A }); for (byte b = 1; b <= 4; ++b) { writer.Write(b); } writer.Write(texture.glType); writer.Write(texture.glTypeSize); writer.Write(texture.glFormat); writer.Write(texture.glInternalFormat); writer.Write(texture.glBaseInternalFormat); writer.Write(texture.pixelWidth); writer.Write(texture.pixelHeight); writer.Write(texture.pixelDepth); writer.Write(texture.numberOfArrayElements); writer.Write(texture.numberOfFaces); writer.Write((UInt32)texture.mipmapLevels.Length); }
public CoreTexture ToCoreTexture(GenericImage image, IGLPixelFormat pixelFormat, IGLDataFormat dataFormat) { CoreTexture tex = pixelFormat.ToCoreTexture(image, dataFormat); if (tex != null) { tex.glInternalFormat = Value; switch (GLValue) { case Values.GL_RED: tex.glBaseInternalFormat = (UInt32)Values.GL_RED; break; case Values.GL_RGB: case Values.GL_R3_G3_B2: case Values.GL_RGB4: case Values.GL_RGB5: case Values.GL_RGB8: case Values.GL_RGB10: case Values.GL_RGB12: case Values.GL_RGB16: tex.glBaseInternalFormat = (UInt32)Values.GL_RGB; break; case Values.GL_RGBA: case Values.GL_RGBA2: case Values.GL_RGBA4: case Values.GL_RGB5_A1: case Values.GL_RGBA8: case Values.GL_RGB10_A2: case Values.GL_RGBA12: case Values.GL_RGBA16: tex.glBaseInternalFormat = (UInt32)Values.GL_RGBA; break; } } return(tex); }
byte[] WriteKeyValuePairsToBuffer(CoreTexture texture) { UTF8Encoding transform = new UTF8Encoding(false, false); BinaryWriter writer = new BinaryWriter(new MemoryStream()); foreach (CoreTextureKeyValuePair pair in texture.keyValuePairs) { byte[] keyInBytes = transform.GetBytes(pair.key); byte[] valueInBytes = transform.GetBytes(pair.value); UInt32 len = (UInt32)(keyInBytes.Length + 1 + valueInBytes.Length + 1); writer.Write(len); writer.Write(keyInBytes); writer.Write((byte)0); writer.Write(valueInBytes); writer.Write((byte)0); uint padding = 3 - ((len + 3) % 4); for (uint p = 0; p < padding; ++p) { writer.Write((byte)0); } } return(((MemoryStream)writer.BaseStream).ToArray()); }
public GenericImage ToGenericImage(CoreTexture texture, IGLDataFormat dataFormat) { GenericImage image = null; if (texture.glFormat == Value && dataFormat.Value == texture.glType) { image = new GenericImage(); image.channels = (uint)Channels; image.width = texture.pixelWidth; image.height = texture.pixelHeight; image.depth = texture.pixelDepth != 0 ? texture.pixelDepth : 1; image.arrays = texture.numberOfArrayElements != 0 ? texture.numberOfArrayElements : 1; image.faces = texture.numberOfFaces; image.mipmapLevels = new GenericImageMipmapLevel[texture.mipmapLevels.Length]; for (int level = 0; level < texture.mipmapLevels.Length; ++level) { image.mipmapLevels[level] = new GenericImageMipmapLevel(); image.mipmapLevels[level].pixels = ToGenericMipImage(texture, dataFormat, level).ToArray(); } } return(image); }
// texture and image contain the same image, for some containers its easyer to use image instead of texture public void Store(string path, CoreTexture texture, GenericImage image) { TGAImage tga = new TGAImage(image); tga.Save(path); }
virtual protected double ToGenericImagePixelAtPixel(CoreTexture texture, IGLDataFormat dataFormat, int mipIndex, int pixelIndex, int channelIndex) { long offset = pixelIndex * dataFormat.PixelSize(Channels); return(dataFormat.ToGenericFormat(texture.mipmapLevels[mipIndex].pixels, (int)offset, channelIndex, Channels)); }
void WriteToBinaryWriter(BinaryWriter writer, CoreTexture texture) { WriteHeader(writer, texture); WriteKeyValuePairs(writer, texture); WriteImages(writer, texture); }
public CoreTexture Load(string path) { Bitmap image = new Bitmap(path); CoreTexture texture = new CoreTexture(); texture.glTypeSize = 1; texture.glType = 0x1401; int channels = 0; switch (image.PixelFormat) { default: case System.Drawing.Imaging.PixelFormat.Undefined: return(null); case System.Drawing.Imaging.PixelFormat.Format16bppRgb555: case System.Drawing.Imaging.PixelFormat.Format16bppRgb565: case System.Drawing.Imaging.PixelFormat.Format24bppRgb: case System.Drawing.Imaging.PixelFormat.Format32bppRgb: texture.glFormat = 0x1907; texture.glInternalFormat = 0x1907; texture.glBaseInternalFormat = 0x1907; channels = 3; break; case System.Drawing.Imaging.PixelFormat.Indexed: case System.Drawing.Imaging.PixelFormat.Format1bppIndexed: case System.Drawing.Imaging.PixelFormat.Format4bppIndexed: case System.Drawing.Imaging.PixelFormat.Format8bppIndexed: case System.Drawing.Imaging.PixelFormat.Gdi: case System.Drawing.Imaging.PixelFormat.Alpha: case System.Drawing.Imaging.PixelFormat.PAlpha: case System.Drawing.Imaging.PixelFormat.Format16bppArgb1555: case System.Drawing.Imaging.PixelFormat.Format32bppPArgb: case System.Drawing.Imaging.PixelFormat.Format16bppGrayScale: case System.Drawing.Imaging.PixelFormat.Format48bppRgb: case System.Drawing.Imaging.PixelFormat.Format64bppPArgb: case System.Drawing.Imaging.PixelFormat.Canonical: case System.Drawing.Imaging.PixelFormat.Format32bppArgb: case System.Drawing.Imaging.PixelFormat.Format64bppArgb: texture.glFormat = 0x1908; texture.glInternalFormat = 0x1908; texture.glBaseInternalFormat = 0x1908; channels = 4; break; } texture.pixelWidth = (UInt32)image.Width; texture.pixelHeight = (UInt32)image.Height; texture.pixelDepth = 0; texture.numberOfArrayElements = 0; texture.numberOfFaces = 1; texture.keyValuePairs = new CoreTextureKeyValuePair[0]; texture.mipmapLevels = new CoreTextureMipmapLevel[1]; texture.mipmapLevels[0] = new CoreTextureMipmapLevel(texture.pixelWidth * texture.pixelHeight * channels); byte[] pixBuf = new byte[] { 0, 0, 0, 1 }; for (int y = 0; y < texture.pixelHeight; ++y) { for (int x = 0; x < texture.pixelWidth; ++x) { Color color = image.GetPixel(x, y); pixBuf[0] = color.R; pixBuf[1] = color.G; pixBuf[2] = color.B; pixBuf[3] = color.A; for (int channel = 0; channel < channels; ++channel) { int offset = (int)((x + y * texture.pixelWidth) * channels) + channel; texture.mipmapLevels[0].pixels[offset] = pixBuf[channel]; } } } return(texture); }
void readKeyValuePairsFromBinaryReader(Func <UInt32> uintReader, Func <UInt32, byte[]> byteReader, UInt32 bytesOfKeyValueData, ref CoreTexture resultTexture) { List <CoreTextureKeyValuePair> keyValuePairs = new List <CoreTextureKeyValuePair>(); while (bytesOfKeyValueData > 4) { UInt32 bytesRead; keyValuePairs.Add(readKeyValuePairFromBinaryReader(uintReader, byteReader, out bytesRead)); bytesOfKeyValueData -= bytesRead; } if (bytesOfKeyValueData > 0) { // skip over rest byteReader(bytesOfKeyValueData); } resultTexture.keyValuePairs = keyValuePairs.ToArray(); }
void readMipmapLevelFromBinaryReader(Func <UInt32> uintReader, Func <UInt32, byte[]> byteReader, ref CoreTexture resultTexture, UInt32 levelIndex) { UInt32 imageSize = uintReader(); imageSize = (imageSize + 3) & ~(UInt32)3; resultTexture.mipmapLevels[levelIndex].pixels = byteReader(imageSize); }
void readMipmapLevelsFromBinaryReader(Func <UInt32> uintReader, Func <UInt32, byte[]> byteReader, ref CoreTexture resultTexture) { for (UInt32 level = 0; level < resultTexture.mipmapLevels.Length; ++level) { resultTexture.mipmapLevels[level] = new CoreTextureMipmapLevel(); readMipmapLevelFromBinaryReader(uintReader, byteReader, ref resultTexture, level); } }
public GenericImage ToGenericImage(CoreTexture texture, IGLPixelFormat pixelFormat, IGLDataFormat dataFormat) { return(pixelFormat.ToGenericImage(texture, dataFormat)); }