home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / ppm / ppmtoyuv.c < prev    next >
C/C++ Source or Header  |  1993-10-04  |  2KB  |  93 lines

  1. /* ppmtoyuv.c - convert a portable pixmap into an Abekas YUV file
  2. **
  3. ** by Marc Boucher
  4. ** Internet: marc@PostImage.COM
  5. ** 
  6. ** Based on Example Conversion Program, A60/A64 Digital Video Interface
  7. ** Manual, page 69.
  8. **
  9. ** Copyright (C) 1991 by DHD PostImage Inc.
  10. ** Copyright (C) 1987 by Abekas Video Systems Inc.
  11. **
  12. ** Permission to use, copy, modify, and distribute this software and its
  13. ** documentation for any purpose and without fee is hereby granted, provided
  14. ** that the above copyright notice appear in all copies and that both that
  15. ** copyright notice and this permission notice appear in supporting
  16. ** documentation.  This software is provided "as is" without express or
  17. ** implied warranty.
  18. */
  19.  
  20. #include "ppm.h"
  21.  
  22. int
  23. main(argc, argv)
  24. char **argv;
  25. {
  26.     FILE *ifp;
  27.     pixel          *pixelrow;
  28.     register pixel *pP;
  29.     int             rows, cols, format, row;
  30.     register int    col;
  31.     pixval          maxval;
  32.     unsigned long   y1, y2=0, u=0, v=0, u0=0, u1, u2, v0=0, v1, v2;
  33.     unsigned char  *yuvbuf;
  34.  
  35.  
  36.     ppm_init(&argc, argv);
  37.  
  38.     if (argc > 2) pm_usage("[ppmfile]");
  39.  
  40.     if (argc == 2) ifp = pm_openr(argv[1]);
  41.     else ifp = stdin;
  42.  
  43.     ppm_readppminit(ifp, &cols, &rows, &maxval, &format);
  44.     pixelrow = ((pixel*) pm_allocrow( cols, sizeof(pixel) ));
  45.     yuvbuf = (unsigned char *) pm_allocrow( cols, 2 );
  46.  
  47.     for (row = 0; row < rows; ++row) {
  48.         unsigned char *yuvptr;
  49.  
  50.         ppm_readppmrow(ifp, pixelrow, cols, maxval, format);
  51.  
  52.         for (col = 0, pP = pixelrow, yuvptr=yuvbuf; col < cols; col += 2, ++pP) {
  53.             pixval r, g, b;
  54.  
  55.             /* first pixel gives Y and 0.5 of chroma */
  56.             r = PPM_GETR(*pP);
  57.             g = PPM_GETG(*pP);
  58.             b = PPM_GETB(*pP);
  59.  
  60.             y1 = 16829 * r + 33039 * g + 6416 * b + (0xffff & y2);
  61.             u1 = -4853 * r - 9530 * g + 14383 * b;
  62.             v1 = 14386 * r - 12046 * g - 2340 * b;
  63.  
  64.             pP++;
  65.             /* second pixel just yields a Y and 0.25 U, 0.25 V */
  66.             r = PPM_GETR(*pP);
  67.             g = PPM_GETG(*pP);
  68.             b = PPM_GETB(*pP);
  69.  
  70.             y2 = 16829 * r + 33039 * g + 6416 * b + (0xffff & y1);
  71.             u2 = -2426 * r - 4765 * g + 7191 * b;
  72.             v2 = 7193 * r - 6023 * g - 1170 * b;
  73.  
  74.             /* filter the chroma */
  75.             u = u0 + u1 + u2 + (0xffff & u);
  76.             v = v0 + v1 + v2 + (0xffff & v);
  77.  
  78.             u0 = u2;
  79.             v0 = v2;
  80.  
  81.             *yuvptr++ = (u >> 16) + 128;
  82.             *yuvptr++ = (y1 >> 16) + 16;
  83.             *yuvptr++ = (v >> 16) + 128;
  84.             *yuvptr++ = (y2 >> 16) + 16;
  85.         }
  86.         fwrite(yuvbuf, cols*2, 1, stdout);
  87.     }
  88.  
  89.     pm_close(ifp);
  90.  
  91.     exit(0);
  92. }
  93.