"Fossies" - the Fresh Open Source Software Archive

Member "sip-0.12.1/src/imread.c" (26 Nov 2016, 4257 Bytes) of package /linux/privat/sip-0.12.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "imread.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.5.6_vs_0.12.1.

    1 /*
    2  * -------------------------------------------------------------------------
    3  * SIP - Scilab Image Processing toolbox
    4  * Copyright (C) 2002-2009  Ricardo Fabbri
    5  *
    6  * This program is free software; you can redistribute it and/or modify
    7  * it under the terms of the GNU General Public License as published by
    8  * the Free Software Foundation; either version 2 of the License, or
    9  * (at your option) any later version.
   10  * 
   11  * This program is distributed in the hope that it will be useful,
   12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14  * GNU General Public License for more details.
   15  * 
   16  * You should have received a copy of the GNU General Public License
   17  * along with this program; if not, write to the Free Software
   18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   19  * -------------------------------------------------------------------------
   20  */ 
   21 
   22 #include <stdio.h>
   23 #include <string.h>
   24 #include <time.h>
   25 #include <sys/types.h>
   26 #include <math.h>
   27 #include "sip_common.h"
   28 
   29 
   30 
   31 
   32 /*----------------------------------------------------------
   33  * int_imread: 
   34  *     interface for imread function.
   35  *     should provide   [RGB]=imread(name) 
   36  *                [Index,Map]=imread(name) at Scilab level 
   37  *  
   38  * TO-DO
   39  *    - return on errors, even if exeption is NULL
   40  *
   41  * $Revision: 1.2 $ $Date: 2009-03-29 21:34:48 $
   42  *----------------------------------------------------------*/
   43 SipExport int 
   44 int_imread(char *fname) 
   45 {
   46    /* Interface variables */ 
   47    HyperMat *Img;
   48    int   m1, n1,l1, /* for name input argument      */
   49          m2, n2,    /* for index output argument    */
   50          minlhs=1, maxlhs=2, minrhs=1, maxrhs=1, i;
   51    double *l2;
   52 
   53    /* Other variables */
   54    unsigned long  imgsize;
   55  
   56    /* ImageMagick variables */
   57    ExceptionInfo  exception;
   58    Image          *image;
   59    ImageInfo      *image_info;
   60    PixelPacket    *pix;
   61    ImageType      imgtype;
   62 
   63    bool stat;
   64 
   65 
   66    CheckRhs(minrhs,maxrhs) ;
   67    CheckLhs(minlhs,maxlhs) ;
   68  
   69    /* Get name (#1) */
   70    GetRhsVar(1, "c", &m1, &n1, &l1);  
   71  
   72    /* Initialize the image info structure and read an image.  */
   73    InitializeMagick(NULL);
   74    GetExceptionInfo(&exception);
   75    image_info=CloneImageInfo((ImageInfo *) NULL);
   76    (void) strncpy(image_info->filename,cstk(l1),MaxTextExtent);
   77     
   78    image=ReadImage(image_info,&exception);
   79 
   80    if (image == (Image *) NULL) {
   81       /* clean up */
   82       if(exception.reason != NULL) {
   83          char errmsg[50];
   84          for (i=0; i<49; i++)
   85             errmsg[i]=' ';
   86          errmsg[49]='\0';
   87          strncpy(errmsg,SipGetLocaleExceptionMessage(exception.severity,exception.reason),50);
   88          DestroyImageInfo(image_info);
   89          DestroyExceptionInfo(&exception);
   90          DestroyMagick();
   91          sip_error(errmsg);
   92       }
   93       DestroyImageInfo(image_info);
   94       DestroyExceptionInfo(&exception);
   95       DestroyMagick();
   96       sip_error("unknown reason");
   97    }
   98    
   99 
  100    m2 = image->rows; n2 = image->columns;
  101 
  102    if (sip_verbose == SIP_WORDY)
  103       sciprint("Size:\t%ld rows X %ld columns\n\r", m2, n2);
  104 
  105    imgsize = m2 * n2;
  106    
  107  
  108    pix=GetImagePixels(image, 0, 0, n2, m2);
  109    if(pix == (PixelPacket *) NULL)
  110       SIP_MAGICK_ERROR;
  111    
  112    switch(image->storage_class) {
  113    case DirectClass: {
  114       imgtype = GetImageType(image, &exception);
  115       if(imgtype == BilevelType) {
  116          stat = magick_binary_image_to_double_array(fname,pix,&l2, m2, n2);
  117          if (!stat) return false;
  118          CreateVarFromPtr(2, "d",&m2,&n2,&l2);  
  119          free(l2);
  120       } else {
  121          stat= magick_truecolor_image_to_double_hypermat(fname,pix,&Img,m2,n2);
  122          if (!stat) return false;
  123          CreateHMat(2,Img);
  124          free_sci_tru_img(&Img);
  125       }
  126       m1 = n1 = 0;
  127       CreateVar(3,"d",&m1,&n1,&l1);
  128       break;
  129    }
  130    case PseudoClass:   {
  131       stat= magick_index_map_to_sci_dbl(fname,image,2);
  132       if (!stat) return false;
  133       break;
  134    }
  135    default: 
  136       sip_error("unknown color class");
  137       break;
  138    }
  139    LhsVar(1) = 2;
  140    LhsVar(2) = 3;
  141 
  142    /* Terminate Imagemagick */
  143    DestroyImageInfo(image_info);
  144    DestroyImage(image);
  145    DestroyExceptionInfo(&exception);
  146    DestroyMagick();
  147    return true;
  148 }