2006-12-09 21:33:38 +00:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2004-07-22 13:58:29 +00:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2009-01-17 22:28:01 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2004-07-22 13:58:29 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-17 22:28:01 +00:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2004-07-22 13:58:29 +00:00
|
|
|
* (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
|
2018-07-11 23:27:07 +02:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2004-07-22 13:58:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2011-04-28 15:50:39 +02:00
|
|
|
#include <cairo.h>
|
2012-03-30 15:08:54 +02:00
|
|
|
#include <gegl.h>
|
2012-05-03 03:36:22 +02:00
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
2004-07-22 13:58:29 +00:00
|
|
|
|
|
|
|
#include "libgimpbase/gimpbase.h"
|
2004-07-22 19:07:28 +00:00
|
|
|
#include "libgimpcolor/gimpcolor.h"
|
2004-07-22 13:58:29 +00:00
|
|
|
|
|
|
|
#include "core-types.h"
|
|
|
|
|
2004-07-22 19:07:28 +00:00
|
|
|
#include "config/gimpxmlparser.h"
|
|
|
|
|
Issue #1682 - Segfault when starting GIMP, due to empty data files
Use gimp_input_data_stream_read_line_always(), instead of
g_input_data_stream_read_line(), in a bunch of places that don't
expect EOF. If we don't do that, the code assumes the GError
parameter is set by the function and returns an error indication,
causing the caller to segfault when it tries to access
error->message. Instead, we now process an empty line when EOF is
reached, which is caught by the normal parsing logic.
Additionally:
- Use gimp_ascii_strto[id]() when loading gradients, generated
brushes, and palettes, to improve error checking for invalid
numeric input.
- Improve gradient-segment endpoint consistency check.
- Allow loading palette files with 0 colors. They can be created
during the session, so we might as well successfully load them.
2018-06-20 14:47:15 -04:00
|
|
|
#include "gimp-utils.h"
|
2004-07-22 13:58:29 +00:00
|
|
|
#include "gimpgradient.h"
|
|
|
|
#include "gimpgradient-load.h"
|
|
|
|
|
|
|
|
#include "gimp-intl.h"
|
|
|
|
|
|
|
|
|
2004-07-26 19:00:22 +00:00
|
|
|
GList *
|
2014-07-04 03:31:03 +02:00
|
|
|
gimp_gradient_load (GimpContext *context,
|
|
|
|
GFile *file,
|
|
|
|
GInputStream *input,
|
|
|
|
GError **error)
|
2004-07-22 13:58:29 +00:00
|
|
|
{
|
2014-07-04 00:15:02 +02:00
|
|
|
GimpGradient *gradient = NULL;
|
2004-07-22 13:58:29 +00:00
|
|
|
GimpGradientSegment *prev;
|
|
|
|
gint num_segments;
|
|
|
|
gint i;
|
2014-07-04 00:15:02 +02:00
|
|
|
GDataInputStream *data_input;
|
|
|
|
gchar *line;
|
|
|
|
gsize line_len;
|
2006-10-04 17:50:35 +00:00
|
|
|
gint linenum;
|
2004-07-22 13:58:29 +00:00
|
|
|
|
2014-07-01 02:30:22 +02:00
|
|
|
g_return_val_if_fail (G_IS_FILE (file), NULL);
|
2014-07-04 03:31:03 +02:00
|
|
|
g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
|
2014-07-01 02:30:22 +02:00
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
2006-10-04 17:50:35 +00:00
|
|
|
|
2014-07-04 00:15:02 +02:00
|
|
|
data_input = g_data_input_stream_new (input);
|
|
|
|
|
2006-10-04 17:50:35 +00:00
|
|
|
linenum = 1;
|
2014-07-04 00:15:02 +02:00
|
|
|
line_len = 1024;
|
Issue #1682 - Segfault when starting GIMP, due to empty data files
Use gimp_input_data_stream_read_line_always(), instead of
g_input_data_stream_read_line(), in a bunch of places that don't
expect EOF. If we don't do that, the code assumes the GError
parameter is set by the function and returns an error indication,
causing the caller to segfault when it tries to access
error->message. Instead, we now process an empty line when EOF is
reached, which is caught by the normal parsing logic.
Additionally:
- Use gimp_ascii_strto[id]() when loading gradients, generated
brushes, and palettes, to improve error checking for invalid
numeric input.
- Improve gradient-segment endpoint consistency check.
- Allow loading palette files with 0 colors. They can be created
during the session, so we might as well successfully load them.
2018-06-20 14:47:15 -04:00
|
|
|
line = gimp_data_input_stream_read_line_always (data_input, &line_len,
|
|
|
|
NULL, error);
|
2014-07-04 00:15:02 +02:00
|
|
|
if (! line)
|
|
|
|
goto failed;
|
2006-10-04 17:50:35 +00:00
|
|
|
|
2006-11-12 20:30:50 +00:00
|
|
|
if (! g_str_has_prefix (line, "GIMP Gradient"))
|
2004-07-22 13:58:29 +00:00
|
|
|
{
|
2014-07-04 18:46:02 +02:00
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2014-07-04 00:15:02 +02:00
|
|
|
_("Not a GIMP gradient file."));
|
|
|
|
g_free (line);
|
|
|
|
goto failed;
|
2004-07-22 13:58:29 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 00:15:02 +02:00
|
|
|
g_free (line);
|
|
|
|
|
2005-05-25 23:25:45 +00:00
|
|
|
gradient = g_object_new (GIMP_TYPE_GRADIENT,
|
|
|
|
"mime-type", "application/x-gimp-gradient",
|
|
|
|
NULL);
|
2004-07-22 13:58:29 +00:00
|
|
|
|
2006-10-04 17:50:35 +00:00
|
|
|
linenum++;
|
2014-07-04 00:15:02 +02:00
|
|
|
line_len = 1024;
|
Issue #1682 - Segfault when starting GIMP, due to empty data files
Use gimp_input_data_stream_read_line_always(), instead of
g_input_data_stream_read_line(), in a bunch of places that don't
expect EOF. If we don't do that, the code assumes the GError
parameter is set by the function and returns an error indication,
causing the caller to segfault when it tries to access
error->message. Instead, we now process an empty line when EOF is
reached, which is caught by the normal parsing logic.
Additionally:
- Use gimp_ascii_strto[id]() when loading gradients, generated
brushes, and palettes, to improve error checking for invalid
numeric input.
- Improve gradient-segment endpoint consistency check.
- Allow loading palette files with 0 colors. They can be created
during the session, so we might as well successfully load them.
2018-06-20 14:47:15 -04:00
|
|
|
line = gimp_data_input_stream_read_line_always (data_input, &line_len,
|
|
|
|
NULL, error);
|
2014-07-04 00:15:02 +02:00
|
|
|
if (! line)
|
|
|
|
goto failed;
|
2006-10-04 17:50:35 +00:00
|
|
|
|
2006-11-12 20:30:50 +00:00
|
|
|
if (g_str_has_prefix (line, "Name: "))
|
2004-07-22 13:58:29 +00:00
|
|
|
{
|
|
|
|
gchar *utf8;
|
|
|
|
|
2006-10-04 17:50:35 +00:00
|
|
|
utf8 = gimp_any_to_utf8 (g_strstrip (line + strlen ("Name: ")), -1,
|
2004-07-22 13:58:29 +00:00
|
|
|
_("Invalid UTF-8 string in gradient file '%s'."),
|
2014-07-01 14:25:37 +02:00
|
|
|
gimp_file_get_utf8_name (file));
|
2006-10-04 17:50:35 +00:00
|
|
|
gimp_object_take_name (GIMP_OBJECT (gradient), utf8);
|
2004-07-22 13:58:29 +00:00
|
|
|
|
2014-07-04 00:15:02 +02:00
|
|
|
g_free (line);
|
|
|
|
|
2006-10-04 17:50:35 +00:00
|
|
|
linenum++;
|
2014-07-04 00:15:02 +02:00
|
|
|
line_len = 1024;
|
Issue #1682 - Segfault when starting GIMP, due to empty data files
Use gimp_input_data_stream_read_line_always(), instead of
g_input_data_stream_read_line(), in a bunch of places that don't
expect EOF. If we don't do that, the code assumes the GError
parameter is set by the function and returns an error indication,
causing the caller to segfault when it tries to access
error->message. Instead, we now process an empty line when EOF is
reached, which is caught by the normal parsing logic.
Additionally:
- Use gimp_ascii_strto[id]() when loading gradients, generated
brushes, and palettes, to improve error checking for invalid
numeric input.
- Improve gradient-segment endpoint consistency check.
- Allow loading palette files with 0 colors. They can be created
during the session, so we might as well successfully load them.
2018-06-20 14:47:15 -04:00
|
|
|
line = gimp_data_input_stream_read_line_always (data_input, &line_len,
|
|
|
|
NULL, error);
|
2014-07-04 00:15:02 +02:00
|
|
|
if (! line)
|
|
|
|
goto failed;
|
2004-07-22 13:58:29 +00:00
|
|
|
}
|
|
|
|
else /* old gradient format */
|
|
|
|
{
|
2006-04-07 10:51:22 +00:00
|
|
|
gimp_object_take_name (GIMP_OBJECT (gradient),
|
2014-07-01 14:25:37 +02:00
|
|
|
g_path_get_basename (gimp_file_get_utf8_name (file)));
|
2004-07-22 13:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
num_segments = atoi (line);
|
|
|
|
|
2014-07-04 00:15:02 +02:00
|
|
|
g_free (line);
|
|
|
|
|
2004-07-22 13:58:29 +00:00
|
|
|
if (num_segments < 1)
|
|
|
|
{
|
2014-07-04 18:46:02 +02:00
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2014-07-04 00:15:02 +02:00
|
|
|
_("File is corrupt."));
|
|
|
|
goto failed;
|
2004-07-22 13:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prev = NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < num_segments; i++)
|
|
|
|
{
|
2006-08-31 23:57:47 +00:00
|
|
|
GimpGradientSegment *seg;
|
|
|
|
gchar *end;
|
Issue #1682 - Segfault when starting GIMP, due to empty data files
Use gimp_input_data_stream_read_line_always(), instead of
g_input_data_stream_read_line(), in a bunch of places that don't
expect EOF. If we don't do that, the code assumes the GError
parameter is set by the function and returns an error indication,
causing the caller to segfault when it tries to access
error->message. Instead, we now process an empty line when EOF is
reached, which is caught by the normal parsing logic.
Additionally:
- Use gimp_ascii_strto[id]() when loading gradients, generated
brushes, and palettes, to improve error checking for invalid
numeric input.
- Improve gradient-segment endpoint consistency check.
- Allow loading palette files with 0 colors. They can be created
during the session, so we might as well successfully load them.
2018-06-20 14:47:15 -04:00
|
|
|
gint color;
|
|
|
|
gint type;
|
|
|
|
gint left_color_type;
|
|
|
|
gint right_color_type;
|
2023-11-26 21:37:52 +01:00
|
|
|
gdouble left_rgba[4];
|
|
|
|
gdouble right_rgba[4];
|
2004-07-22 13:58:29 +00:00
|
|
|
|
|
|
|
seg = gimp_gradient_segment_new ();
|
|
|
|
|
|
|
|
seg->prev = prev;
|
|
|
|
|
|
|
|
if (prev)
|
2006-04-12 12:49:29 +00:00
|
|
|
prev->next = seg;
|
2004-07-22 13:58:29 +00:00
|
|
|
else
|
2006-04-12 12:49:29 +00:00
|
|
|
gradient->segments = seg;
|
2004-07-22 13:58:29 +00:00
|
|
|
|
2006-10-04 17:50:35 +00:00
|
|
|
linenum++;
|
2014-07-04 00:15:02 +02:00
|
|
|
line_len = 1024;
|
Issue #1682 - Segfault when starting GIMP, due to empty data files
Use gimp_input_data_stream_read_line_always(), instead of
g_input_data_stream_read_line(), in a bunch of places that don't
expect EOF. If we don't do that, the code assumes the GError
parameter is set by the function and returns an error indication,
causing the caller to segfault when it tries to access
error->message. Instead, we now process an empty line when EOF is
reached, which is caught by the normal parsing logic.
Additionally:
- Use gimp_ascii_strto[id]() when loading gradients, generated
brushes, and palettes, to improve error checking for invalid
numeric input.
- Improve gradient-segment endpoint consistency check.
- Allow loading palette files with 0 colors. They can be created
during the session, so we might as well successfully load them.
2018-06-20 14:47:15 -04:00
|
|
|
line = gimp_data_input_stream_read_line_always (data_input, &line_len,
|
|
|
|
NULL, error);
|
2014-07-04 00:15:02 +02:00
|
|
|
if (! line)
|
|
|
|
goto failed;
|
2004-07-22 13:58:29 +00:00
|
|
|
|
2023-11-26 21:37:52 +01:00
|
|
|
if (! gimp_ascii_strtod (line, &end, &seg->left) ||
|
|
|
|
! gimp_ascii_strtod (end, &end, &seg->middle) ||
|
|
|
|
! gimp_ascii_strtod (end, &end, &seg->right) ||
|
Issue #1682 - Segfault when starting GIMP, due to empty data files
Use gimp_input_data_stream_read_line_always(), instead of
g_input_data_stream_read_line(), in a bunch of places that don't
expect EOF. If we don't do that, the code assumes the GError
parameter is set by the function and returns an error indication,
causing the caller to segfault when it tries to access
error->message. Instead, we now process an empty line when EOF is
reached, which is caught by the normal parsing logic.
Additionally:
- Use gimp_ascii_strto[id]() when loading gradients, generated
brushes, and palettes, to improve error checking for invalid
numeric input.
- Improve gradient-segment endpoint consistency check.
- Allow loading palette files with 0 colors. They can be created
during the session, so we might as well successfully load them.
2018-06-20 14:47:15 -04:00
|
|
|
|
2023-11-26 21:37:52 +01:00
|
|
|
! gimp_ascii_strtod (end, &end, &left_rgba[0]) ||
|
|
|
|
! gimp_ascii_strtod (end, &end, &left_rgba[1]) ||
|
|
|
|
! gimp_ascii_strtod (end, &end, &left_rgba[2]) ||
|
|
|
|
! gimp_ascii_strtod (end, &end, &left_rgba[3]) ||
|
Issue #1682 - Segfault when starting GIMP, due to empty data files
Use gimp_input_data_stream_read_line_always(), instead of
g_input_data_stream_read_line(), in a bunch of places that don't
expect EOF. If we don't do that, the code assumes the GError
parameter is set by the function and returns an error indication,
causing the caller to segfault when it tries to access
error->message. Instead, we now process an empty line when EOF is
reached, which is caught by the normal parsing logic.
Additionally:
- Use gimp_ascii_strto[id]() when loading gradients, generated
brushes, and palettes, to improve error checking for invalid
numeric input.
- Improve gradient-segment endpoint consistency check.
- Allow loading palette files with 0 colors. They can be created
during the session, so we might as well successfully load them.
2018-06-20 14:47:15 -04:00
|
|
|
|
2023-11-26 21:37:52 +01:00
|
|
|
! gimp_ascii_strtod (end, &end, &right_rgba[0]) ||
|
|
|
|
! gimp_ascii_strtod (end, &end, &right_rgba[1]) ||
|
|
|
|
! gimp_ascii_strtod (end, &end, &right_rgba[2]) ||
|
|
|
|
! gimp_ascii_strtod (end, &end, &right_rgba[3]))
|
Issue #1682 - Segfault when starting GIMP, due to empty data files
Use gimp_input_data_stream_read_line_always(), instead of
g_input_data_stream_read_line(), in a bunch of places that don't
expect EOF. If we don't do that, the code assumes the GError
parameter is set by the function and returns an error indication,
causing the caller to segfault when it tries to access
error->message. Instead, we now process an empty line when EOF is
reached, which is caught by the normal parsing logic.
Additionally:
- Use gimp_ascii_strto[id]() when loading gradients, generated
brushes, and palettes, to improve error checking for invalid
numeric input.
- Improve gradient-segment endpoint consistency check.
- Allow loading palette files with 0 colors. They can be created
during the session, so we might as well successfully load them.
2018-06-20 14:47:15 -04:00
|
|
|
{
|
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
|
|
|
_("Corrupt segment %d."), i);
|
|
|
|
g_free (line);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2023-11-26 21:37:52 +01:00
|
|
|
gegl_color_set_pixel (seg->left_color, babl_format ("R'G'B'A double"), left_rgba);
|
|
|
|
gegl_color_set_pixel (seg->right_color, babl_format ("R'G'B'A double"), right_rgba);
|
|
|
|
|
Issue #1682 - Segfault when starting GIMP, due to empty data files
Use gimp_input_data_stream_read_line_always(), instead of
g_input_data_stream_read_line(), in a bunch of places that don't
expect EOF. If we don't do that, the code assumes the GError
parameter is set by the function and returns an error indication,
causing the caller to segfault when it tries to access
error->message. Instead, we now process an empty line when EOF is
reached, which is caught by the normal parsing logic.
Additionally:
- Use gimp_ascii_strto[id]() when loading gradients, generated
brushes, and palettes, to improve error checking for invalid
numeric input.
- Improve gradient-segment endpoint consistency check.
- Allow loading palette files with 0 colors. They can be created
during the session, so we might as well successfully load them.
2018-06-20 14:47:15 -04:00
|
|
|
switch (sscanf (end, "%d %d %d %d",
|
|
|
|
&type, &color,
|
|
|
|
&left_color_type, &right_color_type))
|
2004-07-22 13:58:29 +00:00
|
|
|
{
|
Issue #1682 - Segfault when starting GIMP, due to empty data files
Use gimp_input_data_stream_read_line_always(), instead of
g_input_data_stream_read_line(), in a bunch of places that don't
expect EOF. If we don't do that, the code assumes the GError
parameter is set by the function and returns an error indication,
causing the caller to segfault when it tries to access
error->message. Instead, we now process an empty line when EOF is
reached, which is caught by the normal parsing logic.
Additionally:
- Use gimp_ascii_strto[id]() when loading gradients, generated
brushes, and palettes, to improve error checking for invalid
numeric input.
- Improve gradient-segment endpoint consistency check.
- Allow loading palette files with 0 colors. They can be created
during the session, so we might as well successfully load them.
2018-06-20 14:47:15 -04:00
|
|
|
case 4:
|
|
|
|
seg->left_color_type = (GimpGradientColor) left_color_type;
|
|
|
|
if (seg->left_color_type < GIMP_GRADIENT_COLOR_FIXED ||
|
|
|
|
seg->left_color_type > GIMP_GRADIENT_COLOR_BACKGROUND_TRANSPARENT)
|
2006-08-31 23:57:47 +00:00
|
|
|
{
|
2014-07-04 18:46:02 +02:00
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2014-07-04 00:15:02 +02:00
|
|
|
_("Corrupt segment %d."), i);
|
2014-07-20 21:40:57 +02:00
|
|
|
g_free (line);
|
2014-07-04 00:15:02 +02:00
|
|
|
goto failed;
|
2006-08-31 23:57:47 +00:00
|
|
|
}
|
Issue #1682 - Segfault when starting GIMP, due to empty data files
Use gimp_input_data_stream_read_line_always(), instead of
g_input_data_stream_read_line(), in a bunch of places that don't
expect EOF. If we don't do that, the code assumes the GError
parameter is set by the function and returns an error indication,
causing the caller to segfault when it tries to access
error->message. Instead, we now process an empty line when EOF is
reached, which is caught by the normal parsing logic.
Additionally:
- Use gimp_ascii_strto[id]() when loading gradients, generated
brushes, and palettes, to improve error checking for invalid
numeric input.
- Improve gradient-segment endpoint consistency check.
- Allow loading palette files with 0 colors. They can be created
during the session, so we might as well successfully load them.
2018-06-20 14:47:15 -04:00
|
|
|
|
|
|
|
seg->right_color_type = (GimpGradientColor) right_color_type;
|
|
|
|
if (seg->right_color_type < GIMP_GRADIENT_COLOR_FIXED ||
|
|
|
|
seg->right_color_type > GIMP_GRADIENT_COLOR_BACKGROUND_TRANSPARENT)
|
|
|
|
{
|
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
|
|
|
_("Corrupt segment %d."), i);
|
|
|
|
g_free (line);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
/* fall thru */
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
seg->type = (GimpGradientSegmentType) type;
|
|
|
|
if (seg->type < GIMP_GRADIENT_SEGMENT_LINEAR ||
|
2018-10-02 21:13:16 -04:00
|
|
|
seg->type > GIMP_GRADIENT_SEGMENT_STEP)
|
Issue #1682 - Segfault when starting GIMP, due to empty data files
Use gimp_input_data_stream_read_line_always(), instead of
g_input_data_stream_read_line(), in a bunch of places that don't
expect EOF. If we don't do that, the code assumes the GError
parameter is set by the function and returns an error indication,
causing the caller to segfault when it tries to access
error->message. Instead, we now process an empty line when EOF is
reached, which is caught by the normal parsing logic.
Additionally:
- Use gimp_ascii_strto[id]() when loading gradients, generated
brushes, and palettes, to improve error checking for invalid
numeric input.
- Improve gradient-segment endpoint consistency check.
- Allow loading palette files with 0 colors. They can be created
during the session, so we might as well successfully load them.
2018-06-20 14:47:15 -04:00
|
|
|
{
|
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
|
|
|
_("Corrupt segment %d."), i);
|
|
|
|
g_free (line);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
seg->color = (GimpGradientSegmentColor) color;
|
|
|
|
if (seg->color < GIMP_GRADIENT_SEGMENT_RGB ||
|
|
|
|
seg->color > GIMP_GRADIENT_SEGMENT_HSV_CW)
|
|
|
|
{
|
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
|
|
|
_("Corrupt segment %d."), i);
|
|
|
|
g_free (line);
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-07-04 18:46:02 +02:00
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2014-07-04 00:15:02 +02:00
|
|
|
_("Corrupt segment %d."), i);
|
2014-07-20 21:40:57 +02:00
|
|
|
g_free (line);
|
2014-07-04 00:15:02 +02:00
|
|
|
goto failed;
|
2006-04-12 12:49:29 +00:00
|
|
|
}
|
2004-07-22 13:58:29 +00:00
|
|
|
|
2014-07-20 21:40:57 +02:00
|
|
|
g_free (line);
|
|
|
|
|
Issue #1682 - Segfault when starting GIMP, due to empty data files
Use gimp_input_data_stream_read_line_always(), instead of
g_input_data_stream_read_line(), in a bunch of places that don't
expect EOF. If we don't do that, the code assumes the GError
parameter is set by the function and returns an error indication,
causing the caller to segfault when it tries to access
error->message. Instead, we now process an empty line when EOF is
reached, which is caught by the normal parsing logic.
Additionally:
- Use gimp_ascii_strto[id]() when loading gradients, generated
brushes, and palettes, to improve error checking for invalid
numeric input.
- Improve gradient-segment endpoint consistency check.
- Allow loading palette files with 0 colors. They can be created
during the session, so we might as well successfully load them.
2018-06-20 14:47:15 -04:00
|
|
|
if (seg->left > seg->middle ||
|
|
|
|
seg->middle > seg->right ||
|
|
|
|
( prev && (prev->right != seg->left)) ||
|
|
|
|
(! prev && (0.0 != seg->left)))
|
2004-12-16 22:59:27 +00:00
|
|
|
{
|
2014-07-04 18:46:02 +02:00
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2014-07-04 00:15:02 +02:00
|
|
|
_("Segments do not span the range 0-1."));
|
|
|
|
goto failed;
|
2004-12-16 22:59:27 +00:00
|
|
|
}
|
|
|
|
|
2004-07-22 13:58:29 +00:00
|
|
|
prev = seg;
|
|
|
|
}
|
|
|
|
|
Issue #1682 - Segfault when starting GIMP, due to empty data files
Use gimp_input_data_stream_read_line_always(), instead of
g_input_data_stream_read_line(), in a bunch of places that don't
expect EOF. If we don't do that, the code assumes the GError
parameter is set by the function and returns an error indication,
causing the caller to segfault when it tries to access
error->message. Instead, we now process an empty line when EOF is
reached, which is caught by the normal parsing logic.
Additionally:
- Use gimp_ascii_strto[id]() when loading gradients, generated
brushes, and palettes, to improve error checking for invalid
numeric input.
- Improve gradient-segment endpoint consistency check.
- Allow loading palette files with 0 colors. They can be created
during the session, so we might as well successfully load them.
2018-06-20 14:47:15 -04:00
|
|
|
if (prev->right != 1.0)
|
2004-12-16 22:59:27 +00:00
|
|
|
{
|
2014-07-04 18:46:02 +02:00
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2014-07-04 00:15:02 +02:00
|
|
|
_("Segments do not span the range 0-1."));
|
|
|
|
goto failed;
|
2004-12-16 22:59:27 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 00:15:02 +02:00
|
|
|
g_object_unref (data_input);
|
2004-07-22 13:58:29 +00:00
|
|
|
|
2004-07-26 19:00:22 +00:00
|
|
|
return g_list_prepend (NULL, gradient);
|
2014-07-04 00:15:02 +02:00
|
|
|
|
|
|
|
failed:
|
|
|
|
|
|
|
|
g_object_unref (data_input);
|
|
|
|
|
|
|
|
if (gradient)
|
|
|
|
g_object_unref (gradient);
|
|
|
|
|
2014-07-04 18:46:02 +02:00
|
|
|
g_prefix_error (error, _("In line %d of gradient file: "), linenum);
|
2014-07-04 00:15:02 +02:00
|
|
|
|
|
|
|
return NULL;
|
2004-07-22 13:58:29 +00:00
|
|
|
}
|
2004-07-22 19:07:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* SVG gradient parser */
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2004-07-26 19:19:04 +00:00
|
|
|
GimpGradient *gradient; /* current gradient */
|
|
|
|
GList *gradients; /* finished gradients */
|
|
|
|
GList *stops;
|
2004-07-22 19:07:28 +00:00
|
|
|
} SvgParser;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2004-07-23 11:31:16 +00:00
|
|
|
gdouble offset;
|
2023-11-26 21:37:52 +01:00
|
|
|
GeglColor *color;
|
|
|
|
gdouble opacity;
|
2004-07-22 19:07:28 +00:00
|
|
|
} SvgStop;
|
|
|
|
|
|
|
|
|
|
|
|
static void svg_parser_start_element (GMarkupParseContext *context,
|
|
|
|
const gchar *element_name,
|
|
|
|
const gchar **attribute_names,
|
|
|
|
const gchar **attribute_values,
|
|
|
|
gpointer user_data,
|
|
|
|
GError **error);
|
|
|
|
static void svg_parser_end_element (GMarkupParseContext *context,
|
|
|
|
const gchar *element_name,
|
|
|
|
gpointer user_data,
|
|
|
|
GError **error);
|
2004-07-23 11:31:16 +00:00
|
|
|
|
2004-07-23 14:00:38 +00:00
|
|
|
static GimpGradientSegment *
|
|
|
|
svg_parser_gradient_segments (GList *stops);
|
|
|
|
|
|
|
|
static SvgStop * svg_parse_gradient_stop (const gchar **names,
|
|
|
|
const gchar **values);
|
2004-07-22 19:07:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
static const GMarkupParser markup_parser =
|
|
|
|
{
|
|
|
|
svg_parser_start_element,
|
|
|
|
svg_parser_end_element,
|
|
|
|
NULL, /* characters */
|
|
|
|
NULL, /* passthrough */
|
|
|
|
NULL /* error */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-07-26 19:00:22 +00:00
|
|
|
GList *
|
2014-07-04 03:31:03 +02:00
|
|
|
gimp_gradient_load_svg (GimpContext *context,
|
|
|
|
GFile *file,
|
|
|
|
GInputStream *input,
|
|
|
|
GError **error)
|
2004-07-22 19:07:28 +00:00
|
|
|
{
|
|
|
|
GimpXmlParser *xml_parser;
|
2007-05-23 10:22:09 +00:00
|
|
|
SvgParser parser = { NULL, };
|
2004-07-22 19:07:28 +00:00
|
|
|
gboolean success;
|
|
|
|
|
2014-07-01 02:30:22 +02:00
|
|
|
g_return_val_if_fail (G_IS_FILE (file), NULL);
|
2014-07-04 03:31:03 +02:00
|
|
|
g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
|
2004-07-22 19:07:28 +00:00
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
2014-07-04 03:31:03 +02:00
|
|
|
/* FIXME input */
|
|
|
|
g_input_stream_close (input, NULL, NULL);
|
|
|
|
|
2004-07-22 19:07:28 +00:00
|
|
|
xml_parser = gimp_xml_parser_new (&markup_parser, &parser);
|
|
|
|
|
2014-07-01 02:30:22 +02:00
|
|
|
success = gimp_xml_parser_parse_gfile (xml_parser, file, error);
|
2004-07-22 19:07:28 +00:00
|
|
|
|
|
|
|
gimp_xml_parser_free (xml_parser);
|
|
|
|
|
2004-07-26 19:19:04 +00:00
|
|
|
if (success && ! parser.gradients)
|
2004-07-23 01:08:34 +00:00
|
|
|
{
|
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2014-07-04 18:46:02 +02:00
|
|
|
_("No linear gradients found."));
|
2004-07-23 01:08:34 +00:00
|
|
|
}
|
2004-07-22 19:07:28 +00:00
|
|
|
|
|
|
|
if (parser.gradient)
|
|
|
|
g_object_unref (parser.gradient);
|
|
|
|
|
2004-07-26 19:19:04 +00:00
|
|
|
if (parser.stops)
|
|
|
|
{
|
2007-05-23 10:22:09 +00:00
|
|
|
GList *list;
|
|
|
|
|
|
|
|
for (list = parser.stops; list; list = list->next)
|
|
|
|
g_slice_free (SvgStop, list->data);
|
|
|
|
|
2004-07-26 19:19:04 +00:00
|
|
|
g_list_free (parser.stops);
|
|
|
|
}
|
2004-07-22 19:07:28 +00:00
|
|
|
|
2004-07-26 19:19:04 +00:00
|
|
|
return g_list_reverse (parser.gradients);
|
2004-07-22 19:07:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
svg_parser_start_element (GMarkupParseContext *context,
|
|
|
|
const gchar *element_name,
|
|
|
|
const gchar **attribute_names,
|
|
|
|
const gchar **attribute_values,
|
|
|
|
gpointer user_data,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
SvgParser *parser = user_data;
|
|
|
|
|
2004-07-26 19:19:04 +00:00
|
|
|
if (! parser->gradient && strcmp (element_name, "linearGradient") == 0)
|
2004-07-22 19:07:28 +00:00
|
|
|
{
|
2004-07-26 19:19:04 +00:00
|
|
|
const gchar *name = NULL;
|
2004-07-22 19:07:28 +00:00
|
|
|
|
2004-07-26 19:19:04 +00:00
|
|
|
while (*attribute_names && *attribute_values)
|
2004-07-22 19:07:28 +00:00
|
|
|
{
|
2004-07-26 19:19:04 +00:00
|
|
|
if (strcmp (*attribute_names, "id") == 0 && *attribute_values)
|
|
|
|
name = *attribute_values;
|
2004-07-22 19:07:28 +00:00
|
|
|
|
2004-07-26 19:19:04 +00:00
|
|
|
attribute_names++;
|
|
|
|
attribute_values++;
|
2004-07-22 19:07:28 +00:00
|
|
|
}
|
|
|
|
|
2004-07-26 19:19:04 +00:00
|
|
|
parser->gradient = g_object_new (GIMP_TYPE_GRADIENT,
|
2005-05-25 23:25:45 +00:00
|
|
|
"name", name,
|
|
|
|
"mime-type", "image/svg+xml",
|
2004-07-26 19:19:04 +00:00
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
else if (parser->gradient && strcmp (element_name, "stop") == 0)
|
|
|
|
{
|
|
|
|
SvgStop *stop = svg_parse_gradient_stop (attribute_names,
|
|
|
|
attribute_values);
|
|
|
|
|
|
|
|
/* The spec clearly states that each gradient stop's offset
|
|
|
|
* value is required to be equal to or greater than the
|
|
|
|
* previous gradient stop's offset value.
|
|
|
|
*/
|
|
|
|
if (parser->stops)
|
|
|
|
stop->offset = MAX (stop->offset,
|
|
|
|
((SvgStop *) parser->stops->data)->offset);
|
|
|
|
|
|
|
|
parser->stops = g_list_prepend (parser->stops, stop);
|
2004-07-22 19:07:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
svg_parser_end_element (GMarkupParseContext *context,
|
|
|
|
const gchar *element_name,
|
|
|
|
gpointer user_data,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
SvgParser *parser = user_data;
|
|
|
|
|
2004-07-26 19:19:04 +00:00
|
|
|
if (parser->gradient &&
|
|
|
|
strcmp (element_name, "linearGradient") == 0)
|
2004-07-23 11:31:16 +00:00
|
|
|
{
|
2014-02-13 22:06:12 +01:00
|
|
|
GList *list;
|
|
|
|
|
2004-07-23 14:00:38 +00:00
|
|
|
parser->gradient->segments = svg_parser_gradient_segments (parser->stops);
|
2004-07-22 19:07:28 +00:00
|
|
|
|
2014-02-13 22:06:12 +01:00
|
|
|
for (list = parser->stops; list; list = list->next)
|
2023-11-26 21:37:52 +01:00
|
|
|
{
|
|
|
|
SvgStop *stop = list->data;
|
|
|
|
|
|
|
|
g_clear_object (&stop->color);
|
|
|
|
g_slice_free (SvgStop, stop);
|
|
|
|
}
|
2014-02-13 22:06:12 +01:00
|
|
|
|
|
|
|
g_list_free (parser->stops);
|
|
|
|
parser->stops = NULL;
|
|
|
|
|
2004-07-26 19:19:04 +00:00
|
|
|
if (parser->gradient->segments)
|
|
|
|
parser->gradients = g_list_prepend (parser->gradients,
|
|
|
|
parser->gradient);
|
|
|
|
else
|
|
|
|
g_object_unref (parser->gradient);
|
|
|
|
|
|
|
|
parser->gradient = NULL;
|
2004-07-23 14:00:38 +00:00
|
|
|
}
|
|
|
|
}
|
2004-07-22 19:07:28 +00:00
|
|
|
|
2004-07-23 14:00:38 +00:00
|
|
|
static GimpGradientSegment *
|
|
|
|
svg_parser_gradient_segments (GList *stops)
|
|
|
|
{
|
2004-07-27 19:34:27 +00:00
|
|
|
GimpGradientSegment *segment;
|
|
|
|
SvgStop *stop;
|
2004-07-23 14:00:38 +00:00
|
|
|
GList *list;
|
2004-07-22 19:07:28 +00:00
|
|
|
|
2004-07-27 19:34:27 +00:00
|
|
|
if (! stops)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
stop = stops->data;
|
|
|
|
|
|
|
|
segment = gimp_gradient_segment_new ();
|
|
|
|
|
2023-11-26 21:37:52 +01:00
|
|
|
g_clear_object (&segment->left_color);
|
|
|
|
segment->left_color = gegl_color_duplicate (stop->color);
|
|
|
|
g_clear_object (&segment->right_color);
|
|
|
|
segment->right_color = gegl_color_duplicate (stop->color);
|
2004-07-22 19:07:28 +00:00
|
|
|
|
2004-07-23 14:00:38 +00:00
|
|
|
/* the list of offsets is sorted from largest to smallest */
|
|
|
|
for (list = g_list_next (stops); list; list = g_list_next (list))
|
|
|
|
{
|
|
|
|
GimpGradientSegment *next = segment;
|
2004-07-23 11:31:16 +00:00
|
|
|
|
2004-07-23 14:00:38 +00:00
|
|
|
segment->left = stop->offset;
|
|
|
|
segment->middle = (segment->left + segment->right) / 2.0;
|
2004-07-23 11:31:16 +00:00
|
|
|
|
2004-07-23 14:00:38 +00:00
|
|
|
segment = gimp_gradient_segment_new ();
|
2004-07-23 11:31:16 +00:00
|
|
|
|
2004-07-23 14:00:38 +00:00
|
|
|
segment->next = next;
|
|
|
|
next->prev = segment;
|
2004-07-23 11:31:16 +00:00
|
|
|
|
2004-07-23 14:00:38 +00:00
|
|
|
segment->right = stop->offset;
|
2023-11-26 21:37:52 +01:00
|
|
|
g_clear_object (&segment->right_color);
|
|
|
|
segment->right_color = gegl_color_duplicate (stop->color);
|
2004-07-23 11:31:16 +00:00
|
|
|
|
2004-07-23 14:00:38 +00:00
|
|
|
stop = list->data;
|
2004-07-23 11:31:16 +00:00
|
|
|
|
2023-11-26 21:37:52 +01:00
|
|
|
g_clear_object (&segment->left_color);
|
|
|
|
segment->left_color = gegl_color_duplicate (stop->color);
|
2004-07-22 19:07:28 +00:00
|
|
|
}
|
|
|
|
|
2004-07-23 14:00:38 +00:00
|
|
|
segment->middle = (segment->left + segment->right) / 2.0;
|
|
|
|
|
|
|
|
if (stop->offset > 0.0)
|
2023-11-26 21:37:52 +01:00
|
|
|
{
|
|
|
|
g_clear_object (&segment->right_color);
|
|
|
|
segment->right_color = gegl_color_duplicate (stop->color);
|
|
|
|
}
|
2004-07-23 14:00:38 +00:00
|
|
|
|
|
|
|
/* FIXME: remove empty segments here or add a GimpGradient API to do that
|
|
|
|
*/
|
|
|
|
|
|
|
|
return segment;
|
|
|
|
}
|
2004-07-23 11:31:16 +00:00
|
|
|
|
2004-07-22 19:07:28 +00:00
|
|
|
static void
|
2004-07-23 11:31:16 +00:00
|
|
|
svg_parse_gradient_stop_style_prop (SvgStop *stop,
|
|
|
|
const gchar *name,
|
|
|
|
const gchar *value)
|
2004-07-22 19:07:28 +00:00
|
|
|
{
|
2004-07-23 11:31:16 +00:00
|
|
|
if (strcmp (name, "stop-color") == 0)
|
|
|
|
{
|
2023-11-26 21:37:52 +01:00
|
|
|
g_clear_object (&stop->color);
|
2024-04-20 12:24:52 +02:00
|
|
|
stop->color = gimp_color_parse_css (value);
|
2004-07-23 11:31:16 +00:00
|
|
|
}
|
|
|
|
else if (strcmp (name, "stop-opacity") == 0)
|
|
|
|
{
|
|
|
|
gdouble opacity = g_ascii_strtod (value, NULL);
|
2004-07-22 19:07:28 +00:00
|
|
|
|
2004-07-23 11:31:16 +00:00
|
|
|
if (errno != ERANGE)
|
2023-11-26 21:37:52 +01:00
|
|
|
stop->opacity = CLAMP (opacity, 0.0, 1.0);
|
2004-07-23 11:31:16 +00:00
|
|
|
}
|
2004-07-22 19:07:28 +00:00
|
|
|
}
|
|
|
|
|
2004-07-23 11:31:16 +00:00
|
|
|
/* very simplistic CSS style parser */
|
2004-07-22 19:07:28 +00:00
|
|
|
static void
|
2004-07-23 11:31:16 +00:00
|
|
|
svg_parse_gradient_stop_style (SvgStop *stop,
|
|
|
|
const gchar *style)
|
2004-07-22 19:07:28 +00:00
|
|
|
{
|
2004-07-23 11:31:16 +00:00
|
|
|
const gchar *end;
|
|
|
|
const gchar *sep;
|
|
|
|
|
|
|
|
while (*style)
|
|
|
|
{
|
|
|
|
while (g_ascii_isspace (*style))
|
|
|
|
style++;
|
|
|
|
|
|
|
|
for (end = style; *end && *end != ';'; end++)
|
|
|
|
/* do nothing */;
|
|
|
|
|
|
|
|
for (sep = style; sep < end && *sep != ':'; sep++)
|
|
|
|
/* do nothing */;
|
2004-07-22 19:07:28 +00:00
|
|
|
|
2004-07-23 11:31:16 +00:00
|
|
|
if (end > sep && sep > style)
|
|
|
|
{
|
2009-07-20 12:39:09 +02:00
|
|
|
gchar *name;
|
|
|
|
gchar *value;
|
|
|
|
|
|
|
|
name = g_strndup (style, sep - style);
|
|
|
|
sep++;
|
2023-11-26 21:37:52 +01:00
|
|
|
value = g_strndup (sep, end - sep);
|
2004-07-23 11:31:16 +00:00
|
|
|
|
|
|
|
svg_parse_gradient_stop_style_prop (stop, name, value);
|
2004-07-22 19:07:28 +00:00
|
|
|
|
2004-07-23 11:31:16 +00:00
|
|
|
g_free (value);
|
|
|
|
g_free (name);
|
|
|
|
}
|
|
|
|
|
|
|
|
style = end;
|
|
|
|
|
|
|
|
if (*style == ';')
|
|
|
|
style++;
|
|
|
|
}
|
2004-07-22 19:07:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SvgStop *
|
|
|
|
svg_parse_gradient_stop (const gchar **names,
|
|
|
|
const gchar **values)
|
|
|
|
{
|
2007-05-23 10:22:09 +00:00
|
|
|
SvgStop *stop = g_slice_new0 (SvgStop);
|
2004-07-22 19:07:28 +00:00
|
|
|
|
2023-11-26 21:37:52 +01:00
|
|
|
stop->color = NULL;
|
|
|
|
stop->opacity = 1.0;
|
2004-07-22 19:07:28 +00:00
|
|
|
|
|
|
|
while (*names && *values)
|
|
|
|
{
|
|
|
|
if (strcmp (*names, "offset") == 0)
|
|
|
|
{
|
|
|
|
gchar *end;
|
|
|
|
|
2004-07-23 11:31:16 +00:00
|
|
|
stop->offset = g_ascii_strtod (*values, &end);
|
2004-07-22 19:07:28 +00:00
|
|
|
|
|
|
|
if (end && *end == '%')
|
2004-07-23 11:31:16 +00:00
|
|
|
stop->offset /= 100.0;
|
2004-07-22 19:07:28 +00:00
|
|
|
|
2004-07-23 11:31:16 +00:00
|
|
|
stop->offset = CLAMP (stop->offset, 0.0, 1.0);
|
2004-07-22 19:07:28 +00:00
|
|
|
}
|
2004-07-23 11:31:16 +00:00
|
|
|
else if (strcmp (*names, "style") == 0)
|
2004-07-22 19:07:28 +00:00
|
|
|
{
|
2004-07-23 11:31:16 +00:00
|
|
|
svg_parse_gradient_stop_style (stop, *values);
|
2004-07-22 19:07:28 +00:00
|
|
|
}
|
2004-07-23 11:31:16 +00:00
|
|
|
else
|
2004-07-22 19:07:28 +00:00
|
|
|
{
|
2004-07-23 11:31:16 +00:00
|
|
|
svg_parse_gradient_stop_style_prop (stop, *names, *values);
|
2004-07-22 19:07:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
names++;
|
|
|
|
values++;
|
|
|
|
}
|
|
|
|
|
2023-11-26 21:37:52 +01:00
|
|
|
if (! stop->color)
|
|
|
|
/* Default stop color is black:
|
|
|
|
* https://svgwg.org/svg2-draft/pservers.html#GradientStops
|
|
|
|
*/
|
|
|
|
stop->color = gegl_color_new ("black");
|
|
|
|
|
|
|
|
gimp_color_set_alpha (stop->color, stop->opacity);
|
|
|
|
|
2004-07-23 11:31:16 +00:00
|
|
|
return stop;
|
2004-07-22 19:07:28 +00:00
|
|
|
}
|