Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use in ChromeWorker #3

Open
Noitidart opened this issue Sep 9, 2015 · 21 comments
Open

Use in ChromeWorker #3

Noitidart opened this issue Sep 9, 2015 · 21 comments

Comments

@Noitidart
Copy link

I was thinking of using this in ChromeWorker for firefox addon. My needs were very basic, I am XHR'ing a file in, its either png, jpeg, or tiff, maybe gif but probably not.

But after loading I need to get the byte data/imagedata.

Then with imagedata's of multiple files i need to:

  • get height/width
  • overlap some at various x/y within a base imagedata
  • scale up/down some
  • save as png

It seems this can handle all of that, but it has tons of other stuff which is awesome. But basically, i couldn't find how to pass in arraybuffer of png/jpeg/tiff and get back bytedata, is it possible with this?

Thanks

@manuels
Copy link
Owner

manuels commented Sep 9, 2015

@Noitidart
Copy link
Author

Wow thanks so much for the fast reply! I did but I was super intimidated by the tons of files. I can't simply copy in interface.js, im not sure what all i have to copy and paste, is there some documentation on this stuff, im new to imagemagick :P

@Noitidart
Copy link
Author

So I dropped it in, and it loaded without errors, but when i try to run the exxample code it throws:

TypeError: FS.root.contents[pseudo_name] is undefined convert-worker.js:49:0

This is the line getting the error var array = FS.root.contents[pseudo_name].contents;

These are the files I dropped into my addon:Noitidart/Icon-Container-Generator@d84bbb7

modules/unixToolbox/config/coder.xml
+122 −0 
modules/unixToolbox/config/delegates.xml
+1,592 −0 
modules/unixToolbox/config/english.xml
+47 −0 
modules/unixToolbox/config/locale.xml
+23 −0 
modules/unixToolbox/config/magic.xml
+59 −0 
modules/unixToolbox/config/policy.xml
+506,530 −0 
modules/unixToolbox/convert-worker.js
+230 −0 
modules/unixToolbox/interface.js

I had to modify interface.js so it loaded into my ChromeWorker global instead of worker:

So I unwrapped the anon: https://github.com/Noitidart/Icon-Container-Generator/blob/d84bbb719f1ae3fb2c81268705d1244b311d3e47/modules/unixToolbox/interface.js#L1

Then call it here:
https://github.com/Noitidart/Icon-Container-Generator/blob/d84bbb719f1ae3fb2c81268705d1244b311d3e47/modules/workers/ICGenWorker.js#L26-L28

I can import convert-worker fine too, on the lines below that. But the sample code throws:

https://github.com/Noitidart/Icon-Container-Generator/blob/d84bbb719f1ae3fb2c81268705d1244b311d3e47/modules/workers/ICGenWorker.js#L41-L47

the addUrl's dont thow

@Noitidart
Copy link
Author

A user just reached out to me and gave me a great idea. I made this addon to take a screenshot of all monitors using native api's for all os's: https://addons.mozilla.org/en-US/firefox/addon/nativeshot/

he wants to extend that to a screen recorder. so for that what i need to do is diff two screenshots, and whatever is the same i need to delete it from the second. does imagemagick js have something that can like diff and keep only the differences in second file? its not hard to do i can just do the byte array comparison but because yours is asm.js i think it would be way way way faster.

thanks so much if you can help me get this working i can get this into the hands of a lot of people. with working examples in firefox addons and apps.

i also do a bunch of documentation so would love to doc this stuff.

@manuels
Copy link
Owner

manuels commented Sep 9, 2015

You are passing the image url directly to convert.
Instead, you must execute a addUrl for it to add it to the 'virtual filesystem' and then invoke convert.run on that path in the virtual filesystem.
For more on comparing images with ImageMagick, checkout this page: http://www.imagemagick.org/Usage/compare/

@Noitidart
Copy link
Author

Thanks checking right away!

@Noitidart
Copy link
Author

I updated it:

https://github.com/Noitidart/Icon-Container-Generator/blob/trying-unixToolbox/modules/workers/ICGenWorker.js#L41

Im getting these errors now:

: NS_ERROR_UNKNOWN_PROTOCOL: convert-worker.js:30:0

@manuels
Copy link
Owner

manuels commented Sep 9, 2015

change

 convert.run('-rotate', '90', OS.Path.join(OS.Constants.Path.desktopDir, 'Image-Box-64.png'), OS.Path.join(OS.Constants.Path.desktopDir, 'Image-Box-64-rot90.jpeg')).then(function() {

to

 convert.run('-rotate', '90', '/Image-Box-64.png', '/Image-Box-64-rot90.jpeg').then(function() {

@Noitidart
Copy link
Author

Thanks man! I tried it but same thing :(
https://github.com/Noitidart/Icon-Container-Generator/blob/trying-unixToolbox/modules/workers/ICGenWorker.js#L44

: unreachable code after return statement convert-worker.js:36113:10
: NS_ERROR_UNKNOWN_PROTOCOL: convert-worker.js:30:0

I even tried adding a slash in front of the image in the getFile:

convert.getFile('Image-Box-64-rot90.jpeg').then(function(real_contents) {

to

convert.getFile('/Image-Box-64-rot90.jpeg').then(function(real_contents) {

didnt fix it :(

@manuels
Copy link
Owner

manuels commented Sep 9, 2015

If the error is in this line, then probably this line is actually the problem (which is invoked by addUrl).
Are you sure you can run an XHR on an OS.Path.join(OS.Constants.Path.desktopDir, ...) URL?

@Noitidart
Copy link
Author

Ahhh you know what, in ChromeWorker, xhr to local files returns a status of 0, which is failure code for non-http: http://stackoverflow.com/questions/25585359/how-to-xhr-local-files/25585841#25585841

I also think I might need to make the path a file uri before xhr'ing.

@Noitidart
Copy link
Author

Holy hell!! It works!! it output this text file:

https://github.com/Noitidart/Icon-Container-Generator/blob/trying-unixToolbox/modules/workers/ICGenWorker.js#L41

data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAAZiS0dEAP8A/wD/oL2nkwAADcRJREFUeNrVm3+MG8d1x79vdpdc8si7051+25IlWj8sWT9OulMQqVF8liW0VZy0TSy1kuMfgR3BaBsEcRsYKWDIbgo0qNsiiRs0cdRETmw5vgQ2kriyUMm2bCkxYud0gm35ojv4JNk6+3w/dD/II5fL3Xn9g0veklzyuNRdaw9AgLtLvnnzmTfzZt68JcxyaXv5qSXS4q1M3AKmNcRYJoEFgGwUQgSllFJAxKXgOIBhwehl0HkCzilCfeX1m/cOzKZ+5L7YubNdFN3jEydOSl8SDx4UrTet3CpZ7CHJn4HAisIayZc4sOxmpuNEOGKfPPf7ea/+tuCpb/2mAUAAVI/fWSdOnORKgtafenKOmqEvM+QBAXF9/oH00E+IGtWVPchYh8XAwKHGnx4b86NfVQB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A3OjE1LS0zMTowMPcJvWgAAAAmdEVYdGRhdGU6bW9kaWZ5ADIwMTUtMDktMDlUMTU6MDc6MTUtLTMxOjAwNa8fdwAAAABJRU5ErkJggg==

Can you please point me in the direction of the docs, i want to see how i can write this with just OS.File.writeAtomic, as I was able to that in places like here:

http://stackoverflow.com/questions/25585359/how-to-xhr-local-files/25585841#25585841

With simply: var promised = OS.File.writeAtomic(file, new Uint8Array(data));

@Noitidart
Copy link
Author

I was trying to write my own function like getFile, but I was trying to do getFileArrBuf but it seems all postMessage as JSON.stringify'ed is this true? is there anyway that I can transfer the byte array? As documented here on mdn: https://developer.mozilla.org/en-US/docs/Web/API/Worker/postMessage#Transfer_Example

@Noitidart
Copy link
Author

This is my attempt to add a getFileArrBuff function: Noitidart/Icon-Container-Generator@d12f5e5

Still working on it, if you can help i would sooo appreciate it, my biggest goal is to try to get it to transfer the byte array rather then JSON serializing it, as transfering byte array is super good for memory.

@Noitidart
Copy link
Author

Ok veryyy interesting, I think i got the arrbuf transfering, please see:

Noitidart/Icon-Container-Generator@9855dd4

However when I write conver the file to .png or to .jpeg i dont see a difference in the written file:
https://github.com/Noitidart/Icon-Container-Generator/blob/9855dd40fbc3b90ffb672c25d14dfdce2b14a806/modules/workers/ICGenWorker.js#L43-L46

i currently do:

  convert.allDone().then(function() {
    convert.run('-rotate', '90', '/Image-Box-64.png', '/Image-Box-64-rot90.png').then(function() {
      convert.getFileArrBuf('Image-Box-64-rot90.png').then(function(real_contents) {
          OS.File.writeAtomic(OS.Path.join(OS.Constants.Path.desktopDir, 'Image-Box-64-TAT.png'), new Uint8Array(real_contents.buf), { tmpPath: OS.Path.join(OS.Constants.Path.desktopDir, 'Image-Box-64-rot-jpeg.txt.tmp') });
      });

but if i change the occourances of Image-Box-64-rot90.png to Image-Box-64-rot90.jpeg and also the writeAtomic to .jpeg i see no difference in file size at all. Is it really a different file type? the chunked data shows PNG at the start no matter if its .jpeg or .png:

this is where i console.logged:

  convert.allDone().then(function() {
    convert.run('-rotate', '90', '/Image-Box-64.png', '/Image-Box-64-rot90.png').then(function() {
      convert.getFile('Image-Box-64-rot90.png').then(function(real_contents) {
          console.log('real_contents:', real_contents);
          OS.File.writeAtomic(OS.Path.join(OS.Constants.Path.desktopDir, 'Image-Box-64-TAT.png'), new Uint8Array(real_contents.buf), { tmpPath: OS.Path.join(OS.Constants.Path.desktopDir, 'Image-Box-64-rot-jpeg.txt.tmp') });
      });

@Noitidart
Copy link
Author

Man so I'm shifting to production style: Noitidart/Icon-Container-Generator@f8fd62a

I do the on load here:

https://github.com/Noitidart/Icon-Container-Generator/blob/trying-unixToolbox/modules/workers/ICGenWorker.js#L96-L112

That triggers the allDone() correctly.

Then in my function later on:

allDone() is never triggering:

https://github.com/Noitidart/Icon-Container-Generator/blob/trying-unixToolbox/modules/workers/ICGenWorker.js#L208-L233

It did a

 gConvert.addUrl("file:///C:/Users/Vayeate/Desktop/disk.png", "/base0")

And I was hoping that allDone() would fire

Also is there somehwere to learn the syntax, like im not sure how to do identify width height, scale and overlap, im not sure what arguments to use etc.

@Noitidart
Copy link
Author

I seem to get the allDone() to work. Every time in the allDone() what I am doing is doing gConvert.promises = [];

not sure if tahts the right thing to do.

Also is there a an opposite to addURL/addData like removeByPsuedoName?

Also its ok that i dont give my pseudoName's an extension right?

@Noitidart
Copy link
Author

I also don't understand this pseudoName business

 gConvert.addUrl('myFilePathHere', '/', 'badge' + i);

I thought the 2nd arg was the path to the pseudoDirectory, and then final is the pseudoName? this way doesnt seem to work. are there any docs on this?

I also can't figure out how to use identify to get the file types and if image or not, and if image the height/width. Man sorry about this im real excited to use this but cant get it working with docs :(

This is what i tried:

gConvert.addUrl('file:///C:/Users/Vayeate/Desktop/disk.png', '/base0');

gConvert.allDone().then(function() {
        gConvert.run('-identify', 'base0').then(function() {
            console.error('args:', JSON.stringify(arguments));
        });
});

It tells me:

Magick: no images defined `/base0' @ error/convert.c/ConvertImageCommand/3066.

I also tried with full path:

Magick: no images defined `file:///C:/Users/Vayeate/Desktop/disk.png' @ error/convert.c/ConvertImageCommand/3066.

@Noitidart
Copy link
Author

So this is my thoughts, because my need is to load an array of images then:

  1. goal is to make output of png's square sizes: [x, y, z]
  2. figure out the width and height of loaded images (use identify)
  3. choose best image closest to x y z and scale them (use convert)
  4. overlay a badge on these images (use composite)

so does this mean i have to put in identify-worker, convert-worker, and composite-worker? and then add the same urls to each one of them? is there anyway to just addUrl to a single one of them so i dont have to load them again in the other two?

Here is a screencast of what I am doing on the main thread right now: I want to move it into a worker: Youtube :: Icon Container Generator

@Noitidart
Copy link
Author

Hey @manuels forgive the bother, but if you are not available to chat this much is there a community/forum or something where I can chat to the users about this? I was looking for the best way to use these 3 workers as a single worker, without adding them all as then im up to 60mb, or maybe if theres a way to minify to remove the functions I dont use (overlap, scale, size)?

@manuels
Copy link
Owner

manuels commented Sep 11, 2015

maybe, gzipping the files is an option?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants