# filters.py
# Convert color image using various filters

from Processing import *

# Load the image to process
img = loadImage("andy-warhol2.jpg")

# Create a window of the same size
w = img.width()
h = img.height()
window( w, h )

image(img, 0, 0)       # Draw the image

# Negative
def negative( c ):
    return color(255-red(c), 255-green(c), 255-blue(c))

# Sepia
def sepia( c ):
    r = int( red(c)*0.393 + green(c)*0.769 + blue(c)*0.189 )
    g = int( red(c)*0.349 + green(c)*0.686 + blue(c)*0.168 )
    b = int( red(c)*0.272 + green(c)*0.534 + blue(c)*0.131 )
    r = constrain( r, 0, 255 )
    g = constrain( g, 0, 255 )
    b = constrain( b, 0, 255 )
    return color(r, g, b)

# Perform the filtering
def filter(o, e):
    image( img, 0, 0)               # Redraw the original image
    loadPixels()                    # Load pixels in preparation for processing

    for i in range(w):              # Loop over all pixels
        for j in range(h):
            c = getPixel(i, j)      # Get the color

            #c = negative(c)         # Filter
            c = sepia(c)            # Filter

            setPixel(i, j, c)       # Update

    updatePixels()                  # Update pixels in image

# When the mouse is pressed, perform the conversion
onMousePressed += filter
