# HG changeset patch # User Matti Hamalainen # Date 1530689652 -10800 # Node ID edcf9039552b784f681453abe7e78dcd9d1e5bc2 # Parent f9bffb2fbb2cf03ccea5a76984aabaae30a04193 Add several new helper functions for loading and saving formats using Javascript facilities. diff -r f9bffb2fbb2c -r edcf9039552b multipaint.pde --- a/multipaint.pde Wed Jul 04 10:33:42 2018 +0300 +++ b/multipaint.pde Wed Jul 04 10:34:12 2018 +0300 @@ -218,12 +218,176 @@ saveBytes(name, g_map); } + boolean fexists(String fname) { // File f = new File(fname); // if (f.exists())return true; return true; } + +function mpLoadFileSelector(ftypes, callback) +{ + var mpUI = stGE("mpUI"); + if (mpUI) + { + stClearChildren(mpUI); + mpUI.style.background = "red"; + mpUI.style.padding = "0.5em"; + + var mobj = stCE("input", "mpFileSelector"); + mobj.type = "file"; + mobj.name = "name"; + mobj.multiple = false; + + if (ftypes != null) + mobj.accept = ftypes; + + stAddEventOb(mobj.name, mobj, "change", + function(evt) + { + var files = evt.target.files; + if (files.length > 0) + { + var freader = new FileReader(); + + freader.onloadend = (function(theFile) { + callback(theFile, new Uint8Array(freader.result)); + }); + + freader.readAsArrayBuffer(files[0]); + } + stClearChildren(mpUI); + mpUI.style.background = null; + }); + + mpUI.appendChild(mobj); + } + else + return null; +} + + +byte[] mpLoadBinaryFile(String url) +{ + var xhr = new XMLHttpRequest(); + xhr.open("GET", url, false); + xhr.overrideMimeType("text/plain; charset=x-user-defined"); + xhr.setRequestHeader("If-Modified-Since", "Fri, 01 Jan 1960 00:00:00 GMT"); + xhr.send(null); + + if (xhr.status !== 200 && xhr.status !== 0) + return null; + + var string = xhr.responseText; + byte[] ret = new byte[string.length]; + + for (var i = 0; i < string.length; i++) + { + ret[i] = string.charCodeAt(i) & 0xff; + } + + return ret; +} + + +void mpSaveBinaryFile(String name, byte[] data) +{ + var blob = new Blob([new Uint8Array(data)], {type: "application/octet-stream"}), + url = window.URL.createObjectURL(blob); + + var alink = stCE("a"); + var mpUI = stGE("mpUI"); + stClearChildren(mpUI); + mpUI.appendChild(alink); + + alink.style = "display: none"; + alink.href = url; + alink.download = name; + alink.click(); + + window.URL.revokeObjectURL(url); +} + + +void mpSavePNGImage(String name) +{ + if (g_data[int('Q')] == 0) + export_image_sans_border(name); + else + export_image(name); +} + + +void mpLoadPNGImage(String name) +{ + int lefth = g_farge; + int righth = g_backg; + storeparameters(); + + g_data[int('d')] = 0; + g_data[int('t')] = 0; + g_data[int('b')] = 1; //old IQ + + import_image(name); + + restoreparameters(); + refreshpalette(); + refresh(); + g_boxreconstruct = 2; + message("Image|loaded"); + selectcolor(0, lefth); + selectcolor(1, righth); +} + + +void mpSaveNativeImage(String name) { + //save the picture page g_map[], make sure some essential parameters are correct + g_map[3] = byte(machine); + g_map[5] = byte(MX); + g_map[7] = byte(MY); + + mpSaveBinaryFile(name, g_map); +} + + +void mpSetNativeImage(byte[] data, bool noError) +{ + if (data == null) { + message("NO FILE"); + return false; + } + + if (data[3] != machine && !noError) { + message("Wrong|machine!"); + return false; + } + + g_map = data; + + refreshpalette(); + consistency(); + g_farge = int(g_realfront); + g_ofarge = g_farge; + g_backg = int(g_realback); + sussborder(); + + return true; +} + + +bool mpLoadNativeImage(String name, bool noError) { + //load the picture page g_map[] with parameters + byte[] data = mpLoadBinaryFile(name); + + if (!mpSetNativeImage(data, noError)) + return false; + + message("Page|loaded"); + return true; +} + + void restore(String name) { //load the picture page g_map[] with parameters boolean im_import = false;