I can now write PNG files :)

This commit is contained in:
2019-12-13 18:18:07 +01:00
parent c84a893380
commit 8ce9251dec
9 changed files with 125 additions and 13 deletions

View File

@@ -1,5 +1,6 @@
/*
* Lecture des images PNG
* L'ecriture est en chantier :}
*/
#include <stdio.h>
@@ -175,13 +176,59 @@ png_close_file(&png);
return 0;
}
/* --------------------------------------------------------------------- */
/* nouveau 13 decembre 2019 */
int fimg_save_as_png(FloatImg *src, char *outname, int flags)
{
png_t png;
png_t png;
int foo, sz, idx;
unsigned char *bytes, *bptr;
double maximum, fk;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( %p '%s' %x )\n", __func__, src, outname, flags);
#endif
/* convert ou floating datas to a byte/rgb array */
/* first, alloc a buffer */
sz = src->width * src->height;
bytes = calloc(sz, 3);
if (NULL==bytes) {
fprintf(stderr, "%s : no mem ?\n", __func__);
exit(3);
}
/* compute max value */
maximum = (double)fimg_get_maxvalue(src);
fk = maximum / 255.0;
fprintf(stderr, " max values %g fk %g\n", maximum, fk);
/* massage des pixels */
bptr = bytes;
for (idx=0; idx<sz; idx++) {
*bptr++ = (unsigned char) (src->R[idx] / fk);
*bptr++ = (unsigned char) (src->G[idx] / fk);
*bptr++ = (unsigned char) (src->B[idx] / fk);
}
return -1;
memset(&png, 0, sizeof(png_t));
png_init(NULL, NULL); /* this is VITAL ! */
foo = png_open_file_write(&png, outname);
if (PNG_NO_ERROR != foo) {
fprintf(stderr, "error in '%s' : open_file_write -> %d\n",
__func__, foo);
return foo;
}
foo = png_set_data(&png, src->width, src->height, 8, PNG_TRUECOLOR, bytes);
if (PNG_NO_ERROR != foo) {
fprintf(stderr, "error in '%s' : set_data -> %d\n",
__func__, foo);
return foo;
}
png_close_file(&png);
free(bytes); /* yolo ? */
return 0;
}
/* --------------------------------------------------------------------- */