|
How to make a copy of a bmap?
#367476
04/13/11 22:13
04/13/11 22:13
|
Joined: Aug 2005
Posts: 238
Caermundh
OP
Member
|
OP
Member
Joined: Aug 2005
Posts: 238
|
I was wondering if its possible or if there is an easy way of creating a copy of a bmap? What I would like to do is something like:
BMAP* my_bmap;
BMAP* my_copy;
my_bmap = bmap_create("my_file.jpg");
my_copy = bmap_copy(my_bmap);
I know there is no bmap_copy() command, but the idea is to create a copy of a given bmap - so i would have 2 seperate bmaps that are the same image. Is that possible?
|
|
|
Re: How to make a copy of a bmap?
[Re: Caermundh]
#367480
04/13/11 22:58
04/13/11 22:58
|
Joined: Oct 2007
Posts: 5,210 İstanbul, Turkey
Quad
Senior Expert
|
Senior Expert
Joined: Oct 2007
Posts: 5,210
İstanbul, Turkey
|
what about: my_bmap = bmap_create("my_file.jpg"); my_copy = bmap_create("my_file.jpg");
3333333333
|
|
|
Re: How to make a copy of a bmap?
[Re: Quad]
#367481
04/13/11 23:07
04/13/11 23:07
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
This should do the trick
BMAP *bmap_copy(BMAP *source)
{
if(!source)
return NULL; // Oh look, invalid data...
var width, height, format, bitDepth;
BMAP *result; // The resulting bitmap
width = bmap_width(source); // Get the width
height = bmap_height(source); // and height of the source bitmap
format = bmap_lock(source, 0); // Lock the source bitmap
// Get the bitdepth of the source
// Only supported by the pixel functions is 16, 24 and 32 bit, so we don't care about the rest
switch(format)
{
case 88:
case 565:
bitDepth = 16;
break;
case 888:
bitDepth = 24;
break;
case 8888:
bitDepth = 32;
break;
default:
bmap_unlock(source);
return NULL; // Well, the bitmap isn't in a format we can use
break;
}
// Create a new, void, bitmap with the exact same size and bit depth
result = bmap_createblack(width, height, bitDepth);
if(result)
{
int x, y;
bmap_lock(result, 0); // Lock the new bitmap so that we can write into it
// Copy all pixels by iterating over each source pixel and storing it in the copy
for(x=0; x<width; x++)
{
for(y=0; y<height; y++)
{
var pixel = pixel_for_bmap(source, x, y);
pixel_to_bmap(result, x, y, pixel);
}
}
bmap_unlock(result); // Unlock the previously locked copy
}
bmap_unlock(source); // Unlock the source
return result; // Return the result, this might be NULL if the bmap_createblack() failed
}
Please remark: The code isn't tested as I don't have Gamestudio, but it should work.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: How to make a copy of a bmap?
[Re: Caermundh]
#367483
04/13/11 23:15
04/13/11 23:15
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
Uh well, try it? I can't answer this for you. Just let the function run a few thousand times with a large bmap and look how long it took.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: How to make a copy of a bmap?
[Re: WretchedSid]
#367484
04/13/11 23:23
04/13/11 23:23
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
Expert
|
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
If you want it faster, try using bmap_process and a shader that just looks up the right input pixel.
Jibb
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: How to make a copy of a bmap?
[Re: JibbSmart]
#367485
04/13/11 23:28
04/13/11 23:28
|
Joined: Jul 2008
Posts: 1,178 England
MrGuest
Serious User
|
Serious User
Joined: Jul 2008
Posts: 1,178
England
|
fastest way i can think of
BMAP *bmap_clone(BMAP* bmp){
if(bmp){
BMAP* bmpNew = malloc(sizeof(BMAP));
memcpy(bmpNew, bmp, sizeof(BMAP));
return(bmpNew);
}else{
return(NULL);
}
}
and to prove it's working, have a bmap called "bmap_to_copy.bmp"
///////////////////////////////
#include <acknex.h>
#include <default.c>
///////////////////////////////
ENTITY* e1;
BMAP* bmp_original = "bmap_to_copy.bmp";
BMAP* bmp_clone;
PANEL* pnl_original = {
pos_x = 0;
flags = SHOW;
}
PANEL* pnl_clone = {
pos_x = 64;
flags = SHOW;
}
void bmap_cross(){
BMAP* tgablitz = pnl_original.bmap;
var format;
var pixel;
format = bmap_lock(tgablitz,0);
if (format >= 565) {
pixel = pixel_for_vec(vector(0,0,255),100,format); // red color
pixel_to_bmap(tgablitz,10,10,pixel);
pixel_to_bmap(tgablitz,10,11,pixel);
pixel_to_bmap(tgablitz,10,12,pixel);
pixel_to_bmap(tgablitz,10,13,pixel);
pixel_to_bmap(tgablitz,10,14,pixel);
pixel_to_bmap(tgablitz,8,12,pixel);
pixel_to_bmap(tgablitz,9,12,pixel);
pixel_to_bmap(tgablitz,11,12,pixel);
pixel_to_bmap(tgablitz,12,12,pixel);
}
bmap_unlock(tgablitz);
}
BMAP *bmap_clone(BMAP* bmp){
BMAP* bmpNew = malloc(sizeof(BMAP));
memcpy(bmpNew, bmp, sizeof(BMAP));
return(bmpNew);
}
void main(){
wait(1);
bmp_clone = bmap_clone(bmp_original);
pnl_original.bmap = bmp_original;
pnl_clone.bmap = bmp_clone;
bmap_cross();
}
|
|
|
Re: How to make a copy of a bmap?
[Re: MrGuest]
#367488
04/14/11 01:41
04/14/11 01:41
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
On the one hand this is a bit of genius. On the other hand I think it's quite a bit dangerous code as it creates an exact copy of the bitmap. In other words there are now two engine objects with the same handle. I don't know if that is a good idea. I'd go with JustSids solution. Maybe change the for loop copying the pixel data to a single call of bmap_blit. Speed isn't that important in this case anyways as you shouldn't create hundreds of bitmap copies.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: How to make a copy of a bmap?
[Re: Uhrwerk]
#367489
04/14/11 02:01
04/14/11 02:01
|
Joined: Jun 2004
Posts: 2,234 Wisconsin USA
FoxHound
Expert
|
Expert
Joined: Jun 2004
Posts: 2,234
Wisconsin USA
|
I'm looking at the code and they would not have the same handle just the same texture. I can test this for sure when I get home but this looks good.
--------------------- There is no signature here.
QUIT LOOKING FOR ONE!
|
|
|
Re: How to make a copy of a bmap?
[Re: FoxHound]
#367490
04/14/11 02:13
04/14/11 02:13
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
They would have the same handle. Have a look at atypes.h if you don't believe me. They would even share the same pixel data and texture buffer. So if you manipulated one of them the changes would affect all copies created with mem_cpy.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
|