# HG changeset patch
# User Motherboard <cantfind@gmail.com>
# Date 1448916433 -3600
#      Mon Nov 30 21:47:13 2015 +0100
# Node ID c1753c6e3286d343c73770f7be676673b7e80a42
# Parent  041d1634af52d87b4b315ed09ea34630d632a5c9
Added tests and demo

diff -r 041d1634af52 -r c1753c6e3286 imquantize.diff
--- a/imquantize.diff	Mon Nov 30 20:52:37 2015 +0100
+++ b/imquantize.diff	Mon Nov 30 21:47:13 2015 +0100
@@ -1,83 +1,169 @@
 # HG changeset patch
 # User Motherboard <cantfind@gmail.com>
-# Date 1448665585 -3600
-#      Sat Nov 28 00:06:25 2015 +0100
-# Node ID 405c4343c10cf4e61d9b36c27f8d70161634869b
-# Parent  1a237dfba2d0fa1ee509369be8b35db9b1b6d89b
-implemented imquantize.
+# Date 1448913157 -3600
+#      Mon Nov 30 20:52:37 2015 +0100
+# Node ID 041d1634af52d87b4b315ed09ea34630d632a5c9
+# Parent  405c4343c10cf4e61d9b36c27f8d70161634869b
+Documentation is now original, added usgae example.
+And the code no longer assumes values are in the range of 0-255.
 
-diff -r 1a237dfba2d0 -r 405c4343c10c inst/imquantize.m
+diff -r 405c4343c10c -r 041d1634af52 imquantize.diff
 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
-+++ b/inst/imquantize.m	Sat Nov 28 00:06:25 2015 +0100
-@@ -0,0 +1,70 @@
-+## Copyright (C) 2015 Master
++++ b/imquantize.diff	Mon Nov 30 20:52:37 2015 +0100
+@@ -0,0 +1,83 @@
++# HG changeset patch
++# User Motherboard <cantfind@gmail.com>
++# Date 1448665585 -3600
++#      Sat Nov 28 00:06:25 2015 +0100
++# Node ID 405c4343c10cf4e61d9b36c27f8d70161634869b
++# Parent  1a237dfba2d0fa1ee509369be8b35db9b1b6d89b
++implemented imquantize.
++
++diff -r 1a237dfba2d0 -r 405c4343c10c inst/imquantize.m
++--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++++ b/inst/imquantize.m	Sat Nov 28 00:06:25 2015 +0100
++@@ -0,0 +1,70 @@
+++## Copyright (C) 2015 Master
+++## 
+++## This program is free software; you can redistribute it and/or modify it
+++## under the terms of the GNU General Public License as published by
+++## the Free Software Foundation; either version 3 of the License, or
+++## (at your option) any later version.
+++## 
+++## This program is distributed in the hope that it will be useful,
+++## but WITHOUT ANY WARRANTY; without even the implied warranty of
+++## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+++## GNU General Public License for more details.
+++## 
+++## You should have received a copy of the GNU General Public License
+++## along with this program.  If not, see <http://www.gnu.org/licenses/>.
+++
+++## -*- texinfo -*- 
+++## @deftypefn {Library Function} {@var{quantImg} = mat} imquant (@var{img} mat, @var{levels} vector, @var{values} vector)
+++##  quant_A = imquantize(A,levels) quantizes image A using specified quantization values contained in the N element vector levels. Output image quant_A is the same size as A and contains N + 1 discrete integer values in the range 1 to N + 1 which are determined by the following criteria:
+++##  If A(k) ≤ levels(1), then quant_A(k) = 1.
+++##  If levels(m-1) < A(k)  ≤  levels(m) , then quant_A(k) = m.
+++##  If A(k) > levels(N), then quant_A(k) = N + 1.
+++##  Note that imquantize assigns values to the two implicitly defined end intervals:
+++##  A(k) ≤ levels(1)
+++##  A(k) > levels(N)
+++##  example
+++##  quant_A = imquantize(___,values) adds the N + 1 element vector values where N = length(levels). Each of the N + 1 elements of values specify the quantization value for one of the N + 1 discrete pixel values in quant_A.
+++##  If A(k) ≤ levels(1), then quant_A(k) = values(1).
+++##  If levels(m-1) < A(k)  ≤  levels(m) , then quant_A(k) = values(m).
+++##  If A(k) > levels(N), then quant_A(k) = values(N + 1).
+++##  example
+++##  [quant_A,index] = imquantize(___) returns an array index such that:
+++##  quant_A = values(index)
+++## @seealso{}
+++## @end deftypefn
+++
+++## Author: Motherboard <cantfind@gmail.com>
+++## Created: 2015-11-27
+++
+++function [quantImg, index] = imquantize (img, levels, values)
+++  N = length(levels);
+++  if ~exist("values","var")
+++    values = 1:(N + 1); %if no values were given, set values to be labels.
+++    valuesPresent = false;
+++  elseif (length(values) ~= N + 1) %if values were given, make sure we have exactly the right amount.
+++    error("values should have length of levels + 1")
+++  else
+++    valuesPresent = true; %if values were given, and everything is fine, we might need to calculate the output index.
+++  endif
+++  quantImg = zeros(size(img));
+++  if isargout(2)          %if index is required, initialize it.
+++    index = quantImg;
+++  endif
+++  if (size(levels,1) == 1)  %check if levels is a column or a row vector to concatinate it correctly with 0 on one side, and 256 on the other.
+++    levels = [0; levels; 256]; 
+++  elseif (size(levels,2) == 1)
+++    levels = [0 levels 256];
+++  else
+++    error("levels should be a vector")  %make sure levels is a vector
+++  endif
+++  for i = 1:(N+1)
+++    indicesToAssign = img >= levels(i) & img < levels(i+1);  %compute the indices to be assign.
+++    quantImg(indicesToAssign) = values(i); 
+++    if (isargout(2) && valuesPresent)   %if index is required, and values were given, assign labels to index.
+++      index(indicesToAssign) = i;
+++    endif
+++  end
+++  if (isargout(2) && ~valuesPresent) %if index is required, but values were not given, index is identical to quantImg
+++    index = quantImg;
+++  endif  
+++endfunction
++\ No newline at end of file
+diff -r 405c4343c10c -r 041d1634af52 inst/imquantize.m
+--- a/inst/imquantize.m	Sat Nov 28 00:06:25 2015 +0100
++++ b/inst/imquantize.m	Mon Nov 30 20:52:37 2015 +0100
+@@ -1,4 +1,4 @@
+-## Copyright (C) 2015 Master
++## Copyright (C) 2015 Motherboard
+ ## 
+ ## This program is free software; you can redistribute it and/or modify it
+ ## under the terms of the GNU General Public License as published by
+@@ -14,25 +14,32 @@
+ ## along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ 
+ ## -*- texinfo -*- 
+-## @deftypefn {Library Function} {@var{quantImg} = mat} imquant (@var{img} mat, @var{levels} vector, @var{values} vector)
+-##  quant_A = imquantize(A,levels) quantizes image A using specified quantization values contained in the N element vector levels. Output image quant_A is the same size as A and contains N + 1 discrete integer values in the range 1 to N + 1 which are determined by the following criteria:
+-##  If A(k) ≤ levels(1), then quant_A(k) = 1.
+-##  If levels(m-1) < A(k)  ≤  levels(m) , then quant_A(k) = m.
+-##  If A(k) > levels(N), then quant_A(k) = N + 1.
+-##  Note that imquantize assigns values to the two implicitly defined end intervals:
+-##  A(k) ≤ levels(1)
+-##  A(k) > levels(N)
+-##  example
+-##  quant_A = imquantize(___,values) adds the N + 1 element vector values where N = length(levels). Each of the N + 1 elements of values specify the quantization value for one of the N + 1 discrete pixel values in quant_A.
+-##  If A(k) ≤ levels(1), then quant_A(k) = values(1).
+-##  If levels(m-1) < A(k)  ≤  levels(m) , then quant_A(k) = values(m).
+-##  If A(k) > levels(N), then quant_A(k) = values(N + 1).
+-##  example
+-##  [quant_A,index] = imquantize(___) returns an array index such that:
+-##  quant_A = values(index)
+-## @seealso{}
+-## @end deftypefn
+-
++## @deftypefn {Library Function} {@var{quantImg} = mat, @var{index} = mat} imquantize (@var{img} mat, @var{levels} vector, @var{values} vector)
++## Usage 1 - label image with values 1:N+1, where N is length(levels).
++## Usgae form: [quantImg] = imquant(img,levels) 
++## for each pixel for which img < levels(1), quantImg is assigned the label 1.
++## similarly, when img > levels(k) and quantImg < levels(k+1), for every k = 1:N, quantImg = k;
++## and when img > levels(N), quantImg = k+1.
 +## 
-+## This program is free software; you can redistribute it and/or modify it
-+## under the terms of the GNU General Public License as published by
-+## the Free Software Foundation; either version 3 of the License, or
-+## (at your option) any later version.
-+## 
-+## This program is distributed in the hope that it will be useful,
-+## but WITHOUT ANY WARRANTY; without even the implied warranty of
-+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-+## GNU General Public License for more details.
-+## 
-+## You should have received a copy of the GNU General Public License
-+## along with this program.  If not, see <http://www.gnu.org/licenses/>.
-+
-+## -*- texinfo -*- 
-+## @deftypefn {Library Function} {@var{quantImg} = mat} imquant (@var{img} mat, @var{levels} vector, @var{values} vector)
-+##  quant_A = imquantize(A,levels) quantizes image A using specified quantization values contained in the N element vector levels. Output image quant_A is the same size as A and contains N + 1 discrete integer values in the range 1 to N + 1 which are determined by the following criteria:
-+##  If A(k) ≤ levels(1), then quant_A(k) = 1.
-+##  If levels(m-1) < A(k)  ≤  levels(m) , then quant_A(k) = m.
-+##  If A(k) > levels(N), then quant_A(k) = N + 1.
-+##  Note that imquantize assigns values to the two implicitly defined end intervals:
-+##  A(k) ≤ levels(1)
-+##  A(k) > levels(N)
-+##  example
-+##  quant_A = imquantize(___,values) adds the N + 1 element vector values where N = length(levels). Each of the N + 1 elements of values specify the quantization value for one of the N + 1 discrete pixel values in quant_A.
-+##  If A(k) ≤ levels(1), then quant_A(k) = values(1).
-+##  If levels(m-1) < A(k)  ≤  levels(m) , then quant_A(k) = values(m).
-+##  If A(k) > levels(N), then quant_A(k) = values(N + 1).
-+##  example
-+##  [quant_A,index] = imquantize(___) returns an array index such that:
-+##  quant_A = values(index)
-+## @seealso{}
-+## @end deftypefn
-+
-+## Author: Motherboard <cantfind@gmail.com>
-+## Created: 2015-11-27
-+
-+function [quantImg, index] = imquantize (img, levels, values)
-+  N = length(levels);
-+  if ~exist("values","var")
-+    values = 1:(N + 1); %if no values were given, set values to be labels.
-+    valuesPresent = false;
-+  elseif (length(values) ~= N + 1) %if values were given, make sure we have exactly the right amount.
-+    error("values should have length of levels + 1")
-+  else
-+    valuesPresent = true; %if values were given, and everything is fine, we might need to calculate the output index.
-+  endif
-+  quantImg = zeros(size(img));
-+  if isargout(2)          %if index is required, initialize it.
-+    index = quantImg;
-+  endif
-+  if (size(levels,1) == 1)  %check if levels is a column or a row vector to concatinate it correctly with 0 on one side, and 256 on the other.
-+    levels = [0; levels; 256]; 
-+  elseif (size(levels,2) == 1)
-+    levels = [0 levels 256];
-+  else
-+    error("levels should be a vector")  %make sure levels is a vector
-+  endif
-+  for i = 1:(N+1)
-+    indicesToAssign = img >= levels(i) & img < levels(i+1);  %compute the indices to be assign.
-+    quantImg(indicesToAssign) = values(i); 
-+    if (isargout(2) && valuesPresent)   %if index is required, and values were given, assign labels to index.
-+      index(indicesToAssign) = i;
-+    endif
-+  end
-+  if (isargout(2) && ~valuesPresent) %if index is required, but values were not given, index is identical to quantImg
-+    index = quantImg;
-+  endif  
-+endfunction
-\ No newline at end of file
++## Usage 2 - quantize image to given values, the values should be a vector of length N+1, where N is length(levels)
++## Usage form: [quantImg] = imquantize(img,levels,values)
++## when img < levels(1), quantImg = values(1).
++## when img > levels(k) and quantImg < levels(k+1), for every k = 1:N-1, quantImg = values(k);
++## when img > levels(N), quantImg = levels(N+1). 
++##
++## Usage form 3: [quantImg, index] = imquantize(img,levels,values)
++## Combine usage form 1, and 2 - where quantImg is the answer obtained as per usage 2
++## and index is the answer obtained as per usage 1, i.e. labeled from 1 to N+1.
++##
++## Example:
++## img = [1 2 3; 4 5 6; 7 8 9];
++## img2 = imquantize(img,[3 6]); % all values in img which are less than 3 would be labeled 1 in img2, 
++##                               % those between 3 (inclusive) and 6 would be labeled 2, and those greater than 6 (inclusive) would be labeled 3. 
++## [img3, img4] = imquantize(img,[2 5 8],[20 40 10 0]) % all values in img which are less than 2, would be assigned 20 in img3 and 1 in img4, 
++##                                                     % those between 2 (inclusive) and 5 would be assigned 40 (2 in img4), 
++##                                                     % those between 5 (inclusive) and 8 would equal 10 (3 in img4)
++##                                                     % and those greater than 8 (inclusive) would be assigned 0 (4 in img4)
++##                                                     
+ ## Author: Motherboard <cantfind@gmail.com>
+ ## Created: 2015-11-27
+ 
+@@ -51,9 +58,9 @@
+     index = quantImg;
+   endif
+   if (size(levels,1) == 1)  %check if levels is a column or a row vector to concatinate it correctly with 0 on one side, and 256 on the other.
+-    levels = [0; levels; 256]; 
++    levels = [-inf levels inf]; 
+   elseif (size(levels,2) == 1)
+-    levels = [0 levels 256];
++    levels = [-inf; levels; inf];
+   else
+     error("levels should be a vector")  %make sure levels is a vector
+   endif
diff -r 041d1634af52 -r c1753c6e3286 inst/imquantize.m
--- a/inst/imquantize.m	Mon Nov 30 20:52:37 2015 +0100
+++ b/inst/imquantize.m	Mon Nov 30 21:47:13 2015 +0100
@@ -74,4 +74,46 @@
   if (isargout(2) && ~valuesPresent) %if index is required, but values were not given, index is identical to quantImg
     index = quantImg;
   endif  
-endfunction
\ No newline at end of file
+endfunction
+
+%!test
+%! img = [1:10;11:20;21:30;31:40;41:50;51:60;61:70];
+%! [img2,img3] = imquantize(img,[5 15 25 30 40 60],[0 1 5 10 20 30 15]);
+%! assert(img2(1,1) == 0)
+%! assert(img2(1,6) == 1)
+%! assert(img2(2,5) == 5)
+%! assert(img2(6,10) == 15)
+%! assert(img2(7,1) == 15)
+%! assert(img3(1,1) == 1)
+%! assert(img3(1,6) == 2)
+%! assert(img3(2,5) == 3)
+%! assert(img3(6,10) == 7)
+%! assert(img3(7,1) == 7)
+
+%!test
+%! img = [-inf 0 10000000; -100000 -3 1/1000000; 5 5 10];
+%! img2 = imquantize(img, 5); %simple threshold
+%! assert(img2(1,1) == 1)
+%! assert(img2(1,2) == 1)
+%! assert(img2(1,3) == 2)
+%! assert(img2(2,1) == 1)
+%! assert(img2(3,1) == 2)
+%! assert(img2(3,3) == 2)
+
+%!demo
+%! img = [1:10;11:20;21:30;31:40;41:50;51:60;61:70];
+%! [img2,img3] = imquantize(img,[5 15 25 30 40 60],[0 1 5 10 20 30 15]);
+%! subplot(2,2,1);
+%! imagesc(img);
+%! title('original image');
+%! colorbar();
+%! subplot(2,2,2);
+%! imagesc(img2);
+%! title('quantized image with costum values');
+%! colorbar();
+%! subplot(2,2,3);
+%! imagesc(img3);
+%! title('quantized image with labels');
+%! colorbar();
+%! #----------------------------------------------
+%! #Plot quantized image.
\ No newline at end of file
