summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/psfile.cc
blob: 506821d7f28e135500d6892e9f4958d6e27a9286 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/*****
 * psfile.cc
 * Andy Hammerlindl 2002/06/10
 *
 * Encapsulates the writing of commands to a PostScript file.
 * Allows identification and removal of redundant commands.
 *****/

#include <ctime>
#include <iomanip>
#include <sstream>
#include <zlib.h>

#include "psfile.h"
#include "settings.h"
#include "errormsg.h"
#include "array.h"

using std::ofstream;
using std::setw;
using vm::array;
using vm::read;

namespace camp {

void checkColorSpace(ColorSpace colorspace)
{
  switch(colorspace) {
    case DEFCOLOR:
    case INVISIBLE:
      reportError("Cannot shade with invisible pen");
    case PATTERN:
      reportError("Cannot shade with pattern");
      break;
    default:
      break;
  }
}
    
psfile::psfile(const string& filename, bool pdfformat)
  : filename(filename), pdfformat(pdfformat), pdf(false),
    transparency(false), buffer(NULL), out(NULL) 
{
  if(filename.empty()) out=&cout;
  else out=new ofstream(filename.c_str());
  if(!out || !*out)
    reportError("Cannot write to "+filename);
}

static const char *inconsistent="inconsistent colorspaces";
  
void psfile::writefromRGB(unsigned char r, unsigned char g, unsigned char b, 
                          ColorSpace colorspace, size_t ncomponents) 
{
  static const double factor=1.0/255.0;
  pen p(r*factor,g*factor,b*factor);
  p.convert();
  if(!p.promote(colorspace))
    reportError(inconsistent);
  write(&p,ncomponents);
}

inline unsigned char average(unsigned char *a, size_t dx, size_t dy)
{
  return ((unsigned) a[0]+(unsigned) a[dx]+(unsigned) a[dy]+
          (unsigned) a[dx+dy])/4;
}

void psfile::dealias(unsigned char *a, size_t width, size_t height, size_t n,
                     bool convertrgb, ColorSpace colorspace)
{
  // Dealias all but the last row and column of pixels.
  size_t istop=width-1;
  size_t jstop=height-1;
  if(convertrgb) {
    size_t nwidth=3*width;
    for(size_t j=0; j < height; ++j) {
      unsigned char *aj=a+nwidth*j;
      for(size_t i=0; i < width; ++i) {
        unsigned char *ai=aj+3*i;
        if(i < istop && j < jstop)
          writefromRGB(average(ai,3,nwidth),
                       average(ai+1,3,nwidth),
                       average(ai+2,3,nwidth),colorspace,n);
        else
          writefromRGB(ai[0],ai[1],ai[2],colorspace,n);
      }
    }
  } else {
    size_t nwidth=n*width;
    for(size_t j=0; j < jstop; ++j) {
      unsigned char *aj=a+nwidth*j;
      for(size_t i=0; i < istop; ++i) {
        unsigned char *ai=aj+n*i;
        for(size_t k=0; k < n; ++k)
          ai[k]=average(ai+k,n,nwidth);
      }
    }
  }
}

void psfile::writeCompressed(const unsigned char *a, size_t size)
{  
  uLongf compressedSize=compressBound(size);
  Bytef *compressed=new Bytef[compressedSize];
  
  if(compress(compressed,&compressedSize,a,size) != Z_OK)
    reportError("image compression failed");

  encode85 e(out);
  for(size_t i=0; i < compressedSize; ++i)
    e.put(compressed[i]);
}
  
void psfile::close()
{
  if(out) {
    out->flush();
    if(!filename.empty()) {
#ifdef __CYGWIN__  
      chmod(filename.c_str(),~settings::mask & 0777);
#endif	    
      if(!out->good())
        // Don't call reportError since this may be called on handled_error.
        reportFatal("Cannot write to "+filename);
      delete out;
      out=NULL;
    }
  }
}

psfile::~psfile()
{
  close();
}
  
void psfile::header()
{
  Int level=settings::getSetting<Int>("level");
  *out << "%!PS-Adobe-" << level << ".0 EPSF-" << level << ".0" << newl;
}
  
void psfile::prologue(const bbox& box)
{
  header();
  BoundingBox(box);
  *out << "%%Creator: " << settings::PROGRAM << " " << settings::VERSION
       << SVN_REVISION <<  newl;

  time_t t; time(&t);
  struct tm *tt = localtime(&t);
  char prev = out->fill('0');
  *out << "%%CreationDate: " << tt->tm_year + 1900 << "."
       << setw(2) << tt->tm_mon+1 << "." << setw(2) << tt->tm_mday << " "
       << setw(2) << tt->tm_hour << ":" << setw(2) << tt->tm_min << ":"
       << setw(2) << tt->tm_sec << newl;
  out->fill(prev);

  *out << "%%Pages: 1" << newl;
  *out << "%%Page: 1 1" << newl;
  
  if(!pdfformat)
    *out 
      << "/Setlinewidth {0 exch dtransform dup abs 1 lt {pop 0}{round} ifelse"
      << newl
      << "idtransform setlinewidth pop} bind def" << newl;
}

void psfile::epilogue()
{
  *out << "showpage" << newl;
  *out << "%%EOF" << newl;
}

void psfile::setcolor(const pen& p, const string& begin="",
                      const string& end="")
{
  if(p.cmyk() && (!lastpen.cmyk() ||
                  (p.cyan() != lastpen.cyan() || 
                   p.magenta() != lastpen.magenta() || 
                   p.yellow() != lastpen.yellow() ||
                   p.black() != lastpen.black()))) {
    *out << begin << p.cyan() << " " << p.magenta() << " " << p.yellow() << " " 
         << p.black() << (pdf ? " k" : " setcmykcolor") << end << newl;
  } else if(p.rgb() && (!lastpen.rgb() || 
                        (p.red() != lastpen.red() || 
                         p.green() != lastpen.green() || 
                         p.blue() != lastpen.blue()))) {
    *out << begin << p.red() << " " << p.green() << " " << p.blue()
         << (pdf ? " rg" : " setrgbcolor") << end << newl;
  } else if(p.grayscale() && (!lastpen.grayscale() ||
                              p.gray() != lastpen.gray())) {
    *out << begin << p.gray() << (pdf ? " g" : " setgray") << end << newl;
  }
}
  
void psfile::setopacity(const pen& p)
{
  if(p.blend() != lastpen.blend()) {
    *out << "/" << p.blend() << " .setblendmode" << newl;
    transparency=true;
  }
  
  if(p.opacity() != lastpen.opacity()) {
    *out << p.opacity() << " .setopacityalpha" << newl;
    transparency=true;
  }
  
  lastpen.settransparency(p);
}

  
void psfile::setpen(pen p)
{
  p.convert();
    
  setopacity(p);
  
  if(!p.fillpattern().empty() && p.fillpattern() != lastpen.fillpattern()) 
    *out << p.fillpattern() << " setpattern" << newl;
  else setcolor(p);
  
  // Defer dynamic linewidth until stroke time in case currentmatrix changes.
  if(p.width() != lastpen.width()) {
    *out << p.width() << (pdfformat ? " setlinewidth" : " Setlinewidth") 
         << newl;
  }
    
  if(p.cap() != lastpen.cap()) {
    *out << p.cap() << " setlinecap" << newl;
  }
    
  if(p.join() != lastpen.join()) {
    *out << p.join() << " setlinejoin" << newl;
  }
    
  if(p.miter() != lastpen.miter()) {
    *out << p.miter() << " setmiterlimit" << newl;
  }
    
  if(p.stroke() != lastpen.stroke() || 
     p.linetype().offset != lastpen.linetype().offset) {
    out->setf(std::ios::fixed);
    *out << "[" << p.stroke() << "] " << std::setprecision(6) 
         << p.linetype().offset << " setdash" << newl;
    out->unsetf(std::ios::fixed);
  }
    
  lastpen=p;
}

void psfile::write(const pen& p)
{
  if(p.cmyk())
    *out << p.cyan() << " " << p.magenta() << " " << p.yellow() << " " 
         << p.black();
  else if(p.rgb())
    *out << p.red() << " " << p.green() << " " << p.blue();
  else if(p.grayscale())
    *out << p.gray();
}
  
void psfile::write(path p, bool newPath)
{
  Int n = p.size();
  assert(n != 0);

  if(newPath) newpath();

  pair z0=p.point((Int) 0);

  // Draw points
  moveto(z0);
  
  for(Int i = 1; i < n; i++) {
    if(p.straight(i-1)) lineto(p.point(i));
    else curveto(p.postcontrol(i-1),p.precontrol(i),p.point(i));
  }

  if(p.cyclic()) {
    if(p.straight(n-1)) lineto(z0);
    else curveto(p.postcontrol(n-1),p.precontrol((Int) 0),z0);
    closepath();
  } else {
    if(n == 1) lineto(z0);
  }
}

void psfile::latticeshade(const vm::array& a, const bbox& b)
{
  checkLevel();
  size_t n=a.size();
  if(n == 0) return;
  
  array *a0=read<array *>(a,0);
  size_t m=a0->size();
  setfirstopacity(*a0);
  
  ColorSpace colorspace=maxcolorspace2(a);
  checkColorSpace(colorspace);
  
  size_t ncomponents=ColorComponents[colorspace];
  
  *out << "<< /ShadingType 1" << newl
       << "/Matrix ";

  write(matrix(b.Min(),b.Max()));
  *out << newl;
  *out << "/ColorSpace /Device" << ColorDeviceSuffix[colorspace] << newl
       << "/Function" << newl
       << "<< /FunctionType 0" << newl
       << "/Order 1" << newl
       << "/Domain [0 1 0 1]" << newl
       << "/Range [";
  for(size_t i=0; i < ncomponents; ++i)
    *out << "0 1 ";
  *out << "]" << newl
       << "/Decode [";
  for(size_t i=0; i < ncomponents; ++i)
    *out << "0 1 ";
  *out << "]" << newl;
  *out << "/BitsPerSample 8" << newl;
  *out << "/Size [" << m << " " << n << "]" << newl
       << "/DataSource <" << newl;

  beginHex();
  for(size_t i=n; i > 0;) {
    array *ai=read<array *>(a,--i);
    checkArray(ai);
    size_t aisize=ai->size();
    if(aisize != m) reportError("shading matrix must be rectangular");
    for(size_t j=0; j < m; j++) {
      pen *p=read<pen *>(ai,j);
      p->convert();
      if(!p->promote(colorspace))
        reportError(inconsistent);
      writeHex(p,ncomponents);
    }
  }
  endHex();

  *out << ">>" << newl
       << ">>" << newl
       << "shfill" << newl;
}

// Axial and radial shading
void psfile::gradientshade(bool axial, const ColorSpace &colorspace,
                           const pen& pena, const pair& a, double ra,
                           const pen& penb, const pair& b, double rb)
{
  checkLevel();
  setopacity(pena);
  checkColorSpace(colorspace);
  
  *out << "<< /ShadingType " << (axial ? "2" : "3") << newl
       << "/ColorSpace /Device" << ColorDeviceSuffix[colorspace] << newl
       << "/Coords [";
  write(a);
  if(!axial) write(ra);
  write(b);
  if(!axial) write(rb);
  *out << "]" << newl
       << "/Extend [true true]" << newl
       << "/Function" << newl
       << "<< /FunctionType 2" << newl
       << "/Domain [0 1]" << newl
       << "/C0 [";
  write(pena);
  *out << "]" << newl
       << "/C1 [";
  write(penb);
  *out << "]" << newl
       << "/N 1" << newl
       << ">>" << newl
       << ">>" << newl
       << "shfill" << newl;
}
  
void psfile::gouraudshade(const array& pens, const array& vertices,
                          const array& edges)
{
  checkLevel();
  size_t size=pens.size();
  if(size == 0) return;
  
  setfirstopacity(pens);
  ColorSpace colorspace=maxcolorspace(pens);

  *out << "<< /ShadingType 4" << newl
       << "/ColorSpace /Device" << ColorDeviceSuffix[colorspace] << newl
       << "/DataSource [" << newl;
  for(size_t i=0; i < size; i++) {
    write(read<Int>(edges,i));
    write(read<pair>(vertices,i));
    pen *p=read<pen *>(pens,i);
    p->convert();
    if(!p->promote(colorspace))
      reportError(inconsistent);
    *out << " ";
    write(*p);
    *out << newl;
  }
  *out << "]" << newl
       << ">>" << newl
       << "shfill" << newl;
}
 
// Tensor-product patch shading
void psfile::tensorshade(const array& pens, const array& boundaries,
                         const array& z)
{
  checkLevel();
  size_t size=pens.size();
  if(size == 0) return;
  size_t nz=z.size();
  
  array *p0=read<array *>(pens,0);
  if(checkArray(p0) != 4)
    reportError("4 pens required");
  setfirstopacity(*p0);
  
  ColorSpace colorspace=maxcolorspace2(pens);
  checkColorSpace(colorspace);

  *out << "<< /ShadingType 7" << newl
       << "/ColorSpace /Device" << ColorDeviceSuffix[colorspace] << newl
       << "/DataSource [" << newl;
  
  for(size_t i=0; i < size; i++) {
    // Only edge flag 0 (new patch) is implemented since the 32% data
    // compression (for RGB) afforded by other edge flags really isn't worth
    // the trouble or confusion for the user. 
    write(0);
    path g=read<path>(boundaries,i);
    if(!(g.cyclic() && g.size() == 4))
      reportError("specify cyclic path of length 4");
    for(Int j=0; j < 4; ++j) {
      write(g.point(j));
      write(g.postcontrol(j));
      write(g.precontrol(j+1));
    }
    if(nz == 0) { // Coons patch
      static double nineth=1.0/9.0;
      for(Int j=0; j < 4; ++j) {
        write(nineth*(-4.0*g.point(j)+6.0*(g.precontrol(j)+g.postcontrol(j))
                      -2.0*(g.point(j-1)+g.point(j+1))
                      +3.0*(g.precontrol(j-1)+g.postcontrol(j+1))
                      -g.point(j+2)));
      }
    } else {
      array *zi=read<array *>(z,i);
      if(checkArray(zi) != 4)
        reportError("specify 4 internal control points for each path");
      for(Int j=0; j < 4; ++j)
        write(read<pair>(zi,j));
    }
    
    array *pi=read<array *>(pens,i);
    if(checkArray(pi) != 4)
      reportError("specify 4 pens for each path");
    for(Int j=0; j < 4; ++j) {
      pen *p=read<pen *>(pi,j);
      p->convert();
      if(!p->promote(colorspace))
        reportError(inconsistent);
      *out << " ";
      write(*p);
    }
    *out << newl;
  }
  
  *out << "]" << newl
       << ">>" << newl
       << "shfill" << newl;
}
 
inline unsigned char byte(double r) // Map [0,1] to [0,255]
{
  if(r < 0.0) r=0.0;
  else if(r > 1.0) r=1.0;
  int a=(int)(256.0*r);
  if(a == 256) a=255;
  return a;
}

void psfile::write(pen *p, size_t ncomponents) 
{
  switch(ncomponents) {
    case 0:
      break;
    case 1: 
      writeByte(byte(p->gray())); 
      break;
    case 3:
      writeByte(byte(p->red())); 
      writeByte(byte(p->green())); 
      writeByte(byte(p->blue())); 
      break;
    case 4:
      writeByte(byte(p->cyan())); 
      writeByte(byte(p->magenta())); 
      writeByte(byte(p->yellow())); 
      writeByte(byte(p->black())); 
    default:
      break;
  }
}

void psfile::writeHex(pen *p, size_t ncomponents) 
{
  switch(ncomponents) {
    case 0:
      break;
    case 1: 
      write2(byte(p->gray())); 
      writenewl();
      break;
    case 3:
      write2(byte(p->red())); 
      write2(byte(p->green())); 
      write2(byte(p->blue())); 
      writenewl();
      break;
    case 4:
      write2(byte(p->cyan())); 
      write2(byte(p->magenta())); 
      write2(byte(p->yellow())); 
      write2(byte(p->black())); 
      writenewl();
    default:
      break;
  }
}

string filter() 
{
  return settings::getSetting<Int>("level") >= 3 ? 
    "1 (~>) /SubFileDecode filter /ASCII85Decode filter\n/FlateDecode" :
    "1 (~>) /SubFileDecode filter /ASCII85Decode";
}

void psfile::imageheader(size_t width, size_t height, ColorSpace colorspace)
{
  size_t ncomponents=ColorComponents[colorspace];
  *out << "/Device" << ColorDeviceSuffix[colorspace] << " setcolorspace" 
       << newl
       << "<<" << newl
       << "/ImageType 1" << newl
       << "/Width " << width << newl
       << "/Height " << height << newl
       << "/BitsPerComponent 8" << newl
       << "/Decode [";
  
  for(size_t i=0; i < ncomponents; ++i)
    *out << "0 1 ";
  
  *out << "]" << newl
       << "/ImageMatrix [" << width << " 0 0 " << height << " 0 0]" << newl
       << "/DataSource currentfile " << filter() << " filter" << newl
       << ">>" << newl
       << "image" << newl;
}

void psfile::image(const array& a, const array& P, bool antialias)
{
  size_t asize=a.size();
  size_t Psize=P.size();
  if(asize == 0 || Psize == 0) return;
  
  array *a0=read<array *>(a,0);
  size_t a0size=a0->size();
  if(a0size == 0) return;
  
  setfirstopacity(P);
  
  ColorSpace colorspace=maxcolorspace(P);
  checkColorSpace(colorspace);
  
  size_t ncomponents=ColorComponents[colorspace];
  
  imageheader(a0size,asize,colorspace);
    
  double min=read<double>(a0,0);
  double max=min;
  for(size_t i=0; i < asize; i++) {
    array *ai=read<array *>(a,i);
    for(size_t j=0; j < a0size; j++) {
      double val=read<double>(ai,j);
      if(val > max) max=val;
      else if(val < min) min=val;
    }
  }
  
  double step=(max == min) ? 0.0 : (Psize-1)/(max-min);
  
  beginImage(ncomponents*a0size*asize);
  for(size_t i=0; i < asize; i++) {
    array *ai=read<array *>(a,i);
    for(size_t j=0; j < a0size; j++) {
      double val=read<double>(ai,j);
      size_t index=(size_t) ((val-min)*step+0.5);
      pen *p=read<pen *>(P,index < Psize ? index : Psize-1);
      p->convert();
      if(!p->promote(colorspace))
        reportError(inconsistent);
      write(p,ncomponents);
    }
  }
  endImage(antialias,a0size,asize,ncomponents);
}

void psfile::image(const array& a, bool antialias)
{
  size_t asize=a.size();
  if(asize == 0) return;
  
  array *a0=read<array *>(a,0);
  size_t a0size=a0->size();
  if(a0size == 0) return;
  
  setfirstopacity(*a0);
  
  ColorSpace colorspace=maxcolorspace2(a);
  checkColorSpace(colorspace);
  
  size_t ncomponents=ColorComponents[colorspace];
  
  imageheader(a0size,asize,colorspace);
    
  beginImage(ncomponents*a0size*asize);
  for(size_t i=0; i < asize; i++) {
    array *ai=read<array *>(a,i);
    for(size_t j=0; j < a0size; j++) {
      pen *p=read<pen *>(ai,j);
      p->convert();
      if(!p->promote(colorspace))
        reportError(inconsistent);
      write(p,ncomponents);
    }
  }
  endImage(antialias,a0size,asize,ncomponents);
}
  
void psfile::outImage(bool antialias, size_t width, size_t height,
                      size_t ncomponents)
{
  if(antialias) dealias(buffer,width,height,ncomponents);
  if(settings::getSetting<Int>("level") >= 3)
    writeCompressed(buffer,count);
  else {
    encode85 e(out);
    for(size_t i=0; i < count; ++i)
      e.put(buffer[i]);
  }
}
  
void psfile::rawimage(unsigned char *a, size_t width, size_t height,
                      bool antialias)
{
  pen p(0.0,0.0,0.0);
  p.convert();
  ColorSpace colorspace=p.colorspace();
  checkColorSpace(colorspace);
  
  size_t ncomponents=ColorComponents[colorspace];
  
  imageheader(width,height,colorspace);
  
  count=ncomponents*width*height;
  if(colorspace == RGB) {
    buffer=a;
    outImage(antialias,width,height,ncomponents);
  } else {
    beginImage(count);
    if(antialias)
      dealias(a,width,height,ncomponents,true,colorspace);
    else {
      size_t height3=3*height;
      for(size_t i=0; i < width; ++i) {
        unsigned char *ai=a+height3*i;
        for(size_t j=0; j < height; ++j) {
          unsigned char *aij=ai+3*j;
          writefromRGB(aij[0],aij[1],aij[2],colorspace,ncomponents);
        }
      }
    }
    endImage(false,width,height,ncomponents);
  }
}

} //namespace camp