gimp/app/core/gimpimage-new.c

363 lines
11 KiB
C
Raw Normal View History

/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995-1999 Spencer Kimball and Peter Mattis
app/widgets/gimpdocked.[ch] renamed GimpDockedIface to 2003-10-11 Sven Neumann <sven@gimp.org> * app/widgets/gimpdocked.[ch] * app/widgets/widgets-types.h: renamed GimpDockedIface to GimpDockedInterface. * app/display/gimpnavigationview.c * app/widgets/gimpcoloreditor.c * app/widgets/gimpcontainereditor.c * app/widgets/gimpcontainerview.c * app/widgets/gimpeditor.c * app/widgets/gimpimageeditor.c * app/widgets/gimpitemtreeview.c * app/widgets/gimptooloptionseditor.c: changed accordingly. * app/config/config-types.h * app/config/gimpconfig.[ch] * app/config/gimpconfig-deserialize.[ch] * app/config/gimpconfig-serialize.[ch] * app/config/gimpconfig-utils.[ch]: added a GimpConfig typedef and changed the GimpConfig API to take GimpConfig instead of GObject pointers. * app/config/gimpconfig-dump.c * app/config/gimprc.c * app/config/test-config.c * app/core/gimp-documents.c * app/core/gimp-parasites.c * app/core/gimp-templates.c * app/core/gimp.[ch] * app/core/gimpcontainer.c * app/core/gimpcontext.c * app/core/gimpdocumentlist.c * app/core/gimpgrid.c * app/core/gimpimage-duplicate.c * app/core/gimpimage-new.c * app/core/gimpimage.c * app/core/gimpparasitelist.c * app/core/gimptemplate.c * app/core/gimptooloptions.c * app/core/gimpviewable.c * app/gui/grid-dialog.c * app/gui/preferences-dialog.c * app/gui/stroke-dialog.c * app/gui/templates-commands.c * app/gui/tool-options-commands.c * app/paint/gimppaintcore.c * app/pdb/gimprc_cmds.c * app/text/gimptext-parasite.c * app/text/gimptext.c * app/text/gimptextlayer.c * app/tools/gimp-tools.c * app/tools/gimptexttool.c * app/widgets/gimpdevices.c * app/widgets/gimptemplateeditor.c * app/widgets/gimptemplateview.c * tools/pdbgen/pdb/gimprc.pdb: changed accordingly.
2003-10-11 22:30:18 +08:00
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gegl.h>
#include "libgimpbase/gimpbase.h"
2005-01-26 03:11:26 +08:00
#include "libgimpconfig/gimpconfig.h"
2005-01-26 03:11:26 +08:00
#include "core-types.h"
Finally landed the new GimpConfig based gimprc parser. It's not finished 2002-11-18 Sven Neumann <sven@gimp.org> Finally landed the new GimpConfig based gimprc parser. It's not finished yet but we need to start somewhere. This release removes the old gimprc.[ch] files. The gimprc format changes slightly, but the changes are minimal. The Preferences dialog is temporarily disabled since it still needs to be ported. If you are are afraid, stay away from CVS for a few days ;-) * app/Makefile.am * app/gimprc.[ch]: removed the old gimprc system. * app/base/Makefile.am * app/base/base-config.[ch]: removed these files in favor of config/gimpbaseconfig.[ch]. * app/core/Makefile.am * app/core/gimpcoreconfig.[ch]: removed these files in favor of config/gimpcoreconfig.[ch]. * app/config/Makefile.am * app/config/config-types.h: moved typedefs into this new file. * app/config/gimpbaseconfig.[ch] * app/config/gimpcoreconfig.[ch] * app/config/gimpdisplayconfig.[ch] * app/config/gimpguiconfig.[ch] * app/config/gimprc.[ch] * app/config/test-config.c: brought into shape for real use. * app/base/base-types.h: include config/config-types.h here. Added a global GimpBaseConfig *base_config variable to ease migration. * app/gui/Makefile.am: temporarily disabled the preferences dialog. * app/app_procs.c * app/undo.c * app/undo_history.c * app/base/base.[ch] * app/base/gimphistogram.c * app/base/pixel-processor.c * app/base/temp-buf.c * app/base/tile-cache.c * app/core/core-types.h * app/core/gimp-documents.c * app/core/gimp.c * app/core/gimpbrush.c * app/core/gimpbrushgenerated.c * app/core/gimpcontext.c * app/core/gimpdrawable-transform.c * app/core/gimpimage-new.c * app/core/gimpimage.c * app/core/gimpimagefile.c * app/core/gimpmodules.c * app/core/gimppattern.c * app/display/Makefile.am * app/display/gimpdisplay-handlers.c * app/display/gimpdisplay.[ch] * app/display/gimpdisplayshell-callbacks.c * app/display/gimpdisplayshell-handlers.c * app/display/gimpdisplayshell-layer-select.c * app/display/gimpdisplayshell-render.c * app/display/gimpdisplayshell-scale.c * app/display/gimpdisplayshell-scroll.c * app/display/gimpdisplayshell-selection.c * app/display/gimpdisplayshell.[ch] * app/display/gimpnavigationview.c * app/file/file-save.c * app/gui/device-status-dialog.c * app/gui/dialogs-constructors.c * app/gui/file-commands.c * app/gui/file-new-dialog.c * app/gui/file-open-dialog.c * app/gui/file-save-dialog.c * app/gui/gui.c * app/gui/menus.c * app/gui/paths-dialog.c * app/gui/resize-dialog.c * app/gui/session.c * app/gui/test-commands.c * app/gui/tips-dialog.c * app/gui/tips-dialog.h * app/gui/user-install-dialog.c * app/gui/view-commands.c * app/paint/gimppaintcore.c * app/plug-in/plug-in.c * app/plug-in/plug-ins.c * app/tools/gimpbezierselecttool.c * app/tools/gimpbucketfilltool.c * app/tools/gimpcolorpickertool.c * app/tools/gimpcroptool.c * app/tools/gimpeditselectiontool.c * app/tools/gimpfuzzyselecttool.c * app/tools/gimpinktool.c * app/tools/gimpmagnifytool.c * app/tools/gimpmeasuretool.c * app/tools/gimppainttool.c * app/tools/gimppathtool.c * app/tools/gimptexttool.[ch] * app/tools/selection_options.c * app/tools/tools.c * app/tools/transform_options.c * app/widgets/gimphelp.c * app/widgets/gimpitemfactory.c * app/widgets/gimpselectioneditor.c * app/xcf/xcf-load.c * tools/pdbgen/pdb/fileops.pdb * tools/pdbgen/pdb/gimprc.pdb * tools/pdbgen/pdb/image.pdb * tools/pdbgen/pdb/layer.pdb * tools/pdbgen/pdb/transform_tools.pdb: use the new config system instead of the old gimprc stuff. * etc/gimprc.in * etc/gimprc_user.in: adapted to the new gimprc format. Will update the man-page later... * app/pdb/fileops_cmds.c * app/pdb/gimprc_cmds.c * app/pdb/image_cmds.c * app/pdb/layer_cmds.c * app/pdb/transform_tools_cmds.c * libgimp/gimpgimprc_pdb.c: regenerated.
2002-11-19 04:50:31 +08:00
#include "config/gimpcoreconfig.h"
#include "gegl/gimp-gegl-utils.h"
#include "gimp.h"
#include "gimpbuffer.h"
#include "gimpchannel.h"
#include "gimpcontext.h"
#include "gimpimage.h"
#include "gimpimage-colormap.h"
#include "gimpimage-new.h"
#include "gimpimage-undo.h"
#include "gimplayer.h"
#include "gimptemplate.h"
#include "gimp-intl.h"
GimpTemplate *
gimp_image_new_get_last_template (Gimp *gimp,
GimpImage *image)
{
GimpTemplate *template;
g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
g_return_val_if_fail (image == NULL || GIMP_IS_IMAGE (image), NULL);
template = gimp_template_new ("image new values");
if (image)
{
gimp_config_sync (G_OBJECT (gimp->config->default_image),
G_OBJECT (template), 0);
gimp_template_set_from_image (template, image);
}
else
{
gimp_config_sync (G_OBJECT (gimp->image_new_last_template),
G_OBJECT (template), 0);
}
return template;
}
void
gimp_image_new_set_last_template (Gimp *gimp,
GimpTemplate *template)
{
g_return_if_fail (GIMP_IS_GIMP (gimp));
g_return_if_fail (GIMP_IS_TEMPLATE (template));
gimp_config_sync (G_OBJECT (template),
G_OBJECT (gimp->image_new_last_template), 0);
}
GimpImage *
gimp_image_new_from_template (Gimp *gimp,
GimpTemplate *template,
GimpContext *context)
{
GimpImage *image;
GimpLayer *layer;
GimpImageType type;
gint width, height;
const gchar *comment;
g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
g_return_val_if_fail (GIMP_IS_TEMPLATE (template), NULL);
g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
image = gimp_create_image (gimp,
gimp_template_get_width (template),
gimp_template_get_height (template),
gimp_template_get_image_type (template),
FALSE);
gimp_image_undo_disable (image);
comment = gimp_template_get_comment (template);
if (comment)
{
GimpParasite *parasite;
parasite = gimp_parasite_new ("gimp-comment",
GIMP_PARASITE_PERSISTENT,
strlen (comment) + 1,
comment);
gimp_image_parasite_attach (image, parasite);
gimp_parasite_free (parasite);
}
gimp_image_set_resolution (image,
gimp_template_get_resolution_x (template),
gimp_template_get_resolution_y (template));
gimp_image_set_unit (image, gimp_template_get_resolution_unit (template));
width = gimp_image_get_width (image);
height = gimp_image_get_height (image);
switch (gimp_template_get_fill_type (template))
{
case GIMP_TRANSPARENT_FILL:
type = ((gimp_template_get_image_type (template) == GIMP_RGB) ?
GIMP_RGBA_IMAGE : GIMP_GRAYA_IMAGE);
break;
default:
type = ((gimp_template_get_image_type (template) == GIMP_RGB) ?
GIMP_RGB_IMAGE : GIMP_GRAY_IMAGE);
break;
}
layer = gimp_layer_new (image, width, height,
gimp_image_get_format (image, type),
_("Background"),
GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
gimp_drawable_fill_by_type (GIMP_DRAWABLE (layer),
context, gimp_template_get_fill_type (template));
gimp_image_add_layer (image, layer, NULL, 0, FALSE);
gimp_image_undo_enable (image);
gimp_image_clean_all (image);
gimp_create_display (gimp, image, gimp_template_get_unit (template), 1.0);
g_object_unref (image);
return image;
}
GimpImage *
gimp_image_new_from_drawable (Gimp *gimp,
GimpDrawable *drawable)
{
GimpItem *item;
GimpImage *image;
GimpImage *new_image;
GimpLayer *new_layer;
GType new_type;
gint off_x, off_y;
GimpImageBaseType type;
gdouble xres;
gdouble yres;
g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
item = GIMP_ITEM (drawable);
image = gimp_item_get_image (item);
type = gimp_drawable_get_base_type (drawable);
new_image = gimp_create_image (gimp,
gimp_item_get_width (item),
gimp_item_get_height (item),
type, TRUE);
gimp_image_undo_disable (new_image);
if (type == GIMP_INDEXED)
gimp_image_set_colormap (new_image,
gimp_image_get_colormap (image),
gimp_image_get_colormap_size (image),
FALSE);
gimp_image_get_resolution (image, &xres, &yres);
gimp_image_set_resolution (new_image, xres, yres);
gimp_image_set_unit (new_image, gimp_image_get_unit (image));
if (GIMP_IS_LAYER (drawable))
new_type = G_TYPE_FROM_INSTANCE (drawable);
else
new_type = GIMP_TYPE_LAYER;
new_layer = GIMP_LAYER (gimp_item_convert (GIMP_ITEM (drawable),
new_image, new_type));
gimp_object_set_name (GIMP_OBJECT (new_layer),
gimp_object_get_name (drawable));
gimp_item_get_offset (GIMP_ITEM (new_layer), &off_x, &off_y);
gimp_item_translate (GIMP_ITEM (new_layer), -off_x, -off_y, FALSE);
gimp_item_set_visible (GIMP_ITEM (new_layer), TRUE, FALSE);
gimp_item_set_linked (GIMP_ITEM (new_layer), FALSE, FALSE);
gimp_layer_set_mode (new_layer, GIMP_NORMAL_MODE, FALSE);
gimp_layer_set_opacity (new_layer, GIMP_OPACITY_OPAQUE, FALSE);
gimp_layer_set_lock_alpha (new_layer, FALSE, FALSE);
gimp_image_add_layer (new_image, new_layer, NULL, 0, TRUE);
gimp_image_undo_enable (new_image);
return new_image;
}
GimpImage *
gimp_image_new_from_component (Gimp *gimp,
GimpImage *image,
GimpChannelType component)
{
GimpImage *new_image;
GimpChannel *channel;
GimpLayer *layer;
const gchar *desc;
gdouble xres;
gdouble yres;
g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);
new_image = gimp_create_image (gimp,
gimp_image_get_width (image),
gimp_image_get_height (image),
GIMP_GRAY, TRUE);
gimp_image_undo_disable (new_image);
gimp_image_get_resolution (image, &xres, &yres);
gimp_image_set_resolution (new_image, xres, yres);
gimp_image_set_unit (new_image, gimp_image_get_unit (image));
channel = gimp_channel_new_from_component (image, component, NULL, NULL);
layer = GIMP_LAYER (gimp_item_convert (GIMP_ITEM (channel),
new_image, GIMP_TYPE_LAYER));
g_object_unref (channel);
gimp_enum_get_value (GIMP_TYPE_CHANNEL_TYPE, component,
NULL, NULL, &desc, NULL);
gimp_object_take_name (GIMP_OBJECT (layer),
g_strdup_printf (_("%s Channel Copy"), desc));
gimp_image_add_layer (new_image, layer, NULL, 0, TRUE);
gimp_image_undo_enable (new_image);
return new_image;
}
GimpImage *
gimp_image_new_from_buffer (Gimp *gimp,
GimpImage *invoke,
GimpBuffer *paste)
{
GimpImage *image;
GimpLayer *layer;
const Babl *format;
gboolean has_alpha;
g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
g_return_val_if_fail (invoke == NULL || GIMP_IS_IMAGE (invoke), NULL);
g_return_val_if_fail (GIMP_IS_BUFFER (paste), NULL);
format = gimp_buffer_get_format (paste);
has_alpha = babl_format_has_alpha (format);
/* create a new image (always of type GIMP_RGB) */
image = gimp_create_image (gimp,
gimp_buffer_get_width (paste),
gimp_buffer_get_height (paste),
gimp_babl_format_get_base_type (format),
TRUE);
gimp_image_undo_disable (image);
if (invoke)
{
gdouble xres;
gdouble yres;
gimp_image_get_resolution (invoke, &xres, &yres);
gimp_image_set_resolution (image, xres, yres);
gimp_image_set_unit (image, gimp_image_get_unit (invoke));
}
layer = gimp_layer_new_from_buffer (gimp_buffer_get_buffer (paste),
image,
gimp_image_get_layer_format (image,
has_alpha),
_("Pasted Layer"),
GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
gimp_image_add_layer (image, layer, NULL, 0, TRUE);
gimp_image_undo_enable (image);
return image;
}
GimpImage *
gimp_image_new_from_pixbuf (Gimp *gimp,
GdkPixbuf *pixbuf,
const gchar *layer_name)
{
GimpImage *new_image;
GimpLayer *layer;
GimpImageBaseType base_type;
gboolean has_alpha = FALSE;
g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
switch (gdk_pixbuf_get_n_channels (pixbuf))
{
case 2: has_alpha = TRUE;
case 1: base_type = GIMP_GRAY;
break;
case 4: has_alpha = TRUE;
case 3: base_type = GIMP_RGB;
break;
default:
g_return_val_if_reached (NULL);
}
new_image = gimp_create_image (gimp,
gdk_pixbuf_get_width (pixbuf),
gdk_pixbuf_get_height (pixbuf),
base_type,
FALSE);
gimp_image_undo_disable (new_image);
layer = gimp_layer_new_from_pixbuf (pixbuf, new_image,
gimp_image_get_layer_format (new_image,
has_alpha),
layer_name,
GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
gimp_image_add_layer (new_image, layer, NULL, 0, TRUE);
gimp_image_undo_enable (new_image);
return new_image;
}