"Fossies" - the Fresh Open Source Software Archive

Member "sip-0.12.1/src/imwrite.c" (20 Sep 2013, 5677 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 "imwrite.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 <string.h>
   23 #include <stdio.h>
   24 #include <time.h>
   25 #include <sys/types.h>
   26 #include "sip_common.h"
   27 
   28 static int check_args(char *fname, int opt_pos);
   29 
   30 /*-----------------------------------------------------------------
   31  * int_imwrite: 
   32  *     interface for imwrite function.
   33  *     TODO
   34  *       - Im (HyperMat) doesn't seem to be freed.
   35  *
   36  * $Revision: 1.2 $ $Date: 2009-03-29 21:34:48 $
   37  *-----------------------------------------------------------------*/
   38 SipExport int 
   39 int_imwrite(char *fname) 
   40 {
   41    /* Interface variables */ 
   42    int   m1, n1, l1, 
   43          minlhs=1, maxlhs=1, minrhs=2, maxrhs=3,
   44          name_rows, name_columns, name,
   45          nopt, iopos; 
   46    static rhs_opts opts[]= { 
   47          {-1,"depth",-1,0,0,NULL},
   48          {-1,"quality",-1,0,0,NULL},
   49            {-1,NULL,-1,0,0,NULL}
   50    };
   51    unsigned nv;
   52    bool stat;
   53 
   54    unsigned long depth, quality;
   55 
   56    /* Other variables */
   57    short int argtype;
   58 
   59    /* ImageMagick variables */
   60    ExceptionInfo  exception;
   61    Image          *image;
   62    ImageInfo      *image_info;
   63    PixelPacket    *pix;
   64 
   65    /* -- Deal with the arguments -- */
   66    nopt = NumOpt();
   67    CheckRhs(minrhs,maxrhs + nopt);
   68    CheckLhs(minlhs,maxlhs);
   69 
   70    if ( getOptionals(pvApiCtx,fname,opts) == 0) return false;
   71    if ( (argtype = check_args(fname, nopt)) == false) return false;
   72 
   73    /* default values if optional arguments are not given */
   74    iopos=Rhs ;
   75    if ( opts[0].iPos == -1 ) {
   76       depth = QuantumDepth;
   77    } else {
   78       double dblDepth = 0;
   79       getScalarDouble(pvApiCtx, opts[0].piAddr, &dblDepth);
   80       depth = (unsigned long)dblDepth;
   81       if ( depth != 8) {
   82         if  (depth == 16 && QuantumDepth < 16) 
   83            sip_error("depth cannot be 16 in your current setup")
   84         else if (depth != 16)
   85            sip_error("depth must be 8 or 16")
   86       }
   87    }
   88 
   89    if ( opts[1].iPos == -1 ) {
   90       quality= 75; /* Default quality/compression for jpeg/png/miff */
   91    } else { 
   92       double dblQuality = 0;
   93       getScalarDouble(pvApiCtx, opts[1].piAddr, &dblQuality);
   94       quality = (unsigned long) dblQuality;
   95       if (dblQuality < 0 || quality > 100) 
   96          sip_error("quality must be in range 0-100")
   97    }
   98 
   99 
  100    /* -- Pass scilab structures to IM -- */
  101    
  102    InitializeMagick(NULL);
  103    GetExceptionInfo(&exception);
  104    image_info=CloneImageInfo((ImageInfo *) NULL);
  105 
  106    image_info->colorspace = RGBColorspace; // @@@ maybe to take this off
  107    image_info->monochrome = 0;
  108    image_info->dither = 0;  // Imagemagick sets this as true by default.
  109                             // But this changes binary images too much.
  110    image_info->depth = depth;
  111    image_info->quality = quality;
  112    image=AllocateImage(image_info);
  113 
  114    nv = 1;
  115    switch (argtype) {
  116       case ARG_2D:
  117             GetRhsVar(nv++, "d", &m1, &n1, &l1);
  118             stat = sci_2D_double_matrix_to_magick(fname, l1, m1, n1, image, &pix);
  119             if (!stat) 
  120                return false;
  121             break;
  122 
  123       case ARG_3D:
  124             stat = sci_3D_double_hypermat_to_magick(fname,nv++,image,&pix); 
  125             if (!stat)
  126                return false;
  127             break;
  128 
  129       case ARG_INDEX_MAP:
  130             stat = sci_index_map_to_magick(fname, nv, image, &pix);
  131             if (!stat) 
  132                return false;
  133             nv+=2;
  134             break;
  135       default:
  136             return false;
  137    }
  138 
  139    GetRhsVar(nv++, "c", &name_rows, &name_columns, &name);  
  140 
  141    /* -- write the image -- */
  142    (void) strncpy(image->filename,cstk(name),MaxTextExtent);
  143    if (WriteImage(image_info,image) == 0)
  144       SIP_MAGICK_ERROR;
  145    
  146    DestroyImageInfo(image_info);
  147    DestroyExceptionInfo(&exception);
  148    DestroyImage(image);
  149    DestroyMagick();
  150    return true;
  151 }
  152 
  153 
  154 /*-----------------------------------------------------------
  155  * check_args:                                                
  156  *    Certify the correctness of the number and type of
  157  *    arguments. 
  158  *    The returning code indicates the sematic type of the
  159  *    argument (non-zero), or invalid argument (zero)
  160  *-----------------------------------------------------------*/
  161 int
  162 check_args(char *fname, int nopts)
  163 {
  164    int nargs;
  165 
  166    nargs = Rhs - nopts;
  167    if (nargs == 2) {
  168       if (VarType(2) != STRING)
  169          sip_error("argument 2 must be a string")
  170       switch (VarType(1)) {
  171          case USUALMATRIX: return ARG_2D; 
  172          case HYPERMATRIX: return ARG_3D; 
  173          default: 
  174             sip_error("argument 1 must be matrix or hypermatrix")
  175       }
  176    } 
  177    /* nargs == 3 */
  178    if(VarType(3) != STRING)
  179       sip_error("argument 3 must be a string")
  180    if((VarType(1) != USUALMATRIX) || (VarType(2) != USUALMATRIX))
  181       sip_error("the first two arguments must be arrays")
  182 
  183    return ARG_INDEX_MAP;
  184 }