<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html > <head><title>Programming in-place functions</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="2009-08-06 15:11:00"> <link rel="stylesheet" type="text/css" href="vipsmanual.css"> </head><body > <!--l. 1--><div class="crosslinks"><p class="noindent">[<a href="vipsmanualse15.html" >prev</a>] [<a href="vipsmanualse15.html#tailvipsmanualse15.html" >prev-tail</a>] [<a href="#tailvipsmanualse16.html">tail</a>] [<a href="vipsmanualch3.html#vipsmanualse16.html" >up</a>] </p></div> <h3 class="sectionHead"><span class="titlemark">3.4 </span> <a id="x23-1030003.4"></a>Programming in-place functions</h3> <!--l. 3--><p class="noindent" >VIPS includes a little support for in-place functions — functions which operate directly on an image, both reading and writing from the same descriptor via the data pointer. This is an extremely dangerous way to handle IO, since any bugs in your program will trash your input image. <!--l. 8--><p class="indent" > Operations of this type should call <span class="obeylines-h"><span class="verb"><span class="pcrr7t-">im_rwcheck()</span></span></span> instead of <span class="obeylines-h"><span class="verb"><span class="pcrr7t-">im_incheck()</span></span></span>. <span class="obeylines-h"><span class="verb"><span class="pcrr7t-">im_rwcheck()</span></span></span> tries to get a descriptor ready for in-place writing. For example, a function which cleared an image to black might be written as: <div class="verbatim"> #include <stdio.h>  <br />#include <memory.h>  <br />  <br />#include <vips/vips.h>  <br />  <br />int  <br />black_inplace( IMAGE ⋆im )  <br />{  <br />   /⋆ Check that we can RW to im.  <br />    ⋆/  <br />   if( im_rwcheck( im ) )  <br />      return( -1 );  <br />  <br />   /⋆ Zap the image!  <br />    ⋆/  <br />   memset( im->data, 0,  <br />      IM_IMAGE_SIZEOF_LINE( im ) ⋆  <br />      im->Ysize );  <br />  <br />   return( 0 );  <br />} </div> <!--l. 35--><p class="nopar" > <!--l. 37--><p class="indent" > This function might be called from an application as: <div class="verbatim"> #include <stdio.h>  <br />#include <stdlib.h>  <br />  <br />#include <vips/vips.h>  <br />  <br />void  <br />zap( char ⋆name )  <br />{  <br />   IMAGE ⋆im;  <br />  <br />   if( !(im = im_open( name, "rw" )) ||  <br />      black_inplace( im ) ||  <br />      im_updatehist( im, "zap image" ) ||  <br />      im_close( im ) )  <br />      error_exit( "failure!" );  <br />} </div> <!--l. 56--><p class="nopar" > <!--l. 86--><div class="crosslinks"><p class="noindent">[<a href="vipsmanualse15.html" >prev</a>] [<a href="vipsmanualse15.html#tailvipsmanualse15.html" >prev-tail</a>] [<a href="vipsmanualse16.html" >front</a>] [<a href="vipsmanualch3.html#vipsmanualse16.html" >up</a>] </p></div> <!--l. 86--><p class="indent" > <a id="tailvipsmanualse16.html"></a> </body></html>