Mercurial > hg > octave-image
annotate inst/imadd.m @ 857:75d6748324de
New functions imgradient and imgradientxy (patch #8265)
* imgradient.m, imgradientxy.m: new function files.
* INDEX: update list of functions.
* NEWS: update list of new functinos for next release.
author | Brandon Miles <brandon.miles7@gmail.com> |
---|---|
date | Sun, 05 Jan 2014 07:27:06 +0000 |
parents | c45838839d86 |
children |
rev | line source |
---|---|
514 | 1 ## Copyright (C) 2011 Carnë Draug <carandraug+dev@gmail.com> |
2 ## | |
561
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
526
diff
changeset
|
3 ## This program is free software; you can redistribute it and/or modify it under |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
526
diff
changeset
|
4 ## the terms of the GNU General Public License as published by the Free Software |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
526
diff
changeset
|
5 ## Foundation; either version 3 of the License, or (at your option) any later |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
526
diff
changeset
|
6 ## version. |
514 | 7 ## |
561
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
526
diff
changeset
|
8 ## This program is distributed in the hope that it will be useful, but WITHOUT |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
526
diff
changeset
|
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
526
diff
changeset
|
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
526
diff
changeset
|
11 ## details. |
514 | 12 ## |
561
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
526
diff
changeset
|
13 ## You should have received a copy of the GNU General Public License along with |
c45838839d86
maint: update license to GPLv3 and mention non GPL files
carandraug
parents:
526
diff
changeset
|
14 ## this program; if not, see <http://www.gnu.org/licenses/>. |
514 | 15 |
16 ## -*- texinfo -*- | |
17 ## @deftypefn {Function File} {@var{out} =} imadd (@var{a}, @var{b}) | |
18 ## @deftypefnx {Function File} {@var{out} =} imadd (@var{a}, @var{b}, @var{class}) | |
19 ## Add image or constant to an image. | |
20 ## | |
21 ## If @var{a} and @var{b} are two images of same size and class, the images are | |
22 ## added. Alternatively, if @var{b} is a floating-point scalar, its value is added | |
23 ## to the image @var{a}. | |
24 ## | |
25 ## The class of @var{out} will be the same as @var{a} unless @var{a} is logical | |
522
1334711928b6
image arithmetic functions: more matlab compatibility, test cases, better help text, bug fixes
carandraug
parents:
521
diff
changeset
|
26 ## in which case @var{out} will be double. Alternatively, it can be |
514 | 27 ## specified with @var{class}. |
28 ## | |
518 | 29 ## @emph{Note 1}: you can force output class to be logical by specifying |
516
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
30 ## @var{class}. This is incompatible with @sc{matlab} which will @emph{not} honour |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
31 ## request to return a logical matrix. |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
32 ## |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
33 ## @emph{Note 2}: the values are truncated to the maximum value of the output |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
34 ## class. |
526 | 35 ## @seealso{imabsdiff, imcomplement, imdivide, imlincomb, immultiply, imsubtract} |
514 | 36 ## @end deftypefn |
37 | |
38 function img = imadd (img, val, out_class = class (img)) | |
39 | |
40 if (nargin < 2 || nargin > 3) | |
41 print_usage; | |
42 endif | |
517
0aadba8eb90c
image arithmetics: create private function that deals with input check and preparation for imadd and future imsubtract/divide/etc
carandraug
parents:
516
diff
changeset
|
43 [img, val] = imarithmetics ("imadd", img, val, out_class); |
514 | 44 |
45 ## output class is the same as input img, unless img is logical in which case | |
516
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
46 ## it should be double. Tested in matlab by Freysh at ##matlab: |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
47 ## - if you imadd 2 logical matrix, it's not the union. You actually get values of 2 |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
48 ## - the previous is true even if you specify "logical" as output class. It does |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
49 ## not honors the request, output will be double class anyway, not even a |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
50 ## warning will be issued (but we in octave are nicer and will) |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
51 ## - you can specify smaller integer types for output than input and values |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
52 ## are truncated. Input uint16 and request uint8, it will be respected |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
53 |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
54 ## this is matlab imcompatible on purpose. We are compatible and return double |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
55 ## anyway, even if both input are logical (and wether this is correct is |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
56 ## already debatable), but if the user forcelly requests output class to be |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
57 ## logical, then he must be expecting it (matlab returns double anyway and |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
58 ## ignores request). |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
59 |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
60 if (nargin > 2 && strcmpi (out_class, "logical")) |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
61 img = img | val; |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
62 else |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
63 img = img + val; |
3b9a3e0a4bf9
imadd: note about matlab compatibility, added support for signed integers, and implement non-matlab where it actually returns double if requested by user
carandraug
parents:
514
diff
changeset
|
64 endif |
514 | 65 |
66 endfunction | |
522
1334711928b6
image arithmetic functions: more matlab compatibility, test cases, better help text, bug fixes
carandraug
parents:
521
diff
changeset
|
67 |
1334711928b6
image arithmetic functions: more matlab compatibility, test cases, better help text, bug fixes
carandraug
parents:
521
diff
changeset
|
68 %!assert (imadd (uint8 ([23 250]), uint8 ([23 250])), uint8 ([46 255])); # default to first class and truncate |
1334711928b6
image arithmetic functions: more matlab compatibility, test cases, better help text, bug fixes
carandraug
parents:
521
diff
changeset
|
69 %!assert (imadd (uint8 ([23 250]), 10), uint8 ([33 255])); # works adding a scalar |
1334711928b6
image arithmetic functions: more matlab compatibility, test cases, better help text, bug fixes
carandraug
parents:
521
diff
changeset
|
70 %!assert (imadd (uint8 ([23 250]), uint8 ([23 250]), "uint16"), uint16 ([46 500])); # defining output class works |
1334711928b6
image arithmetic functions: more matlab compatibility, test cases, better help text, bug fixes
carandraug
parents:
521
diff
changeset
|
71 %!assert (imadd (logical ([ 1 0]), logical ([ 1 1])), double ([ 2 1])); # return double for two logical images |
1334711928b6
image arithmetic functions: more matlab compatibility, test cases, better help text, bug fixes
carandraug
parents:
521
diff
changeset
|
72 %!assert (imadd (logical ([ 1 0]), logical ([ 1 1]), "logical"), logical ([ 1 1])); # this is matlab incompatible on purpose |
524 | 73 %!fail ("imadd (uint8 ([23 250]), uint16 ([23 250]))"); # input need to have same class |