home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / hdf / unix / hdf3_2r2 / util / he_disp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-29  |  10.1 KB  |  459 lines

  1. /***************************************************************************
  2. *
  3. *
  4. *                         NCSA HDF version 3.2r2
  5. *                            October 30, 1992
  6. *
  7. * NCSA HDF Version 3.2 source code and documentation are in the public
  8. * domain.  Specifically, we give to the public domain all rights for future
  9. * licensing of the source code, all resale rights, and all publishing rights.
  10. *
  11. * We ask, but do not require, that the following message be included in all
  12. * derived works:
  13. *
  14. * Portions developed at the National Center for Supercomputing Applications at
  15. * the University of Illinois at Urbana-Champaign, in collaboration with the
  16. * Information Technology Institute of Singapore.
  17. *
  18. * THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE
  19. * SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION,
  20. * WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE
  21. *
  22. ****************************************************************************
  23. */
  24.  
  25. #ifdef RCSID
  26. static char RcsId[] = "@(#)$Revision: 1.6 $";
  27. #endif
  28. /*
  29. $Header: /hdf/hdf/v3.2r2/util/RCS/he_disp.c,v 1.6 1992/10/13 20:30:23 koziol beta koziol $
  30.  
  31. $Log: he_disp.c,v $
  32.  * Revision 1.6  1992/10/13  20:30:23  koziol
  33.  * Updates for v3.2r2 release
  34.  * .,
  35.  *
  36.  * Revision 1.1  1992/09/18  14:56:22  koziol
  37.  * Initial revision
  38.  *
  39.  * Revision 1.7  1992/09/11  18:32:51  chouck
  40.  * Assorted MAC mungings
  41.  *
  42.  * Revision 1.5  1992/08/24  21:59:44  sxu
  43.  * *** empty log message ***
  44.  *
  45.  * Revision 1.4  1992/08/18  19:56:09  chouck
  46.  * Fixed some casting problems for SGI -ansi
  47.  *
  48.  * Revision 1.3  1992/07/15  21:48:48  sxu
  49.  * No change.
  50.  *
  51.  * Revision 1.2  1992/07/14  17:50:30  mlivin
  52.  * removed Sun pixrect stuff
  53.  *
  54.  * Revision 1.1  1992/07/06  22:54:16  mlivin
  55.  * Initial revision
  56.  *
  57. */
  58. /* display.c -- contains code for displaying an image using ICR 
  59.  * this code is plucked from hdfrseq.c
  60.  */
  61. #include "he.h"
  62.  
  63. #define SCRX 1152
  64. #define SCRY 900
  65.  
  66. /* HEdisplay -- stub function for displaying an image using ICR */
  67. #ifdef PROTOTYPE
  68. int HEdisplay(HE_CMD *cmd)
  69. #else
  70. int HEdisplay(cmd)
  71.     HE_CMD *cmd;
  72. #endif
  73. {
  74. #ifndef IBM6000
  75.     register int i;
  76.     int center = 1;
  77.     int xwhere = 0;
  78.     int ywhere = 0;
  79.     int factor = 1;
  80.     int large = 0;
  81.  
  82.     for (i = 1; i < cmd->argc; i++)
  83.     if (cmd->argv[i][0] == '-')
  84.         switch (findOpt(cmd->argv[i] + 1)) 
  85.         {
  86.         case HE_HELP:
  87.         printf("display [-position <xpos> <ypos>] [-expansion <exp>] [-large]\n");
  88.         printf("\t-position\tImage position on console screen\n");
  89.         printf("\t-expansion\tImage expansion factor\n");
  90.         printf("\t-large\t\tMake image as large as posible\n");
  91.         return HE_OK;
  92.         case HE_POSITION:
  93.         center = 0;
  94.         xwhere = atoi(cmd->argv[++i]);
  95.         ywhere = atoi(cmd->argv[++i]);
  96.         if ((xwhere < 0) || (xwhere > SCRX) ||
  97.             (ywhere < 0) || (ywhere > SCRY))
  98.         {
  99.             fprintf(stderr, "Invalid position.\n");
  100.             return HE_FAIL;
  101.         }
  102.         break;
  103.  
  104.         case HE_EXPANSION:
  105.         factor = atoi(cmd->argv[++i]);
  106.         if (factor < 1) factor = 1;
  107.         break;
  108.  
  109.         case HE_LARGE:
  110.         large = 1;
  111.         break;
  112.         case HE_NOTFOUND:
  113.         unkOpt(cmd->argv[i]);
  114.         return HE_FAIL;
  115.         case HE_AMBIG:
  116.         ambigOpt(cmd->argv[i]);
  117.         return HE_FAIL;
  118.         default:
  119.         irrOpt(cmd->argv[i]);
  120.         return HE_FAIL;
  121.         }
  122.     else
  123.     {
  124.         unkArg(cmd->argv[i]);
  125.         return HE_FAIL;
  126.     }
  127.     return display(center, xwhere, ywhere, factor, large);
  128. #else
  129.     printf("Display routines do not work on this platform.\n");
  130.     return 1;
  131. #endif
  132. }
  133.  
  134. #ifdef PROTOTYPE
  135. void goTo(int desc)
  136. #else
  137. void goTo(desc)
  138.     int desc;
  139. #endif
  140. {
  141.     /* goto element of he_desc[desc] */
  142.     /* ask swami */
  143.  
  144.     /* right now this only works for r8 */
  145.     DFR8readref(he_file, he_desc[desc].ref);
  146. }
  147.  
  148.  
  149. #ifndef IBM6000  /* Skip it all */
  150.  
  151. int oldcf=0;            /* old value of compression flag */
  152. int oldx=0,oldy=0;        /* old values of xdim and ydim */
  153. int coldx=0,coldy=0;        /* old values of xdim and ydim for CI8s */
  154. int32 xdim=0,ydim=0;          /* size of image on disk */
  155. int xwhere,ywhere;        /* where to put it on the screen */
  156. int ispal;
  157. int large;            /* should make images as large as possible */
  158. int center;            /* should center the images */
  159. int oldxs=0,oldys=0;        /* old sizes */
  160. int xsize=0,ysize=0;        /* what final size on screen, after blow-up */
  161. int factor;
  162.  
  163. unsigned char rgb[768];        /* storage for a palette */
  164. char *wherebig=NULL;    /* where to store small image */
  165. uint8 *wheresmall = NULL;    /* where to store image-related stuff */
  166.  
  167. #ifdef PROTOTYPE
  168. int getSpace(void)
  169. #else
  170. int getSpace()
  171. #endif
  172. {
  173.     /*  
  174.      *  Don't allocate anything if the image is the same size as before.
  175.      */
  176.     if (oldx != xdim || oldy != ydim) {
  177.     oldx = xdim; oldy = ydim; 
  178.     
  179.     if (wheresmall)
  180.         free(wheresmall);
  181.     
  182.     if (NULL == (wheresmall = (uint8 *) HDgetspace(xdim*ydim))) {
  183.         printf(" Cannot allocate memory, fatal error\n");
  184.         exit(1);
  185.     }
  186.     
  187.     }
  188.     return(0);
  189. }
  190.  
  191. /*************************************************************************/
  192. /*  largeset
  193.  *  Set up the xfact, yfact, xsize and ysize for expanding the image
  194.  *  locally.  
  195.  *
  196.  */
  197. #ifdef PROTOTYPE
  198. int largeSet(void)
  199. #else
  200. int largeSet()
  201. #endif
  202. {
  203.     int
  204.     tmp;
  205.  
  206.     if (large) {
  207.     factor = SCRX/xdim;    /* how much blow-up can we do? */
  208.                 /* calculate expansion factor  */
  209.     tmp = SCRY/ydim;
  210.  
  211.     /* take minimum expansion factor */
  212.     if (factor > tmp) factor = tmp;
  213.     }
  214.     
  215.     xsize = factor*xdim;    /* re-calculate actual pixel dimensions */
  216.     ysize = factor*ydim;
  217.     
  218.     return(factor > 1);        /* is expansion necessary? */
  219. }
  220.  
  221. #ifdef PROTOTYPE
  222. int display(int c, int x, int y, int f, int l)
  223. #else
  224. int display(c, x, y, f, l)
  225.     int c, x, y, f, l;
  226. #endif
  227. {
  228.  
  229.     center = c;
  230.     xwhere = x;
  231.     ywhere = y;
  232.     factor = f;
  233.     large = l;
  234.  
  235.     if (!isRig(he_desc[he_currDesc].tag)) {
  236.  
  237.     fprintf(stderr, "Current element not a image group.\n");
  238.     return HE_FAIL;
  239.     }
  240.  
  241.     goTo(he_currDesc);
  242.  
  243.     if (DFR8getdims(he_file, &xdim, &ydim, &ispal)<0) {
  244.     fprintf(stderr, "Error getting image group.\n");
  245.     return HE_FAIL;
  246.     }
  247.  
  248.     if (he_remote)
  249.     getSpace();        /* get space for image in mem */
  250.  
  251.  
  252. /*
  253. *  Try to successfully load the palette and image from the file
  254. */
  255.     if (DFR8getimage(he_file, wheresmall, xdim, ydim, rgb) < 0) {
  256.     fprintf(stderr, "Error getting image group.\n");
  257.     return HE_FAIL;
  258.     }
  259.  
  260.     if (he_remote) 
  261.     rImage(ispal);    /* display remote image with [palette] */
  262.  
  263.     return HE_OK;
  264.  
  265. }
  266.  
  267.  
  268. /*****************************************************************************/
  269. /*  rimage
  270. *  Remote display of the image using the ICR.
  271. *  Just print the codes to stdout using the protocol.
  272. */
  273. #ifdef PROTOTYPE
  274. int rImage(int usepal)
  275. #else
  276. int rImage(usepal)
  277.     int usepal;
  278. #endif
  279. {
  280.     int i,j,newxsize;
  281.     int8 *thisline, *space, *thischar;
  282.     register unsigned char c;
  283.  
  284. /*
  285. *  Open the window with the W command
  286. */
  287.  
  288.     printf("\033^W;%d;%d;%d;%d;0;rseq^",xwhere,ywhere,xdim*factor,ydim*factor);
  289.  
  290. /*
  291. *  If a palette should be used, send it with the M command.
  292. */
  293.     if (usepal) {
  294.     (void)printf("\033^M;0;256;768;rseq^");  /* start map */
  295.  
  296.     thischar = (int8 *) rgb;
  297.     for (j=0; j<768; j++) {
  298.         c = *thischar++;
  299.         if (c > 31 && c < 123) {
  300.         putchar(c);
  301.         }
  302.         else {
  303.         putchar((c>>6)+123);
  304.         putchar((c & 0x3f) + 32);
  305.         }
  306.     }
  307.     }
  308.  
  309. /*
  310. *  Send the data for the image with RLE encoding for efficiency.
  311. *  Encode each line and send it.
  312. */
  313.     space    = (int8 *) malloc(ydim+128);
  314.     thisline = (int8 *) wheresmall;
  315.  
  316.     for (i = 0; i < ydim; i++) {
  317.         newxsize = rleIt(thisline,space,xdim);
  318.     thisline += xdim;    /* increment to next line */
  319.  
  320.         (void)printf("\033^R;0;%d;%d;%d;rseq^",i*factor,factor,newxsize); 
  321.  
  322.         thischar = space;
  323.         for (j = 0; j < newxsize; j++) {
  324.  
  325. /***********************************************************************/
  326. /*  Encoding of bytes:
  327. *
  328. *  123 precedes #'s 0-63
  329. *  124 precedes #'s 64-127
  330. *  125 precedes #'s 128-191
  331. *  126 precedes #'s 192-255
  332. *  overall:  realchar = (specialchar - 123)*64 + (char-32)
  333. *            specialchar = r div 64 + 123
  334. *            char = r mod 64 + 32
  335. */
  336. /***********************************************************************/
  337.  
  338.         c = *thischar++;      /* get byte to send */
  339.  
  340.         if (c > 31 && c < 123) {
  341.         putchar(c);
  342.         }
  343.         else {
  344.         putchar((c>>6)+123);
  345.         putchar((c & 0x3f) + 32);
  346.         }
  347.         }
  348.     }
  349.  
  350. /*
  351. *  pause for the user
  352. */
  353.  
  354.     free(space);
  355.     return HE_OK;
  356. }
  357.  
  358. /*****************************************************************************/
  359. /* expandimg
  360. *  copy an image memory to memory, expanding byte by byte to get a larger image.
  361. *  no aliasing, just byte replication
  362. */
  363. #ifdef PROTOTYPE
  364. int bigImg(unsigned char *targ, unsigned char *src)
  365. #else
  366. int bigImg(targ, src)
  367.     unsigned char *targ,*src;
  368. #endif
  369. {
  370.     register int i,j,line;
  371.     register unsigned char *p,*q,*oldq;
  372.  
  373.     for (line = 0; line < ydim; line++) {
  374.     p = src+line*xdim;
  375.     oldq = q = targ+line*xsize*factor;
  376.  
  377.     for (i=0; i<xdim; i++,p++)
  378.         for (j=0; j<factor; j++)
  379.         *q++ = *p;
  380.         
  381.     for (i=1; i<factor ; i++) {
  382.         DFmovmem(oldq,q,xsize); /* make one copy of the line */
  383.         q += xsize;
  384.     }
  385.  
  386.     }
  387.     return HE_OK;
  388. }
  389.  
  390.  
  391. /********************************************************************/
  392. /*  rleit
  393. *  compress the data to go out with a simple run-length encoded scheme.
  394. *
  395. */
  396. #ifdef PROTOTYPE
  397. int rleIt(char *buf, char *bufto, int len)
  398. #else
  399. int rleIt(buf, bufto, len)
  400.     int len;
  401.     char *buf,*bufto;
  402. #endif
  403. {
  404.     register char *p,*q,*cfoll,*clead;
  405.     char *begp;
  406.     int i;
  407.  
  408.     p = buf;
  409.     cfoll = bufto;        /* place to copy to */
  410.     clead = cfoll + 1;
  411.     
  412.     begp = p;
  413.     while (len > 0) {        /* encode stuff until gone */
  414.  
  415.     q = p + 1;
  416.     i = len-1;
  417.     while (*p == *q && i+120 > len && i) {
  418.         q++;
  419.         i--;
  420.     }
  421.         
  422.     if (q > p + 2) {    /* three in a row */
  423.         if (p > begp) {
  424.         *cfoll = p - begp;
  425.         cfoll = clead;
  426.         }
  427.         *cfoll++ = 128 | (q-p); /* len of seq */
  428.         *cfoll++ = *p;    /* char of seq */
  429.         len -= q-p;        /* subtract len of seq */
  430.         p = q;
  431.         clead = cfoll+1;
  432.         begp = p;
  433.     }
  434.     else {
  435.         *clead++ = *p++;    /* copy one char */
  436.         len--;
  437.         if (p > begp + 120) {
  438.         *cfoll = p - begp;
  439.         cfoll = clead++;
  440.         begp = p;
  441.         }
  442.     }
  443.     
  444.     }
  445. /*
  446. *  fill in last bytecount
  447. */
  448.     if (p > begp) 
  449.     *cfoll = (p - begp);
  450.     else
  451.     clead--;        /* don't need count position */
  452.     
  453.     return((int)(clead - bufto)); /* how many stored as encoded */
  454. }
  455.  
  456. #endif /* IBM6000 */
  457.  
  458. /* end of display.c */
  459.