comparison files.pde @ 195:b36cfb497223

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