repairing jpegs

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: repairing jpegs

Post by fmw42 »

try searching google for "unix command line jpg reader" and see if you find anything useful
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: repairing jpegs

Post 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.
snibgo's IM pages: im.snibgo.com
toshog
Posts: 23
Joined: 2010-02-12T19:29:43-07:00
Authentication code: 8675308

Re: repairing jpegs

Post 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...
toshog
Posts: 23
Joined: 2010-02-12T19:29:43-07:00
Authentication code: 8675308

Re: repairing jpegs

Post 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...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: repairing jpegs

Post 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.
snibgo's IM pages: im.snibgo.com
toshog
Posts: 23
Joined: 2010-02-12T19:29:43-07:00
Authentication code: 8675308

Re: repairing jpegs

Post 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.
toshog
Posts: 23
Joined: 2010-02-12T19:29:43-07:00
Authentication code: 8675308

Re: repairing jpegs

Post 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...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: repairing jpegs

Post 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.
snibgo's IM pages: im.snibgo.com
toshog
Posts: 23
Joined: 2010-02-12T19:29:43-07:00
Authentication code: 8675308

Re: repairing jpegs

Post by toshog »

i see. understood. again, thank you for the help....
Post Reply