Marcus Mathiassen
Marcus Mathiassen

Reputation: 53

Antialiasing a PPM file

I got this output from the code below circle.ppm and it has a lot of jaggy edges. I need some type of antialiasing method I believe, but I have trouble getting started.

What would be the simplest way of getting rid of them?

//******************** INCLUDE: ********************

#include <fstream>                      // ofstream
#include <iostream>                     // cin, cout
#include <cmath>                        // sqrt, pow

using namespace std;                    // USING STANDARD NAMESPACE

//******************** DECLARATION OF FUNCTIONS: ********************

bool isInsideCircle(int x,int y);       // CHECKS IF (X,Y) IS INSIDE CIRCLE
int Brightness(int x,int y);            // DISTANCE FROM CENTER


//******************** VARIABLES: ********************

const int WIDTH = 255, HEIGHT = 255;    // WIDTH AND HEIGHT RESOLUTION
const int RADIUS = 100;                 // RADIUS OF CIRCLE

const int CENTER_X = 0.5 * WIDTH;       // CENTER X-COORDINATES
const int CENTER_Y = 0.5 * HEIGHT;      // CENTER Y-COORDINATES

int r, g, b;                            // RED, GREEN AND BLUE

//******************** MAIN FUNCTION: ********************

int main() {

  system("open circle.ppm");            // OPENS THE IMAGE
  system("killall ToyViewer");          // CLOSES THE IMAGE

//******************** PPM SETUP: ********************

  ofstream img ("circle.ppm");          // MAKE THE FILE
  img << "P3" << endl;                  // MAGIC NUMBER
  img << WIDTH << endl;                 // WIDTH RESOLUTION
  img << HEIGHT << endl;                // HEIGHT RESOLUTION
  img << "255" << endl;                 // 0-255 MAX RGB VALUE

//******************** FOR EACH PIXEL: ********************

  for (int y = 0; y < HEIGHT; y++) {

      for (int x = 0; x < WIDTH; x++) {

          if (isInsideCircle(x, y)) {

              r = 50 + Brightness(x, y) % 255;
              g = 50 + Brightness(x, y) % 255;
              b = 50 + Brightness(x, y) % 255;
          }

          else {

              r = 0;
              g = 0;
              b = 0;

          }

          img << r << " " << g << " " << b << endl;
      }
  }

//******************** PROGRAM END: ********************

  system("open circle.ppm"); 
  return 0;

}

//******************** FUNCTIONS: ********************

bool isInsideCircle(int x, int y) {

  if (pow((x - CENTER_X),2) + pow((y - CENTER_Y),2) < pow(RADIUS,2)) {

      return true;

  } else {

      return false;
  }
}

int Brightness(int x, int y) {

  return abs( sqrt( pow((x - CENTER_X),2) + pow((y - CENTER_Y),2)) - RADIUS);

}

Upvotes: 1

Views: 251

Answers (1)

MSalters
MSalters

Reputation: 179789

As Daniel points out, you have a sharp drop. Not actually from 1 to 0, but from {50,50,50} to {0,0,0}. That's because of the 50 + Brightness lines.

The easy solution is to use a slightly less sharp drop. Define a Brightness2(x,y) which asymptotically drops from 50 to 0.

int Brightness2(int x, int y) {
  int dx = x - CENTER_X;
  int dy = x - CENTER_Y;
  return 50/(1+(dx*dx+dy*dy-RADIUS*RADIUS));
}

Upvotes: 2

Related Questions