gimpimage-new.c 5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995-1999 Spencer Kimball and Peter Mattis
 * 
 * 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 "config.h"

21
#include <glib-object.h>
22 23 24 25 26 27 28

#include "libgimpbase/gimpbase.h"

#include "core-types.h"

#include "paint-funcs/paint-funcs.h"

29 30
#include "config/gimpcoreconfig.h"

31
#include "gimp.h"
32
#include "gimpbuffer.h"
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#include "gimpdrawable.h"
#include "gimpimage.h"
#include "gimpimage-new.h"
#include "gimplayer.h"

#include "libgimp/gimpintl.h"


GimpImageNewValues *
gimp_image_new_values_new (Gimp      *gimp,
			   GimpImage *gimage)
{
  GimpImageNewValues *values;

  values = g_new0 (GimpImageNewValues, 1);

  if (gimage)
    {
      values->width  = gimp_image_get_width (gimage);
      values->height = gimp_image_get_height (gimage);
      values->unit   = gimp_image_get_unit (gimage);

      gimp_image_get_resolution (gimage,
				 &values->xresolution, 
				 &values->yresolution);

      values->type = gimp_image_base_type (gimage);

61 62
      if (values->type == GIMP_INDEXED)
        values->type = GIMP_RGB; /* no indexed images */
63 64 65
    }
  else
    {
66
      *values = gimp->image_new_last_values;
67 68 69 70
    }

  if (gimp->global_buffer && gimp->have_current_cut_buffer)
    {
71 72
      values->width  = gimp_buffer_get_width (gimp->global_buffer);
      values->height = gimp_buffer_get_height (gimp->global_buffer);
73 74 75 76 77 78 79 80 81 82 83
    }

  return values;
}

void
gimp_image_new_set_default_values (Gimp               *gimp,
				   GimpImageNewValues *values)
{
  g_return_if_fail (values != NULL);

84
  gimp->image_new_last_values = *values;
85 86 87 88 89 90 91 92 93 94 95 96

  gimp->have_current_cut_buffer = FALSE;
}

void
gimp_image_new_values_free (GimpImageNewValues *values)
{
  g_return_if_fail (values != NULL);

  g_free (values);
}

97 98
gsize
gimp_image_new_calculate_memsize (GimpImageNewValues *values)
99
{
100
  gint channels;
101

102 103 104
  channels = ((values->type == GIMP_RGB ? 3 : 1)           /* color     */ +
              (values->fill_type == GIMP_TRANSPARENT_FILL) /* alpha     */ +
              1                                            /* selection */);
105

106
  return channels * values->width * values->height;
107 108 109
}

gchar *
110
gimp_image_new_get_memsize_string (gsize memsize)
111
{
112 113
  if (memsize < 4096)
    {
114
      return g_strdup_printf (_("%lu Bytes"), memsize);
115 116 117 118 119 120 121 122 123 124 125
    }
  else if (memsize < 1024 * 10)
    {
      return g_strdup_printf (_("%.2f KB"), (gdouble) memsize / 1024.0);
    }
  else if (memsize < 1024 * 100)
    {
      return g_strdup_printf (_("%.1f KB"), (gdouble) memsize / 1024.0);
    }
  else if (memsize < 1024 * 1024)
    {
126
      return g_strdup_printf (_("%lu KB"), memsize / 1024);
127 128 129 130 131
    }
  else if (memsize < 1024 * 1024 * 10)
    {
      return g_strdup_printf (_("%.2f MB"), (gdouble) memsize / 1024.0 / 1024.0);
    }
132
  else
133 134 135
    {
      return g_strdup_printf (_("%.1f MB"), (gdouble) memsize / 1024.0 / 1024.0);
    }
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
}

GimpImage *
gimp_image_new_create_image (Gimp               *gimp,
			     GimpImageNewValues *values)
{
  GimpImage     *gimage;
  GimpLayer     *layer;
  GimpImageType  type;
  gint           width, height;

  g_return_val_if_fail (values != NULL, NULL);

  gimp_image_new_set_default_values (gimp, values);

  switch (values->fill_type)
    {
153 154 155
    case GIMP_FOREGROUND_FILL:
    case GIMP_BACKGROUND_FILL:
    case GIMP_WHITE_FILL:
156
      type = (values->type == GIMP_RGB) ? GIMP_RGB_IMAGE : GIMP_GRAY_IMAGE;
157
      break;
158
    case GIMP_TRANSPARENT_FILL:
159
      type = (values->type == GIMP_RGB) ? GIMP_RGBA_IMAGE : GIMP_GRAYA_IMAGE;
160 161
      break;
    default:
162
      type = GIMP_RGB_IMAGE;
163 164 165
      break;
    }

166 167 168 169
  gimage = gimp_create_image (gimp,
			      values->width, values->height,
			      values->type,
			      TRUE);
170 171 172 173 174 175 176 177 178

  gimp_image_set_resolution (gimage, values->xresolution, values->yresolution);
  gimp_image_set_unit (gimage, values->unit);

  width  = gimp_image_get_width (gimage);
  height = gimp_image_get_height (gimage);

  layer = gimp_layer_new (gimage, width, height,
			  type, _("Background"),
179
			  GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
180 181 182 183 184 185 186
 
  if (layer)
    {
      gimp_image_undo_disable (gimage);
      gimp_image_add_layer (gimage, layer, 0);
      gimp_image_undo_enable (gimage);

Michael Natterer's avatar
Michael Natterer committed
187 188 189
      gimp_drawable_fill_by_type (GIMP_DRAWABLE (layer),
				  gimp_get_current_context (gimp),
				  values->fill_type);
190 191 192

      gimp_image_clean_all (gimage);

193 194 195
      gimp_create_display (gimp, gimage, 0x0101);

      g_object_unref (G_OBJECT (gimage));
196 197 198 199
    }

  return gimage;
}