/* qbism.c * * Copyright (C) 2005 by Andy Goth . * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 59 Temple * Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include "qbism.h" /* You're not going to believe this. */ void qbism_render(algo_t* algo, int width, int height, FILE* out) { int y, x; /* Coordinates. */ int step; /* Current transformation step. */ int i; /* General-purpose iterator. */ reg_t* reg; /* Registers. */ float* sr, *cr, *dr; /* Source, control, dest register. */ float sum; /* Temporary variable: sum. */ float dot_product; /* Temporary variable: dot product. */ /* Allocate the registers. */ reg = xmalloc(sizeof(*reg) * algo->num_regs); /* Output MIFF header. */ fprintf(out, "id=ImageMagick\n"); fprintf(out, "columns=%d\n", width); fprintf(out, "rows=%d\n", height); fprintf(out, "depth=16\n"); fprintf(out, "\f\n:\x01a"); /* Render image. */ for (y = 0; y < height; ++y) { for (x = 0; x < width; ++x) { /* Initialize registers for this pixel. */ for (i = 0; i < algo->num_regs; ++i) { reg[i][0] = x / (float)width; reg[i][1] = y / (float)height; reg[i][2] = i / (float)algo->num_regs; } /* Apply transformations. */ for (step = 0; step < algo->num_steps; ++step) { sr = ®[algo->seq[step].source][0]; cr = ®[algo->seq[step].control][0]; dr = ®[algo->seq[step].dest][0]; /* Update the registers for this pixel. */ switch (algo->seq[step].opcode) { case XOP_PROJECTION: dot_product = 0; for (i = 0; i < 3; ++i) { dot_product += sr[i] * cr[i]; } for (i = 0; i < 3; ++i) { dr[i] = dot_product * sr[i]; } break; case XOP_SHIFT: for (i = 0; i < 3; ++i) { dr[i] = sr[i] + cr[i]; if (dr[i] >= 1.0) { dr[i] -= 1.0; } } break; case XOP_SHIFTBACK: for (i = 0; i < 3; ++i) { dr[i] = sr[i] - cr[i]; if (dr[i] <= 0.0) { dr[i] += 1.0; } } break; case XOP_ROTATE: for (i = 0; i < 3; ++i) { dr[i] = sr[(i + 1) % 3]; } break; case XOP_ROTATE2: for (i = 0; i < 3; ++i) { dr[i] = sr[(i + 2) % 3]; } break; case XOP_MULTIPLY: for (i = 0; i < 3; ++i) { dr[i] = sr[i] * cr[i]; } break; case XOP_SINE: for (i = 0; i < 3; ++i) { dr[i] = (sin(20 * sr[i] * cr[i]) + 1) / 2; } break; case XOP_CONDITIONAL: sum = 0.0; for (i = 0; i < 3; ++i) { sum += cr[i]; } if (sum > 0.5) { for (i = 0; i < 3; ++i) { dr[i] = sr[i]; } } else { for (i = 0; i < 3; ++i) { dr[i] = cr[i]; } } break; case XOP_COMPLEMENT: for (i = 0; i < 3; ++i) { dr[i] = 1.0 - sr[i]; } break; } } /* Calculate and output pixel. */ for (i = 0; i < 3; ++i) { fputc((int)(reg[0][i] * 65535 + 0.5) >> 8, out); fputc((int)(reg[0][i] * 65535 + 0.5) >> 0, out); } } } /* Done with reg; free it. */ free(reg); } /* vim: set ts=4 sts=4 sw=4 tw=80 et: */