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

  1. /**************************************************
  2.  * file: emboss/emboss.c
  3.  *
  4.  * Copyright (c) 1997 Eric L. Hernes (erich@rrnet.com)
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. The name of the author may not be used to endorse or promote products
  13.  *    derived from this software withough specific prior written permission
  14.  *
  15.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25.  *
  26.  * $Id: emboss.c,v 1.22 2000/08/22 01:26:54 neo Exp $
  27.  */
  28.  
  29. #ifdef HAVE_CONFIG_H
  30. #include "config.h"
  31. #endif
  32.  
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <stdlib.h>
  36. #include <sys/types.h>
  37.  
  38. #include <gtk/gtk.h>
  39.  
  40. #include <libgimp/gimp.h>
  41. #include <libgimp/gimpui.h>
  42.  
  43. #include "libgimp/stdplugins-intl.h"
  44.  
  45.  
  46. enum
  47. {
  48.   FUNCTION_BUMPMAP = 0,
  49.   FUNCTION_EMBOSS  = 1
  50. };
  51.  
  52. struct Grgb
  53. {
  54.   guint8 red;
  55.   guint8 green;
  56.   guint8 blue;
  57. };
  58.  
  59. struct GRegion
  60. {
  61.   gint32 x;
  62.   gint32 y;
  63.   gint32 width;
  64.   gint32 height;
  65. };
  66.  
  67. struct piArgs
  68. {
  69.   gint32 img;
  70.   gint32 drw;
  71.   gdouble azimuth;
  72.   gdouble elevation;
  73.   gint32 depth;
  74.   gint32 embossp;
  75. };
  76.  
  77. struct embossFilter
  78. {
  79.   gdouble Lx;
  80.   gdouble Ly;
  81.   gdouble Lz;
  82.   gdouble Nz;
  83.   gdouble Nz2;
  84.   gdouble NzLz;
  85.   gdouble bg;
  86. } Filter;
  87.  
  88. /*  preview stuff -- to be removed as soon as we have a real libgimp preview  */
  89.  
  90. struct mwPreview
  91. {
  92.   gint     width;
  93.   gint     height;
  94.   gint     bpp;
  95.   gdouble  scale;
  96.   guchar  *bits;
  97. };
  98.  
  99. #define PREVIEW_SIZE 100
  100.  
  101. static gint do_preview = TRUE;
  102.  
  103. static GtkWidget        * mw_preview_new   (GtkWidget        *parent,
  104.                         struct mwPreview *mwp);
  105. static struct mwPreview * mw_preview_build (GimpDrawable        *drw);
  106.  
  107.  
  108.  
  109. static void query (void);
  110. static void run   (gchar   *name,
  111.            gint     nparam,
  112.            GimpParam  *param,
  113.            gint    *nretvals,
  114.            GimpParam **retvals);
  115.  
  116. static gint pluginCore        (struct piArgs *argp);
  117. static gint pluginCoreIA      (struct piArgs *argp);
  118.  
  119. static void emboss_do_preview (GtkWidget     *preview);
  120.  
  121. static inline void EmbossInit (gdouble  azimuth,
  122.                    gdouble  elevation,
  123.                    gushort  width45);
  124. static inline void EmbossRow  (guchar  *src,
  125.                    guchar  *texture,
  126.                    guchar  *dst,
  127.                    guint    xSize,
  128.                    guint    bypp,
  129.                    gint     alpha);
  130.  
  131. #define DtoR(d) ((d)*(G_PI/(gdouble)180))
  132.  
  133. GimpPlugInInfo PLUG_IN_INFO =
  134. {
  135.   NULL,  /* init  */
  136.   NULL,  /* quit  */
  137.   query, /* query */
  138.   run,   /* run   */
  139. };
  140.  
  141. static struct mwPreview *thePreview;
  142.  
  143. MAIN ()
  144.  
  145. static void
  146. query (void)
  147. {
  148.   static GimpParamDef args[] =
  149.   {
  150.     { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
  151.     { GIMP_PDB_IMAGE, "img", "The Image" },
  152.     { GIMP_PDB_DRAWABLE, "drw", "The Drawable" },
  153.     { GIMP_PDB_FLOAT, "azimuth", "The Light Angle (degrees)" },
  154.     { GIMP_PDB_FLOAT, "elevation", "The Elevation Angle (degrees)" },
  155.     { GIMP_PDB_INT32, "depth", "The Filter Width" },
  156.     { GIMP_PDB_INT32, "embossp", "Emboss or Bumpmap" }
  157.   };
  158.   static gint nargs = sizeof (args) / sizeof (args[0]);
  159.  
  160.   gimp_install_procedure ("plug_in_emboss",
  161.               "Emboss filter",
  162.               "Emboss or Bumpmap the given drawable, specifying "
  163.               "the angle and elevation for the light source.",
  164.               "Eric L. Hernes, John Schlag",
  165.               "Eric L. Hernes",
  166.               "1997",
  167.               N_("<Image>/Filters/Distorts/Emboss..."),
  168.               "RGB*",
  169.               GIMP_PLUGIN,
  170.               nargs, 0,
  171.               args, NULL);
  172. }
  173.  
  174. static void
  175. run (gchar   *name,
  176.      gint     nparam,
  177.      GimpParam  *param,
  178.      gint    *nretvals,
  179.      GimpParam **retvals)
  180. {
  181.   static GimpParam rvals[1];
  182.   struct piArgs args;
  183.   GimpDrawable *drw;
  184.  
  185.   *nretvals = 1;
  186.   *retvals = rvals;
  187.  
  188.   memset(&args, (int) 0, sizeof (struct piArgs));
  189.  
  190.   rvals[0].type = GIMP_PDB_STATUS;
  191.   rvals[0].data.d_status = GIMP_PDB_SUCCESS;
  192.  
  193.   switch (param[0].data.d_int32)
  194.     {
  195.     case GIMP_RUN_INTERACTIVE:
  196.       INIT_I18N_UI();
  197.       args.img = args.drw = 0;
  198.       gimp_get_data ("plug_in_emboss", &args);
  199.       if (args.img == 0 && args.drw == 0)
  200.     {
  201.       /* initial conditions */
  202.       args.azimuth = 30.0;
  203.       args.elevation = 45.0;
  204.       args.depth = 20;
  205.       args.embossp = FUNCTION_EMBOSS;
  206.     }
  207.       args.img = param[1].data.d_image;
  208.       args.drw = param[2].data.d_drawable;
  209.       drw = gimp_drawable_get (args.drw);
  210.       thePreview = mw_preview_build (drw);
  211.  
  212.       if (pluginCoreIA (&args) == -1)
  213.     {
  214.       rvals[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
  215.     }
  216.       else
  217.     {
  218.       gimp_set_data ("plug_in_emboss", &args, sizeof (struct piArgs));
  219.     }
  220.  
  221.       break;
  222.  
  223.     case GIMP_RUN_NONINTERACTIVE:
  224.       if (nparam != 7)
  225.     {
  226.       rvals[0].data.d_status = GIMP_PDB_CALLING_ERROR;
  227.       break;
  228.     }
  229.  
  230.       INIT_I18N();
  231.       args.img = param[1].data.d_image;
  232.       args.drw = param[2].data.d_drawable;
  233.       args.azimuth = param[3].data.d_float;
  234.       args.elevation = param[4].data.d_float;
  235.       args.depth = param[5].data.d_int32;
  236.       args.embossp = param[6].data.d_int32;
  237.  
  238.       if (pluginCore(&args)==-1)
  239.     {
  240.       rvals[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
  241.       break;
  242.     }
  243.       break;
  244.  
  245.     case GIMP_RUN_WITH_LAST_VALS:
  246.       INIT_I18N();
  247.       gimp_get_data("plug_in_emboss", &args);
  248.       /* use this image and drawable, even with last args */
  249.       args.img = param[1].data.d_image;
  250.       args.drw = param[2].data.d_drawable;
  251.       if (pluginCore(&args)==-1) {
  252.         rvals[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
  253.       }
  254.     break;
  255.   }
  256. }
  257.  
  258. #define pixelScale 255.9
  259.  
  260. static void
  261. EmbossInit (gdouble azimuth,
  262.         gdouble elevation,
  263.         gushort width45)
  264. {
  265.   /*
  266.    * compute the light vector from the input parameters.
  267.    * normalize the length to pixelScale for fast shading calculation.
  268.    */
  269.   Filter.Lx = cos (azimuth) * cos (elevation) * pixelScale;
  270.   Filter.Ly = sin (azimuth) * cos (elevation) * pixelScale;
  271.   Filter.Lz = sin (elevation) * pixelScale;
  272.  
  273.   /*
  274.    * constant z component of image surface normal - this depends on the
  275.    * image slope we wish to associate with an angle of 45 degrees, which
  276.    * depends on the width of the filter used to produce the source image.
  277.    */
  278.   Filter.Nz = (6 * 255) / width45;
  279.   Filter.Nz2 = Filter.Nz * Filter.Nz;
  280.   Filter.NzLz = Filter.Nz * Filter.Lz;
  281.  
  282.   /* optimization for vertical normals: L.[0 0 1] */
  283.   Filter.bg = Filter.Lz;
  284. }
  285.  
  286.  
  287. /*
  288.  * ANSI C code from the article
  289.  * "Fast Embossing Effects on Raster Image Data"
  290.  * by John Schlag, jfs@kerner.com
  291.  * in "Graphics Gems IV", Academic Press, 1994
  292.  *
  293.  *
  294.  * Emboss - shade 24-bit pixels using a single distant light source.
  295.  * Normals are obtained by differentiating a monochrome 'bump' image.
  296.  * The unary case ('texture' == NULL) uses the shading result as output.
  297.  * The binary case multiples the optional 'texture' image by the shade.
  298.  * Images are in row major order with interleaved color components (rgbrgb...).
  299.  * E.g., component c of pixel x,y of 'dst' is dst[3*(y*xSize + x) + c].
  300.  *
  301.  */
  302.  
  303. static inline void
  304. EmbossRow (guchar *src,
  305.        guchar *texture,
  306.        guchar *dst,
  307.        guint   xSize,
  308.        guint   bypp,
  309.        gint    alpha)
  310. {
  311.   glong Nx, Ny, NdotL;
  312.   guchar *s1, *s2, *s3;
  313.   gint x, shade, b;
  314.   gint bytes;
  315.  
  316.   /* mung pixels, avoiding edge pixels */
  317.   s1 = src + bypp;
  318.   s2 = s1 + (xSize * bypp);
  319.   s3 = s2 + (xSize * bypp);
  320.   dst += bypp;
  321.  
  322.   bytes = (alpha) ? bypp - 1 : bypp;
  323.  
  324.   if (texture)
  325.     texture += bypp;
  326.  
  327.   for (x = 1; x < xSize - 1; x++, s1 += bypp, s2 += bypp, s3 += bypp)
  328.     {
  329.       /*
  330.        * compute the normal from the src map. the type of the
  331.        * expression before the cast is compiler dependent. in
  332.        * some cases the sum is unsigned, in others it is
  333.        * signed. ergo, cast to signed.
  334.        */
  335.       Nx = (int) (s1[-(int)bypp] + s2[-(int)bypp] + s3[-(int)bypp]
  336.           - s1[bypp] - s2[bypp] - s3[bypp]);
  337.       Ny = (int) (s3[-(int)bypp] + s3[0] + s3[bypp] - s1[-(int)bypp]
  338.           - s1[0] - s1[bypp]);
  339.  
  340.       /* shade with distant light source */
  341.       if ( Nx == 0 && Ny == 0 )
  342.     shade = Filter.bg;
  343.       else if ( (NdotL = Nx * Filter.Lx + Ny * Filter.Ly + Filter.NzLz) < 0 )
  344.     shade = 0;
  345.       else
  346.     shade = NdotL / sqrt(Nx*Nx + Ny*Ny + Filter.Nz2);
  347.  
  348.       /* do something with the shading result */
  349.       if (texture)
  350.     {
  351.       for (b = 0; b < bytes; b++)
  352.         {
  353.           *dst++ = (*texture++ * shade) >> 8;
  354.         }
  355.       if (alpha)
  356.         {
  357.           *dst++ = s2[bytes]; /* preserve the alpha */
  358.           texture++;
  359.         }
  360.     }
  361.       else
  362.     {
  363.       for (b = 0; b < bytes; b++)
  364.         {
  365.           *dst++ = shade;
  366.         }
  367.       if (alpha)
  368.         *dst++ = s2[bytes]; /* preserve the alpha */
  369.     }
  370.     }
  371.   if (texture)
  372.     texture += bypp;
  373. }
  374.  
  375. static gint
  376. pluginCore (struct piArgs *argp)
  377. {
  378.   GimpDrawable *drw;
  379.   GimpPixelRgn src, dst;
  380.   gint p_update;
  381.   gint y;
  382.   gint x1, y1, x2, y2;
  383.   guint width, height;
  384.   gint bypp, rowsize, has_alpha;
  385.   guchar *srcbuf, *dstbuf;
  386.  
  387.   drw = gimp_drawable_get (argp->drw);
  388.  
  389.   gimp_drawable_mask_bounds (argp->drw, &x1, &y1, &x2, &y2);
  390.  
  391.   /* expand the bounds a little */
  392.   x1 = MAX (0, x1 - argp->depth);
  393.   y1 = MAX (0, y1 - argp->depth);
  394.   x2 = MIN (drw->width, x2 + argp->depth);
  395.   y2 = MIN (drw->height, y2 + argp->depth);
  396.  
  397.   width = x2 - x1;
  398.   height = y2 - y1;
  399.   bypp = drw->bpp;
  400.   p_update = height / 20;
  401.   rowsize = width * bypp;
  402.   has_alpha = gimp_drawable_has_alpha (argp->drw);
  403.  
  404.   gimp_pixel_rgn_init (&src, drw, x1, y1, width, height, FALSE, FALSE);
  405.   gimp_pixel_rgn_init (&dst, drw, x1, y1, width, height, TRUE, TRUE);
  406.  
  407.   srcbuf = g_new (guchar, rowsize * 3);
  408.   dstbuf = g_new (guchar, rowsize);
  409.  
  410.   memset (srcbuf, 0, (size_t) rowsize * 3);
  411.   memset (dstbuf, 0, (size_t) rowsize);
  412.  
  413.   EmbossInit (DtoR(argp->azimuth), DtoR(argp->elevation), argp->depth);
  414.   gimp_progress_init (_("Emboss"));
  415.  
  416.   gimp_tile_cache_ntiles ((width + gimp_tile_width () - 1) / gimp_tile_width ());
  417.  
  418.   /* first row */
  419.   gimp_pixel_rgn_get_rect (&src, srcbuf, x1, y1, width, 3);
  420.   memcpy (srcbuf, srcbuf + rowsize, rowsize);
  421.   EmbossRow (srcbuf, argp->embossp ? (guchar *) 0 : srcbuf,
  422.          dstbuf, width, bypp, has_alpha);
  423.   gimp_pixel_rgn_set_row (&dst, dstbuf, 0, 0, width);
  424.  
  425.   /* last row */
  426.   gimp_pixel_rgn_get_rect (&src, srcbuf, x1, y2-3, width, 3);
  427.   memcpy (srcbuf + rowsize * 2, srcbuf + rowsize, rowsize);
  428.   EmbossRow (srcbuf, argp->embossp ? (guchar *) 0 : srcbuf,
  429.          dstbuf, width, bypp, has_alpha);
  430.   gimp_pixel_rgn_set_row (&dst, dstbuf, x1, y2-1, width);
  431.  
  432.   for (y = 0; y < height - 2; y++)
  433.     {
  434.       if (y % p_update == 0)
  435.       gimp_progress_update ((gdouble) y / (gdouble) height);
  436.  
  437.       gimp_pixel_rgn_get_rect (&src, srcbuf, x1, y1+y, width, 3);
  438.       EmbossRow (srcbuf, argp->embossp ? (guchar *) 0 : srcbuf,
  439.          dstbuf, width, bypp, has_alpha);
  440.      gimp_pixel_rgn_set_row (&dst, dstbuf, x1, y1+y+1, width);
  441.   }
  442.   gimp_progress_update (1.0);
  443.  
  444.   g_free (srcbuf);
  445.   g_free (dstbuf);
  446.  
  447.   gimp_drawable_flush (drw);
  448.   gimp_drawable_merge_shadow (drw->id, TRUE);
  449.   gimp_drawable_update (drw->id, x1, y1, width, height);
  450.   gimp_displays_flush ();
  451.  
  452.   return 0;
  453. }
  454.  
  455. gboolean run_flag = FALSE;
  456.  
  457. static void
  458. emboss_ok_callback (GtkWidget *widget,
  459.             gpointer   data)
  460. {
  461.   run_flag = TRUE;
  462.  
  463.   gtk_widget_destroy (GTK_WIDGET (data));
  464. }
  465.  
  466. static void
  467. emboss_radio_button_callback (GtkWidget *widget,
  468.                   gpointer   data)
  469. {
  470.   gimp_radio_button_update (widget, data);
  471.  
  472.   if (do_preview && GTK_TOGGLE_BUTTON (widget)->active)
  473.     emboss_do_preview (NULL);
  474. }
  475.  
  476. static void
  477. emboss_float_adjustment_callback (GtkAdjustment *adj,
  478.                   gpointer       data)
  479. {
  480.   gdouble *value;
  481.  
  482.   value = (gdouble *) data;
  483.  
  484.   *value = adj->value;
  485.  
  486.   if (do_preview)
  487.     emboss_do_preview (NULL);
  488. }
  489.  
  490. static void
  491. emboss_int_adjustment_callback (GtkAdjustment *adj,
  492.                 gpointer       data)
  493. {
  494.   gint *value;
  495.  
  496.   value = (gint *) data;
  497.  
  498.   *value = (gint) adj->value;
  499.  
  500.   if (do_preview)
  501.     emboss_do_preview (NULL);
  502. }
  503.  
  504. static gint
  505. pluginCoreIA (struct piArgs *argp)
  506. {
  507.   GtkWidget *dlg;
  508.   GtkWidget *main_vbox;
  509.   GtkWidget *hbox;
  510.   GtkWidget *table;
  511.   GtkWidget *preview;
  512.   GtkWidget *frame;
  513.   GtkObject *adj;
  514.  
  515.   gimp_ui_init ("emboss", TRUE);
  516.  
  517.   dlg = gimp_dialog_new (_("Emboss"), "emboss",
  518.              gimp_standard_help_func, "filters/emboss.html",
  519.              GTK_WIN_POS_MOUSE,
  520.              FALSE, TRUE, FALSE,
  521.  
  522.              _("OK"), emboss_ok_callback,
  523.              NULL, NULL, NULL, TRUE, FALSE,
  524.              _("Cancel"), gtk_widget_destroy,
  525.              NULL, 1, NULL, FALSE, TRUE,
  526.  
  527.              NULL);
  528.  
  529.   gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
  530.               GTK_SIGNAL_FUNC (gtk_main_quit),
  531.               NULL);
  532.  
  533.   main_vbox = gtk_vbox_new (FALSE, 4);
  534.   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 6);
  535.   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), main_vbox,
  536.               TRUE, TRUE, 0);
  537.   gtk_widget_show (main_vbox);
  538.  
  539.   hbox = gtk_hbox_new (FALSE, 5);
  540.   gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
  541.   gtk_widget_show (hbox);
  542.  
  543.   preview = mw_preview_new (hbox, thePreview);
  544.   gtk_object_set_data (GTK_OBJECT (preview), "piArgs", argp);
  545.   emboss_do_preview (preview);
  546.  
  547.   frame = gimp_radio_group_new2 (TRUE, _("Function"),
  548.                  emboss_radio_button_callback,
  549.                  &argp->embossp, (gpointer) argp->embossp,
  550.  
  551.                  _("Bumpmap"), (gpointer) FUNCTION_BUMPMAP, NULL,
  552.                  _("Emboss"), (gpointer) FUNCTION_EMBOSS, NULL,
  553.  
  554.                  NULL);
  555.  
  556.   gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, 0);
  557.   gtk_widget_show (frame);
  558.  
  559.   frame = gtk_frame_new (_("Parameter Settings"));
  560.   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  561.   gtk_box_pack_start (GTK_BOX (main_vbox), frame, FALSE, FALSE, 0);
  562.   gtk_widget_show (frame);
  563.  
  564.   table = gtk_table_new (3, 3, FALSE);
  565.   gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  566.   gtk_table_set_row_spacings (GTK_TABLE (table), 2);
  567.   gtk_container_set_border_width (GTK_CONTAINER (table), 4);
  568.   gtk_container_add (GTK_CONTAINER (frame), table);
  569.  
  570.   adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 0,
  571.                   _("Azimuth:"), 100, 0,
  572.                   argp->azimuth, 0.0, 360.0, 1.0, 10.0, 2,
  573.                   TRUE, 0, 0,
  574.                   NULL, NULL);
  575.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  576.               GTK_SIGNAL_FUNC (emboss_float_adjustment_callback),
  577.               &argp->azimuth);
  578.  
  579.   adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 1,
  580.                   _("Elevation:"), 100, 0,
  581.                   argp->elevation, 0.0, 180.0, 1.0, 10.0, 2,
  582.                   TRUE, 0, 0,
  583.                   NULL, NULL);
  584.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  585.               GTK_SIGNAL_FUNC (emboss_float_adjustment_callback),
  586.               &argp->elevation);
  587.  
  588.   adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 2,
  589.                   _("Depth:"), 100, 0,
  590.                   argp->depth, 1.0, 100.0, 1.0, 5.0, 0,
  591.                   TRUE, 0, 0,
  592.                   NULL, NULL);
  593.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  594.               GTK_SIGNAL_FUNC (emboss_int_adjustment_callback),
  595.               &argp->depth);
  596.  
  597.   gtk_widget_show (table);
  598.  
  599.   gtk_widget_show (dlg);
  600.  
  601.   gtk_main ();
  602.   gdk_flush ();
  603.  
  604.   if (run_flag)
  605.     return pluginCore (argp);
  606.   else
  607.     return -1;
  608. }
  609.  
  610. static void
  611. emboss_do_preview (GtkWidget *w)
  612. {
  613.   static GtkWidget *theWidget = NULL;
  614.   struct piArgs *ap;
  615.   guchar *dst, *c;
  616.   gint y, rowsize;
  617.  
  618.   if (theWidget == NULL)
  619.     {
  620.       theWidget = w;
  621.     }
  622.  
  623.   ap = gtk_object_get_data (GTK_OBJECT (theWidget), "piArgs");
  624.   rowsize = thePreview->width * thePreview->bpp;
  625.  
  626.   dst = g_malloc (rowsize);
  627.   c = g_malloc (rowsize * 3);
  628.   memcpy (c, thePreview->bits, rowsize);
  629.   memcpy (c + rowsize, thePreview->bits, rowsize * 2);
  630.   EmbossInit (DtoR (ap->azimuth), DtoR (ap->elevation), ap->depth);
  631.  
  632.   EmbossRow (c, ap->embossp ? (guchar *) 0 : c,
  633.              dst, thePreview->width, thePreview->bpp, FALSE);
  634.   gtk_preview_draw_row (GTK_PREVIEW (theWidget),
  635.                         dst, 0, 0, thePreview->width);
  636.  
  637.   memcpy (c,
  638.       thePreview->bits + ((thePreview->height-2) * rowsize),
  639.       rowsize * 2);
  640.   memcpy (c + (rowsize * 2),
  641.       thePreview->bits + ((thePreview->height - 1) * rowsize),
  642.           rowsize);
  643.   EmbossRow (c, ap->embossp ? (guchar *) 0 : c,
  644.              dst, thePreview->width, thePreview->bpp, FALSE);
  645.   gtk_preview_draw_row (GTK_PREVIEW (theWidget),
  646.                         dst, 0, thePreview->height - 1, thePreview->width);
  647.   g_free (c);
  648.  
  649.   for (y = 0, c = thePreview->bits; y<thePreview->height - 2; y++, c += rowsize)
  650.     {
  651.       EmbossRow (c, ap->embossp ? (guchar *) 0 : c,
  652.          dst, thePreview->width, thePreview->bpp, FALSE);
  653.       gtk_preview_draw_row (GTK_PREVIEW (theWidget),
  654.                 dst, 0, y, thePreview->width);
  655.     }
  656.  
  657.   gtk_widget_draw (theWidget, NULL);
  658.   gdk_flush ();
  659.   g_free (dst);
  660. }
  661.  
  662. static void
  663. mw_preview_toggle_callback (GtkWidget *widget,
  664.                             gpointer   data)
  665. {
  666.   gimp_toggle_button_update (widget, data);
  667.  
  668.   if (do_preview)
  669.     emboss_do_preview (NULL);
  670. }
  671.  
  672. static struct mwPreview *
  673. mw_preview_build_virgin (GimpDrawable *drw)
  674. {
  675.   struct mwPreview *mwp;
  676.  
  677.   mwp = g_new (struct mwPreview, 1);
  678.  
  679.   if (drw->width > drw->height)
  680.     {
  681.       mwp->scale  = (gdouble) drw->width / (gdouble) PREVIEW_SIZE;
  682.       mwp->width  = PREVIEW_SIZE;
  683.       mwp->height = drw->height / mwp->scale;
  684.     }
  685.   else
  686.     {
  687.       mwp->scale  = (gdouble) drw->height / (gdouble) PREVIEW_SIZE;
  688.       mwp->height = PREVIEW_SIZE;
  689.       mwp->width  = drw->width / mwp->scale;
  690.     }
  691.  
  692.   mwp->bpp  = 3;
  693.   mwp->bits = NULL;
  694.  
  695.   return mwp;
  696. }
  697.  
  698. static struct mwPreview *
  699. mw_preview_build (GimpDrawable *drw)
  700. {
  701.   struct mwPreview *mwp;
  702.   gint x, y, b;
  703.   guchar *bc;
  704.   guchar *drwBits;
  705.   GimpPixelRgn pr;
  706.  
  707.   mwp = mw_preview_build_virgin (drw);
  708.  
  709.   gimp_pixel_rgn_init (&pr, drw, 0, 0, drw->width, drw->height, FALSE, FALSE);
  710.   drwBits = g_new (guchar, drw->width * drw->bpp);
  711.  
  712.   bc = mwp->bits = g_new (guchar, mwp->width * mwp->height * mwp->bpp);
  713.   for (y = 0; y < mwp->height; y++)
  714.     {
  715.       gimp_pixel_rgn_get_row (&pr, drwBits, 0, (int)(y*mwp->scale), drw->width);
  716.  
  717.       for (x = 0; x < mwp->width; x++)
  718.         {
  719.           for (b = 0; b < mwp->bpp; b++)
  720.             *bc++ = *(drwBits +
  721.                       ((gint) (x * mwp->scale) * drw->bpp) + b % drw->bpp);
  722.         }
  723.     }
  724.   g_free (drwBits);
  725.  
  726.   return mwp;
  727. }
  728.  
  729. static GtkWidget *
  730. mw_preview_new (GtkWidget        *parent,
  731.                 struct mwPreview *mwp)
  732. {
  733.   GtkWidget *preview;
  734.   GtkWidget *frame;
  735.   GtkWidget *pframe;
  736.   GtkWidget *vbox;
  737.   GtkWidget *button;
  738.    
  739.   frame = gtk_frame_new (_("Preview"));
  740.   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  741.   gtk_box_pack_start (GTK_BOX (parent), frame, FALSE, FALSE, 0);
  742.   gtk_widget_show (frame);
  743.  
  744.   vbox = gtk_vbox_new (FALSE, 2);
  745.   gtk_container_set_border_width (GTK_CONTAINER (vbox), 4);
  746.   gtk_container_add (GTK_CONTAINER (frame), vbox);
  747.   gtk_widget_show (vbox);
  748.  
  749.   pframe = gtk_frame_new (NULL);
  750.   gtk_frame_set_shadow_type (GTK_FRAME(pframe), GTK_SHADOW_IN);
  751.   gtk_box_pack_start (GTK_BOX (vbox), pframe, FALSE, FALSE, 0);
  752.   gtk_widget_show (pframe);
  753.  
  754.   preview = gtk_preview_new (GTK_PREVIEW_COLOR);
  755.   gtk_preview_size (GTK_PREVIEW (preview), mwp->width, mwp->height);
  756.   gtk_container_add (GTK_CONTAINER (pframe), preview);
  757.   gtk_widget_show (preview);
  758.  
  759.   button = gtk_check_button_new_with_label (_("Do Preview"));
  760.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), do_preview);
  761.   gtk_signal_connect (GTK_OBJECT (button), "toggled",
  762.                       GTK_SIGNAL_FUNC (mw_preview_toggle_callback),
  763.                       &do_preview);
  764.   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
  765.   gtk_widget_show (button);
  766.  
  767.   return preview;
  768. }
  769.