; Project : Grafik Converter Client
;  
; It`s forbidden to use this software for commercial use!
;
; (c) 2000 Cyborg 

    {* Include sys:coder/preass/Options.p *}

    {* String: Version="$VER: Grafik Converter client (C) CYBORG 2000"*}

    {* Structure sockaddr_in(),sin_len(BYTE),sin_family(BYTE),sin_port(WORD)
                 sin_addr(LONG),sin_zero[8]*}
    
    {* Structure Hostent,H_name(CHAR),h_aliases(APTR),h_addrtype(LONG)
                 h_length(LONG),H_addr_list(CHAR)*}

    {* Structure Scaleoperators(),Type(LONG),XPixel(LONG),YPixel(LONG),Colors(LONG)
                 Aktion1(BYTE),Data1(BYTE),Aktion2(BYTE),Data2(BYTE)
                 Aktion3(BYTE),Data3(BYTE),Aktion4(BYTE),Data4(BYTE)
                 Aktion5(BYTE),Data5(BYTE)*}

    {* IncBlock: Puffer,2048*}

    {* Const AF_INET=2,INADDR_ANY=0
             Sock_STREAM=1
             FIONBIO=$8004667e*}

    {* incVar: Addr,Zeiger,Filelen*}

    {* NewProc=CountString[String][A0]*}

; Effects    

    FlipX=0             ; set data to zero
    FlipY=1             ; set data to zero
    GreyScale=2         ; set data to zero
    Highlight=3         ; needs data 0-255 set
    Negative=4          ; set data to zero
    Random=5            ; needs data 0-255 set
    RIOP=6              ; set data to zero
    RollX=7             ; needs data 0-127 set
    RollY=8             ; needs data 0-127 set
    Pixelise=9          ; needs data 0-255 set
    Shadow=10           ; needs data 0-255 set
    
; Convoles 

    BlurHigh=128
    BlurLow=129
    Chisel=130
    Cross=131
    Diagonal=132
    EdgeDetect=133
    EmbossHigh=134
    EmbossLow=135
    Focus=136
    FrostedGlass=137
    RaiseHigh=138
    RaiseLow=139
    Shake=140
    SharpenHigh=141
    SharpenLow=142
    Slash=143
    Smooth=144
    Texture=145

; TCP_Write sends as much as possible data to the socket to fasten up the.
; transfer speed. And you can use it like the write() from Dos.library
    
TCP_Write[d0,a0,d1,d2]:
    {* StackFrame wr,Socket=d0,msg=a0,MsgLen=d1,flags=d2*}
    While (Wr=TCP_Send(Socket,msg,msglen,flags))#msglen
      {
        if wr<1 
         {
           {* UnFramereturn -1*}
         }
        Msg==Msg+Wr
        Msglen==MsgLen-Wr
      }
    {* UnFrameReturn *}

; recv is a timedepended socket-read, this is usefull for nonblocking
; sockets!

recv[d0,d1,d2,d3]:
    {* Stackframe Socket=d0,Zeiger=d1,Laenge=d2,time=d3,Readlen*}
    While Time>0
     {
      Readlen=TCP_recv(Socket,Zeiger,Laenge,0)
      If Readlen>0 breakwhile
      Time==Time-25
      Delay(25)
     }
    {* UnFrameReturn Readlen*}

TCP_Sendfile[a0,d0]:
    {* StackFrame Filehandle=#0,Socket=d0,Name=a0,Puffer,Len,Rl*}
    IF (Puffer=Allocmem(200,#MEMF_Fast))=0
     {
      Printf("Conv hat keinen Speicher mehr\n")
      {* UnframeReturn 0*}
     }
    {* FLUSH *}
    If (Filehandle=Open(Name,Mode_old))=0
     {
      Printf("Conv kann file nicht oeffnen\n")
      Freemem(Puffer,200)
      {* UnframeReturn 0*}
     }
    Seek(Filehandle,0,offset_end)
    Len=Seek(Filehandle,0,offset_begin)
    While Len>0
     {
       Rl=Read(Filehandle,Puffer,200)
       If TCP_Write(Socket,Puffer,rl,0)<1 breakwhile
       LEN==LEN-RL
     }
    Close(Filehandle)
    Freemem(Puffer,200)
    {* UnFrameReturn 0*}

; TCP_Getfile sets the socket to blocking to wait for the converted
; image. Because we don`t know how much time it takes to convert it,
; we have to wait for ever or a CTRL-C is send to the task, which
; causes miami to quit this operation.
; If convd can`t open the converted file for any reason , you get a 
; errormessage back. (42 chars) . Check this and you know if it
; worked.

TCP_Getfile[a0,d0]:
    {* StackFrame Filehandle=#0,Socket=d0,Name=a0,Puffer,Len,Rl*}
    IF (Puffer=Allocmem(200,#MEMF_Fast))=0
     {
      Printf("Conv hat keinen Speicher mehr\n")
      {* UnframeReturn 0*}
     }
    {* FLUSH *}
    If (Filehandle=Open(Name,Mode_new))=0
     {
      Freemem(Puffer,200)
      Printf("Conv kann File nicht oeffnen\n")
      {* UnframeReturn 0*}
     }
    {* FLUSH *}
    TCP_IoCtlSocket(Socket,FIONBIO,>FIONBIONBTags:0,0)
    Rl=TCP_recv(Socket,Puffer,4,0)
    TCP_IoCtlSocket(Socket,FIONBIO,#FIONBIOTags)
    Printf("Receiving data...\n")
    Write(Filehandle,Puffer,rl)
    While RL>0
     {
       Rl=recv(Socket,Puffer,200,500)
       Write(Filehandle,Puffer,rl)
     }
    Close(Filehandle)
    Freemem(Puffer,200)
    {* UnFrameReturn 0*}

Start:
    OH=output()
    Printf("Conv2 gestartet\n")
    If (socket=TCP_Socket(AF_INET,SOCK_STREAM,0))#-1
     {
        If (he=TCP_gethostbyname("localhost"))#0
         {
          Printf("Hostname ok...\n")

; fill out the sockaddr for a TCP-connection to port 24001
; which is the normal operation port of convd 1.01

          #AF_Inet=>sockaddr_in.sin_family
          #24001=>sockaddr_in.sin_port
          Zeiger=.lhostent.H_addr_list(he)
          addr==(Zeiger)
          addr==(Addr)
          addr=>sockaddr_in.sin_addr
          ret=TCP_Connect(Socket,&Sockaddr_in,sizeof(sockaddr_in))
          If ret#-1
           {
             printf("Connected...\n")
             If TCP_IoCtlSocket(Socket,FIONBIO,>FIONBIOTags:1,0)=-1
              {
               TCP_Errno()
              }
; Set a JPEG as destinationformat  
; Scale it to a size of 500 x 600 pixel
; give it 16 M colors at the end!
; Roll it first 30 pixels to the right.
; get a negative
; Smooth it!
; the other 2 actionfields are free (-1).

             #"JPEG"=>Scaleoperators.type
             #500=>Scaleoperators.XPixel
             #600=>Scaleoperators.YPixel
             #257=>Scaleoperators.Colors
             #ROLLX=>Scaleoperators.aktion1
             #30=>Scaleoperators.data1
             #NEGATIVE=>Scaleoperators.Aktion2
             #0=>Scaleoperators.data2
             #SMOOTH=>Scaleoperators.Aktion3
             #0=>Scaleoperators.data3
             #0=>Scaleoperators.data4
             #0=>Scaleoperators.data5
             #-1=>Scaleoperators.Aktion4
             #-1=>Scaleoperators.Aktion5

; Open picturefile "Pics:Clown_sm.gif" 

             If (Filehandle=Open("pics:clown_sm.gif",#Mode_old))#0
              { 

; get size of file

                seek(Filehandle,0,offset_end)
                len=seek(Filehandle,0,offset_begin)
                Close(Filehandle)
                TCP_Write(Socket,&len,4,0)

; tell convd that`s a structure comes

                TCP_Write(Socket,"STAT",4,0)
                TCP_Write(Socket,&Scaleoperators,sizeof(scaleoperators),0)

; send the picture to the demon

                TCP_SendFile("pics:clown_sm.gif",Socket)
                printf("File send...\nWaiting for file\n")

; get the result 

                TCP_GetFile("ram:test",Socket)
                printf("Got file\n")
              }
           }
         
         }
      {* FLUSH *}
      Printf("Conv beendet sich jetzt\n")
      TCP_CloseSocket(Socket)
     }
    {* Return *}


CountString:
        move.l a1,-(Sp)
        move.l a0,a1
.l1:    cmpi.b #$00,(a1)+
        bne .l1
        lea -1(a1),a1
        sub.l a0,a1
        move.l a1,d0
        move.l (sp)+,a1
        RTS

