public void Rgb2CrTest() { Pixel[] pixBuffer = new Pixel[256]; for (int i = 0; i < pixBuffer.Length; i++) { pixBuffer[i].SetGray((sbyte)(i - 128)); } sbyte[] outBuff = new sbyte[256]; unsafe { fixed(Pixel *p = pixBuffer) fixed(sbyte *pOutBuff = outBuff) { Pixel *pBuff = p; sbyte *pOut = pOutBuff; InterWaveTransform.Rgb2Y(pBuff, 16, 16, 16 * 3, pOut, 16); } } int outLength = outBuff.Length - 1; for (int i = 0; i < outLength; i++) { if (i > 0) { if (i < 94) { Assert.True(unchecked ((byte)outBuff[i]) <= unchecked ((byte)outBuff[i + 1])); } Assert.NotEqual(0, unchecked ((byte)outBuff[i])); } } }
public static extern void GlTexSubImage2D(uint target, int level, int xoffset, int yoffset, int width, int height, uint format, uint type, Pixel *pixels);
public static extern void GlTexImage2D(uint target, int level, uint internalformat, int width, int height, int border, uint format, uint type, Pixel *pixels);
public unsafe static extern void RenderText(int scrW, int scrH, int width, int height, Pixel *pixels);
public unsafe static extern void RenderPixels(int width, int height, Pixel *pixels);
/// <summary> /// Initializes an InterWavePixelMap with color image #bm#. This constructor /// performs the wavelet decomposition of image #bm# and records the /// corresponding wavelet coefficients. Argument #mask# is an optional /// bilevel image specifying the masked pixels(see \Ref{ IW44Image.h}). /// Argument #crcbmode# specifies how the chrominance information should be /// encoded(see \Ref{ CRCBMode}). /// </summary> /// <param name="bm"></param> /// <param name="mask"></param> /// <param name="mode"></param> public unsafe void InitializeEncoder(IPixelMap pm, Bitmap gmask = null, YCrCbMode crcbmode = YCrCbMode.Normal) { /* Free */ CloseEncoder(); // Handle CRCB mode switch (crcbmode) { case YCrCbMode.None: _CrCbHalf = true; _CrCbDelay = -1; break; case YCrCbMode.Half: _CrCbHalf = true; _CrCbDelay = 10; break; case YCrCbMode.Normal: _CrCbHalf = false; _CrCbDelay = 10; break; case YCrCbMode.Full: _CrCbHalf = false; _CrCbDelay = 0; break; } // Prepare mask information sbyte *msk8 = (sbyte *)IntPtr.Zero; int mskrowsize = 0; Bitmap mask = gmask; GCHandle hMask = default(GCHandle); if (mask != null) { hMask = GCHandle.Alloc(mask.Data, GCHandleType.Pinned); msk8 = (sbyte *)hMask.AddrOfPinnedObject(); mskrowsize = mask.GetRowSize(); } /* Create */ int width = pm.Width; int height = pm.Height; sbyte[] sYBuffer = new sbyte[width * height]; GCHandle hYBuffer = GCHandle.Alloc(sYBuffer, GCHandleType.Pinned); sbyte * yBuffer = (sbyte *)hYBuffer.AddrOfPinnedObject(); // Create maps InterWaveMapEncoder eymap = new InterWaveMapEncoder(width, height); _YMap = eymap; GCHandle hData = GCHandle.Alloc(pm.Data, GCHandleType.Pinned); Pixel * pData = (Pixel *)hData.AddrOfPinnedObject(); // Create chrominance maps if (_CrCbDelay >= 0) { sbyte[] sCbBuffer = new sbyte[width * height]; GCHandle hCbBuffer = GCHandle.Alloc(sCbBuffer, GCHandleType.Pinned); sbyte * cbBuffer = (sbyte *)hCbBuffer.AddrOfPinnedObject(); sbyte[] sCrBuffer = new sbyte[width * height]; GCHandle hCrBuffer = GCHandle.Alloc(sCrBuffer, GCHandleType.Pinned); sbyte * crBuffer = (sbyte *)hCrBuffer.AddrOfPinnedObject(); InterWaveMapEncoder ecbmap = new InterWaveMapEncoder(width, height); _CbMap = ecbmap; InterWaveMapEncoder ecrmap = new InterWaveMapEncoder(width, height); _CrMap = ecrmap; // Color space conversion from RGB to YCbCr and channel separation InterWaveTransform.Rgb2YCbCr(pData, width, height, width * 3, yBuffer, cbBuffer, crBuffer, width); // Create YMap eymap.Create(yBuffer, width, msk8, mskrowsize); // Create CbMap ecbmap.Create(cbBuffer, width, msk8, mskrowsize); // Create CrMap ecrmap.Create(crBuffer, width, msk8, mskrowsize); // Perform chrominance reduction (CrCbHalf) if (_CrCbHalf) { ecbmap.Slashres(2); ecrmap.Slashres(2); } if (hCbBuffer.IsAllocated) { hCbBuffer.Free(); } if (hCrBuffer.IsAllocated) { hCrBuffer.Free(); } } else { // Fill buffer with luminance information InterWaveTransform.Rgb2Y(pData, width, height, pm.GetRowSize(), yBuffer, width); // Create YMAP eymap.Create(yBuffer, width, msk8, mskrowsize); // Inversion for gray images sbyte *e = yBuffer + width * height; for (sbyte *b = yBuffer; b < e; b++) { *b = (sbyte)(255 - *b); } } if (hMask.IsAllocated) { hMask.Free(); } hData.Free(); hYBuffer.Free(); }
private unsafe static Surface ReadNv12(ResourceManager rm, ref SlotSurfaceConfig config, ref PlaneOffsets offsets) { InputSurface input = ReadSurface(rm.Gmm, ref config, ref offsets, 1, 2); int width = input.Width; int height = input.Height; int yStride = GetPitch(width, 1); int uvStride = GetPitch(input.UvWidth, 2); Surface output = new Surface(rm.SurfacePool, width, height); if (Sse41.IsSupported) { Vector128 <byte> shufMask = Vector128.Create( (byte)0, (byte)2, (byte)3, (byte)1, (byte)4, (byte)6, (byte)7, (byte)5, (byte)8, (byte)10, (byte)11, (byte)9, (byte)12, (byte)14, (byte)15, (byte)13); Vector128 <short> alphaMask = Vector128.Create(0xffUL << 48).AsInt16(); int yStrideGap = yStride - width; int uvStrideGap = uvStride - input.UvWidth; int widthTrunc = width & ~0xf; fixed(Pixel *dstPtr = output.Data) { Pixel *op = dstPtr; fixed(byte *src0Ptr = input.Buffer0, src1Ptr = input.Buffer1) { byte *i0p = src0Ptr; for (int y = 0; y < height; y++) { byte *i1p = src1Ptr + (y >> 1) * uvStride; int x = 0; for (; x < widthTrunc; x += 16, i0p += 16, i1p += 16) { Vector128 <short> ya0 = Sse41.ConvertToVector128Int16(i0p); Vector128 <short> ya1 = Sse41.ConvertToVector128Int16(i0p + 8); Vector128 <byte> uv = Sse2.LoadVector128(i1p); Vector128 <short> uv0 = Sse2.UnpackLow(uv.AsInt16(), uv.AsInt16()); Vector128 <short> uv1 = Sse2.UnpackHigh(uv.AsInt16(), uv.AsInt16()); Vector128 <short> rgba0 = Sse2.UnpackLow(ya0, uv0); Vector128 <short> rgba1 = Sse2.UnpackHigh(ya0, uv0); Vector128 <short> rgba2 = Sse2.UnpackLow(ya1, uv1); Vector128 <short> rgba3 = Sse2.UnpackHigh(ya1, uv1); rgba0 = Ssse3.Shuffle(rgba0.AsByte(), shufMask).AsInt16(); rgba1 = Ssse3.Shuffle(rgba1.AsByte(), shufMask).AsInt16(); rgba2 = Ssse3.Shuffle(rgba2.AsByte(), shufMask).AsInt16(); rgba3 = Ssse3.Shuffle(rgba3.AsByte(), shufMask).AsInt16(); Vector128 <short> rgba16_0 = Sse41.ConvertToVector128Int16(rgba0.AsByte()); Vector128 <short> rgba16_1 = Sse41.ConvertToVector128Int16(HighToLow(rgba0.AsByte())); Vector128 <short> rgba16_2 = Sse41.ConvertToVector128Int16(rgba1.AsByte()); Vector128 <short> rgba16_3 = Sse41.ConvertToVector128Int16(HighToLow(rgba1.AsByte())); Vector128 <short> rgba16_4 = Sse41.ConvertToVector128Int16(rgba2.AsByte()); Vector128 <short> rgba16_5 = Sse41.ConvertToVector128Int16(HighToLow(rgba2.AsByte())); Vector128 <short> rgba16_6 = Sse41.ConvertToVector128Int16(rgba3.AsByte()); Vector128 <short> rgba16_7 = Sse41.ConvertToVector128Int16(HighToLow(rgba3.AsByte())); rgba16_0 = Sse2.Or(rgba16_0, alphaMask); rgba16_1 = Sse2.Or(rgba16_1, alphaMask); rgba16_2 = Sse2.Or(rgba16_2, alphaMask); rgba16_3 = Sse2.Or(rgba16_3, alphaMask); rgba16_4 = Sse2.Or(rgba16_4, alphaMask); rgba16_5 = Sse2.Or(rgba16_5, alphaMask); rgba16_6 = Sse2.Or(rgba16_6, alphaMask); rgba16_7 = Sse2.Or(rgba16_7, alphaMask); rgba16_0 = Sse2.ShiftLeftLogical(rgba16_0, 2); rgba16_1 = Sse2.ShiftLeftLogical(rgba16_1, 2); rgba16_2 = Sse2.ShiftLeftLogical(rgba16_2, 2); rgba16_3 = Sse2.ShiftLeftLogical(rgba16_3, 2); rgba16_4 = Sse2.ShiftLeftLogical(rgba16_4, 2); rgba16_5 = Sse2.ShiftLeftLogical(rgba16_5, 2); rgba16_6 = Sse2.ShiftLeftLogical(rgba16_6, 2); rgba16_7 = Sse2.ShiftLeftLogical(rgba16_7, 2); Sse2.Store((short *)(op + (uint)x + 0), rgba16_0); Sse2.Store((short *)(op + (uint)x + 2), rgba16_1); Sse2.Store((short *)(op + (uint)x + 4), rgba16_2); Sse2.Store((short *)(op + (uint)x + 6), rgba16_3); Sse2.Store((short *)(op + (uint)x + 8), rgba16_4); Sse2.Store((short *)(op + (uint)x + 10), rgba16_5); Sse2.Store((short *)(op + (uint)x + 12), rgba16_6); Sse2.Store((short *)(op + (uint)x + 14), rgba16_7); } for (; x < width; x++, i1p += (x & 1) * 2) { Pixel *px = op + (uint)x; px->R = Upsample(*i0p++); px->G = Upsample(*i1p); px->B = Upsample(*(i1p + 1)); px->A = 0x3ff; } op += width; i0p += yStrideGap; i1p += uvStrideGap; } } } } else { for (int y = 0; y < height; y++) { int uvBase = (y >> 1) * uvStride; for (int x = 0; x < width; x++) { output.SetR(x, y, Upsample(input.Buffer0[y * yStride + x])); int uvOffs = uvBase + (x & ~1); output.SetG(x, y, Upsample(input.Buffer1[uvOffs])); output.SetB(x, y, Upsample(input.Buffer1[uvOffs + 1])); output.SetA(x, y, 0x3ff); } } } return(output); }
private unsafe static void WriteA8B8G8R8(ResourceManager rm, Surface input, ref OutputSurfaceConfig config, ref PlaneOffsets offsets) { int width = input.Width; int height = input.Height; int stride = GetPitch(width, 4); int dstIndex = rm.BufferPool.Rent(height * stride, out Span <byte> dst); if (Sse2.IsSupported) { int widthTrunc = width & ~7; int strideGap = stride - width * 4; fixed(Pixel *srcPtr = input.Data) { Pixel *ip = srcPtr; fixed(byte *dstPtr = dst) { byte *op = dstPtr; for (int y = 0; y < height; y++, ip += input.Width) { int x = 0; for (; x < widthTrunc; x += 8) { Vector128 <ushort> pixel12 = Sse2.LoadVector128((ushort *)(ip + (uint)x)); Vector128 <ushort> pixel34 = Sse2.LoadVector128((ushort *)(ip + (uint)x + 2)); Vector128 <ushort> pixel56 = Sse2.LoadVector128((ushort *)(ip + (uint)x + 4)); Vector128 <ushort> pixel78 = Sse2.LoadVector128((ushort *)(ip + (uint)x + 6)); pixel12 = Sse2.ShiftRightLogical(pixel12, 2); pixel34 = Sse2.ShiftRightLogical(pixel34, 2); pixel56 = Sse2.ShiftRightLogical(pixel56, 2); pixel78 = Sse2.ShiftRightLogical(pixel78, 2); Vector128 <byte> pixel1234 = Sse2.PackUnsignedSaturate(pixel12.AsInt16(), pixel34.AsInt16()); Vector128 <byte> pixel5678 = Sse2.PackUnsignedSaturate(pixel56.AsInt16(), pixel78.AsInt16()); Sse2.Store(op + 0x00, pixel1234); Sse2.Store(op + 0x10, pixel5678); op += 0x20; } for (; x < width; x++) { Pixel *px = ip + (uint)x; *(op + 0) = Downsample(px->R); *(op + 1) = Downsample(px->G); *(op + 2) = Downsample(px->B); *(op + 3) = Downsample(px->A); op += 4; } op += strideGap; } } } } else { for (int y = 0; y < height; y++) { int baseOffs = y * stride; for (int x = 0; x < width; x++) { int offs = baseOffs + x * 4; dst[offs + 0] = Downsample(input.GetR(x, y)); dst[offs + 1] = Downsample(input.GetG(x, y)); dst[offs + 2] = Downsample(input.GetB(x, y)); dst[offs + 3] = Downsample(input.GetA(x, y)); } } } bool outLinear = config.OutBlkKind == 0; int gobBlocksInY = 1 << config.OutBlkHeight; WriteBuffer(rm, dst, offsets.LumaOffset, outLinear, width, height, 4, gobBlocksInY); rm.BufferPool.Return(dstIndex); }
private unsafe static void WriteNv12(ResourceManager rm, Surface input, ref OutputSurfaceConfig config, ref PlaneOffsets offsets) { int gobBlocksInY = 1 << config.OutBlkHeight; bool outLinear = config.OutBlkKind == 0; int width = Math.Min(config.OutLumaWidth + 1, input.Width); int height = Math.Min(config.OutLumaHeight + 1, input.Height); int yStride = GetPitch(config.OutLumaWidth + 1, 1); int dstYIndex = rm.BufferPool.Rent((config.OutLumaHeight + 1) * yStride, out Span <byte> dstY); if (Sse41.IsSupported) { Vector128 <ushort> mask = Vector128.Create(0xffffUL).AsUInt16(); int widthTrunc = width & ~0xf; int strideGap = yStride - width; fixed(Pixel *srcPtr = input.Data) { Pixel *ip = srcPtr; fixed(byte *dstPtr = dstY) { byte *op = dstPtr; for (int y = 0; y < height; y++, ip += input.Width) { int x = 0; for (; x < widthTrunc; x += 16) { byte *baseOffset = (byte *)(ip + (ulong)(uint)x); Vector128 <ushort> pixelp1 = Sse2.LoadVector128((ushort *)baseOffset); Vector128 <ushort> pixelp2 = Sse2.LoadVector128((ushort *)(baseOffset + 0x10)); Vector128 <ushort> pixelp3 = Sse2.LoadVector128((ushort *)(baseOffset + 0x20)); Vector128 <ushort> pixelp4 = Sse2.LoadVector128((ushort *)(baseOffset + 0x30)); Vector128 <ushort> pixelp5 = Sse2.LoadVector128((ushort *)(baseOffset + 0x40)); Vector128 <ushort> pixelp6 = Sse2.LoadVector128((ushort *)(baseOffset + 0x50)); Vector128 <ushort> pixelp7 = Sse2.LoadVector128((ushort *)(baseOffset + 0x60)); Vector128 <ushort> pixelp8 = Sse2.LoadVector128((ushort *)(baseOffset + 0x70)); pixelp1 = Sse2.And(pixelp1, mask); pixelp2 = Sse2.And(pixelp2, mask); pixelp3 = Sse2.And(pixelp3, mask); pixelp4 = Sse2.And(pixelp4, mask); pixelp5 = Sse2.And(pixelp5, mask); pixelp6 = Sse2.And(pixelp6, mask); pixelp7 = Sse2.And(pixelp7, mask); pixelp8 = Sse2.And(pixelp8, mask); Vector128 <ushort> pixelq1 = Sse41.PackUnsignedSaturate(pixelp1.AsInt32(), pixelp2.AsInt32()); Vector128 <ushort> pixelq2 = Sse41.PackUnsignedSaturate(pixelp3.AsInt32(), pixelp4.AsInt32()); Vector128 <ushort> pixelq3 = Sse41.PackUnsignedSaturate(pixelp5.AsInt32(), pixelp6.AsInt32()); Vector128 <ushort> pixelq4 = Sse41.PackUnsignedSaturate(pixelp7.AsInt32(), pixelp8.AsInt32()); pixelq1 = Sse41.PackUnsignedSaturate(pixelq1.AsInt32(), pixelq2.AsInt32()); pixelq2 = Sse41.PackUnsignedSaturate(pixelq3.AsInt32(), pixelq4.AsInt32()); pixelq1 = Sse2.ShiftRightLogical(pixelq1, 2); pixelq2 = Sse2.ShiftRightLogical(pixelq2, 2); Vector128 <byte> pixel = Sse2.PackUnsignedSaturate(pixelq1.AsInt16(), pixelq2.AsInt16()); Sse2.Store(op, pixel); op += 0x10; } for (; x < width; x++) { Pixel *px = ip + (uint)x; *op++ = Downsample(px->R); } op += strideGap; } } } } else { for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { dstY[y * yStride + x] = Downsample(input.GetR(x, y)); } } } WriteBuffer( rm, dstY, offsets.LumaOffset, outLinear, config.OutLumaWidth + 1, config.OutLumaHeight + 1, 1, gobBlocksInY); rm.BufferPool.Return(dstYIndex); int uvWidth = Math.Min(config.OutChromaWidth + 1, (width + 1) >> 1); int uvHeight = Math.Min(config.OutChromaHeight + 1, (height + 1) >> 1); int uvStride = GetPitch(config.OutChromaWidth + 1, 2); int dstUvIndex = rm.BufferPool.Rent((config.OutChromaHeight + 1) * uvStride, out Span <byte> dstUv); if (Sse2.IsSupported) { int widthTrunc = uvWidth & ~7; int strideGap = uvStride - uvWidth * 2; fixed(Pixel *srcPtr = input.Data) { Pixel *ip = srcPtr; fixed(byte *dstPtr = dstUv) { byte *op = dstPtr; for (int y = 0; y < uvHeight; y++, ip += input.Width * 2) { int x = 0; for (; x < widthTrunc; x += 8) { byte *baseOffset = (byte *)ip + (ulong)(uint)x * 16; Vector128 <uint> pixel1 = Sse2.LoadScalarVector128((uint *)(baseOffset + 0x02)); Vector128 <uint> pixel2 = Sse2.LoadScalarVector128((uint *)(baseOffset + 0x12)); Vector128 <uint> pixel3 = Sse2.LoadScalarVector128((uint *)(baseOffset + 0x22)); Vector128 <uint> pixel4 = Sse2.LoadScalarVector128((uint *)(baseOffset + 0x32)); Vector128 <uint> pixel5 = Sse2.LoadScalarVector128((uint *)(baseOffset + 0x42)); Vector128 <uint> pixel6 = Sse2.LoadScalarVector128((uint *)(baseOffset + 0x52)); Vector128 <uint> pixel7 = Sse2.LoadScalarVector128((uint *)(baseOffset + 0x62)); Vector128 <uint> pixel8 = Sse2.LoadScalarVector128((uint *)(baseOffset + 0x72)); Vector128 <uint> pixel12 = Sse2.UnpackLow(pixel1, pixel2); Vector128 <uint> pixel34 = Sse2.UnpackLow(pixel3, pixel4); Vector128 <uint> pixel56 = Sse2.UnpackLow(pixel5, pixel6); Vector128 <uint> pixel78 = Sse2.UnpackLow(pixel7, pixel8); Vector128 <ulong> pixel1234 = Sse2.UnpackLow(pixel12.AsUInt64(), pixel34.AsUInt64()); Vector128 <ulong> pixel5678 = Sse2.UnpackLow(pixel56.AsUInt64(), pixel78.AsUInt64()); pixel1234 = Sse2.ShiftRightLogical(pixel1234, 2); pixel5678 = Sse2.ShiftRightLogical(pixel5678, 2); Vector128 <byte> pixel = Sse2.PackUnsignedSaturate(pixel1234.AsInt16(), pixel5678.AsInt16()); Sse2.Store(op, pixel); op += 0x10; } for (; x < uvWidth; x++) { Pixel *px = ip + (uint)(x << 1); *op++ = Downsample(px->G); *op++ = Downsample(px->B); } op += strideGap; } } } } else { for (int y = 0; y < uvHeight; y++) { for (int x = 0; x < uvWidth; x++) { int xx = x << 1; int yy = y << 1; int uvOffs = y * uvStride + xx; dstUv[uvOffs + 0] = Downsample(input.GetG(xx, yy)); dstUv[uvOffs + 1] = Downsample(input.GetB(xx, yy)); } } } WriteBuffer( rm, dstUv, offsets.ChromaUOffset, outLinear, config.OutChromaWidth + 1, config.OutChromaHeight + 1, 2, gobBlocksInY); rm.BufferPool.Return(dstUvIndex); }
public void SetPixel(int x, int y, Pixel colour) { Pixel *pixel = PixelAt(x, y); *pixel = colour; }
public static double GetDrawingFitness(Bitmap newBitmap, Bitmap sourceBitmap) { double error = 0; BitmapData newBits = newBitmap.LockBits( new Rectangle(0, 0, Tools.MaxWidth, Tools.MaxHeight), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); BitmapData sourceBits = sourceBitmap.LockBits( new Rectangle(0, 0, Tools.MaxWidth, Tools.MaxHeight), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); unchecked { unsafe { Pixel *p1 = (Pixel *)newBits.Scan0.ToPointer(); Pixel *p2 = (Pixel *)sourceBits.Scan0.ToPointer(); int numPixels = newBits.Width * newBits.Height; for (int i = 0; i < numPixels; i += 1) { int r = p1->R - p2->R; int g = p1->G - p2->G; int b = p1->B - p2->B; error += r * r + g * g + b * b; p1 += 1; p2 += 1; } int x1 = newBits.Width / 4; int x2 = newBits.Width / 4 * 3; int y1 = newBits.Height / 4; int y2 = newBits.Height / 4 * 3; int heightOfCenter = y2 - y1; int widthOfCenter = x2 - x1; p1 += y1 * newBits.Width; p2 += y1 * newBits.Width; for (int y = y1; y < y2; y++) { p1 = (Pixel *)newBits.Scan0.ToPointer(); p2 = (Pixel *)sourceBits.Scan0.ToPointer(); p1 += y * newBits.Width + x1; p2 += y * newBits.Width + x1; for (int x = x1; x < x2; x++) { int r = p1->R - p2->R; int g = p1->G - p2->G; int b = p1->B - p2->B; error += r * r + g * g + b * b; p1 += 1; p2 += 1; } } } } sourceBitmap.UnlockBits(sourceBits); newBitmap.UnlockBits(newBits); return(error); }
public Color GetPixel(int x, int y) { _currentPixel = (Pixel*) (Pointer + y*_width + x*sizeof (Pixel)); return Color.FromArgb(_currentPixel->Alpha, _currentPixel->Red, _currentPixel->Green, _currentPixel->Blue); }
public Color GetPixel(int x, int y) { _currentPixel = (Pixel *)(Pointer + y * _width + x * sizeof(Pixel)); return(Color.FromArgb(_currentPixel->Alpha, _currentPixel->Red, _currentPixel->Green, _currentPixel->Blue)); }