-
Notifications
You must be signed in to change notification settings - Fork 212
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
Performance [suggestion] #30
Comments
Great idea, @eltonmesquita. I'll find some time soon to add in the suggestion. |
We should give some thought to the implementation approach perhaps. Using the the data loop directly has some side effects. Essentially the way the data is being read is one line of pixels at a time from the top left of the image to the bottom right. Arbitrarily setting the number of pixels to skip at that location in the code will essentially lead to slices being taken from the image to provide the palette. For instance, setting the skip from 4 to 128 would mean you're reading 1 out of every 32 pixels. The slices themselves will be oriented depending on the width of the image. If your image has a width which happens to be a multiple of 32 the slice would be nice and vertical. If your width is a factor of 32 you'd get horizontal slices. In all other cases it would be some form of a diagonal slice. Another approach would be to take the center of a 3x3 (or 5x5 etc.) square of pixels each time. and go through the entire image that way. You could use a formula like (2n+1)^2 where n could be your coarseness factor. The formula ensures you always have a square with a central pixel. The approach is a little less flexible but seems more logical somehow. Thoughts? Any more approaches that we could take with this? What do you think would yield the better result for the speed? |
Sorry for the late answer. I'm interested in the center square idea but would this facilitate the providing of a "precision option"? I'm a newbie on image analysis, pardon me if I'm asking something silly. The only other alternative that I can think now is to provide an option for the script to scale down the image, so the script would have a lot less work to do. I don't know if it's a good idea but might be an option. |
That alternative is actually quite feasible as well. The DrawImage() function allows for scaling the image. Modifyling the equivalent function on RGBaster L25 to reflect some form of 'optimizer' would be trivial. Doing so has an effect on the getImageData function insofar as it returning fewer pixels for RGBaster to analyze. A quick and dirty implementation for testing and timing could look like this.
A quick test on a modest image showed the following promising results. optimizer set at 12
optimizer set at 1
I think the next steps would have to be to compare this approach with the originally proposed one to see if letting the browser do the scaling is more efficient than iterating over fewer elements in the Another thing worth checking out is the @eltonmesquita would you be willing to have a go at providing a set of images and test harnas for the different options? |
Yep, I've done some quick test and the performance gains when resizing are impressive and without much loss on the precision of the script. About the On one of my tests I saw the script run in about 40ms from the original ˜400ms on a 1082x394 image when scaled down 6 time. I even have this version with images being scaled down running live on a quite heavy traffic e-commerce and things are pretty good until now. You can see it here on the main banner setting the background based on the image's predominant color). I do have a set of about 26 images with different patterns for testing but it's highly focused for my case, so all the images have nearly the same resolution. I'll try to make a proper test case in the next week and post it here, so we can have something more general and get some proper metrics. |
Sounds promising. I’m in transit and will have further a look when I next can. A little off topic but have you considered responsive images at all? |
I ran a bunch more tests alternatively skipping 8 pixels in the loop or scaling the image down by a factor of 8. I compared that with normal runs as the library is today. I ran the same image through a dozen or so times by firing the library with script. Results Test image: 500 x 718
Based on these results I ran some further tests with a 3264 × 2448 image. There's no comparison. Clearly the DrawImage() scaling wins out in terms of efficiency. What I do find odd though is that scaling the image is faster than drawing the image normally. |
Hello @AlfredJKwack ! I think a test isn't needed anymore, right? ;D
Yeah... That's weird. But browser engines are so well optimized these days that it doesn't come much as a surprise. About this:
Yeah, but the platform used is a legacy one that doesn't offer much control of the HTML, so I had to deal with some major hacks to load a dedicated image for large screen and another one for small screen. Currently it's not possible to use responsive images on this platform, unfortunately. :( |
Hello!
Recently I've used RGBaster in a project and had some performance issues. In my case, the images processed were a bit big and I couldn't resize them because that would brake the layout.
As I dug deep debugging the page trying to find ways to optimize the script I've changed the image data loop to iterate skipping from 4 to 128 pixels and obviously, got a considerable increase in the performance. No more long frames and in my case no impact on the results.
My suggestion is to provide a way to decrease precision so we can gain on performance when needed. It might help a lot in some cases and be of great help for other developers.
If needed, I can provide some measurements and some examples for testing purpose.
The text was updated successfully, but these errors were encountered: