|
fukasawa |
e60969 |
/*- simpleover
|
|
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 |
* Read several PNG files, which should have an alpha channel or transparency
|
|
fukasawa |
e60969 |
* information, and composite them together to produce one or more 16-bit linear
|
|
fukasawa |
e60969 |
* RGBA intermediates. This involves doing the correct 'over' composition to
|
|
fukasawa |
e60969 |
* combine the alpha channels and corresponding data.
|
|
fukasawa |
e60969 |
*
|
|
fukasawa |
e60969 |
* Finally read an output (background) PNG using the 24-bit RGB format (the
|
|
fukasawa |
e60969 |
* PNG will be composited on green (#00ff00) by default if it has an alpha
|
|
fukasawa |
e60969 |
* channel), and apply the intermediate image generated above to specified
|
|
fukasawa |
e60969 |
* locations in the image.
|
|
fukasawa |
e60969 |
*
|
|
fukasawa |
e60969 |
* The command line has the general format:
|
|
fukasawa |
e60969 |
*
|
|
fukasawa |
e60969 |
* simpleover <background.png> [output.png]</background.png>
|
|
fukasawa |
e60969 |
* {--sprite=width,height,name {[--at=x,y] {sprite.png}}}
|
|
fukasawa |
e60969 |
* {--add=name {x,y}}
|
|
fukasawa |
e60969 |
*
|
|
fukasawa |
e60969 |
* The --sprite and --add options may occur multiple times. They are executed
|
|
fukasawa |
e60969 |
* in order. --add may refer to any sprite already read.
|
|
fukasawa |
e60969 |
*
|
|
fukasawa |
e60969 |
* This code is intended to show how to composite multiple images together
|
|
fukasawa |
e60969 |
* correctly. Apart from the libpng Simplified API the only work done in here
|
|
fukasawa |
e60969 |
* is to combine multiple input PNG images into a single sprite; this involves
|
|
fukasawa |
e60969 |
* a Porter-Duff 'over' operation and the input PNG images may, as a result,
|
|
fukasawa |
e60969 |
* be regarded as being layered one on top of the other with the first (leftmost
|
|
fukasawa |
e60969 |
* on the command line) being at the bottom and the last on the top.
|
|
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 <errno.h></errno.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, so long as this
|
|
fukasawa |
e60969 |
* file is linked against a sufficiently recent libpng (1.6+) it is ok to
|
|
fukasawa |
e60969 |
* change this to <png.h>:</png.h>
|
|
fukasawa |
e60969 |
*/
|
|
fukasawa |
e60969 |
#include "../../png.h"
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
#ifdef PNG_SIMPLIFIED_READ_SUPPORTED
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
#define sprite_name_chars 15
|
|
fukasawa |
e60969 |
struct sprite {
|
|
fukasawa |
e60969 |
FILE *file;
|
|
fukasawa |
e60969 |
png_uint_16p buffer;
|
|
fukasawa |
e60969 |
unsigned int width;
|
|
fukasawa |
e60969 |
unsigned int height;
|
|
fukasawa |
e60969 |
char name[sprite_name_chars+1];
|
|
fukasawa |
e60969 |
};
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
#if 0 /* div by 65535 test program */
|
|
fukasawa |
e60969 |
#include <math.h></math.h>
|
|
fukasawa |
e60969 |
#include <stdio.h></stdio.h>
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
int main(void) {
|
|
fukasawa |
e60969 |
double err = 0;
|
|
fukasawa |
e60969 |
unsigned int xerr = 0;
|
|
fukasawa |
e60969 |
unsigned int r = 32769;
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
unsigned int x = 0;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
do {
|
|
fukasawa |
e60969 |
unsigned int t = x + (x >> 16) /*+ (x >> 31)*/ + r;
|
|
fukasawa |
e60969 |
double v = x, errtest;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (t < x) {
|
|
fukasawa |
e60969 |
fprintf(stderr, "overflow: %u+%u -> %u\n", x, r, t);
|
|
fukasawa |
e60969 |
return 1;
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
v /= 65535;
|
|
fukasawa |
e60969 |
errtest = v;
|
|
fukasawa |
e60969 |
t >>= 16;
|
|
fukasawa |
e60969 |
errtest -= t;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (errtest > err) {
|
|
fukasawa |
e60969 |
err = errtest;
|
|
fukasawa |
e60969 |
xerr = x;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (errtest >= .5) {
|
|
fukasawa |
e60969 |
fprintf(stderr, "error: %u/65535 = %f, not %u, error %f\n",
|
|
fukasawa |
e60969 |
x, v, t, errtest);
|
|
fukasawa |
e60969 |
return 0;
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
} while (++x <= 65535U*65535U);
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
printf("error %f @ %u\n", err, xerr);
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
return 0;
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
#endif /* div by 65535 test program */
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
static void
|
|
fukasawa |
e60969 |
sprite_op(const struct sprite *sprite, int x_offset, int y_offset,
|
|
fukasawa |
e60969 |
png_imagep image, const png_uint_16 *buffer)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
/* This is where the Porter-Duff 'Over' operator is evaluated; change this
|
|
fukasawa |
e60969 |
* code to change the operator (this could be parameterized). Any other
|
|
fukasawa |
e60969 |
* image processing operation could be used here.
|
|
fukasawa |
e60969 |
*/
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
/* Check for an x or y offset that pushes any part of the image beyond the
|
|
fukasawa |
e60969 |
* right or bottom of the sprite:
|
|
fukasawa |
e60969 |
*/
|
|
fukasawa |
e60969 |
if ((y_offset < 0 || (unsigned)/*SAFE*/y_offset < sprite->height) &&
|
|
fukasawa |
e60969 |
(x_offset < 0 || (unsigned)/*SAFE*/x_offset < sprite->width))
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
unsigned int y = 0;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (y_offset < 0)
|
|
fukasawa |
e60969 |
y = -y_offset; /* Skip to first visible row */
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
do
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
unsigned int x = 0;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (x_offset < 0)
|
|
fukasawa |
e60969 |
x = -x_offset;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
do
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
/* In and out are RGBA values, so: */
|
|
fukasawa |
e60969 |
const png_uint_16 *in_pixel = buffer + (y * image->width + x)*4;
|
|
fukasawa |
e60969 |
png_uint_32 in_alpha = in_pixel[3];
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
/* This is the optimized Porter-Duff 'Over' operation, when the
|
|
fukasawa |
e60969 |
* input alpha is 0 the output is not changed.
|
|
fukasawa |
e60969 |
*/
|
|
fukasawa |
e60969 |
if (in_alpha > 0)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
png_uint_16 *out_pixel = sprite->buffer +
|
|
fukasawa |
e60969 |
((y+y_offset) * sprite->width + (x+x_offset))*4;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
/* This is the weight to apply to the output: */
|
|
fukasawa |
e60969 |
in_alpha = 65535-in_alpha;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (in_alpha > 0)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
/* The input must be composed onto the output. This means
|
|
fukasawa |
e60969 |
* multiplying the current output pixel value by the inverse
|
|
fukasawa |
e60969 |
* of the input alpha (1-alpha). A division is required but
|
|
fukasawa |
e60969 |
* it is by the constant 65535. Approximate this as:
|
|
fukasawa |
e60969 |
*
|
|
fukasawa |
e60969 |
* (x + (x >> 16) + 32769) >> 16;
|
|
fukasawa |
e60969 |
*
|
|
fukasawa |
e60969 |
* This is exact (and does not overflow) for all values of
|
|
fukasawa |
e60969 |
* x in the range 0..65535*65535. (Note that the calculation
|
|
fukasawa |
e60969 |
* produces the closest integer; the maximum error is <0.5).
|
|
fukasawa |
e60969 |
*/
|
|
fukasawa |
e60969 |
png_uint_32 tmp;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
# define compose(c)\
|
|
fukasawa |
e60969 |
tmp = out_pixel[c] * in_alpha;\
|
|
fukasawa |
e60969 |
tmp = (tmp + (tmp >> 16) + 32769) >> 16;\
|
|
fukasawa |
e60969 |
out_pixel[c] = tmp + in_pixel[c]
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
/* The following is very vectorizable... */
|
|
fukasawa |
e60969 |
compose(0);
|
|
fukasawa |
e60969 |
compose(1);
|
|
fukasawa |
e60969 |
compose(2);
|
|
fukasawa |
e60969 |
compose(3);
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
else
|
|
fukasawa |
e60969 |
out_pixel[0] = in_pixel[0],
|
|
fukasawa |
e60969 |
out_pixel[1] = in_pixel[1],
|
|
fukasawa |
e60969 |
out_pixel[2] = in_pixel[2],
|
|
fukasawa |
e60969 |
out_pixel[3] = in_pixel[3];
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
while (++x < image->width);
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
while (++y < image->height);
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
static int
|
|
fukasawa |
e60969 |
create_sprite(struct sprite *sprite, int *argc, const char ***argv)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
/* Read the arguments and create this sprite. The sprite buffer has already
|
|
fukasawa |
e60969 |
* been allocated. This reads the input PNGs one by one in linear format,
|
|
fukasawa |
e60969 |
* composes them onto the sprite buffer (the code in the function above)
|
|
fukasawa |
e60969 |
* then saves the result, converting it on the fly to PNG RGBA 8-bit format.
|
|
fukasawa |
e60969 |
*/
|
|
fukasawa |
e60969 |
while (*argc > 0)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
char tombstone;
|
|
fukasawa |
e60969 |
int x = 0, y = 0;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if ((*argv)[0][0] == '-' && (*argv)[0][1] == '-')
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
/* The only supported option is --at. */
|
|
fukasawa |
e60969 |
if (sscanf((*argv)[0], "--at=%d,%d%c", &x, &y, &tombstone) != 2)
|
|
fukasawa |
e60969 |
break; /* success; caller will parse this option */
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
++*argv, --*argc;
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
else
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
/* The argument has to be a file name */
|
|
fukasawa |
e60969 |
png_image image;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
image.version = PNG_IMAGE_VERSION;
|
|
fukasawa |
e60969 |
image.opaque = NULL;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (png_image_begin_read_from_file(&image, (*argv)[0]))
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
png_uint_16p buffer;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
image.format = PNG_FORMAT_LINEAR_RGB_ALPHA;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
buffer = malloc(PNG_IMAGE_SIZE(image));
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (buffer != NULL)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
if (png_image_finish_read(&image, NULL/*background*/, buffer,
|
|
fukasawa |
e60969 |
0/*row_stride*/,
|
|
fukasawa |
e60969 |
NULL/*colormap for PNG_FORMAT_FLAG_COLORMAP*/))
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
/* This is the place where the Porter-Duff 'Over' operator
|
|
fukasawa |
e60969 |
* needs to be done by this code. In fact, any image
|
|
fukasawa |
e60969 |
* processing required can be done here; the data is in
|
|
fukasawa |
e60969 |
* the correct format (linear, 16-bit) and source and
|
|
fukasawa |
e60969 |
* destination are in memory.
|
|
fukasawa |
e60969 |
*/
|
|
fukasawa |
e60969 |
sprite_op(sprite, x, y, &image, buffer);
|
|
fukasawa |
e60969 |
free(buffer);
|
|
fukasawa |
e60969 |
++*argv, --*argc;
|
|
fukasawa |
e60969 |
/* And continue to the next argument */
|
|
fukasawa |
e60969 |
continue;
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
else
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
free(buffer);
|
|
fukasawa |
e60969 |
fprintf(stderr, "simpleover: read %s: %s\n", (*argv)[0],
|
|
fukasawa |
e60969 |
image.message);
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
else
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
fprintf(stderr, "simpleover: out of memory: %lu bytes\n",
|
|
fukasawa |
e60969 |
(unsigned long)PNG_IMAGE_SIZE(image));
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
/* png_image_free must be called if we abort the Simplified API
|
|
fukasawa |
e60969 |
* read because of a problem detected in this code. If problems
|
|
fukasawa |
e60969 |
* are detected in the Simplified API it cleans up itself.
|
|
fukasawa |
e60969 |
*/
|
|
fukasawa |
e60969 |
png_image_free(&image);
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
else
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
/* Failed to read the first argument: */
|
|
fukasawa |
e60969 |
fprintf(stderr, "simpleover: %s: %s\n", (*argv)[0], image.message);
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
return 0; /* failure */
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
/* All the sprite operations have completed successfully. Save the RGBA
|
|
fukasawa |
e60969 |
* buffer as a PNG using the simplified write API.
|
|
fukasawa |
e60969 |
*/
|
|
fukasawa |
e60969 |
sprite->file = tmpfile();
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (sprite->file != NULL)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
png_image save;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
memset(&save, 0, sizeof save);
|
|
fukasawa |
e60969 |
save.version = PNG_IMAGE_VERSION;
|
|
fukasawa |
e60969 |
save.opaque = NULL;
|
|
fukasawa |
e60969 |
save.width = sprite->width;
|
|
fukasawa |
e60969 |
save.height = sprite->height;
|
|
fukasawa |
e60969 |
save.format = PNG_FORMAT_LINEAR_RGB_ALPHA;
|
|
fukasawa |
e60969 |
save.flags = PNG_IMAGE_FLAG_FAST;
|
|
fukasawa |
e60969 |
save.colormap_entries = 0;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (png_image_write_to_stdio(&save, sprite->file, 1/*convert_to_8_bit*/,
|
|
fukasawa |
e60969 |
sprite->buffer, 0/*row_stride*/, NULL/*colormap*/))
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
/* Success; the buffer is no longer needed: */
|
|
fukasawa |
e60969 |
free(sprite->buffer);
|
|
fukasawa |
e60969 |
sprite->buffer = NULL;
|
|
fukasawa |
e60969 |
return 1; /* ok */
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
else
|
|
fukasawa |
e60969 |
fprintf(stderr, "simpleover: write sprite %s: %s\n", sprite->name,
|
|
fukasawa |
e60969 |
save.message);
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
else
|
|
fukasawa |
e60969 |
fprintf(stderr, "simpleover: sprite %s: could not allocate tmpfile: %s\n",
|
|
fukasawa |
e60969 |
sprite->name, strerror(errno));
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
return 0; /* fail */
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
static int
|
|
fukasawa |
e60969 |
add_sprite(png_imagep output, png_bytep out_buf, struct sprite *sprite,
|
|
fukasawa |
e60969 |
int *argc, const char ***argv)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
/* Given a --add argument naming this sprite, perform the operations listed
|
|
fukasawa |
e60969 |
* in the following arguments. The arguments are expected to have the form
|
|
fukasawa |
e60969 |
* (x,y), which is just an offset at which to add the sprite to the
|
|
fukasawa |
e60969 |
* output.
|
|
fukasawa |
e60969 |
*/
|
|
fukasawa |
e60969 |
while (*argc > 0)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
char tombstone;
|
|
fukasawa |
e60969 |
int x, y;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if ((*argv)[0][0] == '-' && (*argv)[0][1] == '-')
|
|
fukasawa |
e60969 |
return 1; /* success */
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (sscanf((*argv)[0], "%d,%d%c", &x, &y, &tombstone) == 2)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
/* Now add the new image into the sprite data, but only if it
|
|
fukasawa |
e60969 |
* will fit.
|
|
fukasawa |
e60969 |
*/
|
|
fukasawa |
e60969 |
if (x < 0 || y < 0 ||
|
|
fukasawa |
e60969 |
(unsigned)/*SAFE*/x >= output->width ||
|
|
fukasawa |
e60969 |
(unsigned)/*SAFE*/y >= output->height ||
|
|
fukasawa |
e60969 |
sprite->width > output->width-x ||
|
|
fukasawa |
e60969 |
sprite->height > output->height-y)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
fprintf(stderr, "simpleover: sprite %s @ (%d,%d) outside image\n",
|
|
fukasawa |
e60969 |
sprite->name, x, y);
|
|
fukasawa |
e60969 |
/* Could just skip this, but for the moment it is an error */
|
|
fukasawa |
e60969 |
return 0; /* error */
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
else
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
/* Since we know the sprite fits we can just read it into the
|
|
fukasawa |
e60969 |
* output using the simplified API.
|
|
fukasawa |
e60969 |
*/
|
|
fukasawa |
e60969 |
png_image in;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
in.version = PNG_IMAGE_VERSION;
|
|
fukasawa |
e60969 |
rewind(sprite->file);
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (png_image_begin_read_from_stdio(&in, sprite->file))
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
in.format = PNG_FORMAT_RGB; /* force compose */
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (png_image_finish_read(&in, NULL/*background*/,
|
|
fukasawa |
e60969 |
out_buf + (y*output->width + x)*3/*RGB*/,
|
|
fukasawa |
e60969 |
output->width*3/*row_stride*/,
|
|
fukasawa |
e60969 |
NULL/*colormap for PNG_FORMAT_FLAG_COLORMAP*/))
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
++*argv, --*argc;
|
|
fukasawa |
e60969 |
continue;
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
/* The read failed: */
|
|
fukasawa |
e60969 |
fprintf(stderr, "simpleover: add sprite %s: %s\n", sprite->name,
|
|
fukasawa |
e60969 |
in.message);
|
|
fukasawa |
e60969 |
return 0; /* error */
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
else
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
fprintf(stderr, "simpleover: --add='%s': invalid position %s\n",
|
|
fukasawa |
e60969 |
sprite->name, (*argv)[0]);
|
|
fukasawa |
e60969 |
return 0; /* error */
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
return 1; /* ok */
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
static int
|
|
fukasawa |
e60969 |
simpleover_process(png_imagep output, png_bytep out_buf, int argc,
|
|
fukasawa |
e60969 |
const char **argv)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
int result = 1; /* success */
|
|
fukasawa |
e60969 |
# define csprites 10/*limit*/
|
|
fukasawa |
e60969 |
# define str(a) #a
|
|
fukasawa |
e60969 |
int nsprites = 0;
|
|
fukasawa |
e60969 |
struct sprite sprites[csprites];
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
while (argc > 0)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
result = 0; /* fail */
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (strncmp(argv[0], "--sprite=", 9) == 0)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
char tombstone;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (nsprites < csprites)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
int n;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
sprites[nsprites].width = sprites[nsprites].height = 0;
|
|
fukasawa |
e60969 |
sprites[nsprites].name[0] = 0;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
n = sscanf(argv[0], "--sprite=%u,%u,%" str(sprite_name_chars) "s%c",
|
|
fukasawa |
e60969 |
&sprites[nsprites].width, &sprites[nsprites].height,
|
|
fukasawa |
e60969 |
sprites[nsprites].name, &tombstone);
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if ((n == 2 || n == 3) &&
|
|
fukasawa |
e60969 |
sprites[nsprites].width > 0 && sprites[nsprites].height > 0)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
size_t buf_size, tmp;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
/* Default a name if not given. */
|
|
fukasawa |
e60969 |
if (sprites[nsprites].name[0] == 0)
|
|
fukasawa |
e60969 |
sprintf(sprites[nsprites].name, "sprite-%d", nsprites+1);
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
/* Allocate a buffer for the sprite and calculate the buffer
|
|
fukasawa |
e60969 |
* size:
|
|
fukasawa |
e60969 |
*/
|
|
fukasawa |
e60969 |
buf_size = sizeof (png_uint_16 [4]);
|
|
fukasawa |
e60969 |
buf_size *= sprites[nsprites].width;
|
|
fukasawa |
e60969 |
buf_size *= sprites[nsprites].height;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
/* This can overflow a (size_t); check for this: */
|
|
fukasawa |
e60969 |
tmp = buf_size;
|
|
fukasawa |
e60969 |
tmp /= sprites[nsprites].width;
|
|
fukasawa |
e60969 |
tmp /= sprites[nsprites].height;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (tmp == sizeof (png_uint_16 [4]))
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
sprites[nsprites].buffer = malloc(buf_size);
|
|
fukasawa |
e60969 |
/* This buffer must be initialized to transparent: */
|
|
fukasawa |
e60969 |
memset(sprites[nsprites].buffer, 0, buf_size);
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (sprites[nsprites].buffer != NULL)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
sprites[nsprites].file = NULL;
|
|
fukasawa |
e60969 |
++argv, --argc;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (create_sprite(sprites+nsprites++, &argc, &argv))
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
result = 1; /* still ok */
|
|
fukasawa |
e60969 |
continue;
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
break; /* error */
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
/* Overflow, or OOM */
|
|
fukasawa |
e60969 |
fprintf(stderr, "simpleover: %s: sprite too large\n", argv[0]);
|
|
fukasawa |
e60969 |
break;
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
else
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
fprintf(stderr, "simpleover: %s: invalid sprite (%u,%u)\n",
|
|
fukasawa |
e60969 |
argv[0], sprites[nsprites].width, sprites[nsprites].height);
|
|
fukasawa |
e60969 |
break;
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
else
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
fprintf(stderr, "simpleover: %s: too many sprites\n", argv[0]);
|
|
fukasawa |
e60969 |
break;
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
else if (strncmp(argv[0], "--add=", 6) == 0)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
const char *name = argv[0]+6;
|
|
fukasawa |
e60969 |
int isprite = nsprites;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
++argv, --argc;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
while (--isprite >= 0)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
if (strcmp(sprites[isprite].name, name) == 0)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
if (!add_sprite(output, out_buf, sprites+isprite, &argc, &argv))
|
|
fukasawa |
e60969 |
goto out; /* error in add_sprite */
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
break;
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (isprite < 0) /* sprite not found */
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
fprintf(stderr, "simpleover: --add='%s': sprite not found\n", name);
|
|
fukasawa |
e60969 |
break;
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
else
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
fprintf(stderr, "simpleover: %s: unrecognized operation\n", argv[0]);
|
|
fukasawa |
e60969 |
break;
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
result = 1; /* ok */
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
/* Clean up the cache of sprites: */
|
|
fukasawa |
e60969 |
out:
|
|
fukasawa |
e60969 |
while (--nsprites >= 0)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
if (sprites[nsprites].buffer != NULL)
|
|
fukasawa |
e60969 |
free(sprites[nsprites].buffer);
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (sprites[nsprites].file != NULL)
|
|
fukasawa |
e60969 |
(void)fclose(sprites[nsprites].file);
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
return result;
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
int main(int argc, const char **argv)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
int result = 1; /* default to fail */
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (argc >= 2)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
int argi = 2;
|
|
fukasawa |
e60969 |
const char *output = NULL;
|
|
fukasawa |
e60969 |
png_image image;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (argc > 2 && argv[2][0] != '-'/*an operation*/)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
output = argv[2];
|
|
fukasawa |
e60969 |
argi = 3;
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
image.version = PNG_IMAGE_VERSION;
|
|
fukasawa |
e60969 |
image.opaque = NULL;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (png_image_begin_read_from_file(&image, argv[1]))
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
png_bytep buffer;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
image.format = PNG_FORMAT_RGB; /* 24-bit RGB */
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
buffer = malloc(PNG_IMAGE_SIZE(image));
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (buffer != NULL)
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
png_color background = {0, 0xff, 0}; /* fully saturated green */
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
if (png_image_finish_read(&image, &background, buffer,
|
|
fukasawa |
e60969 |
0/*row_stride*/, NULL/*colormap for PNG_FORMAT_FLAG_COLORMAP */))
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
/* At this point png_image_finish_read has cleaned up the
|
|
fukasawa |
e60969 |
* allocated data in png_image, and only the buffer needs to be
|
|
fukasawa |
e60969 |
* freed.
|
|
fukasawa |
e60969 |
*
|
|
fukasawa |
e60969 |
* Perform the remaining operations:
|
|
fukasawa |
e60969 |
*/
|
|
fukasawa |
e60969 |
if (simpleover_process(&image, buffer, argc-argi, argv+argi))
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
/* Write the output: */
|
|
fukasawa |
e60969 |
if ((output != NULL &&
|
|
fukasawa |
e60969 |
png_image_write_to_file(&image, output,
|
|
fukasawa |
e60969 |
0/*convert_to_8bit*/, buffer, 0/*row_stride*/,
|
|
fukasawa |
e60969 |
NULL/*colormap*/)) ||
|
|
fukasawa |
e60969 |
(output == NULL &&
|
|
fukasawa |
e60969 |
png_image_write_to_stdio(&image, stdout,
|
|
fukasawa |
e60969 |
0/*convert_to_8bit*/, buffer, 0/*row_stride*/,
|
|
fukasawa |
e60969 |
NULL/*colormap*/)))
|
|
fukasawa |
e60969 |
result = 0;
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
else
|
|
fukasawa |
e60969 |
fprintf(stderr, "simpleover: write %s: %s\n",
|
|
fukasawa |
e60969 |
output == NULL ? "stdout" : output, image.message);
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
/* else simpleover_process writes an error message */
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
else
|
|
fukasawa |
e60969 |
fprintf(stderr, "simpleover: read %s: %s\n", argv[1],
|
|
fukasawa |
e60969 |
image.message);
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
free(buffer);
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
else
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
fprintf(stderr, "simpleover: out of memory: %lu bytes\n",
|
|
fukasawa |
e60969 |
(unsigned long)PNG_IMAGE_SIZE(image));
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
/* This is the only place where a 'free' is required; libpng does
|
|
fukasawa |
e60969 |
* the cleanup on error and success, but in this case we couldn't
|
|
fukasawa |
e60969 |
* complete the read because of running out of memory.
|
|
fukasawa |
e60969 |
*/
|
|
fukasawa |
e60969 |
png_image_free(&image);
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
else
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
/* Failed to read the first argument: */
|
|
fukasawa |
e60969 |
fprintf(stderr, "simpleover: %s: %s\n", argv[1], image.message);
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
else
|
|
fukasawa |
e60969 |
{
|
|
fukasawa |
e60969 |
/* Usage message */
|
|
fukasawa |
e60969 |
fprintf(stderr,
|
|
fukasawa |
e60969 |
"simpleover: usage: simpleover background.png [output.png]\n"
|
|
fukasawa |
e60969 |
" Output 'background.png' as a 24-bit RGB PNG file in 'output.png'\n"
|
|
fukasawa |
e60969 |
" or, if not given, stdout. 'background.png' will be composited\n"
|
|
fukasawa |
e60969 |
" on fully saturated green.\n"
|
|
fukasawa |
e60969 |
"\n"
|
|
fukasawa |
e60969 |
" Optionally, before output, process additional PNG files:\n"
|
|
fukasawa |
e60969 |
"\n"
|
|
fukasawa |
e60969 |
" --sprite=width,height,name {[--at=x,y] {sprite.png}}\n"
|
|
fukasawa |
e60969 |
" Produce a transparent sprite of size (width,height) and with\n"
|
|
fukasawa |
e60969 |
" name 'name'.\n"
|
|
fukasawa |
e60969 |
" For each sprite.png composite it using a Porter-Duff 'Over'\n"
|
|
fukasawa |
e60969 |
" operation at offset (x,y) in the sprite (defaulting to (0,0)).\n"
|
|
fukasawa |
e60969 |
" Input PNGs will be truncated to the area of the sprite.\n"
|
|
fukasawa |
e60969 |
"\n"
|
|
fukasawa |
e60969 |
" --add='name' {x,y}\n"
|
|
fukasawa |
e60969 |
" Optionally, before output, composite a sprite, 'name', which\n"
|
|
fukasawa |
e60969 |
" must have been previously produced using --sprite, at each\n"
|
|
fukasawa |
e60969 |
" offset (x,y) in the output image. Each sprite must fit\n"
|
|
fukasawa |
e60969 |
" completely within the output image.\n"
|
|
fukasawa |
e60969 |
"\n"
|
|
fukasawa |
e60969 |
" PNG files are processed in the order they occur on the command\n"
|
|
fukasawa |
e60969 |
" line and thus the first PNG processed appears as the bottommost\n"
|
|
fukasawa |
e60969 |
" in the output image.\n");
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
|
|
fukasawa |
e60969 |
return result;
|
|
fukasawa |
e60969 |
}
|
|
fukasawa |
e60969 |
#endif /* SIMPLIFIED_READ */
|