Mercurial > hg > octave-nkf
annotate scripts/image/rgb2ind.m @ 16713:ab5d0b358b94
rgb2ind.m: fix input validation tests
* rgb2ind.m: Fix input validation tests to accomodate recent change in
input arguments.
author | Mike Miller <mtmiller@ieee.org> |
---|---|
date | Sat, 01 Jun 2013 00:53:30 -0400 |
parents | 8d32a887754a |
children | 12005245b645 |
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 ## |
16631
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
38 ## The input image @var{rgb} must be an N-dimensional RGB image |
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
39 ## (MxNxO...x3 array) where M,N,O... are the image dimensions, and the |
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
40 ## final dimension contains the values in the red, green and blue |
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
41 ## channels. Alternatively, the red, green and blue color channels can |
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
42 ## be input as separate arrays @var{R}, @var{G} and @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
|
43 ## |
16631
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
44 ## The input @var{map} defines the colormap to be used. Alternatively, |
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
45 ## @var{n} or @var{tol} may be used to define the maximum number of |
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
46 ## colors to use in an automatically generated colormap. @var{n} is |
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
47 ## related to @var{tol} by: @var{n} = (floor (1/@var{tol}) + 1)^3; |
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
|
48 ## @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
|
49 ## |
16631
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
50 ## @var{dither_option} is a string which enables or disables dithering: |
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
51 ## 'dither' (default) or 'nodither'. |
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
|
52 ## |
14260
1f911333ed3d
doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
53 ## @seealso{ind2rgb, rgb2hsv, rgb2ntsc} |
3373 | 54 ## @end deftypefn |
55 | |
3202 | 56 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
2312 | 57 ## Created: July 1994 |
58 ## Adapted-By: jwe | |
559 | 59 |
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
|
60 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
|
61 |
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 ## 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
|
63 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
|
64 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
|
65 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
|
66 |
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 ## 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
|
68 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
|
69 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
|
70 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
|
71 else |
16631
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
72 dither_option = "dither"; |
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
|
73 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
|
74 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
|
75 |
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 ## 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
|
77 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
|
78 |
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 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
|
80 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
|
81 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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 else |
16631
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
88 dither_option = "nodither"; |
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
|
89 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
|
90 |
16631
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
91 ## Read the R,G,B inputs |
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
92 elseif (nargin - dither_check==3 || nargin - dither_check==4) |
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
|
93 |
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 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
|
95 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
|
96 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
|
97 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
|
98 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
|
99 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
|
100 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
|
101 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
|
102 else |
16631
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
103 dither_option = "nodither"; |
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 |
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 rgb = reshape ([R(:), G(:), B(:)], [size(R), 3]); |
1024 | 107 |
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
|
108 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
|
109 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
|
110 |
16633
8d32a887754a
rgb2ind.m: Fix handling ND images.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
16631
diff
changeset
|
111 sz = size (rgb); |
8d32a887754a
rgb2ind.m: Fix handling ND images.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
16631
diff
changeset
|
112 |
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
|
113 ## Apply a limited colormap if required |
16631
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
114 if (exist ("option","var")) |
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
|
115 |
16631
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
116 if (size (option, 1)==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
|
117 |
16631
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
118 if (option>0 && option<=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
|
119 ## 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
|
120 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
|
121 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
|
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 ## 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
|
124 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
|
125 endif |
16631
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
126 optionstr = sprintf ("-colors %d",n); |
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
|
127 |
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 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
|
129 |
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 ## 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
|
131 map = option; |
16631
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
132 if (isequal (map(:,1),map(:,2)) || isequal (map(:,1),map(:,3)) |
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
133 || isequal (map(:,2),map(:,3))) |
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
134 error ("rgb2ind: The colormap cannot contain matching R,G, or B channels.") |
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
|
135 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
|
136 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
|
137 map = reshape (map, size (map, 1), 1, 3); |
16631
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
138 imwrite (map, fnmap, "tiff"); |
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
139 optionstr = sprintf ("-map %s", fnmap); |
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
|
140 |
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 |
16633
8d32a887754a
rgb2ind.m: Fix handling ND images.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
16631
diff
changeset
|
143 ## If image is an ND array, convert it to a tiled 2D image |
8d32a887754a
rgb2ind.m: Fix handling ND images.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
16631
diff
changeset
|
144 ## before processing it with Graphicsmagick |
8d32a887754a
rgb2ind.m: Fix handling ND images.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
16631
diff
changeset
|
145 if numel (sz)>3 |
8d32a887754a
rgb2ind.m: Fix handling ND images.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
16631
diff
changeset
|
146 rgb = reshape (rgb, [prod(sz(1:end-2)), sz(end-1), 3]); |
8d32a887754a
rgb2ind.m: Fix handling ND images.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
16631
diff
changeset
|
147 end |
8d32a887754a
rgb2ind.m: Fix handling ND images.
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents:
16631
diff
changeset
|
148 |
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
|
149 ## Prepare the Graphicsmagick dithering option |
16631
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
150 if strcmp (dither_option, "nodither") |
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
151 ditherstr = "+dither"; |
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
152 elseif strcmp (dither_option, "dither") |
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
153 ditherstr = "-dither"; |
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
|
154 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
|
155 |
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 ## 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
|
157 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
|
158 fnb = tmpnam; |
16631
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
159 imwrite (rgb, fna, "tiff"); |
c979e6db619b
rgb2ind.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
16630
diff
changeset
|
160 gmstr = sprintf ("gm convert %s %s %s %s", fna, ditherstr, optionstr, fnb); |
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
|
161 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
|
162 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
|
163 |
5922 | 164 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
165 |
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
|
166 ## 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
|
167 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
|
168 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
|
169 [map,~,x(:)] = unique (reshape(rgb, [pr, 3]), "rows"); |
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 ## 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
|
172 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
|
173 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
|
174 ## 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
|
175 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
|
176 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
|
177 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
|
178 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
|
179 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
|
180 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
|
181 endswitch |
559 | 182 |
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
|
183 ## 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
|
184 ## 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
|
185 ## 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
|
186 ## 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
|
187 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
|
188 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
|
189 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
|
190 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
|
191 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
|
192 ## 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
|
193 endif |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15693
diff
changeset
|
194 |
559 | 195 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
|
196 |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15693
diff
changeset
|
197 |
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
|
198 %% 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
|
199 |
806ea52af230
Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents:
14260
diff
changeset
|
200 %% 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
|
201 %!error rgb2ind () |
16713
ab5d0b358b94
rgb2ind.m: fix input validation tests
Mike Miller <mtmiller@ieee.org>
parents:
16633
diff
changeset
|
202 %!error rgb2ind (1,2,3,4,5,6,7) |
15714
b1cd65881592
Clean up scripts in image directory.
Rik <rik@octave.org>
parents:
15693
diff
changeset
|
203 |