private static unsafe IImage ConvertHdrToBitmap( IList <float> hdr, int width, int height, float max) { var bitmap = new Rgba32Image(width, height); bitmap.Mutate((_, setHandler) => { var offset = 0; for (var y = 0; y < height; ++y) { for (var x = 0; x < width; ++x) { var r = hdr[offset + 0] / max * 255; var g = hdr[offset + 1] / max * 255; var b = hdr[offset + 2] / max * 255; // For some reason, alpha isn't used? // TODO: How is this factored in? var a = 255f; //hdrImage[offset + 3] / max * 255; setHandler(x, y, (byte)r, (byte)g, (byte)b, (byte)a); } } }); return(bitmap); }
public static IImage Create1x1WithColor(Color color) { var bmp = new Rgba32Image(1, 1); bmp.Mutate((_, setHandler) => setHandler(0, 0, color.R, color.G, color.B, color.A)); return(bmp); }
public static unsafe IImage DecompressDXT1( byte[] src, int srcOffset, int width, int height) { int offset = srcOffset; int bcw = (width + 3) / 4; int bch = (height + 3) / 4; int clen_last = (width + 3) % 4 + 1; int[] buffer = new int[16]; int[] colors = new int[4]; var bitmap = new Rgba32Image(width, height); bitmap.Mutate((_, setHandler) => { for (int t = 0; t < bch; t++) { for (int s = 0; s < bcw; s++, offset += 8) { var x = s * 4; int r0, g0, b0, r1, g1, b1; int q0 = src[offset + 0] | src[offset + 1] << 8; int q1 = src[offset + 2] | src[offset + 3] << 8; Rgb565(q0, out r0, out g0, out b0); Rgb565(q1, out r1, out g1, out b1); colors[0] = Color(r0, g0, b0, 255); colors[1] = Color(r1, g1, b1, 255); if (q0 > q1) { colors[2] = Color((r0 * 2 + r1) / 3, (g0 * 2 + g1) / 3, (b0 * 2 + b1) / 3, 255); colors[3] = Color((r0 + r1 * 2) / 3, (g0 + g1 * 2) / 3, (b0 + b1 * 2) / 3, 255); } else { colors[2] = Color((r0 + r1) / 2, (g0 + g1) / 2, (b0 + b1) / 2, 255); } uint d = BitConverter.ToUInt32(src, offset + 4); for (var i = 0; i < 16; i++, d >>= 2) { buffer[i] = colors[d & 3]; } var clen = (s < bcw - 1 ? 4 : clen_last); for (int i = 0, y = t * 4; i < 4 && y < height; i++, y++) { for (var c = 0; c < clen; ++c) { var color = buffer[4 * i + c]; SplitColor_(color, out var r, out var g, out var b, out var a); setHandler(x + c, y, r, g, b, a); } } } } }); return(bitmap); }
internal Ia16Image(Bitmap bitmap) { this.impl_ = new Rgba32Image(bitmap); }
public IImage ToBitmap() { ImageDataFormat imageDataFormat = (ImageDataFormat)null; switch ((byte)this.Header.Format) { case 0: imageDataFormat = ImageDataFormat.I4; break; case 1: imageDataFormat = ImageDataFormat.I8; break; case 2: imageDataFormat = ImageDataFormat.IA4; break; case 3: imageDataFormat = ImageDataFormat.IA8; break; case 4: imageDataFormat = ImageDataFormat.RGB565; break; case 5: imageDataFormat = ImageDataFormat.RGB5A3; break; case 6: imageDataFormat = ImageDataFormat.Rgba32; break; case 14: imageDataFormat = ImageDataFormat.Cmpr; break; } byte[] numArray = imageDataFormat.ConvertFrom( this.Data, (int)this.Header.Width, (int)this.Header.Height, (ProgressChangedEventHandler)null); var width = this.Header.Width; var height = this.Header.Height; var bitmap = new Rgba32Image(width, height); bitmap.Mutate((_, setHandler) => { for (var y = 0; y < height; ++y) { for (var x = 0; x < width; ++x) { var i = 4 * (y * width + x); var b = numArray[i + 0]; var g = numArray[i + 1]; var r = numArray[i + 2]; var a = numArray[i + 3]; setHandler(x, y, r, g, b, a); } } }); return(bitmap); }
public IMipMap <IImage> Read(Stream stream) { using var pfimImage = Pfim.Pfim.FromStream(stream); return(MipMapUtil.From( pfimImage.MipMaps.Select(pfimMipMap => { var mmWidth = pfimMipMap.Width; var mmHeight = pfimMipMap.Height; switch (pfimImage.Format) { case ImageFormat.Rgba32: { var image = new Rgba32Image(mmWidth, mmHeight); image.Mutate((_, setHandler) => { for (var y = 0; y < mmHeight; ++y) { for (var x = 0; x < mmWidth; ++x) { var i = pfimMipMap.DataOffset + 4 * (y * mmWidth + x); var b = pfimImage.Data[i + 0]; var g = pfimImage.Data[i + 1]; var r = pfimImage.Data[i + 2]; var a = pfimImage.Data[i + 3]; setHandler(x, y, r, g, b, a); } } }); return image as IImage; } case ImageFormat.Rgb24: { var image = new Rgb24Image(mmWidth, mmHeight); image.Mutate((_, setHandler) => { for (var y = 0; y < mmHeight; ++y) { for (var x = 0; x < mmWidth; ++x) { var i = pfimMipMap.DataOffset + 3 * (y * mmWidth + x); var b = pfimImage.Data[i + 0]; var g = pfimImage.Data[i + 1]; var r = pfimImage.Data[i + 2]; setHandler(x, y, r, g, b); } } }); return image; } default: throw new NotImplementedException( $"Unsupported Pfim format: {pfimImage.Format}"); } }) .ToList())); }