def main():

    # File handling
    filename = raw_input("What is the filename? ")
    if filename[-11:] != '.compressed':
        raise TypeError("Requires a .compressed file from Compression.py!")
    f = open(filename, 'rb')
    data = pickle.load(f)
    f.close()

    # Additional prep
    filename = filename[:-11]
    tt = 0
    quality = ord(data[0])
    data = data[1:]

    print "Seperating Blocks...",
    t0 = time.clock()
    R, G, B = Unpack_Blocks(data)
    t1 = time.clock()
    tt += (t1 - t0)
    print "took", (t1 - t0), "seconds."

    print "De-Quantizing data...",
    t0 = time.clock()
    R_DQ = DeQuantize(R, quality)
    G_DQ = DeQuantize(G, quality)
    B_DQ = DeQuantize(B, quality)
    t1 = time.clock()
    tt += (t1 - t0)
    print "took", (t1 - t0), "seconds."

    print "Undoing DCTs...",
    t0 = time.clock()
    R_DCTs = arraymap(Undo_DCT, R_DQ, dtype=int, esize=2)
    G_DCTs = arraymap(Undo_DCT, G_DQ, dtype=int, esize=2)
    B_DCTs = arraymap(Undo_DCT, B_DQ, dtype=int, esize=2)
    t1 = time.clock()
    tt += (t1 - t0)
    print "took", (t1 - t0), "seconds."

    print "Merging 8x8 blocks...",
    t0 = time.clock()
    R_Merged, G_Merged, B_Merged = map(Merge_Blocks, (R_DCTs, G_DCTs, B_DCTs))
    t1 = time.clock()
    tt += (t1 - t0)
    print "took", (t1 - t0), "seconds."

    print "Rearranging Data...",
    t0 = time.clock()
    final_array = array([R_Merged, G_Merged, B_Merged]).swapaxes(0, 2)
    t1 = time.clock()
    tt += (t1 - t0)
    print "took", (t1 - t0), "seconds."

    misc.imsave((filename + '.png'), final_array)
    print "\nAll in all...",
    print "everything took", tt, "seconds."
def main():
    
    # File handling
    filename = raw_input("What is the filename? ")
    if filename[-11:] != '.compressed':
        raise TypeError("Requires a .compressed file from Compression.py!")
    f = open(filename,'rb')
    data = pickle.load(f)
    f.close()

    # Additional prep
    filename = filename[:-11]
    tt = 0
    quality = ord(data[0])
    data = data[1:]
    
    print "Seperating Blocks...",
    t0 = time.clock()
    R, G, B = Unpack_Blocks(data)
    t1 = time.clock()
    tt += (t1-t0)
    print "took",(t1-t0),"seconds."
    
    print "De-Quantizing data...",
    t0 = time.clock()
    R_DQ = DeQuantize(R,quality)
    G_DQ = DeQuantize(G,quality)
    B_DQ = DeQuantize(B,quality)
    t1 = time.clock()
    tt += (t1-t0)
    print "took",(t1-t0),"seconds."
    
    print "Undoing DCTs...",
    t0 = time.clock()
    R_DCTs = arraymap(Undo_DCT, R_DQ, dtype=int, esize=2)
    G_DCTs = arraymap(Undo_DCT, G_DQ, dtype=int, esize=2)
    B_DCTs = arraymap(Undo_DCT, B_DQ, dtype=int, esize=2)
    t1 = time.clock()
    tt += (t1-t0)
    print "took",(t1-t0),"seconds."
    
    print "Merging 8x8 blocks...",
    t0 = time.clock()
    R_Merged, G_Merged, B_Merged = map(Merge_Blocks, (R_DCTs, G_DCTs, B_DCTs))
    t1 = time.clock()
    tt += (t1-t0)
    print "took",(t1-t0),"seconds."
    
    print "Rearranging Data...",
    t0 = time.clock()
    final_array = array([R_Merged, G_Merged, B_Merged]).swapaxes(0,2)
    t1 = time.clock()
    tt += (t1-t0)
    print "took",(t1-t0),"seconds."
    
    misc.imsave((filename+'.png'),final_array)
    print "\nAll in all...",
    print "everything took",tt,"seconds."
def Write_To(Q, Red, Grn, Blu, write):
    """Given RGB and a file's write function, writes appropriate headers and
    the contents of the array to the file."""
    L, W = Red.shape
    write(chr(Q))
    write(chr(L))
    write(chr(W))
    arraymap(write,Red)
    arraymap(write,Grn)
    arraymap(write,Blu)
def Write_To(Q, Red, Grn, Blu, write):
    """Given RGB and a file's write function, writes appropriate headers and
    the contents of the array to the file."""
    L, W = Red.shape
    write(chr(Q))
    write(chr(L))
    write(chr(W))
    arraymap(write, Red)
    arraymap(write, Grn)
    arraymap(write, Blu)
def main():
    print "What is the filename?"
    print "Windows:"
    print "winter.png"
    print "bird.png"
    print "Linux Mint:"
    print "./winter-wallpaper-24.png"
    print "./bird.png"
    
    i = image.imread(raw_input(''))
    if (i.shape[0] %8 != 0) or (i.shape[1] %8 != 0):
        raise TypeError("Requires an image whose size is a multiple of 8x8!")
    
    print "What level of image quality would you prefer?\nPlease select an integer 1-25,"
    print "1 being the greatest image quality but least compressed."
    q = int(raw_input(''))
    
    tt = 0
    
    print "Seperating Colors...",
    t0 = time.clock()
    Colors = Split_RGB(i)
    t1 = time.clock()
    tt += (t1-t0)
    print "took",(t1-t0),"seconds."
    # 'Colors' should be a list of the Length*Width arrays for each color value
    
    print "Splitting the image into 8x8 blocks...",
    t0 = time.clock()
    R_Blocks, G_Blocks, B_Blocks = map(Split_Blocks, Colors)
    t1 = time.clock()
    tt += (t1-t0)
    print "took",(t1-t0),"seconds."
    # Each '_Blocks' variable should be an array of 8x8 matricies, each containing
    # the respective R, G or B pixel data for that 8x8 portion of the image
    
    print "Calculating DCTs...",
    t0 = time.clock()
    R_DCTs = arraymap(Calc_DCT, R_Blocks, dtype=int, esize=2)
    G_DCTs = arraymap(Calc_DCT, G_Blocks, dtype=int, esize=2)
    B_DCTs = arraymap(Calc_DCT, B_Blocks, dtype=int, esize=2)
    t1 = time.clock()
    tt += (t1-t0)
    print "took",(t1-t0),"seconds."
##    print '\n Sample before DCT:'
##    print R_Blocks[0,0]
##    print '\n Sample after DCT:'
##    print R_DCTs[0,0]
    # Each '_DCTs' variable should be an array of the DCTs of said 8x8 matrices
    
    print "Quantizing data...",
    t0 = time.clock()
    R_Quantized = Quantize(R_DCTs,q)
    G_Quantized = Quantize(G_DCTs,q)
    B_Quantized = Quantize(B_DCTs,q)
    t1 = time.clock()
    tt += (t1-t0)
    print "took",(t1-t0),"seconds."
##    print 'R_Quantized\n',R_Quantized[0]
##    print 'G_Quantized\n',G_Quantized[0]
##    print 'B_Quantized\n',B_Quantized[0]
    # Each '_Quantized' variable should be an array of lists of each DCT
    # reorganized in a lossy, zigzag fashion

    print "Applying Run Width Algorithm...",
    t0 = time.clock()
    R_RunW = arraymap(Run_Width, R_Quantized)
    G_RunW = arraymap(Run_Width, G_Quantized)
    B_RunW = arraymap(Run_Width, B_Quantized)
    t1 = time.clock()
    tt += (t1-t0)
    print "took",(t1-t0),"seconds."
    # Each '_Quantized' variable should be an array of Run_Width() strings

    print "Saving to file...",
    t0 = time.clock()
    # Original
    #f = open(str(time.time())+'.compressed','w')
    #Write_To(R_RunW, G_RunW, B_RunW, f.write)
    
    # Pickle the array
    #f = open(str(time.time())+'.compressed','wb')
    #pickle.dump((R_RunW, G_RunW, B_RunW), f)
    
    # Pickle the string
    O = appendablestring()
    filename = str(int(time.time()*10))+'.compressed'
    f = open(filename,'wb')
    Write_To(q, R_RunW, G_RunW, B_RunW, O.append)
    pickle.dump(O.gimmie(), f)
    
    f.close()
    t1 = time.clock()
    tt += (t1-t0)
    print "took",(t1-t0),"seconds."

    print "\nAll in all...",
    print "everything took",tt,"seconds."
    print "Filename:",filename
def main():
    print "What is the filename?"
    print "Windows:"
    print "winter.png"
    print "bird.png"
    print "Linux Mint:"
    print "./winter-wallpaper-24.png"
    print "./bird.png"

    i = image.imread(raw_input(''))
    if (i.shape[0] % 8 != 0) or (i.shape[1] % 8 != 0):
        raise TypeError("Requires an image whose size is a multiple of 8x8!")

    print "What level of image quality would you prefer?\nPlease select an integer 1-25,"
    print "1 being the greatest image quality but least compressed."
    q = int(raw_input(''))

    tt = 0

    print "Seperating Colors...",
    t0 = time.clock()
    Colors = Split_RGB(i)
    t1 = time.clock()
    tt += (t1 - t0)
    print "took", (t1 - t0), "seconds."
    # 'Colors' should be a list of the Length*Width arrays for each color value

    print "Splitting the image into 8x8 blocks...",
    t0 = time.clock()
    R_Blocks, G_Blocks, B_Blocks = map(Split_Blocks, Colors)
    t1 = time.clock()
    tt += (t1 - t0)
    print "took", (t1 - t0), "seconds."
    # Each '_Blocks' variable should be an array of 8x8 matricies, each containing
    # the respective R, G or B pixel data for that 8x8 portion of the image

    print "Calculating DCTs...",
    t0 = time.clock()
    R_DCTs = arraymap(Calc_DCT, R_Blocks, dtype=int, esize=2)
    G_DCTs = arraymap(Calc_DCT, G_Blocks, dtype=int, esize=2)
    B_DCTs = arraymap(Calc_DCT, B_Blocks, dtype=int, esize=2)
    t1 = time.clock()
    tt += (t1 - t0)
    print "took", (t1 - t0), "seconds."
    ##    print '\n Sample before DCT:'
    ##    print R_Blocks[0,0]
    ##    print '\n Sample after DCT:'
    ##    print R_DCTs[0,0]
    # Each '_DCTs' variable should be an array of the DCTs of said 8x8 matrices

    print "Quantizing data...",
    t0 = time.clock()
    R_Quantized = Quantize(R_DCTs, q)
    G_Quantized = Quantize(G_DCTs, q)
    B_Quantized = Quantize(B_DCTs, q)
    t1 = time.clock()
    tt += (t1 - t0)
    print "took", (t1 - t0), "seconds."
    ##    print 'R_Quantized\n',R_Quantized[0]
    ##    print 'G_Quantized\n',G_Quantized[0]
    ##    print 'B_Quantized\n',B_Quantized[0]
    # Each '_Quantized' variable should be an array of lists of each DCT
    # reorganized in a lossy, zigzag fashion

    print "Applying Run Width Algorithm...",
    t0 = time.clock()
    R_RunW = arraymap(Run_Width, R_Quantized)
    G_RunW = arraymap(Run_Width, G_Quantized)
    B_RunW = arraymap(Run_Width, B_Quantized)
    t1 = time.clock()
    tt += (t1 - t0)
    print "took", (t1 - t0), "seconds."
    # Each '_Quantized' variable should be an array of Run_Width() strings

    print "Saving to file...",
    t0 = time.clock()
    # Original
    #f = open(str(time.time())+'.compressed','w')
    #Write_To(R_RunW, G_RunW, B_RunW, f.write)

    # Pickle the array
    #f = open(str(time.time())+'.compressed','wb')
    #pickle.dump((R_RunW, G_RunW, B_RunW), f)

    # Pickle the string
    O = appendablestring()
    filename = str(int(time.time() * 10)) + '.compressed'
    f = open(filename, 'wb')
    Write_To(q, R_RunW, G_RunW, B_RunW, O.append)
    pickle.dump(O.gimmie(), f)

    f.close()
    t1 = time.clock()
    tt += (t1 - t0)
    print "took", (t1 - t0), "seconds."

    print "\nAll in all...",
    print "everything took", tt, "seconds."
    print "Filename:", filename