/// <summary> /// Factory-method to generate a ColorCutQuantizer from a PixelBuffer object. /// </summary> public static ColorCutQuantizer FromBitmap(PixelBuffer pixelBuffer, Rectangle region, int maxColors) { int width; int height; int[] pixels; int i, j, index = 0; if (region == null) { width = (int)pixelBuffer.GetWidth(); height = (int)pixelBuffer.GetHeight(); i = 0; j = 0; } else { width = region.Width; height = region.Height; i = region.X; j = region.Y; } Tizen.Log.Info("Palette", "Get pixels rawdata from (" + i + " " + j + " " + width + " " + height + ")" + "\n"); pixels = new int[width * height]; PixelFormat format = pixelBuffer.GetPixelFormat(); int pixelLength = (int)ColorUtils.GetBytesPerPixel(format); IntPtr bufferIntPtr = pixelBuffer.GetBuffer(); unsafe { byte *rawdata = (byte *)bufferIntPtr.ToPointer(); int totalLength = width * height * pixelLength; for (i = 0; i < totalLength; i += pixelLength) { //RGB888 if (pixelLength == 3) { pixels[index++] = (255 & 0xff) << 24 | (rawdata[i] & 0xff) << 16 | (rawdata[i + 1] & 0xff) << 8 | (rawdata[i + 2] & 0xff); } //RGBA8888 else { pixels[index++] = (rawdata[i + 3] & 0xff) << 24 | (rawdata[i] & 0xff) << 16 | (rawdata[i + 1] & 0xff) << 8 | (rawdata[i + 2] & 0xff); } } } return(new ColorCutQuantizer(new ColorHistogram(pixels), maxColors)); }