home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / Lighting / lighting_apply.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-24  |  4.3 KB  |  169 lines

  1. /******************************************************/
  2. /* Apply mapping and shading on the whole input image */
  3. /******************************************************/
  4.  
  5. #include <sys/types.h>
  6.  
  7. #include <gtk/gtk.h>
  8.  
  9. #include <libgimp/gimp.h>
  10.  
  11. #include <gck/gck.h>
  12.  
  13. #include "lighting_main.h"
  14. #include "lighting_image.h"
  15. #include "lighting_shade.h"
  16.  
  17. /*************/
  18. /* Main loop */
  19. /*************/
  20.  
  21. get_ray_func ray_func;
  22.  
  23. void
  24. init_compute (void)
  25. {
  26. }
  27.  
  28. /*
  29. static void
  30. render (gdouble  x,
  31.     gdouble  y,
  32.     GckRGB  *col)
  33. {
  34.   GimpVector3 pos;
  35.  
  36.   pos=int_to_pos(x,y);
  37.  
  38.   *col=(*ray_func)(&pos);
  39. }
  40.  
  41. static void
  42. show_progress (gint min,
  43.            gint max,
  44.            gint curr)
  45. {
  46.   gimp_progress_update ((gdouble)curr / (gdouble)max);
  47. }
  48. */
  49.  
  50. void
  51. compute_image (void)
  52. {
  53.   gint xcount,ycount;
  54.   GckRGB color;
  55.   glong progress_counter=0;
  56.   GimpVector3 p;
  57.   gint32 new_image_id=-1,new_layer_id=-1,index;
  58.   guchar *row = NULL, obpp;
  59.  
  60.   gint has_alpha;
  61.  
  62.   init_compute();
  63.   
  64.   if (mapvals.create_new_image==TRUE || (mapvals.transparent_background==TRUE 
  65.       && !gimp_drawable_has_alpha(input_drawable->id)))
  66.     {
  67.       /* Create a new image */
  68.       /* ================== */
  69.  
  70.       new_image_id=gimp_image_new(width,height,GIMP_RGB);
  71.  
  72.       if (mapvals.transparent_background==TRUE)
  73.         {
  74.           /* Add a layer with an alpha channel */
  75.           /* ================================= */
  76.  
  77.           new_layer_id=gimp_layer_new(new_image_id,"Background",
  78.             width,height,GIMP_RGBA_IMAGE,100.0,GIMP_NORMAL_MODE);
  79.         }
  80.       else
  81.         {
  82.           /* Create a "normal" layer */
  83.           /* ======================= */
  84.  
  85.           new_layer_id=gimp_layer_new(new_image_id,"Background",
  86.             width,height,GIMP_RGB_IMAGE,100.0,GIMP_NORMAL_MODE);
  87.         }
  88.  
  89.       gimp_image_add_layer(new_image_id,new_layer_id,0);
  90.       output_drawable=gimp_drawable_get(new_layer_id);
  91.     }
  92.  
  93.   if (mapvals.bump_mapped==TRUE && mapvals.bumpmap_id!=-1)
  94.     {
  95.       gimp_pixel_rgn_init (&bump_region, gimp_drawable_get(mapvals.bumpmap_id),
  96.         0, 0, width, height, FALSE, FALSE);
  97.     }
  98.   precompute_init(width,height);
  99.  
  100.   if (!mapvals.env_mapped || mapvals.envmap_id==-1)
  101.      ray_func = get_ray_color;
  102.   else
  103.     {
  104.       env_width = gimp_drawable_width (mapvals.envmap_id);
  105.       env_height = gimp_drawable_height (mapvals.envmap_id);
  106.       gimp_pixel_rgn_init (&env_region, gimp_drawable_get(mapvals.envmap_id),
  107.         0, 0, env_width, env_height, FALSE, FALSE);
  108.       ray_func = get_ray_color_ref;
  109.     }
  110.  
  111.   gimp_pixel_rgn_init (&dest_region, output_drawable, 0,0, width,height, TRUE,TRUE);
  112.  
  113.   obpp=gimp_drawable_bpp(output_drawable->id);
  114.   has_alpha=gimp_drawable_has_alpha(output_drawable->id);
  115.  
  116.   row = (guchar *)g_malloc(sizeof(guchar)*(size_t)(obpp)*(size_t)(width));
  117.  
  118.   gimp_progress_init("Lighting Effects");
  119.  
  120. /*  if (mapvals.antialiasing==FALSE)
  121.     { */
  122.       for (ycount=0;ycount<height;ycount++)
  123.         {
  124.           if (mapvals.bump_mapped==TRUE && mapvals.bumpmap_id!=-1)
  125.             precompute_normals(0,width,ycount);
  126.  
  127.           index=0;
  128.           for (xcount=0;xcount<width;xcount++)
  129.             {
  130.               p=int_to_pos(xcount,ycount);
  131.               color=(*ray_func)(&p);
  132.  
  133.               row[index++]=(guchar)(color.r*255.0);
  134.               row[index++]=(guchar)(color.g*255.0);
  135.               row[index++]=(guchar)(color.b*255.0);
  136.  
  137.               if (has_alpha)
  138.                 row[index++]=(guchar)(color.a*255.0);
  139.  
  140.               if ((progress_counter++ % width)==0)
  141.                 gimp_progress_update((gdouble)progress_counter/(gdouble)maxcounter);
  142.             }
  143.  
  144.           gimp_pixel_rgn_set_row(&dest_region,row,0,ycount,width);
  145.         }
  146. /*    }
  147.   else
  148.     gck_adaptive_supersample_area(0,0,width-1,height-1,(gint)mapvals.max_depth,
  149.       mapvals.pixel_treshold,render,poke,show_progress); */
  150.  
  151.   if (row!=NULL)
  152.     g_free(row);
  153.  
  154.   /* Update image */
  155.   /* ============ */
  156.  
  157.   gimp_drawable_flush(output_drawable);
  158.   gimp_drawable_merge_shadow(output_drawable->id, TRUE);
  159.   gimp_drawable_update(output_drawable->id, 0,0, width,height);
  160.  
  161.   if (new_image_id!=-1)
  162.     {
  163.       gimp_display_new(new_image_id);
  164.       gimp_displays_flush();
  165.       gimp_drawable_detach(output_drawable);
  166.     }
  167.  
  168. }
  169.