home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD1.iso / GFX / Converter / DC-PGS21.DMS / in.adf / Extras / DevDocs.LHA / DeveloperDocs / examples / efx / balance.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-03  |  10.0 KB  |  450 lines

  1. /*
  2.     Balance effect.
  3.  
  4.     Copyright © 1995-1996 Almathera Systems Ltd. All Rights Reserved
  5. */
  6.     #include <exec/types.h>
  7.     #include <exec/memory.h>
  8.     #include <dos/dos.h>    
  9.     #include <intuition/intuition.h>
  10.     #include <intuition/gadgetclass.h>
  11.     #include <libraries/gadtools.h>
  12.  
  13.     #include <clib/exec_protos.h>
  14.     #include <clib/dos_protos.h>
  15.     #include <clib/intuition_protos.h>
  16.     #include <clib/gadtools_protos.h>
  17.     #include <clib/graphics_protos.h>
  18.     #include <clib/alib_protos.h>
  19.  
  20.     #include <pragmas/exec_pragmas.h>
  21.     #include <pragmas/dos_pragmas.h>
  22.     #include <pragmas/graphics_pragmas.h>
  23.     #include <pragmas/intuition_pragmas.h>
  24.     #include <pragmas/gadtools_pragmas.h>
  25.  
  26.     #include <photogenics/pgs_protos.h>
  27.     #include <photogenics/pgsrexx_protos.h>
  28.     #include <photogenics/parse.h>
  29.  
  30.     #include <pragmas/pgsrexx_pragmas.h>
  31.     #include <pragmas/pgs_pragmas.h>
  32.  
  33.     #include <photogenics/gio.h>
  34.     #include <photogenics/efx.h>
  35.  
  36.     #include <stdio.h>
  37.     #include <stdlib.h>
  38.     #include <string.h>
  39.     #include <math.h>
  40.  
  41. #define DOSBase giodata->DOSBase
  42. #define PgsBase giodata->PgsBase
  43. #define GfxBase giodata->GfxBase
  44. #define IntuitionBase giodata->IntuitionBase
  45. #define GadToolsBase giodata->GadToolsBase
  46. #define PgsrexxBase giodata->PgsrexxBase
  47.  
  48. __asm ULONG EfxInfo(void)
  49. {
  50.     return(EFX_24BIT|EFX_NOOPTIONS);
  51. }
  52.  
  53. WORD rval = 0;
  54. WORD gval = 0;
  55. WORD bval = 0;
  56.  
  57. void handlemessages(struct GIOData *,struct Window *);
  58. void preview_balance(struct GIOData *,struct Window *);
  59. void apply_balance(struct GIOData *giodata);
  60.  
  61. struct NewGadget ng;
  62. struct VisualInfo *vi;
  63.  
  64. UBYTE *preview;
  65. UBYTE *preview2;
  66.  
  67. struct Gadget *rnumgad,*bnumgad,*gnumgad,*rslidergad,*gslidergad,*bslidergad;
  68.  
  69. LONG pointers[] = {0,0,0};
  70.  
  71. __asm ULONG EfxRender(register __a0 struct GIOData *giodata)
  72. {
  73. struct Window *win;
  74. struct Screen *scn;
  75. struct Gadget *glist,*gad;
  76.  
  77.     giodata->Error = GIO_OK;
  78.  
  79.     preview = 0;
  80.     preview2 = 0;
  81.  
  82.     if((giodata->ArexxString) && (PgsrexxBase))
  83.     {
  84.             LONG array[] = {3, (LONG) pointers,
  85.                             PARSETYPE_INT,-255,255,
  86.                             PARSETYPE_INT,-255,255,
  87.                             PARSETYPE_INT,-255,255};
  88.         
  89.             if(giodata->ArexxError = ParseTemplate(giodata->ArexxString,array))
  90.             {
  91.                 giodata->Error = GIO_AREXXERROR;
  92.                 return(giodata->Error);
  93.             }
  94.  
  95.             rval=pointers[0];
  96.             gval=pointers[1];
  97.             bval=pointers[2];
  98.  
  99.             apply_balance(giodata);
  100.  
  101.     } else {
  102.  
  103.     preview = CreatePreviewImage(giodata,64,64,0);
  104.     if(!preview) goto err;
  105.  
  106.     preview2 = AllocVec(64*64*3,MEMF_CLEAR|MEMF_PUBLIC);
  107.     
  108.     memcpy(preview2,preview,64*64*3);
  109.     
  110.     scn = LockPubScreen("photogenics");
  111.  
  112.     if(!scn) 
  113.     {
  114.         Error("Unable to lock public screen");
  115.         return(giodata->Error = GIO_ABORTED);
  116.     }
  117.  
  118.     gad = CreateContext(&glist);
  119.  
  120.     vi = GetVisualInfo(scn,TAG_END);
  121.  
  122.     if(!vi) return(giodata->Error = GIO_SYSERR);
  123.  
  124.     win = OpenWindowTags(0,
  125.                 WA_Left,            50,
  126.                 WA_Top,                50,
  127.                 WA_InnerWidth,        210+(scn->RastPort.TxWidth*13),
  128.                 WA_InnerHeight,        66+(scn->Font->ta_YSize*3),
  129.                 WA_CloseGadget,        TRUE,
  130.                 WA_DepthGadget,        TRUE,
  131.                 WA_DragBar,            TRUE,
  132.                 WA_PubScreen,        scn,
  133.                 WA_ReportMouse,        -1,
  134.  
  135.                 WA_SimpleRefresh,    TRUE,
  136.                 WA_Activate,        TRUE,
  137.                 WA_Title,            "Colour Balance",
  138.                 WA_IDCMP,            IDCMP_MOUSEMOVE|IDCMP_REFRESHWINDOW|IDCMP_CLOSEWINDOW|
  139.                                     SLIDERIDCMP|BUTTONIDCMP|STRINGIDCMP,
  140.                 0);
  141.  
  142.  
  143.     ng.ng_LeftEdge = 2+win->BorderLeft;
  144.     ng.ng_TopEdge = win->Height-win->BorderBottom-scn->Font->ta_YSize-8;
  145.     ng.ng_Width = 80;
  146.     ng.ng_Height = scn->Font->ta_YSize+6;
  147.     ng.ng_GadgetText = "Accept";
  148.     ng.ng_TextAttr = scn->Font;
  149.     ng.ng_GadgetID = 0;
  150.     ng.ng_Flags = 0;
  151.     ng.ng_VisualInfo = vi;
  152.  
  153.     gad = CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);
  154.  
  155.     ng.ng_LeftEdge = win->Width-win->BorderRight-82;
  156.     ng.ng_GadgetText = "Cancel";
  157.     ng.ng_GadgetID = 1;
  158.  
  159.     gad = CreateGadget(BUTTON_KIND,gad,&ng,TAG_END);
  160.  
  161.  
  162.     ng.ng_TopEdge = win->BorderTop+6+(scn->Font->ta_YSize);
  163.     ng.ng_LeftEdge = 2+win->BorderLeft+(scn->RastPort.TxWidth*7);
  164.     ng.ng_GadgetID = 3;
  165.     ng.ng_Width = 120;
  166.     ng.ng_Height = scn->Font->ta_YSize + 6;
  167.     ng.ng_GadgetText = "Red:";
  168.     
  169.     rslidergad = CreateGadget(SLIDER_KIND,gad,&ng,GTSL_Min,-255,GTSL_Max,255,GTSL_Level,rval,
  170.                                          GA_Disabled,((giodata->Channels & 1)==0),
  171.                                         0);
  172.  
  173.     ng.ng_TopEdge = win->BorderTop+6+(scn->Font->ta_YSize*3);
  174.     ng.ng_GadgetID = 4;
  175.     ng.ng_GadgetText = "Green:";
  176.     
  177.     gslidergad = CreateGadget(SLIDER_KIND,rslidergad,&ng,GTSL_Min,-255,GTSL_Max,255,GTSL_Level,gval,
  178.                                          GA_Disabled,((giodata->Channels & 2)==0),    
  179.                                         0);
  180.  
  181.     ng.ng_TopEdge = win->BorderTop+6+(scn->Font->ta_YSize*5);
  182.     ng.ng_GadgetID = 5;
  183.     ng.ng_GadgetText = "Blue:";
  184.     
  185.     bslidergad = CreateGadget(SLIDER_KIND,gslidergad,&ng,GTSL_Min,-255,GTSL_Max,255,GTSL_Level,bval,
  186.                                          GA_Disabled,((giodata->Channels & 4)==0),
  187.                                         0);
  188.  
  189.  
  190.     ng.ng_TopEdge = win->BorderTop+6+(scn->Font->ta_YSize);
  191.     ng.ng_LeftEdge = 126+win->BorderLeft+(scn->RastPort.TxWidth*7);
  192.     ng.ng_GadgetID = 6;
  193.     ng.ng_Width = (scn->RastPort.TxWidth*6);
  194.  
  195.     ng.ng_GadgetText = "";
  196.  
  197.     rnumgad = CreateGadget(INTEGER_KIND,bslidergad,&ng,GTIN_Number,rval,GTIN_MaxChars,4,
  198.                                          GA_Disabled,((giodata->Channels & 1)==0),
  199.                                         0);
  200.  
  201.  
  202.     ng.ng_TopEdge = win->BorderTop+6+(scn->Font->ta_YSize*3);
  203.     ng.ng_GadgetID = 7;
  204.     gnumgad = CreateGadget(INTEGER_KIND,rnumgad,&ng,GTIN_Number,gval,GTIN_MaxChars,4,
  205.                                          GA_Disabled,((giodata->Channels & 2)==0),
  206.                                         0);
  207.  
  208.     ng.ng_TopEdge = win->BorderTop+6+(scn->Font->ta_YSize*5);
  209.     ng.ng_GadgetID = 8;
  210.     bnumgad = CreateGadget(INTEGER_KIND,gnumgad,&ng,GTIN_Number,bval,GTIN_MaxChars,4,
  211.                                          GA_Disabled,((giodata->Channels & 4)==0),
  212.                                         0);
  213.  
  214.     AddGList(win,glist,0,-1,0);
  215.     RefreshGList(glist,win,0,-1);
  216.     GT_RefreshWindow(win,NULL);
  217.  
  218.     DrawBevelBox(win->RPort,134+win->BorderLeft+(scn->RastPort.TxWidth*13),
  219.                     win->BorderTop+6,68,66,GT_VisualInfo,vi);
  220.     
  221.     preview_balance(giodata,win);
  222.  
  223.     UnlockPubScreen(0,scn);
  224.  
  225.     if(!win)
  226.     {
  227.         Error("Unable to open histogram window!");
  228.         return(giodata->Error = GIO_ABORTED);
  229.     }
  230.  
  231.     handlemessages(giodata,win);
  232.  
  233.     CloseWindow(win);
  234.  
  235.     if(giodata->Error == GIO_OK) apply_balance(giodata);
  236.  
  237.     FreeGadgets(glist);
  238.  
  239.     FreeVisualInfo(vi);
  240.     
  241.     }
  242.  
  243. err:
  244.  
  245.     if(preview)    FreePreviewImage(giodata);
  246.     if(preview2) FreeVec(preview2);
  247.  
  248.     return(giodata->Error);
  249. }
  250.  
  251. __asm ULONG EfxPreRender(register __a0 struct GIOData *giodata)
  252. {
  253.     return(0);
  254. }
  255.  
  256. __asm ULONG EfxPrefs(register __a0 struct GIOData *giodata)
  257. {
  258.     return(0);
  259. }
  260.  
  261. void handlemessages(struct GIOData *giodata, struct Window *win)
  262. {
  263. struct IntuiMessage *msg;
  264. struct Gadget *gad;
  265. BOOL flag = TRUE;
  266. BOOL repreview = FALSE;
  267.  
  268.     while(flag)
  269.     {
  270.         WaitPort(win->UserPort);
  271.         while(msg = GT_GetIMsg(win->UserPort))
  272.         {
  273.             switch(msg->Class)
  274.             {
  275.                 case IDCMP_GADGETUP:
  276.                     gad = (struct Gadget *) msg->IAddress;
  277.                     switch(gad->GadgetID)
  278.                     {
  279.                         case 0:     // Accept
  280.                             flag = FALSE;
  281.                             break;
  282.                         case 1:        // cancel
  283.                         
  284.                             flag = FALSE;
  285.                             giodata->Error = GIO_ABORTED;
  286.                             break;
  287.  
  288.                         case 3:
  289.                             rval = msg->Code;
  290.                             repreview= TRUE;
  291.                             GT_SetGadgetAttrs(rnumgad,win,0,GTIN_Number,(WORD) msg->Code,TAG_DONE);
  292.                             break;
  293.                         case 4:
  294.                             gval = msg->Code;
  295.                             repreview= TRUE;
  296.                             GT_SetGadgetAttrs(gnumgad,win,0,GTIN_Number,(WORD) msg->Code,TAG_DONE);
  297.                             break;
  298.                         case 5:
  299.                             bval = msg->Code;
  300.                             repreview= TRUE;
  301.                             GT_SetGadgetAttrs(bnumgad,win,0,GTIN_Number,(WORD) msg->Code,TAG_DONE);
  302.                             break;
  303.  
  304.                         case 6:
  305.                             rval = ((struct StringInfo *) gad->SpecialInfo)->LongInt;
  306.                             repreview= TRUE;
  307.                             GT_SetGadgetAttrs(rslidergad,win,0,GTSL_Level,rval,TAG_DONE);
  308.                             break;
  309.                         case 7:
  310.                             gval = ((struct StringInfo *) gad->SpecialInfo)->LongInt;
  311.                             repreview= TRUE;
  312.                             GT_SetGadgetAttrs(gslidergad,win,0,GTSL_Level,gval,TAG_DONE);
  313.                             break;
  314.                         case 8:
  315.                             bval = ((struct StringInfo *) gad->SpecialInfo)->LongInt;
  316.                             repreview= TRUE;
  317.                             GT_SetGadgetAttrs(bslidergad,win,0,GTSL_Level,bval,TAG_DONE);
  318.                             break;
  319.                     }
  320.                     break;
  321.                 case IDCMP_CLOSEWINDOW:
  322.                     flag = FALSE;
  323.                     giodata->Error = GIO_ABORTED;
  324.                     break;
  325.  
  326.  
  327.                 case IDCMP_MOUSEMOVE:
  328.                     gad = (struct Gadget *) msg->IAddress;
  329.                     if(gad)
  330.                     {
  331.                         switch(gad->GadgetID) {
  332.                             case 3:
  333.                                 rval = msg->Code;
  334.                                 repreview= TRUE;
  335.                                 GT_SetGadgetAttrs(rnumgad,win,0,GTIN_Number,(WORD) msg->Code,TAG_DONE);
  336.                                 break;
  337.                             case 4:
  338.                                 gval = msg->Code;
  339.                                 repreview= TRUE;
  340.                                 GT_SetGadgetAttrs(gnumgad,win,0,GTIN_Number,(WORD) msg->Code,TAG_DONE);
  341.                                 break;
  342.                             case 5:
  343.                                 bval = msg->Code;
  344.                                 repreview= TRUE;
  345.                                 GT_SetGadgetAttrs(bnumgad,win,0,GTIN_Number,(WORD) msg->Code,TAG_DONE);
  346.                                 break;
  347.                         }
  348.                     }
  349.                     break;
  350.  
  351.                 case IDCMP_REFRESHWINDOW:
  352.                     GT_BeginRefresh(win);
  353.                     GT_EndRefresh(win,TRUE);
  354.                     break;
  355.             }
  356.             GT_ReplyIMsg(msg);
  357.  
  358.         }
  359.         if(repreview) {preview_balance(giodata,win); repreview = FALSE;}
  360.         
  361.     }
  362. }
  363.  
  364.  
  365. void apply_balance(struct GIOData *giodata)
  366. {
  367. UBYTE r,g,b;
  368. int x,y;
  369. int r2,g2,b2;
  370.  
  371.     SetProgress("Changing colour balance..",0);
  372.  
  373.     for(y=giodata->WindY;y<giodata->WindY+giodata->WindHeight;y++)
  374.     {
  375.  
  376.         if(!(y%16)) SetProgress(0,(y-giodata->WindY)*100/giodata->WindHeight);
  377.  
  378.  
  379.         for(x=giodata->WindX;x<giodata->WindX+giodata->WindWidth;x++)
  380.         {
  381.             GetSrcPixel(giodata,x,y,&r,&g,&b);
  382.  
  383.             if(rval)    
  384.             {
  385.                 r2 = r+rval;
  386.                 r2 = min(max(r2,0),255);
  387.             } else r2 = r;
  388.  
  389.             if(gval)
  390.             {
  391.                 g2 = g+gval;
  392.                 g2 = min(max(g2,0),255);
  393.             } else g2 = g;
  394.  
  395.             if(bval)
  396.             {
  397.                 b2 = b+bval;
  398.                 b2 = min(max(b2,0),255);
  399.             } else b2 = b;
  400.  
  401.             PutDestPixel(giodata,x,y,r2,g2,b2);
  402.         }
  403.     }
  404. }
  405.  
  406.  
  407.  
  408. void preview_balance(struct GIOData *giodata,struct Window *win)
  409. {
  410. UBYTE r,g,b;
  411. int x,y;
  412. int r2,g2,b2;
  413.  
  414.         for(y=0;y<64;y++)
  415.         {
  416.  
  417.             for(x=0;x<64;x++)
  418.             {
  419.                 
  420.                 r = preview[x*3+(y*192)];
  421.                 g = preview[x*3+(y*192)+1];
  422.                 b = preview[x*3+(y*192)+2];
  423.  
  424.                 if(rval)    
  425.                 {    r2 = r+rval;
  426.                     r2 = min(max(r2,0),255);
  427.                 } else r2 = r;
  428.  
  429.                 if(gval)
  430.                 {
  431.                     g2 = g+gval;
  432.                     g2 = min(max(g2,0),255);
  433.                 } else g2 = g;
  434.  
  435.                 if(bval)
  436.                 {
  437.                     b2 = b+bval;
  438.                     b2 = min(max(b2,0),255);
  439.                 } else b2 = b;
  440.  
  441.                 preview2[x*3+(y*192)] = r2;
  442.                 preview2[x*3+(y*192)+1] = g2;
  443.                 preview2[x*3+(y*192)+2] = b2;
  444.             }
  445.         }
  446.  
  447.         DrawPreviewImage(giodata,win->RPort,preview2,136+win->BorderLeft+(win->WScreen->RastPort.TxWidth*13),
  448.                         win->BorderTop+7,64,64);
  449. }
  450.