I was thinking about somehow using a static member that all the matrix cells will be
defined by their value + the static value.

but than i thought how can i do it?
what is the easiest way of doing so without the need to run over the 10^6 cells?

thanks

Recommended Answers

All 13 Replies

Loook up memcpy()

not sure if this is what im looking for .
let me explain:
i have the following array [1,2,3,4,5]
now, i would like to add 1 to each cell
for (int i=0;i<5;i++) array[i]++;
so the final result would be [2,3,4,5,6]

is there another way of doing so?
the problem is that i have around 10^6 cells that needs to be changed

thanks

Since all the cells have different values the only way to do it is in a loop, incrementing the cell values one at a time.

Are they all incrementing values like you posted? I suspect they are not because if they were then you would not need the array.

I was thinking about somehow using a static member that all the matrix cells will be defined by their value + the static value.

This is a very interesting idea. It also is easily generalized. You could have a stack of (different) operations and apply them in order when you want to get the value of a specific element. When you want to set the value of a specific element, you'd have to go backwards and also use the inverse operations. However, that would add a small overhead to every Get / Set call that could quickly become very big (if you keep stacking operations). To solve this problem, you could perform a small number of calculations each time you Get / Set some element. You'd need to also keep track of how many elements you have calculated.

The bottom line is that, while, in the end, you have to make all the calculations, this trick allows very good real time responsiveness for even humongous arrays.

Here's a minimal example.

How about a lazy update of the cells? It really doesn't matter what the contents of a cell are until you access it for the latest value. Unless the value of a cell depends on all previous cells, you can apply the update as needed rather than en masse, and that would save a huge amount of time.

The only caveat would be an extra bit of storage necessary to flag whether the cell has its latest value or needs to be updated.

If the values do cascade somehow, you'd probably still get some performance benefits, though it would be more amortized than absolute. In that case something like r0shi's idea would work, where you apply the change to all cells dependent on the current cell retroactively or recursively depending on how difficult the cascade is to reverse.

I'm pretty sure I already responded ... weird O_o
anyway what I was saying is that my program takes a jpg image and converts it
to grayscale.
once finished i want to have a "slide bar" to move and change the darkness of the image
aka raise the blackness of all cells and lower them accordingly.

any ideas / approachs ?
as i need to present those changes infront of an audience, those changes should
be made swiftly!
or i have no choice but to go oneat a time? :<

If you use SFML, you can load your image in a sf::Image, then create a sf::Sprite using that image and play with the sprite's color property to adjust the brightness. It's a O(1) operation and it affects every pixel of your image. Maybe the library you're using also has a similar feature and you're not aware of it yet.

JPGs are not stored by pixel, so adding 1 to any value would change a lot of pixels, not lighten 1. You need to look at the JPG format more closely.

I'm pretty sure the library the OP is using (OpenCV, I presume by looking at some of his/her recent posts) takes care of that.

well still havent decided which one to use.
I'm a bit familier with SFML (still havent got opencv working)
which command were you talking about?
using sfml2 atm and didnt see any brightness modification functions
or am i missing something?

waltP i can use other formats if JPG is problematic, BMP is ok i guess?

thank you both

Have you checked how long it takes to just run through the array and add 1 to all the values? I suspect it's not that long. I was recently profiling some code that was doing arithmetic on arrays like this and the compiler-optimised, release code was running over each element and multiplying three doubles and dividing by a fourth. It only took 10 nanoseconds per element! On this scale, a million elements could be updated in 0.01 seconds. This sounds easily fast enough that the user wouldn't notice the difference.

I'm not saying you don't need to optimise your code, I'd just recommend profiling a simple solution to see if it's really a limiting step in the whole process. Sometimes raw fire-power will do the job for you :)

which command were you talking about?

sf::Sprite::setColor

By default it's set to sf::Color(255, 255, 255, 255) (opaque white).
You could set it to sf::Color(i, i, i, 255) and control i with your slidebar.

If this doesn't give you the result you want, I guess you can always try the
'brute force' approach, as ravenous suggests, if you haven't tried it yet.

hmmm saw that function but thought it was only meant for sf::Shapes to paint their inside
or something like that...
but alright ill give both methods a try ;)

thanks to you all

Be a part of the DaniWeb community

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