home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / unofficial-plug-ins / mathmap / builtins.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-21  |  5.9 KB  |  291 lines

  1. /*
  2.  * builtins.c
  3.  *
  4.  * MathMap
  5.  *
  6.  * Copyright (C) 1997-2000 Mark Probst
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License
  10.  * as published by the Free Software Foundation; either version 2
  11.  * of the License, or (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23. #include <math.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <assert.h>
  28.  
  29. #include <libgimp/gimp.h>
  30.  
  31. #include "builtins.h"
  32. #include "postfix.h"
  33. #include "tags.h"
  34. #include "overload.h"
  35. #include "mathmap.h"
  36.  
  37. extern gint preview_width, preview_height;
  38. extern guchar *fast_image_source;
  39. extern int imageWidth,
  40.     imageHeight,
  41.     previewing;
  42. extern gint sel_x1, sel_y1,
  43.     sel_width, sel_height;
  44. extern double middleX,
  45.     middleY;
  46. extern unsigned char *imageData;
  47. extern int intersamplingEnabled,
  48.     oversamplingEnabled;
  49. extern double user_curve_values[];
  50. extern int user_curve_points;
  51. extern tuple_t gradient_samples[];
  52. extern int num_gradient_samples;
  53. extern int edge_behaviour_color, edge_behaviour_wrap, edge_behaviour_reflect;
  54. extern int edge_behaviour_mode;
  55. extern unsigned char edge_color[4];
  56.  
  57. builtin *firstBuiltin = 0;
  58.  
  59. void mathmap_get_pixel (int drawable_index, int x, int y, unsigned char *pixel);
  60.  
  61. static void
  62. get_pixel (int x, int y, guchar *pixel, int drawable_index)
  63.     if (edge_behaviour_mode == edge_behaviour_wrap)
  64.     {
  65.     if (x < 0)
  66.         x = x % img_width + img_width;
  67.     else if (x >= img_width)
  68.         x %= img_width;
  69.     if (y < 0)
  70.         y = y % img_height + img_height;
  71.     else if (y >= img_height)
  72.         y %= img_height;
  73.     }
  74.     else if (edge_behaviour_mode == edge_behaviour_reflect)
  75.     {
  76.     if (x < 0)
  77.         x = -x % img_width;
  78.     else if (x >= img_width)
  79.         x = (img_width - 1) - (x % img_width);
  80.     if (y < 0)
  81.         y = -y % img_height;
  82.     else if (y >= img_height)
  83.         y = (img_height - 1) - (y % img_height);
  84.     }
  85.  
  86.     if (previewing)
  87.     {
  88.     x = (x - sel_x1) * preview_width / sel_width;
  89.     y = (y - sel_y1) * preview_height / sel_height;
  90.  
  91.     mathmap_get_fast_pixel(drawable_index, x, y, pixel);
  92.     }
  93.     else
  94.     mathmap_get_pixel(drawable_index, x, y, pixel);
  95. }
  96.  
  97. void
  98. getOrigValPixel (float x, float y, unsigned char *pixel, int drawable_index)
  99. {
  100.     x += originX + middleX;
  101.     y = -y + originY + middleY;
  102.  
  103.     if (!oversamplingEnabled)
  104.     {
  105.     x += 0.5;
  106.     y += 0.5;
  107.     }
  108.  
  109.     get_pixel(floor(x), floor(y), pixel, drawable_index);
  110. }
  111.  
  112. void
  113. getOrigValIntersamplePixel (float x, float y, unsigned char *pixel, int drawable_index)
  114. {
  115.     int x1,
  116.     x2,
  117.     y1,
  118.     y2;
  119.     float x2fact,
  120.     y2fact,
  121.     x1fact,
  122.     y1fact,
  123.     p1fact,
  124.     p2fact,
  125.     p3fact,
  126.     p4fact;
  127.     unsigned char pixel1a[4],
  128.     pixel2a[4],
  129.     pixel3a[4],
  130.     pixel4a[4];
  131.     unsigned char *pixel1 = pixel1a,
  132.     *pixel2 = pixel2a,
  133.     *pixel3 = pixel3a,
  134.     *pixel4 = pixel4a;
  135.     int i;
  136.  
  137.     x += middleX + originX;
  138.     y = -y + middleY + originY;
  139.  
  140.     x1 = floor(x);
  141.     x2 = x1 + 1;
  142.     y1 = floor(y);
  143.     y2 = y1 + 1;
  144.     x2fact = (x - x1);
  145.     y2fact = (y - y1);
  146.     x1fact = 1.0 - x2fact;
  147.     y1fact = 1.0 - y2fact;
  148.     p1fact = x1fact * y1fact;
  149.     p2fact = x1fact * y2fact;
  150.     p3fact = x2fact * y1fact;
  151.     p4fact = x2fact * y2fact;
  152.  
  153.     get_pixel(x1, y1, pixel1, drawable_index);
  154.     get_pixel(x1, y2, pixel2, drawable_index);
  155.     get_pixel(x2, y1, pixel3, drawable_index);
  156.     get_pixel(x2, y2, pixel4, drawable_index);
  157.  
  158.     for (i = 0; i < 4; ++i)
  159.     pixel[i] = pixel1[i] * p1fact
  160.         + pixel2[i] * p2fact
  161.         + pixel3[i] * p3fact
  162.         + pixel4[i] * p4fact;
  163. }
  164.  
  165. #define MAX_LINEAR_DIM       10
  166. #define MAT(r,c)             (a[exch[r] * dim + (c)])
  167. #define RHS(r)               (b[exch[r]])
  168.  
  169. void
  170. solve_linear_equations (int dim, float *a, float *b)
  171. {
  172.     float r[MAX_LINEAR_DIM];
  173.     int exch[MAX_LINEAR_DIM];
  174.     int i;
  175.  
  176.     assert(dim <= MAX_LINEAR_DIM);
  177.  
  178.     for (i = 0; i < dim; ++i)
  179.     exch[i] = i;
  180.  
  181.     for (i = 0; i < dim - 1; ++i)
  182.     {
  183.     int p;
  184.  
  185.     for (p = i; p < dim; ++p) /* find pivot element */
  186.         if (MAT(p, i) != 0.0)
  187.         break;
  188.  
  189.     if (p != dim)
  190.     {
  191.         int j;
  192.  
  193.         if (p != i)
  194.         {
  195.         int tmp;
  196.  
  197.         tmp = exch[p];
  198.         exch[p] = exch[i];
  199.         exch[i] = tmp;
  200.         }
  201.  
  202.         for (j = i + 1; j < dim; ++j)
  203.         {
  204.         if (MAT(j, i) != 0.0)
  205.         {
  206.             float f = MAT(i, i) / MAT(j, i);
  207.             int k;
  208.  
  209.             MAT(j, i) = 0.0;
  210.             for (k = i + 1; k < dim; ++k)
  211.             MAT(j, k) = MAT(j, k) * f - MAT(i, k);
  212.  
  213.             RHS(j) = RHS(j) * f - RHS(i);
  214.         }
  215.         }
  216.     }
  217.     }
  218.  
  219.     for (i = dim - 1; i >= 0; --i)
  220.     {
  221.     if (MAT(i, i) == 0.0)
  222.         RHS(i) = 0.0;    /* this should be an error condition */
  223.     else
  224.     {
  225.         int j;
  226.         float v = 0.0;
  227.  
  228.         for (j = i + 1; j < dim; ++j)
  229.         v += MAT(i, j) * r[j];
  230.  
  231.         r[i] = (RHS(i) - v) / MAT(i, i);
  232.     }
  233.     }
  234.  
  235.     for (i = 0; i < dim; ++i)
  236.     b[i] = r[i];
  237.  
  238.     /*
  239.     if (dim == 2)
  240.     {
  241.     float r[2];
  242.  
  243.     if (a[2] != 0)
  244.     {
  245.         r[1] = (b[0] - a[0] * b[1] / a[2]) / (a[1] - a[3] * a[0] / a[2]);
  246.         r[0] = (b[1] - a[3] * r[1]) / a[2];
  247.     }
  248.     else if (a[0] != 0)
  249.     {
  250.         r[1] = (b[1] - a[2] * b[0] / a[0]) / (a[3] - a[1] * a[2] / a[0]);
  251.         r[0] = (b[0] - a[1] * r[1]) / a[0];
  252.     }
  253.     else
  254.         r[0] = r[1] = 0;
  255.  
  256.     b[0] = r[0];
  257.     b[1] = r[1];
  258.     }
  259.     else
  260.     assert(0);
  261.     */
  262.  
  263.     /*
  264.     integer n = dim;
  265.     integer nrhs = 1;
  266.     integer lda = dim;
  267.     integer ldb = dim;
  268.     integer info;
  269.     integer ipiv[dim];
  270.  
  271.     sgesv_(&n, &nrhs, a, &lda, ipiv, b, &ldb, &info);
  272.     */
  273. }
  274.  
  275. #include "builtins_interpreter.c"
  276.  
  277. builtin_function_t
  278. builtin_with_name (const char *name)
  279. {
  280.     if (strcmp(name, "origVal") == 0)
  281.     {
  282.     if (intersamplingEnabled)
  283.         return builtin_origValXYIntersample;
  284.     else
  285.         return builtin_origValXY;
  286.     }
  287.  
  288.     return 0;
  289. }
  290.