I read the pixels of PNG files via libpng
and creates an array of alpha channels (showing visible pixels for a monochromatic image).
int pixels[height][width];
for( int i=0; i<height; i++){
for( int j=0; j<width; j++){
if(pixels[i][j]>0){
double x = (double)j;
double y = (double)i;
}
}
}
This creates steps at curvatures. Anti-aliasing uses lighter pixels to visually create smoothness at the edges. Assuming the image has only black pixels, and the alpha channel is used for anti-aliasing only, I decided to move the points (x,y) according to the alpha value
for( int i=0; i<height; i++){
for( int j=0; j<width; j++){
if(pixels[i][j]>0){ // visible pixels
double x = (double)j;
double y = (double)i;
if(pixels[i][j]<255){ // anti-aliasing partly transparent pixels
if(pixels[i][j-1]==0){ // left edge
x+=(double)(255-pixels[i][j])/255 // move the point to the right
}
else if(pixels[i][j]<255){
if(pixels[i][j+1]==0){ // right edge
x-=(double)(255-pixels[i][j])/255 // move the point to the left
}
// and the same for the vertical edges
}
}
}
This massively improved the edge smoothness, but I believe the logic I followed is totally wrong, as it cannot account for the curves. For example,
Programs like potrace
do this job by transforming edges to paths, but here I just want to re-arrange the pixels points to form smooth edges (imagine we are creating the edges by dots instead of cubic pixels) rather than creating bezier curves.
What is the practical way to capture points at the edge of PNG pixels with smoothness?