home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / app / gimpbrushpipe.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-20  |  10.9 KB  |  410 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  * Copyright (C) 1999 Adrian Likins and Tor Lillqvist
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  */
  19.  
  20. #include "config.h"
  21.  
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25.  
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #ifdef HAVE_UNISTD_H
  29. #include <unistd.h>
  30. #endif
  31. #include <fcntl.h>
  32.  
  33. #ifdef G_OS_WIN32
  34. #include <io.h>
  35. #endif
  36.  
  37. #ifndef _O_BINARY
  38. #define _O_BINARY 0
  39. #endif
  40.  
  41. #include <gtk/gtk.h>
  42.  
  43. #include "apptypes.h"
  44. #include "appenv.h"
  45. #include "brush_header.h"
  46. #include "pattern_header.h"
  47. #include "patterns.h"
  48. #include "gimpbrush.h"
  49. #include "gimpbrushpipe.h"
  50. #include "paint_core.h"
  51. #include "gimprc.h"
  52.  
  53. #include "libgimp/gimpmath.h"
  54. #include "libgimp/gimpparasiteio.h"
  55.  
  56. #include "libgimp/gimpintl.h"
  57.  
  58.  
  59. static GimpBrushClass  *parent_class;
  60.  
  61.  
  62. static GimpBrush * gimp_brush_pipe_select_brush     (PaintCore *paint_core);
  63. static gboolean    gimp_brush_pipe_want_null_motion (PaintCore *paint_core);
  64. static void        gimp_brush_pipe_destroy          (GtkObject *object);
  65.  
  66.  
  67. static GimpBrush *
  68. gimp_brush_pipe_select_brush (PaintCore *paint_core)
  69. {
  70.   GimpBrushPipe *pipe;
  71.   gint i, brushix, ix;
  72.   gdouble angle;
  73.  
  74.   g_return_val_if_fail (paint_core != NULL, NULL);
  75.   g_return_val_if_fail (GIMP_IS_BRUSH_PIPE (paint_core->brush), NULL);
  76.  
  77.   pipe = GIMP_BRUSH_PIPE (paint_core->brush);
  78.  
  79.   if (pipe->nbrushes == 1)
  80.     return GIMP_BRUSH (pipe->current);
  81.  
  82.   brushix = 0;
  83.   for (i = 0; i < pipe->dimension; i++)
  84.     {
  85.       switch (pipe->select[i])
  86.     {
  87.     case PIPE_SELECT_INCREMENTAL:
  88.       ix = (pipe->index[i] + 1) % pipe->rank[i];
  89.       break;
  90.     case PIPE_SELECT_ANGULAR:
  91.       angle = atan2 (paint_core->cury - paint_core->lasty,
  92.              paint_core->curx - paint_core->lastx);
  93.       /* Offset angle to be compatible with PSP tubes */
  94.       angle += G_PI_2;
  95.       /* Map it to the [0..2*G_PI) interval */
  96.       if (angle < 0)
  97.         angle += 2.0 * G_PI;
  98.       else if (angle > 2.0 * G_PI)
  99.         angle -= 2.0 * G_PI;
  100.       ix = RINT (angle / (2.0 * G_PI) * pipe->rank[i]);
  101.       break;
  102.     case PIPE_SELECT_RANDOM:
  103.       /* This probably isn't the right way */
  104.       ix = rand () % pipe->rank[i];
  105.       break;
  106.     case PIPE_SELECT_PRESSURE:
  107.       ix = RINT (paint_core->curpressure * (pipe->rank[i] - 1));
  108.       break;
  109.     case PIPE_SELECT_TILT_X:
  110.       ix = RINT (paint_core->curxtilt / 2.0 * pipe->rank[i]) + pipe->rank[i]/2;
  111.       break;
  112.     case PIPE_SELECT_TILT_Y:
  113.       ix = RINT (paint_core->curytilt / 2.0 * pipe->rank[i]) + pipe->rank[i]/2;
  114.       break;
  115.     case PIPE_SELECT_CONSTANT:
  116.     default:
  117.       ix = pipe->index[i];
  118.       break;
  119.     }
  120.       pipe->index[i] = CLAMP (ix, 0, pipe->rank[i]-1);
  121.       brushix += pipe->stride[i] * pipe->index[i];
  122.     }
  123.  
  124.   /* Make sure is inside bounds */
  125.   brushix = CLAMP (brushix, 0, pipe->nbrushes-1);
  126.  
  127.   pipe->current = pipe->brushes[brushix];
  128.  
  129.   return GIMP_BRUSH (pipe->current);
  130. }
  131.  
  132. static gboolean
  133. gimp_brush_pipe_want_null_motion (PaintCore *paint_core)
  134. {
  135.   GimpBrushPipe *pipe;
  136.   gint i;
  137.  
  138.   g_return_val_if_fail (paint_core != NULL, TRUE);
  139.   g_return_val_if_fail (GIMP_IS_BRUSH_PIPE (paint_core->brush), TRUE);
  140.  
  141.   pipe = GIMP_BRUSH_PIPE (paint_core->brush);
  142.  
  143.   if (pipe->nbrushes == 1)
  144.     return TRUE;
  145.  
  146.   for (i = 0; i < pipe->dimension; i++)
  147.     if (pipe->select[i] == PIPE_SELECT_ANGULAR)
  148.       return FALSE;
  149.  
  150.   return TRUE;
  151. }
  152.  
  153. static void
  154. gimp_brush_pipe_destroy (GtkObject *object)
  155. {
  156.   GimpBrushPipe *pipe;
  157.   gint i;
  158.  
  159.   g_return_if_fail (object != NULL);
  160.   g_return_if_fail (GIMP_IS_BRUSH_PIPE (object));
  161.  
  162.   pipe = GIMP_BRUSH_PIPE (object);
  163.  
  164.   g_free (pipe->rank);
  165.   g_free (pipe->stride);
  166.  
  167.   for (i = 1; i < pipe->nbrushes; i++)
  168.     if (pipe->brushes[i])
  169.       gtk_object_unref (GTK_OBJECT (pipe->brushes[i]));
  170.  
  171.   g_free (pipe->brushes);
  172.   g_free (pipe->select);
  173.   g_free (pipe->index);
  174.  
  175.   if (GTK_OBJECT_CLASS (parent_class)->destroy)
  176.     GTK_OBJECT_CLASS (parent_class)->destroy (object);
  177. }
  178.  
  179. static void
  180. gimp_brush_pipe_class_init (GimpBrushPipeClass *klass)
  181. {
  182.   GtkObjectClass *object_class;
  183.   GimpBrushClass *brush_class;
  184.  
  185.   object_class = GTK_OBJECT_CLASS (klass);
  186.   brush_class = GIMP_BRUSH_CLASS (klass);
  187.  
  188.   parent_class = gtk_type_class (GIMP_TYPE_BRUSH);
  189.  
  190.   brush_class->select_brush     = gimp_brush_pipe_select_brush;
  191.   brush_class->want_null_motion = gimp_brush_pipe_want_null_motion;
  192.  
  193.   object_class->destroy = gimp_brush_pipe_destroy;
  194. }
  195.  
  196. void
  197. gimp_brush_pipe_init (GimpBrushPipe *pipe)
  198. {
  199.   pipe->current   = NULL;
  200.   pipe->dimension = 0;
  201.   pipe->rank      = NULL;
  202.   pipe->stride    = NULL;
  203.   pipe->nbrushes  = 0;
  204.   pipe->brushes   = NULL;
  205.   pipe->select    = NULL;
  206.   pipe->index     = NULL;
  207. }
  208.  
  209. GtkType
  210. gimp_brush_pipe_get_type (void)
  211. {
  212.   static GtkType type = 0;
  213.  
  214.   if (!type)
  215.     {
  216.       GtkTypeInfo info =
  217.       {
  218.     "GimpBrushPipe",
  219.     sizeof (GimpBrushPipe),
  220.     sizeof (GimpBrushPipeClass),
  221.     (GtkClassInitFunc) gimp_brush_pipe_class_init,
  222.     (GtkObjectInitFunc) gimp_brush_pipe_init,
  223.     /* reserved_1 */ NULL,
  224.     /* reserved_2 */ NULL,
  225.     (GtkClassInitFunc) NULL
  226.       };
  227.  
  228.       type = gtk_type_unique (GIMP_TYPE_BRUSH, &info);
  229.     }
  230.  
  231.   return type;
  232. }
  233.  
  234. #include <errno.h>
  235.  
  236. GimpBrush *
  237. gimp_brush_pipe_load (gchar *filename)
  238. {
  239.   GimpBrushPipe     *pipe = NULL;
  240.   GimpPixPipeParams  params;
  241.   gint     i;
  242.   gint     num_of_brushes = 0;
  243.   gint     totalcells;
  244.   gchar   *paramstring;
  245.   GString *buffer;
  246.   gchar    c;
  247.   gint     fd;
  248.  
  249.   g_return_val_if_fail (filename != NULL, NULL);
  250.  
  251.   fd = open (filename, O_RDONLY | _O_BINARY);
  252.   if (fd == -1)
  253.     {
  254.       g_message ("Couldn't open file '%s'", filename);
  255.       return NULL;
  256.     }
  257.  
  258.   /* The file format starts with a painfully simple text header */
  259.  
  260.   /*  get the name  */
  261.   buffer = g_string_new (NULL);
  262.   while (read (fd, &c, 1) == 1 && c != '\n' && buffer->len < 1024)
  263.     g_string_append_c (buffer, c);
  264.     
  265.   if (buffer->len > 0 && buffer->len < 1024)
  266.     {
  267.       pipe = GIMP_BRUSH_PIPE (gtk_type_new (GIMP_TYPE_BRUSH_PIPE));      
  268.       GIMP_BRUSH (pipe)->name = buffer->str;
  269.     }
  270.   g_string_free (buffer, FALSE);
  271.  
  272.   if (!pipe)
  273.     {
  274.       g_message ("Couldn't read name for brush pipe from file '%s'\n", 
  275.          filename);
  276.       close (fd);
  277.       return NULL;
  278.     }
  279.  
  280.   /*  get the number of brushes  */
  281.   buffer = g_string_new (NULL);
  282.   while (read (fd, &c, 1) == 1 && c != '\n' && buffer->len < 1024)
  283.     g_string_append_c (buffer, c);
  284.  
  285.   if (buffer->len > 0 && buffer->len < 1024)
  286.     {
  287.       num_of_brushes = strtol (buffer->str, ¶mstring, 10);
  288.     }
  289.  
  290.   if (num_of_brushes < 1)
  291.     {
  292.       g_message (_("Brush pipes should have at least one brush:\n\"%s\""), 
  293.          filename);
  294.       close (fd);
  295.       gtk_object_sink (GTK_OBJECT (pipe));
  296.       g_string_free (buffer, TRUE);
  297.       return NULL;
  298.     }
  299.  
  300.   while (*paramstring && isspace (*paramstring))
  301.     paramstring++;
  302.  
  303.   if (*paramstring)
  304.     {
  305.       gimp_pixpipe_params_init (¶ms);
  306.       gimp_pixpipe_params_parse (paramstring, ¶ms);
  307.  
  308.       pipe->dimension = params.dim;
  309.       pipe->rank      = g_new0 (gint, pipe->dimension);
  310.       pipe->select    = g_new0 (PipeSelectModes, pipe->dimension);
  311.       pipe->index     = g_new0 (gint, pipe->dimension);
  312.  
  313.       /* placement is not used at all ?? */
  314.       if (params.free_placement_string)
  315.     g_free (params.placement);
  316.  
  317.       for (i = 0; i < pipe->dimension; i++)
  318.     {
  319.       pipe->rank[i] = params.rank[i];
  320.       if (strcmp (params.selection[i], "incremental") == 0)
  321.         pipe->select[i] = PIPE_SELECT_INCREMENTAL;
  322.       else if (strcmp (params.selection[i], "angular") == 0)
  323.         pipe->select[i] = PIPE_SELECT_ANGULAR;
  324.       else if (strcmp (params.selection[i], "velocity") == 0)
  325.         pipe->select[i] = PIPE_SELECT_VELOCITY;
  326.       else if (strcmp (params.selection[i], "random") == 0)
  327.         pipe->select[i] = PIPE_SELECT_RANDOM;
  328.       else if (strcmp (params.selection[i], "pressure") == 0)
  329.         pipe->select[i] = PIPE_SELECT_PRESSURE;
  330.       else if (strcmp (params.selection[i], "xtilt") == 0)
  331.         pipe->select[i] = PIPE_SELECT_TILT_X;
  332.       else if (strcmp (params.selection[i], "ytilt") == 0)
  333.         pipe->select[i] = PIPE_SELECT_TILT_Y;
  334.       else
  335.         pipe->select[i] = PIPE_SELECT_CONSTANT;
  336.       if (params.free_selection_string)
  337.         g_free (params.selection[i]);
  338.       pipe->index[i] = 0;
  339.     }
  340.     }
  341.   else
  342.     {
  343.       pipe->dimension = 1;
  344.       pipe->rank      = g_new (gint, 1);
  345.       pipe->rank[0]   = num_of_brushes;
  346.       pipe->select    = g_new (PipeSelectModes, 1);
  347.       pipe->select[0] = PIPE_SELECT_INCREMENTAL;
  348.       pipe->index     = g_new (gint, 1);
  349.       pipe->index[0]  = 0;
  350.     }
  351.  
  352.   g_string_free (buffer, TRUE);
  353.  
  354.   totalcells = 1;        /* Not all necessarily present, maybe */
  355.   for (i = 0; i < pipe->dimension; i++)
  356.     totalcells *= pipe->rank[i];
  357.   pipe->stride = g_new0 (gint, pipe->dimension);
  358.   for (i = 0; i < pipe->dimension; i++)
  359.     {
  360.       if (i == 0)
  361.     pipe->stride[i] = totalcells / pipe->rank[i];
  362.       else
  363.     pipe->stride[i] = pipe->stride[i-1] / pipe->rank[i];
  364.     }
  365.   g_assert (pipe->stride[pipe->dimension-1] == 1);
  366.  
  367.   pipe->brushes = g_new0 (GimpBrush *, num_of_brushes);
  368.  
  369.   while (pipe->nbrushes < num_of_brushes)
  370.     {
  371.       pipe->brushes[pipe->nbrushes] = gimp_brush_load_brush (fd, filename);
  372.  
  373.       if (pipe->brushes[pipe->nbrushes])
  374.     {
  375.       gtk_object_ref (GTK_OBJECT (pipe->brushes[pipe->nbrushes]));
  376.       gtk_object_sink (GTK_OBJECT (pipe->brushes[pipe->nbrushes]));
  377.       
  378.       g_free (GIMP_BRUSH (pipe->brushes[pipe->nbrushes])->name);
  379.       GIMP_BRUSH (pipe->brushes[pipe->nbrushes])->name = NULL;
  380.     }
  381.       else
  382.     {
  383.       g_message (_("Failed to load one of the brushes in the brush pipe\n\"%s\""), 
  384.                filename);
  385.       close (fd);
  386.       gtk_object_sink (GTK_OBJECT (pipe));
  387.       return NULL;
  388.     }
  389.   
  390.       pipe->nbrushes++;
  391.     }
  392.  
  393.   /* Current brush is the first one. */
  394.   pipe->current = pipe->brushes[0];
  395.  
  396.   /*  just to satisfy the code that relies on this crap  */
  397.   GIMP_BRUSH (pipe)->filename = g_strdup (filename);
  398.   GIMP_BRUSH (pipe)->spacing  = pipe->current->spacing;
  399.   GIMP_BRUSH (pipe)->x_axis   = pipe->current->x_axis;
  400.   GIMP_BRUSH (pipe)->y_axis   = pipe->current->y_axis;
  401.   GIMP_BRUSH (pipe)->mask     = pipe->current->mask;
  402.   GIMP_BRUSH (pipe)->pixmap   = pipe->current->pixmap;
  403.  
  404.   close (fd);
  405.  
  406.   return GIMP_BRUSH (pipe);
  407. }
  408.  
  409.  
  410.