This file is indexed.

/usr/share/octave/packages/image-2.4.1/imcast.m is in octave-image 2.4.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
## Copyright (C) 2014 Carnë Draug <carandraug+dev@gmail.com>
##
## 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  {Function File} {} imcast (@var{img}, @var{type})
## @deftypefnx {Function File} {} imcast (@var{img}, @var{type}, "indexed")
## Convert image to specific data type @var{type}.
##
## Converts a valid image @var{img} into another class @var{type}.  A valid
## image must be of class logical, uint8, uint16, int16, single, or double.
## Conversion of images of class logical is valid, but not the inverse, i.e.,
## conversion of images into logical class is not supported. Use of
## @code{im2bw} is recommended for such cases.
##
## If the image is indexed, the last argument may be the string
## @qcode{"indexed"}.  An indexed image may not be of class int16 or
## single (see @code{isind} for details).
##
## Details on how the conversion is performed is class dependent, and can
## be seen on the help text of @code{im2double}, @code{im2single},
## @code{im2uint8}, @code{im2uint16}, and @code{im2int16}.
##
## @seealso{im2bw, im2uint8, im2double, im2int16, im2single, im2uint16}
## @end deftypefn

function imout = imcast (img, outcls, varargin)

  if (nargin < 2 || nargin > 3)
    print_usage ();
  elseif (nargin == 3 && ! strcmpi (varargin{1}, "indexed"))
    error ("imcast: third argument must be the string \"indexed\"");
  endif

  incls = class (img);
  if (strcmp (outcls, incls))
    imout = img;
    return
  endif

  ## we are dealing with indexed images
  if (nargin == 3)
    if (! isind (img))
      error ("imcast: input should have been an indexed image but it is not.");
    endif

    ## Check that the new class is enough to hold all the previous indices
    ## If we are converting to floating point, then we don't bother
    ## check the range of indices. Also, note that indexed images of
    ## integer class are always unsigned.

    ## we will be converting a floating point image to integer class
    if (strcmp (outcls, "single") || strcmp (outcls, "double"))
      if (isinteger (img))
        imout = cast (img, outcls) +1;
      else
        imout = cast (img, outcls);
      endif

    ## we will be converting an indexed image to integer class
    else
      if (isinteger (img) && intmax (incls) > intmax (outcls) && max (img(:)) > intmax (outcls))
          error ("imcast: IMG has too many colours '%d' for the range of values in %s",
            max (img(:)), outcls);
      elseif (isfloat (img))
        imax = max (img(:)) -1;
        if (imax > intmax (outcls))
          error ("imcast: IMG has too many colours '%d' for the range of values in %s",
            imax, outcls);
        endif
        img -= 1;
      endif
      imout = cast (img, outcls);
    endif

  ## we are dealing with "normal" images
  else
    problem = false; # did we found a bad conversion?
    switch (incls)

      case {"double", "single"}
        switch (outcls)
          case "uint8",  imout = uint8  (img * 255);
          case "uint16", imout = uint16 (img * 65535);
          case "int16",  imout = int16 (double (img * uint16 (65535)) -32768);
          case {"double", "single"}, imout = cast (img, outcls);
          otherwise, problem = true;
        endswitch

      case {"uint8"}
        switch (outcls)
          case "double", imout = double (img) / 255;
          case "single", imout = single (img) / 255;
          case "uint16", imout = uint16 (img) * 257; # 257 comes from 65535/255
          case "int16",  imout = int16 ((double (img) * 257) -32768); # 257 comes from 65535/255
          otherwise, problem = true;
        endswitch

      case {"uint16"}
        switch (outcls)
          case "double", imout = double (img) / 65535;
          case "single", imout = single (img) / 65535;
          case "uint8",  imout = uint8 (img / 257); # 257 comes from 65535/255
          case "int16",  imout = int16 (double (img) -32768);
          otherwise, problem = true;
        endswitch

      case {"logical"}
        switch (outcls)
          case {"double", "single"}
            imout = cast (img, outcls);
          case {"uint8", "uint16", "int16"}
            imout = repmat (intmin (outcls), size (img));
            imout(img) = intmax (outcls);
          otherwise
            problem = true;
        endswitch

      case {"int16"}
        switch (outcls)
          case "double", imout = (double (img) + 32768) / 65535;
          case "single", imout = (single (img) + 32768) / 65535;
          case "uint8",  imout = uint8 ((double (img) + 32768) / 257); # 257 comes from 65535/255
          case "uint16", imout = uint16 (double (img) + 32768);
          otherwise, problem = true;
        endswitch

      otherwise
        error ("imcast: unknown image of class \"%s\"", incls);

    endswitch
    if (problem)
      error ("imcast: unsupported TYPE \"%s\"", outcls);
    endif
  endif

endfunction

%!test
%! im = randi ([0 255], 40, "uint8");
%! assert (imcast (im, "uint8"), im2uint8 (im))
%! assert (imcast (im, "uint16"), im2uint16 (im))
%! assert (imcast (im, "single"), im2single (im))
%! assert (imcast (im, "uint8", "indexed"), im2uint8 (im, "indexed"))
%! assert (imcast (im, "uint16", "indexed"), im2uint16 (im, "indexed"))
%! assert (imcast (im, "single", "indexed"), im2single (im, "indexed"))

%!test
%! im = randi ([1 256], 40, "double");
%! assert (imcast (im, "uint8"), im2uint8 (im))
%! assert (imcast (im, "uint8", "indexed"), im2uint8 (im, "indexed"))
%! assert (imcast (im, "single", "indexed"), im2single (im, "indexed"))

%!test
%! im = randi ([0 65535], 40, "uint16");
%! assert (imcast (im, "uint8"), im2uint8 (im))
%! assert (imcast (im, "single"), im2single (im))
%! assert (imcast (im, "single", "indexed"), im2single (im, "indexed"))

%!test
%! im = randi ([1 255], 40, "double");
%! assert (imcast (im, "uint8", "indexed"), im2uint8 (im, "indexed"))
%! assert (imcast (im, "single", "indexed"), im2single (im, "indexed"))

%!test
%! im = rand (40);
%! assert (imcast (im, "uint8"), im2uint8 (im))

%!error <unknown image of class> imcast (randi (127, 40, "int8"), "uint8")
%!error <unsupported TYPE> imcast (randi (255, 40, "uint8"), "uint32")
%!error <unsupported TYPE> imcast (randi (255, 40, "uint8"), "not a class")
%!error <range of values> imcast (randi ([0 65535], 40, "uint16"), "uint8", "indexed")