apng.c 6.8 KB
Newer Older
1
/*
James R. committed
2
Copyright 2019-2020, James R.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdlib.h>
#include <string.h>

#include "apng.h"

28 29
#define APNG_INFO_acTL 0x20000U

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#define APNG_WROTE_acTL 0x10000U

struct apng_info_def
{
	png_uint_32 mode;
	png_uint_32 valid;

	png_uint_32 num_frames;
	png_uint_32 num_plays;

	long start_acTL;/* acTL is written here */

	png_flush_ptr output_flush_fn;
	apng_seek_ptr output_seek_fn;
	apng_tell_ptr output_tell_fn;

	apng_set_acTL_ptr set_acTL_fn;
};

/* PROTOS (FUCK COMPILER) */
void   apng_seek  (png_structp, apng_const_infop, size_t);
size_t apng_tell  (png_structp, apng_const_infop);
#ifdef PNG_WRITE_FLUSH_SUPPORTED
void   apng_flush (png_structp, apng_infop);
#ifdef PNG_STDIO_SUPPORTED
void   apng_default_flush (png_structp);
#endif/* PNG_STDIO_SUPPORTED */
#endif/* PNG_WRITE_FLUSH_SUPPORTED */
#ifdef PNG_STDIO_SUPPORTED
void   apng_default_seek  (png_structp, size_t);
size_t apng_default_tell  (png_structp);
#endif/* PNG_STDIO_SUPPORTED */
void   apng_write_IEND (png_structp);
void   apng_write_acTL (png_structp, png_uint_32, png_uint_32);
64 65 66 67
#ifndef PNG_WRITE_APNG_SUPPORTED
png_uint_32 apng_set_acTL_dummy (png_structp, png_infop,
		png_uint_32, png_uint_32);
#endif/* PNG_WRITE_APNG_SUPPORTED */
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

apng_infop
apng_create_info_struct (png_structp pngp)
{
	apng_infop ainfop;
	(void)pngp;
	if (( ainfop = calloc(sizeof (apng_info),1) ))
	{
		apng_set_write_fn(pngp, ainfop, 0, 0, 0, 0, 0);
		apng_set_set_acTL_fn(pngp, ainfop, 0);
	}
	return ainfop;
}

void
apng_destroy_info_struct (png_structp pngp, apng_infopp ainfopp)
{
	(void)pngp;
	if (!( pngp && ainfopp ))
		return;

	free((*ainfopp));
}

void
apng_seek (png_structp pngp, apng_const_infop ainfop, size_t l)
{
	(*(ainfop->output_seek_fn))(pngp, l);
}

size_t
apng_tell (png_structp pngp, apng_const_infop ainfop)
{
	return (*(ainfop->output_tell_fn))(pngp);
}

#ifdef PNG_WRITE_FLUSH_SUPPORTED
void
apng_flush (png_structp pngp, apng_infop ainfop)
{
	if (ainfop->output_flush_fn)
		(*(ainfop->output_flush_fn))(pngp);
}

#ifdef PNG_STDIO_SUPPORTED
void
apng_default_flush (png_structp pngp)
{
	if (!( pngp ))
		return;

	fflush((png_FILE_p)png_get_io_ptr);
}
#endif/* PNG_STDIO_SUPPORTED */
#endif/* PNG_WRITE_FLUSH_SUPPORTED */

#ifdef PNG_STDIO_SUPPORTED
void
apng_default_seek (png_structp pngp, size_t l)
{
	if (!( pngp ))
		return;

	if (fseek((png_FILE_p)png_get_io_ptr(pngp), (long)l, SEEK_SET) == -1)
		png_error(pngp, "Seek Error");
}

size_t
apng_default_tell (png_structp pngp)
{
	long l;

	if (!( pngp ))
	{
		png_error(pngp, "Call to apng_default_tell with NULL pngp failed");
	}

	if (( l = ftell((png_FILE_p)png_get_io_ptr(pngp)) ) == -1)
		png_error(pngp, "Tell Error");

	return (size_t)l;
}
#endif/* PNG_STDIO_SUPPORTED */

void
apng_set_write_fn (png_structp pngp, apng_infop ainfop, png_voidp iop,
154
		png_rw_ptr write_f, png_flush_ptr flush_f,
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
		apng_seek_ptr seek_f, apng_tell_ptr tell_f)
{
	if (!( pngp && ainfop ))
		return;

	png_set_write_fn(pngp, iop, write_f, flush_f);

#ifdef PNG_WRITE_FLUSH_SUPPORTED
#ifdef PNG_STDIO_SUPPORTED
	if (!flush_f)
		ainfop->output_flush_fn = &apng_default_flush;
	else
#endif/* PNG_STDIO_SUPPORTED */
		ainfop->output_flush_fn = flush_f;
#endif/* PNG_WRITE_FLUSH_SUPPORTED */
#ifdef PNG_STDIO_SUPPORTED
	if (!seek_f)
		ainfop->output_seek_fn = &apng_default_seek;
	else
#endif/* PNG_STDIO_SUPPORTED */
		ainfop->output_seek_fn  = seek_f;
#ifdef PNG_STDIO_SUPPORTED
	if (!seek_f)
		ainfop->output_tell_fn  = apng_default_tell;
	else
#endif/* PNG_STDIO_SUPPORTED */
		ainfop->output_tell_fn  = tell_f;
}

void
apng_write_IEND (png_structp pngp)
{
	png_byte chunkc[] = "IEND";
	png_write_chunk(pngp, chunkc, 0, 0);
}

void
apng_write_acTL (png_structp pngp, png_uint_32 frames, png_uint_32 plays)
{
	png_byte chunkc[] = "acTL";
	png_byte buf[8];
	png_save_uint_32(buf, frames);
	png_save_uint_32(buf + 4, plays);
	png_write_chunk(pngp, chunkc, buf, 8);
}

png_uint_32
apng_set_acTL (png_structp pngp, png_infop infop, apng_infop ainfop,
		png_uint_32 frames, png_uint_32 plays)
{
	(void)pngp;
	(void)infop;
	if (!( pngp && infop && ainfop ))
		return 0;

	ainfop->num_frames = frames;
	ainfop->num_plays  = plays;

213
	ainfop->valid |= APNG_INFO_acTL;
214 215 216 217 218 219 220 221 222 223 224 225 226

	return 1;
}

void
apng_write_info_before_PLTE (png_structp pngp, png_infop infop,
		apng_infop ainfop)
{
	if (!( pngp && infop && ainfop ))
		return;

	png_write_info_before_PLTE(pngp, infop);

227
	if (( ainfop->valid & APNG_INFO_acTL )&&!( ainfop->mode & APNG_WROTE_acTL ))
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	{
		ainfop->start_acTL = apng_tell(pngp, ainfop);

		apng_write_acTL(pngp, 0, 0);
		/* modified for runtime dynamic linking */
		(*(ainfop->set_acTL_fn))(pngp, infop, PNG_UINT_31_MAX, 0);

		ainfop->mode |= APNG_WROTE_acTL;
	}
}

void
apng_write_info (png_structp pngp, png_infop infop,
		apng_infop ainfop)
{
	apng_write_info_before_PLTE(pngp, infop, ainfop);
	png_write_info(pngp, infop);
}

void
apng_write_end (png_structp pngp, png_infop infop, apng_infop ainfop)
{
	(void)infop;
	apng_write_IEND(pngp);
	apng_seek(pngp, ainfop, ainfop->start_acTL);
	apng_write_acTL(pngp, ainfop->num_frames, ainfop->num_plays);

#ifdef PNG_WRITE_FLUSH_SUPPORTED
#ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
	apng_flush(pngp, infop);
#endif/* PNG_WRITE_FLUSH_SUPPORTED */
#endif/* PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED */
}

262 263 264 265 266 267 268 269 270 271 272 273 274
#ifndef PNG_WRITE_APNG_SUPPORTED
png_uint_32
apng_set_acTL_dummy (png_structp pngp, png_infop infop,
		png_uint_32 frames, png_uint_32 plays)
{
	(void)pngp;
	(void)infop;
	(void)frames;
	(void)plays;
	return 0;
}
#endif/* PNG_WRITE_APNG_SUPPORTED */

275 276 277 278 279 280 281
/* Dynamic runtime linking capable! (Hopefully.) */
void
apng_set_set_acTL_fn (png_structp pngp, apng_infop ainfop,
		apng_set_acTL_ptr set_acTL_f)
{
	(void)pngp;
	if (!ainfop->set_acTL_fn)
282 283 284
#ifndef PNG_WRITE_APNG_SUPPORTED
		ainfop->set_acTL_fn = &apng_set_acTL_dummy;
#else
285
		ainfop->set_acTL_fn = &png_set_acTL;
286
#endif/* PNG_WRITE_APNG_SUPPORTED */
287 288 289
	else
		ainfop->set_acTL_fn = set_acTL_f;
}