Page 2 of 2

Re: repairing jpegs

Posted: 2010-03-03T12:05:20-07:00
by fmw42
try searching google for "unix command line jpg reader" and see if you find anything useful

Re: repairing jpegs

Posted: 2010-03-03T12:30:42-07:00
by snibgo
Have you looked at the first 4 (or whatever) magic bytes? Maybe that's all that is wrong. If so, a simple C program could fix it.

Re: repairing jpegs

Posted: 2010-03-03T15:05:46-07:00
by toshog
fmw42 wrote:try searching google for "unix command line jpg reader" and see if you find anything useful
i did. i tried about 3 - 4. nothing really useful. my guess is that all the open source programs that use libjpeg (and probably most of them, if not all of them, do) will have the same behavior. so unless some of those are using some other/proprietary libraries - like adobe and that converter do - then it doesn't matter much which one you use...

Re: repairing jpegs

Posted: 2010-03-03T15:09:21-07:00
by toshog
snibgo wrote:Have you looked at the first 4 (or whatever) magic bytes? Maybe that's all that is wrong. If so, a simple C program could fix it.
that is my next option. will eventually look into that. c is not the problem. the problem is i do not know much about jpeg headers...

Re: repairing jpegs

Posted: 2010-03-04T02:06:28-07:00
by snibgo
By comparing imgfoo.jpg, a valid jpg file, the JPEG spec at http://www.w3.org/Graphics/JPEG/jfif3.pdf, and /usr/share/ImageMagick-6.4.5/config/magic.xml which contains the line:
<magic name="JPEG" offset="0" target="\377\330\377"/>
it seems imgfoo contains 21 spurious bytes at the start. These can be stripped by a simple C program (or perhaps some shell tool):

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

int main (int argc, char *argv [])
{
  // Strip the first 21 bytes

  FILE * fhin = fopen ("imgfoo.jpg", "rb");
  FILE * fhout = fopen ("imgfoofixed.jpg", "wb");

  char c;
  int i;
  for (i = 0; i < 21; i++) {
    fread (&c, 1, 1, fhin);
  }
  while (fread (&c, 1, 1, fhin)) {
    fwrite (&c, 1, 1, fhout);
  }
  fclose (fhout);
  fclose (fhin);

  return (0);
}
imgfoofixed.jpg is now readable by IM and Gimp.

Re: repairing jpegs

Posted: 2010-03-04T16:56:57-07:00
by toshog
snibgo wrote:imgfoofixed.jpg is now readable by IM and Gimp.
snibgo... i love you man. thanks.... i had a similar script. i just didn't know how many chars to jump... thanks. appreciated.

Re: repairing jpegs

Posted: 2010-03-04T17:00:37-07:00
by toshog
snibgo wrote: it seems imgfoo contains 21 spurious bytes at the start. These can be stripped by a simple C program (or perhaps some shell tool).
one question. how did you know that they are 21? can you explain please...

Re: repairing jpegs

Posted: 2010-03-04T17:47:12-07:00
by snibgo
I dumped them using a filter called DumpBin that converts nonprintable characters to hex. Then I counted the characters until I reached FF D8 FF, which is hex for the octal "\377\330\377", which IM expects to be at the start.

You might find other files have a different number of spurious bytes. If so, you could write a program or script to do the counting each time.

Re: repairing jpegs

Posted: 2010-03-04T17:52:38-07:00
by toshog
i see. understood. again, thank you for the help....