genpng.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*- genpng
  2. *
  3. * COPYRIGHT: Written by John Cunningham Bowler, 2015.
  4. * To the extent possible under law, the author has waived all copyright and
  5. * related or neighboring rights to this work. This work is published from:
  6. * United States.
  7. *
  8. * Generate a PNG with an alpha channel, correctly.
  9. *
  10. * This is a test case generator; the resultant PNG files are only of interest
  11. * to those of us who care about whether the edges of circles are green, red,
  12. * or yellow.
  13. *
  14. * The program generates an RGB+Alpha PNG of a given size containing the given
  15. * shapes on a transparent background:
  16. *
  17. * genpng width height { shape }
  18. * shape ::= color width shape x1 y1 x2 y2
  19. *
  20. * 'color' is:
  21. *
  22. * black white red green yellow blue brown purple pink orange gray cyan
  23. *
  24. * The point is to have colors that are linguistically meaningful plus that old
  25. * bugbear of the department store dress murders, Cyan, the only color we argue
  26. * about.
  27. *
  28. * 'shape' is:
  29. *
  30. * circle: an ellipse
  31. * square: a rectangle
  32. * line: a straight line
  33. *
  34. * Each shape is followed by four numbers, these are two points in the output
  35. * coordinate space (as real numbers) which describe the circle, square, or
  36. * line. The shape is filled if it is preceded by 'filled' (not valid for
  37. * 'line') or is drawn with a line, in which case the width of the line must
  38. * precede the shape.
  39. *
  40. * The whole set of information can be repeated as many times as desired:
  41. *
  42. * shape ::= color width shape x1 y1 x2 y2
  43. *
  44. * color ::= black|white|red|green|yellow|blue
  45. * color ::= brown|purple|pink|orange|gray|cyan
  46. * width ::= filled
  47. * width ::= <number>
  48. * shape ::= circle|square|line
  49. * x1 ::= <number>
  50. * x2 ::= <number>
  51. * y1 ::= <number>
  52. * y2 ::= <number>
  53. *
  54. * The output PNG is generated by down-sampling a 4x supersampled image using
  55. * a bi-cubic filter. The bi-cubic has a 2 (output) pixel width, so an 8x8
  56. * array of super-sampled points contribute to each output pixel. The value of
  57. * a super-sampled point is found using an unfiltered, aliased, infinite
  58. * precision image: Each shape from the last to the first is checked to see if
  59. * the point is in the drawn area and, if it is, the color of the point is the
  60. * color of the shape and the alpha is 1, if not the previous shape is checked.
  61. *
  62. * This is an aliased algorithm because no filtering is done; a point is either
  63. * inside or outside each shape and 'close' points do not contribute to the
  64. * sample. The down-sampling is relied on to correct the error of not using
  65. * a filter.
  66. *
  67. * The line end-caps are 'flat'; they go through the points. The square line
  68. * joins are mitres; the outside of the lines are continued to the point of
  69. * intersection.
  70. */
  71. #include <stddef.h>
  72. #include <stdlib.h>
  73. #include <string.h>
  74. #include <stdio.h>
  75. #include <math.h>
  76. /* Normally use <png.h> here to get the installed libpng, but this is done to
  77. * ensure the code picks up the local libpng implementation:
  78. */
  79. #include "../../png.h"
  80. #if defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
  81. static const struct color
  82. {
  83. const char *name;
  84. double red;
  85. double green;
  86. double blue;
  87. } colors[] =
  88. /* color ::= black|white|red|green|yellow|blue
  89. * color ::= brown|purple|pink|orange|gray|cyan
  90. */
  91. {
  92. { "black", 0, 0, 0 },
  93. { "white", 1, 1, 1 },
  94. { "red", 1, 0, 0 },
  95. { "green", 0, 1, 0 },
  96. { "yellow", 1, 1, 0 },
  97. { "blue", 0, 0, 1 },
  98. { "brown", .5, .125, 0 },
  99. { "purple", 1, 0, 1 },
  100. { "pink", 1, .5, .5 },
  101. { "orange", 1, .5, 0 },
  102. { "gray", 0, .5, .5 },
  103. { "cyan", 0, 1, 1 }
  104. };
  105. #define color_count ((sizeof colors)/(sizeof colors[0]))
  106. static const struct color *
  107. color_of(const char *arg)
  108. {
  109. int icolor = color_count;
  110. while (--icolor >= 0)
  111. {
  112. if (strcmp(colors[icolor].name, arg) == 0)
  113. return colors+icolor;
  114. }
  115. fprintf(stderr, "genpng: invalid color %s\n", arg);
  116. exit(1);
  117. }
  118. static double
  119. width_of(const char *arg)
  120. {
  121. if (strcmp(arg, "filled") == 0)
  122. return 0;
  123. else
  124. {
  125. char *ep = NULL;
  126. double w = strtod(arg, &ep);
  127. if (ep != NULL && *ep == 0 && w > 0)
  128. return w;
  129. }
  130. fprintf(stderr, "genpng: invalid line width %s\n", arg);
  131. exit(1);
  132. }
  133. static double
  134. coordinate_of(const char *arg)
  135. {
  136. char *ep = NULL;
  137. double w = strtod(arg, &ep);
  138. if (ep != NULL && *ep == 0)
  139. return w;
  140. fprintf(stderr, "genpng: invalid coordinate value %s\n", arg);
  141. exit(1);
  142. }
  143. struct arg; /* forward declaration */
  144. typedef int (*shape_fn_ptr)(const struct arg *arg, double x, double y);
  145. /* A function to determine if (x,y) is inside the shape.
  146. *
  147. * There are two implementations:
  148. *
  149. * inside_fn: returns true if the point is inside
  150. * check_fn: returns;
  151. * -1: the point is outside the shape by more than the filter width (2)
  152. * 0: the point may be inside the shape
  153. * +1: the point is inside the shape by more than the filter width
  154. */
  155. #define OUTSIDE (-1)
  156. #define INSIDE (1)
  157. struct arg
  158. {
  159. const struct color *color;
  160. shape_fn_ptr inside_fn;
  161. shape_fn_ptr check_fn;
  162. double width; /* line width, 0 for 'filled' */
  163. double x1, y1, x2, y2;
  164. };
  165. /* IMPLEMENTATION NOTE:
  166. *
  167. * We want the contribution of each shape to the sample corresponding to each
  168. * pixel. This could be obtained by super sampling the image to infinite
  169. * dimensions, finding each point within the shape and assigning that a value
  170. * '1' while leaving every point outside the shape with value '0' then
  171. * downsampling to the image size with sinc; computationally very expensive.
  172. *
  173. * Approximations are as follows:
  174. *
  175. * 1) If the pixel coordinate is within the shape assume the sample has the
  176. * shape color and is opaque, else assume there is no contribution from
  177. * the shape.
  178. *
  179. * This is the equivalent of aliased rendering or resampling an image with
  180. * a block filter. The maximum error in the calculated alpha (which will
  181. * always be 0 or 1) is 0.5.
  182. *
  183. * 2) If the shape is within a square of size 1x1 centered on the pixel assume
  184. * that the shape obscures an amount of the pixel equal to its area within
  185. * that square.
  186. *
  187. * This is the equivalent of 'pixel coverage' alpha calculation or resampling
  188. * an image with a bi-linear filter. The maximum error is over 0.2, but the
  189. * results are often acceptable.
  190. *
  191. * This can be approximated by applying (1) to a super-sampled image then
  192. * downsampling with a bi-linear filter. The error in the super-sampled
  193. * image is 0.5 per sample, but the resampling reduces this.
  194. *
  195. * 3) Use a better filter with a super-sampled image; in the limit this is the
  196. * sinc() approach.
  197. *
  198. * 4) Do the geometric calculation; a bivariate definite integral across the
  199. * shape, unfortunately this means evaluating Si(x), the integral of sinc(x),
  200. * which is still a lot of math.
  201. *
  202. * This code uses approach (3) with a bi-cubic filter and 8x super-sampling
  203. * and method (1) for the super-samples. This means that the sample is either
  204. * 0 or 1, depending on whether the sub-pixel is within or outside the shape.
  205. * The bi-cubic weights are also fixed and the 16 required weights are
  206. * pre-computed here (note that the 'scale' setting will need to be changed if
  207. * 'super' is increased).
  208. *
  209. * The code also calculates a sum to the edge of the filter. This is not
  210. * currently used by could be used to optimize the calculation.
  211. */
  212. #if 0 /* bc code */
  213. scale=10
  214. super=8
  215. define bicubic(x) {
  216. if (x <= 1) return (1.5*x - 2.5)*x*x + 1;
  217. if (x < 2) return (((2.5 - 0.5*x)*x - 4)*x + 2);
  218. return 0;
  219. }
  220. define sum(x) {
  221. auto s;
  222. s = 0;
  223. while (x < 2*super) {
  224. s = s + bicubic(x/super);
  225. x = x + 1;
  226. }
  227. return s;
  228. }
  229. define results(x) {
  230. auto b, s;
  231. b = bicubic(x/super);
  232. s = sum(x);
  233. print " /*", x, "*/ { ", b, ", ", s, " }";
  234. return 1;
  235. }
  236. x=0
  237. while (x<2*super) {
  238. x = x + results(x)
  239. if (x < 2*super) print ","
  240. print "\n"
  241. }
  242. quit
  243. #endif
  244. #define BICUBIC1(x) /* |x| <= 1 */ ((1.5*(x)* - 2.5)*(x)*(x) + 1)
  245. #define BICUBIC2(x) /* 1 < |x| < 2 */ (((2.5 - 0.5*(x))*(x) - 4)*(x) + 2)
  246. #define FILTER_WEIGHT 9 /* Twice the first sum below */
  247. #define FILTER_WIDTH 2 /* Actually half the width; -2..+2 */
  248. #define FILTER_STEPS 8 /* steps per filter unit */
  249. static const double
  250. bicubic[16][2] =
  251. {
  252. /* These numbers are exact; the weight for the filter is 1/9, but this
  253. * would make the numbers inexact, so it is not included here.
  254. */
  255. /* bicubic sum */
  256. /* 0*/ { 1.0000000000, 4.5000000000 },
  257. /* 1*/ { .9638671875, 3.5000000000 },
  258. /* 2*/ { .8671875000, 2.5361328125 },
  259. /* 3*/ { .7275390625, 1.6689453125 },
  260. /* 4*/ { .5625000000, .9414062500 },
  261. /* 5*/ { .3896484375, .3789062500 },
  262. /* 6*/ { .2265625000, -.0107421875 },
  263. /* 7*/ { .0908203125, -.2373046875 },
  264. /* 8*/ { 0, -.3281250000 },
  265. /* 9*/ { -.0478515625, -.3281250000 },
  266. /*10*/ { -.0703125000, -.2802734375 },
  267. /*11*/ { -.0732421875, -.2099609375 },
  268. /*12*/ { -.0625000000, -.1367187500 },
  269. /*13*/ { -.0439453125, -.0742187500 },
  270. /*14*/ { -.0234375000, -.0302734375 },
  271. /*15*/ { -.0068359375, -.0068359375 }
  272. };
  273. static double
  274. alpha_calc(const struct arg *arg, double x, double y)
  275. {
  276. /* For [x-2..x+2],[y-2,y+2] calculate the weighted bicubic given a function
  277. * which tells us whether a point is inside or outside the shape. First
  278. * check if we need to do this at all:
  279. */
  280. switch (arg->check_fn(arg, x, y))
  281. {
  282. case OUTSIDE:
  283. return 0; /* all samples outside the shape */
  284. case INSIDE:
  285. return 1; /* all samples inside the shape */
  286. default:
  287. {
  288. int dy;
  289. double alpha = 0;
  290. # define FILTER_D (FILTER_WIDTH*FILTER_STEPS-1)
  291. for (dy=-FILTER_D; dy<=FILTER_D; ++dy)
  292. {
  293. double wy = bicubic[abs(dy)][0];
  294. if (wy != 0)
  295. {
  296. double alphay = 0;
  297. int dx;
  298. for (dx=-FILTER_D; dx<=FILTER_D; ++dx)
  299. {
  300. double wx = bicubic[abs(dx)][0];
  301. if (wx != 0 && arg->inside_fn(arg, x+dx/16, y+dy/16))
  302. alphay += wx;
  303. }
  304. alpha += wy * alphay;
  305. }
  306. }
  307. /* This needs to be weighted for each dimension: */
  308. return alpha / (FILTER_WEIGHT*FILTER_WEIGHT);
  309. }
  310. }
  311. }
  312. /* These are the shape functions. */
  313. /* "square",
  314. * { inside_square_filled, check_square_filled },
  315. * { inside_square, check_square }
  316. */
  317. static int
  318. square_check(double x, double y, double x1, double y1, double x2, double y2)
  319. /* Is x,y inside the square (x1,y1)..(x2,y2)? */
  320. {
  321. /* Do a modified Cohen-Sutherland on one point, bit patterns that indicate
  322. * 'outside' are:
  323. *
  324. * x<x1 | x<y1 | x<x2 | x<y2
  325. * 0 x 0 x To the right
  326. * 1 x 1 x To the left
  327. * x 0 x 0 Below
  328. * x 1 x 1 Above
  329. *
  330. * So 'inside' is (x<x1) != (x<x2) && (y<y1) != (y<y2);
  331. */
  332. return ((x<x1) ^ (x<x2)) & ((y<y1) ^ (y<y2));
  333. }
  334. static int
  335. inside_square_filled(const struct arg *arg, double x, double y)
  336. {
  337. return square_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2);
  338. }
  339. static int
  340. square_check_line(const struct arg *arg, double x, double y, double w)
  341. /* Check for a point being inside the boundaries implied by the given arg
  342. * and assuming a width 2*w each side of the boundaries. This returns the
  343. * 'check' INSIDE/OUTSIDE/0 result but note the semantics:
  344. *
  345. * +--------------+
  346. * | | OUTSIDE
  347. * | INSIDE |
  348. * | |
  349. * +--------------+
  350. *
  351. * And '0' means within the line boundaries.
  352. */
  353. {
  354. double cx = (arg->x1+arg->x2)/2;
  355. double wx = fabs(arg->x1-arg->x2)/2;
  356. double cy = (arg->y1+arg->y2)/2;
  357. double wy = fabs(arg->y1-arg->y2)/2;
  358. if (square_check(x, y, cx-wx-w, cy-wy-w, cx+wx+w, cy+wy+w))
  359. {
  360. /* Inside, but maybe too far; check for the redundant case where
  361. * the lines overlap:
  362. */
  363. wx -= w;
  364. wy -= w;
  365. if (wx > 0 && wy > 0 && square_check(x, y, cx-wx, cy-wy, cx+wx, cy+wy))
  366. return INSIDE; /* between (inside) the boundary lines. */
  367. return 0; /* inside the lines themselves. */
  368. }
  369. return OUTSIDE; /* outside the boundary lines. */
  370. }
  371. static int
  372. check_square_filled(const struct arg *arg, double x, double y)
  373. {
  374. /* The filter extends +/-FILTER_WIDTH each side of each output point, so
  375. * the check has to expand and contract the square by that amount; '0'
  376. * means close enough to the edge of the square that the bicubic filter has
  377. * to be run, OUTSIDE means alpha==0, INSIDE means alpha==1.
  378. */
  379. return square_check_line(arg, x, y, FILTER_WIDTH);
  380. }
  381. static int
  382. inside_square(const struct arg *arg, double x, double y)
  383. {
  384. /* Return true if within the drawn lines, else false, no need to distinguish
  385. * INSIDE vs OUTSIDE here:
  386. */
  387. return square_check_line(arg, x, y, arg->width/2) == 0;
  388. }
  389. static int
  390. check_square(const struct arg *arg, double x, double y)
  391. {
  392. /* So for this function a result of 'INSIDE' means inside the actual lines.
  393. */
  394. double w = arg->width/2;
  395. if (square_check_line(arg, x, y, w+FILTER_WIDTH) == 0)
  396. {
  397. /* Somewhere close to the boundary lines. If far enough inside one of
  398. * them then we can return INSIDE:
  399. */
  400. w -= FILTER_WIDTH;
  401. if (w > 0 && square_check_line(arg, x, y, w) == 0)
  402. return INSIDE;
  403. /* Point is somewhere in the filter region: */
  404. return 0;
  405. }
  406. else /* Inside or outside the square by more than w+FILTER_WIDTH. */
  407. return OUTSIDE;
  408. }
  409. /* "circle",
  410. * { inside_circle_filled, check_circle_filled },
  411. * { inside_circle, check_circle }
  412. *
  413. * The functions here are analoguous to the square ones; however, they check
  414. * the corresponding ellipse as opposed to the rectangle.
  415. */
  416. static int
  417. circle_check(double x, double y, double x1, double y1, double x2, double y2)
  418. {
  419. if (square_check(x, y, x1, y1, x2, y2))
  420. {
  421. /* Inside the square, so maybe inside the circle too: */
  422. const double cx = (x1 + x2)/2;
  423. const double cy = (y1 + y2)/2;
  424. const double dx = x1 - x2;
  425. const double dy = y1 - y2;
  426. x = (x - cx)/dx;
  427. y = (y - cy)/dy;
  428. /* It is outside if the distance from the center is more than half the
  429. * diameter:
  430. */
  431. return x*x+y*y < .25;
  432. }
  433. return 0; /* outside */
  434. }
  435. static int
  436. inside_circle_filled(const struct arg *arg, double x, double y)
  437. {
  438. return circle_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2);
  439. }
  440. static int
  441. circle_check_line(const struct arg *arg, double x, double y, double w)
  442. /* Check for a point being inside the boundaries implied by the given arg
  443. * and assuming a width 2*w each side of the boundaries. This function has
  444. * the same semantic as square_check_line but tests the circle.
  445. */
  446. {
  447. double cx = (arg->x1+arg->x2)/2;
  448. double wx = fabs(arg->x1-arg->x2)/2;
  449. double cy = (arg->y1+arg->y2)/2;
  450. double wy = fabs(arg->y1-arg->y2)/2;
  451. if (circle_check(x, y, cx-wx-w, cy-wy-w, cx+wx+w, cy+wy+w))
  452. {
  453. /* Inside, but maybe too far; check for the redundant case where
  454. * the lines overlap:
  455. */
  456. wx -= w;
  457. wy -= w;
  458. if (wx > 0 && wy > 0 && circle_check(x, y, cx-wx, cy-wy, cx+wx, cy+wy))
  459. return INSIDE; /* between (inside) the boundary lines. */
  460. return 0; /* inside the lines themselves. */
  461. }
  462. return OUTSIDE; /* outside the boundary lines. */
  463. }
  464. static int
  465. check_circle_filled(const struct arg *arg, double x, double y)
  466. {
  467. return circle_check_line(arg, x, y, FILTER_WIDTH);
  468. }
  469. static int
  470. inside_circle(const struct arg *arg, double x, double y)
  471. {
  472. return circle_check_line(arg, x, y, arg->width/2) == 0;
  473. }
  474. static int
  475. check_circle(const struct arg *arg, double x, double y)
  476. {
  477. /* Exactly as the 'square' code. */
  478. double w = arg->width/2;
  479. if (circle_check_line(arg, x, y, w+FILTER_WIDTH) == 0)
  480. {
  481. w -= FILTER_WIDTH;
  482. if (w > 0 && circle_check_line(arg, x, y, w) == 0)
  483. return INSIDE;
  484. /* Point is somewhere in the filter region: */
  485. return 0;
  486. }
  487. else /* Inside or outside the square by more than w+FILTER_WIDTH. */
  488. return OUTSIDE;
  489. }
  490. /* "line",
  491. * { NULL, NULL }, There is no 'filled' line.
  492. * { inside_line, check_line }
  493. */
  494. static int
  495. line_check(double x, double y, double x1, double y1, double x2, double y2,
  496. double w, double expand)
  497. {
  498. /* Shift all the points to (arg->x1, arg->y1) */
  499. double lx = x2 - x1;
  500. double ly = y2 - y1;
  501. double len2 = lx*lx + ly*ly;
  502. double cross, dot;
  503. x -= x1;
  504. y -= y1;
  505. /* The dot product is the distance down the line, the cross product is
  506. * the distance away from the line:
  507. *
  508. * distance = |cross| / sqrt(len2)
  509. */
  510. cross = x * ly - y * lx;
  511. /* If 'distance' is more than w the point is definitely outside the line:
  512. *
  513. * distance >= w
  514. * |cross| >= w * sqrt(len2)
  515. * cross^2 >= w^2 * len2:
  516. */
  517. if (cross*cross >= (w+expand)*(w+expand)*len2)
  518. return 0; /* outside */
  519. /* Now find the distance *along* the line; this comes from the dot product
  520. * lx.x+ly.y. The actual distance (in pixels) is:
  521. *
  522. * distance = dot / sqrt(len2)
  523. */
  524. dot = lx * x + ly * y;
  525. /* The test for 'outside' is:
  526. *
  527. * distance < 0 || distance > sqrt(len2)
  528. * -> dot / sqrt(len2) > sqrt(len2)
  529. * -> dot > len2
  530. *
  531. * But 'expand' is used for the filter width and needs to be handled too:
  532. */
  533. return dot > -expand && dot < len2+expand;
  534. }
  535. static int
  536. inside_line(const struct arg *arg, double x, double y)
  537. {
  538. return line_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2, arg->width/2, 0);
  539. }
  540. static int
  541. check_line(const struct arg *arg, double x, double y)
  542. {
  543. /* The end caps of the line must be checked too; it's not enough just to
  544. * widen the line by FILTER_WIDTH; 'expand' exists for this purpose:
  545. */
  546. if (line_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2, arg->width/2,
  547. FILTER_WIDTH))
  548. {
  549. /* Inside the line+filter; far enough inside that the filter isn't
  550. * required?
  551. */
  552. if (arg->width > 2*FILTER_WIDTH &&
  553. line_check(x, y, arg->x1, arg->y1, arg->x2, arg->y2, arg->width/2,
  554. -FILTER_WIDTH))
  555. return INSIDE;
  556. return 0;
  557. }
  558. return OUTSIDE;
  559. }
  560. static const struct
  561. {
  562. const char *name;
  563. shape_fn_ptr function[2/*fill,line*/][2];
  564. # define FN_INSIDE 0
  565. # define FN_CHECK 1
  566. } shape_defs[] =
  567. {
  568. { "square",
  569. { { inside_square_filled, check_square_filled },
  570. { inside_square, check_square } }
  571. },
  572. { "circle",
  573. { { inside_circle_filled, check_circle_filled },
  574. { inside_circle, check_circle } }
  575. },
  576. { "line",
  577. { { NULL, NULL },
  578. { inside_line, check_line } }
  579. }
  580. };
  581. #define shape_count ((sizeof shape_defs)/(sizeof shape_defs[0]))
  582. static shape_fn_ptr
  583. shape_of(const char *arg, double width, int f)
  584. {
  585. unsigned int i;
  586. for (i=0; i<shape_count; ++i) if (strcmp(shape_defs[i].name, arg) == 0)
  587. {
  588. shape_fn_ptr fn = shape_defs[i].function[width != 0][f];
  589. if (fn != NULL)
  590. return fn;
  591. fprintf(stderr, "genpng: %s %s not supported\n",
  592. width == 0 ? "filled" : "unfilled", arg);
  593. exit(1);
  594. }
  595. fprintf(stderr, "genpng: %s: not a valid shape name\n", arg);
  596. exit(1);
  597. }
  598. static void
  599. parse_arg(struct arg *arg, const char **argv/*7 arguments*/)
  600. {
  601. /* shape ::= color width shape x1 y1 x2 y2 */
  602. arg->color = color_of(argv[0]);
  603. arg->width = width_of(argv[1]);
  604. arg->inside_fn = shape_of(argv[2], arg->width, FN_INSIDE);
  605. arg->check_fn = shape_of(argv[2], arg->width, FN_CHECK);
  606. arg->x1 = coordinate_of(argv[3]);
  607. arg->y1 = coordinate_of(argv[4]);
  608. arg->x2 = coordinate_of(argv[5]);
  609. arg->y2 = coordinate_of(argv[6]);
  610. }
  611. static png_uint_32
  612. read_wh(const char *name, const char *str)
  613. /* read a PNG width or height */
  614. {
  615. char *ep = NULL;
  616. unsigned long ul = strtoul(str, &ep, 10);
  617. if (ep != NULL && *ep == 0 && ul > 0 && ul <= 0x7fffffff)
  618. return (png_uint_32)/*SAFE*/ul;
  619. fprintf(stderr, "genpng: %s: invalid number %s\n", name, str);
  620. exit(1);
  621. }
  622. static void
  623. pixel(png_uint_16p p, struct arg *args, int nargs, double x, double y)
  624. {
  625. /* Fill in the pixel by checking each shape (args[nargs]) for effects on
  626. * the corresponding sample:
  627. */
  628. double r=0, g=0, b=0, a=0;
  629. while (--nargs >= 0 && a != 1)
  630. {
  631. /* NOTE: alpha_calc can return a value outside the range 0..1 with the
  632. * bicubic filter.
  633. */
  634. const double alpha = alpha_calc(args+nargs, x, y) * (1-a);
  635. r += alpha * args[nargs].color->red;
  636. g += alpha * args[nargs].color->green;
  637. b += alpha * args[nargs].color->blue;
  638. a += alpha;
  639. }
  640. /* 'a' may be negative or greater than 1; if it is, negative clamp the
  641. * pixel to 0 if >1 clamp r/g/b:
  642. */
  643. if (a > 0)
  644. {
  645. if (a > 1)
  646. {
  647. if (r > 1) r = 1;
  648. if (g > 1) g = 1;
  649. if (b > 1) b = 1;
  650. a = 1;
  651. }
  652. /* And fill in the pixel: */
  653. p[0] = (png_uint_16)/*SAFE*/round(r * 65535);
  654. p[1] = (png_uint_16)/*SAFE*/round(g * 65535);
  655. p[2] = (png_uint_16)/*SAFE*/round(b * 65535);
  656. p[3] = (png_uint_16)/*SAFE*/round(a * 65535);
  657. }
  658. else
  659. p[3] = p[2] = p[1] = p[0] = 0;
  660. }
  661. int
  662. main(int argc, const char **argv)
  663. {
  664. int convert_to_8bit = 0;
  665. /* There is one option: --8bit: */
  666. if (argc > 1 && strcmp(argv[1], "--8bit") == 0)
  667. --argc, ++argv, convert_to_8bit = 1;
  668. if (argc >= 3)
  669. {
  670. png_uint_16p buffer;
  671. int nshapes;
  672. png_image image;
  673. # define max_shapes 256
  674. struct arg arg_list[max_shapes];
  675. /* The libpng Simplified API write code requires a fully initialized
  676. * structure.
  677. */
  678. memset(&image, 0, sizeof image);
  679. image.version = PNG_IMAGE_VERSION;
  680. image.opaque = NULL;
  681. image.width = read_wh("width", argv[1]);
  682. image.height = read_wh("height", argv[2]);
  683. image.format = PNG_FORMAT_LINEAR_RGB_ALPHA;
  684. image.flags = 0;
  685. image.colormap_entries = 0;
  686. /* Check the remainder of the arguments */
  687. for (nshapes=0; 3+7*(nshapes+1) <= argc && nshapes < max_shapes;
  688. ++nshapes)
  689. parse_arg(arg_list+nshapes, argv+3+7*nshapes);
  690. if (3+7*nshapes != argc)
  691. {
  692. fprintf(stderr, "genpng: %s: too many arguments\n", argv[3+7*nshapes]);
  693. return 1;
  694. }
  695. /* Create the buffer: */
  696. buffer = malloc(PNG_IMAGE_SIZE(image));
  697. if (buffer != NULL)
  698. {
  699. png_uint_32 y;
  700. /* Write each row... */
  701. for (y=0; y<image.height; ++y)
  702. {
  703. png_uint_32 x;
  704. /* Each pixel in each row: */
  705. for (x=0; x<image.width; ++x)
  706. pixel(buffer + 4*(x + y*image.width), arg_list, nshapes, x, y);
  707. }
  708. /* Write the result (to stdout) */
  709. if (png_image_write_to_stdio(&image, stdout, convert_to_8bit,
  710. buffer, 0/*row_stride*/, NULL/*colormap*/))
  711. {
  712. free(buffer);
  713. return 0; /* success */
  714. }
  715. else
  716. fprintf(stderr, "genpng: write stdout: %s\n", image.message);
  717. free(buffer);
  718. }
  719. else
  720. fprintf(stderr, "genpng: out of memory: %lu bytes\n",
  721. (unsigned long)PNG_IMAGE_SIZE(image));
  722. }
  723. else
  724. {
  725. /* Wrong number of arguments */
  726. fprintf(stderr, "genpng: usage: genpng [--8bit] width height {shape}\n"
  727. " Generate a transparent PNG in RGBA (truecolor+alpha) format\n"
  728. " containing the given shape or shapes. Shapes are defined:\n"
  729. "\n"
  730. " shape ::= color width shape x1 y1 x2 y2\n"
  731. " color ::= black|white|red|green|yellow|blue\n"
  732. " color ::= brown|purple|pink|orange|gray|cyan\n"
  733. " width ::= filled|<number>\n"
  734. " shape ::= circle|square|line\n"
  735. " x1,x2 ::= <number>\n"
  736. " y1,y2 ::= <number>\n"
  737. "\n"
  738. " Numbers are floating point numbers describing points relative to\n"
  739. " the top left of the output PNG as pixel coordinates. The 'width'\n"
  740. " parameter is either the width of the line (in output pixels) used\n"
  741. " to draw the shape or 'filled' to indicate that the shape should\n"
  742. " be filled with the color.\n"
  743. "\n"
  744. " Colors are interpreted loosely to give access to the eight full\n"
  745. " intensity RGB values:\n"
  746. "\n"
  747. " black, red, green, blue, yellow, cyan, purple, white,\n"
  748. "\n"
  749. " Cyan is full intensity blue+green; RGB(0,1,1), plus the following\n"
  750. " lower intensity values:\n"
  751. "\n"
  752. " brown: red+orange: RGB(0.5, 0.125, 0) (dark red+orange)\n"
  753. " pink: red+white: RGB(1.0, 0.5, 0.5)\n"
  754. " orange: red+yellow: RGB(1.0, 0.5, 0)\n"
  755. " gray: black+white: RGB(0.5, 0.5, 0.5)\n"
  756. "\n"
  757. " The RGB values are selected to make detection of aliasing errors\n"
  758. " easy. The names are selected to make the description of errors\n"
  759. " easy.\n"
  760. "\n"
  761. " The PNG is written to stdout, if --8bit is given a 32bpp RGBA sRGB\n"
  762. " file is produced, otherwise a 64bpp RGBA linear encoded file is\n"
  763. " written.\n");
  764. }
  765. return 1;
  766. }
  767. #endif /* SIMPLIFIED_WRITE && STDIO */