fukasawa e60969
/*- genpng
fukasawa e60969
 *
fukasawa e60969
 * COPYRIGHT: Written by John Cunningham Bowler, 2015.
fukasawa e60969
 * To the extent possible under law, the author has waived all copyright and
fukasawa e60969
 * related or neighboring rights to this work.  This work is published from:
fukasawa e60969
 * United States.
fukasawa e60969
 *
fukasawa e60969
 * Generate a PNG with an alpha channel, correctly.
fukasawa e60969
 *
fukasawa e60969
 * This is a test case generator; the resultant PNG files are only of interest
fukasawa e60969
 * to those of us who care about whether the edges of circles are green, red,
fukasawa e60969
 * or yellow.
fukasawa e60969
 *
fukasawa e60969
 * The program generates an RGB+Alpha PNG of a given size containing the given
fukasawa e60969
 * shapes on a transparent background:
fukasawa e60969
 *
fukasawa e60969
 *  genpng width height { shape }
fukasawa e60969
 *    shape ::= color width shape x1 y1 x2 y2
fukasawa e60969
 *
fukasawa e60969
 * 'color' is:
fukasawa e60969
 *
fukasawa e60969
 *  black white red green yellow blue brown purple pink orange gray cyan
fukasawa e60969
 *
fukasawa e60969
 * The point is to have colors that are linguistically meaningful plus that old
fukasawa e60969
 * bugbear of the department store dress murders, Cyan, the only color we argue
fukasawa e60969
 * about.
fukasawa e60969
 *
fukasawa e60969
 * 'shape' is:
fukasawa e60969
 *
fukasawa e60969
 *  circle: an ellipse
fukasawa e60969
 *  square: a rectangle
fukasawa e60969
 *  line: a straight line
fukasawa e60969
 *
fukasawa e60969
 * Each shape is followed by four numbers, these are two points in the output
fukasawa e60969
 * coordinate space (as real numbers) which describe the circle, square, or
fukasawa e60969
 * line.  The shape is filled if it is preceded by 'filled' (not valid for
fukasawa e60969
 * 'line') or is drawn with a line, in which case the width of the line must
fukasawa e60969
 * precede the shape.
fukasawa e60969
 *
fukasawa e60969
 * The whole set of information can be repeated as many times as desired:
fukasawa e60969
 *
fukasawa e60969
 *    shape ::= color width shape x1 y1 x2 y2
fukasawa e60969
 *
fukasawa e60969
 *    color ::= black|white|red|green|yellow|blue
fukasawa e60969
 *    color ::= brown|purple|pink|orange|gray|cyan
fukasawa e60969
 *    width ::= filled
fukasawa e60969
 *    width ::= <number></number>
fukasawa e60969
 *    shape ::= circle|square|line
fukasawa e60969
 *    x1    ::= <number></number>
fukasawa e60969
 *    x2    ::= <number></number>
fukasawa e60969
 *    y1    ::= <number></number>
fukasawa e60969
 *    y2    ::= <number></number>
fukasawa e60969
 *
fukasawa e60969
 * The output PNG is generated by down-sampling a 4x supersampled image using
fukasawa e60969
 * a bi-cubic filter.  The bi-cubic has a 2 (output) pixel width, so an 8x8
fukasawa e60969
 * array of super-sampled points contribute to each output pixel.  The value of
fukasawa e60969
 * a super-sampled point is found using an unfiltered, aliased, infinite
fukasawa e60969
 * precision image: Each shape from the last to the first is checked to see if
fukasawa e60969
 * the point is in the drawn area and, if it is, the color of the point is the
fukasawa e60969
 * color of the shape and the alpha is 1, if not the previous shape is checked.
fukasawa e60969
 *
fukasawa e60969
 * This is an aliased algorithm because no filtering is done; a point is either
fukasawa e60969
 * inside or outside each shape and 'close' points do not contribute to the
fukasawa e60969
 * sample.  The down-sampling is relied on to correct the error of not using
fukasawa e60969
 * a filter.
fukasawa e60969
 *
fukasawa e60969
 * The line end-caps are 'flat'; they go through the points.  The square line
fukasawa e60969
 * joins are mitres; the outside of the lines are continued to the point of
fukasawa e60969
 * intersection.
fukasawa e60969
 */
fukasawa e60969
#include <stddef.h></stddef.h>
fukasawa e60969
#include <stdlib.h></stdlib.h>
fukasawa e60969
#include <string.h></string.h>
fukasawa e60969
#include <stdio.h></stdio.h>
fukasawa e60969
#include <math.h></math.h>
fukasawa e60969
fukasawa e60969
/* Normally use <png.h> here to get the installed libpng, but this is done to</png.h>
fukasawa e60969
 * ensure the code picks up the local libpng implementation:
fukasawa e60969
 */
fukasawa e60969
#include "../../png.h"
fukasawa e60969
fukasawa e60969
#if defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
fukasawa e60969
fukasawa e60969
static const struct color
fukasawa e60969
{
fukasawa e60969
   const char *name;
fukasawa e60969
   double      red;
fukasawa e60969
   double      green;
fukasawa e60969
   double      blue;
fukasawa e60969
} colors[] =
fukasawa e60969
/* color ::= black|white|red|green|yellow|blue
fukasawa e60969
 * color ::= brown|purple|pink|orange|gray|cyan
fukasawa e60969
 */
fukasawa e60969
{
fukasawa e60969
   { "black",   0,    0,  0 },
fukasawa e60969
   { "white",   1,    1,  1 },
fukasawa e60969
   { "red",     1,    0,  0 },
fukasawa e60969
   { "green",   0,    1,  0 },
fukasawa e60969
   { "yellow",  1,    1,  0 },
fukasawa e60969
   { "blue",    0,    0,  1 },
fukasawa e60969
   { "brown",  .5, .125,  0 },
fukasawa e60969
   { "purple",  1,    0,  1 },
fukasawa e60969
   { "pink",    1,   .5, .5 },
fukasawa e60969
   { "orange",  1,   .5,  0 },
fukasawa e60969
   { "gray",    0,   .5, .5 },
fukasawa e60969
   { "cyan",    0,    1,  1 }
fukasawa e60969
};
fukasawa e60969
#define color_count ((sizeof colors)/(sizeof colors[0]))
fukasawa e60969
fukasawa e60969
static const struct color *
fukasawa e60969
color_of(const char *arg)
fukasawa e60969
{
fukasawa e60969
   int icolor = color_count;
fukasawa e60969
fukasawa e60969
   while (--icolor >= 0)
fukasawa e60969
   {
fukasawa e60969
      if (strcmp(colors[icolor].name, arg) == 0)
fukasawa e60969
         return colors+icolor;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   fprintf(stderr, "genpng: invalid color %s\n", arg);
fukasawa e60969
   exit(1);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static double
fukasawa e60969
width_of(const char *arg)
fukasawa e60969
{
fukasawa e60969
   if (strcmp(arg, "filled") == 0)
fukasawa e60969
      return 0;
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      char *ep = NULL;
fukasawa e60969
      double w = strtod(arg, &ep);
fukasawa e60969
fukasawa e60969
      if (ep != NULL && *ep == 0 && w > 0)
fukasawa e60969
         return w;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   fprintf(stderr, "genpng: invalid line width %s\n", arg);
fukasawa e60969
   exit(1);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static double
fukasawa e60969
coordinate_of(const char *arg)
fukasawa e60969
{
fukasawa e60969
   char *ep = NULL;
fukasawa e60969
   double w = strtod(arg, &ep);
fukasawa e60969
fukasawa e60969
   if (ep != NULL && *ep == 0)
fukasawa e60969
      return w;
fukasawa e60969
fukasawa e60969
   fprintf(stderr, "genpng: invalid coordinate value %s\n", arg);
fukasawa e60969
   exit(1);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
struct arg; /* forward declaration */
fukasawa e60969
fukasawa e60969
typedef int (*shape_fn_ptr)(const struct arg *arg, double x, double y);
fukasawa e60969
   /* A function to determine if (x,y) is inside the shape.
fukasawa e60969
    *
fukasawa e60969
    * There are two implementations:
fukasawa e60969
    *
fukasawa e60969
    *    inside_fn: returns true if the point is inside
fukasawa e60969
    *    check_fn:  returns;
fukasawa e60969
    *       -1: the point is outside the shape by more than the filter width (2)
fukasawa e60969
    *        0: the point may be inside the shape
fukasawa e60969
    *       +1: the point is inside the shape by more than the filter width
fukasawa e60969
    */
fukasawa e60969
#define OUTSIDE (-1)
fukasawa e60969
#define INSIDE  (1)
fukasawa e60969
fukasawa e60969
struct arg
fukasawa e60969
{
fukasawa e60969
   const struct color *color;
fukasawa e60969
   shape_fn_ptr        inside_fn;
fukasawa e60969
   shape_fn_ptr        check_fn;
fukasawa e60969
   double              width; /* line width, 0 for 'filled' */
fukasawa e60969
   double              x1, y1, x2, y2;
fukasawa e60969
};
fukasawa e60969
fukasawa e60969
/* IMPLEMENTATION NOTE:
fukasawa e60969
 *
fukasawa e60969
 * We want the contribution of each shape to the sample corresponding to each
fukasawa e60969
 * pixel.  This could be obtained by super sampling the image to infinite
fukasawa e60969
 * dimensions, finding each point within the shape and assigning that a value
fukasawa e60969
 * '1' while leaving every point outside the shape with value '0' then
fukasawa e60969
 * downsampling to the image size with sinc; computationally very expensive.
fukasawa e60969
 *
fukasawa e60969
 * Approximations are as follows:
fukasawa e60969
 *
fukasawa e60969
 * 1) If the pixel coordinate is within the shape assume the sample has the
fukasawa e60969
 *    shape color and is opaque, else assume there is no contribution from
fukasawa e60969
 *    the shape.
fukasawa e60969
 *
fukasawa e60969
 *    This is the equivalent of aliased rendering or resampling an image with
fukasawa e60969
 *    a block filter.  The maximum error in the calculated alpha (which will
fukasawa e60969
 *    always be 0 or 1) is 0.5.
fukasawa e60969
 *
fukasawa e60969
 * 2) If the shape is within a square of size 1x1 centered on the pixel assume
fukasawa e60969
 *    that the shape obscures an amount of the pixel equal to its area within
fukasawa e60969
 *    that square.
fukasawa e60969
 *
fukasawa e60969
 *    This is the equivalent of 'pixel coverage' alpha calculation or resampling
fukasawa e60969
 *    an image with a bi-linear filter.  The maximum error is over 0.2, but the
fukasawa e60969
 *    results are often acceptable.
fukasawa e60969
 *
fukasawa e60969
 *    This can be approximated by applying (1) to a super-sampled image then
fukasawa e60969
 *    downsampling with a bi-linear filter.  The error in the super-sampled
fukasawa e60969
 *    image is 0.5 per sample, but the resampling reduces this.
fukasawa e60969
 *
fukasawa e60969
 * 3) Use a better filter with a super-sampled image; in the limit this is the
fukasawa e60969
 *    sinc() approach.
fukasawa e60969
 *
fukasawa e60969
 * 4) Do the geometric calculation; a bivariate definite integral across the
fukasawa e60969
 *    shape, unfortunately this means evaluating Si(x), the integral of sinc(x),
fukasawa e60969
 *    which is still a lot of math.
fukasawa e60969
 *
fukasawa e60969
 * This code uses approach (3) with a bi-cubic filter and 8x super-sampling
fukasawa e60969
 * and method (1) for the super-samples.  This means that the sample is either
fukasawa e60969
 * 0 or 1, depending on whether the sub-pixel is within or outside the shape.
fukasawa e60969
 * The bi-cubic weights are also fixed and the 16 required weights are
fukasawa e60969
 * pre-computed here (note that the 'scale' setting will need to be changed if
fukasawa e60969
 * 'super' is increased).
fukasawa e60969
 *
fukasawa e60969
 * The code also calculates a sum to the edge of the filter. This is not
fukasawa e60969
 * currently used by could be used to optimize the calculation.
fukasawa e60969
 */
fukasawa e60969
#if 0 /* bc code */
fukasawa e60969
scale=10
fukasawa e60969
super=8
fukasawa e60969
define bicubic(x) {
fukasawa e60969
   if (x <= 1) return (1.5*x - 2.5)*x*x + 1;
fukasawa e60969
   if (x <  2) return (((2.5 - 0.5*x)*x - 4)*x + 2);
fukasawa e60969
   return 0;
fukasawa e60969
}
fukasawa e60969
define sum(x) {
fukasawa e60969
   auto s;
fukasawa e60969
   s = 0;
fukasawa e60969
   while (x < 2*super) {
fukasawa e60969
      s = s + bicubic(x/super);
fukasawa e60969
      x = x + 1;
fukasawa e60969
   }
fukasawa e60969
   return s;
fukasawa e60969
}
fukasawa e60969
define results(x) {
fukasawa e60969
   auto b, s;
fukasawa e60969
   b = bicubic(x/super);
fukasawa e60969
   s = sum(x);
fukasawa e60969
fukasawa e60969
   print "   /*", x, "*/ { ", b, ", ", s, " }";
fukasawa e60969
   return 1;
fukasawa e60969
}
fukasawa e60969
x=0
fukasawa e60969
while (x<2*super) {
fukasawa e60969
   x = x + results(x)
fukasawa e60969
   if (x < 2*super) print ","
fukasawa e60969
   print "\n"
fukasawa e60969
}
fukasawa e60969
quit
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#define BICUBIC1(x) /*     |x| <= 1 */ ((1.5*(x)* - 2.5)*(x)*(x) + 1)
fukasawa e60969
#define BICUBIC2(x) /* 1 < |x| <  2 */ (((2.5 - 0.5*(x))*(x) - 4)*(x) + 2)
fukasawa e60969
#define FILTER_WEIGHT 9 /* Twice the first sum below */
fukasawa e60969
#define FILTER_WIDTH  2 /* Actually half the width; -2..+2 */
fukasawa e60969
#define FILTER_STEPS  8 /* steps per filter unit */
fukasawa e60969
static const double
fukasawa e60969
bicubic[16][2] =
fukasawa e60969
{
fukasawa e60969
   /* These numbers are exact; the weight for the filter is 1/9, but this
fukasawa e60969
    * would make the numbers inexact, so it is not included here.
fukasawa e60969
    */
fukasawa e60969
   /*          bicubic      sum        */
fukasawa e60969
   /* 0*/ { 1.0000000000, 4.5000000000 },
fukasawa e60969
   /* 1*/ {  .9638671875, 3.5000000000 },
fukasawa e60969
   /* 2*/ {  .8671875000, 2.5361328125 },
fukasawa e60969
   /* 3*/ {  .7275390625, 1.6689453125 },
fukasawa e60969
   /* 4*/ {  .5625000000,  .9414062500 },
fukasawa e60969
   /* 5*/ {  .3896484375,  .3789062500 },
fukasawa e60969
   /* 6*/ {  .2265625000, -.0107421875 },
fukasawa e60969
   /* 7*/ {  .0908203125, -.2373046875 },
fukasawa e60969
   /* 8*/ {            0, -.3281250000 },
fukasawa e60969
   /* 9*/ { -.0478515625, -.3281250000 },
fukasawa e60969
   /*10*/ { -.0703125000, -.2802734375 },
fukasawa e60969
   /*11*/ { -.0732421875, -.2099609375 },
fukasawa e60969
   /*12*/ { -.0625000000, -.1367187500 },
fukasawa e60969
   /*13*/ { -.0439453125, -.0742187500 },
fukasawa e60969
   /*14*/ { -.0234375000, -.0302734375 },
fukasawa e60969
   /*15*/ { -.0068359375, -.0068359375 }
fukasawa e60969
};
fukasawa e60969
fukasawa e60969
static double
fukasawa e60969
alpha_calc(const struct arg *arg, double x, double y)
fukasawa e60969
{
fukasawa e60969
   /* For [x-2..x+2],[y-2,y+2] calculate the weighted bicubic given a function
fukasawa e60969
    * which tells us whether a point is inside or outside the shape.  First
fukasawa e60969
    * check if we need to do this at all:
fukasawa e60969
    */
fukasawa e60969
   switch (arg->check_fn(arg, x, y))
fukasawa e60969
   {
fukasawa e60969
      case OUTSIDE:
fukasawa e60969
         return 0; /* all samples outside the shape */
fukasawa e60969
fukasawa e60969
      case INSIDE:
fukasawa e60969
         return 1; /* all samples inside the shape */
fukasawa e60969
fukasawa e60969
      default:
fukasawa e60969
      {
fukasawa e60969
         int dy;
fukasawa e60969
         double alpha = 0;
fukasawa e60969
fukasawa e60969
#        define FILTER_D (FILTER_WIDTH*FILTER_STEPS-1)
fukasawa e60969
         for (dy=-FILTER_D; dy<=FILTER_D; ++dy)
fukasawa e60969
         {
fukasawa e60969
            double wy = bicubic[abs(dy)][0];
fukasawa e60969
fukasawa e60969
            if (wy != 0)
fukasawa e60969
            {
fukasawa e60969
               double alphay = 0;
fukasawa e60969
               int dx;
fukasawa e60969
fukasawa e60969
               for (dx=-FILTER_D; dx<=FILTER_D; ++dx)
fukasawa e60969
               {
fukasawa e60969
                  double wx = bicubic[abs(dx)][0];
fukasawa e60969
fukasawa e60969
                  if (wx != 0 && arg->inside_fn(arg, x+dx/16, y+dy/16))
fukasawa e60969
                     alphay += wx;
fukasawa e60969
               }
fukasawa e60969
fukasawa e60969
               alpha += wy * alphay;
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         /* This needs to be weighted for each dimension: */
fukasawa e60969
         return alpha / (FILTER_WEIGHT*FILTER_WEIGHT);
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* These are the shape functions. */
fukasawa e60969
/* "square",
fukasawa e60969
 * { inside_square_filled, check_square_filled },
fukasawa e60969
 * { inside_square, check_square }
fukasawa e60969
 */
fukasawa e60969
static int
fukasawa e60969
square_check(double x, double y, double x1, double y1, double x2, double y2)
fukasawa e60969
   /* Is x,y inside the square (x1,y1)..(x2,y2)? */
fukasawa e60969
{
fukasawa e60969
   /* Do a modified Cohen-Sutherland on one point, bit patterns that indicate
fukasawa e60969
    * 'outside' are:
fukasawa e60969
    *
fukasawa e60969
    *   x
fukasawa e60969
    *    0      x      0      x     To the right
fukasawa e60969
    *    1      x      1      x     To the left
fukasawa e60969
    *    x      0      x      0     Below
fukasawa e60969
    *    x      1      x      1     Above
fukasawa e60969
    *
fukasawa e60969
    * So 'inside' is (x
fukasawa e60969
    */
fukasawa e60969
   return ((x
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
inside_square_filled(const struct arg *arg, double x, double y)
fukasawa e60969
{
fukasawa e60969
   return square_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
square_check_line(const struct arg *arg, double x, double y, double w)
fukasawa e60969
   /* Check for a point being inside the boundaries implied by the given arg
fukasawa e60969
    * and assuming a width 2*w each side of the boundaries.  This returns the
fukasawa e60969
    * 'check' INSIDE/OUTSIDE/0 result but note the semantics:
fukasawa e60969
    *
fukasawa e60969
    *          +--------------+
fukasawa e60969
    *          |              |   OUTSIDE
fukasawa e60969
    *          |   INSIDE     |
fukasawa e60969
    *          |              |
fukasawa e60969
    *          +--------------+
fukasawa e60969
    *
fukasawa e60969
    * And '0' means within the line boundaries.
fukasawa e60969
    */
fukasawa e60969
{
fukasawa e60969
   double cx = (arg->x1+arg->x2)/2;
fukasawa e60969
   double wx = fabs(arg->x1-arg->x2)/2;
fukasawa e60969
   double cy = (arg->y1+arg->y2)/2;
fukasawa e60969
   double wy = fabs(arg->y1-arg->y2)/2;
fukasawa e60969
fukasawa e60969
   if (square_check(x, y, cx-wx-w, cy-wy-w, cx+wx+w, cy+wy+w))
fukasawa e60969
   {
fukasawa e60969
      /* Inside, but maybe too far; check for the redundant case where
fukasawa e60969
       * the lines overlap:
fukasawa e60969
       */
fukasawa e60969
      wx -= w;
fukasawa e60969
      wy -= w;
fukasawa e60969
      if (wx > 0 && wy > 0 && square_check(x, y, cx-wx, cy-wy, cx+wx, cy+wy))
fukasawa e60969
         return INSIDE; /* between (inside) the boundary lines. */
fukasawa e60969
fukasawa e60969
      return 0; /* inside the lines themselves. */
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return OUTSIDE; /* outside the boundary lines. */
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
check_square_filled(const struct arg *arg, double x, double y)
fukasawa e60969
{
fukasawa e60969
   /* The filter extends +/-FILTER_WIDTH each side of each output point, so
fukasawa e60969
    * the check has to expand and contract the square by that amount; '0'
fukasawa e60969
    * means close enough to the edge of the square that the bicubic filter has
fukasawa e60969
    * to be run, OUTSIDE means alpha==0, INSIDE means alpha==1.
fukasawa e60969
    */
fukasawa e60969
   return square_check_line(arg, x, y, FILTER_WIDTH);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
inside_square(const struct arg *arg, double x, double y)
fukasawa e60969
{
fukasawa e60969
   /* Return true if within the drawn lines, else false, no need to distinguish
fukasawa e60969
    * INSIDE vs OUTSIDE here:
fukasawa e60969
    */
fukasawa e60969
   return square_check_line(arg, x, y, arg->width/2) == 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
check_square(const struct arg *arg, double x, double y)
fukasawa e60969
{
fukasawa e60969
   /* So for this function a result of 'INSIDE' means inside the actual lines.
fukasawa e60969
    */
fukasawa e60969
   double w = arg->width/2;
fukasawa e60969
fukasawa e60969
   if (square_check_line(arg, x, y, w+FILTER_WIDTH) == 0)
fukasawa e60969
   {
fukasawa e60969
      /* Somewhere close to the boundary lines. If far enough inside one of
fukasawa e60969
       * them then we can return INSIDE:
fukasawa e60969
       */
fukasawa e60969
      w -= FILTER_WIDTH;
fukasawa e60969
fukasawa e60969
      if (w > 0 && square_check_line(arg, x, y, w) == 0)
fukasawa e60969
         return INSIDE;
fukasawa e60969
fukasawa e60969
      /* Point is somewhere in the filter region: */
fukasawa e60969
      return 0;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else /* Inside or outside the square by more than w+FILTER_WIDTH. */
fukasawa e60969
      return OUTSIDE;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* "circle",
fukasawa e60969
 * { inside_circle_filled, check_circle_filled },
fukasawa e60969
 * { inside_circle, check_circle }
fukasawa e60969
 *
fukasawa e60969
 * The functions here are analoguous to the square ones; however, they check
fukasawa e60969
 * the corresponding ellipse as opposed to the rectangle.
fukasawa e60969
 */
fukasawa e60969
static int
fukasawa e60969
circle_check(double x, double y, double x1, double y1, double x2, double y2)
fukasawa e60969
{
fukasawa e60969
   if (square_check(x, y, x1, y1, x2, y2))
fukasawa e60969
   {
fukasawa e60969
      /* Inside the square, so maybe inside the circle too: */
fukasawa e60969
      const double cx = (x1 + x2)/2;
fukasawa e60969
      const double cy = (y1 + y2)/2;
fukasawa e60969
      const double dx = x1 - x2;
fukasawa e60969
      const double dy = y1 - y2;
fukasawa e60969
fukasawa e60969
      x = (x - cx)/dx;
fukasawa e60969
      y = (y - cy)/dy;
fukasawa e60969
fukasawa e60969
      /* It is outside if the distance from the center is more than half the
fukasawa e60969
       * diameter:
fukasawa e60969
       */
fukasawa e60969
      return x*x+y*y < .25;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return 0; /* outside */
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
inside_circle_filled(const struct arg *arg, double x, double y)
fukasawa e60969
{
fukasawa e60969
   return circle_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
circle_check_line(const struct arg *arg, double x, double y, double w)
fukasawa e60969
   /* Check for a point being inside the boundaries implied by the given arg
fukasawa e60969
    * and assuming a width 2*w each side of the boundaries.  This function has
fukasawa e60969
    * the same semantic as square_check_line but tests the circle.
fukasawa e60969
    */
fukasawa e60969
{
fukasawa e60969
   double cx = (arg->x1+arg->x2)/2;
fukasawa e60969
   double wx = fabs(arg->x1-arg->x2)/2;
fukasawa e60969
   double cy = (arg->y1+arg->y2)/2;
fukasawa e60969
   double wy = fabs(arg->y1-arg->y2)/2;
fukasawa e60969
fukasawa e60969
   if (circle_check(x, y, cx-wx-w, cy-wy-w, cx+wx+w, cy+wy+w))
fukasawa e60969
   {
fukasawa e60969
      /* Inside, but maybe too far; check for the redundant case where
fukasawa e60969
       * the lines overlap:
fukasawa e60969
       */
fukasawa e60969
      wx -= w;
fukasawa e60969
      wy -= w;
fukasawa e60969
      if (wx > 0 && wy > 0 && circle_check(x, y, cx-wx, cy-wy, cx+wx, cy+wy))
fukasawa e60969
         return INSIDE; /* between (inside) the boundary lines. */
fukasawa e60969
fukasawa e60969
      return 0; /* inside the lines themselves. */
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return OUTSIDE; /* outside the boundary lines. */
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
check_circle_filled(const struct arg *arg, double x, double y)
fukasawa e60969
{
fukasawa e60969
   return circle_check_line(arg, x, y, FILTER_WIDTH);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
inside_circle(const struct arg *arg, double x, double y)
fukasawa e60969
{
fukasawa e60969
   return circle_check_line(arg, x, y, arg->width/2) == 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
check_circle(const struct arg *arg, double x, double y)
fukasawa e60969
{
fukasawa e60969
   /* Exactly as the 'square' code.  */
fukasawa e60969
   double w = arg->width/2;
fukasawa e60969
fukasawa e60969
   if (circle_check_line(arg, x, y, w+FILTER_WIDTH) == 0)
fukasawa e60969
   {
fukasawa e60969
      w -= FILTER_WIDTH;
fukasawa e60969
fukasawa e60969
      if (w > 0 && circle_check_line(arg, x, y, w) == 0)
fukasawa e60969
         return INSIDE;
fukasawa e60969
fukasawa e60969
      /* Point is somewhere in the filter region: */
fukasawa e60969
      return 0;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else /* Inside or outside the square by more than w+FILTER_WIDTH. */
fukasawa e60969
      return OUTSIDE;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* "line",
fukasawa e60969
 * { NULL, NULL },  There is no 'filled' line.
fukasawa e60969
 * { inside_line, check_line }
fukasawa e60969
 */
fukasawa e60969
static int
fukasawa e60969
line_check(double x, double y, double x1, double y1, double x2, double y2,
fukasawa e60969
   double w, double expand)
fukasawa e60969
{
fukasawa e60969
   /* Shift all the points to (arg->x1, arg->y1) */
fukasawa e60969
   double lx = x2 - x1;
fukasawa e60969
   double ly = y2 - y1;
fukasawa e60969
   double len2 = lx*lx + ly*ly;
fukasawa e60969
   double cross, dot;
fukasawa e60969
fukasawa e60969
   x -= x1;
fukasawa e60969
   y -= y1;
fukasawa e60969
fukasawa e60969
   /* The dot product is the distance down the line, the cross product is
fukasawa e60969
    * the distance away from the line:
fukasawa e60969
    *
fukasawa e60969
    *    distance = |cross| / sqrt(len2)
fukasawa e60969
    */
fukasawa e60969
   cross = x * ly - y * lx;
fukasawa e60969
fukasawa e60969
   /* If 'distance' is more than w the point is definitely outside the line:
fukasawa e60969
    *
fukasawa e60969
    *     distance >= w
fukasawa e60969
    *     |cross|  >= w * sqrt(len2)
fukasawa e60969
    *     cross^2  >= w^2 * len2:
fukasawa e60969
    */
fukasawa e60969
   if (cross*cross >= (w+expand)*(w+expand)*len2)
fukasawa e60969
      return 0; /* outside */
fukasawa e60969
fukasawa e60969
   /* Now find the distance *along* the line; this comes from the dot product
fukasawa e60969
    * lx.x+ly.y. The actual distance (in pixels) is:
fukasawa e60969
    *
fukasawa e60969
    *   distance = dot / sqrt(len2)
fukasawa e60969
    */
fukasawa e60969
   dot = lx * x + ly * y;
fukasawa e60969
fukasawa e60969
   /* The test for 'outside' is:
fukasawa e60969
    *
fukasawa e60969
    *    distance < 0 || distance > sqrt(len2)
fukasawa e60969
    *                 -> dot / sqrt(len2) > sqrt(len2)
fukasawa e60969
    *                 -> dot > len2
fukasawa e60969
    *
fukasawa e60969
    * But 'expand' is used for the filter width and needs to be handled too:
fukasawa e60969
    */
fukasawa e60969
   return dot > -expand && dot < len2+expand;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
inside_line(const struct arg *arg, double x, double y)
fukasawa e60969
{
fukasawa e60969
   return line_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2, arg->width/2, 0);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
check_line(const struct arg *arg, double x, double y)
fukasawa e60969
{
fukasawa e60969
   /* The end caps of the line must be checked too; it's not enough just to
fukasawa e60969
    * widen the line by FILTER_WIDTH; 'expand' exists for this purpose:
fukasawa e60969
    */
fukasawa e60969
   if (line_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2, arg->width/2,
fukasawa e60969
       FILTER_WIDTH))
fukasawa e60969
   {
fukasawa e60969
      /* Inside the line+filter; far enough inside that the filter isn't
fukasawa e60969
       * required?
fukasawa e60969
       */
fukasawa e60969
      if (arg->width > 2*FILTER_WIDTH &&
fukasawa e60969
          line_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2, arg->width/2,
fukasawa e60969
             -FILTER_WIDTH))
fukasawa e60969
         return INSIDE;
fukasawa e60969
fukasawa e60969
      return 0;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return OUTSIDE;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static const struct
fukasawa e60969
{
fukasawa e60969
   const char    *name;
fukasawa e60969
   shape_fn_ptr   function[2/*fill,line*/][2];
fukasawa e60969
#  define         FN_INSIDE 0
fukasawa e60969
#  define         FN_CHECK 1
fukasawa e60969
} shape_defs[] =
fukasawa e60969
{
fukasawa e60969
   {  "square",
fukasawa e60969
      {  { inside_square_filled, check_square_filled },
fukasawa e60969
         { inside_square, check_square } }
fukasawa e60969
   },
fukasawa e60969
   {  "circle",
fukasawa e60969
      {  { inside_circle_filled, check_circle_filled },
fukasawa e60969
         { inside_circle, check_circle } }
fukasawa e60969
   },
fukasawa e60969
   {  "line",
fukasawa e60969
      {  { NULL, NULL },
fukasawa e60969
         { inside_line, check_line } }
fukasawa e60969
   }
fukasawa e60969
};
fukasawa e60969
fukasawa e60969
#define shape_count ((sizeof shape_defs)/(sizeof shape_defs[0]))
fukasawa e60969
fukasawa e60969
static shape_fn_ptr
fukasawa e60969
shape_of(const char *arg, double width, int f)
fukasawa e60969
{
fukasawa e60969
   unsigned int i;
fukasawa e60969
fukasawa e60969
   for (i=0; i
fukasawa e60969
   {
fukasawa e60969
      shape_fn_ptr fn = shape_defs[i].function[width != 0][f];
fukasawa e60969
fukasawa e60969
      if (fn != NULL)
fukasawa e60969
         return fn;
fukasawa e60969
fukasawa e60969
      fprintf(stderr, "genpng: %s %s not supported\n",
fukasawa e60969
         width == 0 ? "filled" : "unfilled", arg);
fukasawa e60969
      exit(1);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   fprintf(stderr, "genpng: %s: not a valid shape name\n", arg);
fukasawa e60969
   exit(1);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
parse_arg(struct arg *arg, const char **argv/*7 arguments*/)
fukasawa e60969
{
fukasawa e60969
   /* shape ::= color width shape x1 y1 x2 y2 */
fukasawa e60969
   arg->color = color_of(argv[0]);
fukasawa e60969
   arg->width = width_of(argv[1]);
fukasawa e60969
   arg->inside_fn = shape_of(argv[2], arg->width, FN_INSIDE);
fukasawa e60969
   arg->check_fn = shape_of(argv[2], arg->width, FN_CHECK);
fukasawa e60969
   arg->x1 = coordinate_of(argv[3]);
fukasawa e60969
   arg->y1 = coordinate_of(argv[4]);
fukasawa e60969
   arg->x2 = coordinate_of(argv[5]);
fukasawa e60969
   arg->y2 = coordinate_of(argv[6]);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static png_uint_32
fukasawa e60969
read_wh(const char *name, const char *str)
fukasawa e60969
   /* read a PNG width or height */
fukasawa e60969
{
fukasawa e60969
   char *ep = NULL;
fukasawa e60969
   unsigned long ul = strtoul(str, &ep, 10);
fukasawa e60969
fukasawa e60969
   if (ep != NULL && *ep == 0 && ul > 0 && ul <= 0x7fffffff)
fukasawa e60969
      return (png_uint_32)/*SAFE*/ul;
fukasawa e60969
fukasawa e60969
   fprintf(stderr, "genpng: %s: invalid number %s\n", name, str);
fukasawa e60969
   exit(1);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
static void
fukasawa e60969
pixel(png_uint_16p p, struct arg *args, int nargs, double x, double y)
fukasawa e60969
{
fukasawa e60969
   /* Fill in the pixel by checking each shape (args[nargs]) for effects on
fukasawa e60969
    * the corresponding sample:
fukasawa e60969
    */
fukasawa e60969
   double r=0, g=0, b=0, a=0;
fukasawa e60969
fukasawa e60969
   while (--nargs >= 0 && a != 1)
fukasawa e60969
   {
fukasawa e60969
      /* NOTE: alpha_calc can return a value outside the range 0..1 with the
fukasawa e60969
       * bicubic filter.
fukasawa e60969
       */
fukasawa e60969
      const double alpha = alpha_calc(args+nargs, x, y) * (1-a);
fukasawa e60969
fukasawa e60969
      r += alpha * args[nargs].color->red;
fukasawa e60969
      g += alpha * args[nargs].color->green;
fukasawa e60969
      b += alpha * args[nargs].color->blue;
fukasawa e60969
      a += alpha;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* 'a' may be negative or greater than 1; if it is, negative clamp the
fukasawa e60969
    * pixel to 0 if >1 clamp r/g/b:
fukasawa e60969
    */
fukasawa e60969
   if (a > 0)
fukasawa e60969
   {
fukasawa e60969
      if (a > 1)
fukasawa e60969
      {
fukasawa e60969
         if (r > 1) r = 1;
fukasawa e60969
         if (g > 1) g = 1;
fukasawa e60969
         if (b > 1) b = 1;
fukasawa e60969
         a = 1;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* And fill in the pixel: */
fukasawa e60969
      p[0] = (png_uint_16)/*SAFE*/round(r * 65535);
fukasawa e60969
      p[1] = (png_uint_16)/*SAFE*/round(g * 65535);
fukasawa e60969
      p[2] = (png_uint_16)/*SAFE*/round(b * 65535);
fukasawa e60969
      p[3] = (png_uint_16)/*SAFE*/round(a * 65535);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      p[3] = p[2] = p[1] = p[0] = 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
int
fukasawa e60969
main(int argc, const char **argv)
fukasawa e60969
{
fukasawa e60969
   int convert_to_8bit = 0;
fukasawa e60969
fukasawa e60969
   /* There is one option: --8bit: */
fukasawa e60969
   if (argc > 1 && strcmp(argv[1], "--8bit") == 0)
fukasawa e60969
      --argc, ++argv, convert_to_8bit = 1;
fukasawa e60969
fukasawa e60969
   if (argc >= 3)
fukasawa e60969
   {
fukasawa e60969
      png_uint_16p buffer;
fukasawa e60969
      int nshapes;
fukasawa e60969
      png_image image;
fukasawa e60969
#     define max_shapes 256
fukasawa e60969
      struct arg arg_list[max_shapes];
fukasawa e60969
fukasawa e60969
      /* The libpng Simplified API write code requires a fully initialized
fukasawa e60969
       * structure.
fukasawa e60969
       */
fukasawa e60969
      memset(&image, 0, sizeof image);
fukasawa e60969
      image.version = PNG_IMAGE_VERSION;
fukasawa e60969
      image.opaque = NULL;
fukasawa e60969
      image.width = read_wh("width", argv[1]);
fukasawa e60969
      image.height = read_wh("height", argv[2]);
fukasawa e60969
      image.format = PNG_FORMAT_LINEAR_RGB_ALPHA;
fukasawa e60969
      image.flags = 0;
fukasawa e60969
      image.colormap_entries = 0;
fukasawa e60969
fukasawa e60969
      /* Check the remainder of the arguments */
fukasawa e60969
      for (nshapes=0; 3+7*(nshapes+1) <= argc && nshapes < max_shapes;
fukasawa e60969
           ++nshapes)
fukasawa e60969
         parse_arg(arg_list+nshapes, argv+3+7*nshapes);
fukasawa e60969
fukasawa e60969
      if (3+7*nshapes != argc)
fukasawa e60969
      {
fukasawa e60969
         fprintf(stderr, "genpng: %s: too many arguments\n", argv[3+7*nshapes]);
fukasawa e60969
         return 1;
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      /* Create the buffer: */
fukasawa e60969
      buffer = malloc(PNG_IMAGE_SIZE(image));
fukasawa e60969
fukasawa e60969
      if (buffer != NULL)
fukasawa e60969
      {
fukasawa e60969
         png_uint_32 y;
fukasawa e60969
fukasawa e60969
         /* Write each row... */
fukasawa e60969
         for (y=0; y
fukasawa e60969
         {
fukasawa e60969
            png_uint_32 x;
fukasawa e60969
fukasawa e60969
            /* Each pixel in each row: */
fukasawa e60969
            for (x=0; x
fukasawa e60969
               pixel(buffer + 4*(x + y*image.width), arg_list, nshapes, x, y);
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         /* Write the result (to stdout) */
fukasawa e60969
         if (png_image_write_to_stdio(&image, stdout, convert_to_8bit,
fukasawa e60969
             buffer, 0/*row_stride*/, NULL/*colormap*/))
fukasawa e60969
         {
fukasawa e60969
            free(buffer);
fukasawa e60969
            return 0; /* success */
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            fprintf(stderr, "genpng: write stdout: %s\n", image.message);
fukasawa e60969
fukasawa e60969
         free(buffer);
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         fprintf(stderr, "genpng: out of memory: %lu bytes\n",
fukasawa e60969
               (unsigned long)PNG_IMAGE_SIZE(image));
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
   {
fukasawa e60969
      /* Wrong number of arguments */
fukasawa e60969
      fprintf(stderr, "genpng: usage: genpng [--8bit] width height {shape}\n"
fukasawa e60969
         " Generate a transparent PNG in RGBA (truecolor+alpha) format\n"
fukasawa e60969
         " containing the given shape or shapes.  Shapes are defined:\n"
fukasawa e60969
         "\n"
fukasawa e60969
         "  shape ::= color width shape x1 y1 x2 y2\n"
fukasawa e60969
         "  color ::= black|white|red|green|yellow|blue\n"
fukasawa e60969
         "  color ::= brown|purple|pink|orange|gray|cyan\n"
fukasawa e60969
         "  width ::= filled|<number>\n"</number>
fukasawa e60969
         "  shape ::= circle|square|line\n"
fukasawa e60969
         "  x1,x2 ::= <number>\n"</number>
fukasawa e60969
         "  y1,y2 ::= <number>\n"</number>
fukasawa e60969
         "\n"
fukasawa e60969
         " Numbers are floating point numbers describing points relative to\n"
fukasawa e60969
         " the top left of the output PNG as pixel coordinates.  The 'width'\n"
fukasawa e60969
         " parameter is either the width of the line (in output pixels) used\n"
fukasawa e60969
         " to draw the shape or 'filled' to indicate that the shape should\n"
fukasawa e60969
         " be filled with the color.\n"
fukasawa e60969
         "\n"
fukasawa e60969
         " Colors are interpreted loosely to give access to the eight full\n"
fukasawa e60969
         " intensity RGB values:\n"
fukasawa e60969
         "\n"
fukasawa e60969
         "  black, red, green, blue, yellow, cyan, purple, white,\n"
fukasawa e60969
         "\n"
fukasawa e60969
         " Cyan is full intensity blue+green; RGB(0,1,1), plus the following\n"
fukasawa e60969
         " lower intensity values:\n"
fukasawa e60969
         "\n"
fukasawa e60969
         "  brown:  red+orange:  RGB(0.5, 0.125, 0) (dark red+orange)\n"
fukasawa e60969
         "  pink:   red+white:   RGB(1.0, 0.5,   0.5)\n"
fukasawa e60969
         "  orange: red+yellow:  RGB(1.0, 0.5,   0)\n"
fukasawa e60969
         "  gray:   black+white: RGB(0.5, 0.5,   0.5)\n"
fukasawa e60969
         "\n"
fukasawa e60969
         " The RGB values are selected to make detection of aliasing errors\n"
fukasawa e60969
         " easy. The names are selected to make the description of errors\n"
fukasawa e60969
         " easy.\n"
fukasawa e60969
         "\n"
fukasawa e60969
         " The PNG is written to stdout, if --8bit is given a 32bpp RGBA sRGB\n"
fukasawa e60969
         " file is produced, otherwise a 64bpp RGBA linear encoded file is\n"
fukasawa e60969
         " written.\n");
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   return 1;
fukasawa e60969
}
fukasawa e60969
#endif /* SIMPLIFIED_WRITE && STDIO */