#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include "../../png.h"
#if defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
static const struct color
{
const char *name;
double red;
double green;
double blue;
} colors[] =
{
{ "black", 0, 0, 0 },
{ "white", 1, 1, 1 },
{ "red", 1, 0, 0 },
{ "green", 0, 1, 0 },
{ "yellow", 1, 1, 0 },
{ "blue", 0, 0, 1 },
{ "brown", .5, .125, 0 },
{ "purple", 1, 0, 1 },
{ "pink", 1, .5, .5 },
{ "orange", 1, .5, 0 },
{ "gray", 0, .5, .5 },
{ "cyan", 0, 1, 1 }
};
#define color_count ((sizeof colors)/(sizeof colors[0]))
static const struct color *
color_of(const char *arg)
{
int icolor = color_count;
while (--icolor >= 0)
{
if (strcmp(colors[icolor].name, arg) == 0)
return colors+icolor;
}
fprintf(stderr, "genpng: invalid color %s\n", arg);
exit(1);
}
static double
width_of(const char *arg)
{
if (strcmp(arg, "filled") == 0)
return 0;
else
{
char *ep = NULL;
double w = strtod(arg, &ep);
if (ep != NULL && *ep == 0 && w > 0)
return w;
}
fprintf(stderr, "genpng: invalid line width %s\n", arg);
exit(1);
}
static double
coordinate_of(const char *arg)
{
char *ep = NULL;
double w = strtod(arg, &ep);
if (ep != NULL && *ep == 0)
return w;
fprintf(stderr, "genpng: invalid coordinate value %s\n", arg);
exit(1);
}
struct arg;
typedef int (*shape_fn_ptr)(const struct arg *arg, double x, double y);
#define OUTSIDE (-1)
#define INSIDE (1)
struct arg
{
const struct color *color;
shape_fn_ptr inside_fn;
shape_fn_ptr check_fn;
double width;
double x1, y1, x2, y2;
};
#if 0
scale=10
super=8
define bicubic(x) {
if (x <= 1) return (1.5*x - 2.5)*x*x + 1;
if (x < 2) return (((2.5 - 0.5*x)*x - 4)*x + 2);
return 0;
}
define sum(x) {
auto s;
s = 0;
while (x < 2*super) {
s = s + bicubic(x/super);
x = x + 1;
}
return s;
}
define results(x) {
auto b, s;
b = bicubic(x/super);
s = sum(x);
print " /*", x, "*/ { ", b, ", ", s, " }";
return 1;
}
x=0
while (x<2*super) {
x = x + results(x)
if (x < 2*super) print ","
print "\n"
}
quit
#endif
#define BICUBIC1(x) ((1.5*(x)* - 2.5)*(x)*(x) + 1)
#define BICUBIC2(x) (((2.5 - 0.5*(x))*(x) - 4)*(x) + 2)
#define FILTER_WEIGHT 9
#define FILTER_WIDTH 2
#define FILTER_STEPS 8
static const double
bicubic[16][2] =
{
{ 1.0000000000, 4.5000000000 },
{ .9638671875, 3.5000000000 },
{ .8671875000, 2.5361328125 },
{ .7275390625, 1.6689453125 },
{ .5625000000, .9414062500 },
{ .3896484375, .3789062500 },
{ .2265625000, -.0107421875 },
{ .0908203125, -.2373046875 },
{ 0, -.3281250000 },
{ -.0478515625, -.3281250000 },
{ -.0703125000, -.2802734375 },
{ -.0732421875, -.2099609375 },
{ -.0625000000, -.1367187500 },
{ -.0439453125, -.0742187500 },
{ -.0234375000, -.0302734375 },
{ -.0068359375, -.0068359375 }
};
static double
alpha_calc(const struct arg *arg, double x, double y)
{
switch (arg->check_fn(arg, x, y))
{
case OUTSIDE:
return 0;
case INSIDE:
return 1;
default:
{
int dy;
double alpha = 0;
# define FILTER_D (FILTER_WIDTH*FILTER_STEPS-1)
for (dy=-FILTER_D; dy<=FILTER_D; ++dy)
{
double wy = bicubic[abs(dy)][0];
if (wy != 0)
{
double alphay = 0;
int dx;
for (dx=-FILTER_D; dx<=FILTER_D; ++dx)
{
double wx = bicubic[abs(dx)][0];
if (wx != 0 && arg->inside_fn(arg, x+dx/16, y+dy/16))
alphay += wx;
}
alpha += wy * alphay;
}
}
return alpha / (FILTER_WEIGHT*FILTER_WEIGHT);
}
}
}
static int
square_check(double x, double y, double x1, double y1, double x2, double y2)
{
return ((x<x1) ^ (x<x2)) & ((y<y1) ^ (y<y2));
}
static int
inside_square_filled(const struct arg *arg, double x, double y)
{
return square_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2);
}
static int
square_check_line(const struct arg *arg, double x, double y, double w)
{
double cx = (arg->x1+arg->x2)/2;
double wx = fabs(arg->x1-arg->x2)/2;
double cy = (arg->y1+arg->y2)/2;
double wy = fabs(arg->y1-arg->y2)/2;
if (square_check(x, y, cx-wx-w, cy-wy-w, cx+wx+w, cy+wy+w))
{
wx -= w;
wy -= w;
if (wx > 0 && wy > 0 && square_check(x, y, cx-wx, cy-wy, cx+wx, cy+wy))
return INSIDE;
return 0;
}
return OUTSIDE;
}
static int
check_square_filled(const struct arg *arg, double x, double y)
{
return square_check_line(arg, x, y, FILTER_WIDTH);
}
static int
inside_square(const struct arg *arg, double x, double y)
{
return square_check_line(arg, x, y, arg->width/2) == 0;
}
static int
check_square(const struct arg *arg, double x, double y)
{
double w = arg->width/2;
if (square_check_line(arg, x, y, w+FILTER_WIDTH) == 0)
{
w -= FILTER_WIDTH;
if (w > 0 && square_check_line(arg, x, y, w) == 0)
return INSIDE;
return 0;
}
else
return OUTSIDE;
}
static int
circle_check(double x, double y, double x1, double y1, double x2, double y2)
{
if (square_check(x, y, x1, y1, x2, y2))
{
const double cx = (x1 + x2)/2;
const double cy = (y1 + y2)/2;
const double dx = x1 - x2;
const double dy = y1 - y2;
x = (x - cx)/dx;
y = (y - cy)/dy;
return x*x+y*y < .25;
}
return 0;
}
static int
inside_circle_filled(const struct arg *arg, double x, double y)
{
return circle_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2);
}
static int
circle_check_line(const struct arg *arg, double x, double y, double w)
{
double cx = (arg->x1+arg->x2)/2;
double wx = fabs(arg->x1-arg->x2)/2;
double cy = (arg->y1+arg->y2)/2;
double wy = fabs(arg->y1-arg->y2)/2;
if (circle_check(x, y, cx-wx-w, cy-wy-w, cx+wx+w, cy+wy+w))
{
wx -= w;
wy -= w;
if (wx > 0 && wy > 0 && circle_check(x, y, cx-wx, cy-wy, cx+wx, cy+wy))
return INSIDE;
return 0;
}
return OUTSIDE;
}
static int
check_circle_filled(const struct arg *arg, double x, double y)
{
return circle_check_line(arg, x, y, FILTER_WIDTH);
}
static int
inside_circle(const struct arg *arg, double x, double y)
{
return circle_check_line(arg, x, y, arg->width/2) == 0;
}
static int
check_circle(const struct arg *arg, double x, double y)
{
double w = arg->width/2;
if (circle_check_line(arg, x, y, w+FILTER_WIDTH) == 0)
{
w -= FILTER_WIDTH;
if (w > 0 && circle_check_line(arg, x, y, w) == 0)
return INSIDE;
return 0;
}
else
return OUTSIDE;
}
static int
line_check(double x, double y, double x1, double y1, double x2, double y2,
double w, double expand)
{
double lx = x2 - x1;
double ly = y2 - y1;
double len2 = lx*lx + ly*ly;
double cross, dot;
x -= x1;
y -= y1;
cross = x * ly - y * lx;
if (cross*cross >= (w+expand)*(w+expand)*len2)
return 0;
dot = lx * x + ly * y;
return dot > -expand && dot < len2+expand;
}
static int
inside_line(const struct arg *arg, double x, double y)
{
return line_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2, arg->width/2, 0);
}
static int
check_line(const struct arg *arg, double x, double y)
{
if (line_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2, arg->width/2,
FILTER_WIDTH))
{
if (arg->width > 2*FILTER_WIDTH &&
line_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2, arg->width/2,
-FILTER_WIDTH))
return INSIDE;
return 0;
}
return OUTSIDE;
}
static const struct
{
const char *name;
shape_fn_ptr function[2][2];
# define FN_INSIDE 0
# define FN_CHECK 1
} shape_defs[] =
{
{ "square",
{ { inside_square_filled, check_square_filled },
{ inside_square, check_square } }
},
{ "circle",
{ { inside_circle_filled, check_circle_filled },
{ inside_circle, check_circle } }
},
{ "line",
{ { NULL, NULL },
{ inside_line, check_line } }
}
};
#define shape_count ((sizeof shape_defs)/(sizeof shape_defs[0]))
static shape_fn_ptr
shape_of(const char *arg, double width, int f)
{
unsigned int i;
for (i=0; i<shape_count; ++i) if (strcmp(shape_defs[i].name, arg) == 0)
{
shape_fn_ptr fn = shape_defs[i].function[width != 0][f];
if (fn != NULL)
return fn;
fprintf(stderr, "genpng: %s %s not supported\n",
width == 0 ? "filled" : "unfilled", arg);
exit(1);
}
fprintf(stderr, "genpng: %s: not a valid shape name\n", arg);
exit(1);
}
static void
parse_arg(struct arg *arg, const char **argv)
{
arg->color = color_of(argv[0]);
arg->width = width_of(argv[1]);
arg->inside_fn = shape_of(argv[2], arg->width, FN_INSIDE);
arg->check_fn = shape_of(argv[2], arg->width, FN_CHECK);
arg->x1 = coordinate_of(argv[3]);
arg->y1 = coordinate_of(argv[4]);
arg->x2 = coordinate_of(argv[5]);
arg->y2 = coordinate_of(argv[6]);
}
static png_uint_32
read_wh(const char *name, const char *str)
{
char *ep = NULL;
unsigned long ul = strtoul(str, &ep, 10);
if (ep != NULL && *ep == 0 && ul > 0 && ul <= 0x7fffffff)
return (png_uint_32)ul;
fprintf(stderr, "genpng: %s: invalid number %s\n", name, str);
exit(1);
}
static void
pixel(png_uint_16p p, struct arg *args, int nargs, double x, double y)
{
double r=0, g=0, b=0, a=0;
while (--nargs >= 0 && a != 1)
{
const double alpha = alpha_calc(args+nargs, x, y) * (1-a);
r += alpha * args[nargs].color->red;
g += alpha * args[nargs].color->green;
b += alpha * args[nargs].color->blue;
a += alpha;
}
if (a > 0)
{
if (a > 1)
{
if (r > 1) r = 1;
if (g > 1) g = 1;
if (b > 1) b = 1;
a = 1;
}
p[0] = (png_uint_16)round(r * 65535);
p[1] = (png_uint_16)round(g * 65535);
p[2] = (png_uint_16)round(b * 65535);
p[3] = (png_uint_16)round(a * 65535);
}
else
p[3] = p[2] = p[1] = p[0] = 0;
}
int
main(int argc, const char **argv)
{
int convert_to_8bit = 0;
if (argc > 1 && strcmp(argv[1], "--8bit") == 0)
--argc, ++argv, convert_to_8bit = 1;
if (argc >= 3)
{
png_uint_16p buffer;
int nshapes;
png_image image;
# define max_shapes 256
struct arg arg_list[max_shapes];
memset(&image, 0, sizeof image);
image.version = PNG_IMAGE_VERSION;
image.opaque = NULL;
image.width = read_wh("width", argv[1]);
image.height = read_wh("height", argv[2]);
image.format = PNG_FORMAT_LINEAR_RGB_ALPHA;
image.flags = 0;
image.colormap_entries = 0;
for (nshapes=0; 3+7*(nshapes+1) <= argc && nshapes < max_shapes;
++nshapes)
parse_arg(arg_list+nshapes, argv+3+7*nshapes);
if (3+7*nshapes != argc)
{
fprintf(stderr, "genpng: %s: too many arguments\n", argv[3+7*nshapes]);
return 1;
}
buffer = malloc(PNG_IMAGE_SIZE(image));
if (buffer != NULL)
{
png_uint_32 y;
for (y=0; y<image.height; ++y)
{
png_uint_32 x;
for (x=0; x<image.width; ++x)
pixel(buffer + 4*(x + y*image.width), arg_list, nshapes, x, y);
}
if (png_image_write_to_stdio(&image, stdout, convert_to_8bit,
buffer, 0, NULL))
{
free(buffer);
return 0;
}
else
fprintf(stderr, "genpng: write stdout: %s\n", image.message);
free(buffer);
}
else
fprintf(stderr, "genpng: out of memory: %lu bytes\n",
(unsigned long)PNG_IMAGE_SIZE(image));
}
else
{
fprintf(stderr, "genpng: usage: genpng [--8bit] width height {shape}\n"
" Generate a transparent PNG in RGBA (truecolor+alpha) format\n"
" containing the given shape or shapes. Shapes are defined:\n"
"\n"
" shape ::= color width shape x1 y1 x2 y2\n"
" color ::= black|white|red|green|yellow|blue\n"
" color ::= brown|purple|pink|orange|gray|cyan\n"
" width ::= filled|<number>\n"
" shape ::= circle|square|line\n"
" x1,x2 ::= <number>\n"
" y1,y2 ::= <number>\n"
"\n"
" Numbers are floating point numbers describing points relative to\n"
" the top left of the output PNG as pixel coordinates. The 'width'\n"
" parameter is either the width of the line (in output pixels) used\n"
" to draw the shape or 'filled' to indicate that the shape should\n"
" be filled with the color.\n"
"\n"
" Colors are interpreted loosely to give access to the eight full\n"
" intensity RGB values:\n"
"\n"
" black, red, green, blue, yellow, cyan, purple, white,\n"
"\n"
" Cyan is full intensity blue+green; RGB(0,1,1), plus the following\n"
" lower intensity values:\n"
"\n"
" brown: red+orange: RGB(0.5, 0.125, 0) (dark red+orange)\n"
" pink: red+white: RGB(1.0, 0.5, 0.5)\n"
" orange: red+yellow: RGB(1.0, 0.5, 0)\n"
" gray: black+white: RGB(0.5, 0.5, 0.5)\n"
"\n"
" The RGB values are selected to make detection of aliasing errors\n"
" easy. The names are selected to make the description of errors\n"
" easy.\n"
"\n"
" The PNG is written to stdout, if --8bit is given a 32bpp RGBA sRGB\n"
" file is produced, otherwise a 64bpp RGBA linear encoded file is\n"
" written.\n");
}
return 1;
}
#endif