public void SetPixel(int x, int y, Color color) { _deviceStream.Seek(PointToOffset(x, y), SeekOrigin.Begin); var rgb = color.ToPixel <Rgb24>(); _deviceStream.Write(BitConverter.GetBytes(Rgb565.Pack(rgb.R, rgb.G, rgb.B)), 0, 2); }
/// <summary> /// Gets the image from file. /// </summary> /// <param name="fb0">The FB0.</param> /// <returns></returns> private Image GetImageFromFile(string fb0) { try { Image img = Rgb565.ToImage(fb0); System.IO.File.Delete(fb0); return(img); } catch (Exception ex) { //System.Console.WriteLine ( ex ); return(null); } }
/// <summary> /// Gets the image from raw image. /// </summary> /// <param name="ri">The ri.</param> /// <returns></returns> private Image GetImageFromRawImage(RawImage ri) { try { this.LogDebug("Image Size: {0}", ri.Size.ToString( )); if (ri.Bpp == 16) { return(ri.ToImage(PixelFormat.Format16bppRgb565)); } else if (ri.Bpp == 32) { return(ri.ToImage(PixelFormat.Format32bppArgb)); } else { return(Rgb565.ToImage(ri.Data)); } } catch (Exception ex) { //Console.WriteLine ( ex ); return(null); } }
public BC1Block Encode(Bc1BlockData block) { BC1Block ret; if (block.AlphaMask == 0xFFFF) { ret.PackedValue = BC1Block.TransparentValue; return(ret); } QuantizeValues(block); SpanValues(block, out var r0, out var r1); //quantize the endpoints bool weightValues = !UseUniformWeighting; if (weightValues) { r0.R *= RInvWeight; r0.G *= GInvWeight; r0.B *= BInvWeight; r1.R *= RInvWeight; r1.G *= GInvWeight; r1.B *= BInvWeight; } var pr0 = Rgb565.Pack(r0); var pr1 = Rgb565.Pack(r1); if (block.AlphaMask == 0 && pr0.PackedValue == pr1.PackedValue) { return(new BC1Block(pr0, pr1)); } pr0.Unpack(out r0); pr1.Unpack(out r1); if (weightValues) { r0.R *= RWeight; r0.G *= GWeight; r0.B *= BWeight; r1.R *= RWeight; r1.G *= GWeight; r1.B *= BWeight; } //interp out the steps RgbF32 s0; if ((block.AlphaMask != 0) == (pr0.PackedValue <= pr1.PackedValue)) { ret = new BC1Block(pr0, pr1); block.InterpValues[0] = s0 = r0; block.InterpValues[1] = r1; } else { ret = new BC1Block(pr1, pr0); block.InterpValues[0] = s0 = r1; block.InterpValues[1] = r0; } uint[] pSteps; if (block.AlphaMask != 0) { pSteps = pSteps3; RgbF32.Lerp(out block.InterpValues[2], block.InterpValues[0], block.InterpValues[1], 0.5F); } else { pSteps = pSteps4; RgbF32.Lerp(out block.InterpValues[2], block.InterpValues[0], block.InterpValues[1], 1.0F / 3.0F); RgbF32.Lerp(out block.InterpValues[3], block.InterpValues[0], block.InterpValues[1], 2.0F / 3.0F); } //find the best Values RgbF32 dir; dir.R = block.InterpValues[1].R - s0.R; dir.G = block.InterpValues[1].G - s0.G; dir.B = block.InterpValues[1].B - s0.B; float fSteps = block.AlphaMask != 0 ? 2 : 3; float fScale = (pr0.PackedValue != pr1.PackedValue) ? (fSteps / (dir.R * dir.R + dir.G * dir.G + dir.B * dir.B)) : 0.0F; dir.R *= fScale; dir.G *= fScale; dir.B *= fScale; bool dither = DitherRgb; if (dither) { if (block.Error == null) { block.Error = new RgbF32[16]; } else { Array.Clear(block.Error, 0, 16); } } for (int i = 0; i < block.Values.Length; i++) { if ((block.AlphaMask & (1 << i)) != 0) { ret.PackedValue |= 3U << (32 + i * 2); } else { var cl = block.Values[i]; if (weightValues) { cl.R *= RWeight; cl.G *= GWeight; cl.B *= BWeight; } if (dither) { var e = block.Error[i]; cl.R += e.R; cl.G += e.G; cl.B += e.B; } float fDot = (cl.R - s0.R) * dir.R + (cl.G - s0.G) * dir.G + (cl.B - s0.B) * dir.B; uint iStep; if (fDot <= 0) { iStep = 0; } else if (fDot >= fSteps) { iStep = 1; } else { iStep = pSteps[(int)(fDot + 0.5F)]; } ret.PackedValue |= (ulong)iStep << (32 + i * 2); if (dither) { RgbF32 e, d, interp = block.InterpValues[iStep]; d.R = cl.R - interp.R; d.G = cl.G - interp.G; d.B = cl.B - interp.B; if ((i & 3) != 3) { e = block.Error[i + 1]; e.R += d.R * (7.0F / 16.0F); e.G += d.G * (7.0F / 16.0F); e.B += d.B * (7.0F / 16.0F); block.Error[i + 1] = e; } if (i < 12) { if ((i & 3) != 0) { e = block.Error[i + 3]; e.R += d.R * (3.0F / 16.0F); e.G += d.G * (3.0F / 16.0F); e.B += d.B * (3.0F / 16.0F); block.Error[i + 3] = e; } e = block.Error[i + 4]; e.R += d.R * (5.0F / 16.0F); e.G += d.G * (5.0F / 16.0F); e.B += d.B * (5.0F / 16.0F); block.Error[i + 4] = e; if (3 != (i & 3)) { e = block.Error[i + 5]; e.R += d.R * (1.0F / 16.0F); e.G += d.G * (1.0F / 16.0F); e.B += d.B * (1.0F / 16.0F); block.Error[i + 5] = e; } } } } } return(ret); }