]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/imdither.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / imdither.m
1 ## Copyright (C) 2009 Sergey Kirgizov
2 ##
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.
7 ## 
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.
12
13 ## -*- texinfo -*-
14 ## @deftypefn {Function File} {[@var{Y}, @var{newmap}] = } imdither (@var{img})
15 ## @deftypefnx {Function File} {[@var{Y}, @var{newmap}] = } imdither (@var{img}, @
16 ## @var{colors})
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}, @
20 ## @var{map})
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.
26 ##
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
30 ## convert man page.
31 ##
32 ## where
33 ## @var{dithtype} is a value from list:
34 ## 
35 ## @itemize @bullet
36 ## @item "None"
37 ## @item "FloydSteinberg" (default)
38 ## @item "Riemersma"
39 ## @end itemize 
40 ##
41 ## @var{colors} is a maximum number of colors in result map
42 ##
43 ## TODO: Add facility to use already created colormap over "-remap" option
44 ##
45 ## BUGS: This function return a 0-based indexed images 
46 ## when colormap size is lower or equals to 256 like at cmunique code
47 ## @seealso{cmunique}
48 ##
49 ## @end deftypefn
50
51 function [Y,newmap] = imdither(im,p1,p2,p3)
52   
53   colors="256";
54   dithtype="FloydSteinberg";
55   if  ( nargin < 1 )
56     usage([ ...
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"       
63        ]);
64   endif
65
66   fname = [tmpnam(),".ppm"];
67   if (nargin == 1 || isscalar(p1))
68                 # rgb
69     if (nargin >= 2)
70       colors=sprintf("%d",p1);
71       if (nargin >= 3)
72     dithtype=p2;
73       endif
74     endif
75     opts=["-colors ",colors;"-dither ",dithtype];
76     imwrite(fname,im(:,:,1),im(:,:,2),im(:,:,3),opts);
77     [Y,newmap]=cmunique(imread(fname));        
78     delete(fname);
79   else    
80     if (nargin <= 1)
81       usage([ ...
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"       
88          ]);
89     endif
90                 # indexed
91     if (nargin >= 3)
92       colors=sprintf("%d",p2);
93       if (nargin >= 4)
94     dithtype=p3;
95       endif
96     endif
97     opts=["-colors ",colors;"-dither ",dithtype];
98     im (rows(p1)<=256)
99     imwrite(fname,im,(p1+1),opts);
100     [Y,newmap]=cmunique(imread(fname));    
101     delete(fname);
102   endif
103 endfunction