lighting-apply.c 3.98 KB
Newer Older
Manish Singh's avatar
Manish Singh committed
1 2 3 4
/******************************************************/
/* Apply mapping and shading on the whole input image */
/******************************************************/

5
#include "config.h"
6

7
#include <sys/types.h>
8 9 10

#include <libgimp/gimp.h>

11 12 13 14
#include "lighting-main.h"
#include "lighting-image.h"
#include "lighting-shade.h"
#include "lighting-apply.h"
Manish Singh's avatar
Manish Singh committed
15

16 17
#include "libgimp/stdplugins-intl.h"

18

Manish Singh's avatar
Manish Singh committed
19 20 21 22
/*************/
/* Main loop */
/*************/

23 24
void
compute_image (void)
Manish Singh's avatar
Manish Singh committed
25
{
26
  gint         xcount, ycount;
27
  GimpRGB       color;
28 29 30 31 32 33 34
  glong        progress_counter = 0;
  GimpVector3  p;
  gint32       new_image_id = -1;
  gint32       new_layer_id = -1;
  gint32       index;
  guchar      *row = NULL;
  guchar       obpp;
35 36 37
  gboolean     has_alpha;
  get_ray_func ray_func;

Manish Singh's avatar
Manish Singh committed
38 39


40 41
  if (mapvals.create_new_image == TRUE ||
      (mapvals.transparent_background == TRUE &&
42
       ! gimp_drawable_has_alpha (input_drawable->drawable_id)))
Manish Singh's avatar
Manish Singh committed
43 44 45 46
    {
      /* Create a new image */
      /* ================== */

47
      new_image_id = gimp_image_new (width, height, GIMP_RGB);
Manish Singh's avatar
Manish Singh committed
48

49
      if (mapvals.transparent_background == TRUE)
Manish Singh's avatar
Manish Singh committed
50 51 52 53
        {
          /* Add a layer with an alpha channel */
          /* ================================= */

54 55 56 57
          new_layer_id = gimp_layer_new (new_image_id, "Background",
					 width, height,
					 GIMP_RGBA_IMAGE,
					 100.0,
58
					 gimp_image_get_default_new_layer_mode (new_image_id));
Manish Singh's avatar
Manish Singh committed
59 60 61 62 63 64
        }
      else
        {
          /* Create a "normal" layer */
          /* ======================= */

65 66 67 68
          new_layer_id = gimp_layer_new (new_image_id, "Background",
					 width, height,
					 GIMP_RGB_IMAGE,
					 100.0,
69
					 gimp_image_get_default_new_layer_mode (new_image_id));
Manish Singh's avatar
Manish Singh committed
70 71
        }

72
      gimp_image_insert_layer (new_image_id, new_layer_id, -1, 0);
73
      output_drawable = gimp_drawable_get (new_layer_id);
Manish Singh's avatar
Manish Singh committed
74 75
    }

76
  if (mapvals.bump_mapped == TRUE && mapvals.bumpmap_id != -1)
Manish Singh's avatar
Manish Singh committed
77
    {
78 79
      gimp_pixel_rgn_init (&bump_region, gimp_drawable_get (mapvals.bumpmap_id),
			   0, 0, width, height, FALSE, FALSE);
Manish Singh's avatar
Manish Singh committed
80 81
    }

82 83 84 85 86 87
  precompute_init (width, height);

  if (!mapvals.env_mapped || mapvals.envmap_id == -1)
    {
      ray_func = get_ray_color;
    }
Manish Singh's avatar
Manish Singh committed
88 89
  else
    {
90 91
      env_width = gimp_drawable_width (mapvals.envmap_id);
      env_height = gimp_drawable_height (mapvals.envmap_id);
92 93
      gimp_pixel_rgn_init (&env_region, gimp_drawable_get (mapvals.envmap_id),
			   0, 0, env_width, env_height, FALSE, FALSE);
Manish Singh's avatar
Manish Singh committed
94 95 96
      ray_func = get_ray_color_ref;
    }

97 98
  gimp_pixel_rgn_init (&dest_region, output_drawable,
		       0, 0, width, height, TRUE, TRUE);
Manish Singh's avatar
Manish Singh committed
99

100 101
  obpp = gimp_drawable_bpp (output_drawable->drawable_id);
  has_alpha = gimp_drawable_has_alpha (output_drawable->drawable_id);
Manish Singh's avatar
Manish Singh committed
102

103
  row = g_new (guchar, obpp * width);
Manish Singh's avatar
Manish Singh committed
104

105
  gimp_progress_init (_("Lighting Effects"));
Manish Singh's avatar
Manish Singh committed
106

107 108 109
  /* Init the first row */
  if (mapvals.bump_mapped == TRUE && mapvals.bumpmap_id != -1 && height >= 2)
    interpol_row (0, width, 0);
Manish Singh's avatar
Manish Singh committed
110

111 112 113 114
  for (ycount = 0; ycount < height; ycount++)
    {
      if (mapvals.bump_mapped == TRUE && mapvals.bumpmap_id != -1)
	precompute_normals (0, width, ycount);
Manish Singh's avatar
Manish Singh committed
115

116
      index = 0;
Manish Singh's avatar
Manish Singh committed
117

118 119 120 121
      for (xcount = 0; xcount < width; xcount++)
	{
	  p = int_to_pos (xcount, ycount);
	  color = (* ray_func) (&p);
Manish Singh's avatar
Manish Singh committed
122

123 124 125 126 127 128 129 130 131 132 133 134 135 136
	  row[index++] = (guchar) (color.r * 255.0);
	  row[index++] = (guchar) (color.g * 255.0);
	  row[index++] = (guchar) (color.b * 255.0);

	  if (has_alpha)
	    row[index++] = (guchar) (color.a * 255.0);

	  if ((progress_counter++ % width) == 0)
	    gimp_progress_update ((gdouble) progress_counter /
				  (gdouble) maxcounter);
	}

      gimp_pixel_rgn_set_row (&dest_region, row, 0, ycount, width);
    }
Manish Singh's avatar
Manish Singh committed
137

138 139
  gimp_progress_update (1.0);

140
  g_free (row);
Manish Singh's avatar
Manish Singh committed
141 142 143 144

  /* Update image */
  /* ============ */

145
  gimp_drawable_flush (output_drawable);
146 147
  gimp_drawable_merge_shadow (output_drawable->drawable_id, TRUE);
  gimp_drawable_update (output_drawable->drawable_id, 0, 0, width, height);
Manish Singh's avatar
Manish Singh committed
148 149 150

  if (new_image_id!=-1)
    {
151 152 153
      gimp_display_new (new_image_id);
      gimp_displays_flush ();
      gimp_drawable_detach (output_drawable);
Manish Singh's avatar
Manish Singh committed
154 155 156
    }

}