~ubuntu-branches/debian/jessie/eso-midas/jessie

« back to all changes in this revision

Viewing changes to libsrc/agl/versatec.c

  • Committer: Package Import Robot
  • Author(s): Ole Streicher
  • Date: 2014-04-22 14:44:58 UTC
  • Revision ID: package-import@ubuntu.com-20140422144458-okiwi1assxkkiz39
Tags: upstream-13.09pl1.2+dfsg
ImportĀ upstreamĀ versionĀ 13.09pl1.2+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*===========================================================================
 
2
  Copyright (C) 1995-2005 European Southern Observatory (ESO)
 
3
 
 
4
  This program is free software; you can redistribute it and/or 
 
5
  modify it under the terms of the GNU General Public License as 
 
6
  published by the Free Software Foundation; either version 2 of 
 
7
  the License, or (at your option) any later version.
 
8
 
 
9
  This program is distributed in the hope that it will be useful,
 
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
  GNU General Public License for more details.
 
13
 
 
14
  You should have received a copy of the GNU General Public 
 
15
  License along with this program; if not, write to the Free 
 
16
  Software Foundation, Inc., 675 Massachusetts Ave, Cambridge, 
 
17
  MA 02139, USA.
 
18
 
 
19
  Correspondence concerning ESO-MIDAS should be addressed as follows:
 
20
        Internet e-mail: midas@eso.org
 
21
        Postal address: European Southern Observatory
 
22
                        Data Management Division 
 
23
                        Karl-Schwarzschild-Strasse 2
 
24
                        D 85748 Garching bei Muenchen 
 
25
                        GERMANY
 
26
===========================================================================*/
 
27
 
 
28
/*
 
29
 * HEADER : versatec.c     - Vers 3.6.001  - Jul. 1992 -  F.Tribioli, L.Fini OAA
 
30
 *                         - Vers 3.6.000  - Dic. 1989 -  F. Tribioli, OAA
 
31
 *
 
32
 * Raster generator for Versatec V-80 printers
 
33
 *
 
34
 * Usage: versatec -h    To get the usage help message
 
35
 *
 
36
 * 910228 CG: exit(); -> exit(1); 
 
37
 051021         last modif
 
38
 *
 
39
 */
 
40
 
 
41
#include <stdlib.h>
 
42
#include <stdio.h>
 
43
#include <math.h>
 
44
 
 
45
 
 
46
#include <versatec.h>
 
47
 
 
48
#define FALSE 0
 
49
#define TRUE  1
 
50
 
 
51
static unsigned rowlen=DEFWDTOTPOINTS/8;
 
52
static int channel;              /* Versatec device channel number       */
 
53
static int nrows;                /* Number of rows in the bitmap         */
 
54
 
 
55
static unsigned char codeline[DEFWDTOTPOINTS*2+1];
 
56
                                 /* Array to hold encoded bit map line   */
 
57
static unsigned char * rowpt[MAXROWS];
 
58
                                 /* Array to hold pointers to beginning  */
 
59
                                 /* of rows                              */
 
60
static unsigned char * pixels;   /* Pointer to dynamically allocated     */
 
61
                                 /* space                                */
 
62
 
 
63
                                 /* Bit masks                            */
 
64
static unsigned char bmasks[]={0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
 
65
 
 
66
FILE * infile,* outfile;
 
67
 
 
68
static int clip=FALSE;
 
69
static int wflag=FALSE;
 
70
 
 
71
static double wdfact = 1.0;
 
72
static double lnfact = 1.0;
 
73
 
 
74
static double wdscale;
 
75
static double lnscale;
 
76
 
 
77
static long lnoffset = 0;
 
78
static long wdoffset = 0;
 
79
static long stripguard = 0.0;
 
80
static char csave='!';
 
81
 
 
82
static long nlines;             /* Input file line counter                 */
 
83
static long outlines=0;         /* Output print line counter               */
 
84
 
 
85
static char * writemode = "w";
 
86
 
 
87
void printhelp()
 
88
 {
 
89
  printf("\n\n");
 
90
  printf("Raster generator for Versatec V-80 printers.");
 
91
  printf(" Vers. 3.5 - F. Tribioli, Dec 1989.\n\n");
 
92
  printf("Usage: versatec [?/-h] [-wf] [-lf] [-c] [input] [output]\n\n");
 
93
  printf("where:  input  is the input file (see note below)");
 
94
  printf(" (default is stdin)\n");
 
95
  printf("        output is the output file (default is stdout)\n");
 
96
  printf("        -wf    apply a scaling factor f along paper width\n");
 
97
  printf("        -lf    apply a scaling factor f along paper length\n");
 
98
  printf("        -c     select clipping mode when requested ");
 
99
  printf("width is larger than\n");
 
100
  printf("               available (default is rescaling mode)\n");
 
101
  printf("        -s     enable splitting of large plot when requested ");
 
102
  printf("width is larger\n");
 
103
  printf("               than available (default is rescaling mode). ");
 
104
  printf("Disable -c switch\n");
 
105
  printf("        -h/?   Print this help information\n\n");
 
106
  printf("NOTE: the rasterizing program must be feed with an ");
 
107
  printf("input file generated\n");
 
108
  printf("      by the AGL standard raster driver and then ");
 
109
  printf("sorted alphabetically\n");
 
110
  printf("      with any sorting utility. If the file is not sorted ");
 
111
  printf("the rasterizer\n");
 
112
  printf("      will fail.\n\n");
 
113
  printf("Copyright 1989 by F. Tribioli. This program may be freely ");
 
114
  printf("copied and used\n");
 
115
  printf("provided this copyright notice will remain attached to it.\n\n");
 
116
 }
 
117
 
 
118
void markpixel(wd,ln)
 
119
 double wd,ln;
 
120
 {
 
121
  long wdidx,wdrest,wd0,ln0;
 
122
 
 
123
  wd0 = wd+0.5;
 
124
  ln0 = ln+0.5;
 
125
 
 
126
  wdidx = wd0>>3;
 
127
  wdrest = wd0 & 7;
 
128
 
 
129
  if(ln0>=nrows || ln0<0) return;
 
130
  if(wdidx<0 || wdidx>=rowlen) return;
 
131
  *(rowpt[ln0]+wdidx) |= bmasks[wdrest];
 
132
  wflag=TRUE;
 
133
 }
 
134
  
 
135
/*
 
136
 * routine : getspace. Allocates memory space for the bitmap and sets the
 
137
 *                     required variables
 
138
 */
 
139
 
 
140
void getspace(n_rows)
 
141
 long n_rows;                          /* number of character rows           */
 
142
 {
 
143
 
 
144
  long i, nbytes;
 
145
 
 
146
  if(n_rows > MAXROWS)
 
147
   {
 
148
    printf("Rasterizer error: Bitmap strip length too big\n");
 
149
    exit(1);
 
150
   }
 
151
 
 
152
  nbytes = rowlen * n_rows; 
 
153
 
 
154
  pixels = (unsigned char *)malloc((size_t)nbytes);
 
155
  if(pixels == NULL)
 
156
   {
 
157
    printf("Rasterizer error: Not enough memory for bitmap\n");
 
158
    exit(1);
 
159
   }
 
160
 
 
161
  for(i=0; i<nbytes; i++) 
 
162
   pixels[i] = 0;
 
163
 
 
164
  nrows = n_rows;
 
165
  lnoffset = 0;
 
166
  stripguard = nrows;
 
167
  
 
168
  for(i=0; i<n_rows; i++)                     /* Initialize row pointers   */
 
169
   rowpt[i] = pixels + (i * rowlen);
 
170
 }
 
171
 
 
172
/*
 
173
 * routine : encode. Compress original bitmap line. First short is the 
 
174
 *                   value of the first pixel of the line. The other short
 
175
 *                   are the number of consecutive pixels with the same value.
 
176
 */
 
177
 
 
178
void encode(pt,nout)
 
179
 unsigned char *pt;                    /* pointer to bitmap row to encode    */
 
180
 int *nout;                            /* encoded data length                */
 
181
 {
 
182
  register i,j;
 
183
  register counter=0;
 
184
  unsigned char bitval,bitsave,*pt1;
 
185
 
 
186
  *nout=1;
 
187
  pt1=codeline;
 
188
  *pt1++ = bitsave = (*pt & *bmasks) ? 1 : 0;
 
189
  for(i=0;i<VERSATECBUFSIZE;i++)
 
190
   {
 
191
    for(j=0;j<8;j++)
 
192
     {
 
193
      bitval = (*pt & bmasks[j]) ? 1 : 0;
 
194
      if(bitval == bitsave)
 
195
       counter++;
 
196
      else
 
197
       {
 
198
        *pt1++=(unsigned char)(counter>>8);
 
199
        *pt1++=(unsigned char)(counter & 255);
 
200
        counter=1;
 
201
        *nout+=2;
 
202
        bitsave=bitval;
 
203
       }
 
204
     }
 
205
    pt++;
 
206
   }
 
207
  *pt1++=(unsigned char)(counter>>8);
 
208
  *pt1++=(unsigned char)(counter & 255);
 
209
  *nout+=2;
 
210
 }
 
211
 
 
212
/*
 
213
 * routine: writeout. Write on output disk file a compressed bitmap
 
214
 */
 
215
 
 
216
void writeout()             /* Writes all character rows */
 
217
 {
 
218
  register i,j;
 
219
  int nout,leng;
 
220
  long ndone;
 
221
  unsigned char *pt;
 
222
 
 
223
  if(wflag)
 
224
   for(i=0; i<nrows; i++)
 
225
    {
 
226
     pt=rowpt[i];
 
227
     ndone=0;
 
228
     do
 
229
      {
 
230
       encode(pt+ndone,&leng);
 
231
       if((nout=fwrite(codeline,1,leng,outfile))!=leng)
 
232
        {
 
233
         fprintf(stderr,"VERSATEC - Error writing output file");
 
234
         exit(1);
 
235
        }
 
236
       ndone+=VERSATECBUFSIZE;
 
237
      }
 
238
     while(ndone<rowlen);
 
239
     for(j=0;j<rowlen;j++) *(pt++)=0;
 
240
    }
 
241
 }
 
242
 
 
243
  
 
244
/*
 
245
 * routine : rasterize. Set pixels along vector trace
 
246
 */
 
247
 
 
248
void rasterize(c,wd0,ln0,wd1,ln1,hwid,vwid)
 
249
 char c;                             /* strip identifier                    */
 
250
 double wd0,ln0,wd1,ln1;             /* line ending points coordinates      */
 
251
 double hwid;                        /* Half line width (horizontal)        */
 
252
 double vwid;                        /* Half line width (vertical)          */
 
253
 {
 
254
  double wd,ln,wdend,lnend;
 
255
  double dw,dl,m;
 
256
 
 
257
  wd0 *= wdscale;                     /* First convert coordinates into      */
 
258
  wd1 *= wdscale;                     /* Pixel positions                     */
 
259
 
 
260
  ln0 *= lnscale;
 
261
  ln1 *= lnscale;
 
262
                                     /* physical strip length (=nrows)       */
 
263
  while(c>csave) 
 
264
   {
 
265
     writeout();
 
266
     stripguard+=nrows;
 
267
     lnoffset-=nrows;
 
268
     csave++;
 
269
   }
 
270
 
 
271
  csave=c;                          /* save strip identifier                */
 
272
 
 
273
  ln1+=lnoffset;      /* shift coordinates to fit well in the current strip */
 
274
  ln0+=lnoffset;
 
275
  wd0+=wdoffset;
 
276
  wd1+=wdoffset;
 
277
 
 
278
  if( (wd0<0 && wd1<0) || (wd0>DEFWDTOTPOINTS && wd1>DEFWDTOTPOINTS) ) return;
 
279
 
 
280
  dw=wd1-wd0; dl=ln1-ln0;
 
281
 
 
282
  if( fabs(dw) >= fabs(dl) )
 
283
   {                                /* Stepping along paper width         */
 
284
    if(dw<0.0)
 
285
     {
 
286
      wd=wd1; ln=ln1;
 
287
      wdend  =wd0; lnend  =ln0;
 
288
      m = (dl/dw);
 
289
     }
 
290
    else
 
291
      if(dw>0.0)
 
292
       {
 
293
        wd=wd0; ln=ln0;
 
294
        wdend  =wd1; lnend  =ln1;
 
295
        m = (dl/dw);
 
296
       }
 
297
      else
 
298
       {
 
299
        wd=wd1; ln=ln1;
 
300
        wdend  =wd1; lnend  =ln1;
 
301
        m=0.0;
 
302
        }
 
303
 
 
304
    while(wd<=wdend)
 
305
     {
 
306
      double lna=ln-vwid,lnb=ln+vwid,lnt;
 
307
      for(lnt=lna; lnt<=lnb; lnt++) markpixel(wd,lnt);
 
308
      wd++;
 
309
      ln += m;
 
310
     }
 
311
   }
 
312
  else
 
313
   {                                /* Stepping along paper length        */
 
314
    if(dl<0.0)
 
315
     {
 
316
      wd=wd1; ln=ln1;
 
317
      wdend  =wd0; lnend  =ln0;
 
318
      m = (dw/dl);
 
319
     }
 
320
    else
 
321
      if(dl>0.0)
 
322
       {
 
323
        wd=wd0; ln=ln0;
 
324
        wdend  =wd1; lnend  =ln1;
 
325
        m =  (dw/dl);
 
326
       }
 
327
     else
 
328
      {
 
329
       wd=wd1; ln=ln1;
 
330
       wdend  =wd1; lnend  =ln1;
 
331
       m=0.0;
 
332
      }
 
333
 
 
334
    while(ln<=lnend)
 
335
     {
 
336
      double wda=wd-hwid,wdb=wd+hwid,wdt;
 
337
      for(wdt=wda; wdt<=wdb; wdt++) markpixel(wdt,ln);
 
338
      ln++;
 
339
      wd += m;
 
340
     }
 
341
   }
 
342
 }
 
343
 
 
344
 
 
345
main(argc,argv)
 
346
 int argc;
 
347
 char *argv[];
 
348
 {
 
349
  char *pt;
 
350
  static char strip=0;
 
351
  int  i,split;
 
352
  double paperwd, paperln, stripln;
 
353
  double wdpointsmm, lnpointsmm, dummy;
 
354
  long wdpoints;
 
355
  long stpoints;
 
356
  long nrows;
 
357
 
 
358
 
 
359
  wdpointsmm=DEFWDPOINTSMM;
 
360
  lnpointsmm=DEFLNPOINTSMM;
 
361
 
 
362
  infile = NULL;
 
363
  outfile = NULL;
 
364
 
 
365
  if(argc==1)
 
366
   {
 
367
    printhelp();
 
368
    exit(1);
 
369
   }
 
370
 
 
371
  for(i=1; i<argc; i++)
 
372
   {                                     /* argument selection loop          */
 
373
    pt=argv[i];
 
374
    if((*pt=='-')||(*pt=='/'))
 
375
     {
 
376
      pt++;
 
377
      switch(*(pt++))
 
378
       {
 
379
        case 'w': case 'W':
 
380
         sscanf(pt,"%lf",&wdfact);
 
381
         break;
 
382
 
 
383
        case 'l': case 'L':
 
384
         sscanf(pt,"%lf",&lnfact);
 
385
         break;
 
386
 
 
387
        case 'c': case 'C':
 
388
         clip=TRUE;
 
389
         break;
 
390
 
 
391
        case 's': case 'S':
 
392
         split=TRUE;
 
393
         break;
 
394
 
 
395
        default: 
 
396
         printhelp();
 
397
         exit(1);
 
398
       }
 
399
     }
 
400
    else
 
401
     {
 
402
      if(*argv[i]=='?')
 
403
       {
 
404
        printhelp();
 
405
        return;
 
406
       }
 
407
                                               /* This is input file name    */
 
408
      if(infile==NULL) 
 
409
       {
 
410
        infile=fopen(pt,"r");
 
411
        if(infile==NULL)
 
412
         {
 
413
          fprintf(stderr,"\nVERSATEC - Error opening file %s for input\n",pt);
 
414
          return;
 
415
         }
 
416
       }
 
417
      else
 
418
       if(outfile==NULL)
 
419
        {
 
420
         outfile=fopen(pt,"w");
 
421
         if(outfile==NULL)
 
422
          {
 
423
           fprintf(stderr,"\nVERSATEC - Error opening file %s for output\n",pt);
 
424
           return;
 
425
          }
 
426
        }
 
427
     }
 
428
   }
 
429
 
 
430
  if(infile==NULL) infile = stdin;
 
431
  if(outfile==NULL) outfile = stdout;
 
432
 
 
433
  fscanf(infile, " %lf %lf %lf\n", &paperwd, &paperln, &stripln );
 
434
 
 
435
  paperwd *= wdfact;
 
436
  stripln *= lnfact;
 
437
 
 
438
  wdpoints = paperwd * wdpointsmm + 0.5;   /* Strip limits in pixels       */
 
439
  stpoints = stripln * lnpointsmm + 0.5;   /* coordinates                  */
 
440
 
 
441
  if(wdpoints>DEFWDTOTPOINTS)           /* Check if graph fits into paper  */
 
442
   {
 
443
    if(!split)
 
444
     {
 
445
      if(!clip)
 
446
       {
 
447
        double rescal = (double) DEFWDTOTPOINTS / wdpoints;
 
448
        wdfact *= rescal;
 
449
        lnfact *= rescal;
 
450
        stpoints *= rescal;
 
451
       }
 
452
      wdpoints = DEFWDTOTPOINTS;
 
453
     }
 
454
   }
 
455
  else
 
456
   wdoffset=(DEFWDTOTPOINTS-wdpoints)/2;
 
457
 
 
458
  wdscale = wdpointsmm * wdfact;
 
459
  lnscale = lnpointsmm * lnfact;
 
460
 
 
461
  nrows = stpoints;
 
462
 
 
463
  getspace(nrows);
 
464
 
 
465
  nlines=0;
 
466
 
 
467
  do
 
468
  {
 
469
   do
 
470
   {
 
471
    double wd0,ln0,wd1,ln1;
 
472
    int width;
 
473
    double hwid,vwid;
 
474
    int n;
 
475
 
 
476
    nlines++;
 
477
    n=fscanf(infile, "%c %lf %lf %lf %lf %d\n",
 
478
                      &strip, &wd0, &ln0, &wd1, &ln1, &width );
 
479
    if(n != 6)
 
480
     {
 
481
      if(!feof(infile)) 
 
482
        printf("\nFormat error in line %ld of input file\n\n",nlines);
 
483
      break;
 
484
     }
 
485
 
 
486
    vwid = width*lnfact ;
 
487
    hwid = vwid*wdfact;
 
488
 
 
489
    rasterize(strip,wd0,ln0,wd1,ln1,hwid,vwid);
 
490
   }
 
491
   while (1);                 /* Do loop terminates on input eof           */
 
492
   writeout();                /* Write remaining stripes                   */
 
493
   if(wdpoints>DEFWDTOTPOINTS)
 
494
     writeout();              /* Introduce a blank space between stripes   */
 
495
   lnoffset=0;                /* Reset length parameters                   */ 
 
496
   stripguard=nrows;
 
497
   wdoffset-=DEFWDTOTPOINTS;  /* Set new width offset                      */
 
498
   wdpoints-=DEFWDTOTPOINTS;  /* Set remaining width to plot               */
 
499
   csave='!';                 /* Reset strip identifier                    */
 
500
   rewind(infile);            /* Rewind input file to plot new strip       */
 
501
   fscanf(infile, " %lf %lf %lf\n", &dummy, &dummy, &dummy);
 
502
                              /* Skip header                               */
 
503
  }
 
504
  while(split && wdpoints>0);
 
505
                     /* Do loop terminates when all stripes are writed out */
 
506
  if(infile!=stdin) fclose(infile);
 
507
  if(outfile!=stdout) fclose(outfile);
 
508
  exit(0);
 
509
 }