first shoot

This commit is contained in:
Tonton Th
2019-03-03 16:22:55 +01:00
commit 5a72327882
25 changed files with 1480 additions and 0 deletions

35
lib/Makefile Normal file
View File

@@ -0,0 +1,35 @@
#
# building the base library
#
COPT = -Wall -fpic -g -pg -no-pie -DDEBUG_LEVEL=0
OBJS = fimg-core.o fimg-pnm.o fimg-file.o fimg-math.o \
fimg-timers.o
DEPS = Makefile ../floatimg.h
# modify it 'as you like'
AR=ar
all: $(OBJS) ../libfloatimg.a
# --------------------------------------------
../libfloatimg.a: $(OBJS)
$(AR) r $@ $?
fimg-core.o: fimg-core.c $(DEPS)
gcc $(COPT) -c $<
fimg-pnm.o: fimg-pnm.c $(DEPS)
gcc $(COPT) -c $<
fimg-file.o: fimg-file.c $(DEPS)
gcc $(COPT) -c $<
fimg-math.o: fimg-math.c $(DEPS)
gcc $(COPT) -c $<
fimg-timers.o: fimg-timers.c $(DEPS)
gcc $(COPT) -c $<
# --------------------------------------------

189
lib/fimg-core.c Normal file
View File

@@ -0,0 +1,189 @@
/*
* fimg-core.c
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "string.h"
#include "../floatimg.h"
extern int verbosity; /* must be declared around main() */
/* ---------------------------------------------------------------- */
static int fimg_type_is_valid(int t)
{
switch (t) {
case 1: case 3: case 4: return 1;
}
return 0;
}
/* --------------------------------------------------------------------- */
int fimg_print_version(int k)
{
fprintf(stderr, "*** FloatImg library, alpha v%d (%s, %s)\n",
FIMG_VERSION, __DATE__, __TIME__);
if (51 == k) {
puts("+--------------------+");
puts("| Pastis is coming. |");
puts("+--------------------+");
}
return 0;
}
/* --------------------------------------------------------------------- */
void fimg_printhead(FloatImg *h)
{
printf("%5d %5d %2d %p %p %p %p\n", h->width, h->height, h->type,
h->R, h->G, h->B, h->A);
}
/* --------------------------------------------------------------------- */
int fimg_describe(FloatImg *head, char *txt)
{
printf("----- '%s' at %p -----\n", txt, head);
if( ! fimg_type_is_valid(head->type) ) {
fprintf(stderr, "*** %s *** type %d invalid *** %s ***\n",
__func__, head->type, txt);
return -1;
}
printf(" size %d x %d x %d\n",
head->width, head->height, head->type);
printf(" fval/count %f %d\n", head->fval, head->count);
printf(" pixels@ %p %p %p %p\n",
head->R, head->G, head->B, head->A);
return 0;
}
/* ---------------------------------------------------------------- */
int fimg_create(FloatImg *fimg, int w, int h, int t)
{
int surface, size;
float *fptr;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( %p %d %d %d )\n", __func__, fimg, w, h, t);
#endif
if ( ! fimg_type_is_valid(t) ) {
return -2;
}
memset(fimg, 0, sizeof(FloatImg));
surface = w * h;
size = surface * t * sizeof(float);
#if DEBUG_LEVEL > 1
fprintf(stderr, "surface is %d pixels, need %d bytes\n", surface, size);
#endif
fptr = (float *)malloc(size);
if (NULL==fptr) {
fprintf(stderr, "%s : nom mem, exiting.\n", __func__);
exit(1);
}
#if DEBUG_LEVEL > 1
fprintf(stderr, " got %d bytes at %p\n", size, fptr);
#endif
fimg->width = w; fimg->height = h;
fimg->type = t;
fimg->R = fptr;
if ( (t==3) || (t==4) ) {
fimg->G = fptr + surface;
fimg->B = fptr + surface + surface;
}
if ( t==4 ) fimg->A = fptr + (3 * surface);
return 0;
}
/* --------------------------------------------------------------------- */
int fimg_destroy(FloatImg *fimg)
{
#if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( %p )\n", __func__, fimg);
#endif
if ( ! fimg_type_is_valid(fimg->type) ) {
return -2;
}
free(fimg->R);
memset(fimg, 0, sizeof(FloatImg));
return 0;
}
/* --------------------------------------------------------------------- */
int fimg_clear(FloatImg *fimg)
{
int size;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( %p )\n", __func__, fimg);
#endif
if ( ! fimg_type_is_valid(fimg->type) ) {
fprintf(stderr, "invalid type %d in %s\n", fimg->type, __func__);
return -2;
}
size = fimg->width * fimg->height * fimg->type * sizeof(float);
memset(fimg->R, 0, size);
return -1;
}
/* --------------------------------------------------------------------- */
int fimg_plot_rgb (FloatImg *head, int x, int y,
float r, float g, float b)
{
int offset;
if (head->type < 3) {
#if DEBUG_LEVEL > 1
fprintf(stderr, "%s : type %d is bad.\n", __func__, head->type);
#endif
return -1;
}
offset = x + (y * head->width);
#if DEBUG_LEVEL > 1
fprintf(stderr, ">>> %s ( %p %d %d %f )\n", __func__, head, x, y, gray);
fprintf(stderr, " offset %d\n", offset);
#endif
head->R[offset] = r;
head->G[offset] = g;
head->B[offset] = b;
return 0;
}
/* ---------------------------------------------------------------- */
int fimg_add_rgb(FloatImg *head, int x, int y, float r, float g, float b)
{
int offset;
offset = x + (y * head->width);
head->R[offset] += r;
head->G[offset] += g;
head->B[offset] += b;
return 0;
}
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */

0
lib/fimg-core.h Normal file
View File

125
lib/fimg-file.c Normal file
View File

@@ -0,0 +1,125 @@
/*
* fimg-file.c
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "string.h"
#include "../floatimg.h"
extern int verbosity; /* must be declared around main() */
/* ---------------------------------------------------------------- */
int fimg_fileinfos(char *fname, int *datas)
{
FILE *fp;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( '%s' %p )\n", __func__, fname, datas);
#endif
fp = fopen(fname, "r");
if (NULL==fp) {
perror(fname);
return -1;
}
if (3 != fread(datas, sizeof(int), 3, fp)) {
fprintf(stderr, "%s: %s short read\n", __func__, fname);
fclose(fp);
return -2;
}
fclose(fp);
return 0;
}
/* ---------------------------------------------------------------- */
int fimg_dump_to_file(FloatImg *head, char *fname, int notused)
{
FILE *fp;
int foo, nbre, dims[3];
#if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( %p '%s' %d )\n", __func__, head,
fname, notused);
#endif
if (3 != head->type) {
fprintf(stderr, "%s : bat type %d\n", __func__, head->type);
return -8;
}
fp = fopen(fname, "w");
if (NULL==fp) {
perror(fname);
return -1;
}
dims[0] = head->width; dims[1] = head->height;
dims[2] = head->type;
foo = fwrite(dims, sizeof(int), 3, fp);
if (3 != foo) {
perror(fname);
fclose(fp);
return -2;
}
nbre = head->width*head->height*3;
#if DEBUG_LEVEL
fprintf(stderr, " %s : data at %p\n", __func__, head->R);
#endif
foo = fwrite(head->R, sizeof(float), nbre, fp);
if (nbre!=foo) {
perror(fname);
fclose(fp);
return -3;
}
fclose(fp);
return 0;
}
/* ---------------------------------------------------------------- */
int fimg_create_from_dump(char *fname, FloatImg *head)
{
FILE *fp;
int foo, dims[3];
#if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( '%s' %p )\n", __func__, fname, head);
#endif
fp = fopen(fname, "r");
if (NULL==fp) {
perror(fname);
exit(1);
}
foo = fread(dims, sizeof(int), 3, fp);
if (3 != foo) {
fprintf(stderr, "%s : short read\n", fname);
fclose(fp);
return -1;
}
#if DEBUG_LEVEL
fprintf(stderr, "%s : got [ %d %d %d ] from '%s'\n", __func__,
dims[0], dims[1], dims[2], fname);
#endif
foo = fimg_create(head, dims[0], dims[1], dims[2]);
if (foo) {
fprintf(stderr, "%s : create -> %d\n", __func__, foo);
return foo;
}
foo = fread(head->R, sizeof(float), dims[0]*dims[1]*3, fp);
fclose(fp);
return 0;
}

99
lib/fimg-math.c Normal file
View File

@@ -0,0 +1,99 @@
/*
* fimg-core.c
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "string.h"
#include "../floatimg.h"
extern int verbosity; /* must be declared around main() */
/* ---------------------------------------------------------------- */
float fimg_get_maxvalue(FloatImg *head)
{
float maxval;
int foo;
maxval = 0.0; /* no negative values allowed */
for (foo=0; foo<(head->width*head->height); foo++) {
if (head->R[foo] > maxval) maxval = head->R[foo];
if (head->G[foo] > maxval) maxval = head->G[foo];
if (head->B[foo] > maxval) maxval = head->B[foo];
}
return maxval;
}
/* ---------------------------------------------------------------- */
int fimg_meanvalues(FloatImg *head, float means[4])
{
int idx, surface;
surface = head->width * head->height;
if (surface < 1) return -1;
memset(means, 0, 4*sizeof(float));
for (idx=0; idx<surface; idx++) {
means[0] += head->R[idx];
if (head->type > 2) {
means[1] += head->G[idx];
means[2] += head->B[idx];
}
}
for (idx=0; idx<4; idx++) means[idx] /= (float)surface;
return 0;
}
/* ---------------------------------------------------------------- */
int fimg_to_gray(FloatImg *head)
{
float add;
int foo;
for (foo=0; foo<(head->width*head->height); foo++) {
add = head->R[foo];
add += head->G[foo];
add += head->B[foo];
head->R[foo] = head->G[foo] = head->B[foo] = add;
}
return -1;
}
/* ---------------------------------------------------------------- */
void fimg_add_cste(FloatImg *fi, float value)
{
int nbre, idx;
nbre = fi->width * fi->height * fi->type;
#if DEBUG_LEVEL
fprintf(stderr, "%s, nbre is %d\n", __func__, nbre);
#endif
for (idx=0; idx<nbre; nbre++) {
fi->R[idx] += value;
}
}
/* ---------------------------------------------------------------- */
/* Warning: this function is _very_ slow */
void fimg_drand48(FloatImg *fi, float kmul)
{
int nbre, idx;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %g )\n", __func__, fi, kmul);
#endif
nbre = fi->width * fi->height * fi->type;
for (idx=0; idx<nbre; idx++) {
fi->R[idx] = drand48() * kmul;
}
}
/* ---------------------------------------------------------------- */

76
lib/fimg-pnm.c Normal file
View File

@@ -0,0 +1,76 @@
/*
* fimg-pnm.c
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "string.h"
#include "../floatimg.h"
extern int verbosity; /* must be declared around main() */
/* ---------------------------------------------------------------- */
/* ---------------------------------------------------------------- */
int fimg_save_as_pnm(FloatImg *head, char *fname, int notused)
{
FILE *fp;
float maximum, fk;
int idx, sz, Rv, Gv, Bv, foo;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %-25s ( %p '%s' %d )\n", __func__, head,
fname, notused);
#endif
if (head->type != 3) {
#if DEBUG_LEVEL
fprintf(stderr, "%s : type %d is bad.\n", __func__, head->type);
#endif
return -1;
}
if (NULL==(fp=fopen(fname, "w"))) {
perror(fname);
return -1;
}
fprintf(fp, "P3\n%d %d\n", head->width, head->height);
maximum = fimg_get_maxvalue(head);
fprintf(fp, "# maxval %15f\n", maximum);
fk = maximum / 65536.0;
fprintf(fp, "# divisor %15f\n", fk);
fprintf(fp, "65535\n");
sz = head->width*head->height;
foo = 0;
for (idx=0; idx<sz; idx++) {
if (fk > 0) {
Rv = (int)(head->R[idx] / fk);
Gv = (int)(head->G[idx] / fk);
Bv = (int)(head->B[idx] / fk);
}
else {
Rv = Gv = Bv = 0;
}
foo += fprintf(fp, "%d %d %d", Rv, Gv, Bv);
if (foo > 60) {
fputs("\n", fp); foo = 0;
}
else {
fputs(" ", fp); foo++;
}
}
fputs("\n", fp);
fclose(fp);
return 0;
}
/* ---------------------------------------------------------------- */

43
lib/fimg-timers.c Normal file
View File

@@ -0,0 +1,43 @@
/*
* timers.c
*/
#include <stdio.h>
#include <sys/time.h>
extern int verbosity;
/* ----------------------------------------------------------------- */
static double dtime(void)
{
struct timeval t;
double d;
(void)gettimeofday(&t, NULL);
d = (double)t.tv_sec + (double)t.tv_usec / 1e6;
return d;
}
/* ----------------------------------------------------------------- */
static double memory_time;
double fimg_timer_set(int whot)
{
double current;
current = dtime();
#if DEBUG_LEVEL
fprintf(stderr, "%s ( %d ) -> current %f\n", __func__, whot, current);
#endif
memory_time = current;
return memory_time;
}
/* ----------------------------------------------------------------- */
double fimg_timer_get(int whot)
{
double current;
current = dtime();
return current - memory_time;
}
/* ----------------------------------------------------------------- */