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
|
#include "mbl_eps_writer.h"
//:
// \file
// \brief Class to generate simple EPS files containing images and lines
// \author Tim Cootes
#include <vil/vil_plane.h>
#include <vil/vil_crop.h>
#include <vcl_algorithm.h>
#include <vcl_cassert.h>
//=======================================================================
// Dflt ctor
//=======================================================================
mbl_eps_writer::mbl_eps_writer()
: nx_(100.0),ny_(100.0),sx_(1.0),sy_(1.0)
{
}
//: Open file and write header, given bounding box
// Bounding box specified in "points" (72 points=1 inch)
mbl_eps_writer::mbl_eps_writer(const char* path, double nx, double ny)
: sx_(1.0),sy_(1.0)
{
open(path,nx,ny);
}
//=======================================================================
// Destructor
//=======================================================================
mbl_eps_writer::~mbl_eps_writer()
{
}
//: Define shade of subsequent lines [0,1] = black-white
void mbl_eps_writer::set_grey_shade(double shade)
{
if (shade<0.0) shade=0.0;
if (shade>1.0) shade=1.0;
ofs_<<shade<<" setgray\n";
}
//: Define colour of subsequent lines r,g,b in [0,1]
void mbl_eps_writer::set_rgb(double r, double g, double b)
{
ofs_<<r<<' '<<g<<' '<<b<<" setrgbcolor\n";
}
//: Set colour of subsequent graphics using a named colour
// Valid options include black,white,grey,red,green,blue,
// cyan,yellow. Note: Probably need a tidy map thing to
// do this properly. Sets to grey if colour not known.
void mbl_eps_writer::set_colour(const vcl_string& colour_name)
{
if (colour_name=="black") { set_grey_shade(0.0); return; }
if (colour_name=="white") { set_grey_shade(1.0); return; }
if (colour_name=="grey") { set_grey_shade(0.5); return; }
if (colour_name=="red") { set_rgb(1,0,0); return; }
if (colour_name=="green") { set_rgb(0,1,0); return; }
if (colour_name=="blue") { set_rgb(0,0,1); return; }
if (colour_name=="cyan") { set_rgb(0,1,1); return; }
if (colour_name=="yellow") { set_rgb(1,1,0); return; }
// Colour not recognised, so set to grey.
set_grey_shade(0.5);
}
//: Define scaling factor
void mbl_eps_writer::set_scaling(double s)
{
sx_=s; sy_=s;
}
//: Define scaling factor
void mbl_eps_writer::set_scaling(double sx, double sy)
{
sx_=sx; sy_=sy;
}
//: Open file and write header
bool mbl_eps_writer::open(const char* path, double nx, double ny)
{
ofs_.open(path,vcl_ios_out);
if (!ofs_) return false;
ofs_<<"%!PS-Adobe-1.0\n"
<<"%%Creator: mbl_eps_writer\n"
// <<"%%Title: shapes\n"
<<"%%BoundingBox: 0 0 "<<nx<<' '<<ny<<'\n'
<<"gsave\n"
<<"% Define some simple macros to save space\n"
<<"/M {moveto} def\n"
<<"/L {lineto} def\n\n"
<<"/CP {closepath} def\n\n";
nx_ = nx; ny_=ny;
return true;
}
//: Creates file and draws image, setting bounding box to that of image
// Sets scaling to pixel widths, so that subsequent points are
// interpreted in pixel units.
bool mbl_eps_writer::open(const char* path,
const vil_image_view<vxl_byte>& image,
double pixel_width_x, double pixel_width_y)
{
if (!open(path,image.ni()*pixel_width_x,image.nj()*pixel_width_y))
return false;
set_scaling(pixel_width_x,pixel_width_y);
draw_image(image,0,0,1,1);
return true;
}
//: Write tail and close
void mbl_eps_writer::close()
{
ofs_<<"grestore\n";
ofs_.close();
}
//: Define line width.
void mbl_eps_writer::set_line_width(double w)
{
ofs_<<w<<" setlinewidth\n";
}
//: Draws circle of radius r around p
void mbl_eps_writer::draw_circle(const vgl_point_2d<double>& p, double r)
{
ofs_<<"newpath\n\n"
<<sx_*p.x()<<' '<<ny_-sy_*p.y()<<' '<<sx_*r<<" 0 360 arc CP\n\n"
<<"stroke"<<vcl_endl;
}
//: Draws disk of radius r around p
void mbl_eps_writer::draw_disk(const vgl_point_2d<double>& p, double r)
{
ofs_<<"newpath\n"
<<sx_*p.x()<<' '<<ny_-sy_*p.y()<<' '<<sx_*r<<" 0 360 arc CP fill\n"
<<"stroke"<<vcl_endl;
}
//: Draws line segment from p1 to p2
void mbl_eps_writer::draw_line(const vgl_point_2d<double>& p1, const vgl_point_2d<double>& p2)
{
ofs_<<"newpath\n"
<<sx_*p1.x()<<' '<<ny_-sy_*p1.y()<<" M "
<<sx_*p2.x()<<' '<<ny_-sy_*p2.y()<<" L\n"
<<"stroke\n";
}
//: Draws polygon connecting points.
// If closed, then adds line joining last to first point.
// If filled, then fills with current colour/greyshade.
void mbl_eps_writer::draw_polygon(const vcl_vector<vgl_point_2d<double> >& pts,
bool closed, bool filled)
{
if (pts.size()<2) return;
ofs_<<"newpath\n"
<<sx_*pts[0].x()<<' '<<ny_-sy_*pts[0].y()<<" M ";
for (unsigned i=1;i<pts.size();++i)
ofs_<<sx_*pts[i].x()<<' '<<ny_-sy_*pts[i].y()<<" L\n";
if (closed)
{
ofs_<<sx_*pts[0].x()<<' '<<ny_-sy_*pts[0].y()<<" L CP\n";
}
if (filled) ofs_<<"fill ";
ofs_<<"stroke\n";
}
//: Writes first plane of image in hex format to os
void mbl_eps_writer::write_image_data(vcl_ostream& os,
const vil_image_view<vxl_byte>& image)
{
unsigned ni=image.ni(),nj=image.nj();
ofs_<<"{<";
for (unsigned j=0;j<nj;++j)
{
// Write each row
for (unsigned i=0;i<ni;++i)
{
ofs_<<vcl_hex<<int(image(i,j))/16<<int(image(i,j))%16;
}
ofs_<<vcl_endl;
}
ofs_<<">}\n"
<<vcl_dec; // Ensure returns to decimal
}
void mbl_eps_writer::draw_image(const vil_image_view<vxl_byte>& image,
double tx, double ty,
double pixel_width_x, double pixel_width_y)
{
unsigned ni=image.ni(),nj=image.nj();
if (ni<256 && nj<256)
{
draw_image_block(image,tx,ty,pixel_width_x,pixel_width_y);
return;
}
// Split into smaller blocks
unsigned max_w = 200;
unsigned ni_blocks = 1+(ni-1)/max_w;
unsigned nj_blocks = 1+(nj-1)/max_w;
for (unsigned j=0;j<nj_blocks;++j)
for (unsigned i=0;i<ni_blocks;++i)
{
unsigned wi=vcl_min(max_w,ni-(i*max_w));
unsigned wj=vcl_min(max_w,nj-(j*max_w));
vil_image_view<vxl_byte> cropped=vil_crop(image,i*max_w,wi,j*max_w,wj);
double tx1=tx+i*max_w*pixel_width_x;
double ty1=ty+j*max_w*pixel_width_y;
draw_image_block(cropped,tx1,ty1,pixel_width_x,pixel_width_y);
}
}
//: Creates a greyscale image at (tx,ty) with given pixel widths
// Size in points given by sx(),sy() * given values.
// Image must be no bigger than 255x255
// Some postscript can't cope with bigger blocks.
void mbl_eps_writer::draw_grey_image_block(
const vil_image_view<vxl_byte>& image,
double tx, double ty,
double pixel_width_x, double pixel_width_y)
{
assert(image.ni()<256);
assert(image.nj()<256);
ofs_<<"gsave\n"
<<sx_*tx<<' '<<ny_-sy_*ty<<" translate\n";
unsigned ni=image.ni(),nj=image.nj();
ofs_<<sx_*ni*pixel_width_x<<" -"<<sy_*nj*pixel_width_y<<" scale\n"
<<ni<<' '<<nj<<" % Image size\n"
<<"8 % Bits per pixel\n"
<<"[ "<<ni<<" 0 0 "<<nj<<" 0 0]\n"; // Transformation (unit sqr to pixels)
// Now draw the image data
write_image_data(ofs_,image);
ofs_<<"image\n"
<<"grestore\n";
}
//: Creates a colour image with given pixel widths
// Image assumed to be a 3 plane image.
void mbl_eps_writer::draw_rgb_image_block(
const vil_image_view<vxl_byte>& image,
double tx, double ty,
double pixel_width_x, double pixel_width_y)
{
assert(image.ni()<256);
assert(image.nj()<256);
ofs_<<"gsave\n"
<<sx_*tx<<' '<<ny_-sy_*ty<<" translate\n";
unsigned ni=image.ni(),nj=image.nj();
ofs_<<sx_*ni*pixel_width_x<<" -"<<sy_*nj*pixel_width_y<<" scale\n"
<<ni<<' '<<nj<<" % Image size\n"
<<"8 % Bits per pixel\n"
<<"[ "<<ni<<" 0 0 "<<nj<<" 0 0]\n"; // Transformation (unit sqr to pixels)
// Now draw the image data
write_image_data(ofs_,vil_plane(image,0)); // Red
write_image_data(ofs_,vil_plane(image,1)); // Green
write_image_data(ofs_,vil_plane(image,2)); // Blue
ofs_<<"true 3 colorimage\n"
<<"grestore\n";
}
//: Creates an image at (tx,ty) with given pixel widths
// Size in points given by sx(),sy() * given values.
void mbl_eps_writer::draw_image_block(const vil_image_view<vxl_byte>& image,
double tx, double ty,
double pixel_width_x, double pixel_width_y)
{
if (image.nplanes()==1)
draw_grey_image_block(image,tx,ty,pixel_width_x,pixel_width_y);
else
if (image.nplanes()==3)
draw_rgb_image_block(image,tx,ty,pixel_width_x,pixel_width_y);
else
vcl_cerr<<"mbl_eps_writer::draw_image_block() Can't cope with image."<<vcl_endl;
}
|