"Fossies" - the Fresh Open Source Software Archive

Member "sip-0.12.1/macros/imshow.sci" (20 Sep 2013, 7251 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) Scilab source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "imshow.sci": 0.5.6_vs_0.12.1.

    1 function imshow(img, arg2, strf, viewer)
    2 //
    3 // Displays image in Scilab graphic window.
    4 //    imshow(I)      // I assumed to be 0-1
    5 //    imshow(I,n)    // I assumed to be 0-1
    6 //    imshow(I,[low high]) 
    7 //    imshow(X,map)  // X assumed to be 1-N
    8 //    imshow(RGB)    // RGB 0-1
    9 //    imshow(RGB, arg2, [, strf, viewer])    // RGB 0-1
   10 //    imshow filename
   11 //
   12 // AUTHOR
   13 //    Ricardo Fabbri  <rfabbri@(not this part) gmail d0t com>
   14 //    Cybernetic Vision Research Group
   15 //    Luciano da Fontoura Costa, supervisor.
   16 //    http://siptoolbox.sf.net
   17 //
   18 // TODO
   19 //
   20 // - incorporate other Matplot parameters 
   21 // - use internal imagemagick API for 'display'-style viewer without 
   22 // writing image to disk
   23 //
   24 // $Revision: 1.2 $ $Date: 2009-03-29 21:34:48 $
   25 
   26 [lhs, rhs] = argn(0);
   27 if rhs == 0 then
   28    error('Invalid number of arguments.');
   29 end
   30 
   31 if ~exists('strf','local')
   32    strf='040'
   33 else
   34    rhs=rhs-1
   35 end
   36 
   37 if ~exists('viewer','local')
   38   viewer=SIPVIEWER
   39 end
   40 
   41 if viewer <> 'sci'
   42   if getos() == 'Windows'
   43     error('external viewer not supported on Windows');
   44   else
   45     try
   46       tmpname = tempname();
   47     catch
   48       tmpname = TMPDIR + '/sip-imshow' + string(round(rand()*100000));
   49     end
   50     tmpname = tmpname + '-imshow.png';
   51     // remove temp file only at the beginning
   52     unix('rm -f ' + TMPDIR + '/*imshow.png');
   53   end
   54 end
   55 
   56 
   57 // TODO
   58 // double-buffering will be default for the next SIP release
   59 // TODO do it the modern way as in sip-intro.dem
   60 //
   61 //prev_pixmap_mode = xget('pixmap');
   62 //
   63 //// double-buffering:
   64 //if ~exists('sip_disable_pixbuffer') 
   65 //   xset('pixmap',1);
   66 //end
   67 
   68 select type(img)
   69    case 1 then    // paletted or grayscale image 
   70       if ~exists('arg2','local')
   71         if viewer == 'sci'
   72           [m,n]=size(img)
   73           xset('colormap', graycolormap(256))
   74           try
   75             set(gcf(), 'axes_size', [n m]);
   76             set(gca(), 'tight_limits','on')
   77           catch
   78             xset('wdim',n,m)
   79           end
   80           Matplot(img*255 + 1,strf)
   81         else
   82           if sip_get_verbose() == 'wordy'
   83             disp ('writing temporary image file ' + tmpname );
   84           end
   85           imwrite(img, tmpname);
   86         end
   87       else
   88          [m2,n2] = size(arg2)
   89          [m,n]=size(img)
   90          if n2 == 1 then
   91             if type(arg2) ~= 1 then    // imshow(img,ncolors)
   92                error("2nd argument must be a scalar.")
   93             end
   94             if (arg2 < 3) // workaround to a colormap bug
   95               mygraycmap = [0 0 0; 1 1 1];
   96             else
   97               mygraycmap = graycolormap(arg2);
   98             end
   99             img = img*(arg2-1)+1;
  100             if viewer == 'sci'
  101               xset('colormap', mygraycmap);
  102             else
  103               if sip_get_verbose() == 'wordy'
  104                 disp ('writing temporary image file ' + tmpname );
  105               end
  106               imwrite(img, mygraycmap, tmpname);
  107             end
  108          elseif (n2==0 | n2==2) then   // [] or [n1 n2]
  109             if n2~=0 then
  110                img(img<=arg2(1))=arg2(1)
  111                img(img>=arg2(2))=arg2(2)
  112             end
  113             if viewer == 'sci'
  114               img=round(normal(img,256,1));
  115               xset('colormap', graycolormap(256))
  116             else
  117               img=normal(img);
  118               if sip_get_verbose() == 'wordy'
  119                 disp ('writing temporary image file ' + tmpname );
  120               end
  121               imwrite(img, tmpname);
  122             end
  123          elseif (n2 == 3) then         // imshow(img,map)
  124             if viewer == 'sci'
  125               if m2 == 65536
  126                  //
  127                  // Scilab reserves black & white colors, so only max 65534
  128                  // entries are permited in the colormap :-( 
  129                  // Black and white are always accessible by m2+1 and m2+2, 
  130                  // respectively, no matter what is the current colormap.
  131                  //
  132                  // I'm ashamed of this code... we must urgently improve scilab's
  133                  // image display. Please help us if you can :-)
  134                  //
  135                  img2 = ind2rgb(img,arg2);
  136                  arg2=sip_approx_true_cmap(11)  // 11 levels (11^3 colors)
  137                  img=sip_index_true_cmap(img2,11)
  138               end
  139               xset('colormap', arg2)
  140             else
  141               if sip_get_verbose() == 'wordy'
  142                 disp ('writing temporary image file ' + tmpname );
  143               end
  144               imwrite(img, arg2, tmpname);
  145             end
  146          else
  147             error('Invalid size of 2nd argument.')
  148          end
  149          try
  150            set(gcf(), 'axes_size', [n m]);
  151            set(gca(), 'tight_limits','on')
  152          catch
  153            xset('wdim',n,m)
  154          end
  155          Matplot(img,strf)
  156       end
  157    case 17 then   // truecolor image
  158       dims=size(img)
  159       if dims(3) ~= 3 then
  160          error('RGB image must have 3rd dimension equal to 3.')
  161       end
  162       if viewer == 'sci'
  163         // The following works, at the cost of reduction
  164         // of the number of colors to 40^3 ~= 2^16
  165         // Scilab unfortunately doesn't work with 25bit colordepth
  166         // My thanks to Bruno Pincon
  167         printf('This may take a while.');
  168         if argn(2) == 1
  169            nlevels = 20  // 20^3 colors only, for speed
  170         else
  171            nlevels = arg2
  172         end
  173         xset('colormap',sip_approx_true_cmap(nlevels))
  174         try
  175           set(gcf(), 'axes_size', [dims(2) dims(1)]);
  176           set(gca(), 'tight_limits','on')
  177         catch
  178           xset('wdim',n,m)
  179         end
  180         Matplot(sip_index_true_cmap(img,nlevels),strf)
  181       else
  182         if sip_get_verbose() == 'wordy'
  183           disp ('writing temporary image file ' + tmpname );
  184         end
  185         imwrite(img, tmpname);
  186       end
  187    case 10 then   // filename
  188       if viewer == 'sci'
  189         map=0;
  190         [image,map] = imread(img)
  191         imshow(image,map)
  192       else
  193         tmpname = img;
  194       end
  195    else
  196       error('1st argument of invalid type.')
  197 end
  198 
  199 // if xget('pixmap') == 1,  xset('wshow'), end
  200 
  201 // Part of double-buffer default for next release:
  202 //xset('pixmap',prev_pixmap_mode);
  203 //
  204 if viewer <> 'sci'
  205   mycmd = viewer + ' ' + tmpname + ' &';
  206   if sip_get_verbose() == 'wordy'
  207     disp ('running viewer command: ' + mycmd);
  208   end
  209   unix_w(mycmd);
  210 end
  211 
  212 endfunction
  213 
  214 //
  215 // -------------------------------------------------------------------------
  216 // SIP - Scilab Image Processing toolbox
  217 // Copyright (C) 2002-2009  Ricardo Fabbri
  218 //
  219 // This program is free software; you can redistribute it and/or modify
  220 // it under the terms of the GNU General Public License as published by
  221 // the Free Software Foundation; either version 2 of the License, or
  222 // (at your option) any later version.
  223 //
  224 // This program is distributed in the hope that it will be useful,
  225 // but WITHOUT ANY WARRANTY; without even the implied warranty of
  226 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  227 // GNU General Public License for more details.
  228 //
  229 // You should have received a copy of the GNU General Public License
  230 // along with this program; if not, write to the Free Software
  231 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  232 // -------------------------------------------------------------------------
  233 //