File: eps.cpp

package info (click to toggle)
kdegraphics 2%3A980312-3
  • links: PTS
  • area: contrib
  • in suites: hamm
  • size: 6,168 kB
  • ctags: 5,778
  • sloc: ansic: 26,607; cpp: 21,205; sh: 5,224; makefile: 1,651; perl: 108
file content (101 lines) | stat: -rw-r--r-- 1,822 bytes parent folder | download
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
#include <unistd.h>
#include <stdio.h>
#include <qimage.h>

#include "eps.h"

#define OK (1==1)
#define FAIL (1==0)

#define CMDBUFLEN   4096
#define BUFLEN 200
char buf[BUFLEN+1];

#define BBOX "%%BoundingBox:"
#define BBOX_LEN strlen(BBOX)

int bbox (char *fileName, int *x1, int *y1, int *x2, int *y2)
{
  FILE * file;
  int a, b, c, d;
  int ret = FALSE;

  file = fopen (fileName, "r");

  do {
    if (! fgets (buf, BUFLEN, file)) break;
      
    if (strncmp (buf, BBOX, BBOX_LEN) == 0)
    {
      ret = sscanf (buf, "%s %d %d %d %d", buf, &a, &b, &c, &d);    
      if (ret == 5) {
        ret = TRUE;
        break;
      } 
    }  
  } while (1==1);

  *x1 = a;
  *y1 = b;
  *x2 = c;
  *y2 = d;

  fclose (file);
  return ret;
}  

void read_ps_epsf (QImageIO *image)
{
  FILE * file1, * file2;
  char * tmpFileName;
  int x1, y1, x2, y2;

  QImage myimage;

  char cmdBuf [CMDBUFLEN];

  tmpFileName = tmpnam(NULL); 

  if (! bbox (image->fileName(), &x1, &y1, &x2, &y2)) return;
 
  // x1, y1 -> translation
  // x2, y2 -> new size

  x2 -= x1;
  y2 -= y1;
  
  sprintf (cmdBuf, "gs -sOutputFile=%s -q -g%dx%d -dNOPAUSE -sDEVICE=ppm -c 0 0 moveto 1000 0 lineto 1000 1000 lineto 0 1000 lineto 1 1 254 255 div setrgbcolor fill 0 0 0 setrgbcolor - -c showpage quit",
         tmpFileName, x2, y2);    

  file1 = popen (cmdBuf, "w");
  
  if (! file1) return;

  fprintf (file1, "\n%d %d translate\n", -x1, -y1);

  // open src file

  file2 = fopen (image->fileName(), "r");

  if (file2)
  {
    do {
      if (! fgets (buf, BUFLEN, file2)) break;  

      fputs (buf, file1);
    } while (TRUE);

    if (file2) fclose (file2);  
  }
  pclose (file1);

  myimage.load (tmpFileName);
  myimage.createHeuristicMask();

  unlink (tmpFileName);

  image->setImage (myimage);
  image->setStatus (0);

  return;
}