annotate scripts/image/ind2rgb.m @ 17118:c97a26408ee0

Implement PixelRegion option for imread(). * imread.m: document new option. * private/__imread__.m: parse new option and set defaults. * __magick_read__.cc (calculate region): new option to calculate the region to be read, shifts in memory required, and output image size. (read_indexed_images, read_images): implement reading of only specific regions of an image. (__magick_read__): get octave_scalar_map at start for simplicity.
author Carnë Draug <carandraug@octave.org>
date Wed, 31 Jul 2013 21:28:48 +0100
parents a4969508008e
children d63878346099
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 13279
diff changeset
1 ## Copyright (C) 1994-2012 John W. Eaton
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
2 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
3 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
4 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6247
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6247
diff changeset
8 ## your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
9 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
13 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
14 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6247
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6247
diff changeset
17 ## <http://www.gnu.org/licenses/>.
1024
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 917
diff changeset
18
3381
69b167451491 [project @ 1999-12-15 20:48:10 by jwe]
jwe
parents: 3373
diff changeset
19 ## -*- texinfo -*-
15712
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
20 ## @deftypefn {Function File} {@var{rgb} =} ind2rgb (@var{x}, @var{map})
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
21 ## @deftypefnx {Function File} {[@var{R}, @var{G}, @var{B}] =} ind2rgb (@var{x}, @var{map})
2311
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
22 ## Convert an indexed image to red, green, and blue color components.
15712
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
23 ##
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
24 ## The image @var{x} must be an indexed image which will be converted using the
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
25 ## colormap @var{map}. If @var{map} does not contain enough colors for the
15712
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
26 ## image, pixels in @var{x} outside the range are mapped to the last color in
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
27 ## the map.
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 15515
diff changeset
28 ##
16826
a4969508008e doc: Periodic spellcheck of the documentation.
Rik <rik@octave.org>
parents: 16816
diff changeset
29 ## The output may be a single RGB image (@nospell{MxNx3} matrix where M and N
a4969508008e doc: Periodic spellcheck of the documentation.
Rik <rik@octave.org>
parents: 16816
diff changeset
30 ## are the original image @var{x} dimensions, one for each of the red, green
a4969508008e doc: Periodic spellcheck of the documentation.
Rik <rik@octave.org>
parents: 16816
diff changeset
31 ## and blue channels). Alternatively, the individual red, green, and blue
a4969508008e doc: Periodic spellcheck of the documentation.
Rik <rik@octave.org>
parents: 16816
diff changeset
32 ## color matrices of size @nospell{MxN} may be returned.
15712
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
33 ##
16826
a4969508008e doc: Periodic spellcheck of the documentation.
Rik <rik@octave.org>
parents: 16816
diff changeset
34 ## Multi-dimensional indexed images (of size @nospell{MxNx1xK}) are also
a4969508008e doc: Periodic spellcheck of the documentation.
Rik <rik@octave.org>
parents: 16816
diff changeset
35 ## supported.
16707
4c11e9bcb796 ind2rgb: add support for multidimensional indexed images.
Carnë Draug <carandraug@octave.org>
parents: 16434
diff changeset
36 ##
14260
1f911333ed3d doc: Update docstrings for functions in image/ directory
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
37 ## @seealso{rgb2ind, ind2gray, hsv2rgb, ntsc2rgb}
3373
36405da8e173 [project @ 1999-11-23 20:54:17 by jwe]
jwe
parents: 3202
diff changeset
38 ## @end deftypefn
904
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 559
diff changeset
39
3202
44d82b369c78 [project @ 1998-10-29 20:27:57 by jwe]
jwe
parents: 2847
diff changeset
40 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
2312
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
41 ## Created: July 1994
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
42 ## Adapted-By: jwe
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
43
15712
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
44 function [R, G, B] = ind2rgb (x, map)
1024
56520a75b5b3 [project @ 1995-01-11 20:30:04 by jwe]
jwe
parents: 917
diff changeset
45
15712
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
46 if (nargin != 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5775
diff changeset
47 print_usage ();
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
48 endif
15689
14b7679891dd ind2sub ind2gray: merge common code in private function, expanding fix for images when integers, input check, and expansion of colormap to ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15688
diff changeset
49 [x, map] = ind2x ("ind2rgb", x, map);
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
50
6247
7b04118f04df [project @ 2007-01-19 03:21:16 by jwe]
jwe
parents: 6046
diff changeset
51 ## Compute result
16434
aa81cfa5c359 ind2x.m and ind2rgb.m: Enable handling of ND images
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents: 15714
diff changeset
52 sz = size (x);
aa81cfa5c359 ind2x.m and ind2rgb.m: Enable handling of ND images
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents: 15714
diff changeset
53 R = reshape (map(x(:), 1), sz);
aa81cfa5c359 ind2x.m and ind2rgb.m: Enable handling of ND images
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents: 15714
diff changeset
54 G = reshape (map(x(:), 2), sz);
aa81cfa5c359 ind2x.m and ind2rgb.m: Enable handling of ND images
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents: 15714
diff changeset
55 B = reshape (map(x(:), 3), sz);
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
56
16434
aa81cfa5c359 ind2x.m and ind2rgb.m: Enable handling of ND images
Adam H Aitkenhead <adamhaitkenhead@gmail.com>
parents: 15714
diff changeset
57 ## Use ND array if only one output is requested.
6247
7b04118f04df [project @ 2007-01-19 03:21:16 by jwe]
jwe
parents: 6046
diff changeset
58 if (nargout <= 1)
16707
4c11e9bcb796 ind2rgb: add support for multidimensional indexed images.
Carnë Draug <carandraug@octave.org>
parents: 16434
diff changeset
59 if (ndims (x) == 2)
4c11e9bcb796 ind2rgb: add support for multidimensional indexed images.
Carnë Draug <carandraug@octave.org>
parents: 16434
diff changeset
60 R = reshape ([R(:); G(:); B(:)], [sz, 3]);
4c11e9bcb796 ind2rgb: add support for multidimensional indexed images.
Carnë Draug <carandraug@octave.org>
parents: 16434
diff changeset
61 elseif (ndims (x) == 4)
4c11e9bcb796 ind2rgb: add support for multidimensional indexed images.
Carnë Draug <carandraug@octave.org>
parents: 16434
diff changeset
62 R = permute (reshape ([R(:); G(:); B(:)], [sz(1) sz(2) sz(4) 3]), [1 2 4 3]);
4c11e9bcb796 ind2rgb: add support for multidimensional indexed images.
Carnë Draug <carandraug@octave.org>
parents: 16434
diff changeset
63 else
4c11e9bcb796 ind2rgb: add support for multidimensional indexed images.
Carnë Draug <carandraug@octave.org>
parents: 16434
diff changeset
64 ## we should never reach here since ind2x() should filter them out
4c11e9bcb796 ind2rgb: add support for multidimensional indexed images.
Carnë Draug <carandraug@octave.org>
parents: 16434
diff changeset
65 error ("ind2rgb: an indexed image must have 2 or 4 dimensions.");
4c11e9bcb796 ind2rgb: add support for multidimensional indexed images.
Carnë Draug <carandraug@octave.org>
parents: 16434
diff changeset
66 endif
6247
7b04118f04df [project @ 2007-01-19 03:21:16 by jwe]
jwe
parents: 6046
diff changeset
67 endif
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 15515
diff changeset
68
559
4e826edfbc56 [project @ 1994-07-25 22:18:28 by jwe]
jwe
parents:
diff changeset
69 endfunction
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 15515
diff changeset
70
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
71
15688
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
72 %!shared img, map, ergb, rgb, r, g, b
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
73 %! img = [2 4 5; 3 2 5; 1 2 4];
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
74 %! map = [0.0 0.0 0.0
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
75 %! 0.2 0.4 0.6
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
76 %! 0.4 0.4 0.5
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
77 %! 0.3 0.7 1.0
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
78 %! 0.1 0.5 0.8];
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
79 %! ergb(:,:,1) = [0.2 0.3 0.1; 0.4 0.2 0.1; 0.0 0.2 0.3];
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
80 %! ergb(:,:,2) = [0.4 0.7 0.5; 0.4 0.4 0.5; 0.0 0.4 0.7];
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
81 %! ergb(:,:,3) = [0.6 1.0 0.8; 0.5 0.6 0.8; 0.0 0.6 1.0];
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
82 %! ## test basic usage with 1 and 3 outputs
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
83 %! [rgb] = ind2rgb (img, map);
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
84 %! [r, g, b] = ind2rgb (img, map);
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
85 %!assert (ergb, rgb);
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
86 %!assert (ergb, reshape ([r(:) g(:) b(:)], [size(img) 3]));
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
87 %! ## test correction for integers
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
88 %! img = uint8 (img -1);
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
89 %! [rgb] = ind2rgb (img, map);
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
90 %!assert (ergb, rgb);
4db08f52a6ed ind2rgb: correction for indexes when image is uint class. Added tests block.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15515
diff changeset
91 %! ## check it fails when image is a float with an index value of 0
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
92 %!fail ("[rgb] = ind2rgb (double(img), map)")
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 15515
diff changeset
93
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
94 %% Test input validation
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 15515
diff changeset
95 %!error ind2rgb ()
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 15515
diff changeset
96 %!error ind2rgb (1,2,3)
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
97 %!error <X must be an indexed image> ind2rgb ({1}, jet (64))
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
98 %!error <X must be an indexed image> ind2rgb (1+i, jet (64))
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
99 %!error <X must be an indexed image> ind2rgb (sparse (1), jet (64))
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
100 %!error <X must be an indexed image> ind2rgb (0, jet (64))
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
101 %!error <X must be an indexed image> ind2rgb (1.1, jet (64))
15683
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 15515
diff changeset
102 %!error <MAP must be a valid colormap> ind2rgb (1, {1})
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 15515
diff changeset
103 %!error <MAP must be a valid colormap> ind2rgb (1, 1+i)
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 15515
diff changeset
104 %!error <MAP must be a valid colormap> ind2rgb (1, ones (2,2,2))
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 15515
diff changeset
105 %!error <MAP must be a valid colormap> ind2rgb (1, ones (2,4))
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 15515
diff changeset
106 %!error <MAP must be a valid colormap> ind2rgb (1, [-1])
806ea52af230 Overhaul m-files in image directory to provide better support for images stored as integers.
Rik <rik@octave.org>
parents: 15515
diff changeset
107 %!error <MAP must be a valid colormap> ind2rgb (1, [2])
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
108