package com.haxademic.core.draw.image;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Base64;

import javax.imageio.ImageIO;

import processing.core.PImage;

public class Base64Image {
	// adapted from: https://forum.processing.org/two/discussion/6958/pimage-base64-encode-and-decode
	// more info: https://docs.oracle.com/javase/tutorial/2d/images/saveimage.html
	// String writerNames[] = ImageIO.getWriterFormatNames();
	// [JPG, jpg, bmp, BMP, gif, GIF, WBMP, png, PNG, wbmp, jpeg, JPEG]
	
	public static String encodePImageToBase64(PImage img, String format) throws UnsupportedEncodingException, IOException {
		String result = null;
		BufferedImage buffImage = (BufferedImage)img.getNative();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ImageIO.write(buffImage, format, out);
		result = Base64.getEncoder().encodeToString(out.toByteArray());
		return result;
	}

	public static PImage decodePImageFromBase64(String base64Str) {
		PImage result = null;
		byte[] decodedBytes = Base64.getDecoder().decode(base64Str);
		ByteArrayInputStream in = new ByteArrayInputStream(decodedBytes);
		BufferedImage bImageFromConvert;
		try {
			bImageFromConvert = ImageIO.read(in);
			BufferedImage convertedImg = new BufferedImage(bImageFromConvert.getWidth(), bImageFromConvert.getHeight(), BufferedImage.TYPE_INT_ARGB);
			convertedImg.getGraphics().drawImage(bImageFromConvert, 0, 0, null);
			result = new PImage(convertedImg);
			return result;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
}

@@ -770,58 +770,123 @@
 %7B%0A%09%09
-String result = null;%0A%09%09BufferedImage buffImage = 
+return encodePImageToBase64((BufferedImage)img.getNative(), format);%0A%09%7D%0A%0A%09public static String encodePImageToBase64
 (Buf
@@ -895,32 +895,110 @@
 redImage
-)img.getNative()
+ img, String format) throws UnsupportedEncodingException, IOException %7B%0A%09%09String result = null
 ;%0A%09%09Byte
@@ -1070,17 +1070,11 @@
 ite(
-buffImage
+img
 , fo
@@ -1167,24 +1167,25 @@
  result;%0A%09%7D%0A
+%09
 %0A%09public sta

Add ability to pass a BufferedImage as part of threading strategy