i write this:
------------------------ imdither.m -------------------------------
## Copyright (C) 2009 Sergey Kirgizov
##
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version. USE THIS SOFTWARE AT YOUR OWN RISK.


## -*- texinfo -*-
## @deftypefn {Function File} {...@var{y}, @var{newmap}] = }
imdither(@var{img})
## reduce the number a colors of rgb or indexed image.
## @deftypefnx {Function File} {...@var{y}, @var{newmap}] = }
imdither(@var{img},@var{colors})
## @deftypefnx {Function File} {...@var{y}, @var{newmap}] = }
imdither(@var{img},@var{colors},@var{dithtype})
##
## @deftypefnx {Function File} {...@var{y}, @var{newmap}] = }
imdither(@var{img},@var{map})
## @deftypefnx {Function File} {...@var{y}, @var{newmap}] = }
imdither(@var{img},@var{map},@var{colors})
## @deftypefnx {Function File} {...@var{y}, @var{newmap}] = }
imdither(@var{img},@var{map},@var{colors},@var{dithtype})
##
## Note: this requires the ImageMagick "convert" utility.
## get this from www.imagemagick.org if required
## additional documentation of options is available from the
## convert man page.
##
## where
## @var{dithtype} is a value from list:
##
## @itemize @bullet
## @item "None"
## @item "FloydSteinberg" (default)
## @item "Riemersma"
## @end itemize
##
## @var{colors} is a maximum number of colors in result map
##
## TODO: add facility to use already created colormap over "-remap" option
##
## BUGS: this function return a 0-based indexed images
## when colormap size is lower or equals to 256 like at cmunique code
## @seealso{cmunique}
##
## @end deftypefn

function [Y,newmap] = imdither(im,p1,p2,p3)

  colors="256";
  dithtype="FloydSteinberg";
  if  ( nargin < 1 )
    usage([ ...
       "imdither( rgb )\n", ...
       "imdither( rgb,colors )\n", ...
       "imdither( rgb,colors,dithtype )\n", ...
       "imdither( img,map )\n", ...
       "imdither( img,map,colors )\n", ...
       "imdither( img,map,colors,dithtype )\n"
       ]);
  endif

  fname = [tmpnam(),".ppm"];
  if (nargin == 1 || isscalar(p1))
                # rgb
    if (nargin >= 2)
      colors=sprintf("%d",p1);
      if (nargin >= 3)
    dithtype=p2;
      endif
    endif
    opts=["-colors ",colors;"-dither ",dithtype];
    imwrite(fname,im(:,:,1),im(:,:,2),im(:,:,3),opts);
    [Y,newmap]=cmunique(imread(fname));
    delete(fname);
  else
    if (nargin <= 1)
      usage([ ...
         "imdither( rgb )\n", ...
         "imdither( rgb,colors )\n", ...
         "imdither( rgb,colors,dithtype )\n", ...
         "imdither( img,map )\n", ...
         "imdither( img,map,colors )\n", ...
         "imdither( img,map,colors,dithtype )\n"
         ]);
    endif
                # indexed
    if (nargin >= 3)
      colors=sprintf("%d",p2);
      if (nargin >= 4)
    dithtype=p3;
      endif
    endif
    opts=["-colors ",colors;"-dither ",dithtype];
    im (rows(p1)<=256)
    imwrite(fname,im,(p1+1),opts);
    [Y,newmap]=cmunique(imread(fname));
    delete(fname);
  endif
endfunction
------------------------ imdither.m -------------------------------
p.s. sorry for my very bad english
------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
Octave-dev mailing list
Octave-dev@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/octave-dev

Reply via email to