render_local.c
author Tero Marttila <terom@fixme.fi>
Sat, 07 Jun 2008 05:05:18 +0300
changeset 13 ee426f453cf5
parent 12 43297144f196
child 17 8e8b56b0e0f5
permissions -rw-r--r--
* fix some (of the) stupid things in Makefile
* increment remote_node->current_load in remote_pool_get
* re-add render_init
* add render_raw module to handle non-PNG rendering
* update render_local to support RENDER_RAW
* working (but limited and inefficient) implementation of render_multi
* fixes to render_png
* improve/clean up render_remote
* mark internal function static
* make web_main use render_multi
* random bugfixes (possibly due to vim acting weird re file recovery post-crash)

committer: Tero Marttila <terom@fixme.fi>
#include <stdlib.h>
#include <time.h>
#include <assert.h>

#include "common.h"
#include "render_internal.h"
#include "render_local.h"
#include "render_png.h"
#include "render_raw.h"
#include "render_mandelbrot.h"

int render_local (struct render *render, double *duration) {
    unsigned char *rowbuf = NULL;
    struct render_png *png_ctx = NULL;
    struct render_raw *raw_ctx = NULL;
    clock_t t1, t2;
    
    if (duration)
        *duration = -1;

    // alloc the memory buffer
    if (!(rowbuf = malloc(render->img_w)))
        ERROR("malloc");
 
    // what mode?
    switch (render->mode) {
        case RENDER_PNG :
            // the render_png stuff
            if (!(png_ctx = render_png_init(render)))
                ERROR("render_png_init");
                
            //  set render_* to use the render_png
            if (render_local_mem(render, &rowbuf, (int(*)(void *arg, unsigned char *)) &render_png_row, png_ctx))
                ERROR("render_local_mem");

            break;

        case RENDER_RAW :
            // the render_raw stuff
            if (!(raw_ctx = render_raw_init(render)))
                ERROR("render_raw_init");

            //  set render_* to use the render_raw
            if (render_local_mem(render, &rowbuf, (int(*)(void *arg, unsigned char *)) &render_raw_row, raw_ctx))
                ERROR("render_local_mem");

            break;

        default :
            assert(0);
    }
  
    // then we can actually render
    t1 = clock();

    if (render_mandelbrot(render))
        ERROR("render_mandelbrot");

    t2 = clock();
    
    // calculate the time taken
    if (duration)
        *duration = ((double)(t2 - t1))/CLOCKS_PER_SEC;

    // success
    return 0;

error:
    free(rowbuf);

    if (png_ctx)
        render_png_abort(png_ctx);

    if (raw_ctx)
        render_raw_abort(raw_ctx);

    return -1;
}