home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / app / layer.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-17  |  51.3 KB  |  1,925 lines

  1. /* TODO: make sure has_alpha gets set */
  2.  
  3. /* The GIMP -- an image manipulation program
  4.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19.  */
  20.  
  21. #include "config.h"
  22.  
  23. #include <stdlib.h>
  24. #include <string.h>
  25.  
  26. #include <glib.h>
  27.  
  28. #include "drawable.h"
  29. #include "floating_sel.h"
  30. #include "gdisplay.h"
  31. #include "gimage.h"
  32. #include "gimage_mask.h"
  33. #include "layer.h"
  34. #include "paint_funcs.h"
  35. #include "temp_buf.h"
  36. #include "parasitelist.h"
  37. #include "undo.h"
  38. #include "gimpsignal.h"
  39. #include "gimppreviewcache.h"
  40.  
  41. #include "layer_pvt.h"
  42. #include "tile_manager_pvt.h"
  43. #include "tile.h"            /* ick. */
  44.  
  45. #include "libgimp/gimpmath.h"
  46. #include "libgimp/gimpparasite.h"
  47.  
  48. #include "libgimp/gimpintl.h"
  49.  
  50.  
  51. enum
  52. {
  53.   REMOVED,
  54.   LAST_SIGNAL
  55. };
  56.  
  57. static void gimp_layer_class_init    (GimpLayerClass *klass);
  58. static void gimp_layer_init          (GimpLayer      *layer);
  59. static void gimp_layer_destroy       (GtkObject      *object);
  60. static void layer_invalidate_preview (GtkObject      *object);
  61.  
  62. static void gimp_layer_mask_class_init (GimpLayerMaskClass *klass);
  63. static void gimp_layer_mask_init       (GimpLayerMask      *layermask);
  64. static void gimp_layer_mask_destroy    (GtkObject          *object);
  65.  
  66. static TempBuf * layer_preview_private      (Layer *layer,
  67.                          gint   width,
  68.                          gint   height);
  69. static TempBuf * layer_mask_preview_private (Layer *layer,
  70.                          gint   width,
  71.                          gint   height);
  72.  
  73.  
  74. static guint layer_signals[LAST_SIGNAL] = { 0 };
  75.  
  76. /*
  77.   static guint layer_mask_signals[LAST_SIGNAL] = { 0 };
  78.  */
  79.  
  80. static GimpDrawableClass *layer_parent_class = NULL;
  81. static GimpChannelClass  *layer_mask_parent_class = NULL;
  82.  
  83. GtkType
  84. gimp_layer_get_type (void)
  85. {
  86.   static GtkType layer_type = 0;
  87.  
  88.   if (!layer_type)
  89.     {
  90.       GtkTypeInfo layer_info =
  91.       {
  92.     "GimpLayer",
  93.     sizeof (GimpLayer),
  94.     sizeof (GimpLayerClass),
  95.     (GtkClassInitFunc) gimp_layer_class_init,
  96.     (GtkObjectInitFunc) gimp_layer_init,
  97.         /* reserved_1 */ NULL,
  98.     /* reserved_2 */ NULL,
  99.     (GtkClassInitFunc) NULL,
  100.       };
  101.  
  102.       layer_type = gtk_type_unique (gimp_drawable_get_type (), &layer_info);
  103.     }
  104.  
  105.   return layer_type;
  106. }
  107.  
  108. static void
  109. gimp_layer_class_init (GimpLayerClass *class)
  110. {
  111.   GtkObjectClass    *object_class;
  112.   GimpDrawableClass *drawable_class;
  113.  
  114.   object_class = (GtkObjectClass*) class;
  115.   drawable_class = (GimpDrawableClass*) class;
  116.  
  117.   layer_parent_class = gtk_type_class (gimp_drawable_get_type ());
  118.  
  119.   layer_signals[REMOVED] =
  120.       gimp_signal_new ("removed",
  121.                0, object_class->type, 0, gimp_sigtype_void);
  122.  
  123.   gtk_object_class_add_signals (object_class, layer_signals, LAST_SIGNAL);
  124.  
  125.   object_class->destroy = gimp_layer_destroy;
  126.   drawable_class->invalidate_preview = layer_invalidate_preview;
  127. }
  128.  
  129. static void
  130. gimp_layer_init (GimpLayer *layer)
  131. {
  132. }
  133.  
  134. GtkType
  135. gimp_layer_mask_get_type (void)
  136. {
  137.   static GtkType layer_mask_type = 0;
  138.  
  139.   if (!layer_mask_type)
  140.     {
  141.       GtkTypeInfo layer_mask_info =
  142.       {
  143.     "GimpLayerMask",
  144.     sizeof (GimpLayerMask),
  145.     sizeof (GimpLayerMaskClass),
  146.     (GtkClassInitFunc) gimp_layer_mask_class_init,
  147.     (GtkObjectInitFunc) gimp_layer_mask_init,
  148.         /* reserved_1 */ NULL,
  149.     /* reserved_2 */ NULL,
  150.     (GtkClassInitFunc) NULL,
  151.       };
  152.  
  153.       layer_mask_type = gtk_type_unique (gimp_channel_get_type (), 
  154.                      &layer_mask_info);
  155.     }
  156.  
  157.   return layer_mask_type;
  158. }
  159.  
  160. static void
  161. gimp_layer_mask_class_init (GimpLayerMaskClass *class)
  162. {
  163.   GtkObjectClass *object_class;
  164.  
  165.   object_class = (GtkObjectClass*) class;
  166.   layer_mask_parent_class = gtk_type_class (gimp_channel_get_type ());
  167.  
  168.   /*
  169.   gtk_object_class_add_signals (object_class, layer_mask_signals, LAST_SIGNAL);
  170.   */
  171.  
  172.   object_class->destroy = gimp_layer_mask_destroy;
  173. }
  174.  
  175. static void
  176. gimp_layer_mask_init (GimpLayerMask *layermask)
  177. {
  178. }
  179.  
  180. /*  static functions  */
  181.  
  182. static void transform_color     (GImage            *gimage,
  183.                  PixelRegion       *layerPR,
  184.                  PixelRegion       *bufPR,
  185.                  GimpDrawable      *drawable,
  186.                  GimpImageBaseType  type);
  187. static void layer_preview_scale (GimpImageBaseType  type,
  188.                  guchar            *cmap,
  189.                  PixelRegion       *srcPR,
  190.                  PixelRegion       *destPR,
  191.                  gint               subsample);
  192.  
  193. /*
  194.  *  Static variables
  195.  */
  196. gint layer_get_count = 0;
  197.  
  198.  
  199. /********************************/
  200. /*  Local function definitions  */
  201. /********************************/
  202.  
  203. static void
  204. layer_invalidate_preview (GtkObject *object)
  205. {
  206.   GimpLayer *layer;
  207.  
  208.   g_return_if_fail (object != NULL);
  209.   g_return_if_fail (GIMP_IS_LAYER (object));
  210.   
  211.   layer = GIMP_LAYER (object);
  212.  
  213.   if (layer_is_floating_sel (layer)) 
  214.     floating_sel_invalidate (layer);
  215. }
  216.  
  217. static void
  218. transform_color (GImage            *gimage,
  219.          PixelRegion       *layerPR,
  220.          PixelRegion       *bufPR,
  221.          GimpDrawable      *drawable,
  222.          GimpImageBaseType  type)
  223. {
  224.   gint      i;
  225.   gint      h;
  226.   guchar   *s;
  227.   guchar   *d;
  228.   gpointer  pr;
  229.  
  230.   for (pr = pixel_regions_register (2, layerPR, bufPR); 
  231.        pr != NULL; 
  232.        pr = pixel_regions_process (pr))
  233.     {
  234.       h = layerPR->h;
  235.       s = bufPR->data;
  236.       d = layerPR->data;
  237.  
  238.       while (h--)
  239.     {
  240.       for (i = 0; i < layerPR->w; i++)
  241.         {
  242.           gimage_transform_color (gimage, drawable,
  243.                       s + (i * bufPR->bytes),
  244.                       d + (i * layerPR->bytes), type);
  245.           /*  copy alpha channel  */
  246.           d[(i + 1) * layerPR->bytes - 1] = s[(i + 1) * bufPR->bytes - 1];
  247.         }
  248.  
  249.       s += bufPR->rowstride;
  250.       d += layerPR->rowstride;
  251.     }
  252.     }
  253. }
  254.  
  255. /**************************/
  256. /*  Function definitions  */
  257. /**************************/
  258.  
  259. Layer *
  260. layer_new (GimpImage        *gimage,
  261.        gint              width,
  262.        gint              height,
  263.        GimpImageType     type,
  264.        gchar            *name,
  265.        gint              opacity,
  266.        LayerModeEffects  mode)
  267. {
  268.   Layer *layer;
  269.  
  270.   if (width == 0 || height == 0)
  271.     {
  272.       g_message (_("Zero width or height layers not allowed."));
  273.       return NULL;
  274.     }
  275.  
  276.   layer = gtk_type_new (gimp_layer_get_type ());
  277.  
  278.   gimp_drawable_configure (GIMP_DRAWABLE (layer),
  279.                gimage, width, height, type, name);
  280.   layer->linked         = FALSE;
  281.   layer->preserve_trans = FALSE;
  282.  
  283.   /*  no layer mask is present at start  */
  284.   layer->mask       = NULL;
  285.   layer->apply_mask = FALSE;
  286.   layer->edit_mask  = FALSE;
  287.   layer->show_mask  = FALSE;
  288.  
  289.   /*  mode and opacity  */
  290.   layer->mode    = mode;
  291.   layer->opacity = opacity;
  292.  
  293.   /*  floating selection variables  */
  294.   layer->fs.backing_store  = NULL;
  295.   layer->fs.drawable       = NULL;
  296.   layer->fs.initial        = TRUE;
  297.   layer->fs.boundary_known = FALSE;
  298.   layer->fs.segs           = NULL;
  299.   layer->fs.num_segs       = 0;
  300.  
  301.   return layer;
  302. }
  303.  
  304. Layer *
  305. layer_ref (Layer *layer)
  306. {
  307.   gtk_object_ref  (GTK_OBJECT (layer));
  308.   gtk_object_sink (GTK_OBJECT (layer));
  309.  
  310.   return layer;
  311. }
  312.  
  313. void
  314. layer_unref (Layer *layer)
  315. {
  316.   gtk_object_unref (GTK_OBJECT (layer));
  317. }
  318.  
  319. Layer *
  320. layer_copy (Layer    *layer,
  321.         gboolean  add_alpha)
  322. {
  323.   gchar         *layer_name;
  324.   Layer         *new_layer;
  325.   GimpImageType  new_type;
  326.   gchar         *ext;
  327.   gint           number;
  328.   gchar         *name;
  329.   gint           len;
  330.   PixelRegion    srcPR;
  331.   PixelRegion    destPR;
  332.  
  333.   /*  formulate the new layer name  */
  334.   name = layer_get_name (layer);
  335.   ext = strrchr (name, '#');
  336.   len = strlen (_("copy"));
  337.  
  338.   if ((strlen(name) >= len &&
  339.        strcmp (&name[strlen (name) - len], _("copy")) == 0) ||
  340.       (ext && (number = atoi (ext + 1)) > 0 && 
  341.        ((gint) (log10 (number) + 1)) == strlen (ext + 1)))
  342.     /* don't have redundant "copy"s */
  343.     layer_name = g_strdup (name);
  344.   else
  345.     layer_name = g_strdup_printf (_("%s copy"), name);
  346.  
  347.   /*  when copying a layer, the copy ALWAYS has an alpha channel  */
  348.   if (add_alpha)
  349.     {
  350.       switch (GIMP_DRAWABLE (layer)->type)
  351.     {
  352.     case RGB_GIMAGE:
  353.       new_type = RGBA_GIMAGE;
  354.       break;
  355.     case GRAY_GIMAGE:
  356.       new_type = GRAYA_GIMAGE;
  357.       break;
  358.     case INDEXED_GIMAGE:
  359.       new_type = INDEXEDA_GIMAGE;
  360.       break;
  361.     default:
  362.       new_type = GIMP_DRAWABLE (layer)->type;
  363.       break;
  364.     }
  365.     }
  366.   else
  367.     new_type = GIMP_DRAWABLE (layer)->type;
  368.  
  369.   /*  allocate a new layer object  */
  370.   new_layer = layer_new (GIMP_DRAWABLE (layer)->gimage,
  371.              GIMP_DRAWABLE (layer)->width,
  372.              GIMP_DRAWABLE (layer)->height,
  373.              new_type, layer_name, layer->opacity, layer->mode);
  374.   if (!new_layer)
  375.     {
  376.       g_message ("layer_copy: could not allocate new layer");
  377.       goto cleanup;
  378.     }
  379.  
  380.   GIMP_DRAWABLE (new_layer)->offset_x = GIMP_DRAWABLE (layer)->offset_x;
  381.   GIMP_DRAWABLE (new_layer)->offset_y = GIMP_DRAWABLE (layer)->offset_y;
  382.   GIMP_DRAWABLE (new_layer)->visible  = GIMP_DRAWABLE (layer)->visible;
  383.   new_layer->linked         = layer->linked;
  384.   new_layer->preserve_trans = layer->preserve_trans;
  385.  
  386.   /*  copy the contents across layers  */
  387.   if (new_type == GIMP_DRAWABLE (layer)->type)
  388.     {
  389.       pixel_region_init (&srcPR, GIMP_DRAWABLE (layer)->tiles, 
  390.              0, 0, 
  391.              GIMP_DRAWABLE (layer)->width, 
  392.              GIMP_DRAWABLE (layer)->height, 
  393.              FALSE);
  394.       pixel_region_init (&destPR, GIMP_DRAWABLE(new_layer)->tiles, 
  395.              0, 0, 
  396.              GIMP_DRAWABLE (layer)->width, 
  397.              GIMP_DRAWABLE (layer)->height, 
  398.              TRUE);
  399.       copy_region (&srcPR, &destPR);
  400.     }
  401.   else
  402.     {
  403.       pixel_region_init (&srcPR, GIMP_DRAWABLE (layer)->tiles, 
  404.              0, 0, 
  405.              GIMP_DRAWABLE (layer)->width, 
  406.              GIMP_DRAWABLE (layer)->height, 
  407.              FALSE);
  408.       pixel_region_init (&destPR, GIMP_DRAWABLE (new_layer)->tiles, 
  409.              0, 0, 
  410.              GIMP_DRAWABLE (layer)->width, 
  411.              GIMP_DRAWABLE(layer)->height, 
  412.              TRUE);
  413.       add_alpha_region (&srcPR, &destPR);
  414.     }
  415.  
  416.   /*  duplicate the layer mask if necessary  */
  417.   if (layer->mask)
  418.     {
  419.       new_layer->mask       = layer_mask_ref (layer_mask_copy (layer->mask));
  420.       new_layer->apply_mask = layer->apply_mask;
  421.       new_layer->edit_mask  = layer->edit_mask;
  422.       new_layer->show_mask  = layer->show_mask;
  423.  
  424.       layer_mask_set_layer (new_layer->mask, new_layer);
  425.     }
  426.  
  427.   /* copy the parasites */
  428.   gtk_object_unref (GTK_OBJECT (GIMP_DRAWABLE (new_layer)->parasites));
  429.   GIMP_DRAWABLE (new_layer)->parasites 
  430.     = parasite_list_copy (GIMP_DRAWABLE (layer)->parasites);
  431.  
  432.  cleanup:
  433.   /*  free up the layer_name memory  */
  434.   g_free (layer_name);
  435.  
  436.   return new_layer;
  437. }
  438.  
  439. Layer *
  440. layer_new_from_tiles (GimpImage        *gimage,
  441.                       GimpImageType     layer_type,
  442.               TileManager      *tiles,
  443.               gchar            *name,
  444.               gint              opacity,
  445.               LayerModeEffects  mode)
  446. {
  447.   Layer       *new_layer;
  448.   PixelRegion  layerPR;
  449.   PixelRegion  bufPR;
  450.  
  451.   /*  Function copies buffer to a layer
  452.    *  taking into consideration the possibility of transforming
  453.    *  the contents to meet the requirements of the target image type
  454.    */
  455.  
  456.   /*  If no image or no tile manager, return NULL  */
  457.   if (!gimage || !tiles )
  458.     return NULL;
  459.   
  460.   /*  the layer_type needs to have alpha */
  461.   g_return_val_if_fail (GIMP_IMAGE_TYPE_HAS_ALPHA (layer_type), NULL);
  462.  
  463.    /*  Create the new layer  */
  464.   new_layer = layer_new (0, tiles->width, tiles->height,
  465.              layer_type, name, opacity, mode);
  466.  
  467.   if (!new_layer)
  468.     {
  469.       g_message ("layer_new_from_tiles: could not allocate new layer");
  470.       return NULL;
  471.     }
  472.  
  473.   /*  Configure the pixel regions  */
  474.   pixel_region_init (&layerPR, GIMP_DRAWABLE (new_layer)->tiles, 
  475.              0, 0, 
  476.              GIMP_DRAWABLE (new_layer)->width, 
  477.              GIMP_DRAWABLE (new_layer)->height, 
  478.              TRUE);
  479.   pixel_region_init (&bufPR, tiles, 
  480.              0, 0, 
  481.              GIMP_DRAWABLE (new_layer)->width, 
  482.              GIMP_DRAWABLE (new_layer)->height, 
  483.              FALSE);
  484.  
  485.   if ((tiles->bpp == 4 && GIMP_DRAWABLE (new_layer)->type == RGBA_GIMAGE) ||
  486.       (tiles->bpp == 2 && GIMP_DRAWABLE (new_layer)->type == GRAYA_GIMAGE))
  487.     /*  If we want a layer the same type as the buffer  */
  488.     copy_region (&bufPR, &layerPR);
  489.   else
  490.     /*  Transform the contents of the buf to the new_layer  */
  491.     transform_color (gimage, &layerPR, &bufPR, GIMP_DRAWABLE (new_layer),
  492.              (tiles->bpp == 4) ? RGB : GRAY);
  493.   
  494.   return new_layer;
  495. }
  496.  
  497. LayerMask *
  498. layer_add_mask (Layer     *layer,
  499.         LayerMask *mask)
  500. {
  501.   if (layer->mask)
  502.     return NULL;
  503.  
  504.   layer->mask = layer_mask_ref (mask);
  505.   mask->layer = layer;
  506.  
  507.   /*  Set the application mode in the layer to "apply"  */
  508.   layer->apply_mask = TRUE;
  509.   layer->edit_mask  = TRUE;
  510.   layer->show_mask  = FALSE;
  511.  
  512.   drawable_update (GIMP_DRAWABLE (layer),
  513.            0, 0,
  514.            GIMP_DRAWABLE (layer)->width, 
  515.            GIMP_DRAWABLE (layer)->height);
  516.  
  517.   return layer->mask;
  518. }
  519.  
  520. LayerMask *
  521. layer_create_mask (Layer       *layer,
  522.            AddMaskType  add_mask_type)
  523. {
  524.   PixelRegion  maskPR;
  525.   PixelRegion  layerPR;
  526.   LayerMask   *mask;
  527.   gchar       *mask_name;
  528.   guchar       black[3] = {0, 0, 0};
  529.   guchar       white_mask = OPAQUE_OPACITY;
  530.   guchar       black_mask = TRANSPARENT_OPACITY;
  531.  
  532.   mask_name = g_strdup_printf (_("%s mask"), GIMP_DRAWABLE (layer)->name);
  533.  
  534.   /*  Create the layer mask  */
  535.   mask = layer_mask_new (GIMP_DRAWABLE (layer)->gimage,
  536.              GIMP_DRAWABLE (layer)->width,
  537.              GIMP_DRAWABLE (layer)->height,
  538.              mask_name, OPAQUE_OPACITY, black);
  539.   GIMP_DRAWABLE (mask)->offset_x = GIMP_DRAWABLE (layer)->offset_x;
  540.   GIMP_DRAWABLE (mask)->offset_y = GIMP_DRAWABLE (layer)->offset_y;
  541.  
  542.   pixel_region_init (&maskPR, GIMP_DRAWABLE (mask)->tiles, 
  543.              0, 0, 
  544.              GIMP_DRAWABLE (mask)->width, GIMP_DRAWABLE (mask)->height, 
  545.              TRUE);
  546.  
  547.   switch (add_mask_type)
  548.     {
  549.     case ADD_WHITE_MASK:
  550.       color_region (&maskPR, &white_mask);
  551.       break;
  552.     case ADD_BLACK_MASK:
  553.       color_region (&maskPR, &black_mask);
  554.       break;
  555.     case ADD_ALPHA_MASK:
  556.       /*  Extract the layer's alpha channel  */
  557.       if (layer_has_alpha (layer))
  558.     {
  559.       pixel_region_init (&layerPR, GIMP_DRAWABLE (layer)->tiles, 
  560.                  0, 0, 
  561.                  GIMP_DRAWABLE (layer)->width, 
  562.                  GIMP_DRAWABLE (layer)->height, 
  563.                  FALSE);
  564.       extract_alpha_region (&layerPR, NULL, &maskPR);
  565.     }
  566.       break;
  567.     }
  568.  
  569.   g_free (mask_name);
  570.   return mask;
  571. }
  572.  
  573. Layer *
  574. layer_get_ID (gint ID)
  575. {
  576.   GimpDrawable *drawable;
  577.  
  578.   drawable = drawable_get_ID (ID);
  579.   if (drawable && GIMP_IS_LAYER (drawable)) 
  580.     return GIMP_LAYER (drawable);
  581.   else
  582.     return NULL;
  583. }
  584.  
  585. void
  586. layer_delete (Layer *layer)
  587. {
  588.   gtk_object_unref (GTK_OBJECT (layer));
  589. }
  590.  
  591. static void
  592. gimp_layer_destroy (GtkObject *object)
  593. {
  594.   GimpLayer *layer;
  595.   
  596.   g_return_if_fail (object != NULL);
  597.   g_return_if_fail (GIMP_IS_LAYER (object));
  598.  
  599.   layer = GIMP_LAYER (object);
  600.  
  601.   /*  if a layer mask exists, free it  */
  602.   if (layer->mask)
  603.     layer_mask_delete (layer->mask);
  604.  
  605.   /*  free the layer boundary if it exists  */
  606.   if (layer->fs.segs)
  607.     g_free (layer->fs.segs);
  608.  
  609.   /*  free the floating selection if it exists  */
  610.   if (layer_is_floating_sel (layer))
  611.     {
  612.       tile_manager_destroy (layer->fs.backing_store);
  613.     }
  614.  
  615.   if (GTK_OBJECT_CLASS (layer_parent_class)->destroy)
  616.     (*GTK_OBJECT_CLASS (layer_parent_class)->destroy) (object);
  617. }
  618.  
  619. /* The removed signal is sent out when the layer is no longer
  620.  * associcated with an image.  It's needed because layers aren't
  621.  * destroyed immediately, but kept around for undo purposes.  Connect
  622.  * to the removed signal to update bits of UI that are tied to a
  623.  * particular layer. */
  624. void
  625. layer_removed (Layer    *layer,
  626.            gpointer  data)
  627. {
  628.   g_return_if_fail (layer != NULL);
  629.   g_return_if_fail (GIMP_IS_LAYER (layer));
  630.  
  631.   gtk_signal_emit (GTK_OBJECT (layer), layer_signals[REMOVED]);
  632. }
  633.  
  634. void
  635. layer_apply_mask (Layer         *layer,
  636.           MaskApplyMode  mode)
  637. {
  638.   PixelRegion srcPR, maskPR;
  639.  
  640.   if (!layer->mask)
  641.     return;
  642.  
  643.   /*  this operation can only be done to layers with an alpha channel  */
  644.   if (! layer_has_alpha (layer))
  645.     return;
  646.  
  647.   /*  Need to save the mask here for undo  */
  648.  
  649.   if (mode == APPLY)
  650.     {
  651.       /*  Put this apply mask operation on the undo stack  */
  652.       drawable_apply_image (GIMP_DRAWABLE (layer),
  653.                 0, 0,
  654.                 GIMP_DRAWABLE (layer)->width,
  655.                 GIMP_DRAWABLE (layer)->height,
  656.                 NULL, FALSE);
  657.  
  658.       /*  Combine the current layer's alpha channel and the mask  */
  659.       pixel_region_init (&srcPR, GIMP_DRAWABLE (layer)->tiles, 
  660.              0, 0, 
  661.              GIMP_DRAWABLE (layer)->width, 
  662.              GIMP_DRAWABLE(layer)->height, 
  663.              TRUE);
  664.       pixel_region_init (&maskPR, GIMP_DRAWABLE (layer->mask)->tiles, 
  665.              0, 0, 
  666.              GIMP_DRAWABLE (layer)->width, 
  667.              GIMP_DRAWABLE(layer)->height, 
  668.              FALSE);
  669.  
  670.       apply_mask_to_region (&srcPR, &maskPR, OPAQUE_OPACITY);
  671.       GIMP_DRAWABLE (layer)->preview_valid = FALSE;
  672.  
  673.       layer->mask       = NULL;
  674.       layer->apply_mask = FALSE;
  675.       layer->edit_mask  = FALSE;
  676.       layer->show_mask  = FALSE;
  677.     }
  678.   else if (mode == DISCARD)
  679.     {
  680.       layer->mask       = NULL;
  681.       layer->apply_mask = FALSE;
  682.       layer->edit_mask  = FALSE;
  683.       layer->show_mask  = FALSE;
  684.     }
  685. }
  686.  
  687. void
  688. layer_translate (Layer    *layer,
  689.          gint      off_x,
  690.          gint      off_y)
  691. {
  692.   /*  the undo call goes here  */
  693.   undo_push_layer_displace (GIMP_DRAWABLE (layer)->gimage, layer);
  694.  
  695.   /*  update the affected region  */
  696.   drawable_update (GIMP_DRAWABLE (layer), 0, 0, 
  697.            GIMP_DRAWABLE (layer)->width, 
  698.            GIMP_DRAWABLE (layer)->height);
  699.  
  700.   /*  invalidate the selection boundary because of a layer modification  */
  701.   layer_invalidate_boundary (layer);
  702.  
  703.   /*  update the layer offsets  */
  704.   GIMP_DRAWABLE (layer)->offset_x += off_x;
  705.   GIMP_DRAWABLE (layer)->offset_y += off_y;
  706.  
  707.   /*  update the affected region  */
  708.   drawable_update (GIMP_DRAWABLE (layer), 
  709.            0, 0, 
  710.            GIMP_DRAWABLE (layer)->width, 
  711.            GIMP_DRAWABLE (layer)->height);
  712.  
  713.   if (layer->mask) 
  714.     {
  715.       GIMP_DRAWABLE (layer->mask)->offset_x += off_x;
  716.       GIMP_DRAWABLE (layer->mask)->offset_y += off_y;
  717.  
  718.       /*  invalidate the mask preview  */
  719.       gimp_drawable_invalidate_preview (GIMP_DRAWABLE (layer->mask),
  720.                     FALSE);
  721.     }
  722. }
  723.  
  724. void
  725. layer_add_alpha (Layer *layer)
  726. {
  727.   PixelRegion srcPR, destPR;
  728.   TileManager *new_tiles;
  729.   GimpImageType type;
  730.  
  731.   /*  Don't bother if the layer already has alpha  */
  732.   switch (GIMP_DRAWABLE (layer)->type)
  733.     {
  734.     case RGB_GIMAGE:
  735.       type = RGBA_GIMAGE;
  736.       break;
  737.     case GRAY_GIMAGE:
  738.       type = GRAYA_GIMAGE;
  739.       break;
  740.     case INDEXED_GIMAGE:
  741.       type = INDEXEDA_GIMAGE;
  742.       break;
  743.     case RGBA_GIMAGE:
  744.     case GRAYA_GIMAGE:
  745.     case INDEXEDA_GIMAGE:
  746.     default:
  747.       return;
  748.       break;
  749.     }
  750.  
  751.   /*  Configure the pixel regions  */
  752.   pixel_region_init (&srcPR, GIMP_DRAWABLE (layer)->tiles, 
  753.              0, 0, 
  754.              GIMP_DRAWABLE (layer)->width, 
  755.              GIMP_DRAWABLE (layer)->height, 
  756.              FALSE);
  757.  
  758.   /*  Allocate the new layer, configure dest region  */
  759.   new_tiles = tile_manager_new (GIMP_DRAWABLE (layer)->width, 
  760.                 GIMP_DRAWABLE (layer)->height, 
  761.                 GIMP_DRAWABLE (layer)->bytes + 1);
  762.   pixel_region_init (&destPR, new_tiles, 
  763.              0, 0, 
  764.              GIMP_DRAWABLE (layer)->width, 
  765.              GIMP_DRAWABLE (layer)->height, 
  766.              TRUE);
  767.  
  768.   /*  Add an alpha channel  */
  769.   add_alpha_region (&srcPR, &destPR);
  770.  
  771.   /*  Push the layer on the undo stack  */
  772.   undo_push_layer_mod (GIMP_DRAWABLE (layer)->gimage, layer);
  773.  
  774.   /*  Configure the new layer  */
  775.   GIMP_DRAWABLE (layer)->tiles         = new_tiles;
  776.   GIMP_DRAWABLE (layer)->type          = type;
  777.   GIMP_DRAWABLE (layer)->bytes         = GIMP_DRAWABLE(layer)->bytes + 1;
  778.   GIMP_DRAWABLE (layer)->has_alpha     = GIMP_IMAGE_TYPE_HAS_ALPHA (type);
  779.   GIMP_DRAWABLE (layer)->preview_valid = FALSE;
  780.  
  781.   gtk_signal_emit_by_name (GTK_OBJECT (gimp_drawable_gimage (GIMP_DRAWABLE (layer))),
  782.                "restructure");
  783. }
  784.  
  785. static void
  786. layer_scale_lowlevel (Layer *layer,
  787.               gint   new_width,
  788.               gint   new_height,
  789.               gint   new_offset_x,
  790.               gint   new_offset_y)
  791. {
  792.   PixelRegion srcPR, destPR;
  793.   TileManager *new_tiles;
  794.  
  795.   /*  Update the old layer position  */
  796.   drawable_update (GIMP_DRAWABLE (layer),
  797.            0, 0,
  798.            GIMP_DRAWABLE (layer)->width, 
  799.            GIMP_DRAWABLE (layer)->height);
  800.  
  801.   /*  Configure the pixel regions  */
  802.   pixel_region_init (&srcPR, GIMP_DRAWABLE(layer)->tiles, 
  803.              0, 0, 
  804.              GIMP_DRAWABLE (layer)->width, 
  805.              GIMP_DRAWABLE (layer)->height, 
  806.              FALSE);
  807.  
  808.   /*  Allocate the new layer, configure dest region  */
  809.   new_tiles = tile_manager_new (new_width, new_height, 
  810.                 GIMP_DRAWABLE (layer)->bytes);
  811.   pixel_region_init (&destPR, new_tiles, 
  812.              0, 0, 
  813.              new_width, new_height, 
  814.              TRUE);
  815.  
  816.   /*  Scale the layer -
  817.    *   If the layer is of type INDEXED, then we don't use pixel-value
  818.    *   resampling because that doesn't necessarily make sense for INDEXED
  819.    *   images.
  820.    */
  821.   if ((GIMP_DRAWABLE (layer)->type == INDEXED_GIMAGE) || 
  822.       (GIMP_DRAWABLE (layer)->type == INDEXEDA_GIMAGE))
  823.     scale_region_no_resample (&srcPR, &destPR);
  824.   else
  825.     scale_region (&srcPR, &destPR);
  826.  
  827.   /*  Push the layer on the undo stack  */
  828.   undo_push_layer_mod (GIMP_DRAWABLE (layer)->gimage, layer);
  829.  
  830.   /*  Configure the new layer  */
  831.  
  832.   GIMP_DRAWABLE (layer)->offset_x = new_offset_x;
  833.   GIMP_DRAWABLE (layer)->offset_y = new_offset_y;
  834.   GIMP_DRAWABLE (layer)->tiles    = new_tiles;
  835.   GIMP_DRAWABLE (layer)->width    = new_width;
  836.   GIMP_DRAWABLE (layer)->height   = new_height;
  837.  
  838.   /*  If there is a layer mask, make sure it gets scaled also  */
  839.   if (layer->mask) 
  840.     {
  841.       GIMP_DRAWABLE (layer->mask)->offset_x = GIMP_DRAWABLE (layer)->offset_x;
  842.       GIMP_DRAWABLE (layer->mask)->offset_y = GIMP_DRAWABLE (layer)->offset_y;
  843.       channel_scale (GIMP_CHANNEL (layer->mask), new_width, new_height);
  844.     }
  845.  
  846.   /*  Make sure we're not caching any old selection info  */
  847.   layer_invalidate_boundary (layer);
  848.   
  849.   /*  Update the new layer position  */
  850.  
  851.   drawable_update (GIMP_DRAWABLE (layer),
  852.            0, 0,
  853.            GIMP_DRAWABLE (layer)->width, 
  854.            GIMP_DRAWABLE (layer)->height);
  855. }
  856.  
  857. /**
  858.  * layer_check_scaling:
  859.  * @layer:      Layer to check
  860.  * @new_width:  proposed width of layer, in pixels
  861.  * @new_height: proposed height of layer, in pixels
  862.  *
  863.  * Scales layer dimensions, then snaps them to pixel centers
  864.  *
  865.  * Returns FALSE if any dimension reduces to zero as a result 
  866.  *         of this; otherwise, returns TRUE.
  867.  */
  868. gboolean 
  869. layer_check_scaling (Layer  *layer,
  870.              gint    new_width,
  871.              gint    new_height)
  872. {
  873.    GImage  *gimage           = GIMP_DRAWABLE (layer)->gimage;
  874.    gdouble  img_scale_w      = (gdouble) new_width / (gdouble) gimage->width;
  875.    gdouble  img_scale_h      = (gdouble) new_height / (gdouble) gimage->height;
  876.    gint     new_layer_width  = 
  877.      (gint) (0.5 + img_scale_w * (gdouble) GIMP_DRAWABLE (layer)->width); 
  878.    gint     new_layer_height = 
  879.      (gint) (0.5 + img_scale_h * (gdouble) GIMP_DRAWABLE (layer)->height);
  880.  
  881.    return (new_layer_width != 0 && new_layer_height != 0);  
  882. }
  883.  
  884. /**
  885.  * layer_scale_by_factors:
  886.  * @layer: Layer to be transformed by explicit width and height factors.
  887.  * @w_factor: scale factor to apply to width and horizontal offset
  888.  * @h_factor: scale factor to apply to height and vertical offset
  889.  * 
  890.  * Scales layer dimensions and offsets by uniform width and
  891.  * height factors.
  892.  * 
  893.  * Use layer_scale_by_factors() in circumstances when the
  894.  * same width and height scaling factors are to be uniformly
  895.  * applied to a set of layers. In this context, the layer's
  896.  * dimensions and offsets from the sides of the containing
  897.  * image all change by these predetermined factors. By fiat,
  898.  * the fixed point of the transform is the upper left hand
  899.  * corner of the image. Returns gboolean FALSE if a requested
  900.  * scale factor is zero or if a scaling zero's out a layer
  901.  * dimension; returns TRUE otherwise.
  902.  *
  903.  * Use layer_scale() in circumstances where new layer width
  904.  * and height dimensions are predetermined instead.
  905.  *
  906.  * Side effects: Undo set created for layer. Old layer imagery 
  907.  *               scaled & painted to new layer tiles. 
  908.  *
  909.  * Returns: TRUE, if the scaled layer has positive dimensions
  910.  *          FALSE if the scaled layer has at least one zero dimension
  911.  */
  912. gboolean
  913. layer_scale_by_factors (Layer   *layer,
  914.             gdouble  w_factor,
  915.             gdouble  h_factor)
  916. {
  917.   gint new_width, new_height;
  918.   gint new_offset_x, new_offset_y;
  919.   
  920.   if (w_factor == 0.0 || h_factor == 0.0)
  921.     {
  922.       g_message ("layer_scale_by_factors: Error. Requested width or height scale equals zero.");
  923.       return FALSE;
  924.     }
  925.  
  926.   new_offset_x = 
  927.     (gint) (0.5 + w_factor * (gdouble) GIMP_DRAWABLE (layer)->offset_x);
  928.   new_offset_y = 
  929.     (gint) (0.5 + h_factor * (gdouble) GIMP_DRAWABLE (layer)->offset_y);
  930.   new_width    = 
  931.     (gint) (0.5 + w_factor * (gdouble) GIMP_DRAWABLE (layer)->width);
  932.   new_height   = 
  933.     (gint) (0.5 + h_factor * (gdouble) GIMP_DRAWABLE (layer)->height);
  934.  
  935.   if (new_width != 0 && new_height != 0)
  936.     {
  937.       layer_scale_lowlevel (layer, 
  938.                 new_width, new_height, new_offset_x, new_offset_y);
  939.       return TRUE;
  940.     }
  941.   else
  942.     return FALSE;
  943. }
  944.  
  945. /**
  946.  * layer_scale:
  947.  * @layer:        The layer to be transformed by width & height scale factors
  948.  * @new_width:    The width that layer will acquire
  949.  * @new_height:   The height that the layer will acquire
  950.  * @local_origin: sets fixed point of the scaling transform. See below.
  951.  *
  952.  * Sets layer dimensions to new_width and
  953.  * new_height. Derives vertical and horizontal scaling
  954.  * transforms from new width and height. If local_origin is
  955.  * TRUE, the fixed point of the scaling transform coincides
  956.  * with the layer's center point.  Otherwise, the fixed
  957.  * point is taken to be [-GIMP_DRAWABLE(layer)->offset_x,
  958.  * -GIMP_DRAWABLE(layer)->offset_y].
  959.  *
  960.  * Since this function derives scale factors from new and
  961.  * current layer dimensions, these factors will vary from
  962.  * layer to layer because of aliasing artifacts; factor
  963.  * variations among layers can be quite large where layer
  964.  * dimensions approach pixel dimensions. Use 
  965.  * layer_scale_by_factors where constant scales are to
  966.  * be uniformly applied to a number of layers.
  967.  *
  968.  * Side effects: undo set created for layer.
  969.  *               Old layer imagery scaled 
  970.  *               & painted to new layer tiles 
  971.  */
  972. void
  973. layer_scale (Layer   *layer,
  974.          gint     new_width,
  975.          gint     new_height,
  976.          gboolean local_origin)
  977. {
  978.   gint new_offset_x, new_offset_y;
  979.  
  980.   if (new_width == 0 || new_height == 0)
  981.     {
  982.       g_message ("layer_scale: Error. Requested width or height equals zero.");
  983.       return;
  984.     }
  985.   if (local_origin)
  986.     {
  987.       new_offset_x = GIMP_DRAWABLE(layer)->offset_x + 
  988.     ((GIMP_DRAWABLE(layer)->width  - new_width) / 2.0);
  989.       new_offset_y = GIMP_DRAWABLE(layer)->offset_y + 
  990.     ((GIMP_DRAWABLE(layer)->height - new_height) / 2.0);
  991.     }
  992.   else
  993.     {
  994.       new_offset_x = (gint)(((gdouble) new_width * 
  995.                  GIMP_DRAWABLE(layer)->offset_x / 
  996.                  (gdouble) GIMP_DRAWABLE(layer)->width));
  997.       new_offset_y = (gint)(((gdouble) new_height * 
  998.                  GIMP_DRAWABLE(layer)->offset_y / 
  999.                  (gdouble) GIMP_DRAWABLE(layer)->height));
  1000.     }
  1001.   layer_scale_lowlevel (layer, 
  1002.             new_width, new_height, 
  1003.             new_offset_x, new_offset_y);
  1004. }
  1005.  
  1006. void
  1007. layer_resize (Layer *layer,
  1008.           gint   new_width,
  1009.           gint   new_height,
  1010.           gint   offx,
  1011.           gint   offy)
  1012. {
  1013.   PixelRegion srcPR, destPR;
  1014.   TileManager *new_tiles;
  1015.   int w, h;
  1016.   int x1, y1, x2, y2;
  1017.  
  1018.   if (!new_width || !new_height)
  1019.     return;
  1020.  
  1021.   x1 = CLAMP (offx, 0, new_width);
  1022.   y1 = CLAMP (offy, 0, new_height);
  1023.   x2 = CLAMP ((offx + GIMP_DRAWABLE(layer)->width),  0, new_width);
  1024.   y2 = CLAMP ((offy + GIMP_DRAWABLE(layer)->height), 0, new_height);
  1025.   w = x2 - x1;
  1026.   h = y2 - y1;
  1027.  
  1028.   if (offx > 0)
  1029.     {
  1030.       x1 = 0;
  1031.       x2 = offx;
  1032.     }
  1033.   else
  1034.     {
  1035.       x1 = -offx;
  1036.       x2 = 0;
  1037.     }
  1038.  
  1039.   if (offy > 0)
  1040.     {
  1041.       y1 = 0;
  1042.       y2 = offy;
  1043.     }
  1044.   else
  1045.     {
  1046.       y1 = -offy;
  1047.       y2 = 0;
  1048.     }
  1049.  
  1050.   /*  Update the old layer position  */
  1051.   drawable_update (GIMP_DRAWABLE(layer),
  1052.            0, 0,
  1053.            GIMP_DRAWABLE(layer)->width, GIMP_DRAWABLE(layer)->height);
  1054.  
  1055.   /*  Configure the pixel regions  */
  1056.   pixel_region_init (&srcPR, GIMP_DRAWABLE(layer)->tiles, 
  1057.              x1, y1, 
  1058.              w, h, 
  1059.              FALSE);
  1060.  
  1061.   /*  Allocate the new layer, configure dest region  */
  1062.   new_tiles = tile_manager_new (new_width, new_height, 
  1063.                 GIMP_DRAWABLE(layer)->bytes);
  1064.   pixel_region_init (&destPR, new_tiles, 
  1065.              0, 0, 
  1066.              new_width, new_height, 
  1067.              TRUE);
  1068.  
  1069.   /*  fill with the fill color  */
  1070.   if (layer_has_alpha (layer))
  1071.     {
  1072.       /*  Set to transparent and black  */
  1073.       unsigned char bg[4] = {0, 0, 0, 0};
  1074.       color_region (&destPR, bg);
  1075.     }
  1076.   else
  1077.     {
  1078.       unsigned char bg[3];
  1079.       gimage_get_background (GIMP_DRAWABLE(layer)->gimage, 
  1080.                  GIMP_DRAWABLE(layer), bg);
  1081.       color_region (&destPR, bg);
  1082.     }
  1083.   pixel_region_init (&destPR, new_tiles, 
  1084.              x2, y2, 
  1085.              w, h, 
  1086.              TRUE);
  1087.  
  1088.   /*  copy from the old to the new  */
  1089.   if (w && h)
  1090.     copy_region (&srcPR, &destPR);
  1091.  
  1092.   /*  Push the layer on the undo stack  */
  1093.   undo_push_layer_mod (GIMP_DRAWABLE(layer)->gimage, layer);
  1094.  
  1095.   /*  Configure the new layer  */
  1096.   GIMP_DRAWABLE(layer)->tiles = new_tiles;
  1097.   GIMP_DRAWABLE(layer)->offset_x = x1 + GIMP_DRAWABLE(layer)->offset_x - x2;
  1098.   GIMP_DRAWABLE(layer)->offset_y = y1 + GIMP_DRAWABLE(layer)->offset_y - y2;
  1099.   GIMP_DRAWABLE(layer)->width = new_width;
  1100.   GIMP_DRAWABLE(layer)->height = new_height;
  1101.  
  1102.   /*  If there is a layer mask, make sure it gets resized also  */
  1103.   if (layer->mask)
  1104.     {
  1105.       GIMP_DRAWABLE(layer->mask)->offset_x = GIMP_DRAWABLE(layer)->offset_x;
  1106.       GIMP_DRAWABLE(layer->mask)->offset_y = GIMP_DRAWABLE(layer)->offset_y;
  1107.       channel_resize (GIMP_CHANNEL (layer->mask),
  1108.               new_width, new_height, offx, offy);
  1109.     }
  1110.  
  1111.   /*  Make sure we're not caching any old selection info  */
  1112.   layer_invalidate_boundary (layer);
  1113.  
  1114.   /*  update the new layer area  */
  1115.   drawable_update (GIMP_DRAWABLE(layer),
  1116.            0, 0,
  1117.            GIMP_DRAWABLE(layer)->width, GIMP_DRAWABLE(layer)->height);
  1118. }
  1119.  
  1120. void
  1121. layer_resize_to_image (Layer *layer)
  1122. {
  1123.   GImage *gimage;
  1124.   gint offset_x;
  1125.   gint offset_y;
  1126.  
  1127.   if (!(gimage = GIMP_DRAWABLE (layer)->gimage))
  1128.     return;
  1129.  
  1130.   undo_push_group_start (gimage, LAYER_RESIZE_UNDO);
  1131.  
  1132.   if (layer_is_floating_sel (layer))
  1133.     floating_sel_relax (layer, TRUE);
  1134.  
  1135.    gimp_drawable_offsets (GIMP_DRAWABLE (layer), &offset_x, &offset_y);
  1136.    layer_resize (layer, gimage->width, gimage->height, offset_x, offset_y);
  1137.  
  1138.   if (layer_is_floating_sel (layer))
  1139.     floating_sel_rigor (layer, TRUE);
  1140.  
  1141.   undo_push_group_end (gimage);
  1142. }
  1143.  
  1144. BoundSeg *
  1145. layer_boundary (Layer *layer,
  1146.         gint  *num_segs)
  1147. {
  1148.   BoundSeg *new_segs;
  1149.  
  1150.   /*  Create the four boundary segments that encompass this
  1151.    *  layer's boundary.
  1152.    */
  1153.   new_segs = (BoundSeg *) g_malloc (sizeof (BoundSeg) * 4);
  1154.   *num_segs = 4;
  1155.  
  1156.   /*  if the layer is a floating selection  */
  1157.   if (layer_is_floating_sel (layer))
  1158.     {
  1159.       /*  if the owner drawable is a channel, just return nothing  */
  1160.       if (GIMP_IS_CHANNEL (layer->fs.drawable))
  1161.     {
  1162.       *num_segs = 0;
  1163.       return NULL;
  1164.     }
  1165.       /*  otherwise, set the layer to the owner drawable  */
  1166.       else
  1167.     layer = GIMP_LAYER (layer->fs.drawable);
  1168.     }
  1169.  
  1170.   new_segs[0].x1 = GIMP_DRAWABLE(layer)->offset_x;
  1171.   new_segs[0].y1 = GIMP_DRAWABLE(layer)->offset_y;
  1172.   new_segs[0].x2 = GIMP_DRAWABLE(layer)->offset_x;
  1173.   new_segs[0].y2 = GIMP_DRAWABLE(layer)->offset_y + GIMP_DRAWABLE(layer)->height;
  1174.   new_segs[0].open = 1;
  1175.  
  1176.   new_segs[1].x1 = GIMP_DRAWABLE(layer)->offset_x;
  1177.   new_segs[1].y1 = GIMP_DRAWABLE(layer)->offset_y;
  1178.   new_segs[1].x2 = GIMP_DRAWABLE(layer)->offset_x + GIMP_DRAWABLE(layer)->width;
  1179.   new_segs[1].y2 = GIMP_DRAWABLE(layer)->offset_y;
  1180.   new_segs[1].open = 1;
  1181.  
  1182.   new_segs[2].x1 = GIMP_DRAWABLE(layer)->offset_x + GIMP_DRAWABLE(layer)->width;
  1183.   new_segs[2].y1 = GIMP_DRAWABLE(layer)->offset_y;
  1184.   new_segs[2].x2 = GIMP_DRAWABLE(layer)->offset_x + GIMP_DRAWABLE(layer)->width;
  1185.   new_segs[2].y2 = GIMP_DRAWABLE(layer)->offset_y + GIMP_DRAWABLE(layer)->height;
  1186.   new_segs[2].open = 0;
  1187.  
  1188.   new_segs[3].x1 = GIMP_DRAWABLE(layer)->offset_x;
  1189.   new_segs[3].y1 = GIMP_DRAWABLE(layer)->offset_y + GIMP_DRAWABLE(layer)->height;
  1190.   new_segs[3].x2 = GIMP_DRAWABLE(layer)->offset_x + GIMP_DRAWABLE(layer)->width;
  1191.   new_segs[3].y2 = GIMP_DRAWABLE(layer)->offset_y + GIMP_DRAWABLE(layer)->height;
  1192.   new_segs[3].open = 0;
  1193.  
  1194.   return new_segs;
  1195. }
  1196.  
  1197. void
  1198. layer_invalidate_boundary (Layer *layer)
  1199. {
  1200.   GImage *gimage;
  1201.   Channel *mask;
  1202.  
  1203.   /*  first get the selection mask channel  */
  1204.   if (! (gimage = drawable_gimage (GIMP_DRAWABLE (layer))))
  1205.     return;
  1206.  
  1207.   /*  Turn the current selection off  */
  1208.   gdisplays_selection_visibility (gimage, SelectionOff);
  1209.  
  1210.   /*  clear the affected region surrounding the layer  */
  1211.   gdisplays_selection_visibility (gimage, SelectionLayerOff); 
  1212.  
  1213.   mask = gimage_get_mask (gimage);
  1214.  
  1215.   /*  Only bother with the bounds if there is a selection  */
  1216.   if (! channel_is_empty (mask))
  1217.     {
  1218.       mask->bounds_known   = FALSE;
  1219.       mask->boundary_known = FALSE;
  1220.     }
  1221.  
  1222.   if (layer_is_floating_sel(layer))
  1223.       floating_sel_invalidate(layer);
  1224. }
  1225.  
  1226. gint
  1227. layer_pick_correlate (Layer *layer,
  1228.               gint   x,
  1229.               gint   y)
  1230. {
  1231.   Tile *tile;
  1232.   Tile *mask_tile;
  1233.   int val;
  1234.  
  1235.   /*  Is the point inside the layer?
  1236.    *  First transform the point to layer coordinates...
  1237.    */
  1238.   x -= GIMP_DRAWABLE(layer)->offset_x;
  1239.   y -= GIMP_DRAWABLE(layer)->offset_y;
  1240.   if (x >= 0 && x < GIMP_DRAWABLE(layer)->width &&
  1241.       y >= 0 && y < GIMP_DRAWABLE(layer)->height &&
  1242.       GIMP_DRAWABLE(layer)->visible)
  1243.     {
  1244.       /*  If the point is inside, and the layer has no
  1245.        *  alpha channel, success!
  1246.        */
  1247.       if (! layer_has_alpha (layer))
  1248.     return TRUE;
  1249.  
  1250.       /*  Otherwise, determine if the alpha value at
  1251.        *  the given point is non-zero
  1252.        */
  1253.       tile = tile_manager_get_tile (GIMP_DRAWABLE(layer)->tiles, 
  1254.                     x, y, TRUE, FALSE);
  1255.  
  1256.       val = * ((unsigned char*) tile_data_pointer (tile,
  1257.                            x % TILE_WIDTH,
  1258.                            y % TILE_HEIGHT) +
  1259.                 tile_bpp (tile) - 1);
  1260.       if (layer->mask)
  1261.     {
  1262.       unsigned char *ptr;
  1263.       mask_tile = tile_manager_get_tile (GIMP_DRAWABLE(layer->mask)->tiles,
  1264.                          x, y, TRUE, FALSE);
  1265.       ptr = tile_data_pointer (mask_tile, x % TILE_WIDTH, y % TILE_HEIGHT);
  1266.       val = val * (*ptr) / 255;
  1267.       tile_release (mask_tile, FALSE);
  1268.     }
  1269.  
  1270.       tile_release (tile, FALSE);
  1271.  
  1272.       if (val > 63)
  1273.     return TRUE;
  1274.     }
  1275.  
  1276.   return FALSE;
  1277. }
  1278.  
  1279. /**********************/
  1280. /*  access functions  */
  1281. /**********************/
  1282.  
  1283. void
  1284. layer_set_name (Layer *layer,
  1285.         gchar *name)
  1286. {
  1287.   gimp_drawable_set_name (GIMP_DRAWABLE (layer), name);
  1288. }
  1289.  
  1290. gchar *
  1291. layer_get_name (Layer *layer)
  1292. {
  1293.   return gimp_drawable_get_name (GIMP_DRAWABLE (layer));
  1294. }
  1295.  
  1296. guchar *
  1297. layer_data (Layer *layer)
  1298. {
  1299.   return NULL;
  1300. }
  1301.  
  1302. LayerMask *
  1303. layer_get_mask (Layer *layer)
  1304. {
  1305.   return layer->mask;
  1306. }
  1307.  
  1308. gboolean
  1309. layer_has_alpha (Layer *layer)
  1310. {
  1311.   g_return_val_if_fail (layer != NULL, FALSE);
  1312.   g_return_val_if_fail (GIMP_IS_LAYER (layer), FALSE);
  1313.  
  1314.   return GIMP_IMAGE_TYPE_HAS_ALPHA (GIMP_DRAWABLE (layer)->type);
  1315. }
  1316.  
  1317. gboolean
  1318. layer_is_floating_sel (Layer *layer)
  1319. {
  1320.   if (layer != NULL && layer->fs.drawable != NULL)
  1321.     return TRUE;
  1322.   else
  1323.     return FALSE;
  1324. }
  1325.  
  1326. gboolean
  1327. layer_linked (Layer *layer)
  1328. {
  1329.   return layer->linked;
  1330. }
  1331.  
  1332.  
  1333. TempBuf *
  1334. layer_preview (Layer *layer,
  1335.            gint   width,
  1336.            gint   height)
  1337. {
  1338.   /* Ok prime the cache with a large preview if the cache is invalid */
  1339.   if (! GIMP_DRAWABLE (layer)->preview_valid &&
  1340.       width  <= PREVIEW_CACHE_PRIME_WIDTH    &&
  1341.       height <= PREVIEW_CACHE_PRIME_HEIGHT   &&
  1342.       GIMP_DRAWABLE (layer)->gimage          &&
  1343.       GIMP_DRAWABLE (layer)->gimage->width  > PREVIEW_CACHE_PRIME_WIDTH   &&
  1344.       GIMP_DRAWABLE (layer)->gimage->height > PREVIEW_CACHE_PRIME_HEIGHT)
  1345.     {
  1346.       TempBuf * tb = layer_preview_private (layer,
  1347.                         PREVIEW_CACHE_PRIME_WIDTH,
  1348.                         PREVIEW_CACHE_PRIME_HEIGHT);
  1349.       
  1350.       /* Save the 2nd call */
  1351.       if (width  == PREVIEW_CACHE_PRIME_WIDTH &&
  1352.       height == PREVIEW_CACHE_PRIME_HEIGHT)
  1353.     return tb;
  1354.     }
  1355.  
  1356.   /* Second call - should NOT visit the tile cache...*/
  1357.   return layer_preview_private (layer, width, height);
  1358. }
  1359.  
  1360. static TempBuf *
  1361. layer_preview_private (Layer *layer,
  1362.                gint   width,
  1363.                gint   height)
  1364. {
  1365.   GImage            *gimage;
  1366.   TempBuf           *preview_buf;
  1367.   PixelRegion        srcPR, destPR;
  1368.   GimpImageBaseType  type;
  1369.   gint               bytes;
  1370.   gint               subsample;
  1371.   TempBuf           *ret_buf;
  1372.  
  1373.   g_return_val_if_fail (layer != NULL, NULL);
  1374.   g_return_val_if_fail (GIMP_IS_LAYER (layer), NULL);
  1375.  
  1376.   type  = RGB;
  1377.   bytes = 0;
  1378.  
  1379.   /*  The easy way  */
  1380.   if (GIMP_DRAWABLE (layer)->preview_valid &&
  1381.       (ret_buf = 
  1382.        gimp_preview_cache_get (&(GIMP_DRAWABLE (layer)->preview_cache), 
  1383.                    width, height)))
  1384.     return ret_buf;
  1385.   /*  The hard way  */
  1386.   else
  1387.     {
  1388.       gimage = GIMP_DRAWABLE (layer)->gimage;
  1389.       switch (GIMP_DRAWABLE (layer)->type)
  1390.     {
  1391.     case RGB_GIMAGE: case RGBA_GIMAGE:
  1392.       type  = RGB;
  1393.       bytes = GIMP_DRAWABLE (layer)->bytes;
  1394.       break;
  1395.     case GRAY_GIMAGE: case GRAYA_GIMAGE:
  1396.       type  = GRAY;
  1397.       bytes = GIMP_DRAWABLE (layer)->bytes;
  1398.       break;
  1399.     case INDEXED_GIMAGE: case INDEXEDA_GIMAGE:
  1400.       type  = INDEXED;
  1401.       bytes = (GIMP_DRAWABLE (layer)->type == INDEXED_GIMAGE) ? 3 : 4;
  1402.       break;
  1403.     }
  1404.  
  1405.       /*  calculate 'acceptable' subsample  */
  1406.       subsample = 1;
  1407.       /* handle some truncation errors */
  1408.       if (width < 1) width = 1;
  1409.       if (height < 1) height = 1;
  1410.  
  1411.       while ((width * (subsample + 1) * 2 < GIMP_DRAWABLE (layer)->width) &&
  1412.          (height * (subsample + 1) * 2 < GIMP_DRAWABLE (layer)->height)) 
  1413.     subsample = subsample + 1;
  1414.  
  1415.       pixel_region_init (&srcPR, GIMP_DRAWABLE (layer)->tiles, 
  1416.              0, 0,
  1417.              GIMP_DRAWABLE (layer)->width, 
  1418.              GIMP_DRAWABLE (layer)->height, 
  1419.              FALSE);
  1420.  
  1421.       preview_buf = temp_buf_new (width, height, bytes, 0, 0, NULL);
  1422.  
  1423.       destPR.bytes     = preview_buf->bytes;
  1424.       destPR.w         = width;
  1425.       destPR.h         = height;
  1426.       destPR.rowstride = width * destPR.bytes;
  1427.       destPR.data      = temp_buf_data (preview_buf);
  1428.  
  1429.       layer_preview_scale (type, gimage->cmap, &srcPR, &destPR, subsample);
  1430.  
  1431.       if (!GIMP_DRAWABLE (layer)->preview_valid)
  1432.     gimp_preview_cache_invalidate (&(GIMP_DRAWABLE(layer)->preview_cache));
  1433.  
  1434.       GIMP_DRAWABLE (layer)->preview_valid = TRUE;
  1435.  
  1436.       gimp_preview_cache_add (&(GIMP_DRAWABLE (layer)->preview_cache), 
  1437.                   preview_buf);
  1438.  
  1439.       return preview_buf;
  1440.     }
  1441. }
  1442.  
  1443. TempBuf *
  1444. layer_mask_preview (Layer *layer,
  1445.             gint   width,
  1446.             gint   height)
  1447. {
  1448.   /* Ok prime the cache with a large preview if the cache is invalid */
  1449.   if (! GIMP_DRAWABLE (layer)->preview_valid &&
  1450.       width  <= PREVIEW_CACHE_PRIME_WIDTH    &&
  1451.       height <= PREVIEW_CACHE_PRIME_HEIGHT   &&
  1452.       GIMP_DRAWABLE (layer)->gimage          &&
  1453.       GIMP_DRAWABLE (layer)->gimage->width  > PREVIEW_CACHE_PRIME_WIDTH   &&
  1454.       GIMP_DRAWABLE (layer)->gimage->height > PREVIEW_CACHE_PRIME_HEIGHT)
  1455.     {
  1456.       TempBuf * tb = layer_preview_private (layer,
  1457.                         PREVIEW_CACHE_PRIME_WIDTH,
  1458.                         PREVIEW_CACHE_PRIME_HEIGHT);
  1459.       
  1460.       /* Save the 2nd call */
  1461.       if (width  == PREVIEW_CACHE_PRIME_WIDTH &&
  1462.       height == PREVIEW_CACHE_PRIME_HEIGHT)
  1463.     return tb;
  1464.     }
  1465.  
  1466.   /* Second call - should NOT visit the tile cache...*/
  1467.   return layer_mask_preview_private (layer, width, height);
  1468. }
  1469.  
  1470. static TempBuf *
  1471. layer_mask_preview_private (Layer *layer,
  1472.                 gint   width,
  1473.                 gint   height)
  1474. {
  1475.   TempBuf     *preview_buf;
  1476.   LayerMask   *mask;
  1477.   PixelRegion  srcPR, destPR;
  1478.   gint         subsample;
  1479.   TempBuf     *ret_buf;
  1480.  
  1481.   g_return_val_if_fail (layer != NULL, NULL);
  1482.   g_return_val_if_fail (GIMP_IS_LAYER (layer), NULL);
  1483.  
  1484.   mask = layer->mask;
  1485.   if (!mask)
  1486.     return NULL;
  1487.  
  1488.   /*  The easy way  */
  1489.   if (GIMP_DRAWABLE(mask)->preview_valid &&
  1490.       (ret_buf = gimp_preview_cache_get (&(GIMP_DRAWABLE(mask)->preview_cache),
  1491.                      width, height)))
  1492.     return ret_buf;
  1493.   /*  The hard way  */
  1494.   else
  1495.     {
  1496.       /*  calculate 'acceptable' subsample  */
  1497.       subsample = 1;
  1498.       if (width < 1) width = 1;
  1499.       if (height < 1) height = 1;
  1500.       while ((width * (subsample + 1) * 2 < GIMP_DRAWABLE(layer)->width) &&
  1501.          (height * (subsample + 1) * 2 < GIMP_DRAWABLE(layer)->height))
  1502.     subsample = subsample + 1;
  1503.  
  1504.       pixel_region_init (&srcPR, GIMP_DRAWABLE(mask)->tiles, 
  1505.              0, 0, 
  1506.              GIMP_DRAWABLE(mask)->width, 
  1507.              GIMP_DRAWABLE(mask)->height, 
  1508.              FALSE);
  1509.  
  1510.       preview_buf = temp_buf_new (width, height, 1, 0, 0, NULL);
  1511.  
  1512.       destPR.bytes     = preview_buf->bytes;
  1513.       destPR.w         = width;
  1514.       destPR.h         = height;
  1515.       destPR.rowstride = width * destPR.bytes;
  1516.       destPR.data      = temp_buf_data (preview_buf);
  1517.  
  1518.       layer_preview_scale (GRAY, NULL, &srcPR, &destPR, subsample);
  1519.  
  1520.       if (!GIMP_DRAWABLE (mask)->preview_valid)
  1521.     gimp_preview_cache_invalidate (&(GIMP_DRAWABLE (mask)->preview_cache));
  1522.  
  1523.       GIMP_DRAWABLE (mask)->preview_valid = TRUE;
  1524.       gimp_preview_cache_add (&(GIMP_DRAWABLE (mask)->preview_cache),
  1525.                   preview_buf);
  1526.  
  1527.       return preview_buf;
  1528.     }
  1529. }
  1530.  
  1531. Tattoo
  1532. layer_get_tattoo (const Layer *layer)
  1533. {
  1534.   return (gimp_drawable_get_tattoo (GIMP_DRAWABLE (layer)));
  1535. }
  1536.  
  1537. void
  1538. layer_set_tattoo (const Layer *layer, 
  1539.           Tattoo       value)
  1540. {
  1541.   gimp_drawable_set_tattoo (GIMP_DRAWABLE (layer), value);
  1542. }
  1543.  
  1544.  
  1545. void
  1546. layer_invalidate_previews (GimpImage *gimage)
  1547. {
  1548.   GSList *tmp;
  1549.   Layer  *layer;
  1550.  
  1551.   g_return_if_fail (gimage != NULL);
  1552.  
  1553.   for (tmp = gimage->layers; tmp; tmp = g_slist_next (tmp))
  1554.     {
  1555.       layer = (Layer *) tmp->data;
  1556.       gimp_drawable_invalidate_preview (GIMP_DRAWABLE(layer), TRUE);
  1557.     }
  1558. }
  1559.  
  1560. static void
  1561. layer_preview_scale (GimpImageBaseType  type,
  1562.              guchar            *cmap,
  1563.              PixelRegion       *srcPR,
  1564.              PixelRegion       *destPR,
  1565.              gint               subsample)
  1566. {
  1567. #define EPSILON 0.000001
  1568.   guchar  *src, *s;
  1569.   guchar  *dest, *d;
  1570.   gdouble *row, *r;
  1571.   gint     destwidth;
  1572.   gint     src_row, src_col;
  1573.   gint     bytes, b;
  1574.   gint     width, height;
  1575.   gint     orig_width, orig_height;
  1576.   gdouble  x_rat, y_rat;
  1577.   gdouble  x_cum, y_cum;
  1578.   gdouble  x_last, y_last;
  1579.   gdouble *x_frac, y_frac, tot_frac;
  1580.   gint     i, j;
  1581.   gint     frac;
  1582.   gboolean advance_dest;
  1583.   guchar   rgb[MAX_CHANNELS];
  1584.  
  1585.   orig_width  = srcPR->w / subsample;
  1586.   orig_height = srcPR->h / subsample;
  1587.   width       = destPR->w;
  1588.   height      = destPR->h;
  1589.  
  1590.   /*  Some calculations...  */
  1591.   bytes     = destPR->bytes;
  1592.   destwidth = destPR->rowstride;
  1593.  
  1594.   /*  the data pointers...  */
  1595.   src  = g_new (guchar, orig_width * bytes);
  1596.   dest = destPR->data;
  1597.  
  1598.   /*  find the ratios of old x to new x and old y to new y  */
  1599.   x_rat = (gdouble) orig_width  / (gdouble) width;
  1600.   y_rat = (gdouble) orig_height / (gdouble) height;
  1601.  
  1602.   /*  allocate an array to help with the calculations  */
  1603.   row    = g_new (gdouble, width * bytes);
  1604.   x_frac = g_new (gdouble, width + orig_width);
  1605.  
  1606.   /*  initialize the pre-calculated pixel fraction array  */
  1607.   src_col = 0;
  1608.   x_cum = (gdouble) src_col;
  1609.   x_last = x_cum;
  1610.  
  1611.   for (i = 0; i < width + orig_width; i++)
  1612.     {
  1613.       if (x_cum + x_rat <= (src_col + 1 + EPSILON))
  1614.     {
  1615.       x_cum += x_rat;
  1616.       x_frac[i] = x_cum - x_last;
  1617.     }
  1618.       else
  1619.     {
  1620.       src_col ++;
  1621.       x_frac[i] = src_col - x_last;
  1622.     }
  1623.       x_last += x_frac[i];
  1624.     }
  1625.  
  1626.   /*  clear the "row" array  */
  1627.   memset (row, 0, sizeof (gdouble) * width * bytes);
  1628.  
  1629.   /*  counters...  */
  1630.   src_row = 0;
  1631.   y_cum = (double) src_row;
  1632.   y_last = y_cum;
  1633.  
  1634.   pixel_region_get_row (srcPR, 
  1635.             0, 
  1636.             src_row * subsample, 
  1637.             orig_width * subsample, 
  1638.             src, 
  1639.             subsample);
  1640.  
  1641.   /*  Scale the selected region  */
  1642.   for (i = 0; i < height; )
  1643.     {
  1644.       src_col = 0;
  1645.       x_cum = (gdouble) src_col;
  1646.  
  1647.       /* determine the fraction of the src pixel we are using for y */
  1648.       if (y_cum + y_rat <= (src_row + 1 + EPSILON))
  1649.     {
  1650.       y_cum += y_rat;
  1651.       y_frac = y_cum - y_last;
  1652.       advance_dest = TRUE;
  1653.     }
  1654.       else
  1655.     {
  1656.       src_row ++;
  1657.       y_frac = src_row - y_last;
  1658.       advance_dest = FALSE;
  1659.     }
  1660.  
  1661.       y_last += y_frac;
  1662.  
  1663.       s = src;
  1664.       r = row;
  1665.  
  1666.       frac = 0;
  1667.       j = width;
  1668.  
  1669.       while (j)
  1670.     {
  1671.       tot_frac = x_frac[frac++] * y_frac;
  1672.  
  1673.       /*  If indexed, transform the color to RGB  */
  1674.       if (type == INDEXED)
  1675.         {
  1676.           map_to_color (2, cmap, s, rgb);
  1677.  
  1678.           r[RED_PIX] += rgb[RED_PIX] * tot_frac;
  1679.           r[GREEN_PIX] += rgb[GREEN_PIX] * tot_frac;
  1680.           r[BLUE_PIX] += rgb[BLUE_PIX] * tot_frac;
  1681.           if (bytes == 4)
  1682.         r[ALPHA_PIX] += s[ALPHA_I_PIX] * tot_frac;
  1683.         }
  1684.       else
  1685.         for (b = 0; b < bytes; b++)
  1686.           r[b] += s[b] * tot_frac;
  1687.  
  1688.       /*  increment the destination  */
  1689.       if (x_cum + x_rat <= (src_col + 1 + EPSILON))
  1690.         {
  1691.           r += bytes;
  1692.           x_cum += x_rat;
  1693.           j--;
  1694.         }
  1695.  
  1696.       /* increment the source */
  1697.       else
  1698.         {
  1699.           s += srcPR->bytes;
  1700.           src_col++;
  1701.         }
  1702.     }
  1703.  
  1704.       if (advance_dest)
  1705.     {
  1706.       tot_frac = 1.0 / (x_rat * y_rat);
  1707.  
  1708.       /*  copy "row" to "dest"  */
  1709.       d = dest;
  1710.       r = row;
  1711.  
  1712.       j = width;
  1713.       while (j--)
  1714.         {
  1715.           b = bytes;
  1716.           while (b--)
  1717.         *d++ = (guchar) ((*r++ * tot_frac)+0.5);
  1718.         }
  1719.  
  1720.       dest += destwidth;
  1721.  
  1722.       /*  clear the "row" array  */
  1723.       memset (row, 0, sizeof (gdouble) * destwidth);
  1724.  
  1725.       i++;
  1726.     }
  1727.       else
  1728.     pixel_region_get_row (srcPR, 
  1729.                   0, 
  1730.                   src_row * subsample, 
  1731.                   orig_width * subsample, 
  1732.                   src, 
  1733.                   subsample);
  1734.     }
  1735.  
  1736.   /*  free up temporary arrays  */
  1737.   g_free (row);
  1738.   g_free (x_frac);
  1739.   g_free (src);
  1740. }
  1741.  
  1742. static void
  1743. gimp_layer_mask_destroy (GtkObject *object)
  1744. {
  1745.   GimpLayerMask *layermask;
  1746.   
  1747.   g_return_if_fail (object != NULL);
  1748.   g_return_if_fail (GIMP_IS_LAYER_MASK (object));
  1749.  
  1750.   layermask = GIMP_LAYER_MASK (object);
  1751.  
  1752.   if (GTK_OBJECT_CLASS (layer_mask_parent_class)->destroy)
  1753.     (*GTK_OBJECT_CLASS (layer_mask_parent_class)->destroy) (object);
  1754. }
  1755.  
  1756. LayerMask *
  1757. layer_mask_new (GimpImage *gimage,
  1758.         gint       width,
  1759.         gint       height,
  1760.         gchar     *name,
  1761.         gint       opacity,
  1762.         guchar    *col)
  1763. {
  1764.   LayerMask *layer_mask;
  1765.   gint       i;
  1766.  
  1767.   layer_mask = gtk_type_new (gimp_layer_mask_get_type ());
  1768.  
  1769.   gimp_drawable_configure (GIMP_DRAWABLE (layer_mask), 
  1770.                gimage, width, height, GRAY_GIMAGE, name);
  1771.  
  1772.   /*  set the layer_mask color and opacity  */
  1773.   for (i = 0; i < 3; i++)
  1774.     GIMP_CHANNEL (layer_mask)->col[i]       = col[i];
  1775.  
  1776.   GIMP_CHANNEL (layer_mask)->opacity        = opacity;
  1777.   GIMP_CHANNEL (layer_mask)->show_masked    = TRUE;
  1778.  
  1779.   /*  selection mask variables  */
  1780.   GIMP_CHANNEL (layer_mask)->empty          = TRUE;
  1781.   GIMP_CHANNEL (layer_mask)->segs_in        = NULL;
  1782.   GIMP_CHANNEL (layer_mask)->segs_out       = NULL;
  1783.   GIMP_CHANNEL (layer_mask)->num_segs_in    = 0;
  1784.   GIMP_CHANNEL (layer_mask)->num_segs_out   = 0;
  1785.   GIMP_CHANNEL (layer_mask)->bounds_known   = TRUE;
  1786.   GIMP_CHANNEL (layer_mask)->boundary_known = TRUE;
  1787.   GIMP_CHANNEL (layer_mask)->x1             = GIMP_CHANNEL (layer_mask)->y1 = 0;
  1788.   GIMP_CHANNEL (layer_mask)->x2             = width;
  1789.   GIMP_CHANNEL (layer_mask)->y2             = height;
  1790.  
  1791.   return layer_mask;
  1792. }
  1793.  
  1794. LayerMask *
  1795. layer_mask_copy (LayerMask *layer_mask)
  1796. {
  1797.   gchar       *layer_mask_name;
  1798.   LayerMask   *new_layer_mask;
  1799.   PixelRegion  srcPR, destPR;
  1800.  
  1801.   /*  formulate the new layer_mask name  */
  1802.   layer_mask_name = g_strdup_printf (_("%s copy"), 
  1803.                      GIMP_DRAWABLE(layer_mask)->name);
  1804.  
  1805.   /*  allocate a new layer_mask object  */
  1806.   new_layer_mask = layer_mask_new (GIMP_DRAWABLE(layer_mask)->gimage, 
  1807.                    GIMP_DRAWABLE(layer_mask)->width, 
  1808.                    GIMP_DRAWABLE(layer_mask)->height, 
  1809.                    layer_mask_name, 
  1810.                    GIMP_CHANNEL(layer_mask)->opacity, 
  1811.                    GIMP_CHANNEL(layer_mask)->col);
  1812.   GIMP_DRAWABLE(new_layer_mask)->visible = 
  1813.     GIMP_DRAWABLE(layer_mask)->visible;
  1814.   GIMP_DRAWABLE(new_layer_mask)->offset_x = 
  1815.     GIMP_DRAWABLE(layer_mask)->offset_x;
  1816.   GIMP_DRAWABLE(new_layer_mask)->offset_y = 
  1817.     GIMP_DRAWABLE(layer_mask)->offset_y;
  1818.   GIMP_CHANNEL(new_layer_mask)->show_masked = 
  1819.     GIMP_CHANNEL(layer_mask)->show_masked;
  1820.  
  1821.   /*  copy the contents across layer masks  */
  1822.   pixel_region_init (&srcPR, GIMP_DRAWABLE(layer_mask)->tiles, 
  1823.              0, 0, 
  1824.              GIMP_DRAWABLE(layer_mask)->width, 
  1825.              GIMP_DRAWABLE(layer_mask)->height, 
  1826.              FALSE);
  1827.   pixel_region_init (&destPR, GIMP_DRAWABLE(new_layer_mask)->tiles, 
  1828.              0, 0, 
  1829.              GIMP_DRAWABLE(layer_mask)->width, 
  1830.              GIMP_DRAWABLE(layer_mask)->height, 
  1831.              TRUE);
  1832.   copy_region (&srcPR, &destPR);
  1833.  
  1834.   /*  free up the layer_mask_name memory  */
  1835.   g_free (layer_mask_name);
  1836.  
  1837.   return new_layer_mask;
  1838. }
  1839.  
  1840. LayerMask *
  1841. layer_mask_get_ID (gint ID)
  1842. {
  1843.   GimpDrawable *drawable;
  1844.  
  1845.   drawable = drawable_get_ID (ID);
  1846.  
  1847.   if (drawable && GIMP_IS_LAYER_MASK (drawable)) 
  1848.     return GIMP_LAYER_MASK (drawable);
  1849.   else
  1850.     return NULL;
  1851. }
  1852.  
  1853. void
  1854. layer_mask_delete (LayerMask *layermask)
  1855. {
  1856.   gtk_object_unref (GTK_OBJECT (layermask));
  1857. }
  1858.  
  1859. LayerMask *
  1860. layer_mask_ref (LayerMask *mask)
  1861. {
  1862.   gtk_object_ref  (GTK_OBJECT (mask));
  1863.   gtk_object_sink (GTK_OBJECT (mask));
  1864.  
  1865.   return mask;
  1866. }
  1867.  
  1868. void
  1869. layer_mask_unref (LayerMask *mask)
  1870. {
  1871.   gtk_object_unref (GTK_OBJECT (mask));
  1872. }
  1873.  
  1874. void
  1875. layer_mask_set_layer (LayerMask *mask,
  1876.               Layer     *layer)
  1877. {
  1878.   mask->layer = layer;
  1879. }
  1880.  
  1881. Layer *
  1882. layer_mask_get_layer (LayerMask *mask)
  1883. {
  1884.   return mask->layer;
  1885. }
  1886.  
  1887. void
  1888. channel_layer_mask (Channel *mask,
  1889.             Layer   *layer)
  1890. {
  1891.   PixelRegion srcPR, destPR;
  1892.   guchar      empty = 0;
  1893.   gint        x1, y1, x2, y2;
  1894.  
  1895.   /*  push the current mask onto the undo stack  */
  1896.   channel_push_undo (mask);
  1897.  
  1898.   /*  clear the mask  */
  1899.   pixel_region_init (&destPR, GIMP_DRAWABLE(mask)->tiles, 
  1900.              0, 0, 
  1901.              GIMP_DRAWABLE(mask)->width, GIMP_DRAWABLE(mask)->height, 
  1902.              TRUE);
  1903.   color_region (&destPR, &empty);
  1904.  
  1905.   x1 = CLAMP (GIMP_DRAWABLE(layer)->offset_x, 0, GIMP_DRAWABLE(mask)->width);
  1906.   y1 = CLAMP (GIMP_DRAWABLE(layer)->offset_y, 0, GIMP_DRAWABLE(mask)->height);
  1907.   x2 = CLAMP (GIMP_DRAWABLE(layer)->offset_x + GIMP_DRAWABLE(layer)->width, 
  1908.           0, GIMP_DRAWABLE(mask)->width);
  1909.   y2 = CLAMP (GIMP_DRAWABLE(layer)->offset_y + GIMP_DRAWABLE(layer)->height, 
  1910.           0, GIMP_DRAWABLE(mask)->height);
  1911.  
  1912.   pixel_region_init (&srcPR, GIMP_DRAWABLE(layer->mask)->tiles,
  1913.              (x1 - GIMP_DRAWABLE(layer)->offset_x), 
  1914.              (y1 - GIMP_DRAWABLE(layer)->offset_y),
  1915.              (x2 - x1), (y2 - y1), 
  1916.              FALSE);
  1917.   pixel_region_init (&destPR, GIMP_DRAWABLE(mask)->tiles, 
  1918.              x1, y1, 
  1919.              (x2 - x1), (y2 - y1), 
  1920.              TRUE);
  1921.   copy_region (&srcPR, &destPR);
  1922.  
  1923.   mask->bounds_known = FALSE;
  1924. }
  1925.