Hello everyone,
I am looking to convert a sub bitmap to a byte array to save into an SQLite database. I have done it in .NET but I am having a lot of trouble doing it in C++. What I have now looks something like this:
size_t *size = new size_t();
ALLEGRO_FILE *SavFil = malloc(size);
al_save_bitmap_f(SavFil, ".bmp", al_create_sub_bitmap(World, x, y, 16, 16));
but that is totally wrong. How do I access the byte info inside of ALLEGRO_FILE?
do I need to use ALLEGRO_FILE_INTERFACE to access that information?
Thanks for the help!
You will need to use the memfile addon and al_open_memfile()
Thanks for the quick reply! I am trying to use the memfile, here is what I have:
SubBmp = al_create_sub_bitmap(World, x, y, 16, 16);
al_draw_bitmap(SubBmp, 0,0,NULL);
al_flip_display();
data_size = al_get_bitmap_width(SubBmp) * al_get_bitmap_height(SubBmp) * al_get_pixel_size(al_get_bitmap_format(SubBmp));
ALLEGRO_BITMAP *bmp = al_create_sub_bitmap(World, x, y, 16, 16);
data = (char*)calloc(1, data_size);
ALLEGRO_FILE *memfile = al_open_memfile(buffer, data_size, "w+b");
bool result = al_save_bitmap_f(memfile, ".bmp", SubBmp);
size_t bytes = al_fread(memfile, buffer, data_size);
//Test if buffer has bitmap array
FILE *fp;
fp=fopen("c:\\test.bin", "wb");
fwrite(buffer, sizeof(buffer[0]), sizeof(buffer)/sizeof(buffer[0]), fp);
al_fclose(memfile);
free(data);
Still nothing in the buffer, or at lest i don't think there is.
This also seems like the wrong way of doing it. If you guys could help me on the right path I would really appreciate it. Thanks
I wouldn't do it quite that way. It would be easier to lock the bitmap and read the data directly.
Ex :
Untested, but you get the idea. The bitmap is locked using RGBA_8888 (allegro will perform necessary conversions for you), and then a simple memcpy copies the memory into the newly allocated buffer and the function returns the buffer and stores the size in the int pointer provided to the function.
You can use <code>code goes here...</code> tags to show your code. Makes it easier to read.
Untested, but you get the idea. The bitmap is locked using RGBA_8888 (allegro will perform necessary conversions for you), and then a simple memcpy copies the memory into the newly allocated buffer and the function returns the buffer and stores the size in the int pointer provided to the function.
Depends if he wants the actual bitmap headers or not.
Well, in that case I would just save the bitmap to a temporary file on the hard drive and not mess around with a memfile. It would be much easier to get a fix on the actual size of the bitmap file.
Depends if he wants it to hit the disk or not
The point I was trying to make is that he has to know the exact layout of the file if he wants the headers and he is going to use a memfile, otherwise he doesn't know the exact size of the buffer he needs. That's why I suggested saving to disk.
In the code above, where does buffer come from? you shoyld use data to initailze the memfile and save that, I think. Also , you seem to draw to the wrong bitmap. It's hard to say without the complete code. Could you psot a complete, compilable example of what you're trying to do?
Hello, What I am trying to do is read a Large bitmap into a database.
It should be easy, but I am having more trouble than I was thinking it was.
here is my full code:
Hmmm, your coee seems to be missing the point. I have attached an example that cuts up a bitmap and saves it to files using the memfile addon. However, I think you could adapt without too much effort to saving to sqlite using the real_size and buffer.
Here is the code repeated:
Oh and here is an example of how to save a binary blob, which would be a bitmap file in your case, using sqlite3 http://www.sqlite.org/cvstrac/wiki?p=BlobExample