Hi all:
As the title states, I need to know how to convert Allegro *BITMAP
to char, so I can store the chars into my buffer, char name[50].
Can someone help me on how to do this? I have search the internet but
have not found how this can be done. If you need more info, let me know.
Thanks for your time.
I'm guessing since you reference BITMAP and not ALLEGRO_BITMAP that you mean A4.
If so, then this should hopefully help: https://www.allegro.cc/manual/4/api/direct-access-to-video-memory/
Hi LennyLen:
No, I am using allegro5. Sorry, I did not mean to mislead anyone.
I should have stated that fact. Hear is why I think this is what I
need to do:
Right click image in file manager.
I then string copy argv[1] into my char name[100].
My image loads and displays on screen.
I am able to modify image.
I then save image to "sample.png".
Everything is fine.
Now my original char name[100] is no longer valid.
But if there was a way to update the name buffer with
the new info, I would be very happy!
Is this possible or have I completely lost my mind?
Thanks for your reply, have a great day!
Your original post said to put an allegro bitmap into char buffer.
Now you just want to know how to modify a char buffer?
strcpy_s(my_buffer, size of my buffer, new name);
Hi Daniel:
You are correct. I had a name to begin with. It was stored in argv[1].
Now I have no name. All I have now is a modified bitmap. There is no name
associated with this bitmap. If I try to associate a name I would create
a seg fault because I can't copy *bitmap into a char buffer.
Am i missing something here? Please explain.
Learn the difference between a pointer and an array. char[100] is an array. char* is a pointer. If you want to copy into a char buffer fine, declare an array of char large enough to hold + 1 for the null or allocate memory for it. Then use strncpy to copy it into your char buffer.
Hi Edgar:
Yeah, I already know the difference. If I read you correctly, you are
saying I need to save it like I would save to a .png file except to a
buffer instead! Is this correct? If so, I already do this to a file, but
I end up saving the complete backbuffer. If I have it in the buffer, I
can save only the image. Kind of crazy I guess, but that is what I got.
Saving to a file, I can do, but saving to a buffer I will have to do some
research. Never done that before. Thanks for the tip, learn something
new every day! I will mark this closed! Thanks again!
So you do want to save a bitmap to a char buffer. I am thoroughly confused.
It's not so easy.
The ALLEGRO_BITMAP pointer doesn't just point to the ALLEGRO_BITMAP struct. There are internal data pointers as well that point somewhere else.
It is doable.
Hardest part would be calculating the buffer size needed?
Take a look at the struct.
You could also look at the register save and load functions. Make your own that loads/saves to/from a char buffer.
After all that, I don't know why you would want to.
EDIT:
I'm still confused. Do you need to save the filename of the bitmap or the bitmap itself to a buffer?
>EDIT:
>I'm still confused. Do you need to save the filename of the bitmap or the bitmap
>itself to a buffer?
I wonder that also. Please be very clear what you want, and array of rgb stored in char, or a string name to save the file as?
Edgar and Daniel:
Sorry, I guess I am going at this the wrong way. I think I can clear
this up:
I am now in my file manager, I right click on an image, image shows on screen.
Everything is fine. I take the first argument argv[1] and strcpy into my char
name[100] buffer. I then save to "sample,png" file. I then open my file manager
and I see the file ONLY. Bear with me now. Code below:
if(lp == true); { // saving DEFAULT image al_set_target_bitmap(temp_image); temp_image = al_load_bitmap(name); al_save_bitmap("sample.png", temp_image); al_destroy_bitmap(temp_image); }
Notice the (name) buffer called. Everything is still fine. I am happy!!!
Now I want to modify that image. Buffer called name[] is no longer valid,
Now I save the modified image. Code below:
if(rt == true || mr == true || dt == true) { // saving ROTATED OR MIRROR images temp_image = al_create_bitmap(screen_width, working_screen_height); al_set_target_bitmap(temp_image); al_draw_bitmap(al_get_backbuffer(display), 0, 0, 0); al_set_target_backbuffer(display); al_save_bitmap("sample.png", temp_image); al_destroy_bitmap(temp_image); } Sorry, the code ran over to the next line.
I go back to the file manager. I see the "sample.png" file but I also see the
complete backbuffer. I am NOT happy!!! The first thing you are going to say
is 'That is what you ask for'. Right, because if I had ask for image_width and
image_height, I would have gotten pure garbage. Now, if I had the name buffer
updated I could have used the first code I posted and I would be happy again!
Either the name[] buffer needs to be updated or my second code needs to change.
I hope it is clear now. Thanks again for your time.
You modify the image, name hasn't changed since you set it to argv[1]
What do you want name to be if image is modified?
First code
1. set temporary_bitmap to target. Why this line and what is temp_image at this point?
2. load temporary with filename from name buffer
3. save temporary_as 'sample.png'
4. delete temporary
Second code
1. create a temporary bitmap size of display
2. set temporary as target
3. draw display back buffer to temporary
4. save temporary as 'sample.png'
5. destroy temporary
So, what's the problem? Why do you say name[] is invalid if image is modified.
also, use a bitmap to keep your bitmap in and don't use the display's back buffer. Do you need to save the entire display? OR just the bitmap you modified?
Maybe if you explain a bit what your app does.
Hi Daniel:
I want you to do me a favor. Go back and read the very first line of your
last post. That is exactly my problem. If I bring up image representing argv[1]
and I modify that image, argv[1] does NOT exist any longer at this sitting. So,
what I want is name[] buffer updated to reflect the changes to argv[1]. Then
and only then can I use the first code to save the image and get only the image
without the complete backbuffer. If I try to save argv[1] after the modification
I will receive the original argv[1], NOT what I want. I hope this helps.
You said you had a char buffer called name.
At the start of your program, copy argv[1] to name. No need to ever use argv after that.
If you modify your image, modify name.
Here's and app that loads a bitmap from argv[1], modifies it, and save
Left mouse down: puts a black dot
F2: saves image to sample.png
Escape: exits
It's a matter of shallow vs deep copy. If you just point to argv[1], any changes to it will change argv[1]. Just make a char buffer and copy the new name into it.
You could also add a function to load a different bitmap and modify name. If you had some sort of fileloader dialog or whatever.
Hi Daniel and Edgar:
Well it looks like I am stuck with what I have. Any changes I make I get
kick right back to codeblocks. I ran your program Daniel and it ran fine with
some minor changes. I also investigated what Edgar posted. I am a little concerned about what happened. He talked like the argv[1] would be alive for the
complete sitting. I found that not to be the case. I rotated the bitmap
and all went well except when I tried to copy argv[1] to another buffer. Back
to codeblocks I went in a flash. I hope Edgar is not upset with what I said. I
would not question him at all. Both of you know more about this than I do. I
am just saying what I found on my machine. My program runs fine I already have
it in my Linux system. I click on an image and the image is on the screen. I might look into what you just posted about loading a new file from the disk.
Thanks to both of you for your help!
Are you changing argv in your code? While it is not constant, if you don't modify it, then it will always point to the same array of constant strings. Also, it will persist throughout runtime, You shouldn't have any program accessing it at any point (inside of scope). If outside of scope, then make a buffer.
char name[256] = ""; // valid throughout source file int main(int argc, char** argv) // valid only inside main. { }
Unless your modifying it.
Also, can post code?
Hi Daniel:
I am sure the problem in losing argv[1] is because I leave main(). The best I
can do to get what I want is a complete rewrite of the program. It was a very
expensive lesson to learn. I go back and forth out of main() all the time. The
main problem is having to save the complete backbuffer after changing argv[1].
It is too big of a problem to try to change what I have now! I hate to give it up
because it is the only image viewer I have. I will start Monday to rewrite what
I have now keeping in mind saving argv[1] all through the program.
This is probably a stupid suggestion and not applicable, but is there any way to declare argv[1] static? I know when you want to keep a value in a variable for when you re-enter a function, you can make it static and value does not reinitialize.
static int count = 0;
Just thinking out loud.
Or maybe write the name to global variable or file? Again, just shooting in the dark.
Once again, why and huh?
Reenter function?
They are already static because they will only ever be alive while your program is running. Which is the entire time. Just because you jump to other code and back doesn't change that.
Hi Daniel:
Hey, I lied to you! I didn't wait till next week, I rewrote enough to test
out the life of argv[1]. OK, I am inside of main the whole time. I know what
you and Edgar said about staying within main and I did. The name buffer is
still not updating. In fact, the program is worse than before the rewrite. I
don't doubt what you are saying. I would never do that. It is not what I see
on my end. The only time I am able to save ONLY the image is when I bring up
the image and save without any changes. I guess I have completed this project.
Three months is enough time to spend on it. It works and works fine and I am
happy I got this far. Time to move on to something else. Thanks for the help!
Don't give up.
The name buffer is still not updating
Are you not updating the name buffer? It won't magically change unless you change it.
Did you create a name buffer or are you only using argv[1]?
I'm still confused on what you are doing or not doing or what you are trying to accomplish.