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

  1. /* pjtoppm.c - convert an HP PainJetXL image to a portable pixmap file
  2. **
  3. ** Copyright (C) 1990 by Christos Zoulas (christos@ee.cornell.edu)
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "ppm.h"
  14.  
  15. static char usage[] =  "[paintjetfile]";
  16.  
  17. static int egetc ARGS((FILE *fp));
  18. static int
  19. egetc(fp)
  20. FILE *fp;
  21. {
  22.     int c;
  23.     if ((c = fgetc(fp)) == -1)
  24.     pm_error("unexpected end of file");
  25.     return(c);
  26. }
  27.  
  28. int
  29. main(argc, argv)
  30. int argc;
  31. char *argv[];
  32. {
  33.     int cmd, val;
  34.     char buffer[BUFSIZ];
  35.     int planes = 3, rows = -1, cols = -1;
  36.     int r = 0, c = 0, p = 0, i;
  37.     unsigned char **image = NULL;
  38.     int *imlen;
  39.     FILE *fp = stdin;
  40.     int mode;
  41.     int argn;
  42.     unsigned char bf[3];
  43.     pixel *pixrow;
  44.  
  45.  
  46.     ppm_init(&argc, argv);
  47.     argn = 1;
  48.     if (argn != argc)
  49.     fp = pm_openr(argv[argn++]);
  50.     else
  51.     fp = stdin;
  52.  
  53.     if (argn != argc)
  54.     pm_usage(usage);
  55.  
  56.     while ((c = fgetc(fp)) != -1) {
  57.     if (c != '\033')
  58.         continue;
  59.     switch (c = egetc(fp)) {
  60.     case 'E':    /* reset */
  61.         break;
  62.     case '*':
  63.         cmd = egetc(fp);
  64.         for (i = 0; i < BUFSIZ; i++) {
  65.         if (!isdigit(c = egetc(fp)) && c != '+' && c != '-')
  66.             break;
  67.         buffer[i] = c;
  68.         }
  69.         if (i != 0) {
  70.         buffer[i] = '\0';
  71.         if (sscanf(buffer, "%d", &val) != 1) 
  72.             pm_error("bad value `%s' at <ESC>*%c%c", buffer, cmd, c);
  73.         }
  74.         else
  75.         val = -1;
  76.         switch (cmd) {
  77.         case 't':
  78.         switch (c) {
  79.         case 'J':    /* render */
  80.             break;
  81.         case 'K':    /* back scale */
  82.             break;
  83.         case 'I':    /* gamma */
  84.             break;
  85.         case 'R':
  86.             break;    /* set resolution */
  87.         default:
  88.             pm_message("uninmplemented <ESC>*%c%d%c", cmd, val, c);
  89.             break;
  90.         }
  91.         break;
  92.         case 'r':
  93.         switch (c) {
  94.         case 'S':    /* width */
  95.             cols = val;
  96.             break;
  97.         case 'T':    /* height */
  98.             rows = val;
  99.             break;
  100.         case 'U':    /* planes */
  101.             planes = val;
  102.             if (planes != 3) 
  103.             pm_error("can handle only 3 plane files");
  104.             break;
  105.         case 'A':    /* begin raster */
  106.             break;
  107.         case 'B':
  108.         case 'C':    /* end raster */
  109.             break;
  110.         case 'V':
  111.             break;    /* set deci height */
  112.         case 'H':
  113.             break;    /* set deci width */
  114.         default:
  115.             pm_message("uninmplemented <ESC>*%c%d%c", cmd, val, c);
  116.             break;
  117.         }
  118.         break;
  119.         case 'b':
  120.         switch (c) {
  121.         case 'M':    /* transmission mode */
  122.             if (val != 0 && val != 1)
  123.             pm_error("unimplemented trasmission mode %d", val);
  124.             mode = val;
  125.             break;
  126.         case 'V':    /* send plane */
  127.         case 'W':    /* send last plane */
  128.             if (rows == -1 || r >= rows || image == NULL) {
  129.             if (rows == -1 || r >= rows)
  130.                 rows += 100;
  131.             if (image == NULL) {
  132.                 image = (unsigned char **) 
  133.                 malloc(rows * planes * sizeof(unsigned char *));
  134.                 imlen = (int *) malloc(rows * planes * sizeof(int));
  135.             }
  136.             else {
  137.                 image = (unsigned char **) 
  138.                 realloc(image, rows * planes * 
  139.                     sizeof(unsigned char *));
  140.                 imlen = (int *) realloc(imlen, rows * planes * 
  141.                             sizeof(int));
  142.             }
  143.             }
  144.             if (image == NULL || imlen == NULL)
  145.             pm_error("out of memory");
  146.             if (p == planes) 
  147.             pm_error("too many planes");
  148.             cols = cols > val ? cols : val;
  149.             imlen[r * planes + p] = val;
  150.             image[r * planes + p] = (unsigned char *) 
  151.             malloc(val * sizeof(unsigned char));
  152.             if (image[r * planes + p] == NULL) 
  153.             pm_error("out of memory");
  154.             if (fread(image[r * planes + p], 1, val, fp) != val) 
  155.             pm_error("short data");
  156.             if (c == 'V')
  157.             p++;
  158.             else {
  159.             p = 0;
  160.             r++;
  161.             }
  162.             break;
  163.         default:
  164.             pm_message("uninmplemented <ESC>*%c%d%c", cmd, val, c);
  165.             break;
  166.         }
  167.         break;
  168.         case 'p': /* Position */
  169.         if (p != 0) 
  170.             pm_error("changed position in the middle of transferring planes");
  171.         switch (c) {
  172.         case 'X':
  173.             pm_message("can only position in y");
  174.             break;
  175.         case 'Y':
  176.             if (buffer[0] == '+')
  177.             val = r + val;
  178.             if (buffer[0] == '-')
  179.             val = r - val;
  180.             for (; val > r; r++) 
  181.             for (p = 0; p < 3; p++) {
  182.                 imlen[r * planes + p] = 0;
  183.                 image[r * planes + p] = NULL;
  184.             }
  185.             r = val;
  186.             break;
  187.         default:
  188.             pm_message("uninmplemented <ESC>*%c%d%c", cmd, val, c);
  189.             break;
  190.         }
  191.         default:
  192.         pm_message("uninmplemented <ESC>*%c%d%c", cmd, val, c);
  193.         break;
  194.         }
  195.     }
  196.     }
  197.     pm_close(fp);
  198.     rows = r;
  199.     if (mode == 1) {
  200.     unsigned char *buf;
  201.     int newcols = 0;
  202.     newcols = 10240; /* It could not be larger that that! */
  203.     cols = 0;
  204.     for (r = 0; r < rows; r++) {
  205.         if (image[r * planes] == NULL)
  206.         continue;
  207.         for (p = 0; p < planes; p++) {
  208.         buf = (unsigned char *) malloc(newcols * 
  209.                            sizeof(unsigned char *));
  210.         if (buf == NULL) 
  211.             pm_error("out of memory");
  212.         for (i = 0, c = 0; c < imlen[p + r * planes]; c += 2)
  213.             for (cmd = image[p + r * planes][c],
  214.              val = image[p + r * planes][c+1]; 
  215.              cmd >= 0 && i < newcols; cmd--, i++) 
  216.              buf[i] = val;
  217.         cols = cols > i ? cols : i;
  218.         free(image[p + r * planes]);
  219.         /* 
  220.          * This is less than what we have so it realloc should 
  221.          * not return null. Even if it does, tough! We will
  222.          * lose a line, and probably die on the next line anyway
  223.          */
  224.         image[p + r * planes] = (unsigned char *) realloc(buf, i);
  225.         }
  226.     }
  227.     cols *= 8;
  228.     }
  229.             
  230.        
  231.     ppm_writeppminit(stdout, cols, rows, (pixval) 255, 0);
  232.     pixrow = ppm_allocrow(cols);
  233.     for (r = 0; r < rows; r++) {
  234.     if (image[r * planes] == NULL) {
  235.         for (c = 0; c < cols; c++)
  236.         PPM_ASSIGN(pixrow[c], 0, 0, 0);
  237.         continue;
  238.     }
  239.     for (cmd = 0, c = 0; c < cols; c += 8, cmd++) 
  240.         for (i = 0; i < 8 & c + i < cols; i++) {
  241.         for (p = 0; p < planes; p++) 
  242.             if (mode == 0 && cmd >= imlen[r * planes + p])
  243.             bf[p] = 0;
  244.             else
  245.             bf[p] = (image[r * planes + p][cmd] & 
  246.                      (1 << (7 - i))) ? 255 : 0;
  247.         PPM_ASSIGN(pixrow[c + i], bf[0], bf[1], bf[2]);
  248.         }
  249.         ppm_writeppmrow(stdout, pixrow, cols, (pixval) 255, 0);
  250.     }
  251.     pm_close(stdout);
  252.     exit(0);
  253. }
  254.