What is the reason the Image and Bitmap classes do not implement custom / hashcode equality logic?

From the MSDN documentation, it seems that both GetHashCode () and Equals () were not overridden in Bitmap. They are not exaggerated in the Image. Thus, both classes use the Object version for them, just compare the links. I was not too convinced, so I decided to run Reflector to test this. It seems MSDN is correct in this matter.

So, is there any special reason why guys from MS will not implement the โ€œcomparison logic", at least for the Bitmap class? I think this is acceptable for Image, as it is an abstract class, but not just for the Bitmap class. I can see in many situations, computing a hash code can be an expensive operation, but it would be nice if it used some kind of caching.

If you want to compare 2 bitmaps, will I have to resort to performing the entire image by comparing each of its pixels?

thanks

+4
source share
4 answers

Let me drop this question; is there any special reason why they will implement such a thing?

On the one hand, it would be very, very expensive to calculate a hash, making all this useless for hash tables and the like. Just try to imagine how this is done on an integer number of 1920x1200 bitmaps; By doing this even once for each bitmap, it will slow down the scanning program. I would expect that 9 times out of 10, when someone needs to compare two raster images, they want link equality, not pixel pixel equality.

And what you are talking about in your question is not a lazy assessment, it's caching. Caching is a non-trivial function to implement, and each function starts at minus 100 points .

Given all this, my answer to this will be that methods are not overridden, because overridden versions will not be particularly useful for many people compared to the cost of implementing and maintaining such a function. If you really need pixel comparisons (or checksums or similar things), you can always implement them yourself in 10 lines or so.

+2
source

Images and bitmaps are usually mutable reference types. Variable reference types should never override Equals(Object) , since Equals(Object) should not indicate whether two objects have the same state at the time of its call, but whether two objects will always and forever be equivalent. Even if two mutable objects have the same state at the same time, this in no way means that they will always do this.

+1
source

The semantics of equality in the .net structure are pretty well standardized; It would seem to violate the principle of least surprise if links to bitmaps are checked for equality in the form in which you expected.

0
source

When a built-in comparison of raster images, which is not implemented by MS, is performed, it is necessary to resort to a comparison of each of the pixels. Of course, MS is likely to do raw memory comparisons (possibly in an insecure block), but you can do the same if you want. Despite this, comparing two bitmaps will always be expensive. And you canโ€™t rely on hashes just because calculating a hash will usually be as expensive as comparing ... and you have to rely on raw pixel comparisons to check for equality when the hashes match, because the hashes only prove the inequality.

I did not present a demonstration that Equals and GetHashCode useless in Bitmap . However, I presented a demonstration that they are often going to offer unexpected and unexpected costs to users and will not be a good solution in normal use cases. Adding extra features to this type is expensive, and I'm not sure if this will be of great benefit. And it was often expensive. This is what you need to implement, or use a third-party library.

0
source

Source: https://habr.com/ru/post/1308711/


All Articles