]> Creatis software - CreaPhase.git/blob - octave_packages/image-1.0.15/isrgb.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / image-1.0.15 / isrgb.m
1 ## Copyright (C) 2004 Josep Mones i Teixidor
2 ##
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 2 of the License, or
6 ## (at your option) any later version.
7 ##
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 ## GNU General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU General Public License
14 ## along with this program; If not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {@var{flag} = } isrgb (@var{A})
18 ## Returns true if parameter is a RGB image.
19 ##
20 ## @code{flag=isrgb(A)} returns 1 if @var{A} is a RGB image and 0 if
21 ## not.
22 ##
23 ## To the decide @code{isrgb} uses the follow algorithm:
24 ## @itemize @bullet
25 ## @item
26 ## If @var{A} is of class double then it checks if all values are
27 ## between 0 and 1, and if size is m-by-n-by-3.
28 ## @item
29 ## If @var{A} is of class uint16, uint8 or logical then it checks is m-by-n-by-3.
30 ## @end itemize
31 ##
32 ## @strong{Compatibility notes:}
33 ##
34 ## Information needed on whether MATLAB accepts logical arrays as RGB
35 ## images (now this functions accepts them if they are m-by-n-by-3 arrays.
36 ##
37 ## @end deftypefn
38
39 ## TODO: Check if logical arrays should be considered RGB
40
41 ## Author:  Josep Mones i Teixidor <jmones@puntbarra.com>
42
43 function flag = isrgb(A)
44   if (nargin!=1)
45     usage("flag=isrgb(A)");
46   endif
47   
48   if(ismatrix(A))
49     flag=1;
50     s=size(A);
51     if(length(s)!=3 || s(3)!=3)
52       flag=0; ## false if not m-by-n-by-3
53     elseif(strcmp(typeinfo(A),"matrix") && (any(A(:)<0) || any(A(:)>1)))
54       flag=0; ## false if class double but items are <0 or >1
55     endif
56   else
57     flag=0;
58   endif
59 endfunction
60
61
62 %!demo
63 %! isrgb(rand(1,2,3))
64 %! # A 1-by-2-by-3 double matrix with elements between 0 and 1 is a RGB image.
65
66
67 %!# Non-matrix
68 %!assert(isrgb("this is not a RGB image"),0);
69
70 %!# Double matrix tests
71 %!assert(isrgb(rand(5,5)),0);
72 %!assert(isrgb(rand(5,5,1,5)),0);
73 %!assert(isrgb(rand(5,5,3,5)),0);
74 %!assert(isrgb(rand(5,5,3)),1);
75 %!assert(isrgb(ones(5,5,3)),1);
76 %!assert(isrgb(ones(5,5,3)+.0001),0);
77 %!assert(isrgb(zeros(5,5,3)-.0001),0);
78
79 %!# Logical
80 %!assert(isrgb(logical(round(rand(5,5,3)))),1);