home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / gflare / asupsample.c next >
Encoding:
C/C++ Source or Header  |  1999-09-06  |  9.2 KB  |  336 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * General, non-jittered adaptive supersampling library
  5.  * Copyright (C) 1997 Federico Mena Quintero
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  */
  22.  
  23.  
  24. /* This code is *largely* based on the sources for POV-Ray 3.0.  I am
  25.  * grateful to the POV-Team for such a great program and for making
  26.  * their sources available.  All comments / bug reports /
  27.  * etc. regarding this library should be addressed to me, not to the
  28.  * POV-Ray team.  Any bugs are my responsibility, not theirs.
  29.  */
  30.  
  31.  
  32. #include <math.h>
  33. #include <string.h>
  34. #include <gtk/gtk.h>
  35. #include "asupsample.h"
  36.  
  37.  
  38. /***** Types *****/
  39.  
  40. typedef struct {
  41.     char    ready;
  42.     color_t color;
  43. } sample_t;
  44.  
  45.  
  46. /***** Local functions *****/
  47.  
  48. static unsigned long render_sub_pixel(int max_depth, int depth, sample_t **block,
  49.                       int x, int y, int x1, int y1, int x3, int y3,
  50.                       double threshold, int sub_pixel_size, render_func_t render_func,
  51.                       color_t *color, void *render_data);
  52.  
  53. static double color_dist(color_t c1, color_t c2);
  54.  
  55.  
  56. /***** Functions *****/
  57.  
  58. /*****/
  59.  
  60. unsigned long
  61. adaptive_supersample_area(int x1, int y1, int x2, int y2, int max_depth, double threshold,
  62.               render_func_t render_func, void *render_data,
  63.               put_pixel_func_t put_pixel_func, void *put_pixel_data,
  64.               progress_func_t progress_func, void *progress_data)
  65. {
  66.     int              x, y, width;                 /* Counters, width of region */
  67.     int              xt, xtt, yt;                 /* Temporary counters */
  68.     int              sub_pixel_size;              /* Numbe of samples per pixel (1D) */
  69.     size_t           row_size;                    /* Memory needed for one row */
  70.      color_t          color;                       /* Rendered pixel's color */
  71.     sample_t         tmp_sample;                  /* For swapping samples */
  72.     sample_t       *top_row, *bot_row, *tmp_row; /* Sample rows */
  73.     sample_t      **block;                       /* Sample block matrix */
  74.     unsigned long   num_samples;
  75.  
  76.     /* Initialize color */
  77.  
  78.     color.r = 0.0;
  79.     color.g = 0.0;
  80.     color.b = 0.0;
  81.     color.a = 0.0;
  82.  
  83.     /* Calculate sub-pixel size */
  84.  
  85.     sub_pixel_size = 1 << max_depth; /* 2**max_depth */
  86.  
  87.     /* Create row arrays */
  88.  
  89.     width = x2 - x1 + 1;
  90.  
  91.     row_size = (sub_pixel_size * width + 1) * sizeof(sample_t);
  92.  
  93.     top_row = g_malloc(row_size);
  94.     bot_row = g_malloc(row_size);
  95.  
  96.     for (x = 0; x < (sub_pixel_size * width + 1); x++) {
  97.         top_row[x].ready   = 0;
  98.         
  99.         top_row[x].color.r = 0.0;
  100.         top_row[x].color.g = 0.0;
  101.         top_row[x].color.b = 0.0;
  102.         top_row[x].color.a = 0.0;
  103.  
  104.         bot_row[x].ready   = 0;
  105.         
  106.         bot_row[x].color.r = 0.0;
  107.         bot_row[x].color.g = 0.0;
  108.         bot_row[x].color.b = 0.0;
  109.         bot_row[x].color.a = 0.0;
  110.     } /* for */
  111.  
  112.     /* Allocate block matrix */
  113.  
  114.     block = g_malloc((sub_pixel_size + 1) * sizeof(sample_t *)); /* Rows */
  115.  
  116.     for (y = 0; y < (sub_pixel_size + 1); y++)
  117.         block[y] = g_malloc((sub_pixel_size + 1) * sizeof(sample_t)); /* Columns */
  118.  
  119.     for (y = 0; y < (sub_pixel_size + 1); y++)
  120.         for (x = 0; x < (sub_pixel_size + 1); x++) {
  121.             block[y][x].ready = 0;
  122.  
  123.             block[y][x].color.r = 0.0;
  124.             block[y][x].color.g = 0.0;
  125.             block[y][x].color.b = 0.0;
  126.             block[y][x].color.a = 0.0;
  127.         } /* for */
  128.  
  129.     /* Render region */
  130.  
  131.     num_samples = 0;
  132.  
  133.     for (y = y1; y <= y2; y++) {
  134.         /* Clear the bottom row */
  135.  
  136.         for (xt = 0; xt < (sub_pixel_size * width + 1); xt++)
  137.             bot_row[xt].ready = 0;
  138.  
  139.         /* Clear first column */
  140.  
  141.         for (yt = 0; yt < (sub_pixel_size + 1); yt++)
  142.             block[yt][0].ready = 0;
  143.  
  144.         /* Render row */
  145.  
  146.         for (x = x1; x <= x2; x++) {
  147.             /* Initialize block by clearing all but first row/column */
  148.  
  149.             for (yt = 1; yt < (sub_pixel_size + 1); yt++)
  150.                 for (xt = 1; xt < (sub_pixel_size + 1); xt++)
  151.                     block[yt][xt].ready = 0;
  152.  
  153.             /* Copy samples from top row to block */
  154.  
  155.             for (xtt = 0, xt = (x - x1) * sub_pixel_size;
  156.                  xtt < (sub_pixel_size + 1);
  157.                  xtt++, xt++)
  158.                 block[0][xtt] = top_row[xt];
  159.  
  160.             /* Render pixel on (x, y) */
  161.  
  162.             num_samples += render_sub_pixel(max_depth, 1, block, x, y, 0, 0,
  163.                             sub_pixel_size, sub_pixel_size,
  164.                             threshold, sub_pixel_size, render_func, &color,
  165.                             render_data);
  166.  
  167.             if (put_pixel_func)
  168.                 (*put_pixel_func)(x, y, color, put_pixel_data);
  169.  
  170.             /* Copy block information to rows */
  171.  
  172.             top_row[((x - x1) + 1) * sub_pixel_size] = block[0][sub_pixel_size];
  173.  
  174.             for (xtt = 0, xt = (x - x1) * sub_pixel_size;
  175.                  xtt < (sub_pixel_size + 1);
  176.                  xtt++, xt++)
  177.                 bot_row[xt] = block[sub_pixel_size][xtt];
  178.  
  179.             /* Swap first and last columns */
  180.  
  181.             for (yt = 0; yt < (sub_pixel_size + 1); yt++) {
  182.                 tmp_sample             = block[yt][0];
  183.                 block[yt][0]           = block[yt][sub_pixel_size];
  184.                 block[yt][sub_pixel_size] = tmp_sample;
  185.             } /* for */
  186.         } /* for */
  187.  
  188.         /* Swap rows */
  189.  
  190.         tmp_row = top_row;
  191.         top_row = bot_row;
  192.         bot_row = tmp_row;
  193.  
  194.         /* Call progress display function */
  195.  
  196.         if (progress_func)
  197.             (*progress_func)(y1, y2, y, progress_data);
  198.     } /* for */
  199.  
  200.     /* Free memory */
  201.  
  202.     for (y = 0; y < (sub_pixel_size + 1); y++)
  203.         g_free(block[y]);
  204.  
  205.     g_free(block);
  206.     g_free(top_row);
  207.     g_free(bot_row);
  208.  
  209.     return num_samples;
  210. } /* adaptive_supersample_area */
  211.  
  212.  
  213. /*****/
  214.  
  215. static unsigned long
  216. render_sub_pixel(int max_depth, int depth, sample_t **block,
  217.          int x, int y, int x1, int y1, int x3, int y3,
  218.          double threshold, int sub_pixel_size, render_func_t render_func,
  219.          color_t *color, void *render_data)
  220. {
  221.     int           x2, y2;   /* Coords of center sample */
  222.     double        dx1, dy1; /* Delta to upper left sample */
  223.     double        dx3, dy3; /* Delta to lower right sample */
  224.     color_t       c1, c2, c3, c4; /* Sample colors */
  225.     unsigned long num_samples;
  226.  
  227.     /* Get offsets for corners */
  228.  
  229.     dx1 = (double) (x1 - sub_pixel_size / 2) / sub_pixel_size;
  230.     dx3 = (double) (x3 - sub_pixel_size / 2) / sub_pixel_size;
  231.  
  232.     dy1 = (double) (y1 - sub_pixel_size / 2) / sub_pixel_size;
  233.     dy3 = (double) (y3 - sub_pixel_size / 2) / sub_pixel_size;
  234.  
  235.     num_samples = 0;
  236.  
  237.     /* Render upper left sample */
  238.  
  239.     if (!block[y1][x1].ready) {
  240.         num_samples++;
  241.         (*render_func)(x + dx1, y + dy1, &c1, render_data);
  242.  
  243.         block[y1][x1].ready = 1;
  244.         block[y1][x1].color = c1;
  245.     } else
  246.         c1 = block[y1][x1].color;
  247.  
  248.     /* Render upper right sample */
  249.  
  250.     if (!block[y1][x3].ready) {
  251.         num_samples++;
  252.         (*render_func)(x + dx3, y + dy1, &c2, render_data);
  253.  
  254.         block[y1][x3].ready = 1;
  255.         block[y1][x3].color = c2;
  256.     } else
  257.         c2 = block[y1][x3].color;
  258.  
  259.     /* Render lower left sample */
  260.  
  261.     if (!block[y3][x1].ready) {
  262.         num_samples++;
  263.         (*render_func)(x + dx1, y + dy3, &c3, render_data);
  264.  
  265.         block[y3][x1].ready = 1;
  266.         block[y3][x1].color = c3;
  267.     } else
  268.         c3 = block[y3][x1].color;
  269.  
  270.     /* Render lower right sample */
  271.  
  272.     if (!block[y3][x3].ready) {
  273.         num_samples++;
  274.         (*render_func)(x + dx3, y + dy3, &c4, render_data);
  275.  
  276.         block[y3][x3].ready = 1;
  277.         block[y3][x3].color = c4;
  278.     } else
  279.         c4 = block[y3][x3].color;
  280.  
  281.     /* Check for supersampling */
  282.  
  283.     if (depth <= max_depth) {
  284.         /* Check whether we have tu supersample */
  285.  
  286.         if ((color_dist(c1, c2) >= threshold) ||
  287.             (color_dist(c1, c3) >= threshold) ||
  288.             (color_dist(c1, c4) >= threshold) ||
  289.             (color_dist(c2, c3) >= threshold) ||
  290.             (color_dist(c2, c4) >= threshold) ||
  291.             (color_dist(c3, c4) >= threshold)) {
  292.             /* Calc coordinates of center subsample */
  293.  
  294.             x2 = (x1 + x3) / 2;
  295.             y2 = (y1 + y3) / 2;
  296.  
  297.             /* Render sub-blocks */
  298.  
  299.             num_samples += render_sub_pixel(max_depth, depth + 1, block, x, y, x1, y1, x2, y2,
  300.                             threshold, sub_pixel_size, render_func, &c1,
  301.                             render_data);
  302.  
  303.             num_samples += render_sub_pixel(max_depth, depth + 1, block, x, y, x2, y1, x3, y2,
  304.                             threshold, sub_pixel_size, render_func, &c2,
  305.                             render_data);
  306.  
  307.             num_samples += render_sub_pixel(max_depth, depth + 1, block, x, y, x1, y2, x2, y3,
  308.                             threshold, sub_pixel_size, render_func, &c3,
  309.                             render_data);
  310.  
  311.             num_samples += render_sub_pixel(max_depth, depth + 1, block, x, y, x2, y2, x3, y3,
  312.                             threshold, sub_pixel_size, render_func, &c4,
  313.                             render_data);
  314.         } /* if */
  315.     } /* if */
  316.  
  317.     color->r = 0.25 * (c1.r + c2.r + c3.r + c4.r);
  318.     color->g = 0.25 * (c1.g + c2.g + c3.g + c4.g);
  319.     color->b = 0.25 * (c1.b + c2.b + c3.b + c4.b);
  320.     color->a = 0.25 * (c1.a + c2.a + c3.a + c4.a);
  321.  
  322.     return num_samples;
  323. } /* render_sub_pixel */
  324.  
  325.  
  326. /*****/
  327.  
  328. static double
  329. color_dist(color_t c1, color_t c2)
  330. {
  331.     return fabs(c1.r - c2.r) +
  332.            fabs(c1.g - c2.g) +
  333.            fabs(c1.b - c2.b) +
  334.            fabs(c1.a - c2.a);
  335. } /* color_dist */
  336.