Skip to content

Instantly share code, notes, and snippets.

@clausecker
Created August 24, 2012 20:47
Show Gist options
  • Save clausecker/3455445 to your computer and use it in GitHub Desktop.
Save clausecker/3455445 to your computer and use it in GitHub Desktop.
Improvement for the Paeth transformation
diff --git a/image.c b/image.c
index f22fc6f..36fbe46 100644
--- a/image.c
+++ b/image.c
@@ -257,9 +257,9 @@ void image_transform_fast_rev( struct image *image )
void image_transform( struct image *image )
{
int x, y, i, width, height, aerr, berr, cerr, ia, ib, ic;
- struct pixel p;
+ struct pixel a, b, c, p;
struct pixel *pixels;
- int pr, pg, pb;
+ int px, py, pz, qx, qy, qz;
image->transform = 2;
@@ -276,35 +276,28 @@ void image_transform( struct image *image )
ib = i-width;
ic = i-1-width;
- pr = pixels[ ia ].x + pixels[ ib ].x - pixels[ ic ].x;
- pg = pixels[ ia ].y + pixels[ ib ].y - pixels[ ic ].y;
- pb = pixels[ ia ].z + pixels[ ib ].z - pixels[ ic ].z;
+ a = pixels[ ia ];
+ b = pixels[ ib ];
+ c = pixels[ ic ];
- p = pixels[ ia ];
- aerr = abs(p.x - pr);
- aerr += abs(p.y - pg);
- aerr += abs(p.z - pb);
+ px = b.x - c.x;
+ py = b.y - c.y;
+ pz = b.z - c.z;
- p = pixels[ ib ];
- berr = abs(p.x - pr);
- berr += abs(p.y - pg);
- berr += abs(p.z - pb);
-
- p = pixels[ ic ];
- cerr = abs(p.x - pr);
- cerr += abs(p.y - pg);
- cerr += abs(p.z - pb);
-
- if( ( aerr < berr ) && ( aerr < cerr ) )
- p = pixels[ ia ];
- else if( berr < cerr )
- p = pixels[ ib ];
- else
- p = pixels[ ic ];
-
- pixels[ i ].x -= p.x;
- pixels[ i ].y -= p.y;
- pixels[ i ].z -= p.z;
+ qx = a.x - c.x;
+ qy = a.y - c.y;
+ qz = a.z - c.z;
+
+ aerr = abs(px) + abs(py) + abs(pz);
+ berr = abs(qx) + abs(qy) + abs(qz);
+ cerr = abs(px + qx) + abs(py + qy) + abs(pz + qz);
+
+ if (berr < aerr) aerr = berr, a = b;
+ if (cerr < aerr) a = c;
+
+ pixels[ i ].x -= a.x;
+ pixels[ i ].y -= a.y;
+ pixels[ i ].z -= a.z;
}
i = y*width;
@@ -338,9 +331,9 @@ void image_transform( struct image *image )
void image_transform_rev( struct image *image )
{
int x, y, i, width, height, aerr, berr, cerr, ia, ib, ic;
- struct pixel p;
+ struct pixel p, a, b, c;
struct pixel *pixels;
- int pr, pg, pb;
+ int px, py, pz, qx, qy, qz;
image->transform = 0;
@@ -376,35 +369,28 @@ void image_transform_rev( struct image *image )
ib = i-width;
ic = i-1-width;
- pr = pixels[ ia ].x + pixels[ ib ].x - pixels[ ic ].x;
- pg = pixels[ ia ].y + pixels[ ib ].y - pixels[ ic ].y;
- pb = pixels[ ia ].z + pixels[ ib ].z - pixels[ ic ].z;
-
- p = pixels[ ia ];
- aerr = abs(p.x - pr);
- aerr += abs(p.y - pg);
- aerr += abs(p.z - pb);
-
- p = pixels[ ib ];
- berr = abs(p.x - pr);
- berr += abs(p.y - pg);
- berr += abs(p.z - pb);
-
- p = pixels[ ic ];
- cerr = abs(p.x - pr);
- cerr += abs(p.y - pg);
- cerr += abs(p.z - pb);
-
- if( ( aerr < berr ) && ( aerr < cerr ) )
- p = pixels[ ia ];
- else if( berr < cerr )
- p = pixels[ ib ];
- else
- p = pixels[ ic ];
-
- pixels[ i ].x += p.x;
- pixels[ i ].y += p.y;
- pixels[ i ].z += p.z;
+ a = pixels[ ia ];
+ b = pixels[ ib ];
+ c = pixels[ ic ];
+
+ px = b.x - c.x;
+ py = b.y - c.y;
+ pz = b.z - c.z;
+
+ qx = a.x - c.x;
+ qy = a.y - c.y;
+ qz = a.z - c.z;
+
+ aerr = abs(px) + abs(py) + abs(pz);
+ berr = abs(qx) + abs(qy) + abs(qz);
+ cerr = abs(px + qx) + abs(py + qy) + abs(pz + qz);
+
+ if (berr < aerr) aerr = berr, a = b;
+ if (cerr < aerr) a = c;
+
+ pixels[ i ].x += a.x;
+ pixels[ i ].y += a.y;
+ pixels[ i ].z += a.z;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment