Debian Codegraph @

cvd_src/image_io/save_postscript.cxx

raw symbols SCIP
1 #include <cvd/internal/io/save_postscript.h>
2
3 #include <cvd/internal/load_and_save.h>
4 #include <iomanip>
5 #include <iostream>
6 #include <sstream>
7 #include <string>
8 using namespace std;
9
10 namespace CVD
11 {
12
13 void output_eps_header(ostream& o, int xs, int ys)
14 {
15 //Standard EPS header. This is test more by concensus from various postscript handlers
16 //than with any specific reference to documentation, but it seems to work.
17 o << "%!PS-Adobe-3.0 EPSF-3.0"
18 "\n"
19 "%%BoundingBox: 0 0 "
20 << xs << " " << ys << "\n"
21 "%%Creator: libCVD"
22 "\n"
23 "%%Pages: 0"
24 "\n"
25 "%%EndComments"
26 "\n"
27 "%%EndProlog"
28 "\n"
29 //This saves the dictionary state and enables a temporary dictionary.
30 //this should prevent the eps file from polluting the vm or dictionary.
31 //it's kind of the job of whatever includes EPS to do this, but this makes
32 //the job easier and improves reliability. Note that in Level 2 ps, the
33 //dictionary can expand if more than 20 commands are added later.
34 //If custom PS is added, it must leave the stack empty at the end.
35 "save"
36 "\n"
37 "20 dict begin"
38 "\n";
39 }
40
41 void output_eps_header(ostream& o, const ImageRef& s)
42 {
43 output_eps_header(o, s.x, s.y);
44 }
45
46 void output_eps_footer(ostream& o)
47 {
48 //End the temporary dictionary and restore the VM and graphics state.
49 o << "end restore\n"
50 //I'm not sure exactly what the end of an EPS image should be, but this
51 //seems to work.
52 << "%%EOF\n";
53 }
54
55 namespace PS
56 {
57
58 class WritePimpl
59 {
60 public:
61 WritePimpl(std::ostream&, ImageRef size, const string& type, const string& extra_header = "");
62
63 void write_raw_pixel_lines(const unsigned char*, int);
64 ~WritePimpl();
65 int channels() { return m_channels; }
66 long x_size() const { return xs; }
67 long y_size() const { return ys; }
68
69 std::ostream& o;
70
71 private:
72 int xs, ys;
73 int m_channels;
74 std::string bytes_to_base85(int n);
75 void output_header();
76 int lines;
77 unsigned char buf[4];
78 int num_in_buf;
79 string currentline;
80 };
81
82 string WritePimpl::bytes_to_base85(int n)
83 {
84 //This function converts 4 raw bytes to 5 base-85 bytes. If there
85 //are less than 4 bytes, then the data is zero padded.
86
87 //Conversion to base85 is done the obvious way, but 33 is added
88 //to each number to make it a printable byte.
89
90 //There is a shortcut which means that !!!!! is abbreviated as z
91
92 unsigned long int num = 0;
93
94 for(int i = 0; i < n; i++)
95 num |= ((unsigned long)buf[i]) << ((3 - i) * 8);
96
97 if(num == 0)
98 return "z";
99
100 string r;
101 r.resize(5);
102
103 for(int i = 0; i < 5; i++)
104 {
105 r[4 - i] = (char)(num % 85 + 33);
106 num /= 85;
107 }
108
109 return r;
110 }
111
112 void WritePimpl::output_header()
113 {
114 //Output the required instructions. This sets up a 72 dpi image, the ``right'' way up,
115 //going from 0,0 - xs, ys. Since numbers are 72 per inch, the image is 72 dpi
116 o << " 0 " << ys << " translate 1 -1 scale " << xs << " " << ys << " 8 matrix currentfile\n"
117 " /ASCII85Decode filter false "
118 << m_channels << " colorimage\n";
119
120 // <xsize> <ysize> <bits per channel> <transform> <datasrc n> ... <datasrc 1> <multi> <num of channels> colorimage
121 // transform is an affine transformation matrix. We use the identity here,
122 // and preform the the transform before the image, so that any subsequent
123 // graphics appear in the coordinate frame of the image.
124 }
125
126 WritePimpl::WritePimpl(std::ostream& out, ImageRef size, const string& type, const string& extra_header)
127 : o(out)
128 {
129 xs = size.x;
130 ys = size.y;
131 num_in_buf = lines = 0;
132
133 if(type == "unsigned char")
134 m_channels = 1;
135 else if(type == "CVD::Rgb<unsigned char>")
136 m_channels = 3;
137 else
138 throw Exceptions::Image_IO::UnsupportedImageSubType("PS", type);
139
140 out << extra_header;
141 output_header();
142 currentline.reserve(80);
143 }
144
145 WritePimpl::~WritePimpl()
146 {
147 //Is there anything left in the buffer?
148 if(num_in_buf != 0)
149 currentline += bytes_to_base85(num_in_buf);
150
151 //Output if there is anything left
152 if(currentline.size())
153 o << currentline << endl;
154
155 //Delimit the data
156 if(lines == ys)
157 o << "~>\n";
158 }
159
160 void WritePimpl::write_raw_pixel_lines(const unsigned char* data, int nlines)
161 {
162 if(nlines + lines > ys)
163 throw CVD::Exceptions::Image_IO::InternalLibraryError("CVD", "Write past end of image.");
164
165 const unsigned char* end = data + (nlines * xs) * m_channels;
166 const unsigned char* d = data;
167
168 for(; d < end; d++)
169 {
170 //Stuff 4 bytes in to the buffer
171 buf[num_in_buf++] = *d;
172
173 //Stuff the processed bytes in to a string, if need be
174 if(num_in_buf == 4)
175 {
176 currentline += bytes_to_base85(num_in_buf);
177 num_in_buf = 0;
178 }
179
180 //Output the line of it is wide enough
181 if(currentline.size() >= 75)
182 {
183 o << currentline << endl;
184 currentline.resize(0);
185 }
186 }
187
188 lines += nlines;
189 }
190
191 ////////////////////////////////////////////////////////////////////////////////
192 //
193 // Public interfaces to image writing.
194 //
195
196 writer::writer(ostream& o, ImageRef size, const string& s, const map<string, Parameter<>>&)
197 : t(new WritePimpl(o, size, s))
198 {
199 }
200
201 writer::~writer()
202 {
203 }
204
205 void writer::write_raw_pixel_line(const byte* data)
206 {
207 t->write_raw_pixel_lines(data, 1);
208 }
209
210 void writer::write_raw_pixel_line(const Rgb<byte>* data)
211 {
212 t->write_raw_pixel_lines((byte*)data, 1);
213 }
214
215 string eps_header(ImageRef s)
216 {
217 ostringstream os;
218 output_eps_header(os, s);
219 return os.str();
220 }
221
222 eps_writer::eps_writer(ostream& o, ImageRef size, const string& s, const map<string, Parameter<>>&)
223 : t(new WritePimpl(o, size, s, eps_header(size)))
224 {
225 }
226
227 eps_writer::~eps_writer()
228 {
229 ostream& o = t->o;
230 //Destruct the PS writer to print the footer.,
231 t.reset();
232 output_eps_footer(o);
233 }
234
235 void eps_writer::write_raw_pixel_line(const byte* data)
236 {
237 t->write_raw_pixel_lines(data, 1);
238 }
239
240 void eps_writer::write_raw_pixel_line(const Rgb<byte>* data)
241 {
242 t->write_raw_pixel_lines((byte*)data, 1);
243 }
244
245 }
246 }