home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / pnm / oct-utils.cc < prev    next >
C/C++ Source or Header  |  2000-01-15  |  3KB  |  134 lines

  1. /*
  2. *******************************************************************************
  3. ** Interface functions to Octave                                             **
  4. ** (c) 1997, Klaus Gebhardt                                                  **
  5. *******************************************************************************
  6. */
  7.  
  8. #include "oct-img.h"
  9.  
  10. extern "C"
  11. {
  12.   UCHAR **malloc_uchar_matrix (UINT, UINT);
  13.   INT   **malloc_int_matrix   (UINT, UINT);
  14. }
  15.  
  16.  
  17. UCHAR **gif_colormap (octave_value map, INT *colors, BOOL *grey)
  18. {
  19.   UCHAR **cmap;
  20.   INT i;
  21.  
  22.   if (grey)  *grey = OCT_GREY;
  23.   *colors = (INT) map.rows ();
  24.  
  25.   if (!map.is_matrix_type () || !map.is_real_type () ||
  26.       (((unsigned long) map.columns ()) != 3) || ((*colors) < 1))
  27.     {
  28.       ::error ("gif_colormap: colormap must be a real N x 3 matrix");
  29.       return NULL;
  30.     }
  31.  
  32.   Matrix Map = map.matrix_value ();
  33.  
  34.   cmap = malloc_uchar_matrix (3, (*colors));
  35.   if (cmap)
  36.     {
  37.       for (i = 0; i < (*colors); i++)
  38.     {
  39.       cmap[0][i] = (UCHAR) (min (max (Map (i, 0), 0), 1) * 255.);
  40.       cmap[1][i] = (UCHAR) (min (max (Map (i, 1), 0), 1) * 255.);
  41.       cmap[2][i] = (UCHAR) (min (max (Map (i, 2), 0), 1) * 255.);
  42.       if (grey)
  43.         if ((cmap[0][i] != cmap[1][i]) || (cmap[0][i] != cmap[2][i]))
  44.           *grey = OCT_RGB;
  45.     }
  46.  
  47.       if (grey && ((*grey) == OCT_GREY) && ((*colors) < 3))
  48.     {
  49.       if (((*colors) == 1) && ((cmap[0][0] == 0) || (cmap[0][0] == 255)))
  50.         *grey = OCT_BLACKWHITE;
  51.       else
  52.         {
  53.           if ((cmap[0][0]*cmap[0][1] == 0) &&
  54.           (cmap[0][0]+cmap[0][1] == 255))
  55.         *grey = OCT_BLACKWHITE;
  56.         }
  57.     }
  58.     }
  59.   else
  60.     ::error ("gif_colormap: out of memory");
  61.  
  62.   return cmap;
  63. }
  64.  
  65.  
  66. void oct_colormap (Matrix& Map, UCHAR **cmap, INT col_min, INT col_max)
  67. {
  68.   INT i;
  69.  
  70.   for (i = col_min; i < col_max + 1; i++)
  71.     {
  72.       Map (i - col_min, 0) = ((OCTAVE) cmap[0][i]) / 255.;
  73.       Map (i - col_min, 1) = ((OCTAVE) cmap[1][i]) / 255.;
  74.       Map (i - col_min, 2) = ((OCTAVE) cmap[2][i]) / 255.;
  75.     }
  76.  
  77.   free (cmap);
  78. }
  79.  
  80.  
  81. INT **gif_pixels (octave_value img, INT colors, UINT *nr, UINT *nc)
  82. {
  83.   INT **x;
  84.   INT c;
  85.   UINT i, j;
  86.  
  87.   *nr = (UINT) img.rows ();
  88.   *nc = (UINT) img.columns ();
  89.  
  90.   if (colors < 1)
  91.     {
  92.       ::error ("gif_pixels: number of colors must be positive");
  93.       return NULL;
  94.     }
  95.  
  96.   if (!img.is_matrix_type () || !img.is_real_type () ||
  97.       ((*nc) < 1) || ((*nr) < 1))
  98.     {
  99.       ::error ("gif_pixels: img must be a real matrix");
  100.       return NULL;
  101.     }
  102.  
  103.   Matrix X = img.matrix_value ();
  104.  
  105.   x = malloc_int_matrix ((*nr), (*nc));
  106.   if (x)
  107.     {
  108.       for (i = 0; i < (*nr); i++)
  109.     {
  110.       for (j = 0; j < (*nc); j++)
  111.         {
  112.           c = min (max ((INT) (X (i, j) - 1.), 0), colors - 1);
  113.           x[i][j] = c;
  114.         }
  115.     }
  116.     }
  117.   else
  118.     ::error ("gif_pixels: out of memory");
  119.  
  120.   return x;
  121. }
  122.  
  123.  
  124. void oct_pixels (Matrix& X, INT **x, INT col_min, UINT nr, UINT nc)
  125. {
  126.   UINT i, j;
  127.  
  128.   for (i = 0; i < nr; i++)
  129.     for (j = 0; j < nc; j++)
  130.       X (i, j) = ((double) (x[i][j])) - ((double) col_min) + 1.;
  131.  
  132.   free (x);
  133. }
  134.