render.h
author Tero Marttila <terom@fixme.fi>
Sat, 31 May 2008 02:22:27 +0300
changeset 1 6aa1a0d1f88d
child 2 69f8c0acaac7
permissions -rw-r--r--
partial commit of new render module, render_file should work, web_main doesn't

committer: Tero Marttila <terom@fixme.fi>
#ifndef RENDER_H
#define RENDER_H

#include <sys/types.h>
#include <stdio.h>

/*
 * This module provides various ways to render mandelbrot images in various formats
 */

// custom I/O callbacks function signatures
typedef int (*render_ctx_write_cb)(const unsigned char *data, size_t length, void *arg);
typedef int (*render_ctx_flush_cb)(void *arg);

// callback return codes
#define RENDER_CB_OK 0
#define RENDER_CB_ERR 1

#define RENDER_OK 0
#define RENDER_ERR 1

// output types
enum {
    RENDER_RAW,     // raw pixel data
    RENDER_PNG,     // a png image
};

typedef struct render_ctx {
/* render mode */
    int mode;

/* image size */
    u_int32_t img_w;
    u_int32_t img_h;

/* mandelbrot region */
    double      x1;
    double      y1;
    double      x2;
    double      y2;

/* I/O parameters */
    // if this is non-NULL, use libpng's normal IO on this stream
    FILE *io_stream;

    // called to handle the output data
    render_ctx_write_cb io_write_fn;

    // called when the output data should be flushed - can be safely ignored if not needed
    render_ctx_flush_cb io_flush_fn;

    // the callback argument
    void *io_cb_arg;

    // error status
    int io_error;
} render_t;

/*
 * Clear out the render context and set the mode
 */
int render_init (render_t *ctx, int mode);

/*
 * What kind of image to render, PNG or RAW?
 */
int render_set_mode (render_t *ctx, int mode);

/*
 * What size of image to render
 */
int render_set_size (render_t *ctx, u_int32_t img_w, u_int32_t img_h);

/*
 * Select what region to render
 */

// the "full" mandelbrot region
#define REGION_X1 -2.0
#define REGION_Y1 -1.5
#define REGION_X2 1.0
#define REGION_Y2 1.5

int render_region_full (render_t *ctx);
int render_region_raw (render_t *ctx, double x1, double y1, double x2, double y2);

/*
 * How to handle the I/O
 */
int render_io_custom (render_t *ctx, render_ctx_write_cb write_fn, render_ctx_flush_cb flush_fn, void *arg);
int render_io_stream (render_t *ctx, FILE *fh);

/*
 * Remote rendering
 */

#pragma pack(push)
#pragma pack(1)

struct render_cmd {
    u_int32_t   img_w;
    u_int32_t   img_h;

    double      x1;
    double      y1;
    double      x2;
    double      y2;
};

#pragma pack(pop)

#define RENDER_PORT 6159
#define RENDER_CMD_SIZE sizeof(struct render_cmd)

/*
 * build the command to send for remote rendering
 */
int render_cmd_build (render_t *ctx, struct render_cmd *cmd);

#endif /* RENDER_H */