Advertisement

REALLY nasty array problem - Need urgent help

Started by May 21, 2000 02:54 AM
11 comments, last by TheGecko 24 years, 6 months ago
Ok bear with me while I try to explain this problem to you. I'm working on compression schemes and one of them requires that I build a color table for all the RGB pixels in a video.What happenes is that my program reads my entire file 3 bytes at a time (one for red,one for green and one for blue) and builds a table to accomodate that color.Take this example: in my file I have a 3x3 24bit bitmap: 0 0 0 125 145 3 1 1 1 1 1 1 0 0 0 44 75 13 1 1 1 250 78 87 0 0 0 (each number represents an R,G or B value) Ok now what my application does is it reads the first 3 values (0,0,0) and checks to see if they are in the table or not.Now since this is the first set of values read,there is obviously nothing in that table so it creates an index 0 and places the values (0,0,0) into it.Next it reads the next 3 (124,145,3) and checks the table again.It places is into the table since it isn't found.And this keeps going till all the data has been read.So our final table should look something like this: Index.........value ------------------- 0.............0,0,0 1.............124,145,3 2.............1,1,1 3.............44,75,13 4.............250,78,87 and the final compressed buffer will be: 0 1 2 2 0 3 2 4 0 Get it? Ok now my dilemma,I cannot tell before hand how many elements are going to be in my table.Is there any way I can create an array and keep adding to it without knowing it's final size? this is a serious problem that has me stumped.Don't ask me to take the worst case scenario.I the case of a 24 bit color video or bitmap,there are 16million colors and I'm NOT going to create an array with 16million elements. So,there you have it.If you guys are a little unclear as to what I'm getting at then post here and I'll get back to you. ----------------- www.DigitalSV.com Edited by - TheGecko on 5/21/00 2:58:08 AM
Create an array which has got as much elements as the picture has got pixels. You could also allow only less than that and test if there's an array overflow, because if you need as much elements as pixels in the picture, you can't compress the image.

Visit our homepage: www.rarebyte.de.st

GA

Edited by - ga on May 21, 2000 6:25:16 AM
Visit our homepage: www.rarebyte.de.stGA
Advertisement
I don''t know if this is what you want. But how about creating 2 arrays. 1 being a temperory array and the other, the array in which you store your compressed buffer.

When your program begins, allocate the array with enough space for holding, say 256 entries (you decide, you''d know better). Have a counter that keeps track of the number of entries in the array that are used. Once the array is full, copy the contents into the temperory array, resize the array by another say,256 elements (so it now can hold 512 elements) and copy all the elements back into the array from the temperory array.Then repeat the process.

I don''t know if this is what you''d want. But I do know that this is not going to win any award for speed.



========================================================
If something sounds stupid but works, it's not stupid
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
You might want to try this:

class MyArray{   MyArray(){Next = 0};   char Colors[256][3];   MyArray * Next;}<\pre>whenever you fill the array, you create a new MyArray object, making Next point to it. Remember, whenever you go through 256 elements on an object, you have to get the next object and continue from there.    
Sounds like the perfect job for an STL vector

std::vector v;
v.push_back(1);
v.push_back(2);
v.push_back(3);

// v contains 1 2 3
// v.size(); gives you amount of element : here 3
v.erase(v.begin() + 1);
// v contains 1 3
// v size now gives 2

Hope this helps





LOL! This is the 5th messageboard where the STL vector solution shows itself.Thank you all for your ideas.I now firmly believe that Vectors are the solution (just because it was suggested on other messageboards!)

But I have NO CLUE about STL or Vectors for that matter. Anyone got any links to the topic?

-----------------
www.DigitalSV.com
Advertisement
why not just execute a function that ''compiles'' the bitmap once and have it return the number of colors.

then dynamically create an array with the return value and load the bitmap into the array.
___________________________Freeware development:ruinedsoft.com
Got it working using Vectors! That MSDN CD isn''t so useless after all! Thanx for all the replies.

To answer your question iwasbiggs,I will still need to have an array to compare and see the colors that I compile (<--was that even in proper english?) so the same problem still remains.But I solved all this using vectors and my life is all normal again.Thanx to all those that helped

-----------------
www.DigitalSV.com
Just a little note about efficiency:
You''re doing a linear search of the data every time you pick up a new value from your file. If your file is very long, this could be costly.

You might want to look at using a sorted container and see if it improves your access speed, probably an STL set (std::set) in your case. The overhead is higher with a set, but searches are of logarithmic complexity instead of linear (i.e. faster).

If you have the time, I''d suggest doing it both ways and then running some tests to experimentally check which is faster.
quote: Ok now my dilemma,I cannot tell before hand how many elements are going to be in my table. Is there any way I can create an array and keep adding to it without knowing it''s final size?


I was suprised that no one mentioned Dynamic Arrays.

This topic is closed to new replies.

Advertisement