home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / tools / magicprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-02  |  9.7 KB  |  326 lines

  1. #include "header.def"
  2. #include "imagedef.h"
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. U_IMAGE    uimg;
  7.  
  8. #define    ibuf    uimg.src
  9. #define    frm    uimg.frames
  10. #define    cln    uimg.width
  11. #define    row    uimg.height
  12.  
  13. #ifndef    LDLIB
  14. #define    LDLIB    "/usr/local/magicprt.pro"
  15. #endif
  16. #ifndef    MFont
  17. #define    MFont    "NewCenturySchlbk-Bold"
  18. #endif
  19. #define    FBWR    4.0
  20. #define    FWR    3.29
  21. #ifndef    IGP
  22. #define    IGP    0
  23. #endif
  24.  
  25. /* MAGICPRINT.C - display images as halftones on a Postscript device
  26. %
  27. %    Copyright (c)    1990    Jin, Guojun
  28. */
  29. char    usage[]="options\n\
  30. -h & -w specify the output halftone size in inches. They default to an aspect\n\
  31.     ratio equal to that of the images, and if none is specified,\n\
  32.     the maximum dimension is 6 inches.  The screen can be either\n\
  33.     a circle screen (the default) or -l a line screen.\n\
  34. The screen frequency (in screen cells per inch) is set by -f; the screen angle\n\
  35.     in degrees is set by -a. The defaults are 60 cells per inch and\n\
  36.     45 degrees, they provide 25 gray levels. Gray levels are mapped\n\
  37.     linearly to percent of dots painted. This filter works for byte and\n\
  38.     bit-packed (IFMT_MSBF) images.\n\
  39. -F font_name    \n\
  40. -S #    set FONT and its SIZE for printing title string.\n\
  41. -P #    is to get height where to place the title string.\n\
  42. -B    place string at Bottom of a page.\n\
  43. -H    print the all header information below the image\n\
  44. -X[#]    put header in a box. Usually, the box size is same as image's.\n\
  45.     An immediately following real number will change the box width\n\
  46.     (range 3.8 - 8.4 inch).\n\
  47. -d    print history and description only\n\
  48. -g #    insert gap between image and box(in dots).\n\
  49. -T    command line is the title string.\n\
  50. -t | -v string    \n\
  51.     print the string under or at right side of the image\n\
  52. -b #    begin at frame #.\n\
  53. -n #    print the number of frames which only you want.\n\
  54. -c    center the image. Default image Position is on the top of a page.\n\
  55. -p [#]    image top Position.\n\
  56.     If not further # given, top of an image is 1.5 inch from TOP.\n\
  57. -m    make upside down image like in the water.\n\
  58. -s    page height (default = 11)    \n\
  59. -o #    change or adjust the number of frame printed on footnote, used for\n\
  60.     the input frames have already been cut off some.\n",
  61. buffer[128], proline[128], *ldlib=LDLIB, *FONT=MFont;
  62.  
  63. /*
  64. % Compile:
  65. % cc -O -o magicprint $(@DEF) magicprt.c -lscs6 -lccs -lhips -lrle -ltiff -lm \
  66. %    -ljpeg -lpixrect
  67. %
  68. % AUTHOR:    Jin Guojun - LBL    12/10/90
  69. */
  70.  
  71. #define    FontW(fontsz)    fontsz/fwr
  72.  
  73. /*    This offset is for top and bottom margin adjust    */
  74. #ifndef    MGPOffset
  75. #define    MGPOffset    .05    /* 0.04-0.4 inch Printer depends */
  76. #endif
  77. #ifndef    VT
  78. #define    VT    10
  79. #endif
  80.  
  81. #define    DtoA    M_PI/180
  82. #define    Mmirror(x, ref)    { register int tmp=(int)(beta/ref);    \
  83.     x=(tmp&1) ? (tmp+1)*ref-x : x-tmp*ref;    }
  84. #define    GValue()    arget(argc, argv, &i, &c)
  85. #define    SValue()    avset(argc, argv, &i, &c, 1)
  86.  
  87. bool    lflag, setflag, mirror, rota, SeqOnly;
  88. float    fwr=FBWR, ih, iw, MaxImgH = -MGPOffset * 2,
  89.     sf=60, sa=45;
  90.  
  91.  
  92. main(argc, argv)
  93. int    argc;
  94. char*    *argv;
  95. {
  96. FILE    *profile;
  97. bool    bottom=0,    /*    set title at bottom of the page    */
  98.     center=0,    /*    plcae image in middle of a page    */
  99.     printhd=0,    /*    print all header under image    */
  100.     title=0, Vt=0;    /*    title vertically at right side    */
  101. int    i, fr,py=0,    /*    footnote position from bottom    */
  102.     offf=1,    /*    offset of frame number for printing    */
  103.     bgn_p=1,    /*    begining frame number    */
  104.     num_p=1,    /*    number of page will be printed    */
  105.     linesp,    xg=0, FSZ=10;
  106. float    sy=0, boxw=0,    /*    size & bool    */
  107.     pph=10.6, ppw=8.4,
  108.     leftbd, margin=.1,
  109.     alph, beta, radius;
  110. char    *ip, *Title=0, *vstr=0;
  111. MType    fsize;
  112.  
  113. #define    c    fr
  114.  
  115. format_init(&uimg, IMAGE_INIT_TYPE, HIPS, -1, *argv, "D5-1");
  116. uimg.o_form = IFMT_BYTE;
  117.  
  118. for (i=1; i<argc; i++)
  119.     if (*argv[i] == '-')    {
  120.     c = 1;
  121.     switch(argv[i][c++])    {
  122.     case 'a':sa = GValue();    goto    sfa;
  123.     case 'f':sf = GValue();
  124. sfa:        setflag++;    break;
  125.     case 'g':    xg = GValue();    break;
  126.     case 'h':    ih = GValue();    break;
  127.     case 'w':    iw = GValue();    break;
  128.     case 'l':    lflag++;    setflag++;    break;
  129.     case 'm':    message("Image is mirrored into water\n");
  130.         mirror++;        break;
  131.     case 'd':    SeqOnly++;    goto    prthd;
  132.     case 'X':if (!argv[i][c])    boxw=1;
  133.         else    boxw = atof(argv[i]+c);
  134. prthd:    case 'H':    printhd++;    break;
  135.     case 'b':    bgn_p = GValue();    break;
  136.     case 'B':    bottom++;    break;
  137.     case 'c':    center++;    break;
  138.     case 'n':    num_p = GValue();    break;
  139.     case 'o':    offf = GValue();    break;
  140.     case 'P':    py = GValue() * 72;    break;
  141.     case 'r':    rota = GValue();    break;
  142.     case 'p':    sy = GValue();
  143.         if (!sy)    sy = 9.6;    break;
  144.     case 's':    pph = GValue();    break;
  145.     case 'F':
  146.         if (SValue())    FONT = argv[i]+c;
  147.         if (FONT[strlen(FONT)-1] != 'd')    fwr = FWR;
  148.         break;
  149.     case 'S':    FSZ = GValue();    break;
  150.     case 'v':if (SValue())    {    vstr=argv[i]+c;    break;    }
  151.     case 'V':Vt++;
  152.     case 't':if (SValue())    Title = argv[i]+c;
  153.     case 'T':title++;    break;
  154.     default:
  155. info:        usage_n_options(usage, i, argv[i]);
  156.     }
  157.     }
  158.     else if ((in_fp=freopen(uimg.name=argv[i], "rb", stdin)) == NULL)
  159.         syserr("can not open %s for input", argv[i]);
  160. io_test(fileno(in_fp), goto    info);
  161.  
  162. if ((*uimg.header_handle)(HEADER_READ, &uimg, 0, 0))
  163.     prgmerr('t', "unknown image type");
  164. if (printhd && uimg.in_type != uimg.o_type)
  165.     (*uimg.std_swif)(FI_INIT_NAME, &uimg, uimg.name, 0),
  166.     (*uimg.header_handle)(HEADER_TO, &uimg, 0, 0);
  167.  
  168. MaxImgH += pph;
  169. if (pph > 14)    ppw = 10.8;
  170.  
  171. if (!ih)
  172.    if (!iw)
  173.     if (row > cln)
  174.     {    ih = 6.;    iw = ih * cln / row;    }
  175.     else {    iw = 6.;    ih = iw * row / cln;    }
  176.    else    ih = iw * row / cln;
  177. else    if (!iw)
  178. scale:    iw = ih * cln / row;
  179.  
  180. if (title)    {
  181.    if (center && ih > MaxImgH || ih > MaxImgH + MGPOffset * 2)
  182.     {    ih = MaxImgH;    goto    scale;    }
  183.    if (!Title)    {
  184.     Title = zalloc((MType)argc, 16L);
  185.     strcpy(Title, *argv);
  186.     for (i=1; i<argc; i++)
  187.     {    strcat(Title, " ");
  188.         strcat(Title, argv[i]);
  189.     }
  190.    }
  191. }
  192. if (!sy) {
  193.     sy = pph - ih - MGPOffset;
  194.     if (center)
  195.         sy /= 2.;
  196. }
  197. else    sy -= ih;
  198. ih /= 2.;    iw /= 2.;
  199.  
  200. linesp = (FSZ>>2) + VT;
  201. if (!py)
  202.    if (!bottom && ih < pph/2.)
  203.     py = (sy - .2) * 72 - (FSZ >> 4);/* 72 points = 1 inch */
  204.    else    py = linesp;
  205.  
  206. if (uimg.o_form!=IFMT_BYTE && uimg.in_type!=FITS &&
  207.     uimg.in_form!=IFMT_BYTE && uimg.in_form!=IFMT_MSBF)
  208.     prgmerr('f', "image must be in byte format");
  209. fsize = (uimg.in_form==IFMT_MSBF)? (((cln + 7)/8) * row) : (row * cln);
  210.  
  211. radius = sqrt(ih*ih + iw*iw);
  212. alph = atan2(ih, iw);
  213. beta = abs(rota)*DtoA;
  214. Mmirror(beta, M_PI_2);
  215.  
  216. {
  217. register float    fh=2*radius*sin(alph+rota*DtoA), fw=2*radius*cos(alph-rota*DtoA);
  218. message("Image size is %d rows by %d columns & printed as => %.3f x %.3f\n",
  219.     row, cln, ih*2, iw*2);
  220. message("%s screen:  %.3f cells per inch, angle %.3f degrees, diagonal=%.3f\n",
  221.     lflag ? "Line" : "Circle", sf, sa, alph);
  222. message("Rotate %d => %.3f height x %.3f width\n", rota, fh, fw);
  223. if (fh>pph || fw>ppw)
  224.     mesg("printing size too large");
  225. }
  226. /*    if begin page not set properly, set it to page 1 */
  227. if (bgn_p > frm || bgn_p < 1)    bgn_p = 1;
  228.  
  229. /*    if # of pages for printing out of range, reset it    */
  230. num_p += --bgn_p;
  231. if (num_p<1 || num_p>frm)    num_p = frm;
  232.  
  233. message("%s: Total %d frame(s).    Start at frame %d and print %d frame(s)\n",
  234.     *argv, frm, bgn_p+offf, num_p-bgn_p);
  235.  
  236. ibuf = zalloc(fsize, (MType)sizeof(char));
  237.  
  238. if ((profile=fopen(ldlib, "r")) == NULL)    /* copy prologue */
  239.     syserr("can't open profile file %s", ldlib);
  240. while(fgets(proline, 80, profile))    fputs(proline, stdout);
  241. printf("%.2f %.2f %.2f %.2f setpos\n", ppw / 2., sy, iw, ih);
  242.  
  243. /*    gausy for printing notation and header    */
  244. if (!boxw || 3.8 > boxw || boxw > ppw){
  245.     leftbd = ppw/2. - iw + margin;
  246.     if (leftbd > 2.2)    leftbd=2.2;
  247. }
  248. else leftbd = (ppw - boxw)/2 + margin;
  249.  
  250. py -= (radius*sin(alph+beta) - ih)*72;
  251. printf("%d %.2f %.2f %d %d %.2f setform\n",
  252.     py-linesp*title+xg, -margin, leftbd, rota && IGP, -linesp, FontW(FSZ));
  253.  
  254. if (bgn_p)    fseek(in_fp, bgn_p * fsize, 1);
  255.  
  256. for(fr=bgn_p; fr<num_p; fr++){
  257.     (*uimg.std_swif)(FI_LOAD_FILE, &uimg, uimg.load_all=0, No);
  258.     message("%u bytes read in frame[%d]\n", fsize, fr+offf);
  259.  
  260.     printf("gsave\n");    /*    save graphic state.    */
  261.  
  262.     if(setflag)    /*    set screen if necessary    */
  263.         if(lflag)    printf("%f %f setline\n", sf, sa);
  264.         else        printf("%f %f setcirc\n", sf, sa);
  265.  
  266. /* set up string for temporary postscript storage of one row of image */
  267.     printf("%d setimstr\n", (uimg.in_form==IFMT_MSBF)? ((cln + 7) / 8):cln);
  268.  
  269. /* set up parameters for magicprint */
  270.     printf("%d %d setrc\n", cln, row);
  271.     printf("%d setnb\n", (uimg.in_form==IFMT_MSBF) ? 1 : 8);
  272.  
  273. /* set up position, scale and output single page data */
  274.     if (rota)
  275.         printf("%.2f %.2f %d Rotate\n",    radius,90-alph*180/M_PI-rota,rota);
  276.     else    printf("placeim\n");
  277.     printf("scaleim\n");
  278.     if (mirror)    printf("imusd\n");    /*    up side down    */
  279.     else        printf("imgo\n");
  280.  
  281.     for(i=0, ip=ibuf; i < fsize;){
  282.         printf("%02x", (byte) *ip++);
  283.         if (!(++i % 35))    printf("\n");
  284.     }
  285. /* restore graphics state & start text output    */
  286.     printf("\ngrestore\n/%s findfont %d scalefont setfont\n", FONT, FSZ);
  287. if (printhd){
  288. char*    phd = (char*)(*uimg.std_swif)(FI_HIPS_HEADER_FORMAT, &uimg, NULL);
  289.     if (SeqOnly)
  290.         phd = strchr(phd+64, 'S');
  291.     printf("LeftBd UC moveto newline\n");
  292.     do {
  293.         for (i=0; *phd != '\n'; i++)
  294.             if (*phd == '|' && *(phd+1) == '\\')
  295.             {    phd+=2;    i--;    }
  296.             else    buffer[i] = *phd++;
  297.         buffer[i]=0;
  298.         if (!strncmp(buffer, "Number of fr", 12))
  299.         {    sprintf(proline, "       :: [frame %d]", fr+offf);
  300.             strcat(buffer, proline);
  301.         }
  302.         i ^= i;
  303.         while(buffer[i] == ' ')    i++;
  304.         if (strlen(buffer+i))
  305.             printf("(%s) show\nnewline\n", buffer+i);
  306.     } while(*++phd);
  307.     if (boxw)    printf("Box\n");
  308.     }
  309. if (title | Vt){
  310.     if (num_p > 1 && !printhd)
  311.         sprintf(buffer, "%s : frame %d", Title, fr+offf);
  312.     else    strcpy(buffer, Title);
  313.     printf("gsave\n");
  314.     if(Vt>1)    printf("(%s) vshow\n", buffer);
  315.     else if(Vt)
  316.         printf("RS %d %.1f Vput\n(%s) show\n", strlen(buffer), pph/2., buffer);
  317.     else    printf("%d %d Hput\n(%s) show\n",py, strlen(buffer), buffer);
  318.     printf("grestore\n");
  319.     }
  320. if(vstr) printf("%.2f %d %.2f Vput\n(%s) show\n",
  321.         radius*cos(alph-beta), strlen(vstr), sy+ih, vstr);
  322. printf("showpage\n");
  323. }
  324. printf("restorestate\n");
  325. }
  326.