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

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 2 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include <string.h>
  22.  
  23. #include <gtk/gtk.h>
  24.  
  25. #include "apptypes.h"
  26.  
  27. #include "cursorutil.h"
  28. #include "drawable.h"
  29. #include "floating_sel.h"
  30. #include "gdisplay.h"
  31. #include "gimage_mask.h"
  32. #include "gimpcontext.h"
  33. #include "paint_funcs.h"
  34. #include "parasitelist.h"
  35. #include "undo.h"
  36. #include "gimpsignal.h"
  37. #include "gimpparasite.h"
  38. #include "path.h"
  39. #include "gimprc.h"
  40.  
  41. #include "tile_manager.h"
  42. #include "tile.h"
  43. #include "layer_pvt.h"
  44. #include "drawable_pvt.h"        /* ick ick. */
  45.  
  46. #include "libgimp/gimpcolorspace.h"
  47. #include "libgimp/gimplimits.h"
  48. #include "libgimp/gimpmath.h"
  49. #include "libgimp/gimpparasite.h"
  50.  
  51. #include "libgimp/gimpintl.h"
  52.  
  53.  
  54. #ifdef DEBUG
  55. #define TRC(x) printf x
  56. #else
  57. #define TRC(x)
  58. #endif
  59.  
  60.  
  61. /*  Local function declarations  */
  62. static void     gimp_image_destroy               (GtkObject *object);
  63. static void     gimp_image_free_projection       (GimpImage *);
  64. static void     gimp_image_allocate_shadow       (GimpImage *, gint, gint, gint);
  65. static void     gimp_image_allocate_projection   (GimpImage *);
  66. static void     gimp_image_free_layers           (GimpImage *);
  67. static void     gimp_image_free_channels         (GimpImage *);
  68. static void     gimp_image_construct_layers      (GimpImage *, gint, gint, gint, gint);
  69. static void     gimp_image_construct_channels    (GimpImage *, gint, gint, gint, gint);
  70. static void     gimp_image_initialize_projection (GimpImage *, gint, gint, gint, gint);
  71. static void     gimp_image_get_active_channels   (GimpImage *, GimpDrawable *, gint *);
  72.  
  73. /*  projection functions  */
  74. static void     project_intensity            (GimpImage *, Layer *,
  75.                           PixelRegion *, PixelRegion *,
  76.                           PixelRegion *);
  77. static void     project_intensity_alpha      (GimpImage *, Layer *,
  78.                           PixelRegion *, PixelRegion *,
  79.                           PixelRegion *);
  80. static void     project_indexed              (GimpImage *, Layer *,
  81.                           PixelRegion *, PixelRegion *);
  82. static void     project_channel              (GimpImage *, Channel *,
  83.                           PixelRegion *, PixelRegion *);
  84.  
  85. /*
  86.  *  Global variables
  87.  */
  88. gint valid_combinations[][MAX_CHANNELS + 1] =
  89. {
  90.   /* RGB GIMAGE */
  91.   { -1, -1, -1, COMBINE_INTEN_INTEN, COMBINE_INTEN_INTEN_A },
  92.   /* RGBA GIMAGE */
  93.   { -1, -1, -1, COMBINE_INTEN_A_INTEN, COMBINE_INTEN_A_INTEN_A },
  94.   /* GRAY GIMAGE */
  95.   { -1, COMBINE_INTEN_INTEN, COMBINE_INTEN_INTEN_A, -1, -1 },
  96.   /* GRAYA GIMAGE */
  97.   { -1, COMBINE_INTEN_A_INTEN, COMBINE_INTEN_A_INTEN_A, -1, -1 },
  98.   /* INDEXED GIMAGE */
  99.   { -1, COMBINE_INDEXED_INDEXED, COMBINE_INDEXED_INDEXED_A, -1, -1 },
  100.   /* INDEXEDA GIMAGE */
  101.   { -1, -1, COMBINE_INDEXED_A_INDEXED_A, -1, -1 },
  102. };
  103.  
  104. guint32 next_guide_id = 1;  /* For generating guide_ID handles for PDB stuff */
  105.  
  106.  
  107. /*
  108.  *  Static variables
  109.  */
  110.  
  111. enum {
  112.   CLEAN,
  113.   DIRTY,
  114.   REPAINT,
  115.   RENAME,
  116.   RESIZE,
  117.   RESTRUCTURE,
  118.   COLORMAP_CHANGED,
  119.   UNDO_EVENT,
  120.   LAST_SIGNAL
  121. };
  122.  
  123. static guint            gimp_image_signals[LAST_SIGNAL];
  124. static GimpObjectClass *parent_class;
  125.  
  126.  
  127. static void
  128. gimp_image_class_init (GimpImageClass *klass)
  129. {
  130.   GtkObjectClass *object_class;
  131.   GtkType         type;
  132.   
  133.   object_class = GTK_OBJECT_CLASS(klass);
  134.   parent_class = gtk_type_class (gimp_object_get_type ());
  135.   
  136.   type=object_class->type;
  137.  
  138.   object_class->destroy =  gimp_image_destroy;
  139.  
  140.   gimp_image_signals[CLEAN] =
  141.       gimp_signal_new ("clean", GTK_RUN_FIRST, type, 0,
  142.                gimp_sigtype_void);
  143.   gimp_image_signals[DIRTY] =
  144.       gimp_signal_new ("dirty", GTK_RUN_FIRST, type, 0,
  145.                gimp_sigtype_void);
  146.   gimp_image_signals[REPAINT] =
  147.       gimp_signal_new ("repaint", GTK_RUN_FIRST, type, 0,
  148.                gimp_sigtype_int_int_int_int);
  149.   gimp_image_signals[RENAME] =
  150.       gimp_signal_new ("rename", GTK_RUN_FIRST, type, 0,
  151.                gimp_sigtype_void);
  152.   gimp_image_signals[RESIZE] =
  153.       gimp_signal_new ("resize", GTK_RUN_FIRST, type, 0,
  154.                gimp_sigtype_void);
  155.   gimp_image_signals[RESTRUCTURE] =
  156.       gimp_signal_new ("restructure", GTK_RUN_FIRST, type, 0,
  157.                gimp_sigtype_void);
  158.   gimp_image_signals[COLORMAP_CHANGED] =
  159.       gimp_signal_new ("colormap_changed", GTK_RUN_FIRST, type, 0,
  160.                gimp_sigtype_int);
  161.   gimp_image_signals[UNDO_EVENT] = 
  162.       gimp_signal_new ("undo_event", GTK_RUN_FIRST, type, 0,
  163.                gimp_sigtype_int);
  164.   
  165.   gtk_object_class_add_signals (object_class, gimp_image_signals, LAST_SIGNAL);
  166. }
  167.  
  168.  
  169. /* static functions */
  170.  
  171. static void 
  172. gimp_image_init (GimpImage *gimage)
  173. {
  174.   gimage->has_filename          = FALSE;
  175.   gimage->num_cols              = 0;
  176.   gimage->cmap                  = NULL;
  177.   gimage->disp_count            = 0;
  178.   gimage->instance_count        = 0;
  179.   gimage->shadow                = NULL;
  180.   gimage->dirty                 = 1;
  181.   gimage->undo_on               = TRUE;
  182.   gimage->construct_flag        = -1;
  183.   gimage->tattoo_state          = 0;
  184.   gimage->projection            = NULL;
  185.   gimage->guides                = NULL;
  186.   gimage->layers                = NULL;
  187.   gimage->channels              = NULL;
  188.   gimage->layer_stack           = NULL;
  189.   gimage->undo_stack            = NULL;
  190.   gimage->redo_stack            = NULL;
  191.   gimage->undo_bytes            = 0;
  192.   gimage->undo_levels           = 0;
  193.   gimage->group_count           = 0;
  194.   gimage->pushing_undo_group    = UNDO_NULL;
  195.   gimage->comp_preview_valid[0] = FALSE;
  196.   gimage->comp_preview_valid[1] = FALSE;
  197.   gimage->comp_preview_valid[2] = FALSE;
  198.   gimage->comp_preview          = NULL;
  199.   gimage->parasites             = parasite_list_new ();
  200.   gimage->xresolution           = default_xresolution;
  201.   gimage->yresolution           = default_yresolution;
  202.   gimage->unit                  = default_units;
  203.   gimage->save_proc             = NULL;
  204.   gimage->paths                 = NULL;
  205. }
  206.  
  207. GtkType 
  208. gimp_image_get_type (void) 
  209. {
  210.   static GtkType type;
  211.  
  212.   GIMP_TYPE_INIT (type,
  213.           GimpImage,
  214.           GimpImageClass,
  215.           gimp_image_init,
  216.           gimp_image_class_init,
  217.           GIMP_TYPE_OBJECT);
  218.   return type;
  219. }
  220.  
  221. static void
  222. gimp_image_allocate_projection (GimpImage *gimage)
  223. {
  224.   if (gimage->projection)
  225.     gimp_image_free_projection (gimage);
  226.  
  227.   /*  Find the number of bytes required for the projection.
  228.    *  This includes the intensity channels and an alpha channel
  229.    *  if one doesn't exist.
  230.    */
  231.   switch (gimp_image_base_type (gimage))
  232.     {
  233.     case RGB:
  234.     case INDEXED:
  235.       gimage->proj_bytes = 4;
  236.       gimage->proj_type = RGBA_GIMAGE;
  237.       break;
  238.     case GRAY:
  239.       gimage->proj_bytes = 2;
  240.       gimage->proj_type = GRAYA_GIMAGE;
  241.       break;
  242.     default:
  243.       g_assert_not_reached ();
  244.     }
  245.  
  246.   /*  allocate the new projection  */
  247.   gimage->projection = tile_manager_new (gimage->width, gimage->height, gimage->proj_bytes);
  248.   tile_manager_set_user_data (gimage->projection, (void *) gimage);
  249.   tile_manager_set_validate_proc (gimage->projection, gimp_image_validate);
  250. }
  251.  
  252. static void
  253. gimp_image_free_projection (GimpImage *gimage)
  254. {
  255.   if (gimage->projection)
  256.     tile_manager_destroy (gimage->projection);
  257.  
  258.   gimage->projection = NULL;
  259. }
  260.  
  261. static void
  262. gimp_image_allocate_shadow (GimpImage *gimage, 
  263.                 gint       width, 
  264.                 gint       height, 
  265.                 gint       bpp)
  266. {
  267.   /*  allocate the new projection  */
  268.   gimage->shadow = tile_manager_new (width, height, bpp);
  269. }
  270.  
  271.  
  272. /* function definitions */
  273.  
  274. GimpImage *
  275. gimp_image_new (gint               width,
  276.         gint               height,
  277.         GimpImageBaseType  base_type)
  278. {
  279.   GimpImage *gimage = GIMP_IMAGE (gtk_type_new (gimp_image_get_type ()));
  280.   gint i;
  281.  
  282.   gimage->filename  = NULL;
  283.   gimage->width     = width;
  284.   gimage->height    = height;
  285.   gimage->base_type = base_type;
  286.  
  287.   switch (base_type)
  288.     {
  289.     case RGB:
  290.     case GRAY:
  291.       break;
  292.     case INDEXED:
  293.       /* always allocate 256 colors for the colormap */
  294.       gimage->num_cols = 0;
  295.       gimage->cmap     = (guchar *) g_malloc0 (COLORMAP_SIZE);
  296.       break;
  297.     default:
  298.       break;
  299.     }
  300.  
  301.   /*  configure the active pointers  */
  302.   gimage->active_layer   = NULL;
  303.   gimage->active_channel = NULL;  /* no default active channel */
  304.   gimage->floating_sel   = NULL;
  305.  
  306.   /*  set all color channels visible and active  */
  307.   for (i = 0; i < MAX_CHANNELS; i++)
  308.     {
  309.       gimage->visible[i] = TRUE;
  310.       gimage->active[i]  = TRUE;
  311.     }
  312.  
  313.   /* create the selection mask */
  314.   gimage->selection_mask = channel_new_mask (gimage,
  315.                          gimage->width, gimage->height);
  316.  
  317.   /* set the qmask properties */
  318.   gimage->qmask_state    = FALSE;
  319.   gimage->qmask_opacity  = 50;
  320.   gimage->qmask_color[0] = 255; 
  321.   gimage->qmask_color[1] = 0; 
  322.   gimage->qmask_color[2] = 0; 
  323.  
  324.   return gimage;
  325. }
  326.  
  327. void
  328. gimp_image_set_filename (GimpImage *gimage, 
  329.              gchar     *filename)
  330. {
  331.   gchar    *new_filename;
  332.   gchar    *old_filename;
  333.   gboolean  free_old;
  334.  
  335.   /* 
  336.    * WARNING: this function will free the current filename even if you are 
  337.    * setting it to itself so any pointer you hold to the filename will be
  338.    * invalid after this call.  So please use with care.
  339.    */
  340.  
  341.   new_filename = g_strdup (filename);
  342.   old_filename = gimage->filename;
  343.   free_old = gimage->has_filename;
  344.  
  345.   if (filename && filename[0])
  346.     {
  347.       gimage->filename = new_filename;
  348.       gimage->has_filename = TRUE;
  349.     }
  350.   else
  351.     {
  352.       gimage->filename = NULL;
  353.       gimage->has_filename = FALSE;
  354.     }
  355.  
  356.   if (free_old)
  357.     g_free (old_filename);
  358.  
  359.   gtk_signal_emit (GTK_OBJECT (gimage), gimp_image_signals[RENAME]);
  360. }
  361.  
  362. void
  363. gimp_image_set_resolution (GimpImage *gimage,
  364.                gdouble    xresolution,
  365.                gdouble    yresolution)
  366. {
  367.   /* nothing to do if setting res to the same as before */
  368.   if ((ABS (gimage->xresolution - xresolution) < 1e-5) &&
  369.       (ABS (gimage->yresolution - yresolution) < 1e-5))
  370.       return;
  371.  
  372.   /* don't allow to set the resolution out of bounds */
  373.   if (xresolution < GIMP_MIN_RESOLUTION || xresolution > GIMP_MAX_RESOLUTION ||
  374.       yresolution < GIMP_MIN_RESOLUTION || yresolution > GIMP_MAX_RESOLUTION)
  375.     return;
  376.  
  377.   undo_push_resolution (gimage);
  378.  
  379.   gimage->xresolution = xresolution;
  380.   gimage->yresolution = yresolution;
  381.  
  382.   /* really just want to recalc size and repaint */
  383.   gdisplays_shrink_wrap (gimage);
  384. }
  385.  
  386. void
  387. gimp_image_get_resolution (GimpImage *gimage,
  388.                gdouble   *xresolution,
  389.                gdouble   *yresolution)
  390. {
  391.   g_return_if_fail (xresolution && yresolution);
  392.  
  393.   *xresolution = gimage->xresolution;
  394.   *yresolution = gimage->yresolution;
  395. }
  396.  
  397. void
  398. gimp_image_set_unit (GimpImage *gimage,
  399.              GimpUnit   unit)
  400. {
  401.   undo_push_resolution (gimage);
  402.  
  403.   gimage->unit = unit;
  404. }
  405.  
  406. GimpUnit
  407. gimp_image_get_unit (GimpImage *gimage)
  408. {
  409.   return gimage->unit;
  410. }
  411.  
  412. void
  413. gimp_image_set_save_proc (GimpImage     *gimage, 
  414.               PlugInProcDef *proc)
  415. {
  416.   gimage->save_proc = proc;
  417. }
  418.  
  419. PlugInProcDef *
  420. gimp_image_get_save_proc (GimpImage *gimage)
  421. {
  422.   return gimage->save_proc;
  423. }
  424.  
  425. void
  426. gimp_image_resize (GimpImage *gimage, 
  427.            gint       new_width, 
  428.            gint       new_height,
  429.            gint       offset_x, 
  430.            gint       offset_y)
  431. {
  432.   Channel *channel;
  433.   Layer   *layer;
  434.   Layer   *floating_layer;
  435.   GSList  *list;
  436.   GList   *guide_list;
  437.  
  438.   gimp_add_busy_cursors();
  439.  
  440.   g_assert (new_width > 0 && new_height > 0);
  441.  
  442.   /*  Get the floating layer if one exists  */
  443.   floating_layer = gimp_image_floating_sel (gimage);
  444.  
  445.   undo_push_group_start (gimage, IMAGE_RESIZE_UNDO);
  446.  
  447.   /*  Relax the floating selection  */
  448.   if (floating_layer)
  449.     floating_sel_relax (floating_layer, TRUE);
  450.  
  451.   /*  Push the image size to the stack  */
  452.   undo_push_gimage_mod (gimage);
  453.  
  454.   /*  Set the new width and height  */
  455.   gimage->width  = new_width;
  456.   gimage->height = new_height;
  457.  
  458.   /*  Resize all channels  */
  459.   list = gimage->channels;
  460.   while (list)
  461.     {
  462.       channel = (Channel *) list->data;
  463.       channel_resize (channel, new_width, new_height, offset_x, offset_y);
  464.       list = g_slist_next (list);
  465.  
  466.     }
  467.  
  468.   /*  Reposition or remove any guides  */
  469.   guide_list = gimage->guides;
  470.   while (guide_list)
  471.     {
  472.       Guide *guide;
  473.  
  474.       guide = (Guide*) guide_list->data;
  475.       guide_list = g_list_next (guide_list);
  476.  
  477.       switch (guide->orientation)
  478.     {
  479.     case ORIENTATION_HORIZONTAL:
  480.       undo_push_guide (gimage, guide);
  481.       guide->position += offset_y;
  482.       if (guide->position < 0 || guide->position > new_height)
  483.         gimp_image_delete_guide (gimage, guide);
  484.       break;
  485.     case ORIENTATION_VERTICAL:
  486.       undo_push_guide (gimage, guide);
  487.       guide->position += offset_x;
  488.       if (guide->position < 0 || guide->position > new_width)
  489.         gimp_image_delete_guide (gimage, guide);
  490.       break;
  491.     default:
  492.       g_error("Unknown guide orientation I.\n");
  493.     }
  494.     }
  495.  
  496.   /*  Don't forget the selection mask!  */
  497.   channel_resize (gimage->selection_mask,
  498.           new_width, new_height, offset_x, offset_y);
  499.   gimage_mask_invalidate (gimage);
  500.  
  501.   /*  Reposition all layers  */
  502.   list = gimage->layers;
  503.   while (list)
  504.     {
  505.       layer = (Layer *) list->data;
  506.       layer_translate (layer, offset_x, offset_y);
  507.       list = g_slist_next (list);
  508.     }
  509.  
  510.   /*  Make sure the projection matches the gimage size  */
  511.   gimp_image_projection_realloc (gimage);
  512.  
  513.   /*  Rigor the floating selection  */
  514.   if (floating_layer)
  515.     floating_sel_rigor (floating_layer, TRUE);
  516.  
  517.   gtk_signal_emit (GTK_OBJECT (gimage), gimp_image_signals[RESIZE]);
  518.  
  519.   gimp_remove_busy_cursors (NULL);
  520. }
  521.  
  522. gint
  523. gimp_image_get_width (const GimpImage *gimage)
  524. {
  525.   return gimage->width;
  526. }
  527.  
  528. gint
  529. gimp_image_get_height (const GimpImage *gimage)
  530. {
  531.   return gimage->height;
  532. }
  533.  
  534. void
  535. gimp_image_scale (GimpImage *gimage, 
  536.           gint       new_width, 
  537.           gint       new_height)
  538. {
  539.   Channel *channel;
  540.   Layer   *layer;
  541.   Layer   *floating_layer;
  542.   GSList  *list;
  543.   GSList  *remove = NULL;
  544.   GList   *glist;
  545.   Guide   *guide;
  546.   gint     old_width;
  547.   gint     old_height;
  548.   gdouble  img_scale_w = 1.0;
  549.   gdouble  img_scale_h = 1.0;
  550.  
  551.   if ((new_width == 0) || (new_height == 0))
  552.     {
  553.       g_message (("gimp_image_scale: Scaling to zero width or height has been rejected."));
  554.       return;
  555.     }
  556.  
  557.   gimp_add_busy_cursors ();
  558.  
  559.   /*  Get the floating layer if one exists  */
  560.   floating_layer = gimp_image_floating_sel (gimage);
  561.  
  562.   undo_push_group_start (gimage, IMAGE_SCALE_UNDO);
  563.  
  564.   /*  Relax the floating selection  */
  565.   if (floating_layer)
  566.     floating_sel_relax (floating_layer, TRUE);
  567.  
  568.   /*  Push the image size to the stack  */
  569.   undo_push_gimage_mod (gimage);
  570.  
  571.   /*  Set the new width and height  */
  572.  
  573.   old_width      = gimage->width;
  574.   old_height     = gimage->height;
  575.   gimage->width  = new_width;
  576.   gimage->height = new_height;
  577.   img_scale_w    = (gdouble)new_width / (gdouble)old_width;
  578.   img_scale_h    = (gdouble)new_height / (gdouble)old_height;
  579.  
  580.   /*  Scale all channels  */
  581.   for (list = gimage->channels; list; list = g_slist_next (list))
  582.     {
  583.       channel = (Channel *) list->data;
  584.       channel_scale (channel, new_width, new_height);
  585.     }
  586.  
  587.   /*  Don't forget the selection mask!  */
  588.   /*  if (channel_is_empty(gimage->selection_mask))
  589.         channel_resize(gimage->selection_mask, new_width, new_height, 0, 0)
  590.       else
  591.   */
  592.         
  593.   channel_scale (gimage->selection_mask, new_width, new_height);
  594.   gimage_mask_invalidate (gimage);
  595.  
  596.   /*  Scale all layers  */
  597.   list = gimage->layers;
  598.   for (list = gimage->layers; list; list = g_slist_next (list))
  599.     {
  600.       layer = (Layer *) list->data;
  601.       if (layer_scale_by_factors (layer, img_scale_w, img_scale_h) == FALSE)
  602.     {
  603.       /* Since 0 < img_scale_w, img_scale_h, failure due to one or more     */
  604.       /* vanishing scaled layer dimensions. Implicit delete implemented     */
  605.       /* here. Upstream warning implemented in resize_check_layer_scaling() */
  606.           /* [resize.c line 1295], which offers the user the chance to bail out.*/
  607.  
  608.           remove = g_slist_append (remove, layer);
  609.         }
  610.     }
  611.   /* We defer removing layers lost to scaling until now            */
  612.   /* so as not to mix the operations of iterating over and removal */
  613.   /* from gimage->layers.                                          */  
  614.  
  615.   for (list = remove; list; list = g_slist_next (list))
  616.     {
  617.       layer = list->data;
  618.       gimage_remove_layer (gimage, layer);
  619.     }
  620.   g_slist_free (remove);
  621.  
  622.   /*  Scale any Guides  */
  623.   for (glist = gimage->guides; glist; glist = g_list_next (glist))
  624.     {
  625.       guide = (Guide*) glist->data;
  626.  
  627.       switch (guide->orientation)
  628.     {
  629.     case ORIENTATION_HORIZONTAL:
  630.       undo_push_guide (gimage, guide);
  631.       guide->position = (guide->position * new_height) / old_height;
  632.       break;
  633.     case ORIENTATION_VERTICAL:
  634.       undo_push_guide (gimage, guide);
  635.       guide->position = (guide->position * new_width) / old_width;
  636.       break;
  637.     default:
  638.       g_error("Unknown guide orientation II.\n");
  639.     }
  640.     }
  641.  
  642.   /*  Make sure the projection matches the gimage size  */
  643.   gimp_image_projection_realloc (gimage);
  644.  
  645.   /*  Rigor the floating selection  */
  646.   if (floating_layer)
  647.     floating_sel_rigor (floating_layer, TRUE);
  648.  
  649.   gtk_signal_emit (GTK_OBJECT (gimage), gimp_image_signals[RESIZE]);
  650.  
  651.   gimp_remove_busy_cursors (NULL);
  652. }
  653.  
  654. TileManager *
  655. gimp_image_shadow (GimpImage *gimage, 
  656.            gint       width, 
  657.            gint       height, 
  658.            gint       bpp)
  659. {
  660.   if (gimage->shadow &&
  661.       ((width != tile_manager_level_width (gimage->shadow)) ||
  662.        (height != tile_manager_level_height (gimage->shadow)) ||
  663.        (bpp != tile_manager_level_bpp (gimage->shadow))))
  664.     gimp_image_free_shadow (gimage);
  665.   else if (gimage->shadow)
  666.     return gimage->shadow;
  667.  
  668.   gimp_image_allocate_shadow (gimage, width, height, bpp);
  669.  
  670.   return gimage->shadow;
  671. }
  672.  
  673. void
  674. gimp_image_free_shadow (GimpImage *gimage)
  675. {
  676.   /*  Free the shadow buffer from the specified gimage if it exists  */
  677.   if (gimage->shadow)
  678.     tile_manager_destroy (gimage->shadow);
  679.  
  680.   gimage->shadow = NULL;
  681. }
  682.  
  683. static void
  684. gimp_image_destroy (GtkObject *object)
  685. {
  686.   GimpImage* gimage = GIMP_IMAGE (object);
  687.  
  688.   gimp_image_free_projection (gimage);
  689.   gimp_image_free_shadow (gimage);
  690.   
  691.   if (gimage->cmap)
  692.     g_free (gimage->cmap);
  693.   
  694.   if (gimage->has_filename)
  695.     g_free (gimage->filename);
  696.   
  697.   gimp_image_free_layers (gimage);
  698.   gimp_image_free_channels (gimage);
  699.   channel_delete (gimage->selection_mask);
  700.  
  701.   if (gimage->comp_preview)
  702.     temp_buf_free (gimage->comp_preview);
  703.  
  704.   if (gimage->parasites)
  705.     gtk_object_unref (GTK_OBJECT (gimage->parasites));
  706. }
  707.  
  708. void
  709. gimp_image_apply_image (GimpImage     *gimage,
  710.             GimpDrawable     *drawable,
  711.             PixelRegion     *src2PR,
  712.             gint              undo,
  713.             gint              opacity,
  714.             LayerModeEffects  mode,
  715.             /*  alternative to using drawable tiles as src1: */
  716.             TileManager     *src1_tiles,
  717.             gint              x,
  718.             gint              y)
  719. {
  720.   Channel     *mask;
  721.   gint         x1, y1, x2, y2;
  722.   gint         offset_x, offset_y;
  723.   PixelRegion  src1PR, destPR, maskPR;
  724.   gint         operation;
  725.   gint         active [MAX_CHANNELS];
  726.  
  727.   /*  get the selection mask if one exists  */
  728.   mask = (gimage_mask_is_empty (gimage)) ? NULL : gimp_image_get_mask (gimage);
  729.  
  730.   /*  configure the active channel array  */
  731.   gimp_image_get_active_channels (gimage, drawable, active);
  732.  
  733.   /*  determine what sort of operation is being attempted and
  734.    *  if it's actually legal...
  735.    */
  736.   operation = valid_combinations [drawable_type (drawable)][src2PR->bytes];
  737.   if (operation == -1)
  738.     {
  739.       g_message ("gimp_image_apply_image sent illegal parameters");
  740.       return;
  741.     }
  742.  
  743.   /*  get the layer offsets  */
  744.   drawable_offsets (drawable, &offset_x, &offset_y);
  745.  
  746.   /*  make sure the image application coordinates are within gimage bounds  */
  747.   x1 = CLAMP (x, 0, drawable_width  (drawable));
  748.   y1 = CLAMP (y, 0, drawable_height (drawable));
  749.   x2 = CLAMP (x + src2PR->w, 0, drawable_width  (drawable));
  750.   y2 = CLAMP (y + src2PR->h, 0, drawable_height (drawable));
  751.  
  752.   if (mask)
  753.     {
  754.       /*  make sure coordinates are in mask bounds ...
  755.        *  we need to add the layer offset to transform coords
  756.        *  into the mask coordinate system
  757.        */
  758.       x1 = CLAMP (x1, -offset_x, drawable_width (GIMP_DRAWABLE(mask))-offset_x);
  759.       y1 = CLAMP (y1, -offset_y, drawable_height(GIMP_DRAWABLE(mask))-offset_y);
  760.       x2 = CLAMP (x2, -offset_x, drawable_width (GIMP_DRAWABLE(mask))-offset_x);
  761.       y2 = CLAMP (y2, -offset_y, drawable_height(GIMP_DRAWABLE(mask))-offset_y);
  762.     }
  763.  
  764.   /*  If the calling procedure specified an undo step...  */
  765.   if (undo)
  766.     undo_push_image (gimp_drawable_gimage (drawable), drawable, x1, y1, x2, y2);
  767.  
  768.   /* configure the pixel regions
  769.    *  If an alternative to using the drawable's data as src1 was provided...
  770.    */
  771.   if (src1_tiles)
  772.     pixel_region_init (&src1PR, src1_tiles, 
  773.                x1, y1, (x2 - x1), (y2 - y1), FALSE);
  774.   else
  775.     pixel_region_init (&src1PR, drawable_data (drawable), 
  776.                x1, y1, (x2 - x1), (y2 - y1), FALSE);
  777.   pixel_region_init (&destPR, drawable_data (drawable), 
  778.              x1, y1, (x2 - x1), (y2 - y1), TRUE);
  779.   pixel_region_resize (src2PR, 
  780.                src2PR->x + (x1 - x), src2PR->y + (y1 - y), 
  781.                (x2 - x1), (y2 - y1));
  782.  
  783.   if (mask)
  784.     {
  785.       gint mx, my;
  786.  
  787.       /*  configure the mask pixel region
  788.        *  don't use x1 and y1 because they are in layer
  789.        *  coordinate system.  Need mask coordinate system
  790.        */
  791.       mx = x1 + offset_x;
  792.       my = y1 + offset_y;
  793.  
  794.       pixel_region_init (&maskPR, 
  795.              drawable_data (GIMP_DRAWABLE(mask)), 
  796.              mx, my, 
  797.              (x2 - x1), (y2 - y1), 
  798.              FALSE);
  799.       combine_regions (&src1PR, src2PR, &destPR, &maskPR, NULL,
  800.                opacity, mode, active, operation);
  801.     }
  802.   else
  803.     combine_regions (&src1PR, src2PR, &destPR, NULL, NULL,
  804.              opacity, mode, active, operation);
  805. }
  806.  
  807. /* Similar to gimp_image_apply_image but works in "replace" mode (i.e.
  808.    transparent pixels in src2 make the result transparent rather
  809.    than opaque.
  810.  
  811.    Takes an additional mask pixel region as well.
  812. */
  813. void
  814. gimp_image_replace_image (GimpImage    *gimage, 
  815.               GimpDrawable *drawable, 
  816.               PixelRegion  *src2PR,
  817.               gint          undo, 
  818.               gint          opacity,
  819.               PixelRegion  *maskPR,
  820.               gint          x, 
  821.               gint          y)
  822. {
  823.   Channel     *mask;
  824.   gint         x1, y1, x2, y2;
  825.   gint         offset_x, offset_y;
  826.   PixelRegion  src1PR, destPR;
  827.   PixelRegion  mask2PR, tempPR;
  828.   guchar      *temp_data;
  829.   gint         operation;
  830.   gint         active [MAX_CHANNELS];
  831.  
  832.   /*  get the selection mask if one exists  */
  833.   mask = (gimage_mask_is_empty (gimage)) ? NULL : gimp_image_get_mask (gimage);
  834.  
  835.   /*  configure the active channel array  */
  836.   gimp_image_get_active_channels (gimage, drawable, active);
  837.  
  838.   /*  determine what sort of operation is being attempted and
  839.    *  if it's actually legal...
  840.    */
  841.   operation = valid_combinations [drawable_type (drawable)][src2PR->bytes];
  842.   if (operation == -1)
  843.     {
  844.       g_message ("gimp_image_apply_image sent illegal parameters");
  845.       return;
  846.     }
  847.  
  848.   /*  get the layer offsets  */
  849.   drawable_offsets (drawable, &offset_x, &offset_y);
  850.  
  851.   /*  make sure the image application coordinates are within gimage bounds  */
  852.   x1 = CLAMP (x, 0, drawable_width (drawable));
  853.   y1 = CLAMP (y, 0, drawable_height (drawable));
  854.   x2 = CLAMP (x + src2PR->w, 0, drawable_width (drawable));
  855.   y2 = CLAMP (y + src2PR->h, 0, drawable_height (drawable));
  856.  
  857.   if (mask)
  858.     {
  859.       /*  make sure coordinates are in mask bounds ...
  860.        *  we need to add the layer offset to transform coords
  861.        *  into the mask coordinate system
  862.        */
  863.       x1 = CLAMP (x1, -offset_x, drawable_width (GIMP_DRAWABLE(mask))-offset_x);
  864.       y1 = CLAMP (y1, -offset_y, drawable_height(GIMP_DRAWABLE(mask))-offset_y);
  865.       x2 = CLAMP (x2, -offset_x, drawable_width (GIMP_DRAWABLE(mask))-offset_x);
  866.       y2 = CLAMP (y2, -offset_y, drawable_height(GIMP_DRAWABLE(mask))-offset_y);
  867.     }
  868.  
  869.   /*  If the calling procedure specified an undo step...  */
  870.   if (undo)
  871.     drawable_apply_image (drawable, x1, y1, x2, y2, NULL, FALSE);
  872.  
  873.   /* configure the pixel regions
  874.    *  If an alternative to using the drawable's data as src1 was provided...
  875.    */
  876.   pixel_region_init (&src1PR, drawable_data (drawable), x1, y1, (x2 - x1), (y2 - y1), FALSE);
  877.   pixel_region_init (&destPR, drawable_data (drawable), x1, y1, (x2 - x1), (y2 - y1), TRUE);
  878.   pixel_region_resize (src2PR, src2PR->x + (x1 - x), src2PR->y + (y1 - y), (x2 - x1), (y2 - y1));
  879.  
  880.   if (mask)
  881.     {
  882.       int mx, my;
  883.  
  884.       /*  configure the mask pixel region
  885.        *  don't use x1 and y1 because they are in layer
  886.        *  coordinate system.  Need mask coordinate system
  887.        */
  888.       mx = x1 + offset_x;
  889.       my = y1 + offset_y;
  890.  
  891.       pixel_region_init (&mask2PR, 
  892.              drawable_data (GIMP_DRAWABLE(mask)), 
  893.              mx, my, 
  894.              (x2 - x1), (y2 - y1), 
  895.              FALSE);
  896.  
  897.       tempPR.bytes = 1;
  898.       tempPR.x = 0;
  899.       tempPR.y = 0;
  900.       tempPR.w = x2 - x1;
  901.       tempPR.h = y2 - y1;
  902.       tempPR.rowstride = tempPR.w * tempPR.bytes;
  903.       temp_data = g_malloc (tempPR.h * tempPR.rowstride);
  904.       tempPR.data = temp_data;
  905.  
  906.       copy_region (&mask2PR, &tempPR);
  907.  
  908.       /* apparently, region operations can mutate some PR data. */
  909.       tempPR.x = 0;
  910.       tempPR.y = 0;
  911.       tempPR.w = x2 - x1;
  912.       tempPR.h = y2 - y1;
  913.       tempPR.data = temp_data;
  914.  
  915.       apply_mask_to_region (&tempPR, maskPR, OPAQUE_OPACITY);
  916.  
  917.       tempPR.x = 0;
  918.       tempPR.y = 0;
  919.       tempPR.w = x2 - x1;
  920.       tempPR.h = y2 - y1;
  921.       tempPR.data = temp_data;
  922.  
  923.       combine_regions_replace (&src1PR, src2PR, &destPR, &tempPR, NULL,
  924.                opacity, active, operation);
  925.  
  926.       g_free (temp_data);
  927.     }
  928.   else
  929.     combine_regions_replace (&src1PR, src2PR, &destPR, maskPR, NULL,
  930.              opacity, active, operation);
  931. }
  932.  
  933. /* Get rid of these! A "foreground" is an UI concept.. */
  934.  
  935. void
  936. gimp_image_get_foreground (GimpImage    *gimage, 
  937.                GimpDrawable *drawable, 
  938.                guchar       *fg)
  939. {
  940.   guchar pfg[3];
  941.  
  942.   /*  Get the palette color  */
  943.   gimp_context_get_foreground (NULL, &pfg[0], &pfg[1], &pfg[2]);
  944.  
  945.   gimp_image_transform_color (gimage, drawable, pfg, fg, RGB);
  946. }
  947.  
  948. void
  949. gimp_image_get_background (GimpImage    *gimage, 
  950.                GimpDrawable *drawable, 
  951.                guchar       *bg)
  952. {
  953.   guchar pbg[3];
  954.  
  955.   /*  Get the palette color  */
  956.   gimp_context_get_background (NULL, &pbg[0], &pbg[1], &pbg[2]);
  957.  
  958.   gimp_image_transform_color (gimage, drawable, pbg, bg, RGB);
  959. }
  960.  
  961. guchar *
  962. gimp_image_get_color_at (GimpImage *gimage, 
  963.              gint       x, 
  964.              gint       y)
  965. {
  966.   Tile   *tile;
  967.   guchar *src;
  968.   guchar *dest;
  969.  
  970.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  971.  
  972.   if (x < 0 || y < 0 || x >= gimage->width || y >= gimage->height)
  973.     return NULL;
  974.   
  975.   dest = g_new (unsigned char, 5);
  976.   tile = tile_manager_get_tile (gimp_image_composite (gimage), x, y,
  977.                 TRUE, FALSE);
  978.   src = tile_data_pointer (tile, x % TILE_WIDTH, y % TILE_HEIGHT);
  979.   gimp_image_get_color (gimage, gimp_image_composite_type (gimage), dest, src);
  980.  
  981.   if (GIMP_IMAGE_TYPE_HAS_ALPHA (gimp_image_composite_type (gimage)))
  982.     dest[3] = src[gimp_image_composite_bytes (gimage) - 1];
  983.   else
  984.     dest[3] = 255;
  985.  
  986.   dest[4] = 0;
  987.   tile_release (tile, FALSE);
  988.  
  989.   return dest;
  990. }
  991.  
  992. void
  993. gimp_image_get_color (GimpImage     *gimage, 
  994.               GimpImageType  d_type,
  995.               guchar        *rgb, 
  996.               guchar        *src)
  997. {
  998.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  999.  
  1000.   switch (d_type)
  1001.     {
  1002.     case RGB_GIMAGE: case RGBA_GIMAGE:
  1003.       map_to_color (0, NULL, src, rgb);
  1004.       break;
  1005.     case GRAY_GIMAGE: case GRAYA_GIMAGE:
  1006.       map_to_color (1, NULL, src, rgb);
  1007.       break;
  1008.     case INDEXED_GIMAGE: case INDEXEDA_GIMAGE:
  1009.       map_to_color (2, gimage->cmap, src, rgb);
  1010.       break;
  1011.     }
  1012. }
  1013.  
  1014. void
  1015. gimp_image_transform_color (GimpImage         *gimage, 
  1016.                 GimpDrawable      *drawable,
  1017.                 guchar            *src, 
  1018.                 guchar            *dest, 
  1019.                 GimpImageBaseType  type)
  1020. {
  1021.   GimpImageType d_type;
  1022.  
  1023.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  1024.  
  1025.   d_type = (drawable != NULL) ? drawable_type (drawable) :
  1026.     gimp_image_base_type_with_alpha (gimage);
  1027.  
  1028.   switch (type)
  1029.     {
  1030.     case RGB:
  1031.       switch (d_type)
  1032.     {
  1033.     case RGB_GIMAGE: case RGBA_GIMAGE:
  1034.       /*  Straight copy  */
  1035.       *dest++ = *src++;
  1036.       *dest++ = *src++;
  1037.       *dest++ = *src++;
  1038.       break;
  1039.     case GRAY_GIMAGE: case GRAYA_GIMAGE:
  1040.       /*  NTSC conversion  */
  1041.       *dest = INTENSITY (src[RED_PIX],
  1042.                  src[GREEN_PIX],
  1043.                  src[BLUE_PIX]);
  1044.       break;
  1045.     case INDEXED_GIMAGE: case INDEXEDA_GIMAGE:
  1046.       /*  Least squares method  */
  1047.       *dest = map_rgb_to_indexed (gimage->cmap,
  1048.                       gimage->num_cols,
  1049.                       gimage,
  1050.                       src[RED_PIX],
  1051.                       src[GREEN_PIX],
  1052.                       src[BLUE_PIX]);
  1053.       break;
  1054.     }
  1055.       break;
  1056.     case GRAY:
  1057.       switch (d_type)
  1058.     {
  1059.     case RGB_GIMAGE: case RGBA_GIMAGE:
  1060.       /*  Gray to RG&B */
  1061.       *dest++ = *src;
  1062.       *dest++ = *src;
  1063.       *dest++ = *src;
  1064.       break;
  1065.     case GRAY_GIMAGE: case GRAYA_GIMAGE:
  1066.       /*  Straight copy  */
  1067.       *dest = *src;
  1068.       break;
  1069.     case INDEXED_GIMAGE: case INDEXEDA_GIMAGE:
  1070.       /*  Least squares method  */
  1071.       *dest = map_rgb_to_indexed (gimage->cmap,
  1072.                       gimage->num_cols,
  1073.                       gimage,
  1074.                       src[GRAY_PIX],
  1075.                       src[GRAY_PIX],
  1076.                       src[GRAY_PIX]);
  1077.       break;
  1078.     }
  1079.       break;
  1080.     default:
  1081.       break;
  1082.     }
  1083. }
  1084.  
  1085. Guide*
  1086. gimp_image_add_hguide (GimpImage *gimage)
  1087. {
  1088.   Guide *guide;
  1089.  
  1090.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  1091.  
  1092.   guide = g_new (Guide, 1);
  1093.   guide->ref_count   = 0;
  1094.   guide->position    = -1;
  1095.   guide->guide_ID    = next_guide_id++;
  1096.   guide->orientation = ORIENTATION_HORIZONTAL;
  1097.  
  1098.   gimage->guides = g_list_prepend (gimage->guides, guide);
  1099.  
  1100.   return guide;
  1101. }
  1102.  
  1103. Guide*
  1104. gimp_image_add_vguide (GimpImage *gimage)
  1105. {
  1106.   Guide *guide;
  1107.  
  1108.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  1109.  
  1110.   guide = g_new (Guide, 1);
  1111.   guide->ref_count   = 0;
  1112.   guide->position    = -1;
  1113.   guide->guide_ID    = next_guide_id++;
  1114.   guide->orientation = ORIENTATION_VERTICAL;
  1115.  
  1116.   gimage->guides = g_list_prepend (gimage->guides, guide);
  1117.  
  1118.   return guide;
  1119. }
  1120.  
  1121. void
  1122. gimp_image_add_guide (GimpImage *gimage,
  1123.               Guide     *guide)
  1124. {
  1125.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  1126.  
  1127.   gimage->guides = g_list_prepend (gimage->guides, guide);
  1128. }
  1129.  
  1130. void
  1131. gimp_image_remove_guide (GimpImage *gimage,
  1132.              Guide     *guide)
  1133. {
  1134.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  1135.  
  1136.   gimage->guides = g_list_remove (gimage->guides, guide);
  1137. }
  1138.  
  1139. void
  1140. gimp_image_delete_guide (GimpImage *gimage,
  1141.              Guide     *guide) 
  1142. {
  1143.   guide->position = -1;
  1144.  
  1145.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  1146.  
  1147.   if (guide->ref_count <= 0)
  1148.     {
  1149.       gimage->guides = g_list_remove (gimage->guides, guide);
  1150.       g_free (guide);
  1151.     }
  1152. }
  1153.  
  1154.  
  1155. GimpParasite *
  1156. gimp_image_parasite_find (const GimpImage *gimage, 
  1157.               const gchar     *name)
  1158. {
  1159.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  1160.  
  1161.   return parasite_list_find (gimage->parasites, name);
  1162. }
  1163.  
  1164. static void
  1165. list_func (gchar          *key, 
  1166.        GimpParasite   *p, 
  1167.        gchar        ***cur)
  1168. {
  1169.   *(*cur)++ = (gchar *) g_strdup (key);
  1170. }
  1171.  
  1172. gchar **
  1173. gimp_image_parasite_list (GimpImage *gimage, 
  1174.               gint      *count)
  1175. {
  1176.   gchar **list;
  1177.   gchar **cur;
  1178.  
  1179.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  1180.  
  1181.   *count = parasite_list_length (gimage->parasites);
  1182.   cur = list = g_new (gchar*, *count);
  1183.  
  1184.   parasite_list_foreach (gimage->parasites, (GHFunc) list_func, &cur);
  1185.   
  1186.   return list;
  1187. }
  1188.  
  1189. void
  1190. gimp_image_parasite_attach (GimpImage    *gimage, 
  1191.                 GimpParasite *parasite)
  1192. {
  1193.   g_return_if_fail (GIMP_IS_IMAGE (gimage) && parasite != NULL);
  1194.  
  1195.   /* only set the dirty bit manually if we can be saved and the new
  1196.      parasite differs from the current one and we aren't undoable */
  1197.   if (gimp_parasite_is_undoable (parasite))
  1198.     undo_push_image_parasite (gimage, parasite);
  1199.  
  1200.   /*  We used to push an cantundo on te stack here. This made the undo stack
  1201.       unusable (NULL on the stack) and prevented people from undoing after a 
  1202.       save (since most save plug-ins attach an undoable comment parasite).
  1203.       Now we simply attach the parasite without pushing an undo. That way it's
  1204.       undoable but does not block the undo system.   --Sven
  1205.    */
  1206.  
  1207.   parasite_list_add (gimage->parasites, parasite);
  1208.  
  1209.   if (gimp_parasite_has_flag (parasite, GIMP_PARASITE_ATTACH_PARENT))
  1210.     {
  1211.       parasite_shift_parent (parasite);
  1212.       gimp_parasite_attach (parasite);
  1213.     }
  1214. }
  1215.  
  1216. void
  1217. gimp_image_parasite_detach (GimpImage   *gimage, 
  1218.                 const gchar *parasite)
  1219. {
  1220.   GimpParasite *p;
  1221.  
  1222.   g_return_if_fail (GIMP_IS_IMAGE (gimage) && parasite != NULL);
  1223.  
  1224.   if (!(p = parasite_list_find (gimage->parasites, parasite)))
  1225.     return;
  1226.  
  1227.   if (gimp_parasite_is_undoable (p))
  1228.     undo_push_image_parasite_remove (gimage, gimp_parasite_name (p));
  1229.  
  1230.   parasite_list_remove (gimage->parasites, parasite);
  1231. }
  1232.  
  1233. Tattoo
  1234. gimp_image_get_new_tattoo (GimpImage *image)
  1235. {
  1236.   image->tattoo_state++;
  1237.   if (image->tattoo_state <= 0)
  1238.     g_warning ("Tattoo state has become corrupt (2.1 billion operation limit exceded)");
  1239.   return (image->tattoo_state);
  1240. }
  1241.  
  1242. Tattoo
  1243. gimp_image_get_tattoo_state (GimpImage *image)
  1244. {
  1245.   return (image->tattoo_state);
  1246. }
  1247.  
  1248. gboolean
  1249. gimp_image_set_tattoo_state (GimpImage *gimage, 
  1250.                  Tattoo     val)
  1251. {
  1252.   Layer    *layer;
  1253.   GSList   *layers;
  1254.   gboolean  retval = TRUE;
  1255.   Channel  *channel;
  1256.   GSList   *channels;
  1257.   Tattoo    maxval = 0;
  1258.   Path     *pptr   = NULL;
  1259.   PathList *plist;
  1260.  
  1261.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  1262.  
  1263.   for (layers = gimage->layers; layers; layers = g_slist_next (layers))
  1264.     {
  1265.       Tattoo ltattoo;
  1266.       layer = (Layer *) layers->data;
  1267.       
  1268.       ltattoo = layer_get_tattoo (layer);
  1269.       if (ltattoo > maxval)
  1270.     maxval = ltattoo;
  1271.       if (gimp_image_get_channel_by_tattoo (gimage, ltattoo) != NULL)
  1272.     {
  1273.       retval = FALSE; /* Oopps duplicated tattoo in channel */
  1274.     }
  1275.  
  1276.       /* Now check path an't got this tattoo */
  1277.       if (path_get_path_by_tattoo (gimage, ltattoo) != NULL)
  1278.     {
  1279.       retval = FALSE; /* Oopps duplicated tattoo in layer */
  1280.     }
  1281.     }
  1282.  
  1283.   /* Now check that the paths channel tattoos don't overlap */
  1284.   for (channels = gimage->channels; 
  1285.        channels; 
  1286.        channels = g_slist_next (channels))
  1287.     {
  1288.       Tattoo ctattoo;
  1289.       channel = (Channel *) channels->data;
  1290.       
  1291.       ctattoo = channel_get_tattoo (channel);
  1292.       if (ctattoo > maxval)
  1293.     maxval = ctattoo;
  1294.       /* Now check path an't got this tattoo */
  1295.       if (path_get_path_by_tattoo (gimage, ctattoo) != NULL)
  1296.     {
  1297.       retval = FALSE; /* Oopps duplicated tattoo in layer */
  1298.     }
  1299.     }
  1300.  
  1301.   /* Find the max tatto value in the paths */
  1302.   plist = gimage->paths;
  1303.       
  1304.   if (plist && plist->bz_paths)
  1305.     {
  1306.       Tattoo  ptattoo;
  1307.       GSList *pl = plist->bz_paths;
  1308.  
  1309.       while (pl)
  1310.     {
  1311.       pptr = pl->data;
  1312.  
  1313.       ptattoo = path_get_tattoo (pptr);
  1314.       
  1315.       if (ptattoo > maxval)
  1316.         maxval = ptattoo;
  1317.       
  1318.       pl = pl->next;
  1319.     }
  1320.     }
  1321.  
  1322.   if (val < maxval)
  1323.     retval = FALSE;
  1324.   /* Must check the state is valid */
  1325.   if (retval == TRUE)
  1326.     gimage->tattoo_state = val;
  1327.  
  1328.   return retval;
  1329. }
  1330.  
  1331. void
  1332. gimp_image_colormap_changed (GimpImage *gimage, 
  1333.                  gint       col)
  1334. {
  1335.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  1336.   g_return_if_fail (col < gimage->num_cols);
  1337.  
  1338.   gtk_signal_emit (GTK_OBJECT (gimage),
  1339.            gimp_image_signals[COLORMAP_CHANGED],
  1340.            col);
  1341. }
  1342.  
  1343. void
  1344. gimp_image_set_paths (GimpImage *gimage,
  1345.               PathList  *paths)
  1346. {
  1347.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  1348.  
  1349.   gimage->paths = paths;
  1350. }
  1351.  
  1352. PathList *
  1353. gimp_image_get_paths (GimpImage *gimage)
  1354. {
  1355.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  1356.  
  1357.   return gimage->paths;
  1358. }
  1359.     
  1360. /************************************************************/
  1361. /*  Projection functions                                    */
  1362. /************************************************************/
  1363.  
  1364. static void
  1365. project_intensity (GimpImage   *gimage, 
  1366.            Layer       *layer,
  1367.            PixelRegion *src, 
  1368.            PixelRegion *dest, 
  1369.            PixelRegion *mask)
  1370. {
  1371.   if (! gimage->construct_flag)
  1372.     initial_region (src, dest, mask, NULL, layer->opacity,
  1373.             layer->mode, gimage->visible, INITIAL_INTENSITY);
  1374.   else
  1375.     combine_regions (dest, src, dest, mask, NULL, layer->opacity,
  1376.              layer->mode, gimage->visible, COMBINE_INTEN_A_INTEN);
  1377. }
  1378.  
  1379. static void
  1380. project_intensity_alpha (GimpImage   *gimage, 
  1381.              Layer       *layer,
  1382.              PixelRegion *src, 
  1383.              PixelRegion *dest,
  1384.              PixelRegion *mask)
  1385. {
  1386.   if (! gimage->construct_flag)
  1387.     initial_region (src, dest, mask, NULL, layer->opacity,
  1388.             layer->mode, gimage->visible, INITIAL_INTENSITY_ALPHA);
  1389.   else
  1390.     combine_regions (dest, src, dest, mask, NULL, layer->opacity,
  1391.              layer->mode, gimage->visible, COMBINE_INTEN_A_INTEN_A);
  1392. }
  1393.  
  1394. static void
  1395. project_indexed (GimpImage   *gimage, 
  1396.          Layer       *layer,
  1397.          PixelRegion *src, 
  1398.          PixelRegion *dest)
  1399. {
  1400.   if (! gimage->construct_flag)
  1401.     initial_region (src, dest, NULL, gimage->cmap, layer->opacity,
  1402.             layer->mode, gimage->visible, INITIAL_INDEXED);
  1403.   else
  1404.     g_message ("Unable to project indexed image.");
  1405. }
  1406.  
  1407. static void
  1408. project_indexed_alpha (GimpImage   *gimage, 
  1409.                Layer       *layer,
  1410.                PixelRegion *src, 
  1411.                PixelRegion *dest,
  1412.                PixelRegion *mask)
  1413. {
  1414.   if (! gimage->construct_flag)
  1415.     initial_region (src, dest, mask, gimage->cmap, layer->opacity,
  1416.             layer->mode, gimage->visible, INITIAL_INDEXED_ALPHA);
  1417.   else
  1418.     combine_regions (dest, src, dest, mask, gimage->cmap, layer->opacity,
  1419.              layer->mode, gimage->visible, COMBINE_INTEN_A_INDEXED_A);
  1420. }
  1421.  
  1422. static void
  1423. project_channel (GimpImage   *gimage, 
  1424.          Channel     *channel,
  1425.          PixelRegion *src, 
  1426.          PixelRegion *src2)
  1427. {
  1428.   gint type;
  1429.  
  1430.   if (! gimage->construct_flag)
  1431.     {
  1432.       type = (channel->show_masked) ?
  1433.     INITIAL_CHANNEL_MASK : INITIAL_CHANNEL_SELECTION;
  1434.       initial_region (src2, src, NULL, channel->col, channel->opacity,
  1435.               NORMAL_MODE, NULL, type);
  1436.     }
  1437.   else
  1438.     {
  1439.       type = (channel->show_masked) ?
  1440.     COMBINE_INTEN_A_CHANNEL_MASK : COMBINE_INTEN_A_CHANNEL_SELECTION;
  1441.       combine_regions (src, src2, src, NULL, channel->col, channel->opacity,
  1442.                NORMAL_MODE, NULL, type);
  1443.     }
  1444. }
  1445.  
  1446. /************************************************************/
  1447. /*  Layer/Channel functions                                 */
  1448. /************************************************************/
  1449.  
  1450. static void
  1451. gimp_image_free_layers (GimpImage *gimage)
  1452. {
  1453.   GSList *list;
  1454.   Layer  *layer;
  1455.  
  1456.   for (list = gimage->layers; list; list = g_slist_next (list))
  1457.     {
  1458.       layer = (Layer *) list->data;
  1459.       layer_delete (layer);
  1460.     }
  1461.   g_slist_free (gimage->layers);
  1462.   g_slist_free (gimage->layer_stack);
  1463. }
  1464.  
  1465.  
  1466. static void
  1467. gimp_image_free_channels (GimpImage *gimage)
  1468. {
  1469.   GSList  *list;
  1470.   Channel *channel;
  1471.  
  1472.   for (list = gimage->channels; list; list = g_slist_next (list))
  1473.     {
  1474.       channel = (Channel *) list->data;
  1475.       channel_delete (channel);
  1476.     }
  1477.   g_slist_free (gimage->channels);
  1478. }
  1479.  
  1480.  
  1481. static void
  1482. gimp_image_construct_layers (GimpImage *gimage, 
  1483.                  gint       x, 
  1484.                  gint       y, 
  1485.                  gint       w, 
  1486.                  gint       h)
  1487. {
  1488.   Layer       *layer;
  1489.   gint         x1, y1, x2, y2;
  1490.   PixelRegion  src1PR, src2PR, maskPR;
  1491.   PixelRegion * mask;
  1492.   GSList      *list;
  1493.   GSList      *reverse_list = NULL;
  1494.   gint         off_x;
  1495.   gint         off_y;
  1496.  
  1497.   /*  composite the floating selection if it exists  */
  1498.   if ((layer = gimp_image_floating_sel (gimage)))
  1499.     floating_sel_composite (layer, x, y, w, h, FALSE);
  1500.  
  1501.   /* Note added by Raph Levien, 27 Jan 1998
  1502.  
  1503.      This looks it was intended as an optimization, but it seems to
  1504.      have correctness problems. In particular, if all channels are
  1505.      turned off, the screen simply does not update the projected
  1506.      image. It should be black. Turning off this optimization seems to
  1507.      restore correct behavior. At some future point, it may be
  1508.      desirable to turn the optimization back on.
  1509.  
  1510.      */
  1511. #if 0
  1512.   /*  If all channels are not visible, simply return  */
  1513.   switch (gimp_image_base_type (gimage))
  1514.     {
  1515.     case RGB:
  1516.       if (! gimp_image_get_component_visible (gimage, RED_CHANNEL) &&
  1517.       ! gimp_image_get_component_visible (gimage, GREEN_CHANNEL) &&
  1518.       ! gimp_image_get_component_visible (gimage, BLUE_CHANNEL))
  1519.     return;
  1520.       break;
  1521.     case GRAY:
  1522.       if (! gimp_image_get_component_visible (gimage, GRAY_CHANNEL))
  1523.     return;
  1524.       break;
  1525.     case INDEXED:
  1526.       if (! gimp_image_get_component_visible (gimage, INDEXED_CHANNEL))
  1527.     return;
  1528.       break;
  1529.     }
  1530. #endif
  1531.  
  1532.   for (list = gimage->layers; list; list = g_slist_next (list))
  1533.     {
  1534.       layer = (Layer *) list->data;
  1535.  
  1536.       /*  only add layers that are visible and not floating selections 
  1537.       to the list  */
  1538.       if (!layer_is_floating_sel (layer) && 
  1539.       drawable_visible (GIMP_DRAWABLE(layer)))
  1540.     reverse_list = g_slist_prepend (reverse_list, layer);
  1541.     }
  1542.  
  1543.   while (reverse_list)
  1544.     {
  1545.       layer = (Layer *) reverse_list->data;
  1546.       drawable_offsets (GIMP_DRAWABLE(layer), &off_x, &off_y);
  1547.  
  1548.       x1 = CLAMP (off_x, x, x + w);
  1549.       y1 = CLAMP (off_y, y, y + h);
  1550.       x2 = CLAMP (off_x + drawable_width (GIMP_DRAWABLE(layer)), x, x + w);
  1551.       y2 = CLAMP (off_y + drawable_height (GIMP_DRAWABLE(layer)), y, y + h);
  1552.  
  1553.       /* configure the pixel regions  */
  1554.       pixel_region_init (&src1PR, gimp_image_projection (gimage), 
  1555.              x1, y1, (x2 - x1), (y2 - y1), 
  1556.              TRUE);
  1557.  
  1558.       /*  If we're showing the layer mask instead of the layer...  */
  1559.       if (layer->mask && layer->show_mask)
  1560.     {
  1561.       pixel_region_init (&src2PR, 
  1562.                  drawable_data (GIMP_DRAWABLE(layer->mask)),
  1563.                  (x1 - off_x), (y1 - off_y),
  1564.                  (x2 - x1), (y2 - y1), FALSE);
  1565.  
  1566.       copy_gray_to_region (&src2PR, &src1PR);
  1567.     }
  1568.       /*  Otherwise, normal  */
  1569.       else
  1570.     {
  1571.       pixel_region_init (&src2PR, 
  1572.                  drawable_data (GIMP_DRAWABLE (layer)),
  1573.                  (x1 - off_x), (y1 - off_y),
  1574.                  (x2 - x1), (y2 - y1), FALSE);
  1575.  
  1576.       if (layer->mask && layer->apply_mask)
  1577.         {
  1578.           pixel_region_init (&maskPR, 
  1579.                  drawable_data (GIMP_DRAWABLE (layer->mask)),
  1580.                  (x1 - off_x), (y1 - off_y),
  1581.                  (x2 - x1), (y2 - y1), FALSE);
  1582.           mask = &maskPR;
  1583.         }
  1584.       else
  1585.         mask = NULL;
  1586.  
  1587.       /*  Based on the type of the layer, project the layer onto the
  1588.        *  projection image...
  1589.        */
  1590.       switch (drawable_type (GIMP_DRAWABLE(layer)))
  1591.         {
  1592.         case RGB_GIMAGE: case GRAY_GIMAGE:
  1593.           /* no mask possible */
  1594.           project_intensity (gimage, layer, &src2PR, &src1PR, mask);
  1595.           break;
  1596.  
  1597.         case RGBA_GIMAGE: case GRAYA_GIMAGE:
  1598.           project_intensity_alpha (gimage, layer, &src2PR, &src1PR, mask);
  1599.           break;
  1600.  
  1601.         case INDEXED_GIMAGE:
  1602.           /* no mask possible */
  1603.           project_indexed (gimage, layer, &src2PR, &src1PR);
  1604.           break;
  1605.  
  1606.         case INDEXEDA_GIMAGE:
  1607.           project_indexed_alpha (gimage, layer, &src2PR, &src1PR, mask);
  1608.           break;
  1609.  
  1610.         default:
  1611.           break;
  1612.         }
  1613.     }
  1614.       gimage->construct_flag = 1;  /*  something was projected  */
  1615.  
  1616.       reverse_list = g_slist_next (reverse_list);
  1617.     }
  1618.  
  1619.   g_slist_free (reverse_list);
  1620. }
  1621.  
  1622. static void
  1623. gimp_image_construct_channels (GimpImage *gimage, 
  1624.                    gint       x, 
  1625.                    gint       y, 
  1626.                    gint       w, 
  1627.                    gint       h)
  1628. {
  1629.   Channel     *channel;
  1630.   PixelRegion  src1PR, src2PR;
  1631.   GSList      *list;
  1632.   GSList      *reverse_list = NULL;
  1633.  
  1634.   /*  reverse the channel list  */
  1635.   for (list = gimage->channels; list; list = g_slist_next (list))
  1636.     reverse_list = g_slist_prepend (reverse_list, list->data);
  1637.  
  1638.   while (reverse_list)
  1639.     {
  1640.       channel = (Channel *) reverse_list->data;
  1641.  
  1642.       if (drawable_visible (GIMP_DRAWABLE (channel)))
  1643.     {
  1644.       /* configure the pixel regions  */
  1645.       pixel_region_init (&src1PR, gimp_image_projection (gimage), 
  1646.                  x, y, w, h, 
  1647.                  TRUE);
  1648.       pixel_region_init (&src2PR, drawable_data (GIMP_DRAWABLE(channel)), 
  1649.                  x, y, w, h, 
  1650.                  FALSE);
  1651.  
  1652.       project_channel (gimage, channel, &src1PR, &src2PR);
  1653.  
  1654.       gimage->construct_flag = 1;
  1655.     }
  1656.  
  1657.       reverse_list = g_slist_next (reverse_list);
  1658.     }
  1659.  
  1660.   g_slist_free (reverse_list);
  1661. }
  1662.  
  1663. static void
  1664. gimp_image_initialize_projection (GimpImage *gimage, 
  1665.                   gint       x, 
  1666.                   gint       y, 
  1667.                   gint       w, 
  1668.                   gint       h)
  1669. {
  1670.   GSList      *list;
  1671.   Layer       *layer;
  1672.   gint         coverage = 0;
  1673.   PixelRegion  PR;
  1674.   guchar       clear[4] = { 0, 0, 0, 0 };
  1675.  
  1676.   /*  this function determines whether a visible layer
  1677.    *  provides complete coverage over the image.  If not,
  1678.    *  the projection is initialized to transparent
  1679.    */
  1680.   
  1681.   for (list = gimage->layers; list; list = g_slist_next (list))
  1682.     {
  1683.       gint off_x, off_y;
  1684.  
  1685.       layer = (Layer *) list->data;
  1686.       drawable_offsets (GIMP_DRAWABLE(layer), &off_x, &off_y);
  1687.  
  1688.       if (drawable_visible (GIMP_DRAWABLE(layer)) &&
  1689.       ! layer_has_alpha (layer) &&
  1690.       (off_x <= x) &&
  1691.       (off_y <= y) &&
  1692.       (off_x + drawable_width (GIMP_DRAWABLE(layer)) >= x + w) &&
  1693.       (off_y + drawable_height (GIMP_DRAWABLE(layer)) >= y + h))
  1694.     {
  1695.       coverage = 1;
  1696.       break;
  1697.     }
  1698.     }
  1699.  
  1700.   if (!coverage)
  1701.     {
  1702.       pixel_region_init (&PR, gimp_image_projection (gimage), 
  1703.              x, y, w, h, TRUE);
  1704.       color_region (&PR, clear);
  1705.     }
  1706. }
  1707.  
  1708. static void
  1709. gimp_image_get_active_channels (GimpImage    *gimage, 
  1710.                 GimpDrawable *drawable, 
  1711.                 gint         *active)
  1712. {
  1713.   Layer *layer;
  1714.   gint   i;
  1715.  
  1716.   /*  first, blindly copy the gimage active channels  */
  1717.   for (i = 0; i < MAX_CHANNELS; i++)
  1718.     active[i] = gimage->active[i];
  1719.  
  1720.   /*  If the drawable is a channel (a saved selection, etc.)
  1721.    *  make sure that the alpha channel is not valid
  1722.    */
  1723.   if (GIMP_IS_CHANNEL (drawable))
  1724.     active[ALPHA_G_PIX] = 0;  /*  no alpha values in channels  */
  1725.   else
  1726.     {
  1727.       /*  otherwise, check whether preserve transparency is
  1728.        *  enabled in the layer and if the layer has alpha
  1729.        */
  1730.       if (GIMP_IS_LAYER (drawable))
  1731.     {
  1732.       layer = GIMP_LAYER (drawable);
  1733.       if (layer_has_alpha (layer) && layer->preserve_trans)
  1734.         active[drawable_bytes (drawable) - 1] = 0;
  1735.     }
  1736.     }
  1737. }
  1738.  
  1739. void
  1740. gimp_image_construct (GimpImage *gimage, 
  1741.               gint       x, 
  1742.               gint       y, 
  1743.               gint       w, 
  1744.               gint       h,
  1745.               gboolean   can_use_cowproject)
  1746. {
  1747.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  1748.  
  1749. #if 0
  1750.   gint xoff;
  1751.   gint yoff;
  1752.   
  1753.   /*  set the construct flag, used to determine if anything
  1754.    *  has been written to the gimage raw image yet.
  1755.    */
  1756.   gimage->construct_flag = 0;
  1757.  
  1758.   if (gimage->layers)
  1759.     {
  1760.       gimp_drawable_offsets (GIMP_DRAWABLE((Layer*)(gimage->layers->data)),
  1761.                  &xoff, &yoff);
  1762.     }
  1763.  
  1764.   if (/*can_use_cowproject &&*/
  1765.       (gimage->layers) &&                         /* There's a layer.      */
  1766.       (!g_slist_next(gimage->layers)) &&          /* It's the only layer.  */
  1767.       (layer_has_alpha((Layer*)(gimage->layers->data))) && /* It's !flat.  */
  1768.                                                   /* It's visible.         */
  1769.       (drawable_visible (GIMP_DRAWABLE ((Layer*)(gimage->layers->data)))) &&
  1770.       (drawable_width (GIMP_DRAWABLE ((Layer*)(gimage->layers->data))) ==
  1771.        gimage->width) &&
  1772.       (drawable_height (GIMP_DRAWABLE ((Layer*)(gimage->layers->data))) ==
  1773.        gimage->height) &&                         /* Covers all.           */
  1774.                                                   /* Not indexed.          */
  1775.       (!drawable_indexed (GIMP_DRAWABLE ((Layer*)(gimage->layers->data)))) &&
  1776.       (((Layer*)(gimage->layers->data))->opacity == OPAQUE_OPACITY) /*opaq */
  1777.       )
  1778.     {
  1779.       gint xoff;
  1780.       gint yoff;
  1781.       
  1782.       gimp_drawable_offsets (GIMP_DRAWABLE ((Layer*)(gimage->layers->data)),
  1783.                  &xoff, &yoff);
  1784.  
  1785.  
  1786.       if ((xoff==0) && (yoff==0)) /* Starts at 0,0         */
  1787.     {
  1788.       PixelRegion srcPR, destPR;
  1789.       gpointer    pr;
  1790.     
  1791.       g_warning("Can use cow-projection hack.  Yay!");
  1792.  
  1793.       pixel_region_init (&srcPR, gimp_drawable_data
  1794.                  (GIMP_DRAWABLE
  1795.                   ((Layer*)(gimage->layers->data))),
  1796.                  x, y, w,h, FALSE);
  1797.       pixel_region_init (&destPR,
  1798.                  gimp_image_projection (gimage),
  1799.                  x, y, w,h, TRUE);
  1800.  
  1801.       for (pr = pixel_regions_register (2, &srcPR, &destPR);
  1802.            pr != NULL;
  1803.            pr = pixel_regions_process (pr))
  1804.         {
  1805.           tile_manager_map_over_tile (destPR.tiles,
  1806.                       destPR.curtile, srcPR.curtile);
  1807.         }
  1808.  
  1809.       gimage->construct_flag = 1;
  1810.       gimp_image_construct_channels (gimage, x, y, w, h);
  1811.  
  1812.       return;
  1813.     }
  1814.     }
  1815. #else
  1816.   gimage->construct_flag = 0;
  1817. #endif
  1818.   
  1819.   /*  First, determine if the projection image needs to be
  1820.    *  initialized--this is the case when there are no visible
  1821.    *  layers that cover the entire canvas--either because layers
  1822.    *  are offset or only a floating selection is visible
  1823.    */
  1824.   gimp_image_initialize_projection (gimage, x, y, w, h);
  1825.   
  1826.   /*  call functions which process the list of layers and
  1827.    *  the list of channels
  1828.    */
  1829.   gimp_image_construct_layers (gimage, x, y, w, h);
  1830.   gimp_image_construct_channels (gimage, x, y, w, h);
  1831. }
  1832.  
  1833. void
  1834. gimp_image_invalidate_without_render (GimpImage *gimage, 
  1835.                       gint       x,
  1836.                       gint       y,
  1837.                       gint       w,
  1838.                       gint       h,
  1839.                       gint       x1,
  1840.                       gint       y1,
  1841.                       gint       x2,
  1842.                       gint       y2)
  1843. {
  1844.   Tile        *tile;
  1845.   TileManager *tm;
  1846.   gint         i, j;
  1847.  
  1848.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  1849.  
  1850.   tm = gimp_image_projection (gimage);
  1851.  
  1852.   /*  invalidate all tiles which are located outside of the displayed area
  1853.    *   all tiles inside the displayed area are constructed.
  1854.    */
  1855.   for (i = y; i < (y + h); i += (TILE_HEIGHT - (i % TILE_HEIGHT)))
  1856.     for (j = x; j < (x + w); j += (TILE_WIDTH - (j % TILE_WIDTH)))
  1857.       {
  1858.     tile = tile_manager_get_tile (tm, j, i, FALSE, FALSE);
  1859.  
  1860.         /*  check if the tile is outside the bounds  */
  1861.         if ((MIN ((j + tile_ewidth(tile)), x2) - MAX (j, x1)) <= 0)
  1862.           {
  1863.             tile_invalidate_tile (&tile, tm, j, i);
  1864.           }
  1865.         else if (MIN ((i + tile_eheight(tile)), y2) - MAX (i, y1) <= 0)
  1866.           {
  1867.             tile_invalidate_tile (&tile, tm, j, i);
  1868.           }
  1869.       }
  1870. }
  1871.  
  1872. void
  1873. gimp_image_invalidate (GimpImage *gimage, 
  1874.                gint       x,
  1875.                gint       y,
  1876.                gint       w,
  1877.                gint       h,
  1878.                gint       x1,
  1879.                gint       y1,
  1880.                gint       x2,
  1881.                gint       y2)
  1882. {
  1883.   Tile        *tile;
  1884.   TileManager *tm;
  1885.   gint         i, j;
  1886.   gint         startx, starty;
  1887.   gint         endx, endy;
  1888.   gint         tilex, tiley;
  1889.  
  1890.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  1891.  
  1892.   tm = gimp_image_projection (gimage);
  1893.  
  1894.   startx = x;
  1895.   starty = y;
  1896.   endx   = x + w;
  1897.   endy   = y + h;
  1898.  
  1899.   /*  invalidate all tiles which are located outside of the displayed area
  1900.    *   all tiles inside the displayed area are constructed.
  1901.    */
  1902.   for (i = y; i < (y + h); i += (TILE_HEIGHT - (i % TILE_HEIGHT)))
  1903.     for (j = x; j < (x + w); j += (TILE_WIDTH - (j % TILE_WIDTH)))
  1904.       {
  1905.     tile = tile_manager_get_tile (tm, j, i, FALSE, FALSE);
  1906.  
  1907.         /*  check if the tile is outside the bounds  */
  1908.         if ((MIN ((j + tile_ewidth(tile)), x2) - MAX (j, x1)) <= 0)
  1909.           {
  1910.             tile_invalidate_tile (&tile, tm, j, i);
  1911.             if (j < x1)
  1912.               startx = MAX (startx, (j + tile_ewidth(tile)));
  1913.             else
  1914.               endx = MIN (endx, j);
  1915.           }
  1916.         else if (MIN ((i + tile_eheight(tile)), y2) - MAX (i, y1) <= 0)
  1917.           {
  1918.             tile_invalidate_tile (&tile, tm, j, i);
  1919.             if (i < y1)
  1920.               starty = MAX (starty, (i + tile_eheight(tile)));
  1921.             else
  1922.               endy = MIN (endy, i);
  1923.           }
  1924.         else
  1925.           {
  1926.             /*  If the tile is not valid, make sure we get the entire tile
  1927.              *   in the construction extents
  1928.              */
  1929.             if (tile_is_valid(tile) == FALSE)
  1930.               {
  1931.                 tilex = j - (j % TILE_WIDTH);
  1932.                 tiley = i - (i % TILE_HEIGHT);
  1933.                 
  1934.                 startx = MIN (startx, tilex);
  1935.                 endx = MAX (endx, tilex + tile_ewidth(tile));
  1936.                 starty = MIN (starty, tiley);
  1937.                 endy = MAX (endy, tiley + tile_eheight(tile));
  1938.                 
  1939.                 tile_mark_valid (tile); /* hmmmmmmm..... */
  1940.               }
  1941.           }
  1942.       }
  1943.  
  1944.   if ((endx - startx) > 0 && (endy - starty) > 0)
  1945.     gimp_image_construct (gimage, 
  1946.               startx, starty, 
  1947.               (endx - startx), (endy - starty), 
  1948.               TRUE);
  1949. }
  1950.  
  1951. void
  1952. gimp_image_validate (TileManager *tm, 
  1953.              Tile        *tile)
  1954. {
  1955.   GimpImage *gimage;
  1956.   gint       x, y;
  1957.   gint       w, h;
  1958.  
  1959.   gimp_add_busy_cursors_until_idle ();
  1960.  
  1961.   /*  Get the gimage from the tilemanager  */
  1962.   gimage = (GimpImage *) tile_manager_get_user_data (tm);
  1963.  
  1964.   /*  Find the coordinates of this tile  */
  1965.   tile_manager_get_tile_coordinates (tm, tile, &x, &y);
  1966.   w = tile_ewidth  (tile);
  1967.   h = tile_eheight (tile);
  1968.   
  1969.   gimp_image_construct (gimage, x, y, w, h, FALSE);
  1970. }
  1971.  
  1972. gint
  1973. gimp_image_get_layer_index (GimpImage *gimage, 
  1974.                 Layer     *layer_arg)
  1975. {
  1976.   Layer  *layer;
  1977.   GSList *layers;
  1978.   gint    index;
  1979.  
  1980.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
  1981.  
  1982.   for (layers = gimage->layers, index = 0; 
  1983.        layers;
  1984.        layers = g_slist_next (layers), index++)
  1985.     {
  1986.       layer = (Layer *) layers->data;
  1987.       if (layer == layer_arg)
  1988.     return index;
  1989.     }
  1990.  
  1991.   return -1;
  1992. }
  1993.  
  1994. gint
  1995. gimp_image_get_channel_index (GimpImage *gimage, 
  1996.                   Channel   *channel_ID)
  1997. {
  1998.   Channel *channel;
  1999.   GSList  *channels;
  2000.   gint     index;
  2001.  
  2002.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
  2003.  
  2004.   for (channels = gimage->channels, index = 0;
  2005.        channels;
  2006.        channels = g_slist_next (channels), index++)
  2007.     {
  2008.       channel = (Channel *) channels->data;
  2009.       if (channel == channel_ID)
  2010.     return index;
  2011.     }
  2012.  
  2013.   return -1;
  2014. }
  2015.  
  2016. Layer *
  2017. gimp_image_get_active_layer (GimpImage *gimage)
  2018. {
  2019.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2020.  
  2021.   return gimage->active_layer;
  2022. }
  2023.  
  2024. Channel *
  2025. gimp_image_get_active_channel (GimpImage *gimage)
  2026. {
  2027.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2028.  
  2029.   return gimage->active_channel;
  2030. }
  2031.  
  2032. Layer *
  2033. gimp_image_get_layer_by_tattoo (GimpImage *gimage, 
  2034.                 Tattoo     tattoo)
  2035. {
  2036.   Layer  *layer;
  2037.   GSList *layers;
  2038.  
  2039.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2040.  
  2041.   for (layers = gimage->layers; 
  2042.        layers; 
  2043.        layers = g_slist_next (layers))
  2044.     {
  2045.       layer = (Layer *) layers->data;
  2046.       if (layer_get_tattoo (layer) == tattoo)
  2047.     return layer;
  2048.     }
  2049.  
  2050.   return NULL;
  2051. }
  2052.  
  2053. Channel *
  2054. gimp_image_get_channel_by_tattoo (GimpImage *gimage, 
  2055.                   Tattoo     tattoo)
  2056. {
  2057.   Channel *channel;
  2058.   GSList  *channels;
  2059.  
  2060.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2061.  
  2062.   for (channels = gimage->channels; 
  2063.        channels; 
  2064.        channels = g_slist_next (channels))
  2065.     {
  2066.       channel = (Channel *) channels->data;
  2067.       if (channel_get_tattoo (channel) == tattoo)
  2068.     return channel;
  2069.     }
  2070.  
  2071.   return NULL;
  2072. }
  2073.  
  2074. Channel *
  2075. gimp_image_get_channel_by_name (GimpImage *gimage, 
  2076.                 char      *name)
  2077. {
  2078.   Channel *channel;
  2079.   GSList  *channels;
  2080.  
  2081.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2082.  
  2083.   for (channels = gimage->channels; 
  2084.        channels; 
  2085.        channels = g_slist_next (channels))
  2086.     {
  2087.       channel = (Channel *) channels->data;
  2088.       if (! strcmp(channel_get_name (channel),name) )
  2089.       return channel;
  2090.     }
  2091.  
  2092.   return NULL;
  2093. }
  2094.  
  2095. gint
  2096. gimp_image_get_component_active (GimpImage   *gimage, 
  2097.                  ChannelType  type)
  2098. {
  2099.   /*  No sanity checking here...  */
  2100.   switch (type)
  2101.     {
  2102.     case RED_CHANNEL:     return gimage->active[RED_PIX]; break;
  2103.     case GREEN_CHANNEL:   return gimage->active[GREEN_PIX]; break;
  2104.     case BLUE_CHANNEL:    return gimage->active[BLUE_PIX]; break;
  2105.     case GRAY_CHANNEL:    return gimage->active[GRAY_PIX]; break;
  2106.     case INDEXED_CHANNEL: return gimage->active[INDEXED_PIX]; break;
  2107.     default: return FALSE; break;
  2108.     }
  2109. }
  2110.  
  2111. gint
  2112. gimp_image_get_component_visible (GimpImage   *gimage, 
  2113.                   ChannelType  type)
  2114. {
  2115.   /*  No sanity checking here...  */
  2116.   switch (type)
  2117.     {
  2118.     case RED_CHANNEL:     return gimage->visible[RED_PIX]; break;
  2119.     case GREEN_CHANNEL:   return gimage->visible[GREEN_PIX]; break;
  2120.     case BLUE_CHANNEL:    return gimage->visible[BLUE_PIX]; break;
  2121.     case GRAY_CHANNEL:    return gimage->visible[GRAY_PIX]; break;
  2122.     case INDEXED_CHANNEL: return gimage->visible[INDEXED_PIX]; break;
  2123.     default: return FALSE; break;
  2124.     }
  2125. }
  2126.  
  2127. Channel *
  2128. gimp_image_get_mask (GimpImage *gimage)
  2129. {
  2130.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2131.  
  2132.   return gimage->selection_mask;
  2133. }
  2134.  
  2135. gboolean
  2136. gimp_image_layer_boundary (GimpImage  *gimage, 
  2137.                BoundSeg  **segs, 
  2138.                int        *num_segs)
  2139. {
  2140.   Layer *layer;
  2141.  
  2142.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  2143.  
  2144.   /*  The second boundary corresponds to the active layer's
  2145.    *  perimeter...
  2146.    */
  2147.   if ((layer = gimage->active_layer))
  2148.     {
  2149.       *segs = layer_boundary (layer, num_segs);
  2150.       return TRUE;
  2151.     }
  2152.   else
  2153.     {
  2154.       *segs = NULL;
  2155.       *num_segs = 0;
  2156.       return FALSE;
  2157.     }
  2158. }
  2159.  
  2160. Layer *
  2161. gimp_image_set_active_layer (GimpImage *gimage, 
  2162.                  Layer     *layer)
  2163. {
  2164.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2165.  
  2166.   /*  First, find the layer in the gimage
  2167.    *  If it isn't valid, find the first layer that is
  2168.    */
  2169.   if (gimp_image_get_layer_index (gimage, layer) == -1)
  2170.     {
  2171.       if (! gimage->layers)
  2172.     return NULL;
  2173.       layer = (Layer *) gimage->layers->data;
  2174.     }
  2175.  
  2176.   if (! layer)
  2177.     return NULL;
  2178.  
  2179.   /*  Configure the layer stack to reflect this change  */
  2180.   gimage->layer_stack = g_slist_remove (gimage->layer_stack, (void *) layer);
  2181.   gimage->layer_stack = g_slist_prepend (gimage->layer_stack, (void *) layer);
  2182.  
  2183.   /*  invalidate the selection boundary because of a layer modification  */
  2184.   layer_invalidate_boundary (layer);
  2185.  
  2186.   /*  Set the active layer  */
  2187.   gimage->active_layer   = layer;
  2188.   gimage->active_channel = NULL;
  2189.  
  2190.   /*  return the layer  */
  2191.   return layer;
  2192. }
  2193.  
  2194. Channel *
  2195. gimp_image_set_active_channel (GimpImage *gimage, 
  2196.                    Channel   *channel)
  2197. {
  2198.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2199.  
  2200.   /*  Not if there is a floating selection  */
  2201.   if (gimp_image_floating_sel (gimage))
  2202.     return NULL;
  2203.  
  2204.   /*  First, find the channel
  2205.    *  If it doesn't exist, find the first channel that does
  2206.    */
  2207.   if (! channel) 
  2208.     {
  2209.       if (! gimage->channels)
  2210.     {
  2211.       gimage->active_channel = NULL;
  2212.       return NULL;
  2213.     }
  2214.       channel = (Channel *) gimage->channels->data;
  2215.     }
  2216.  
  2217.   /*  Set the active channel  */
  2218.   gimage->active_channel = channel;
  2219.  
  2220.   /*  return the channel  */
  2221.   return channel;
  2222. }
  2223.  
  2224. Channel *
  2225. gimp_image_unset_active_channel (GimpImage *gimage)
  2226. {
  2227.   Channel *channel;
  2228.  
  2229.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2230.  
  2231.   /*  make sure there is an active channel  */
  2232.   if (! (channel = gimage->active_channel))
  2233.     return NULL;
  2234.  
  2235.   /*  Set the active channel  */
  2236.   gimage->active_channel = NULL;
  2237.  
  2238.   return channel;
  2239. }
  2240.  
  2241. void
  2242. gimp_image_set_component_active (GimpImage   *gimage, 
  2243.                  ChannelType  type, 
  2244.                  gint         value)
  2245. {
  2246.   /*  No sanity checking here...  */
  2247.   switch (type)
  2248.     {
  2249.     case RED_CHANNEL:     gimage->active[RED_PIX] = value; break;
  2250.     case GREEN_CHANNEL:   gimage->active[GREEN_PIX] = value; break;
  2251.     case BLUE_CHANNEL:    gimage->active[BLUE_PIX] = value; break;
  2252.     case GRAY_CHANNEL:    gimage->active[GRAY_PIX] = value; break;
  2253.     case INDEXED_CHANNEL: gimage->active[INDEXED_PIX] = value; break;
  2254.     case AUXILLARY_CHANNEL: break;
  2255.     }
  2256.  
  2257.   /*  If there is an active channel and we mess with the components,
  2258.    *  the active channel gets unset...
  2259.    */
  2260.   if (type != AUXILLARY_CHANNEL)
  2261.     gimp_image_unset_active_channel (gimage);
  2262. }
  2263.  
  2264. void
  2265. gimp_image_set_component_visible (GimpImage   *gimage, 
  2266.                   ChannelType  type, 
  2267.                   gint         value)
  2268. {
  2269.   /*  No sanity checking here...  */
  2270.   switch (type)
  2271.     {
  2272.     case RED_CHANNEL:     gimage->visible[RED_PIX] = value; break;
  2273.     case GREEN_CHANNEL:   gimage->visible[GREEN_PIX] = value; break;
  2274.     case BLUE_CHANNEL:    gimage->visible[BLUE_PIX] = value; break;
  2275.     case GRAY_CHANNEL:    gimage->visible[GRAY_PIX] = value; break;
  2276.     case INDEXED_CHANNEL: gimage->visible[INDEXED_PIX] = value; break;
  2277.     default: break;
  2278.     }
  2279. }
  2280.  
  2281. Layer *
  2282. gimp_image_pick_correlate_layer (GimpImage *gimage, 
  2283.                  gint       x, 
  2284.                  gint       y)
  2285. {
  2286.   Layer  *layer;
  2287.   GSList *list;
  2288.  
  2289.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2290.  
  2291.   list = gimage->layers;
  2292.   while (list)
  2293.     {
  2294.       layer = (Layer *) list->data;
  2295.       if (layer_pick_correlate (layer, x, y))
  2296.     return layer;
  2297.  
  2298.       list = g_slist_next (list);
  2299.     }
  2300.  
  2301.   return NULL;
  2302. }
  2303.  
  2304.  
  2305.  
  2306. Layer *
  2307. gimp_image_raise_layer (GimpImage *gimage, 
  2308.             Layer     *layer_arg)
  2309. {
  2310.   GSList *list;
  2311.   gint    curpos;
  2312.   
  2313.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2314.   
  2315.   list = gimage->layers;
  2316.   
  2317.   curpos = g_slist_index (list, layer_arg);
  2318.   if (curpos < 0)
  2319.     return NULL;  /* invalid "layer_arg" */
  2320.   
  2321.   /* is this the top layer already? */
  2322.   if (curpos == 0)
  2323.     {
  2324.       g_message (_("Layer cannot be raised any further"));
  2325.       return NULL;
  2326.     }
  2327.   
  2328.   return gimp_image_position_layer (gimage, layer_arg, curpos-1, TRUE);
  2329. }
  2330.  
  2331.  
  2332. Layer *
  2333. gimp_image_lower_layer (GimpImage *gimage, 
  2334.             Layer     *layer_arg)
  2335. {
  2336.   GSList *list;
  2337.   gint    curpos;
  2338.   guint   length;
  2339.   
  2340.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2341.   
  2342.   list = gimage->layers;
  2343.   
  2344.   curpos = g_slist_index (list, layer_arg);
  2345.   if (curpos < 0)
  2346.     return NULL;  /* invalid "layer_arg" */
  2347.   
  2348.   /* is this the bottom layer already? */
  2349.   length = g_slist_length (list);
  2350.   if (curpos == length-1)
  2351.     {
  2352.       g_message (_("Layer cannot be lowered any further"));
  2353.       return NULL;
  2354.     }
  2355.   
  2356.   return gimp_image_position_layer (gimage, layer_arg, curpos+1, TRUE);
  2357. }
  2358.  
  2359.  
  2360.  
  2361. Layer *
  2362. gimp_image_raise_layer_to_top (GimpImage *gimage, 
  2363.                    Layer     *layer_arg)
  2364. {
  2365.   GSList *list;
  2366.   gint    curpos;
  2367.   
  2368.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2369.  
  2370.   list = gimage->layers;
  2371.   
  2372.   curpos = g_slist_index (list, layer_arg);
  2373.   if (curpos < 0)
  2374.     return NULL;
  2375.   
  2376.   if (curpos == 0)
  2377.     {
  2378.       g_message (_("Layer is already on top"));
  2379.       return NULL;
  2380.     }
  2381.   
  2382.   if (! layer_has_alpha (layer_arg))
  2383.     {
  2384.       g_message (_("Can't raise Layer without alpha"));
  2385.       return NULL;
  2386.     }
  2387.   
  2388.   return gimp_image_position_layer (gimage, layer_arg, 0, TRUE);
  2389. }
  2390.  
  2391.  
  2392. Layer *
  2393. gimp_image_lower_layer_to_bottom (GimpImage *gimage, 
  2394.                   Layer     *layer_arg)
  2395. {
  2396.   GSList *list;
  2397.   gint    curpos;
  2398.   guint   length;
  2399.   
  2400.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2401.  
  2402.   list = gimage->layers;
  2403.  
  2404.   curpos = g_slist_index (list, layer_arg);
  2405.   if (curpos < 0)
  2406.     return NULL;
  2407.   
  2408.   length = g_slist_length (list);
  2409.   
  2410.   if (curpos == length-1)
  2411.     {
  2412.       g_message (_("Layer is already on bottom"));
  2413.       return NULL;
  2414.     }
  2415.   
  2416.   return gimp_image_position_layer (gimage, layer_arg, length-1, TRUE);
  2417. }
  2418.  
  2419.  
  2420. Layer *
  2421. gimp_image_position_layer (GimpImage *gimage, 
  2422.                Layer     *layer_arg,
  2423.                gint       new_index,
  2424.                gboolean   push_undo)
  2425. {
  2426.   Layer  *layer;
  2427.   GSList *list;
  2428.   GSList *next;
  2429.   gint    x_min, y_min, x_max, y_max;
  2430.   gint    off_x, off_y;
  2431.   gint    index;
  2432.   gint    list_length;
  2433.  
  2434.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2435.  
  2436.   list = gimage->layers;
  2437.   list_length = g_slist_length (list);
  2438.  
  2439.   next = NULL;
  2440.   layer = NULL;
  2441.   index = 0;
  2442.  
  2443.   /* find layer_arg */
  2444.   index = g_slist_index (list, layer_arg);
  2445.   if (index < 0)
  2446.     {
  2447.       /* The requested layer was not found in the layerstack
  2448.        * Return without changing anything
  2449.        */
  2450.       return NULL;
  2451.     }
  2452.  
  2453.   if (new_index < 0)
  2454.     new_index = 0;
  2455.  
  2456.   if (new_index >= list_length)
  2457.     new_index = list_length - 1;
  2458.  
  2459.   if (new_index == index)
  2460.     return NULL;
  2461.  
  2462.   /* check if we want to move it below a bottom layer without alpha */
  2463.   layer = (Layer *) g_slist_last (list)->data;
  2464.   if (new_index == list_length - 1 &&
  2465.       !layer_has_alpha (layer))
  2466.     {
  2467.       g_message (_("BG has no alpha, layer was placed above"));
  2468.       new_index--;
  2469.     }
  2470.  
  2471.   if (push_undo)
  2472.       undo_push_layer_reposition (gimage, layer_arg);
  2473.  
  2474.   list = g_slist_remove (gimage->layers, layer_arg);
  2475.   gimage->layers = g_slist_insert (list, layer_arg, new_index);
  2476.  
  2477.   /* update the affected area (== area of layer_arg) */
  2478.   drawable_offsets (GIMP_DRAWABLE(layer_arg), &off_x, &off_y);
  2479.   x_min = off_x;
  2480.   y_min = off_y;
  2481.   x_max = off_x + drawable_width (GIMP_DRAWABLE (layer_arg));
  2482.   y_max = off_y + drawable_height (GIMP_DRAWABLE (layer_arg));
  2483.   gtk_signal_emit (GTK_OBJECT (gimage),
  2484.            gimp_image_signals[REPAINT],
  2485.            x_min, y_min, x_max, y_max);
  2486.  
  2487.   /*  invalidate the composite preview  */
  2488.   gimp_image_invalidate_preview (gimage);
  2489.  
  2490.   return layer_arg;
  2491. }
  2492.  
  2493. Layer *
  2494. gimp_image_merge_visible_layers (GimpImage *gimage, 
  2495.                  MergeType  merge_type)
  2496. {
  2497.   GSList   *layer_list;
  2498.   GSList   *merge_list       = NULL;
  2499.   gboolean  had_floating_sel = FALSE;
  2500.   Layer    *layer            = NULL;
  2501.  
  2502.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2503.  
  2504.   /* if there's a floating selection, anchor it */
  2505.   if (gimp_image_floating_sel (gimage))
  2506.     {
  2507.       floating_sel_anchor (gimage->floating_sel);
  2508.       had_floating_sel = TRUE;
  2509.     }
  2510.  
  2511.   layer_list = gimage->layers;
  2512.   while (layer_list)
  2513.     {
  2514.       layer = (Layer *) layer_list->data;
  2515.       if (drawable_visible (GIMP_DRAWABLE(layer)))
  2516.     merge_list = g_slist_append (merge_list, layer);
  2517.  
  2518.       layer_list = g_slist_next (layer_list);
  2519.     }
  2520.  
  2521.   if (merge_list && merge_list->next)
  2522.     {
  2523.       gimp_add_busy_cursors ();
  2524.       layer = gimp_image_merge_layers (gimage, merge_list, merge_type);
  2525.       g_slist_free (merge_list);
  2526.       gimp_remove_busy_cursors (NULL);
  2527.  
  2528.       return layer;
  2529.     }
  2530.   else
  2531.     {
  2532.       g_slist_free (merge_list);
  2533.  
  2534.       /* If there was a floating selection, we have done something.
  2535.      No need to warn the user. Return the active layer instead */
  2536.       if (had_floating_sel)
  2537.     return layer;
  2538.       else
  2539.     g_message (_("There are not enough visible layers for a merge.\nThere must be at least two."));
  2540.  
  2541.       return NULL;
  2542.     }
  2543. }
  2544.  
  2545. Layer *
  2546. gimp_image_flatten (GimpImage *gimage)
  2547. {
  2548.   GSList *layer_list;
  2549.   GSList *merge_list = NULL;
  2550.   Layer  *layer;
  2551.  
  2552.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2553.  
  2554.   gimp_add_busy_cursors ();
  2555.  
  2556.   /* if there's a floating selection, anchor it */
  2557.   if (gimp_image_floating_sel (gimage))
  2558.     floating_sel_anchor (gimage->floating_sel);
  2559.  
  2560.   layer_list = gimage->layers;
  2561.   while (layer_list)
  2562.     {
  2563.       layer = (Layer *) layer_list->data;
  2564.       if (drawable_visible (GIMP_DRAWABLE (layer)))
  2565.     merge_list = g_slist_append (merge_list, layer);
  2566.  
  2567.       layer_list = g_slist_next (layer_list);
  2568.     }
  2569.  
  2570.   layer = gimp_image_merge_layers (gimage, merge_list, FLATTEN_IMAGE);
  2571.   g_slist_free (merge_list);
  2572.  
  2573.   gimp_remove_busy_cursors (NULL);
  2574.  
  2575.   return layer;
  2576. }
  2577.  
  2578. Layer *
  2579. gimp_image_merge_down (GimpImage *gimage,
  2580.                Layer     *current_layer,
  2581.                MergeType  merge_type)
  2582. {
  2583.   GSList *layer_list;
  2584.   GSList *merge_list = NULL;
  2585.   Layer  *layer      = NULL;
  2586.   
  2587.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2588.  
  2589.   layer_list = gimage->layers;
  2590.   while (layer_list)
  2591.     {
  2592.       layer = (Layer *) layer_list->data;
  2593.       if (layer == current_layer)
  2594.     {
  2595.       layer_list = g_slist_next (layer_list);
  2596.       while (layer_list)
  2597.         {
  2598.           layer = (Layer *) layer_list->data;
  2599.           if (drawable_visible (GIMP_DRAWABLE (layer)))
  2600.         {
  2601.           merge_list = g_slist_append (merge_list, layer);
  2602.           layer_list = NULL;
  2603.         }
  2604.           else 
  2605.         layer_list = g_slist_next (layer_list);
  2606.         }
  2607.       merge_list = g_slist_prepend (merge_list, current_layer);
  2608.     }
  2609.       else
  2610.     layer_list = g_slist_next (layer_list);
  2611.     }
  2612.   
  2613.   if (merge_list && merge_list->next)
  2614.     {
  2615.       gimp_add_busy_cursors ();
  2616.       layer = gimp_image_merge_layers (gimage, merge_list, merge_type);
  2617.       g_slist_free (merge_list);
  2618.       gimp_remove_busy_cursors (NULL);
  2619.       return layer;
  2620.     }
  2621.   else 
  2622.     {
  2623.       g_message (_("There are not enough visible layers for a merge down."));
  2624.       g_slist_free (merge_list);
  2625.       return NULL;
  2626.     }
  2627. }
  2628.  
  2629. Layer *
  2630. gimp_image_merge_layers (GimpImage *gimage, 
  2631.              GSList    *merge_list, 
  2632.              MergeType  merge_type)
  2633. {
  2634.   GSList           *reverse_list = NULL;
  2635.   PixelRegion       src1PR, src2PR, maskPR;
  2636.   PixelRegion      *mask;
  2637.   Layer            *merge_layer;
  2638.   Layer            *layer;
  2639.   Layer            *bottom;
  2640.   LayerModeEffects  bottom_mode;
  2641.   guchar            bg[4] = {0, 0, 0, 0};
  2642.   GimpImageType     type;
  2643.   gint              count;
  2644.   gint              x1, y1, x2, y2;
  2645.   gint              x3, y3, x4, y4;
  2646.   gint              operation;
  2647.   gint              position;
  2648.   gint              active[MAX_CHANNELS] = {1, 1, 1, 1};
  2649.   gint              off_x, off_y;
  2650.   gchar            *name;
  2651.  
  2652.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2653.  
  2654.   layer = NULL;
  2655.   type  = RGBA_GIMAGE;
  2656.   x1 = y1 = x2 = y2 = 0;
  2657.   bottom = NULL;
  2658.   bottom_mode = NORMAL_MODE;
  2659.  
  2660.   /*  Get the layer extents  */
  2661.   count = 0;
  2662.   while (merge_list)
  2663.     {
  2664.       layer = (Layer *) merge_list->data;
  2665.       drawable_offsets (GIMP_DRAWABLE (layer), &off_x, &off_y);
  2666.  
  2667.       switch (merge_type)
  2668.     {
  2669.     case EXPAND_AS_NECESSARY:
  2670.     case CLIP_TO_IMAGE:
  2671.       if (!count)
  2672.         {
  2673.           x1 = off_x;
  2674.           y1 = off_y;
  2675.           x2 = off_x + drawable_width (GIMP_DRAWABLE(layer));
  2676.           y2 = off_y + drawable_height (GIMP_DRAWABLE(layer));
  2677.         }
  2678.       else
  2679.         {
  2680.           if (off_x < x1)
  2681.         x1 = off_x;
  2682.           if (off_y < y1)
  2683.         y1 = off_y;
  2684.           if ((off_x + drawable_width (GIMP_DRAWABLE(layer))) > x2)
  2685.         x2 = (off_x + drawable_width (GIMP_DRAWABLE(layer)));
  2686.           if ((off_y + drawable_height (GIMP_DRAWABLE(layer))) > y2)
  2687.         y2 = (off_y + drawable_height (GIMP_DRAWABLE(layer)));
  2688.         }
  2689.       if (merge_type == CLIP_TO_IMAGE)
  2690.         {
  2691.           x1 = CLAMP (x1, 0, gimage->width);
  2692.           y1 = CLAMP (y1, 0, gimage->height);
  2693.           x2 = CLAMP (x2, 0, gimage->width);
  2694.           y2 = CLAMP (y2, 0, gimage->height);
  2695.         }
  2696.       break;
  2697.  
  2698.     case CLIP_TO_BOTTOM_LAYER:
  2699.       if (merge_list->next == NULL)
  2700.         {
  2701.           x1 = off_x;
  2702.           y1 = off_y;
  2703.           x2 = off_x + drawable_width (GIMP_DRAWABLE(layer));
  2704.           y2 = off_y + drawable_height (GIMP_DRAWABLE(layer));
  2705.         }
  2706.       break;
  2707.  
  2708.     case FLATTEN_IMAGE:
  2709.       if (merge_list->next == NULL)
  2710.         {
  2711.           x1 = 0;
  2712.           y1 = 0;
  2713.           x2 = gimage->width;
  2714.           y2 = gimage->height;
  2715.         }
  2716.       break;
  2717.     }
  2718.  
  2719.       count ++;
  2720.       reverse_list = g_slist_prepend (reverse_list, layer);
  2721.       merge_list = g_slist_next (merge_list);
  2722.     }
  2723.  
  2724.   if ((x2 - x1) == 0 || (y2 - y1) == 0)
  2725.     return NULL;
  2726.  
  2727.   /*  Start a merge undo group  */
  2728.   undo_push_group_start (gimage, LAYER_MERGE_UNDO);
  2729.  
  2730.   name = g_strdup (drawable_get_name (GIMP_DRAWABLE(layer)));
  2731.  
  2732.   if (merge_type == FLATTEN_IMAGE ||
  2733.       drawable_type (GIMP_DRAWABLE (layer)) == INDEXED_GIMAGE)
  2734.     {
  2735.       switch (gimp_image_base_type (gimage))
  2736.     {
  2737.     case RGB: type = RGB_GIMAGE; break;
  2738.     case GRAY: type = GRAY_GIMAGE; break;
  2739.     case INDEXED: type = INDEXED_GIMAGE; break;
  2740.     }
  2741.  
  2742.       merge_layer = layer_new (gimage, (x2 - x1), (y2 - y1),
  2743.                    type, drawable_get_name (GIMP_DRAWABLE(layer)),
  2744.                    OPAQUE_OPACITY, NORMAL_MODE);
  2745.       if (!merge_layer)
  2746.     {
  2747.       g_message ("gimp_image_merge_layers: could not allocate merge layer");
  2748.       return NULL;
  2749.     }
  2750.  
  2751.       GIMP_DRAWABLE (merge_layer)->offset_x = x1;
  2752.       GIMP_DRAWABLE (merge_layer)->offset_y = y1;
  2753.  
  2754.       /*  get the background for compositing  */
  2755.       gimp_image_get_background (gimage, GIMP_DRAWABLE (merge_layer), bg);
  2756.  
  2757.       /*  init the pixel region  */
  2758.       pixel_region_init (&src1PR, 
  2759.              drawable_data (GIMP_DRAWABLE (merge_layer)), 
  2760.              0, 0, 
  2761.              gimage->width, gimage->height, 
  2762.              TRUE);
  2763.  
  2764.       /*  set the region to the background color  */
  2765.       color_region (&src1PR, bg);
  2766.  
  2767.       position = 0;
  2768.     }
  2769.   else
  2770.     {
  2771.       /*  The final merged layer inherits the name of the bottom most layer
  2772.        *  and the resulting layer has an alpha channel
  2773.        *  whether or not the original did
  2774.        *  Opacity is set to 100% and the MODE is set to normal
  2775.        */
  2776.  
  2777.       merge_layer = layer_new (gimage, (x2 - x1), (y2 - y1),
  2778.                    drawable_type_with_alpha (GIMP_DRAWABLE(layer)),
  2779.                    "merged layer",
  2780.                    OPAQUE_OPACITY, NORMAL_MODE);
  2781.  
  2782.       if (!merge_layer)
  2783.     {
  2784.       g_message ("gimp_image_merge_layers: could not allocate merge layer");
  2785.       return NULL;
  2786.     }
  2787.  
  2788.       GIMP_DRAWABLE (merge_layer)->offset_x = x1;
  2789.       GIMP_DRAWABLE (merge_layer)->offset_y = y1;
  2790.  
  2791.       /*  Set the layer to transparent  */
  2792.       pixel_region_init (&src1PR, 
  2793.              drawable_data (GIMP_DRAWABLE(merge_layer)), 
  2794.              0, 0, 
  2795.              (x2 - x1), (y2 - y1), 
  2796.              TRUE);
  2797.  
  2798.       /*  set the region to 0's  */
  2799.       color_region (&src1PR, bg);
  2800.  
  2801.       /*  Find the index in the layer list of the bottom layer--we need this
  2802.        *  in order to add the final, merged layer to the layer list correctly
  2803.        */
  2804.       layer = (Layer *) reverse_list->data;
  2805.       position = 
  2806.     g_slist_length (gimage->layers) - 
  2807.     gimp_image_get_layer_index (gimage, layer);
  2808.       
  2809.       /* set the mode of the bottom layer to normal so that the contents
  2810.        *  aren't lost when merging with the all-alpha merge_layer
  2811.        *  Keep a pointer to it so that we can set the mode right after it's
  2812.        *  been merged so that undo works correctly.
  2813.        */
  2814.       bottom = layer;
  2815.       bottom_mode = bottom->mode;
  2816.  
  2817.       /* DISSOLVE_MODE is special since it is the only mode that does not
  2818.        *  work on the projection with the lower layer, but only locally on
  2819.        *  the layers alpha channel. 
  2820.        */
  2821.       if (bottom->mode != DISSOLVE_MODE)
  2822.     bottom->mode = NORMAL_MODE;
  2823.     }
  2824.  
  2825.   /* Copy the tattoo and parasites of the bottom layer to the new layer */
  2826.   layer_set_tattoo(merge_layer, layer_get_tattoo(layer));
  2827.   GIMP_DRAWABLE(merge_layer)->parasites =
  2828.     parasite_list_copy (GIMP_DRAWABLE(layer)->parasites);
  2829.  
  2830.   while (reverse_list)
  2831.     {
  2832.       layer = (Layer *) reverse_list->data;
  2833.  
  2834.       /*  determine what sort of operation is being attempted and
  2835.        *  if it's actually legal...
  2836.        */
  2837.       operation = valid_combinations [drawable_type (GIMP_DRAWABLE(merge_layer))][drawable_bytes (GIMP_DRAWABLE(layer))];
  2838.       if (operation == -1)
  2839.     {
  2840.       g_message ("gimp_image_merge_layers attempting to merge incompatible layers\n");
  2841.       return NULL;
  2842.     }
  2843.  
  2844.       drawable_offsets (GIMP_DRAWABLE(layer), &off_x, &off_y);
  2845.       x3 = CLAMP (off_x, x1, x2);
  2846.       y3 = CLAMP (off_y, y1, y2);
  2847.       x4 = CLAMP (off_x + drawable_width (GIMP_DRAWABLE(layer)), x1, x2);
  2848.       y4 = CLAMP (off_y + drawable_height (GIMP_DRAWABLE(layer)), y1, y2);
  2849.  
  2850.       /* configure the pixel regions  */
  2851.       pixel_region_init (&src1PR, 
  2852.              drawable_data (GIMP_DRAWABLE(merge_layer)), 
  2853.              (x3 - x1), (y3 - y1), (x4 - x3), (y4 - y3), 
  2854.              TRUE);
  2855.       pixel_region_init (&src2PR, 
  2856.              drawable_data (GIMP_DRAWABLE(layer)), 
  2857.              (x3 - off_x), (y3 - off_y),
  2858.              (x4 - x3), (y4 - y3), 
  2859.              FALSE);
  2860.  
  2861.       if (layer->mask)
  2862.     {
  2863.       pixel_region_init (&maskPR, 
  2864.                  drawable_data (GIMP_DRAWABLE(layer->mask)), 
  2865.                  (x3 - off_x), (y3 - off_y),
  2866.                  (x4 - x3), (y4 - y3), 
  2867.                  FALSE);
  2868.       mask = &maskPR;
  2869.     }
  2870.       else
  2871.     mask = NULL;
  2872.  
  2873.       combine_regions (&src1PR, &src2PR, &src1PR, mask, NULL,
  2874.                layer->opacity, layer->mode, active, operation);
  2875.  
  2876.       gimp_image_remove_layer (gimage, layer);
  2877.       reverse_list = g_slist_next (reverse_list);
  2878.     }
  2879.  
  2880.   /* Save old mode in undo */
  2881.   if (bottom)
  2882.     bottom->mode = bottom_mode;
  2883.  
  2884.   g_slist_free (reverse_list);
  2885.  
  2886.   /*  if the type is flatten, remove all the remaining layers  */
  2887.   if (merge_type == FLATTEN_IMAGE)
  2888.     {
  2889.       merge_list = gimage->layers;
  2890.       while (merge_list)
  2891.     {
  2892.       layer = (Layer *) merge_list->data;
  2893.       merge_list = g_slist_next (merge_list);
  2894.       gimp_image_remove_layer (gimage, layer);
  2895.     }
  2896.  
  2897.       gimp_image_add_layer (gimage, merge_layer, position);
  2898.     }
  2899.   else
  2900.     {
  2901.       /*  Add the layer to the gimage  */
  2902.       gimp_image_add_layer (gimage, merge_layer,
  2903.                 (g_slist_length (gimage->layers) - position + 1));
  2904.     }
  2905.  
  2906.   /* set the name after the original layers have been removed so we don't
  2907.      end up with #2 appended to the name */
  2908.   drawable_set_name (GIMP_DRAWABLE(merge_layer), name);
  2909.   g_free (name);
  2910.  
  2911.   /*  End the merge undo group  */
  2912.   undo_push_group_end (gimage);
  2913.  
  2914.   /*  Update the gimage  */
  2915.   GIMP_DRAWABLE(merge_layer)->visible = TRUE;
  2916.  
  2917.   gtk_signal_emit (GTK_OBJECT(gimage), gimp_image_signals[RESTRUCTURE]);
  2918.  
  2919.   drawable_update (GIMP_DRAWABLE(merge_layer), 
  2920.            0, 0, 
  2921.            drawable_width (GIMP_DRAWABLE(merge_layer)), 
  2922.            drawable_height (GIMP_DRAWABLE(merge_layer)));
  2923.  
  2924.   /*reinit_layer_idlerender (gimage, merge_layer);*/
  2925.  
  2926.   return merge_layer;
  2927. }
  2928.  
  2929. Layer *
  2930. gimp_image_add_layer (GimpImage *gimage, 
  2931.               Layer     *float_layer, 
  2932.               gint       position)
  2933. {
  2934.   LayerUndo *lu;
  2935.   GSList    *ll;
  2936.  
  2937.   if (GIMP_DRAWABLE (float_layer)->gimage != NULL && 
  2938.       GIMP_DRAWABLE (float_layer)->gimage != gimage) 
  2939.     {
  2940.       g_message ("gimp_image_add_layer: attempt to add layer to wrong image");
  2941.       return NULL;
  2942.     }
  2943.  
  2944.   for (ll = gimage->layers; ll; ll = g_slist_next (ll)) 
  2945.     if (ll->data == float_layer) 
  2946.       {
  2947.     g_message ("gimp_image_add_layer: trying to add layer to image twice");
  2948.     return NULL;
  2949.       }
  2950.  
  2951.   /*  Prepare a layer undo and push it  */
  2952.   lu = g_new (LayerUndo, 1);
  2953.   lu->layer         = float_layer;
  2954.   lu->prev_position = 0;
  2955.   lu->prev_layer    = gimage->active_layer;
  2956.   undo_push_layer (gimage, LAYER_ADD_UNDO, lu);
  2957.  
  2958.   /*  If the layer is a floating selection, set the ID  */
  2959.   if (layer_is_floating_sel (float_layer))
  2960.     gimage->floating_sel = float_layer;
  2961.  
  2962.   /*  let the layer know about the gimage  */
  2963.   gimp_drawable_set_gimage (GIMP_DRAWABLE (float_layer), gimage);
  2964.   
  2965.   /*  If the layer has a mask, set the mask's gimage and layer */
  2966.   if (float_layer->mask)
  2967.     {
  2968.       gimp_drawable_set_gimage (GIMP_DRAWABLE (float_layer->mask), gimage);
  2969.     }
  2970.  
  2971.   /*  add the layer to the list at the specified position  */
  2972.   if (position == -1)
  2973.     position = gimp_image_get_layer_index (gimage, gimage->active_layer);
  2974.  
  2975.   if (position != -1)
  2976.     {
  2977.       /*  If there is a floating selection (and this isn't it!),
  2978.        *  make sure the insert position is greater than 0
  2979.        */
  2980.       if (gimp_image_floating_sel (gimage) &&
  2981.       (gimage->floating_sel != float_layer) && position == 0)
  2982.     position = 1;
  2983.  
  2984.       gimage->layers = 
  2985.     g_slist_insert (gimage->layers, layer_ref (float_layer), position);
  2986.     }
  2987.   else
  2988.     {
  2989.       gimage->layers = 
  2990.     g_slist_prepend (gimage->layers, layer_ref (float_layer));
  2991.     }
  2992.  
  2993.   gimage->layer_stack = g_slist_prepend (gimage->layer_stack, float_layer);
  2994.  
  2995.   /*  notify the layers dialog of the currently active layer  */
  2996.   gimp_image_set_active_layer (gimage, float_layer);
  2997.  
  2998.   /*  update the new layer's area  */
  2999.   drawable_update (GIMP_DRAWABLE (float_layer),
  3000.            0, 0,
  3001.            drawable_width  (GIMP_DRAWABLE (float_layer)),
  3002.            drawable_height (GIMP_DRAWABLE (float_layer)));
  3003.  
  3004.   /*  invalidate the composite preview  */
  3005.   gimp_image_invalidate_preview (gimage);
  3006.  
  3007.   return float_layer;
  3008. }
  3009.  
  3010. Layer *
  3011. gimp_image_remove_layer (GimpImage *gimage, 
  3012.              Layer     *layer)
  3013. {
  3014.   LayerUndo *lu;
  3015.  
  3016.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3017.  
  3018.   if (layer)
  3019.     {
  3020.       /*  Prepare a layer undo--push it at the end  */
  3021.       lu = g_new (LayerUndo, 1);
  3022.       lu->layer         = layer;
  3023.       lu->prev_position = gimp_image_get_layer_index (gimage, layer);
  3024.       lu->prev_layer    = layer;
  3025.  
  3026.       gimage->layers = g_slist_remove (gimage->layers, layer);
  3027.       gimage->layer_stack = g_slist_remove (gimage->layer_stack, layer);
  3028.  
  3029.       /*  If this was the floating selection, reset the fs pointer  */
  3030.       if (gimage->floating_sel == layer)
  3031.     {
  3032.       gimage->floating_sel = NULL;
  3033.  
  3034.       floating_sel_reset (layer);
  3035.     }
  3036.       if (gimage->active_layer == layer)
  3037.     {
  3038.       if (gimage->layers)
  3039.         gimage_set_active_layer (gimage, gimage->layer_stack->data);
  3040.       else
  3041.         gimage->active_layer = NULL;
  3042.     }
  3043.  
  3044.       /* Send out REMOVED signal from layer */
  3045.       layer_removed (layer, gimage);
  3046.  
  3047.       /*  Push the layer undo--It is important it goes here since layer might
  3048.        *   be immediately destroyed if the undo push fails
  3049.        */
  3050.       undo_push_layer (gimage, LAYER_REMOVE_UNDO, lu);
  3051.  
  3052.       /*  invalidate the composite preview  */
  3053.       gimp_image_invalidate_preview (gimage);
  3054.       gdisplays_update_full(gimage);
  3055.  
  3056.       return NULL;
  3057.     }
  3058.   else
  3059.     return NULL;
  3060. }
  3061.  
  3062. LayerMask *
  3063. gimp_image_add_layer_mask (GimpImage *gimage, 
  3064.                Layer     *layer, 
  3065.                LayerMask *mask)
  3066. {
  3067.   LayerMaskUndo *lmu;
  3068.  
  3069.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3070.  
  3071.   if (layer->mask != NULL)
  3072.     {
  3073.       g_message(_("Unable to add a layer mask since\nthe layer already has one."));
  3074.       return (NULL);
  3075.     }
  3076.  
  3077.   if (drawable_indexed (GIMP_DRAWABLE (layer)))
  3078.     {
  3079.       g_message(_("Unable to add a layer mask to a\nlayer in an indexed image."));
  3080.       return (NULL);
  3081.     }
  3082.  
  3083.   if (! layer_has_alpha (layer))
  3084.     {
  3085.       g_message (_("Cannot add layer mask to a layer\nwith no alpha channel."));
  3086.       return (NULL);
  3087.     }
  3088.  
  3089.   if ((drawable_width (GIMP_DRAWABLE(layer)) !=
  3090.        drawable_width (GIMP_DRAWABLE(mask))) ||
  3091.       (drawable_height (GIMP_DRAWABLE(layer)) !=
  3092.        drawable_height (GIMP_DRAWABLE(mask))))
  3093.     {
  3094.       g_message(_("Cannot add layer mask of different dimensions than specified layer."));
  3095.       return NULL;
  3096.     }
  3097.  
  3098.   layer_add_mask (layer, mask);
  3099.  
  3100.   /*  Prepare a layer undo and push it  */
  3101.   lmu = g_new (LayerMaskUndo, 1);
  3102.   lmu->layer      = layer;
  3103.   lmu->mask       = mask;
  3104.   lmu->apply_mask = layer->apply_mask;
  3105.   lmu->edit_mask  = layer->edit_mask;
  3106.   lmu->show_mask  = layer->show_mask;
  3107.   undo_push_layer_mask (gimage, LAYER_MASK_ADD_UNDO, lmu);
  3108.   
  3109.   return mask;
  3110. }
  3111.  
  3112. Channel *
  3113. gimp_image_remove_layer_mask (GimpImage     *gimage, 
  3114.                   Layer         *layer, 
  3115.                   MaskApplyMode  mode)
  3116. {
  3117.   LayerMaskUndo *lmu;
  3118.   gint           off_x;
  3119.   gint           off_y;
  3120.  
  3121.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage) && GIMP_IS_LAYER (layer), NULL);
  3122.  
  3123.   if (!layer || !layer->mask)
  3124.     return NULL;
  3125.  
  3126.   /*  Start an undo group  */
  3127.   undo_push_group_start (gimage, LAYER_APPLY_MASK_UNDO);
  3128.  
  3129.   /*  Prepare a layer mask undo--push it below  */
  3130.   lmu = g_new (LayerMaskUndo, 1);
  3131.   lmu->layer      = layer;
  3132.   lmu->mask       = layer->mask;
  3133.   lmu->mode       = mode;
  3134.   lmu->apply_mask = layer->apply_mask;
  3135.   lmu->edit_mask  = layer->edit_mask;
  3136.   lmu->show_mask  = layer->show_mask;
  3137.  
  3138.   layer_apply_mask (layer, mode);
  3139.  
  3140.   /*  Push the undo--Important to do it here, AFTER the call
  3141.    *   to layer_apply_mask, in case the undo push fails and the
  3142.    *   mask is deleted
  3143.    */
  3144.   undo_push_layer_mask (gimage, LAYER_MASK_REMOVE_UNDO, lmu);
  3145.  
  3146.   /*  end the undo group  */
  3147.   undo_push_group_end (gimage);
  3148.  
  3149.   /*  If applying actually changed the view  */
  3150.   if ((mode == APPLY   && (!lmu->apply_mask || lmu->show_mask)) ||
  3151.       (mode == DISCARD && ( lmu->apply_mask || lmu->show_mask)))
  3152.     {
  3153.       gimp_image_invalidate_preview (gimage);
  3154.  
  3155.       drawable_offsets (GIMP_DRAWABLE (layer), &off_x, &off_y);
  3156.       gtk_signal_emit (GTK_OBJECT (gimage),
  3157.                gimp_image_signals[REPAINT],
  3158.                off_x, off_y,
  3159.                drawable_width  (GIMP_DRAWABLE (layer)),
  3160.                drawable_height (GIMP_DRAWABLE (layer)));
  3161.     }
  3162.  
  3163.   return NULL;
  3164. }
  3165.  
  3166. Channel *
  3167. gimp_image_raise_channel (GimpImage *gimage, 
  3168.               Channel   *channel_arg)
  3169. {
  3170.   Channel *channel;
  3171.   Channel *prev_channel;
  3172.   GSList  *list;
  3173.   GSList  *prev;
  3174.   gint     index = -1;
  3175.  
  3176.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3177.  
  3178.   list = gimage->channels;
  3179.   prev = NULL;
  3180.   prev_channel = NULL;
  3181.  
  3182.   while (list)
  3183.     {
  3184.       channel = (Channel *) list->data;
  3185.       if (prev)
  3186.     prev_channel = (Channel *) prev->data;
  3187.  
  3188.       if (channel == channel_arg)
  3189.     {
  3190.       if (prev)
  3191.         {
  3192.           list->data = prev_channel;
  3193.           prev->data = channel;
  3194.           drawable_update (GIMP_DRAWABLE (channel),
  3195.                    0, 0,
  3196.                    drawable_width  (GIMP_DRAWABLE (channel)),
  3197.                    drawable_height (GIMP_DRAWABLE (channel)));
  3198.           return prev_channel;
  3199.         }
  3200.       else
  3201.         {
  3202.           g_message (_("Channel cannot be raised any further"));
  3203.           return NULL;
  3204.         }
  3205.     }
  3206.  
  3207.       prev = list;
  3208.       index++;
  3209.       list = g_slist_next (list);
  3210.     }
  3211.  
  3212.   return NULL;
  3213. }
  3214.  
  3215. Channel *
  3216. gimp_image_lower_channel (GimpImage *gimage, 
  3217.               Channel   *channel_arg)
  3218. {
  3219.   Channel *channel;
  3220.   Channel *next_channel;
  3221.   GSList  *list;
  3222.   GSList  *next;
  3223.   gint     index = 0;
  3224.  
  3225.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3226.  
  3227.   list = gimage->channels;
  3228.   next_channel = NULL;
  3229.  
  3230.   while (list)
  3231.     {
  3232.       channel = (Channel *) list->data;
  3233.       next = g_slist_next (list);
  3234.  
  3235.       if (next)
  3236.     next_channel = (Channel *) next->data;
  3237.       index++;
  3238.  
  3239.       if (channel == channel_arg)
  3240.     {
  3241.       if (next)
  3242.         {
  3243.           list->data = next_channel;
  3244.           next->data = channel;
  3245.           drawable_update (GIMP_DRAWABLE (channel),
  3246.                    0, 0,
  3247.                    drawable_width  (GIMP_DRAWABLE (channel)),
  3248.                    drawable_height (GIMP_DRAWABLE (channel)));
  3249.           return next_channel;
  3250.         }
  3251.       else
  3252.         {
  3253.           g_message (_("Channel cannot be lowered any further"));
  3254.           return NULL;
  3255.         }
  3256.     }
  3257.  
  3258.       list = next;
  3259.     }
  3260.  
  3261.   return NULL;
  3262. }
  3263.  
  3264. Channel *
  3265. gimp_image_position_channel (GimpImage *gimage, 
  3266.                  Channel   *channel_arg,
  3267.                  gint       new_index)
  3268. {
  3269.   Channel *channel;
  3270.   GSList  *list;
  3271.   GSList  *next;
  3272.   gint     index;
  3273.   gint     list_length;
  3274.  
  3275.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3276.  
  3277.   next    = NULL;
  3278.   channel = NULL;
  3279.  
  3280.   /* find channel_arg */
  3281.   for (list = gimage->channels, index = 0, list_length = g_slist_length (list);
  3282.        list;
  3283.        list = g_slist_next (list), index++)
  3284.     {
  3285.       channel = (Channel *) list->data;
  3286.       if (channel == channel_arg)
  3287.     {
  3288.       break;
  3289.     }
  3290.     }
  3291.  
  3292.   if (channel != channel_arg)
  3293.     {
  3294.       /* The requested channel was not found in the channel stack
  3295.        * Return without changing anything
  3296.        */
  3297.       return NULL;
  3298.     }
  3299.  
  3300.   if (new_index < 0)
  3301.     new_index = 0;
  3302.  
  3303.   if (new_index >= list_length)
  3304.     new_index = list_length - 1;
  3305.  
  3306.   list = g_slist_remove (gimage->channels, channel);
  3307.   gimage->channels = g_slist_insert (list, channel, new_index);
  3308.  
  3309.   drawable_update (GIMP_DRAWABLE (channel),
  3310.            0, 0,
  3311.            drawable_width  (GIMP_DRAWABLE (channel)),
  3312.            drawable_height (GIMP_DRAWABLE (channel)));
  3313.  
  3314.   return channel;
  3315. }
  3316.  
  3317. Channel *
  3318. gimp_image_add_channel (GimpImage *gimage, 
  3319.             Channel   *channel, 
  3320.             gint       position)
  3321. {
  3322.   ChannelUndo *cu;
  3323.   GSList      *cc;
  3324.  
  3325.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage) && 
  3326.             GIMP_IS_CHANNEL (channel), NULL);
  3327.  
  3328.   if (GIMP_DRAWABLE(channel)->gimage != NULL &&
  3329.       GIMP_DRAWABLE(channel)->gimage != gimage)
  3330.     {
  3331.       g_message ("gimp_image_add_channel: attempt to add channel to wrong image");
  3332.       return NULL;
  3333.     }
  3334.  
  3335.   for (cc = gimage->channels; cc; cc = g_slist_next (cc))
  3336.     if (cc->data == channel) 
  3337.       {
  3338.     g_message ("gimp_image_add_channel: trying to add channel to image twice");
  3339.     return NULL;
  3340.       }
  3341.  
  3342.   /*  Prepare a channel undo and push it  */
  3343.   cu = g_new (ChannelUndo, 1);
  3344.   cu->channel       = channel;
  3345.   cu->prev_position = 0;
  3346.   cu->prev_channel  = gimage->active_channel;
  3347.   undo_push_channel (gimage, CHANNEL_ADD_UNDO, cu);
  3348.  
  3349.   /*  add the channel to the list  */
  3350.   gimage->channels = g_slist_prepend (gimage->channels, channel_ref (channel));
  3351.  
  3352.   /*  notify this gimage of the currently active channel  */
  3353.   gimp_image_set_active_channel (gimage, channel);
  3354.  
  3355.   /*  if channel is visible, update the image  */
  3356.   if (drawable_visible (GIMP_DRAWABLE(channel)))
  3357.     drawable_update (GIMP_DRAWABLE(channel), 
  3358.              0, 0, 
  3359.              drawable_width (GIMP_DRAWABLE(channel)), 
  3360.              drawable_height (GIMP_DRAWABLE(channel)));
  3361.  
  3362.   return channel;
  3363. }
  3364.  
  3365. Channel *
  3366. gimp_image_remove_channel (GimpImage *gimage, 
  3367.                Channel   *channel)
  3368. {
  3369.   ChannelUndo *cu;
  3370.  
  3371.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage) && 
  3372.             GIMP_IS_CHANNEL (channel), NULL);
  3373.  
  3374.   /*  Prepare a channel undo--push it below  */
  3375.   cu = g_new (ChannelUndo, 1);
  3376.   cu->channel       = channel;
  3377.   cu->prev_position = gimp_image_get_channel_index (gimage, channel);
  3378.   cu->prev_channel  = gimage->active_channel;
  3379.   
  3380.   gimage->channels = g_slist_remove (gimage->channels, channel);
  3381.   
  3382.   if (gimage->active_channel == channel)
  3383.     {
  3384.       if (gimage->channels)
  3385.     gimage->active_channel = (((Channel *) gimage->channels->data));
  3386.       else
  3387.     gimage->active_channel = NULL;
  3388.     }
  3389.   
  3390.   if (drawable_visible (GIMP_DRAWABLE(channel)))
  3391.     drawable_update (GIMP_DRAWABLE(channel), 
  3392.              0, 0, 
  3393.              drawable_width (GIMP_DRAWABLE(channel)), 
  3394.              drawable_height (GIMP_DRAWABLE(channel)));
  3395.   
  3396.   /* Send out REMOVED signal from channel */
  3397.   channel_removed (channel, gimage);
  3398.   
  3399.   /*  Important to push the undo here in case the push fails  */
  3400.   undo_push_channel (gimage, CHANNEL_REMOVE_UNDO, cu);
  3401.   
  3402.   return channel;
  3403. }
  3404.  
  3405. /************************************************************/
  3406. /*  Access functions                                        */
  3407. /************************************************************/
  3408.  
  3409. gboolean
  3410. gimp_image_is_empty (GimpImage *gimage)
  3411. {
  3412.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), TRUE);
  3413.  
  3414.   return (! gimage->layers);
  3415. }
  3416.  
  3417. GimpDrawable *
  3418. gimp_image_active_drawable (GimpImage *gimage)
  3419. {
  3420.   Layer *layer;
  3421.  
  3422.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3423.  
  3424.   /*  If there is an active channel (a saved selection, etc.),
  3425.    *  we ignore the active layer
  3426.    */
  3427.   if (gimage->active_channel != NULL)
  3428.     return GIMP_DRAWABLE (gimage->active_channel);
  3429.   else if (gimage->active_layer != NULL)
  3430.     {
  3431.       layer = gimage->active_layer;
  3432.       if (layer->mask && layer->edit_mask)
  3433.     return GIMP_DRAWABLE(layer->mask);
  3434.       else
  3435.     return GIMP_DRAWABLE(layer);
  3436.     }
  3437.   else
  3438.     return NULL;
  3439. }
  3440.  
  3441. GimpImageBaseType
  3442. gimp_image_base_type (GimpImage *gimage)
  3443. {  
  3444.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
  3445.  
  3446.   return gimage->base_type;
  3447. }
  3448.  
  3449. GimpImageType
  3450. gimp_image_base_type_with_alpha (GimpImage *gimage)
  3451. {
  3452.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
  3453.  
  3454.   switch (gimage->base_type)
  3455.     {
  3456.     case RGB:
  3457.       return RGBA_GIMAGE;
  3458.     case GRAY:
  3459.       return GRAYA_GIMAGE;
  3460.     case INDEXED:
  3461.       return INDEXEDA_GIMAGE;
  3462.     }
  3463.   return RGB_GIMAGE;
  3464. }
  3465.  
  3466. gchar *
  3467. gimp_image_filename (GimpImage *gimage)
  3468. {
  3469.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3470.  
  3471.   if (gimage->has_filename)
  3472.     return gimage->filename;
  3473.   else
  3474.     return _("Untitled");
  3475. }
  3476.  
  3477. gboolean
  3478. gimp_image_undo_is_enabled (GimpImage *gimage)
  3479. {
  3480.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  3481.  
  3482.   return gimage->undo_on;
  3483. }
  3484.  
  3485. gboolean
  3486. gimp_image_undo_freeze (GimpImage *gimage)
  3487. {
  3488.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  3489.  
  3490.   gimage->undo_on = FALSE;
  3491.  
  3492.   return TRUE;
  3493. }
  3494.  
  3495. gboolean
  3496. gimp_image_undo_thaw (GimpImage *gimage)
  3497. {
  3498.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  3499.  
  3500.   gimage->undo_on = TRUE;
  3501.  
  3502.   return TRUE;
  3503. }
  3504.  
  3505. gboolean
  3506. gimp_image_undo_disable (GimpImage *gimage)
  3507. {
  3508.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  3509.  
  3510.   return gimp_image_undo_freeze (gimage);
  3511. }
  3512.  
  3513. gboolean
  3514. gimp_image_undo_enable (GimpImage *gimage)
  3515. {
  3516.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  3517.  
  3518.   /*  Free all undo steps as they are now invalidated  */
  3519.   undo_free (gimage);
  3520.  
  3521.   return gimp_image_undo_thaw (gimage);
  3522. }
  3523.  
  3524. void
  3525. gimp_image_undo_event (GimpImage *gimage, 
  3526.                int        event)
  3527. {
  3528.   gtk_signal_emit (GTK_OBJECT (gimage), gimp_image_signals[UNDO_EVENT], event);
  3529. }
  3530.  
  3531.  
  3532. /* NOTE about the gimage->dirty counter:
  3533.  *   If 0, then the image is clean (ie, copy on disk is the same as the one 
  3534.  *      in memory).
  3535.  *   If positive, then that's the number of dirtying operations done
  3536.  *       on the image since the last save.
  3537.  *   If negative, then user has hit undo and gone back in time prior
  3538.  *       to the saved copy.  Hitting redo will eventually come back to
  3539.  *       the saved copy.
  3540.  *
  3541.  *   The image is dirty (ie, needs saving) if counter is non-zero.
  3542.  *
  3543.  *   If the counter is around 10000, this is due to undo-ing back
  3544.  *   before a saved version, then mutating the image (thus destroying
  3545.  *   the redo stack).  Once this has happened, it's impossible to get
  3546.  *   the image back to the state on disk, since the redo info has been
  3547.  *   freed.  See undo.c for the gorey details.
  3548.  */
  3549.  
  3550.  
  3551. /*
  3552.  * NEVER CALL gimp_image_dirty() directly!
  3553.  *
  3554.  * If your code has just dirtied the image, push an undo instead.
  3555.  * Failing that, push the trivial undo which tells the user the
  3556.  * command is not undoable: undo_push_cantundo() (But really, it would
  3557.  * be best to push a proper undo).  If you just dirty the image
  3558.  * without pushing an undo then the dirty count is increased, but
  3559.  * popping that many undo actions won't lead to a clean image.
  3560.  */
  3561.  
  3562. gint
  3563. gimp_image_dirty (GimpImage *gimage)
  3564. {
  3565.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  3566.  
  3567.   gimage->dirty++;
  3568.   gtk_signal_emit(GTK_OBJECT(gimage), gimp_image_signals[DIRTY]);
  3569.   
  3570.   TRC (("dirty %d -> %d\n", gimage->dirty-1, gimage->dirty));
  3571.   
  3572.   return gimage->dirty;
  3573. }
  3574.  
  3575. gint
  3576. gimp_image_clean (GimpImage *gimage)
  3577. {
  3578.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  3579.  
  3580.   gimage->dirty--;
  3581.   gtk_signal_emit(GTK_OBJECT(gimage), gimp_image_signals[CLEAN]);
  3582.   
  3583.   TRC (("clean %d -> %d\n", gimage->dirty+1, gimage->dirty));
  3584.   
  3585.   return gimage->dirty;
  3586. }
  3587.  
  3588. void
  3589. gimp_image_clean_all (GimpImage *gimage)
  3590. {
  3591.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  3592.  
  3593.   gimage->dirty = 0;
  3594.  
  3595.   gtk_signal_emit (GTK_OBJECT(gimage), gimp_image_signals[CLEAN]);
  3596. }
  3597.  
  3598. Layer *
  3599. gimp_image_floating_sel (GimpImage *gimage)
  3600. {
  3601.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  3602.  
  3603.   if (gimage->floating_sel == NULL)
  3604.     return NULL;
  3605.   else
  3606.     return gimage->floating_sel;
  3607. }
  3608.  
  3609. guchar *
  3610. gimp_image_cmap (GimpImage *gimage)
  3611. {
  3612.   return drawable_cmap (gimp_image_active_drawable (gimage));
  3613. }
  3614.  
  3615. /************************************************************/
  3616. /*  Projection access functions                             */
  3617. /************************************************************/
  3618.  
  3619. TileManager *
  3620. gimp_image_projection (GimpImage *gimage)
  3621. {
  3622.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3623.  
  3624.   if ((gimage->projection == NULL) ||
  3625.       (tile_manager_level_width (gimage->projection) != gimage->width) ||
  3626.       (tile_manager_level_height (gimage->projection) != gimage->height))
  3627.     gimp_image_allocate_projection (gimage);
  3628.   
  3629.   return gimage->projection;
  3630. }
  3631.  
  3632. GimpImageType
  3633. gimp_image_projection_type (GimpImage *gimage)
  3634. {
  3635.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
  3636.  
  3637.   return gimage->proj_type;
  3638. }
  3639.  
  3640. gint
  3641. gimp_image_projection_bytes (GimpImage *gimage)
  3642. {
  3643.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
  3644.  
  3645.   return gimage->proj_bytes;
  3646. }
  3647.  
  3648. gint
  3649. gimp_image_projection_opacity (GimpImage *gimage)
  3650. {
  3651.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
  3652.  
  3653.   return OPAQUE_OPACITY;
  3654. }
  3655.  
  3656. void
  3657. gimp_image_projection_realloc (GimpImage *gimage)
  3658. {
  3659.   gimp_image_allocate_projection (gimage);
  3660. }
  3661.  
  3662. /************************************************************/
  3663. /*  Composition access functions                            */
  3664. /************************************************************/
  3665.  
  3666. TileManager *
  3667. gimp_image_composite (GimpImage *gimage)
  3668. {
  3669.   return gimp_image_projection (gimage);
  3670. }
  3671.  
  3672. GimpImageType
  3673. gimp_image_composite_type (GimpImage *gimage)
  3674. {
  3675.   return gimp_image_projection_type (gimage);
  3676. }
  3677.  
  3678. gint
  3679. gimp_image_composite_bytes (GimpImage *gimage)
  3680. {
  3681.   return gimp_image_projection_bytes (gimage);
  3682. }
  3683.  
  3684. TempBuf *
  3685. gimp_image_construct_composite_preview (GimpImage *gimage, 
  3686.                     gint       width, 
  3687.                     gint       height)
  3688. {
  3689.   Layer       *layer;
  3690.   Layer       *floating_sel;
  3691.   PixelRegion  src1PR, src2PR, maskPR;
  3692.   PixelRegion *mask;
  3693.   TempBuf     *comp;
  3694.   TempBuf     *layer_buf;
  3695.   TempBuf     *mask_buf;
  3696.   GSList      *list;
  3697.   GSList      *reverse_list = NULL;
  3698.   gdouble      ratio;
  3699.   gint         x, y, w, h;
  3700.   gint         x1, y1, x2, y2;
  3701.   gint         bytes;
  3702.   gint         construct_flag;
  3703.   gint         visible[MAX_CHANNELS] = {1, 1, 1, 1};
  3704.   gint         off_x, off_y;
  3705.  
  3706.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3707.  
  3708.   ratio = (gdouble) width / (gdouble) gimage->width;
  3709.  
  3710.   switch (gimp_image_base_type (gimage))
  3711.     {
  3712.     case RGB:
  3713.     case INDEXED:
  3714.       bytes = 4;
  3715.       break;
  3716.     case GRAY:
  3717.       bytes = 2;
  3718.       break;
  3719.     default:
  3720.       bytes = 0;
  3721.       break;
  3722.     }
  3723.  
  3724.   /*  The construction buffer  */
  3725.   comp = temp_buf_new (width, height, bytes, 0, 0, NULL);
  3726.   memset (temp_buf_data (comp), 0, comp->width * comp->height * comp->bytes);
  3727.  
  3728.   floating_sel = NULL;
  3729.   for (list = gimage->layers; list; list = g_slist_next (list))
  3730.     {
  3731.       layer = (Layer *) list->data;
  3732.  
  3733.       /*  only add layers that are visible to the list  */
  3734.       if (drawable_visible (GIMP_DRAWABLE (layer)))
  3735.     {
  3736.       /*  floating selections are added right above the layer 
  3737.           they are attached to  */
  3738.       if (layer_is_floating_sel (layer))
  3739.         floating_sel = layer;
  3740.       else
  3741.         {
  3742.           if (floating_sel && 
  3743.           floating_sel->fs.drawable == GIMP_DRAWABLE (layer))
  3744.         reverse_list = g_slist_prepend (reverse_list, floating_sel);
  3745.  
  3746.           reverse_list = g_slist_prepend (reverse_list, layer);
  3747.         }
  3748.     }
  3749.     }
  3750.  
  3751.   construct_flag = 0;
  3752.  
  3753.   while (reverse_list)
  3754.     {
  3755.       layer = (Layer *) reverse_list->data;
  3756.  
  3757.       drawable_offsets (GIMP_DRAWABLE (layer), &off_x, &off_y);
  3758.  
  3759.       x = (gint) RINT (ratio * off_x);
  3760.       y = (gint) RINT (ratio * off_y); 
  3761.       w = (gint) RINT (ratio * drawable_width (GIMP_DRAWABLE (layer))); 
  3762.       h = (gint) RINT (ratio * drawable_height (GIMP_DRAWABLE (layer))); 
  3763.       
  3764.       x1 = CLAMP (x, 0, width);
  3765.       y1 = CLAMP (y, 0, height);
  3766.       x2 = CLAMP (x + w, 0, width);
  3767.       y2 = CLAMP (y + h, 0, height);
  3768.  
  3769.       src1PR.bytes     = comp->bytes;
  3770.       src1PR.x         = x1;
  3771.       src1PR.y         = y1;
  3772.       src1PR.w         = (x2 - x1);
  3773.       src1PR.h         = (y2 - y1);
  3774.       src1PR.rowstride = comp->width * src1PR.bytes;
  3775.       src1PR.data      = 
  3776.     temp_buf_data (comp) + y1 * src1PR.rowstride + x1 * src1PR.bytes;
  3777.  
  3778.       layer_buf = layer_preview (layer, w, h);
  3779.       src2PR.bytes     = layer_buf->bytes;
  3780.       src2PR.w         = src1PR.w;  
  3781.       src2PR.h         = src1PR.h;
  3782.       src2PR.x         = src1PR.x; 
  3783.       src2PR.y         = src1PR.y;
  3784.       src2PR.rowstride = layer_buf->width * src2PR.bytes;
  3785.       src2PR.data      = temp_buf_data (layer_buf) + 
  3786.     (y1 - y) * src2PR.rowstride + (x1 - x) * src2PR.bytes;
  3787.  
  3788.       if (layer->mask && layer->apply_mask)
  3789.     {
  3790.       mask_buf = layer_mask_preview (layer, w, h);
  3791.       maskPR.bytes     = mask_buf->bytes;
  3792.       maskPR.rowstride = mask_buf->width;
  3793.       maskPR.data      = mask_buf_data (mask_buf) +
  3794.         (y1 - y) * maskPR.rowstride + (x1 - x) * maskPR.bytes;
  3795.       mask = &maskPR;
  3796.     }
  3797.       else
  3798.     mask = NULL;
  3799.  
  3800.       /*  Based on the type of the layer, project the layer onto the
  3801.        *   composite preview...
  3802.        *  Indexed images are actually already converted to RGB and RGBA,
  3803.        *   so just project them as if they were type "intensity"
  3804.        *  Send in all TRUE for visible since that info doesn't matter
  3805.        *   for previews
  3806.        */
  3807.       switch (drawable_type (GIMP_DRAWABLE(layer)))
  3808.     {
  3809.     case RGB_GIMAGE: case GRAY_GIMAGE: case INDEXED_GIMAGE:
  3810.       if (! construct_flag)
  3811.         initial_region (&src2PR, &src1PR, 
  3812.                 mask, NULL, layer->opacity,
  3813.                 layer->mode, visible, INITIAL_INTENSITY);
  3814.       else
  3815.         combine_regions (&src1PR, &src2PR, &src1PR, 
  3816.                  mask, NULL, layer->opacity,
  3817.                  layer->mode, visible, COMBINE_INTEN_A_INTEN);
  3818.       break;
  3819.  
  3820.     case RGBA_GIMAGE: case GRAYA_GIMAGE: case INDEXEDA_GIMAGE:
  3821.       if (! construct_flag)
  3822.         initial_region (&src2PR, &src1PR, 
  3823.                 mask, NULL, layer->opacity,
  3824.                 layer->mode, visible, INITIAL_INTENSITY_ALPHA);
  3825.       else
  3826.         combine_regions (&src1PR, &src2PR, &src1PR, 
  3827.                  mask, NULL, layer->opacity,
  3828.                  layer->mode, visible, COMBINE_INTEN_A_INTEN_A);
  3829.       break;
  3830.  
  3831.     default:
  3832.       break;
  3833.     }
  3834.  
  3835.       construct_flag = 1;
  3836.  
  3837.       reverse_list = g_slist_next (reverse_list);
  3838.     }
  3839.  
  3840.   g_slist_free (reverse_list);
  3841.  
  3842.   return comp;
  3843. }
  3844.  
  3845. TempBuf *
  3846. gimp_image_composite_preview (GimpImage   *gimage, 
  3847.                   ChannelType  type,
  3848.                   gint         width, 
  3849.                   gint         height)
  3850. {
  3851.   gint channel;
  3852.  
  3853.   switch (type)
  3854.     {
  3855.     case RED_CHANNEL:     channel = RED_PIX; break;
  3856.     case GREEN_CHANNEL:   channel = GREEN_PIX; break;
  3857.     case BLUE_CHANNEL:    channel = BLUE_PIX; break;
  3858.     case GRAY_CHANNEL:    channel = GRAY_PIX; break;
  3859.     case INDEXED_CHANNEL: channel = INDEXED_PIX; break;
  3860.     default: return NULL;
  3861.     }
  3862.  
  3863.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3864.  
  3865.   /*  The easy way  */
  3866.   if (gimage->comp_preview_valid[channel] &&
  3867.       gimage->comp_preview->width == width &&
  3868.       gimage->comp_preview->height == height)
  3869.     return gimage->comp_preview;
  3870.   /*  The hard way  */
  3871.   else
  3872.     {
  3873.       if (gimage->comp_preview)
  3874.     temp_buf_free (gimage->comp_preview);
  3875.  
  3876.       /*  Actually construct the composite preview from the layer previews!
  3877.        *  This might seem ridiculous, but it's actually the best way, given
  3878.        *  a number of unsavory alternatives.
  3879.        */
  3880.       gimage->comp_preview = 
  3881.     gimp_image_construct_composite_preview (gimage, width, height);
  3882.       gimage->comp_preview_valid[channel] = TRUE;
  3883.  
  3884.       return gimage->comp_preview;
  3885.     }
  3886. }
  3887.  
  3888. gboolean
  3889. gimp_image_preview_valid (GimpImage   *gimage, 
  3890.               ChannelType  type)
  3891. {
  3892.   switch (type)
  3893.     {
  3894.     case RED_CHANNEL:     return gimage->comp_preview_valid[RED_PIX];     break;
  3895.     case GREEN_CHANNEL:   return gimage->comp_preview_valid[GREEN_PIX];   break;
  3896.     case BLUE_CHANNEL:    return gimage->comp_preview_valid[BLUE_PIX];    break;
  3897.     case GRAY_CHANNEL:    return gimage->comp_preview_valid[GRAY_PIX];    break;
  3898.     case INDEXED_CHANNEL: return gimage->comp_preview_valid[INDEXED_PIX]; break;
  3899.     default:              return TRUE;
  3900.     }
  3901. }
  3902.  
  3903. void
  3904. gimp_image_invalidate_preview (GimpImage *gimage)
  3905. {
  3906.   Layer *layer;
  3907.  
  3908.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  3909.  
  3910.   /*  Invalidate the floating sel if it exists  */
  3911.   if ((layer = gimp_image_floating_sel (gimage)))
  3912.     floating_sel_invalidate (layer);
  3913.  
  3914.   gimage->comp_preview_valid[0] = FALSE;
  3915.   gimage->comp_preview_valid[1] = FALSE;
  3916.   gimage->comp_preview_valid[2] = FALSE;
  3917. }
  3918.