@name Some HTTP Image Thing
@inputs Screen:wirelink
@outputs 
@persist ImageData:array TmpData:array Pixels:table Parse Display Index X Y W H
@trigger 

if (first())
{
    runOnTick(1)
    runOnHTTP(1)
    
    #Format
    Screen[1048569] = 1
    
    #Clear
    Screen[1048574] = 1
    
    #set the size for now
    W = 111
    H = 111 
        
    #Set resolution
    Screen[1048572] = W
    Screen[1048573] = H 
                    
    httpRequest("http://failcakemidi.uphero.com/testimg")    
}

if (httpClk())
{
    ImageData = httpData():explode("\n")
    TmpData   = ImageData[1, string]:explode(" ")
    print(ImageData)
    Parse = X = Y = 1
}

if (Parse)
{
    while (perf())
    {
        Colors = TmpData[X, string]:explode(",")
        
        R = Colors[1,string]:toNumber()
        G = Colors[2,string]:toNumber()
        B = Colors[3,string]:toNumber()
        
        Pixels:pushTable(table(R,G,B))
        
        X++
        
        if (X > W)
        {
            X = 1            
            Y++
            
            TmpData = ImageData[Y, string]:explode(" ")
                        
            if (Y > H)
            {
                print("Done Parsing")
                
                #free up the memory as we don't need it anymore
                ImageData:clear()
                TmpData:clear()
                
                Display = 1
                Parse   = 0
                break
            }   
        }
    }
}
elseif (Display)
{
    while (perf())
    {
        Pixel             = Pixels[Index+1, table]
        Address           = Index*3
        Screen[Address]   = Pixel[1,number]
        Screen[Address+1] = Pixel[2,number]
        Screen[Address+2] = Pixel[3,number]
                
        Index++
                
        if (Index > W*H)
        {
            print("Done Displaying")
            
            Display = 0
            Index = 0
            break
        }
    }
}
