1 ## Copyright (C) 2009 Sergey Kirgizov
3 ## This program is free software; you can redistribute it and/or
4 ## modify it under the terms of the GNU General Public License
5 ## as published by the Free Software Foundation; either version 3
6 ## of the License, or (at your option) any later version.
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
14 ## @deftypefn {Function File} {[@var{Y}, @var{newmap}] = } imdither (@var{img})
15 ## @deftypefnx {Function File} {[@var{Y}, @var{newmap}] = } imdither (@var{img}, @
17 ## @deftypefnx {Function File} {[@var{Y}, @var{newmap}] = } imdither (@var{img}, @
18 ## @var{colors}, @var{dithtype})
19 ## @deftypefnx {Function File} {[@var{Y}, @var{newmap}] = } imdither (@var{img}, @
21 ## @deftypefnx {Function File} {[@var{Y}, @var{newmap}] = } imdither (@var{img}, @
22 ## @var{map}, @var{colors})
23 ## @deftypefnx {Function File} {[@var{Y}, @var{newmap}] = } imdither(@var{img}, @
24 ## @var{map}, @var{colors}, @var{dithtype})
25 ## Reduce the number a colors of rgb or indexed image.
27 ## Note: this requires the ImageMagick "convert" utility.
28 ## get this from www.imagemagick.org if required
29 ## additional documentation of options is available from the
33 ## @var{dithtype} is a value from list:
37 ## @item "FloydSteinberg" (default)
41 ## @var{colors} is a maximum number of colors in result map
43 ## TODO: Add facility to use already created colormap over "-remap" option
45 ## BUGS: This function return a 0-based indexed images
46 ## when colormap size is lower or equals to 256 like at cmunique code
51 function [Y,newmap] = imdither(im,p1,p2,p3)
54 dithtype="FloydSteinberg";
57 "imdither( rgb )\n", ...
58 "imdither( rgb,colors )\n", ...
59 "imdither( rgb,colors,dithtype )\n", ...
60 "imdither( img,map )\n", ...
61 "imdither( img,map,colors )\n", ...
62 "imdither( img,map,colors,dithtype )\n"
66 fname = [tmpnam(),".ppm"];
67 if (nargin == 1 || isscalar(p1))
70 colors=sprintf("%d",p1);
75 opts=["-colors ",colors;"-dither ",dithtype];
76 imwrite(fname,im(:,:,1),im(:,:,2),im(:,:,3),opts);
77 [Y,newmap]=cmunique(imread(fname));
82 "imdither( rgb )\n", ...
83 "imdither( rgb,colors )\n", ...
84 "imdither( rgb,colors,dithtype )\n", ...
85 "imdither( img,map )\n", ...
86 "imdither( img,map,colors )\n", ...
87 "imdither( img,map,colors,dithtype )\n"
92 colors=sprintf("%d",p2);
97 opts=["-colors ",colors;"-dither ",dithtype];
99 imwrite(fname,im,(p1+1),opts);
100 [Y,newmap]=cmunique(imread(fname));