FloatImg4PythonBinding/lib/fimg-core.c

197 lines
4.2 KiB
C
Raw Normal View History

2019-03-04 02:22:55 +11:00
/*
* 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) {
2019-03-04 04:03:50 +11:00
fprintf(stderr, "%s : no mem, exiting.\n", __func__);
2019-03-04 02:22:55 +11:00
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
2019-03-04 04:07:20 +11:00
if (NULL == fimg) {
fprintf(stderr, "%s : parameter is null\n", __func__);
return -1;
}
2019-03-04 02:22:55 +11:00
if ( ! fimg_type_is_valid(fimg->type) ) {
return -2;
}
2019-03-04 04:07:20 +11:00
if (NULL == fimg->R) {
fprintf(stderr, "%s : %p already freeed\n", __func__, fimg);
return -3;
}
2019-03-04 02:22:55 +11:00
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;
}
/* --------------------------------------------------------------------- */
/* --------------------------------------------------------------------- */