home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hstream / htee.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-24  |  4.8 KB  |  214 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 hd, ohd;
  26.     sframe_header fhd;
  27.     int       frame, pad = 0;
  28.     int       stream_type;
  29.     Filename  filename1, filename2, filename3;
  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.  
  50.     filename3 = "<stdout>";
  51.     fprintf(stderr, "%s: Input file= %s;  Output file= %s and %s \n", argv[0],
  52.         filename1, filename2, filename3);
  53.  
  54.     fp1 = hfopenr(filename1);
  55.     fp2 = ffopen(filename2, "w+");
  56.  
  57.     fread_header(fp1, &hd, filename1);
  58.  
  59.     stream_type = get_sinfo(&sinfo,0);
  60.     if (!stream_type) {
  61.     fprintf(stderr, "This program for stream files only! \n");
  62.     exit(0);
  63.     }
  64. #ifdef HAVE_JPEG
  65.     if (sinfo.comp_type == 1) {
  66.     if (findparam(&hd, "JPEG-info") != NULLPAR) {
  67.         int       two = 2, *xjpeg;
  68.         getparam(&hd, "JPEG-info", PFINT, &two, &xjpeg);
  69.         sinfo.qfactor = xjpeg[0];
  70.         sinfo.dsize = xjpeg[1];
  71.     }
  72.     }
  73. #endif
  74.  
  75.     dup_headern(&hd, &ohd);
  76.     set_stream_param(&hd, stream_type, sinfo.comp_type, sinfo.fps,
  77.              sinfo.qfactor, sinfo.dsize);
  78.     set_stream_param(&ohd, stream_type, sinfo.comp_type, sinfo.fps,
  79.              sinfo.qfactor, sinfo.dsize);
  80.     fwrite_header(fp2, &hd, filename2);
  81.     fwrite_header(stdout, &ohd, filename3);
  82.  
  83.     for (frame = 0; frame < hd.num_frame; frame++) {
  84.     fprintf(stderr, "%s: frame %d \n", argv[0], frame);
  85.  
  86.     if (sinfo.comp_type) {
  87.         if (copy_jfif_header(fp1, fp2) != HIPS_OK) {
  88.         break;
  89.         }
  90.     }
  91.     if (copy_stream_header(fp1, fp2, stdout, &hd, &fhd, frame) != HIPS_OK) {
  92.         break;
  93.     }
  94.     hd.sizeimage = fhd.size;
  95.     if (sinfo.comp_type && hd.image != NULL) {
  96.         free(hd.image);
  97.         hd.image = NULL;
  98.     }
  99.     alloc_image(&hd);
  100.     if (fread(hd.image, fhd.size, 1, fp1) != 1) {
  101.         fprintf(stderr, "%s error: reading frame data, file %s\n",
  102.         argv[0], filename1);
  103.         break;
  104.     }
  105.     if (fwrite(hd.image, fhd.size, 1, fp2) != 1) {
  106.         fprintf(stderr, "%s error: writing frame data, file %s\n",
  107.         argv[0], filename2);
  108.     }
  109.     if (fwrite(hd.image, fhd.size, 1, stdout) != 1) {
  110.         fprintf(stderr, "%s error: writing frame data, file %s\n",
  111.         argv[0], filename3);
  112.     }
  113.     }
  114.  
  115.     fprintf(stderr, "found %d frames in this file, fixing header... \n", frame);
  116.  
  117.     fflush(fp2);
  118.     data_end_loc = ftell(fp2);
  119.  
  120.     fseek(fp2, (long) 0, 0);    /* to beggining */
  121.     fread_header(fp2, &hd, filename2);
  122.     data_start_loc = ftell(fp2);
  123.  
  124.     hd.num_frame = frame;
  125.  
  126.     sinfo.dsize = (int) (data_end_loc - data_start_loc);
  127.     set_stream_param(&hd, stream_type, sinfo.comp_type, sinfo.fps,
  128.              sinfo.qfactor, sinfo.dsize);
  129.  
  130.     fseek(fp2, (long) 0, 0);    /* to beggining */
  131.     fwrite_header(fp2, &hd, filename2);
  132.     fclose(fp1);
  133.     fclose(fp2);
  134.  
  135.     exit(0);
  136.  
  137. }
  138.  
  139. /**********************************************************************/
  140. copy_jfif_header(fp1, fp2, fp3)
  141.     FILE     *fp1, *fp2, *fp3;
  142. {
  143.     int       skip = 0, i;
  144.     register int c;
  145.  
  146.     /*
  147.      * NOTE: for frame 0, this also copies the initial header and Q-tables
  148.      */
  149.  
  150.     while (1) {
  151.     c = fgetc(fp1);
  152.     skip++;
  153.     if (c == EOF) {        /* dont write the EOF */
  154.         fprintf(stderr, "Found EOF \n");
  155.         return (-1);
  156.     }
  157.     fputc(c, fp2);
  158.     fputc(c, fp3);
  159.     if (c == 0xFF) {
  160.         c = fgetc(fp1);
  161.         fputc(c, fp2);
  162.         fputc(c, fp3);
  163.         skip++;
  164.         if (c == 0xDA)
  165.         break;
  166.     }
  167.     }
  168.     c = fgetc(fp1);
  169.     fputc(c, fp2);
  170.     fputc(c, fp3);
  171.     skip++;
  172.  
  173. #ifdef DEBUG
  174.     fprintf(stderr, "copied %d bytes of JFIF header \n", skip);
  175. #endif
  176.  
  177.     c = fgetc(fp1);
  178.     fputc(c, fp2);
  179.     fputc(c, fp3);
  180.     skip = c - 2;
  181.  
  182.     for (i = 0; i < skip; i++) {
  183.     c = fgetc(fp1);
  184.     fputc(c, fp2);
  185.     fputc(c, fp3);
  186.     }
  187. #ifdef DEBUG
  188.     fprintf(stderr, "copied %d more bytes of JFIF header \n", skip);
  189. #endif
  190.  
  191.     return HIPS_OK;
  192. }
  193.  
  194. /***********************************************************/
  195. copy_stream_header(fp1, fp2, fp3, hd, fhd, frame)
  196.     FILE     *fp1, *fp2, *fp3;
  197.     struct header *hd;
  198.     sframe_header *fhd;
  199.     int       frame;
  200. {
  201.     register int i, s;
  202.  
  203.     if (read_stream_header(fp1, hd, fhd, frame) == HIPS_ERROR)
  204.         return(HIPS_ERROR);
  205.  
  206.     if(write_stream_header(fp2, hd, fhd, frame) == HIPS_ERROR)
  207.         return(HIPS_ERROR);
  208.  
  209.     if(write_stream_header(fp3, hd, fhd, frame) == HIPS_ERROR)
  210.         return(HIPS_ERROR);
  211.  
  212.     return HIPS_OK;
  213. }
  214.