Like Tree1Likes

Algorithm that compares two images

This is a discussion on Algorithm that compares two images within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by oogabooga You only need to do the seek ONCE, before the double loop, to the position that ...

  1. #16
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Quote Originally Posted by oogabooga View Post
    You only need to do the seek ONCE, before the double loop, to the position that the pixel data begins. After that, each time you do a read, the file pointer is automatically incremented.
    That is fine until you get a bitmap where the bitmap has padding at the end of the line.

    E.g. a B&W bitmap 14 pixels wide has a 2-byte width, with two columns of unused pixels.
    Or for higher bit-depth examples: BMP file format - Wikipedia, the free encyclopedia

    But there are hundreds of little gotchas like that with the BMP format, which is why I recommend something like the DIB24 class I posted a link to earlier, assuming this is for Windows only.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  2. #17
    Registered User
    Join Date
    Apr 2008
    Posts
    190
    oogabooga wrote:
    dFile.get(byte); n = (n << 8) | (byte & 0xff);
    //cout << setw(3) << (byte & 0xff) << ' ';
    dFile.get(byte); n = (n << 8) | (byte & 0xff);
    I assume the second of these should be (n << 16) rather than (n << 8)?

    Yeah Once I get this, I will probably move to your class your posted iMalc, I just wanna get it first!

    Also, what does % mean in this case. I have seen this around but its a really hard thing to google when you dont know what its called...i.e
    Code:
    for (int i = 0; i < 4 - dInfo.imageWidth % 4; i++)
    Last edited by a.mlw.walker; 10-08-2012 at 02:21 AM.

  3. #18
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by iMalc View Post
    That is fine until you get a bitmap where the bitmap has padding at the end of the line.
    It handles the padding at the end of each line. I've mentioned the padding in 3 or 4 posts!
    iMalc likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #19
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by a.mlw.walker View Post
    I assume the second of these should be (n << 16) rather than (n << 8)?
    Don't make assumptions. It should be exactly as I wrote it. It works perfectly. Did you even try it?

    Also, what does % mean in this case. I have seen this around but its a really hard thing to google when you dont know what its called...i.e
    Code:
    for (int i = 0; i < 4 - dInfo.imageWidth % 4; i++)
    Look up the "modulus operator".
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #20
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Quote Originally Posted by oogabooga View Post
    It handles the padding at the end of each line. I've mentioned the padding in 3 or 4 posts!
    Oh whoops, so you did, my mistake! Sorry about that.
    I guess that's just an unusual way to do it.

    Hmm, in fact it looks wrong. This loop will read an extra 1 to 4 characters, but it should be possible to read no extra characters when the thing is aligned perfectly:
    Code:
            // Skip padding bytes at the end of each row.
            for (int i = 0; i < 4 - dInfo.imageWidth % 4; i++)
                dFile.get(byte);
    Something like (4 - dInfo.imageWidth % 4) % 4 would do it correctly.

    Of course this all still shows that it's best finding a library to deal with it.
    Last edited by iMalc; 10-08-2012 at 12:31 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #21
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by iMalc View Post
    Hmm, in fact it looks wrong. This loop will read an extra 1 to 4 characters, but it should be possible to read no extra characters when the thing is aligned perfectly
    You're right, it's wrong. Sigh.

    Quote Originally Posted by iMalc View Post
    Of course this all still shows that it's best finding a library to deal with it.
    I agree. In all their generality bmp's are fairly complicated. I was assuming a 24-bit bmp (without compression, of course).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #22
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Instead of attempting to remove the padding from the rows of pixels, why not just load the data as-is? You just have to remember that the stride of the image is not necessarily equal to 3*width, but could be more than that -- just keep the stride in a variable. It's not a terribly big deal to manage that.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-14-2009, 06:42 AM
  2. compares 2 arguments and replaces characters
    By help123 in forum C Programming
    Replies: 9
    Last Post: 04-14-2005, 09:22 AM
  3. Images
    By face_master in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2002, 12:03 AM
  4. Compares C++ and Pascal
    By Jaguar in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-19-2002, 11:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21