PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

copying pictures around,

Hi all, I'm using the EasyCE code, and I've come up with the following code to copy one picture (smaller) inside of another (larger), my question is can it be done faster?, I know it can be for non-trans as you can use memcpy....?:

Code:

void place_item_trans(int destsizex, int xpos, int ypos, int xsize, int ysize, int number_items, int item, int transcolor, PIXEL* src, PIXEL* dest)
//
//destsizex: is the number of "pixels" in the destination buffer
//xpos, ypos: the destination location of picture
//sizex, sizey: the sources' picture size in pixels.
//number_items: if the source is a picture array, the number of them,
//item: the tile to retrieve 0 to number_items-1;
//transcolor: the trans color in the tile.
//src, dest: pointers to the buffers....
{
for (int y = 0; y < ysize; y++)
{
for (int x = 0; x < xsize; x++)
{
if (src[xsize * number_items * y + (item * xsize) + x] != transcolor)
dest[destsizex * (y + ypos) + xpos + x] = src[xsize * number_items * y + (item * xsize) + x];
}
}
}
brendan · 01 Feb 2002, 01:10 UTC — BTW
BTW, incase your wondering why the tiles are horozontal and not vertical, which tends to go against the "easyce" buffers etc and moke the code worse, it's because I'm using an external level designer, and it places the tiles together horozontally.....bugger :(

-brendan
Hello. It's one of those slow days @ work :cry: .

I'm looking at the code, not the algorithm here.
It has *not* been compiled, or tested for validity - I'll let you do that.

Generally, to optimise loops, first you need to work out:
what CHANGES in the loop
and thus,
what can be moved OUT of the loop
In this case, all that's changing is x and y, and even then by an increment of 1.

So, we can replace any multiplies by y with an addition in the x loop.

Also, we can precalculate a LOT of the other data in this case. We can get down to performing NO multiplies in the main loop (multiplies are generally slower than additions).

Code:
void place_item_trans(int destsizex, int xpos, int ypos, int xsize, int ysize, int number_items, int item, int transcolor, PIXEL* src, PIXEL* dest)
//
//destsizex: is the number of "pixels" in the destination buffer
//xpos, ypos: the destination location of picture
//sizex, sizey: the sources' picture size in pixels.
//number_items: if the source is a picture array, the number of them,
//item: the tile to retrieve 0 to number_items-1;
//transcolor: the trans color in the tile.
//src, dest: pointers to the buffers....
{
; These datatypes may need to be a long
unsigned int l_vertical_base;
unsigned int l_x_mult;
unsigned int l_xy_mult;

; moved multiplies out of inner loop
l_vertical_base = (destsizex * ypos) + xpos;
l_x_mult = (xsize*number_items);
l_xy_mult = (item * xsize);

for (int y = 0; y < ysize; y++)
{
for (int x = 0; x < xsize; x++)
{
if (src[l_xy_mult + x] != transcolor)
dest[l_vertical_base + x] = src[l_xy_mult + x];
}

l_vertical_base += destsizex; move down a row
l_xy_mult+=l_x_mult;

}
}


Hope that helps, cheers,

Refractor
brendan · 03 Feb 2002, 23:02 UTC — almost
aside from the ;'s for comments it's fine, my question was more in regard to the if statement, is there a way of "or" memcpy()ing the data, etc, somthing thats fast, to bypass the if.....

:)
For an ORing memcpy (unoptimized, probably could use assembler for more speed), use something like the following:

Code:

void memcopyor(char* dst, char* src, int count)
{
do
{
*dst++ |= *src++;
} while (count--);
}


replace the "|=" with "^=" for exclusive-or, etc.

To do a masked blit like this, first "and" the background with a mask (black where not transparent). Then "or" (or xor) the sprite with the background. The sprite has black in the transparent areas.

As far as speed benefits, I haven't done any tests, and I'm not sure how much the various PPC processors gain from removing the "if"s. I don't think the code would be significantly faster on the ARM at least, since assembly instructions can be executed conditionally:
Code:

CMP r0, 0
OR.EQ r0, r1

instead of
Code:

CMP r0, 0
JNE notequal
OR r0, r1
notequal:

Note that these examples aren' actually valid ARM assebmbly, but they provide a reasonably clear example of how the code would probably compile on the arm.

The SH3 and MIPS processors don't have this feature, so you might want to blit this way anyway to help these processors, since they need more help than the ARM.