Help!! I'm sooo close... My 50 images come with funny distortions (some examples below)

enter image description here

enter image description here

My pseudo code is as follows:

// open card file

// if card file NULL, don't continue

// read 512 bytes

// do this until the end of file

  // when 512 bytes started with jpg signature 

       // write 512 bytes to new file
       // if the next 512 bytes is eof, break => which may also be the issue 

       // when the next 512 bytes did not start with jpg signature
            // continue writing to the already opened new file
            // if the next 512 bytes is eof, break => again... hmmm

       // close new files
       // increment the number of new file

// close card file

// return 0

I'm suspecting the issue lies with the way I check the jpg signatures. In the first while loop I use the following code

while(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] >= 0xe0 || buffer[3] <= 0xef))

and on the second while loop I use this code

while(buffer[0] != 0xff && buffer[1] != 0xd8 && buffer[2] != 0xff && (buffer[3] != 0xe0 || buffer[3] != 0xef))

I don't know whether i can put the full version of my current code here without breaching the academic honesty policy thingy. Could someone please advise the best way to do this? Thanks in advance.


Both tests have problems. First, look at this subclause of the first test: (buffer[3] >= 0xe0 || buffer[3] <= 0xef) This clause is always true. I'll let you think about why, but if you can't figure it out, leave a comment and I'll explain.

Next, the second test is also flawed. A complex logic test like this cannot be negated simply by changing each 'test' to 'not test' as was done. It's a couple weeks of a digital logic class to explain in detail, so I won't try here. (It's a very common error for new programmers, so don't feel alone. ) Fortunately, there's a simple solution. Negate the test as a whole, not in parts. Simply demonstrated by example, say you have a complex test like a && b && c && (d || e) To negate this, simply apply NOT to the whole: !( a && b && c && (d || e) )

There may be more issues, but without seeing actual code, it's extremely difficult to diagnose. Happy coding!

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Thanks, Cliff!! You are a life saver! I changed the 2nd while loop to negate the whole as you suggested and it fixed it. Though, when I changed the subclause in the 1st while loop, the compiler spitted one file only instead of 50, so I keep the 1st while loop as it is (it might be something to do with how it's connected to the rest of the codes). Anyway, all 50 images now visually flawless, Yayy!! BUT, I bump into another problem with check 50 not passing two things (recovers middle and last files) regardless. Any idea why? Or maybe I should post it as another question? Thanks again – MTreacy Oct 29 '16 at 22:21
  • Probably time for another question. As for the first condition, the problem is that you have an OR condition where it should be an AND condition. It may be working, but it's still wrong. If the first 3 bytes are correct but the 4th byte is not part of a signature, it would still be recognizes as a signature. – Cliff B Oct 29 '16 at 22:55
  • Again, you've helped me getting through! I've changed the OR to AND conditions, tweaked my code a little then run it through check50. It passes all check points successfully!! You are awesome!! BIG THANKS!! – MTreacy Oct 30 '16 at 0:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .