| 1 | /* |
|---|
| 2 | * libpipi Proper image processing implementation library |
|---|
| 3 | * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org> |
|---|
| 4 | * All Rights Reserved |
|---|
| 5 | * |
|---|
| 6 | * $Id$ |
|---|
| 7 | * |
|---|
| 8 | * This library is free software. It comes without any warranty, to |
|---|
| 9 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 10 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 11 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | /* |
|---|
| 16 | * context.c: processing stack handling routines |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #include "config.h" |
|---|
| 20 | #include "common.h" |
|---|
| 21 | |
|---|
| 22 | #include <stdio.h> |
|---|
| 23 | #include <stdlib.h> |
|---|
| 24 | #include <stdarg.h> |
|---|
| 25 | #include <string.h> |
|---|
| 26 | |
|---|
| 27 | #include "pipi.h" |
|---|
| 28 | #include "pipi_internals.h" |
|---|
| 29 | |
|---|
| 30 | pipi_context_t *pipi_create_context() |
|---|
| 31 | { |
|---|
| 32 | pipi_context_t *ret; |
|---|
| 33 | |
|---|
| 34 | ret = malloc(sizeof(pipi_context_t)); |
|---|
| 35 | memset(ret, 0, sizeof(pipi_context_t)); |
|---|
| 36 | |
|---|
| 37 | return ret; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | void pipi_destroy_context(pipi_context_t *ctx) |
|---|
| 41 | { |
|---|
| 42 | free(ctx); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | int pipi_command(pipi_context_t *ctx, char const *cmd, ...) |
|---|
| 46 | { |
|---|
| 47 | if(!strcmp(cmd, "load")) |
|---|
| 48 | { |
|---|
| 49 | char const *file; |
|---|
| 50 | va_list ap; |
|---|
| 51 | |
|---|
| 52 | va_start(ap, cmd); |
|---|
| 53 | file = va_arg(ap, char const *); |
|---|
| 54 | va_end(ap); |
|---|
| 55 | ctx->images[ctx->nimages] = pipi_load(file); |
|---|
| 56 | if(ctx->images[ctx->nimages] == NULL) |
|---|
| 57 | return -1; |
|---|
| 58 | ctx->nimages++; |
|---|
| 59 | } |
|---|
| 60 | else if(!strcmp(cmd, "save")) |
|---|
| 61 | { |
|---|
| 62 | char const *file; |
|---|
| 63 | va_list ap; |
|---|
| 64 | |
|---|
| 65 | if(ctx->nimages <= 0) |
|---|
| 66 | return -1; |
|---|
| 67 | ctx->nimages--; |
|---|
| 68 | va_start(ap, cmd); |
|---|
| 69 | file = va_arg(ap, char const *); |
|---|
| 70 | va_end(ap); |
|---|
| 71 | pipi_save(ctx->images[ctx->nimages], file); |
|---|
| 72 | pipi_free(ctx->images[ctx->nimages]); |
|---|
| 73 | } |
|---|
| 74 | else if(!strcmp(cmd, "dither")) |
|---|
| 75 | { |
|---|
| 76 | pipi_image_t *src, *dst; |
|---|
| 77 | char const *method; |
|---|
| 78 | va_list ap; |
|---|
| 79 | |
|---|
| 80 | if(ctx->nimages <= 0) |
|---|
| 81 | return -1; |
|---|
| 82 | va_start(ap, cmd); |
|---|
| 83 | method = va_arg(ap, char const *); |
|---|
| 84 | va_end(ap); |
|---|
| 85 | src = ctx->images[ctx->nimages - 1]; |
|---|
| 86 | dst = NULL; |
|---|
| 87 | if(!strcmp(method, "fs")) |
|---|
| 88 | dst = pipi_dither_floydsteinberg(src, 0); |
|---|
| 89 | else if(!strcmp(method, "sfs")) |
|---|
| 90 | dst = pipi_dither_floydsteinberg(src, 1); |
|---|
| 91 | else if(!strcmp(method, "jajuni")) |
|---|
| 92 | dst = pipi_dither_jajuni(src, 0); |
|---|
| 93 | else if(!strcmp(method, "sjajuni")) |
|---|
| 94 | dst = pipi_dither_jajuni(src, 1); |
|---|
| 95 | else if(!strcmp(method, "ost")) |
|---|
| 96 | dst = pipi_dither_ostromoukhov(src, 0); |
|---|
| 97 | else if(!strcmp(method, "sost")) |
|---|
| 98 | dst = pipi_dither_ostromoukhov(src, 1); |
|---|
| 99 | else if(!strcmp(method, "ordered")) |
|---|
| 100 | { |
|---|
| 101 | if(ctx->nimages < 2) |
|---|
| 102 | return -1; |
|---|
| 103 | dst = pipi_dither_ordered(ctx->images[ctx->nimages - 2], src); |
|---|
| 104 | pipi_free(ctx->images[ctx->nimages - 2]); |
|---|
| 105 | ctx->nimages--; |
|---|
| 106 | } |
|---|
| 107 | else if(!strcmp(method, "random")) |
|---|
| 108 | dst = pipi_dither_random(src); |
|---|
| 109 | else if(!strcmp(method, "dbs")) |
|---|
| 110 | dst = pipi_dither_dbs(src); |
|---|
| 111 | if(dst == NULL) |
|---|
| 112 | return -1; |
|---|
| 113 | pipi_free(src); |
|---|
| 114 | ctx->images[ctx->nimages - 1] = dst; |
|---|
| 115 | } |
|---|
| 116 | else if(!strcmp(cmd, "blur")) |
|---|
| 117 | { |
|---|
| 118 | pipi_image_t *src, *dst; |
|---|
| 119 | char const *arg; |
|---|
| 120 | va_list ap; |
|---|
| 121 | |
|---|
| 122 | if(ctx->nimages <= 0) |
|---|
| 123 | return -1; |
|---|
| 124 | va_start(ap, cmd); |
|---|
| 125 | arg = va_arg(ap, char const *); |
|---|
| 126 | va_end(ap); |
|---|
| 127 | src = ctx->images[ctx->nimages - 1]; |
|---|
| 128 | dst = pipi_gaussian_blur(src, atof(arg)); |
|---|
| 129 | if(dst == NULL) |
|---|
| 130 | return -1; |
|---|
| 131 | pipi_free(src); |
|---|
| 132 | ctx->images[ctx->nimages - 1] = dst; |
|---|
| 133 | } |
|---|
| 134 | else if(!strcmp(cmd, "wrap")) |
|---|
| 135 | { |
|---|
| 136 | if(ctx->nimages <= 0) |
|---|
| 137 | return -1; |
|---|
| 138 | ctx->images[ctx->nimages - 1]->wrap = 1; |
|---|
| 139 | } |
|---|
| 140 | else if(!strcmp(cmd, "gray")) |
|---|
| 141 | { |
|---|
| 142 | if(ctx->nimages <= 0) |
|---|
| 143 | return -1; |
|---|
| 144 | pipi_getpixels(ctx->images[ctx->nimages - 1], PIPI_PIXELS_Y_F); |
|---|
| 145 | } |
|---|
| 146 | else if(!strcmp(cmd, "free")) |
|---|
| 147 | { |
|---|
| 148 | if(ctx->nimages <= 0) |
|---|
| 149 | return -1; |
|---|
| 150 | ctx->nimages--; |
|---|
| 151 | pipi_free(ctx->images[ctx->nimages]); |
|---|
| 152 | } |
|---|
| 153 | else if(!strcmp(cmd, "dup")) |
|---|
| 154 | { |
|---|
| 155 | if(ctx->nimages <= 0) |
|---|
| 156 | return -1; |
|---|
| 157 | ctx->images[ctx->nimages] = pipi_copy(ctx->images[ctx->nimages - 1]); |
|---|
| 158 | ctx->nimages++; |
|---|
| 159 | } |
|---|
| 160 | else |
|---|
| 161 | { |
|---|
| 162 | return -1; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | return 0; |
|---|
| 166 | } |
|---|
| 167 | |
|---|