/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sister.kelompok5;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import javax.imageio.ImageIO;

public class Message extends UnicastRemoteObject implements MessageInterface {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public Message() throws RemoteException {        
    }

	public void Convert(byte[] imageByteArray, int fileNumber, String fileName, String fileExtension) throws IOException {
		// Handle byte array of image input
		BufferedImage imageIn = ImageIO.read(new ByteArrayInputStream(imageByteArray));
		
		// Get image attributes
		int height = imageIn.getHeight();
		int width = imageIn.getWidth();
		
		BufferedImage imageAttributes = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
		
		// Convert to grayscale
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				Color color = new Color(imageIn.getRGB(x, y));
				
				int temp = (int) (color.getRed() * 0.299 + color.getGreen() * 0.587 + color.getBlue() * 0.114);
				
				imageAttributes.setRGB(x, y, temp << 16 | temp << 8 | temp);
			}
		}
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		// Get byte array image again
		ImageIO.write(imageAttributes, fileExtension, baos);
		
		byte[] imageOut = baos.toByteArray();
		
		// Construct image like before
		BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageOut));
		String pathNewFile = "";
			
		File path = new File(pathNewFile + fileNumber + "_" + fileName + "_grayscale." + fileExtension);
			
		ImageIO.write(image, fileExtension, path);
	}
    
}

@@ -263,29 +263,8 @@
 am;%0A
-import java.io.File;%0A
 impo
@@ -612,12 +612,14 @@
 lic 
-void
+byte%5B%5D
  Con
@@ -1582,290 +1582,23 @@
 %09%0A%09%09
-// Construct image like before%0A%09%09BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageOut));%0A%09%09String pathNewFile = %22%22;%0A%09%09%09%0A%09%09File path = new File(pathNewFile + fileNumber + %22_%22 + fileName + %22_grayscale.%22 + fileExtension);%0A%09%09%09%0A%09%09ImageIO.write(image, fileExtension, path)
+return imageOut
 ;%0A%09%7D

move image writer to client