home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hstream / fixframes.c next >
Encoding:
C/C++ Source or Header  |  1993-03-25  |  4.3 KB  |  198 lines

  1.  
  2. /*
  3.  *  fixframes.c  : correct the number of frames in a HIPS stream file
  4.  *
  5.  *  This program must be used with hvideo when writing to stdout
  6.  *
  7.  *
  8.  * Usage:       fixframes infile outfile
  9.  *
  10.  *  Brian Tierney, LBL
  11.  */
  12.  
  13. #include <hipl_format.h>
  14. #include <stdio.h>
  15.  
  16. /*
  17. #define DEBUG
  18. */
  19.  
  20. main(argc, argv)
  21.     int       argc;
  22.     char    **argv;
  23.  
  24. {
  25.     struct header hd1, hd2;
  26.     sframe_header fhd;
  27.     int       frame;
  28.     int       stream_type;
  29.     Filename  filename1, filename2;
  30.     FILE     *fp1, *fp2;
  31.     stream_info sinfo;
  32.     int       get_sinfo();
  33.     long      data_end_loc, data_start_loc;
  34.  
  35.     Progname = strsave(*argv);
  36.     hipserrlev = HEL_SEVERE;    /* only exit if severe errors */
  37.  
  38.     if (argc != 2 && argc != 3) {
  39.     fprintf(stderr, "Usage:  %s infile outfile \n\n", argv[0]);
  40.     exit(0);
  41.     }
  42.     if (argc == 2) {
  43.     filename1 = "<stdin>";
  44.     filename2 = argv[1];
  45.     } else {
  46.     filename1 = argv[1];
  47.     filename2 = argv[2];
  48.     }
  49.     fprintf(stderr, "%s: Input file= %s;  Output file= %s \n", argv[0],
  50.         filename1, filename2);
  51.  
  52.     fp1 = hfopenr(filename1);
  53.     fp2 = ffopen(filename2, "w+");
  54.  
  55.     fread_header(fp1, &hd1, filename1);
  56.  
  57.     stream_type = get_sinfo(&sinfo, 0);
  58.     if (!stream_type) {
  59.     fprintf(stderr, "This program for stream files only! \n");
  60.     exit(0);
  61.     }
  62. #ifdef HAVE_JPEG
  63.     if (sinfo.comp_type == 1) {
  64.     if (findparam(&hd1, "JPEG-info") != NULLPAR) {
  65.         int       two = 2, *xjpeg;
  66.         getparam(&hd1, "JPEG-info", PFINT, &two, &xjpeg);
  67.         sinfo.qfactor = xjpeg[0];
  68.         sinfo.dsize = xjpeg[1];
  69.     }
  70.     }
  71. #endif
  72.  
  73.     dup_headern(&hd1, &hd2);
  74.     set_stream_param(&hd2, stream_type, sinfo.comp_type, sinfo.fps,
  75.              sinfo.qfactor, sinfo.dsize);
  76.     fwrite_header(fp2, &hd2, filename2);
  77.  
  78.     for (frame = 0; frame < hd1.num_frame; frame++) {
  79.     fprintf(stderr, "%s: frame %d \n", argv[0], frame);
  80.  
  81.     if (sinfo.comp_type) {
  82.         if (copy_jfif_header(fp1, fp2) != HIPS_OK) {
  83.         break;
  84.         }
  85.     }
  86.     if (copy_stream_header(fp1, fp2, &hd1, &fhd, frame) != HIPS_OK) {
  87.         break;
  88.     }
  89.     hd1.sizeimage = fhd.size;
  90.     if (sinfo.comp_type && hd1.image != NULL) {
  91.         free(hd1.image);
  92.         hd1.image = NULL;
  93.     }
  94.     alloc_image(&hd1);
  95.     if (fread(hd1.image, fhd.size, 1, fp1) != 1) {
  96.         fprintf(stderr, "%s error: reading frame data, file %s\n",
  97.             argv[0], filename1);
  98.         break;
  99.     }
  100.     if (fwrite(hd1.image, fhd.size, 1, fp2) != 1) {
  101.         fprintf(stderr, "%s error: writing frame data, file %s\n",
  102.             argv[0], filename2);
  103.     }
  104.     }
  105.  
  106.     fprintf(stderr, "found %d frames in this file, fixing header... \n", frame);
  107.  
  108.     fflush(fp2);
  109.     data_end_loc = ftell(fp2);
  110.  
  111.     fseek(fp2, (long) 0, 0);    /* to beggining */
  112.     fread_header(fp2, &hd2, filename2);
  113.     data_start_loc = ftell(fp2);
  114.  
  115.     hd2.num_frame = frame;
  116.  
  117.     sinfo.dsize = (int) (data_end_loc - data_start_loc);
  118.     set_stream_param(&hd2, stream_type, sinfo.comp_type, sinfo.fps,
  119.              sinfo.qfactor, sinfo.dsize);
  120.  
  121.     fseek(fp2, (long) 0, 0);    /* to beggining */
  122.     fwrite_header(fp2, &hd2, filename2);
  123.     fclose(fp1);
  124.     fclose(fp2);
  125.  
  126.     exit(0);
  127.  
  128. }
  129.  
  130. /**********************************************************************/
  131. copy_jfif_header(fp1, fp2)
  132.     FILE     *fp1, *fp2;
  133. {
  134.     int       skip = 0, i;
  135.     register int c;
  136.  
  137.     /*
  138.      * NOTE: for frame 0, this also copies the initial header and Q-tables
  139.      */
  140.  
  141.     while (1) {
  142.     c = fgetc(fp1);
  143.     skip++;
  144.     if (c == EOF) {        /* dont write the EOF */
  145.         fprintf(stderr, "Found EOF \n");
  146.         return (-1);
  147.     }
  148.     fputc(c, fp2);
  149.     if (c == 0xFF) {
  150.         c = fgetc(fp1);
  151.         fputc(c, fp2);
  152.         skip++;
  153.         if (c == 0xDA)
  154.         break;
  155.     }
  156.     }
  157.     c = fgetc(fp1);
  158.     fputc(c, fp2);
  159.     skip++;
  160.  
  161. #ifdef DEBUG
  162.     fprintf(stderr, "copied %d bytes of JFIF header \n", skip);
  163. #endif
  164.  
  165.     c = fgetc(fp1);
  166.     fputc(c, fp2);
  167.     skip = c - 2;
  168.  
  169.     for (i = 0; i < skip; i++) {
  170.     c = fgetc(fp1);
  171.     fputc(c, fp2);
  172.     }
  173. #ifdef DEBUG
  174.     fprintf(stderr, "copied %d more bytes of JFIF header \n", skip);
  175. #endif
  176.  
  177.     return HIPS_OK;
  178. }
  179.  
  180.  
  181. /***********************************************************/
  182. copy_stream_header(fp1, fp2, hd, fhd, frame)
  183.     FILE     *fp1, *fp2;
  184.     struct header *hd;
  185.     sframe_header *fhd;
  186.     int       frame;
  187. {
  188.     register int i, s;
  189.  
  190.     if (read_stream_header(fp1, hd, fhd, frame) == HIPS_ERROR)
  191.     return (HIPS_ERROR);
  192.  
  193.     if (write_stream_header(fp2, hd, fhd, frame) == HIPS_ERROR)
  194.     return (HIPS_ERROR);
  195.  
  196.     return HIPS_OK;
  197. }
  198.