BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/ImageIO.h
Go to the documentation of this file.
00001 // Copyright (c) 2005, 2006 ASCLEPIOS Project, INRIA Sophia-Antipolis (France)
00002 // Copyright (c) 2007 Geometrica Project, INRIA Sophia-Antipolis (France) 
00003 // Copyright (c) 2008 GeometryFactory, Sophia-Antipolis (France) 
00004 // All rights reserved.
00005 //
00006 // The files in this directory are part of the ImageIO Library.
00007 // You can redistribute them and/or  modify them under the terms of the
00008 // GNU Lesser General Public License as published by the Free Software Foundation;
00009 // version 2.1 of the License.
00010 // See the file LICENSE.LGPL distributed with CGAL.
00011 //
00012 // Licensees holding a valid commercial license may use this file in
00013 // accordance with the commercial license agreement provided with the software.
00014 //
00015 // These files are provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/CGALimageIO/include/CGAL/ImageIO.h $
00018 // $Id: ImageIO.h 46133 2008-10-07 13:00:59Z lrineau $
00019 //
00020 
00021 #ifndef IMAGEIO_H
00022 #define IMAGEIO_H
00023 
00024 #include <CGAL/compiler_config.h>
00025 
00026 #include <CGAL/auto_link/ImageIO.h>
00027 
00028 #include <stdlib.h>
00029 #include <stdio.h>
00030 #include <boost/cstdint.hpp> // for uint32_t, etc.
00031 
00032 #ifdef CGAL_USE_ZLIB
00033 #include <zlib.h>
00034 /* see http://www.gzip.org/zlib/
00035    for details and documentation
00036 */
00037 #endif
00038 
00039 
00040 
00041 #if (defined(_LINUX_) || defined(_SOLARIS_))
00042 
00043 /* should be declared in stdio.h
00044  */
00045 extern int fileno( FILE *stream);
00046 extern FILE *fdopen (int fildes, const char *mode);
00047 /* should be declared in string.h
00048  */
00049 #ifndef __cplusplus
00050 extern char *strdup(const char *s);
00051 extern int strncasecmp(const char *s1, const char *s2, size_t n);
00052 #endif
00053 
00054 #endif
00055 
00056 
00057 
00058 
00059 
00060 #ifndef LONGINT
00061 
00062 #if (defined _ALPHA_ || (defined _SGI_ && (defined _64_ || defined _64_M4_ || defined _64_M3_)))
00063 /* the 64-bits type on 64-bits platform (long int) */
00064 #define LONGINT long  int
00065 #else
00066 #ifdef __GNUC__
00067 /* the 64-bits type on 32-bits platform (long long int) */
00068 #define LONGINT long long int
00069 #else
00070 /*#define LONGINT __int64 */
00071 #define LONGINT long int
00072 #endif
00073 #endif
00074 
00075 #endif
00076 
00077 
00078 
00079 
00080 
00081 
00082 
00083 
00085 typedef enum {
00087   OM_CLOSE,
00089   OM_STD,
00091 #ifdef CGAL_USE_ZLIB
00092   OM_GZ,
00093 #endif
00094 
00095   OM_FILE
00096 } OPEN_MODE;
00097 
00098 
00100 typedef enum {
00102   DM_BINARY,
00104   DM_ASCII
00105 } DATA_MODE;
00106 
00107 
00109 typedef enum {
00111   WK_FIXED,
00113   WK_FLOAT,
00115   WK_UNKNOWN
00116 } WORD_KIND;
00117 
00118 
00120 typedef enum {
00122   SGN_SIGNED,
00124   SGN_UNSIGNED,
00126   SGN_UNKNOWN
00127 } SIGN;
00128 
00129 
00131 typedef enum {
00133   END_LITTLE,
00135   END_BIG,
00137   END_UNKNOWN
00138 } ENDIANNESS;
00139 
00140 
00142 typedef enum {
00144   VM_INTERLACED,
00146   VM_NON_INTERLACED,
00148   VM_SCALAR
00149 } VECTORIAL_MODE;
00150 
00151 
00152 
00153 #ifdef CGAL_USE_ZLIB
00154 typedef gzFile _ImageIO_file;
00155 #else
00156 typedef FILE*  _ImageIO_file;
00157 #endif
00158 
00159 #define IMAGE_FORMAT_NAME_LENGTH 100
00160 
00161 
00162 struct point_image;
00163 
00169 typedef int (*TEST_IMAGE_FORMAT)(char *,const char *);
00176 typedef int (*READ_IMAGE_HEADER)(const char *, struct point_image *);
00184 typedef int (*WRITE_IMAGE)(char *,struct point_image *);
00185 
00186 
00188 typedef struct imformat {
00189 
00191   TEST_IMAGE_FORMAT testImageFormat;
00192 
00194   READ_IMAGE_HEADER readImageHeader;
00195 
00198   WRITE_IMAGE writeImage;
00199 
00200   /* the file extension of format (including a dot ".": if several 
00201      extensions may be used, they should be separed with a 
00202      comma ".inr,.inr.gz" */
00203   char fileExtension[IMAGE_FORMAT_NAME_LENGTH];
00204   
00206   char realName[IMAGE_FORMAT_NAME_LENGTH];
00207   /* pointer towards the next image format*/
00208   struct imformat *next;
00209 } IMAGE_FORMAT, *PTRIMAGE_FORMAT;;
00210 
00212 typedef struct point_image {
00214   unsigned int xdim;
00216   unsigned int ydim;
00218   unsigned int zdim;
00220   unsigned int vdim;
00221 
00223   double vx;
00225   double vy;
00227   double vz;
00228 
00230   float tx;
00232   float ty;
00234   float tz;
00235 
00237   float rx;
00239   float ry;
00241   float rz;
00242 
00244   int cx;
00246   int cy;
00248   int cz;
00249 
00251   float spm_offset;
00252   float spm_scale;
00253 
00255   void *data;
00256 
00258   unsigned int wdim;
00260   PTRIMAGE_FORMAT imageFormat;
00262   VECTORIAL_MODE vectMode;
00264   WORD_KIND wordKind;
00266   SIGN sign;
00267 
00270   char **user;
00272   unsigned int nuser;
00273 
00275   _ImageIO_file fd;
00276 
00277 
00279   OPEN_MODE openMode;
00281   ENDIANNESS endianness;
00283   DATA_MODE dataMode;
00284 
00285 } _image;
00286 
00287 
00288 
00290 #define ImageIO_NO_ERROR 0
00291 #define ImageIO_UNKNOWN_TYPE -1
00292 #define ImageIO_OPENING -2
00293 #define ImageIO_READING_HEADER -3
00294 #define ImageIO_READING_IMAGE -4
00295 #define ImageIO_WRITING_HEADER -3
00296 #define ImageIO_WRITING_IMAGE -4
00297 #define ImageIO_WRITING_DATA  -5
00298 
00299 
00300 
00301 
00302 
00304 _image *_initImage();
00305 
00308 void _freeImage(_image *im);
00309 
00321 _image *_createImage(int x, int y, int z, int v,
00322                      float vx, float vy, float vz, int w,
00323                      WORD_KIND wk, SIGN sgn);
00324 
00325 
00357 _image* _readImage(const char *name);
00358 
00375 _image* _readNonInterlacedImage(const char *name);
00376 
00379 _image* _readImage_raw(const char *name,
00380                        const unsigned int rx,
00381                        const unsigned int ry,
00382                        const unsigned int rz,
00383                        const double vx = 1.,
00384                        const double vy = 1.,
00385                        const double vz = 1.,
00386                        const unsigned int offset = 0);
00387 
00388 
00394 int _writeImage(_image *im, const char *name);
00395 
00402 void _getNextSlice(_image *im);
00403 
00404 
00409 int addImageFormat( PTRIMAGE_FORMAT format);
00410 
00411 
00413 PTRIMAGE_FORMAT firstImageFormat();
00414 
00420 _image* _readImageHeader(const char *name);
00421 _image *_readImageHeaderAndGetError( const char *name, int *error );
00422 
00426 int _readImageData(_image *im);
00427 
00432 int _readNonInterlacedImageData(_image *im);
00433 
00437 int _readNonInterlacedFileData(_image *im);
00438 
00439 
00440 
00441 
00447 void _openWriteImage(_image* im, const char *name) ;
00448    
00453 void _openReadImage(_image *im, const char *name);
00454 
00457 void _closeImage(_image *im);
00458 
00461 void _get_image_bounding_box(_image* im,
00462                              double* x_min, double* y_min, double* z_min,
00463                              double* x_max, double* y_max, double* z_max);
00464 
00466 ENDIANNESS  _getEndianness(); 
00468 void initSupportedFileFormat();
00470 void printSupportedFileFormat();
00472 void removeSupportedFileFormat();
00473 
00474 
00477 PTRIMAGE_FORMAT imageType(const char *fileName);
00478 
00479 extern "C" {
00481 typedef void *(*ALLOCATION_FUNCTION)(size_t);
00482 
00484 typedef void (*DEALLOCATION_FUNCTION)(void *);
00485 }
00486 
00490 void setImageIOAllocationRoutines(ALLOCATION_FUNCTION alloc,
00491                                   DEALLOCATION_FUNCTION del);
00492 
00493 
00494 
00496 void *ImageIO_alloc(size_t);
00498 void ImageIO_free(void *);
00499 
00504 size_t ImageIO_write(const _image *im, const void *buf, size_t len);
00505 
00506 
00511 size_t ImageIO_read(const _image *im, void *buf, size_t len);
00512 
00515 char *ImageIO_gets( const _image *im, char *str, int size );
00516 
00519 int ImageIO_seek( const _image *im, long offset, int whence );
00520 
00523 int ImageIO_error( const _image *im );
00524 
00527 int ImageIO_close( _image *im );
00528 
00532 float triLinInterp(const _image* image, float posx, float posy, float posz,
00533                    const float value_outside = 0.);
00534 
00536 inline float trilinear_interpolation(const _image* image,
00537                                      float posx, float posy, float posz)
00538 {
00539   return triLinInterp(image, posx, posy, posz);
00540 }
00541 
00542 namespace CGAL {
00543 namespace IMAGEIO {
00544 
00545 //
00546 // The following definition are for the evaluate function.
00547 // 
00548 template <WORD_KIND wordKind, SIGN sign, unsigned int wdim>
00549 struct Word_type_generator
00550 {
00551 };
00552 
00553 template <SIGN sign>
00554 struct Word_type_generator<WK_FLOAT, sign, 4>
00555 {
00556   typedef float type;
00557 };
00558 
00559 template <SIGN sign>
00560 struct Word_type_generator<WK_FLOAT, sign, 8>
00561 {
00562   typedef double type;
00563 };
00564 
00565 template <>
00566 struct Word_type_generator<WK_FIXED, SGN_SIGNED, 1>
00567 {
00568 //   typedef boost::int8_t type;
00569   typedef char type;
00570 };
00571 
00572 template <>
00573 struct Word_type_generator<WK_FIXED, SGN_UNSIGNED, 1>
00574 {
00575   typedef boost::uint8_t type;
00576 };
00577 
00578 template <>
00579 struct Word_type_generator<WK_FIXED, SGN_SIGNED, 2>
00580 {
00581   typedef boost::int16_t type;
00582 };
00583 
00584 template <>
00585 struct Word_type_generator<WK_FIXED, SGN_UNSIGNED, 2>
00586 {
00587   typedef boost::uint16_t type;
00588 };
00589 
00590 template <>
00591 struct Word_type_generator<WK_FIXED, SGN_SIGNED, 4>
00592 {
00593   typedef boost::int32_t type;
00594 };
00595 
00596 template <>
00597 struct Word_type_generator<WK_FIXED, SGN_UNSIGNED, 4>
00598 {
00599   typedef boost::uint32_t type;
00600 };
00601 
00602 template <WORD_KIND wordKind, SIGN sign, unsigned int wdim>
00603 inline
00604 typename Word_type_generator<wordKind, sign, wdim>::type
00605 static_evaluate(const _image* image,
00606                 const unsigned int i,
00607                 const unsigned int j,
00608                 const unsigned int k)
00609 {
00610   typedef typename Word_type_generator<wordKind, sign, wdim>::type Word;
00611 
00612   return static_evaluate<Word>(image, i, j, k);
00613 }
00614 
00615 template <typename Word>
00616 inline
00617 Word
00618 static_evaluate(const _image* image,
00619                 const unsigned int i,
00620                 const unsigned int j,
00621                 const unsigned int k)
00622 {
00623   return ((Word*)image->data)[(k * image->ydim + j) * image->xdim + i];
00624 }
00625 
00626 } // end namespace IMAGEIO
00627 } // end namespace CGAL
00628 
00629 #define CGAL_IMAGE_IO_CASE(image_ptr,code)                                                 \
00630   switch(image_ptr->wordKind)                                                              \
00631   {                                                                                        \
00632   case WK_FLOAT:                                                                           \
00633     switch(image_ptr->wdim)                                                                \
00634     {                                                                                      \
00635     case 4: {                                                                              \
00636       typedef CGAL::IMAGEIO::Word_type_generator<WK_FLOAT, SGN_UNKNOWN, 4>::type Word;     \
00637       code;                                                                                \
00638       break;                                                                               \
00639     }                                                                                      \
00640     case 8: {                                                                              \
00641       typedef CGAL::IMAGEIO::Word_type_generator<WK_FLOAT, SGN_UNKNOWN, 8>::type Word;     \
00642       code;                                                                                \
00643       break;                                                                               \
00644     }                                                                                      \
00645     default:                                                                               \
00646       break;                                                                               \
00647     }                                                                                      \
00648     break;                                                                                 \
00649   case WK_FIXED:                                                                           \
00650     switch(image_ptr->wdim)                                                                \
00651     {                                                                                      \
00652     case 2: {                                                                              \
00653       if(image_ptr->sign == SGN_SIGNED) {                                                  \
00654         typedef CGAL::IMAGEIO::Word_type_generator<WK_FIXED, SGN_SIGNED, 2>::type Word;    \
00655         code;                                                                              \
00656         break;                                                                             \
00657       }                                                                                    \
00658       else {                                                                               \
00659         typedef CGAL::IMAGEIO::Word_type_generator<WK_FIXED, SGN_UNSIGNED, 2>::type Word;  \
00660         code;                                                                              \
00661         break;                                                                             \
00662       }                                                                                    \
00663     }                                                                                      \
00664     case 1: {                                                                              \
00665       if(image_ptr->sign == SGN_SIGNED) {                                                  \
00666         typedef CGAL::IMAGEIO::Word_type_generator<WK_FIXED, SGN_SIGNED, 1>::type Word;    \
00667         code;                                                                              \
00668         break;                                                                             \
00669       }                                                                                    \
00670       else {                                                                               \
00671         typedef CGAL::IMAGEIO::Word_type_generator<WK_FIXED, SGN_UNSIGNED, 1>::type Word;  \
00672         code;                                                                              \
00673         break;                                                                             \
00674       }                                                                                    \
00675     }                                                                                      \
00676     case 4: {                                                                              \
00677       if(image_ptr->sign == SGN_SIGNED) {                                                  \
00678         typedef CGAL::IMAGEIO::Word_type_generator<WK_FIXED, SGN_SIGNED, 4>::type Word;    \
00679         code;                                                                              \
00680         break;                                                                             \
00681       }                                                                                    \
00682       else {                                                                               \
00683         typedef CGAL::IMAGEIO::Word_type_generator<WK_FIXED, SGN_UNSIGNED, 4>::type Word;  \
00684         code;                                                                              \
00685         break;                                                                             \
00686       }                                                                                    \
00687     }                                                                                      \
00688     default:                                                                               \
00689       break;                                                                               \
00690     }                                                                                      \
00691     break;                                                                                 \
00692   default:                                                                                 \
00693     break;                                                                                 \
00694   }
00695 
00696 float evaluate(const _image* image,const unsigned int  i,const unsigned int  j,const unsigned int  k);
00697 
00698 
00701 void convertImageTypeToFloat(_image* image);
00702 
00703 
00704 #endif // end IMAGEIO_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines