The problem is that I am trying to modify the code to invert the image in C and optimize it to make it run faster than the original by any method I could think of. After tried to use blocking and loop unrolling, it still does not seem to speed up. Maybe I did it in a wrong way as I'm not really fully understanding these methods.
void invert(int width, int height, char *code)
{
int x, y, z;
int i, n;
char RGB[4]="RGB";
for (z=0; z<3; z++)
for (y=0; y<width; y++)
for (x=0; x<height; x++)
{
b[x][y][z] = a[x][y][z];
n = strlen(code);
for(i=0; i<n; i++)
{
if (code[i]==RGB[z])
{
b[x][y][z] = 255 - a[x][y][z];
}
}
}
return;
}
void invert_optimized(int width, int height, char *code)
{
int x, y, z , xx , yy;
int i, n;
char RGB[4]="RGB";
for (z=0; z<3; z++)
for (xx=0; xx<height; xx+=4)
for (yy=0; yy<width; yy+=4)
for (x=xx; x<4+xx; x++)
for (y=yy; y<4+yy; y++)
{
c[x][y][0] = a[x][y][0];
c[x][y][1] = a[x][y][1];
c[x][y][2] = a[x][y][2];
n = strlen(code);
for(i=0; i<n; i++)
{
if (code[i]==RGB[z])
{
c[x][y][z] = 255 - a[x][y][z];
}
}
}
return;
}
I also was thinking about inline
, but the problem is that I don't know how to implement it for this code and I don't think it will work for this either.
a
? What is in the stringcode
? My best guess is thata
is array of size[height][width][3]
with 8-bit RGB values, and thatcode
is a string consisting of which of theRGB
values to invert. Is that correct? \$\endgroup\$