Mercurial > octave-nkf
comparison scripts/image/gray2ind.m @ 15714:b1cd65881592
Clean up scripts in image directory.
Use Octave coding conventions. Redo docstrings. Add %!tests.
* brighten.m: Put input validation first. Use iscolormap to simplify
input checking.
* cmunique.m: Use faster method of validating input class.
* colormap.m: Tweak docstring. Improve input validation.
* contrast.m: Tweak docstring. Use cmap instead of map as variable
name for clarity.
* gray2ind.m: Wrap long lines. Use faster method of validating input class.
Delete unreachable code for n>65536.
* hsv2rgb.m: Use faster method of validating input class.
* imwrite.m: Tweak FIXME notes.
* ind2gray.m: Use correct caller name for ind2x. Update %!tests
with new 2-input calling convention.
* ind2rgb.m: Tweak docstring. Update %!tests with new 2-input
calling convention.
* iscolormap.m: Tweak docstring. Re-order validation tests.
* ntsc2rgb.m: Use faster method of validating input class. Better input
validation. Add %!tests.
* private/ind2x.m: Use more descriptive variable names.
* rgb2hsv.m: Tweak docstring. Use faster method of validating input class.
* rgb2ind.m: Tweak docstring. Wrap long lines.
* rgb2ntsc.m: Use faster method of validating input class. Improve input
validation. Add %!tests.
* rgbplot.m: Match variable names in docstring to those in function
prototype.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 02 Dec 2012 10:02:57 -0800 |
parents | a1b634240352 |
children | 3dec0a57ab55 |
comparison
equal
deleted
inserted
replaced
15713:168e380c8f18 | 15714:b1cd65881592 |
---|---|
26 ## | 26 ## |
27 ## The indexed image will consist of @var{n} different intensity values. | 27 ## The indexed image will consist of @var{n} different intensity values. |
28 ## If not given @var{n} defaults to 64 for grayscale images or 2 for | 28 ## If not given @var{n} defaults to 64 for grayscale images or 2 for |
29 ## binary black and white images. | 29 ## binary black and white images. |
30 ## | 30 ## |
31 ## The output @var{img} is of class uint8 or uint 16 if @var{n} is less than or | 31 ## The output @var{img} is of class uint8 if @var{n} is less than or |
32 ## equal to 256 or 65536 respectively. Otherwise, the output is of class double. | 32 ## equal to 256; Otherwise the return class is uint16. |
33 ## @seealso{ind2gray, rgb2ind} | 33 ## @seealso{ind2gray, rgb2ind} |
34 ## @end deftypefn | 34 ## @end deftypefn |
35 | 35 |
36 ## Author: Tony Richardson <arichard@stark.cc.oh.us> | 36 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
37 ## Created: July 1994 | 37 ## Created: July 1994 |
44 elseif (! isreal (I) || issparse (I)) | 44 elseif (! isreal (I) || issparse (I)) |
45 error ("gray2ind: I must be a grayscale or binary image"); | 45 error ("gray2ind: I must be a grayscale or binary image"); |
46 elseif (! isscalar (n) || n < 1 || n > 65536) | 46 elseif (! isscalar (n) || n < 1 || n > 65536) |
47 error ("gray2ind: N must be a positive integer in the range [1, 65536]"); | 47 error ("gray2ind: N must be a positive integer in the range [1, 65536]"); |
48 elseif (! ismatrix (I) || ndims (I) != 2) | 48 elseif (! ismatrix (I) || ndims (I) != 2) |
49 error ("gray2ind: first input argument must be a gray scale image"); | 49 error ("gray2ind: I must be a grayscale or binary image"); |
50 endif | 50 endif |
51 | 51 |
52 ## default n is different if image is logical | 52 ## default n is different if image is logical |
53 if (nargin == 1 && islogical (I)) | 53 if (nargin == 1 && islogical (I)) |
54 n = 2; | 54 n = 2; |
55 endif | 55 endif |
56 | 56 |
57 if (! isscalar (n) || n < 0) | |
58 error ("gray2ind: second input argument must be a positive integer"); | |
59 endif | |
60 | |
61 cls = class (I); | 57 cls = class (I); |
62 if (! any (isa (I, {"logical", "uint8", "uint16", "int16", "single", "double"}))) | 58 if (! any (strcmp (cls, {"logical", "uint8", "uint16", "int16", "single", "double"}))) |
63 error ("gray2ind: invalid data type '%s'", cls); | 59 error ("gray2ind: invalid data type '%s'", cls); |
64 elseif (isfloat (I) && (min (I(:) < 0) || max (I(:) > 1))) | 60 elseif (isfloat (I) && (min (I(:) < 0) || max (I(:) > 1))) |
65 error ("gray2ind: floating point images may only contain values between 0 and 1"); | 61 error ("gray2ind: floating point images may only contain values between 0 and 1"); |
66 endif | 62 endif |
67 | 63 |
73 scale = double (intmax (cls)) - low; | 69 scale = double (intmax (cls)) - low; |
74 I = double (I) - low; | 70 I = double (I) - low; |
75 else | 71 else |
76 scale = 1; | 72 scale = 1; |
77 endif | 73 endif |
74 I *= (n-1)/scale; | |
75 | |
78 ## Note: no separate call to round () necessary because | 76 ## Note: no separate call to round () necessary because |
79 ## type conversion does that automatically. | 77 ## type conversion does that automatically. |
80 I = I * ((n-1)/scale); | |
81 if (n < 256) | 78 if (n < 256) |
82 I = uint8 (I); | 79 I = uint8 (I); |
83 elseif (n < 65536) | 80 else |
84 I = uint16 (I); | 81 I = uint16 (I); |
85 else | |
86 ## if uint16 is not enough, we return double in which case index | |
87 ## values should start at 1. | |
88 I = round (I) + 1; | |
89 endif | 82 endif |
83 | |
90 endfunction | 84 endfunction |
85 | |
91 | 86 |
92 %!assert (gray2ind ([0 0.25 0.5 1]), uint8 ([0 16 32 63])) | 87 %!assert (gray2ind ([0 0.25 0.5 1]), uint8 ([0 16 32 63])) |
93 %!assert (gray2ind ([0 0.25 0.5 1], 400), uint16 ([0 100 200 399])) | 88 %!assert (gray2ind ([0 0.25 0.5 1], 400), uint16 ([0 100 200 399])) |
94 %!assert (gray2ind (logical ([1 0 0 1])), uint8 ([1 0 0 1])) | 89 %!assert (gray2ind (logical ([1 0 0 1])), uint8 ([1 0 0 1])) |
95 %!assert (gray2ind (uint8 ([0 64 128 192 255])), uint8 ([0 16 32 47 63])) | 90 %!assert (gray2ind (uint8 ([0 64 128 192 255])), uint8 ([0 16 32 47 63])) |