/// <inheritdoc cref="Save"/> public byte[] Save(IEnumerable <Color> colors, EncodingSaveContext saveContext) { // Get colors in unswizzled order, so the framework applies the swizzle correctly var paddedWidth = GetPaddedWidth(saveContext.Size.Width); var swizzle = GetSwizzle(saveContext.Size.Width); var colorData = new byte[saveContext.Size.Width * saveContext.Size.Height * 4]; var index = 0; foreach (var color in colors) { var targetPoint = swizzle.Get(index / 4); var textureIndex = (targetPoint.Y * paddedWidth + targetPoint.X) * 4; colorData[textureIndex] = color.R; colorData[textureIndex + 1] = color.G; colorData[textureIndex + 2] = color.B; colorData[textureIndex + 3] = color.A; index += 4; } // Initialize PVR Texture var pvrTexture = PvrTexture.Create(colorData, (uint)saveContext.Size.Width, (uint)saveContext.Size.Height, 1, PixelFormat.RGBA8888, ChannelType.UnsignedByteNorm, ColorSpace.Linear); // Transcode texture to PVRTC pvrTexture.Transcode((PixelFormat)_format, ChannelType.UnsignedByteNorm, ColorSpace.Linear, CompressionQuality.PVRTCHigh); return(pvrTexture.GetData()); }
/// <inheritdoc cref="Load"/> public IEnumerable <Color> Load(byte[] tex, EncodingLoadContext loadContext) { // Initialize PVR Texture var pvrTexture = PvrTexture.Create(tex, (uint)loadContext.Size.Width, (uint)loadContext.Size.Height, 1, (PixelFormat)_format, ChannelType.UnsignedByte, ColorSpace.Linear); // Transcode texture to RGBA8888 var successful = pvrTexture.Transcode(PixelFormat.RGBA8888, ChannelType.UnsignedByteNorm, ColorSpace.Linear, CompressionQuality.PVRTCHigh); if (!successful) { throw new InvalidOperationException("Transcoding with PVRTexLib was not successful."); } // Yield colors // Get colors in unswizzled order, so the framework applies the swizzle correctly var paddedWidth = GetPaddedWidth(loadContext.Size.Width); var swizzle = GetSwizzle(loadContext.Size.Width); var textureData = pvrTexture.GetData(); for (var y = 0; y < loadContext.Size.Height; y++) { for (var x = 0; x < loadContext.Size.Width; x++) { var sourcePoint = swizzle.Get(y * paddedWidth + x); var textureIndex = (sourcePoint.Y * paddedWidth + sourcePoint.X) * 4; yield return(Color.FromArgb(textureData[textureIndex + 3], textureData[textureIndex], textureData[textureIndex + 1], textureData[textureIndex + 2])); } } }
/// <inheritdoc cref="Load"/> public IEnumerable <Color> Load(byte[] tex, EncodingLoadContext loadContext) { // Initialize PVR Texture var pvrTexture = PvrTexture.Create(tex, (uint)loadContext.Size.Width, (uint)loadContext.Size.Height, 1, (PixelFormat)_format, ChannelType.UnsignedByte, ColorSpace.Linear); // Transcode texture to RGBA8888 var successful = pvrTexture.Transcode(PixelFormat.RGBA8888, ChannelType.UnsignedByteNorm, ColorSpace.Linear, CompressionQuality.PVRTCHigh); if (!successful) { throw new InvalidOperationException("Transcoding with PVRTexLib was not successful."); } // Yield colors var textureData = pvrTexture.GetData(); for (var i = 0L; i < textureData.Length; i += 4) { yield return(Color.FromArgb(textureData[i + 3], textureData[i], textureData[i + 1], textureData[i + 2])); } }
/// <inheritdoc cref="Save"/> public byte[] Save(IEnumerable <Color> colors, EncodingSaveContext saveContext) { var colorData = new byte[saveContext.Size.Width * saveContext.Size.Height * 4]; var index = 0; foreach (var color in colors) { colorData[index++] = color.R; colorData[index++] = color.G; colorData[index++] = color.B; colorData[index++] = color.A; } // Initialize PVR Texture var pvrTexture = PvrTexture.Create(colorData, (uint)saveContext.Size.Width, (uint)saveContext.Size.Height, 1, PixelFormat.RGBA8888, ChannelType.UnsignedByteNorm, ColorSpace.Linear); // Transcode texture to PVRTC pvrTexture.Transcode((PixelFormat)_format, ChannelType.UnsignedByteNorm, ColorSpace.Linear, CompressionQuality.PVRTCHigh); return(pvrTexture.GetData()); }