1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 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 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 <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <cairo.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpbase/gimpparasiteio.h"
#include "libgimpcolor/gimpcolor.h"
#include "core/core-types.h"
#include "core/gimp.h"
#include "core/gimpbrushpipe.h"
#include "core/gimpbrushpipe-load.h"
#include "core/gimpbrush-private.h"
#include "core/gimpdrawable.h"
#include "core/gimpimage.h"
#include "core/gimplayer-new.h"
#include "core/gimpparamspecs.h"
#include "core/gimptempbuf.h"
#include "pdb/gimpprocedure.h"
#include "file-data-gbr.h"
#include "file-data-gih.h"
#include "gimp-intl.h"
/* local function prototypes */
static GimpImage * file_gih_pipe_to_image (Gimp *gimp,
GimpBrushPipe *pipe);
static GimpBrushPipe * file_gih_image_to_pipe (GimpImage *image,
const gchar *name,
gdouble spacing,
const gchar *paramstring);
/* public functions */
GimpValueArray *
file_gih_load_invoker (GimpProcedure *procedure,
Gimp *gimp,
GimpContext *context,
GimpProgress *progress,
const GimpValueArray *args,
GError **error)
{
GimpValueArray *return_vals;
GimpImage *image = NULL;
GFile *file;
GInputStream *input;
GError *my_error = NULL;
gimp_set_busy (gimp);
file = g_value_get_object (gimp_value_array_index (args, 1));
input = G_INPUT_STREAM (g_file_read (file, NULL, &my_error));
if (input)
{
GList *list = gimp_brush_pipe_load (context, file, input, error);
if (list)
{
GimpBrushPipe *pipe = list->data;
g_list_free (list);
image = file_gih_pipe_to_image (gimp, pipe);
g_object_unref (pipe);
}
g_object_unref (input);
}
else
{
g_propagate_prefixed_error (error, my_error,
_("Could not open '%s' for reading: "),
gimp_file_get_utf8_name (file));
}
return_vals = gimp_procedure_get_return_values (procedure, image != NULL,
error ? *error : NULL);
if (image)
g_value_set_object (gimp_value_array_index (return_vals, 1), image);
gimp_unset_busy (gimp);
return return_vals;
}
GimpValueArray *
file_gih_save_invoker (GimpProcedure *procedure,
Gimp *gimp,
GimpContext *context,
GimpProgress *progress,
const GimpValueArray *args,
GError **error)
{
GimpValueArray *return_vals;
GimpImage *image;
GimpBrushPipe *pipe;
const gchar *name;
const gchar *params;
GFile *file;
gint spacing;
gboolean success;
gimp_set_busy (gimp);
image = g_value_get_object (gimp_value_array_index (args, 1));
/* XXX: drawable list is currently unused. GIH saving just uses the
* whole layer list.
*/
file = g_value_get_object (gimp_value_array_index (args, 3));
spacing = g_value_get_int (gimp_value_array_index (args, 4));
name = g_value_get_string (gimp_value_array_index (args, 5));
params = g_value_get_string (gimp_value_array_index (args, 6));
pipe = file_gih_image_to_pipe (image, name, spacing, params);
gimp_data_set_file (GIMP_DATA (pipe), file, TRUE, TRUE);
success = gimp_data_save (GIMP_DATA (pipe), error);
g_object_unref (pipe);
return_vals = gimp_procedure_get_return_values (procedure, success,
error ? *error : NULL);
gimp_unset_busy (gimp);
return return_vals;
}
/* private functions */
static GimpImage *
file_gih_pipe_to_image (Gimp *gimp,
GimpBrushPipe *pipe)
{
GimpImage *image;
const gchar *name;
GimpImageBaseType base_type;
GimpParasite *parasite;
gchar spacing[8];
gint i;
if (gimp_brush_get_pixmap (pipe->current))
base_type = GIMP_RGB;
else
base_type = GIMP_GRAY;
name = gimp_object_get_name (pipe);
image = gimp_image_new (gimp, 1, 1, base_type,
GIMP_PRECISION_U8_NON_LINEAR);
parasite = gimp_parasite_new ("gimp-brush-pipe-name",
GIMP_PARASITE_PERSISTENT,
strlen (name) + 1, name);
gimp_image_parasite_attach (image, parasite, FALSE);
gimp_parasite_free (parasite);
g_snprintf (spacing, sizeof (spacing), "%d",
gimp_brush_get_spacing (GIMP_BRUSH (pipe)));
parasite = gimp_parasite_new ("gimp-brush-pipe-spacing",
GIMP_PARASITE_PERSISTENT,
strlen (spacing) + 1, spacing);
gimp_image_parasite_attach (image, parasite, FALSE);
gimp_parasite_free (parasite);
for (i = 0; i < pipe->n_brushes; i++)
{
GimpLayer *layer;
layer = file_gbr_brush_to_layer (image, pipe->brushes[i]);
gimp_image_add_layer (image, layer, NULL, i, FALSE);
}
if (pipe->params)
{
GimpPixPipeParams params;
gchar *paramstring;
/* Since we do not (yet) load the pipe as described in the
* header, but use one layer per brush, we have to alter the
* paramstring before attaching it as a parasite.
*
* (this comment copied over from file-gih, whatever "as
* described in the header" means) -- mitch
*/
gimp_pixpipe_params_init (¶ms);
gimp_pixpipe_params_parse (pipe->params, ¶ms);
params.cellwidth = gimp_image_get_width (image);
params.cellheight = gimp_image_get_height (image);
params.cols = 1;
params.rows = 1;
paramstring = gimp_pixpipe_params_build (¶ms);
if (paramstring)
{
parasite = gimp_parasite_new ("gimp-brush-pipe-parameters",
GIMP_PARASITE_PERSISTENT,
strlen (paramstring) + 1,
paramstring);
gimp_image_parasite_attach (image, parasite, FALSE);
gimp_parasite_free (parasite);
g_free (paramstring);
}
gimp_pixpipe_params_free (¶ms);
}
return image;
}
static GimpBrushPipe *
file_gih_image_to_pipe (GimpImage *image,
const gchar *name,
gdouble spacing,
const gchar *paramstring)
{
GimpBrushPipe *pipe;
GimpPixPipeParams params;
GList *layers;
GList *list;
GList *brushes = NULL;
gint image_width;
gint image_height;
gint i;
pipe = g_object_new (GIMP_TYPE_BRUSH_PIPE,
"name", name,
"mime-type", "image/x-gimp-gih",
"spacing", spacing,
NULL);
gimp_pixpipe_params_init (¶ms);
gimp_pixpipe_params_parse (paramstring, ¶ms);
image_width = gimp_image_get_width (image);
image_height = gimp_image_get_height (image);
layers = gimp_image_get_layer_iter (image);
for (list = layers; list; list = g_list_next (list))
{
GimpLayer *layer = list->data;
gint width;
gint height;
gint offset_x;
gint offset_y;
gint row;
width = gimp_item_get_width (GIMP_ITEM (layer));
height = gimp_item_get_height (GIMP_ITEM (layer));
gimp_item_get_offset (GIMP_ITEM (layer), &offset_x, &offset_y);
/* Since we assume positive layer offsets we need to make sure this
* is always the case or we will crash for grayscale layers.
* See issue #6436. */
if (offset_x < 0)
{
g_warning (_("Negative x offset: %d for layer %s corrected."),
offset_x, gimp_object_get_name (layer));
width += offset_x;
offset_x = 0;
}
if (offset_y < 0)
{
g_warning (_("Negative y offset: %d for layer %s corrected."),
offset_y, gimp_object_get_name (layer));
height += offset_y;
offset_y = 0;
}
for (row = 0; row < params.rows; row++)
{
gint y, ynext;
gint thisy, thish;
gint col;
y = (row * image_height) / params.rows;
ynext = ((row + 1) * image_height / params.rows);
/* Assume layer is offset to positive direction in x and y.
* That's reasonable, as otherwise all of the layer
* won't be visible.
* thisy and thisx are in the drawable's coordinate space.
*/
thisy = MAX (0, y - offset_y);
thish = (ynext - offset_y) - thisy;
thish = MIN (thish, height - thisy);
for (col = 0; col < params.cols; col++)
{
GimpBrush *brush;
gint x, xnext;
gint thisx, thisw;
x = (col * image_width / params.cols);
xnext = ((col + 1) * image_width / params.cols);
thisx = MAX (0, x - offset_x);
thisw = (xnext - offset_x) - thisx;
thisw = MIN (thisw, width - thisx);
brush = file_gbr_drawable_to_brush (GIMP_DRAWABLE (layer),
GEGL_RECTANGLE (thisx, thisy,
thisw, thish),
gimp_object_get_name (layer),
spacing);
brushes = g_list_prepend (brushes, brush);
}
}
}
brushes = g_list_reverse (brushes);
pipe->n_brushes = g_list_length (brushes);
pipe->brushes = g_new0 (GimpBrush *, pipe->n_brushes);
for (list = brushes, i = 0; list; list = g_list_next (list), i++)
pipe->brushes[i] = list->data;
g_list_free (brushes);
gimp_pixpipe_params_free (¶ms);
gimp_brush_pipe_set_params (pipe, paramstring);
return pipe;
}
|