Read binary file with extendscript - binary

Limited to using Extendscript in Photoshop I'm trying to write and then read in the same binary file.
I can write the file okay, but I'm not sure where I'm going wrong with the read part.
The data will be RGB colours in hex, so I'll either want to return the data from the read function as array or a string. Only I can't even get it to tell me the file just written exists. And I'm not sure if I should be using seek() or read(). Confused.
var f = new File("D:\\temp\\bin.act");
var w = write_binary(f);
var r = read_binary(w);
alert(r);
function write_binary(afile)
{
afile.encoding = "BINARY";
afile.open ("w");
for(i = 0; i < 256; i++)
{
afile.write(String.fromCharCode (i));
}
afile.close();
}
function read_binary(afile)
{
var f = new File(afile);
f.open("r");
f.encoding = "BINARY";
//var data = f.read();
//if(f.exists) alert(afile);
//alert (data);
var arr = [];
for (var i = 0; i < f.length; i+=4)
{
f.seek(i, 0);
var hex = f.readch().charCodeAt(0).toString(16);
if(hex.length === 1) hex = "0" + hex;
arr.push(hex);
}
return arr;
}

You can read it like this:
// Open binary file
var afile = "/Users/mark/StackOverflow/data.bin"
var f = new File(afile);
f.open("r");
f.encoding = "BINARY";
alert('OK');
var hexstring=""
while (true){
var b = f.readch().charCodeAt(0);
if(f.eof){break;}
var hex = b.toString(16);
if(hex.length === 1) hex = "0" + hex;
hexstring += hex;
}
alert(hexstring);
The corresponding writing part of this answer is here.

Related

Is a unicode code point can be displayed normally?

I want to know whether a unicode code point can be displayed in chrome, and check it in golang, so I can escape some parts of a string before i send it to chrome.
I have found that there is three types of unicode code point can not be displayed normally in chrome :
Space . like code point 0x20(32). ' '
Font not exist. There is a question mark in the square box. (occupy two English letters width) like code point 0x378(888) "͸"
Invaild code point value. A diamond-shaped black box with a question mark inside (occupy one English letter width) like code point 0xd800(55296) "�"
I found that I can use js and OffscreenCanvas to render all of the unicode code point and check if them looks like above three types.
Then I got a function that can tell you whether a rune/character can be displayed normally by the operating system. https://github.com/fastslothlab/fslRune
js code:
(function(){
var start = 0;
var end = 1114111;
const imageSize = 20;
var tufoCiList = [32,888,55296];
var num = end-start+1;
var startTime = performance.now()
var hasDataCiList = [];
var c = new OffscreenCanvas(imageSize,imageSize);
var ctx = c.getContext("2d");
function getImageDataByCi(ci){
var s = String.fromCodePoint(ci);
ctx.clearRect(0,0,imageSize,imageSize);
ctx.fillText(s, 5, 15);
var imgD = ctx.getImageData(0,0,imageSize,imageSize);
var hasNoneZero = false;
var buf = imgD.data;
return buf;
}
var tofuBufList = [];
for (var i in tufoCiList){
tofuBufList.push(getImageDataByCi(tufoCiList[i]));
}
function debugImage(){
c.convertToBlob({
type: "image/png",
}).then(function(blob) {
var url = URL.createObjectURL(blob);
var imgObj = document.createElement("img");
imgObj.src = url;
document.body.appendChild(imgObj);
console.log(url);
});
}
function bufEqual(buf1, buf2){
if (buf1.byteLength != buf2.byteLength) return false;
for (var i = 0 ; i != buf1.byteLength ; i++){
if (buf1[i] != buf2[i]) return false;
}
return true;
}
for (var ci =start;ci<=end;ci++){
var buf = getImageDataByCi(ci);
var hasFound = false;
for (var i in tofuBufList){
if (bufEqual(buf,tofuBufList[i])){
hasFound = true;
break;
}
}
if (hasFound){
continue;
}
hasDataCiList.push(ci);
}
console.log((performance.now()-startTime)/num);
console.log(JSON.stringify(hasDataCiList));
})();

Hash txt strings in GAS, incorrect line ending

I want hash (md5) some txt strings in GAS, and have a problem, may be
incorrect line ending.
Example:
test
test
correct hash 76ce9f441de2ed5de337d391ad4516b7
using GAS i getting wrong hash: e8230113fbba92427c1c41cf34a80c13
function test() {
var data = 'test\
test';
Logger.log(data.MD5());
return (data.MD5());
}
String.prototype.MD5 = function(charset, toByte) {
charset = charset || Utilities.Charset.UTF_8;
var digest = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, this, charset);
if (toByte) return digest;
var __ = '';
for (i = 0; i < digest.length; i++) {
var byte = digest[i];
if (byte < 0) byte += 256;
var bStr = byte.toString(16);
if (bStr.length == 1) bStr = '0' + bStr;
__ += bStr;
}
return __;
}
As already #Ameen mentioned you are checking different strings
function test(){
var s1 = 'test\ntest';
var s2 = 'test\r\ntest';
Logger.log(s1.MD5() === '76ce9f441de2ed5de337d391ad4516b7');
Logger.log(s2.MD5() === '76ce9f441de2ed5de337d391ad4516b7');
}
[19-03-22 18:03:11:441 MSK] false
[19-03-22 18:03:11:442 MSK] true
A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.
It seems you're working under Windows.

Angular 2 - Parsing Excel worksheet to Json

I have an Excel file with the following content:
Inside my component.ts, I extract the Excel's content as follow:
var testUrl= "excel.xlsx";
var oReq = new XMLHttpRequest();
oReq.open("GET", testUrl, true);
oReq.responseType = "arraybuffer";
oReq.onload = function(e) {
var arraybuffer = oReq.response;
var data = new Uint8Array(arraybuffer);
var arr = new Array();
for(var i = 0; i != data.length; ++i){
arr[i] = String.fromCharCode(data[i]);
}
var bstr = arr.join("");
var workbook = XLSX.read(bstr, {type:"binary"});
var first_sheet_name = workbook.SheetNames[0];
var worksheet = workbook.Sheets[first_sheet_name];
var json = XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]], {header:1, raw:true});
var jsonOut = JSON.stringify(json);
console.log("test"+jsonOut);
}
oReq.onerror = function(e) {
console.log(e);
}
oReq.send();
XLSX.utils.sheet_to_json will format JSON as follow:
However, I would like the JSON to be as follow:
Most probably I would need to manually create the JSON, but can anyone help me point to the direction on how I can accomplish this?
In your case we need to modify the JSON data by looping over XLSX.utils.sheet_to_json JSON object:
// This object will contain the data in the format we want
var finalObj = { "object": []};
// Variables to track where to insert the data
var locIndex, firstCondIndex, secondCondIndex,
lockey, firstCondKey, secondCondkey;
// We need to initialize all indexes to -1 so that on first time we can get 0, as arrays start with 0 in javascript
locIndex = -1;
// here obj is XLSX.utils.sheet_to_json
obj.object.map((value, index) => {
// we don't want to consider name of columns which is first element of array
if(!index) return;
// Go inside only if not null
if(value[0]) {
// For Location
finalObj.object.push(createObj(value[0]));
locIndex++;
// We also need to store key names to push it's children
lockey = value[0];
firstCondIndex = -1;
}
if(value[1]) {
// For First Condition
finalObj.object[locIndex][lockey].push(createObj(value[1]));
firstCondIndex++;
firstCondKey = value[1];
secondCondIndex = -1;
}
if(value[2]) {
// For Second Condition
finalObj.object[locIndex][lockey][firstCondIndex][firstCondKey].push(createObj(value[2]));
secondCondIndex++;
secondCondkey = value[2];
}
if(value[3]) {
// For Products
// We just push the string
finalObj.object[locIndex][lockey][firstCondIndex][firstCondKey][secondCondIndex][secondCondkey].push(value[3]);
}
});
function createObj(val) {
// We need to initialize blank array so we can push the children of that element later on
var obj = {};
obj[val] = [];
return obj;
}
console.log(finalObj);

Execute Code as Fast as Possible

I am using node.js with my WebStorm IDE to parse a large JSON file (~500 megabytes). Here is my code:
fs = require("fs");
fs.readFile('C:/Users/.../Documents/AAPL.json', 'utf8', function (err,data) {
for (i = 0; i < 1000; i++) {
var hex = JSON.parse(data)[i]._source.layers.data["data.data"];
var askPrice = parseInt(hex.substring(215, 239).split(":").reverse().join(""),16);
var bidPrice = parseInt(hex.substring(192, 215).split(":").reverse().join(""),16);
var symbol = hex.substring(156, 179);
var timestamp = hex.substring(132, 155);
var askSize = hex.substring(240, 251);
var bidSize = hex.substring(180, 191);
var price = String((+bidPrice+askPrice)/2);
var realprice = price.slice(0, price.length - 4) + "." + price.slice(price.length - 4);
function hex2a(hexx) {
var hex = hexx.toString();
var str = '';
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
if(JSON.parse(data)[i]._source.layers.data["data.len"] == 84 && realprice.length == 8 && +realprice <154 && +realprice >145) {
console.log(i + " " + hex2a(symbol.replace(/:/g, "")) + " sold for " + realprice + " at " + parseInt(timestamp.split(":").reverse().join(""), 16));
}
}
});
The problem I am running into however is that my IDE is parsing this file at an extremely slow speed, roughly 1 iteration a second. I do not think this is because I have a slow computer, for I have a high end rig with a core i7 7700k and a gtx 1070. I tried executing the code in the console with the same result. I tried trimming down the code and again I achieved the same speed:
fs = require("fs");
fs.readFile('C:/Users/Brandt Winkler Prins/Documents/AAPL.json', 'utf8', function (err,data) {
for (i = 0; i < 12000; i++) {
var hex = JSON.parse(data)[i]._source.layers.data["data.data"];
var askPrice = parseInt(hex.substring(215, 239).split(":").reverse().join(""),16);
var bidPrice = parseInt(hex.substring(192, 215).split(":").reverse().join(""),16);
var price = String((+bidPrice+askPrice)/2);
var realprice = price.slice(0, price.length - 4) + "." + price.slice(price.length - 4);
if(+realprice <154 && +realprice >145) {
console.log(realprice);
}
}
});
How should I execute my code to get my data as fast as possible?
You're running JSON.parse(data) every iteration, that might take quite some time for a 500MB json file.
The solution would be to move it out of the loop and reuse the parsed object:
var obj = JSON.parse(data);
for (...

Is it possible to create multiple images in Photoshop using HEX values from a CSV file

I need to create 100's of single colour .png files in photoshop where I can call hex values from a CSV file to change the colour of the image and save as the corrisponding name in the CSV file.
What is the best way of doing this?
Below is my function for reading a csv file. Once you have the colors, you can use 'app.documents.add()' to create a new document with an initial fill color, save etc. For more info check out your Photoshop JavaScript Reference pdf in your Photoshop install directory.
Misc.ReadCsvData = function (csvFile, bHeaderOnly) {
if (bHeaderOnly == undefined)
bHeaderOnly = false;
var line, temp, csvRecord, tempArr;
var csvData = new Array();
var tempArr2 = new Array();
csvFile.open("r");
var startsWithQuote = new RegExp('^\s*"');
var endsWithQuote = new RegExp('"\s*$');
var doubleQuotes = new RegExp('""', "g");
var hasData = new RegExp('[a-zA-Z0-9]');
var bInsideQuote = false;
var iFieldCount = -1;
var sTemp;
while (!csvFile.eof) {
line = csvFile.readln();
if (line.match(hasData)) {
tempArr = line.split(",");
tempArr2 = new Array();
for (var i = 0; i < tempArr.length; i++) {
if (!bInsideQuote && !tempArr[i].match(startsWithQuote)) {
tempArr2.push(tempArr[i].replace(doubleQuotes, '"'));
} else if (!bInsideQuote && tempArr[i].match(startsWithQuote) && tempArr[i].match(endsWithQuote)) {
tempArr2.push(tempArr[i].replace(startsWithQuote, "").replace(endsWithQuote, "").replace(doubleQuotes, '"'));
} else if (!bInsideQuote && tempArr[i].match(startsWithQuote)) {
bInsideQuote = true;
sTemp = tempArr[i];
} else if (bInsideQuote) {
sTemp = sTemp + ", " + tempArr[i];
if (tempArr[i].match(endsWithQuote)) {
tempArr2.push(sTemp.replace(startsWithQuote, "").replace(endsWithQuote, "").replace(doubleQuotes, '"'));
bInsideQuote = false;
}
}
}
if (iFieldCount < 0)
iFieldCount = tempArr2.length;
if (iFieldCount != tempArr2.length) {
throw "Cannot parse csv format.";
}
csvData.push(tempArr2);
if (bHeaderOnly)
break;
}
}
csvFile.close();
return csvData;
}