2017-07-18 20:53:00 +00:00
|
|
|
#include "compat.h"
|
|
|
|
#include "pngwrite.h"
|
2019-09-22 21:15:46 +00:00
|
|
|
#include "crc32_.h"
|
2017-07-18 20:53:00 +00:00
|
|
|
|
2019-03-01 08:51:50 +00:00
|
|
|
#include "vfs.h"
|
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
pngwrite_t png;
|
|
|
|
|
2019-03-01 08:51:50 +00:00
|
|
|
#define png_write_buf(p, size) buildvfs_fwrite(p, size, 1, png.file)
|
2017-07-18 20:53:00 +00:00
|
|
|
|
2018-04-04 20:48:10 +00:00
|
|
|
static FORCE_INLINE void png_write_uint32(uint32_t const in)
|
2017-07-18 20:53:00 +00:00
|
|
|
{
|
2018-04-04 20:48:10 +00:00
|
|
|
uint32_t const buf = B_BIG32(in);
|
|
|
|
png_write_buf(&buf, sizeof(uint32_t));
|
2017-07-18 20:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void png_write_chunk(uint32_t const size, char const *const type,
|
|
|
|
uint8_t const *const data, uint32_t flags)
|
|
|
|
{
|
2019-09-22 21:15:46 +00:00
|
|
|
uLongf chunk_size = (flags & CHUNK_COMPRESSED) ? compressBound(size) : size;
|
2017-07-18 20:53:00 +00:00
|
|
|
uint8_t * const chunk = (uint8_t *) Xcalloc(1, 4 + chunk_size);
|
|
|
|
|
|
|
|
Bmemcpy(chunk, type, 4);
|
|
|
|
|
|
|
|
if (flags & CHUNK_COMPRESSED)
|
2019-09-22 21:15:46 +00:00
|
|
|
compress(chunk + 4, (uLongf *) &chunk_size, data, size);
|
2017-07-18 20:53:00 +00:00
|
|
|
else
|
|
|
|
Bmemcpy(chunk + 4, data, size);
|
|
|
|
|
|
|
|
png_write_uint32(chunk_size);
|
|
|
|
png_write_buf(chunk, chunk_size + 4);
|
|
|
|
|
|
|
|
uint32_t crc = Bcrc32(NULL, 0, 0L);
|
|
|
|
crc = Bcrc32(chunk, chunk_size + 4, crc);
|
|
|
|
png_write_uint32(crc);
|
|
|
|
|
2019-06-25 11:29:08 +00:00
|
|
|
Xfree(chunk);
|
2017-07-18 20:53:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void png_set_pal(uint8_t const * const data, int numentries)
|
|
|
|
{
|
|
|
|
png.pal_entries = numentries;
|
|
|
|
png.pal_data = (uint8_t *)Xmalloc(numentries * 3);
|
|
|
|
|
|
|
|
Bmemcpy(png.pal_data, data, numentries * 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void png_set_text(char const * const keyword, char const * const text)
|
|
|
|
{
|
|
|
|
unsigned const keylen = Bstrlen(keyword);
|
|
|
|
Bassert(keylen < 79);
|
|
|
|
unsigned const textlen = Bstrlen(text);
|
|
|
|
|
|
|
|
png.textlen = keylen + textlen + 1;
|
|
|
|
png.text = (uint8_t *) Xrealloc(png.text, png.textlen);
|
|
|
|
|
|
|
|
Bmemcpy(png.text, keyword, keylen);
|
|
|
|
*(png.text + keylen) = 0;
|
|
|
|
Bmemcpy(png.text + keylen + 1, text, textlen);
|
|
|
|
}
|
|
|
|
|
2019-05-19 03:56:13 +00:00
|
|
|
void png_write(buildvfs_FILE const file, int const width, int const height,
|
2018-04-04 20:48:10 +00:00
|
|
|
uint8_t const type, uint8_t const * const data)
|
2017-07-18 20:53:00 +00:00
|
|
|
{
|
|
|
|
png.file = file;
|
2017-07-29 20:39:46 +00:00
|
|
|
|
2017-07-18 20:53:00 +00:00
|
|
|
png_write_buf("\x89\x50\x4E\x47\x0D\x0A\x1A\x0A", 8);
|
|
|
|
|
2019-05-19 03:56:13 +00:00
|
|
|
png_ihdr_t const png_header = { B_BIG32((unsigned)width), B_BIG32((unsigned)height), 8, type, 0 };
|
2017-07-18 20:53:00 +00:00
|
|
|
png_write_chunk(sizeof(png_ihdr_t), "IHDR", (uint8_t const *)&png_header, 0);
|
|
|
|
|
|
|
|
if (png.text)
|
|
|
|
{
|
|
|
|
png_write_chunk(png.textlen, "tEXt", png.text, 0);
|
|
|
|
DO_FREE_AND_NULL(png.text);
|
|
|
|
}
|
|
|
|
|
2019-05-19 03:56:13 +00:00
|
|
|
int const bytesPerPixel = (type == PNG_TRUECOLOR ? 3 : 1);
|
|
|
|
int const bytesPerLine = width * bytesPerPixel;
|
2017-07-18 20:53:00 +00:00
|
|
|
|
2018-04-04 20:48:10 +00:00
|
|
|
if (png.pal_data)
|
|
|
|
{
|
2017-07-18 20:53:00 +00:00
|
|
|
png_write_chunk(png.pal_entries * 3, "PLTE", png.pal_data, 0);
|
2018-04-04 20:48:10 +00:00
|
|
|
DO_FREE_AND_NULL(png.pal_data);
|
|
|
|
}
|
2017-07-18 20:53:00 +00:00
|
|
|
|
2019-05-19 03:56:13 +00:00
|
|
|
int const linesiz = height * bytesPerLine + height;
|
2017-07-18 20:53:00 +00:00
|
|
|
uint8_t *lines = (uint8_t *) Xcalloc(1, linesiz);
|
|
|
|
|
2019-05-19 03:56:13 +00:00
|
|
|
for (int i = 0; i < height; i++)
|
2017-07-18 20:53:00 +00:00
|
|
|
Bmemcpy(lines + i * bytesPerLine + i + 1, data + i * bytesPerLine, bytesPerLine);
|
|
|
|
|
|
|
|
png_write_chunk(linesiz, "IDAT", lines, CHUNK_COMPRESSED);
|
|
|
|
png_write_chunk(0, "IEND", NULL, 0);
|
|
|
|
|
2019-06-25 11:29:08 +00:00
|
|
|
Xfree(lines);
|
2017-07-18 20:53:00 +00:00
|
|
|
}
|