/* Add Layer 0.9 --- image filter plug-in for The Gimp * (c) Simon Budig * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */ #include #include #include #include #include #define PLUG_IN_NAME "plug_in_add_layer" #define PLUG_IN_VERSION "Oct 2002, 0.9" /***** Prototypes *****/ static void query (void); static void run (gchar *name, gint nparams, GimpParam *param, gint *nreturn_vals, GimpParam **return_vals); static gint32 add_layer (gint32 image_id, gint32 drawable_id); /***** Variables *****/ GimpPlugInInfo PLUG_IN_INFO = { NULL, /* init_proc */ NULL, /* quit_proc */ query, /* query_proc */ run /* run_proc */ }; /***** Functions *****/ MAIN () static void query (void) { static GimpParamDef args[] = { { GIMP_PDB_INT32, "run_mode", "Interactive (0), non-interactive (1)" }, { GIMP_PDB_IMAGE, "image", "Input image" }, { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" }, }; static GimpParamDef return_vals[] = { { GIMP_PDB_LAYER, "Curl Layer", "The newly created layer." } }; gimp_install_procedure (PLUG_IN_NAME, "Add Layer Demo Plugin", "This plug-in simply creates a new layer.", "Simon Budig", "Simon Budig", PLUG_IN_VERSION, "/Filters/Demo/Add Layer", "RGB*, GRAY*, INDEXED*", GIMP_PLUGIN, G_N_ELEMENTS (args), G_N_ELEMENTS (return_vals), args, return_vals); } static void run (gchar *name, gint nparams, GimpParam *param, gint *nreturn_vals, GimpParam **return_vals) { static GimpParam values[2]; GimpRunMode run_mode; GimpPDBStatusType status = GIMP_PDB_SUCCESS; run_mode = param[0].data.d_int32; *nreturn_vals = 2; *return_vals = values; values[0].type = GIMP_PDB_STATUS; values[0].data.d_status = status; values[1].type = GIMP_PDB_LAYER; values[1].data.d_layer = -1; switch (run_mode) { case GIMP_RUN_INTERACTIVE: case GIMP_RUN_NONINTERACTIVE: /* Make sure all the arguments are there! */ if (nparams != 3) status = GIMP_PDB_CALLING_ERROR; break; case GIMP_RUN_WITH_LAST_VALS: default: break; } if (status == GIMP_PDB_SUCCESS) { values[1].data.d_layer = add_layer (param[1].data.d_image, param[2].data.d_drawable); if (run_mode != GIMP_RUN_NONINTERACTIVE) gimp_displays_flush (); } values[0].data.d_status = status; } static gint32 add_layer (gint32 image_id, gint32 drawable_id) { gint32 nlayers; gint32 *image_layers; gint32 new_layertype=GIMP_RGBA_IMAGE; gint32 new_layer_id; gint drawable_position; gint offset_x, offset_y; GimpDrawable *drawable, *new_layer; /* * we need an undo group because we are not only creating * but also clearing and ofsetting the layer */ gimp_undo_push_group_start (image_id); drawable = gimp_drawable_get (drawable_id); /* * Determine Position of original Layer in the Layer stack by * searching for the known drawable_id of the source layer. */ image_layers = gimp_image_get_layers (image_id, &nlayers); drawable_position = 0; while (drawable_position < nlayers && image_layers[drawable_position] != drawable->drawable_id) drawable_position++; /* We need to add the correct type of layer. * * You have make sure ultimately that all layers except the * bottommost have an alpha channel. The bottommost may or may not * have an alpha channel. */ switch (gimp_image_base_type (image_id)) { case GIMP_RGB: new_layertype = GIMP_RGBA_IMAGE; break; case GIMP_GRAY: new_layertype = GIMP_GRAYA_IMAGE; break; case GIMP_INDEXED: new_layertype = GIMP_INDEXEDA_IMAGE; break; } /* * now create the layer and add it to the image */ new_layer = gimp_drawable_get (gimp_layer_new (image_id, "Newly created Layer", drawable->width, drawable->height, new_layertype, 100, GIMP_NORMAL_MODE)); new_layer_id = new_layer->drawable_id; gimp_image_add_layer (image_id, new_layer_id, drawable_position); /* * Setting the offsets to the same as the source drawable */ gimp_drawable_offsets (drawable->drawable_id, &offset_x, &offset_y); gimp_layer_set_offsets (new_layer->drawable_id, offset_x, offset_y); /* * The new layer contains pure junk - we need to fill it with a * defined content. Here we just clear it. */ gimp_drawable_fill (new_layer->drawable_id, GIMP_TRANSPARENT_FILL); /* * make the Gimp aware of the changes, update the display */ gimp_drawable_flush (new_layer); gimp_drawable_update (new_layer->drawable_id, 0, 0, new_layer->width, new_layer->height); gimp_drawable_detach (new_layer); /* * end the undo group */ gimp_undo_push_group_end (image_id); return new_layer_id; }