home *** CD-ROM | disk | FTP | other *** search
/ The Best of Mecomp Multimedia 2 / MECOMP-CD-II.iso / amiga / grafix / stripjpeg / stripjpeg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-07  |  3.1 KB  |  156 lines

  1. /*
  2. **      $VER: StripJPEG.c 1.10 (7.5.98)
  3. **
  4. **      Strips proprietary headers from JFIF-JPEG files
  5. **
  6. **      (C) Copyright 1998 Andreas R. Kleinert
  7. **      Freeware. All Rights Reserved.
  8. */
  9.  
  10. #define __USE_SYSBASE
  11.  
  12. #include <exec/types.h>
  13. #include <exec/memory.h>
  14.  
  15. #include <proto/exec.h>
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20.  
  21.  
  22. #ifndef N
  23. #define N (NULL)
  24. #endif /* N */
  25.  
  26.  
  27. char ver_text [] = "\0$VER: StripJPEG 1.10 (7.5.98)";
  28.  
  29.  
  30. /* *************************************************** */
  31. /* *                                                 * */
  32. /* * Additional Base Declarations                    * */
  33. /* *                                                 * */
  34. /* *************************************************** */
  35.  
  36. extern struct ExecBase *SysBase;
  37.  
  38.  
  39. /* *************************************************** */
  40. /* *                                                 * */
  41. /* * MAIN                                            * */
  42. /* *                                                 * */
  43. /* *************************************************** */
  44.  
  45. void __regargs __chkabort(void) { }
  46. void __regargs _CXBRK(void)     { }
  47.  
  48.  
  49. void main(long argc, char **argv)
  50. {
  51.  ULONG error = 0;
  52.  
  53.  if(argc == 3)
  54.   {
  55.    FILE *in, *out;
  56.  
  57.    in = fopen(argv[1], "rb");
  58.    if(in)
  59.     {
  60.      ULONG i, found = FALSE;
  61.      char *inbuf;
  62.      UBYTE chk[256];
  63.  
  64.      inbuf = malloc(16384);
  65.      if(inbuf) setbuf(in, inbuf);
  66.  
  67.      fread(&chk[0], 256, 1, in);
  68.  
  69.      for(i=0; i<248; i++)
  70.       {
  71.        if(!strncmp(&chk[i], "JPEG8BIM", 8))
  72.         {
  73.          found = TRUE;
  74.          break;
  75.         }
  76.       }
  77.  
  78.      if(found)
  79.       {
  80.        found = FALSE;
  81.  
  82.        if(chk[32] == 'M')
  83.         if(chk[33] == 'A')
  84.          if(chk[34] == 'C')
  85.           if(chk[35] == '_')
  86.            if(chk[36] == '3')
  87.             {
  88.              fread(&chk[0], 256, 1, in);
  89.              fread(&chk[0], 256, 1, in);
  90.  
  91.              i = 0;
  92.             }
  93.  
  94.        for( ; i<252; i++)
  95.         {
  96.          if(chk[i] == 0xFF)
  97.            if(chk[i+1] == 0xD8)
  98.              if(chk[i+2] == 0xFF)
  99.                if(chk[i+3] == 0xE0) { found = TRUE; break; }
  100.         }
  101.  
  102.        if(found)
  103.         {
  104.          out = fopen(argv[2], "wb");
  105.          if(out)
  106.           {
  107.            int c;
  108.            char *outbuf;
  109.  
  110.            outbuf = malloc(16384);
  111.            if(outbuf) setbuf(out, outbuf);
  112.  
  113.            fwrite(&chk[i], 256-i, 1, out);
  114.  
  115.            while( (c = fgetc(in)) != EOF) fputc(c, out);
  116.  
  117.            fclose(out);
  118.  
  119.            if(outbuf) free(outbuf);
  120.  
  121.           }else
  122.           {
  123.            printf("\n ERROR: Can't open outfile\n");
  124.            error = 20;
  125.           }
  126.         }else
  127.         {
  128.          printf("\n ERROR: Unsupported kind of proprietary JPEG header\n");
  129.          error = 20;
  130.         }
  131.       }else
  132.       {
  133.        printf("\n ERROR: Is not a JPEG with proprietary header\n");
  134.        error = 20;
  135.       }
  136.  
  137.      fclose(in);
  138.  
  139.      if(inbuf) free(inbuf);
  140.  
  141.     }else
  142.     {
  143.      printf("\n ERROR: Can't open infile\n");
  144.      error = 20;
  145.     }
  146.  
  147.   }else
  148.   {
  149.    printf("\n Strips proprietary headers from JFIF-JPEG files.");
  150.    printf("\n Written 1998 by Andreas R. Kleinert <Andreas_Kleinert@t-online.de>");
  151.    printf("\n Usage: INFILE OUTFILE\n\n");
  152.   }
  153.  
  154.  exit(error);
  155. }
  156.