help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C/C++ Interface to Image Magick libs.


From: adler
Subject: Re: C/C++ Interface to Image Magick libs.
Date: Tue, 24 Apr 2001 22:29:26 -0400 (EDT)

On Mon, 23 Apr 2001, Hayram Nicacio wrote:
>       I?ve already taken a look at the mailing list archives
> and I haven?t found any references on how to write C/C++ oct
> files. Is there any kind of tutorial or reference? The ideia is
> to write an oct file that uses the Image Magick libs
> (http://www.imagemagick.org) so octave could read/write any kind
> of image file.

ImageMagick works well with octave. I'm including some sample
code for reading and writing images:

INSTRUCTIONS (for Redhat linux 6.2):

1. Install These RPMS

ImageMagick-5.2.9-2
ImageMagick-Magick++-5.2.9-2
ImageMagick-devel-5.2.9-2
ImageMagick-perl-5.2.9-2

2. Compile the code this way

mkoctfile -I/usr/include/magick/ imw.cc `Magick++-config --ldflags --libs`


NOTES:

1. This code does _no_ error checking
2. This code is not particularly efficient
3. Works for RGB images, components are 0-1


DISCUSSION:

ImageMagick is a really powerful image library, and it
would be great to have a good link to octave. This would
allow images to be displayed without writing to files, and
modifications made in the GUI could be imported back into
octave, as well as many other great things ...

I've been thinking about doing this for a while, but the 
ImageMagick API changes regularly. The change from 4.x
to 5.x was (understandably) large, but even in the 5.2.x
series the API is not stable 8-(

I remember that octave used to compile without exception
support - that seems to have changed, so the Magick++
API works well in octave now.
_______________________________________
Andy Adler,                address@hidden


-----------read images-------------

#include <Magick++.h> 
#include <octave/oct.h>

DEFUN_DLD (imread, args, , "read iamges with imagemagick")
{
   octave_value_list retval;
   try { 
      string imgname= args(0).string_value();
      Magick::Image img( imgname );
      unsigned int cols= img.columns();
      unsigned int rows= img.rows();
      Matrix im_r( cols, rows);
      Matrix im_g( cols, rows);
      Matrix im_b( cols, rows);
      img.write( 0,0,cols,rows,"R",MagickLib::DoublePixel, im_r.fortran_vec() );
      img.write( 0,0,cols,rows,"G",MagickLib::DoublePixel, im_g.fortran_vec() );
      img.write( 0,0,cols,rows,"B",MagickLib::DoublePixel, im_b.fortran_vec() );
      retval(0)= im_r.transpose();
      retval(1)= im_g.transpose();
      retval(2)= im_b.transpose();
   } 
   catch( Magick::Exception &error_ ) 
   { 
      cout << "Caught exception: " << error_.what() << endl; 
   } 
   return retval;
}



-----------write images-------------

#include <Magick++.h> 
#include <octave/oct.h>

DEFUN_DLD (imwrite, args, , "write images with imagemagick")
{
   string imgname= args(0).string_value();
   Matrix im_r= args(1).matrix_value();
   Matrix im_g= args(2).matrix_value();
   Matrix im_b= args(3).matrix_value();
   int cols= im_r.columns();
   int rows= im_r.rows();

   unsigned char pixels[ 3*rows*cols ];
   unsigned char *pix= pixels;
   for (int i=0; i<rows; i++ )
     for (int j=0; j<cols; j++ ) {
        *pix++ = (unsigned char) (im_r(i,j)*255); 
        *pix++ = (unsigned char) (im_g(i,j)*255); 
        *pix++ = (unsigned char) (im_b(i,j)*255); 
     }
   Magick::Image img( cols, rows, "RGB", MagickLib::CharPixel, pixels);
   img.write( imgname );

   octave_value_list retval;
   return retval;
}




-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]