| <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" |
| "http://www.w3.org/TR/html4/loose.dtd"> |
| <html > |
| <head><title>Introduction</title> |
| <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
| <meta name="generator" content="TeX4ht (http://www.cse.ohio-state.edu/~gurari/TeX4ht/)"> |
| <meta name="originator" content="TeX4ht (http://www.cse.ohio-state.edu/~gurari/TeX4ht/)"> |
| <!-- 3,html --> |
| <meta name="src" content="vipsmanual.tex"> |
| <meta name="date" content="2010-06-09 21:39:00"> |
| <link rel="stylesheet" type="text/css" href="vipsmanual.css"> |
| </head><body |
| > |
| <!--l. 1--><div class="crosslinks"><p class="noindent">[<a |
| href="vipsmanualse2.html" >next</a>] [<a |
| href="#tailvipsmanualse1.html">tail</a>] [<a |
| href="vipsmanualch1.html#vipsmanualse1.html" >up</a>] </p></div> |
| <h3 class="sectionHead"><span class="titlemark">1.1 </span> <a |
| id="x6-50001.1"></a>Introduction</h3> |
| <!--l. 2--><p class="noindent" ><a name="nip_label_sec:cpp"></a> |
| <!--l. 4--><p class="indent" > This chapter describes the C++ API for the VIPS image |
| processing library. The C++ API is as efficient as the C |
| interface to VIPS, but is far easier to use: almost all |
| creation, destruction and error handling issues are handled |
| for you automatically. <!--l. 9--><p class="indent" > The Python interface is a very simple wrapping of |
| this C++ API generated automatically with SWIG. It |
| adds a few utility methods noted below, but otherwise |
| the two interfaces are identical other than language |
| syntax. |
| <!--l. 14--><p class="noindent" > |
| <h4 class="subsectionHead"><span class="titlemark">1.1.1 </span> <a |
| id="x6-60001.1.1"></a>If you’ve used the C API</h4> |
| <!--l. 16--><p class="noindent" >To show how much easier the VIPS C++ API is to use, |
| compare Figure <a |
| href="vipsmanualse8.html#x14-420002.2.5">2.2.5<!--tex4ht:ref: fg:negative --></a> to Figure <a |
| href="#x6-60011">1.1<!--tex4ht:ref: fg:invert-c++ --></a>. Figure <a |
| href="#x6-60022">1.2<!--tex4ht:ref: fg:invert-py --></a> is the same |
| thing in Python. |
| <!--l. 19--><p class="indent" > A typical build line for the C++ program might be: |
| |
| |
| |
| <div class="verbatim" id="verbatim-1"> |
| g++ invert.cc \ |
|  <br />  ‘pkg-config vipsCC-7.18 \ |
|  <br />    --cflags --libs‘ |
| </div> |
| <!--l. 25--><p class="nopar" > |
| <!--l. 27--><p class="indent" > The key points are: |
| <ul class="itemize1"> |
| <li class="itemize">You just include <span class="obeylines-h"><span class="verb"><span |
| class="pcrr7t-"><vips/vips></span></span></span> — this then gets |
| all of the other includes you need. Everything is in |
| the <span class="obeylines-h"><span class="verb"><span |
| class="pcrr7t-">vips</span></span></span> namespace. |
| </li> |
| <li class="itemize">The C++ API replaces all of the VIPS C types — |
| <span class="obeylines-h"><span class="verb"><span |
| class="pcrr7t-">IMAGE</span></span></span> becomes <span class="obeylines-h"><span class="verb"><span |
| class="pcrr7t-">VImage</span></span></span> and so on. The C++ API |
| also includes <span class="obeylines-h"><span class="verb"><span |
| class="pcrr7t-">VDisplay</span></span></span>, <span class="obeylines-h"><span class="verb"><span |
| class="pcrr7t-">VMask</span></span></span> and <span class="obeylines-h"><span class="verb"><span |
| class="pcrr7t-">VError</span></span></span>. |
| </li> |
| <li class="itemize">Image processing operations are member functions |
| of the <span class="obeylines-h"><span class="verb"><span |
| class="pcrr7t-">VImage</span></span></span> |
| class — here, <span class="obeylines-h"><span class="verb"><span |
| class="pcrr7t-">VImage(</span><span |
| class="pcrr7t-"> argv[1]</span><span |
| class="pcrr7t-"> )</span></span></span> creates a |
| new <span class="obeylines-h"><span class="verb"><span |
| class="pcrr7t-">VImage</span></span></span> object using the first argument to |
| initialise it (the input filename). It then calls the |
| member function <span class="obeylines-h"><span class="verb"><span |
| class="pcrr7t-">invert()</span></span></span>, which inverts the |
| <span class="obeylines-h"><span class="verb"><span |
| class="pcrr7t-">VImage</span></span></span> and returns a new <span class="obeylines-h"><span class="verb"><span |
| class="pcrr7t-">VImage</span></span></span>. Finally it |
| calls the member function <span class="obeylines-h"><span class="verb"><span |
| class="pcrr7t-">write()</span></span></span>, which writes |
| the result image to the named file. |
| </li> |
| <li class="itemize">The VIPS C++ API uses exceptions — the |
| <span class="obeylines-h"><span class="verb"><span |
| class="pcrr7t-">VError</span></span></span> class is covered later. If you run this |
| program with a bad input file, for example, you get |
| the following output: |
| <div class="verbatim" id="verbatim-2"> |
| $ invert jim fred |
|  <br />invert: VIPS error: format_for_file: |
|  <br />  file "jim" not found |
| </div> |
| <!--l. 57--><p class="nopar" > |
| </li></ul> |
| |
| |
| |
| <!--l. 62--><p class="indent" > <a |
| id="x6-60011"></a><hr class="float"><div class="float" |
| > |
| |
| |
| |
| |
| <div class="verbatim" id="verbatim-3"> |
| #include <iostream> |
|  <br />#include <vips/vips> |
|  <br /> |
|  <br />int |
|  <br />main (int argc, char ⋆⋆argv) |
|  <br />{ |
|  <br />  if (argc != 3) |
|  <br />    { |
|  <br />      std::cerr << "usage: " << argv[0] << " infile outfile\n"; |
|  <br />      return (1); |
|  <br />    } |
|  <br /> |
|  <br />  try |
|  <br />  { |
|  <br />    vips::VImage fred (argv[1]); |
|  <br /> |
|  <br />    fred.invert ().write (argv[2]); |
|  <br />  } |
|  <br />  catch (vips::VError e) |
|  <br />  { |
|  <br />    e.perror (argv[0]); |
|  <br />  } |
|  <br /> |
|  <br />  return (0); |
|  <br />} |
| </div> |
| <!--l. 88--><p class="nopar" > |
| <br /><div class="caption" |
| ><span class="id">Figure 1.1: </span><span |
| class="content"><span |
| class="pcrr7t-">invert </span>program in C++</span></div><!--tex4ht:label?: x6-60011 --> |
| |
| |
| |
| |
| </div><hr class="endfloat" /> |
| |
| |
| |
| |
| <!--l. 94--><p class="indent" > <a |
| id="x6-60022"></a><hr class="float"><div class="float" |
| > |
| |
| |
| |
| |
| <div class="verbatim" id="verbatim-4"> |
| #!/usr/bin/python |
|  <br /> |
|  <br />import sys |
|  <br />from vipsCC import ⋆ |
|  <br /> |
|  <br />try: |
|  <br />  a = VImage.VImage (sys.argv[1]) |
|  <br />  a.invert ().write (sys.argv[2]) |
|  <br />except VError.VError, e: |
|  <br />  e.perror (sys.argv[0]) |
| </div> |
| <!--l. 105--><p class="nopar" > |
| <br /><div class="caption" |
| ><span class="id">Figure 1.2: </span><span |
| class="content"><span |
| class="pcrr7t-">invert </span>program in Python</span></div><!--tex4ht:label?: x6-60022 --> |
| |
| |
| |
| |
| </div><hr class="endfloat" /> |
| |
| |
| |
| |
| <!--l. 1--><div class="crosslinks"><p class="noindent">[<a |
| href="vipsmanualse2.html" >next</a>] [<a |
| href="vipsmanualse1.html" >front</a>] [<a |
| href="vipsmanualch1.html#vipsmanualse1.html" >up</a>] </p></div> |
| <!--l. 1--><p class="indent" > <a |
| id="tailvipsmanualse1.html"></a> |
| </body></html> |