Mercurial > octave-antonio
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 |
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 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
1024 | 18 |
3381 | 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 | 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 | 38 ## @end deftypefn |
904 | 39 |
3202 | 40 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
2312 | 41 ## Created: July 1994 |
42 ## Adapted-By: jwe | |
559 | 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 | 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 | 47 print_usage (); |
559 | 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 | 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 | 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 | 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 | 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 | 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 |