libtthimage/Lib/png.c

87 lines
1.8 KiB
C
Raw Normal View History

2022-06-26 11:06:35 +02:00
/*
2022-09-13 00:11:53 +02:00
------ libtthimage ------
2022-06-26 11:06:35 +02:00
PNG aka "portable network graphics"s.
*/
#include <stdio.h>
2022-09-13 00:11:53 +02:00
#include <stdlib.h>
2022-06-26 11:06:35 +02:00
#include <unistd.h>
#include <string.h>
#include <png.h>
2022-06-30 20:33:31 +02:00
#include <zlib.h>
#undef DEBUG_LEVEL
#define DEBUG_LEVEL 1
#include "../tthimage.h"
/*
2022-09-13 00:11:53 +02:00
* http://www.libpng.org/pub/png/pngbook.html
2022-06-30 20:33:31 +02:00
*/
/*::------------------------------------------------------------------::*/
2022-09-13 00:11:53 +02:00
void write_png_version_info(void)
2022-06-30 20:33:31 +02:00
{
2022-09-13 00:11:53 +02:00
fprintf(stderr, " Compiled with libpng %s; using libpng %s.\n",
PNG_LIBPNG_VER_STRING, png_libpng_ver);
fprintf(stderr, " Compiled with zlib %s; using zlib %s.\n",
ZLIB_VERSION, zlib_version);
2022-06-30 20:33:31 +02:00
}
2022-06-26 11:06:35 +02:00
/*::------------------------------------------------------------------::*/
Image_Desc * Image_PNG_alloc_load(char *fname, int k)
{
2022-06-30 20:33:31 +02:00
#if DEBUG_LEVEL
2022-09-13 00:11:53 +02:00
fprintf(stderr, ">>> %s ( '%s' %d )\n", __func__, fname, k);
write_png_version_info();
2022-06-30 20:33:31 +02:00
#endif
2022-06-26 11:06:35 +02:00
2022-07-07 12:52:00 +02:00
return NULL;
2022-06-26 11:06:35 +02:00
}
/*::------------------------------------------------------------------::*/
2022-09-13 00:11:53 +02:00
/*
* http://zarb.org/~gc/html/libpng.html
* https://gist.github.com/niw/5963798
*/
2022-06-26 11:06:35 +02:00
int Image_save_as_PNG(Image_Desc *img, char *fname, int p1, int p2)
{
2022-09-13 00:11:53 +02:00
FILE *fp;
int x, y, foo;
png_structp png;
png_infop info;
2022-06-26 11:06:35 +02:00
#if DEBUG_LEVEL
2022-09-13 00:11:53 +02:00
fprintf(stderr, ">>> %s ( %p '%s' %d %d )\n", __func__, img, fname, p1, p2);
write_png_version_info();
2022-06-26 11:06:35 +02:00
#endif
2022-09-13 00:11:53 +02:00
fp = fopen(fname, "wb");
if (NULL == fp) {
perror(fname);
return BASIC_IO_WR_ERR;
}
png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
info = png_create_info_struct(png);
if (setjmp(png_jmpbuf(png))) abort();
png_init_io(png, fp);
png_set_IHDR( png,
info,
img->width, img->height,
8,
PNG_COLOR_TYPE_RGBA,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT );
png_write_info(png, info);
2022-06-26 11:06:35 +02:00
return FULL_NUCKED;
}
/*::------------------------------------------------------------------::*/