Skip to content

Instantly share code, notes, and snippets.

@FloooD
Created February 6, 2012 20:35

Revisions

  1. FloooD revised this gist Feb 6, 2012. 3 changed files with 46 additions and 45 deletions.
    27 changes: 0 additions & 27 deletions 24.pgm
    Original file line number Diff line number Diff line change
    @@ -1,27 +0,0 @@
    P2
    24 24 255

    0 0 0 0 0 0 0 101 183 222 244 253 253 244 222 183 101 0 0 0 0 0 0 0
    0 0 0 0 0 100 212 254 255 255 255 255 255 255 255 255 254 212 100 0 0 0 0 0
    0 0 0 0 162 250 255 255 255 255 255 255 255 255 255 255 255 255 250 162 0 0 0 0
    0 0 0 181 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 181 0 0 0
    0 0 162 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 162 0 0
    0 100 250 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 250 100 0
    0 212 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 212 0
    101 254 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 254 101
    183 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 183
    222 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 222
    244 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 244
    253 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 253
    253 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 253
    244 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 244
    222 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 222
    183 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 183
    101 254 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 254 101
    0 212 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 212 0
    0 100 250 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 250 100 0
    0 0 162 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 162 0 0
    0 0 0 181 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 181 0 0 0
    0 0 0 0 162 250 255 255 255 255 255 255 255 255 255 255 255 255 250 162 0 0 0 0
    0 0 0 0 0 100 212 254 255 255 255 255 255 255 255 255 254 212 100 0 0 0 0 0
    0 0 0 0 0 0 0 101 183 222 244 253 253 244 222 183 101 0 0 0 0 0 0 0
    2 changes: 1 addition & 1 deletion Makefile
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    all:
    gcc -O3 -lm -Wall -pedantic -std=c99 -o circ circ.c
    gcc -O3 -lm -Wall -pedantic -std=c99 -o circ circ.c
    62 changes: 45 additions & 17 deletions circ.c
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,9 @@
    #include <stdlib.h>
    #include <math.h>

    #define PADDING 4
    #define GAMMA_CORRECTED
    #define SUBPIXEL_RATIO 0.5 //1 = full subpx aa, 0 = grayscale

    double indef_integ(double r, double x)
    {
    @@ -23,10 +25,9 @@ int in(double r, double x, double y)
    return x*x + y*y < r*r;
    }

    double *circle(double rad)
    double *circle(double rad, int r) //r = half of length of square
    {
    int y, x;
    const int r = ceil(fabs(rad));
    double *c = malloc(4*r*r*sizeof(double));

    #define L(a,b,c,d) (((a)<<3)|((b)<<2)|((c)<<1)|(d))
    @@ -65,12 +66,8 @@ double *circle(double rad)
    return c;
    }

    int lin2lin(double d)
    {
    return (int)(255*d+0.5);
    }

    int lin2srgb(double d)
    #ifdef GAMMA_CORRECTED
    int d2i(double d)
    {
    if (d<=0.0)
    return 0;
    @@ -81,6 +78,12 @@ int lin2srgb(double d)
    else
    return (int)(3294.6*d+0.5);
    }
    #else
    int d2i(double d)
    {
    return (int)(255*d+0.5);
    }
    #endif

    void pgm(double *a, int w, int h)
    {
    @@ -89,23 +92,48 @@ void pgm(double *a, int w, int h)
    "%d %d 255\n\n", w, h);
    for (y = 0; y < h; y++) {
    for (x = 0; x < w; x++)
    #ifdef GAMMA_CORRECTED
    printf("%3d ", lin2srgb(a[y*w+x]));
    #else
    printf("%3d ", lin2lin(a[y*w+x]));
    #endif
    printf("%3d ", d2i(a[y*w+x]));
    printf("\n");
    }
    }

    void ppm(double *a, int w, int h)
    {
    int y, x;
    printf("P3\n"
    "%d %d 255\n\n", w, h);
    for (y = 0; y < h; y++) {
    for (x = 0; x < w; x++)
    printf("%3d %3d %3d ", d2i(a[3*(y*w+x)]), d2i(a[3*(y*w+x)+1]), d2i(a[3*(y*w+x)+2]));
    printf("\n");
    }
    }



    int main(int argc, char *argv[])
    {
    if (argc != 2)
    printf("Usage: %s radius > output.pgm\n", argv[0]);
    printf("Usage: %s radius > output.pbm\n", argv[0]);
    else {
    double rad = atof(argv[1]);
    int wh = 2*ceil(fabs(rad));
    pgm(circle(rad), wh, wh);
    int r = ceil(fabs(rad)) + PADDING;
    double *a = circle(rad, r);
    #ifndef SUBPIXEL_RATIO
    pgm(a, 2*r, 2*r);
    free(a);
    #else
    double *b = circle(3*rad, 3*r);
    double *c = malloc(3*4*r*r*sizeof(double));
    int y, x, s;
    for (y = 0; y < 2*r; y++) for (x = 0; x < 2*r; x++) for (s = 0; s < 3; s++)
    c[3*(y*2*r+x)+s] =
    SUBPIXEL_RATIO*(b[3*y*6*r+3*x+s]+b[(3*y+1)*6*r+3*x+s]+b[(3*y+2)*6*r+3*x+s])/3.0
    +(1-SUBPIXEL_RATIO)*a[y*2*r+x];
    ppm(c, 2*r, 2*r);
    free(b);
    free(c);
    #endif
    }
    return 0;
    }
    }
  2. FloooD revised this gist Feb 6, 2012. 2 changed files with 1 addition and 2 deletions.
    2 changes: 1 addition & 1 deletion Makefile
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,2 @@
    all:
    gcc -O3 -lm -Wall -pedantic -std=c99 -o f f.c
    gcc -O3 -lm -Wall -pedantic -std=c99 -o circ circ.c
    1 change: 0 additions & 1 deletion circ.c
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
  3. FloooD created this gist Feb 6, 2012.
    27 changes: 27 additions & 0 deletions 24.pgm
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    P2
    24 24 255

    0 0 0 0 0 0 0 101 183 222 244 253 253 244 222 183 101 0 0 0 0 0 0 0
    0 0 0 0 0 100 212 254 255 255 255 255 255 255 255 255 254 212 100 0 0 0 0 0
    0 0 0 0 162 250 255 255 255 255 255 255 255 255 255 255 255 255 250 162 0 0 0 0
    0 0 0 181 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 181 0 0 0
    0 0 162 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 162 0 0
    0 100 250 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 250 100 0
    0 212 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 212 0
    101 254 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 254 101
    183 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 183
    222 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 222
    244 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 244
    253 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 253
    253 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 253
    244 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 244
    222 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 222
    183 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 183
    101 254 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 254 101
    0 212 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 212 0
    0 100 250 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 250 100 0
    0 0 162 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 162 0 0
    0 0 0 181 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 181 0 0 0
    0 0 0 0 162 250 255 255 255 255 255 255 255 255 255 255 255 255 250 162 0 0 0 0
    0 0 0 0 0 100 212 254 255 255 255 255 255 255 255 255 254 212 100 0 0 0 0 0
    0 0 0 0 0 0 0 101 183 222 244 253 253 244 222 183 101 0 0 0 0 0 0 0
    2 changes: 2 additions & 0 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    all:
    gcc -O3 -lm -Wall -pedantic -std=c99 -o f f.c
    112 changes: 112 additions & 0 deletions circ.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,112 @@

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>

    #define GAMMA_CORRECTED

    double indef_integ(double r, double x)
    {
    if (fabs(x) > fabs(r)) {
    printf("indef_integ: |%f| > |%f|\n", x, r);
    exit(1);
    }
    return 0.5*(x*sqrt(r*r-x*x)+r*r*asin(x/r));
    }

    double def_integ(double r, double left, double right)
    {
    return indef_integ(r, right) - indef_integ(r, left);
    }

    int in(double r, double x, double y)
    {
    return x*x + y*y < r*r;
    }

    double *circle(double rad)
    {
    int y, x;
    const int r = ceil(fabs(rad));
    double *c = malloc(4*r*r*sizeof(double));

    #define L(a,b,c,d) (((a)<<3)|((b)<<2)|((c)<<1)|(d))
    for (y = r-1; y >= 0; y--) for (x = 0; x < r && x <=y; x++) {
    switch (L(in(rad,x,y),in(rad,x+1,y),in(rad,x,y+1),in(rad,x+1,y+1))) {
    case L(0,0,0,0):
    c[(y+r)*2*r+(x+r)] = 0.0;
    break;
    case L(1,1,1,1):
    c[(y+r)*2*r+(x+r)] = 1.0;
    break;
    case L(1,1,0,0):
    c[(y+r)*2*r+(x+r)] = def_integ(rad, x, x+1) - y;
    break;
    case L(1,0,0,0): {
    double d = sqrt(rad*rad-y*y) - x;
    c[(y+r)*2*r+(x+r)] = def_integ(rad, x, x+d) - y*d;
    break;
    }
    case L(1,1,1,0):
    c[(y+r)*2*r+(x+r)] = def_integ(rad, x, x+1) - y - c[(y+r+1)*2*r+(x+r)];
    break;
    default:
    c[(y+r)*2*r+(x+r)] = 0.0;
    break;
    }
    }
    #undef L

    for (y = 0; y < r; y++) for (x = r-1; x >= 0 && x > y; x--)
    c[(y+r)*2*r+(x+r)] = c[(x+r)*2*r+(y+r)];
    for (y = r; y < 2*r; y++) for (x = 0; x < r; x++)
    c[y*2*r+x] = c[y*2*r+(2*r-1-x)];
    for (y = 0; y < r; y++) for (x = 0; x < 2*r; x++)
    c[y*2*r+x] = c[(2*r-1-y)*2*r+x];
    return c;
    }

    int lin2lin(double d)
    {
    return (int)(255*d+0.5);
    }

    int lin2srgb(double d)
    {
    if (d<=0.0)
    return 0;
    else if (d>=1.0)
    return 255;
    else if (d>0.0031308)
    return (int)(pow(d, 1.0/2.4)*269.025-13.525);
    else
    return (int)(3294.6*d+0.5);
    }

    void pgm(double *a, int w, int h)
    {
    int y, x;
    printf("P2\n"
    "%d %d 255\n\n", w, h);
    for (y = 0; y < h; y++) {
    for (x = 0; x < w; x++)
    #ifdef GAMMA_CORRECTED
    printf("%3d ", lin2srgb(a[y*w+x]));
    #else
    printf("%3d ", lin2lin(a[y*w+x]));
    #endif
    printf("\n");
    }
    }

    int main(int argc, char *argv[])
    {
    if (argc != 2)
    printf("Usage: %s radius > output.pgm\n", argv[0]);
    else {
    double rad = atof(argv[1]);
    int wh = 2*ceil(fabs(rad));
    pgm(circle(rad), wh, wh);
    }
    return 0;
    }