File: rcm_misc.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 (484 lines) | stat: -rw-r--r-- 12,552 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This is a plug-in for the GIMP.
 *
 * Colormap-Rotation plug-in. Exchanges two color ranges.
 *
 * Copyright (C) 1999 Sven Anders (anderss@fmi.uni-passau.de)
 *                    Based on code from Pavel Grinfeld (pavel@ml.com)
 *
 *
 * 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.
 */

/*----------------------------------------------------------------------------
 * Change log:
 *
 * Version 2.0, 04 April 1999.
 *  Nearly complete rewrite, made plug-in stable.
 *  (Works with GIMP 1.1 and GTK+ 1.2)
 *
 * Version 1.0, 27 March 1997.
 *  Initial (unstable) release by Pavel Grinfeld
 *
 *----------------------------------------------------------------------------*/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>

#include <gtk/gtk.h>

#include "libgimp/gimp.h"
#include "libgimp/gimpui.h"

#include "rcm.h"
#include "rcm_misc.h"
#include "rcm_gdk.h"

float
arctg (float y,
       float x)
{
  float temp = atan2(y,x);
  return (temp<0) ? (temp+TP) : temp;
}

float
min_prox (float alpha,
	  float beta,
	  float angle)
{
  gfloat temp1 = MIN (angle_mod_2PI (alpha - angle),
                      TP - angle_mod_2PI (alpha - angle));
  gfloat temp2 = MIN (angle_mod_2PI (beta - angle),
                      TP - angle_mod_2PI (beta - angle));

  return MIN(temp1, temp2);
}

float*
closest (float *alpha,
	 float *beta,
	 float  angle)
{
  gfloat temp_alpha = MIN (angle_mod_2PI (*alpha-angle),
                           TP - angle_mod_2PI (*alpha-angle));

  gfloat temp_beta  = MIN (angle_mod_2PI (*beta -angle),
                           TP - angle_mod_2PI (*beta -angle));

  if (temp_alpha-temp_beta < 0)
    return alpha;
  else
    return beta;
}

float
angle_mod_2PI (float angle)
{
  if (angle < 0)
    return angle + TP;
  else if (angle > TP)
    return angle - TP;
  else
    return angle;
}


/* supporting routines  */

float
rcm_linear (float A,
	    float B,
	    float C,
	    float D,
	    float x)
{
  if (B > A)
    {
      if (A<=x && x<=B)
        return C+(D-C)/(B-A)*(x-A);
      else if (A<=x+TP && x+TP<=B)
        return C+(D-C)/(B-A)*(x+TP-A);
      else
        return x;
    }
  else
    {
      if (B<=x && x<=A)
        return C+(D-C)/(B-A)*(x-A);
      else if (B<=x+TP && x+TP<=A)
        return C+(D-C)/(B-A)*(x+TP-A);
      else
        return x;
    }
}

float
rcm_left_end (RcmAngle *angle)
{
  gfloat alpha  = angle->alpha;
  gfloat beta   = angle->beta;
  gint   cw_ccw = angle->cw_ccw;

  switch (cw_ccw)
    {
    case (-1):
      if (alpha < beta) return alpha + TP;

    default:
      return alpha; /* 1 */
    }
}

float
rcm_right_end (RcmAngle *angle)
{
  gfloat alpha  = angle->alpha;
  gfloat beta   = angle->beta;
  gint   cw_ccw = angle->cw_ccw;

  switch (cw_ccw)
    {
    case 1:
      if (beta < alpha) return beta + TP;

    default:
      return beta; /* -1 */
    }
}

float
rcm_angle_inside_slice (float     angle,
			RcmAngle *slice)
{
  return angle_mod_2PI (slice->cw_ccw * (slice->beta-angle)) /
         angle_mod_2PI (slice->cw_ccw * (slice->beta-slice->alpha));
}

gboolean
rcm_is_gray (float s)
{
  return (s <= Current.Gray->gray_sat);
}


/* reduce image/selection for preview */

ReducedImage*
rcm_reduce_image (GimpDrawable *drawable,
		  GimpDrawable *mask,
		  gint          LongerSize,
		  gint          Slctn)
{
  guint32       gimage;
  GimpPixelRgn  srcPR, srcMask;
  ReducedImage *temp;
  guchar       *tempRGB, *src_row, *tempmask, *src_mask_row;
  gint          i, j, whichcol, whichrow, x1, x2, y1, y2;
  gint          RH, RW, width, height, bytes;
  gint          NoSelectionMade;
  gint          offx, offy;
  gdouble      *tempHSV, H, S, V;

  bytes = drawable->bpp;
  temp = g_new0 (ReducedImage, 1);

  /* get bounds of image or selection */

  gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);

  if (((x2-x1) != drawable->width) || ((y2-y1) != drawable->height))
    NoSelectionMade = FALSE;
  else
    NoSelectionMade = TRUE;

  switch (Slctn)
    {
    case ENTIRE_IMAGE:
      x1 = 0;
      x2 = drawable->width;
      y1 = 0;
      y2 = drawable->height;
      break;

    case SELECTION_IN_CONTEXT:
      x1 = MAX (0, x1 - (x2-x1) / 2.0);
      x2 = MIN (drawable->width, x2 + (x2-x1) / 2.0);
      y1 = MAX (0, y1 - (y2-y1) / 2.0);
      y2 = MIN (drawable->height, y2 + (y2-y1) / 2.0);
      break;

    default:
      break; /* take selection dimensions */
    }

  /* clamp to image size since this is the size of the mask */

  gimp_drawable_offsets (drawable->drawable_id, &offx, &offy);
  gimage = gimp_drawable_get_image (drawable->drawable_id);

  x1 = CLAMP (x1, - offx, gimp_image_width (gimage) - offx);
  x2 = CLAMP (x2, - offx, gimp_image_width (gimage) - offx);
  y1 = CLAMP (y1, - offy, gimp_image_height (gimage) - offy);
  y2 = CLAMP (y2, - offy, gimp_image_height (gimage) - offy);

  /* calculate size of preview */

  width  = x2 - x1;
  height = y2 - y1;

  if (width < 1 || height < 1)
    return temp;

  if (width > height)
    {
      RW = LongerSize;
      RH = (float) height * (float) LongerSize / (float) width;
    }
  else
    {
      RH = LongerSize;
      RW = (float)width * (float) LongerSize / (float) height;
    }

  /* allocate memory */

  tempRGB  = g_new (guchar, RW * RH * bytes);
  tempHSV  = g_new (gdouble, RW * RH * bytes);
  tempmask = g_new (guchar, RW * RH);

  gimp_pixel_rgn_init (&srcPR, drawable, x1, y1, width, height, FALSE, FALSE);
  gimp_pixel_rgn_init (&srcMask, mask,
                       x1 + offx, y1 + offy, width, height, FALSE, FALSE);

  src_row = g_new (guchar, width * bytes);
  src_mask_row = g_new (guchar, width * bytes);

  /* reduce image */

  for (i = 0; i < RH; i++)
    {
      whichrow = (float)i * (float)height / (float)RH;
      gimp_pixel_rgn_get_row (&srcPR, src_row, x1, y1 + whichrow, width);
      gimp_pixel_rgn_get_row (&srcMask, src_mask_row,
                              x1 + offx, y1 + offy + whichrow, width);

      for (j = 0; j < RW; j++)
        {
          whichcol = (float)j * (float)width / (float)RW;

          if (NoSelectionMade)
            tempmask[i*RW+j] = 255;
          else
            tempmask[i*RW+j] = src_mask_row[whichcol];

          gimp_rgb_to_hsv4 (&src_row[whichcol*bytes], &H, &S, &V);

          tempRGB[i*RW*bytes+j*bytes+0] = src_row[whichcol*bytes+0];
          tempRGB[i*RW*bytes+j*bytes+1] = src_row[whichcol*bytes+1];
          tempRGB[i*RW*bytes+j*bytes+2] = src_row[whichcol*bytes+2];

          tempHSV[i*RW*bytes+j*bytes+0] = H;
          tempHSV[i*RW*bytes+j*bytes+1] = S;
          tempHSV[i*RW*bytes+j*bytes+2] = V;

          if (bytes == 4)
            tempRGB[i*RW*bytes+j*bytes+3] = src_row[whichcol*bytes+3];
        }
    }

  /* return values */

  temp->width  = RW;
  temp->height = RH;
  temp->rgb    = tempRGB;
  temp->hsv    = tempHSV;
  temp->mask   = tempmask;

  return temp;
}


/* render before/after preview */
void
rcm_render_preview (GtkWidget *preview)
{
  ReducedImage *reduced;
  gint          version;
  gint          RW, RH, bytes, i, j;
  gboolean      unchanged, skip;
  guchar       *rgb_array, *a;
  gdouble       H, S, V;
  gdouble      *hsv_array;
  guchar        rgb[3];
  gfloat        degree;

  g_return_if_fail (preview != NULL);

  version = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (preview), "mode"));

  reduced = Current.reduced;
  RW = reduced->width;
  RH = reduced->height;
  bytes = Current.drawable->bpp;
  hsv_array = reduced->hsv;
  rgb_array = reduced->rgb;

  a = g_new (guchar, 4 * RW * RH);

  if (version == CURRENT)
    {
      for (i = 0; i < RH; i++)
        {
          for (j = 0; j < RW; j++)
            {
              unchanged = TRUE;
              skip = FALSE;

              H = hsv_array[i*RW*bytes + j*bytes + 0];
              S = hsv_array[i*RW*bytes + j*bytes + 1];
              V = hsv_array[i*RW*bytes + j*bytes + 2];

              if (rcm_is_gray(S) && (reduced->mask[i*RW+j] != 0))
                {
                  switch (Current.Gray_to_from)
                    {
                    case GRAY_FROM:
                      if (rcm_angle_inside_slice (Current.Gray->hue,
                                                  Current.From->angle) <= 1)
                        {
                          H = Current.Gray->hue/TP;
                          S = Current.Gray->satur;
                        }
                      else
                        skip = TRUE;
                      break;

                    case GRAY_TO:
                      unchanged = FALSE;
                      skip = TRUE;
                      gimp_hsv_to_rgb4 (rgb,
                                        Current.Gray->hue/TP,
                                        Current.Gray->satur,
                                        V);
                      break;

                    default:
                      break;
                    }
                }

              if (!skip)
                {
                  unchanged = FALSE;
                  H = rcm_linear (rcm_left_end (Current.From->angle),
                                  rcm_right_end (Current.From->angle),
                                  rcm_left_end (Current.To->angle),
                                  rcm_right_end (Current.To->angle),
                                  H * TP);

                  H = angle_mod_2PI(H) / TP;
                  gimp_hsv_to_rgb4 (rgb, H,S,V);
                }

              if (unchanged)
                degree = 0;
              else
                degree = reduced->mask[i*RW+j] / 255.0;

              a[(i*RW+j)*4+0] = (1-degree) * rgb_array[i*RW*bytes + j*bytes + 0] + degree * rgb[0];
              a[(i*RW+j)*4+1] = (1-degree) * rgb_array[i*RW*bytes + j*bytes + 1] + degree * rgb[1];
              a[(i*RW+j)*4+2] = (1-degree) * rgb_array[i*RW*bytes + j*bytes + 2] + degree * rgb[2];

              /* apply transparency */
              if (bytes == 4)
                a[(i*RW+j)*4+3] = rgb_array[i*RW*bytes+j*bytes+3];
              else
                a[(i*RW+j)*4+3] = 255;
            }
        }
    }
  else /* ORIGINAL */
    {
      for (i = 0; i < RH; i++)
        {
          for (j = 0; j < RW; j++)
            {
              a[(i*RW+j)*4+0] = rgb_array[i*RW*bytes + j*bytes + 0];
              a[(i*RW+j)*4+1] = rgb_array[i*RW*bytes + j*bytes + 1];
              a[(i*RW+j)*4+2] = rgb_array[i*RW*bytes + j*bytes + 2];

              if (bytes == 4)
                a[(i*RW+j)*4+3] = rgb_array[i*RW*bytes+j*bytes+3];
              else
                a[(i*RW+j)*4+3] = 255;
            }
        }
    }

  gimp_preview_area_draw (GIMP_PREVIEW_AREA (preview),
                          0, 0, RW, RH,
                          GIMP_RGBA_IMAGE,
                          a,
                          RW * 4);
  g_free (a);
}

/* render circle */

void
rcm_render_circle (GtkWidget *preview,
		   int        sum,
		   int        margin)
{
  gint i, j;
  gdouble h, s, v;
  guchar *a;

  if (preview == NULL) return;

  a = g_new (guchar, 3*sum*sum);

  for (j = 0; j < sum; j++)
    {
      for (i = 0; i < sum; i++)
        {
          s = sqrt ((SQR (i - sum / 2.0) + SQR (j - sum / 2.0)) / (float) SQR (sum / 2.0 - margin));
          if (s > 1)
            {
              a[(j*sum+i)*3+0] = 255;
              a[(j*sum+i)*3+1] = 255;
              a[(j*sum+i)*3+2] = 255;
            }
          else
            {
              h = arctg (sum / 2.0 - j, i - sum / 2.0) / (2 * G_PI);
              v = 1 - sqrt (s) / 4;
              gimp_hsv_to_rgb4 (&a[(j*sum+i)*3], h, s, v);
            }
        }
    }
  gimp_preview_area_draw (GIMP_PREVIEW_AREA (preview),
                          0, 0, sum, sum,
                          GIMP_RGB_IMAGE,
                          a,
                          sum * 3);
  g_free (a);
}