public static byte[] CompressImage(Image image, DXTCompression compression) { Bitmap bitmap = new Bitmap(image); byte[] pixels = new byte[image.Width * image.Height * 4]; List <Color> pixelList = new List <Color>(); for (int j = 0; j < image.Height; j++) { for (int i = 0; i < image.Width; i++) { Color color = bitmap.GetPixel(i, j); pixelList.Add(color); } } int index = 0; for (int i = 0; i < pixelList.Count; i++) { Color color = pixelList[i]; pixels[index + 0] = color.B; pixels[index + 1] = color.G; pixels[index + 2] = color.R; pixels[index + 3] = color.A; index += 4; } byte[] result = CompressionTools.CompressTexture(pixels, image.Width, image.Height, compression); return(result); }
public RangeFit(ColourSet colours, DXTCompression compression) : base(colours, compression) { bool colourMetricPerceptual = false; // initialize the metric bool perceptual = colourMetricPerceptual; if (perceptual) { Metric = new Vec3(0.2126f, 0.7152f, 0.0722f); } else { Metric = new Vec3(0); } // initialize the best error BestError = float.MaxValue; // cache some values int count = Colours.Count; Vec3[] values = Colours.Points; float[] weights = Colours.Weights; // get the covariance matrix /*Sym3x3 covariance = ComputeWeightedCovariance(count, values, weights); * * // compute the principle components * Vec3 principle = ComputePrincipleComponent(covariance); * * // get the min and max range as the codebook endpoints * Vec3 start = new Vec3(0.0f); * Vec3 end = new Vec3(0.0f); * if (count > 0) * { * float min, max; * * // compute the range * start = end = values[0]; * * }*/ }
public static byte[] DecompressTexture(byte[] source, int width, int height, DXTCompression compression) { return(DDS.DecompressTexture(source, width, height, (DDSCompression)compression)); }
public static byte[] DecompressImage(byte[] data, int width, int height, DXTCompression compression) { return(CompressionTools.DecompressTexture(data, width, height, compression)); }
public ClusterFit(ColourSet colours, DXTCompression compression) : base(colours, compression) { Order = new byte[16 * MaxIterations]; PointWeights = new Vec4[16]; }
public static ImageInfo SetImage(Image newImage, CompressionType compression) { int width = newImage.Width; int height = newImage.Height; byte[] header = null; byte[] data = null; if (compression == CompressionType.BGRA) { Bitmap image = new Bitmap(newImage); MemoryStream mspixels = new MemoryStream(); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { Color pixel = image.GetPixel(j, i); mspixels.WriteByte(pixel.B); mspixels.WriteByte(pixel.G); mspixels.WriteByte(pixel.R); mspixels.WriteByte(pixel.A); } } data = mspixels.ToArray(); MemoryStream msx = new MemoryStream(); BinaryWriter bw = new BinaryWriter(msx); bw.Write((int)0); bw.Write((int)0); bw.Write((int)0); bw.Write((int)1); bw.Write((int)0x15); bw.Write((short)width); bw.Write((short)height); bw.Write((int)0x15); bw.Write((int)0); bw.Flush(); header = msx.ToArray(); bw.Close(); } else { DXTCompression dxt = DXTCompression.DXT1; if (compression == CompressionType.DXT3) { dxt = DXTCompression.DXT1; } else if (compression == CompressionType.DXT5) { dxt = DXTCompression.DXT5; } data = ImageUtil.CompressImage(newImage, dxt); MemoryStream msx = new MemoryStream(); BinaryWriter bw = new BinaryWriter(msx); bw.Write((int)0); bw.Write((int)0); bw.Write((int)0); bw.Write((int)1); bw.Write(Encoding.ASCII.GetBytes(compression.ToString())); bw.Write((short)width); bw.Write((short)height); bw.Write((int)0x15); bw.Write((int)0); bw.Flush(); header = msx.ToArray(); bw.Close(); } return(new ImageInfo(header, data)); }
public ColourSet(byte[] rgba, int mask, DXTCompression compression) { Count = 0; Transparent = false; Points = new Vec3[16]; Weights = new float[16]; Remap = new int[16]; // Check the compression mode for dxt1 bool isDxt1 = compression == DXTCompression.DXT1; bool weightByAlpha = false; // Create the minimal set for (int i = 0; i < 16; ++i) { // Check if this pixel is enabled int bit = 1 << i; if ((mask & bit) == 0) { Remap[i] = -1; continue; } // Check for transparent pixels when using dxt1 if (isDxt1 && rgba[4 * i + 3] < 128) { Remap[i] = -1; Transparent = true; continue; } // Loop over previous points for a match for (int j = 0; ; ++j) { // allocate a new point if (j == i) { // normalize coordinates to [0,1] float x = (float)rgba[4 * i + 0] / 255.0f; float y = (float)rgba[4 * i + 1] / 255.0f; float z = (float)rgba[4 * i + 2] / 255.0f; // ensure there is always non-zero weight even for zero alpha float w = (float)(rgba[4 * i + 3] + 1) / 256.0f; // add the point Points[Count] = new Vec3(x, y, z); Weights[Count] = (weightByAlpha ? w : 1.0f); Remap[i] = Count; // Advance ++Count; break; } // check for a match int oldbit = 1 << j; bool match = ((mask & oldbit) != 0) && (rgba[4 * i + 0] == rgba[4 * j + 0]) && (rgba[4 * i + 1] == rgba[4 * j + 1]) && (rgba[4 * i + 2] == rgba[4 * j + 2]) && (rgba[4 * j + 3] >= 128 || !isDxt1); if (match) { // get the index of the match int index = Remap[j]; // ensure there is always non-zero weight even for zero alpha float w = (float)(rgba[4 * i + 3] + 1) / 256.0f; // map to this point and increase the weight Weights[index] += (weightByAlpha ? w : 1.0f); Remap[i] = index; break; } } } // square root the weights for (int i = 0; i < Count; ++i) { Weights[i] = (float)Math.Sqrt(Weights[i]); } }
public ColourFit(ColourSet colours, DXTCompression compression) { Colours = colours; Compression = compression; }