Beispiel #1
0
/*----------------------------------------------------------
 * int_imread: 
 *     interface for imread function.
 *     should provide   [RGB]=imread(name) 
 *                [Index,Map]=imread(name) at Scilab level 
 *  
 * TO-DO
 *    - return on errors, even if exeption is NULL
 *
 * $Revision: 1.2 $ $Date: 2009-03-29 21:34:48 $
 *----------------------------------------------------------*/
SipExport int 
int_imread(char *fname) 
{
   /* Interface variables */ 
   HyperMat *Img;
   int   m1, n1,l1, /* for name input argument      */
         m2, n2,    /* for index output argument    */
         minlhs=1, maxlhs=2, minrhs=1, maxrhs=1, i;
   double *l2;

   /* Other variables */
   unsigned long  imgsize;
 
   /* ImageMagick variables */
   ExceptionInfo  exception;
   Image          *image;
   ImageInfo      *image_info;
   PixelPacket    *pix;
   ImageType      imgtype;

   bool stat;


   CheckRhs(minrhs,maxrhs) ;
   CheckLhs(minlhs,maxlhs) ;
 
   /* Get name (#1) */
   GetRhsVar(1, "c", &m1, &n1, &l1);  
 
   /* Initialize the image info structure and read an image.  */
   InitializeMagick(NULL);
   GetExceptionInfo(&exception);
   image_info=CloneImageInfo((ImageInfo *) NULL);
   (void) strncpy(image_info->filename,cstk(l1),MaxTextExtent);
    
   image=ReadImage(image_info,&exception);

   if (image == (Image *) NULL) {
      /* clean up */
      if(exception.reason != NULL) {
         char errmsg[50];
         for (i=0; i<49; i++)
            errmsg[i]=' ';
         errmsg[49]='\0';
         strncpy(errmsg,SipGetLocaleExceptionMessage(exception.severity,exception.reason),50);
         DestroyImageInfo(image_info);
         DestroyExceptionInfo(&exception);
         DestroyMagick();
         sip_error(errmsg);
      }
      DestroyImageInfo(image_info);
      DestroyExceptionInfo(&exception);
      DestroyMagick();
      sip_error("unknown reason");
   }
   

   m2 = image->rows; n2 = image->columns;

   if (sip_verbose == SIP_WORDY)
      sciprint("Size:\t%ld rows X %ld columns\n\r", m2, n2);

   imgsize = m2 * n2;
   
 
   pix=GetImagePixels(image, 0, 0, n2, m2);
   if(pix == (PixelPacket *) NULL)
      SIP_MAGICK_ERROR;
   
   switch(image->storage_class) {
   case DirectClass: {
      imgtype = GetImageType(image, &exception);
      if(imgtype == BilevelType) {
         stat = magick_binary_image_to_double_array(fname,pix,&l2, m2, n2);
         if (!stat) return false;
         CreateVarFromPtr(2, "d",&m2,&n2,&l2);  
         free(l2);
      } else {
         stat= magick_truecolor_image_to_double_hypermat(fname,pix,&Img,m2,n2);
         if (!stat) return false;
         CreateHMat(2,Img);
         free_sci_tru_img(&Img);
      }
      m1 = n1 = 0;
      CreateVar(3,"d",&m1,&n1,&l1);
      break;
   }
   case PseudoClass:   {
      stat= magick_index_map_to_sci_dbl(fname,image,2);
      if (!stat) return false;
      break;
   }
   default: 
      sip_error("unknown color class");
      break;
   }
   LhsVar(1) = 2;
   LhsVar(2) = 3;

   /* Terminate Imagemagick */
   DestroyImageInfo(image_info);
   DestroyImage(image);
   DestroyExceptionInfo(&exception);
   DestroyMagick();
   return true;
}
Beispiel #2
0
int C2F(intehm)()
{
    /*
     *  Extraction routine for an hypermatrix of type REAL_OR_COMPLEX, BOOLEAN
     *  and INTEGER (the 6 types of scilab ints)
     *
     *    He = ehm ( v_1, v_2, ..., v_nb_iv, H )
     *
     */
    HyperMat H, He;
    int dec, i, k, l, m, n, mn, ntot, ind_max;
    int *j, ier, one = 1, zero = 0, ltot, nb_index_vectors, final_dimsize, lr, lc;
    int *P, *Pe;
    short int *siP, *siPe;
    char  *cP, *cPe;

    /*   CheckLhs(minlhs,maxlhs); */

    if ( Rhs < 2 )
    {
        Scierror(999, _("%s: Wrong number of input arguments: at least %d expected.\n"), "hmops", 2);
        return(0);
    };

    if ( ! get_hmat(Rhs, &H) )
    {
        Scierror(999, _("%s: Wrong type for input argument(s): An hypermatrix expected.\n"), "hmops");
        return 0;
    }
    else if ( H.type == NOT_REAL_or_CMPLX_or_BOOL_or_INT  || H.type == OLD_HYPERMAT )
    {
        /*  do the extraction with the macro %hm_e  */
        Fin = -Fin;
        return 0;
    }

    nb_index_vectors = Rhs - 1;
    if ( H.dimsize <  nb_index_vectors )
    {
        Scierror(999, _("%s: Wrong number of input arguments: at most %d expected.\n"), "hmops", H.dimsize);
        return 0;
    }
    else if ( H.dimsize > nb_index_vectors )  /* reshape H */
    {
        ReshapeHMat(Rhs + 1, &H, nb_index_vectors );
        dec = Rhs + 1;
    }
    else
    {
        dec = Rhs;
    }

    if ( H.size == 0 )   /* the hypermat is empty => return an empty matrix ? */
    {
        CreateVar(dec + 1, MATRIX_OF_DOUBLE_DATATYPE, &zero, &zero, &l);
        LhsVar(1) = dec + 1;
        PutLhsVar();
        return 0;
    }


    ntot = 1;   /* will be the nb of elts of the extracted hmat or mat */
    for ( i = 1 ; i <= nb_index_vectors ; i++ )
    {
        ier = create_index_vector(i, dec + i, &mn, H.dims[i - 1], &ind_max);
        if ( ier == 0  ||  ind_max > H.dims[i - 1] )
        {
            Scierror(999, _("%s: Bad index #%d in hypermatrix extraction. "), "hmops", i);
            return 0;
        }
        if ( mn == 0 )   /* the vector index is [] => we return an empty matrix */
        {
            CreateVar(dec + i + 1, MATRIX_OF_DOUBLE_DATATYPE, &zero, &zero, &l);
            LhsVar(1) = dec + i + 1;
            PutLhsVar();
            return 0;
        }
        ntot *= mn;
    }

    /*  For the Matlab compatibility : an hypermatrix of profil n1 x ... x nj x ... x nk
     *  with  nj > 1 and nj+1 = ... = nk = 1 becomes an hypermatrix of profil n1 x ... x nj
     *  Moreover, in scilab, if nj <= 2, we get in fact a matrix.
     */
    final_dimsize = nb_index_vectors;
    while (final_dimsize > 1 && get_length(dec + final_dimsize) == 1)
    {
        final_dimsize--;
    }
    if ( final_dimsize > 2 )   /* we create an hypermatrix for the extraction result */
    {
        He.dimsize = final_dimsize;
        He.size = ntot;
        He.it = H.it;
        He.type = H.type;
        CreateHMat(dec + Rhs, &He);
        for ( k = 0 ; k < final_dimsize ; k++ )
        {
            He.dims[k] = get_length(dec + k + 1);
        }
    }
    else                /* we create a matrix  for the extraction result */
    {
        m = get_length(dec + 1);
        if (final_dimsize > 1)
        {
            n = get_length(dec + 2);
        }
        else
        {
            n = 1;
        }
        switch (H.type)
        {
            case (sci_matrix):
                CreateCVar(dec + Rhs, MATRIX_OF_DOUBLE_DATATYPE, &(H.it), &m, &n, &lr, &lc);
                He.R = stk(lr);
                if ( H.it == 1 )
                {
                    He.I = stk(lc);
                }
                break;
            case (sci_boolean):
                CreateVar(dec + Rhs, MATRIX_OF_BOOLEAN_DATATYPE, &m, &n, &lr);
                He.P = (void *) istk(lr);
                break;
            case (sci_ints):
                lr = H.it;
                CreateVar(dec + Rhs, MATRIX_OF_VARIABLE_SIZE_INTEGER_DATATYPE, &m, &n, &lr);
                He.P = (void *) istk(lr);
                break;
        }
    }

    /* indices computing */
    ltot = 4;
    CreateVar(dec + Rhs + 1, MATRIX_OF_VARIABLE_SIZE_INTEGER_DATATYPE, &ntot, &one, &ltot);
    j = istk(ltot);
    compute_indices(dec, nb_index_vectors, H.dims, j);

    /*  fill the resulting hypermatrix or matrix  */
    switch ( H.type )
    {
        case (sci_matrix) :
            for ( k = 0 ; k < ntot ; k++ )
            {
                He.R[k] = H.R[j[k]];
            }
            if (H.it == 1)
                for ( k = 0 ; k < ntot ; k++ )
                {
                    He.I[k] = H.I[j[k]];
                }
            break;

        case (sci_boolean) :     /* (sci_boolean stored with 4 bytes) */
            Pe = (int *) He.P ;
            P = (int *) H.P;
            for ( k = 0 ; k < ntot ; k++ )
            {
                Pe[k] = P[j[k]];
            }
            break;

        case (sci_ints) :
            if ( H.it == I_INT32  ||  H.it == I_UINT32 )
            {
                Pe = (int *) He.P;
                P = (int *) H.P;
                for ( k = 0 ; k < ntot ; k++ )
                {
                    Pe[k] = P[j[k]];
                }
            }
            else if ( H.it == I_INT16  ||  H.it == I_UINT16 )
            {
                siPe = (short int *) He.P;
                siP = (short int *) H.P;
                for ( k = 0 ; k < ntot ; k++ )
                {
                    siPe[k] = siP[j[k]];
                }
            }
            else    /* SCI_INT8 and SCI_UINT8 : 1 Byte int */
            {
                cPe = (char *) He.P;
                cP = (char *) H.P;
                for ( k = 0 ; k < ntot ; k++ )
                {
                    cPe[k] = cP[j[k]];
                }
            }
            break;
    }

    LhsVar(1) = dec + Rhs;
    PutLhsVar();
    return 0;
}