/* * libpipi Proper image processing implementation library * Copyright (c) 2004-2008 Sam Hocevar * All Rights Reserved * * $Id$ * * This library is free software. It comes without any warranty, to * the extent permitted by applicable law. You can redistribute it * and/or modify it under the terms of the Do What The Fuck You Want * To Public License, Version 2, as published by Sam Hocevar. See * http://sam.zoy.org/wtfpl/COPYING for more details. */ /* * context.c: processing stack handling routines */ #include "config.h" #include "common.h" #include #include #include #include #include "pipi.h" #include "pipi_internals.h" pipi_context_t *pipi_create_context() { pipi_context_t *ret; ret = malloc(sizeof(pipi_context_t)); memset(ret, 0, sizeof(pipi_context_t)); return ret; } void pipi_destroy_context(pipi_context_t *ctx) { free(ctx); } int pipi_command(pipi_context_t *ctx, char const *cmd, ...) { if(!strcmp(cmd, "load")) { char const *file; va_list ap; va_start(ap, cmd); file = va_arg(ap, char const *); va_end(ap); ctx->images[ctx->nimages] = pipi_load(file); if(ctx->images[ctx->nimages] == NULL) return -1; ctx->nimages++; } else if(!strcmp(cmd, "save")) { char const *file; va_list ap; if(ctx->nimages <= 0) return -1; ctx->nimages--; va_start(ap, cmd); file = va_arg(ap, char const *); va_end(ap); pipi_save(ctx->images[ctx->nimages], file); pipi_free(ctx->images[ctx->nimages]); } else if(!strcmp(cmd, "dither")) { pipi_image_t *src, *dst; char const *method; va_list ap; if(ctx->nimages <= 0) return -1; va_start(ap, cmd); method = va_arg(ap, char const *); va_end(ap); src = ctx->images[ctx->nimages - 1]; dst = NULL; if(!strcmp(method, "fs")) dst = pipi_dither_floydsteinberg(src, 0); else if(!strcmp(method, "sfs")) dst = pipi_dither_floydsteinberg(src, 1); else if(!strcmp(method, "jajuni")) dst = pipi_dither_jajuni(src, 0); else if(!strcmp(method, "sjajuni")) dst = pipi_dither_jajuni(src, 1); else if(!strcmp(method, "ost")) dst = pipi_dither_ostromoukhov(src, 0); else if(!strcmp(method, "sost")) dst = pipi_dither_ostromoukhov(src, 1); else if(!strcmp(method, "ordered")) { if(ctx->nimages < 2) return -1; dst = pipi_dither_ordered(ctx->images[ctx->nimages - 2], src); pipi_free(ctx->images[ctx->nimages - 2]); ctx->nimages--; } else if(!strcmp(method, "random")) dst = pipi_dither_random(src); else if(!strcmp(method, "dbs")) dst = pipi_dither_dbs(src); if(dst == NULL) return -1; pipi_free(src); ctx->images[ctx->nimages - 1] = dst; } else if(!strcmp(cmd, "blur")) { pipi_image_t *src, *dst; char const *arg; va_list ap; if(ctx->nimages <= 0) return -1; va_start(ap, cmd); arg = va_arg(ap, char const *); va_end(ap); src = ctx->images[ctx->nimages - 1]; dst = pipi_gaussian_blur(src, atof(arg)); if(dst == NULL) return -1; pipi_free(src); ctx->images[ctx->nimages - 1] = dst; } else if(!strcmp(cmd, "mean")) { pipi_image_t *dst; if(ctx->nimages < 2) return -1; dst = pipi_mean(ctx->images[ctx->nimages - 2], ctx->images[ctx->nimages - 1]); if(dst == NULL) return -1; pipi_free(ctx->images[ctx->nimages - 2]); pipi_free(ctx->images[ctx->nimages - 1]); ctx->images[ctx->nimages - 2] = dst; ctx->nimages--; } else if(!strcmp(cmd, "min")) { pipi_image_t *dst; if(ctx->nimages < 2) return -1; dst = pipi_min(ctx->images[ctx->nimages - 2], ctx->images[ctx->nimages - 1]); if(dst == NULL) return -1; pipi_free(ctx->images[ctx->nimages - 2]); pipi_free(ctx->images[ctx->nimages - 1]); ctx->images[ctx->nimages - 2] = dst; ctx->nimages--; } else if(!strcmp(cmd, "max")) { pipi_image_t *dst; if(ctx->nimages < 2) return -1; dst = pipi_max(ctx->images[ctx->nimages - 2], ctx->images[ctx->nimages - 1]); if(dst == NULL) return -1; pipi_free(ctx->images[ctx->nimages - 2]); pipi_free(ctx->images[ctx->nimages - 1]); ctx->images[ctx->nimages - 2] = dst; ctx->nimages--; } else if(!strcmp(cmd, "wrap")) { if(ctx->nimages <= 0) return -1; ctx->images[ctx->nimages - 1]->wrap = 1; } else if(!strcmp(cmd, "autocontrast")) { pipi_image_t *tmp; if(ctx->nimages <= 0) return -1; tmp = ctx->images[ctx->nimages - 1]; ctx->images[ctx->nimages - 1] = pipi_autocontrast(tmp); pipi_free(tmp); } else if(!strcmp(cmd, "gray")) { if(ctx->nimages <= 0) return -1; pipi_getpixels(ctx->images[ctx->nimages - 1], PIPI_PIXELS_Y_F); } else if(!strcmp(cmd, "free")) { if(ctx->nimages <= 0) return -1; ctx->nimages--; pipi_free(ctx->images[ctx->nimages]); } else if(!strcmp(cmd, "dup")) { if(ctx->nimages <= 0) return -1; ctx->images[ctx->nimages] = pipi_copy(ctx->images[ctx->nimages - 1]); ctx->nimages++; } else { return -1; } return 0; }