static int PVRTDecompressPVRTC(byte[] pCompressedData, int Do2bitMode, int XDim, int YDim, int IsPVRII, ref byte[] pResultImage) { // TODO: actually do something w/ the IsPVRII flag... Pixel32[] pDecompressedData; int XTrueDim = Math.Max(XDim, ((Do2bitMode == 1) ? 16 : 8)); int YTrueDim = Math.Max(YDim, 8); if (XTrueDim != XDim || YTrueDim != YDim) { pDecompressedData = new Pixel32[XTrueDim * YTrueDim]; } else { pDecompressedData = new Pixel32[XDim * YDim]; } int retval = pvrtcDecompress(pCompressedData, ref pDecompressedData, (uint)XTrueDim, (uint)YTrueDim, (byte)(Do2bitMode == 1 ? 2 : 4)); for (int x = 0; x < XDim; ++x) { for (int y = 0; y < YDim; ++y) { pResultImage[(x + y * XDim) * 4 + 2] = pDecompressedData[x + y * XTrueDim].red; pResultImage[(x + y * XDim) * 4 + 1] = pDecompressedData[x + y * XTrueDim].green; pResultImage[(x + y * XDim) * 4 + 0] = pDecompressedData[x + y * XTrueDim].blue; pResultImage[(x + y * XDim) * 4 + 3] = pDecompressedData[x + y * XTrueDim].alpha; } } return(retval); }
public void SetPixel(Pixel32 pixel) { Pixel = pixel; PixelCMYK = new CMYK(pixel); PixelHSV = new HSV(pixel); UpdatePixelInfo(); }
public void CreateFromGrayscale() { byte value = 128; var pixel = new Pixel32(value); var color = Color.FromArgb(value, value, value); Assert.AreEqual(color.ToArgb(), BytesUtils.GetDataFromArgb(pixel.A, pixel.R, pixel.G, pixel.B)); }
public void FromHexString() { var pixel = Pixel32.FromHex("FF78715D"); Assert.AreEqual(255, pixel.A); Assert.AreEqual(120, pixel.R); Assert.AreEqual(113, pixel.G); Assert.AreEqual(93, pixel.B); }
public void CreateNewFromRGB() { byte r = 123; byte g = 98; byte b = 40; var pixel = new Pixel32(r, g, b); var color = Color.FromArgb(r, g, b); Assert.AreEqual(color.ToArgb(), BytesUtils.GetDataFromArgb(pixel.A, pixel.R, pixel.G, pixel.B)); }
public void UpdateColor(int x, int y, Pixel32 pixel) { if (view == null) { return; } view.SetCoordinates(x, y); view.SetPixel(pixel); }
public int Blur(int sprite, int radius) { IntPtr src = this.engine.GetSpriteGraphic(sprite); int srcWidth, srcHeight; int coldepth; this.engine.GetBitmapDimensions(src, out srcWidth, out srcHeight, out coldepth); IntPtr srccharbuffer = this.engine.GetRawBitmapSurface(src); var srcintbuffer = (int**)srccharbuffer; int negrad = -1 * radius; //use a 1Dimensional array since the array is on the free store, not the stack var pixels = new Pixel32[(srcWidth + (radius * 2)) * (srcHeight + (radius * 2))]; // this defines a copy of the individual channels in class form. var dest = new Pixel32[(srcWidth + (radius * 2)) * (srcHeight + (radius * 2))]; // this is the destination sprite. both have a border all the way round equal to the radius for the blurring. var temp = new Pixel32[(srcWidth + (radius * 2)) * (srcHeight + (radius * 2))]; int arraywidth = srcWidth + (radius * 2); //define the array width since its used many times in the algorithm for (int y = 0; y < srcHeight; y++) { //copy the sprite to the Pixels class array for (int x = 0; x < srcWidth; x++) { int locale = XYToLocale(x + radius, y + radius, arraywidth); pixels[locale] = new Pixel32 { Red = GetR32(srcintbuffer[y][x]), Green = GetG32(srcintbuffer[y][x]), Blue = GetB32(srcintbuffer[y][x]), Alpha = GetA32(srcintbuffer[y][x]) }; } } int numofpixels = (radius * 2 + 1); for (int y = 0; y < srcHeight; y++) { int totalr = 0; int totalg = 0; int totalb = 0; int totala = 0; // Process entire window for first pixel for (int kx = negrad; kx <= radius; kx++) { int locale = XYToLocale(kx + radius, y + radius, arraywidth); totala += pixels[locale].Alpha; totalr += (pixels[locale].Red * pixels[locale].Alpha) / 255; totalg += (pixels[locale].Green * pixels[locale].Alpha) / 255; totalb += (pixels[locale].Blue * pixels[locale].Alpha) / 255; } { int locale = XYToLocale(radius, y + radius, arraywidth); temp[locale] = new Pixel32 { Red = totalr / numofpixels, Green = totalg / numofpixels, Blue = totalb / numofpixels, Alpha = totala / numofpixels }; } // Subsequent pixels just update window total for (int x = 1; x < srcWidth; x++) { // Subtract pixel leaving window int locale = XYToLocale(x - 1, y + radius, arraywidth); totala -= pixels[locale].Alpha; totalr -= (pixels[locale].Red * pixels[locale].Alpha) / 255; totalg -= (pixels[locale].Green * pixels[locale].Alpha) / 255; totalb -= (pixels[locale].Blue * pixels[locale].Alpha) / 255; // Add pixel entering window locale = XYToLocale(x + radius + radius, y + radius, arraywidth); totala += pixels[locale].Alpha; totalr += (pixels[locale].Red * pixels[locale].Alpha) / 255; totalg += (pixels[locale].Green * pixels[locale].Alpha) / 255; totalb += (pixels[locale].Blue * pixels[locale].Alpha) / 255; locale = XYToLocale(x + radius, y + radius, arraywidth); temp[locale] = new Pixel32 { Red = totalr / numofpixels, Green = totalg / numofpixels, Blue = totalb / numofpixels, Alpha = totala / numofpixels }; } } for (int x = 0; x < srcWidth; x++) { int totalr = 0; int totalg = 0; int totalb = 0; int totala = 0; // Process entire window for first pixel for (int ky = negrad; ky <= radius; ky++) { int locale = XYToLocale(x + radius, ky + radius, arraywidth); totala += temp[locale].Alpha; totalr += (temp[locale].Red * temp[locale].Alpha) / 255; totalg += (temp[locale].Green * temp[locale].Alpha) / 255; totalb += (temp[locale].Blue * temp[locale].Alpha) / 255; } { int locale = XYToLocale(x + radius, radius, arraywidth); dest[locale] = new Pixel32 { Red = totalr / numofpixels, Green = totalg / numofpixels, Blue = totalb / numofpixels, Alpha = totala / numofpixels }; } // Subsequent pixels just update window total for (int y = 1; y < srcHeight; y++) { // Subtract pixel leaving window int locale = XYToLocale(x + radius, y - 1, arraywidth); totala -= temp[locale].Alpha; totalr -= (temp[locale].Red * temp[locale].Alpha) / 255; totalg -= (temp[locale].Green * temp[locale].Alpha) / 255; totalb -= (temp[locale].Blue * temp[locale].Alpha) / 255; // Add pixel entering window locale = XYToLocale(x + radius, y + radius + radius, arraywidth); totala += temp[locale].Alpha; totalr += (temp[locale].Red * temp[locale].Alpha) / 255; totalg += (temp[locale].Green * temp[locale].Alpha) / 255; totalb += (temp[locale].Blue * temp[locale].Alpha) / 255; locale = XYToLocale(x + radius, y + radius, arraywidth); dest[locale] = new Pixel32 { Red = totalr / numofpixels, Green = totalg / numofpixels, Blue = totalb / numofpixels, Alpha = totala / numofpixels }; } } for (int y = 0; y < srcHeight; y++) { for (int x = 0; x < srcWidth; x++) { int locale = XYToLocale(x + radius, y + radius, arraywidth); srcintbuffer[y][x] = dest[locale].GetColorAsInt(); //write the destination array to the main buffer } } this.engine.ReleaseBitmapSurface(src); return 0; }
static int pvrtcDecompress(byte[] pCompressedData, ref Pixel32[] pDecompressedData, uint ui32Width, uint ui32Height, byte ui8Bpp) { uint ui32WordWidth = 4; uint ui32WordHeight = 4; if (ui8Bpp == 2) { ui32WordWidth = 8; } uint[] pWordMembers = new uint[pCompressedData.Length / 4]; for (int i = 0; i < pCompressedData.Length; i += 4) { pWordMembers[i / 4] = BitConverter.ToUInt32(pCompressedData, i); } int i32NumXWords = (int)(ui32Width / ui32WordWidth); int i32NumYWords = (int)(ui32Height / ui32WordHeight); PVRTCWordIndices indices; Pixel32[] pPixels = new Pixel32[ui32WordWidth * ui32WordHeight]; for (int wordY = -1; wordY < i32NumYWords - 1; wordY++) { for (int wordX = -1; wordX < i32NumXWords - 1; wordX++) { indices = new PVRTCWordIndices( (int)wrapWordIndex((uint)i32NumXWords, wordX), (int)wrapWordIndex((uint)i32NumYWords, wordY), (int)wrapWordIndex((uint)i32NumXWords, wordX + 1), (int)wrapWordIndex((uint)i32NumYWords, wordY), (int)wrapWordIndex((uint)i32NumXWords, wordX), (int)wrapWordIndex((uint)i32NumYWords, wordY + 1), (int)wrapWordIndex((uint)i32NumXWords, wordX + 1), (int)wrapWordIndex((uint)i32NumYWords, wordY + 1)); uint[] WordOffsets = new uint[4] { TwiddleUV((uint)i32NumXWords, (uint)i32NumYWords, (uint)indices.P[0], (uint)indices.P[1]) * 2, TwiddleUV((uint)i32NumXWords, (uint)i32NumYWords, (uint)indices.Q[0], (uint)indices.Q[1]) * 2, TwiddleUV((uint)i32NumXWords, (uint)i32NumYWords, (uint)indices.R[0], (uint)indices.R[1]) * 2, TwiddleUV((uint)i32NumXWords, (uint)i32NumYWords, (uint)indices.S[0], (uint)indices.S[1]) * 2, }; PVRTCWord P, Q, R, S; P.u32ColorData = pWordMembers[WordOffsets[0] + 1]; P.u32ModulationData = pWordMembers[WordOffsets[0]]; Q.u32ColorData = pWordMembers[WordOffsets[1] + 1]; Q.u32ModulationData = pWordMembers[WordOffsets[1]]; R.u32ColorData = pWordMembers[WordOffsets[2] + 1]; R.u32ModulationData = pWordMembers[WordOffsets[2]]; S.u32ColorData = pWordMembers[WordOffsets[3] + 1]; S.u32ModulationData = pWordMembers[WordOffsets[3]]; pvrtcGetDecompressedPixels(P, Q, R, S, ref pPixels, ui8Bpp); mapDecompressedData(ref pDecompressedData, (int)ui32Width, pPixels, indices, ui8Bpp); } } return((int)(ui32Width * ui32Height / (uint)(ui32WordWidth / 2))); }
static void interpolateColors(Pixel32 P, Pixel32 Q, Pixel32 R, Pixel32 S, ref Pixel128S[] pPixel, byte ui8Bpp) { uint ui32WordWidth = 4; uint ui32WordHeight = 4; if (ui8Bpp == 2) { ui32WordWidth = 8; } Pixel128S hP = new Pixel128S() { red = (int)P.red, green = (int)P.green, blue = (int)P.blue, alpha = (int)P.alpha }; Pixel128S hQ = new Pixel128S() { red = (int)Q.red, green = (int)Q.green, blue = (int)Q.blue, alpha = (int)Q.alpha }; Pixel128S hR = new Pixel128S() { red = (int)R.red, green = (int)R.green, blue = (int)R.blue, alpha = (int)R.alpha }; Pixel128S hS = new Pixel128S() { red = (int)S.red, green = (int)S.green, blue = (int)S.blue, alpha = (int)S.alpha }; Pixel128S QminusP = new Pixel128S() { red = hQ.red - hP.red, green = hQ.green - hP.green, blue = hQ.blue - hP.blue, alpha = hQ.alpha - hP.alpha }; Pixel128S SminusR = new Pixel128S() { red = hS.red - hR.red, green = hS.green - hR.green, blue = hS.blue - hR.blue, alpha = hS.alpha - hR.alpha }; hP.red *= (int)ui32WordWidth; hP.green *= (int)ui32WordWidth; hP.blue *= (int)ui32WordWidth; hP.alpha *= (int)ui32WordWidth; hR.red *= (int)ui32WordWidth; hR.green *= (int)ui32WordWidth; hR.blue *= (int)ui32WordWidth; hR.alpha *= (int)ui32WordWidth; if (ui8Bpp == 2) { for (uint x = 0; x < ui32WordWidth; x++) { Pixel128S result = new Pixel128S() { red = 4 * hP.red, green = 4 * hP.green, blue = 4 * hP.blue, alpha = 4 * hP.alpha }; Pixel128S dY = new Pixel128S() { red = hR.red - hP.red, green = hR.green - hP.green, blue = hR.blue - hP.blue, alpha = hR.alpha - hP.alpha }; for (uint y = 0; y < ui32WordHeight; y++) { pPixel[y * ui32WordWidth + x].red = (int)((result.red >> 7) + (result.red >> 2)); pPixel[y * ui32WordWidth + x].green = (int)((result.green >> 7) + (result.green >> 2)); pPixel[y * ui32WordWidth + x].blue = (int)((result.blue >> 7) + (result.blue >> 2)); pPixel[y * ui32WordWidth + x].alpha = (int)((result.alpha >> 5) + (result.alpha >> 1)); result.red += dY.red; result.green += dY.green; result.blue += dY.blue; result.alpha += dY.alpha; } hP.red += QminusP.red; hP.green += QminusP.green; hP.blue += QminusP.blue; hP.alpha += QminusP.alpha; hR.red += SminusR.red; hR.green += SminusR.green; hR.blue += SminusR.blue; hR.alpha += SminusR.alpha; } } else { for (uint y = 0; y < ui32WordHeight; y++) { Pixel128S result = new Pixel128S() { red = 4 * hP.red, green = 4 * hP.green, blue = 4 * hP.blue, alpha = 4 * hP.alpha }; Pixel128S dY = new Pixel128S() { red = hR.red - hP.red, green = hR.green - hP.green, blue = hR.blue - hP.blue, alpha = hR.alpha - hP.alpha }; for (uint x = 0; x < ui32WordWidth; x++) { pPixel[y * ui32WordWidth + x].red = (int)((result.red >> 6) + (result.red >> 1)); pPixel[y * ui32WordWidth + x].green = (int)((result.green >> 6) + (result.green >> 1)); pPixel[y * ui32WordWidth + x].blue = (int)((result.blue >> 6) + (result.blue >> 1)); pPixel[y * ui32WordWidth + x].alpha = (int)((result.alpha >> 4) + (result.alpha)); result.red += dY.red; result.green += dY.green; result.blue += dY.blue; result.alpha += dY.alpha; } hP.red += QminusP.red; hP.green += QminusP.green; hP.blue += QminusP.blue; hP.alpha += QminusP.alpha; hR.red += SminusR.red; hR.green += SminusR.green; hR.blue += SminusR.blue; hR.alpha += SminusR.alpha; } } }
public void ToHexString() { var pixel = new Pixel32(255, 130, 209, 255); Assert.AreEqual("FF82D1FF", pixel.ToHex()); }
public void Pixel32Initialization() { BytesUtils.GetArgbFromData(startColor, out byte a, out byte r, out byte g, out byte b); var pixel32 = new Pixel32(a, r, g, b); }