annotate files.pde @ 200:dffdbdb4ded6

Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript Blobs, which is what mpSaveBinaryFile() converts given byte array into.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 29 Aug 2018 14:06:55 +0300
parents b36cfb497223
children c6c1dd769566
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 boolean mpHaveLocalStorage()
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 var test = 'mpLSTest';
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 try {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 localStorage.setItem(test, test);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 if (localStorage.getItem(test) == test)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 localStorage.removeItem(test);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 return true;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 catch (e) {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 return false;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 return false;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 function mpLoadFileSelector(fmtname, fmtexts, fcallback)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 var mpUI = stGE("mpUI");
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 if (mpUI)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24 stClearChildren(mpUI);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 mpUI.style.background = "red";
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 mpUI.style.padding = "0.5em";
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28 mobj = stCE("input", "mpFileSelector");
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 mobj.type = "file";
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 mobj.name = "name";
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 mobj.multiple = false;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 if (fmtexts != null)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 mobj.accept = fmtexts;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
35
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
36 stAddEventOb(mobj.name, mobj, "change",
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37 function(evt)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 var files = evt.target.files;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 if (files.length > 0)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
41 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
42 var freader = new FileReader();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44 freader.onloadend = (function(theFile) {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 fcallback(theFile, new Uint8Array(freader.result));
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 });
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 freader.readAsArrayBuffer(files[0]);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 stClearChildren(mpUI);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 mpUI.style.background = null;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 });
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54 mpUI.appendChild(mobj);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 mobj = stCE("span", "mpFileInfo");
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 mobj.innerHTML = "Load / import an '<b>"+ fmtname +"</b>' file.";
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 mpUI.appendChild(mobj);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 else
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 return null;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
63
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
64
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 //
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 // Basically the same as Processing loadBytes(), but it seems
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 // that Processing.JS's loadBytes() is broken at least in v1.4.8
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68 // and does not return byte-clean data. So roll a replacement of
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 // our own design. --ccr
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 //
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 byte[] mpLoadBinaryFile(String url)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 var xhr = new XMLHttpRequest();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 xhr.open("GET", url, false);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 xhr.overrideMimeType("text/plain; charset=x-user-defined");
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 xhr.setRequestHeader("If-Modified-Since", "Fri, 01 Jan 1960 00:00:00 GMT");
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 xhr.send(null);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 if (xhr.status !== 200 && xhr.status !== 0)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80 return null;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 var string = xhr.responseText;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 byte[] ret = new byte[string.length];
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 for (var i = 0; i < string.length; i++)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 ret[i] = string.charCodeAt(i) & 0xff;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 return ret;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 Blob mpMakeBinaryBlob(byte[] data)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 var blob = null;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97 if (data == null)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 return null;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 if (typeof(data) == "string")
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101 blob = new Blob([data], {type: "application/octet-stream"});
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 else
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 if (typeof(data) == "object")
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 blob = new Blob([new Uint8Array(data)], {type: "application/octet-stream"});
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106 return blob;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110 //
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 // "Save" a byte array to file. Basically creates a blob URI
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 // and dumps it in the DOM, giving user a download.
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 //
200
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
114 boolean mpSaveBinaryBlob(String name, void blob)
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 var url = window.URL.createObjectURL(blob);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 if (url == null)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 console.log("Could not create URL from BLOB object.");
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 return false;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 var alink = stCE("a");
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 var mpUI = stGE("mpUI");
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 stClearChildren(mpUI);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 mpUI.appendChild(alink);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 alink.style = "display: none";
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 alink.href = url;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 alink.download = name;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 alink.click();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 window.URL.revokeObjectURL(url);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 return true;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137
200
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
138 boolean mpSaveBinaryFile(String name, byte[] data)
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
139 {
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
140 var blob = mpMakeBinaryBlob(data);
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
141 if (blob == null)
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
142 {
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
143 console.log("Could not create BLOB from data.");
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
144 return false;
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
145 }
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
146
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
147 return mpSaveBinaryBlob(name, blob);
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
148 }
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
149
dffdbdb4ded6 Split mpSaveBinaryFile() into mpSaveBinaryBlob() for saving JavaScript
Matti Hamalainen <ccr@tnsp.org>
parents: 195
diff changeset
150
195
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 // bordh/v = 64, 32, omag = 1
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 void mpSavePNGImage(String name, int fmt, boolean border, int bordh, int bordv, int omag)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154 PImage simg = mpRenderImage(border, bordh, bordv, omag);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 // if (g_data[int('Q')] == 0)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157 if (simg !== null)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 // XXX TODO .. actually save the image, something like ..
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 //simg.canvas.toBlob(function(idata){ mpSaveBinaryFile(name, idata); }, "image/png", 0.95);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 int mpLoadPNGImage(String name)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167 PImage simg = null;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168 if (simg == null)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169 return -1;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 int lefth = g_farge;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 int righth = g_backg;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 storeparameters();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 g_data[int('d')] = 0;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 g_data[int('t')] = 0;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 g_data[int('b')] = 1; //old IQ
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179 if (!mpImportFromImage(simg))
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180 return -2;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182 restoreparameters();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
183 refreshpalette();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184 refresh();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185 g_boxreconstruct = 2;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186 selectcolor(0, lefth);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 selectcolor(1, righth);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188 return 0;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 byte[] mpGetNativeImage()
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194 //save the picture page g_map[], make sure some essential parameters are correct
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195 g_map[3] = byte(g_machine);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196 g_map[5] = byte(MX);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
197 g_map[7] = byte(MY);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199 return g_map;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 int mpSetNativeImage(byte[] data, boolean noError)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
205 if (data == null)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206 return -1;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208 if (data[3] != g_machine && !noError)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209 return -2;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211 store_undo();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
212
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213 g_map = data;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
215 refreshpalette();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216 consistency();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217 g_farge = int(g_realfront);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218 g_ofarge = g_farge;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219 g_backg = int(g_realback);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220 sussborder();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222 return 0;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226 int mpLoadNativeImage(String name, boolean noError)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228 return mpSetNativeImage(
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229 mpLoadBinaryFile(name), noError);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
231
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233 boolean mpLoadPalette(String fname)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
235 if (g_map[13] != C64)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237 return false;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240 byte fdata[] = mpLoadbinaryFile(fname);
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 if (fdata == null ||
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242 fdata.length != 772 ||
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
243 fdata[0x301] != byte(0x10) ||
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244 fdata[0x302] != byte(0xff) ||
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245 fdata[0x303] != byte(0xff))
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 return false;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250 for (int n = 0; n < 16; n++)
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251 {
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 makecolor(n,
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253 int(fdata[n * 3]),
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
254 int(fdata[n * 3 + 1]),
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255 int(fdata[n * 3 + 2]));
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256 }
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 mpSetUIColors();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 sussborder();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 refresh_all();
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263 return true;
b36cfb497223 Move file related functions to files.pde
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264 }