## Copyright (C) 2016 Pantxo Diribarne
## 
## 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 {} {@var{retval} =} build_default_image (@var{input1}, @var{input2})
##
## @seealso{}
## @end deftypefn

## Author: Pantxo Diribarne <pantxo@gre046552>
## Created: 2016-09-02

function build_default_image (fname)
  cmap = colormap ("viridis");
  bgcolor = cmap(end,:);
  set (gcf, "position", [0 0 400 600], "color", bgcolor, ...
       "inverthardcopy", "off", "paperpositionmode", "manual")
  sombrero ();
  hs = get (gca, "children");
  set (hs, "linestyle", "none")
  set (gca, "position", [0 0 1 1])
  axis off
  tmp = [tmpnam() ".png"];
  print (tmp, "-S64,64");
  im = imread (tmp);
  bright = sum (cmap, 2)/3; #brightness, monotonic for viridis cmap
  minb = min (bright);
  maxb = max (bright);
  keyboard
  idx = ceil (interp1 (bright', 1:64, sum (im, 3)/255/3));
  imshow (idx, cmap);
  cmap = colormap ("viridis");
  idx = flipud (idx');
  id = fopen (fname, "w+");
  fprintf (id, "%s{\n", header ())
  nval = numel (idx);
  ii = 1;
  tmp = "";
  while nval
    tmp = [tmp "  "];
    if (nval >= 18)
      tmp = [tmp sprintf("%d,", idx(ii:ii+17))];
      ii += 18;
      nval -= 18;
    else
      tmp = [tmp sprintf("%d,", idx(ii:end))];
      nval = 0;
    endif
      tmp = [tmp "\n"];
  endwhile
 
  fprintf (id, "%s%s", tmp(1:end-2), "\n};\n\n#endif\n");
  fclose (id);
endfunction

function s = header ()
  s ="/*\n\
\n\
Copyright (C) 2014-2016 Pantxo Diribarne\n\
\n\
This file is part of Octave.\n\
\n\
Octave is free software; you can redistribute it and/or modify it\n\
under the terms of the GNU General Public License as published by the\n\
Free Software Foundation; either version 3 of the License, or (at your\n\
option) any later version.\n\
\n\
Octave is distributed in the hope that it will be useful, but WITHOUT\n\
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\n\
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\n\
for more details.\n\
\n\
You should have received a copy of the GNU General Public License\n\
along with Octave; see the file COPYING.  If not, see\n\
<http://www.gnu.org/licenses/>.\n\
\n\
*/\n\
\n\
#if ! defined (octave_octave_default_image_h)\n\
#define octave_octave_default_image_h 1\n\
\n\
#include \"octave-config.h\"\n\
\n\
static char default_im_data[] =\n";
endfunction