annotate scripts/image/private/ind2x.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 4c11e9bcb796
children abf384f5d243
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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:
diff changeset
1 ## Copyright (C) 1994-2012 John W. Eaton
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:
diff changeset
2 ## Copyright (C) 2012 Carnë Draug
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:
diff changeset
3 ##
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:
diff changeset
4 ## This file is part of Octave.
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:
diff changeset
5 ##
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:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
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:
diff changeset
7 ## under the terms of the GNU General Public License as published by
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:
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
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:
diff changeset
9 ## your option) any later version.
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:
diff changeset
10 ##
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:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
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:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
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:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
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:
diff changeset
14 ## General Public License for more details.
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:
diff changeset
15 ##
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:
diff changeset
16 ## You should have received a copy of the GNU General Public License
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:
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
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:
diff changeset
18 ## <http://www.gnu.org/licenses/>.
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:
diff changeset
19
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
20 ## private function for the ind2XXX functions which have a lot of code in common
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:
diff changeset
21
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
22 function [x, map] = ind2x (caller, x, map)
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:
diff changeset
23
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:
diff changeset
24 ## Check if X is an indexed image.
16707
4c11e9bcb796 ind2rgb: add support for multidimensional indexed images.
Carnë Draug <carandraug@octave.org>
parents: 16434
diff changeset
25 ## an indexed image is defined has having only 2D, and that's how matlab
4c11e9bcb796 ind2rgb: add support for multidimensional indexed images.
Carnë Draug <carandraug@octave.org>
parents: 16434
diff changeset
26 ## behaves. But we want to support ND images, so we will allow up to 4D
4c11e9bcb796 ind2rgb: add support for multidimensional indexed images.
Carnë Draug <carandraug@octave.org>
parents: 16434
diff changeset
27 ## and check that the 3rd is a singleton
4c11e9bcb796 ind2rgb: add support for multidimensional indexed images.
Carnë Draug <carandraug@octave.org>
parents: 16434
diff changeset
28 if (all (ndims (x) != [2 4]) || size (x, 3) != 1 || issparse (x) ||
4c11e9bcb796 ind2rgb: add support for multidimensional indexed images.
Carnë Draug <carandraug@octave.org>
parents: 16434
diff changeset
29 (isfloat (x) && ! isindex (x)) ||
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
30 ! any (strcmp (class (x), {"uint8", "uint16", "single", "double"})))
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
31 error ("%s: X must be an indexed image", caller);
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:
diff changeset
32 endif
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:
diff changeset
33
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
34 ## Check if map is a valid colormap.
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:
diff changeset
35 if (! iscolormap (map))
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
36 error ("%s: MAP must be a valid colormap", caller);
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:
diff changeset
37 endif
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:
diff changeset
38
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:
diff changeset
39 ## Do we have enough colors in the color map?
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:
diff changeset
40 ## there's an offset of 1 when the indexed image is an integer class so we fix
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:
diff changeset
41 ## it now and convert it to float only if really necessary and even then only
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
42 ## to single precision since that is enough for both uint8 and uint16.
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:
diff changeset
43 maxidx = max (x(:));
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:
diff changeset
44 if (isinteger (x))
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:
diff changeset
45 if (maxidx == intmax (class (x)))
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:
diff changeset
46 x = single (x);
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:
diff changeset
47 endif
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:
diff changeset
48 x += 1;
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:
diff changeset
49 maxidx += 1;
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:
diff changeset
50 endif
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:
diff changeset
51
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
52 num_colors = rows (map);
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
53 if (num_colors < maxidx)
15712
74ff287f41cb Remove default colormap for ind2rgb and ind2gray.
Carnë Draug <carandraug+dev@gmail.com>
parents: 15693
diff changeset
54 ## Pad with the last color in the map for matlab compatibility
15714
b1cd65881592 Clean up scripts in image directory.
Rik <rik@octave.org>
parents: 15712
diff changeset
55 pad = repmat (map(end,:), maxidx - num_colors, 1);
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:
diff changeset
56 map(end+1:maxidx, :) = pad;
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:
diff changeset
57 endif
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:
diff changeset
58
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:
diff changeset
59 endfunction