Hello Everyone,

I recently came up with a really nice little piece of code that allows me to merge images quickly (by merging I mean fade in and out). The only problem is, the tests worked fine on small scale (like small images), but once I started to go larger, my resources quickly become consumed and this nice little process wasn't so nice anymore

Anyway, to do this index through two byte arrays, and merge them, which some math, and add them to a list. This is currently how I perform the task

for (int i = 0; i < FrontImageBytes.Count; i++)
    NewImage.Add((byte) (((int) (FrontPercent * FrontImageBytes [i])) + ((int) (BackPercent * BackImageBytes [i]))));

Again this works fine on small scale, but one in large scale, it's horrible.

My question to you then is how can I improve the performance of indexing through two byte arrays, merging them while applying my math formula to them? I am thinking something along streams, but I have no clue.

Hope this makes sense (kind of whipped this up as I am in a hurry)

Recommended Answers

All 3 Replies

You don't specifically say but I assume the final image is the same size as either of the 2 images being merged. If so, instead of creating a new image, try overwriting the front image instead, that should cut down on the resources that are used:

for (int i = 0; i < FrontImageBytes.Count; i++)
    FrontImageBytes[i] = ((byte) (((int) (FrontPercent * FrontImageBytes [i])) + ((int) (BackPercent * BackImageBytes [i]))));

If you need to keep the front image, you could write it to a file first.

Yes I should have pointed out that both images are the same size, and have been configured so that their Bits Per Pixel values match as well.

I believe I tried the above, and while it did somewhat help, I still wasn't able to improve performance much (I think the problem is it's indexing through so many indexes ... I am talking millions, it's taxing the system).

I wish there was something like LINQ where I could simply go "all these bytes now equal that". But every attempt I have tried (I Have tried creating dictionaries of the values, so the math formula only has to be done once per cycle ... sadly it's slower).

I plan to try another theory, of creating a string of the values, doing a find/replace concept, then taking the results and creating the byte array out of them. But I fear I'll still be suffering performance (I ran this with images around 1920x1080, with 3 threads, each having it's own, I was running over 300+ MB of RAM, and 35% of my CPU)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.