Skip to content

Commit

Permalink
Introduce fz_function type.
Browse files Browse the repository at this point in the history
Make pdf_functions be derived from these. This is to prepare
for transfer function support.
  • Loading branch information
robinwatts committed Jan 16, 2024
1 parent 767fd7f commit e05c18f
Show file tree
Hide file tree
Showing 3 changed files with 413 additions and 269 deletions.
57 changes: 57 additions & 0 deletions include/mupdf/fitz/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,63 @@ int fz_lookup_blendmode(const char *name);
*/
const char *fz_blendmode_name(int blendmode);

/*
Generic function type.
Different function implementations will derive from this.
*/
typedef struct fz_function fz_function;

typedef void (fz_function_eval_fn)(fz_context *, fz_function *, const float *, float *);

enum
{
FZ_FUNCTION_MAX_N = FZ_MAX_COLORS,
FZ_FUNCTION_MAX_M = FZ_MAX_COLORS
};

struct fz_function
{
fz_storable storable;
size_t size;
int m; /* number of input values */
int n; /* number of output values */

fz_function_eval_fn *eval;
};

fz_function *fz_new_function_of_size(fz_context *ctx, int size, size_t size2, int m, int n, fz_function_eval_fn *eval, fz_store_drop_fn *drop);

#define fz_new_derived_function(CTX,TYPE,SIZE,M,N,EVAL,DROP) \
((TYPE*)Memento_label(fz_new_function_of_size(CTX,sizeof(TYPE),SIZE,M,N,EVAL,DROP), #TYPE))

/*
Evaluate a function.
Input vector = (in[0], ..., in[inlen-1])
Output vector = (out[0], ..., out[outlen-1])
If inlen or outlen do not match that expected by the function, this
routine will truncate or extend the input/output (with 0's) as
required.
*/
void fz_eval_function(fz_context *ctx, fz_function *func, const float *in, int inlen, float *out, int outlen);

/*
Keep a function reference.
*/
fz_function *fz_keep_function(fz_context *ctx, fz_function *func);

/*
Drop a function reference.
*/
void fz_drop_function(fz_context *ctx, fz_function *func);

/*
Function size
*/
size_t fz_function_size(fz_context *ctx, fz_function *func);

/**
The device structure is public to allow devices to be
implemented outside of fitz.
Expand Down
61 changes: 61 additions & 0 deletions source/fitz/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -837,3 +837,64 @@ fz_structure_from_string(const char *str)
if (!strcmp(str, "Form")) return FZ_STRUCTURE_FORM;
return FZ_STRUCTURE_INVALID;
}

fz_function *
fz_keep_function(fz_context *ctx, fz_function *func)
{
return fz_keep_storable(ctx, &func->storable);
}

void
fz_drop_function(fz_context *ctx, fz_function *func)
{
fz_drop_storable(ctx, &func->storable);
}

size_t
fz_function_size(fz_context *ctx, fz_function *func)
{
return (func ? func->size : 0);
}

void
fz_eval_function(fz_context *ctx, fz_function *func, const float *in, int inlen, float *out, int outlen)
{
float fakein[FZ_FUNCTION_MAX_M];
float fakeout[FZ_FUNCTION_MAX_N];
int i;

if (inlen < func->m)
{
for (i = 0; i < inlen; ++i)
fakein[i] = in[i];
for (; i < func->m; ++i)
fakein[i] = 0;
in = fakein;
}

if (outlen < func->n)
{
func->eval(ctx, func, in, fakeout);
for (i = 0; i < outlen; ++i)
out[i] = fakeout[i];
}
else
{
func->eval(ctx, func, in, out);
for (i = func->n; i < outlen; ++i)
out[i] = 0;
}
}

fz_function *
fz_new_function_of_size(fz_context *ctx, int size, size_t size2, int m, int n, fz_function_eval_fn *eval, fz_store_drop_fn *drop)
{
fz_function *fn = fz_calloc(ctx, 1, size);

FZ_INIT_STORABLE(fn, 1, drop);
fn->eval = eval;
fn->m = m;
fn->n = n;

return fn;
}
Loading

0 comments on commit e05c18f

Please sign in to comment.