home *** CD-ROM | disk | FTP | other *** search
/ Global Amiga Experience / globalamigaexperience.iso / compressed / graphic / arteffectdemodisk1.dms / arteffectdemodisk1.adf / Developer / mosaic.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  7KB  |  260 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <strings.h>
  4.  
  5. #include <exec/types.h>
  6. #include <exec/memory.h>
  7. #include <dos/dos.h>
  8. #include <dos/rdargs.h>
  9. #include <egs/egsutil.h>
  10. #include <egs/ownall.h>
  11.  
  12. #include <proto/exec.h>
  13. #include <proto/dos.h>
  14. #include <egs/proto/egsutil.h>
  15. #include "ArtEffect:Developer/picotalk.h"
  16.  
  17. const char ver[] = {"$VER: Mosaic 1.1 (21.01.95)"};
  18.  
  19. struct Library *EGBBase;
  20. struct Library *EGSUtilBase;
  21. struct Library *EGSIntuiBase;
  22.  
  23. struct EI_Window *window;
  24. struct MsgPort *myport;
  25.  
  26. enum gadgets { GAD_WSIZE=100, GAD_HSIZE, GAD_START };
  27.  
  28. BOOL SendMsg(struct Message *msg)
  29. {
  30.     struct MsgPort *port;
  31.     BOOL success;
  32.  
  33.     msg->mn_Node.ln_Type = NT_MESSAGE;
  34.     msg->mn_ReplyPort = myport;
  35.     msg->mn_Length = sizeof(struct PicoMsg);
  36.  
  37.     Forbid();
  38.     if (port = FindPort("PicoTalk")){
  39.         PutMsg(port, msg);
  40.         WaitPort(myport);
  41.         msg=GetMsg(myport);
  42.         success = TRUE;
  43.     } else
  44.         success = FALSE;
  45.  
  46.     Permit();
  47.     return success;
  48. }
  49.  
  50. int ExecuteOp(struct PicoMsg *msg, int wval, int hval)
  51. {
  52.     struct E_EBitMap *bitmap;
  53.     ULONG winid;
  54.  
  55.     msg->command = PTALK_OBTAIN_CURRENT;
  56.     msg->arg.winid = 0;
  57.     msg->res.alloc.bitmap = NULL;
  58.     SendMsg((struct Message *)msg);
  59.     winid = msg->res.alloc.winid;
  60.     bitmap = msg->res.alloc.bitmap;
  61.     if (bitmap){
  62.         int x,y,sx,sy;
  63.         int xval,yval;
  64.  
  65.         for (y=0; y<bitmap->Height; y+=hval){
  66.             for (x=0; x<bitmap->Width; x+=wval){
  67.                 ULONG red,green,blue;
  68.                 UBYTE *data;
  69.  
  70.                 red=green=blue=0;
  71.                 xval = ((x+wval)<bitmap->Width) ? wval : (bitmap->Width-x);
  72.                 yval = ((y+hval)<bitmap->Height) ? hval : (bitmap->Height-y);
  73.  
  74.                 // evaluate color
  75.                 for (sy=0; sy<yval; sy++){
  76.                     data = bitmap->Typekey.PixelMap.Planes.Dest;
  77.                     data += x*4 + (y+sy)*bitmap->BytesPerRow;
  78.  
  79.                     for (sx=0; sx<xval; sx++){
  80.                         red += *data++;
  81.                         green += *data++;
  82.                         blue += *data;
  83.                         data += 2;
  84.                     }
  85.                 }
  86.                 red /= xval*yval; green /= xval*yval; blue /= xval*yval;
  87.  
  88.                 // write color;
  89.                 for (sy=0; sy<yval; sy++){
  90.                     data = bitmap->Typekey.PixelMap.Planes.Dest;
  91.                     data += x*4 + (y+sy)*bitmap->BytesPerRow;
  92.  
  93.                     for (sx=0; sx<xval; sx++){
  94.                         *data++ = red;
  95.                         *data++ = green;
  96.                         *data = blue;
  97.                         // skip alpha channel: alpha channel is also used for your calculations
  98.                         // you may also change the alpha channel, and afterwards your result will be mixed accordingly.
  99.                         // you define the visibility 255: your value, 0: old value
  100.                         data += 2;
  101.                     }
  102.                 }                                    
  103.             }
  104.             // allways has to be used (does the automatic alpha channel support)
  105.             msg->command = PTALK_UPDATE_WINDOW;
  106.             msg->arg.update.winid = winid;
  107.             msg->arg.update.left = 0;
  108.             msg->arg.update.top = y;
  109.             msg->arg.update.width = bitmap->Width;
  110.             msg->arg.update.height = yval;
  111.             SendMsg((struct Message *)msg);
  112.         }
  113.         msg->command = PTALK_RELEASE_WINDOW;
  114.         msg->arg.winid = winid;
  115.         SendMsg((struct Message *)msg);
  116.  
  117.         return 0;
  118.     } else
  119.         return -30;
  120. }
  121.  
  122. main(int argc, char **argv)
  123. {
  124.     struct PicoMsg *msg;
  125.     EB_GadContext con;
  126.     struct EI_Screen *screen;
  127.     ULONG appid;
  128.     ULONG sigbits;
  129.     BOOL loop=TRUE;
  130.  
  131.     EGBBase = OpenLibrary("egsgadbox.library",7);
  132.     EGSUtilBase = OpenLibrary("egsutil.library",7);
  133.     EGSIntuiBase = OpenLibrary("egsintui.library",7);
  134.     myport=CreateMsgPort();
  135.     msg=AllocVec(sizeof(struct PicoMsg), MEMF_PUBLIC|MEMF_CLEAR);
  136.  
  137.     if (msg && myport && EGSIntuiBase && EGSUtilBase && EGBBase){
  138.         msg->command = PTALK_NOTIFY;
  139.         msg->arg.notify.ApplicationPort = myport;
  140.         msg->arg.notify.ApplicationName = "MOSAIC";
  141.         if (SendMsg((struct Message *)msg)){
  142.             appid = msg->res.notify.ApplicationId;
  143.             screen = msg->res.notify.screen;
  144.  
  145.             if (con = EB_CreateGadContext(NULL,NULL,-1,-1)){
  146.                 if (!ET_CreateWindow(con,NULL,
  147.                     ET_CreateVertiBox(con,
  148.                         ET_CreateVertiTable(con,
  149.                             ET_CreateHorizBox(con,
  150.                                 EB_CreateText(con,"Element-Width"),
  151.                                 EB_NewMinWidth(ET_CreateValueGad(con,1,256,GAD_WSIZE,TAG_DONE),128),
  152.                                 ET_DONE),
  153.                             ET_CreateHorizBox(con,
  154.                                 EB_CreateText(con,"Element-Height"),
  155.                                 EB_NewMinWidth(ET_CreateValueGad(con,1,256,GAD_HSIZE,TAG_DONE),128),
  156.                                 ET_DONE),
  157.                             ET_DONE),
  158.                         EB_CreateVertiFill(con,EB_FILL_ALL,-1),
  159.                         EB_CreateTextAction(con, "_Modify Picture", GAD_START, EB_FILL_ALL),
  160.                         ET_DONE),
  161.                     ETW_SysGadgets, EI_WINDOWCLOSE|EI_WINDOWFRONT|EI_WINDOWDRAG,
  162.                     ETW_Title, "Mosaic",
  163.                     ETW_Flags, EI_WINDOWACTIVE|EI_SIZEBRIGHT|EI_SMART_REFRESH|EI_WINDOWCENTER,
  164.                     ETW_IDCMP, EI_iGADGETDOWN|EI_iCLOSEWINDOW,
  165.                     ETW_AutoResize, TRUE,
  166.                     ETW_PubScreen, screen,
  167.                     TAG_DONE)){
  168.  
  169.                     int wval,hval;
  170.  
  171.                     window = EB_OpenGadWindow(con);
  172.                     ET_ModifyValueGad(window,(struct ET_ValueGad *)EB_GCFindGadget(con,GAD_WSIZE),2);
  173.                     ET_ModifyValueGad(window,(struct ET_ValueGad *)EB_GCFindGadget(con,GAD_HSIZE),2);
  174.                     wval = hval = 2;
  175.  
  176.                     sigbits = (1<<window->UserPort->mp_SigBit)|(1<<myport->mp_SigBit);
  177.                     do {
  178.                         union {
  179.                             struct Message *msg;
  180.                             struct EI_EIntuiMsg *intui;
  181.                         } execmsg;
  182.  
  183.                         Wait(sigbits);
  184.  
  185.                         { struct PicoMsg *msg2;
  186.                         while (msg2 = (struct PicoMsg *)GetMsg(myport)){
  187.                             switch (msg2->command){
  188.                             case PTALK_APPLICATION_QUIT:
  189.                                 loop=FALSE;
  190.                                 break;
  191.  
  192.                             case PTALK_APPLICATION_CMD:
  193.                                 {
  194.                                 struct RDArgs *rdargs;
  195.                                 long *size[2];
  196.  
  197.                                 // parse rexx command
  198.                                 if (rdargs = AllocDosObject(DOS_RDARGS, NULL)){
  199.                                     rdargs->RDA_Source.CS_Buffer = msg2->arg.cmd.CommandString;
  200.                                     rdargs->RDA_Source.CS_Length = strlen(msg2->arg.cmd.CommandString);
  201.                                     rdargs->RDA_Source.CS_CurChr = 0;
  202.                                     rdargs->RDA_DAList = NULL;
  203.                                     rdargs->RDA_Buffer = NULL;
  204.                                     rdargs->RDA_Flags = RDAF_NOPROMPT;
  205.  
  206.                                     if (ReadArgs("XSIZE/N/A,YSIZE/N/A", (long *)&size, rdargs)){
  207.                                         if ((*size[0] >= 1) && (*size[0] <= 256) && (*size[1] >= 1) && (*size[1] <= 256))
  208.                                             msg2->res.cmd.error = ExecuteOp(msg, *size[0], *size[1]);
  209.                                         else
  210.                                             msg2->res.cmd.error = -30;
  211.                                     } else
  212.                                         msg2->res.cmd.error = -20;
  213.  
  214.                                     FreeArgs( rdargs );
  215.                                     FreeDosObject(DOS_RDARGS, rdargs);
  216.                                 }
  217.                                 }
  218.                                 break;
  219.                             }
  220.                             ReplyMsg(&msg2->msg);
  221.                         }
  222.                         }
  223.                         while (execmsg.msg = GetMsg(window->UserPort)){
  224.                             switch (execmsg.intui->Class){
  225.                             case EI_iGADGETDOWN:
  226.                                 switch (((struct EI_Gadget *)execmsg.intui->IAddress)->GadgetID){
  227.                                 case GAD_WSIZE:
  228.                                     wval = ((struct ET_ValueGad *)execmsg.intui->IAddress)->Value;
  229.                                     break;
  230.                                 case GAD_HSIZE:
  231.                                     hval = ((struct ET_ValueGad *)execmsg.intui->IAddress)->Value;
  232.                                     break;
  233.                                 case GAD_START:
  234.                                     ExecuteOp(msg, wval,hval);
  235.                                     break;
  236.                                 }
  237.                                 break;
  238.                             case EI_iCLOSEWINDOW:
  239.                                 loop=FALSE;
  240.                                 break;
  241.                             }
  242.                             ReplyMsg(execmsg.msg);
  243.                         }
  244.                     } while (loop);
  245.                     EB_CloseGadWindow(con);
  246.                 }
  247.                 EB_DeleteGadContext(con);
  248.             }
  249.             msg->command = PTALK_LEAVE;
  250.             msg->arg.leave.ApplicationId = appid;
  251.             SendMsg((struct Message *)msg);
  252.         }
  253.     }
  254.     if (msg) FreeVec(msg);
  255.     if (myport) DeleteMsgPort(myport);
  256.     if (EGBBase) CloseLibrary(EGBBase);
  257.     if (EGSUtilBase) CloseLibrary(EGSUtilBase);
  258.     if (EGSIntuiBase) CloseLibrary(EGSIntuiBase);
  259. }
  260.