I can now write PNG files :)

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

1
.gitignore vendored
View File

@ -26,6 +26,7 @@ doc/*.ilg
doc/*.ind doc/*.ind
funcs/t funcs/t
funcs/*.png
scripts/*.fimg scripts/*.fimg
scripts/*.pnm scripts/*.pnm

View File

@ -2,7 +2,7 @@
* floatimg.h * floatimg.h
*/ */
#define FIMG_VERSION 80 #define FIMG_VERSION 81
/* /*
* in memory descriptor * in memory descriptor

View File

@ -8,7 +8,7 @@ OBJS = fimg-png.o fimg-tiff.o misc-plots.o filtrage.o utils.o \
#--------------------------------------------------------------- #---------------------------------------------------------------
t: t.c $(DEPS) ../libfloatimg.a t: t.c $(DEPS) ../libfloatimg.a
gcc $(COPT) $< ../libfloatimg.a -lnetpbm -o $@ gcc $(COPT) $< ../libfloatimg.a -lnetpbm -lpnglite -lm -o $@
#--------------------------------------------------------------- #---------------------------------------------------------------

View File

@ -1,5 +1,6 @@
/* /*
* Lecture des images PNG * Lecture des images PNG
* L'ecriture est en chantier :}
*/ */
#include <stdio.h> #include <stdio.h>
@ -175,13 +176,59 @@ png_close_file(&png);
return 0; return 0;
} }
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
/* nouveau 13 decembre 2019 */
int fimg_save_as_png(FloatImg *src, char *outname, int flags) 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;
} }
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */

View File

@ -51,11 +51,30 @@ printf("%-10s %d\n\n", fname, foo);
return 0; return 0;
} }
/* --------------------------------------------------------------------- */ /* --------------------------------------------------------------------- */
int main(int argc, char *argv[]) int essai_ecrire_png(char *fname)
{ {
FloatImg fimg;
int foo; int foo;
foo = essai_detect_type(); fimg_create(&fimg, 512, 512, FIMG_TYPE_RGB);
for (foo=0; foo<512; foo++) {
fimg_plot_rgb(&fimg, foo, foo, 17000.0, 8000.0, 11111.1);
}
fimg_save_as_pnm(&fimg, "quux.pnm", 0);
foo = fimg_save_as_png(&fimg, fname, 0);
return 0;
}
/* --------------------------------------------------------------------- */
int main(int argc, char *argv[])
{
int foo;
puts("++++++++++++++++++++++++++++++++");
foo = essai_ecrire_png("dessin.png");
return 0; return 0;
} }

View File

@ -4,6 +4,7 @@ cp libfloatimg.a /usr/local/lib
cp floatimg.h /usr/local/include cp floatimg.h /usr/local/include
cp tools/mkfimg tools/fimg2pnm tools/fimgops \ cp tools/mkfimg tools/fimg2pnm tools/fimgops \
tools/fimg2png \
tools/png2fimg tools/fimgstats tools/fimgfx \ tools/png2fimg tools/fimgstats tools/fimgfx \
/usr/local/bin /usr/local/bin

View File

@ -30,7 +30,7 @@ fimg2pnm: fimg2pnm.c $(DEPS)
gcc $(COPT) $< ../libfloatimg.a -o $@ gcc $(COPT) $< ../libfloatimg.a -o $@
fimg2png: fimg2png.c $(DEPS) fimg2png: fimg2png.c $(DEPS)
gcc $(COPT) $< ../libfloatimg.a -o $@ gcc $(COPT) $< ../libfloatimg.a -lpnglite -o $@
addtga2fimg: addtga2fimg.c $(DEPS) addtga2fimg: addtga2fimg.c $(DEPS)
gcc $(COPT) $< ../libfloatimg.a -limageSO -lm -o $@ gcc $(COPT) $< ../libfloatimg.a -limageSO -lm -o $@

View File

@ -1,13 +1,56 @@
/*
* converting a floatimg to a PNG
*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "../floatimg.h" #include "../floatimg.h"
/* --------------------------------------------------------------------- */ int verbosity;
/* ----------------------------------------------------------------- */
int convertir_fimg_en_PNG(char *srcname, char *dstname, int notused)
{
int foo, infos[3];
FloatImg fimg;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %25s ( '%s' '%s' %d )\n", __func__,
srcname, dstname, notused);
#endif
foo = fimg_fileinfos(srcname, infos);
if (foo) { fprintf(stderr, "'%s' get dims -> %d\n", srcname, foo); }
if (verbosity) {
fprintf(stderr, "image '%s' is %d x %d %s\n",
srcname, infos[0], infos[1],
fimg_str_type(infos[2]));
}
foo = fimg_create_from_dump(srcname, &fimg);
if (foo) {
fprintf(stderr, "create fimg from '%s' -> %d\n", srcname, foo);
return -1;
}
foo = fimg_save_as_png(&fimg, dstname, 0);
if (foo) {
fprintf(stderr, "saving as png '%s' -> %d\n", dstname, foo);
return -1;
}
fimg_destroy(&fimg);
return -1;
}
/* ----------------------------------------------------------------- */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
// int foo; int foo;
if (3 != argc) { if (3 != argc) {
fimg_print_version(1); fimg_print_version(1);
@ -15,9 +58,10 @@ if (3 != argc) {
exit(1); exit(1);
} }
fprintf(stderr, "ah ah ah, no working code here !\n"); foo = convertir_fimg_en_PNG(argv[1], argv[2], 0);
fprintf(stderr, "%s : got a %d from convertor\n", argv[0], foo);
return 0; return 0;
} }
/* --------------------------------------------------------------------- */ /* ----------------------------------------------------------------- */

View File

@ -1,6 +1,6 @@
COPT = -Wall -fpic -g -no-pie -DDEBUG_LEVEL=1 COPT = -Wall -fpic -g -no-pie -DDEBUG_LEVEL=0
DEPS = ../floatimg.h ../libfloatimg.a Makefile DEPS = ../floatimg.h ../libfloatimg.a Makefile
all: grabvidseq t all: grabvidseq t