home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / rcm / rcm_misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-24  |  11.8 KB  |  493 lines

  1. /*
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * This is a plug-in for the GIMP.
  5.  *
  6.  * Colormap-Rotation plug-in. Exchanges two color ranges.
  7.  *
  8.  * Copyright (C) 1999 Sven Anders (anderss@fmi.uni-passau.de)
  9.  *                    Based on code from Pavel Grinfeld (pavel@ml.com)
  10.  *
  11.  *
  12.  * This program is free software; you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation; either version 2 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program; if not, write to the Free Software
  24.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  25.  */
  26.  
  27. /*-----------------------------------------------------------------------------------
  28.  * Change log:
  29.  * 
  30.  * Version 2.0, 04 April 1999.
  31.  *  Nearly complete rewrite, made plug-in stable.
  32.  *  (Works with GIMP 1.1 and GTK+ 1.2)
  33.  *
  34.  * Version 1.0, 27 March 1997.
  35.  *  Initial (unstable) release by Pavel Grinfeld
  36.  *
  37.  *-----------------------------------------------------------------------------------*/
  38.  
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <math.h>
  42.  
  43. #include <gtk/gtk.h>
  44. #include <libgimp/gimp.h>
  45.  
  46. #include "rcm.h"
  47. #include "rcm_misc.h"
  48. #include "rcm_gdk.h"
  49.  
  50. /*-----------------------------------------------------------------------------------*/
  51.  
  52. float 
  53. arctg (float y, 
  54.        float x)
  55. {
  56.   float temp = atan2(y,x);
  57.   return (temp<0) ? (temp+TP) : temp;
  58. }
  59.  
  60. inline 
  61. float sign (float x)
  62. {
  63.   return (x<0)?(-1):(1);
  64. }
  65.  
  66. /*-----------------------------------------------------------------------------------*/
  67.  
  68. float 
  69. min_prox (float alpha, 
  70.       float beta, 
  71.       float angle)
  72. {
  73.   gfloat temp1 = MIN(angle_mod_2PI(alpha - angle),
  74.            TP-angle_mod_2PI(alpha - angle));
  75.   gfloat temp2 = MIN(angle_mod_2PI(beta - angle),
  76.            TP-angle_mod_2PI(beta - angle));
  77.  
  78.   return MIN(temp1, temp2);
  79. }
  80.  
  81. float*
  82. closest (float *alpha, 
  83.      float *beta, 
  84.      float  angle)
  85. {
  86.   float temp_alpha = MIN(angle_mod_2PI(*alpha-angle),
  87.              TP-angle_mod_2PI(*alpha-angle));
  88.  
  89.   float temp_beta  = MIN(angle_mod_2PI(*beta -angle),
  90.              TP-angle_mod_2PI(*beta -angle));
  91.  
  92.   if (temp_alpha-temp_beta<0) 
  93.     return alpha;
  94.   else 
  95.     return beta;
  96. }
  97.  
  98. float 
  99. angle_mod_2PI (float angle)
  100. {
  101.   if (angle < 0) 
  102.     return angle + TP;
  103.   else if (angle > TP)
  104.     return angle - TP;
  105.   else
  106.     return angle;
  107. }
  108.  
  109. /*-----------------------------------------------------------------------------------*/
  110. /* supporting routines  */
  111. /*-----------------------------------------------------------------------------------*/
  112.  
  113. float 
  114. rcm_linear (float A, 
  115.         float B, 
  116.         float C, 
  117.         float D, 
  118.         float x)
  119. {
  120.   if (B > A)
  121.     if (A<=x && x<=B)
  122.       return C+(D-C)/(B-A)*(x-A);
  123.     else if (A<=x+TP && x+TP<=B)
  124.       return C+(D-C)/(B-A)*(x+TP-A);    
  125.     else
  126.       return x;
  127.   else 
  128.     if (B<=x && x<=A)
  129.       return C+(D-C)/(B-A)*(x-A);
  130.     else if (B<=x+TP && x+TP<=A)
  131.       return C+(D-C)/(B-A)*(x+TP-A);    
  132.     else
  133.       return x;
  134. }
  135.  
  136. float 
  137. rcm_left_end (RcmAngle *angle)
  138. {
  139.   gfloat alpha  = angle->alpha;
  140.   gfloat beta   = angle->beta;
  141.   gint   cw_ccw = angle->cw_ccw;
  142.   
  143.   switch (cw_ccw)
  144.   {
  145.     case (-1): if (alpha < beta) return alpha + TP;
  146.     default: return alpha; /* 1 */
  147.   }
  148. }
  149.  
  150. float 
  151. rcm_right_end (RcmAngle *angle)
  152. {
  153.   gfloat alpha  = angle->alpha;
  154.   gfloat beta   = angle->beta;
  155.   gint   cw_ccw = angle->cw_ccw;
  156.   
  157.   switch (cw_ccw)
  158.   {
  159.     case 1: if (beta < alpha) return beta + TP;
  160.     default: return beta; /* -1 */
  161.   }
  162. }
  163.  
  164. float 
  165. rcm_angle_inside_slice (float     angle, 
  166.             RcmAngle *slice)
  167. {
  168.   return angle_mod_2PI(slice->cw_ccw * (slice->beta-angle)) /
  169.          angle_mod_2PI(slice->cw_ccw * (slice->beta-slice->alpha));
  170. }
  171.  
  172. gint 
  173. rcm_is_gray (float s)
  174. {
  175.   if (s <= Current.Gray->gray_sat) return 1;
  176.   return 0;
  177. }
  178.  
  179. /*-----------------------------------------------------------------------------------*/
  180. /* reduce image/selection for preview */
  181. /*-----------------------------------------------------------------------------------*/
  182.  
  183. ReducedImage*
  184. rcm_reduce_image (GimpDrawable *drawable, 
  185.           GimpDrawable *mask, 
  186.           gint       LongerSize, 
  187.           gint       Slctn)
  188. {
  189.   GimpPixelRgn srcPR, srcMask;
  190.   ReducedImage *temp;
  191.   guchar *tempRGB, *src_row, *tempmask, *src_mask_row;
  192.   gint i, j, whichcol, whichrow, x1, x2, y1, y2;
  193.   gint RH, RW, width, height, bytes;
  194.   gint NoSelectionMade;
  195.   gdouble *tempHSV, H, S, V;
  196.  
  197.   bytes = drawable->bpp;  
  198.   temp = g_new (ReducedImage, 1);
  199.  
  200.   /* get bounds of image or selection */
  201.  
  202.   gimp_drawable_mask_bounds(drawable->id, &x1, &y1, &x2, &y2);
  203.  
  204.   if ( ((x2-x1) != drawable->width) && ((y2-y1) != drawable->height)) 
  205.     NoSelectionMade = FALSE;
  206.   else
  207.     NoSelectionMade = TRUE;
  208.  
  209.   switch (Slctn)
  210.   {
  211.     case ENTIRE_IMAGE:
  212.       x1 = 0;
  213.       x2 = drawable->width;
  214.       y1 = 0;
  215.       y2 = drawable->height;
  216.       break;  
  217.  
  218.     case SELECTION_IN_CONTEXT:
  219.       x1 = MAX(0, x1 - (x2-x1) / 2.0);
  220.       x2 = MIN(drawable->width, x2 + (x2-x1) / 2.0);
  221.       y1 = MAX(0, y1 - (y2-y1) / 2.0);
  222.       y2 = MIN(drawable->height, y2 + (y2-y1) / 2.0);
  223.       break;  
  224.  
  225.     default:
  226.       break; /* take selection dimensions */
  227.   }
  228.  
  229.   /* calculate size of preview */
  230.   
  231.   width  = x2 - x1;
  232.   height = y2 - y1;
  233.  
  234.   if (width > height)
  235.   {
  236.     RW = LongerSize;
  237.     RH = (float) height * (float) LongerSize / (float) width;
  238.   }
  239.   else
  240.   {
  241.     RH = LongerSize;
  242.     RW = (float)width * (float) LongerSize / (float) height;
  243.   }
  244.  
  245.   /* allocate memory */
  246.   
  247.   tempRGB  = g_new (guchar, RW * RH * bytes);
  248.   tempHSV  = g_new (gdouble, RW * RH * bytes);
  249.   tempmask = g_new (guchar, RW * RH);
  250.  
  251.   gimp_pixel_rgn_init(&srcPR, drawable, x1, y1, width, height, FALSE, FALSE);
  252.   gimp_pixel_rgn_init(&srcMask, mask, x1, y1, width, height, FALSE, FALSE);
  253.  
  254.   src_row = g_new (guchar, width * bytes);
  255.   src_mask_row = g_new (guchar, width * bytes);
  256.  
  257.   /* reduce image */
  258.  
  259.   for (i=0; i<RH; i++)
  260.   {
  261.     whichrow = (float)i * (float)height / (float)RH;
  262.     gimp_pixel_rgn_get_row (&srcPR, src_row, x1, y1+whichrow, width);   
  263.     gimp_pixel_rgn_get_row (&srcMask, src_mask_row, x1, y1+whichrow, width);
  264.    
  265.     for (j=0; j<RW; j++)
  266.     {
  267.       whichcol = (float)j * (float)width / (float)RW;
  268.  
  269.       if (NoSelectionMade)
  270.     tempmask[i*RW+j] = 255;
  271.       else
  272.     tempmask[i*RW+j] = src_mask_row[whichcol];
  273.       
  274.       gimp_rgb_to_hsv4 (&src_row[whichcol*bytes], &H, &S, &V);
  275.  
  276.       tempRGB[i*RW*bytes+j*bytes+0] = src_row[whichcol*bytes+0];
  277.       tempRGB[i*RW*bytes+j*bytes+1] = src_row[whichcol*bytes+1];
  278.       tempRGB[i*RW*bytes+j*bytes+2] = src_row[whichcol*bytes+2];
  279.  
  280.       tempHSV[i*RW*bytes+j*bytes+0] = H;
  281.       tempHSV[i*RW*bytes+j*bytes+1] = S;
  282.       tempHSV[i*RW*bytes+j*bytes+2] = V;
  283.  
  284.       if (bytes == 4)
  285.     tempRGB[i*RW*bytes+j*bytes+3] = src_row[whichcol*bytes+3];
  286.  
  287.     } /* for j */
  288.   } /* for i */
  289.  
  290.   /* return values */
  291.  
  292.   temp->width = RW;
  293.   temp->height = RH;
  294.   temp->rgb = tempRGB;
  295.   temp->hsv = tempHSV;
  296.   temp->mask = tempmask;  
  297.  
  298.   return temp;
  299. }
  300.  
  301. /*-----------------------------------------------------------------------------------*/
  302. /* render before/after preview */
  303. /*-----------------------------------------------------------------------------------*/
  304.  
  305. static gint 
  306. rcm_fake_transparency (gint i, 
  307.                gint j)
  308. {
  309.   if ( ((i%20)-10)*((j%20)-10) > 0 ) 
  310.     return 102;
  311.  
  312.   return 153;
  313. }
  314.  
  315. void 
  316. rcm_render_preview (GtkWidget *preview, 
  317.             gint       version)
  318.   ReducedImage *reduced;
  319.   gint RW, RH, bytes, i, j, k, unchanged, skip;
  320.   guchar *rgb_array, *a;
  321.   gdouble H, S, V;
  322.   gdouble *hsv_array;
  323.   guchar rgb[3];
  324.   float degree, transp;
  325.  
  326.   /* init some variables */
  327.  
  328.   reduced = Current.reduced;
  329.   RW = reduced->width;
  330.   RH = reduced->height;
  331.   bytes = Current.drawable->bpp;
  332.   hsv_array = reduced->hsv;
  333.   rgb_array = reduced->rgb;
  334.  
  335.   a = g_new (guchar, bytes * RW);
  336.   
  337.   if (preview == NULL)
  338.   {
  339.     printf("Asked to preview a NULL! Shouldn't happen!\n");
  340.     return;
  341.   }
  342.  
  343.   if (version == CURRENT) 
  344.   {
  345.     for (i=0; i<RH; i++)
  346.     {
  347.       for (j=0; j<RW; j++)
  348.       {
  349.     unchanged = 1; /* TRUE */
  350.     skip = 0; /* FALSE */
  351.     
  352.     H = hsv_array[i*RW*bytes + j*bytes + 0];
  353.     S = hsv_array[i*RW*bytes + j*bytes + 1];
  354.     V = hsv_array[i*RW*bytes + j*bytes + 2];
  355.     
  356.     if (rcm_is_gray(S) && (reduced->mask[i*RW+j] != 0))
  357.     {
  358.       switch (Current.Gray_to_from)
  359.       {
  360.         case GRAY_FROM:
  361.           if (rcm_angle_inside_slice(Current.Gray->hue, Current.From->angle) <= 1) 
  362.           {
  363.         H = Current.Gray->hue/TP;
  364.         S = Current.Gray->satur;
  365.           }
  366.           else 
  367.         skip = 1;
  368.           break;
  369.         
  370.         case GRAY_TO:  
  371.           unchanged = 0; 
  372.           skip = 1;
  373.           gimp_hsv_to_rgb4 (rgb, Current.Gray->hue/TP, Current.Gray->satur, V);
  374.         break;
  375.         
  376.         default: break;
  377.       } /* switch */
  378.     } /* if */
  379.  
  380.     if (!skip)
  381.     {
  382.       unchanged = 0;
  383.       H = rcm_linear(rcm_left_end(Current.From->angle),
  384.              rcm_right_end(Current.From->angle),
  385.              rcm_left_end(Current.To->angle),
  386.              rcm_right_end(Current.To->angle),
  387.              H*TP);    
  388.  
  389.       H = angle_mod_2PI(H) / TP;
  390.       gimp_hsv_to_rgb4 (rgb, H,S,V);
  391.     } /* if (!skip) */
  392.     
  393.     if (unchanged)degree = 0;
  394.     else
  395.       degree = reduced->mask[i*RW+j] / 255.0;
  396.     
  397.     a[j*3+0] = (1-degree) * rgb_array[i*RW*bytes + j*bytes + 0] + degree * rgb[0];
  398.     a[j*3+1] = (1-degree) * rgb_array[i*RW*bytes + j*bytes + 1] + degree * rgb[1];
  399.     a[j*3+2] = (1-degree) * rgb_array[i*RW*bytes + j*bytes + 2] + degree * rgb[2];
  400.     
  401.     /* apply transparency */
  402.     if (bytes == 4) 
  403.     {
  404.       for (k=0; k<3; k++)
  405.       {
  406.         /*        transp = reduced->mask[i*RW*bytes+j*bytes+3] / 255.0; */
  407.         transp = rgb_array[i*RW*bytes+j*bytes+3] / 255.0;
  408.         a[3*j+k] = transp * a[3*j+k] + (1-transp) * rcm_fake_transparency(i,j);
  409.       }
  410.  
  411.     } /* if */
  412.  
  413.       } /* for j */
  414.  
  415.       gtk_preview_draw_row(GTK_PREVIEW(preview), a, 0, i, RW);
  416.  
  417.     } /* for i */
  418.   }
  419.   else /* ORIGINAL */
  420.   {
  421.     for (i=0; i<RH; i++)
  422.     {
  423.       for (j=0; j<RW; j++)
  424.       {
  425.     a[j*3+0] = rgb_array[i*RW*bytes + j*bytes + 0];
  426.     a[j*3+1] = rgb_array[i*RW*bytes + j*bytes + 1];
  427.     a[j*3+2] = rgb_array[i*RW*bytes + j*bytes + 2];
  428.       
  429.     if (bytes == 4) 
  430.     {
  431.       for (k=0; k<3; k++)
  432.       {
  433.         transp = rgb_array[i*RW*bytes+j*bytes+3] / 255.0;
  434.         a[3*j+k] = transp * a[3*j+k] + (1-transp) * rcm_fake_transparency(i,j);
  435.       }
  436.     } /* if */
  437.  
  438.       } /* for j */
  439.  
  440.       gtk_preview_draw_row(GTK_PREVIEW(preview), a, 0, i, RW);
  441.  
  442.     } /* for i */
  443.   }
  444.  
  445.   g_free (a); 
  446.   gtk_widget_draw(preview, NULL);
  447.   gdk_flush();
  448. }
  449.  
  450. /*-----------------------------------------------------------------------------------*/
  451. /* render circle */
  452. /*-----------------------------------------------------------------------------------*/
  453.  
  454. void 
  455. rcm_render_circle (GtkWidget *preview, 
  456.            int        sum, 
  457.            int        margin)
  458.   gint i, j;
  459.   gdouble h, s, v;
  460.   guchar *a;
  461.  
  462.   a = g_new (guchar, 3*sum);
  463.   
  464.   if (preview == NULL) return;
  465.   
  466.   for (j = 0; j < sum; j++)
  467.   {
  468.     for (i = 0; i < sum; i++)
  469.     {
  470.       s = sqrt ((SQR (i - sum / 2.0) + SQR (j - sum / 2.0)) / (float) SQR (sum / 2.0 - margin));
  471.       if (s > 1)
  472.       {
  473.     a[i*3+0] = 255;
  474.     a[i*3+1] = 255;
  475.     a[i*3+2] = 255;
  476.       }
  477.       else
  478.       {
  479.     h = arctg (sum / 2.0 - j, i - sum / 2.0) / (2 * G_PI);
  480.     v = 1 - sqrt (s) / 4;
  481.     gimp_hsv_to_rgb4 (&a[i*3], h, s, v);
  482.       }    
  483.     }   
  484.     gtk_preview_draw_row(GTK_PREVIEW(preview), a, 0, j, sum);
  485.   }
  486.   
  487.   g_free (a); 
  488.   gtk_widget_draw (preview, NULL);
  489.   gdk_flush ();
  490. }
  491.