I am trying to upload a JPEG file and remove all black and white pixels from the image
C # code:
... m_SrcImage = new Bitmap(imagePath); Rectangle r = new Rectangle(0, 0, m_SrcImage.Width, m_SrcImage.Height); BitmapData bd = m_SrcImage.LockBits(r, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); //Load Colors int[] colours = new int[m_SrcImage.Width * m_SrcImage.Height]; Marshal.Copy(bd.Scan0, colours, 0, colours.Length); m_SrcImage.UnlockBits(bd); int len = colours.Length; List<Color> result = new List<Color>(len); for (int i = 0; i < len; ++i) { uint w = ((uint)colours[i]) & 0x00FFFFFF; //Delete alpha-channel if (w != 0x00000000 && w != 0x00FFFFFF) //Check pixel is not black or white { w |= 0xFF000000; //Return alpha channel result.Add(Color.FromArgb((int)w)); } } ...
After that I try to find unique colors in the list with this code
result.Sort((a, b) => { return aR != bR ? aR - bR : aG != bG ? aG - bG : aB != bB ? aB - bB : 0; }); List<Color> uniqueColors = new List<Color>( result.Count); Color rgbTemp = result[0]; for (int i = 0; i < len; ++i) { if (rgbTemp == result[i]) { continue; } uniqueColors.Add(rgbTemp); rgbTemp = result[i]; } uniqueColors.Add(rgbTemp);
And this code produces different results on different machines on the same image!
For example, on this image it produces:
- 43198 unique colors on XP SP3 with .NET version 4
- 43,168 unique colors on Win7 Ultimate with .NEt version 4.5
You can download the minimum test project here . It simply opens the selected image and creates a txt file with unique colors.
Another fact. Some pixels are read differently on different machines. I am comparing txt files with notepad ++ and it shows that some pixels have different RGB components. The difference is 1 for each component, for example.
- Win7 pixel: 255,200,100
- WinXP pixel: 254 199 99
I read this post
stackoverflow.com/questions/2419598/why-might-different-computers-calculate-different-arithmetic-results-in-vb-net
(sorry, I don't have enough ratings for a normal link).
... but there was no information on how to fix it.
The project was compiled for the .NET 4 Client profile on a computer running Windows 7 in the VS 2015 Commumity Edition.