Mercurial > octave-nkf
annotate scripts/image/rgb2ind.m @ 16630:5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
* rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
author | Adam H Aitkenhead <adamhaitkenhead@gmail.com> |
---|---|
date | Sat, 04 May 2013 10:53:43 +0100 |
parents | 6ae93518356c |
children | c979e6db619b |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
1 ## Copyright (C) 1994-2012 John W. Eaton |
15687
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
2 ## Copyright (C) 2012 Carnë Draug |
16630
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
3 ## Copyright (C) 2013 Adam H Aitkenhead |
2313 | 4 ## |
5 ## This file is part of Octave. | |
6 ## | |
7 ## Octave is free software; you can redistribute it and/or modify it | |
8 ## under the terms of the GNU General Public License as published by | |
7016 | 9 ## the Free Software Foundation; either version 3 of the License, or (at |
10 ## your option) any later version. | |
2313 | 11 ## |
12 ## Octave is distributed in the hope that it will be useful, but | |
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 ## General Public License for more details. | |
16 ## | |
17 ## You should have received a copy of the GNU General Public License | |
7016 | 18 ## along with Octave; see the file COPYING. If not, see |
19 ## <http://www.gnu.org/licenses/>. | |
1024 | 20 |
3381 | 21 ## -*- texinfo -*- |
5922 | 22 ## @deftypefn {Function File} {[@var{x}, @var{map}] =} rgb2ind (@var{rgb}) |
16630
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
23 ## @deftypefnx {Function File} {[@var{x}, @var{map}] =} rgb2ind (@var{rgb}, @var{map}) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
24 ## @deftypefnx {Function File} {[@var{x}, @var{map}] =} rgb2ind (@var{rgb}, @var{n}) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
25 ## @deftypefnx {Function File} {[@var{x}, @var{map}] =} rgb2ind (@var{rgb}, @var{tol}) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
26 ## @deftypefnx {Function File} {[@var{x}, @var{map}] =} rgb2ind (@var{rgb}, @var{map}, @var{dither_option}) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
27 ## @deftypefnx {Function File} {[@var{x}, @var{map}] =} rgb2ind (@var{rgb}, @var{n}, @var{dither_option}) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
28 ## @deftypefnx {Function File} {[@var{x}, @var{map}] =} rgb2ind (@var{rgb}, @var{tol}, @var{dither_option}) |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
29 ## @deftypefnx {Function File} {[@var{x}, @var{map}] =} rgb2ind (@var{R}, @var{G}, @var{B}) |
16630
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
30 ## @deftypefnx {Function File} {[@var{x}, @var{map}] =} rgb2ind (@var{R}, @var{G}, @var{B}, @var{map}) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
31 ## @deftypefnx {Function File} {[@var{x}, @var{map}] =} rgb2ind (@var{R}, @var{G}, @var{B}, @var{n}) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
32 ## @deftypefnx {Function File} {[@var{x}, @var{map}] =} rgb2ind (@var{R}, @var{G}, @var{B}, @var{tol}) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
33 ## @deftypefnx {Function File} {[@var{x}, @var{map}] =} rgb2ind (@var{R}, @var{G}, @var{B}, @var{map}, @var{dither_option}) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
34 ## @deftypefnx {Function File} {[@var{x}, @var{map}] =} rgb2ind (@var{R}, @var{G}, @var{B}, @var{n}, @var{dither_option}) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
35 ## @deftypefnx {Function File} {[@var{x}, @var{map}] =} rgb2ind (@var{R}, @var{G}, @var{B}, @var{tol}, @var{dither_option}) |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15693
diff
changeset
|
36 ## Convert an image in red-green-blue (RGB) color space to an indexed image. |
16630
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
37 ## |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
38 ## The input image @var{rgb} must be an N-dimensional RGB image (MxNxO...x3 array) where M,N,O... are the |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
39 ## image dimensions, and the final dimension contains the values in the red, green and blue |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
40 ## channels. Alternatively, the red, green and blue color channels can be input as separate arrays @var{R}, @var{G} and @var{B}. |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
41 ## |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
42 ## @var{map} defines the colormap to be used. Alternatively, @var{n} or @var{tol} may be used to define |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
43 ## the maximum number of colors to use in an automatically generated colormap. @var{n} is related to @var{tol} |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
44 ## by: @var{n} = (floor (1/@var{tol}) + 1)^3; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
45 ## @var{tol} must be >0 and <=1. |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
46 ## |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
47 ## @var{dither_option} is a string which enables or disables dithering: 'dither' (default) or 'nodither'. |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
48 ## |
14260
1f911333ed3d
doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
49 ## @seealso{ind2rgb, rgb2hsv, rgb2ntsc} |
3373 | 50 ## @end deftypefn |
51 | |
3202 | 52 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
2312 | 53 ## Created: July 1994 |
54 ## Adapted-By: jwe | |
559 | 55 |
16630
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
56 function [x, map] = rgb2ind (varargin) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
57 |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
58 ## Gather the inputs |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
59 if (nargin < 1 || nargin > 6) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
60 print_usage (); |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
61 else |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
62 |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
63 ## Test for dither_option, by checking if the final input is a string |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
64 if ischar (varargin{end}) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
65 dither_option = varargin{end}; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
66 dither_check = true; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
67 else |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
68 dither_option = 'dither'; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
69 dither_check = false; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
70 endif |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
71 |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
72 ## Read the rgb input |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
73 if (nargin-dither_check==1 || nargin-dither_check==2) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
74 |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
75 rgb = varargin{1}; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
76 if (size (rgb)(end) != 3) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
77 error ("rgb2ind: The input image must be an RGB image (MxNxO...x3 array)."); |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
78 elseif (min (rgb(:)) < 0 || max (rgb(:)) > 1) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
79 error ("rgb2ind: The input image must contain values between 0 and 1."); |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
80 endif |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
81 if (nargin-dither_check==2) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
82 option = varargin{2}; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
83 else |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
84 dither_option = 'nodither'; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
85 endif |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
86 |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
87 ## Read the R,G,B inputs |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
88 elseif (nargin-dither_check==3 || nargin-dither_check==4) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
89 |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
90 R = varargin{1}; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
91 G = varargin{2}; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
92 B = varargin{3}; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
93 if (! size_equal (R, G, B)) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
94 error ("rgb2ind: R, G, and B must have the same size"); |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
95 endif |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
96 if (nargin-dither_check==4) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
97 option = varargin{4}; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
98 else |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
99 dither_option = 'nodither'; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
100 endif |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
101 |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
102 rgb = reshape ([R(:), G(:), B(:)], [size(R), 3]); |
1024 | 103 |
16630
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
104 endif |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
105 endif |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
106 |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
107 ## Apply a limited colormap if required |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
108 if (exist ('option','var')) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
109 |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
110 if (size (option,1)==1) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
111 |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
112 if option>0 && option<=1 |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
113 ## option: tol |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
114 tol = option; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
115 n = (floor (1/option) + 1)^3; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
116 else |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
117 ## option: n |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
118 n = option; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
119 endif |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
120 optionstr = sprintf ('-colors %d',n); |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
121 |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
122 else |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
123 |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
124 ## option: map |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
125 map = option; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
126 if (isequal (map(:,1),map(:,2)) || isequal (map(:,1),map(:,3)) || isequal (map(:,2),map(:,3))) |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
127 error ('rgb2ind: The colormap cannot contain matching R,G, or B channels.') |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
128 endif |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
129 fnmap = tmpnam; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
130 map = reshape (map, size (map, 1), 1, 3); |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
131 imwrite (map, fnmap, 'tiff'); |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
132 optionstr = sprintf ('-map %s', fnmap); |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
133 |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
134 endif |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
135 |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
136 ## Prepare the Graphicsmagick dithering option |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
137 if strcmp (dither_option, 'nodither') |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
138 ditherstr = '+dither'; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
139 elseif strcmp (dither_option, 'dither') |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
140 ditherstr = '-dither'; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
141 endif |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
142 |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
143 ## Perform the image processing using Graphicsmagick |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
144 fna = tmpnam; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
145 fnb = tmpnam; |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
146 imwrite (rgb, fna, 'tiff'); |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
147 gmstr = sprintf ('gm convert %s %s %s %s', fna, ditherstr, optionstr, fnb); |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
148 system (gmstr); |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
149 rgb = imread (fnb); |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
150 |
5922 | 151 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
152 |
16630
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
153 ## Conversion of rgb image to x,map |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
154 sz = size (rgb); |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
155 pr = prod (sz(1:end-1)); |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
156 x = zeros (sz(1:end-1)); |
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
157 [map,~,x(:)] = unique (reshape(rgb, [pr, 3]), "rows"); |
559 | 158 |
15687
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
159 ## a colormap is of class double and values between 0 and 1 |
16630
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
160 switch (class (rgb)) |
15687
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
161 case {"single", "double", "logical"} |
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
162 ## do nothing, return the same |
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
163 case {"uint8", "uint16"} |
16630
5d6243c2acbf
rgb2ind.m: Enable handling of ND images, and enable n,tol,map,dither as inputs.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
15715
diff
changeset
|
164 map = double (map) / double (intmax (class (rgb))); |
15687
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
165 case "int16" |
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
166 map = (double (im) + 32768) / 65535; |
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
167 otherwise |
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
168 error ("unsupported image class %s", im_class); |
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
169 endswitch |
559 | 170 |
15687
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
171 ## we convert to the smallest class necessary to encode the image. Matlab |
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
172 ## documentation does not mention what it does when uint16 is not enough... |
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
173 ## When an indexed image is of integer class, there's a -1 offset to the |
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
174 ## colormap, hence the adjustment |
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
175 if (rows (map) < 256) |
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
176 x = uint8 (x - 1); |
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
177 elseif (rows (map) < 65536) |
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
178 x = uint16 (x - 1); |
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
179 else |
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
180 ## leave it as double |
89c8d8f8f87b
rgb2ind: find unique RGB values for indexed image and scale colormap for correct range
Carnë Draug <carandraug+dev@gmail.com>
parents:
14260
diff
changeset
|
181 endif |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15693
diff
changeset
|
182 |
559 | 183 endfunction |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
184 |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15693
diff
changeset
|
185 |
15683
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
186 %% FIXME: Need some functional tests or %!demo blocks |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
187 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
188 %% Test input validation |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
189 %!error rgb2ind () |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
190 %!error rgb2ind (1,2) |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
191 %!error rgb2ind (1,2,3,4) |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15693
diff
changeset
|
192 |