package com.wheany;

import com.vaadin.server.StreamResource;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.*;
import java.io.*;

public class ImageResource
        implements StreamResource.StreamSource {
    int reloads = 0;

    BufferedImage myImg;

    public ImageResource(File input) throws IOException {
        BufferedImage in = ImageIO.read(input);

        myImg = new BufferedImage(in.getWidth(), in.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = myImg.createGraphics();
        g.drawImage(in, 0, 0, null);
        g.dispose();
    }

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    private double value = 0;

    /* We need to implement this method that returns
     * the resource as a stream. */
    public InputStream getStream () {
        /* Create an image and draw something on it. */
        BufferedImage image = new BufferedImage (myImg.getWidth(), myImg.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics drawable = image.getGraphics();
        drawable.drawImage(myImg, 0, 0, null);
        drawable.setColor(Color.lightGray);
        drawable.fillRect(0,0,200,200);
        drawable.setColor(Color.yellow);
        drawable.fillOval(25,25,150,150);
        drawable.setColor(Color.blue);
        drawable.drawRect(0,0,199,199);
        drawable.setColor(Color.black);
        drawable.drawString("Reloads="+reloads, 75, 100);
        drawable.drawString("Value="+value, 75, 125);
        reloads++;

        try {
            /* Write the image to a buffer. */
            ByteArrayOutputStream imagebuffer = new ByteArrayOutputStream();
            ImageIO.write(image, "png", imagebuffer);

            /* Return a stream from the buffer. */
            return new ByteArrayInputStream(
                    imagebuffer.toByteArray());
        } catch (IOException e) {
            return null;
        }
    }
}

@@ -149,16 +149,49 @@
 .io.*;%0A%0A
+import static java.lang.Math.*;%0A%0A
 public c
@@ -298,21 +298,140 @@
 edImage 
-myImg
+original;%0A    BufferedImage adjusted;%0A%0A    private int RAdjust = 0;%0A    private int GAdjust = 0;%0A    private int BAdjust = 0
 ;%0A%0A    p
@@ -540,21 +540,24 @@
         
-myImg
+original
  = new B
@@ -615,33 +615,32 @@
 dImage.TYPE_INT_
-A
 RGB);%0A        Gr
@@ -652,21 +652,24 @@
 s2D g = 
-myImg
+original
 .createG
@@ -736,16 +736,1081 @@
 spose();
+%0A%0A        adjusted = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_RGB);%0A        updateAdjusted();%0A    %7D%0A    %0A    private int clamp_0_255(int value) %7B%0A        return max(0, min(255, value));%0A    %7D%0A%0A    private void updateAdjusted() %7B%0A        int%5B%5D originalRGB = original.getRGB(0, 0, original.getWidth(), original.getHeight(), null, 0, original.getWidth());%0A        int%5B%5D adjustedRGB = new int%5BoriginalRGB.length%5D;%0A        %0A        for (int i = 0; i %3C originalRGB.length; i++) %7B%0A            int r = (originalRGB%5Bi%5D & 0x00FF0000) %3E%3E%3E 16;%0A            int g = (originalRGB%5Bi%5D & 0x0000FF00) %3E%3E%3E 8;%0A            int b = (originalRGB%5Bi%5D & 0x000000FF);%0A            %0A            int adjustedR = clamp_0_255(r + RAdjust);%0A            int adjustedG = clamp_0_255(g + GAdjust);%0A            int adjustedB = clamp_0_255(b + BAdjust);%0A            %0A            adjustedRGB%5Bi%5D = (adjustedR %3C%3C 16) %7C (adjustedG %3C%3C 8) %7C adjustedB;%0A        %7D%0A%0A        adjusted.setRGB(0,0,original.getWidth(),original.getHeight(),adjustedRGB,0,original.getWidth());
 %0A    %7D%0A%0A
@@ -2208,21 +2208,24 @@
 dImage (
-myImg
+adjusted
 .getWidt
@@ -2229,21 +2229,24 @@
 idth(), 
-myImg
+adjusted
 .getHeig
@@ -2274,17 +2274,16 @@
 YPE_INT_
-A
 RGB);%0A  
@@ -2360,13 +2360,16 @@
 age(
-myImg
+adjusted
 , 0,

Copy image by copying the raw RGB values, prepare for adjustments