views:

161

answers:

3

Hi,

I have the following code:

static unsigned char S0_gif[] = {
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x0f, 0x00, 0x0f, 0x00, 0x91, 0x02,
..
};

It's a hex representation of a gif file. I have 500 gifs that I need to store like that so I want to use a vector to make it easier for access.

Something like:

vector<char[]> gifs; 
gif.push_back( {0x47, 0x49,..} );

Then in the loop:
{
MakeImage(gif[i], sizeof gif[i] );
}

I cannot find the right code for that. Any help would be greatly appreciated.

Petry

A: 

Looks like you are storing all 500 GIF files in a row. You cannot detect size of each without parsing its header. If your function MakeImage could parse GIF header you could return pointer to the next image from it.

Then the loop will look like:

char* img_ptr = S0_gif;
while ( img_ptr ) img_ptr = MakeImage( img_ptr );
Kirill V. Lyadvinsky
+1  A: 

You cant do that, because vectors store constant sized structures, and youre's are variable sized. What you can do however, is store a vector of vector :)

vector<vector<char> > gifs; // note the neccessary space between > >
gif.push_back( vector<char>( S0_gif, S0_gif + sizeof(S0_gif) )  );

Then in the loop:
{
MakeImage( gifs[i] );
}

Another idea, if they are indeed stored as static variables, is not to store the data twice:

vector< unsigned char * > gifs; 
vector< size_t > gifsizes; 
gifs.push_back( S0_gif );
gifsizes.push_back( sizeof(S0_gif) );

Then in the loop:
{
MakeImage( gifs[i], gifsizes[i] );
}

Disclaimer : I probably forgot some &'s, feel free to correct me.

Kornel Kisielewicz
Indeed I want to avoid storing data twice, so I tried your second idea but I get this MSVC2008 compilation error:error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'unsigned char (*)[819]' to 'const char *const conversion requires reinterpret_cast, C-style cast or function-style cast
Petry
A: 

I believe that the best solution is to generate a C/CPP file that declares a vector of images. All the rest means writing code, which is not generally recommended for a lot of initialization (my opinion).

unsigned char *Array[]={
S0_gif,
S1_gif,
S2_gif,
S3_gif,
...
};

The code for generating this can be easily written in a scripting language (bash, perl, python, etc). It should be something like this:

print "char *Array[]={"
for i in range(0,500)
   print "S"+i+"_gif"
print "};"

Is this a solution to your question?

Iulian Şerbănoiu
The thing is that I need to store the images inside the executable. I have the c++ code that will turn binary files into char array.
Petry
Using the C++ code to turn binary into array you can create S0_gif, S1_gif and so on. After that you use that small script to create a C/C++ array that contains all the images represented as an unsigned char array. Isn't this what you want? [Of course after creating the CPP file you simply add it into the compilation process - and this means that it will be in the final executable]
Iulian Şerbănoiu
Exactly, I need array that will contain all images represented as an unsigned char array. I tried with your first piece of code but I get this compilation error: error C2440: 'initializing' : cannot convert from 'unsigned char [819]' to 'char *'
Petry
my code is char *; use unsigned char * (I modified my post now)
Iulian Şerbănoiu
Like that it compiles fine but the image is not decoded at all. When I use only MakeImage(S0_gif, sizeof S0_gif) it works fine.<br>When I use MakeImage(Array[0], sizeof Array[0]) it doesn't.
Petry