Bug 30165 - Math.Floor does not always return a correct value
Summary: Math.Floor does not always return a correct value
Status: NEW
Alias: None
Product: Runtime
Classification: Mono
Component: General (show other bugs)
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-05-16 13:00 UTC by manuel
Modified: 2015-05-18 11:59 UTC (History)
2 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description manuel 2015-05-16 13:00:16 UTC
In the following code Math.Floor once in a while (with very low probability) produces an incorrect value ending up with an out of range exception while accessing an array. We did correct this issue by replacing it by a cast to byte.
If first though it was an indirect issue caused by using pointers directly, but it seems it is not. This issue happens in both iOS and Android
 
		public static Color GetMostFrequentColorInRGB(RGBA_Bitmap bitmap, RGBA_Bitmap hsvMask){
			int countBytes = bitmap.Width * bitmap.Height;
			int classes = 20;
			float dx = 256 / (float)classes;

			var histogram = new uint[classes, classes, classes];


			PixelData* pBase = bitmap.PixelAt (0, 0);
			HSV* pBaseMask = (HSV*)hsvMask.PixelAt (0, 0);

			for (int i = 0; i < countBytes; i++) {


				if (pBaseMask->s > 50 && pBaseMask->v > 100) {

					int r = (int)Math.Floor(pBase->red / dx);
					int g = (int)Math.Floor(pBase->green / dx);
					int b = (int)Math.Floor(pBase->blue / dx);

					histogram [r, g, b]++;
				}

				pBase++;
				pBaseMask++;
			}

			GC.KeepAlive(bitmap);  
			GC.KeepAlive(hsvMask);  

			return MostFrequent (histogram, classes);

		}

		private static int MostFrequent(int[] histogram){
			int mostFrequentCount = histogram[0];
			int mostFrequent = 0;

			for (int i = 1; i < histogram.Length; i++) {
				if (mostFrequentCount < histogram [i]) {
					mostFrequentCount = histogram [i];
					mostFrequent = i;
				}
			}

			return mostFrequent;
		}



Solution was

		public static Color GetMostFrequentColorInRGB(RGBA_Bitmap bitmap, RGBA_Bitmap hsvMask){
			int countBytes = bitmap.Width * bitmap.Height;
			int classes = 20;
			float dx = 256 / (float)classes;

			var histogram = new uint[classes, classes, classes];


			PixelData* pBase = bitmap.PixelAt (0, 0);
			HSV* pBaseMask = (HSV*)hsvMask.PixelAt (0, 0);

			for (int i = 0; i < countBytes; i++) {


				if (pBaseMask->s > 50 && pBaseMask->v > 100) {

					int r = (int)((byte)pBase->red / dx);
					int g = (int)((byte)pBase->green / dx);
 					int b = (int)((byte)pBase->blue / dx);

					histogram [r, g, b]++;
				}

				pBase++;
				pBaseMask++;
			}

			GC.KeepAlive(bitmap);  
			GC.KeepAlive(hsvMask);  

			return MostFrequent (histogram, classes);

		}
Comment 1 manuel 2015-05-18 11:59:35 UTC
sorry, what solved was direct cast to int instead of using Floor

Just for reference 

	public struct PixelData
	{
		public byte red;
		public byte green;
		public byte blue;
		public byte a;

	}


	public struct HSV
	{
		public byte h;
		public byte s;
		public byte v;
		public byte a;

	}

and MostFrequent looks like

		private static Color MostFrequent(uint[,,] histogram, int classes){
			int mostFreqR = 0;
			int mostFreqG = 0;
			int mostFreqB = 0;

			uint mostFrequent = 0;


			for (int r = 0; r < classes; r++) 
				for(int g = 0; g<classes; g++)
					for(int b = 0; b<classes; b++){
						if (mostFrequent < histogram [r, g, b]) {
							mostFrequent = histogram [r, g, b];
							mostFreqR = r;
							mostFreqG = g;
							mostFreqB = b;

						}

			}

			int dx = (int)(256 / (double)classes);

			return Color.FromArgb((int)((mostFreqR) * dx), (int)((mostFreqG ) * dx), (int)((mostFreqB) * dx));
		}

Note You need to log in before you can comment on or make changes to this bug.