home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TIFF / TACS40.ZIP / TIFTOPS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-13  |  7.1 KB  |  320 lines

  1. /* 
  2.  * tiff to PS converter
  3.  *
  4.  * written orginally to convert TIFF to a 4 bit format for ron ball
  5.  * update to create a post script file on June 11, 1987
  6.  *
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <sys\types.h>
  11. #include <sys\stat.h>
  12. #include <fcntl.h>
  13. #include <windows.h>
  14. #include <time.h>
  15.  
  16. #define TAG_IMWIDTH    256
  17. #define TAG_IMLENGTH    257
  18. #define TAG_BITSPIX    258
  19. #define TAG_PHOTOINTERP    262
  20. #define TAG_SAMPIX    277
  21. #define TAG_ROW_PER_STRIP    278
  22. #define FULL_RES 1
  23. #define BITSPERBYTE    8
  24. #define RATIO    (300.0/72.0)
  25.  
  26. float    pwidth;
  27. float    pheight;
  28. int    ifd,ofd;
  29.  
  30.  
  31. int width;        /* width of the image in pixels */
  32. int length;        /* length of the image in pixels */
  33. int bitspix;         /* bits per pixel */
  34. int tncount;        /* what line of the image are we reading. */
  35. int bytes_per_line;    /* how many bytes does this scan line take */
  36. int samppix;
  37. int blacklevel;
  38. int negflag;
  39. long rows_per_strip;    /* rows per strip flag */
  40. LPSTR linebuffer;
  41. LPSTR bigalloc();
  42.  
  43. int tiffin(ifd)
  44. int ifd;
  45. {
  46. int bytesreq;
  47. int numtags;
  48. char *malloc();
  49.  
  50. /* tiffin reads the file header and tags list from the tiff file */
  51.  
  52. if(!read_tag(ifd,FULL_RES,TAG_SAMPIX,(LPSTR)&samppix,(short)sizeof(samppix))){
  53.     /*error */
  54.     samppix = 1;
  55.     }
  56.  
  57. if(!read_tag(ifd,FULL_RES,TAG_BITSPIX,(LPSTR)&bitspix,(short)sizeof(bitspix))){
  58.     /*error */
  59.     bitspix = 1;
  60.     }
  61.  
  62. if(!read_tag(ifd,FULL_RES,TAG_IMWIDTH,(LPSTR)&width,(short)sizeof(width))){
  63.     /*error */
  64.     exit(4);
  65.     }
  66. pwidth = (float)width/(RATIO);
  67.  
  68. if(!read_tag(ifd,FULL_RES,TAG_IMLENGTH,(LPSTR)&length,(short)sizeof(length))){
  69.     /*error */
  70.     exit(4);
  71.     }
  72.  
  73. if(!read_tag(ifd,FULL_RES,TAG_ROW_PER_STRIP,(LPSTR)&rows_per_strip,(short)sizeof(rows_per_strip))){
  74.     /*error */
  75.     /* if tag not present, then value == image height */
  76.     rows_per_strip = length;
  77.     }
  78.  
  79. if(!read_tag(ifd,FULL_RES,TAG_PHOTOINTERP,(LPSTR)&blacklevel,(short)sizeof(blacklevel))){
  80.     /*error*/
  81.     /* white is black.  ff == black 00 = white  */
  82.     blacklevel = 0;
  83.     }
  84. pheight = (float)length/(RATIO);
  85.  
  86.  
  87. bytes_per_line = ((((width& 0x1)?width+1:width)*bitspix * samppix)+BITSPERBYTE-1)/BITSPERBYTE;
  88.  
  89. }
  90.  
  91.  
  92. /* decodeline - read a line of data from a tiff file.  This function assumes
  93.            that the user is reading the file one line at a time.
  94.         The function requires a file descriptor (handle in dos parlance)
  95.         and a buffer that is big enough to store the uncompressed image.
  96.     */
  97.  
  98. int decodeline(ifd,buffer,PostScript)
  99. int ifd;
  100. LPSTR buffer;
  101. int PostScript;
  102. {
  103. int retval;
  104.  
  105.     if((retval = read_image(ifd,FULL_RES,1,(long)tncount,rows_per_strip,
  106.         (LPSTR)buffer,(long)bytes_per_line*rows_per_strip)) == NULL) {
  107.         return(-1);
  108.       }
  109.     return(tncount+=rows_per_strip);
  110. }
  111.  
  112.  
  113. main(argc,argv)
  114. int argc;
  115. char **argv;
  116. {
  117.  
  118.     int PostScript;    /* are we converting to PostScript or RBFormat */
  119.     int var;    /* argument pointer */
  120.     
  121.     /* input filename of output file and open it */
  122.  
  123.  
  124.     PostScript = TRUE;
  125.     var = 1;
  126.  
  127.     if(argc !=3 && argc !=4 )
  128.         {
  129.         printf("usage: tiftops [-n] infil.tif outfil.eps\n");
  130.         exit(1);
  131.         }
  132.  
  133.     /* check to see which format we'll be writing out */
  134.  
  135.     if(argc == 4 && !strcmp(argv[var],"-n")) {
  136.         negflag = 1;
  137.         var++;
  138.     }
  139.  
  140.     if((ifd = open(argv[var++],O_RDONLY|O_BINARY)) == -1)
  141.         {
  142.         /* error */
  143.         exit(1);
  144.         }
  145.  
  146.  
  147.     if((ofd = open(argv[var],O_CREAT|O_TRUNC|O_BINARY|O_WRONLY,
  148.                 S_IREAD|S_IWRITE)) == -1) {
  149.         /* error */
  150.         exit(1);
  151.         }
  152.  
  153.     tiffin(ifd);
  154.  
  155.     linebuffer = (LPSTR)bigalloc(bytes_per_line * rows_per_strip);
  156.  
  157.  
  158.     if(PostScript) {
  159.         write_PS(ofd,width,length,bitspix);
  160.     } else {
  161.         write_he(ofd,width,length,bitspix);
  162.     }
  163.  
  164.  
  165.  
  166.     tncount = 0;
  167.     
  168.  
  169.     while(tncount < length ) {
  170.         if(tncount % 100 == 0)
  171.             printf("processing line %d\n",tncount);
  172.         if(decodeline(ifd,linebuffer,PostScript) == -1) {
  173.             printf("file translation unsuccessful \n");
  174.             break;
  175.         }
  176.         write_line(ofd,(LPSTR)linebuffer,PostScript);
  177.     }
  178.     finish_ofile(ofd,PostScript);
  179.     bigfree(linebuffer);
  180.     close_read(ifd);
  181.     close(ifd);
  182.     close(ofd);
  183.  
  184. }
  185.  
  186. write_he(ofd,wd,ln,bp)
  187. int wd;
  188. int ln;
  189. int bp;
  190. int ofd;
  191. {
  192.  
  193.     write(ofd,&wd,2);
  194.     write(ofd,&ln,2);
  195.     write(ofd,&bp,2);
  196. }
  197.  
  198. /* write the Encapsulated PostScript Header */
  199. #define PSTYPE    "%%!PS-Adobe-2.0 EPSF-1.2\n\r"
  200. #define BOUNDER "%%%%BoundingBox: %d %d %f %f\n\r"
  201. #define CREATE    "%%Creator: TIFF to Encapsulated PostScript\n%%Title: Image.TIF\n\r"
  202. #define CRDTE    "%%%%CreationDate: %s\r"
  203. #define ENDCOM    "%%EndComments\n\r"
  204. #define TRAILER    "%%Trailer\n\r"
  205. #define POINT_HEIGHT    "/height %d def\n\r"
  206. #define POINT_WIDTH    "/width %d def\n\r"
  207. #define GREY_LEVEL    "/grey %d def\n\r "
  208. #define GSAVE        "gsave \n\r"
  209. #define GRESTORE    "grestore \n\r"
  210. #define IMG_CMD        "/theimage \n\r{ width height grey [%d 0 0 -%d 0 %d]\n {currentfile picstr readhexstring pop} image \n\r} def\n\r"
  211. #define SHOWPAGE    "showpage\n\r"
  212. #define SCALE    "%f %f scale\n\r"
  213. #define STRING  "/picstr %d string def \n\r"
  214. #define THE_IMAGE "theimage\r\n"
  215.  
  216. #define CONCATPROC    "/concatprocs \n\r { /proc2 exch cvlit def \n\r /proc1 exch cvlit def \n\r "
  217. #define CONCAT1    "/newproc proc1 length proc2 length add array def \n\r"
  218. #define CONCAT2 "newproc 0 proc1 putinterval \n\r newproc proc1 length proc2 putinterval \n\r"
  219. #define CONCAT3 "newproc cvx \n\r } def \n\r"
  220. #define EXCHV "{1 exch sub } currenttransfer concatprocs settransfer \n\r"
  221.  
  222.  
  223.  
  224. write_PS(of,wd,ln,bp)
  225. int of;
  226. int wd;
  227. int ln;
  228. int bp;
  229.     {
  230.  
  231.     int cou;
  232.     long ltime;
  233.     struct tm *tmst;
  234.     struct tm *localtime();
  235.     char *asctime();
  236.     int count;
  237.     char obuf[200];
  238.  
  239.     cou = write(of,PSTYPE,strlen(PSTYPE));    /* !PS - ADOBE - EPSF */
  240.  
  241.     count = sprintf(obuf,BOUNDER,0,0,pwidth,pheight);
  242.     cou += write(of,obuf,count);    /* BOUNDING BOX */
  243.  
  244.     cou += write(of,CREATE,strlen(CREATE));  /* CREATOR, TITLE */
  245.  
  246.     time(<ime);     /* get the time */
  247.     tmst = localtime(<ime);
  248.     count = sprintf(obuf,CRDTE,asctime(tmst));    /* creation time */
  249.     cou += write(of,obuf,count);
  250.  
  251.     cou += write(of,ENDCOM,strlen(ENDCOM));
  252.  
  253.     cou += write(of,"\n",1);
  254.     
  255.     cou += write(of,GSAVE,strlen(GSAVE));
  256.  
  257.  
  258.     write(of,CONCATPROC,strlen(CONCATPROC));
  259.     write(of,CONCAT1,strlen(CONCAT1));
  260.     write(of,CONCAT2,strlen(CONCAT2));
  261.     write(of,CONCAT3,strlen(CONCAT3));
  262.  
  263.     count = sprintf(obuf,GREY_LEVEL,bitspix*samppix);
  264.     cou += write(of,obuf,count);
  265.  
  266.     count = sprintf(obuf,POINT_HEIGHT, length  );
  267.     cou += write(of,obuf,count);
  268.  
  269.     count = sprintf(obuf,POINT_WIDTH,width);
  270.     cou += write(of,obuf,count);
  271.  
  272.     count = sprintf(obuf,STRING,bytes_per_line);
  273.     cou += write(of,obuf,count);
  274.  
  275.     if(negflag || !blacklevel) {
  276.         write(of,EXCHV,sizeof(EXCHV));
  277.     }
  278.  
  279.     count = sprintf(obuf,SCALE,pwidth,pheight);
  280.     cou += write(of,obuf,count);
  281.  
  282.     count = sprintf(obuf,IMG_CMD,width,length,length);
  283.     cou += write(of,obuf,count);
  284.  
  285.     cou += write(of,GSAVE,strlen(GSAVE));
  286.  
  287.     cou += write(of,THE_IMAGE,strlen(THE_IMAGE));
  288.  
  289.  
  290. }
  291.  
  292.     
  293.  
  294.  
  295. write_line(of,buf,PS)
  296. int of;
  297. LPSTR buf;
  298. int PS;
  299. {
  300.     
  301.     if(PS) {
  302.         tlpWrite(of,(LPSTR)buf,bytes_per_line*rows_per_strip);
  303.     } else { 
  304.         tlpWrite(of,(LPSTR)buf,bytes_per_line*2);
  305.     }
  306. }
  307.  
  308.  
  309. finish_ofile(of)
  310. int of;
  311. {
  312.     int cou;
  313.  
  314.     cou = 0;
  315.     cou += write(of,GRESTORE,strlen(GRESTORE));
  316.     cou += write(of,SHOWPAGE,strlen(SHOWPAGE));
  317.     cou += write(of,GRESTORE,strlen(GRESTORE));
  318.     cou += write(of,TRAILER,strlen(TRAILER));
  319. }
  320.