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

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <gtk/gtk.h>
  5.  
  6. #include <libgimp/gimp.h>
  7.  
  8. #include "fp.h"
  9.  
  10. extern FP_Params Current;
  11.  
  12. extern GimpDrawable *drawable, *mask;
  13. extern ReducedImage *reduced;
  14.  
  15. extern gint nudgeArray[256];
  16.  
  17. gint colorSign[3][ALL_PRIMARY]=
  18. {{1,-1,-1,-1,1,1},{-1,1,-1,1,1,-1},{-1,-1,1,1,-1,1}};
  19.  
  20.  
  21. void initializeFilterPacks()
  22. {
  23.   gint i, j;
  24.   for (i=0; i<256; i++) 
  25.     for (j=BY_HUE; j<JUDGE_BY; j++) {
  26.       Current.redAdj   [j][i]=0;
  27.       Current.greenAdj [j][i]=0;
  28.       Current.blueAdj  [j][i]=0;
  29.       Current.satAdj   [j][i]=0;
  30.     }
  31. }
  32.  
  33. void resetFilterPacks()
  34. {
  35.   initializeFilterPacks();
  36.   refreshPreviews(Current.VisibleFrames);  
  37. }
  38.  
  39. ReducedImage *Reduce_The_Image(GimpDrawable *drawable,
  40.                    GimpDrawable *mask,
  41.                    gint LongerSize, 
  42.                    gint Slctn)
  43. {
  44.   gint RH, RW, width, height, bytes=drawable->bpp;  
  45.   ReducedImage *temp=(ReducedImage *)malloc(sizeof(ReducedImage));
  46.   guchar *tempRGB, *src_row, *tempmask, *src_mask_row,R,G,B;
  47.   gint i, j, whichcol, whichrow, x1, x2, y1, y2;
  48.   GimpPixelRgn srcPR, srcMask;
  49.   gint NoSelectionMade=TRUE;
  50.   hsv *tempHSV, H, S, V;
  51.  
  52.   gimp_drawable_mask_bounds (drawable->id, &x1, &y1, &x2, &y2);
  53.   width  = x2-x1;
  54.   height = y2-y1;
  55.  
  56.   if (width != drawable->width && height != drawable->height) 
  57.     NoSelectionMade=FALSE;
  58.  
  59.   if (Slctn==0) {
  60.     x1=0;
  61.     x2=drawable->width;
  62.     y1=0;
  63.     y2=drawable->height;
  64.   }  
  65.  
  66.   if (Slctn==2) {
  67.     x1=MAX(0,                x1-width/2.0);
  68.     x2=MIN(drawable->width,  x2+width/2.0);
  69.     y1=MAX(0,                y1-height/2.0);
  70.     y2=MIN(drawable->height, y2+height/2.0);
  71.   }  
  72.  
  73.   width  = x2-x1;
  74.   height = y2-y1;
  75.  
  76.   if (width>height) {
  77.     RW=LongerSize;
  78.     RH=(float) height * (float) LongerSize/ (float) width;
  79.   }
  80.   else {
  81.     RH=LongerSize;
  82.     RW=(float)width * (float) LongerSize/ (float) height;
  83.   }
  84.   
  85.   tempRGB   = (guchar *) malloc(RW*RH*bytes);
  86.   tempHSV   = (hsv *)    malloc(RW*RH*bytes*sizeof(hsv));
  87.   tempmask  = (guchar *) malloc(RW*RH);
  88.  
  89.   gimp_pixel_rgn_init (&srcPR, drawable, x1, y1, width, height, FALSE, FALSE);
  90.   gimp_pixel_rgn_init (&srcMask, mask, x1, y1, width, height, FALSE, FALSE);
  91.   
  92.   src_row = (guchar *) malloc (width*bytes);
  93.   src_mask_row = (guchar *) malloc (width*bytes);
  94.  
  95.   for (i=0; i<RH; i++) {
  96.     whichrow=(float)i*(float)height/(float)RH;
  97.     gimp_pixel_rgn_get_row (&srcPR, src_row, x1, y1+whichrow, width);   
  98.     gimp_pixel_rgn_get_row (&srcMask, src_mask_row, x1, y1+whichrow, width);
  99.    
  100.     for (j=0; j<RW; j++) {
  101.       whichcol=(float)j*(float)width/(float)RW;
  102.  
  103.       if (NoSelectionMade)
  104.     tempmask[i*RW+j]=255;
  105.       else
  106.     tempmask[i*RW+j]=src_mask_row[whichcol];
  107.  
  108.       R=src_row[whichcol*bytes+0];
  109.       G=src_row[whichcol*bytes+1];
  110.       B=src_row[whichcol*bytes+2];
  111.       
  112.       H = R/255.0;
  113.       S = G/255.0;
  114.       V = B/255.0;
  115.       gimp_rgb_to_hsv_double(&H,&S,&V);
  116.  
  117.       tempRGB[i*RW*bytes+j*bytes+0]=R;
  118.       tempRGB[i*RW*bytes+j*bytes+1]=G;
  119.       tempRGB[i*RW*bytes+j*bytes+2]=B;
  120.  
  121.       tempHSV[i*RW*bytes+j*bytes+0]=H;
  122.       tempHSV[i*RW*bytes+j*bytes+1]=S;
  123.       tempHSV[i*RW*bytes+j*bytes+2]=V;
  124.  
  125.       if (bytes==4)
  126.     tempRGB[i*RW*bytes+j*bytes+3]=src_row[whichcol*bytes+3];
  127.          
  128.     }
  129.   }
  130.   temp->width=RW;
  131.   temp->height=RH;
  132.   temp->rgb=tempRGB;
  133.   temp->hsv=tempHSV;
  134.   temp->mask=tempmask;  
  135.   return temp;
  136. }
  137.  
  138. /*************************************************************/
  139. /************** The Preview Function *************************/
  140. void 
  141. fp_render_preview(GtkWidget     *preview, 
  142.           gint          changewhat,
  143.           gint          changewhich)
  144.   guchar *a;
  145.   gint Inten, bytes=drawable->bpp;
  146.   gint i, j, k, nudge, M, m, middle,JudgeBy;
  147.   float partial;
  148.   gint RW=reduced->width;
  149.   gint RH=reduced->height;
  150.   gint backupP[3], P[3], tempSat[JUDGE_BY][256];
  151.  
  152.   a =(guchar *) malloc(bytes*RW);
  153.    
  154.   if (changewhat==SATURATION)
  155.     for (k=0; k<256; k++) {
  156.       for (JudgeBy=BY_HUE; JudgeBy<JUDGE_BY; JudgeBy++)
  157.     tempSat[JudgeBy][k]=0; 
  158.     tempSat[Current.ValueBy][k] += changewhich*nudgeArray[(k+Current.Offset)%256];
  159.     }
  160.       
  161.   for (i=0; i<RH; i++) {
  162.     for (j=0; j<RW; j++) {
  163.       
  164.       backupP[0] = P[0]  = (int) reduced->rgb[i*RW*bytes + j*bytes + 0];
  165.       backupP[1] = P[1]  = (int) reduced->rgb[i*RW*bytes + j*bytes + 1];
  166.       backupP[2] = P[2]  = (int) reduced->rgb[i*RW*bytes + j*bytes + 2];
  167.       
  168.       m = MIN(MIN(P[0],P[1]),P[2]);
  169.       M = MAX(MAX(P[0],P[1]),P[2]);
  170.       middle=(M+m)/2;
  171.       for (k=0; k<3; k++)
  172.     if (P[k]!=m && P[k]!=M) middle=P[k];
  173.       
  174.       partial = reduced->mask[i*RW+j]/255.0;
  175.       
  176.       for (JudgeBy=BY_HUE; JudgeBy<JUDGE_BY; JudgeBy++) {
  177.     if (!Current.Touched[JudgeBy]) continue;
  178.     
  179.     Inten   = reduced->hsv[i*RW*bytes + j*bytes + JudgeBy]*255.0;
  180.     
  181.     /*DO SATURATION FIRST*/
  182.     if (changewhat != NONEATALL) {
  183.       if (M!=m) {
  184.         for (k=0; k<3; k++)
  185.           if (backupP[k] == M)
  186.         P[k] = MAX(P[k]+partial*Current.satAdj[JudgeBy][Inten],middle);
  187.           else if (backupP[k] == m)
  188.         P[k] = MIN(P[k]-partial*Current.satAdj[JudgeBy][Inten],middle); 
  189.       }
  190.       P[0]  += partial*Current.redAdj[JudgeBy][Inten];
  191.       P[1]  += partial*Current.greenAdj[JudgeBy][Inten];
  192.       P[2]  += partial*Current.blueAdj[JudgeBy][Inten];
  193.     }
  194.       }
  195.       
  196.       Inten   = reduced->hsv[i*RW*bytes + j*bytes + Current.ValueBy]*255.0;
  197.       nudge   = partial*nudgeArray[(Inten+Current.Offset)%256];  
  198.  
  199.       switch (changewhat) {
  200.       case HUE:
  201.     P[0]  += colorSign[RED][changewhich]   * nudge;
  202.     P[1]  += colorSign[GREEN][changewhich] * nudge; 
  203.     P[2]  += colorSign[BLUE][changewhich]  * nudge;
  204.     break;
  205.     
  206.       case SATURATION:
  207.     for (JudgeBy=BY_HUE; JudgeBy<JUDGE_BY; JudgeBy++)
  208.       for (k=0; k<3; k++)
  209.         if (M!=m) {
  210.           if (backupP[k] == M)
  211.         P[k] = MAX(P[k]+
  212.                partial*tempSat[JudgeBy][Inten],middle);
  213.           else if (backupP[k] == m)
  214.         P[k] = MIN(P[k]-
  215.                partial*tempSat[JudgeBy][Inten],middle);
  216.         }
  217.     break;
  218.  
  219.       case VALUE:
  220.     P[0]  += changewhich  * nudge;
  221.     P[1]  += changewhich  * nudge;
  222.     P[2]  += changewhich  * nudge;
  223.     break;
  224.  
  225.       default: 
  226.     break;
  227.       }
  228.       
  229.       a[j*3+0] = MAX(0,MIN(P[0], 255));
  230.       a[j*3+1] = MAX(0,MIN(P[1], 255));
  231.       a[j*3+2] = MAX(0,MIN(P[2], 255));
  232.  
  233.       if (bytes==4) 
  234.     for (k=0; k<3; k++) {
  235.       float transp=reduced->rgb[i*RW*bytes+j*bytes+3]/255.0;
  236.       a[3*j+k]=transp*a[3*j+k]+(1-transp)*fp_fake_transparency(i,j);
  237.     }
  238.     }
  239.     gtk_preview_draw_row( GTK_PREVIEW(preview),a,0,i,RW);
  240.   }
  241.   
  242.   free(a); 
  243.   gtk_widget_draw(preview,NULL);
  244.   gdk_flush();
  245. }
  246.  
  247. void      
  248. Update_Current_FP     (gint   changewhat,
  249.                gint   changewhich)
  250. {
  251.   int i, nudge;
  252.   
  253.   for (i=0; i<256; i++) {
  254.     
  255.     fp_Create_Nudge(nudgeArray);
  256.     nudge=nudgeArray[(i+Current.Offset)%256];
  257.     
  258.     switch (changewhat) {
  259.     case HUE:
  260.       Current.redAdj[Current.ValueBy][i]   += 
  261.     colorSign[RED][changewhich] * nudge;
  262.       
  263.       Current.greenAdj[Current.ValueBy][i] += 
  264.     colorSign[GREEN][changewhich] * nudge;
  265.       
  266.       Current.blueAdj[Current.ValueBy][i]  += 
  267.     colorSign[BLUE][changewhich]  * nudge;
  268.       break;
  269.       
  270.     case SATURATION:
  271.       Current.satAdj[Current.ValueBy][i] += changewhich*nudge;
  272.       break;
  273.       
  274.     case VALUE:
  275.       Current.redAdj[Current.ValueBy][i]   += changewhich * nudge;
  276.       Current.greenAdj[Current.ValueBy][i] += changewhich * nudge;
  277.       Current.blueAdj[Current.ValueBy][i]  += changewhich * nudge;
  278.       break;
  279.       
  280.     default:
  281.       break;
  282.     } /* switch */
  283.     
  284.   } /* for */
  285. }
  286.  
  287.  
  288. void
  289. fp_create_smoothness_graph        (GtkWidget *preview)
  290. {
  291.   guchar data[256*3];
  292.   gint nArray[256];
  293.   int i, j, toBeBlack;
  294.  
  295.   fp_Create_Nudge(nArray);
  296.  
  297.   for (i=0; i<MAX_ROUGHNESS; i++)
  298.     {
  299.       int coor=MAX_ROUGHNESS-i;
  300.       for (j=0; j<256; j++) {
  301.     data[3*j+0]=255;
  302.     data[3*j+1]=255;
  303.     data[3*j+2]=255;
  304.     if  (!(i%(MAX_ROUGHNESS/4))) {
  305.       data[3*j+0]=255;
  306.       data[3*j+1]=128;
  307.       data[3*j+2]=128;
  308.     }
  309.     if  (!((j+1)%32)) {
  310.       data[3*j+0]=255;
  311.       data[3*j+1]=128;
  312.       data[3*j+2]=128;
  313.     }
  314.     toBeBlack=0;
  315.     if (nArray[j]==coor) toBeBlack=1;
  316.  
  317.     if (j<255) {
  318.       int jump=abs(nArray[j]-nArray[j+1]);
  319.       if (   abs(coor-nArray[j])   < jump 
  320.           && abs(coor-nArray[j+1]) < jump)
  321.         toBeBlack=1;
  322.     }
  323.     if (toBeBlack) {
  324.       data[3*j+0]=0;
  325.       data[3*j+1]=0;
  326.       data[3*j+2]=0;
  327.     }  
  328.       }
  329.       gtk_preview_draw_row( GTK_PREVIEW(preview),data,0,i,256);
  330.     }
  331.   gtk_widget_draw(preview,NULL);
  332.   gdk_flush();
  333. }
  334.  
  335. void
  336. fp_range_preview_spill(GtkWidget *preview, int type)
  337. {
  338.   guchar data[256*3];
  339.   int i, j;
  340.   hsv R,G,B;
  341.   
  342.   for (i=0; i<RANGE_HEIGHT; i++) {
  343.     for (j=0; j<256; j++)
  344.       if  (!((j+1)%32)) {
  345.     data[3*j+0]=255;
  346.     data[3*j+1]=128;
  347.     data[3*j+2]=128;
  348.       }
  349.       else
  350.     switch (type) {
  351.     case BY_VAL:
  352.       data[3*j+0]=j-Current.Offset;
  353.       data[3*j+1]=j-Current.Offset;
  354.       data[3*j+2]=j-Current.Offset;
  355.       break;
  356.     case BY_HUE:
  357.       R = (hsv)((j-Current.Offset+256)%256)/255.0;
  358.       G = 1.0;
  359.       B = .5;
  360.       gimp_hsv_to_rgb_double(&R, &G, &B);
  361.       data[3*j+0]=R*255;
  362.       data[3*j+1]=G*255;
  363.       data[3*j+2]=B*255;
  364.       break;
  365.     case BY_SAT:
  366.       R = .5;
  367.       G = (hsv)((j-(gint)Current.Offset+256)%256)/255.0;
  368.       B = .5;
  369.       gimp_hsv_to_rgb_double(&R,&G,&B);
  370.       data[3*j+0]=R*255;
  371.       data[3*j+1]=G*255;
  372.       data[3*j+2]=B*255;
  373.       break;
  374.     }
  375.     gtk_preview_draw_row( GTK_PREVIEW(preview),data,0,i,256);
  376.   }
  377.   gtk_widget_draw(preview,NULL);
  378.   gdk_flush();
  379. }
  380.  
  381.  
  382. void  fp_Create_Nudge(gint  *adjArray)
  383. {
  384.   int left, right, middle,i;
  385.   /* The following function was determined by trial and error */
  386.   double Steepness=pow(1-Current.Alias,4)*.8; 
  387.  
  388.   left = (Current.Range == SHADOWS) ? 0 : Current.Cutoffs[Current.Range-1];
  389.   right = Current.Cutoffs[Current.Range];
  390.   middle = (left + right)/2; 
  391.  
  392.   if (Current.Alias!=0)
  393.     for (i=0; i<256; i++)
  394.       if (i<=middle)
  395.     adjArray[i] = MAX_ROUGHNESS * 
  396.       Current.Rough*(1+tanh(Steepness*(i-left)))/2;
  397.       else
  398.     adjArray[i] = MAX_ROUGHNESS * 
  399.       Current.Rough*(1+tanh(Steepness*(right-i)))/2;
  400.   else
  401.      for (i=0; i<256; i++)
  402.        if (left<=i && i<=right)
  403.      adjArray[i] = MAX_ROUGHNESS * Current.Rough;
  404.        else
  405.      adjArray[i] = 0;
  406. }
  407.  
  408. gint fp_fake_transparency(gint i, gint j)
  409. {
  410.   if ( ((i%20)- 10) * ((j%20)- 10)>0   )
  411.     return 64;
  412.   else 
  413.     return 196;
  414. }
  415.