home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / software / utilities / pcd-dt35 / source / dispatcher.c next >
C/C++ Source or Header  |  2000-01-22  |  5KB  |  247 lines

  1. #include <clib/alib_protos.h>
  2. #include <pragma/datatypes_lib.h>
  3. #include <pragma/dos_lib.h>
  4. #include <pragma/exec_lib.h>
  5. #include <pragma/intuition_lib.h>
  6. #include <pragma/graphics_lib.h>
  7. #include <pragma/utility_lib.h>
  8. #include <datatypes/pictureclass.h>
  9. #include <dos/dostags.h>
  10. #include <exec/memory.h>
  11. #include <intuition/icclass.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14.  
  15. struct Arg1
  16. {
  17. long *res;
  18. };
  19.  
  20. struct Arg2
  21. {
  22. long res;
  23. };
  24.  
  25. static void ReadPrefs(Arg2 *arg)
  26. {
  27. char *var;
  28. arg->res=2;
  29. if(var=(char *)AllocVec(256,0))
  30.     {
  31.     if(GetVar("ENV:DataTypes/pcd.prefs",var,256,LV_VAR|GVF_GLOBAL_ONLY)>=0)
  32.         {
  33.         RDArgs *rdargs;
  34.       if(rdargs=(RDArgs *)AllocDosObject(DOS_RDARGS,0))
  35.             {
  36.             RDArgs *args;
  37.             Arg1 para={0};
  38.             rdargs->RDA_Source.CS_Buffer=var;
  39.             rdargs->RDA_Source.CS_Length=strlen(var);
  40.             rdargs->RDA_Source.CS_CurChr=0;
  41.             if(args=ReadArgs("RESOLUTION/A/N",(long *)¶,rdargs))
  42.                 {
  43.                 if(para.res)
  44.                     {
  45.                     arg->res=*para.res;
  46.                     if(arg->res<0) arg->res=0;
  47.                     else if(arg->res>5) arg->res=5;
  48.                     }
  49.                 FreeArgs(args);
  50.                 }
  51.             FreeDosObject(DOS_RDARGS,rdargs);
  52.             }
  53.         }
  54.     FreeVec(var);
  55.     }
  56. }
  57.  
  58. ULONG Colors2DT(Object *obj,UBYTE *color)
  59. {
  60. UBYTE *cr;
  61. ULONG *cregs,i,num,error=0;
  62. GetDTAttrs(obj,PDTA_NumColors,&num,PDTA_ColorRegisters,&cr,PDTA_CRegs,&cregs,TAG_END);
  63. if(cr&&cregs)
  64.     {
  65.     for(i=0;i<num;i++,cr+=3,cregs+=3,color+=4)
  66.         {
  67.         cr[0]=color[1];
  68.         cr[1]=color[2];
  69.         cr[2]=color[3];
  70.         cregs[0]=cr[0]<<24;
  71.         cregs[1]=cr[1]<<24;
  72.         cregs[2]=cr[2]<<24;
  73.         }
  74.     }
  75. else error=ERROR_NO_FREE_STORE;
  76. return error;
  77. }
  78.  
  79. static ULONG ReadP6_V43(Object *obj,BitMapHeader *bh,BPTR file)
  80. {
  81. ULONG error=0,len=bh->bmh_Width*bh->bmh_Height*3;
  82. UBYTE *pix;
  83. if(pix=(UBYTE *)AllocVec(len,0))
  84.     {
  85.     if(Read(file,pix,len)==len) DoMethod(obj,PDTM_WRITEPIXELARRAY,pix,PBPAFMT_RGB,bh->bmh_Width*3,0,0,bh->bmh_Width,bh->bmh_Height);
  86.     else error=DTERROR_NOT_ENOUGH_DATA;
  87.     FreeVec(pix);
  88.     }
  89. else if(pix=(UBYTE *)AllocVec(bh->bmh_Width*3,0))
  90.     {
  91.     UWORD y;
  92.     for(y=0;y<bh->bmh_Height;y++)
  93.         {
  94.         if(Read(file,pix,bh->bmh_Width*3)!=bh->bmh_Width*3)
  95.             {
  96.             error=DTERROR_NOT_ENOUGH_DATA;
  97.             break;
  98.             }
  99.         DoMethod(obj,PDTM_WRITEPIXELARRAY,pix,PBPAFMT_RGB,bh->bmh_Width*3,0,y,bh->bmh_Width,1);
  100.         }
  101.     FreeVec(pix);
  102.     }
  103. else error=ERROR_NO_FREE_STORE;
  104. return error;
  105. }
  106.  
  107. static ULONG ReadNum(BPTR file)
  108. {
  109. ULONG retval=0;
  110. for(;;)
  111.     {
  112.     char c;
  113.     if(Read(file,&c,1)!=1) return 0;
  114.     if(c==10||c==32) break;
  115.     retval*=10;
  116.     retval+=c-'0';
  117.     }
  118. return retval;
  119. }
  120.  
  121. static ULONG GetPicture(IClass *cl,Object *obj)
  122. {
  123. ULONG error=0;
  124. BPTR nil;
  125. if(nil=Open("NIL:",MODE_OLDFILE))
  126.     {
  127.     char *fname,*dname,tmp[L_tmpnam];
  128.     Arg2 arg;
  129.     ReadPrefs(&arg);
  130.     GetDTAttrs(obj,DTA_Name,&dname,TAG_END);
  131.     if(dname)
  132.         {
  133.         SetDTAttrs(obj,0,0,DTA_ObjName,dname,TAG_END);
  134.         tmpnam(tmp);
  135.         strncpy(tmp,"PIPE:",5);
  136.         if(fname=(char *)AllocVec(strlen(dname)+L_tmpnam+50,0))
  137.             {
  138.             SPrintf(fname,"run pcdtoppm >%s -%lu -ppm \"%s%\"",tmp,arg.res+1,dname);
  139.             if(Execute(fname,nil,nil)!=DOSFALSE)
  140.                 {
  141.                 BPTR file;
  142.                 if(file=Open(tmp,MODE_OLDFILE))
  143.                     {
  144.                     BitMapHeader *bh;
  145.                     char head[3];
  146.                     GetDTAttrs(obj,PDTA_BitMapHeader,&bh,TAG_END);
  147.                     if(bh)
  148.                         {
  149.                         if(Read(file,head,3)==3)
  150.                             {
  151.                             bh->bmh_Width=ReadNum(file);
  152.                             bh->bmh_PageWidth=bh->bmh_Width;
  153.                             bh->bmh_Height=ReadNum(file);
  154.                             bh->bmh_PageHeight=bh->bmh_Height;
  155.                             bh->bmh_Compression=1;
  156.                             ReadNum(file);
  157.                             bh->bmh_Depth=24;
  158.                             SetDTAttrs(obj,0,0,DTA_ErrorNumber,&error,DTA_NominalHoriz,bh->bmh_Width,DTA_NominalVert,bh->bmh_Height,PDTA_ModeID,0,PDTA_SourceMode,PMODE_V43,TAG_END);
  159.                             if(!error) error=ReadP6_V43(obj,bh,file);
  160.                             }
  161.                         else error=DTERROR_NOT_ENOUGH_DATA;
  162.                         }
  163.                     else error=ERROR_NO_FREE_STORE;
  164.                     Close(file);
  165.                     }
  166.                 else error=IoErr();
  167.                 }
  168.             else error=IoErr();
  169.             FreeVec(fname);
  170.             }
  171.         else error=ERROR_NO_FREE_STORE;
  172.         }
  173.     else error=ERROR_NO_FREE_STORE;
  174.     Close(nil);
  175.     }
  176. else error=IoErr();
  177. return error;
  178. }
  179.  
  180. static ULONG mNew(IClass *cl,Object *obj,opSet *msg)
  181. {
  182. TagItem *ti;
  183. if(ti=FindTagItem(DTA_SourceType,msg->ops_AttrList))
  184.     {
  185.     if(ti->ti_Data!=DTST_FILE&&ti->ti_Data!=DTST_CLIPBOARD&&ti->ti_Data!=DTST_RAM)
  186.         {
  187.         SetIoErr(ERROR_OBJECT_WRONG_TYPE);
  188.         return 0;
  189.         }
  190.     }
  191. if(obj==(Object *)cl)
  192.     {
  193.     if(obj=(Object *)DoSuperMethodA(cl,obj,Msg(msg)))
  194.         {
  195.         ULONG error;
  196.         if(error=GetPicture(cl,obj))
  197.             {
  198.             SetIoErr(error);
  199.             CoerceMethod(cl,obj,OM_DISPOSE);
  200.             obj=0;
  201.             }
  202.         }
  203.     }
  204. else
  205.     {
  206.     SetIoErr(ERROR_NOT_IMPLEMENTED);
  207.     obj=0;
  208.     }
  209. return ULONG(obj);
  210. }
  211.  
  212. static ULONG mSet(IClass *cl,Object *obj,opSet *msg)
  213. {
  214. ULONG retval;
  215. if(retval=DoSuperMethodA(cl,obj,Msg(msg)))
  216.     {
  217.     RastPort *rp;
  218.     if(rp=ObtainGIRPort(msg->ops_GInfo))
  219.         {
  220.         DoMethod(obj,GM_RENDER,msg->ops_GInfo,rp,GREDRAW_UPDATE);
  221.         ReleaseGIRPort(rp);
  222.         retval=0;
  223.         }
  224.     }
  225. return retval;
  226. }
  227.  
  228. extern "C" ULONG Dispatcher(register __a0 IClass *cl,register __a2 Object *obj,register __a1 Msg msg)
  229. {
  230. ULONG retval;
  231. switch(msg->MethodID)
  232.     {
  233.     case OM_NEW:
  234.         retval=mNew(cl,obj,(opSet *)msg);
  235.         break;
  236.     case OM_UPDATE:
  237.         if(DoMethod(obj,ICM_CHECKLOOP)) break;
  238.     case OM_SET:
  239.         retval=mSet(cl,obj,(opSet *)msg);
  240.         break;
  241.     default:
  242.         retval=DoSuperMethodA(cl,obj,msg);
  243.         }
  244. return retval;
  245. }
  246.  
  247.