home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume14 / rast / rast.c < prev    next >
C/C++ Source or Header  |  1988-05-08  |  6KB  |  343 lines

  1. #include "rast.h"
  2.  
  3. RASTER *Ropen(f,m)
  4. char *f;
  5. int m;
  6. {
  7.     RASTER *im, *Ropen_read(), *Ropen_write();
  8.  
  9.     im = NULL;
  10.     if (m == R) im = Ropen_read(f);
  11.     if (m == W) im = Ropen_write(f);
  12.     return(im);
  13. }
  14.  
  15. RASTER *Rdopen(f,m)
  16. int f,m;
  17. {
  18.     RASTER *im, *Rdopen_stdin(), *Rdopen_stdout();
  19.  
  20.     im = NULL;
  21.     if (f == 0 && m == R) im = Rdopen_stdin();
  22.     if (f == 1 && m == W) im = Rdopen_stdout();
  23.     return(im);
  24. }
  25.  
  26. RASTER *Ropen_read(f)
  27. char *f;
  28. {
  29.     RASTER *im;
  30.     char *malloc();
  31.  
  32.     im = (RASTER *)malloc(sizeof(RASTER));
  33.     im->fp = fopen(f,"r");
  34.     im->mode = R;
  35.     if (im->fp == NULL) {
  36.         fprintf(stderr,"read open fails for file \"%s\"!\n",f);
  37.         im = NULL;
  38.         return(im);
  39.     }
  40.     return(im);
  41. }
  42.  
  43. RASTER *Rdopen_stdin()
  44. {
  45.     RASTER *im;
  46.     char *malloc();
  47.  
  48.     im = (RASTER *)malloc(sizeof(RASTER));
  49.     im->fp = stdin;
  50.     im->mode = R;
  51.     if (im->fp == NULL) {
  52.         fprintf(stderr,"read open fails for stdin!\n");
  53.         im = NULL;
  54.         return(im);
  55.     }
  56.     return(im);
  57. }
  58.  
  59. RASTER *Ropen_write(f)
  60. char *f;
  61. {
  62.     RASTER *im;
  63.     char *malloc();
  64.  
  65.     im = (RASTER *)malloc(sizeof(RASTER));
  66.     im->fp = fopen(f,"w");
  67.     if (im->fp == NULL) {
  68.         fprintf(stderr,"write open fails for file \"%s\"!\n",f);
  69.         im = NULL;
  70.         return(im);
  71.     }
  72.  
  73.     im->mode      = W;
  74.     im->width     = 0;
  75.     im->height    = 0;
  76.     im->depth     = 0;
  77.     im->maplength = 0;
  78.     im->maptype   = RMT_NONE;
  79.  
  80.     if (im->fp == NULL) im = NULL;
  81.     return(im);
  82. }
  83.  
  84. RASTER *Rdopen_stdout()
  85. {
  86.     RASTER *im;
  87.     char *malloc();
  88.  
  89.     im = (RASTER *)malloc(sizeof(RASTER));
  90.     im->fp = stdout;
  91.     if (im->fp == NULL) {
  92.         fprintf(stderr,"write open fails for stdout!\n");
  93.         im = NULL;
  94.         return(im);
  95.     }
  96.  
  97.     im->mode      = W;
  98.     im->width     = 0;
  99.     im->height    = 0;
  100.     im->depth     = 0;
  101.     im->maplength = 0;
  102.     im->maptype   = RMT_NONE;
  103.  
  104.     return(im);
  105. }
  106.  
  107. Rgetheader(im)
  108. RASTER *im;
  109. {
  110.     char *malloc();
  111.     int i, rc, ll, extra;
  112.     struct rasterfile rhead;
  113.  
  114.     rc = fread(&rhead,(sizeof (struct rasterfile)),1,im->fp);
  115.     
  116.     if (rc <= 0) {
  117.         fprintf(stderr,"Rgetheader failed!\n");
  118.         return(0);
  119.     }
  120.     if (rhead.ras_magic != RAS_MAGIC) return(0);
  121.  
  122.     im->height    = rhead.ras_height;
  123.     im->width     = rhead.ras_width;
  124.     im->depth     = rhead.ras_depth;
  125.     im->length    = rhead.ras_length;
  126.     im->type      = rhead.ras_type;
  127.     im->maptype   = rhead.ras_maptype;
  128.     im->maplength = rhead.ras_maplength;
  129.  
  130.     extra = (16 - ((im->width * im->depth) % 16)) % 16;
  131.     ll = ((im->width * im->depth) + extra) / 8;
  132.  
  133.     im->linelen   = ll;
  134.     im->cache     = malloc(ll);
  135.     im->c_pixel   = im->width + 1;
  136.     im->c_pr      = mem_point(im->width,1,im->depth,im->cache);
  137.     if (im->maplength > 0) {
  138.         im->map = malloc(im->maplength);
  139.         for (i = 0; i < im->maplength; i++) im->map[i] = getc(im->fp);
  140.     }
  141.     return(1);
  142. }
  143.  
  144. Rputheader(im)
  145. RASTER *im;
  146. {
  147.     char *malloc();
  148.     int i, rc, ll, extra;
  149.     struct rasterfile rhead;
  150.  
  151.     extra = (16 - ((im->width * im->depth) % 16)) % 16;
  152.     ll = ((im->width * im->depth) + extra) / 8;
  153.  
  154.     if (im->width == 0) {
  155.         fprintf(stderr,"rasterfile width is zero!\n");
  156.         return(0);
  157.     }
  158.     if (im->height == 0) {
  159.         fprintf(stderr,"rasterfile height is zero!\n");
  160.         return(0);
  161.     }
  162.     if (im->depth == 0) {
  163.         fprintf(stderr,"rasterfile depth is zero!\n");
  164.         return(0);
  165.     }
  166.  
  167.     rhead.ras_magic     = RAS_MAGIC;
  168.     rhead.ras_width     = im->width;
  169.     rhead.ras_height    = im->height;
  170.     rhead.ras_depth     = im->depth;
  171.     rhead.ras_length    = ll * im->height;
  172.     rhead.ras_type      = RT_STANDARD;
  173.     rhead.ras_maptype   = im->maptype;
  174.     rhead.ras_maplength = im->maplength;
  175.  
  176.     im->linelen   = ll;
  177.     im->cache     = malloc(ll);
  178.     im->c_pixel   = 0;
  179.     im->c_pr      = mem_point(im->width,1,im->depth,im->cache);
  180.  
  181.     rc = fwrite(&rhead,(sizeof (struct rasterfile)),1,im->fp);
  182.     
  183.     if (rc <= 0) {
  184.         fprintf(stderr,"Rputheader failed!\n");
  185.         return(0);
  186.     }
  187.  
  188.     if (im->maptype != RMT_NONE && im->maplength > 0)
  189.         fwrite(im->map,im->maplength,1,im->fp);
  190.     return(1);
  191. }
  192.  
  193. Rgetpix(im)
  194. RASTER *im;
  195. {
  196.     int pix;
  197.     
  198.     if (im->mode != R) {
  199.         fprintf(stderr,"attempt to read rasterfile opened for write!\n");
  200.         return(-1);
  201.     }
  202.  
  203.     if (im->c_pixel >= im->width) {
  204.         fread(im->cache,im->linelen,1,im->fp);
  205.         im->c_pixel = 0;
  206.     }
  207.  
  208.     pix = pr_get(im->c_pr,im->c_pixel,0);
  209.     im->c_pixel++;
  210.     return(pix);
  211. }
  212.  
  213. Rputpix(im,pix)
  214. RASTER *im;
  215. int pix;
  216. {
  217.     if (im->mode != W) {
  218.         fprintf(stderr,"attempt to write rasterfile opened for read!\n");
  219.         return(-1);
  220.     }
  221.  
  222.     pr_put(im->c_pr,im->c_pixel,0,pix);
  223.     im->c_pixel++;
  224.     
  225.     if (im->c_pixel >= im->width) {
  226.         fwrite(im->cache,im->linelen,1,im->fp);
  227.         im->c_pixel = 0;
  228.     }
  229. }
  230.  
  231. Rclose(im)
  232. RASTER *im;
  233. {
  234.     if (im->mode == W) fflush(im->fp);
  235.     fclose(im->fp);
  236. }
  237.  
  238. Rinfo(im)
  239. RASTER *im;
  240. {
  241.     fprintf(stderr,"  height    = %d\n",im->height);
  242.     fprintf(stderr,"  width     = %d\n",im->width);
  243.     fprintf(stderr,"  depth     = %d\n",im->depth);
  244.     fprintf(stderr,"  length    = %d\n",im->length);
  245.     fprintf(stderr,"  type      = %d\n",im->type);
  246.     fprintf(stderr,"  maptype   = %d\n",im->maptype);
  247.     fprintf(stderr,"  maplength = %d\n",im->maplength);
  248. }
  249.  
  250. Rinitmap(im,t,l)
  251. RASTER *im;
  252. int t,l;
  253. {
  254.     char *malloc();
  255.  
  256.     im->maptype   = t;
  257.     im->maplength = l;
  258.     im->map       = malloc(l);
  259. }
  260.  
  261. Rputmap(im,i,r,g,b)
  262. RASTER *im;
  263. int i,r,g,b;
  264. {
  265.     int x;
  266.     
  267.     switch (im->maptype) {
  268.         case RMT_NONE: 
  269.             fprintf(stderr,"can't put in a null map!\n");
  270.             break;
  271.         case RMT_RAW:
  272.             im->map[i] = r;
  273.             break;
  274.         case RMT_EQUAL_RGB:
  275.             x = i * 3;
  276.             im->map[x++] = r;
  277.             im->map[x++] = g;
  278.             im->map[x]   = b;
  279.             break;
  280.         default:
  281.             fprintf(stderr,"unknown map type in rasterfile!\n");
  282.     }
  283. }
  284.  
  285. Rgetmap(im,i,r,g,b)
  286. RASTER *im;
  287. int i,*r,*g,*b;
  288. {
  289.     int x;
  290.     
  291.     switch (im->maptype) {
  292.         case RMT_NONE: 
  293.             fprintf(stderr,"can't get from a null map!\n");
  294.             break;
  295.         case RMT_RAW:
  296.             *r = im->map[i];
  297.             break;
  298.         case RMT_EQUAL_RGB:
  299.             x = i * 3;
  300.             *r = im->map[x++];
  301.             *g = im->map[x++];
  302.             *b = im->map[x];
  303.             break;
  304.         default:
  305.             fprintf(stderr,"unknown map type in rasterfile!\n");
  306.     }
  307. }
  308.  
  309. Rgetmappedpix(im,r,g,b)
  310. RASTER *im;
  311. int *r,*g,*b;
  312. {
  313.     int pix, x;
  314.  
  315.     if (im->mode != R) {
  316.         fprintf(stderr,"attempt to read rasterfile opened for write!\n");
  317.         return(-1);
  318.     }
  319.     
  320.     if (im->c_pixel >= im->width) {
  321.         fread(im->cache,im->linelen,1,im->fp);
  322.         im->c_pixel = 0;
  323.     }
  324.  
  325.     pix = pr_get(im->c_pr,im->c_pixel,0);
  326.     im->c_pixel++;
  327.     
  328.     switch (im->maptype) {
  329.         case RMT_RAW: 
  330.             *r = im->map[pix];
  331.             break;
  332.         case RMT_EQUAL_RGB:
  333.             x = pix * 3;
  334.             *r = im->map[x++];
  335.             *g = im->map[x++];
  336.             *b = im->map[x];
  337.             break;
  338.         default:
  339.             *r = pix;
  340.             break;
  341.     }
  342. }
  343.