File: gimpscanconvert.c

package info (click to toggle)
gimp 2.2.13-1etch4
  • links: PTS
  • area: main
  • in suites: etch
  • size: 94,832 kB
  • ctags: 47,113
  • sloc: ansic: 524,858; xml: 36,798; lisp: 9,870; sh: 9,409; makefile: 7,923; python: 2,674; perl: 2,589; yacc: 520; lex: 334
file content (605 lines) | stat: -rw-r--r-- 15,944 bytes parent folder | download | duplicates (3)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/* 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"

#include <string.h>

#include <glib-object.h>

#include <libart_lgpl/libart.h>
#include <libart_lgpl/art_svp_intersect.h>

#include "libgimpmath/gimpmath.h"

#include "core-types.h"

#include "base/pixel-region.h"
#include "base/tile-manager.h"

#include "gimpscanconvert.h"


struct _GimpScanConvert
{
  gdouble      ratio_xy;

  /* stuff necessary for the _add_polygons API...  :-/  */
  gboolean     got_first;
  gboolean     need_closing;
  GimpVector2  first;
  GimpVector2  prev;

  gboolean     have_open;
  guint        num_nodes;
  ArtVpath    *vpath;

  ArtSVP      *svp;         /* Sorted vector path
                               (extension no longer possible)          */

  /* stuff necessary for the rendering callback */
  guchar      *buf;
  gint         rowstride;
  gint         x0, x1;
  gboolean     antialias;
};

/* private functions */

static void   gimp_scan_convert_finish           (GimpScanConvert *sc);
static void   gimp_scan_convert_close_add_points (GimpScanConvert *sc);

static void   gimp_scan_convert_render_callback (gpointer          user_data,
                                                 gint              y,
                                                 gint              start_value,
                                                 ArtSVPRenderAAStep *steps,
                                                 gint              n_steps);

/*  public functions  */

GimpScanConvert *
gimp_scan_convert_new (void)
{
  GimpScanConvert *sc;

  sc = g_new0 (GimpScanConvert, 1);

  sc->ratio_xy  = 1.0;

  return sc;
}

void
gimp_scan_convert_free (GimpScanConvert *sc)
{
  g_return_if_fail (sc != NULL);

  if (sc->vpath)
    art_free (sc->vpath);
  if (sc->svp)
    art_svp_free (sc->svp);

  g_free (sc);
}

/* set the Pixel-Ratio (width / height) for the pixels.
 */
void
gimp_scan_convert_set_pixel_ratio (GimpScanConvert *sc,
                                   gdouble          ratio_xy)
{
  g_return_if_fail (sc != NULL);

  /* we only need the relative resolution */
  sc->ratio_xy = ratio_xy;
}


/* Add "n_points" from "points" to the polygon currently being
 * described by "scan_converter". DEPRECATED.
 */
void
gimp_scan_convert_add_points (GimpScanConvert *sc,
                              guint            n_points,
                              GimpVector2     *points,
                              gboolean         new_polygon)
{
  gint  i;

  g_return_if_fail (sc != NULL);
  g_return_if_fail (points != NULL);
  g_return_if_fail (n_points > 0);
  g_return_if_fail (sc->svp == NULL);

  /* We need an extra nodes to end the path */
  sc->vpath = art_renew (sc->vpath, ArtVpath, sc->num_nodes + n_points + 1);

  if (sc->num_nodes == 0 || new_polygon)
    {
      if (sc->need_closing)
        gimp_scan_convert_close_add_points (sc);

      sc->got_first = FALSE;
    }

  /* We have to compress multiple identical coordinates */

  for (i = 0; i < n_points; i++)
    {
      if (sc->got_first == FALSE ||
          sc->prev.x != points[i].x || sc->prev.y != points[i].y)
        {
          sc->vpath[sc->num_nodes].code = ((! sc->got_first) || new_polygon) ?
                                           ART_MOVETO : ART_LINETO;
          sc->vpath[sc->num_nodes].x = points[i].x;
          sc->vpath[sc->num_nodes].y = points[i].y;
          sc->num_nodes++;
          sc->prev = points[i];

          if (!sc->got_first)
            {
              sc->got_first = TRUE;
              sc->first = points[i];
            }
        }
    }

  sc->need_closing = TRUE;

  sc->vpath[sc->num_nodes].code = ART_END;
  sc->vpath[sc->num_nodes].x = 0.0;
  sc->vpath[sc->num_nodes].y = 0.0;
}


static void
gimp_scan_convert_close_add_points (GimpScanConvert *sc)
{
  if (sc->need_closing &&
      (sc->prev.x != sc->first.x || sc->prev.y != sc->first.y))
    {
      sc->vpath = art_renew (sc->vpath, ArtVpath, sc->num_nodes + 2);
      sc->vpath[sc->num_nodes].code = ART_LINETO;
      sc->vpath[sc->num_nodes].x = sc->first.x;
      sc->vpath[sc->num_nodes].y = sc->first.y;
      sc->num_nodes++;
      sc->vpath[sc->num_nodes].code = ART_END;
      sc->vpath[sc->num_nodes].x = 0.0;
      sc->vpath[sc->num_nodes].y = 0.0;
    }

  sc->need_closing = FALSE;
}


/* Add a polygon with "npoints" "points" that may be open or closed.
 * It is not recommended to mix gimp_scan_convert_add_polyline with
 * gimp_scan_convert_add_points.
 *
 * Please note that you should use gimp_scan_convert_stroke() if you
 * specify open polygons.
 */

void
gimp_scan_convert_add_polyline (GimpScanConvert *sc,
                                guint            n_points,
                                GimpVector2     *points,
                                gboolean         closed)
{
  GimpVector2  prev;
  gint i;

  g_return_if_fail (sc != NULL);
  g_return_if_fail (points != NULL);
  g_return_if_fail (n_points > 0);
  g_return_if_fail (sc->svp == NULL);

  if (sc->need_closing)
    gimp_scan_convert_close_add_points (sc);

  if (!closed)
    sc->have_open = TRUE;

  /* make sure that we have enough space for the nodes */
  sc->vpath = art_renew (sc->vpath, ArtVpath,
                         sc->num_nodes + n_points + 2);

  for (i = 0; i < n_points; i++)
    {
      /* compress multiple identical coordinates */
      if (i == 0 ||
          prev.x != points[i].x ||
          prev.y != points[i].y)
        {
          sc->vpath[sc->num_nodes].code = (i == 0 ? (closed ?
                                                     ART_MOVETO :
                                                     ART_MOVETO_OPEN) :
                                                    ART_LINETO);
          sc->vpath[sc->num_nodes].x = points[i].x;
          sc->vpath[sc->num_nodes].y = points[i].y;
          sc->num_nodes++;
          prev = points[i];
        }
    }

  /* close the polyline when needed */
  if (closed && (prev.x != points[0].x ||
                 prev.y != points[0].y))
    {
      sc->vpath[sc->num_nodes].x = points[0].x;
      sc->vpath[sc->num_nodes].y = points[0].y;
      sc->vpath[sc->num_nodes].code = ART_LINETO;
      sc->num_nodes++;
    }

  sc->vpath[sc->num_nodes].code = ART_END;
  sc->vpath[sc->num_nodes].x = 0.0;
  sc->vpath[sc->num_nodes].y = 0.0;

  /* If someone wants to mix this function with _add_points ()
   * try to do something reasonable...
   */

  sc->got_first = FALSE;
}



/* Stroke the content of a GimpScanConvert. The next
 * gimp_scan_convert_render() will result in the outline of the polygon
 * defined with the commands above.
 *
 * You cannot add additional polygons after this command.
 */
void
gimp_scan_convert_stroke (GimpScanConvert *sc,
                          gdouble          width,
                          GimpJoinStyle    join,
                          GimpCapStyle     cap,
                          gdouble          miter,
                          gdouble          dash_offset,
                          GArray          *dash_info)
{
  ArtSVP                *stroke;
  ArtPathStrokeJoinType  artjoin = 0;
  ArtPathStrokeCapType   artcap  = 0;

  g_return_if_fail (sc->svp == NULL);

  if (sc->need_closing)
    gimp_scan_convert_close_add_points (sc);

  switch (join)
    {
    case GIMP_JOIN_MITER:
      artjoin = ART_PATH_STROKE_JOIN_MITER;
      break;
    case GIMP_JOIN_ROUND:
      artjoin = ART_PATH_STROKE_JOIN_ROUND;
      break;
    case GIMP_JOIN_BEVEL:
      artjoin = ART_PATH_STROKE_JOIN_BEVEL;
      break;
    }

  switch (cap)
    {
    case GIMP_CAP_BUTT:
      artcap = ART_PATH_STROKE_CAP_BUTT;
      break;
    case GIMP_CAP_ROUND:
      artcap = ART_PATH_STROKE_CAP_ROUND;
      break;
    case GIMP_CAP_SQUARE:
      artcap = ART_PATH_STROKE_CAP_SQUARE;
      break;
    }

  if (sc->ratio_xy != 1.0)
    {
      gint i;

      for (i = 0; i < sc->num_nodes; i++)
        sc->vpath[i].x *= sc->ratio_xy;
    }

  if (dash_info && dash_info->len >= 2)
    {
      ArtVpath     *dash_vpath;
      ArtVpathDash  dash;
      gdouble      *dashes;
      gint          i;

      dash.offset = dash_offset * MAX (width, 1.0);

      dashes = g_new (gdouble, dash_info->len);

      for (i=0; i < dash_info->len ; i++)
        dashes[i] = MAX (width, 1.0) * g_array_index (dash_info, gdouble, i);

      dash.n_dash = dash_info->len;
      dash.dash = dashes;

      /* correct 0.0 in the first element (starts with a gap) */

      if (dash.dash[0] == 0.0)
        {
          gdouble first;

          first = dash.dash[1];

          /* shift the pattern to really starts with a dash and
           * use the offset to skip into it.
           */
          for (i=0; i < dash_info->len - 2; i++)
            {
              dash.dash[i] = dash.dash[i+2];
              dash.offset += dash.dash[i];
            }

          if (dash_info->len % 2 == 1)
            {
              dash.dash[dash_info->len - 2] = first;
              dash.n_dash --;
            }
          else
            {
              if (dash_info->len < 3)
                {
                  /* empty stroke */
                  art_free (sc->vpath);
                  sc->vpath = NULL;
                }
              else
                {
                  dash.dash [dash_info->len - 3] += first;
                  dash.n_dash -= 2;
                }
            }
        }

      /* correct odd number of dash specifiers */

      if (dash.n_dash % 2 == 1)
        {
          gdouble last;

          last = dash.dash[dash.n_dash - 1];
          dash.dash[0] += last;
          dash.offset += last;
          dash.n_dash --;
        }


      if (sc->vpath)
        {
          dash_vpath = art_vpath_dash (sc->vpath, &dash);
          art_free (sc->vpath);
          sc->vpath = dash_vpath;
        }

      g_free (dashes);
    }

  if (sc->vpath)
    {
      stroke = art_svp_vpath_stroke (sc->vpath, artjoin, artcap,
                                     width, miter, 0.2);

      if (sc->ratio_xy != 1.0)
        {
          ArtSVPSeg *segment;
          ArtPoint  *point;
          gint       i, j;

          for (i = 0; i < stroke->n_segs; i++)
            {
              segment = stroke->segs + i;
              segment->bbox.x0 /= sc->ratio_xy;
              segment->bbox.x1 /= sc->ratio_xy;

              for (j=0; j < segment->n_points  ; j++)
                {
                  point = segment->points + j;
                  point->x /= sc->ratio_xy;
                }
            }
        }

      sc->svp = stroke;
    }
}


/* This is a more low level version. Expects a tile manager of depth 1.
 *
 * You cannot add additional polygons after this command.
 */
void
gimp_scan_convert_render (GimpScanConvert *sc,
                          TileManager     *tile_manager,
                          gint             off_x,
                          gint             off_y,
                          gboolean         antialias)
{
  PixelRegion  maskPR;
  gpointer     pr;

  g_return_if_fail (sc != NULL);
  g_return_if_fail (tile_manager != NULL);

  gimp_scan_convert_finish (sc);

  if (!sc->svp)
    return;

  pixel_region_init (&maskPR, tile_manager, 0, 0,
                     tile_manager_width (tile_manager),
                     tile_manager_height (tile_manager),
                     TRUE);

  g_return_if_fail (maskPR.bytes == 1);

  sc->antialias = antialias;

  for (pr = pixel_regions_register (1, &maskPR);
       pr != NULL;
       pr = pixel_regions_process (pr))
    {
      sc->buf = maskPR.data;
      sc->rowstride = maskPR.rowstride;
      sc->x0 = off_x + maskPR.x;
      sc->x1 = off_x + maskPR.x + maskPR.w;

      art_svp_render_aa (sc->svp,
                         sc->x0,
                         off_y + maskPR.y,
                         sc->x1,
                         off_y + maskPR.y + maskPR.h,
                         gimp_scan_convert_render_callback, sc);

    }
}


/* private function to convert the vpath to a svp when not using
 * gimp_scan_convert_stroke
 */
static void
gimp_scan_convert_finish (GimpScanConvert *sc)
{
  ArtSVP       *svp, *svp2;
  ArtSvpWriter *swr;

  /* return gracefully on empty path */
  if (!sc->vpath)
    return;

  if (sc->need_closing)
    gimp_scan_convert_close_add_points (sc);

  if (sc->svp)
    return;   /* We already have a valid SVP */

  /* Debug output of libart path */
  /* {
   *   gint i;
   *   for (i=0; i < sc->num_nodes + 1; i++)
   *     {
   *       g_printerr ("X: %f, Y: %f, Type: %d\n", sc->vpath[i].x,
   *                                               sc->vpath[i].y,
   *                                               sc->vpath[i].code );
   *     }
   * }
   */

  if (sc->have_open)
    {
      gint i;

      for (i = 0; i < sc->num_nodes; i++)
        if (sc->vpath[i].code == ART_MOVETO_OPEN)
          {
            g_printerr ("Fixing ART_MOVETO_OPEN - result might be incorrect\n");
            sc->vpath[i].code = ART_MOVETO;
          }
    }

  svp = art_svp_from_vpath (sc->vpath);

  swr = art_svp_writer_rewind_new (ART_WIND_RULE_ODDEVEN);
  art_svp_intersector (svp, swr);

  svp2 = art_svp_writer_rewind_reap (swr); /* this also frees swr */

  art_svp_free (svp);

  sc->svp = svp2;
}


/*
 * private function to render libart SVPRenderAASteps into the pixel region
 *
 * A function pretty similiar to this could be used to implement a
 * lookup table for the values (just change VALUE_TO_PIXEL).
 *
 * from the libart documentation:
 *
 * The value 0x8000 represents 0% coverage by the polygon, while
 * 0xff8000 represents 100% coverage. This format is designed so that
 * >> 16 results in a standard 0x00..0xff value range, with nice
 * rounding.
 */

static void
gimp_scan_convert_render_callback (gpointer            user_data,
                                   gint                y,
                                   gint                start_value,
                                   ArtSVPRenderAAStep *steps,
                                   gint                n_steps)
{
  GimpScanConvert *sc        = user_data;
  gint             cur_value = start_value;
  gint             k, run_x0, run_x1;

#define VALUE_TO_PIXEL(x) (sc->antialias ? \
                           ((x) >> 16)   : \
                           (((x) & (1 << 23) ? 255 : 0)))

  if (n_steps > 0)
    {
      run_x1 = steps[0].x;

      if (run_x1 > sc->x0)
        memset (sc->buf,
                VALUE_TO_PIXEL (cur_value),
                run_x1 - sc->x0);

      for (k = 0; k < n_steps - 1; k++)
        {
          cur_value += steps[k].delta;

          run_x0 = run_x1;
          run_x1 = steps[k + 1].x;

          if (run_x1 > run_x0)
            memset (sc->buf + run_x0 - sc->x0,
                    VALUE_TO_PIXEL (cur_value),
                    run_x1 - run_x0);
        }

      cur_value += steps[k].delta;

      if (sc->x1 > run_x1)
        memset (sc->buf + run_x1 - sc->x0,
                VALUE_TO_PIXEL (cur_value),
                sc->x1 - run_x1);
    }
  else
    {
      memset (sc->buf,
              VALUE_TO_PIXEL (cur_value),
              sc->x1 - sc->x0);
    }

  sc->buf += sc->rowstride;

#undef VALUE_TO_PIXEL
}