public static void ImageFromPackedBuffer(WriteableBitmap target, byte[] packedBuffer, int offset, bool rotate, int stride, int width, int height, PixelFormat pixelFormat, byte[] tempIntermediate = null) { var intermediateImage = tempIntermediate ?? new byte[stride * height]; BadgeImage.PackedBufferToIntermediateImage(packedBuffer, intermediateImage, pixelFormat, offset, rotate); ImageFromIntermediate(target, intermediateImage, stride, width, height); }
public bool SameDimentions(int widthInPixels, int height, PixelFormat packedFormat) { return (PackedFormat == packedFormat && Height == height && WidthInBlocks == BadgeImage.CalculatePackedPixelBlocks(widthInPixels)); }
public void PackBuffer(bool rotate) { if (PackedBuffer == null) { PackedBuffer = new byte[BadgeImage.CalculatePackedBufferSize(WidthInPixels, Height, PackedFormat)]; } BadgeImage.IntermediateImagetoPackedBuffer(IntermediateImage, PackedBuffer, PackedFormat, 0, rotate); }
public BadgeRenderTarget(int widthInPixels, int height, PixelFormat packedFormat, byte[] intermediateImage = null) { WidthInBlocks = BadgeImage.CalculatePackedPixelBlocks(widthInPixels); WidthInPixels = widthInPixels; Height = height; PackedFormat = packedFormat; IntermediateStride = WidthInBlocks * BadgeCaps.PixelsPerBlockBitPlane; IntermediateImage = intermediateImage ?? new byte[IntermediateStride * height]; }
public static void Unpack8GrayPix(Pix2x8 pix, out byte p0, out byte p1, out byte p2, out byte p3, out byte p4, out byte p5, out byte p6, out byte p7) { p7 = BadgeImage.PixToSrgbGray((byte)(((pix.Value & 0x0100) >> 7) | ((pix.Value & 0x01) >> 0))); p6 = BadgeImage.PixToSrgbGray((byte)(((pix.Value & 0x0200) >> 8) | ((pix.Value & 0x02) >> 1))); p5 = BadgeImage.PixToSrgbGray((byte)(((pix.Value & 0x0400) >> 9) | ((pix.Value & 0x04) >> 2))); p4 = BadgeImage.PixToSrgbGray((byte)(((pix.Value & 0x0800) >> 10) | ((pix.Value & 0x08) >> 3))); p3 = BadgeImage.PixToSrgbGray((byte)(((pix.Value & 0x1000) >> 11) | ((pix.Value & 0x10) >> 4))); p2 = BadgeImage.PixToSrgbGray((byte)(((pix.Value & 0x2000) >> 12) | ((pix.Value & 0x20) >> 5))); p1 = BadgeImage.PixToSrgbGray((byte)(((pix.Value & 0x4000) >> 13) | ((pix.Value & 0x40) >> 6))); p0 = BadgeImage.PixToSrgbGray((byte)(((pix.Value & 0x8000) >> 14) | ((pix.Value & 0x80) >> 7))); }
public static void IntermediateImagetoPackedBuffer(byte[] intermediateImage, byte[] packedBuffer, PixelFormat pixelFormat, int offset, bool rotate) { int packedI = offset; if (rotate) { for (int p = intermediateImage.Length - 8; p >= 0; p -= 8) { var pix = BadgeImage.Pack8GrayPix( intermediateImage[p + 7], intermediateImage[p + 6], intermediateImage[p + 5], intermediateImage[p + 4], intermediateImage[p + 3], intermediateImage[p + 2], intermediateImage[p + 1], intermediateImage[p + 0]); packedBuffer[packedI++] = (byte)(pix.Value >> 8); if (pixelFormat == PixelFormat.TwoBits) { packedBuffer[packedI++] = (byte)(pix.Value & 0xFF); } } } else { for (int p = 0; p < intermediateImage.Length; p += 8) { var pix = BadgeImage.Pack8GrayPix( intermediateImage[p + 0], intermediateImage[p + 1], intermediateImage[p + 2], intermediateImage[p + 3], intermediateImage[p + 4], intermediateImage[p + 5], intermediateImage[p + 6], intermediateImage[p + 7]); packedBuffer[packedI++] = (byte)(pix.Value >> 8); if (pixelFormat == PixelFormat.TwoBits) { packedBuffer[packedI++] = (byte)(pix.Value & 0xFF); } } } }
public void Render(BadgeRenderTarget rt, int parentRenderX, int parentRenderY) { if (EnableBlend) { BadgeImage.Blit( rt.IntermediateImage, rt.IntermediateStride, rt.WidthInPixels, rt.Height, m_cachedIntermediate.IntermediateImage, m_alphaMask, m_cachedIntermediate.IntermediateStride, m_cachedIntermediate.WidthInPixels, m_cachedIntermediate.Height, parentRenderX + RenderX, parentRenderY + RenderY, ClipX, ClipY, ClipWidth, ClipHeight); } else { BadgeImage.Blit( rt.IntermediateImage, rt.IntermediateStride, rt.WidthInPixels, rt.Height, m_cachedIntermediate.IntermediateImage, m_cachedIntermediate.IntermediateStride, m_cachedIntermediate.WidthInPixels, m_cachedIntermediate.Height, parentRenderX + RenderX, parentRenderY + RenderY, ClipX, ClipY, ClipWidth, ClipHeight); } }
public static void DitherImage(byte[] intermediateImage, int stride, int width, int height) { for (int y = 0, srcIy = 0; y < height; ++y, srcIy += stride) { for (int x = 0, srcI = srcIy; x < width; ++x, ++srcI) { int p = intermediateImage[srcI]; int withError = p; //p > (255 - 32) ? 255 : p + 32; byte rounded = BadgeImage.PixToSrgbGray(BadgeImage.SrgbGrayToPix((byte)withError)); intermediateImage[srcI] = rounded; int newError = withError - rounded; int scaledError = (newError << 10) / 42; for (int ei = 0; ei < s_StuckiPattern.Length; ++ei) { var e = s_StuckiPattern[ei]; int ex = x + e.Item1; int ey = y + e.Item2; if (ex >= 0 && ey >= 0 && ex < width && ey < height) { int weightedError = (scaledError * e.Item3) >> 10; int diffusedIndex = ey * stride + ex; int newValue = intermediateImage[diffusedIndex] + weightedError; if (newValue < 0) { intermediateImage[diffusedIndex] = 0; } else if (newValue > 255) { intermediateImage[diffusedIndex] = 255; } else { intermediateImage[diffusedIndex] = (byte)newValue; } } } } } }
public static void Read32BitImage(byte[] intermediateImage, BitmapSource src, int srcX, int srcY, int stride, int width, int height) { System.Diagnostics.Debug.Assert(src.Format.BitsPerPixel == 32); int[] pix = new int[width * height]; src.CopyPixels(new Int32Rect(srcX, srcY, width, height), pix, width * sizeof(int), 0); int si = 0; int di0 = 0; for (int y = 0; y < height; ++y, di0 += stride) { int di = di0; for (int x = 0; x < width; ++x, ++si, ++di) { int p = pix[si]; intermediateImage[di] = BadgeImage.ToGray((p >> 16) & 0xFF, (p >> 8) & 0xFF, (p >> 0) & 0xFF); } } }
public static void ReadBitmap(byte[] intermediateImage, Bitmap bitmap, int srcX, int srcY, int stride, int width, int height) { var data = bitmap.LockBits( new System.Drawing.Rectangle(srcX, srcY, width, height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); try { IntPtr p0 = data.Scan0; for (int y = 0, i0 = 0; y < height; ++y, p0 += data.Stride, i0 += stride) { IntPtr p = p0; for (int x = 0, i = i0; x < width; ++x, ++i, p += 4) { int val = Marshal.ReadInt32(p); intermediateImage[i] = BadgeImage.ToGray((val >> 16) & 0xFF, (val >> 8) & 0xFF, (val >> 0) & 0xFF); } } } finally { bitmap.UnlockBits(data); } }
public void DitherImage() { BadgeImage.DitherImage(IntermediateImage, IntermediateStride, WidthInPixels, Height); }
public static Color ColorFromPix(byte value) { byte g = BadgeImage.PixToSrgbGray(value); return(Color.FromRgb(g, g, g)); }