static void gimp_brush_editor_update_brush (GtkAdjustment *adjustment, GimpBrushEditor *editor) { GimpBrushGenerated *brush; gdouble radius; gint spikes; gdouble hardness; gdouble ratio; gdouble angle; gdouble spacing; if (! GIMP_IS_BRUSH_GENERATED (GIMP_DATA_EDITOR (editor)->data)) return; brush = GIMP_BRUSH_GENERATED (GIMP_DATA_EDITOR (editor)->data); radius = gtk_adjustment_get_value (editor->radius_data); spikes = ROUND (gtk_adjustment_get_value (editor->spikes_data)); hardness = gtk_adjustment_get_value (editor->hardness_data); ratio = gtk_adjustment_get_value (editor->aspect_ratio_data); angle = gtk_adjustment_get_value (editor->angle_data); spacing = gtk_adjustment_get_value (editor->spacing_data); if (radius != gimp_brush_generated_get_radius (brush) || spikes != gimp_brush_generated_get_spikes (brush) || hardness != gimp_brush_generated_get_hardness (brush) || ratio != gimp_brush_generated_get_aspect_ratio (brush) || angle != gimp_brush_generated_get_angle (brush) || spacing != gimp_brush_get_spacing (GIMP_BRUSH (brush))) { g_signal_handlers_block_by_func (brush, gimp_brush_editor_notify_brush, editor); gimp_data_freeze (GIMP_DATA (brush)); g_object_freeze_notify (G_OBJECT (brush)); gimp_brush_generated_set_radius (brush, radius); gimp_brush_generated_set_spikes (brush, spikes); gimp_brush_generated_set_hardness (brush, hardness); gimp_brush_generated_set_aspect_ratio (brush, ratio); gimp_brush_generated_set_angle (brush, angle); gimp_brush_set_spacing (GIMP_BRUSH (brush), spacing); g_object_thaw_notify (G_OBJECT (brush)); gimp_data_thaw (GIMP_DATA (brush)); g_signal_handlers_unblock_by_func (brush, gimp_brush_editor_notify_brush, editor); } }
static GValueArray * gimp_brush_select_run_callback (GimpPdbDialog *dialog, GimpObject *object, gboolean closing) { GimpBrush *brush = GIMP_BRUSH (object); GimpArray *array; GValueArray *return_vals; array = gimp_array_new (temp_buf_data (brush->mask), brush->mask->width * brush->mask->height * brush->mask->bytes, TRUE); return_vals = gimp_pdb_execute_procedure_by_name (dialog->pdb, dialog->caller_context, NULL, dialog->callback_name, G_TYPE_STRING, object->name, G_TYPE_DOUBLE, gimp_context_get_opacity (dialog->context) * 100.0, GIMP_TYPE_INT32, GIMP_BRUSH_SELECT (dialog)->spacing, GIMP_TYPE_INT32, gimp_context_get_paint_mode (dialog->context), GIMP_TYPE_INT32, brush->mask->width, GIMP_TYPE_INT32, brush->mask->height, GIMP_TYPE_INT32, array->length, GIMP_TYPE_INT8_ARRAY, array, GIMP_TYPE_INT32, closing, G_TYPE_NONE); gimp_array_free (array); return return_vals; }
static void gimp_brush_editor_set_data (GimpDataEditor *editor, GimpData *data) { GimpBrushEditor *brush_editor = GIMP_BRUSH_EDITOR (editor); GimpBrushGeneratedShape shape = GIMP_BRUSH_GENERATED_CIRCLE; gdouble radius = 0.0; gint spikes = 2; gdouble hardness = 0.0; gdouble ratio = 0.0; gdouble angle = 0.0; gdouble spacing = 0.0; if (editor->data) g_signal_handlers_disconnect_by_func (editor->data, gimp_brush_editor_notify_brush, editor); GIMP_DATA_EDITOR_CLASS (parent_class)->set_data (editor, data); if (editor->data) g_signal_connect (editor->data, "notify", G_CALLBACK (gimp_brush_editor_notify_brush), editor); gimp_view_set_viewable (GIMP_VIEW (editor->view), GIMP_VIEWABLE (data)); if (editor->data) { spacing = gimp_brush_get_spacing (GIMP_BRUSH (editor->data)); if (GIMP_IS_BRUSH_GENERATED (editor->data)) { GimpBrushGenerated *brush = GIMP_BRUSH_GENERATED (editor->data); shape = gimp_brush_generated_get_shape (brush); radius = gimp_brush_generated_get_radius (brush); spikes = gimp_brush_generated_get_spikes (brush); hardness = gimp_brush_generated_get_hardness (brush); ratio = gimp_brush_generated_get_aspect_ratio (brush); angle = gimp_brush_generated_get_angle (brush); } } gtk_widget_set_sensitive (brush_editor->options_box, editor->data_editable); gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (brush_editor->shape_group), shape); gtk_adjustment_set_value (brush_editor->radius_data, radius); gtk_adjustment_set_value (brush_editor->spikes_data, spikes); gtk_adjustment_set_value (brush_editor->hardness_data, hardness); gtk_adjustment_set_value (brush_editor->aspect_ratio_data, ratio); gtk_adjustment_set_value (brush_editor->angle_data, angle); gtk_adjustment_set_value (brush_editor->spacing_data, spacing); }
static void gimp_brush_pipe_finalize (GObject *object) { GimpBrushPipe *pipe = GIMP_BRUSH_PIPE (object); if (pipe->rank) { g_free (pipe->rank); pipe->rank = NULL; } if (pipe->stride) { g_free (pipe->stride); pipe->stride = NULL; } if (pipe->brushes) { gint i; for (i = 0; i < pipe->n_brushes; i++) if (pipe->brushes[i]) g_object_unref (pipe->brushes[i]); g_free (pipe->brushes); pipe->brushes = NULL; } if (pipe->select) { g_free (pipe->select); pipe->select = NULL; } if (pipe->index) { g_free (pipe->index); pipe->index = NULL; } GIMP_BRUSH (pipe)->mask = NULL; GIMP_BRUSH (pipe)->pixmap = NULL; G_OBJECT_CLASS (parent_class)->finalize (object); }
static void brush_preview_drop_brush (GtkWidget *widget, gint x, gint y, GimpViewable *viewable, gpointer data) { GimpContext *context = GIMP_CONTEXT (data); gimp_context_set_brush (context, GIMP_BRUSH (viewable)); }
static void gimp_brush_clipboard_constructed (GObject *object) { GimpBrushClipboard *brush = GIMP_BRUSH_CLIPBOARD (object); G_OBJECT_CLASS (parent_class)->constructed (object); g_assert (GIMP_IS_GIMP (brush->gimp)); g_signal_connect_object (brush->gimp, "buffer-changed", G_CALLBACK (gimp_brush_clipboard_buffer_changed), brush, 0); gimp_brush_clipboard_buffer_changed (brush->gimp, GIMP_BRUSH (brush)); }
static void gimp_brush_generated_dirty (GimpData *data) { GimpBrushGenerated *brush = GIMP_BRUSH_GENERATED (data); GimpBrush *gbrush = GIMP_BRUSH (brush); if (gbrush->mask) temp_buf_free (gbrush->mask); gbrush->mask = gimp_brush_generated_calc (brush, brush->shape, brush->radius, brush->spikes, brush->hardness, brush->aspect_ratio, brush->angle, &gbrush->x_axis, &gbrush->y_axis); GIMP_DATA_CLASS (parent_class)->dirty (data); }
GList * gimp_brush_generated_load (GimpContext *context, GFile *file, GInputStream *input, GError **error) { GimpBrush *brush; GDataInputStream *data_input; gchar *string; gsize string_len; gint linenum; gchar *name = NULL; GimpBrushGeneratedShape shape = GIMP_BRUSH_GENERATED_CIRCLE; gboolean have_shape = FALSE; gint spikes = 2; gdouble spacing; gdouble radius; gdouble hardness; gdouble aspect_ratio; gdouble angle; g_return_val_if_fail (G_IS_FILE (file), NULL); g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); data_input = g_data_input_stream_new (input); /* make sure the file we are reading is the right type */ linenum = 1; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; if (! g_str_has_prefix (string, "GIMP-VBR")) { g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ, _("Not a GIMP brush file.")); g_free (string); goto failed; } g_free (string); /* make sure we are reading a compatible version */ linenum++; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; if (! g_str_has_prefix (string, "1.0")) { if (! g_str_has_prefix (string, "1.5")) { g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ, _("Unknown GIMP brush version.")); g_free (string); goto failed; } else { have_shape = TRUE; } } g_free (string); /* read name */ linenum++; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; g_strstrip (string); /* the empty string is not an allowed name */ if (strlen (string) < 1) { name = g_strdup (_("Untitled")); } else { name = gimp_any_to_utf8 (string, -1, _("Invalid UTF-8 string in brush file '%s'."), gimp_file_get_utf8_name (file)); } g_free (string); if (have_shape) { GEnumClass *enum_class; GEnumValue *shape_val; enum_class = g_type_class_peek (GIMP_TYPE_BRUSH_GENERATED_SHAPE); /* read shape */ linenum++; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; g_strstrip (string); shape_val = g_enum_get_value_by_nick (enum_class, string); if (! shape_val) { g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ, _("Unknown GIMP brush shape.")); g_free (string); goto failed; } g_free (string); shape = shape_val->value; } /* read brush spacing */ linenum++; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; spacing = g_ascii_strtod (string, NULL); g_free (string); /* read brush radius */ linenum++; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; radius = g_ascii_strtod (string, NULL); g_free (string); if (have_shape) { /* read number of spikes */ linenum++; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; spikes = CLAMP (atoi (string), 2, 20); g_free (string); } /* read brush hardness */ linenum++; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; hardness = g_ascii_strtod (string, NULL); g_free (string); /* read brush aspect_ratio */ linenum++; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; aspect_ratio = g_ascii_strtod (string, NULL); g_free (string); /* read brush angle */ linenum++; string_len = 256; string = g_data_input_stream_read_line (data_input, &string_len, NULL, error); if (! string) goto failed; angle = g_ascii_strtod (string, NULL); g_free (string); g_object_unref (data_input); brush = GIMP_BRUSH (gimp_brush_generated_new (name, shape, radius, spikes, hardness, aspect_ratio, angle)); g_free (name); gimp_brush_set_spacing (brush, spacing); return g_list_prepend (NULL, brush); failed: g_object_unref (data_input); if (name) g_free (name); g_prefix_error (error, _("In line %d of brush file: "), linenum); return NULL; }
static void gimp_brush_editor_notify_brush (GimpBrushGenerated *brush, GParamSpec *pspec, GimpBrushEditor *editor) { GtkAdjustment *adj = NULL; gdouble value = 0.0; if (! strcmp (pspec->name, "shape")) { g_signal_handlers_block_by_func (editor->shape_group, gimp_brush_editor_update_shape, editor); gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (editor->shape_group), brush->shape); g_signal_handlers_unblock_by_func (editor->shape_group, gimp_brush_editor_update_shape, editor); adj = editor->radius_data; value = brush->radius; } else if (! strcmp (pspec->name, "radius")) { adj = editor->radius_data; value = brush->radius; } else if (! strcmp (pspec->name, "spikes")) { adj = editor->spikes_data; value = brush->spikes; } else if (! strcmp (pspec->name, "hardness")) { adj = editor->hardness_data; value = brush->hardness; } else if (! strcmp (pspec->name, "angle")) { adj = editor->angle_data; value = brush->angle; } else if (! strcmp (pspec->name, "aspect-ratio")) { adj = editor->aspect_ratio_data; value = brush->aspect_ratio; } else if (! strcmp (pspec->name, "spacing")) { adj = editor->spacing_data; value = GIMP_BRUSH (brush)->spacing; } if (adj) { g_signal_handlers_block_by_func (adj, gimp_brush_editor_update_brush, editor); gtk_adjustment_set_value (adj, value); g_signal_handlers_unblock_by_func (adj, gimp_brush_editor_update_brush, editor); } }
GList * gimp_brush_generated_load (GimpContext *context, const gchar *filename, GError **error) { GimpBrush *brush; FILE *file; gchar string[256]; gint linenum; gchar *name = NULL; GimpBrushGeneratedShape shape = GIMP_BRUSH_GENERATED_CIRCLE; gboolean have_shape = FALSE; gint spikes = 2; gdouble spacing; gdouble radius; gdouble hardness; gdouble aspect_ratio; gdouble angle; g_return_val_if_fail (filename != NULL, NULL); g_return_val_if_fail (g_path_is_absolute (filename), NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); file = g_fopen (filename, "rb"); if (! file) { g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN, _("Could not open '%s' for reading: %s"), gimp_filename_to_utf8 (filename), g_strerror (errno)); return NULL; } /* make sure the file we are reading is the right type */ errno = 0; linenum = 1; if (! fgets (string, sizeof (string), file)) goto failed; if (! g_str_has_prefix (string, "GIMP-VBR")) { g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ, _("Fatal parse error in brush file '%s': " "Not a GIMP brush file."), gimp_filename_to_utf8 (filename)); goto failed; } /* make sure we are reading a compatible version */ errno = 0; linenum++; if (! fgets (string, sizeof (string), file)) goto failed; if (! g_str_has_prefix (string, "1.0")) { if (! g_str_has_prefix (string, "1.5")) { g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ, _("Fatal parse error in brush file '%s': " "Unknown GIMP brush version in line %d."), gimp_filename_to_utf8 (filename), linenum); goto failed; } else { have_shape = TRUE; } } /* read name */ errno = 0; linenum++; if (! fgets (string, sizeof (string), file)) goto failed; g_strstrip (string); /* the empty string is not an allowed name */ if (strlen (string) < 1) g_strlcpy (string, _("Untitled"), sizeof (string)); name = gimp_any_to_utf8 (string, -1, _("Invalid UTF-8 string in brush file '%s'."), gimp_filename_to_utf8 (filename)); if (have_shape) { GEnumClass *enum_class; GEnumValue *shape_val; enum_class = g_type_class_peek (GIMP_TYPE_BRUSH_GENERATED_SHAPE); /* read shape */ errno = 0; linenum++; if (! fgets (string, sizeof (string), file)) goto failed; g_strstrip (string); shape_val = g_enum_get_value_by_nick (enum_class, string); if (! shape_val) { g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ, _("Fatal parse error in brush file '%s': " "Unknown GIMP brush shape in line %d."), gimp_filename_to_utf8 (filename), linenum); goto failed; } shape = shape_val->value; } /* read brush spacing */ errno = 0; linenum++; if (! fgets (string, sizeof (string), file)) goto failed; spacing = g_ascii_strtod (string, NULL); /* read brush radius */ errno = 0; linenum++; if (! fgets (string, sizeof (string), file)) goto failed; radius = g_ascii_strtod (string, NULL); if (have_shape) { /* read number of spikes */ errno = 0; linenum++; if (! fgets (string, sizeof (string), file)) goto failed; spikes = CLAMP (atoi (string), 2, 20); } /* read brush hardness */ errno = 0; linenum++; if (! fgets (string, sizeof (string), file)) goto failed; hardness = g_ascii_strtod (string, NULL); /* read brush aspect_ratio */ errno = 0; linenum++; if (! fgets (string, sizeof (string), file)) goto failed; aspect_ratio = g_ascii_strtod (string, NULL); /* read brush angle */ errno = 0; linenum++; if (! fgets (string, sizeof (string), file)) goto failed; angle = g_ascii_strtod (string, NULL); fclose (file); brush = GIMP_BRUSH (gimp_brush_generated_new (name, shape, radius, spikes, hardness, aspect_ratio, angle)); g_free (name); brush->spacing = spacing; return g_list_prepend (NULL, brush); failed: fclose (file); if (name) g_free (name); if (error && *error == NULL) { gchar *msg; if (errno) msg = g_strdup_printf (_("Line %d: %s"), linenum, g_strerror (errno)); else msg = g_strdup_printf (_("File is truncated in line %d"), linenum); g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ, _("Error while reading brush file '%s': %s"), gimp_filename_to_utf8 (filename), msg); g_free (msg); } return NULL; }
static GimpBrush * gimp_brush_pipe_select_brush (GimpBrush *brush, const GimpCoords *last_coords, const GimpCoords *current_coords) { GimpBrushPipe *pipe = GIMP_BRUSH_PIPE (brush); gint i, brushix, ix; gdouble velocity; if (pipe->n_brushes == 1) return GIMP_BRUSH (pipe->current); brushix = 0; for (i = 0; i < pipe->dimension; i++) { switch (pipe->select[i]) { case PIPE_SELECT_INCREMENTAL: ix = (pipe->index[i] + 1) % pipe->rank[i]; break; case PIPE_SELECT_ANGULAR: /* Coords angle is already nomalized, * offset by 90 degrees is still needed * because hoses were made PS compatible*/ ix = (gint) RINT ((1.0 - current_coords->direction + 0.25) * pipe->rank[i]) % pipe->rank[i]; break; case PIPE_SELECT_VELOCITY: velocity = current_coords->velocity; /* Max velocity is 3.0, picking stamp as a ratio*/ ix = ROUND ((3.0 / velocity) * pipe->rank[i]); break; case PIPE_SELECT_RANDOM: /* This probably isn't the right way */ ix = g_random_int_range (0, pipe->rank[i]); break; case PIPE_SELECT_PRESSURE: ix = RINT (current_coords->pressure * (pipe->rank[i] - 1)); break; case PIPE_SELECT_TILT_X: ix = RINT (current_coords->xtilt / 2.0 * pipe->rank[i]) + pipe->rank[i] / 2; break; case PIPE_SELECT_TILT_Y: ix = RINT (current_coords->ytilt / 2.0 * pipe->rank[i]) + pipe->rank[i] / 2; break; case PIPE_SELECT_CONSTANT: default: ix = pipe->index[i]; break; } pipe->index[i] = CLAMP (ix, 0, pipe->rank[i] - 1); brushix += pipe->stride[i] * pipe->index[i]; } /* Make sure is inside bounds */ brushix = CLAMP (brushix, 0, pipe->n_brushes - 1); pipe->current = pipe->brushes[brushix]; return GIMP_BRUSH (pipe->current); }
gboolean gimp_brush_generated_save (GimpData *data, GError **error) { GimpBrushGenerated *brush = GIMP_BRUSH_GENERATED (data); const gchar *name = gimp_object_get_name (data); FILE *file; gchar buf[G_ASCII_DTOSTR_BUF_SIZE]; gboolean have_shape = FALSE; g_return_val_if_fail (name != NULL && *name != '\0', FALSE); file = g_fopen (gimp_data_get_filename (data), "wb"); if (! file) { g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN, _("Could not open '%s' for writing: %s"), gimp_filename_to_utf8 (gimp_data_get_filename (data)), g_strerror (errno)); return FALSE; } /* write magic header */ fprintf (file, "GIMP-VBR\n"); /* write version */ if (brush->shape != GIMP_BRUSH_GENERATED_CIRCLE || brush->spikes > 2) { fprintf (file, "1.5\n"); have_shape = TRUE; } else { fprintf (file, "1.0\n"); } /* write name */ fprintf (file, "%.255s\n", name); if (have_shape) { GEnumClass *enum_class; GEnumValue *shape_val; enum_class = g_type_class_peek (GIMP_TYPE_BRUSH_GENERATED_SHAPE); /* write shape */ shape_val = g_enum_get_value (enum_class, brush->shape); fprintf (file, "%s\n", shape_val->value_nick); } /* write brush spacing */ fprintf (file, "%s\n", g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f", gimp_brush_get_spacing (GIMP_BRUSH (brush)))); /* write brush radius */ fprintf (file, "%s\n", g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f", brush->radius)); if (have_shape) { /* write brush spikes */ fprintf (file, "%d\n", brush->spikes); } /* write brush hardness */ fprintf (file, "%s\n", g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f", brush->hardness)); /* write brush aspect_ratio */ fprintf (file, "%s\n", g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f", brush->aspect_ratio)); /* write brush angle */ fprintf (file, "%s\n", g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f", brush->angle)); fclose (file); return TRUE; }
gboolean gimp_brush_generated_save (GimpData *data, GOutputStream *output, GError **error) { GimpBrushGenerated *brush = GIMP_BRUSH_GENERATED (data); const gchar *name = gimp_object_get_name (data); GString *string; gchar buf[G_ASCII_DTOSTR_BUF_SIZE]; gboolean have_shape = FALSE; g_return_val_if_fail (name != NULL && *name != '\0', FALSE); /* write magic header */ string = g_string_new ("GIMP-VBR\n"); /* write version */ if (brush->shape != GIMP_BRUSH_GENERATED_CIRCLE || brush->spikes > 2) { g_string_append (string, "1.5\n"); have_shape = TRUE; } else { g_string_append (string, "1.0\n"); } /* write name */ g_string_append_printf (string, "%.255s\n", name); if (have_shape) { GEnumClass *enum_class; GEnumValue *shape_val; enum_class = g_type_class_peek (GIMP_TYPE_BRUSH_GENERATED_SHAPE); /* write shape */ shape_val = g_enum_get_value (enum_class, brush->shape); g_string_append_printf (string, "%s\n", shape_val->value_nick); } /* write brush spacing */ g_string_append_printf (string, "%s\n", g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f", gimp_brush_get_spacing (GIMP_BRUSH (brush)))); /* write brush radius */ g_string_append_printf (string, "%s\n", g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f", brush->radius)); if (have_shape) { /* write brush spikes */ g_string_append_printf (string, "%d\n", brush->spikes); } /* write brush hardness */ g_string_append_printf (string, "%s\n", g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f", brush->hardness)); /* write brush aspect_ratio */ g_string_append_printf (string, "%s\n", g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f", brush->aspect_ratio)); /* write brush angle */ g_string_append_printf (string, "%s\n", g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f", brush->angle)); if (! g_output_stream_write_all (output, string->str, string->len, NULL, NULL, error)) { g_string_free (string, TRUE); return FALSE; } g_string_free (string, TRUE); return TRUE; }
GList * gimp_brush_pipe_load (GimpContext *context, const gchar *filename, GError **error) { GimpBrushPipe *pipe = NULL; GimpPixPipeParams params; gint i; gint num_of_brushes = 0; gint totalcells; gchar *paramstring; GString *buffer; gchar c; gint fd; g_return_val_if_fail (filename != NULL, NULL); g_return_val_if_fail (g_path_is_absolute (filename), NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); fd = g_open (filename, O_RDONLY | _O_BINARY, 0); if (fd == -1) { g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN, _("Could not open '%s' for reading: %s"), gimp_filename_to_utf8 (filename), g_strerror (errno)); return NULL; } /* The file format starts with a painfully simple text header */ /* get the name */ buffer = g_string_new (NULL); while (read (fd, &c, 1) == 1 && c != '\n' && buffer->len < 1024) g_string_append_c (buffer, c); if (buffer->len > 0 && buffer->len < 1024) { gchar *utf8 = gimp_any_to_utf8 (buffer->str, buffer->len, _("Invalid UTF-8 string in brush file '%s'."), gimp_filename_to_utf8 (filename)); pipe = g_object_new (GIMP_TYPE_BRUSH_PIPE, "name", utf8, "mime-type", "image/x-gimp-gih", NULL); g_free (utf8); } g_string_free (buffer, TRUE); if (! pipe) { g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ, _("Fatal parse error in brush file '%s': " "File is corrupt."), gimp_filename_to_utf8 (filename)); close (fd); return NULL; } /* get the number of brushes */ buffer = g_string_new (NULL); while (read (fd, &c, 1) == 1 && c != '\n' && buffer->len < 1024) g_string_append_c (buffer, c); if (buffer->len > 0 && buffer->len < 1024) { num_of_brushes = strtol (buffer->str, ¶mstring, 10); } if (num_of_brushes < 1) { g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ, _("Fatal parse error in brush file '%s': " "File is corrupt."), gimp_filename_to_utf8 (filename)); close (fd); g_object_unref (pipe); g_string_free (buffer, TRUE); return NULL; } while (*paramstring && g_ascii_isspace (*paramstring)) paramstring++; if (*paramstring) { gimp_pixpipe_params_init (¶ms); gimp_pixpipe_params_parse (paramstring, ¶ms); pipe->dimension = params.dim; pipe->rank = g_new0 (gint, pipe->dimension); pipe->select = g_new0 (PipeSelectModes, pipe->dimension); pipe->index = g_new0 (gint, pipe->dimension); /* placement is not used at all ?? */ if (params.free_placement_string) g_free (params.placement); for (i = 0; i < pipe->dimension; i++) { pipe->rank[i] = MAX (1, params.rank[i]); if (strcmp (params.selection[i], "incremental") == 0) pipe->select[i] = PIPE_SELECT_INCREMENTAL; else if (strcmp (params.selection[i], "angular") == 0) pipe->select[i] = PIPE_SELECT_ANGULAR; else if (strcmp (params.selection[i], "velocity") == 0) pipe->select[i] = PIPE_SELECT_VELOCITY; else if (strcmp (params.selection[i], "random") == 0) pipe->select[i] = PIPE_SELECT_RANDOM; else if (strcmp (params.selection[i], "pressure") == 0) pipe->select[i] = PIPE_SELECT_PRESSURE; else if (strcmp (params.selection[i], "xtilt") == 0) pipe->select[i] = PIPE_SELECT_TILT_X; else if (strcmp (params.selection[i], "ytilt") == 0) pipe->select[i] = PIPE_SELECT_TILT_Y; else pipe->select[i] = PIPE_SELECT_CONSTANT; if (params.free_selection_string) g_free (params.selection[i]); pipe->index[i] = 0; } } else { pipe->dimension = 1; pipe->rank = g_new (gint, 1); pipe->rank[0] = num_of_brushes; pipe->select = g_new (PipeSelectModes, 1); pipe->select[0] = PIPE_SELECT_INCREMENTAL; pipe->index = g_new (gint, 1); pipe->index[0] = 0; } g_string_free (buffer, TRUE); totalcells = 1; /* Not all necessarily present, maybe */ for (i = 0; i < pipe->dimension; i++) totalcells *= pipe->rank[i]; pipe->stride = g_new0 (gint, pipe->dimension); for (i = 0; i < pipe->dimension; i++) { if (i == 0) pipe->stride[i] = totalcells / pipe->rank[i]; else pipe->stride[i] = pipe->stride[i-1] / pipe->rank[i]; } g_assert (pipe->stride[pipe->dimension-1] == 1); pipe->brushes = g_new0 (GimpBrush *, num_of_brushes); while (pipe->n_brushes < num_of_brushes) { GError *my_error = NULL; pipe->brushes[pipe->n_brushes] = gimp_brush_load_brush (context, fd, filename, &my_error); if (pipe->brushes[pipe->n_brushes]) { gimp_object_set_name (GIMP_OBJECT (pipe->brushes[pipe->n_brushes]), NULL); } else { g_propagate_error (error, my_error); close (fd); g_object_unref (pipe); return NULL; } pipe->n_brushes++; } close (fd); /* Current brush is the first one. */ pipe->current = pipe->brushes[0]; /* just to satisfy the code that relies on this crap */ GIMP_BRUSH (pipe)->spacing = pipe->current->spacing; GIMP_BRUSH (pipe)->x_axis = pipe->current->x_axis; GIMP_BRUSH (pipe)->y_axis = pipe->current->y_axis; GIMP_BRUSH (pipe)->mask = pipe->current->mask; GIMP_BRUSH (pipe)->pixmap = pipe->current->pixmap; return g_list_prepend (NULL, pipe); }